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.
21 static void usage(const char *s
)
23 printf("%s <rom file>\n", s
);
27 static void FseekEnd(FILE *fp
, long o
)
29 if (fseek(fp
, o
, SEEK_END
) != 0) {
30 puts("fseek() error!\n");
35 void dump_fw(FILE *dst
, FILE *src
, long offset
)
37 static unsigned char buf
[65536];
42 printf("Dumping firmware at -0x%lx...", -offset
);
44 FseekEnd(src
, offset
);
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
++)
55 if (_cksum
== cksum
) {
58 puts("checksum fail");
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
[])
72 FILE *fp
= fopen(argv
[1], "rb");
75 puts("Error opening file!");
79 char *basename
= strrchr(argv
[1], '/');
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
);
92 FILE *fw1
= fopen(fn1
, "wb");
93 FILE *fw2
= fopen(fn2
, "wb");
97 romsz
= ftell(fp
) + 1;
98 printf("size of %s: 0x%lx\n", argv
[1], romsz
);
101 puts("The ROM size must be multiple of 0x100");
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
);