ar71xx: switch more targets to dynamic partitioning
[openwrt/openwrt.git] / tools / firmware-utils / src / tplink-safeloader.c
1 /*
2 Copyright (c) 2014, Matthias Schiffer <mschiffer@universe-factory.net>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
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.
13
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.
24 */
25
26
27 /*
28 tplink-safeloader
29
30 Image generation tool for the TP-LINK SafeLoader as seen on
31 TP-LINK Pharos devices (CPE210/220/510/520)
32 */
33
34
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdbool.h>
38 #include <stdio.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43 #include <unistd.h>
44
45 #include <arpa/inet.h>
46
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <limits.h>
50
51 #include "md5.h"
52
53
54 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
55
56
57 #define MAX_PARTITIONS 32
58
59 /** An image partition table entry */
60 struct image_partition_entry {
61 const char *name;
62 size_t size;
63 uint8_t *data;
64 };
65
66 /** A flash partition table entry */
67 struct flash_partition_entry {
68 char *name;
69 uint32_t base;
70 uint32_t size;
71 };
72
73 /** Firmware layout description */
74 struct device_info {
75 const char *id;
76 const char *vendor;
77 const char *support_list;
78 char support_trail;
79 const char *soft_ver;
80 struct flash_partition_entry partitions[MAX_PARTITIONS+1];
81 const char *first_sysupgrade_partition;
82 const char *last_sysupgrade_partition;
83 };
84
85 /** The content of the soft-version structure */
86 struct __attribute__((__packed__)) soft_version {
87 uint32_t magic;
88 uint32_t zero;
89 uint8_t pad1;
90 uint8_t version_major;
91 uint8_t version_minor;
92 uint8_t version_patch;
93 uint8_t year_hi;
94 uint8_t year_lo;
95 uint8_t month;
96 uint8_t day;
97 uint32_t rev;
98 uint8_t pad2;
99 };
100
101
102 static const uint8_t jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
103
104
105 /**
106 Salt for the MD5 hash
107
108 Fortunately, TP-LINK seems to use the same salt for most devices which use
109 the new image format.
110 */
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,
116 };
117
118
119 /** Firmware layout table */
120 static struct device_info boards[] = {
121 /** Firmware layout for the CPE210/220 */
122 {
123 .id = "CPE210",
124 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
125 .support_list =
126 "SupportList:\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',
135 .soft_ver = NULL,
136
137 .partitions = {
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},
151 {NULL, 0, 0}
152 },
153
154 .first_sysupgrade_partition = "os-image",
155 .last_sysupgrade_partition = "support-list",
156 },
157
158 /** Firmware layout for the CPE210 V2 */
159 {
160 .id = "CPE210V2",
161 .vendor = "CPE210(TP-LINK|UN|N300-2|00000000):2.0\r\n",
162 .support_list =
163 "SupportList:\r\n"
164 "CPE210(TP-LINK|EU|N300-2|00000000):2.0\r\n"
165 "CPE210(TP-LINK|EU|N300-2|45550000):2.0\r\n"
166 "CPE210(TP-LINK|EU|N300-2|55530000):2.0\r\n"
167 "CPE210(TP-LINK|UN|N300-2|00000000):2.0\r\n"
168 "CPE210(TP-LINK|UN|N300-2|45550000):2.0\r\n"
169 "CPE210(TP-LINK|UN|N300-2|55530000):2.0\r\n"
170 "CPE210(TP-LINK|US|N300-2|55530000):2.0\r\n"
171 "CPE210(TP-LINK|UN|N300-2):2.0\r\n"
172 "CPE210(TP-LINK|EU|N300-2):2.0\r\n"
173 "CPE210(TP-LINK|US|N300-2):2.0\r\n",
174 .support_trail = '\xff',
175 .soft_ver = NULL,
176
177 .partitions = {
178 {"fs-uboot", 0x00000, 0x20000},
179 {"partition-table", 0x20000, 0x02000},
180 {"default-mac", 0x30000, 0x00020},
181 {"product-info", 0x31100, 0x00100},
182 {"device-info", 0x31400, 0x00400},
183 {"signature", 0x32000, 0x00400},
184 {"device-id", 0x33000, 0x00100},
185 {"os-image", 0x40000, 0x1c0000},
186 {"file-system", 0x200000, 0x5b0000},
187 {"soft-version", 0x7b0000, 0x00100},
188 {"support-list", 0x7b1000, 0x01000},
189 {"user-config", 0x7c0000, 0x10000},
190 {"default-config", 0x7d0000, 0x10000},
191 {"log", 0x7e0000, 0x10000},
192 {"radio", 0x7f0000, 0x10000},
193 {NULL, 0, 0}
194 },
195
196 .first_sysupgrade_partition = "os-image",
197 .last_sysupgrade_partition = "support-list",
198 },
199
200 /** Firmware layout for the CPE510/520 */
201 {
202 .id = "CPE510",
203 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
204 .support_list =
205 "SupportList:\r\n"
206 "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
207 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
208 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
209 "CPE510(TP-LINK|US|N300-5):1.1\r\n"
210 "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
211 "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
212 "CPE520(TP-LINK|US|N300-5):1.1\r\n"
213 "CPE520(TP-LINK|EU|N300-5):1.1\r\n"
214 "CPE510(TP-LINK|EU|N300-5|00000000):2.0\r\n"
215 "CPE510(TP-LINK|EU|N300-5|45550000):2.0\r\n"
216 "CPE510(TP-LINK|EU|N300-5|55530000):2.0\r\n"
217 "CPE510(TP-LINK|UN|N300-5|00000000):2.0\r\n"
218 "CPE510(TP-LINK|UN|N300-5|45550000):2.0\r\n"
219 "CPE510(TP-LINK|UN|N300-5|55530000):2.0\r\n"
220 "CPE510(TP-LINK|US|N300-5|55530000):2.0\r\n"
221 "CPE510(TP-LINK|UN|N300-5):2.0\r\n"
222 "CPE510(TP-LINK|EU|N300-5):2.0\r\n"
223 "CPE510(TP-LINK|US|N300-5):2.0\r\n",
224 .support_trail = '\xff',
225 .soft_ver = NULL,
226
227 .partitions = {
228 {"fs-uboot", 0x00000, 0x20000},
229 {"partition-table", 0x20000, 0x02000},
230 {"default-mac", 0x30000, 0x00020},
231 {"product-info", 0x31100, 0x00100},
232 {"signature", 0x32000, 0x00400},
233 {"os-image", 0x40000, 0x1c0000},
234 {"file-system", 0x200000, 0x5b0000},
235 {"soft-version", 0x7b0000, 0x00100},
236 {"support-list", 0x7b1000, 0x00400},
237 {"user-config", 0x7c0000, 0x10000},
238 {"default-config", 0x7d0000, 0x10000},
239 {"log", 0x7e0000, 0x10000},
240 {"radio", 0x7f0000, 0x10000},
241 {NULL, 0, 0}
242 },
243
244 .first_sysupgrade_partition = "os-image",
245 .last_sysupgrade_partition = "support-list",
246 },
247
248 {
249 .id = "WBS210",
250 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
251 .support_list =
252 "SupportList:\r\n"
253 "WBS210(TP-LINK|UN|N300-2):1.20\r\n"
254 "WBS210(TP-LINK|US|N300-2):1.20\r\n"
255 "WBS210(TP-LINK|EU|N300-2):1.20\r\n",
256 .support_trail = '\xff',
257 .soft_ver = NULL,
258
259 .partitions = {
260 {"fs-uboot", 0x00000, 0x20000},
261 {"partition-table", 0x20000, 0x02000},
262 {"default-mac", 0x30000, 0x00020},
263 {"product-info", 0x31100, 0x00100},
264 {"signature", 0x32000, 0x00400},
265 {"os-image", 0x40000, 0x1c0000},
266 {"file-system", 0x200000, 0x5b0000},
267 {"soft-version", 0x7b0000, 0x00100},
268 {"support-list", 0x7b1000, 0x00400},
269 {"user-config", 0x7c0000, 0x10000},
270 {"default-config", 0x7d0000, 0x10000},
271 {"log", 0x7e0000, 0x10000},
272 {"radio", 0x7f0000, 0x10000},
273 {NULL, 0, 0}
274 },
275
276 .first_sysupgrade_partition = "os-image",
277 .last_sysupgrade_partition = "support-list",
278 },
279
280 {
281 .id = "WBS510",
282 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
283 .support_list =
284 "SupportList:\r\n"
285 "WBS510(TP-LINK|UN|N300-5):1.20\r\n"
286 "WBS510(TP-LINK|US|N300-5):1.20\r\n"
287 "WBS510(TP-LINK|EU|N300-5):1.20\r\n",
288 .support_trail = '\xff',
289 .soft_ver = NULL,
290
291 .partitions = {
292 {"fs-uboot", 0x00000, 0x20000},
293 {"partition-table", 0x20000, 0x02000},
294 {"default-mac", 0x30000, 0x00020},
295 {"product-info", 0x31100, 0x00100},
296 {"signature", 0x32000, 0x00400},
297 {"os-image", 0x40000, 0x1c0000},
298 {"file-system", 0x200000, 0x5b0000},
299 {"soft-version", 0x7b0000, 0x00100},
300 {"support-list", 0x7b1000, 0x00400},
301 {"user-config", 0x7c0000, 0x10000},
302 {"default-config", 0x7d0000, 0x10000},
303 {"log", 0x7e0000, 0x10000},
304 {"radio", 0x7f0000, 0x10000},
305 {NULL, 0, 0}
306 },
307
308 .first_sysupgrade_partition = "os-image",
309 .last_sysupgrade_partition = "support-list",
310 },
311
312 /** Firmware layout for the C2600 */
313 {
314 .id = "C2600",
315 .vendor = "",
316 .support_list =
317 "SupportList:\r\n"
318 "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
319 .support_trail = '\x00',
320 .soft_ver = NULL,
321
322 /**
323 We use a bigger os-image partition than the stock images (and thus
324 smaller file-system), as our kernel doesn't fit in the stock firmware's
325 2 MB os-image since kernel 4.14.
326 */
327 .partitions = {
328 {"SBL1", 0x00000, 0x20000},
329 {"MIBIB", 0x20000, 0x20000},
330 {"SBL2", 0x40000, 0x20000},
331 {"SBL3", 0x60000, 0x30000},
332 {"DDRCONFIG", 0x90000, 0x10000},
333 {"SSD", 0xa0000, 0x10000},
334 {"TZ", 0xb0000, 0x30000},
335 {"RPM", 0xe0000, 0x20000},
336 {"fs-uboot", 0x100000, 0x70000},
337 {"uboot-env", 0x170000, 0x40000},
338 {"radio", 0x1b0000, 0x40000},
339 {"os-image", 0x1f0000, 0x400000}, /* Stock: base 0x1f0000 size 0x200000 */
340 {"file-system", 0x5f0000, 0x1900000}, /* Stock: base 0x3f0000 size 0x1b00000 */
341 {"default-mac", 0x1ef0000, 0x00200},
342 {"pin", 0x1ef0200, 0x00200},
343 {"product-info", 0x1ef0400, 0x0fc00},
344 {"partition-table", 0x1f00000, 0x10000},
345 {"soft-version", 0x1f10000, 0x10000},
346 {"support-list", 0x1f20000, 0x10000},
347 {"profile", 0x1f30000, 0x10000},
348 {"default-config", 0x1f40000, 0x10000},
349 {"user-config", 0x1f50000, 0x40000},
350 {"qos-db", 0x1f90000, 0x40000},
351 {"usb-config", 0x1fd0000, 0x10000},
352 {"log", 0x1fe0000, 0x20000},
353 {NULL, 0, 0}
354 },
355
356 .first_sysupgrade_partition = "os-image",
357 .last_sysupgrade_partition = "file-system"
358 },
359
360 /** Firmware layout for the C25v1 */
361 {
362 .id = "ARCHER-C25-V1",
363 .support_list =
364 "SupportList:\n"
365 "{product_name:ArcherC25,product_ver:1.0.0,special_id:00000000}\n"
366 "{product_name:ArcherC25,product_ver:1.0.0,special_id:55530000}\n"
367 "{product_name:ArcherC25,product_ver:1.0.0,special_id:45550000}\n",
368 .support_trail = '\x00',
369 .soft_ver = "soft_ver:1.0.0\n",
370
371 /* We're using a dynamic kernel/rootfs split here */
372 .partitions = {
373 {"factory-boot", 0x00000, 0x20000},
374 {"fs-uboot", 0x20000, 0x10000},
375 {"firmware", 0x30000, 0x7a0000}, /* Stock: name os-image base 0x30000 size 0x100000 */
376 /* Stock: name file-system base 0x130000 size 0x6a0000 */
377 {"user-config", 0x7d0000, 0x04000},
378 {"default-mac", 0x7e0000, 0x00100},
379 {"device-id", 0x7e0100, 0x00100},
380 {"extra-para", 0x7e0200, 0x00100},
381 {"pin", 0x7e0300, 0x00100},
382 {"support-list", 0x7e0400, 0x00400},
383 {"soft-version", 0x7e0800, 0x00400},
384 {"product-info", 0x7e0c00, 0x01400},
385 {"partition-table", 0x7e2000, 0x01000},
386 {"profile", 0x7e3000, 0x01000},
387 {"default-config", 0x7e4000, 0x04000},
388 {"merge-config", 0x7ec000, 0x02000},
389 {"qos-db", 0x7ee000, 0x02000},
390 {"radio", 0x7f0000, 0x10000},
391 {NULL, 0, 0}
392 },
393
394 .first_sysupgrade_partition = "os-image",
395 .last_sysupgrade_partition = "file-system",
396 },
397
398 /** Firmware layout for the C58v1 */
399 {
400 .id = "ARCHER-C58-V1",
401 .vendor = "",
402 .support_list =
403 "SupportList:\r\n"
404 "{product_name:Archer C58,product_ver:1.0.0,special_id:00000000}\r\n"
405 "{product_name:Archer C58,product_ver:1.0.0,special_id:45550000}\r\n"
406 "{product_name:Archer C58,product_ver:1.0.0,special_id:55530000}\r\n",
407 .support_trail = '\x00',
408 .soft_ver = "soft_ver:1.0.0\n",
409
410 .partitions = {
411 {"fs-uboot", 0x00000, 0x10000},
412 {"default-mac", 0x10000, 0x00200},
413 {"pin", 0x10200, 0x00200},
414 {"product-info", 0x10400, 0x00100},
415 {"partition-table", 0x10500, 0x00800},
416 {"soft-version", 0x11300, 0x00200},
417 {"support-list", 0x11500, 0x00100},
418 {"device-id", 0x11600, 0x00100},
419 {"profile", 0x11700, 0x03900},
420 {"default-config", 0x15000, 0x04000},
421 {"user-config", 0x19000, 0x04000},
422 {"os-image", 0x20000, 0x180000},
423 {"file-system", 0x1a0000, 0x648000},
424 {"certyficate", 0x7e8000, 0x08000},
425 {"radio", 0x7f0000, 0x10000},
426 {NULL, 0, 0}
427 },
428
429 .first_sysupgrade_partition = "os-image",
430 .last_sysupgrade_partition = "file-system",
431 },
432
433 /** Firmware layout for the C59v1 */
434 {
435 .id = "ARCHER-C59-V1",
436 .vendor = "",
437 .support_list =
438 "SupportList:\r\n"
439 "{product_name:Archer C59,product_ver:1.0.0,special_id:00000000}\r\n"
440 "{product_name:Archer C59,product_ver:1.0.0,special_id:45550000}\r\n"
441 "{product_name:Archer C59,product_ver:1.0.0,special_id:52550000}\r\n"
442 "{product_name:Archer C59,product_ver:1.0.0,special_id:55530000}\r\n",
443 .support_trail = '\x00',
444 .soft_ver = "soft_ver:1.0.0\n",
445
446 /* We're using a dynamic kernel/rootfs split here */
447 .partitions = {
448 {"fs-uboot", 0x00000, 0x10000},
449 {"default-mac", 0x10000, 0x00200},
450 {"pin", 0x10200, 0x00200},
451 {"device-id", 0x10400, 0x00100},
452 {"product-info", 0x10500, 0x0fb00},
453 {"firmware", 0x20000, 0xe30000},
454 {"partition-table", 0xe50000, 0x10000},
455 {"soft-version", 0xe60000, 0x10000},
456 {"support-list", 0xe70000, 0x10000},
457 {"profile", 0xe80000, 0x10000},
458 {"default-config", 0xe90000, 0x10000},
459 {"user-config", 0xea0000, 0x40000},
460 {"usb-config", 0xee0000, 0x10000},
461 {"certificate", 0xef0000, 0x10000},
462 {"qos-db", 0xf00000, 0x40000},
463 {"log", 0xfe0000, 0x10000},
464 {"radio", 0xff0000, 0x10000},
465 {NULL, 0, 0}
466 },
467
468 .first_sysupgrade_partition = "os-image",
469 .last_sysupgrade_partition = "file-system",
470 },
471
472 /** Firmware layout for the C59v2 */
473 {
474 .id = "ARCHER-C59-V2",
475 .vendor = "",
476 .support_list =
477 "SupportList:\r\n"
478 "{product_name:Archer C59,product_ver:2.0.0,special_id:00000000}\r\n"
479 "{product_name:Archer C59,product_ver:2.0.0,special_id:45550000}\r\n"
480 "{product_name:Archer C59,product_ver:2.0.0,special_id:55530000}\r\n",
481 .support_trail = '\x00',
482 .soft_ver = "soft_ver:2.0.0 Build 20161206 rel.7303\n",
483
484 /** We're using a dynamic kernel/rootfs split here */
485 .partitions = {
486 {"factory-boot", 0x00000, 0x20000},
487 {"fs-uboot", 0x20000, 0x10000},
488 {"default-mac", 0x30000, 0x00200},
489 {"pin", 0x30200, 0x00200},
490 {"device-id", 0x30400, 0x00100},
491 {"product-info", 0x30500, 0x0fb00},
492 {"firmware", 0x40000, 0xe10000},
493 {"partition-table", 0xe50000, 0x10000},
494 {"soft-version", 0xe60000, 0x10000},
495 {"support-list", 0xe70000, 0x10000},
496 {"profile", 0xe80000, 0x10000},
497 {"default-config", 0xe90000, 0x10000},
498 {"user-config", 0xea0000, 0x40000},
499 {"usb-config", 0xee0000, 0x10000},
500 {"certificate", 0xef0000, 0x10000},
501 {"extra-para", 0xf00000, 0x10000},
502 {"qos-db", 0xf10000, 0x30000},
503 {"log", 0xfe0000, 0x10000},
504 {"radio", 0xff0000, 0x10000},
505 {NULL, 0, 0}
506 },
507
508 .first_sysupgrade_partition = "os-image",
509 .last_sysupgrade_partition = "file-system",
510 },
511
512 /** Firmware layout for the C60v1 */
513 {
514 .id = "ARCHER-C60-V1",
515 .vendor = "",
516 .support_list =
517 "SupportList:\r\n"
518 "{product_name:Archer C60,product_ver:1.0.0,special_id:00000000}\r\n"
519 "{product_name:Archer C60,product_ver:1.0.0,special_id:45550000}\r\n"
520 "{product_name:Archer C60,product_ver:1.0.0,special_id:55530000}\r\n",
521 .support_trail = '\x00',
522 .soft_ver = "soft_ver:1.0.0\n",
523
524 .partitions = {
525 {"fs-uboot", 0x00000, 0x10000},
526 {"default-mac", 0x10000, 0x00200},
527 {"pin", 0x10200, 0x00200},
528 {"product-info", 0x10400, 0x00100},
529 {"partition-table", 0x10500, 0x00800},
530 {"soft-version", 0x11300, 0x00200},
531 {"support-list", 0x11500, 0x00100},
532 {"device-id", 0x11600, 0x00100},
533 {"profile", 0x11700, 0x03900},
534 {"default-config", 0x15000, 0x04000},
535 {"user-config", 0x19000, 0x04000},
536 {"os-image", 0x20000, 0x180000},
537 {"file-system", 0x1a0000, 0x648000},
538 {"certyficate", 0x7e8000, 0x08000},
539 {"radio", 0x7f0000, 0x10000},
540 {NULL, 0, 0}
541 },
542
543 .first_sysupgrade_partition = "os-image",
544 .last_sysupgrade_partition = "file-system",
545 },
546
547 /** Firmware layout for the C60v2 */
548 {
549 .id = "ARCHER-C60-V2",
550 .vendor = "",
551 .support_list =
552 "SupportList:\r\n"
553 "{product_name:Archer C60,product_ver:2.0.0,special_id:42520000}\r\n"
554 "{product_name:Archer C60,product_ver:2.0.0,special_id:45550000}\r\n"
555 "{product_name:Archer C60,product_ver:2.0.0,special_id:55530000}\r\n",
556 .support_trail = '\x00',
557 .soft_ver = "soft_ver:2.0.0\n",
558
559 .partitions = {
560 {"factory-boot", 0x00000, 0x1fb00},
561 {"default-mac", 0x1fb00, 0x00200},
562 {"pin", 0x1fd00, 0x00100},
563 {"product-info", 0x1fe00, 0x00100},
564 {"device-id", 0x1ff00, 0x00100},
565 {"fs-uboot", 0x20000, 0x10000},
566 {"os-image", 0x30000, 0x180000},
567 {"file-system", 0x1b0000, 0x620000},
568 {"soft-version", 0x7d9500, 0x00100},
569 {"support-list", 0x7d9600, 0x00100},
570 {"extra-para", 0x7d9700, 0x00100},
571 {"profile", 0x7d9800, 0x03000},
572 {"default-config", 0x7dc800, 0x03000},
573 {"partition-table", 0x7df800, 0x00800},
574 {"user-config", 0x7e0000, 0x0c000},
575 {"certificate", 0x7ec000, 0x04000},
576 {"radio", 0x7f0000, 0x10000},
577 {NULL, 0, 0}
578 },
579
580 .first_sysupgrade_partition = "os-image",
581 .last_sysupgrade_partition = "file-system",
582 },
583
584 /** Firmware layout for the C5 */
585 {
586 .id = "ARCHER-C5-V2",
587 .vendor = "",
588 .support_list =
589 "SupportList:\r\n"
590 "{product_name:ArcherC5,product_ver:2.0.0,special_id:00000000}\r\n"
591 "{product_name:ArcherC5,product_ver:2.0.0,special_id:55530000}\r\n"
592 "{product_name:ArcherC5,product_ver:2.0.0,special_id:4A500000}\r\n", /* JP version */
593 .support_trail = '\x00',
594 .soft_ver = NULL,
595
596 .partitions = {
597 {"fs-uboot", 0x00000, 0x40000},
598 {"os-image", 0x40000, 0x200000},
599 {"file-system", 0x240000, 0xc00000},
600 {"default-mac", 0xe40000, 0x00200},
601 {"pin", 0xe40200, 0x00200},
602 {"product-info", 0xe40400, 0x00200},
603 {"partition-table", 0xe50000, 0x10000},
604 {"soft-version", 0xe60000, 0x00200},
605 {"support-list", 0xe61000, 0x0f000},
606 {"profile", 0xe70000, 0x10000},
607 {"default-config", 0xe80000, 0x10000},
608 {"user-config", 0xe90000, 0x50000},
609 {"log", 0xee0000, 0x100000},
610 {"radio_bk", 0xfe0000, 0x10000},
611 {"radio", 0xff0000, 0x10000},
612 {NULL, 0, 0}
613 },
614
615 .first_sysupgrade_partition = "os-image",
616 .last_sysupgrade_partition = "file-system"
617 },
618
619 /** Firmware layout for the C7 */
620 {
621 .id = "ARCHER-C7-V4",
622 .support_list =
623 "SupportList:\n"
624 "{product_name:Archer C7,product_ver:4.0.0,special_id:00000000}\n"
625 "{product_name:Archer C7,product_ver:4.0.0,special_id:41550000}\n"
626 "{product_name:Archer C7,product_ver:4.0.0,special_id:45550000}\n"
627 "{product_name:Archer C7,product_ver:4.0.0,special_id:4B520000}\n"
628 "{product_name:Archer C7,product_ver:4.0.0,special_id:42520000}\n"
629 "{product_name:Archer C7,product_ver:4.0.0,special_id:4A500000}\n"
630 "{product_name:Archer C7,product_ver:4.0.0,special_id:52550000}\n"
631 "{product_name:Archer C7,product_ver:4.0.0,special_id:54570000}\n"
632 "{product_name:Archer C7,product_ver:4.0.0,special_id:55530000}\n"
633 "{product_name:Archer C7,product_ver:4.0.0,special_id:43410000}\n",
634 .support_trail = '\x00',
635 .soft_ver = "soft_ver:1.0.0\n",
636
637 /* We're using a dynamic kernel/rootfs split here */
638 .partitions = {
639 {"factory-boot", 0x00000, 0x20000},
640 {"fs-uboot", 0x20000, 0x20000},
641 {"firmware", 0x40000, 0xEC0000}, /* Stock: name os-image base 0x40000 size 0x120000 */
642 /* Stock: name file-system base 0x160000 size 0xda0000 */
643 {"default-mac", 0xf00000, 0x00200},
644 {"pin", 0xf00200, 0x00200},
645 {"device-id", 0xf00400, 0x00100},
646 {"product-info", 0xf00500, 0x0fb00},
647 {"soft-version", 0xf10000, 0x00100},
648 {"extra-para", 0xf11000, 0x01000},
649 {"support-list", 0xf12000, 0x0a000},
650 {"profile", 0xf1c000, 0x04000},
651 {"default-config", 0xf20000, 0x10000},
652 {"user-config", 0xf30000, 0x40000},
653 {"qos-db", 0xf70000, 0x40000},
654 {"certificate", 0xfb0000, 0x10000},
655 {"partition-table", 0xfc0000, 0x10000},
656 {"log", 0xfd0000, 0x20000},
657 {"radio", 0xff0000, 0x10000},
658 {NULL, 0, 0}
659 },
660
661 .first_sysupgrade_partition = "os-image",
662 .last_sysupgrade_partition = "file-system",
663 },
664
665 /** Firmware layout for the C7 v5*/
666 {
667 .id = "ARCHER-C7-V5",
668 .support_list =
669 "SupportList:\n"
670 "{product_name:Archer C7,product_ver:5.0.0,special_id:00000000}\n"
671 "{product_name:Archer C7,product_ver:5.0.0,special_id:55530000}\n",
672
673 .support_trail = '\x00',
674 .soft_ver = "soft_ver:1.0.0\n",
675
676 /* We're using a dynamic kernel/rootfs split here */
677 .partitions = {
678 {"factory-boot", 0x00000, 0x20000},
679 {"fs-uboot", 0x20000, 0x20000},
680 {"partition-table", 0x40000, 0x10000},
681 {"radio", 0x50000, 0x10000},
682 {"default-mac", 0x60000, 0x00200},
683 {"pin", 0x60200, 0x00200},
684 {"device-id", 0x60400, 0x00100},
685 {"product-info", 0x60500, 0x0fb00},
686 {"soft-version", 0x70000, 0x01000},
687 {"extra-para", 0x71000, 0x01000},
688 {"support-list", 0x72000, 0x0a000},
689 {"profile", 0x7c000, 0x04000},
690 {"user-config", 0x80000, 0x40000},
691
692
693 {"firmware", 0xc0000, 0xf00000}, /* Stock: name os-image base 0xc0000 size 0x120000 */
694 /* Stock: name file-system base 0x1e0000 size 0xde0000 */
695
696 {"log", 0xfc0000, 0x20000},
697 {"certificate", 0xfe0000, 0x10000},
698 {"default-config", 0xff0000, 0x10000},
699 {NULL, 0, 0}
700
701 },
702
703 .first_sysupgrade_partition = "os-image",
704 .last_sysupgrade_partition = "file-system",
705 },
706
707 /** Firmware layout for the C9 */
708 {
709 .id = "ARCHERC9",
710 .vendor = "",
711 .support_list =
712 "SupportList:\n"
713 "{product_name:ArcherC9,"
714 "product_ver:1.0.0,"
715 "special_id:00000000}\n",
716 .support_trail = '\x00',
717 .soft_ver = NULL,
718
719 .partitions = {
720 {"fs-uboot", 0x00000, 0x40000},
721 {"os-image", 0x40000, 0x200000},
722 {"file-system", 0x240000, 0xc00000},
723 {"default-mac", 0xe40000, 0x00200},
724 {"pin", 0xe40200, 0x00200},
725 {"product-info", 0xe40400, 0x00200},
726 {"partition-table", 0xe50000, 0x10000},
727 {"soft-version", 0xe60000, 0x00200},
728 {"support-list", 0xe61000, 0x0f000},
729 {"profile", 0xe70000, 0x10000},
730 {"default-config", 0xe80000, 0x10000},
731 {"user-config", 0xe90000, 0x50000},
732 {"log", 0xee0000, 0x100000},
733 {"radio_bk", 0xfe0000, 0x10000},
734 {"radio", 0xff0000, 0x10000},
735 {NULL, 0, 0}
736 },
737
738 .first_sysupgrade_partition = "os-image",
739 .last_sysupgrade_partition = "file-system"
740 },
741
742 /** Firmware layout for the EAP120 */
743 {
744 .id = "EAP120",
745 .vendor = "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
746 .support_list =
747 "SupportList:\r\n"
748 "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
749 .support_trail = '\xff',
750 .soft_ver = NULL,
751
752 .partitions = {
753 {"fs-uboot", 0x00000, 0x20000},
754 {"partition-table", 0x20000, 0x02000},
755 {"default-mac", 0x30000, 0x00020},
756 {"support-list", 0x31000, 0x00100},
757 {"product-info", 0x31100, 0x00100},
758 {"soft-version", 0x32000, 0x00100},
759 {"os-image", 0x40000, 0x180000},
760 {"file-system", 0x1c0000, 0x600000},
761 {"user-config", 0x7c0000, 0x10000},
762 {"backup-config", 0x7d0000, 0x10000},
763 {"log", 0x7e0000, 0x10000},
764 {"radio", 0x7f0000, 0x10000},
765 {NULL, 0, 0}
766 },
767
768 .first_sysupgrade_partition = "os-image",
769 .last_sysupgrade_partition = "file-system"
770 },
771
772 /** Firmware layout for the TL-WA850RE v2 */
773 {
774 .id = "TLWA850REV2",
775 .vendor = "",
776 .support_list =
777 "SupportList:\n"
778 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
779 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
780 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
781 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
782 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
783 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
784 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
785 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
786 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
787 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
788 .support_trail = '\x00',
789 .soft_ver = NULL,
790
791 /**
792 576KB were moved from file-system to os-image
793 in comparison to the stock image
794 */
795 .partitions = {
796 {"fs-uboot", 0x00000, 0x20000},
797 {"os-image", 0x20000, 0x150000},
798 {"file-system", 0x170000, 0x240000},
799 {"partition-table", 0x3b0000, 0x02000},
800 {"default-mac", 0x3c0000, 0x00020},
801 {"pin", 0x3c0100, 0x00020},
802 {"product-info", 0x3c1000, 0x01000},
803 {"soft-version", 0x3c2000, 0x00100},
804 {"support-list", 0x3c3000, 0x01000},
805 {"profile", 0x3c4000, 0x08000},
806 {"user-config", 0x3d0000, 0x10000},
807 {"default-config", 0x3e0000, 0x10000},
808 {"radio", 0x3f0000, 0x10000},
809 {NULL, 0, 0}
810 },
811
812 .first_sysupgrade_partition = "os-image",
813 .last_sysupgrade_partition = "file-system"
814 },
815
816 /** Firmware layout for the TL-WA855RE v1 */
817 {
818 .id = "TLWA855REV1",
819 .vendor = "",
820 .support_list =
821 "SupportList:\n"
822 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
823 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
824 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
825 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
826 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
827 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
828 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
829 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
830 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
831 .support_trail = '\x00',
832 .soft_ver = NULL,
833
834 .partitions = {
835 {"fs-uboot", 0x00000, 0x20000},
836 {"os-image", 0x20000, 0x150000},
837 {"file-system", 0x170000, 0x240000},
838 {"partition-table", 0x3b0000, 0x02000},
839 {"default-mac", 0x3c0000, 0x00020},
840 {"pin", 0x3c0100, 0x00020},
841 {"product-info", 0x3c1000, 0x01000},
842 {"soft-version", 0x3c2000, 0x00100},
843 {"support-list", 0x3c3000, 0x01000},
844 {"profile", 0x3c4000, 0x08000},
845 {"user-config", 0x3d0000, 0x10000},
846 {"default-config", 0x3e0000, 0x10000},
847 {"radio", 0x3f0000, 0x10000},
848 {NULL, 0, 0}
849 },
850
851 .first_sysupgrade_partition = "os-image",
852 .last_sysupgrade_partition = "file-system"
853 },
854
855 /** Firmware layout for the TL-WR1043 v5 */
856 {
857 .id = "TLWR1043NV5",
858 .vendor = "",
859 .support_list =
860 "SupportList:\n"
861 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:45550000}\n"
862 "{product_name:TL-WR1043N,product_ver:5.0.0,special_id:55530000}\n",
863 .support_trail = '\x00',
864 .soft_ver = "soft_ver:1.0.0\n",
865 .partitions = {
866 {"factory-boot", 0x00000, 0x20000},
867 {"fs-uboot", 0x20000, 0x20000},
868 {"os-image", 0x40000, 0x180000},
869 {"file-system", 0x1c0000, 0xd40000},
870 {"default-mac", 0xf00000, 0x00200},
871 {"pin", 0xf00200, 0x00200},
872 {"device-id", 0xf00400, 0x00100},
873 {"product-info", 0xf00500, 0x0fb00},
874 {"soft-version", 0xf10000, 0x01000},
875 {"extra-para", 0xf11000, 0x01000},
876 {"support-list", 0xf12000, 0x0a000},
877 {"profile", 0xf1c000, 0x04000},
878 {"default-config", 0xf20000, 0x10000},
879 {"user-config", 0xf30000, 0x40000},
880 {"qos-db", 0xf70000, 0x40000},
881 {"certificate", 0xfb0000, 0x10000},
882 {"partition-table", 0xfc0000, 0x10000},
883 {"log", 0xfd0000, 0x20000},
884 {"radio", 0xff0000, 0x10000},
885 {NULL, 0, 0}
886 },
887 .first_sysupgrade_partition = "os-image",
888 .last_sysupgrade_partition = "file-system"
889 },
890
891 /** Firmware layout for the TL-WR1043 v4 */
892 {
893 .id = "TLWR1043NDV4",
894 .vendor = "",
895 .support_list =
896 "SupportList:\n"
897 "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
898 .support_trail = '\x00',
899 .soft_ver = NULL,
900
901 /* We're using a dynamic kernel/rootfs split here */
902 .partitions = {
903 {"fs-uboot", 0x00000, 0x20000},
904 {"firmware", 0x20000, 0xf30000},
905 {"default-mac", 0xf50000, 0x00200},
906 {"pin", 0xf50200, 0x00200},
907 {"product-info", 0xf50400, 0x0fc00},
908 {"soft-version", 0xf60000, 0x0b000},
909 {"support-list", 0xf6b000, 0x04000},
910 {"profile", 0xf70000, 0x04000},
911 {"default-config", 0xf74000, 0x0b000},
912 {"user-config", 0xf80000, 0x40000},
913 {"partition-table", 0xfc0000, 0x10000},
914 {"log", 0xfd0000, 0x20000},
915 {"radio", 0xff0000, 0x10000},
916 {NULL, 0, 0}
917 },
918
919 .first_sysupgrade_partition = "os-image",
920 .last_sysupgrade_partition = "file-system"
921 },
922
923 /** Firmware layout for the TL-WR902AC v1 */
924 {
925 .id = "TL-WR902AC-V1",
926 .vendor = "",
927 .support_list =
928 "SupportList:\n"
929 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:45550000}\n"
930 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:55530000}\n",
931 .support_trail = '\x00',
932 .soft_ver = NULL,
933
934 /**
935 384KB were moved from file-system to os-image
936 in comparison to the stock image
937 */
938 .partitions = {
939 {"fs-uboot", 0x00000, 0x20000},
940 {"os-image", 0x20000, 0x180000},
941 {"file-system", 0x1a0000, 0x5b0000},
942 {"default-mac", 0x750000, 0x00200},
943 {"pin", 0x750200, 0x00200},
944 {"product-info", 0x750400, 0x0fc00},
945 {"soft-version", 0x760000, 0x0b000},
946 {"support-list", 0x76b000, 0x04000},
947 {"profile", 0x770000, 0x04000},
948 {"default-config", 0x774000, 0x0b000},
949 {"user-config", 0x780000, 0x40000},
950 {"partition-table", 0x7c0000, 0x10000},
951 {"log", 0x7d0000, 0x20000},
952 {"radio", 0x7f0000, 0x10000},
953 {NULL, 0, 0}
954 },
955
956 .first_sysupgrade_partition = "os-image",
957 .last_sysupgrade_partition = "file-system",
958 },
959
960 /** Firmware layout for the TL-WR942N V1 */
961 {
962 .id = "TLWR942NV1",
963 .vendor = "",
964 .support_list =
965 "SupportList:\r\n"
966 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
967 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
968 .support_trail = '\x00',
969 .soft_ver = NULL,
970
971 .partitions = {
972 {"fs-uboot", 0x00000, 0x20000},
973 {"os-image", 0x20000, 0x180000},
974 {"file-system", 0x1a0000, 0xca0000},
975 {"default-mac", 0xe40000, 0x00200},
976 {"pin", 0xe40200, 0x00200},
977 {"product-info", 0xe40400, 0x0fc00},
978 {"partition-table", 0xe50000, 0x10000},
979 {"soft-version", 0xe60000, 0x10000},
980 {"support-list", 0xe70000, 0x10000},
981 {"profile", 0xe80000, 0x10000},
982 {"default-config", 0xe90000, 0x10000},
983 {"user-config", 0xea0000, 0x40000},
984 {"qos-db", 0xee0000, 0x40000},
985 {"certificate", 0xf20000, 0x10000},
986 {"usb-config", 0xfb0000, 0x10000},
987 {"log", 0xfc0000, 0x20000},
988 {"radio-bk", 0xfe0000, 0x10000},
989 {"radio", 0xff0000, 0x10000},
990 {NULL, 0, 0}
991 },
992
993 .first_sysupgrade_partition = "os-image",
994 .last_sysupgrade_partition = "file-system",
995 },
996
997 /** Firmware layout for the RE350 v1 */
998 {
999 .id = "RE350-V1",
1000 .vendor = "",
1001 .support_list =
1002 "SupportList:\n"
1003 "{product_name:RE350,product_ver:1.0.0,special_id:45550000}\n"
1004 "{product_name:RE350,product_ver:1.0.0,special_id:00000000}\n"
1005 "{product_name:RE350,product_ver:1.0.0,special_id:41550000}\n"
1006 "{product_name:RE350,product_ver:1.0.0,special_id:55530000}\n"
1007 "{product_name:RE350,product_ver:1.0.0,special_id:43410000}\n"
1008 "{product_name:RE350,product_ver:1.0.0,special_id:4b520000}\n"
1009 "{product_name:RE350,product_ver:1.0.0,special_id:4a500000}\n",
1010 .support_trail = '\x00',
1011 .soft_ver = NULL,
1012
1013 /** We're using a dynamic kernel/rootfs split here */
1014 .partitions = {
1015 {"fs-uboot", 0x00000, 0x20000},
1016 {"firmware", 0x20000, 0x5e0000},
1017 {"partition-table", 0x600000, 0x02000},
1018 {"default-mac", 0x610000, 0x00020},
1019 {"pin", 0x610100, 0x00020},
1020 {"product-info", 0x611100, 0x01000},
1021 {"soft-version", 0x620000, 0x01000},
1022 {"support-list", 0x621000, 0x01000},
1023 {"profile", 0x622000, 0x08000},
1024 {"user-config", 0x630000, 0x10000},
1025 {"default-config", 0x640000, 0x10000},
1026 {"radio", 0x7f0000, 0x10000},
1027 {NULL, 0, 0}
1028 },
1029
1030 .first_sysupgrade_partition = "os-image",
1031 .last_sysupgrade_partition = "file-system"
1032 },
1033
1034 /** Firmware layout for the RE355 */
1035 {
1036 .id = "RE355",
1037 .vendor = "",
1038 .support_list =
1039 "SupportList:\r\n"
1040 "{product_name:RE355,product_ver:1.0.0,special_id:00000000}\r\n"
1041 "{product_name:RE355,product_ver:1.0.0,special_id:55530000}\r\n"
1042 "{product_name:RE355,product_ver:1.0.0,special_id:45550000}\r\n"
1043 "{product_name:RE355,product_ver:1.0.0,special_id:4A500000}\r\n"
1044 "{product_name:RE355,product_ver:1.0.0,special_id:43410000}\r\n"
1045 "{product_name:RE355,product_ver:1.0.0,special_id:41550000}\r\n"
1046 "{product_name:RE355,product_ver:1.0.0,special_id:4B520000}\r\n"
1047 "{product_name:RE355,product_ver:1.0.0,special_id:55534100}\r\n",
1048 .support_trail = '\x00',
1049 .soft_ver = NULL,
1050
1051 /* We're using a dynamic kernel/rootfs split here */
1052 .partitions = {
1053 {"fs-uboot", 0x00000, 0x20000},
1054 {"firmware", 0x20000, 0x5e0000},
1055 {"partition-table", 0x600000, 0x02000},
1056 {"default-mac", 0x610000, 0x00020},
1057 {"pin", 0x610100, 0x00020},
1058 {"product-info", 0x611100, 0x01000},
1059 {"soft-version", 0x620000, 0x01000},
1060 {"support-list", 0x621000, 0x01000},
1061 {"profile", 0x622000, 0x08000},
1062 {"user-config", 0x630000, 0x10000},
1063 {"default-config", 0x640000, 0x10000},
1064 {"radio", 0x7f0000, 0x10000},
1065 {NULL, 0, 0}
1066 },
1067
1068 .first_sysupgrade_partition = "os-image",
1069 .last_sysupgrade_partition = "file-system"
1070 },
1071
1072 /** Firmware layout for the RE450 */
1073 {
1074 .id = "RE450",
1075 .vendor = "",
1076 .support_list =
1077 "SupportList:\r\n"
1078 "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
1079 "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
1080 "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
1081 "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
1082 "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
1083 "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
1084 "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
1085 "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
1086 .support_trail = '\x00',
1087 .soft_ver = NULL,
1088
1089 /** We're using a dynamic kernel/rootfs split here */
1090 .partitions = {
1091 {"fs-uboot", 0x00000, 0x20000},
1092 {"firmware", 0x20000, 0x5e0000},
1093 {"partition-table", 0x600000, 0x02000},
1094 {"default-mac", 0x610000, 0x00020},
1095 {"pin", 0x610100, 0x00020},
1096 {"product-info", 0x611100, 0x01000},
1097 {"soft-version", 0x620000, 0x01000},
1098 {"support-list", 0x621000, 0x01000},
1099 {"profile", 0x622000, 0x08000},
1100 {"user-config", 0x630000, 0x10000},
1101 {"default-config", 0x640000, 0x10000},
1102 {"radio", 0x7f0000, 0x10000},
1103 {NULL, 0, 0}
1104 },
1105
1106 .first_sysupgrade_partition = "os-image",
1107 .last_sysupgrade_partition = "file-system"
1108 },
1109
1110 /** Firmware layout for the RE450 v2 */
1111 {
1112 .id = "RE450-V2",
1113 .vendor = "",
1114 .support_list =
1115 "SupportList:\r\n"
1116 "{product_name:RE450,product_ver:2.0.0,special_id:00000000}\r\n"
1117 "{product_name:RE450,product_ver:2.0.0,special_id:55530000}\r\n"
1118 "{product_name:RE450,product_ver:2.0.0,special_id:45550000}\r\n"
1119 "{product_name:RE450,product_ver:2.0.0,special_id:4A500000}\r\n"
1120 "{product_name:RE450,product_ver:2.0.0,special_id:43410000}\r\n"
1121 "{product_name:RE450,product_ver:2.0.0,special_id:41550000}\r\n"
1122 "{product_name:RE450,product_ver:2.0.0,special_id:41530000}\r\n"
1123 "{product_name:RE450,product_ver:2.0.0,special_id:4B520000}\r\n"
1124 "{product_name:RE450,product_ver:2.0.0,special_id:42520000}\r\n",
1125 .support_trail = '\x00',
1126 .soft_ver = NULL,
1127
1128 /* We're using a dynamic kernel/rootfs split here */
1129 .partitions = {
1130 {"fs-uboot", 0x00000, 0x20000},
1131 {"firmware", 0x20000, 0x5e0000},
1132 {"partition-table", 0x600000, 0x02000},
1133 {"default-mac", 0x610000, 0x00020},
1134 {"pin", 0x610100, 0x00020},
1135 {"product-info", 0x611100, 0x01000},
1136 {"soft-version", 0x620000, 0x01000},
1137 {"support-list", 0x621000, 0x01000},
1138 {"profile", 0x622000, 0x08000},
1139 {"user-config", 0x630000, 0x10000},
1140 {"default-config", 0x640000, 0x10000},
1141 {"radio", 0x7f0000, 0x10000},
1142
1143 {NULL, 0, 0}
1144 },
1145
1146 .first_sysupgrade_partition = "os-image",
1147 .last_sysupgrade_partition = "file-system"
1148 },
1149
1150 {}
1151 };
1152
1153 #define error(_ret, _errno, _str, ...) \
1154 do { \
1155 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
1156 strerror(_errno)); \
1157 if (_ret) \
1158 exit(_ret); \
1159 } while (0)
1160
1161
1162 /** Stores a uint32 as big endian */
1163 static inline void put32(uint8_t *buf, uint32_t val) {
1164 buf[0] = val >> 24;
1165 buf[1] = val >> 16;
1166 buf[2] = val >> 8;
1167 buf[3] = val;
1168 }
1169
1170 /** Allocates a new image partition */
1171 static struct image_partition_entry alloc_image_partition(const char *name, size_t len) {
1172 struct image_partition_entry entry = {name, len, malloc(len)};
1173 if (!entry.data)
1174 error(1, errno, "malloc");
1175
1176 return entry;
1177 }
1178
1179 /** Frees an image partition */
1180 static void free_image_partition(struct image_partition_entry entry) {
1181 free(entry.data);
1182 }
1183
1184 static time_t source_date_epoch = -1;
1185 static void set_source_date_epoch() {
1186 char *env = getenv("SOURCE_DATE_EPOCH");
1187 char *endptr = env;
1188 errno = 0;
1189 if (env && *env) {
1190 source_date_epoch = strtoull(env, &endptr, 10);
1191 if (errno || (endptr && *endptr != '\0')) {
1192 fprintf(stderr, "Invalid SOURCE_DATE_EPOCH");
1193 exit(1);
1194 }
1195 }
1196 }
1197
1198 /** Generates the partition-table partition */
1199 static struct image_partition_entry make_partition_table(const struct flash_partition_entry *p) {
1200 struct image_partition_entry entry = alloc_image_partition("partition-table", 0x800);
1201
1202 char *s = (char *)entry.data, *end = (char *)(s+entry.size);
1203
1204 *(s++) = 0x00;
1205 *(s++) = 0x04;
1206 *(s++) = 0x00;
1207 *(s++) = 0x00;
1208
1209 size_t i;
1210 for (i = 0; p[i].name; i++) {
1211 size_t len = end-s;
1212 size_t w = snprintf(s, len, "partition %s base 0x%05x size 0x%05x\n", p[i].name, p[i].base, p[i].size);
1213
1214 if (w > len-1)
1215 error(1, 0, "flash partition table overflow?");
1216
1217 s += w;
1218 }
1219
1220 s++;
1221
1222 memset(s, 0xff, end-s);
1223
1224 return entry;
1225 }
1226
1227
1228 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
1229 static inline uint8_t bcd(uint8_t v) {
1230 return 0x10 * (v/10) + v%10;
1231 }
1232
1233
1234 /** Generates the soft-version partition */
1235 static struct image_partition_entry make_soft_version(uint32_t rev) {
1236 struct image_partition_entry entry = alloc_image_partition("soft-version", sizeof(struct soft_version));
1237 struct soft_version *s = (struct soft_version *)entry.data;
1238
1239 time_t t;
1240
1241 if (source_date_epoch != -1)
1242 t = source_date_epoch;
1243 else if (time(&t) == (time_t)(-1))
1244 error(1, errno, "time");
1245
1246 struct tm *tm = localtime(&t);
1247
1248 s->magic = htonl(0x0000000c);
1249 s->zero = 0;
1250 s->pad1 = 0xff;
1251
1252 s->version_major = 0;
1253 s->version_minor = 0;
1254 s->version_patch = 0;
1255
1256 s->year_hi = bcd((1900+tm->tm_year)/100);
1257 s->year_lo = bcd(tm->tm_year%100);
1258 s->month = bcd(tm->tm_mon+1);
1259 s->day = bcd(tm->tm_mday);
1260 s->rev = htonl(rev);
1261
1262 s->pad2 = 0xff;
1263
1264 return entry;
1265 }
1266
1267 static struct image_partition_entry make_soft_version_from_string(const char *soft_ver) {
1268 /** String length _including_ the terminating zero byte */
1269 uint32_t ver_len = strlen(soft_ver) + 1;
1270 /** Partition contains 64 bit header, the version string, and one additional null byte */
1271 size_t partition_len = 2*sizeof(uint32_t) + ver_len + 1;
1272 struct image_partition_entry entry = alloc_image_partition("soft-version", partition_len);
1273
1274 uint32_t *len = (uint32_t *)entry.data;
1275 len[0] = htonl(ver_len);
1276 len[1] = 0;
1277 memcpy(&len[2], soft_ver, ver_len);
1278
1279 entry.data[partition_len - 1] = 0;
1280
1281 return entry;
1282 }
1283
1284 /** Generates the support-list partition */
1285 static struct image_partition_entry make_support_list(struct device_info *info) {
1286 size_t len = strlen(info->support_list);
1287 struct image_partition_entry entry = alloc_image_partition("support-list", len + 9);
1288
1289 put32(entry.data, len);
1290 memset(entry.data+4, 0, 4);
1291 memcpy(entry.data+8, info->support_list, len);
1292 entry.data[len+8] = info->support_trail;
1293
1294 return entry;
1295 }
1296
1297 /** Creates a new image partition with an arbitrary name from a file */
1298 static struct image_partition_entry read_file(const char *part_name, const char *filename, bool add_jffs2_eof, struct flash_partition_entry *file_system_partition) {
1299 struct stat statbuf;
1300
1301 if (stat(filename, &statbuf) < 0)
1302 error(1, errno, "unable to stat file `%s'", filename);
1303
1304 size_t len = statbuf.st_size;
1305
1306 if (add_jffs2_eof)
1307 if (file_system_partition)
1308 len = ALIGN(len + file_system_partition->base, 0x10000) + sizeof(jffs2_eof_mark) - file_system_partition->base;
1309 else
1310 len = ALIGN(len, 0x10000) + sizeof(jffs2_eof_mark);
1311
1312 struct image_partition_entry entry = alloc_image_partition(part_name, len);
1313
1314 FILE *file = fopen(filename, "rb");
1315 if (!file)
1316 error(1, errno, "unable to open file `%s'", filename);
1317
1318 if (fread(entry.data, statbuf.st_size, 1, file) != 1)
1319 error(1, errno, "unable to read file `%s'", filename);
1320
1321 if (add_jffs2_eof) {
1322 uint8_t *eof = entry.data + statbuf.st_size, *end = entry.data+entry.size;
1323
1324 memset(eof, 0xff, end - eof - sizeof(jffs2_eof_mark));
1325 memcpy(end - sizeof(jffs2_eof_mark), jffs2_eof_mark, sizeof(jffs2_eof_mark));
1326 }
1327
1328 fclose(file);
1329
1330 return entry;
1331 }
1332
1333 /** Creates a new image partition from arbitrary data */
1334 static struct image_partition_entry put_data(const char *part_name, const char *datain, size_t len) {
1335
1336 struct image_partition_entry entry = alloc_image_partition(part_name, len);
1337
1338 memcpy(entry.data, datain, len);
1339
1340 return entry;
1341 }
1342
1343 /**
1344 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
1345
1346 Example image partition table:
1347
1348 fwup-ptn partition-table base 0x00800 size 0x00800
1349 fwup-ptn os-image base 0x01000 size 0x113b45
1350 fwup-ptn file-system base 0x114b45 size 0x1d0004
1351 fwup-ptn support-list base 0x2e4b49 size 0x000d1
1352
1353 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
1354 the end of the partition table is marked with a zero byte.
1355
1356 The firmware image must contain at least the partition-table and support-list partitions
1357 to be accepted. There aren't any alignment constraints for the image partitions.
1358
1359 The partition-table partition contains the actual flash layout; partitions
1360 from the image partition table are mapped to the corresponding flash partitions during
1361 the firmware upgrade. The support-list partition contains a list of devices supported by
1362 the firmware image.
1363
1364 The base offsets in the firmware partition table are relative to the end
1365 of the vendor information block, so the partition-table partition will
1366 actually start at offset 0x1814 of the image.
1367
1368 I think partition-table must be the first partition in the firmware image.
1369 */
1370 static void put_partitions(uint8_t *buffer, const struct flash_partition_entry *flash_parts, const struct image_partition_entry *parts) {
1371 size_t i, j;
1372 char *image_pt = (char *)buffer, *end = image_pt + 0x800;
1373
1374 size_t base = 0x800;
1375 for (i = 0; parts[i].name; i++) {
1376 for (j = 0; flash_parts[j].name; j++) {
1377 if (!strcmp(flash_parts[j].name, parts[i].name)) {
1378 if (parts[i].size > flash_parts[j].size)
1379 error(1, 0, "%s partition too big (more than %u bytes)", flash_parts[j].name, (unsigned)flash_parts[j].size);
1380 break;
1381 }
1382 }
1383
1384 assert(flash_parts[j].name);
1385
1386 memcpy(buffer + base, parts[i].data, parts[i].size);
1387
1388 size_t len = end-image_pt;
1389 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);
1390
1391 if (w > len-1)
1392 error(1, 0, "image partition table overflow?");
1393
1394 image_pt += w;
1395
1396 base += parts[i].size;
1397 }
1398 }
1399
1400 /** Generates and writes the image MD5 checksum */
1401 static void put_md5(uint8_t *md5, uint8_t *buffer, unsigned int len) {
1402 MD5_CTX ctx;
1403
1404 MD5_Init(&ctx);
1405 MD5_Update(&ctx, md5_salt, (unsigned int)sizeof(md5_salt));
1406 MD5_Update(&ctx, buffer, len);
1407 MD5_Final(md5, &ctx);
1408 }
1409
1410
1411 /**
1412 Generates the firmware image in factory format
1413
1414 Image format:
1415
1416 Bytes (hex) Usage
1417 ----------- -----
1418 0000-0003 Image size (4 bytes, big endian)
1419 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
1420 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
1421 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
1422 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
1423 1014-1813 Image partition table (2048 bytes, padded with 0xff)
1424 1814-xxxx Firmware partitions
1425 */
1426 static void * generate_factory_image(struct device_info *info, const struct image_partition_entry *parts, size_t *len) {
1427 *len = 0x1814;
1428
1429 size_t i;
1430 for (i = 0; parts[i].name; i++)
1431 *len += parts[i].size;
1432
1433 uint8_t *image = malloc(*len);
1434 if (!image)
1435 error(1, errno, "malloc");
1436
1437 memset(image, 0xff, *len);
1438 put32(image, *len);
1439
1440 if (info->vendor) {
1441 size_t vendor_len = strlen(info->vendor);
1442 put32(image+0x14, vendor_len);
1443 memcpy(image+0x18, info->vendor, vendor_len);
1444 }
1445
1446 put_partitions(image + 0x1014, info->partitions, parts);
1447 put_md5(image+0x04, image+0x14, *len-0x14);
1448
1449 return image;
1450 }
1451
1452 /**
1453 Generates the firmware image in sysupgrade format
1454
1455 This makes some assumptions about the provided flash and image partition tables and
1456 should be generalized when TP-LINK starts building its safeloader into hardware with
1457 different flash layouts.
1458 */
1459 static void * generate_sysupgrade_image(struct device_info *info, const struct image_partition_entry *image_parts, size_t *len) {
1460 size_t i, j;
1461 size_t flash_first_partition_index = 0;
1462 size_t flash_last_partition_index = 0;
1463 const struct flash_partition_entry *flash_first_partition = NULL;
1464 const struct flash_partition_entry *flash_last_partition = NULL;
1465 const struct image_partition_entry *image_last_partition = NULL;
1466
1467 /** Find first and last partitions */
1468 for (i = 0; info->partitions[i].name; i++) {
1469 if (!strcmp(info->partitions[i].name, info->first_sysupgrade_partition)) {
1470 flash_first_partition = &info->partitions[i];
1471 flash_first_partition_index = i;
1472 } else if (!strcmp(info->partitions[i].name, info->last_sysupgrade_partition)) {
1473 flash_last_partition = &info->partitions[i];
1474 flash_last_partition_index = i;
1475 }
1476 }
1477
1478 assert(flash_first_partition && flash_last_partition);
1479 assert(flash_first_partition_index < flash_last_partition_index);
1480
1481 /** Find last partition from image to calculate needed size */
1482 for (i = 0; image_parts[i].name; i++) {
1483 if (!strcmp(image_parts[i].name, info->last_sysupgrade_partition)) {
1484 image_last_partition = &image_parts[i];
1485 break;
1486 }
1487 }
1488
1489 assert(image_last_partition);
1490
1491 *len = flash_last_partition->base - flash_first_partition->base + image_last_partition->size;
1492
1493 uint8_t *image = malloc(*len);
1494 if (!image)
1495 error(1, errno, "malloc");
1496
1497 memset(image, 0xff, *len);
1498
1499 for (i = flash_first_partition_index; i <= flash_last_partition_index; i++) {
1500 for (j = 0; image_parts[j].name; j++) {
1501 if (!strcmp(info->partitions[i].name, image_parts[j].name)) {
1502 if (image_parts[j].size > info->partitions[i].size)
1503 error(1, 0, "%s partition too big (more than %u bytes)", info->partitions[i].name, (unsigned)info->partitions[i].size);
1504 memcpy(image + info->partitions[i].base - flash_first_partition->base, image_parts[j].data, image_parts[j].size);
1505 break;
1506 }
1507
1508 assert(image_parts[j].name);
1509 }
1510 }
1511
1512 return image;
1513 }
1514
1515 /** Generates an image according to a given layout and writes it to a file */
1516 static void build_image(const char *output,
1517 const char *kernel_image,
1518 const char *rootfs_image,
1519 uint32_t rev,
1520 bool add_jffs2_eof,
1521 bool sysupgrade,
1522 struct device_info *info) {
1523
1524 size_t i;
1525
1526 struct image_partition_entry parts[7] = {};
1527
1528 struct flash_partition_entry *firmware_partition = NULL;
1529 struct flash_partition_entry *os_image_partition = NULL;
1530 struct flash_partition_entry *file_system_partition = NULL;
1531 size_t firmware_partition_index = 0;
1532
1533 for (i = 0; info->partitions[i].name; i++) {
1534 if (!strcmp(info->partitions[i].name, "firmware"))
1535 {
1536 firmware_partition = &info->partitions[i];
1537 firmware_partition_index = i;
1538 }
1539 }
1540
1541 if (firmware_partition)
1542 {
1543 os_image_partition = &info->partitions[firmware_partition_index];
1544 file_system_partition = &info->partitions[firmware_partition_index + 1];
1545
1546 struct stat kernel;
1547 if (stat(kernel_image, &kernel) < 0)
1548 error(1, errno, "unable to stat file `%s'", kernel_image);
1549
1550 if (kernel.st_size > firmware_partition->size)
1551 error(1, 0, "kernel overflowed firmware partition\n");
1552
1553 for (i = MAX_PARTITIONS-1; i >= firmware_partition_index + 1; i--)
1554 info->partitions[i+1] = info->partitions[i];
1555
1556 file_system_partition->name = "file-system";
1557 file_system_partition->base = firmware_partition->base + kernel.st_size;
1558
1559 /* Align partition start to erase blocks for factory images only */
1560 if (!sysupgrade)
1561 file_system_partition->base = ALIGN(firmware_partition->base + kernel.st_size, 0x10000);
1562
1563 file_system_partition->size = firmware_partition->size - file_system_partition->base;
1564
1565 os_image_partition->name = "os-image";
1566 os_image_partition->size = kernel.st_size;
1567 }
1568
1569 parts[0] = make_partition_table(info->partitions);
1570 if (info->soft_ver)
1571 parts[1] = make_soft_version_from_string(info->soft_ver);
1572 else
1573 parts[1] = make_soft_version(rev);
1574
1575 parts[2] = make_support_list(info);
1576 parts[3] = read_file("os-image", kernel_image, false, NULL);
1577 parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof, file_system_partition);
1578
1579 /* Some devices need the extra-para partition to accept the firmware */
1580 if (strcasecmp(info->id, "ARCHER-C25-V1") == 0 ||
1581 strcasecmp(info->id, "ARCHER-C59-V2") == 0 ||
1582 strcasecmp(info->id, "ARCHER-C60-V2") == 0 ||
1583 strcasecmp(info->id, "TLWR1043NV5") == 0) {
1584 const char mdat[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
1585 parts[5] = put_data("extra-para", mdat, 11);
1586 } else if (strcasecmp(info->id, "ARCHER-C7-V4") == 0 || strcasecmp(info->id, "ARCHER-C7-V5") == 0) {
1587 const char mdat[11] = {0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0xca, 0x00, 0x01, 0x00, 0x00};
1588 parts[5] = put_data("extra-para", mdat, 11);
1589 }
1590
1591 size_t len;
1592 void *image;
1593 if (sysupgrade)
1594 image = generate_sysupgrade_image(info, parts, &len);
1595 else
1596 image = generate_factory_image(info, parts, &len);
1597
1598 FILE *file = fopen(output, "wb");
1599 if (!file)
1600 error(1, errno, "unable to open output file");
1601
1602 if (fwrite(image, len, 1, file) != 1)
1603 error(1, 0, "unable to write output file");
1604
1605 fclose(file);
1606
1607 free(image);
1608
1609 for (i = 0; parts[i].name; i++)
1610 free_image_partition(parts[i]);
1611 }
1612
1613 /** Usage output */
1614 static void usage(const char *argv0) {
1615 fprintf(stderr,
1616 "Usage: %s [OPTIONS...]\n"
1617 "\n"
1618 "Options:\n"
1619 " -h show this help\n"
1620 "\n"
1621 "Create a new image:\n"
1622 " -B <board> create image for the board specified with <board>\n"
1623 " -k <file> read kernel image from the file <file>\n"
1624 " -r <file> read rootfs image from the file <file>\n"
1625 " -o <file> write output to the file <file>\n"
1626 " -V <rev> sets the revision number to <rev>\n"
1627 " -j add jffs2 end-of-filesystem markers\n"
1628 " -S create sysupgrade instead of factory image\n"
1629 "Extract an old image:\n"
1630 " -x <file> extract all oem firmware partition\n"
1631 " -d <dir> destination to extract the firmware partition\n"
1632 " -z <file> convert an oem firmware into a sysupgade file. Use -o for output file\n",
1633 argv0
1634 );
1635 };
1636
1637
1638 static struct device_info *find_board(const char *id)
1639 {
1640 struct device_info *board = NULL;
1641
1642 for (board = boards; board->id != NULL; board++)
1643 if (strcasecmp(id, board->id) == 0)
1644 return board;
1645
1646 return NULL;
1647 }
1648
1649 static int add_flash_partition(
1650 struct flash_partition_entry *part_list,
1651 size_t max_entries,
1652 const char *name,
1653 unsigned long base,
1654 unsigned long size)
1655 {
1656 int ptr;
1657 /* check if the list has a free entry */
1658 for (ptr = 0; ptr < max_entries; ptr++, part_list++) {
1659 if (part_list->name == NULL &&
1660 part_list->base == 0 &&
1661 part_list->size == 0)
1662 break;
1663 }
1664
1665 if (ptr == max_entries) {
1666 error(1, 0, "No free flash part entry available.");
1667 }
1668
1669 part_list->name = calloc(1, strlen(name) + 1);
1670 if (!part_list->name) {
1671 error(1, 0, "Unable to allocate memory");
1672 }
1673
1674 memcpy((char *)part_list->name, name, strlen(name));
1675 part_list->base = base;
1676 part_list->size = size;
1677
1678 return 0;
1679 }
1680
1681 /** read the partition table into struct flash_partition_entry */
1682 static int read_partition_table(
1683 FILE *file, long offset,
1684 struct flash_partition_entry *entries, size_t max_entries,
1685 int type)
1686 {
1687 char buf[2048];
1688 char *ptr, *end;
1689 const char *parthdr = NULL;
1690 const char *fwuphdr = "fwup-ptn";
1691 const char *flashhdr = "partition";
1692
1693 /* TODO: search for the partition table */
1694
1695 switch(type) {
1696 case 0:
1697 parthdr = fwuphdr;
1698 break;
1699 case 1:
1700 parthdr = flashhdr;
1701 break;
1702 default:
1703 error(1, 0, "Invalid partition table");
1704 }
1705
1706 if (fseek(file, offset, SEEK_SET) < 0)
1707 error(1, errno, "Can not seek in the firmware");
1708
1709 if (fread(buf, 1, 2048, file) < 0)
1710 error(1, errno, "Can not read fwup-ptn from the firmware");
1711
1712 buf[2047] = '\0';
1713
1714 /* look for the partition header */
1715 if (memcmp(buf, parthdr, strlen(parthdr)) != 0) {
1716 fprintf(stderr, "DEBUG: can not find fwuphdr\n");
1717 return 1;
1718 }
1719
1720 ptr = buf;
1721 end = buf + sizeof(buf);
1722 while ((ptr + strlen(parthdr)) < end &&
1723 memcmp(ptr, parthdr, strlen(parthdr)) == 0) {
1724 char *end_part;
1725 char *end_element;
1726
1727 char name[32] = { 0 };
1728 int name_len = 0;
1729 unsigned long base = 0;
1730 unsigned long size = 0;
1731
1732 end_part = memchr(ptr, '\n', (end - ptr));
1733 if (end_part == NULL) {
1734 /* in theory this should never happen, because a partition always ends with 0x09, 0x0D, 0x0A */
1735 break;
1736 }
1737
1738 for (int i = 0; i <= 4; i++) {
1739 if (end_part <= ptr)
1740 break;
1741
1742 end_element = memchr(ptr, 0x20, (end_part - ptr));
1743 if (end_element == NULL) {
1744 error(1, errno, "Ignoring the rest of the partition entries.");
1745 break;
1746 }
1747
1748 switch (i) {
1749 /* partition header */
1750 case 0:
1751 ptr = end_element + 1;
1752 continue;
1753 /* name */
1754 case 1:
1755 name_len = (end_element - ptr) > 31 ? 31 : (end_element - ptr);
1756 strncpy(name, ptr, name_len);
1757 name[name_len] = '\0';
1758 ptr = end_element + 1;
1759 continue;
1760
1761 /* string "base" */
1762 case 2:
1763 ptr = end_element + 1;
1764 continue;
1765
1766 /* actual base */
1767 case 3:
1768 base = strtoul(ptr, NULL, 16);
1769 ptr = end_element + 1;
1770 continue;
1771
1772 /* string "size" */
1773 case 4:
1774 ptr = end_element + 1;
1775 /* actual size. The last element doesn't have a sepeartor */
1776 size = strtoul(ptr, NULL, 16);
1777 /* the part ends with 0x09, 0x0d, 0x0a */
1778 ptr = end_part + 1;
1779 add_flash_partition(entries, max_entries, name, base, size);
1780 continue;
1781 }
1782 }
1783 }
1784
1785 return 0;
1786 }
1787
1788 static void write_partition(
1789 FILE *input_file,
1790 size_t firmware_offset,
1791 struct flash_partition_entry *entry,
1792 FILE *output_file)
1793 {
1794 char buf[4096];
1795 size_t offset;
1796
1797 fseek(input_file, entry->base + firmware_offset, SEEK_SET);
1798
1799 for (offset = 0; sizeof(buf) + offset <= entry->size; offset += sizeof(buf)) {
1800 if (fread(buf, sizeof(buf), 1, input_file) < 0)
1801 error(1, errno, "Can not read partition from input_file");
1802
1803 if (fwrite(buf, sizeof(buf), 1, output_file) < 0)
1804 error(1, errno, "Can not write partition to output_file");
1805 }
1806 /* write last chunk smaller than buffer */
1807 if (offset < entry->size) {
1808 offset = entry->size - offset;
1809 if (fread(buf, offset, 1, input_file) < 0)
1810 error(1, errno, "Can not read partition from input_file");
1811 if (fwrite(buf, offset, 1, output_file) < 0)
1812 error(1, errno, "Can not write partition to output_file");
1813 }
1814 }
1815
1816 static int extract_firmware_partition(FILE *input_file, size_t firmware_offset, struct flash_partition_entry *entry, const char *output_directory)
1817 {
1818 FILE *output_file;
1819 char output[PATH_MAX];
1820
1821 snprintf(output, PATH_MAX, "%s/%s", output_directory, entry->name);
1822 output_file = fopen(output, "wb+");
1823 if (output_file == NULL) {
1824 error(1, errno, "Can not open output file %s", output);
1825 }
1826
1827 write_partition(input_file, firmware_offset, entry, output_file);
1828
1829 fclose(output_file);
1830
1831 return 0;
1832 }
1833
1834 /** extract all partitions from the firmware file */
1835 static int extract_firmware(const char *input, const char *output_directory)
1836 {
1837 struct flash_partition_entry entries[16] = { 0 };
1838 size_t max_entries = 16;
1839 size_t firmware_offset = 0x1014;
1840 FILE *input_file;
1841
1842 struct stat statbuf;
1843
1844 /* check input file */
1845 if (stat(input, &statbuf)) {
1846 error(1, errno, "Can not read input firmware %s", input);
1847 }
1848
1849 /* check if output directory exists */
1850 if (stat(output_directory, &statbuf)) {
1851 error(1, errno, "Failed to stat output directory %s", output_directory);
1852 }
1853
1854 if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
1855 error(1, errno, "Given output directory is not a directory %s", output_directory);
1856 }
1857
1858 input_file = fopen(input, "rb");
1859
1860 if (read_partition_table(input_file, firmware_offset, entries, 16, 0) != 0) {
1861 error(1, 0, "Error can not read the partition table (fwup-ptn)");
1862 }
1863
1864 for (int i = 0; i < max_entries; i++) {
1865 if (entries[i].name == NULL &&
1866 entries[i].base == 0 &&
1867 entries[i].size == 0)
1868 continue;
1869
1870 extract_firmware_partition(input_file, firmware_offset, &entries[i], output_directory);
1871 }
1872
1873 return 0;
1874 }
1875
1876 static struct flash_partition_entry *find_partition(
1877 struct flash_partition_entry *entries, size_t max_entries,
1878 const char *name, const char *error_msg)
1879 {
1880 for (int i = 0; i < max_entries; i++, entries++) {
1881 if (strcmp(entries->name, name) == 0)
1882 return entries;
1883 }
1884
1885 error(1, 0, "%s", error_msg);
1886 return NULL;
1887 }
1888
1889 static void write_ff(FILE *output_file, size_t size)
1890 {
1891 char buf[4096];
1892 int offset;
1893
1894 memset(buf, 0xff, sizeof(buf));
1895
1896 for (offset = 0; offset + sizeof(buf) < size ; offset += sizeof(buf)) {
1897 if (fwrite(buf, sizeof(buf), 1, output_file) < 0)
1898 error(1, errno, "Can not write 0xff to output_file");
1899 }
1900
1901 /* write last chunk smaller than buffer */
1902 if (offset < size) {
1903 offset = size - offset;
1904 if (fwrite(buf, offset, 1, output_file) < 0)
1905 error(1, errno, "Can not write partition to output_file");
1906 }
1907 }
1908
1909 static void convert_firmware(const char *input, const char *output)
1910 {
1911 struct flash_partition_entry fwup[MAX_PARTITIONS] = { 0 };
1912 struct flash_partition_entry flash[MAX_PARTITIONS] = { 0 };
1913 struct flash_partition_entry *fwup_os_image = NULL, *fwup_file_system = NULL;
1914 struct flash_partition_entry *flash_os_image = NULL, *flash_file_system = NULL;
1915 struct flash_partition_entry *fwup_partition_table = NULL;
1916 size_t firmware_offset = 0x1014;
1917 FILE *input_file, *output_file;
1918
1919 struct stat statbuf;
1920
1921 /* check input file */
1922 if (stat(input, &statbuf)) {
1923 error(1, errno, "Can not read input firmware %s", input);
1924 }
1925
1926 input_file = fopen(input, "rb");
1927 if (!input_file)
1928 error(1, 0, "Can not open input firmware %s", input);
1929
1930 output_file = fopen(output, "wb");
1931 if (!output_file)
1932 error(1, 0, "Can not open output firmware %s", output);
1933
1934 if (read_partition_table(input_file, firmware_offset, fwup, MAX_PARTITIONS, 0) != 0) {
1935 error(1, 0, "Error can not read the partition table (fwup-ptn)");
1936 }
1937
1938 fwup_os_image = find_partition(fwup, MAX_PARTITIONS,
1939 "os-image", "Error can not find os-image partition (fwup)");
1940 fwup_file_system = find_partition(fwup, MAX_PARTITIONS,
1941 "file-system", "Error can not find file-system partition (fwup)");
1942 fwup_partition_table = find_partition(fwup, MAX_PARTITIONS,
1943 "partition-table", "Error can not find partition-table partition");
1944
1945 /* the flash partition table has a 0x00000004 magic haeder */
1946 if (read_partition_table(input_file, firmware_offset + fwup_partition_table->base + 4, flash, MAX_PARTITIONS, 1) != 0)
1947 error(1, 0, "Error can not read the partition table (flash)");
1948
1949 flash_os_image = find_partition(flash, MAX_PARTITIONS,
1950 "os-image", "Error can not find os-image partition (flash)");
1951 flash_file_system = find_partition(flash, MAX_PARTITIONS,
1952 "file-system", "Error can not find file-system partition (flash)");
1953
1954 /* write os_image to 0x0 */
1955 write_partition(input_file, firmware_offset, fwup_os_image, output_file);
1956 write_ff(output_file, flash_os_image->size - fwup_os_image->size);
1957
1958 /* write file-system behind os_image */
1959 fseek(output_file, flash_file_system->base - flash_os_image->base, SEEK_SET);
1960 write_partition(input_file, firmware_offset, fwup_file_system, output_file);
1961 write_ff(output_file, flash_file_system->size - fwup_file_system->size);
1962
1963 fclose(output_file);
1964 fclose(input_file);
1965 }
1966
1967 int main(int argc, char *argv[]) {
1968 const char *board = NULL, *kernel_image = NULL, *rootfs_image = NULL, *output = NULL;
1969 const char *extract_image = NULL, *output_directory = NULL, *convert_image = NULL;
1970 bool add_jffs2_eof = false, sysupgrade = false;
1971 unsigned rev = 0;
1972 struct device_info *info;
1973 set_source_date_epoch();
1974
1975 while (true) {
1976 int c;
1977
1978 c = getopt(argc, argv, "B:k:r:o:V:jSh:x:d:z:");
1979 if (c == -1)
1980 break;
1981
1982 switch (c) {
1983 case 'B':
1984 board = optarg;
1985 break;
1986
1987 case 'k':
1988 kernel_image = optarg;
1989 break;
1990
1991 case 'r':
1992 rootfs_image = optarg;
1993 break;
1994
1995 case 'o':
1996 output = optarg;
1997 break;
1998
1999 case 'V':
2000 sscanf(optarg, "r%u", &rev);
2001 break;
2002
2003 case 'j':
2004 add_jffs2_eof = true;
2005 break;
2006
2007 case 'S':
2008 sysupgrade = true;
2009 break;
2010
2011 case 'h':
2012 usage(argv[0]);
2013 return 0;
2014
2015 case 'd':
2016 output_directory = optarg;
2017 break;
2018
2019 case 'x':
2020 extract_image = optarg;
2021 break;
2022
2023 case 'z':
2024 convert_image = optarg;
2025 break;
2026
2027 default:
2028 usage(argv[0]);
2029 return 1;
2030 }
2031 }
2032
2033 if (extract_image || output_directory) {
2034 if (!extract_image)
2035 error(1, 0, "No factory/oem image given via -x <file>. Output directory is only valid with -x");
2036 if (!output_directory)
2037 error(1, 0, "Can not extract an image without output directory. Use -d <dir>");
2038 extract_firmware(extract_image, output_directory);
2039 } else if (convert_image) {
2040 if (!output)
2041 error(1, 0, "Can not convert a factory/oem image into sysupgrade image without output file. Use -o <file>");
2042 convert_firmware(convert_image, output);
2043 } else {
2044 if (!board)
2045 error(1, 0, "no board has been specified");
2046 if (!kernel_image)
2047 error(1, 0, "no kernel image has been specified");
2048 if (!rootfs_image)
2049 error(1, 0, "no rootfs image has been specified");
2050 if (!output)
2051 error(1, 0, "no output filename has been specified");
2052
2053 info = find_board(board);
2054
2055 if (info == NULL)
2056 error(1, 0, "unsupported board %s", board);
2057
2058 build_image(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, info);
2059 }
2060
2061 return 0;
2062 }