5 #define IMG_SIZE 0x3e0000
7 #define KERNEL_START 0x020000
8 #define KERNEL_SIZE 0x0b0000
10 #define ROOTFS_START 0x0d0000
11 #define ROOTFS_SIZE 0x30ffb2
18 void print_usage(void)
20 fprintf(stderr
, "usage: dgfirmware [<opts>] <img>\n");
21 fprintf(stderr
, " <img> firmware image filename\n");
22 fprintf(stderr
, " <opts> -h print this message\n");
23 fprintf(stderr
, " -f fix the checksum\n");
24 fprintf(stderr
, " -x <file> extract the rootfs file to <file>\n");
25 fprintf(stderr
, " -xk <file> extract the kernel to <file>\n");
26 fprintf(stderr
, " -m <file> merge in rootfs fil\e from <file>\n");
27 fprintf(stderr
, " -k <file> merge in kernel from <file>\n");
28 fprintf(stderr
, " -w <file> write back the modified firmware\n");
32 unsigned char* read_img(const char *fname
)
38 fp
= fopen(fname
, "rb");
44 fseek(fp
, 0, SEEK_END
);
47 if (size
!= IMG_SIZE
) {
48 fprintf(stderr
, "%s: image file has wrong size\n", app_name
);
55 img
= malloc(IMG_SIZE
);
62 if (fread(img
, 1, IMG_SIZE
, fp
) != IMG_SIZE
) {
63 fprintf(stderr
, "%s: can't read image file\n", app_name
);
73 void write_img(unsigned char* img
, const char *fname
)
77 fp
= fopen(fname
, "wb");
83 if (fwrite(img
, 1, IMG_SIZE
, fp
) != IMG_SIZE
) {
84 fprintf(stderr
, "%s: can't write image file\n", app_name
);
91 void write_rootfs(unsigned char* img
, const char *fname
)
95 fp
= fopen(fname
, "wb");
101 if (fwrite(img
+ROOTFS_START
, 1, ROOTFS_SIZE
, fp
) != ROOTFS_SIZE
) {
102 fprintf(stderr
, "%s: can't write image file\n", app_name
);
109 void write_kernel(unsigned char* img
, const char *fname
)
113 fp
= fopen(fname
, "wb");
119 if (fwrite(img
+KERNEL_START
, 1, KERNEL_SIZE
, fp
) != KERNEL_SIZE
) {
120 fprintf(stderr
, "%s: can't write kernel file\n", app_name
);
127 unsigned char* read_rootfs(unsigned char* img
, const char *fname
)
133 for (i
=ROOTFS_START
; i
<ROOTFS_START
+ROOTFS_SIZE
; i
++)
136 fp
= fopen(fname
, "rb");
142 fseek(fp
, 0, SEEK_END
);
145 if (size
> ROOTFS_SIZE
) {
146 fprintf(stderr
, "%s: rootfs image file is too big\n", app_name
);
153 if (fread(img
+ROOTFS_START
, 1, size
, fp
) != size
) {
154 fprintf(stderr
, "%s: can't read rootfs image file\n", app_name
);
164 unsigned char* read_kernel(unsigned char* img
, const char *fname
)
170 for (i
=KERNEL_START
; i
<KERNEL_START
+KERNEL_SIZE
; i
++)
173 fp
= fopen(fname
, "rb");
179 fseek(fp
, 0, SEEK_END
);
182 if (size
> KERNEL_SIZE
) {
183 fprintf(stderr
, "%s: kernel binary file is too big\n", app_name
);
190 if (fread(img
+KERNEL_START
, 1, size
, fp
) != size
) {
191 fprintf(stderr
, "%s: can't read kernel file\n", app_name
);
201 int get_checksum(unsigned char* img
)
205 s
= img
[0x3dfffc] + (img
[0x3dfffd]<<8);
211 void set_checksum(unsigned char*img
, unsigned short sum
)
213 img
[0x3dfffc] = sum
& 0xff;
214 img
[0x3dfffd] = (sum
>>8) & 0xff;
218 int compute_checksum(unsigned char* img
)
223 for (i
=0; i
<0x3dfffc; i
++)
230 int main(int argc
, char* argv
[])
232 char *img_fname
= NULL
;
233 char *rootfs_fname
= NULL
;
234 char *kernel_fname
= NULL
;
235 char *new_img_fname
= NULL
;
237 int do_fix_checksum
= 0;
239 int do_write_rootfs
= 0;
240 int do_read_rootfs
= 0;
241 int do_write_kernel
= 0;
242 int do_read_kernel
= 0;
246 unsigned short img_checksum
;
247 unsigned short real_checksum
;
251 for (i
=1; i
<argc
; i
++) {
252 if (!strcmp(argv
[i
], "-h")) {
256 else if (!strcmp(argv
[i
], "-f")) {
259 else if (!strcmp(argv
[i
], "-x")) {
261 fprintf(stderr
, "%s: missing argument\n", app_name
);
265 rootfs_fname
= argv
[i
+1];
268 else if (!strcmp(argv
[i
], "-xk")) {
270 fprintf(stderr
, "%s: missing argument\n", app_name
);
274 kernel_fname
= argv
[i
+1];
277 else if (!strcmp(argv
[i
], "-m")) {
279 fprintf(stderr
, "%s: missing argument\n", app_name
);
283 rootfs_fname
= argv
[i
+1];
286 else if (!strcmp(argv
[i
], "-k")) {
288 fprintf(stderr
, "%s: missing argument\n", app_name
);
292 kernel_fname
= argv
[i
+1];
295 else if (!strcmp(argv
[i
], "-w")) {
297 fprintf(stderr
, "%s: missing argument\n", app_name
);
301 new_img_fname
= argv
[i
+1];
304 else if (img_fname
!= 0) {
305 fprintf(stderr
, "%s: too many arguments\n", app_name
);
313 if (img_fname
== NULL
) {
314 fprintf(stderr
, "%s: missing argument\n", app_name
);
318 if ((do_read_rootfs
&& do_write_rootfs
) ||
319 (do_read_kernel
&& do_write_kernel
)) {
320 fprintf(stderr
, "%s: conflictuous options\n", app_name
);
324 printf ("** Read firmware file\n");
325 img
= read_img(img_fname
);
327 printf ("Firmware product: %s\n", img
+0x3dffbd);
328 printf ("Firmware version: 1.%02d.%02d\n", (img
[0x3dffeb] & 0x7f), img
[0x3dffec]);
330 if (do_write_rootfs
) {
331 printf ("** Write rootfs file\n");
332 write_rootfs(img
, rootfs_fname
);
335 if (do_write_kernel
) {
336 printf ("** Write kernel file\n");
337 write_kernel(img
, kernel_fname
);
340 if (do_read_rootfs
) {
341 printf ("** Read rootfs file\n");
342 read_rootfs(img
, rootfs_fname
);
346 if (do_read_kernel
) {
347 printf ("** Read kernel file\n");
348 read_kernel(img
, kernel_fname
);
352 img_checksum
= get_checksum(img
);
353 real_checksum
= compute_checksum(img
);
355 printf ("image checksum = %04x\n", img_checksum
);
356 printf ("real checksum = %04x\n", real_checksum
);
358 if (do_fix_checksum
) {
359 if (img_checksum
!= real_checksum
) {
360 printf ("** Bad Checksum, fix it\n");
361 set_checksum(img
, real_checksum
);
364 printf ("** Checksum is correct, good\n");
369 printf ("** Write image file\n");
370 write_img(img
, new_img_fname
);
This page took 0.067469 seconds and 5 git commands to generate.