kvm: qemu: fix timer rearm fallout from last qemu merge
[kvm-userspace.git] / vgabios / biossums.c
blobb0378fbdcd5c66165a774b8d38ba8f200ecd3aea
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>
20 #include <string.h>
22 typedef unsigned char byte;
24 void check( int value, char* message );
26 #define MAX_BIOS_DATA 0x10000
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 );
34 #define PMID_LEN 20
35 #define PMID_CHKSUM 19
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 );
43 byte bios_data[MAX_BIOS_DATA];
44 long bios_len;
47 int main(int argc, char* argv[])
49 FILE* stream;
50 long offset, tmp_offset;
51 byte bios_len_byte, cur_val = 0, new_val = 0;
52 int hits, modified;
54 if (argc != 2) {
55 printf( "Error. Need a file-name as an argument.\n" );
56 exit( EXIT_FAILURE );
59 if ((stream = fopen(argv[1], "rb")) == NULL) {
60 printf("Error opening %s for reading.\n", argv[1]);
61 exit(EXIT_FAILURE);
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);
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;
79 bios_len_byte = (byte)(bios_len / 512);
80 if (bios_len_byte != bios_data[2]) {
81 if (modified == 0) {
82 bios_len += 0x200;
84 bios_data[2] = (byte)(bios_len / 512);
85 modified = 1;
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++;
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]++;
106 modified = 1;
108 if (hits >= 2) {
109 printf( "Multiple PMID entries! No checksum set." );
111 if (hits) {
112 printf("\n");
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;
134 printf( "\n" );
136 } while (cur_val != new_val);
138 if (modified == 1) {
139 if ((stream = fopen( argv[1], "wb")) == NULL) {
140 printf("Error opening %s for writing.\n", argv[1]);
141 exit(EXIT_FAILURE);
143 if (fwrite(bios_data, 1, bios_len, stream) < bios_len) {
144 printf("Error writing %d KBytes to %s.\n", bios_len / 1024, argv[1]);
145 fclose(stream);
146 exit(EXIT_FAILURE);
148 fclose(stream);
151 return (EXIT_SUCCESS);
155 void check( int okay, char* message ) {
157 if( !okay ) {
158 printf( "\n\nError. %s.\n", message );
159 exit( EXIT_FAILURE );
164 long chksum_bios_get_offset( byte* data, long offset ) {
166 return (bios_len - 1);
170 byte chksum_bios_calc_value( byte* data, long offset ) {
172 int i;
173 byte sum;
175 sum = 0;
176 for( i = 0; i < offset; i++ ) {
177 sum = sum + *( data + i );
179 sum = -sum; /* iso ensures -s + s == 0 on unsigned types */
180 return( sum );
184 byte chksum_bios_get_value( byte* data, long offset ) {
186 return( *( data + offset ) );
190 void chksum_bios_set_value( byte* data, long offset, byte value ) {
192 *( data + offset ) = value;
196 byte chksum_pmid_calc_value( byte* data, long offset ) {
198 int i;
199 int len;
200 byte sum;
202 len = PMID_LEN;
203 check((offset + len) <= (bios_len - 1), "PMID entry length out of bounds" );
204 sum = 0;
205 for( i = 0; i < len; i++ ) {
206 if( i != PMID_CHKSUM ) {
207 sum = sum + *( data + offset + i );
210 sum = -sum;
211 return( sum );
215 long chksum_pmid_get_offset( byte* data, long offset ) {
217 long result = -1L;
219 while ((offset + PMID_LEN) < (bios_len - 1)) {
220 offset = offset + 1;
221 if( *( data + offset + 0 ) == 'P' && \
222 *( data + offset + 1 ) == 'M' && \
223 *( data + offset + 2 ) == 'I' && \
224 *( data + offset + 3 ) == 'D' ) {
225 result = offset;
226 break;
229 return( result );
233 byte chksum_pmid_get_value( byte* data, long offset ) {
235 check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
236 return( *( data + offset + PMID_CHKSUM ) );
240 void chksum_pmid_set_value( byte* data, long offset, byte value ) {
242 check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
243 *( data + offset + PMID_CHKSUM ) = value;