2 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
4 * This tool was based on:
5 * TP-Link WR941 V2 firmware checksum fixing tool.
6 * Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
18 #include <unistd.h> /* for unlink() */
20 #include <getopt.h> /* for getopt() */
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
30 #include "mktplinkfw-lib.h"
33 extern char *progname
;
34 static unsigned char jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
36 struct flash_layout
*find_layout(struct flash_layout
*layouts
, const char *id
)
38 struct flash_layout
*ret
;
39 struct flash_layout
*l
;
42 for (l
= layouts
; l
->id
!= NULL
; l
++){
43 if (strcasecmp(id
, l
->id
) == 0) {
52 void get_md5(const char *data
, int size
, uint8_t *md5
)
57 MD5_Update(&ctx
, data
, size
);
61 int get_file_stat(struct file_info
*fdata
)
66 if (fdata
->file_name
== NULL
)
69 res
= stat(fdata
->file_name
, &st
);
71 ERRS("stat failed on %s", fdata
->file_name
);
75 fdata
->file_size
= st
.st_size
;
79 int read_to_buf(const struct file_info
*fdata
, char *buf
)
82 int ret
= EXIT_FAILURE
;
84 f
= fopen(fdata
->file_name
, "r");
86 ERRS("could not open \"%s\" for reading", fdata
->file_name
);
91 fread(buf
, fdata
->file_size
, 1, f
);
93 ERRS("unable to read from file \"%s\"", fdata
->file_name
);
105 int pad_jffs2(char *buf
, int currlen
, int maxlen
)
111 pad_mask
= (64 * 1024);
112 while ((len
< maxlen
) && (pad_mask
!= 0)) {
116 for (i
= 10; i
< 32; i
++) {
122 len
= ALIGN(len
, mask
);
124 for (i
= 10; i
< 32; i
++) {
126 if ((len
& (mask
- 1)) == 0)
130 for (i
= 0; i
< sizeof(jffs2_eof_mark
); i
++)
131 buf
[len
+ i
] = jffs2_eof_mark
[i
];
133 len
+= sizeof(jffs2_eof_mark
);
139 int write_fw(const char *ofname
, const char *data
, int len
)
142 int ret
= EXIT_FAILURE
;
144 f
= fopen(ofname
, "w");
146 ERRS("could not open \"%s\" for writing", ofname
);
151 fwrite(data
, len
, 1, f
);
153 ERRS("unable to write output file");
157 DBG("firmware file \"%s\" completed", ofname
);
164 if (ret
!= EXIT_SUCCESS
) {
171 /* Helper functions to inspect_fw() representing different output formats */
172 inline void inspect_fw_pstr(const char *label
, const char *str
)
174 printf("%-23s: %s\n", label
, str
);
177 inline void inspect_fw_phex(const char *label
, uint32_t val
)
179 printf("%-23s: 0x%08x\n", label
, val
);
182 inline void inspect_fw_phexdec(const char *label
, uint32_t val
)
184 printf("%-23s: 0x%08x / %8u bytes\n", label
, val
, val
);
187 inline void inspect_fw_pmd5sum(const char *label
, const uint8_t *val
, const char *text
)
191 printf("%-23s:", label
);
192 for (i
=0; i
<MD5SUM_LEN
; i
++)
193 printf(" %02x", val
[i
]);
194 printf(" %s\n", text
);