1 /* This file is part of the coreboot project. */
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.
18 static void usage(const char *s
)
20 printf("%s <rom file>\n", s
);
24 static void FseekEnd(FILE *fp
, long o
)
26 if (fseek(fp
, o
, SEEK_END
) != 0) {
27 puts("fseek() error!\n");
32 void dump_fw(FILE *dst
, FILE *src
, long offset
)
34 static unsigned char buf
[65536];
39 printf("Dumping firmware at -0x%lx...", -offset
);
41 FseekEnd(src
, offset
);
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
++)
52 if (_cksum
== cksum
) {
55 puts("checksum fail");
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
[])
69 FILE *fp
= fopen(argv
[1], "rb");
72 puts("Error opening file!");
76 char *basename
= strrchr(argv
[1], '/');
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
);
89 FILE *fw1
= fopen(fn1
, "wb");
90 FILE *fw2
= fopen(fn2
, "wb");
94 romsz
= ftell(fp
) + 1;
95 printf("size of %s: 0x%lx\n", argv
[1], romsz
);
98 puts("The ROM size must be multiple of 0x100");
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
);