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