fakerom.c
14.3 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* fakerom.c: routines to read .text section of a coff file into a rom file
* for subsequent download to the ultra64 development system.
*
* Cloned from an OS conversion utility suite
*
* Copyright 1995, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*
* $Revision: 1.2 $
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef __sgi__
#include "sex.h"
#include "sym.h"
#include "cmplrs/stsupport.h"
#include "filehdr.h"
#include "ldfcn.h"
#include "scnhdr.h"
#else
#include "sym.h"
#include "ecoff.h"
#include "mips.h"
#include <sys/stat.h>
#endif
#include "errno.h"
#include "ramrom.h"
unsigned int Address; /* Address being Dumped. */
unsigned int Data0; /* Data being Dumped. */
unsigned int Data1; /* Data being Dumped. */
int Swap = 0; /* Swap Text Words. */
char *OFileName; /* Object File To Dump. */
char *AFileName; /* ASCII File To Dump into. */
char *SName; /* Section to Dump. */
#ifdef __sgi__
LDFILE *LDPtr; /* File's ld Pointer. */
SCNHDR SHeader; /* Section Header. */
FILE *AFile; /* ASCII File. */
#endif
extern int Extract(unsigned int *buf);
extern int readCoff(char *fname, unsigned int *buf);
extern void usage(void);
static void getBootFile(char *bootFileName);
static void getPif2BootFile(char *pif2bootFileName);
static void getRomheaderFile(char *headerFileName);
static char *gloadFindFile(char *fullpath, char *postRootSuffix, char *fname);
char *progName;
unsigned char *bootBuf, *headerBuf, *pif2bootBuf;
int bootWordAlignedByteSize, pif2bootWordAlignedByteSize,
headerWordAlignedByteSize;
void
usage(void)
{
fprintf(stderr,
"usage: fakerom <bootstrap coff executable> <main program coff executable>\n");
exit(1);
}
main(int argc, char *argv[])
{
char *coffFileName;
char *bootFileName;
int coffFd;
unsigned char *coffBuf;
int coffTextSize, i;
char romFileName[256];
FILE *romFile;
char errMessage[256];
struct stat buf;
unsigned char zerobyte = 0;
if ( (argc != 3) ||
((bootFileName = argv[1]) == NULL) || (strlen(bootFileName) == 0) ||
((coffFileName = argv[2]) == NULL) || (strlen(coffFileName) == 0) ) {
usage();
}
progName = argv[0];
/*
* Create the romFileName from the coffFileName by tacking on a
* .rom suffix.
*/
strcpy(romFileName, coffFileName);
strcat(romFileName, ".rom");
if ((coffFd = open(coffFileName, O_RDONLY | O_NOCTTY)) < 0) {
sprintf(errMessage,
"%s: unable to open %s", argv[0], coffFileName);
perror(errMessage);
exit(1);
}
/*
* Find out how big the bootstrap executable is, then malloc a buffer
* to hold the contents (which we will Extract() into).
*/
if (fstat(coffFd, &buf) < 0) {
sprintf(errMessage, "%s unable to stat %s", argv[0], coffFileName);
perror(errMessage);
close(coffFd);
exit(1);
}
coffBuf = (unsigned char *)malloc(buf.st_size);
if (coffBuf == NULL) {
fprintf(stderr, "%s: unable to malloc buffer to hold %d bytes\n",
argv[0], buf.st_size);
close(coffFd);
exit(1);
}
close(coffFd);
coffTextSize = readCoff(coffFileName, (unsigned int *)coffBuf);
/*
* Now open the raw rom file, and copy the .text data from the malloc'd
* buffer into the raw rom file.
*/
if ((romFile = fopen(romFileName, "w")) == NULL) {
sprintf(errMessage,
"%s: unable to open %s", argv[0], romFileName);
perror(errMessage);
free(coffBuf);
exit(1);
}
/*
* Load the bootstrap, pif2bootstrap, and romheader files into buffers,
* then write these buffers out to the rom image file.
*/
getBootFile(bootFileName);
getPif2BootFile("./pif2DiagBoot");
getRomheaderFile("./diag.romheader");
if (fwrite(headerBuf, sizeof(char), headerWordAlignedByteSize, romFile) !=
headerWordAlignedByteSize) {
fprintf(stderr, "makerom: %s: write error\n", romFile);
return(-1);
}
if (fseek(romFile, RAMROM_BOOTSTRAP_OFFSET, SEEK_SET) != 0) {
fprintf(stderr, "fakerom: %s: fseek error (%s)\n",
romFile, sys_errlist[errno]);
return(-1);
}
if (fwrite(bootBuf, sizeof(char), bootWordAlignedByteSize, romFile) !=
bootWordAlignedByteSize) {
fprintf(stderr, "fakerom: %s: write error\n", romFile);
return(-1);
}
if (fseek(romFile, RAMROM_PIF2BOOTSTRAP_OFFSET, SEEK_SET) != 0) {
fprintf(stderr, "fakerom: %s: fseek error (%s)\n",
romFile, sys_errlist[errno]);
return(-1);
}
if (fwrite(pif2bootBuf, sizeof(char), pif2bootWordAlignedByteSize, romFile)
!= pif2bootWordAlignedByteSize) {
fprintf(stderr, "fakerom: %s: write error\n", romFile);
return(-1);
}
if (fseek(romFile, RAMROM_GAME_OFFSET, SEEK_SET) != 0) {
fprintf(stderr, "fakerom: %s: fseek error (%s)\n",
romFile, sys_errlist[errno]);
return(-1);
}
/*
* Now write the tinymon coff data into the file.
*/
if ( fwrite((const void *)coffBuf, (size_t)sizeof(char),
(size_t)coffTextSize, romFile) != coffTextSize ) {
sprintf(errMessage,
"%s: short write to %s", argv[0], romFileName);
perror(errMessage);
fclose(romFile);
free(coffBuf);
exit(1);
}
fclose(romFile);
free(coffBuf);
if (bootBuf)
free(bootBuf);
if (pif2bootBuf)
free(pif2bootBuf);
if (headerBuf)
free(headerBuf);
exit(0);
}
static void
getBootFile(char *bootFileName)
{
int bootFd;
char scratchFileName[256];
struct stat buf;
char errMessage[256];
/*
* Load the bootstrap file (provide a default if none was supplied)
*/
if (!bootFileName) {
if ( gloadFindFile(scratchFileName, "/usr/lib/PR", "Boot") ) {
bootFileName = scratchFileName;
}
}
if (bootFileName) {
if ((bootFd = open(bootFileName, O_RDONLY | O_NOCTTY)) < 0) {
sprintf(errMessage, "%s: unable to open %s", progName, bootFileName);
perror(errMessage);
exit(1);
}
/*
* Load the bootstrap file.
*/
if (fstat(bootFd, &buf) < 0) {
sprintf(errMessage, "%s unable to stat %s", progName, bootFileName);
perror(errMessage);
close(bootFd);
exit(1);
}
bootBuf = (unsigned char *)malloc(buf.st_size);
if (bootBuf == NULL) {
fprintf(stderr, "%s: unable to malloc buffer to hold %d bytes\n",
progName, buf.st_size);
close(bootFd);
exit(1);
}
close(bootFd);
bootWordAlignedByteSize = readCoff(bootFileName, (unsigned int *)bootBuf);
} else {
bootBuf = NULL;
}
}
static void
getPif2BootFile(char *pif2bootFileName)
{
int pif2bootFd;
char scratchFileName[256];
struct stat buf;
char errMessage[256];
/*
* Load the pif2bootstrap file (provide a default if none was supplied)
*/
if (!pif2bootFileName) {
if ( gloadFindFile(scratchFileName, "/usr/diags/ucode",
"pif2DiagBoot") ) {
pif2bootFileName = scratchFileName;
}
}
if (pif2bootFileName) {
if ((pif2bootFd = open(pif2bootFileName, O_RDONLY | O_NOCTTY)) < 0) {
sprintf(errMessage, "%s: unable to open %s", progName, pif2bootFileName);
perror(errMessage);
exit(1);
}
/*
* Load the pif2bootstrap file.
*/
if (fstat(pif2bootFd, &buf) < 0) {
sprintf(errMessage, "%s unable to stat %s", progName, pif2bootFileName);
perror(errMessage);
close(pif2bootFd);
exit(1);
}
pif2bootBuf = (unsigned char *)malloc(buf.st_size);
if (pif2bootBuf == NULL) {
fprintf(stderr, "%s: unable to malloc buffer to hold %d bytes\n",
progName, buf.st_size);
close(pif2bootFd);
exit(1);
}
close(pif2bootFd);
pif2bootWordAlignedByteSize = readCoff(pif2bootFileName,
(unsigned int *)pif2bootBuf);
} else {
pif2bootBuf = NULL;
}
}
static void
getRomheaderFile(char *headerFileName)
{
int headerFd;
char scratchFileName[256];
struct stat buf;
char errMessage[256];
char nibbleString[2];
int nibbleVal;
int i, readPtr, retval;
/*
* Load the romheader file.
* It's in ascii format, so we convert each character to hexadecimal and
* prepare words of data to be loaded into memory.
*/
if (headerFileName) {
if ((headerFd = open(headerFileName, O_RDONLY | O_NOCTTY)) < 0) {
sprintf(errMessage, "%s unable to open %s", progName, headerFileName);
perror(errMessage);
exit(1);
}
/*
* Load the romheader file
*/
if (fstat(headerFd, &buf) < 0) {
sprintf(errMessage, "%s unable to stat %s", progName, headerFileName);
perror(errMessage);
close(headerFd);
exit(1);
}
headerWordAlignedByteSize = buf.st_size;
/*
* Since each byte corresponds to an ascii character describing a
* nibble, this is twice as big as we need.
*/
headerBuf = (unsigned char *)malloc(buf.st_size);
if (headerBuf == NULL) {
fprintf(stderr, "%s: unable to malloc buffer to hold %d bytes\n",
progName, buf.st_size);
close(headerFd);
exit(1);
}
nibbleString[1] = '\0';
for (i = 0, readPtr = 0; readPtr < buf.st_size; i++, readPtr++) {
retval = read(headerFd, ( (void *)(&(nibbleString[0])) ), 1);
if (retval != 1) {
fprintf(stderr, "%s: short read from %s.\n",
progName, headerFileName);
free(headerBuf);
close(headerFd);
exit(1);
}
/*
* Eat line feeds at end of line.
*/
if ( (nibbleString[0] == '\n') &&
(++readPtr < buf.st_size) ) {
retval = read(headerFd, ( (void *)(&(nibbleString[0])) ), 1);
if (retval != 1) {
fprintf(stderr, "%s: short read from %s.\n",
progName, headerFileName);
free(headerBuf);
close(headerFd);
exit(1);
}
}
/*
* Do a hex to int conversion on each nibble as they come in.
*/
nibbleVal = strtol(nibbleString, NULL, 16);
if (i % 2) {
headerBuf[ (i >> 1) ] |= nibbleVal;
} else {
headerBuf[ (i >> 1) ] = (nibbleVal << 4);
}
#ifdef DEBUG
printf("headerBuf[%d] now = 0x%x, char read was 0x%x\n",
(i >> 1), headerBuf[ (i >> 1) ], nibbleString[0]);
#endif
}
close(headerFd);
} else {
headerBuf = NULL;
}
}
static char *
gloadFindFile(char *fullpath, char *postRootSuffix, char *fname)
{
char *rootname, *rootpath;
int fd;
int try;
for (try = 0; try <= 2; try++) {
fullpath[0] = '\0';
switch (try) {
case 0: rootname = "ROOT"; break;
case 1: rootname = "WORKAREA"; break;
case 2: rootname = NULL; break;
}
if (rootname != NULL) {
if ((rootpath = (char *)getenv(rootname)) == NULL)
continue;
strcat(fullpath, rootpath);
}
if (postRootSuffix) {
strcat(fullpath, postRootSuffix);
strcat(fullpath, "/");
}
strcat(fullpath, fname);
if (!access(fullpath, R_OK))
return(fullpath);
}
fprintf(stderr, "gloadFindFile: can't find file %s\n", fullpath);
fullpath[0] = '\0';
return(NULL);
}
/*
* Read a COFF file into a buffer. Return number of bytes read,
* -1 if error.
*/
int
readCoff(char *fname, unsigned int *buf)
{
int textSize, dataSize, bssSize;
OFileName = fname;
SName = ".text"; /* set section name = .text */
textSize = Extract(buf);
if (textSize < 0)
return(-1);
return( textSize );
}
int
Extract(unsigned int *buf)
{
int bytesRead;
#ifdef __sgi__
if ((LDPtr = (LDFILE *) ldopen (OFileName, LDPtr)) == NULL)
{ /* Open File. */
fprintf (stderr, "Extract(): Cannot open %s.\n", OFileName);
return (-1);
}
switch (HEADER (LDPtr).f_magic)
{
case MIPSEBMAGIC:
case MIPSEBMAGIC_2:
case MIPSEBMAGIC_3:
Swap = (gethostsex () == LITTLEENDIAN);
break;
case MIPSELMAGIC:
case MIPSELMAGIC_2:
case MIPSELMAGIC_3:
Swap = (gethostsex () == BIGENDIAN);
break;
}
if (ldnshread (LDPtr, SName, &SHeader) == FAILURE)
{
fprintf (stderr, "Warning: %s has no %s section.\n", OFileName, SName);
} else {
FSEEK (LDPtr, SHeader.s_scnptr, BEGINNING);
for (Address = SHeader.s_paddr;
(Address - SHeader.s_paddr) < SHeader.s_size; Address += 8) {
FREAD ((char *)&Data0, 1, 4, LDPtr);
if (Swap) Data0 = swap_word (Data0);
FREAD ((char *)&Data1, 1, 4, LDPtr);
if (Swap) Data1 = swap_word (Data1);
if (Swap) {
*buf = Data1;
*(buf+1) = Data0;
} else {
*buf = Data0;
*(buf+1) = Data1;
}
buf += 2;
}
}
ldclose (LDPtr); /* Close File. */
return(SHeader.s_size); /* # of bytes read */
#else
FILE *fp;
FILHDR hdr;
SCNHDR scn;
int n, i;
unsigned short magic;
if ((fp = fopen(OFileName, "r")) == NULL) {
fprintf(stderr, "Extract(): Cannot open %s.\n", OFileName);
return -1;
}
fread(&hdr, sizeof hdr, 1, fp);
magic = hdr.f_magic[1] | hdr.f_magic[0] << 8;
switch (magic) {
case MIPS_MAGIC_BIG:
case MIPS_MAGIC_BIG2:
case MIPS_MAGIC_BIG3:
#if 0
Swap = (gethostsex () == LITTLEENDIAN);
#endif
break;
case MIPS_MAGIC_LITTLE:
case MIPS_MAGIC_LITTLE2:
case MIPS_MAGIC_LITTLE3:
#if 0
Swap = (gethostsex () == BIGENDIAN);
#endif
break;
}
/* skip aout header */
fseek(fp, sizeof(AOUTHDR), SEEK_CUR);
n = ntohs(hdr.f_nscns[0] | hdr.f_nscns[1] << 8);
for(i = 0; i < n; i++) {
fread(&scn, sizeof scn, 1, fp);
if (strcmp(scn.s_name, SName) == 0) {
fseek(fp, ntohl(scn.s_scnptr[0] | scn.s_scnptr[1] << 8 | scn.s_scnptr[2] << 16 | scn.s_scnptr[3] << 24), SEEK_SET);
bytesRead = fread(buf, 1, ntohl(scn.s_size[0] | scn.s_size[1] << 8 | scn.s_size[2] << 16 | scn.s_size[3] << 24), fp);
break;
}
}
fclose(fp);
return bytesRead;
#endif
}