Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / sgabios / csum8.c
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * compute rom checksum byte
17  */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <sys/types.h>
24
25 #define MAX_SIZE 65536
26 unsigned char buf[MAX_SIZE];
27
28 int main(int argc, char **argv)
29 {
30   ssize_t fsize;
31   int i, sum, fd;
32   unsigned char csum;
33
34   if (argc < 2) {
35     fprintf(stderr, "usage: %s filename\n", argv[0]);
36     exit(1);
37   }
38   if ((fd = open(argv[1], O_RDWR)) < 0) {
39     perror(argv[1]);
40     exit(1);
41   }
42   if ((fsize = read(fd, buf, MAX_SIZE)) < 0) {
43     perror(argv[1]);
44     exit(1);
45   }
46   if (fsize >= MAX_SIZE && read(fd, &buf[MAX_SIZE - 1], 1) > 0) {
47     fprintf(stderr, "FAIL: %s is larger than %d bytes\n", argv[1], MAX_SIZE);
48     exit(1);
49   }
50   i = fsize - 2048 * (fsize / 2048);
51   if (i != 2047) {
52     fprintf(stderr, "FAIL: %s is %zd bytes, need 2K pad-1\n", argv[1], fsize);
53     exit(1);
54   }
55   for (i = sum = 0; i < fsize; i++) {
56     sum += buf[i];
57   }
58   sum &= 0xff;
59   csum = -sum & 0xff;
60   write(fd, &csum, 1);
61   close(fd);
62   fprintf(stderr, "%s: sum = 0x%02x, wrote byte 0x%02x\n", argv[1], sum, csum);
63   return 0;
64 }