- missing license text added in biossums.c
[vgabios.git] / biossums.c
blobb8e7f0892422b263a3c3887193a7bdb518f1589e
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 LEN_BIOS_DATA 0x8000
26 #define MAX_OFFSET (LEN_BIOS_DATA - 1)
29 #define BIOS_OFFSET 0x7FFF
31 long chksum_bios_get_offset( byte* data, long offset );
32 byte chksum_bios_calc_value( byte* data, long offset );
33 byte chksum_bios_get_value( byte* data, long offset );
34 void chksum_bios_set_value( byte* data, long offset, byte value );
37 #define PMID_LEN 20
38 #define PMID_CHKSUM 19
40 long chksum_pmid_get_offset( byte* data, long offset );
41 byte chksum_pmid_calc_value( byte* data, long offset );
42 byte chksum_pmid_get_value( byte* data, long offset );
43 void chksum_pmid_set_value( byte* data, long offset, byte value );
46 byte bios_data[LEN_BIOS_DATA];
49 int main( int argc, char* argv[] ) {
51 FILE* stream;
52 long offset, tmp_offset;
53 byte cur_val = 0, new_val = 0;
54 int hits;
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 if( fread( bios_data, 1, LEN_BIOS_DATA, stream ) >= LEN_BIOS_DATA ) {
67 printf( "Error reading max. 32767 Bytes from %s.\n", argv[1] );
68 fclose( stream );
69 exit( EXIT_FAILURE );
71 fclose( stream );
73 hits = 0;
74 offset = 0L;
75 while( (tmp_offset = chksum_pmid_get_offset( bios_data, offset )) != -1L ) {
76 offset = tmp_offset;
77 cur_val = chksum_pmid_get_value( bios_data, offset );
78 new_val = chksum_pmid_calc_value( bios_data, offset );
79 printf( "\nPMID entry at: 0x%4lX\n", offset );
80 printf( "Current checksum: 0x%02X\n", cur_val );
81 printf( "Calculated checksum: 0x%02X ", new_val );
82 hits++;
84 if( hits == 1 && cur_val != new_val ) {
85 printf( "Setting checksum." );
86 chksum_pmid_set_value( bios_data, offset, new_val );
88 if( hits >= 2 ) {
89 printf( "Multiple PMID entries! No checksum set." );
91 if( hits ) {
92 printf( "\n" );
96 offset = 0L;
97 offset = chksum_bios_get_offset( bios_data, offset );
98 cur_val = chksum_bios_get_value( bios_data, offset );
99 new_val = chksum_bios_calc_value( bios_data, offset );
100 printf( "\nBios checksum at: 0x%4lX\n", offset );
101 printf( "Current checksum: 0x%02X\n", cur_val );
102 printf( "Calculated checksum: 0x%02X ", new_val );
103 if( cur_val != new_val ) {
104 printf( "Setting checksum." );
105 chksum_bios_set_value( bios_data, offset, new_val );
107 printf( "\n" );
110 if(( stream = fopen( argv[1], "wb" )) == NULL ) {
111 printf( "Error opening %s for writing.\n", argv[1] );
112 exit( EXIT_FAILURE );
114 if( fwrite( bios_data, 1, LEN_BIOS_DATA, stream ) < LEN_BIOS_DATA ) {
115 printf( "Error writing 32KBytes to %s.\n", argv[1] );
116 fclose( stream );
117 exit( EXIT_FAILURE );
119 fclose( stream );
121 return( EXIT_SUCCESS );
125 void check( int okay, char* message ) {
127 if( !okay ) {
128 printf( "\n\nError. %s.\n", message );
129 exit( EXIT_FAILURE );
134 long chksum_bios_get_offset( byte* data, long offset ) {
136 return( BIOS_OFFSET );
140 byte chksum_bios_calc_value( byte* data, long offset ) {
142 int i;
143 byte sum;
145 sum = 0;
146 for( i = 0; i < MAX_OFFSET; i++ ) {
147 sum = sum + *( data + i );
149 sum = -sum; /* iso ensures -s + s == 0 on unsigned types */
150 return( sum );
154 byte chksum_bios_get_value( byte* data, long offset ) {
156 return( *( data + BIOS_OFFSET ) );
160 void chksum_bios_set_value( byte* data, long offset, byte value ) {
162 *( data + BIOS_OFFSET ) = value;
166 byte chksum_pmid_calc_value( byte* data, long offset ) {
168 int i;
169 int len;
170 byte sum;
172 len = PMID_LEN;
173 check( offset + len <= MAX_OFFSET, "PMID entry length out of bounds" );
174 sum = 0;
175 for( i = 0; i < len; i++ ) {
176 if( i != PMID_CHKSUM ) {
177 sum = sum + *( data + offset + i );
180 sum = -sum;
181 return( sum );
185 long chksum_pmid_get_offset( byte* data, long offset ) {
187 long result = -1L;
189 while( offset + PMID_LEN < MAX_OFFSET ) {
190 offset = offset + 1;
191 if( *( data + offset + 0 ) == 'P' && \
192 *( data + offset + 1 ) == 'M' && \
193 *( data + offset + 2 ) == 'I' && \
194 *( data + offset + 3 ) == 'D' ) {
195 result = offset;
196 break;
199 return( result );
203 byte chksum_pmid_get_value( byte* data, long offset ) {
205 check( offset + PMID_CHKSUM <= MAX_OFFSET, "PMID checksum out of bounds" );
206 return( *( data + offset + PMID_CHKSUM ) );
210 void chksum_pmid_set_value( byte* data, long offset, byte value ) {
212 check( offset + PMID_CHKSUM <= MAX_OFFSET, "PMID checksum out of bounds" );
213 *( data + offset + PMID_CHKSUM ) = value;