-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathkpgmld.c
366 lines (289 loc) · 9.06 KB
/
kpgmld.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* kpgmld.c - program load
*
* Copyright (C) 2001 Lineo, Inc.
* 2013-2019 The EmuTOS development team
*
* Authors:
* SCC Steven C. Cavender
* MAD Martin Doering
*
* This file is distributed under the GPL, version 2 or at your
* option any later version. See doc/license.txt for details.
*/
/* #define ENABLE_KDEBUG */
#include "config.h"
#include "portab.h"
#include "fs.h"
#include "proc.h"
#include "gemerror.h"
#include "pghdr.h"
#include "string.h"
#include "kprint.h"
/*
* forward prototypes
*/
static LONG pgmld01(FH h, PD *pdptr, PGMHDR01 *hd);
static LONG pgfix01(void *lastcp, LONG nrelbytes, PGMINFO *pi);
/*
* kpgmhdrld - load program header
*
* contrary to what was before, we load the prg header first,
* then allocate the basepage, choosing the memory pool according to
* the flags and the amount of memory needed. Then, we actually load
* the program.
*/
LONG kpgmhdrld(char *s, PGMHDR01 *hd, FH *h)
{
LONG r;
WORD magic;
r = xopen(s, 0); /* open file for read */
if (r < 0L)
return r;
*h = (FH) r ; /* get file handle */
r = xread(*h, 2L, &magic); /* read magic number */
if (r < 0L)
return r;
if (r != 2)
return EPLFMT;
/* alternate executable formats will not be handled */
if (magic != 0x601a)
{
KDEBUG(("BDOS xpgmld: Unknown executable format!\n"));
return EPLFMT;
}
/* read in the program header */
r = xread(*h, (LONG)sizeof(PGMHDR01), hd);
if (r < 0L)
return r;
if (r != (LONG)sizeof(PGMHDR01))
return EPLFMT;
return 0;
}
/*
* kpgmld - load program except the header (which has already been read)
*
* The program space follows PD
*
* Arguments:
* p - ptr to PD
* h - file handle opened by kpgmhdrld
* hd - the program header read by kpgmhdrld
*/
LONG kpgmld(PD *p, FH h, PGMHDR01 *hd)
{
LONG r;
r = pgmld01(h, p, hd);
KDEBUG(("BDOS pgmld01: return code=0x%lx\n",r));
xclose(h);
return r;
}
/*
* pgmld01 - oldest known gemdos load format
* It is very similar to cp/m 68k load in the (open) program file with
* handle 'h' using load file strategy like cp/m 68k. Specifically:
*
* - read in program header and determine format parameters
* - seek past the symbol table to the start of the relo info
* - read in the first offset (it's different than the rest in that
* it is a longword instead of a byte).
* - make the first adjustment until we run out of relocation info or
* we have an error
* - read in relocation info into the bss area
* - call pgfix01() to fix up the code using that info
* - zero out the bss
*/
static LONG pgmld01(FH h, PD *pdptr, PGMHDR01 *hd)
{
PGMINFO *pi;
PD *p;
PGMINFO pinfo;
char *cp;
LONG relst;
LONG flen;
LONG r;
pi = &pinfo;
p = pdptr;
relst = 0;
/* calculate program load info */
pi->pi_tlen=hd->h01_tlen;
pi->pi_dlen=hd->h01_dlen;
flen = pi->pi_tlen + pi->pi_dlen;
pi->pi_blen = hd->h01_blen;
pi->pi_slen = hd->h01_slen;
pi->pi_tpalen = p->p_hitpa - p->p_lowtpa - sizeof(PD);
pi->pi_tbase = (char *) (p+1); /* 1st byte after PD */
pi->pi_bbase = pi->pi_tbase + flen;
pi->pi_dbase = pi->pi_tbase + pi->pi_tlen;
/*
* see if there is enough room to load in the file, then see if
* the requested bss space is larger than the space we have to offer
*/
if ((flen > pi->pi_tpalen) || (pi->pi_tpalen-flen < pi->pi_blen))
return ENSMEM;
/* initialize PD fields */
memcpy(&p->p_tbase, &pi->pi_tbase, 6 * sizeof(long));
/*
* read in the program file (text and data)
* if there is an error reading in the file or if it is an abs
* file, then we are finished
*/
r = xread(h,flen,pi->pi_tbase);
if (r < 0)
return r;
if (hd->h01_abs)
return 0; /* do we need to clr bss here? */
/*
* if not an absolute format, position past the symbols and start
* the reloc pointer (flen is tlen + dlen).
*/
/********** should change hard coded 0x1c ******************/
KDEBUG(("BDOS pgmld01: flen=0x%lx, pi_slen=0x%lx\n",flen,pi->pi_slen));
r = xlseek(flen+pi->pi_slen+0x1c,h,0);
if (r < 0L)
return r;
r = xread(h,(long)sizeof(relst),&relst);
KDEBUG(("BDOS pgmld01: relst=0x%lx\n",relst));
if (r < 0L)
return r;
if (relst != 0)
{
cp = pi->pi_tbase + relst;
/* make sure we didn't wrap memory or overrun the bss */
if ((cp < pi->pi_tbase) || (cp >= pi->pi_bbase))
return EPLFMT;
*((long *)(cp)) += (long)pi->pi_tbase ; /* 1st fixup */
flen = (long)p->p_hitpa - (long)pi->pi_bbase; /* M01.01.0925.01 */
for ( ; ; )
{
/* read in more relocation info */
r = xread(h,flen,pi->pi_bbase);
if (r <= 0)
break;
/* do fixups using that info */
r = pgfix01(cp, r, pi);
if (r <= 0)
break;
}
if (r < 0) /* M01.01.1023.01 */
return r;
}
/* clear the bss or the whole heap */
if (hd->h01_flags & PF_FASTLOAD)
{
flen = pi->pi_blen; /* clear only the bss */
}
else
{
flen = (long)p->p_hitpa - (long)pi->pi_bbase; /* clear the whole heap */
}
if (flen > 0)
bzero(pi->pi_bbase, flen);
return 0;
}
/*
* pgfix01 - do the next set of fixups
*
* returns:
* addr of last modified longword in code segment (cp)
* 0 if error or done
* stat01:
* >0: all offsets in bss used up, read in more
* =0: offset of 0 encountered, no more fixups
* <0: EPLFMT (load file format error)
*
* Arguments:
* nrelbytes - number of avail rel values
* pi - program info pointer
*/
static LONG pgfix01(void *lastcp, LONG nrelbytes, PGMINFO *pi)
{
UBYTE *cp; /* code pointer */
UBYTE *rp; /* relocation info pointer */
LONG n; /* nbr of relocation bytes */
UBYTE *bbase; /* base addr of bss segment */
LONG tbase; /* base addr of text segment */
cp = lastcp;
rp = (UBYTE *)pi->pi_bbase;
n = nrelbytes;
tbase = (LONG)pi->pi_tbase;
bbase = (UBYTE *)pi->pi_bbase;
while(n-- && (*rp != 0))
{
if (*rp == 1)
cp += 0xfe;
else
{
cp += *rp; /* add the byte at rp to cp, don't sign ext */
if (cp >= bbase)
return EPLFMT;
*((long *)cp) += tbase;
}
++rp;
}
return (++n == 0) ? 1 : 0;
}
#if DETECT_NATIVE_FEATURES
LONG kpgm_relocate(PD *p, long length)
{
char *cp;
LONG *rp;
LONG flen;
PGMINFO pinfo;
PGMINFO *pi;
PGMHDR01 *hd;
UWORD abs_flag;
KDEBUG(("BDOS kpgm_relocate: lotpa=%p hitpa=%p len=0x%lx\n",p->p_lowtpa,p->p_hitpa,length));
hd = (PGMHDR01*)(((char*)(p+1)) + 2);
pi = &pinfo;
abs_flag = hd->h01_abs;
pi->pi_tlen=hd->h01_tlen;
pi->pi_dlen=hd->h01_dlen;
flen = pi->pi_tlen + pi->pi_dlen;
pi->pi_blen = hd->h01_blen;
pi->pi_slen = hd->h01_slen;
pi->pi_tpalen = p->p_hitpa - p->p_lowtpa - sizeof(PD);
pi->pi_tbase = (char *)(p+1); /* 1st byte after PD */
pi->pi_bbase = pi->pi_tbase + flen;
pi->pi_dbase = pi->pi_tbase + pi->pi_tlen;
/* move the code to the right position (after the PD - basepage) */
memmove(p+1, (char*)(p+1) +2+sizeof(PGMHDR01), flen);
KDEBUG(("BDOS kpgm_relocate: tlen=0x%lx, dlen=0x%lx, slen=0x%lx\n",
pi->pi_tlen,pi->pi_dlen,pi->pi_slen));
KDEBUG(("BDOS kpgm_relocate: flen=0x%lx, tpalen=0x%lx\n",flen,pi->pi_tpalen));
/*
* see if there is enough room to load in the file, then see if
* the requested bss space is larger than the space we have to offer
*/
if ((flen > pi->pi_tpalen) || (pi->pi_tpalen-flen < pi->pi_blen))
{
KDEBUG(("BDOS kpgm_relocate: ENSMEM\n"));
return ENSMEM;
}
KDEBUG(("BDOS kpgm_relocate: abs=0x%x\n",abs_flag));
/* initialize PD fields */
memcpy(&p->p_tbase, &pi->pi_tbase, 6*sizeof(long));
if (abs_flag)
return 0;
/* relocation information present */
rp = (LONG*) (pi->pi_tbase+2+sizeof(PGMHDR01)+flen+pi->pi_slen);
if (*rp)
{
cp = pi->pi_tbase + *rp++;
/* make sure we didn't wrap memory or overrun the bss */
if ((cp < pi->pi_tbase) || (cp >= pi->pi_bbase))
return EPLFMT;
*((long *)(cp)) += (long)pi->pi_tbase; /* 1st fixup */
/* move the relocation info to the pi_bbase */
length -= ((long)rp) - (long)pi->pi_tbase;
memmove(pi->pi_bbase, rp, length);
/* fixup with the reloc information available */
pgfix01(cp, length, pi);
}
/* clear the whole heap */
flen = (long)p->p_hitpa - (long)pi->pi_bbase;
if (flen > 0)
bzero(pi->pi_bbase, flen);
return 0;
}
#endif