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