-
Notifications
You must be signed in to change notification settings - Fork 0
/
romfixup.c
95 lines (75 loc) · 2.06 KB
/
romfixup.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
/* Very simple OpROM fixup utility by John "Lameguy" Wilbert Villamor
* 2022 Meido-Tek Productions
*
* This is a very simple utility that pads a Option ROM and calculates the
* appropriate checksum byte to prepare it as a valid ROM image.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE *fp;
const char *infile;
unsigned char *rombuff;
unsigned char csum;
long i;
long fsize,romsize;
/* usage text */
if( argc <= 1 )
{
printf("Usage:\n");
printf(" romfixup <file>\n");
return 0;
}
infile = argv[1];
/* open file */
fp = fopen(infile, "rb");
if( !fp )
{
printf("I cannot open %s!\n", infile);
return 1;
}
/* get file size */
fseek(fp, 0, SEEK_END);
fsize = ftell(fp);
if( fsize > 32768 )
{
fclose(fp);
printf("ERROR: ROM image exceeds 32K!\n");
return 1;
}
/* get size of ROM from the generic BIOS header */
i = 0;
fseek(fp, 2, SEEK_SET);
fread(&i, 1, 1, fp);
romsize = i<<9;
printf("ROM size defined by header: %d bytes\n", romsize);
if( fsize > romsize )
{
fclose(fp);
printf("ERROR: ROM image is larger than size defined in header!\n");
return 1;
}
/* allocate and fill buffer with FFs */
rombuff = (unsigned char*)malloc(romsize);
memset(rombuff, 0xff, romsize);
/* load the ROM image */
fseek(fp, 0, SEEK_SET);
fread(rombuff, 1, fsize, fp);
fclose(fp);
/* checksum the ROM image minus 1 byte */
csum = 0;
for(i=0; i<romsize-1; i++)
csum += rombuff[i];
/* apply two's complement to the result */
rombuff[i] = 256-csum;
printf("Computed checksum: %x\n", rombuff[i]);
printf("Test result: %x\n", ((unsigned char)csum+rombuff[i])%0x100);
/* Write the fixed ROM */
fp = fopen(infile, "wb");
fwrite(rombuff, 1, romsize, fp);
fclose(fp);
free(rombuff);
return 0;
} /* main */