VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/BIOS/biossums.c@ 5450

Last change on this file since 5450 was 5450, checked in by vboxsync, 17 years ago

Synced with updated VGA/VESA BIOS.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
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#include <stdlib.h>
19#include <stdio.h>
20
21typedef unsigned char byte;
22
23void check( int value, char* message );
24
25#define MAX_BIOS_DATA 0x10000
26
27long chksum_bios_get_offset( byte* data, long offset );
28byte chksum_bios_calc_value( byte* data, long offset );
29byte chksum_bios_get_value( byte* data, long offset );
30void chksum_bios_set_value( byte* data, long offset, byte value );
31
32
33#define PMID_LEN 20
34#define PMID_CHKSUM 19
35
36long chksum_pmid_get_offset( byte* data, long offset );
37byte chksum_pmid_calc_value( byte* data, long offset );
38byte chksum_pmid_get_value( byte* data, long offset );
39void chksum_pmid_set_value( byte* data, long offset, byte value );
40
41
42byte bios_data[MAX_BIOS_DATA];
43long bios_len;
44
45
46int main(int argc, char* argv[])
47{
48 FILE* stream;
49 long offset, tmp_offset;
50 byte bios_len_byte, cur_val = 0, new_val = 0;
51 int hits, modified;
52
53 if (argc != 2) {
54 printf( "Error. Need a file-name as an argument.\n" );
55 exit( EXIT_FAILURE );
56 }
57
58 if ((stream = fopen(argv[1], "rb")) == NULL) {
59 printf("Error opening %s for reading.\n", argv[1]);
60 exit(EXIT_FAILURE);
61 }
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);
68 }
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;
77 }
78 bios_len_byte = (byte)(bios_len / 512);
79 if (bios_len_byte != bios_data[2]) {
80 if (modified == 0) {
81 bios_len += 0x200;
82 }
83 bios_data[2] = (byte)(bios_len / 512);
84 modified = 1;
85 }
86
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++;
97 }
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]++;
104 }
105 modified = 1;
106 }
107 if (hits >= 2) {
108 printf( "Multiple PMID entries! No checksum set." );
109 }
110 if (hits) {
111 printf("\n");
112 }
113
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;
132 }
133 printf( "\n" );
134 }
135 } while (cur_val != new_val);
136
137 if (modified == 1) {
138#ifdef VBOX
139 long new_bios_len;
140#endif
141 if ((stream = fopen( argv[1], "wb")) == NULL) {
142 printf("Error opening %s for writing.\n", argv[1]);
143 exit(EXIT_FAILURE);
144 }
145#ifdef VBOX
146 if (bios_len <= 0x8000) /* 32k */
147 new_bios_len = 0x8000;
148 else if (bios_len <= 0xC000) /* 48k */
149 new_bios_len = 0xC000;
150 else if (bios_len > 0xC000) /* 64k */
151 new_bios_len = MAX_BIOS_DATA;
152
153 if (fwrite(bios_data, 1, new_bios_len, stream) < new_bios_len) {
154#else
155 if (fwrite(bios_data, 1, bios_len, stream) < bios_len) {
156#endif
157 printf("Error writing %d KBytes to %s.\n", bios_len / 1024, argv[1]);
158 fclose(stream);
159 exit(EXIT_FAILURE);
160 }
161 fclose(stream);
162 }
163
164 return (EXIT_SUCCESS);
165}
166
167
168void check( int okay, char* message ) {
169
170 if( !okay ) {
171 printf( "\n\nError. %s.\n", message );
172 exit( EXIT_FAILURE );
173 }
174}
175
176
177long chksum_bios_get_offset( byte* data, long offset ) {
178
179 return (bios_len - 1);
180}
181
182
183byte chksum_bios_calc_value( byte* data, long offset ) {
184
185 int i;
186 byte sum;
187
188 sum = 0;
189 for( i = 0; i < offset; i++ ) {
190 sum = sum + *( data + i );
191 }
192 sum = -sum; /* iso ensures -s + s == 0 on unsigned types */
193 return( sum );
194}
195
196
197byte chksum_bios_get_value( byte* data, long offset ) {
198
199 return( *( data + offset ) );
200}
201
202
203void chksum_bios_set_value( byte* data, long offset, byte value ) {
204
205 *( data + offset ) = value;
206}
207
208
209byte chksum_pmid_calc_value( byte* data, long offset ) {
210
211 int i;
212 int len;
213 byte sum;
214
215 len = PMID_LEN;
216 check((offset + len) <= (bios_len - 1), "PMID entry length out of bounds" );
217 sum = 0;
218 for( i = 0; i < len; i++ ) {
219 if( i != PMID_CHKSUM ) {
220 sum = sum + *( data + offset + i );
221 }
222 }
223 sum = -sum;
224 return( sum );
225}
226
227
228long chksum_pmid_get_offset( byte* data, long offset ) {
229
230 long result = -1L;
231
232 while ((offset + PMID_LEN) < (bios_len - 1)) {
233 offset = offset + 1;
234 if( *( data + offset + 0 ) == 'P' && \
235 *( data + offset + 1 ) == 'M' && \
236 *( data + offset + 2 ) == 'I' && \
237 *( data + offset + 3 ) == 'D' ) {
238 result = offset;
239 break;
240 }
241 }
242 return( result );
243}
244
245
246byte chksum_pmid_get_value( byte* data, long offset ) {
247
248 check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
249 return( *( data + offset + PMID_CHKSUM ) );
250}
251
252
253void chksum_pmid_set_value( byte* data, long offset, byte value ) {
254
255 check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
256 *( data + offset + PMID_CHKSUM ) = value;
257}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette