- the biossums utility no longer modifies VGABIOS images with proper checksum
[vgabios.git] / biossums.c
blob5014efc49baeca2fe21f12527c855046dd65ee84
1 /* biossums.c --- written by Eike W. for the Bochs BIOS */
2 /* adapted for the LGPL'd VGABIOS by vruppert */
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.
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.
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
18 #include <stdlib.h>
19 #include <stdio.h>
21 typedef unsigned char byte;
23 void check( int value, char* message );
25 #define MAX_BIOS_DATA 0x10000
27 long chksum_bios_get_offset( byte* data, long offset );
28 byte chksum_bios_calc_value( byte* data, long offset );
29 byte chksum_bios_get_value( byte* data, long offset );
30 void chksum_bios_set_value( byte* data, long offset, byte value );
33 #define PMID_LEN 20
34 #define PMID_CHKSUM 19
36 long chksum_pmid_get_offset( byte* data, long offset );
37 byte chksum_pmid_calc_value( byte* data, long offset );
38 byte chksum_pmid_get_value( byte* data, long offset );
39 void chksum_pmid_set_value( byte* data, long offset, byte value );
42 byte bios_data[MAX_BIOS_DATA];
43 long bios_len;
46 int main(int argc, char* argv[])
48 FILE* stream;
49 long offset, tmp_offset;
50 byte bios_len_byte, cur_val = 0, new_val = 0;
51 int hits, modified;
53 if (argc != 2) {
54 printf( "Error. Need a file-name as an argument.\n" );
55 exit( EXIT_FAILURE );
58 if ((stream = fopen(argv[1], "rb")) == NULL) {
59 printf("Error opening %s for reading.\n", argv[1]);
60 exit(EXIT_FAILURE);
62 memset(bios_data, 0, MAX_BIOS_DATA);
63 bios_len = fread(bios_data, 1, MAX_BIOS_DATA, stream);
64 if (bios_len > MAX_BIOS_DATA) {
65 printf("Error reading max. 65536 Bytes from %s.\n", argv[1]);
66 fclose(stream);
67 exit(EXIT_FAILURE);
69 fclose(stream);
70 modified = 0;
71 if (bios_len < 0x8000) {
72 bios_len = 0x8000;
73 modified = 1;
74 } else if ((bios_len & 0x1FF) != 0) {
75 bios_len = (bios_len + 0x200) & ~0x1FF;
76 modified = 1;
78 bios_len_byte = (byte)(bios_len / 512);
79 if (bios_len_byte != bios_data[2]) {
80 if (modified == 0) {
81 bios_len += 0x200;
83 bios_data[2] = (byte)(bios_len / 512);
84 modified = 1;
87 hits = 0;
88 offset = 0L;
89 while( (tmp_offset = chksum_pmid_get_offset( bios_data, offset )) != -1L ) {
90 offset = tmp_offset;
91 cur_val = chksum_pmid_get_value( bios_data, offset );
92 new_val = chksum_pmid_calc_value( bios_data, offset );
93 printf( "\nPMID entry at: 0x%4lX\n", offset );
94 printf( "Current checksum: 0x%02X\n", cur_val );
95 printf( "Calculated checksum: 0x%02X ", new_val );
96 hits++;
98 if ((hits == 1) && (cur_val != new_val)) {
99 printf("Setting checksum.");
100 chksum_pmid_set_value( bios_data, offset, new_val );
101 if (modified == 0) {
102 bios_len += 0x200;
103 bios_data[2]++;
105 modified = 1;
107 if (hits >= 2) {
108 printf( "Multiple PMID entries! No checksum set." );
110 if (hits) {
111 printf("\n");
114 offset = 0L;
115 do {
116 offset = chksum_bios_get_offset(bios_data, offset);
117 cur_val = chksum_bios_get_value(bios_data, offset);
118 new_val = chksum_bios_calc_value(bios_data, offset);
119 if ((cur_val != new_val) && (modified == 0)) {
120 bios_len += 0x200;
121 bios_data[2]++;
122 modified = 1;
123 } else {
124 printf("\nBios checksum at: 0x%4lX\n", offset);
125 printf("Current checksum: 0x%02X\n", cur_val);
126 printf("Calculated checksum: 0x%02X ", new_val);
127 if (cur_val != new_val) {
128 printf("Setting checksum.");
129 chksum_bios_set_value(bios_data, offset, new_val);
130 cur_val = new_val;
131 modified = 1;
133 printf( "\n" );
135 } while (cur_val != new_val);
137 if (modified == 1) {
138 if ((stream = fopen( argv[1], "wb")) == NULL) {
139 printf("Error opening %s for writing.\n", argv[1]);
140 exit(EXIT_FAILURE);
142 if (fwrite(bios_data, 1, bios_len, stream) < bios_len) {
143 printf("Error writing %d KBytes to %s.\n", bios_len / 1024, argv[1]);
144 fclose(stream);
145 exit(EXIT_FAILURE);
147 fclose(stream);
150 return (EXIT_SUCCESS);
154 void check( int okay, char* message ) {
156 if( !okay ) {
157 printf( "\n\nError. %s.\n", message );
158 exit( EXIT_FAILURE );
163 long chksum_bios_get_offset( byte* data, long offset ) {
165 return (bios_len - 1);
169 byte chksum_bios_calc_value( byte* data, long offset ) {
171 int i;
172 byte sum;
174 sum = 0;
175 for( i = 0; i < offset; i++ ) {
176 sum = sum + *( data + i );
178 sum = -sum; /* iso ensures -s + s == 0 on unsigned types */
179 return( sum );
183 byte chksum_bios_get_value( byte* data, long offset ) {
185 return( *( data + offset ) );
189 void chksum_bios_set_value( byte* data, long offset, byte value ) {
191 *( data + offset ) = value;
195 byte chksum_pmid_calc_value( byte* data, long offset ) {
197 int i;
198 int len;
199 byte sum;
201 len = PMID_LEN;
202 check((offset + len) <= (bios_len - 1), "PMID entry length out of bounds" );
203 sum = 0;
204 for( i = 0; i < len; i++ ) {
205 if( i != PMID_CHKSUM ) {
206 sum = sum + *( data + offset + i );
209 sum = -sum;
210 return( sum );
214 long chksum_pmid_get_offset( byte* data, long offset ) {
216 long result = -1L;
218 while ((offset + PMID_LEN) < (bios_len - 1)) {
219 offset = offset + 1;
220 if( *( data + offset + 0 ) == 'P' && \
221 *( data + offset + 1 ) == 'M' && \
222 *( data + offset + 2 ) == 'I' && \
223 *( data + offset + 3 ) == 'D' ) {
224 result = offset;
225 break;
228 return( result );
232 byte chksum_pmid_get_value( byte* data, long offset ) {
234 check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
235 return( *( data + offset + PMID_CHKSUM ) );
239 void chksum_pmid_set_value( byte* data, long offset, byte value ) {
241 check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
242 *( data + offset + PMID_CHKSUM ) = value;