1 /* SPDX-License-Identifier: GPL-2.0-only */
8 static void usage(const char *s
)
10 printf("%s <rom file>\n", s
);
14 static void FseekEnd(FILE *fp
, long o
)
16 if (fseek(fp
, o
, SEEK_END
) != 0) {
17 puts("fseek() error!\n");
22 void dump_fw(FILE *dst
, FILE *src
, long offset
)
24 static unsigned char buf
[65536];
29 printf("Dumping firmware at -0x%lx...", -offset
);
31 FseekEnd(src
, offset
);
34 unsigned short _cksum
= 0;
35 fread(&len
, 2, 1, src
);
36 fread(&cksum
, 2, 1, src
);
37 fread(buf
, len
, 1, src
);
39 for (size_t i
= 0; i
< len
; i
++)
42 if (_cksum
== cksum
) {
45 puts("checksum fail");
49 fwrite(&len
, 2, 1, dst
);
50 fwrite(&cksum
, 2, 1, dst
);
51 fwrite(buf
, len
, 1, dst
);
54 int main(int argc
, char *argv
[])
59 FILE *fp
= fopen(argv
[1], "rb");
62 puts("Error opening file!");
66 char *basename
= strrchr(argv
[1], '/');
70 basename
= basename
+ 1;
72 int len
= strlen(basename
);
73 char fn1
[len
+ 5], fn2
[len
+ 5];
74 strcpy(fn1
, basename
);
75 strcpy(fn2
, basename
);
79 FILE *fw1
= fopen(fn1
, "wb");
80 FILE *fw2
= fopen(fn2
, "wb");
84 romsz
= ftell(fp
) + 1;
85 printf("size of %s: 0x%lx\n", argv
[1], romsz
);
88 puts("The ROM size must be multiple of 0x100");
92 /* read offset of fw1 and fw2 */
93 unsigned char offs
[8];
95 fread(offs
, 8, 1, fp
);
97 assert(offs
[0] + offs
[2] == 0xff);
98 assert(offs
[1] + offs
[3] == 0xff);
99 assert(offs
[4] + offs
[6] == 0xff);
100 assert(offs
[5] + offs
[7] == 0xff);
101 long offw1
= (offs
[0] << 16) | (offs
[1] << 8);
102 long offw2
= (offs
[4] << 16) | (offs
[5] << 8);
104 dump_fw(fw1
, fp
, offw1
);
105 dump_fw(fw2
, fp
, offw2
);