Annotation of elwix/tools/mktplinkfw/mktplinkfw.c, revision 1.1.2.1
1.1.2.1 ! misho 1: /*
! 2: * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
! 3: *
! 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>
! 7: *
! 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.
! 11: *
! 12: */
! 13:
! 14: #include <stdio.h>
! 15: #include <stdlib.h>
! 16: #include <stdint.h>
! 17: #include <string.h>
! 18: #include <unistd.h> /* for unlink() */
! 19: #include <libgen.h>
! 20: #include <getopt.h> /* for getopt() */
! 21: #include <stdarg.h>
! 22: #include <errno.h>
! 23: #include <sys/stat.h>
! 24: #include <arpa/inet.h>
! 25:
! 26: #include <openssl/md5.h>
! 27:
! 28: #define HOST_TO_BE32(x) htonl(x)
! 29: #define BE32_TO_HOST(x) ntohl(x)
! 30:
! 31: /*
! 32: * This is linux specific; it completely fails to get the endianness
! 33: * correct when building on FreeBSD.
! 34: */
! 35: #if 0
! 36: #if (__BYTE_ORDER == __BIG_ENDIAN)
! 37: # define HOST_TO_BE32(x) (x)
! 38: # define BE32_TO_HOST(x) (x)
! 39: #else
! 40: # define HOST_TO_BE32(x) bswap_32(x)
! 41: # define BE32_TO_HOST(x) bswap_32(x)
! 42: #endif
! 43: #endif
! 44:
! 45: #define HEADER_VERSION_V1 0x01000000
! 46: #define HWID_TL_MR3220_V1 0x32200001
! 47: #define HWID_TL_MR3420_V1 0x34200001
! 48: #define HWID_TL_WA901ND_V1 0x09010001
! 49: #define HWID_TL_WA901ND_V2 0x09010002
! 50: #define HWID_TL_WR703N_V1 0x07030101
! 51: #define HWID_TL_WR741ND_V1 0x07410001
! 52: #define HWID_TL_WR740N_V1 0x07400001
! 53: #define HWID_TL_WR740N_V3 0x07400300
! 54: #define HWID_TL_WR743ND_V1 0x07430001
! 55: #define HWID_TL_WR841N_V1_5 0x08410002
! 56: #define HWID_TL_WR841ND_V3 0x08410003
! 57: #define HWID_TL_WR841ND_V5 0x08410005
! 58: #define HWID_TL_WR841ND_V7 0x08410007
! 59: #define HWID_TL_WR941ND_V2 0x09410002
! 60: #define HWID_TL_WR941ND_V4 0x09410004
! 61: #define HWID_TL_WR1043ND_V1 0x10430001
! 62:
! 63: #define MD5SUM_LEN 16
! 64:
! 65: struct file_info {
! 66: char *file_name; /* name of the file */
! 67: uint32_t file_size; /* length of the file */
! 68: };
! 69:
! 70: struct fw_header {
! 71: uint32_t version; /* header version */
! 72: char vendor_name[24];
! 73: char fw_version[36];
! 74: uint32_t hw_id; /* hardware id */
! 75: uint32_t hw_rev; /* hardware revision */
! 76: uint32_t unk1;
! 77: uint8_t md5sum1[MD5SUM_LEN];
! 78: uint32_t unk2;
! 79: uint8_t md5sum2[MD5SUM_LEN];
! 80: uint32_t unk3;
! 81: uint32_t kernel_la; /* kernel load address */
! 82: uint32_t kernel_ep; /* kernel entry point */
! 83: uint32_t fw_length; /* total length of the firmware */
! 84: uint32_t kernel_ofs; /* kernel data offset */
! 85: uint32_t kernel_len; /* kernel data length */
! 86: uint32_t rootfs_ofs; /* rootfs data offset */
! 87: uint32_t rootfs_len; /* rootfs data length */
! 88: uint32_t boot_ofs; /* bootloader data offset */
! 89: uint32_t boot_len; /* bootloader data length */
! 90: uint8_t pad[360];
! 91: } __attribute__ ((packed));
! 92:
! 93: struct board_info {
! 94: char *id;
! 95: uint32_t hw_id;
! 96: uint32_t hw_rev;
! 97: uint32_t fw_max_len;
! 98: uint32_t kernel_la;
! 99: uint32_t kernel_ep;
! 100: uint32_t rootfs_ofs;
! 101: };
! 102:
! 103: /*
! 104: * Globals
! 105: */
! 106: static char *ofname;
! 107: static char *progname;
! 108: static char *vendor = "TP-LINK Technologies";
! 109: static char *version = "ver. 1.0";
! 110:
! 111: static char *board_id;
! 112: static struct board_info *board;
! 113: static struct file_info kernel_info;
! 114: static uint32_t kernel_la = 0;
! 115: static uint32_t kernel_ep = 0;
! 116: static struct file_info rootfs_info;
! 117: static uint32_t rootfs_ofs = 0;
! 118: static struct file_info boot_info;
! 119: static int combined;
! 120: static int strip_padding;
! 121:
! 122: static struct file_info inspect_info;
! 123: static int extract = 0;
! 124:
! 125: char md5salt_normal[MD5SUM_LEN] = {
! 126: 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
! 127: 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
! 128: };
! 129:
! 130: char md5salt_boot[MD5SUM_LEN] = {
! 131: 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
! 132: 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
! 133: };
! 134:
! 135: static struct board_info boards[] = {
! 136: {
! 137: .id = "TL-MR3220v1",
! 138: .hw_id = HWID_TL_MR3220_V1,
! 139: .hw_rev = 1,
! 140: .fw_max_len = 0x3c0000,
! 141: .kernel_la = 0x80060000,
! 142: .kernel_ep = 0x80060000,
! 143: .rootfs_ofs = 0x140000,
! 144: }, {
! 145: .id = "TL-MR3420v1",
! 146: .hw_id = HWID_TL_MR3420_V1,
! 147: .hw_rev = 1,
! 148: .fw_max_len = 0x3c0000,
! 149: .kernel_la = 0x80060000,
! 150: .kernel_ep = 0x80060000,
! 151: .rootfs_ofs = 0x140000,
! 152: }, {
! 153: .id = "TL-WA901NDv1",
! 154: .hw_id = HWID_TL_WA901ND_V1,
! 155: .hw_rev = 1,
! 156: .fw_max_len = 0x3c0000,
! 157: .kernel_la = 0x80060000,
! 158: .kernel_ep = 0x80060000,
! 159: .rootfs_ofs = 0x140000,
! 160: }, {
! 161: .id = "TL-WA901NDv2",
! 162: .hw_id = HWID_TL_WA901ND_V2,
! 163: .hw_rev = 1,
! 164: .fw_max_len = 0x3c0000,
! 165: .kernel_la = 0x80060000,
! 166: .kernel_ep = 0x80060000,
! 167: .rootfs_ofs = 0x140000,
! 168: }, {
! 169: .id = "TL-WR741NDv1",
! 170: .hw_id = HWID_TL_WR741ND_V1,
! 171: .hw_rev = 1,
! 172: .fw_max_len = 0x3c0000,
! 173: .kernel_la = 0x80060000,
! 174: .kernel_ep = 0x80060000,
! 175: .rootfs_ofs = 0x140000,
! 176: }, {
! 177: .id = "TL-WR740Nv1",
! 178: .hw_id = HWID_TL_WR740N_V1,
! 179: .hw_rev = 1,
! 180: .fw_max_len = 0x3c0000,
! 181: .kernel_la = 0x80060000,
! 182: .kernel_ep = 0x80060000,
! 183: .rootfs_ofs = 0x140000,
! 184: }, {
! 185: .id = "TL-WR740Nv3",
! 186: .hw_id = HWID_TL_WR740N_V3,
! 187: .hw_rev = 1,
! 188: .fw_max_len = 0x3c0000,
! 189: .kernel_la = 0x80060000,
! 190: .kernel_ep = 0x80060000,
! 191: .rootfs_ofs = 0x140000,
! 192: }, {
! 193: .id = "TL-WR743NDv1",
! 194: .hw_id = HWID_TL_WR743ND_V1,
! 195: .hw_rev = 1,
! 196: .fw_max_len = 0x3c0000,
! 197: .kernel_la = 0x80060000,
! 198: .kernel_ep = 0x80060000,
! 199: .rootfs_ofs = 0x140000,
! 200: }, {
! 201: .id = "TL-WR841Nv1.5",
! 202: .hw_id = HWID_TL_WR841N_V1_5,
! 203: .hw_rev = 2,
! 204: .fw_max_len = 0x3c0000,
! 205: .kernel_la = 0x80060000,
! 206: .kernel_ep = 0x80060000,
! 207: .rootfs_ofs = 0x140000,
! 208: }, {
! 209: .id = "TL-WR841NDv3",
! 210: .hw_id = HWID_TL_WR841ND_V3,
! 211: .hw_rev = 3,
! 212: .fw_max_len = 0x3c0000,
! 213: .kernel_la = 0x80060000,
! 214: .kernel_ep = 0x80060000,
! 215: .rootfs_ofs = 0x140000,
! 216: }, {
! 217: .id = "TL-WR841NDv5",
! 218: .hw_id = HWID_TL_WR841ND_V5,
! 219: .hw_rev = 1,
! 220: .fw_max_len = 0x3c0000,
! 221: .kernel_la = 0x80060000,
! 222: .kernel_ep = 0x80060000,
! 223: .rootfs_ofs = 0x140000,
! 224: }, {
! 225: .id = "TL-WR841NDv7",
! 226: .hw_id = HWID_TL_WR841ND_V7,
! 227: .hw_rev = 1,
! 228: .fw_max_len = 0x3c0000,
! 229: .kernel_la = 0x80060000,
! 230: .kernel_ep = 0x80060000,
! 231: .rootfs_ofs = 0x140000,
! 232: }, {
! 233: .id = "TL-WR941NDv2",
! 234: .hw_id = HWID_TL_WR941ND_V2,
! 235: .hw_rev = 2,
! 236: .fw_max_len = 0x3c0000,
! 237: .kernel_la = 0x80060000,
! 238: .kernel_ep = 0x80060000,
! 239: .rootfs_ofs = 0x140000,
! 240: }, {
! 241: .id = "TL-WR941NDv4",
! 242: .hw_id = HWID_TL_WR941ND_V4,
! 243: .hw_rev = 1,
! 244: .fw_max_len = 0x3c0000,
! 245: .kernel_la = 0x80060000,
! 246: .kernel_ep = 0x80060000,
! 247: .rootfs_ofs = 0x140000,
! 248: }, {
! 249: .id = "TL-WR1043NDv1",
! 250: .hw_id = HWID_TL_WR1043ND_V1,
! 251: .hw_rev = 1,
! 252: .fw_max_len = 0x7c0000,
! 253: .kernel_la = 0x80060000,
! 254: .kernel_ep = 0x80060000,
! 255: .rootfs_ofs = 0x140000,
! 256: }, {
! 257: .id = "TL-WR703Nv1",
! 258: .hw_id = HWID_TL_WR703N_V1,
! 259: .hw_rev = 1,
! 260: .fw_max_len = 0x3c0000,
! 261: .kernel_la = 0x80060000,
! 262: .kernel_ep = 0x80060000,
! 263: .rootfs_ofs = 0x100000,
! 264: }, {
! 265: /* terminating entry */
! 266: }
! 267: };
! 268:
! 269: /*
! 270: * Message macros
! 271: */
! 272: #define ERR(fmt, ...) do { \
! 273: fflush(0); \
! 274: fprintf(stderr, "[%s] *** error: " fmt "\n", \
! 275: progname, ## __VA_ARGS__ ); \
! 276: } while (0)
! 277:
! 278: #define ERRS(fmt, ...) do { \
! 279: int save = errno; \
! 280: fflush(0); \
! 281: fprintf(stderr, "[%s] *** error: " fmt " %s\n", \
! 282: progname, ## __VA_ARGS__, strerror(save)); \
! 283: } while (0)
! 284:
! 285: #define DBG(fmt, ...) do { \
! 286: fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
! 287: } while (0)
! 288:
! 289: static struct board_info *find_board(char *id)
! 290: {
! 291: struct board_info *ret;
! 292: struct board_info *board;
! 293:
! 294: ret = NULL;
! 295: for (board = boards; board->id != NULL; board++){
! 296: if (strcasecmp(id, board->id) == 0) {
! 297: ret = board;
! 298: break;
! 299: }
! 300: };
! 301:
! 302: return ret;
! 303: }
! 304:
! 305: static struct board_info *find_board_by_hwid(uint32_t hw_id)
! 306: {
! 307: struct board_info *board;
! 308:
! 309: for (board = boards; board->id != NULL; board++) {
! 310: if (hw_id == board->hw_id)
! 311: return board;
! 312: };
! 313:
! 314: return NULL;
! 315: }
! 316:
! 317:
! 318: static void usage(int status)
! 319: {
! 320: FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
! 321: struct board_info *board;
! 322:
! 323: fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
! 324: fprintf(stream,
! 325: "\n"
! 326: "Options:\n"
! 327: " -B <board> create image for the board specified with <board>\n"
! 328: " -c use combined kernel image\n"
! 329: " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
! 330: " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
! 331: " -k <file> read kernel image from the file <file>\n"
! 332: " -r <file> read rootfs image from the file <file>\n"
! 333: " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n"
! 334: " -o <file> write output to the file <file>\n"
! 335: " -s strip padding from the end of the image\n"
! 336: " -N <vendor> set image vendor to <vendor>\n"
! 337: " -V <version> set image version to <version>\n"
! 338: " -i <file> inspect given firmware file <file>\n"
! 339: " -x extract kernel and rootfs while inspecting (requires -i)\n"
! 340: " -h show this screen\n"
! 341: );
! 342:
! 343: exit(status);
! 344: }
! 345:
! 346: static int get_md5(char *data, int size, char *md5)
! 347: {
! 348: MD5_CTX ctx;
! 349:
! 350: MD5_Init(&ctx);
! 351: MD5_Update(&ctx, data, size);
! 352: MD5_Final((u_char*) md5, &ctx);
! 353:
! 354: return 0;
! 355: }
! 356:
! 357: static int get_file_stat(struct file_info *fdata)
! 358: {
! 359: struct stat st;
! 360: int res;
! 361:
! 362: if (fdata->file_name == NULL)
! 363: return 0;
! 364:
! 365: res = stat(fdata->file_name, &st);
! 366: if (res){
! 367: ERRS("stat failed on %s", fdata->file_name);
! 368: return res;
! 369: }
! 370:
! 371: fdata->file_size = st.st_size;
! 372: return 0;
! 373: }
! 374:
! 375: static int read_to_buf(struct file_info *fdata, char *buf)
! 376: {
! 377: FILE *f;
! 378: int ret = EXIT_FAILURE;
! 379:
! 380: f = fopen(fdata->file_name, "r");
! 381: if (f == NULL) {
! 382: ERRS("could not open \"%s\" for reading", fdata->file_name);
! 383: goto out;
! 384: }
! 385:
! 386: errno = 0;
! 387: fread(buf, fdata->file_size, 1, f);
! 388: if (errno != 0) {
! 389: ERRS("unable to read from file \"%s\"", fdata->file_name);
! 390: goto out_close;
! 391: }
! 392:
! 393: ret = EXIT_SUCCESS;
! 394:
! 395: out_close:
! 396: fclose(f);
! 397: out:
! 398: return ret;
! 399: }
! 400:
! 401: static int check_options(void)
! 402: {
! 403: int ret;
! 404:
! 405: if (inspect_info.file_name) {
! 406: ret = get_file_stat(&inspect_info);
! 407: if (ret)
! 408: return ret;
! 409:
! 410: return 0;
! 411: } else if (extract) {
! 412: ERR("no firmware for inspection specified");
! 413: return -1;
! 414: }
! 415:
! 416: if (board_id == NULL) {
! 417: ERR("no board specified");
! 418: return -1;
! 419: }
! 420:
! 421: board = find_board(board_id);
! 422: if (board == NULL) {
! 423: ERR("unknown/unsupported board id \"%s\"", board_id);
! 424: return -1;
! 425: }
! 426: if (!kernel_la)
! 427: kernel_la = board->kernel_la;
! 428: if (!kernel_ep)
! 429: kernel_ep = board->kernel_ep;
! 430: if (!rootfs_ofs)
! 431: rootfs_ofs = board->rootfs_ofs;
! 432:
! 433: if (kernel_info.file_name == NULL) {
! 434: ERR("no kernel image specified");
! 435: return -1;
! 436: }
! 437:
! 438: ret = get_file_stat(&kernel_info);
! 439: if (ret)
! 440: return ret;
! 441:
! 442: if (combined) {
! 443: if (kernel_info.file_size >
! 444: board->fw_max_len - sizeof(struct fw_header)) {
! 445: ERR("kernel image is too big");
! 446: return -1;
! 447: }
! 448: } else {
! 449: if (kernel_info.file_size >
! 450: rootfs_ofs - sizeof(struct fw_header)) {
! 451: ERR("kernel image is too big");
! 452: return -1;
! 453: }
! 454: if (rootfs_info.file_name == NULL) {
! 455: ERR("no rootfs image specified");
! 456: return -1;
! 457: }
! 458:
! 459: ret = get_file_stat(&rootfs_info);
! 460: if (ret)
! 461: return ret;
! 462:
! 463: if (rootfs_info.file_size >
! 464: (board->fw_max_len - rootfs_ofs)) {
! 465: ERR("rootfs image is too big");
! 466: return -1;
! 467: }
! 468: }
! 469:
! 470: if (ofname == NULL) {
! 471: ERR("no output file specified");
! 472: return -1;
! 473: }
! 474:
! 475: return 0;
! 476: }
! 477:
! 478: static void fill_header(char *buf, int len)
! 479: {
! 480: struct fw_header *hdr = (struct fw_header *)buf;
! 481:
! 482: memset(hdr, 0, sizeof(struct fw_header));
! 483:
! 484: hdr->version = HOST_TO_BE32(HEADER_VERSION_V1);
! 485: strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
! 486: strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
! 487: hdr->hw_id = HOST_TO_BE32(board->hw_id);
! 488: hdr->hw_rev = HOST_TO_BE32(board->hw_rev);
! 489:
! 490: if (boot_info.file_size == 0)
! 491: memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1));
! 492: else
! 493: memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1));
! 494:
! 495: hdr->kernel_la = HOST_TO_BE32(kernel_la);
! 496: hdr->kernel_ep = HOST_TO_BE32(kernel_ep);
! 497: hdr->fw_length = HOST_TO_BE32(board->fw_max_len);
! 498: hdr->kernel_ofs = HOST_TO_BE32(sizeof(struct fw_header));
! 499: hdr->kernel_len = HOST_TO_BE32(kernel_info.file_size);
! 500: if (!combined) {
! 501: hdr->rootfs_ofs = HOST_TO_BE32(rootfs_ofs);
! 502: hdr->rootfs_len = HOST_TO_BE32(rootfs_info.file_size);
! 503: }
! 504:
! 505: get_md5(buf, len, (char*) hdr->md5sum1);
! 506: }
! 507:
! 508: static int write_fw(char *data, int len)
! 509: {
! 510: FILE *f;
! 511: int ret = EXIT_FAILURE;
! 512:
! 513: f = fopen(ofname, "w");
! 514: if (f == NULL) {
! 515: ERRS("could not open \"%s\" for writing", ofname);
! 516: goto out;
! 517: }
! 518:
! 519: errno = 0;
! 520: fwrite(data, len, 1, f);
! 521: if (errno) {
! 522: ERRS("unable to write output file");
! 523: goto out_flush;
! 524: }
! 525:
! 526: DBG("firmware file \"%s\" completed", ofname);
! 527:
! 528: ret = EXIT_SUCCESS;
! 529:
! 530: out_flush:
! 531: fflush(f);
! 532: fclose(f);
! 533: if (ret != EXIT_SUCCESS) {
! 534: unlink(ofname);
! 535: }
! 536: out:
! 537: return ret;
! 538: }
! 539:
! 540: static int build_fw(void)
! 541: {
! 542: int buflen;
! 543: char *buf;
! 544: char *p;
! 545: int ret = EXIT_FAILURE;
! 546: int writelen = 0;
! 547:
! 548: buflen = board->fw_max_len;
! 549:
! 550: buf = malloc(buflen);
! 551: if (!buf) {
! 552: ERR("no memory for buffer\n");
! 553: goto out;
! 554: }
! 555:
! 556: memset(buf, 0xff, buflen);
! 557: p = buf + sizeof(struct fw_header);
! 558: ret = read_to_buf(&kernel_info, p);
! 559: if (ret)
! 560: goto out_free_buf;
! 561:
! 562: writelen = kernel_info.file_size;
! 563:
! 564: if (!combined) {
! 565: p = buf + rootfs_ofs;
! 566: ret = read_to_buf(&rootfs_info, p);
! 567: if (ret)
! 568: goto out_free_buf;
! 569:
! 570: writelen = rootfs_ofs + rootfs_info.file_size;
! 571: }
! 572:
! 573: if (!strip_padding)
! 574: writelen = buflen;
! 575:
! 576: fill_header(buf, writelen);
! 577: ret = write_fw(buf, writelen);
! 578: if (ret)
! 579: goto out_free_buf;
! 580:
! 581: ret = EXIT_SUCCESS;
! 582:
! 583: out_free_buf:
! 584: free(buf);
! 585: out:
! 586: return ret;
! 587: }
! 588:
! 589: /* Helper functions to inspect_fw() representing different output formats */
! 590: static inline void inspect_fw_pstr(char *label, char *str)
! 591: {
! 592: printf("%-23s: %s\n", label, str);
! 593: }
! 594:
! 595: static inline void inspect_fw_phex(char *label, uint32_t val)
! 596: {
! 597: printf("%-23s: 0x%08x\n", label, val);
! 598: }
! 599:
! 600: static inline void inspect_fw_phexpost(char *label,
! 601: uint32_t val, char *post)
! 602: {
! 603: printf("%-23s: 0x%08x (%s)\n", label, val, post);
! 604: }
! 605:
! 606: static inline void inspect_fw_phexdef(char *label,
! 607: uint32_t val, uint32_t defval)
! 608: {
! 609: printf("%-23s: 0x%08x ", label, val);
! 610:
! 611: if (val == defval)
! 612: printf("(== OpenWrt default)\n");
! 613: else
! 614: printf("(OpenWrt default: 0x%08x)\n", defval);
! 615: }
! 616:
! 617: static inline void inspect_fw_phexexp(char *label,
! 618: uint32_t val, uint32_t expval)
! 619: {
! 620: printf("%-23s: 0x%08x ", label, val);
! 621:
! 622: if (val == expval)
! 623: printf("(ok)\n");
! 624: else
! 625: printf("(expected: 0x%08x)\n", expval);
! 626: }
! 627:
! 628: static inline void inspect_fw_phexdec(char *label, uint32_t val)
! 629: {
! 630: printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
! 631: }
! 632:
! 633: static inline void inspect_fw_phexdecdef(char *label,
! 634: uint32_t val, uint32_t defval)
! 635: {
! 636: printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
! 637:
! 638: if (val == defval)
! 639: printf("(== OpenWrt default)\n");
! 640: else
! 641: printf("(OpenWrt default: 0x%08x)\n", defval);
! 642: }
! 643:
! 644: static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
! 645: {
! 646: int i;
! 647:
! 648: printf("%-23s:", label);
! 649: for (i=0; i<MD5SUM_LEN; i++)
! 650: printf(" %02x", val[i]);
! 651: printf(" %s\n", text);
! 652: }
! 653:
! 654: static int inspect_fw(void)
! 655: {
! 656: char *buf;
! 657: struct fw_header *hdr;
! 658: uint8_t md5sum[MD5SUM_LEN];
! 659: struct board_info *board;
! 660: int ret = EXIT_FAILURE;
! 661:
! 662: buf = malloc(inspect_info.file_size);
! 663: if (!buf) {
! 664: ERR("no memory for buffer!\n");
! 665: goto out;
! 666: }
! 667:
! 668: ret = read_to_buf(&inspect_info, buf);
! 669: if (ret)
! 670: goto out_free_buf;
! 671: hdr = (struct fw_header *)buf;
! 672:
! 673: inspect_fw_pstr("File name", inspect_info.file_name);
! 674: inspect_fw_phexdec("File size", inspect_info.file_size);
! 675:
! 676: if (BE32_TO_HOST(hdr->version) != HEADER_VERSION_V1) {
! 677: ERR("file does not seem to have V1 header!\n");
! 678: goto out_free_buf;
! 679: }
! 680:
! 681: inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
! 682:
! 683: if (BE32_TO_HOST(hdr->unk1) != 0)
! 684: inspect_fw_phexdec("Unknown value 1", hdr->unk1);
! 685:
! 686: memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
! 687: if (BE32_TO_HOST(hdr->boot_len) == 0)
! 688: memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
! 689: else
! 690: memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum));
! 691: get_md5(buf, inspect_info.file_size, (char*) hdr->md5sum1);
! 692:
! 693: if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) {
! 694: inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)");
! 695: inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, "");
! 696: } else {
! 697: inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)");
! 698: }
! 699: if (BE32_TO_HOST(hdr->unk2) != 0)
! 700: inspect_fw_phexdec("Unknown value 2", hdr->unk2);
! 701: inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2,
! 702: "(purpose yet unknown, unchecked here)");
! 703: if (BE32_TO_HOST(hdr->unk3) != 0)
! 704: inspect_fw_phexdec("Unknown value 3", hdr->unk3);
! 705:
! 706: printf("\n");
! 707:
! 708: inspect_fw_pstr("Vendor name", hdr->vendor_name);
! 709: inspect_fw_pstr("Firmware version", hdr->fw_version);
! 710: board = find_board_by_hwid(BE32_TO_HOST(hdr->hw_id));
! 711: if (board) {
! 712: inspect_fw_phexpost("Hardware ID",
! 713: BE32_TO_HOST(hdr->hw_id), board->id);
! 714: inspect_fw_phexexp("Hardware Revision",
! 715: BE32_TO_HOST(hdr->hw_rev), board->hw_rev);
! 716: } else {
! 717: inspect_fw_phexpost("Hardware ID",
! 718: BE32_TO_HOST(hdr->hw_id), "unknown");
! 719: inspect_fw_phex("Hardware Revision",
! 720: BE32_TO_HOST(hdr->hw_rev));
! 721: }
! 722:
! 723: printf("\n");
! 724:
! 725: inspect_fw_phexdec("Kernel data offset",
! 726: BE32_TO_HOST(hdr->kernel_ofs));
! 727: inspect_fw_phexdec("Kernel data length",
! 728: BE32_TO_HOST(hdr->kernel_len));
! 729: if (board) {
! 730: inspect_fw_phexdef("Kernel load address",
! 731: BE32_TO_HOST(hdr->kernel_la),
! 732: board->kernel_la);
! 733: inspect_fw_phexdef("Kernel entry point",
! 734: BE32_TO_HOST(hdr->kernel_ep),
! 735: board->kernel_ep);
! 736: inspect_fw_phexdecdef("Rootfs data offset",
! 737: BE32_TO_HOST(hdr->rootfs_ofs),
! 738: board->rootfs_ofs);
! 739: } else {
! 740: inspect_fw_phex("Kernel load address",
! 741: BE32_TO_HOST(hdr->kernel_la));
! 742: inspect_fw_phex("Kernel entry point",
! 743: BE32_TO_HOST(hdr->kernel_ep));
! 744: inspect_fw_phexdec("Rootfs data offset",
! 745: BE32_TO_HOST(hdr->rootfs_ofs));
! 746: }
! 747: inspect_fw_phexdec("Rootfs data length",
! 748: BE32_TO_HOST(hdr->rootfs_len));
! 749: inspect_fw_phexdec("Boot loader data offset",
! 750: BE32_TO_HOST(hdr->boot_ofs));
! 751: inspect_fw_phexdec("Boot loader data length",
! 752: BE32_TO_HOST(hdr->boot_len));
! 753: inspect_fw_phexdec("Total firmware length",
! 754: BE32_TO_HOST(hdr->fw_length));
! 755:
! 756: if (extract) {
! 757: FILE *fp;
! 758: char *filename;
! 759:
! 760: printf("\n");
! 761:
! 762: filename = malloc(strlen(inspect_info.file_name) + 8);
! 763: sprintf(filename, "%s-kernel", inspect_info.file_name);
! 764: printf("Extracting kernel to \"%s\"...\n", filename);
! 765: fp = fopen(filename, "w");
! 766: if (fp) {
! 767: if (!fwrite(buf + BE32_TO_HOST(hdr->kernel_ofs),
! 768: BE32_TO_HOST(hdr->kernel_len), 1, fp)) {
! 769: ERR("error in fwrite(): %s", strerror(errno));
! 770: }
! 771: fclose(fp);
! 772: } else {
! 773: ERR("error in fopen(): %s", strerror(errno));
! 774: }
! 775: free(filename);
! 776:
! 777: filename = malloc(strlen(inspect_info.file_name) + 8);
! 778: sprintf(filename, "%s-rootfs", inspect_info.file_name);
! 779: printf("Extracting rootfs to \"%s\"...\n", filename);
! 780: fp = fopen(filename, "w");
! 781: if (fp) {
! 782: if (!fwrite(buf + BE32_TO_HOST(hdr->rootfs_ofs),
! 783: BE32_TO_HOST(hdr->rootfs_len), 1, fp)) {
! 784: ERR("error in fwrite(): %s", strerror(errno));
! 785: }
! 786: fclose(fp);
! 787: } else {
! 788: ERR("error in fopen(): %s", strerror(errno));
! 789: }
! 790: free(filename);
! 791: }
! 792:
! 793: out_free_buf:
! 794: free(buf);
! 795: out:
! 796: return ret;
! 797: }
! 798:
! 799: int main(int argc, char *argv[])
! 800: {
! 801: int ret = EXIT_FAILURE;
! 802: int err;
! 803:
! 804: FILE *outfile;
! 805:
! 806: progname = basename(argv[0]);
! 807:
! 808: while ( 1 ) {
! 809: int c;
! 810:
! 811: c = getopt(argc, argv, "B:E:L:V:N:ci:k:r:R:o:xhs");
! 812: if (c == -1)
! 813: break;
! 814:
! 815: switch (c) {
! 816: case 'B':
! 817: board_id = optarg;
! 818: break;
! 819: case 'E':
! 820: sscanf(optarg, "0x%x", &kernel_ep);
! 821: break;
! 822: case 'L':
! 823: sscanf(optarg, "0x%x", &kernel_la);
! 824: break;
! 825: case 'V':
! 826: version = optarg;
! 827: break;
! 828: case 'N':
! 829: vendor = optarg;
! 830: break;
! 831: case 'c':
! 832: combined++;
! 833: break;
! 834: case 'k':
! 835: kernel_info.file_name = optarg;
! 836: break;
! 837: case 'r':
! 838: rootfs_info.file_name = optarg;
! 839: break;
! 840: case 'R':
! 841: sscanf(optarg, "0x%x", &rootfs_ofs);
! 842: break;
! 843: case 'o':
! 844: ofname = optarg;
! 845: break;
! 846: case 's':
! 847: strip_padding = 1;
! 848: break;
! 849: case 'i':
! 850: inspect_info.file_name = optarg;
! 851: break;
! 852: case 'x':
! 853: extract = 1;
! 854: break;
! 855: case 'h':
! 856: usage(EXIT_SUCCESS);
! 857: break;
! 858: default:
! 859: usage(EXIT_FAILURE);
! 860: break;
! 861: }
! 862: }
! 863:
! 864: ret = check_options();
! 865: if (ret)
! 866: goto out;
! 867:
! 868: if (!inspect_info.file_name)
! 869: ret = build_fw();
! 870: else
! 871: ret = inspect_fw();
! 872:
! 873: out:
! 874: return ret;
! 875: }
! 876:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>