lib/imd_cbmem: Remove indirection through cbmem_get_imd()
[coreboot.git] / util / kbc1126 / kbc1126_ec_dump.c
blob124a475a28eb75779bcccb29fcc746cb71638838
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2017 Iru Cai <mytbk920423@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <assert.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
21 static void usage(const char *s)
23 printf("%s <rom file>\n", s);
24 exit(1);
27 static void FseekEnd(FILE *fp, long o)
29 if (fseek(fp, o, SEEK_END) != 0) {
30 puts("fseek() error!\n");
31 exit(1);
35 void dump_fw(FILE *dst, FILE *src, long offset)
37 static unsigned char buf[65536];
39 if (offset > 0)
40 offset -= 0x1000000;
42 printf("Dumping firmware at -0x%lx...", -offset);
44 FseekEnd(src, offset);
45 unsigned short len;
46 unsigned short cksum;
47 unsigned short _cksum = 0;
48 fread(&len, 2, 1, src);
49 fread(&cksum, 2, 1, src);
50 fread(buf, len, 1, src);
52 for (size_t i = 0; i < len; i++)
53 _cksum += buf[i];
55 if (_cksum == cksum) {
56 puts("checksum ok");
57 } else {
58 puts("checksum fail");
59 exit(1);
62 fwrite(&len, 2, 1, dst);
63 fwrite(&cksum, 2, 1, dst);
64 fwrite(buf, len, 1, dst);
67 int main(int argc, char *argv[])
69 if (argc != 2)
70 usage(argv[0]);
72 FILE *fp = fopen(argv[1], "rb");
74 if (fp == NULL) {
75 puts("Error opening file!");
76 exit(1);
79 char *basename = strrchr(argv[1], '/');
80 if (basename == NULL)
81 basename = argv[1];
82 else
83 basename = basename + 1;
85 int len = strlen(basename);
86 char fn1[len + 5], fn2[len + 5];
87 strcpy(fn1, basename);
88 strcpy(fn2, basename);
89 strcat(fn1, ".fw1");
90 strcat(fn2, ".fw2");
92 FILE *fw1 = fopen(fn1, "wb");
93 FILE *fw2 = fopen(fn2, "wb");
95 long romsz;
96 FseekEnd(fp, -1);
97 romsz = ftell(fp) + 1;
98 printf("size of %s: 0x%lx\n", argv[1], romsz);
100 if (romsz & 0xff) {
101 puts("The ROM size must be multiple of 0x100");
102 exit(1);
105 /* read offset of fw1 and fw2 */
106 unsigned char offs[8];
107 FseekEnd(fp, -0x100);
108 fread(offs, 8, 1, fp);
110 assert(offs[0] + offs[2] == 0xff);
111 assert(offs[1] + offs[3] == 0xff);
112 assert(offs[4] + offs[6] == 0xff);
113 assert(offs[5] + offs[7] == 0xff);
114 long offw1 = (offs[0] << 16) | (offs[1] << 8);
115 long offw2 = (offs[4] << 16) | (offs[5] << 8);
117 dump_fw(fw1, fp, offw1);
118 dump_fw(fw2, fp, offw2);
120 fclose(fp);
121 fclose(fw1);
122 fclose(fw2);
123 return 0;