-
Notifications
You must be signed in to change notification settings - Fork 9
/
fix_checksum.c
129 lines (103 loc) · 3.76 KB
/
fix_checksum.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
/*************************************************************************
* fix_checksum.c - Fix BIOS extension ROM checksum
*
* Copyright (C) 2011 - 2021 Sergey Kiselev.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
struct stat *st;
char *rom_buf;
int file_size, rom_size, area, area_start, area_end, area_sum, i, in, out;
unsigned char checksum;
if (argc < 6 || argc % 3 != 0) {
fprintf(stderr, "Usage: %s <input_file> <output_file>\n",argv[0]);
fprintf(stderr, " <area1_start> <area1_end> <area1_sumaddr>\n");
fprintf(stderr, " [<area2_start> <area2_end> <area2_sumaddr>] ..\n\n");
fprintf(stderr, "<input_file> - File name of the input image file.\n");
fprintf(stderr, "<output_file> - File name of the output image file.\n");
fprintf(stderr, "<areaX_start> - Hexadecimal start address of a ROM area X\n");
fprintf(stderr, "<areaX_end> - Hexadecimal end address of a ROM area X\n");
fprintf(stderr, "<areaX_sum> - Hexadecimal address of the byte to be changed to\n");
fprintf(stderr, " correct the checksum for area X.\n");
exit(1);
}
st = malloc(sizeof(struct stat));
if (stat(argv[1], st) == -1) {
fprintf(stderr, "Failed to stat '%s'\n", argv[1]);
exit(2);
}
file_size = st->st_size;
printf("DEBUG: ROM file size is %d\n", file_size);
if (file_size < 5) {
fprintf(stderr, "File is too short\n");
exit(2);
}
rom_buf = malloc(file_size);
if ((in = open(argv[1], O_RDONLY)) == -1) {
fprintf(stderr, "Failed to open '%s'\n", argv[1]);
exit(2);
}
if (read(in, rom_buf, file_size) != file_size) {
fprintf(stderr, "Short read\n");
exit(2);
}
close(in);
rom_size = rom_buf[2] * 512;
printf("DEBUG: ROM code size is %d\n", rom_size);
if (rom_size > file_size) {
fprintf(stderr, "ROM code size is bigger than ROM file size\n");
exit(2);
}
for (area = 1; area < argc / 3; area++) {
sscanf(argv[area*3], "%x", &area_start);
sscanf(argv[area*3+1], "%x", &area_end);
sscanf(argv[area*3+2], "%x", &area_sum);
if (area_start > area_end) {
fprintf(stderr, "Area %d start address %04X is bigger than end address %04X\n", area, area_start, area_end);
exit(1);
}
if (area_sum < area_start || area_sum > area_end) {
fprintf(stderr, "Area %d checksum address %04X is not within the area %04X - %04X\n", area, area_sum, area_start, area_end);
exit(1);
}
checksum = 0;
for (i = area_start; i <= area_end; i++) {
if (i != area_sum) checksum += rom_buf[i];
}
printf("DEBUG: Area %d: Original checksum: 0x%02X\n", area, checksum);
rom_buf[area_sum] = -checksum;
checksum = 0;
for (i = area_start; i <= area_end; i++) checksum += rom_buf[i];
printf("DEBUG: Area %d: Fixed checksum: 0x%02X\n", area, checksum);
}
if ((out = creat(argv[2], 0777)) == -1) {
fprintf(stderr, "Failed to open '%s'\n", argv[2]);
exit(3);
}
if (write(out, rom_buf, file_size) != file_size) {
fprintf(stderr, "Short write\n");
exit(3);
}
close(out);
return 0;
}