| 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 | |
| 25 | #include <arpa/inet.h> |
| 26 | #include <netinet/in.h> |
| 27 | |
| 28 | #include "md5.h" |
| 29 | |
| 30 | #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); }) |
| 31 | |
| 32 | #define HEADER_VERSION_V1 0x01000000 |
| 33 | #define HWID_TL_MR3020_V1 0x30200001 |
| 34 | #define HWID_TL_MR3220_V1 0x32200001 |
| 35 | #define HWID_TL_MR3220_V2 0x32200002 |
| 36 | #define HWID_TL_MR3420_V1 0x34200001 |
| 37 | #define HWID_TL_WA701N_V1 0x07010001 |
| 38 | #define HWID_TL_WA7510N_V1 0x75100001 |
| 39 | #define HWID_TL_WA801ND_V1 0x08010001 |
| 40 | #define HWID_TL_WA901ND_V1 0x09010001 |
| 41 | #define HWID_TL_WA901ND_V2 0x09010002 |
| 42 | #define HWID_TL_WR703N_V1 0x07030101 |
| 43 | #define HWID_TL_WR741ND_V1 0x07410001 |
| 44 | #define HWID_TL_WR741ND_V4 0x07410004 |
| 45 | #define HWID_TL_WR740N_V1 0x07400001 |
| 46 | #define HWID_TL_WR740N_V3 0x07400003 |
| 47 | #define HWID_TL_WR743ND_V1 0x07430001 |
| 48 | #define HWID_TL_WR841N_V1_5 0x08410002 |
| 49 | #define HWID_TL_WR841ND_V3 0x08410003 |
| 50 | #define HWID_TL_WR841ND_V5 0x08410005 |
| 51 | #define HWID_TL_WR841ND_V7 0x08410007 |
| 52 | #define HWID_TL_WR941ND_V2 0x09410002 |
| 53 | #define HWID_TL_WR941ND_V4 0x09410004 |
| 54 | #define HWID_TL_WR1043ND_V1 0x10430001 |
| 55 | #define HWID_TL_WR1041N_V2 0x10410002 |
| 56 | #define HWID_TL_WR2543N_V1 0x25430001 |
| 57 | |
| 58 | #define MD5SUM_LEN 16 |
| 59 | |
| 60 | struct file_info { |
| 61 | char *file_name; /* name of the file */ |
| 62 | uint32_t file_size; /* length of the file */ |
| 63 | }; |
| 64 | |
| 65 | struct fw_header { |
| 66 | uint32_t version; /* header version */ |
| 67 | char vendor_name[24]; |
| 68 | char fw_version[36]; |
| 69 | uint32_t hw_id; /* hardware id */ |
| 70 | uint32_t hw_rev; /* hardware revision */ |
| 71 | uint32_t unk1; |
| 72 | uint8_t md5sum1[MD5SUM_LEN]; |
| 73 | uint32_t unk2; |
| 74 | uint8_t md5sum2[MD5SUM_LEN]; |
| 75 | uint32_t unk3; |
| 76 | uint32_t kernel_la; /* kernel load address */ |
| 77 | uint32_t kernel_ep; /* kernel entry point */ |
| 78 | uint32_t fw_length; /* total length of the firmware */ |
| 79 | uint32_t kernel_ofs; /* kernel data offset */ |
| 80 | uint32_t kernel_len; /* kernel data length */ |
| 81 | uint32_t rootfs_ofs; /* rootfs data offset */ |
| 82 | uint32_t rootfs_len; /* rootfs data length */ |
| 83 | uint32_t boot_ofs; /* bootloader data offset */ |
| 84 | uint32_t boot_len; /* bootloader data length */ |
| 85 | uint16_t ver_hi; |
| 86 | uint16_t ver_mid; |
| 87 | uint16_t ver_lo; |
| 88 | uint8_t pad[354]; |
| 89 | } __attribute__ ((packed)); |
| 90 | |
| 91 | struct flash_layout { |
| 92 | char *id; |
| 93 | uint32_t fw_max_len; |
| 94 | uint32_t kernel_la; |
| 95 | uint32_t kernel_ep; |
| 96 | uint32_t rootfs_ofs; |
| 97 | }; |
| 98 | |
| 99 | struct board_info { |
| 100 | char *id; |
| 101 | uint32_t hw_id; |
| 102 | uint32_t hw_rev; |
| 103 | char *layout_id; |
| 104 | }; |
| 105 | |
| 106 | /* |
| 107 | * Globals |
| 108 | */ |
| 109 | static char *ofname; |
| 110 | static char *progname; |
| 111 | static char *vendor = "TP-LINK Technologies"; |
| 112 | static char *version = "ver. 1.0"; |
| 113 | static char *fw_ver = "0.0.0"; |
| 114 | |
| 115 | static char *board_id; |
| 116 | static struct board_info *board; |
| 117 | static char *layout_id; |
| 118 | static struct flash_layout *layout; |
| 119 | static char *opt_hw_id; |
| 120 | static uint32_t hw_id; |
| 121 | static char *opt_hw_rev; |
| 122 | static uint32_t hw_rev; |
| 123 | static int fw_ver_lo; |
| 124 | static int fw_ver_mid; |
| 125 | static int fw_ver_hi; |
| 126 | static struct file_info kernel_info; |
| 127 | static uint32_t kernel_la = 0; |
| 128 | static uint32_t kernel_ep = 0; |
| 129 | static uint32_t kernel_len = 0; |
| 130 | static struct file_info rootfs_info; |
| 131 | static uint32_t rootfs_ofs = 0; |
| 132 | static uint32_t rootfs_align; |
| 133 | static struct file_info boot_info; |
| 134 | static int combined; |
| 135 | static int strip_padding; |
| 136 | static int add_jffs2_eof; |
| 137 | static unsigned char jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde}; |
| 138 | |
| 139 | static struct file_info inspect_info; |
| 140 | static int extract = 0; |
| 141 | |
| 142 | char md5salt_normal[MD5SUM_LEN] = { |
| 143 | 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb, |
| 144 | 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38, |
| 145 | }; |
| 146 | |
| 147 | char md5salt_boot[MD5SUM_LEN] = { |
| 148 | 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa, |
| 149 | 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42, |
| 150 | }; |
| 151 | |
| 152 | static struct flash_layout layouts[] = { |
| 153 | { |
| 154 | .id = "4M", |
| 155 | .fw_max_len = 0x3c0000, |
| 156 | .kernel_la = 0x80060000, |
| 157 | .kernel_ep = 0x80060000, |
| 158 | .rootfs_ofs = 0x140000, |
| 159 | }, { |
| 160 | .id = "4Mlzma", |
| 161 | .fw_max_len = 0x3c0000, |
| 162 | .kernel_la = 0x80060000, |
| 163 | .kernel_ep = 0x80060000, |
| 164 | .rootfs_ofs = 0x100000, |
| 165 | }, { |
| 166 | .id = "8M", |
| 167 | .fw_max_len = 0x7c0000, |
| 168 | .kernel_la = 0x80060000, |
| 169 | .kernel_ep = 0x80060000, |
| 170 | .rootfs_ofs = 0x140000, |
| 171 | }, { |
| 172 | .id = "8Mlzma", |
| 173 | .fw_max_len = 0x7c0000, |
| 174 | .kernel_la = 0x80060000, |
| 175 | .kernel_ep = 0x80060000, |
| 176 | .rootfs_ofs = 0x100000, |
| 177 | }, { |
| 178 | /* terminating entry */ |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | static struct board_info boards[] = { |
| 183 | { |
| 184 | .id = "TL-MR3020v1", |
| 185 | .hw_id = HWID_TL_MR3020_V1, |
| 186 | .hw_rev = 1, |
| 187 | .layout_id = "4Mlzma", |
| 188 | }, { |
| 189 | .id = "TL-MR3220v1", |
| 190 | .hw_id = HWID_TL_MR3220_V1, |
| 191 | .hw_rev = 1, |
| 192 | .layout_id = "4M", |
| 193 | }, { |
| 194 | .id = "TL-MR3220v2", |
| 195 | .hw_id = HWID_TL_MR3220_V2, |
| 196 | .hw_rev = 1, |
| 197 | .layout_id = "4Mlzma", |
| 198 | }, { |
| 199 | .id = "TL-MR3420v1", |
| 200 | .hw_id = HWID_TL_MR3420_V1, |
| 201 | .hw_rev = 1, |
| 202 | .layout_id = "4M", |
| 203 | }, { |
| 204 | .id = "TL-WA701Nv1", |
| 205 | .hw_id = HWID_TL_WA701N_V1, |
| 206 | .hw_rev = 1, |
| 207 | .layout_id = "4M", |
| 208 | }, { |
| 209 | .id = "TL-WA7510N", |
| 210 | .hw_id = HWID_TL_WA7510N_V1, |
| 211 | .hw_rev = 1, |
| 212 | .layout_id = "4M", |
| 213 | }, { |
| 214 | .id = "TL-WA801NDv1", |
| 215 | .hw_id = HWID_TL_WA801ND_V1, |
| 216 | .hw_rev = 1, |
| 217 | .layout_id = "4M", |
| 218 | }, { |
| 219 | .id = "TL-WA901NDv1", |
| 220 | .hw_id = HWID_TL_WA901ND_V1, |
| 221 | .hw_rev = 1, |
| 222 | .layout_id = "4M", |
| 223 | }, { |
| 224 | .id = "TL-WA901NDv2", |
| 225 | .hw_id = HWID_TL_WA901ND_V2, |
| 226 | .hw_rev = 1, |
| 227 | .layout_id = "4M", |
| 228 | }, { |
| 229 | .id = "TL-WR741NDv1", |
| 230 | .hw_id = HWID_TL_WR741ND_V1, |
| 231 | .hw_rev = 1, |
| 232 | .layout_id = "4M", |
| 233 | }, { |
| 234 | .id = "TL-WR741NDv4", |
| 235 | .hw_id = HWID_TL_WR741ND_V4, |
| 236 | .hw_rev = 1, |
| 237 | .layout_id = "4Mlzma", |
| 238 | }, { |
| 239 | .id = "TL-WR740Nv1", |
| 240 | .hw_id = HWID_TL_WR740N_V1, |
| 241 | .hw_rev = 1, |
| 242 | .layout_id = "4M", |
| 243 | }, { |
| 244 | .id = "TL-WR740Nv3", |
| 245 | .hw_id = HWID_TL_WR740N_V3, |
| 246 | .hw_rev = 1, |
| 247 | .layout_id = "4M", |
| 248 | }, { |
| 249 | .id = "TL-WR743NDv1", |
| 250 | .hw_id = HWID_TL_WR743ND_V1, |
| 251 | .hw_rev = 1, |
| 252 | .layout_id = "4M", |
| 253 | }, { |
| 254 | .id = "TL-WR841Nv1.5", |
| 255 | .hw_id = HWID_TL_WR841N_V1_5, |
| 256 | .hw_rev = 2, |
| 257 | .layout_id = "4M", |
| 258 | }, { |
| 259 | .id = "TL-WR841NDv3", |
| 260 | .hw_id = HWID_TL_WR841ND_V3, |
| 261 | .hw_rev = 3, |
| 262 | .layout_id = "4M", |
| 263 | }, { |
| 264 | .id = "TL-WR841NDv5", |
| 265 | .hw_id = HWID_TL_WR841ND_V5, |
| 266 | .hw_rev = 1, |
| 267 | .layout_id = "4M", |
| 268 | }, { |
| 269 | .id = "TL-WR841NDv7", |
| 270 | .hw_id = HWID_TL_WR841ND_V7, |
| 271 | .hw_rev = 1, |
| 272 | .layout_id = "4M", |
| 273 | }, { |
| 274 | .id = "TL-WR941NDv2", |
| 275 | .hw_id = HWID_TL_WR941ND_V2, |
| 276 | .hw_rev = 2, |
| 277 | .layout_id = "4M", |
| 278 | }, { |
| 279 | .id = "TL-WR941NDv4", |
| 280 | .hw_id = HWID_TL_WR941ND_V4, |
| 281 | .hw_rev = 1, |
| 282 | .layout_id = "4M", |
| 283 | }, { |
| 284 | .id = "TL-WR1041Nv2", |
| 285 | .hw_id = HWID_TL_WR1041N_V2, |
| 286 | .hw_rev = 1, |
| 287 | .layout_id = "4Mlzma", |
| 288 | }, { |
| 289 | .id = "TL-WR1043NDv1", |
| 290 | .hw_id = HWID_TL_WR1043ND_V1, |
| 291 | .hw_rev = 1, |
| 292 | .layout_id = "8M", |
| 293 | }, { |
| 294 | .id = "TL-WR2543Nv1", |
| 295 | .hw_id = HWID_TL_WR2543N_V1, |
| 296 | .hw_rev = 1, |
| 297 | .layout_id = "8Mlzma", |
| 298 | }, { |
| 299 | .id = "TL-WR703Nv1", |
| 300 | .hw_id = HWID_TL_WR703N_V1, |
| 301 | .hw_rev = 1, |
| 302 | .layout_id = "4Mlzma", |
| 303 | }, { |
| 304 | /* terminating entry */ |
| 305 | } |
| 306 | }; |
| 307 | |
| 308 | /* |
| 309 | * Message macros |
| 310 | */ |
| 311 | #define ERR(fmt, ...) do { \ |
| 312 | fflush(0); \ |
| 313 | fprintf(stderr, "[%s] *** error: " fmt "\n", \ |
| 314 | progname, ## __VA_ARGS__ ); \ |
| 315 | } while (0) |
| 316 | |
| 317 | #define ERRS(fmt, ...) do { \ |
| 318 | int save = errno; \ |
| 319 | fflush(0); \ |
| 320 | fprintf(stderr, "[%s] *** error: " fmt "\n", \ |
| 321 | progname, ## __VA_ARGS__, strerror(save)); \ |
| 322 | } while (0) |
| 323 | |
| 324 | #define DBG(fmt, ...) do { \ |
| 325 | fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \ |
| 326 | } while (0) |
| 327 | |
| 328 | static struct board_info *find_board(char *id) |
| 329 | { |
| 330 | struct board_info *ret; |
| 331 | struct board_info *board; |
| 332 | |
| 333 | ret = NULL; |
| 334 | for (board = boards; board->id != NULL; board++){ |
| 335 | if (strcasecmp(id, board->id) == 0) { |
| 336 | ret = board; |
| 337 | break; |
| 338 | } |
| 339 | }; |
| 340 | |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | static struct board_info *find_board_by_hwid(uint32_t hw_id) |
| 345 | { |
| 346 | struct board_info *board; |
| 347 | |
| 348 | for (board = boards; board->id != NULL; board++) { |
| 349 | if (hw_id == board->hw_id) |
| 350 | return board; |
| 351 | }; |
| 352 | |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | static struct flash_layout *find_layout(char *id) |
| 357 | { |
| 358 | struct flash_layout *ret; |
| 359 | struct flash_layout *l; |
| 360 | |
| 361 | ret = NULL; |
| 362 | for (l = layouts; l->id != NULL; l++){ |
| 363 | if (strcasecmp(id, l->id) == 0) { |
| 364 | ret = l; |
| 365 | break; |
| 366 | } |
| 367 | }; |
| 368 | |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | static void usage(int status) |
| 373 | { |
| 374 | FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout; |
| 375 | struct board_info *board; |
| 376 | |
| 377 | fprintf(stream, "Usage: %s [OPTIONS...]\n", progname); |
| 378 | fprintf(stream, |
| 379 | "\n" |
| 380 | "Options:\n" |
| 381 | " -B <board> create image for the board specified with <board>\n" |
| 382 | " -c use combined kernel image\n" |
| 383 | " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n" |
| 384 | " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n" |
| 385 | " -H <hwid> use hardware id specified with <hwid>\n" |
| 386 | " -F <id> use flash layout specified with <id>\n" |
| 387 | " -k <file> read kernel image from the file <file>\n" |
| 388 | " -r <file> read rootfs image from the file <file>\n" |
| 389 | " -a <align> align the rootfs start on an <align> bytes boundary\n" |
| 390 | " -R <offset> overwrite rootfs offset with <offset> (hexval prefixed with 0x)\n" |
| 391 | " -o <file> write output to the file <file>\n" |
| 392 | " -s strip padding from the end of the image\n" |
| 393 | " -j add jffs2 end-of-filesystem markers\n" |
| 394 | " -N <vendor> set image vendor to <vendor>\n" |
| 395 | " -V <version> set image version to <version>\n" |
| 396 | " -v <version> set firmware version to <version>\n" |
| 397 | " -i <file> inspect given firmware file <file>\n" |
| 398 | " -x extract kernel and rootfs while inspecting (requires -i)\n" |
| 399 | " -h show this screen\n" |
| 400 | ); |
| 401 | |
| 402 | exit(status); |
| 403 | } |
| 404 | |
| 405 | static int get_md5(char *data, int size, char *md5) |
| 406 | { |
| 407 | MD5_CTX ctx; |
| 408 | |
| 409 | MD5_Init(&ctx); |
| 410 | MD5_Update(&ctx, data, size); |
| 411 | MD5_Final(md5, &ctx); |
| 412 | } |
| 413 | |
| 414 | static int get_file_stat(struct file_info *fdata) |
| 415 | { |
| 416 | struct stat st; |
| 417 | int res; |
| 418 | |
| 419 | if (fdata->file_name == NULL) |
| 420 | return 0; |
| 421 | |
| 422 | res = stat(fdata->file_name, &st); |
| 423 | if (res){ |
| 424 | ERRS("stat failed on %s", fdata->file_name); |
| 425 | return res; |
| 426 | } |
| 427 | |
| 428 | fdata->file_size = st.st_size; |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static int read_to_buf(struct file_info *fdata, char *buf) |
| 433 | { |
| 434 | FILE *f; |
| 435 | int ret = EXIT_FAILURE; |
| 436 | |
| 437 | f = fopen(fdata->file_name, "r"); |
| 438 | if (f == NULL) { |
| 439 | ERRS("could not open \"%s\" for reading", fdata->file_name); |
| 440 | goto out; |
| 441 | } |
| 442 | |
| 443 | errno = 0; |
| 444 | fread(buf, fdata->file_size, 1, f); |
| 445 | if (errno != 0) { |
| 446 | ERRS("unable to read from file \"%s\"", fdata->file_name); |
| 447 | goto out_close; |
| 448 | } |
| 449 | |
| 450 | ret = EXIT_SUCCESS; |
| 451 | |
| 452 | out_close: |
| 453 | fclose(f); |
| 454 | out: |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | static int check_options(void) |
| 459 | { |
| 460 | int ret; |
| 461 | |
| 462 | if (inspect_info.file_name) { |
| 463 | ret = get_file_stat(&inspect_info); |
| 464 | if (ret) |
| 465 | return ret; |
| 466 | |
| 467 | return 0; |
| 468 | } else if (extract) { |
| 469 | ERR("no firmware for inspection specified"); |
| 470 | return -1; |
| 471 | } |
| 472 | |
| 473 | if (board_id == NULL && opt_hw_id == NULL) { |
| 474 | ERR("either board or hardware id must be specified"); |
| 475 | return -1; |
| 476 | } |
| 477 | |
| 478 | if (board_id) { |
| 479 | board = find_board(board_id); |
| 480 | if (board == NULL) { |
| 481 | ERR("unknown/unsupported board id \"%s\"", board_id); |
| 482 | return -1; |
| 483 | } |
| 484 | if (layout_id == NULL) |
| 485 | layout_id = board->layout_id; |
| 486 | |
| 487 | hw_id = board->hw_id; |
| 488 | hw_rev = board->hw_rev; |
| 489 | } else { |
| 490 | if (layout_id == NULL) { |
| 491 | ERR("flash layout is not specified"); |
| 492 | return -1; |
| 493 | } |
| 494 | hw_id = strtoul(opt_hw_id, NULL, 0); |
| 495 | |
| 496 | if (opt_hw_rev) |
| 497 | hw_rev = strtoul(opt_hw_rev, NULL, 0); |
| 498 | else |
| 499 | hw_rev = 1; |
| 500 | } |
| 501 | |
| 502 | layout = find_layout(layout_id); |
| 503 | if (layout == NULL) { |
| 504 | ERR("unknown flash layout \"%s\"", layout_id); |
| 505 | return -1; |
| 506 | } |
| 507 | |
| 508 | if (!kernel_la) |
| 509 | kernel_la = layout->kernel_la; |
| 510 | if (!kernel_ep) |
| 511 | kernel_ep = layout->kernel_ep; |
| 512 | if (!rootfs_ofs) |
| 513 | rootfs_ofs = layout->rootfs_ofs; |
| 514 | |
| 515 | if (kernel_info.file_name == NULL) { |
| 516 | ERR("no kernel image specified"); |
| 517 | return -1; |
| 518 | } |
| 519 | |
| 520 | ret = get_file_stat(&kernel_info); |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | |
| 524 | kernel_len = kernel_info.file_size; |
| 525 | |
| 526 | if (combined) { |
| 527 | if (kernel_info.file_size > |
| 528 | layout->fw_max_len - sizeof(struct fw_header)) { |
| 529 | ERR("kernel image is too big"); |
| 530 | return -1; |
| 531 | } |
| 532 | } else { |
| 533 | if (rootfs_info.file_name == NULL) { |
| 534 | ERR("no rootfs image specified"); |
| 535 | return -1; |
| 536 | } |
| 537 | |
| 538 | ret = get_file_stat(&rootfs_info); |
| 539 | if (ret) |
| 540 | return ret; |
| 541 | |
| 542 | if (rootfs_align) { |
| 543 | kernel_len += sizeof(struct fw_header); |
| 544 | kernel_len = ALIGN(kernel_len, rootfs_align); |
| 545 | kernel_len -= sizeof(struct fw_header); |
| 546 | |
| 547 | DBG("kernel length aligned to %u", kernel_len); |
| 548 | |
| 549 | if (kernel_len + rootfs_info.file_size > |
| 550 | layout->fw_max_len - sizeof(struct fw_header)) { |
| 551 | ERR("images are too big"); |
| 552 | return -1; |
| 553 | } |
| 554 | } else { |
| 555 | if (kernel_info.file_size > |
| 556 | rootfs_ofs - sizeof(struct fw_header)) { |
| 557 | ERR("kernel image is too big"); |
| 558 | return -1; |
| 559 | } |
| 560 | |
| 561 | if (rootfs_info.file_size > |
| 562 | (layout->fw_max_len - rootfs_ofs)) { |
| 563 | ERR("rootfs image is too big"); |
| 564 | return -1; |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if (ofname == NULL) { |
| 570 | ERR("no output file specified"); |
| 571 | return -1; |
| 572 | } |
| 573 | |
| 574 | ret = sscanf(fw_ver, "%d.%d.%d", &fw_ver_hi, &fw_ver_mid, &fw_ver_lo); |
| 575 | if (ret != 3) { |
| 576 | ERR("invalid firmware version '%s'", fw_ver); |
| 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static void fill_header(char *buf, int len) |
| 584 | { |
| 585 | struct fw_header *hdr = (struct fw_header *)buf; |
| 586 | |
| 587 | memset(hdr, 0, sizeof(struct fw_header)); |
| 588 | |
| 589 | hdr->version = htonl(HEADER_VERSION_V1); |
| 590 | strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name)); |
| 591 | strncpy(hdr->fw_version, version, sizeof(hdr->fw_version)); |
| 592 | hdr->hw_id = htonl(hw_id); |
| 593 | hdr->hw_rev = htonl(hw_rev); |
| 594 | |
| 595 | if (boot_info.file_size == 0) |
| 596 | memcpy(hdr->md5sum1, md5salt_normal, sizeof(hdr->md5sum1)); |
| 597 | else |
| 598 | memcpy(hdr->md5sum1, md5salt_boot, sizeof(hdr->md5sum1)); |
| 599 | |
| 600 | hdr->kernel_la = htonl(kernel_la); |
| 601 | hdr->kernel_ep = htonl(kernel_ep); |
| 602 | hdr->fw_length = htonl(layout->fw_max_len); |
| 603 | hdr->kernel_ofs = htonl(sizeof(struct fw_header)); |
| 604 | hdr->kernel_len = htonl(kernel_len); |
| 605 | if (!combined) { |
| 606 | hdr->rootfs_ofs = htonl(rootfs_ofs); |
| 607 | hdr->rootfs_len = htonl(rootfs_info.file_size); |
| 608 | } |
| 609 | |
| 610 | hdr->ver_hi = htons(fw_ver_hi); |
| 611 | hdr->ver_mid = htons(fw_ver_mid); |
| 612 | hdr->ver_lo = htons(fw_ver_lo); |
| 613 | |
| 614 | get_md5(buf, len, hdr->md5sum1); |
| 615 | } |
| 616 | |
| 617 | static int pad_jffs2(char *buf, int currlen) |
| 618 | { |
| 619 | int len; |
| 620 | uint32_t pad_mask; |
| 621 | |
| 622 | len = currlen; |
| 623 | pad_mask = (64 * 1024); |
| 624 | while ((len < layout->fw_max_len) && (pad_mask != 0)) { |
| 625 | uint32_t mask; |
| 626 | int i; |
| 627 | |
| 628 | for (i = 10; i < 32; i++) { |
| 629 | mask = 1 << i; |
| 630 | if (pad_mask & mask) |
| 631 | break; |
| 632 | } |
| 633 | |
| 634 | len = ALIGN(len, mask); |
| 635 | |
| 636 | for (i = 10; i < 32; i++) { |
| 637 | mask = 1 << i; |
| 638 | if ((len & (mask - 1)) == 0) |
| 639 | pad_mask &= ~mask; |
| 640 | } |
| 641 | |
| 642 | for (i = 0; i < sizeof(jffs2_eof_mark); i++) |
| 643 | buf[len + i] = jffs2_eof_mark[i]; |
| 644 | |
| 645 | len += sizeof(jffs2_eof_mark); |
| 646 | } |
| 647 | |
| 648 | return len; |
| 649 | } |
| 650 | |
| 651 | static int write_fw(char *data, int len) |
| 652 | { |
| 653 | FILE *f; |
| 654 | int ret = EXIT_FAILURE; |
| 655 | |
| 656 | f = fopen(ofname, "w"); |
| 657 | if (f == NULL) { |
| 658 | ERRS("could not open \"%s\" for writing", ofname); |
| 659 | goto out; |
| 660 | } |
| 661 | |
| 662 | errno = 0; |
| 663 | fwrite(data, len, 1, f); |
| 664 | if (errno) { |
| 665 | ERRS("unable to write output file"); |
| 666 | goto out_flush; |
| 667 | } |
| 668 | |
| 669 | DBG("firmware file \"%s\" completed", ofname); |
| 670 | |
| 671 | ret = EXIT_SUCCESS; |
| 672 | |
| 673 | out_flush: |
| 674 | fflush(f); |
| 675 | fclose(f); |
| 676 | if (ret != EXIT_SUCCESS) { |
| 677 | unlink(ofname); |
| 678 | } |
| 679 | out: |
| 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static int build_fw(void) |
| 684 | { |
| 685 | int buflen; |
| 686 | char *buf; |
| 687 | char *p; |
| 688 | int ret = EXIT_FAILURE; |
| 689 | int writelen = 0; |
| 690 | |
| 691 | buflen = layout->fw_max_len; |
| 692 | |
| 693 | buf = malloc(buflen); |
| 694 | if (!buf) { |
| 695 | ERR("no memory for buffer\n"); |
| 696 | goto out; |
| 697 | } |
| 698 | |
| 699 | memset(buf, 0xff, buflen); |
| 700 | p = buf + sizeof(struct fw_header); |
| 701 | ret = read_to_buf(&kernel_info, p); |
| 702 | if (ret) |
| 703 | goto out_free_buf; |
| 704 | |
| 705 | writelen = sizeof(struct fw_header) + kernel_len; |
| 706 | |
| 707 | if (!combined) { |
| 708 | if (rootfs_align) |
| 709 | p = buf + writelen; |
| 710 | else |
| 711 | p = buf + rootfs_ofs; |
| 712 | |
| 713 | ret = read_to_buf(&rootfs_info, p); |
| 714 | if (ret) |
| 715 | goto out_free_buf; |
| 716 | |
| 717 | if (rootfs_align) |
| 718 | writelen += rootfs_info.file_size; |
| 719 | else |
| 720 | writelen = rootfs_ofs + rootfs_info.file_size; |
| 721 | |
| 722 | if (add_jffs2_eof) |
| 723 | writelen = pad_jffs2(buf, writelen); |
| 724 | } |
| 725 | |
| 726 | if (!strip_padding) |
| 727 | writelen = buflen; |
| 728 | |
| 729 | fill_header(buf, writelen); |
| 730 | ret = write_fw(buf, writelen); |
| 731 | if (ret) |
| 732 | goto out_free_buf; |
| 733 | |
| 734 | ret = EXIT_SUCCESS; |
| 735 | |
| 736 | out_free_buf: |
| 737 | free(buf); |
| 738 | out: |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | /* Helper functions to inspect_fw() representing different output formats */ |
| 743 | static inline void inspect_fw_pstr(char *label, char *str) |
| 744 | { |
| 745 | printf("%-23s: %s\n", label, str); |
| 746 | } |
| 747 | |
| 748 | static inline void inspect_fw_phex(char *label, uint32_t val) |
| 749 | { |
| 750 | printf("%-23s: 0x%08x\n", label, val); |
| 751 | } |
| 752 | |
| 753 | static inline void inspect_fw_phexpost(char *label, |
| 754 | uint32_t val, char *post) |
| 755 | { |
| 756 | printf("%-23s: 0x%08x (%s)\n", label, val, post); |
| 757 | } |
| 758 | |
| 759 | static inline void inspect_fw_phexdef(char *label, |
| 760 | uint32_t val, uint32_t defval) |
| 761 | { |
| 762 | printf("%-23s: 0x%08x ", label, val); |
| 763 | |
| 764 | if (val == defval) |
| 765 | printf("(== OpenWrt default)\n"); |
| 766 | else |
| 767 | printf("(OpenWrt default: 0x%08x)\n", defval); |
| 768 | } |
| 769 | |
| 770 | static inline void inspect_fw_phexexp(char *label, |
| 771 | uint32_t val, uint32_t expval) |
| 772 | { |
| 773 | printf("%-23s: 0x%08x ", label, val); |
| 774 | |
| 775 | if (val == expval) |
| 776 | printf("(ok)\n"); |
| 777 | else |
| 778 | printf("(expected: 0x%08x)\n", expval); |
| 779 | } |
| 780 | |
| 781 | static inline void inspect_fw_phexdec(char *label, uint32_t val) |
| 782 | { |
| 783 | printf("%-23s: 0x%08x / %8u bytes\n", label, val, val); |
| 784 | } |
| 785 | |
| 786 | static inline void inspect_fw_phexdecdef(char *label, |
| 787 | uint32_t val, uint32_t defval) |
| 788 | { |
| 789 | printf("%-23s: 0x%08x / %8u bytes ", label, val, val); |
| 790 | |
| 791 | if (val == defval) |
| 792 | printf("(== OpenWrt default)\n"); |
| 793 | else |
| 794 | printf("(OpenWrt default: 0x%08x)\n", defval); |
| 795 | } |
| 796 | |
| 797 | static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text) |
| 798 | { |
| 799 | int i; |
| 800 | |
| 801 | printf("%-23s:", label); |
| 802 | for (i=0; i<MD5SUM_LEN; i++) |
| 803 | printf(" %02x", val[i]); |
| 804 | printf(" %s\n", text); |
| 805 | } |
| 806 | |
| 807 | static int inspect_fw(void) |
| 808 | { |
| 809 | char *buf; |
| 810 | struct fw_header *hdr; |
| 811 | uint8_t md5sum[MD5SUM_LEN]; |
| 812 | struct board_info *board; |
| 813 | int ret = EXIT_FAILURE; |
| 814 | |
| 815 | buf = malloc(inspect_info.file_size); |
| 816 | if (!buf) { |
| 817 | ERR("no memory for buffer!\n"); |
| 818 | goto out; |
| 819 | } |
| 820 | |
| 821 | ret = read_to_buf(&inspect_info, buf); |
| 822 | if (ret) |
| 823 | goto out_free_buf; |
| 824 | hdr = (struct fw_header *)buf; |
| 825 | |
| 826 | inspect_fw_pstr("File name", inspect_info.file_name); |
| 827 | inspect_fw_phexdec("File size", inspect_info.file_size); |
| 828 | |
| 829 | if (ntohl(hdr->version) != HEADER_VERSION_V1) { |
| 830 | ERR("file does not seem to have V1 header!\n"); |
| 831 | goto out_free_buf; |
| 832 | } |
| 833 | |
| 834 | inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header)); |
| 835 | |
| 836 | if (ntohl(hdr->unk1) != 0) |
| 837 | inspect_fw_phexdec("Unknown value 1", hdr->unk1); |
| 838 | |
| 839 | memcpy(md5sum, hdr->md5sum1, sizeof(md5sum)); |
| 840 | if (ntohl(hdr->boot_len) == 0) |
| 841 | memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum)); |
| 842 | else |
| 843 | memcpy(hdr->md5sum1, md5salt_boot, sizeof(md5sum)); |
| 844 | get_md5(buf, inspect_info.file_size, hdr->md5sum1); |
| 845 | |
| 846 | if (memcmp(md5sum, hdr->md5sum1, sizeof(md5sum))) { |
| 847 | inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(*ERROR*)"); |
| 848 | inspect_fw_pmd5sum(" --> expected", hdr->md5sum1, ""); |
| 849 | } else { |
| 850 | inspect_fw_pmd5sum("Header MD5Sum1", md5sum, "(ok)"); |
| 851 | } |
| 852 | if (ntohl(hdr->unk2) != 0) |
| 853 | inspect_fw_phexdec("Unknown value 2", hdr->unk2); |
| 854 | inspect_fw_pmd5sum("Header MD5Sum2", hdr->md5sum2, |
| 855 | "(purpose yet unknown, unchecked here)"); |
| 856 | if (ntohl(hdr->unk3) != 0) |
| 857 | inspect_fw_phexdec("Unknown value 3", hdr->unk3); |
| 858 | |
| 859 | printf("\n"); |
| 860 | |
| 861 | inspect_fw_pstr("Vendor name", hdr->vendor_name); |
| 862 | inspect_fw_pstr("Firmware version", hdr->fw_version); |
| 863 | board = find_board_by_hwid(ntohl(hdr->hw_id)); |
| 864 | if (board) { |
| 865 | layout = find_layout(board->layout_id); |
| 866 | inspect_fw_phexpost("Hardware ID", |
| 867 | ntohl(hdr->hw_id), board->id); |
| 868 | inspect_fw_phexexp("Hardware Revision", |
| 869 | ntohl(hdr->hw_rev), board->hw_rev); |
| 870 | } else { |
| 871 | inspect_fw_phexpost("Hardware ID", |
| 872 | ntohl(hdr->hw_id), "unknown"); |
| 873 | inspect_fw_phex("Hardware Revision", |
| 874 | ntohl(hdr->hw_rev)); |
| 875 | } |
| 876 | |
| 877 | printf("\n"); |
| 878 | |
| 879 | inspect_fw_phexdec("Kernel data offset", |
| 880 | ntohl(hdr->kernel_ofs)); |
| 881 | inspect_fw_phexdec("Kernel data length", |
| 882 | ntohl(hdr->kernel_len)); |
| 883 | if (board) { |
| 884 | inspect_fw_phexdef("Kernel load address", |
| 885 | ntohl(hdr->kernel_la), |
| 886 | layout ? layout->kernel_la : 0xffffffff); |
| 887 | inspect_fw_phexdef("Kernel entry point", |
| 888 | ntohl(hdr->kernel_ep), |
| 889 | layout ? layout->kernel_ep : 0xffffffff); |
| 890 | inspect_fw_phexdecdef("Rootfs data offset", |
| 891 | ntohl(hdr->rootfs_ofs), |
| 892 | layout ? layout->rootfs_ofs : 0xffffffff); |
| 893 | } else { |
| 894 | inspect_fw_phex("Kernel load address", |
| 895 | ntohl(hdr->kernel_la)); |
| 896 | inspect_fw_phex("Kernel entry point", |
| 897 | ntohl(hdr->kernel_ep)); |
| 898 | inspect_fw_phexdec("Rootfs data offset", |
| 899 | ntohl(hdr->rootfs_ofs)); |
| 900 | } |
| 901 | inspect_fw_phexdec("Rootfs data length", |
| 902 | ntohl(hdr->rootfs_len)); |
| 903 | inspect_fw_phexdec("Boot loader data offset", |
| 904 | ntohl(hdr->boot_ofs)); |
| 905 | inspect_fw_phexdec("Boot loader data length", |
| 906 | ntohl(hdr->boot_len)); |
| 907 | inspect_fw_phexdec("Total firmware length", |
| 908 | ntohl(hdr->fw_length)); |
| 909 | |
| 910 | if (extract) { |
| 911 | FILE *fp; |
| 912 | char *filename; |
| 913 | |
| 914 | printf("\n"); |
| 915 | |
| 916 | filename = malloc(strlen(inspect_info.file_name) + 8); |
| 917 | sprintf(filename, "%s-kernel", inspect_info.file_name); |
| 918 | printf("Extracting kernel to \"%s\"...\n", filename); |
| 919 | fp = fopen(filename, "w"); |
| 920 | if (fp) { |
| 921 | if (!fwrite(buf + ntohl(hdr->kernel_ofs), |
| 922 | ntohl(hdr->kernel_len), 1, fp)) { |
| 923 | ERR("error in fwrite(): %s", strerror(errno)); |
| 924 | } |
| 925 | fclose(fp); |
| 926 | } else { |
| 927 | ERR("error in fopen(): %s", strerror(errno)); |
| 928 | } |
| 929 | free(filename); |
| 930 | |
| 931 | filename = malloc(strlen(inspect_info.file_name) + 8); |
| 932 | sprintf(filename, "%s-rootfs", inspect_info.file_name); |
| 933 | printf("Extracting rootfs to \"%s\"...\n", filename); |
| 934 | fp = fopen(filename, "w"); |
| 935 | if (fp) { |
| 936 | if (!fwrite(buf + ntohl(hdr->rootfs_ofs), |
| 937 | ntohl(hdr->rootfs_len), 1, fp)) { |
| 938 | ERR("error in fwrite(): %s", strerror(errno)); |
| 939 | } |
| 940 | fclose(fp); |
| 941 | } else { |
| 942 | ERR("error in fopen(): %s", strerror(errno)); |
| 943 | } |
| 944 | free(filename); |
| 945 | } |
| 946 | |
| 947 | out_free_buf: |
| 948 | free(buf); |
| 949 | out: |
| 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | int main(int argc, char *argv[]) |
| 954 | { |
| 955 | int ret = EXIT_FAILURE; |
| 956 | int err; |
| 957 | |
| 958 | FILE *outfile; |
| 959 | |
| 960 | progname = basename(argv[0]); |
| 961 | |
| 962 | while ( 1 ) { |
| 963 | int c; |
| 964 | |
| 965 | c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xhsjv:"); |
| 966 | if (c == -1) |
| 967 | break; |
| 968 | |
| 969 | switch (c) { |
| 970 | case 'a': |
| 971 | sscanf(optarg, "0x%x", &rootfs_align); |
| 972 | break; |
| 973 | case 'B': |
| 974 | board_id = optarg; |
| 975 | break; |
| 976 | case 'H': |
| 977 | opt_hw_id = optarg; |
| 978 | break; |
| 979 | case 'E': |
| 980 | sscanf(optarg, "0x%x", &kernel_ep); |
| 981 | break; |
| 982 | case 'F': |
| 983 | layout_id = optarg; |
| 984 | break; |
| 985 | case 'W': |
| 986 | opt_hw_rev = optarg; |
| 987 | break; |
| 988 | case 'L': |
| 989 | sscanf(optarg, "0x%x", &kernel_la); |
| 990 | break; |
| 991 | case 'V': |
| 992 | version = optarg; |
| 993 | break; |
| 994 | case 'v': |
| 995 | fw_ver = optarg; |
| 996 | break; |
| 997 | case 'N': |
| 998 | vendor = optarg; |
| 999 | break; |
| 1000 | case 'c': |
| 1001 | combined++; |
| 1002 | break; |
| 1003 | case 'k': |
| 1004 | kernel_info.file_name = optarg; |
| 1005 | break; |
| 1006 | case 'r': |
| 1007 | rootfs_info.file_name = optarg; |
| 1008 | break; |
| 1009 | case 'R': |
| 1010 | sscanf(optarg, "0x%x", &rootfs_ofs); |
| 1011 | break; |
| 1012 | case 'o': |
| 1013 | ofname = optarg; |
| 1014 | break; |
| 1015 | case 's': |
| 1016 | strip_padding = 1; |
| 1017 | break; |
| 1018 | case 'i': |
| 1019 | inspect_info.file_name = optarg; |
| 1020 | break; |
| 1021 | case 'j': |
| 1022 | add_jffs2_eof = 1; |
| 1023 | break; |
| 1024 | case 'x': |
| 1025 | extract = 1; |
| 1026 | break; |
| 1027 | case 'h': |
| 1028 | usage(EXIT_SUCCESS); |
| 1029 | break; |
| 1030 | default: |
| 1031 | usage(EXIT_FAILURE); |
| 1032 | break; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | ret = check_options(); |
| 1037 | if (ret) |
| 1038 | goto out; |
| 1039 | |
| 1040 | if (!inspect_info.file_name) |
| 1041 | ret = build_fw(); |
| 1042 | else |
| 1043 | ret = inspect_fw(); |
| 1044 | |
| 1045 | out: |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | |