1 | /* biossums.c --- written by Eike W. for the Bochs BIOS */
|
---|
2 | /* adapted for the LGPL'd VGABIOS by vruppert */
|
---|
3 |
|
---|
4 | /* This library is free software; you can redistribute it and/or
|
---|
5 | * modify it under the terms of the GNU Lesser General Public
|
---|
6 | * License as published by the Free Software Foundation; either
|
---|
7 | * version 2 of the License, or (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This library is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
12 | * Lesser General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU Lesser General Public
|
---|
15 | * License along with this library; if not, write to the Free Software
|
---|
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
17 | */
|
---|
18 | #include <stdlib.h>
|
---|
19 | #include <stdio.h>
|
---|
20 | #include <string.h>
|
---|
21 |
|
---|
22 | typedef unsigned char byte;
|
---|
23 |
|
---|
24 | void check( int value, char* message );
|
---|
25 |
|
---|
26 | #define MAX_BIOS_DATA 0x10000
|
---|
27 |
|
---|
28 | long chksum_bios_get_offset( byte* data, long offset );
|
---|
29 | byte chksum_bios_calc_value( byte* data, long offset );
|
---|
30 | byte chksum_bios_get_value( byte* data, long offset );
|
---|
31 | void chksum_bios_set_value( byte* data, long offset, byte value );
|
---|
32 |
|
---|
33 |
|
---|
34 | #define PMID_LEN 20
|
---|
35 | #define PMID_CHKSUM 19
|
---|
36 |
|
---|
37 | long chksum_pmid_get_offset( byte* data, long offset );
|
---|
38 | byte chksum_pmid_calc_value( byte* data, long offset );
|
---|
39 | byte chksum_pmid_get_value( byte* data, long offset );
|
---|
40 | void chksum_pmid_set_value( byte* data, long offset, byte value );
|
---|
41 |
|
---|
42 |
|
---|
43 | byte bios_data[MAX_BIOS_DATA];
|
---|
44 | long bios_len;
|
---|
45 |
|
---|
46 |
|
---|
47 | int main(int argc, char* argv[])
|
---|
48 | {
|
---|
49 | FILE* stream;
|
---|
50 | long offset, tmp_offset;
|
---|
51 | byte bios_len_byte, cur_val = 0, new_val = 0;
|
---|
52 | int hits, modified;
|
---|
53 |
|
---|
54 | if (argc != 2) {
|
---|
55 | printf( "Error. Need a file-name as an argument.\n" );
|
---|
56 | exit( EXIT_FAILURE );
|
---|
57 | }
|
---|
58 |
|
---|
59 | if ((stream = fopen(argv[1], "rb")) == NULL) {
|
---|
60 | printf("Error opening %s for reading.\n", argv[1]);
|
---|
61 | exit(EXIT_FAILURE);
|
---|
62 | }
|
---|
63 | memset(bios_data, 0, MAX_BIOS_DATA);
|
---|
64 | bios_len = fread(bios_data, 1, MAX_BIOS_DATA, stream);
|
---|
65 | if (bios_len > MAX_BIOS_DATA) {
|
---|
66 | printf("Error reading max. 65536 Bytes from %s.\n", argv[1]);
|
---|
67 | fclose(stream);
|
---|
68 | exit(EXIT_FAILURE);
|
---|
69 | }
|
---|
70 | fclose(stream);
|
---|
71 | modified = 0;
|
---|
72 | if (bios_len < 0x8000) {
|
---|
73 | bios_len = 0x8000;
|
---|
74 | modified = 1;
|
---|
75 | } else if ((bios_len & 0x1FF) != 0) {
|
---|
76 | bios_len = (bios_len + 0x200) & ~0x1FF;
|
---|
77 | modified = 1;
|
---|
78 | }
|
---|
79 | bios_len_byte = (byte)(bios_len / 512);
|
---|
80 | if (bios_len_byte != bios_data[2]) {
|
---|
81 | if (modified == 0) {
|
---|
82 | bios_len += 0x200;
|
---|
83 | }
|
---|
84 | bios_data[2] = (byte)(bios_len / 512);
|
---|
85 | modified = 1;
|
---|
86 | }
|
---|
87 |
|
---|
88 | hits = 0;
|
---|
89 | offset = 0L;
|
---|
90 | while( (tmp_offset = chksum_pmid_get_offset( bios_data, offset )) != -1L ) {
|
---|
91 | offset = tmp_offset;
|
---|
92 | cur_val = chksum_pmid_get_value( bios_data, offset );
|
---|
93 | new_val = chksum_pmid_calc_value( bios_data, offset );
|
---|
94 | printf( "\nPMID entry at: 0x%4lX\n", offset );
|
---|
95 | printf( "Current checksum: 0x%02X\n", cur_val );
|
---|
96 | printf( "Calculated checksum: 0x%02X ", new_val );
|
---|
97 | hits++;
|
---|
98 | }
|
---|
99 | if ((hits == 1) && (cur_val != new_val)) {
|
---|
100 | printf("Setting checksum.");
|
---|
101 | chksum_pmid_set_value( bios_data, offset, new_val );
|
---|
102 | if (modified == 0) {
|
---|
103 | bios_len += 0x200;
|
---|
104 | bios_data[2]++;
|
---|
105 | }
|
---|
106 | modified = 1;
|
---|
107 | }
|
---|
108 | if (hits >= 2) {
|
---|
109 | printf( "Multiple PMID entries! No checksum set." );
|
---|
110 | }
|
---|
111 | if (hits) {
|
---|
112 | printf("\n");
|
---|
113 | }
|
---|
114 |
|
---|
115 | offset = 0L;
|
---|
116 | do {
|
---|
117 | offset = chksum_bios_get_offset(bios_data, offset);
|
---|
118 | cur_val = chksum_bios_get_value(bios_data, offset);
|
---|
119 | new_val = chksum_bios_calc_value(bios_data, offset);
|
---|
120 | if ((cur_val != new_val) && (modified == 0)) {
|
---|
121 | bios_len += 0x200;
|
---|
122 | bios_data[2]++;
|
---|
123 | modified = 1;
|
---|
124 | } else {
|
---|
125 | printf("\nBios checksum at: 0x%4lX\n", offset);
|
---|
126 | printf("Current checksum: 0x%02X\n", cur_val);
|
---|
127 | printf("Calculated checksum: 0x%02X ", new_val);
|
---|
128 | if (cur_val != new_val) {
|
---|
129 | printf("Setting checksum.");
|
---|
130 | chksum_bios_set_value(bios_data, offset, new_val);
|
---|
131 | cur_val = new_val;
|
---|
132 | modified = 1;
|
---|
133 | }
|
---|
134 | printf( "\n" );
|
---|
135 | }
|
---|
136 | } while (cur_val != new_val);
|
---|
137 |
|
---|
138 | if (modified == 1) {
|
---|
139 | #ifdef VBOX
|
---|
140 | long new_bios_len;
|
---|
141 | #endif
|
---|
142 | if ((stream = fopen( argv[1], "wb")) == NULL) {
|
---|
143 | printf("Error opening %s for writing.\n", argv[1]);
|
---|
144 | exit(EXIT_FAILURE);
|
---|
145 | }
|
---|
146 | #ifdef VBOX
|
---|
147 | if (bios_len <= 0x8000) /* 32k */
|
---|
148 | new_bios_len = 0x8000;
|
---|
149 | else if (bios_len <= 0xC000) /* 48k */
|
---|
150 | new_bios_len = 0xC000;
|
---|
151 | else if (bios_len > 0xC000) /* 64k */
|
---|
152 | new_bios_len = MAX_BIOS_DATA;
|
---|
153 |
|
---|
154 | if (fwrite(bios_data, 1, new_bios_len, stream) < new_bios_len) {
|
---|
155 | #else
|
---|
156 | if (fwrite(bios_data, 1, bios_len, stream) < bios_len) {
|
---|
157 | #endif
|
---|
158 | printf("Error writing %ld KBytes to %s.\n", bios_len / 1024, argv[1]);
|
---|
159 | fclose(stream);
|
---|
160 | exit(EXIT_FAILURE);
|
---|
161 | }
|
---|
162 | fclose(stream);
|
---|
163 | }
|
---|
164 |
|
---|
165 | return (EXIT_SUCCESS);
|
---|
166 | }
|
---|
167 |
|
---|
168 |
|
---|
169 | void check( int okay, char* message ) {
|
---|
170 |
|
---|
171 | if( !okay ) {
|
---|
172 | printf( "\n\nError. %s.\n", message );
|
---|
173 | exit( EXIT_FAILURE );
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | long chksum_bios_get_offset( byte* data, long offset ) {
|
---|
179 |
|
---|
180 | return (bios_len - 1);
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | byte chksum_bios_calc_value( byte* data, long offset ) {
|
---|
185 |
|
---|
186 | int i;
|
---|
187 | byte sum;
|
---|
188 |
|
---|
189 | sum = 0;
|
---|
190 | for( i = 0; i < offset; i++ ) {
|
---|
191 | sum = sum + *( data + i );
|
---|
192 | }
|
---|
193 | sum = -sum; /* iso ensures -s + s == 0 on unsigned types */
|
---|
194 | return( sum );
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | byte chksum_bios_get_value( byte* data, long offset ) {
|
---|
199 |
|
---|
200 | return( *( data + offset ) );
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | void chksum_bios_set_value( byte* data, long offset, byte value ) {
|
---|
205 |
|
---|
206 | *( data + offset ) = value;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | byte chksum_pmid_calc_value( byte* data, long offset ) {
|
---|
211 |
|
---|
212 | int i;
|
---|
213 | int len;
|
---|
214 | byte sum;
|
---|
215 |
|
---|
216 | len = PMID_LEN;
|
---|
217 | check((offset + len) <= (bios_len - 1), "PMID entry length out of bounds" );
|
---|
218 | sum = 0;
|
---|
219 | for( i = 0; i < len; i++ ) {
|
---|
220 | if( i != PMID_CHKSUM ) {
|
---|
221 | sum = sum + *( data + offset + i );
|
---|
222 | }
|
---|
223 | }
|
---|
224 | sum = -sum;
|
---|
225 | return( sum );
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | long chksum_pmid_get_offset( byte* data, long offset ) {
|
---|
230 |
|
---|
231 | long result = -1L;
|
---|
232 |
|
---|
233 | while ((offset + PMID_LEN) < (bios_len - 1)) {
|
---|
234 | offset = offset + 1;
|
---|
235 | if( *( data + offset + 0 ) == 'P' && \
|
---|
236 | *( data + offset + 1 ) == 'M' && \
|
---|
237 | *( data + offset + 2 ) == 'I' && \
|
---|
238 | *( data + offset + 3 ) == 'D' ) {
|
---|
239 | result = offset;
|
---|
240 | break;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | return( result );
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | byte chksum_pmid_get_value( byte* data, long offset ) {
|
---|
248 |
|
---|
249 | check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
|
---|
250 | return( *( data + offset + PMID_CHKSUM ) );
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | void chksum_pmid_set_value( byte* data, long offset, byte value ) {
|
---|
255 |
|
---|
256 | check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
|
---|
257 | *( data + offset + PMID_CHKSUM ) = value;
|
---|
258 | }
|
---|