AUTHORS, util/: Drop individual copyright notices
[coreboot.git] / util / kbc1126 / kbc1126_ec_dump.c
bloba96464a76f63ebb4a834ff69489da2db01fc4b98
1 /* This file is part of the coreboot project. */
2 /*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2 of the License.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <assert.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
18 static void usage(const char *s)
20 printf("%s <rom file>\n", s);
21 exit(1);
24 static void FseekEnd(FILE *fp, long o)
26 if (fseek(fp, o, SEEK_END) != 0) {
27 puts("fseek() error!\n");
28 exit(1);
32 void dump_fw(FILE *dst, FILE *src, long offset)
34 static unsigned char buf[65536];
36 if (offset > 0)
37 offset -= 0x1000000;
39 printf("Dumping firmware at -0x%lx...", -offset);
41 FseekEnd(src, offset);
42 unsigned short len;
43 unsigned short cksum;
44 unsigned short _cksum = 0;
45 fread(&len, 2, 1, src);
46 fread(&cksum, 2, 1, src);
47 fread(buf, len, 1, src);
49 for (size_t i = 0; i < len; i++)
50 _cksum += buf[i];
52 if (_cksum == cksum) {
53 puts("checksum ok");
54 } else {
55 puts("checksum fail");
56 exit(1);
59 fwrite(&len, 2, 1, dst);
60 fwrite(&cksum, 2, 1, dst);
61 fwrite(buf, len, 1, dst);
64 int main(int argc, char *argv[])
66 if (argc != 2)
67 usage(argv[0]);
69 FILE *fp = fopen(argv[1], "rb");
71 if (fp == NULL) {
72 puts("Error opening file!");
73 exit(1);
76 char *basename = strrchr(argv[1], '/');
77 if (basename == NULL)
78 basename = argv[1];
79 else
80 basename = basename + 1;
82 int len = strlen(basename);
83 char fn1[len + 5], fn2[len + 5];
84 strcpy(fn1, basename);
85 strcpy(fn2, basename);
86 strcat(fn1, ".fw1");
87 strcat(fn2, ".fw2");
89 FILE *fw1 = fopen(fn1, "wb");
90 FILE *fw2 = fopen(fn2, "wb");
92 long romsz;
93 FseekEnd(fp, -1);
94 romsz = ftell(fp) + 1;
95 printf("size of %s: 0x%lx\n", argv[1], romsz);
97 if (romsz & 0xff) {
98 puts("The ROM size must be multiple of 0x100");
99 exit(1);
102 /* read offset of fw1 and fw2 */
103 unsigned char offs[8];
104 FseekEnd(fp, -0x100);
105 fread(offs, 8, 1, fp);
107 assert(offs[0] + offs[2] == 0xff);
108 assert(offs[1] + offs[3] == 0xff);
109 assert(offs[4] + offs[6] == 0xff);
110 assert(offs[5] + offs[7] == 0xff);
111 long offw1 = (offs[0] << 16) | (offs[1] << 8);
112 long offw2 = (offs[4] << 16) | (offs[5] << 8);
114 dump_fw(fw1, fp, offw1);
115 dump_fw(fw2, fp, offw2);
117 fclose(fp);
118 fclose(fw1);
119 fclose(fw2);
120 return 0;