tools/firmware-utils: mktplinkfw move build_fw() to lib

This patch moves build_fw() to mktplinkfw-lib.c

The versions of mktplinkfw.c and mktplinkfw2.c had slight
differences in code flow, the version from mktplinkfw.c has been
preferred.

While it's expected that this change will not affect mktplinkfw2,
all use cases could not be tested and so this particular change
is committed separately.

Signed-off-by: Thibaut VARÈNE <hacks@slashdirt.org>
openwrt-18.06
Thibaut VARÈNE 2017-10-03 20:19:52 +02:00 committed by Mathias Kresin
parent 10324fcb39
commit 9e768b9aac
4 changed files with 102 additions and 148 deletions

View File

@ -30,9 +30,22 @@
#include "mktplinkfw-lib.h"
#include "md5.h"
extern char *ofname;
extern char *progname;
extern uint32_t kernel_len;
extern struct file_info kernel_info;
extern struct file_info rootfs_info;
extern struct flash_layout *layout;
extern uint32_t rootfs_ofs;
extern uint32_t rootfs_align;
extern int combined;
extern int strip_padding;
extern int add_jffs2_eof;
static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
void fill_header(char *buf, int len);
struct flash_layout *find_layout(struct flash_layout *layouts, const char *id)
{
struct flash_layout *ret;
@ -102,7 +115,7 @@ out:
return ret;
}
int pad_jffs2(char *buf, int currlen, int maxlen)
static int pad_jffs2(char *buf, int currlen, int maxlen)
{
int len;
uint32_t pad_mask;
@ -193,3 +206,66 @@ inline void inspect_fw_pmd5sum(const char *label, const uint8_t *val, const char
printf(" %02x", val[i]);
printf(" %s\n", text);
}
// header_size = sizeof(struct fw_header)
int build_fw(size_t header_size)
{
int buflen;
char *buf;
char *p;
int ret = EXIT_FAILURE;
int writelen = 0;
writelen = header_size + kernel_len;
if (combined)
buflen = writelen;
else
buflen = layout->fw_max_len;
buf = malloc(buflen);
if (!buf) {
ERR("no memory for buffer\n");
goto out;
}
memset(buf, 0xff, buflen);
p = buf + header_size;
ret = read_to_buf(&kernel_info, p);
if (ret)
goto out_free_buf;
if (!combined) {
if (rootfs_align)
p = buf + writelen;
else
p = buf + rootfs_ofs;
ret = read_to_buf(&rootfs_info, p);
if (ret)
goto out_free_buf;
if (rootfs_align)
writelen += rootfs_info.file_size;
else
writelen = rootfs_ofs + rootfs_info.file_size;
if (add_jffs2_eof)
writelen = pad_jffs2(buf, writelen, layout->fw_max_len);
}
if (!strip_padding)
writelen = buflen;
fill_header(buf, writelen);
ret = write_fw(ofname, buf, writelen);
if (ret)
goto out_free_buf;
ret = EXIT_SUCCESS;
out_free_buf:
free(buf);
out:
return ret;
}

View File

@ -58,11 +58,11 @@ struct flash_layout *find_layout(struct flash_layout *layouts, const char *id);
void get_md5(const char *data, int size, uint8_t *md5);
int get_file_stat(struct file_info *fdata);
int read_to_buf(const struct file_info *fdata, char *buf);
int pad_jffs2(char *buf, int currlen, int maxlen);
int write_fw(const char *ofname, const char *data, int len);
inline void inspect_fw_pstr(const char *label, const char *str);
inline void inspect_fw_phex(const char *label, uint32_t val);
inline void inspect_fw_phexdec(const char *label, uint32_t val);
inline void inspect_fw_pmd5sum(const char *label, const uint8_t *val, const char *text);
int build_fw(size_t header_size);
#endif /* mktplinkfw_lib_h */

View File

@ -71,7 +71,7 @@ struct fw_region {
/*
* Globals
*/
static char *ofname;
char *ofname;
char *progname;
static char *vendor = "TP-LINK Technologies";
static char *version = "ver. 1.0";
@ -79,7 +79,7 @@ static char *fw_ver = "0.0.0";
static uint32_t hdr_ver = HEADER_VERSION_V1;
static char *layout_id;
static struct flash_layout *layout;
struct flash_layout *layout;
static char *opt_hw_id;
static uint32_t hw_id;
static char *opt_hw_rev;
@ -90,17 +90,17 @@ static const struct fw_region *region;
static int fw_ver_lo;
static int fw_ver_mid;
static int fw_ver_hi;
static struct file_info kernel_info;
struct file_info kernel_info;
static uint32_t kernel_la = 0;
static uint32_t kernel_ep = 0;
static uint32_t kernel_len = 0;
static struct file_info rootfs_info;
static uint32_t rootfs_ofs = 0;
static uint32_t rootfs_align;
uint32_t kernel_len = 0;
struct file_info rootfs_info;
uint32_t rootfs_ofs = 0;
uint32_t rootfs_align;
static struct file_info boot_info;
static int combined;
static int strip_padding;
static int add_jffs2_eof;
int combined;
int strip_padding;
int add_jffs2_eof;
static uint32_t fw_max_len;
static uint32_t reserved_space;
@ -355,7 +355,7 @@ static int check_options(void)
return 0;
}
static void fill_header(char *buf, int len)
void fill_header(char *buf, int len)
{
struct fw_header *hdr = (struct fw_header *)buf;
@ -408,69 +408,6 @@ static void fill_header(char *buf, int len)
get_md5(buf, len, hdr->md5sum1);
}
static int build_fw(void)
{
int buflen;
char *buf;
char *p;
int ret = EXIT_FAILURE;
int writelen = 0;
writelen = sizeof(struct fw_header) + kernel_len;
if (combined)
buflen = writelen;
else
buflen = layout->fw_max_len;
buf = malloc(buflen);
if (!buf) {
ERR("no memory for buffer\n");
goto out;
}
memset(buf, 0xff, buflen);
p = buf + sizeof(struct fw_header);
ret = read_to_buf(&kernel_info, p);
if (ret)
goto out_free_buf;
if (!combined) {
if (rootfs_align)
p = buf + writelen;
else
p = buf + rootfs_ofs;
ret = read_to_buf(&rootfs_info, p);
if (ret)
goto out_free_buf;
if (rootfs_align)
writelen += rootfs_info.file_size;
else
writelen = rootfs_ofs + rootfs_info.file_size;
if (add_jffs2_eof)
writelen = pad_jffs2(buf, writelen, layout->fw_max_len);
}
if (!strip_padding)
writelen = buflen;
fill_header(buf, writelen);
ret = write_fw(ofname, buf, writelen);
if (ret)
goto out_free_buf;
ret = EXIT_SUCCESS;
out_free_buf:
free(buf);
out:
return ret;
}
static int inspect_fw(void)
{
char *buf;
@ -686,7 +623,7 @@ int main(int argc, char *argv[])
goto out;
if (!inspect_info.file_name)
ret = build_fw();
ret = build_fw(sizeof(struct fw_header));
else
ret = inspect_fw();

View File

@ -75,7 +75,7 @@ struct board_info {
/*
* Globals
*/
static char *ofname;
char *ofname;
char *progname;
static char *vendor = "TP-LINK Technologies";
static char *version = "ver. 1.0";
@ -87,7 +87,7 @@ static struct board_info custom_board;
static struct board_info *board;
static char *layout_id;
static struct flash_layout *layout;
struct flash_layout *layout;
static char *opt_hw_id;
static char *opt_hw_rev;
static char *opt_hw_ver_add;
@ -96,17 +96,17 @@ static int fw_ver_mid;
static int fw_ver_hi;
static int sver_lo;
static int sver_hi;
static struct file_info kernel_info;
struct file_info kernel_info;
static uint32_t kernel_la = 0;
static uint32_t kernel_ep = 0;
static uint32_t kernel_len = 0;
static struct file_info rootfs_info;
static uint32_t rootfs_ofs = 0;
static uint32_t rootfs_align;
uint32_t kernel_len = 0;
struct file_info rootfs_info;
uint32_t rootfs_ofs = 0;
uint32_t rootfs_align;
static struct file_info boot_info;
static int combined;
static int strip_padding;
static int add_jffs2_eof;
int combined;
int strip_padding;
int add_jffs2_eof;
static struct file_info inspect_info;
static int extract = 0;
@ -312,7 +312,7 @@ static int check_options(void)
return 0;
}
static void fill_header(char *buf, int len)
void fill_header(char *buf, int len)
{
struct fw_header *hdr = (struct fw_header *)buf;
unsigned ver_len;
@ -374,65 +374,6 @@ static void fill_header(char *buf, int len)
get_md5(buf, len, hdr->md5sum1);
}
static int build_fw(void)
{
int buflen;
char *buf;
char *p;
int ret = EXIT_FAILURE;
int writelen = 0;
buflen = layout->fw_max_len;
buf = malloc(buflen);
if (!buf) {
ERR("no memory for buffer\n");
goto out;
}
memset(buf, 0xff, buflen);
p = buf + sizeof(struct fw_header);
ret = read_to_buf(&kernel_info, p);
if (ret)
goto out_free_buf;
writelen = sizeof(struct fw_header) + kernel_len;
if (!combined) {
if (rootfs_align)
p = buf + writelen;
else
p = buf + rootfs_ofs;
ret = read_to_buf(&rootfs_info, p);
if (ret)
goto out_free_buf;
if (rootfs_align)
writelen += rootfs_info.file_size;
else
writelen = rootfs_ofs + rootfs_info.file_size;
if (add_jffs2_eof)
writelen = pad_jffs2(buf, writelen, layout->fw_max_len);
}
if (!strip_padding)
writelen = buflen;
fill_header(buf, writelen);
ret = write_fw(ofname, buf, writelen);
if (ret)
goto out_free_buf;
ret = EXIT_SUCCESS;
out_free_buf:
free(buf);
out:
return ret;
}
static int inspect_fw(void)
{
char *buf;
@ -671,7 +612,7 @@ int main(int argc, char *argv[])
goto out;
if (!inspect_info.file_name)
ret = build_fw();
ret = build_fw(sizeof(struct fw_header));
else
ret = inspect_fw();