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 |
|
---|
19 | /*
|
---|
20 | * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
21 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
22 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
23 | * a choice of LGPL license versions is made available with the language indicating
|
---|
24 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
25 | * of the LGPL is applied is otherwise unspecified.
|
---|
26 | */
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <stdio.h>
|
---|
29 | #include <string.h>
|
---|
30 |
|
---|
31 | typedef unsigned char byte;
|
---|
32 |
|
---|
33 | void check( int value, char* message );
|
---|
34 |
|
---|
35 | #define MAX_BIOS_DATA 0x10000
|
---|
36 |
|
---|
37 | long chksum_bios_get_offset( byte* data, long offset );
|
---|
38 | byte chksum_bios_calc_value( byte* data, long offset );
|
---|
39 | byte chksum_bios_get_value( byte* data, long offset );
|
---|
40 | void chksum_bios_set_value( byte* data, long offset, byte value );
|
---|
41 |
|
---|
42 |
|
---|
43 | #define PMID_LEN 20
|
---|
44 | #define PMID_CHKSUM 19
|
---|
45 |
|
---|
46 | long chksum_pmid_get_offset( byte* data, long offset );
|
---|
47 | byte chksum_pmid_calc_value( byte* data, long offset );
|
---|
48 | byte chksum_pmid_get_value( byte* data, long offset );
|
---|
49 | void chksum_pmid_set_value( byte* data, long offset, byte value );
|
---|
50 |
|
---|
51 |
|
---|
52 | byte bios_data[MAX_BIOS_DATA];
|
---|
53 | long bios_len;
|
---|
54 |
|
---|
55 |
|
---|
56 | int main(int argc, char* argv[])
|
---|
57 | {
|
---|
58 | FILE* stream;
|
---|
59 | long offset, tmp_offset;
|
---|
60 | byte bios_len_byte, cur_val = 0, new_val = 0;
|
---|
61 | int hits, modified;
|
---|
62 |
|
---|
63 | if (argc != 2) {
|
---|
64 | printf( "Error. Need a file-name as an argument.\n" );
|
---|
65 | exit( EXIT_FAILURE );
|
---|
66 | }
|
---|
67 |
|
---|
68 | if ((stream = fopen(argv[1], "rb")) == NULL) {
|
---|
69 | printf("Error opening %s for reading.\n", argv[1]);
|
---|
70 | exit(EXIT_FAILURE);
|
---|
71 | }
|
---|
72 | memset(bios_data, 0, MAX_BIOS_DATA);
|
---|
73 | bios_len = fread(bios_data, 1, MAX_BIOS_DATA, stream);
|
---|
74 | if (bios_len > MAX_BIOS_DATA) {
|
---|
75 | printf("Error reading max. 65536 Bytes from %s.\n", argv[1]);
|
---|
76 | fclose(stream);
|
---|
77 | exit(EXIT_FAILURE);
|
---|
78 | }
|
---|
79 | fclose(stream);
|
---|
80 | modified = 0;
|
---|
81 | if (bios_len < 0x8000) {
|
---|
82 | bios_len = 0x8000;
|
---|
83 | modified = 1;
|
---|
84 | } else if ((bios_len & 0x1FF) != 0) {
|
---|
85 | bios_len = (bios_len + 0x200) & ~0x1FF;
|
---|
86 | modified = 1;
|
---|
87 | }
|
---|
88 | bios_len_byte = (byte)(bios_len / 512);
|
---|
89 | if (bios_len_byte != bios_data[2]) {
|
---|
90 | if (modified == 0) {
|
---|
91 | bios_len += 0x200;
|
---|
92 | }
|
---|
93 | bios_data[2] = (byte)(bios_len / 512);
|
---|
94 | modified = 1;
|
---|
95 | }
|
---|
96 |
|
---|
97 | hits = 0;
|
---|
98 | offset = 0L;
|
---|
99 | while( (tmp_offset = chksum_pmid_get_offset( bios_data, offset )) != -1L ) {
|
---|
100 | offset = tmp_offset;
|
---|
101 | cur_val = chksum_pmid_get_value( bios_data, offset );
|
---|
102 | new_val = chksum_pmid_calc_value( bios_data, offset );
|
---|
103 | printf( "\nPMID entry at: 0x%4lX\n", offset );
|
---|
104 | printf( "Current checksum: 0x%02X\n", cur_val );
|
---|
105 | printf( "Calculated checksum: 0x%02X ", new_val );
|
---|
106 | hits++;
|
---|
107 | }
|
---|
108 | if ((hits == 1) && (cur_val != new_val)) {
|
---|
109 | printf("Setting checksum.");
|
---|
110 | chksum_pmid_set_value( bios_data, offset, new_val );
|
---|
111 | if (modified == 0) {
|
---|
112 | bios_len += 0x200;
|
---|
113 | bios_data[2]++;
|
---|
114 | }
|
---|
115 | modified = 1;
|
---|
116 | }
|
---|
117 | if (hits >= 2) {
|
---|
118 | printf( "Multiple PMID entries! No checksum set." );
|
---|
119 | }
|
---|
120 | if (hits) {
|
---|
121 | printf("\n");
|
---|
122 | }
|
---|
123 |
|
---|
124 | offset = 0L;
|
---|
125 | do {
|
---|
126 | offset = chksum_bios_get_offset(bios_data, offset);
|
---|
127 | cur_val = chksum_bios_get_value(bios_data, offset);
|
---|
128 | new_val = chksum_bios_calc_value(bios_data, offset);
|
---|
129 | if ((cur_val != new_val) && (modified == 0)) {
|
---|
130 | bios_len += 0x200;
|
---|
131 | bios_data[2]++;
|
---|
132 | modified = 1;
|
---|
133 | } else {
|
---|
134 | printf("\nBios checksum at: 0x%4lX\n", offset);
|
---|
135 | printf("Current checksum: 0x%02X\n", cur_val);
|
---|
136 | printf("Calculated checksum: 0x%02X ", new_val);
|
---|
137 | if (cur_val != new_val) {
|
---|
138 | printf("Setting checksum.");
|
---|
139 | chksum_bios_set_value(bios_data, offset, new_val);
|
---|
140 | cur_val = new_val;
|
---|
141 | modified = 1;
|
---|
142 | }
|
---|
143 | printf( "\n" );
|
---|
144 | }
|
---|
145 | } while (cur_val != new_val);
|
---|
146 |
|
---|
147 | if (modified == 1) {
|
---|
148 | #ifdef VBOX
|
---|
149 | size_t new_bios_len;
|
---|
150 | #endif
|
---|
151 | if ((stream = fopen( argv[1], "wb")) == NULL) {
|
---|
152 | printf("Error opening %s for writing.\n", argv[1]);
|
---|
153 | exit(EXIT_FAILURE);
|
---|
154 | }
|
---|
155 | #ifdef VBOX
|
---|
156 | if (bios_len <= 0x8000) /* 32k */
|
---|
157 | new_bios_len = 0x8000;
|
---|
158 | else if (bios_len <= 0xC000) /* 48k */
|
---|
159 | new_bios_len = 0xC000;
|
---|
160 | else if (bios_len > 0xC000) /* 64k */
|
---|
161 | new_bios_len = MAX_BIOS_DATA;
|
---|
162 |
|
---|
163 | if (fwrite(bios_data, 1, new_bios_len, stream) < new_bios_len) {
|
---|
164 | #else
|
---|
165 | if (fwrite(bios_data, 1, bios_len, stream) < bios_len) {
|
---|
166 | #endif
|
---|
167 | printf("Error writing %ld KBytes to %s.\n", bios_len / 1024, argv[1]);
|
---|
168 | fclose(stream);
|
---|
169 | exit(EXIT_FAILURE);
|
---|
170 | }
|
---|
171 | fclose(stream);
|
---|
172 | }
|
---|
173 |
|
---|
174 | return (EXIT_SUCCESS);
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | void check( int okay, char* message ) {
|
---|
179 |
|
---|
180 | if( !okay ) {
|
---|
181 | printf( "\n\nError. %s.\n", message );
|
---|
182 | exit( EXIT_FAILURE );
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 | long chksum_bios_get_offset( byte* data, long offset ) {
|
---|
188 |
|
---|
189 | return (bios_len - 1);
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | byte chksum_bios_calc_value( byte* data, long offset ) {
|
---|
194 |
|
---|
195 | int i;
|
---|
196 | byte sum;
|
---|
197 |
|
---|
198 | sum = 0;
|
---|
199 | for( i = 0; i < offset; i++ ) {
|
---|
200 | sum = sum + *( data + i );
|
---|
201 | }
|
---|
202 | sum = -sum; /* iso ensures -s + s == 0 on unsigned types */
|
---|
203 | return( sum );
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | byte chksum_bios_get_value( byte* data, long offset ) {
|
---|
208 |
|
---|
209 | return( *( data + offset ) );
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | void chksum_bios_set_value( byte* data, long offset, byte value ) {
|
---|
214 |
|
---|
215 | *( data + offset ) = value;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | byte chksum_pmid_calc_value( byte* data, long offset ) {
|
---|
220 |
|
---|
221 | int i;
|
---|
222 | int len;
|
---|
223 | byte sum;
|
---|
224 |
|
---|
225 | len = PMID_LEN;
|
---|
226 | check((offset + len) <= (bios_len - 1), "PMID entry length out of bounds" );
|
---|
227 | sum = 0;
|
---|
228 | for( i = 0; i < len; i++ ) {
|
---|
229 | if( i != PMID_CHKSUM ) {
|
---|
230 | sum = sum + *( data + offset + i );
|
---|
231 | }
|
---|
232 | }
|
---|
233 | sum = -sum;
|
---|
234 | return( sum );
|
---|
235 | }
|
---|
236 |
|
---|
237 |
|
---|
238 | long chksum_pmid_get_offset( byte* data, long offset ) {
|
---|
239 |
|
---|
240 | long result = -1L;
|
---|
241 |
|
---|
242 | while ((offset + PMID_LEN) < (bios_len - 1)) {
|
---|
243 | offset = offset + 1;
|
---|
244 | if( *( data + offset + 0 ) == 'P' && \
|
---|
245 | *( data + offset + 1 ) == 'M' && \
|
---|
246 | *( data + offset + 2 ) == 'I' && \
|
---|
247 | *( data + offset + 3 ) == 'D' ) {
|
---|
248 | result = offset;
|
---|
249 | break;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | return( result );
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | byte chksum_pmid_get_value( byte* data, long offset ) {
|
---|
257 |
|
---|
258 | check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
|
---|
259 | return( *( data + offset + PMID_CHKSUM ) );
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | void chksum_pmid_set_value( byte* data, long offset, byte value ) {
|
---|
264 |
|
---|
265 | check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
|
---|
266 | *( data + offset + PMID_CHKSUM ) = value;
|
---|
267 | }
|
---|