2 Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 Image generation tool for the TP-LINK SafeLoader as seen on
31 TP-LINK Pharos devices (CPE210/220/510/520)
45 #include <arpa/inet.h>
47 #include <sys/types.h>
54 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
57 #define MAX_PARTITIONS 32
59 /** An image partition table entry */
60 struct image_partition_entry
{
66 /** A flash partition table entry */
67 struct flash_partition_entry
{
73 /** Firmware layout description */
77 const char *support_list
;
80 const struct flash_partition_entry partitions
[MAX_PARTITIONS
+1];
81 const char *first_sysupgrade_partition
;
82 const char *last_sysupgrade_partition
;
85 /** The content of the soft-version structure */
86 struct __attribute__((__packed__
)) soft_version
{
90 uint8_t version_major
;
91 uint8_t version_minor
;
92 uint8_t version_patch
;
102 static const uint8_t jffs2_eof_mark
[4] = {0xde, 0xad, 0xc0, 0xde};
106 Salt for the MD5 hash
108 Fortunately, TP-LINK seems to use the same salt for most devices which use
109 the new image format.
111 static const uint8_t md5_salt
[16] = {
112 0x7a, 0x2b, 0x15, 0xed,
113 0x9b, 0x98, 0x59, 0x6d,
114 0xe5, 0x04, 0xab, 0x44,
115 0xac, 0x2a, 0x9f, 0x4e,
119 /** Firmware layout table */
120 static struct device_info boards
[] = {
121 /** Firmware layout for the CPE210/220 */
124 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
127 "CPE210(TP-LINK|UN|N300-2):1.0\r\n"
128 "CPE210(TP-LINK|UN|N300-2):1.1\r\n"
129 "CPE210(TP-LINK|US|N300-2):1.1\r\n"
130 "CPE210(TP-LINK|EU|N300-2):1.1\r\n"
131 "CPE220(TP-LINK|UN|N300-2):1.1\r\n"
132 "CPE220(TP-LINK|US|N300-2):1.1\r\n"
133 "CPE220(TP-LINK|EU|N300-2):1.1\r\n",
134 .support_trail
= '\xff',
138 {"fs-uboot", 0x00000, 0x20000},
139 {"partition-table", 0x20000, 0x02000},
140 {"default-mac", 0x30000, 0x00020},
141 {"product-info", 0x31100, 0x00100},
142 {"signature", 0x32000, 0x00400},
143 {"os-image", 0x40000, 0x1c0000},
144 {"file-system", 0x200000, 0x5b0000},
145 {"soft-version", 0x7b0000, 0x00100},
146 {"support-list", 0x7b1000, 0x00400},
147 {"user-config", 0x7c0000, 0x10000},
148 {"default-config", 0x7d0000, 0x10000},
149 {"log", 0x7e0000, 0x10000},
150 {"radio", 0x7f0000, 0x10000},
154 .first_sysupgrade_partition
= "os-image",
155 .last_sysupgrade_partition
= "support-list",
158 /** Firmware layout for the CPE510/520 */
161 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
164 "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
165 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
166 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
167 "CPE510(TP-LINK|US|N300-5):1.1\r\n"
168 "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
169 "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
170 "CPE520(TP-LINK|US|N300-5):1.1\r\n"
171 "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
172 .support_trail
= '\xff',
176 {"fs-uboot", 0x00000, 0x20000},
177 {"partition-table", 0x20000, 0x02000},
178 {"default-mac", 0x30000, 0x00020},
179 {"product-info", 0x31100, 0x00100},
180 {"signature", 0x32000, 0x00400},
181 {"os-image", 0x40000, 0x1c0000},
182 {"file-system", 0x200000, 0x5b0000},
183 {"soft-version", 0x7b0000, 0x00100},
184 {"support-list", 0x7b1000, 0x00400},
185 {"user-config", 0x7c0000, 0x10000},
186 {"default-config", 0x7d0000, 0x10000},
187 {"log", 0x7e0000, 0x10000},
188 {"radio", 0x7f0000, 0x10000},
192 .first_sysupgrade_partition
= "os-image",
193 .last_sysupgrade_partition
= "support-list",
198 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
201 "WBS210(TP-LINK|UN|N300-2):1.20\r\n"
202 "WBS210(TP-LINK|US|N300-2):1.20\r\n"
203 "WBS210(TP-LINK|EU|N300-2):1.20\r\n",
204 .support_trail
= '\xff',
208 {"fs-uboot", 0x00000, 0x20000},
209 {"partition-table", 0x20000, 0x02000},
210 {"default-mac", 0x30000, 0x00020},
211 {"product-info", 0x31100, 0x00100},
212 {"signature", 0x32000, 0x00400},
213 {"os-image", 0x40000, 0x1c0000},
214 {"file-system", 0x200000, 0x5b0000},
215 {"soft-version", 0x7b0000, 0x00100},
216 {"support-list", 0x7b1000, 0x00400},
217 {"user-config", 0x7c0000, 0x10000},
218 {"default-config", 0x7d0000, 0x10000},
219 {"log", 0x7e0000, 0x10000},
220 {"radio", 0x7f0000, 0x10000},
224 .first_sysupgrade_partition
= "os-image",
225 .last_sysupgrade_partition
= "support-list",
230 .vendor
= "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
233 "WBS510(TP-LINK|UN|N300-5):1.20\r\n"
234 "WBS510(TP-LINK|US|N300-5):1.20\r\n"
235 "WBS510(TP-LINK|EU|N300-5):1.20\r\n",
236 .support_trail
= '\xff',
240 {"fs-uboot", 0x00000, 0x20000},
241 {"partition-table", 0x20000, 0x02000},
242 {"default-mac", 0x30000, 0x00020},
243 {"product-info", 0x31100, 0x00100},
244 {"signature", 0x32000, 0x00400},
245 {"os-image", 0x40000, 0x1c0000},
246 {"file-system", 0x200000, 0x5b0000},
247 {"soft-version", 0x7b0000, 0x00100},
248 {"support-list", 0x7b1000, 0x00400},
249 {"user-config", 0x7c0000, 0x10000},
250 {"default-config", 0x7d0000, 0x10000},
251 {"log", 0x7e0000, 0x10000},
252 {"radio", 0x7f0000, 0x10000},
256 .first_sysupgrade_partition
= "os-image",
257 .last_sysupgrade_partition
= "support-list",
260 /** Firmware layout for the C2600 */
266 "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
267 .support_trail
= '\x00',
271 We use a bigger os-image partition than the stock images (and thus
272 smaller file-system), as our kernel doesn't fit in the stock firmware's
273 2 MB os-image since kernel 4.14.
276 {"SBL1", 0x00000, 0x20000},
277 {"MIBIB", 0x20000, 0x20000},
278 {"SBL2", 0x40000, 0x20000},
279 {"SBL3", 0x60000, 0x30000},
280 {"DDRCONFIG", 0x90000, 0x10000},
281 {"SSD", 0xa0000, 0x10000},
282 {"TZ", 0xb0000, 0x30000},
283 {"RPM", 0xe0000, 0x20000},
284 {"fs-uboot", 0x100000, 0x70000},
285 {"uboot-env", 0x170000, 0x40000},
286 {"radio", 0x1b0000, 0x40000},
287 {"os-image", 0x1f0000, 0x400000}, /* Stock: base 0x1f0000 size 0x200000 */
288 {"file-system", 0x5f0000, 0x1900000}, /* Stock: base 0x3f0000 size 0x1b00000 */
289 {"default-mac", 0x1ef0000, 0x00200},
290 {"pin", 0x1ef0200, 0x00200},
291 {"product-info", 0x1ef0400, 0x0fc00},
292 {"partition-table", 0x1f00000, 0x10000},
293 {"soft-version", 0x1f10000, 0x10000},
294 {"support-list", 0x1f20000, 0x10000},
295 {"profile", 0x1f30000, 0x10000},
296 {"default-config", 0x1f40000, 0x10000},
297 {"user-config", 0x1f50000, 0x40000},
298 {"qos-db", 0x1f90000, 0x40000},
299 {"usb-config", 0x1fd0000, 0x10000},
300 {"log", 0x1fe0000, 0x20000},
304 .first_sysupgrade_partition
= "os-image",
305 .last_sysupgrade_partition
= "file-system"
308 /** Firmware layout for the C25v1 */
310 .id
= "ARCHER-C25-V1",
313 "{product_name:ArcherC25,product_ver:1.0.0,special_id:00000000}\n"
314 "{product_name:ArcherC25,product_ver:1.0.0,special_id:55530000}\n"
315 "{product_name:ArcherC25,product_ver:1.0.0,special_id:45550000}\n",
316 .support_trail
= '\x00',
317 .soft_ver
= "soft_ver:1.0.0\n",
320 We use a bigger os-image partition than the stock images (and thus
321 smaller file-system), as our kernel doesn't fit in the stock firmware's
325 {"factory-boot", 0x00000, 0x20000},
326 {"fs-uboot", 0x20000, 0x10000},
327 {"os-image", 0x30000, 0x180000}, /* Stock: base 0x30000 size 0x100000 */
328 {"file-system", 0x1b0000, 0x620000}, /* Stock: base 0x130000 size 0x6a0000 */
329 {"user-config", 0x7d0000, 0x04000},
330 {"default-mac", 0x7e0000, 0x00100},
331 {"device-id", 0x7e0100, 0x00100},
332 {"extra-para", 0x7e0200, 0x00100},
333 {"pin", 0x7e0300, 0x00100},
334 {"support-list", 0x7e0400, 0x00400},
335 {"soft-version", 0x7e0800, 0x00400},
336 {"product-info", 0x7e0c00, 0x01400},
337 {"partition-table", 0x7e2000, 0x01000},
338 {"profile", 0x7e3000, 0x01000},
339 {"default-config", 0x7e4000, 0x04000},
340 {"merge-config", 0x7ec000, 0x02000},
341 {"qos-db", 0x7ee000, 0x02000},
342 {"radio", 0x7f0000, 0x10000},
346 .first_sysupgrade_partition
= "os-image",
347 .last_sysupgrade_partition
= "file-system",
350 /** Firmware layout for the C58v1 */
352 .id
= "ARCHER-C58-V1",
356 "{product_name:Archer C58,product_ver:1.0.0,special_id:00000000}\r\n"
357 "{product_name:Archer C58,product_ver:1.0.0,special_id:45550000}\r\n"
358 "{product_name:Archer C58,product_ver:1.0.0,special_id:55530000}\r\n",
359 .support_trail
= '\x00',
360 .soft_ver
= "soft_ver:1.0.0\n",
363 {"fs-uboot", 0x00000, 0x10000},
364 {"default-mac", 0x10000, 0x00200},
365 {"pin", 0x10200, 0x00200},
366 {"product-info", 0x10400, 0x00100},
367 {"partition-table", 0x10500, 0x00800},
368 {"soft-version", 0x11300, 0x00200},
369 {"support-list", 0x11500, 0x00100},
370 {"device-id", 0x11600, 0x00100},
371 {"profile", 0x11700, 0x03900},
372 {"default-config", 0x15000, 0x04000},
373 {"user-config", 0x19000, 0x04000},
374 {"os-image", 0x20000, 0x180000},
375 {"file-system", 0x1a0000, 0x648000},
376 {"certyficate", 0x7e8000, 0x08000},
377 {"radio", 0x7f0000, 0x10000},
381 .first_sysupgrade_partition
= "os-image",
382 .last_sysupgrade_partition
= "file-system",
385 /** Firmware layout for the C59v1 */
387 .id
= "ARCHER-C59-V1",
391 "{product_name:Archer C59,product_ver:1.0.0,special_id:00000000}\r\n"
392 "{product_name:Archer C59,product_ver:1.0.0,special_id:45550000}\r\n"
393 "{product_name:Archer C59,product_ver:1.0.0,special_id:52550000}\r\n"
394 "{product_name:Archer C59,product_ver:1.0.0,special_id:55530000}\r\n",
395 .support_trail
= '\x00',
396 .soft_ver
= "soft_ver:1.0.0\n",
399 {"fs-uboot", 0x00000, 0x10000},
400 {"default-mac", 0x10000, 0x00200},
401 {"pin", 0x10200, 0x00200},
402 {"device-id", 0x10400, 0x00100},
403 {"product-info", 0x10500, 0x0fb00},
404 {"os-image", 0x20000, 0x180000},
405 {"file-system", 0x1a0000, 0xcb0000},
406 {"partition-table", 0xe50000, 0x10000},
407 {"soft-version", 0xe60000, 0x10000},
408 {"support-list", 0xe70000, 0x10000},
409 {"profile", 0xe80000, 0x10000},
410 {"default-config", 0xe90000, 0x10000},
411 {"user-config", 0xea0000, 0x40000},
412 {"usb-config", 0xee0000, 0x10000},
413 {"certificate", 0xef0000, 0x10000},
414 {"qos-db", 0xf00000, 0x40000},
415 {"log", 0xfe0000, 0x10000},
416 {"radio", 0xff0000, 0x10000},
420 .first_sysupgrade_partition
= "os-image",
421 .last_sysupgrade_partition
= "file-system",
424 /** Firmware layout for the C60v1 */
426 .id
= "ARCHER-C60-V1",
430 "{product_name:Archer C60,product_ver:1.0.0,special_id:00000000}\r\n"
431 "{product_name:Archer C60,product_ver:1.0.0,special_id:45550000}\r\n"
432 "{product_name:Archer C60,product_ver:1.0.0,special_id:55530000}\r\n",
433 .support_trail
= '\x00',
434 .soft_ver
= "soft_ver:1.0.0\n",
437 {"fs-uboot", 0x00000, 0x10000},
438 {"default-mac", 0x10000, 0x00200},
439 {"pin", 0x10200, 0x00200},
440 {"product-info", 0x10400, 0x00100},
441 {"partition-table", 0x10500, 0x00800},
442 {"soft-version", 0x11300, 0x00200},
443 {"support-list", 0x11500, 0x00100},
444 {"device-id", 0x11600, 0x00100},
445 {"profile", 0x11700, 0x03900},
446 {"default-config", 0x15000, 0x04000},
447 {"user-config", 0x19000, 0x04000},
448 {"os-image", 0x20000, 0x180000},
449 {"file-system", 0x1a0000, 0x648000},
450 {"certyficate", 0x7e8000, 0x08000},
451 {"radio", 0x7f0000, 0x10000},
455 .first_sysupgrade_partition
= "os-image",
456 .last_sysupgrade_partition
= "file-system",
459 /** Firmware layout for the C60v2 */
461 .id
= "ARCHER-C60-V2",
465 "{product_name:Archer C60,product_ver:2.0.0,special_id:42520000}\r\n"
466 "{product_name:Archer C60,product_ver:2.0.0,special_id:45550000}\r\n"
467 "{product_name:Archer C60,product_ver:2.0.0,special_id:55530000}\r\n",
468 .support_trail
= '\x00',
469 .soft_ver
= "soft_ver:2.0.0\n",
472 {"factory-boot", 0x00000, 0x1fb00},
473 {"default-mac", 0x1fb00, 0x00200},
474 {"pin", 0x1fd00, 0x00100},
475 {"product-info", 0x1fe00, 0x00100},
476 {"device-id", 0x1ff00, 0x00100},
477 {"fs-uboot", 0x20000, 0x10000},
478 {"os-image", 0x30000, 0x180000},
479 {"file-system", 0x1b0000, 0x620000},
480 {"soft-version", 0x7d9500, 0x00100},
481 {"support-list", 0x7d9600, 0x00100},
482 {"extra-para", 0x7d9700, 0x00100},
483 {"profile", 0x7d9800, 0x03000},
484 {"default-config", 0x7dc800, 0x03000},
485 {"partition-table", 0x7df800, 0x00800},
486 {"user-config", 0x7e0000, 0x0c000},
487 {"certificate", 0x7ec000, 0x04000},
488 {"radio", 0x7f0000, 0x10000},
492 .first_sysupgrade_partition
= "os-image",
493 .last_sysupgrade_partition
= "file-system",
496 /** Firmware layout for the C5 */
498 .id
= "ARCHER-C5-V2",
502 "{product_name:ArcherC5,product_ver:2.0.0,special_id:00000000}\r\n"
503 "{product_name:ArcherC5,product_ver:2.0.0,special_id:55530000}\r\n"
504 "{product_name:ArcherC5,product_ver:2.0.0,special_id:4A500000}\r\n", /* JP version */
505 .support_trail
= '\x00',
509 {"fs-uboot", 0x00000, 0x40000},
510 {"os-image", 0x40000, 0x200000},
511 {"file-system", 0x240000, 0xc00000},
512 {"default-mac", 0xe40000, 0x00200},
513 {"pin", 0xe40200, 0x00200},
514 {"product-info", 0xe40400, 0x00200},
515 {"partition-table", 0xe50000, 0x10000},
516 {"soft-version", 0xe60000, 0x00200},
517 {"support-list", 0xe61000, 0x0f000},
518 {"profile", 0xe70000, 0x10000},
519 {"default-config", 0xe80000, 0x10000},
520 {"user-config", 0xe90000, 0x50000},
521 {"log", 0xee0000, 0x100000},
522 {"radio_bk", 0xfe0000, 0x10000},
523 {"radio", 0xff0000, 0x10000},
527 .first_sysupgrade_partition
= "os-image",
528 .last_sysupgrade_partition
= "file-system"
531 /** Firmware layout for the C7 */
533 .id
= "ARCHER-C7-V4",
536 "{product_name:Archer C7,product_ver:4.0.0,special_id:00000000}\n"
537 "{product_name:Archer C7,product_ver:4.0.0,special_id:41550000}\n"
538 "{product_name:Archer C7,product_ver:4.0.0,special_id:45550000}\n"
539 "{product_name:Archer C7,product_ver:4.0.0,special_id:4B520000}\n"
540 "{product_name:Archer C7,product_ver:4.0.0,special_id:42520000}\n"
541 "{product_name:Archer C7,product_ver:4.0.0,special_id:4A500000}\n"
542 "{product_name:Archer C7,product_ver:4.0.0,special_id:52550000}\n"
543 "{product_name:Archer C7,product_ver:4.0.0,special_id:54570000}\n"
544 "{product_name:Archer C7,product_ver:4.0.0,special_id:55530000}\n"
545 "{product_name:Archer C7,product_ver:4.0.0,special_id:43410000}\n",
546 .support_trail
= '\x00',
547 .soft_ver
= "soft_ver:1.0.0\n",
550 We use a bigger os-image partition than the stock images (and thus
551 smaller file-system), as our kernel doesn't fit in the stock firmware's
555 {"factory-boot", 0x00000, 0x20000},
556 {"fs-uboot", 0x20000, 0x20000},
557 {"os-image", 0x40000, 0x180000}, /* Stock: base 0x40000 size 0x120000 */
558 {"file-system", 0x1c0000, 0xd40000}, /* Stock: base 0x160000 size 0xda0000 */
559 {"default-mac", 0xf00000, 0x00200},
560 {"pin", 0xf00200, 0x00200},
561 {"device-id", 0xf00400, 0x00100},
562 {"product-info", 0xf00500, 0x0fb00},
563 {"soft-version", 0xf10000, 0x00100},
564 {"extra-para", 0xf11000, 0x01000},
565 {"support-list", 0xf12000, 0x0a000},
566 {"profile", 0xf1c000, 0x04000},
567 {"default-config", 0xf20000, 0x10000},
568 {"user-config", 0xf30000, 0x40000},
569 {"qos-db", 0xf70000, 0x40000},
570 {"certificate", 0xfb0000, 0x10000},
571 {"partition-table", 0xfc0000, 0x10000},
572 {"log", 0xfd0000, 0x20000},
573 {"radio", 0xff0000, 0x10000},
577 .first_sysupgrade_partition
= "os-image",
578 .last_sysupgrade_partition
= "file-system",
581 /** Firmware layout for the C7 v5*/
583 .id
= "ARCHER-C7-V5",
586 "{product_name:Archer C7,product_ver:5.0.0,special_id:00000000}\n"
587 "{product_name:Archer C7,product_ver:5.0.0,special_id:55530000}\n",
589 .support_trail
= '\x00',
590 .soft_ver
= "soft_ver:1.0.0\n",
593 We use a bigger os-image partition than the stock images (and thus
594 smaller file-system), as our kernel doesn't fit in the stock firmware's
598 {"factory-boot", 0x00000, 0x20000},
599 {"fs-uboot", 0x20000, 0x20000},
600 {"partition-table", 0x40000, 0x10000},
601 {"radio", 0x50000, 0x10000},
602 {"default-mac", 0x60000, 0x00200},
603 {"pin", 0x60200, 0x00200},
604 {"device-id", 0x60400, 0x00100},
605 {"product-info", 0x60500, 0x0fb00},
606 {"soft-version", 0x70000, 0x01000},
607 {"extra-para", 0x71000, 0x01000},
608 {"support-list", 0x72000, 0x0a000},
609 {"profile", 0x7c000, 0x04000},
610 {"user-config", 0x80000, 0x40000},
613 {"os-image", 0xc0000, 0x180000}, /* Stock: base 0xc0000 size 0x120000 */
614 {"file-system", 0x240000, 0xd80000}, /* Stock: base 0x1e0000 size 0xde0000 */
616 {"log", 0xfc0000, 0x20000},
617 {"certificate", 0xfe0000, 0x10000},
618 {"default-config", 0xff0000, 0x10000},
623 .first_sysupgrade_partition
= "os-image",
624 .last_sysupgrade_partition
= "file-system",
627 /** Firmware layout for the C9 */
633 "{product_name:ArcherC9,"
635 "special_id:00000000}\n",
636 .support_trail
= '\x00',
640 {"fs-uboot", 0x00000, 0x40000},
641 {"os-image", 0x40000, 0x200000},
642 {"file-system", 0x240000, 0xc00000},
643 {"default-mac", 0xe40000, 0x00200},
644 {"pin", 0xe40200, 0x00200},
645 {"product-info", 0xe40400, 0x00200},
646 {"partition-table", 0xe50000, 0x10000},
647 {"soft-version", 0xe60000, 0x00200},
648 {"support-list", 0xe61000, 0x0f000},
649 {"profile", 0xe70000, 0x10000},
650 {"default-config", 0xe80000, 0x10000},
651 {"user-config", 0xe90000, 0x50000},
652 {"log", 0xee0000, 0x100000},
653 {"radio_bk", 0xfe0000, 0x10000},
654 {"radio", 0xff0000, 0x10000},
658 .first_sysupgrade_partition
= "os-image",
659 .last_sysupgrade_partition
= "file-system"
662 /** Firmware layout for the EAP120 */
665 .vendor
= "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
668 "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
669 .support_trail
= '\xff',
673 {"fs-uboot", 0x00000, 0x20000},
674 {"partition-table", 0x20000, 0x02000},
675 {"default-mac", 0x30000, 0x00020},
676 {"support-list", 0x31000, 0x00100},
677 {"product-info", 0x31100, 0x00100},
678 {"soft-version", 0x32000, 0x00100},
679 {"os-image", 0x40000, 0x180000},
680 {"file-system", 0x1c0000, 0x600000},
681 {"user-config", 0x7c0000, 0x10000},
682 {"backup-config", 0x7d0000, 0x10000},
683 {"log", 0x7e0000, 0x10000},
684 {"radio", 0x7f0000, 0x10000},
688 .first_sysupgrade_partition
= "os-image",
689 .last_sysupgrade_partition
= "file-system"
692 /** Firmware layout for the TL-WA850RE v2 */
698 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
699 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
700 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
701 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
702 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
703 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
704 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
705 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
706 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
707 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
708 .support_trail
= '\x00',
712 576KB were moved from file-system to os-image
713 in comparison to the stock image
716 {"fs-uboot", 0x00000, 0x20000},
717 {"os-image", 0x20000, 0x150000},
718 {"file-system", 0x170000, 0x240000},
719 {"partition-table", 0x3b0000, 0x02000},
720 {"default-mac", 0x3c0000, 0x00020},
721 {"pin", 0x3c0100, 0x00020},
722 {"product-info", 0x3c1000, 0x01000},
723 {"soft-version", 0x3c2000, 0x00100},
724 {"support-list", 0x3c3000, 0x01000},
725 {"profile", 0x3c4000, 0x08000},
726 {"user-config", 0x3d0000, 0x10000},
727 {"default-config", 0x3e0000, 0x10000},
728 {"radio", 0x3f0000, 0x10000},
732 .first_sysupgrade_partition
= "os-image",
733 .last_sysupgrade_partition
= "file-system"
736 /** Firmware layout for the TL-WA855RE v1 */
742 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
743 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
744 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
745 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
746 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
747 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
748 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
749 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
750 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
751 .support_trail
= '\x00',
755 {"fs-uboot", 0x00000, 0x20000},
756 {"os-image", 0x20000, 0x150000},
757 {"file-system", 0x170000, 0x240000},
758 {"partition-table", 0x3b0000, 0x02000},
759 {"default-mac", 0x3c0000, 0x00020},
760 {"pin", 0x3c0100, 0x00020},
761 {"product-info", 0x3c1000, 0x01000},
762 {"soft-version", 0x3c2000, 0x00100},
763 {"support-list", 0x3c3000, 0x01000},
764 {"profile", 0x3c4000, 0x08000},
765 {"user-config", 0x3d0000, 0x10000},
766 {"default-config", 0x3e0000, 0x10000},
767 {"radio", 0x3f0000, 0x10000},
771 .first_sysupgrade_partition
= "os-image",
772 .last_sysupgrade_partition
= "file-system"
775 /** Firmware layout for the TL-WR1043 v5 */
781 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:45550000}\n"
782 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:55530000}\n",
783 .support_trail
= '\x00',
784 .soft_ver
= "soft_ver:1.0.0\n",
786 {"factory-boot", 0x00000, 0x20000},
787 {"fs-uboot", 0x20000, 0x20000},
788 {"os-image", 0x40000, 0x180000},
789 {"file-system", 0x1c0000, 0xd40000},
790 {"default-mac", 0xf00000, 0x00200},
791 {"pin", 0xf00200, 0x00200},
792 {"device-id", 0xf00400, 0x00100},
793 {"product-info", 0xf00500, 0x0fb00},
794 {"soft-version", 0xf10000, 0x01000},
795 {"extra-para", 0xf11000, 0x01000},
796 {"support-list", 0xf12000, 0x0a000},
797 {"profile", 0xf1c000, 0x04000},
798 {"default-config", 0xf20000, 0x10000},
799 {"user-config", 0xf30000, 0x40000},
800 {"qos-db", 0xf70000, 0x40000},
801 {"certificate", 0xfb0000, 0x10000},
802 {"partition-table", 0xfc0000, 0x10000},
803 {"log", 0xfd0000, 0x20000},
804 {"radio", 0xff0000, 0x10000},
807 .first_sysupgrade_partition
= "os-image",
808 .last_sysupgrade_partition
= "file-system"
811 /** Firmware layout for the TL-WR1043 v4 */
813 .id
= "TLWR1043NDV4",
817 "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
818 .support_trail
= '\x00',
822 We use a bigger os-image partition than the stock images (and thus
823 smaller file-system), as our kernel doesn't fit in the stock firmware's
827 {"fs-uboot", 0x00000, 0x20000},
828 {"os-image", 0x20000, 0x200000},
829 {"file-system", 0x220000, 0xd30000},
830 {"default-mac", 0xf50000, 0x00200},
831 {"pin", 0xf50200, 0x00200},
832 {"product-info", 0xf50400, 0x0fc00},
833 {"soft-version", 0xf60000, 0x0b000},
834 {"support-list", 0xf6b000, 0x04000},
835 {"profile", 0xf70000, 0x04000},
836 {"default-config", 0xf74000, 0x0b000},
837 {"user-config", 0xf80000, 0x40000},
838 {"partition-table", 0xfc0000, 0x10000},
839 {"log", 0xfd0000, 0x20000},
840 {"radio", 0xff0000, 0x10000},
844 .first_sysupgrade_partition
= "os-image",
845 .last_sysupgrade_partition
= "file-system"
848 /** Firmware layout for the TL-WR902AC v1 */
850 .id
= "TL-WR902AC-V1",
854 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:45550000}\n"
855 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:55530000}\n",
856 .support_trail
= '\x00',
860 384KB were moved from file-system to os-image
861 in comparison to the stock image
864 {"fs-uboot", 0x00000, 0x20000},
865 {"os-image", 0x20000, 0x180000},
866 {"file-system", 0x1a0000, 0x5b0000},
867 {"default-mac", 0x750000, 0x00200},
868 {"pin", 0x750200, 0x00200},
869 {"product-info", 0x750400, 0x0fc00},
870 {"soft-version", 0x760000, 0x0b000},
871 {"support-list", 0x76b000, 0x04000},
872 {"profile", 0x770000, 0x04000},
873 {"default-config", 0x774000, 0x0b000},
874 {"user-config", 0x780000, 0x40000},
875 {"partition-table", 0x7c0000, 0x10000},
876 {"log", 0x7d0000, 0x20000},
877 {"radio", 0x7f0000, 0x10000},
881 .first_sysupgrade_partition
= "os-image",
882 .last_sysupgrade_partition
= "file-system",
885 /** Firmware layout for the TL-WR942N V1 */
891 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
892 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
893 .support_trail
= '\x00',
897 {"fs-uboot", 0x00000, 0x20000},
898 {"os-image", 0x20000, 0x180000},
899 {"file-system", 0x1a0000, 0xca0000},
900 {"default-mac", 0xe40000, 0x00200},
901 {"pin", 0xe40200, 0x00200},
902 {"product-info", 0xe40400, 0x0fc00},
903 {"partition-table", 0xe50000, 0x10000},
904 {"soft-version", 0xe60000, 0x10000},
905 {"support-list", 0xe70000, 0x10000},
906 {"profile", 0xe80000, 0x10000},
907 {"default-config", 0xe90000, 0x10000},
908 {"user-config", 0xea0000, 0x40000},
909 {"qos-db", 0xee0000, 0x40000},
910 {"certificate", 0xf20000, 0x10000},
911 {"usb-config", 0xfb0000, 0x10000},
912 {"log", 0xfc0000, 0x20000},
913 {"radio-bk", 0xfe0000, 0x10000},
914 {"radio", 0xff0000, 0x10000},
918 .first_sysupgrade_partition
= "os-image",
919 .last_sysupgrade_partition
= "file-system",
922 /** Firmware layout for the RE350 v1 */
928 "{product_name:RE350,product_ver:1.0.0,special_id:45550000}\n"
929 "{product_name:RE350,product_ver:1.0.0,special_id:00000000}\n"
930 "{product_name:RE350,product_ver:1.0.0,special_id:41550000}\n"
931 "{product_name:RE350,product_ver:1.0.0,special_id:55530000}\n"
932 "{product_name:RE350,product_ver:1.0.0,special_id:43410000}\n"
933 "{product_name:RE350,product_ver:1.0.0,special_id:4b520000}\n"
934 "{product_name:RE350,product_ver:1.0.0,special_id:4a500000}\n",
935 .support_trail
= '\x00',
939 The original os-image partition is too small,
940 so we enlarge it to 1.75M
943 {"fs-uboot", 0x00000, 0x20000},
944 {"os-image", 0x20000, 0x1c0000},
945 {"file-system", 0x1e0000, 0x420000},
946 {"partition-table", 0x600000, 0x02000},
947 {"default-mac", 0x610000, 0x00020},
948 {"pin", 0x610100, 0x00020},
949 {"product-info", 0x611100, 0x01000},
950 {"soft-version", 0x620000, 0x01000},
951 {"support-list", 0x621000, 0x01000},
952 {"profile", 0x622000, 0x08000},
953 {"user-config", 0x630000, 0x10000},
954 {"default-config", 0x640000, 0x10000},
955 {"radio", 0x7f0000, 0x10000},
959 .first_sysupgrade_partition
= "os-image",
960 .last_sysupgrade_partition
= "file-system"
963 /** Firmware layout for the RE355 */
969 "{product_name:RE355,product_ver:1.0.0,special_id:00000000}\r\n"
970 "{product_name:RE355,product_ver:1.0.0,special_id:55530000}\r\n"
971 "{product_name:RE355,product_ver:1.0.0,special_id:45550000}\r\n"
972 "{product_name:RE355,product_ver:1.0.0,special_id:4A500000}\r\n"
973 "{product_name:RE355,product_ver:1.0.0,special_id:43410000}\r\n"
974 "{product_name:RE355,product_ver:1.0.0,special_id:41550000}\r\n"
975 "{product_name:RE355,product_ver:1.0.0,special_id:4B520000}\r\n"
976 "{product_name:RE355,product_ver:1.0.0,special_id:55534100}\r\n",
977 .support_trail
= '\x00',
981 The flash partition table for RE355;
982 it is almost the same as the one used by the stock images,
983 576KB were moved from file-system to os-image.
986 {"fs-uboot", 0x00000, 0x20000},
987 {"os-image", 0x20000, 0x180000},
988 {"file-system", 0x1a0000, 0x460000},
989 {"partition-table", 0x600000, 0x02000},
990 {"default-mac", 0x610000, 0x00020},
991 {"pin", 0x610100, 0x00020},
992 {"product-info", 0x611100, 0x01000},
993 {"soft-version", 0x620000, 0x01000},
994 {"support-list", 0x621000, 0x01000},
995 {"profile", 0x622000, 0x08000},
996 {"user-config", 0x630000, 0x10000},
997 {"default-config", 0x640000, 0x10000},
998 {"radio", 0x7f0000, 0x10000},
1002 .first_sysupgrade_partition
= "os-image",
1003 .last_sysupgrade_partition
= "file-system"
1006 /** Firmware layout for the RE450 */
1012 "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
1013 "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
1014 "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
1015 "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
1016 "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
1017 "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
1018 "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
1019 "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
1020 .support_trail
= '\x00',
1024 The flash partition table for RE450;
1025 it is almost the same as the one used by the stock images,
1026 576KB were moved from file-system to os-image.
1029 {"fs-uboot", 0x00000, 0x20000},
1030 {"os-image", 0x20000, 0x180000},
1031 {"file-system", 0x1a0000, 0x460000},
1032 {"partition-table", 0x600000, 0x02000},
1033 {"default-mac", 0x610000, 0x00020},
1034 {"pin", 0x610100, 0x00020},
1035 {"product-info", 0x611100, 0x01000},
1036 {"soft-version", 0x620000, 0x01000},
1037 {"support-list", 0x621000, 0x01000},
1038 {"profile", 0x622000, 0x08000},
1039 {"user-config", 0x630000, 0x10000},
1040 {"default-config", 0x640000, 0x10000},
1041 {"radio", 0x7f0000, 0x10000},
1045 .first_sysupgrade_partition
= "os-image",
1046 .last_sysupgrade_partition
= "file-system"
1052 #define error(_ret, _errno, _str, ...) \
1054 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
1055 strerror(_errno)); \
1061 /** Stores a uint32 as big endian */
1062 static inline void put32(uint8_t *buf
, uint32_t val
) {
1069 /** Allocates a new image partition */
1070 static struct image_partition_entry
alloc_image_partition(const char *name
, size_t len
) {
1071 struct image_partition_entry entry
= {name
, len
, malloc(len
)};
1073 error(1, errno
, "malloc");
1078 /** Frees an image partition */
1079 static void free_image_partition(struct image_partition_entry entry
) {
1083 static time_t source_date_epoch
= -1;
1084 static void set_source_date_epoch() {
1085 char *env
= getenv("SOURCE_DATE_EPOCH");
1089 source_date_epoch
= strtoull(env
, &endptr
, 10);
1090 if (errno
|| (endptr
&& *endptr
!= '\0')) {
1091 fprintf(stderr
, "Invalid SOURCE_DATE_EPOCH");
1097 /** Generates the partition-table partition */
1098 static struct image_partition_entry
make_partition_table(const struct flash_partition_entry
*p
) {
1099 struct image_partition_entry entry
= alloc_image_partition("partition-table", 0x800);
1101 char *s
= (char *)entry
.data
, *end
= (char *)(s
+entry
.size
);
1109 for (i
= 0; p
[i
].name
; i
++) {
1111 size_t w
= snprintf(s
, len
, "partition %s base 0x%05x size 0x%05x\n", p
[i
].name
, p
[i
].base
, p
[i
].size
);
1114 error(1, 0, "flash partition table overflow?");
1121 memset(s
, 0xff, end
-s
);
1127 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
1128 static inline uint8_t bcd(uint8_t v
) {
1129 return 0x10 * (v
/10) + v
%10;
1133 /** Generates the soft-version partition */
1134 static struct image_partition_entry
make_soft_version(uint32_t rev
) {
1135 struct image_partition_entry entry
= alloc_image_partition("soft-version", sizeof(struct soft_version
));
1136 struct soft_version
*s
= (struct soft_version
*)entry
.data
;
1140 if (source_date_epoch
!= -1)
1141 t
= source_date_epoch
;
1142 else if (time(&t
) == (time_t)(-1))
1143 error(1, errno
, "time");
1145 struct tm
*tm
= localtime(&t
);
1147 s
->magic
= htonl(0x0000000c);
1151 s
->version_major
= 0;
1152 s
->version_minor
= 0;
1153 s
->version_patch
= 0;
1155 s
->year_hi
= bcd((1900+tm
->tm_year
)/100);
1156 s
->year_lo
= bcd(tm
->tm_year
%100);
1157 s
->month
= bcd(tm
->tm_mon
+1);
1158 s
->day
= bcd(tm
->tm_mday
);
1159 s
->rev
= htonl(rev
);
1166 static struct image_partition_entry
make_soft_version_from_string(const char *soft_ver
) {
1167 /** String length _including_ the terminating zero byte */
1168 uint32_t ver_len
= strlen(soft_ver
) + 1;
1169 /** Partition contains 64 bit header, the version string, and one additional null byte */
1170 size_t partition_len
= 2*sizeof(uint32_t) + ver_len
+ 1;
1171 struct image_partition_entry entry
= alloc_image_partition("soft-version", partition_len
);
1173 uint32_t *len
= (uint32_t *)entry
.data
;
1174 len
[0] = htonl(ver_len
);
1176 memcpy(&len
[2], soft_ver
, ver_len
);
1178 entry
.data
[partition_len
- 1] = 0;
1183 /** Generates the support-list partition */
1184 static struct image_partition_entry
make_support_list(const struct device_info
*info
) {
1185 size_t len
= strlen(info
->support_list
);
1186 struct image_partition_entry entry
= alloc_image_partition("support-list", len
+ 9);
1188 put32(entry
.data
, len
);
1189 memset(entry
.data
+4, 0, 4);
1190 memcpy(entry
.data
+8, info
->support_list
, len
);
1191 entry
.data
[len
+8] = info
->support_trail
;
1196 /** Creates a new image partition with an arbitrary name from a file */
1197 static struct image_partition_entry
read_file(const char *part_name
, const char *filename
, bool add_jffs2_eof
) {
1198 struct stat statbuf
;
1200 if (stat(filename
, &statbuf
) < 0)
1201 error(1, errno
, "unable to stat file `%s'", filename
);
1203 size_t len
= statbuf
.st_size
;
1206 len
= ALIGN(len
, 0x10000) + sizeof(jffs2_eof_mark
);
1208 struct image_partition_entry entry
= alloc_image_partition(part_name
, len
);
1210 FILE *file
= fopen(filename
, "rb");
1212 error(1, errno
, "unable to open file `%s'", filename
);
1214 if (fread(entry
.data
, statbuf
.st_size
, 1, file
) != 1)
1215 error(1, errno
, "unable to read file `%s'", filename
);
1217 if (add_jffs2_eof
) {
1218 uint8_t *eof
= entry
.data
+ statbuf
.st_size
, *end
= entry
.data
+entry
.size
;
1220 memset(eof
, 0xff, end
- eof
- sizeof(jffs2_eof_mark
));
1221 memcpy(end
- sizeof(jffs2_eof_mark
), jffs2_eof_mark
, sizeof(jffs2_eof_mark
));
1229 /** Creates a new image partition from arbitrary data */
1230 static struct image_partition_entry
put_data(const char *part_name
, const char *datain
, size_t len
) {
1232 struct image_partition_entry entry
= alloc_image_partition(part_name
, len
);
1234 memcpy(entry
.data
, datain
, len
);
1240 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
1242 Example image partition table:
1244 fwup-ptn partition-table base 0x00800 size 0x00800
1245 fwup-ptn os-image base 0x01000 size 0x113b45
1246 fwup-ptn file-system base 0x114b45 size 0x1d0004
1247 fwup-ptn support-list base 0x2e4b49 size 0x000d1
1249 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
1250 the end of the partition table is marked with a zero byte.
1252 The firmware image must contain at least the partition-table and support-list partitions
1253 to be accepted. There aren't any alignment constraints for the image partitions.
1255 The partition-table partition contains the actual flash layout; partitions
1256 from the image partition table are mapped to the corresponding flash partitions during
1257 the firmware upgrade. The support-list partition contains a list of devices supported by
1260 The base offsets in the firmware partition table are relative to the end
1261 of the vendor information block, so the partition-table partition will
1262 actually start at offset 0x1814 of the image.
1264 I think partition-table must be the first partition in the firmware image.
1266 static void put_partitions(uint8_t *buffer
, const struct flash_partition_entry
*flash_parts
, const struct image_partition_entry
*parts
) {
1268 char *image_pt
= (char *)buffer
, *end
= image_pt
+ 0x800;
1270 size_t base
= 0x800;
1271 for (i
= 0; parts
[i
].name
; i
++) {
1272 for (j
= 0; flash_parts
[j
].name
; j
++) {
1273 if (!strcmp(flash_parts
[j
].name
, parts
[i
].name
)) {
1274 if (parts
[i
].size
> flash_parts
[j
].size
)
1275 error(1, 0, "%s partition too big (more than %u bytes)", flash_parts
[j
].name
, (unsigned)flash_parts
[j
].size
);
1280 assert(flash_parts
[j
].name
);
1282 memcpy(buffer
+ base
, parts
[i
].data
, parts
[i
].size
);
1284 size_t len
= end
-image_pt
;
1285 size_t w
= snprintf(image_pt
, len
, "fwup-ptn %s base 0x%05x size 0x%05x\t\r\n", parts
[i
].name
, (unsigned)base
, (unsigned)parts
[i
].size
);
1288 error(1, 0, "image partition table overflow?");
1292 base
+= parts
[i
].size
;
1296 /** Generates and writes the image MD5 checksum */
1297 static void put_md5(uint8_t *md5
, uint8_t *buffer
, unsigned int len
) {
1301 MD5_Update(&ctx
, md5_salt
, (unsigned int)sizeof(md5_salt
));
1302 MD5_Update(&ctx
, buffer
, len
);
1303 MD5_Final(md5
, &ctx
);
1308 Generates the firmware image in factory format
1314 0000-0003 Image size (4 bytes, big endian)
1315 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
1316 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
1317 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
1318 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
1319 1014-1813 Image partition table (2048 bytes, padded with 0xff)
1320 1814-xxxx Firmware partitions
1322 static void * generate_factory_image(const struct device_info
*info
, const struct image_partition_entry
*parts
, size_t *len
) {
1326 for (i
= 0; parts
[i
].name
; i
++)
1327 *len
+= parts
[i
].size
;
1329 uint8_t *image
= malloc(*len
);
1331 error(1, errno
, "malloc");
1333 memset(image
, 0xff, *len
);
1337 size_t vendor_len
= strlen(info
->vendor
);
1338 put32(image
+0x14, vendor_len
);
1339 memcpy(image
+0x18, info
->vendor
, vendor_len
);
1342 put_partitions(image
+ 0x1014, info
->partitions
, parts
);
1343 put_md5(image
+0x04, image
+0x14, *len
-0x14);
1349 Generates the firmware image in sysupgrade format
1351 This makes some assumptions about the provided flash and image partition tables and
1352 should be generalized when TP-LINK starts building its safeloader into hardware with
1353 different flash layouts.
1355 static void * generate_sysupgrade_image(const struct device_info
*info
, const struct image_partition_entry
*image_parts
, size_t *len
) {
1357 size_t flash_first_partition_index
= 0;
1358 size_t flash_last_partition_index
= 0;
1359 const struct flash_partition_entry
*flash_first_partition
= NULL
;
1360 const struct flash_partition_entry
*flash_last_partition
= NULL
;
1361 const struct image_partition_entry
*image_last_partition
= NULL
;
1363 /** Find first and last partitions */
1364 for (i
= 0; info
->partitions
[i
].name
; i
++) {
1365 if (!strcmp(info
->partitions
[i
].name
, info
->first_sysupgrade_partition
)) {
1366 flash_first_partition
= &info
->partitions
[i
];
1367 flash_first_partition_index
= i
;
1368 } else if (!strcmp(info
->partitions
[i
].name
, info
->last_sysupgrade_partition
)) {
1369 flash_last_partition
= &info
->partitions
[i
];
1370 flash_last_partition_index
= i
;
1374 assert(flash_first_partition
&& flash_last_partition
);
1375 assert(flash_first_partition_index
< flash_last_partition_index
);
1377 /** Find last partition from image to calculate needed size */
1378 for (i
= 0; image_parts
[i
].name
; i
++) {
1379 if (!strcmp(image_parts
[i
].name
, info
->last_sysupgrade_partition
)) {
1380 image_last_partition
= &image_parts
[i
];
1385 assert(image_last_partition
);
1387 *len
= flash_last_partition
->base
- flash_first_partition
->base
+ image_last_partition
->size
;
1389 uint8_t *image
= malloc(*len
);
1391 error(1, errno
, "malloc");
1393 memset(image
, 0xff, *len
);
1395 for (i
= flash_first_partition_index
; i
<= flash_last_partition_index
; i
++) {
1396 for (j
= 0; image_parts
[j
].name
; j
++) {
1397 if (!strcmp(info
->partitions
[i
].name
, image_parts
[j
].name
)) {
1398 if (image_parts
[j
].size
> info
->partitions
[i
].size
)
1399 error(1, 0, "%s partition too big (more than %u bytes)", info
->partitions
[i
].name
, (unsigned)info
->partitions
[i
].size
);
1400 memcpy(image
+ info
->partitions
[i
].base
- flash_first_partition
->base
, image_parts
[j
].data
, image_parts
[j
].size
);
1404 assert(image_parts
[j
].name
);
1411 /** Generates an image according to a given layout and writes it to a file */
1412 static void build_image(const char *output
,
1413 const char *kernel_image
,
1414 const char *rootfs_image
,
1418 const struct device_info
*info
) {
1420 struct image_partition_entry parts
[7] = {};
1422 parts
[0] = make_partition_table(info
->partitions
);
1424 parts
[1] = make_soft_version_from_string(info
->soft_ver
);
1426 parts
[1] = make_soft_version(rev
);
1428 parts
[2] = make_support_list(info
);
1429 parts
[3] = read_file("os-image", kernel_image
, false);
1430 parts
[4] = read_file("file-system", rootfs_image
, add_jffs2_eof
);
1432 /* Some devices need the extra-para partition to accept the firmware */
1433 if (strcasecmp(info
->id
, "ARCHER-C25-V1") == 0 ||
1434 strcasecmp(info
->id
, "ARCHER-C60-V2") == 0 ||
1435 strcasecmp(info
->id
, "TLWR1043NV5") == 0) {
1436 const char mdat
[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
1437 parts
[5] = put_data("extra-para", mdat
, 11);
1438 } else if (strcasecmp(info
->id
, "ARCHER-C7-V4") == 0 || strcasecmp(info
->id
, "ARCHER-C7-V5") == 0) {
1439 const char mdat
[11] = {0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0xca, 0x00, 0x01, 0x00, 0x00};
1440 parts
[5] = put_data("extra-para", mdat
, 11);
1446 image
= generate_sysupgrade_image(info
, parts
, &len
);
1448 image
= generate_factory_image(info
, parts
, &len
);
1450 FILE *file
= fopen(output
, "wb");
1452 error(1, errno
, "unable to open output file");
1454 if (fwrite(image
, len
, 1, file
) != 1)
1455 error(1, 0, "unable to write output file");
1462 for (i
= 0; parts
[i
].name
; i
++)
1463 free_image_partition(parts
[i
]);
1467 static void usage(const char *argv0
) {
1469 "Usage: %s [OPTIONS...]\n"
1472 " -h show this help\n"
1474 "Create a new image:\n"
1475 " -B <board> create image for the board specified with <board>\n"
1476 " -k <file> read kernel image from the file <file>\n"
1477 " -r <file> read rootfs image from the file <file>\n"
1478 " -o <file> write output to the file <file>\n"
1479 " -V <rev> sets the revision number to <rev>\n"
1480 " -j add jffs2 end-of-filesystem markers\n"
1481 " -S create sysupgrade instead of factory image\n"
1482 "Extract an old image:\n"
1483 " -x <file> extract all oem firmware partition\n"
1484 " -d <dir> destination to extract the firmware partition\n"
1485 " -z <file> convert an oem firmware into a sysupgade file. Use -o for output file\n",
1491 static const struct device_info
*find_board(const char *id
)
1493 struct device_info
*board
= NULL
;
1495 for (board
= boards
; board
->id
!= NULL
; board
++)
1496 if (strcasecmp(id
, board
->id
) == 0)
1502 static int add_flash_partition(
1503 struct flash_partition_entry
*part_list
,
1510 /* check if the list has a free entry */
1511 for (ptr
= 0; ptr
< max_entries
; ptr
++, part_list
++) {
1512 if (part_list
->name
== NULL
&&
1513 part_list
->base
== 0 &&
1514 part_list
->size
== 0)
1518 if (ptr
== max_entries
) {
1519 error(1, 0, "No free flash part entry available.");
1522 part_list
->name
= calloc(1, strlen(name
) + 1);
1523 memcpy((char *)part_list
->name
, name
, strlen(name
));
1524 part_list
->base
= base
;
1525 part_list
->size
= size
;
1530 /** read the partition table into struct flash_partition_entry */
1531 static int read_partition_table(
1532 FILE *file
, long offset
,
1533 struct flash_partition_entry
*entries
, size_t max_entries
,
1538 const char *parthdr
= NULL
;
1539 const char *fwuphdr
= "fwup-ptn";
1540 const char *flashhdr
= "partition";
1542 /* TODO: search for the partition table */
1552 error(1, 0, "Invalid partition table");
1555 if (fseek(file
, offset
, SEEK_SET
) < 0)
1556 error(1, errno
, "Can not seek in the firmware");
1558 if (fread(buf
, 1, 2048, file
) < 0)
1559 error(1, errno
, "Can not read fwup-ptn from the firmware");
1563 /* look for the partition header */
1564 if (memcmp(buf
, parthdr
, strlen(parthdr
)) != 0) {
1565 fprintf(stderr
, "DEBUG: can not find fwuphdr\n");
1570 end
= buf
+ sizeof(buf
);
1571 while ((ptr
+ strlen(parthdr
)) < end
&&
1572 memcmp(ptr
, parthdr
, strlen(parthdr
)) == 0) {
1576 char name
[32] = { 0 };
1578 unsigned long base
= 0;
1579 unsigned long size
= 0;
1581 end_part
= memchr(ptr
, '\n', (end
- ptr
));
1582 if (end_part
== NULL
) {
1583 /* in theory this should never happen, because a partition always ends with 0x09, 0x0D, 0x0A */
1587 for (int i
= 0; i
<= 4; i
++) {
1588 if (end_part
<= ptr
)
1591 end_element
= memchr(ptr
, 0x20, (end_part
- ptr
));
1592 if (end_element
== NULL
) {
1593 error(1, errno
, "Ignoring the rest of the partition entries.");
1598 /* partition header */
1600 ptr
= end_element
+ 1;
1604 name_len
= (end_element
- ptr
) > 31 ? 31 : (end_element
- ptr
);
1605 strncpy(name
, ptr
, name_len
);
1606 name
[name_len
] = '\0';
1607 ptr
= end_element
+ 1;
1612 ptr
= end_element
+ 1;
1617 base
= strtoul(ptr
, NULL
, 16);
1618 ptr
= end_element
+ 1;
1623 ptr
= end_element
+ 1;
1624 /* actual size. The last element doesn't have a sepeartor */
1625 size
= strtoul(ptr
, NULL
, 16);
1626 /* the part ends with 0x09, 0x0d, 0x0a */
1628 add_flash_partition(entries
, max_entries
, name
, base
, size
);
1637 static void write_partition(
1639 size_t firmware_offset
,
1640 struct flash_partition_entry
*entry
,
1646 fseek(input_file
, entry
->base
+ firmware_offset
, SEEK_SET
);
1648 for (offset
= 0; sizeof(buf
) + offset
<= entry
->size
; offset
+= sizeof(buf
)) {
1649 if (fread(buf
, sizeof(buf
), 1, input_file
) < 0)
1650 error(1, errno
, "Can not read partition from input_file");
1652 if (fwrite(buf
, sizeof(buf
), 1, output_file
) < 0)
1653 error(1, errno
, "Can not write partition to output_file");
1655 /* write last chunk smaller than buffer */
1656 if (offset
< entry
->size
) {
1657 offset
= entry
->size
- offset
;
1658 if (fread(buf
, offset
, 1, input_file
) < 0)
1659 error(1, errno
, "Can not read partition from input_file");
1660 if (fwrite(buf
, offset
, 1, output_file
) < 0)
1661 error(1, errno
, "Can not write partition to output_file");
1665 static int extract_firmware_partition(FILE *input_file
, size_t firmware_offset
, struct flash_partition_entry
*entry
, const char *output_directory
)
1668 char output
[PATH_MAX
];
1670 snprintf(output
, PATH_MAX
, "%s/%s", output_directory
, entry
->name
);
1671 output_file
= fopen(output
, "wb+");
1672 if (output_file
== NULL
) {
1673 error(1, errno
, "Can not open output file %s", output
);
1676 write_partition(input_file
, firmware_offset
, entry
, output_file
);
1678 fclose(output_file
);
1683 /** extract all partitions from the firmware file */
1684 static int extract_firmware(const char *input
, const char *output_directory
)
1686 struct flash_partition_entry entries
[16] = { 0 };
1687 size_t max_entries
= 16;
1688 size_t firmware_offset
= 0x1014;
1691 struct stat statbuf
;
1693 /* check input file */
1694 if (stat(input
, &statbuf
)) {
1695 error(1, errno
, "Can not read input firmware %s", input
);
1698 /* check if output directory exists */
1699 if (stat(output_directory
, &statbuf
)) {
1700 error(1, errno
, "Failed to stat output directory %s", output_directory
);
1703 if ((statbuf
.st_mode
& S_IFMT
) != S_IFDIR
) {
1704 error(1, errno
, "Given output directory is not a directory %s", output_directory
);
1707 input_file
= fopen(input
, "rb");
1709 if (read_partition_table(input_file
, firmware_offset
, entries
, 16, 0) != 0) {
1710 error(1, 0, "Error can not read the partition table (fwup-ptn)");
1713 for (int i
= 0; i
< max_entries
; i
++) {
1714 if (entries
[i
].name
== NULL
&&
1715 entries
[i
].base
== 0 &&
1716 entries
[i
].size
== 0)
1719 extract_firmware_partition(input_file
, firmware_offset
, &entries
[i
], output_directory
);
1725 static struct flash_partition_entry
*find_partition(
1726 struct flash_partition_entry
*entries
, size_t max_entries
,
1727 const char *name
, const char *error_msg
)
1729 for (int i
= 0; i
< max_entries
; i
++, entries
++) {
1730 if (strcmp(entries
->name
, name
) == 0)
1734 error(1, 0, "%s", error_msg
);
1738 static void write_ff(FILE *output_file
, size_t size
)
1743 memset(buf
, 0xff, sizeof(buf
));
1745 for (offset
= 0; offset
+ sizeof(buf
) < size
; offset
+= sizeof(buf
)) {
1746 if (fwrite(buf
, sizeof(buf
), 1, output_file
) < 0)
1747 error(1, errno
, "Can not write 0xff to output_file");
1750 /* write last chunk smaller than buffer */
1751 if (offset
< size
) {
1752 offset
= size
- offset
;
1753 if (fwrite(buf
, offset
, 1, output_file
) < 0)
1754 error(1, errno
, "Can not write partition to output_file");
1758 static void convert_firmware(const char *input
, const char *output
)
1760 struct flash_partition_entry fwup
[MAX_PARTITIONS
] = { 0 };
1761 struct flash_partition_entry flash
[MAX_PARTITIONS
] = { 0 };
1762 struct flash_partition_entry
*fwup_os_image
= NULL
, *fwup_file_system
= NULL
;
1763 struct flash_partition_entry
*flash_os_image
= NULL
, *flash_file_system
= NULL
;
1764 struct flash_partition_entry
*fwup_partition_table
= NULL
;
1765 size_t firmware_offset
= 0x1014;
1766 FILE *input_file
, *output_file
;
1768 struct stat statbuf
;
1770 /* check input file */
1771 if (stat(input
, &statbuf
)) {
1772 error(1, errno
, "Can not read input firmware %s", input
);
1775 input_file
= fopen(input
, "rb");
1777 error(1, 0, "Can not open input firmware %s", input
);
1779 output_file
= fopen(output
, "wb");
1781 error(1, 0, "Can not open output firmware %s", output
);
1783 if (read_partition_table(input_file
, firmware_offset
, fwup
, MAX_PARTITIONS
, 0) != 0) {
1784 error(1, 0, "Error can not read the partition table (fwup-ptn)");
1787 fwup_os_image
= find_partition(fwup
, MAX_PARTITIONS
,
1788 "os-image", "Error can not find os-image partition (fwup)");
1789 fwup_file_system
= find_partition(fwup
, MAX_PARTITIONS
,
1790 "file-system", "Error can not find file-system partition (fwup)");
1791 fwup_partition_table
= find_partition(fwup
, MAX_PARTITIONS
,
1792 "partition-table", "Error can not find partition-table partition");
1794 /* the flash partition table has a 0x00000004 magic haeder */
1795 if (read_partition_table(input_file
, firmware_offset
+ fwup_partition_table
->base
+ 4, flash
, MAX_PARTITIONS
, 1) != 0)
1796 error(1, 0, "Error can not read the partition table (flash)");
1798 flash_os_image
= find_partition(flash
, MAX_PARTITIONS
,
1799 "os-image", "Error can not find os-image partition (flash)");
1800 flash_file_system
= find_partition(flash
, MAX_PARTITIONS
,
1801 "file-system", "Error can not find file-system partition (flash)");
1803 /* write os_image to 0x0 */
1804 write_partition(input_file
, firmware_offset
, fwup_os_image
, output_file
);
1805 write_ff(output_file
, flash_os_image
->size
- fwup_os_image
->size
);
1807 /* write file-system behind os_image */
1808 fseek(output_file
, flash_file_system
->base
- flash_os_image
->base
, SEEK_SET
);
1809 write_partition(input_file
, firmware_offset
, fwup_file_system
, output_file
);
1810 write_ff(output_file
, flash_file_system
->size
- fwup_file_system
->size
);
1812 fclose(output_file
);
1816 int main(int argc
, char *argv
[]) {
1817 const char *board
= NULL
, *kernel_image
= NULL
, *rootfs_image
= NULL
, *output
= NULL
;
1818 const char *extract_image
= NULL
, *output_directory
= NULL
, *convert_image
= NULL
;
1819 bool add_jffs2_eof
= false, sysupgrade
= false;
1821 const struct device_info
*info
;
1822 set_source_date_epoch();
1827 c
= getopt(argc
, argv
, "B:k:r:o:V:jSh:x:d:z:");
1837 kernel_image
= optarg
;
1841 rootfs_image
= optarg
;
1849 sscanf(optarg
, "r%u", &rev
);
1853 add_jffs2_eof
= true;
1865 output_directory
= optarg
;
1869 extract_image
= optarg
;
1873 convert_image
= optarg
;
1882 if (extract_image
|| output_directory
) {
1884 error(1, 0, "No factory/oem image given via -x <file>. Output directory is only valid with -x");
1885 if (!output_directory
)
1886 error(1, 0, "Can not extract an image without output directory. Use -d <dir>");
1887 extract_firmware(extract_image
, output_directory
);
1888 } else if (convert_image
) {
1890 error(1, 0, "Can not convert a factory/oem image into sysupgrade image without output file. Use -o <file>");
1891 convert_firmware(convert_image
, output
);
1894 error(1, 0, "no board has been specified");
1896 error(1, 0, "no kernel image has been specified");
1898 error(1, 0, "no rootfs image has been specified");
1900 error(1, 0, "no output filename has been specified");
1902 info
= find_board(board
);
1905 error(1, 0, "unsupported board %s", board
);
1907 build_image(output
, kernel_image
, rootfs_image
, rev
, add_jffs2_eof
, sysupgrade
, info
);