Move libkvm-all.c code to qemu-kvm.c
[qemu-kvm/fedora.git] / kvm / vgabios / biossums.c
blobd5816f420b311359eef6f0787ebebd44e7fa9d12
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 );
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 );
41 #define PCIR_LEN 24
43 long chksum_pcir_get_offset( byte* data, long offset );
46 byte bios_data[MAX_BIOS_DATA];
47 long bios_len;
50 int main(int argc, char* argv[])
52 FILE* stream;
53 long offset, tmp_offset, pcir_offset;
54 byte bios_len_byte, cur_val = 0, new_val = 0;
55 int hits, modified;
57 if (argc != 2) {
58 printf( "Error. Need a file-name as an argument.\n" );
59 exit( EXIT_FAILURE );
62 if ((stream = fopen(argv[1], "rb")) == NULL) {
63 printf("Error opening %s for reading.\n", argv[1]);
64 exit(EXIT_FAILURE);
66 memset(bios_data, 0, MAX_BIOS_DATA);
67 bios_len = fread(bios_data, 1, MAX_BIOS_DATA, stream);
68 if (bios_len > MAX_BIOS_DATA) {
69 printf("Error reading max. 65536 Bytes from %s.\n", argv[1]);
70 fclose(stream);
71 exit(EXIT_FAILURE);
73 fclose(stream);
74 modified = 0;
75 if (bios_len < 0x8000) {
76 bios_len = 0x8000;
77 modified = 1;
78 } else if ((bios_len & 0x1FF) != 0) {
79 bios_len = (bios_len + 0x200) & ~0x1FF;
80 modified = 1;
82 bios_len_byte = (byte)(bios_len / 512);
83 if (bios_len_byte != bios_data[2]) {
84 if (modified == 0) {
85 bios_len += 0x200;
87 bios_data[2] = (byte)(bios_len / 512);
88 modified = 1;
91 hits = 0;
92 offset = 0L;
93 while( (tmp_offset = chksum_pmid_get_offset( bios_data, offset )) != -1L ) {
94 offset = tmp_offset;
95 cur_val = chksum_pmid_get_value( bios_data, offset );
96 new_val = chksum_pmid_calc_value( bios_data, offset );
97 printf( "\nPMID entry at: 0x%4lX\n", offset );
98 printf( "Current checksum: 0x%02X\n", cur_val );
99 printf( "Calculated checksum: 0x%02X ", new_val );
100 hits++;
102 if ((hits == 1) && (cur_val != new_val)) {
103 printf("Setting checksum.");
104 chksum_pmid_set_value( bios_data, offset, new_val );
105 if (modified == 0) {
106 bios_len += 0x200;
107 bios_data[2]++;
109 modified = 1;
111 if (hits >= 2) {
112 printf( "Multiple PMID entries! No checksum set." );
114 if (hits) {
115 printf("\n");
118 offset = 0L;
119 pcir_offset = chksum_pcir_get_offset( bios_data, offset );
120 if (pcir_offset != -1L) {
121 if (bios_data[pcir_offset + 16] != bios_data[2]) {
122 bios_data[pcir_offset + 16] = bios_data[2];
123 if (modified == 0) {
124 bios_len += 0x200;
125 bios_data[2]++;
126 bios_data[pcir_offset + 16]++;
128 modified = 1;
132 offset = 0L;
133 do {
134 offset = chksum_bios_get_offset(bios_data, offset);
135 cur_val = chksum_bios_get_value(bios_data, offset);
136 new_val = chksum_bios_calc_value(bios_data, offset);
137 if ((cur_val != new_val) && (modified == 0)) {
138 bios_len += 0x200;
139 bios_data[2]++;
140 if (pcir_offset != -1L) {
141 bios_data[pcir_offset + 16]++;
143 modified = 1;
144 } else {
145 printf("\nBios checksum at: 0x%4lX\n", offset);
146 printf("Current checksum: 0x%02X\n", cur_val);
147 printf("Calculated checksum: 0x%02X ", new_val);
148 if (cur_val != new_val) {
149 printf("Setting checksum.");
150 chksum_bios_set_value(bios_data, offset, new_val);
151 cur_val = new_val;
152 modified = 1;
154 printf( "\n" );
156 } while (cur_val != new_val);
158 if (modified == 1) {
159 if ((stream = fopen( argv[1], "wb")) == NULL) {
160 printf("Error opening %s for writing.\n", argv[1]);
161 exit(EXIT_FAILURE);
163 if (fwrite(bios_data, 1, bios_len, stream) < bios_len) {
164 printf("Error writing %d KBytes to %s.\n", bios_len / 1024, argv[1]);
165 fclose(stream);
166 exit(EXIT_FAILURE);
168 fclose(stream);
171 return (EXIT_SUCCESS);
175 void check( int okay, char* message ) {
177 if( !okay ) {
178 printf( "\n\nError. %s.\n", message );
179 exit( EXIT_FAILURE );
184 long chksum_bios_get_offset( byte* data, long offset ) {
186 return (bios_len - 1);
190 byte chksum_bios_calc_value( byte* data, long offset ) {
192 int i;
193 byte sum;
195 sum = 0;
196 for( i = 0; i < offset; i++ ) {
197 sum = sum + *( data + i );
199 sum = -sum; /* iso ensures -s + s == 0 on unsigned types */
200 return( sum );
204 byte chksum_bios_get_value( byte* data, long offset ) {
206 return( *( data + offset ) );
210 void chksum_bios_set_value( byte* data, long offset, byte value ) {
212 *( data + offset ) = value;
216 byte chksum_pmid_calc_value( byte* data, long offset ) {
218 int i;
219 int len;
220 byte sum;
222 len = PMID_LEN;
223 check((offset + len) <= (bios_len - 1), "PMID entry length out of bounds" );
224 sum = 0;
225 for( i = 0; i < len; i++ ) {
226 if( i != PMID_CHKSUM ) {
227 sum = sum + *( data + offset + i );
230 sum = -sum;
231 return( sum );
235 long chksum_pmid_get_offset( byte* data, long offset ) {
237 long result = -1L;
239 while ((offset + PMID_LEN) < (bios_len - 1)) {
240 offset = offset + 1;
241 if( *( data + offset + 0 ) == 'P' && \
242 *( data + offset + 1 ) == 'M' && \
243 *( data + offset + 2 ) == 'I' && \
244 *( data + offset + 3 ) == 'D' ) {
245 result = offset;
246 break;
249 return( result );
253 byte chksum_pmid_get_value( byte* data, long offset ) {
255 check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
256 return( *( data + offset + PMID_CHKSUM ) );
260 void chksum_pmid_set_value( byte* data, long offset, byte value ) {
262 check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
263 *( data + offset + PMID_CHKSUM ) = value;
267 long chksum_pcir_get_offset( byte* data, long offset ) {
269 long result = -1L;
271 while ((offset + PCIR_LEN) < (bios_len - 1)) {
272 offset = offset + 1;
273 if( *( data + offset + 0 ) == 'P' && \
274 *( data + offset + 1 ) == 'C' && \
275 *( data + offset + 2 ) == 'I' && \
276 *( data + offset + 3 ) == 'R' ) {
277 result = offset;
278 break;
281 return( result );