X-Git-Url: http://git.openwrt.org/?p=openwrt%2Fopenwrt.git;a=blobdiff_plain;f=tools%2Ffirmware-utils%2Fsrc%2Fmktplinkfw.c;h=c5378620500b60c7670f2e07df3e1a3dca17150e;hp=695d4f5c772057b0d1bd84167e4a68f288edbbf8;hb=b3cb0e75884222e141a40443c25d20deedf7a552;hpb=51b6448746f15d4ffccad79344fd4daf85a9a5c6 diff --git a/tools/firmware-utils/src/mktplinkfw.c b/tools/firmware-utils/src/mktplinkfw.c index 695d4f5c77..c537862050 100644 --- a/tools/firmware-utils/src/mktplinkfw.c +++ b/tools/firmware-utils/src/mktplinkfw.c @@ -19,6 +19,8 @@ #include #include /* for getopt() */ #include +#include +#include #include #include @@ -28,6 +30,7 @@ #include "md5.h" #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); }) +#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0])) #define HEADER_VERSION_V1 0x01000000 #define HEADER_VERSION_V2 0x02000000 @@ -45,7 +48,7 @@ struct fw_header { char fw_version[36]; uint32_t hw_id; /* hardware id */ uint32_t hw_rev; /* hardware revision */ - uint32_t unk1; + uint32_t region_code; /* region code */ uint8_t md5sum1[MD5SUM_LEN]; uint32_t unk2; uint8_t md5sum2[MD5SUM_LEN]; @@ -62,7 +65,10 @@ struct fw_header { uint16_t ver_hi; uint16_t ver_mid; uint16_t ver_lo; - uint8_t pad[354]; + uint8_t pad[130]; + char region_str1[32]; + char region_str2[32]; + uint8_t pad2[160]; } __attribute__ ((packed)); struct flash_layout { @@ -73,6 +79,12 @@ struct flash_layout { uint32_t rootfs_ofs; }; +struct fw_region { + char name[4]; + uint32_t code; +}; + + /* * Globals */ @@ -90,6 +102,8 @@ static uint32_t hw_id; static char *opt_hw_rev; static uint32_t hw_rev; static uint32_t opt_hdr_ver = 1; +static char *country; +static const struct fw_region *region; static int fw_ver_lo; static int fw_ver_mid; static int fw_ver_hi; @@ -103,7 +117,6 @@ static uint32_t rootfs_align; static struct file_info boot_info; static int combined; static int strip_padding; -static int ignore_size; static int add_jffs2_eof; static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde}; static uint32_t fw_max_len; @@ -111,6 +124,7 @@ static uint32_t reserved_space; static struct file_info inspect_info; static int extract = 0; +static bool endian_swap = false; static const char md5salt_normal[MD5SUM_LEN] = { 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb, @@ -170,6 +184,12 @@ static struct flash_layout layouts[] = { } }; +static const struct fw_region regions[] = { + /* Default region (universal) uses code 0 as well */ + {"US", 1}, + {"EU", 0}, +}; + /* * Message macros */ @@ -206,6 +226,17 @@ static struct flash_layout *find_layout(const char *id) return ret; } +static const struct fw_region * find_region(const char *country) { + size_t i; + + for (i = 0; i < ARRAY_SIZE(regions); i++) { + if (strcasecmp(regions[i].name, country) == 0) + return ®ions[i]; + } + + return NULL; +} + static void usage(int status) { fprintf(stderr, "Usage: %s [OPTIONS...]\n", progname); @@ -213,10 +244,12 @@ static void usage(int status) "\n" "Options:\n" " -c use combined kernel image\n" +" -e swap endianness in kernel load address and entry point\n" " -E overwrite kernel entry point with (hexval prefixed with 0x)\n" " -L overwrite kernel load address with (hexval prefixed with 0x)\n" " -H use hardware id specified with \n" " -W use hardware revision specified with \n" +" -C set region code to \n" " -F use flash layout specified with \n" " -k read kernel image from the file \n" " -r read rootfs image from the file \n" @@ -224,7 +257,6 @@ static void usage(int status) " -R overwrite rootfs offset with (hexval prefixed with 0x)\n" " -o write output to the file \n" " -s strip padding from the end of the image\n" -" -S ignore firmware size limit (only for combined images)\n" " -j add jffs2 end-of-filesystem markers\n" " -N set image vendor to \n" " -V set image version to \n" @@ -314,7 +346,7 @@ static int check_options(void) } hw_id = strtoul(opt_hw_id, NULL, 0); - if (layout_id == NULL) { + if (!combined && layout_id == NULL) { ERR("flash layout is not specified"); return -1; } @@ -324,25 +356,38 @@ static int check_options(void) else hw_rev = 1; - layout = find_layout(layout_id); - if (layout == NULL) { - ERR("unknown flash layout \"%s\"", layout_id); - return -1; + if (country) { + region = find_region(country); + if (!region) { + ERR("unknown region code \"%s\"", country); + return -1; + } } - if (!kernel_la) - kernel_la = layout->kernel_la; - if (!kernel_ep) - kernel_ep = layout->kernel_ep; - if (!rootfs_ofs) - rootfs_ofs = layout->rootfs_ofs; + if (combined) { + if (!kernel_la || !kernel_ep) { + ERR("kernel loading address and entry point must be specified for combined image"); + return -1; + } + } else { + layout = find_layout(layout_id); + if (layout == NULL) { + ERR("unknown flash layout \"%s\"", layout_id); + return -1; + } - if (reserved_space > layout->fw_max_len) { - ERR("reserved space is not valid"); - return -1; - } + if (!kernel_la) + kernel_la = layout->kernel_la; + if (!kernel_ep) + kernel_ep = layout->kernel_ep; + if (!rootfs_ofs) + rootfs_ofs = layout->rootfs_ofs; - fw_max_len = layout->fw_max_len - reserved_space; + if (reserved_space > layout->fw_max_len) { + ERR("reserved space is not valid"); + return -1; + } + } if (kernel_info.file_name == NULL) { ERR("no kernel image specified"); @@ -355,18 +400,9 @@ static int check_options(void) kernel_len = kernel_info.file_size; - if (combined) { - exceed_bytes = kernel_info.file_size - (fw_max_len - sizeof(struct fw_header)); - if (exceed_bytes > 0) { - if (!ignore_size) { - ERR("kernel image is too big by %i bytes", exceed_bytes); - return -1; - } - layout->fw_max_len = sizeof(struct fw_header) + - kernel_info.file_size + - reserved_space; - } - } else { + if (!combined) { + fw_max_len = layout->fw_max_len - reserved_space; + if (rootfs_info.file_name == NULL) { ERR("no rootfs image specified"); return -1; @@ -438,17 +474,18 @@ static void fill_header(char *buf, int len) hdr->hw_id = htonl(hw_id); hdr->hw_rev = htonl(hw_rev); - if (boot_info.file_size == 0) - memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1)); - else - memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1)); - hdr->kernel_la = htonl(kernel_la); hdr->kernel_ep = htonl(kernel_ep); - hdr->fw_length = htonl(layout->fw_max_len); hdr->kernel_ofs = htonl(sizeof(struct fw_header)); hdr->kernel_len = htonl(kernel_len); + if (!combined) { + if (boot_info.file_size == 0) + memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1)); + else + memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1)); + + hdr->fw_length = htonl(layout->fw_max_len); hdr->rootfs_ofs = htonl(rootfs_ofs); hdr->rootfs_len = htonl(rootfs_info.file_size); } @@ -457,7 +494,25 @@ static void fill_header(char *buf, int len) hdr->ver_mid = htons(fw_ver_mid); hdr->ver_lo = htons(fw_ver_lo); - get_md5(buf, len, hdr->md5sum1); + if (region) { + hdr->region_code = htonl(region->code); + snprintf( + hdr->region_str1, sizeof(hdr->region_str1), "00000000;%02X%02X%02X%02X;", + region->name[0], region->name[1], region->name[2], region->name[3] + ); + snprintf( + hdr->region_str2, sizeof(hdr->region_str2), "%02X%02X%02X%02X", + region->name[0], region->name[1], region->name[2], region->name[3] + ); + } + + if (endian_swap) { + hdr->kernel_la = bswap_32(hdr->kernel_la); + hdr->kernel_ep = bswap_32(hdr->kernel_ep); + } + + if (!combined) + get_md5(buf, len, hdr->md5sum1); } static int pad_jffs2(char *buf, int currlen) @@ -534,7 +589,12 @@ static int build_fw(void) int ret = EXIT_FAILURE; int writelen = 0; - buflen = layout->fw_max_len; + writelen = sizeof(struct fw_header) + kernel_len; + + if (combined) + buflen = writelen; + else + buflen = layout->fw_max_len; buf = malloc(buflen); if (!buf) { @@ -548,7 +608,6 @@ static int build_fw(void) if (ret) goto out_free_buf; - writelen = sizeof(struct fw_header) + kernel_len; if (!combined) { if (rootfs_align) @@ -596,11 +655,6 @@ static inline void inspect_fw_phex(const char *label, uint32_t val) printf("%-23s: 0x%08x\n", label, val); } -static inline void inspect_fw_phexpost(const char *label, uint32_t val, const char *post) -{ - printf("%-23s: 0x%08x (%s)\n", label, val, post); -} - static inline void inspect_fw_phexdec(const char *label, uint32_t val) { printf("%-23s: 0x%08x / %8u bytes\n", label, val, val); @@ -645,9 +699,6 @@ static int inspect_fw(void) inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header)); - if (ntohl(hdr->unk1) != 0) - inspect_fw_phexdec("Unknown value 1", hdr->unk1); - memcpy(md5sum, hdr->md5sum1, sizeof(md5sum)); if (ntohl(hdr->boot_len) == 0) memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum)); @@ -674,6 +725,7 @@ static int inspect_fw(void) inspect_fw_pstr("Firmware version", hdr->fw_version); inspect_fw_phex("Hardware ID", ntohl(hdr->hw_id)); inspect_fw_phex("Hardware Revision", ntohl(hdr->hw_rev)); + inspect_fw_phex("Region code", ntohl(hdr->region_code)); printf("\n"); @@ -748,7 +800,7 @@ int main(int argc, char *argv[]) while ( 1 ) { int c; - c = getopt(argc, argv, "a:H:E:F:L:m:V:N:W:ci:k:r:R:o:xX:hsSjv:"); + c = getopt(argc, argv, "a:H:E:F:L:m:V:N:W:C:ci:k:r:R:o:xX:ehsjv:"); if (c == -1) break; @@ -768,6 +820,9 @@ int main(int argc, char *argv[]) case 'W': opt_hw_rev = optarg; break; + case 'C': + country = optarg; + break; case 'L': sscanf(optarg, "0x%x", &kernel_la); break; @@ -801,9 +856,6 @@ int main(int argc, char *argv[]) case 's': strip_padding = 1; break; - case 'S': - ignore_size = 1; - break; case 'i': inspect_info.file_name = optarg; break; @@ -813,6 +865,9 @@ int main(int argc, char *argv[]) case 'x': extract = 1; break; + case 'e': + endian_swap = true; + break; case 'h': usage(EXIT_SUCCESS); break;