OpenCores
URL https://opencores.org/ocsvn/zet86/zet86/trunk

Subversion Repositories zet86

[/] [zet86/] [trunk/] [soc/] [bios/] [vgasums.c] - Blame information for rev 54

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 34 zeus
/* 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
#include <string.h>
21
 
22
typedef unsigned char byte;
23
 
24
void check( int value, char* message );
25
 
26
#define MAX_BIOS_DATA 0x10000
27
 
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 );
32
 
33
 
34
#define PMID_LEN        20
35
#define PMID_CHKSUM     19
36
 
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 );
41
 
42
 
43
byte bios_data[MAX_BIOS_DATA];
44
long bios_len;
45
 
46
 
47
int main( int argc, char* argv[] ) {
48
 
49
  FILE* stream;
50
  long  offset, tmp_offset;
51
  byte  cur_val = 0, new_val = 0;
52
  int   hits;
53
 
54
 
55
  if (argc != 2) {
56
    printf( "Error. Need a file-name as an argument.\n" );
57
    exit( EXIT_FAILURE );
58
  }
59
 
60
  if ((stream = fopen(argv[1], "rb")) == NULL) {
61
    printf("Error opening %s for reading.\n", argv[1]);
62
    exit(EXIT_FAILURE);
63
  }
64
  memset(bios_data, 0, MAX_BIOS_DATA);
65
  bios_len = fread(bios_data, 1, MAX_BIOS_DATA, stream);
66
  if (bios_len >= MAX_BIOS_DATA) {
67
    printf("Error reading max. 65535 Bytes from %s.\n", argv[1]);
68
    fclose(stream);
69
    exit(EXIT_FAILURE);
70
  }
71
  fclose(stream);
72
  if (bios_len < 0x7FFF) {
73
    bios_len = 0x8000;
74
  } else {
75
    bios_len = (bios_len + 0x201) & ~0x1FF;
76
  }
77
  bios_data[2] = (byte)(bios_len / 512);
78
 
79
  hits   = 0;
80
  offset = 0L;
81
  while( (tmp_offset = chksum_pmid_get_offset( bios_data, offset )) != -1L ) {
82
    offset  = tmp_offset;
83
    cur_val = chksum_pmid_get_value(  bios_data, offset );
84
    new_val = chksum_pmid_calc_value( bios_data, offset );
85
    printf( "\nPMID entry at: 0x%4lX\n", offset  );
86
    printf( "Current checksum:     0x%02X\n",   cur_val );
87
    printf( "Calculated checksum:  0x%02X  ",   new_val );
88
    hits++;
89
  }
90
  if( hits == 1 && cur_val != new_val ) {
91
    printf( "Setting checksum." );
92
    chksum_pmid_set_value( bios_data, offset, new_val );
93
  }
94
  if( hits >= 2 ) {
95
    printf( "Multiple PMID entries! No checksum set." );
96
  }
97
  if( hits ) {
98
    printf( "\n" );
99
  }
100
 
101
 
102
  offset  = 0L;
103
  offset  = chksum_bios_get_offset( bios_data, offset );
104
  cur_val = chksum_bios_get_value(  bios_data, offset );
105
  new_val = chksum_bios_calc_value( bios_data, offset );
106
  printf( "\nBios checksum at:   0x%4lX\n", offset  );
107
  printf( "Current checksum:     0x%02X\n",   cur_val );
108
  printf( "Calculated checksum:  0x%02X  ",   new_val );
109
  if( cur_val != new_val ) {
110
    printf( "Setting checksum." );
111
    chksum_bios_set_value( bios_data, offset, new_val );
112
  }
113
  printf( "\n" );
114
 
115
 
116
  if(( stream = fopen( argv[1], "wb" )) == NULL ) {
117
    printf( "Error opening %s for writing.\n", argv[1] );
118
    exit( EXIT_FAILURE );
119
  }
120
  if( fwrite( bios_data, 1, bios_len, stream ) < bios_len ) {
121
    printf( "Error writing %d KBytes to %s.\n", bios_len / 1024, argv[1] );
122
    fclose( stream );
123
    exit( EXIT_FAILURE );
124
  }
125
  fclose( stream );
126
 
127
  return( EXIT_SUCCESS );
128
}
129
 
130
 
131
void check( int okay, char* message ) {
132
 
133
  if( !okay ) {
134
    printf( "\n\nError. %s.\n", message );
135
    exit( EXIT_FAILURE );
136
  }
137
}
138
 
139
 
140
long chksum_bios_get_offset( byte* data, long offset ) {
141
 
142
  return (bios_len - 1);
143
}
144
 
145
 
146
byte chksum_bios_calc_value( byte* data, long offset ) {
147
 
148
  int   i;
149
  byte  sum;
150
 
151
  sum = 0;
152
  for( i = 0; i < offset; i++ ) {
153
    sum = sum + *( data + i );
154
  }
155
  sum = -sum;          /* iso ensures -s + s == 0 on unsigned types */
156
  return( sum );
157
}
158
 
159
 
160
byte chksum_bios_get_value( byte* data, long offset ) {
161
 
162
  return( *( data + offset ) );
163
}
164
 
165
 
166
void chksum_bios_set_value( byte* data, long offset, byte value ) {
167
 
168
  *( data + offset ) = value;
169
}
170
 
171
 
172
byte chksum_pmid_calc_value( byte* data, long offset ) {
173
 
174
  int           i;
175
  int           len;
176
  byte sum;
177
 
178
  len = PMID_LEN;
179
  check((offset + len) <= (bios_len - 1), "PMID entry length out of bounds" );
180
  sum = 0;
181
  for( i = 0; i < len; i++ ) {
182
    if( i != PMID_CHKSUM ) {
183
      sum = sum + *( data + offset + i );
184
    }
185
  }
186
  sum = -sum;
187
  return( sum );
188
}
189
 
190
 
191
long chksum_pmid_get_offset( byte* data, long offset ) {
192
 
193
  long result = -1L;
194
 
195
  while ((offset + PMID_LEN) < (bios_len - 1)) {
196
    offset = offset + 1;
197
    if( *( data + offset + 0 ) == 'P' && \
198
        *( data + offset + 1 ) == 'M' && \
199
        *( data + offset + 2 ) == 'I' && \
200
        *( data + offset + 3 ) == 'D' ) {
201
      result = offset;
202
      break;
203
    }
204
  }
205
  return( result );
206
}
207
 
208
 
209
byte chksum_pmid_get_value( byte* data, long offset ) {
210
 
211
  check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
212
  return(  *( data + offset + PMID_CHKSUM ) );
213
}
214
 
215
 
216
void chksum_pmid_set_value( byte* data, long offset, byte value ) {
217
 
218
  check((offset + PMID_CHKSUM) <= (bios_len - 1), "PMID checksum out of bounds" );
219
  *( data + offset + PMID_CHKSUM ) = value;
220
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.