/* biossums.c --- written by Eike W. for the Bochs BIOS */
|
/* biossums.c --- written by Eike W. for the Bochs BIOS */
|
/* adapted for the LGPL'd VGABIOS by vruppert */
|
/* adapted for the LGPL'd VGABIOS by vruppert */
|
|
|
/* This library is free software; you can redistribute it and/or
|
/* This library is free software; you can redistribute it and/or
|
* modify it under the terms of the GNU Lesser General Public
|
* modify it under the terms of the GNU Lesser General Public
|
* License as published by the Free Software Foundation; either
|
* License as published by the Free Software Foundation; either
|
* version 2 of the License, or (at your option) any later version.
|
* version 2 of the License, or (at your option) any later version.
|
*
|
*
|
* This library is distributed in the hope that it will be useful,
|
* This library is distributed in the hope that it will be useful,
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
* Lesser General Public License for more details.
|
* Lesser General Public License for more details.
|
*
|
*
|
* You should have received a copy of the GNU Lesser General Public
|
* You should have received a copy of the GNU Lesser General Public
|
* License along with this library; if not, write to the Free Software
|
* License along with this library; if not, write to the Free Software
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
*/
|
*/
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <string.h>
|
#include <string.h>
|
|
|
typedef unsigned char byte;
|
typedef unsigned char byte;
|
|
|
void check( int value, char* message );
|
void check( int value, char* message );
|
|
|
#define MAX_BIOS_DATA 0x10000
|
#define MAX_BIOS_DATA 0x10000
|
|
|
long chksum_bios_get_offset( byte* data, long offset );
|
long chksum_bios_get_offset( byte* data, long offset );
|
byte chksum_bios_calc_value( byte* data, long offset );
|
byte chksum_bios_calc_value( byte* data, long offset );
|
byte chksum_bios_get_value( byte* data, long offset );
|
byte chksum_bios_get_value( byte* data, long offset );
|
void chksum_bios_set_value( byte* data, long offset, byte value );
|
void chksum_bios_set_value( byte* data, long offset, byte value );
|
|
|
|
|
#define PMID_LEN 20
|
#define PMID_LEN 20
|
#define PMID_CHKSUM 19
|
#define PMID_CHKSUM 19
|
|
|
long chksum_pmid_get_offset( byte* data, long offset );
|
long chksum_pmid_get_offset( byte* data, long offset );
|
byte chksum_pmid_calc_value( byte* data, long offset );
|
byte chksum_pmid_calc_value( byte* data, long offset );
|
byte chksum_pmid_get_value( byte* data, long offset );
|
byte chksum_pmid_get_value( byte* data, long offset );
|
void chksum_pmid_set_value( byte* data, long offset, byte value );
|
void chksum_pmid_set_value( byte* data, long offset, byte value );
|
|
|
|
|
byte bios_data[MAX_BIOS_DATA];
|
byte bios_data[MAX_BIOS_DATA];
|
long bios_len;
|
long bios_len;
|
|
|
|
|
int main( int argc, char* argv[] ) {
|
int main( int argc, char* argv[] ) {
|
|
|
FILE* stream;
|
FILE* stream;
|
long offset, tmp_offset;
|
long offset, tmp_offset;
|
byte cur_val = 0, new_val = 0;
|
byte cur_val = 0, new_val = 0;
|
int hits;
|
int hits;
|
|
|
|
|
if (argc != 2) {
|
if (argc != 2) {
|
printf( "Error. Need a file-name as an argument.\n" );
|
printf( "Error. Need a file-name as an argument.\n" );
|
exit( EXIT_FAILURE );
|
exit( EXIT_FAILURE );
|
}
|
}
|
|
|
if ((stream = fopen(argv[1], "rb")) == NULL) {
|
if ((stream = fopen(argv[1], "rb")) == NULL) {
|
printf("Error opening %s for reading.\n", argv[1]);
|
printf("Error opening %s for reading.\n", argv[1]);
|
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
}
|
}
|
memset(bios_data, 0, MAX_BIOS_DATA);
|
memset(bios_data, 0, MAX_BIOS_DATA);
|
bios_len = fread(bios_data, 1, MAX_BIOS_DATA, stream);
|
bios_len = fread(bios_data, 1, MAX_BIOS_DATA, stream);
|
if (bios_len >= MAX_BIOS_DATA) {
|
if (bios_len >= MAX_BIOS_DATA) {
|
printf("Error reading max. 65535 Bytes from %s.\n", argv[1]);
|
printf("Error reading max. 65535 Bytes from %s.\n", argv[1]);
|
fclose(stream);
|
fclose(stream);
|
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
}
|
}
|
fclose(stream);
|
fclose(stream);
|
if (bios_len < 0x7FFF) {
|
if (bios_len < 0x7FFF) {
|
bios_len = 0x8000;
|
bios_len = 0x8000;
|
} else {
|
} else {
|
bios_len = (bios_len + 0x201) & ~0x1FF;
|
bios_len = (bios_len + 0x201) & ~0x1FF;
|
}
|
}
|
bios_data[2] = (byte)(bios_len / 512);
|
bios_data[2] = (byte)(bios_len / 512);
|
|
|
hits = 0;
|
hits = 0;
|
offset = 0L;
|
offset = 0L;
|
while( (tmp_offset = chksum_pmid_get_offset( bios_data, offset )) != -1L ) {
|
while( (tmp_offset = chksum_pmid_get_offset( bios_data, offset )) != -1L ) {
|
offset = tmp_offset;
|
offset = tmp_offset;
|
cur_val = chksum_pmid_get_value( bios_data, offset );
|
cur_val = chksum_pmid_get_value( bios_data, offset );
|
new_val = chksum_pmid_calc_value( bios_data, offset );
|
new_val = chksum_pmid_calc_value( bios_data, offset );
|
printf( "\nPMID entry at: 0x%4lX\n", offset );
|
printf( "\nPMID entry at: 0x%4lX\n", offset );
|
printf( "Current checksum: 0x%02X\n", cur_val );
|
printf( "Current checksum: 0x%02X\n", cur_val );
|
printf( "Calculated checksum: 0x%02X ", new_val );
|
printf( "Calculated checksum: 0x%02X ", new_val );
|
hits++;
|
hits++;
|
}
|
}
|
if( hits == 1 && cur_val != new_val ) {
|
if( hits == 1 && cur_val != new_val ) {
|
printf( "Setting checksum." );
|
printf( "Setting checksum." );
|
chksum_pmid_set_value( bios_data, offset, new_val );
|
chksum_pmid_set_value( bios_data, offset, new_val );
|
}
|
}
|
if( hits >= 2 ) {
|
if( hits >= 2 ) {
|
printf( "Multiple PMID entries! No checksum set." );
|
printf( "Multiple PMID entries! No checksum set." );
|
}
|
}
|
if( hits ) {
|
if( hits ) {
|
printf( "\n" );
|
printf( "\n" );
|
}
|
}
|
|
|
|
|
offset = 0L;
|
offset = 0L;
|
offset = chksum_bios_get_offset( bios_data, offset );
|
offset = chksum_bios_get_offset( bios_data, offset );
|
cur_val = chksum_bios_get_value( bios_data, offset );
|
cur_val = chksum_bios_get_value( bios_data, offset );
|
new_val = chksum_bios_calc_value( bios_data, offset );
|
new_val = chksum_bios_calc_value( bios_data, offset );
|
printf( "\nBios checksum at: 0x%4lX\n", offset );
|
printf( "\nBios checksum at: 0x%4lX\n", offset );
|
printf( "Current checksum: 0x%02X\n", cur_val );
|
printf( "Current checksum: 0x%02X\n", cur_val );
|
printf( "Calculated checksum: 0x%02X ", new_val );
|
printf( "Calculated checksum: 0x%02X ", new_val );
|
if( cur_val != new_val ) {
|
if( cur_val != new_val ) {
|
printf( "Setting checksum." );
|
printf( "Setting checksum." );
|
chksum_bios_set_value( bios_data, offset, new_val );
|
chksum_bios_set_value( bios_data, offset, new_val );
|
}
|
}
|
printf( "\n" );
|
printf( "\n" );
|
|
|
|
|
if(( stream = fopen( argv[1], "wb" )) == NULL ) {
|
if(( stream = fopen( argv[1], "wb" )) == NULL ) {
|
printf( "Error opening %s for writing.\n", argv[1] );
|
printf( "Error opening %s for writing.\n", argv[1] );
|
exit( EXIT_FAILURE );
|
exit( EXIT_FAILURE );
|
}
|
}
|
if( fwrite( bios_data, 1, bios_len, stream ) < bios_len ) {
|
if( fwrite( bios_data, 1, bios_len, stream ) < bios_len ) {
|
printf( "Error writing %d KBytes to %s.\n", bios_len / 1024, argv[1] );
|
printf( "Error writing %d KBytes to %s.\n", bios_len / 1024, argv[1] );
|
fclose( stream );
|
fclose( stream );
|
exit( EXIT_FAILURE );
|
exit( EXIT_FAILURE );
|
}
|
}
|
fclose( stream );
|
fclose( stream );
|
|
|
return( EXIT_SUCCESS );
|
return( EXIT_SUCCESS );
|
}
|
}
|
|
|
|
|
void check( int okay, char* message ) {
|
void check( int okay, char* message ) {
|
|
|
if( !okay ) {
|
if( !okay ) {
|
printf( "\n\nError. %s.\n", message );
|
printf( "\n\nError. %s.\n", message );
|
exit( EXIT_FAILURE );
|
exit( EXIT_FAILURE );
|
}
|
}
|
}
|
}
|
|
|
|
|
long chksum_bios_get_offset( byte* data, long offset ) {
|
long chksum_bios_get_offset( byte* data, long offset ) {
|
|
|
return (bios_len - 1);
|
return (bios_len - 1);
|
}
|
}
|
|
|
|
|
byte chksum_bios_calc_value( byte* data, long offset ) {
|
byte chksum_bios_calc_value( byte* data, long offset ) {
|
|
|
int i;
|
int i;
|
byte sum;
|
byte sum;
|
|
|
sum = 0;
|
sum = 0;
|
for( i = 0; i < offset; i++ ) {
|
for( i = 0; i < offset; i++ ) {
|
sum = sum + *( data + i );
|
sum = sum + *( data + i );
|
}
|
}
|
sum = -sum; /* iso ensures -s + s == 0 on unsigned types */
|
sum = -sum; /* iso ensures -s + s == 0 on unsigned types */
|
return( sum );
|
return( sum );
|
}
|
}
|
|
|
|
|
byte chksum_bios_get_value( byte* data, long offset ) {
|
byte chksum_bios_get_value( byte* data, long offset ) {
|
|
|
return( *( data + offset ) );
|
return( *( data + offset ) );
|
}
|
}
|
|
|
|
|
void chksum_bios_set_value( byte* data, long offset, byte value ) {
|
void chksum_bios_set_value( byte* data, long offset, byte value ) {
|
|
|
*( data + offset ) = value;
|
*( data + offset ) = value;
|
}
|
}
|
|
|
|
|
byte chksum_pmid_calc_value( byte* data, long offset ) {
|
byte chksum_pmid_calc_value( byte* data, long offset ) {
|
|
|
int i;
|
int i;
|
int len;
|
int len;
|
byte sum;
|
byte sum;
|
|
|
len = PMID_LEN;
|
len = PMID_LEN;
|
check((offset + len) <= (bios_len - 1), "PMID entry length out of bounds" );
|
check((offset + len) <= (bios_len - 1), "PMID entry length out of bounds" );
|
sum = 0;
|
sum = 0;
|
for( i = 0; i < len; i++ ) {
|
for( i = 0; i < len; i++ ) {
|
if( i != PMID_CHKSUM ) {
|
if( i != PMID_CHKSUM ) {
|
sum = sum + *( data + offset + i );
|
sum = sum + *( data + offset + i );
|
}
|
}
|
}
|
}
|
sum = -sum;
|
sum = -sum;
|
return( sum );
|
return( sum );
|
}
|
}
|
|
|
|
|
long chksum_pmid_get_offset( byte* data, long offset ) {
|
long chksum_pmid_get_offset( byte* data, long offset ) {
|
|
|
long result = -1L;
|
long result = -1L;
|
|
|
while ((offset + PMID_LEN) < (bios_len - 1)) {
|
while ((offset + PMID_LEN) < (bios_len - 1)) {
|
offset = offset + 1;
|
offset = offset + 1;
|
if( *( data + offset + 0 ) == 'P' && \
|
if( *( data + offset + 0 ) == 'P' && \
|
*( data + offset + 1 ) == 'M' && \
|
*( data + offset + 1 ) == 'M' && \
|
*( data + offset + 2 ) == 'I' && \
|
*( data + offset + 2 ) == 'I' && \
|
*( data + offset + 3 ) == 'D' ) {
|
*( data + offset + 3 ) == 'D' ) {
|
result = offset;
|
result = offset;
|
break;
|
break;
|
}
|
}
|
}
|
}
|
return( result );
|
return( result );
|
}
|
}
|
|
|
|
|
byte chksum_pmid_get_value( byte* data, long offset ) {
|
byte chksum_pmid_get_value( byte* data, long offset ) {
|
|
|
check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
|
check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
|
return( *( data + offset + PMID_CHKSUM ) );
|
return( *( data + offset + PMID_CHKSUM ) );
|
}
|
}
|
|
|
|
|
void chksum_pmid_set_value( byte* data, long offset, byte value ) {
|
void chksum_pmid_set_value( byte* data, long offset, byte value ) {
|
|
|
check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
|
check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
|
*( data + offset + PMID_CHKSUM ) = value;
|
*( data + offset + PMID_CHKSUM ) = value;
|
}
|
}
|
|
|