aa8cc31304d04b4d9c25ba2c7e604925837a22ed
[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
50 #include "md5.h"
51
52
53 #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
54
55
56 #define MAX_PARTITIONS 32
57
58 /** An image partition table entry */
59 struct image_partition_entry {
60 const char *name;
61 size_t size;
62 uint8_t *data;
63 };
64
65 /** A flash partition table entry */
66 struct flash_partition_entry {
67 const char *name;
68 uint32_t base;
69 uint32_t size;
70 };
71
72 /** Firmware layout description */
73 struct device_info {
74 const char *id;
75 const char *vendor;
76 const char *support_list;
77 char support_trail;
78 const char *soft_ver;
79 const struct flash_partition_entry partitions[MAX_PARTITIONS+1];
80 const char *first_sysupgrade_partition;
81 const char *last_sysupgrade_partition;
82 };
83
84 /** The content of the soft-version structure */
85 struct __attribute__((__packed__)) soft_version {
86 uint32_t magic;
87 uint32_t zero;
88 uint8_t pad1;
89 uint8_t version_major;
90 uint8_t version_minor;
91 uint8_t version_patch;
92 uint8_t year_hi;
93 uint8_t year_lo;
94 uint8_t month;
95 uint8_t day;
96 uint32_t rev;
97 uint8_t pad2;
98 };
99
100
101 static const uint8_t jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
102
103
104 /**
105 Salt for the MD5 hash
106
107 Fortunately, TP-LINK seems to use the same salt for most devices which use
108 the new image format.
109 */
110 static const uint8_t md5_salt[16] = {
111 0x7a, 0x2b, 0x15, 0xed,
112 0x9b, 0x98, 0x59, 0x6d,
113 0xe5, 0x04, 0xab, 0x44,
114 0xac, 0x2a, 0x9f, 0x4e,
115 };
116
117
118 /** Firmware layout table */
119 static struct device_info boards[] = {
120 /** Firmware layout for the CPE210/220 */
121 {
122 .id = "CPE210",
123 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
124 .support_list =
125 "SupportList:\r\n"
126 "CPE210(TP-LINK|UN|N300-2):1.0\r\n"
127 "CPE210(TP-LINK|UN|N300-2):1.1\r\n"
128 "CPE210(TP-LINK|US|N300-2):1.1\r\n"
129 "CPE210(TP-LINK|EU|N300-2):1.1\r\n"
130 "CPE220(TP-LINK|UN|N300-2):1.1\r\n"
131 "CPE220(TP-LINK|US|N300-2):1.1\r\n"
132 "CPE220(TP-LINK|EU|N300-2):1.1\r\n",
133 .support_trail = '\xff',
134 .soft_ver = NULL,
135
136 .partitions = {
137 {"fs-uboot", 0x00000, 0x20000},
138 {"partition-table", 0x20000, 0x02000},
139 {"default-mac", 0x30000, 0x00020},
140 {"product-info", 0x31100, 0x00100},
141 {"signature", 0x32000, 0x00400},
142 {"os-image", 0x40000, 0x170000},
143 {"soft-version", 0x1b0000, 0x00100},
144 {"support-list", 0x1b1000, 0x00400},
145 {"file-system", 0x1c0000, 0x600000},
146 {"user-config", 0x7c0000, 0x10000},
147 {"default-config", 0x7d0000, 0x10000},
148 {"log", 0x7e0000, 0x10000},
149 {"radio", 0x7f0000, 0x10000},
150 {NULL, 0, 0}
151 },
152
153 .first_sysupgrade_partition = "os-image",
154 .last_sysupgrade_partition = "file-system",
155 },
156
157 /** Firmware layout for the CPE510/520 */
158 {
159 .id = "CPE510",
160 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
161 .support_list =
162 "SupportList:\r\n"
163 "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
164 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
165 "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
166 "CPE510(TP-LINK|US|N300-5):1.1\r\n"
167 "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
168 "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
169 "CPE520(TP-LINK|US|N300-5):1.1\r\n"
170 "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
171 .support_trail = '\xff',
172 .soft_ver = NULL,
173
174 .partitions = {
175 {"fs-uboot", 0x00000, 0x20000},
176 {"partition-table", 0x20000, 0x02000},
177 {"default-mac", 0x30000, 0x00020},
178 {"product-info", 0x31100, 0x00100},
179 {"signature", 0x32000, 0x00400},
180 {"os-image", 0x40000, 0x170000},
181 {"soft-version", 0x1b0000, 0x00100},
182 {"support-list", 0x1b1000, 0x00400},
183 {"file-system", 0x1c0000, 0x600000},
184 {"user-config", 0x7c0000, 0x10000},
185 {"default-config", 0x7d0000, 0x10000},
186 {"log", 0x7e0000, 0x10000},
187 {"radio", 0x7f0000, 0x10000},
188 {NULL, 0, 0}
189 },
190
191 .first_sysupgrade_partition = "os-image",
192 .last_sysupgrade_partition = "file-system",
193 },
194
195 {
196 .id = "WBS210",
197 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
198 .support_list =
199 "SupportList:\r\n"
200 "WBS210(TP-LINK|UN|N300-2):1.20\r\n"
201 "WBS210(TP-LINK|US|N300-2):1.20\r\n"
202 "WBS210(TP-LINK|EU|N300-2):1.20\r\n",
203 .support_trail = '\xff',
204 .soft_ver = NULL,
205
206 .partitions = {
207 {"fs-uboot", 0x00000, 0x20000},
208 {"partition-table", 0x20000, 0x02000},
209 {"default-mac", 0x30000, 0x00020},
210 {"product-info", 0x31100, 0x00100},
211 {"signature", 0x32000, 0x00400},
212 {"os-image", 0x40000, 0x170000},
213 {"soft-version", 0x1b0000, 0x00100},
214 {"support-list", 0x1b1000, 0x00400},
215 {"file-system", 0x1c0000, 0x600000},
216 {"user-config", 0x7c0000, 0x10000},
217 {"default-config", 0x7d0000, 0x10000},
218 {"log", 0x7e0000, 0x10000},
219 {"radio", 0x7f0000, 0x10000},
220 {NULL, 0, 0}
221 },
222
223 .first_sysupgrade_partition = "os-image",
224 .last_sysupgrade_partition = "file-system",
225 },
226
227 {
228 .id = "WBS510",
229 .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
230 .support_list =
231 "SupportList:\r\n"
232 "WBS510(TP-LINK|UN|N300-5):1.20\r\n"
233 "WBS510(TP-LINK|US|N300-5):1.20\r\n"
234 "WBS510(TP-LINK|EU|N300-5):1.20\r\n",
235 .support_trail = '\xff',
236 .soft_ver = NULL,
237
238 .partitions = {
239 {"fs-uboot", 0x00000, 0x20000},
240 {"partition-table", 0x20000, 0x02000},
241 {"default-mac", 0x30000, 0x00020},
242 {"product-info", 0x31100, 0x00100},
243 {"signature", 0x32000, 0x00400},
244 {"os-image", 0x40000, 0x170000},
245 {"soft-version", 0x1b0000, 0x00100},
246 {"support-list", 0x1b1000, 0x00400},
247 {"file-system", 0x1c0000, 0x600000},
248 {"user-config", 0x7c0000, 0x10000},
249 {"default-config", 0x7d0000, 0x10000},
250 {"log", 0x7e0000, 0x10000},
251 {"radio", 0x7f0000, 0x10000},
252 {NULL, 0, 0}
253 },
254
255 .first_sysupgrade_partition = "os-image",
256 .last_sysupgrade_partition = "file-system",
257 },
258
259 /** Firmware layout for the C2600 */
260 {
261 .id = "C2600",
262 .vendor = "",
263 .support_list =
264 "SupportList:\r\n"
265 "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
266 .support_trail = '\x00',
267 .soft_ver = NULL,
268
269 .partitions = {
270 {"SBL1", 0x00000, 0x20000},
271 {"MIBIB", 0x20000, 0x20000},
272 {"SBL2", 0x40000, 0x20000},
273 {"SBL3", 0x60000, 0x30000},
274 {"DDRCONFIG", 0x90000, 0x10000},
275 {"SSD", 0xa0000, 0x10000},
276 {"TZ", 0xb0000, 0x30000},
277 {"RPM", 0xe0000, 0x20000},
278 {"fs-uboot", 0x100000, 0x70000},
279 {"uboot-env", 0x170000, 0x40000},
280 {"radio", 0x1b0000, 0x40000},
281 {"os-image", 0x1f0000, 0x200000},
282 {"file-system", 0x3f0000, 0x1b00000},
283 {"default-mac", 0x1ef0000, 0x00200},
284 {"pin", 0x1ef0200, 0x00200},
285 {"product-info", 0x1ef0400, 0x0fc00},
286 {"partition-table", 0x1f00000, 0x10000},
287 {"soft-version", 0x1f10000, 0x10000},
288 {"support-list", 0x1f20000, 0x10000},
289 {"profile", 0x1f30000, 0x10000},
290 {"default-config", 0x1f40000, 0x10000},
291 {"user-config", 0x1f50000, 0x40000},
292 {"qos-db", 0x1f90000, 0x40000},
293 {"usb-config", 0x1fd0000, 0x10000},
294 {"log", 0x1fe0000, 0x20000},
295 {NULL, 0, 0}
296 },
297
298 .first_sysupgrade_partition = "os-image",
299 .last_sysupgrade_partition = "file-system"
300 },
301
302 /** Firmware layout for the C25v1 */
303 {
304 .id = "ARCHER-C25-V1",
305 .support_list =
306 "SupportList:\n"
307 "{product_name:ArcherC25,product_ver:1.0.0,special_id:00000000}\n"
308 "{product_name:ArcherC25,product_ver:1.0.0,special_id:55530000}\n"
309 "{product_name:ArcherC25,product_ver:1.0.0,special_id:45550000}\n",
310 .support_trail = '\x00',
311 .soft_ver = "soft_ver:1.0.0\n",
312
313 /**
314 We use a bigger os-image partition than the stock images (and thus
315 smaller file-system), as our kernel doesn't fit in the stock firmware's
316 1MB os-image.
317 */
318 .partitions = {
319 {"factory-boot", 0x00000, 0x20000},
320 {"fs-uboot", 0x20000, 0x10000},
321 {"os-image", 0x30000, 0x180000}, /* Stock: base 0x30000 size 0x100000 */
322 {"file-system", 0x1b0000, 0x620000}, /* Stock: base 0x130000 size 0x6a0000 */
323 {"user-config", 0x7d0000, 0x04000},
324 {"default-mac", 0x7e0000, 0x00100},
325 {"device-id", 0x7e0100, 0x00100},
326 {"extra-para", 0x7e0200, 0x00100},
327 {"pin", 0x7e0300, 0x00100},
328 {"support-list", 0x7e0400, 0x00400},
329 {"soft-version", 0x7e0800, 0x00400},
330 {"product-info", 0x7e0c00, 0x01400},
331 {"partition-table", 0x7e2000, 0x01000},
332 {"profile", 0x7e3000, 0x01000},
333 {"default-config", 0x7e4000, 0x04000},
334 {"merge-config", 0x7ec000, 0x02000},
335 {"qos-db", 0x7ee000, 0x02000},
336 {"radio", 0x7f0000, 0x10000},
337 {NULL, 0, 0}
338 },
339
340 .first_sysupgrade_partition = "os-image",
341 .last_sysupgrade_partition = "file-system",
342 },
343
344 /** Firmware layout for the C58v1 */
345 {
346 .id = "ARCHER-C58-V1",
347 .vendor = "",
348 .support_list =
349 "SupportList:\r\n"
350 "{product_name:Archer C58,product_ver:1.0.0,special_id:00000000}\r\n"
351 "{product_name:Archer C58,product_ver:1.0.0,special_id:45550000}\r\n"
352 "{product_name:Archer C58,product_ver:1.0.0,special_id:55530000}\r\n",
353 .support_trail = '\x00',
354 .soft_ver = "soft_ver:1.0.0\n",
355
356 .partitions = {
357 {"fs-uboot", 0x00000, 0x10000},
358 {"default-mac", 0x10000, 0x00200},
359 {"pin", 0x10200, 0x00200},
360 {"product-info", 0x10400, 0x00100},
361 {"partition-table", 0x10500, 0x00800},
362 {"soft-version", 0x11300, 0x00200},
363 {"support-list", 0x11500, 0x00100},
364 {"device-id", 0x11600, 0x00100},
365 {"profile", 0x11700, 0x03900},
366 {"default-config", 0x15000, 0x04000},
367 {"user-config", 0x19000, 0x04000},
368 {"os-image", 0x20000, 0x150000},
369 {"file-system", 0x170000, 0x678000},
370 {"certyficate", 0x7e8000, 0x08000},
371 {"radio", 0x7f0000, 0x10000},
372 {NULL, 0, 0}
373 },
374
375 .first_sysupgrade_partition = "os-image",
376 .last_sysupgrade_partition = "file-system",
377 },
378
379 /** Firmware layout for the C59v1 */
380 {
381 .id = "ARCHER-C59-V1",
382 .vendor = "",
383 .support_list =
384 "SupportList:\r\n"
385 "{product_name:Archer C59,product_ver:1.0.0,special_id:00000000}\r\n"
386 "{product_name:Archer C59,product_ver:1.0.0,special_id:45550000}\r\n"
387 "{product_name:Archer C59,product_ver:1.0.0,special_id:52550000}\r\n"
388 "{product_name:Archer C59,product_ver:1.0.0,special_id:55530000}\r\n",
389 .support_trail = '\x00',
390 .soft_ver = "soft_ver:1.0.0\n",
391
392 .partitions = {
393 {"fs-uboot", 0x00000, 0x10000},
394 {"default-mac", 0x10000, 0x00200},
395 {"pin", 0x10200, 0x00200},
396 {"device-id", 0x10400, 0x00100},
397 {"product-info", 0x10500, 0x0fb00},
398 {"os-image", 0x20000, 0x180000},
399 {"file-system", 0x1a0000, 0xcb0000},
400 {"partition-table", 0xe50000, 0x10000},
401 {"soft-version", 0xe60000, 0x10000},
402 {"support-list", 0xe70000, 0x10000},
403 {"profile", 0xe80000, 0x10000},
404 {"default-config", 0xe90000, 0x10000},
405 {"user-config", 0xea0000, 0x40000},
406 {"usb-config", 0xee0000, 0x10000},
407 {"certificate", 0xef0000, 0x10000},
408 {"qos-db", 0xf00000, 0x40000},
409 {"log", 0xfe0000, 0x10000},
410 {"radio", 0xff0000, 0x10000},
411 {NULL, 0, 0}
412 },
413
414 .first_sysupgrade_partition = "os-image",
415 .last_sysupgrade_partition = "file-system",
416 },
417
418 /** Firmware layout for the C60v1 */
419 {
420 .id = "ARCHER-C60-V1",
421 .vendor = "",
422 .support_list =
423 "SupportList:\r\n"
424 "{product_name:Archer C60,product_ver:1.0.0,special_id:00000000}\r\n"
425 "{product_name:Archer C60,product_ver:1.0.0,special_id:45550000}\r\n"
426 "{product_name:Archer C60,product_ver:1.0.1,special_id:45550000}\r\n"
427 "{product_name:Archer C60,product_ver:1.0.0,special_id:52550000}\r\n"
428 "{product_name:Archer C60,product_ver:1.0.0,special_id:55530000}\r\n",
429 .support_trail = '\x00',
430 .soft_ver = "soft_ver:1.0.0\n",
431
432 .partitions = {
433 {"fs-uboot", 0x00000, 0x10000},
434 {"default-mac", 0x10000, 0x00200},
435 {"pin", 0x10200, 0x00200},
436 {"product-info", 0x10400, 0x00100},
437 {"partition-table", 0x10500, 0x00800},
438 {"soft-version", 0x11300, 0x00200},
439 {"support-list", 0x11500, 0x00100},
440 {"device-id", 0x11600, 0x00100},
441 {"profile", 0x11700, 0x03900},
442 {"default-config", 0x15000, 0x04000},
443 {"user-config", 0x19000, 0x04000},
444 {"os-image", 0x20000, 0x150000},
445 {"file-system", 0x170000, 0x678000},
446 {"certyficate", 0x7e8000, 0x08000},
447 {"radio", 0x7f0000, 0x10000},
448 {NULL, 0, 0}
449 },
450
451 .first_sysupgrade_partition = "os-image",
452 .last_sysupgrade_partition = "file-system",
453 },
454
455 /** Firmware layout for the C5 */
456 {
457 .id = "ARCHER-C5-V2",
458 .vendor = "",
459 .support_list =
460 "SupportList:\r\n"
461 "{product_name:ArcherC5,product_ver:2.0.0,special_id:00000000}\r\n"
462 "{product_name:ArcherC5,product_ver:2.0.0,special_id:55530000}\r\n"
463 "{product_name:ArcherC5,product_ver:2.0.0,special_id:4A500000}\r\n", /* JP version */
464 .support_trail = '\x00',
465 .soft_ver = NULL,
466
467 .partitions = {
468 {"fs-uboot", 0x00000, 0x40000},
469 {"os-image", 0x40000, 0x200000},
470 {"file-system", 0x240000, 0xc00000},
471 {"default-mac", 0xe40000, 0x00200},
472 {"pin", 0xe40200, 0x00200},
473 {"product-info", 0xe40400, 0x00200},
474 {"partition-table", 0xe50000, 0x10000},
475 {"soft-version", 0xe60000, 0x00200},
476 {"support-list", 0xe61000, 0x0f000},
477 {"profile", 0xe70000, 0x10000},
478 {"default-config", 0xe80000, 0x10000},
479 {"user-config", 0xe90000, 0x50000},
480 {"log", 0xee0000, 0x100000},
481 {"radio_bk", 0xfe0000, 0x10000},
482 {"radio", 0xff0000, 0x10000},
483 {NULL, 0, 0}
484 },
485
486 .first_sysupgrade_partition = "os-image",
487 .last_sysupgrade_partition = "file-system"
488 },
489
490 /** Firmware layout for the C9 */
491 {
492 .id = "ARCHERC9",
493 .vendor = "",
494 .support_list =
495 "SupportList:\n"
496 "{product_name:ArcherC9,"
497 "product_ver:1.0.0,"
498 "special_id:00000000}\n",
499 .support_trail = '\x00',
500 .soft_ver = NULL,
501
502 .partitions = {
503 {"fs-uboot", 0x00000, 0x40000},
504 {"os-image", 0x40000, 0x200000},
505 {"file-system", 0x240000, 0xc00000},
506 {"default-mac", 0xe40000, 0x00200},
507 {"pin", 0xe40200, 0x00200},
508 {"product-info", 0xe40400, 0x00200},
509 {"partition-table", 0xe50000, 0x10000},
510 {"soft-version", 0xe60000, 0x00200},
511 {"support-list", 0xe61000, 0x0f000},
512 {"profile", 0xe70000, 0x10000},
513 {"default-config", 0xe80000, 0x10000},
514 {"user-config", 0xe90000, 0x50000},
515 {"log", 0xee0000, 0x100000},
516 {"radio_bk", 0xfe0000, 0x10000},
517 {"radio", 0xff0000, 0x10000},
518 {NULL, 0, 0}
519 },
520
521 .first_sysupgrade_partition = "os-image",
522 .last_sysupgrade_partition = "file-system"
523 },
524
525 /** Firmware layout for the EAP120 */
526 {
527 .id = "EAP120",
528 .vendor = "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
529 .support_list =
530 "SupportList:\r\n"
531 "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
532 .support_trail = '\xff',
533 .soft_ver = NULL,
534
535 .partitions = {
536 {"fs-uboot", 0x00000, 0x20000},
537 {"partition-table", 0x20000, 0x02000},
538 {"default-mac", 0x30000, 0x00020},
539 {"support-list", 0x31000, 0x00100},
540 {"product-info", 0x31100, 0x00100},
541 {"soft-version", 0x32000, 0x00100},
542 {"os-image", 0x40000, 0x180000},
543 {"file-system", 0x1c0000, 0x600000},
544 {"user-config", 0x7c0000, 0x10000},
545 {"backup-config", 0x7d0000, 0x10000},
546 {"log", 0x7e0000, 0x10000},
547 {"radio", 0x7f0000, 0x10000},
548 {NULL, 0, 0}
549 },
550
551 .first_sysupgrade_partition = "os-image",
552 .last_sysupgrade_partition = "file-system"
553 },
554
555 /** Firmware layout for the TL-WA850RE v2 */
556 {
557 .id = "TLWA850REV2",
558 .vendor = "",
559 .support_list =
560 "SupportList:\n"
561 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
562 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
563 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
564 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
565 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
566 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
567 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
568 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
569 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
570 "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
571 .support_trail = '\x00',
572 .soft_ver = NULL,
573
574 /**
575 576KB were moved from file-system to os-image
576 in comparison to the stock image
577 */
578 .partitions = {
579 {"fs-uboot", 0x00000, 0x20000},
580 {"os-image", 0x20000, 0x150000},
581 {"file-system", 0x170000, 0x240000},
582 {"partition-table", 0x3b0000, 0x02000},
583 {"default-mac", 0x3c0000, 0x00020},
584 {"pin", 0x3c0100, 0x00020},
585 {"product-info", 0x3c1000, 0x01000},
586 {"soft-version", 0x3c2000, 0x00100},
587 {"support-list", 0x3c3000, 0x01000},
588 {"profile", 0x3c4000, 0x08000},
589 {"user-config", 0x3d0000, 0x10000},
590 {"default-config", 0x3e0000, 0x10000},
591 {"radio", 0x3f0000, 0x10000},
592 {NULL, 0, 0}
593 },
594
595 .first_sysupgrade_partition = "os-image",
596 .last_sysupgrade_partition = "file-system"
597 },
598
599 /** Firmware layout for the TL-WA855RE v1 */
600 {
601 .id = "TLWA855REV1",
602 .vendor = "",
603 .support_list =
604 "SupportList:\n"
605 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
606 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
607 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
608 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
609 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
610 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
611 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
612 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
613 "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
614 .support_trail = '\x00',
615 .soft_ver = NULL,
616
617 .partitions = {
618 {"fs-uboot", 0x00000, 0x20000},
619 {"os-image", 0x20000, 0x150000},
620 {"file-system", 0x170000, 0x240000},
621 {"partition-table", 0x3b0000, 0x02000},
622 {"default-mac", 0x3c0000, 0x00020},
623 {"pin", 0x3c0100, 0x00020},
624 {"product-info", 0x3c1000, 0x01000},
625 {"soft-version", 0x3c2000, 0x00100},
626 {"support-list", 0x3c3000, 0x01000},
627 {"profile", 0x3c4000, 0x08000},
628 {"user-config", 0x3d0000, 0x10000},
629 {"default-config", 0x3e0000, 0x10000},
630 {"radio", 0x3f0000, 0x10000},
631 {NULL, 0, 0}
632 },
633
634 .first_sysupgrade_partition = "os-image",
635 .last_sysupgrade_partition = "file-system"
636 },
637
638 /** Firmware layout for the TL-WR1043 v4 */
639 {
640 .id = "TLWR1043NDV4",
641 .vendor = "",
642 .support_list =
643 "SupportList:\n"
644 "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
645 .support_trail = '\x00',
646 .soft_ver = NULL,
647
648 /**
649 We use a bigger os-image partition than the stock images (and thus
650 smaller file-system), as our kernel doesn't fit in the stock firmware's
651 1MB os-image.
652 */
653 .partitions = {
654 {"fs-uboot", 0x00000, 0x20000},
655 {"os-image", 0x20000, 0x180000},
656 {"file-system", 0x1a0000, 0xdb0000},
657 {"default-mac", 0xf50000, 0x00200},
658 {"pin", 0xf50200, 0x00200},
659 {"product-info", 0xf50400, 0x0fc00},
660 {"soft-version", 0xf60000, 0x0b000},
661 {"support-list", 0xf6b000, 0x04000},
662 {"profile", 0xf70000, 0x04000},
663 {"default-config", 0xf74000, 0x0b000},
664 {"user-config", 0xf80000, 0x40000},
665 {"partition-table", 0xfc0000, 0x10000},
666 {"log", 0xfd0000, 0x20000},
667 {"radio", 0xff0000, 0x10000},
668 {NULL, 0, 0}
669 },
670
671 .first_sysupgrade_partition = "os-image",
672 .last_sysupgrade_partition = "file-system"
673 },
674
675 /** Firmware layout for the TL-WR902AC v1 */
676 {
677 .id = "TL-WR902AC-V1",
678 .vendor = "",
679 .support_list =
680 "SupportList:\n"
681 "{product_name:TL-WR902AC,product_ver:1.0.0,special_id:45550000}\n",
682 .support_trail = '\x00',
683 .soft_ver = NULL,
684
685 /**
686 384KB were moved from file-system to os-image
687 in comparison to the stock image
688 */
689 .partitions = {
690 {"fs-uboot", 0x00000, 0x20000},
691 {"os-image", 0x20000, 0x160000},
692 {"file-system", 0x180000, 0x5d0000},
693 {"default-mac", 0x750000, 0x00200},
694 {"pin", 0x750200, 0x00200},
695 {"product-info", 0x750400, 0x0fc00},
696 {"soft-version", 0x760000, 0x0b000},
697 {"support-list", 0x76b000, 0x04000},
698 {"profile", 0x770000, 0x04000},
699 {"default-config", 0x774000, 0x0b000},
700 {"user-config", 0x780000, 0x40000},
701 {"partition-table", 0x7c0000, 0x10000},
702 {"log", 0x7d0000, 0x20000},
703 {"radio", 0x7f0000, 0x10000},
704 {NULL, 0, 0}
705 },
706
707 .first_sysupgrade_partition = "os-image",
708 .last_sysupgrade_partition = "file-system",
709 },
710
711 /** Firmware layout for the TL-WR942N V1 */
712 {
713 .id = "TLWR942NV1",
714 .vendor = "",
715 .support_list =
716 "SupportList:\r\n"
717 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
718 "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
719 .support_trail = '\x00',
720 .soft_ver = NULL,
721
722 .partitions = {
723 {"fs-uboot", 0x00000, 0x20000},
724 {"os-image", 0x20000, 0x150000},
725 {"file-system", 0x170000, 0xcd0000},
726 {"default-mac", 0xe40000, 0x00200},
727 {"pin", 0xe40200, 0x00200},
728 {"product-info", 0xe40400, 0x0fc00},
729 {"partition-table", 0xe50000, 0x10000},
730 {"soft-version", 0xe60000, 0x10000},
731 {"support-list", 0xe70000, 0x10000},
732 {"profile", 0xe80000, 0x10000},
733 {"default-config", 0xe90000, 0x10000},
734 {"user-config", 0xea0000, 0x40000},
735 {"qos-db", 0xee0000, 0x40000},
736 {"certificate", 0xf20000, 0x10000},
737 {"usb-config", 0xfb0000, 0x10000},
738 {"log", 0xfc0000, 0x20000},
739 {"radio-bk", 0xfe0000, 0x10000},
740 {"radio", 0xff0000, 0x10000},
741 {NULL, 0, 0}
742 },
743
744 .first_sysupgrade_partition = "os-image",
745 .last_sysupgrade_partition = "file-system",
746 },
747
748 /** Firmware layout for the RE450 */
749 {
750 .id = "RE450",
751 .vendor = "",
752 .support_list =
753 "SupportList:\r\n"
754 "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
755 "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
756 "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
757 "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
758 "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
759 "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
760 "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
761 "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
762 .support_trail = '\x00',
763 .soft_ver = NULL,
764
765 /**
766 The flash partition table for RE450;
767 it is almost the same as the one used by the stock images,
768 576KB were moved from file-system to os-image.
769 */
770 .partitions = {
771 {"fs-uboot", 0x00000, 0x20000},
772 {"os-image", 0x20000, 0x150000},
773 {"file-system", 0x170000, 0x4a0000},
774 {"partition-table", 0x600000, 0x02000},
775 {"default-mac", 0x610000, 0x00020},
776 {"pin", 0x610100, 0x00020},
777 {"product-info", 0x611100, 0x01000},
778 {"soft-version", 0x620000, 0x01000},
779 {"support-list", 0x621000, 0x01000},
780 {"profile", 0x622000, 0x08000},
781 {"user-config", 0x630000, 0x10000},
782 {"default-config", 0x640000, 0x10000},
783 {"radio", 0x7f0000, 0x10000},
784 {NULL, 0, 0}
785 },
786
787 .first_sysupgrade_partition = "os-image",
788 .last_sysupgrade_partition = "file-system"
789 },
790
791 {}
792 };
793
794 #define error(_ret, _errno, _str, ...) \
795 do { \
796 fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
797 strerror(_errno)); \
798 if (_ret) \
799 exit(_ret); \
800 } while (0)
801
802
803 /** Stores a uint32 as big endian */
804 static inline void put32(uint8_t *buf, uint32_t val) {
805 buf[0] = val >> 24;
806 buf[1] = val >> 16;
807 buf[2] = val >> 8;
808 buf[3] = val;
809 }
810
811 /** Allocates a new image partition */
812 static struct image_partition_entry alloc_image_partition(const char *name, size_t len) {
813 struct image_partition_entry entry = {name, len, malloc(len)};
814 if (!entry.data)
815 error(1, errno, "malloc");
816
817 return entry;
818 }
819
820 /** Frees an image partition */
821 static void free_image_partition(struct image_partition_entry entry) {
822 free(entry.data);
823 }
824
825 static time_t source_date_epoch = -1;
826 static void set_source_date_epoch() {
827 char *env = getenv("SOURCE_DATE_EPOCH");
828 char *endptr = env;
829 errno = 0;
830 if (env && *env) {
831 source_date_epoch = strtoull(env, &endptr, 10);
832 if (errno || (endptr && *endptr != '\0')) {
833 fprintf(stderr, "Invalid SOURCE_DATE_EPOCH");
834 exit(1);
835 }
836 }
837 }
838
839 /** Generates the partition-table partition */
840 static struct image_partition_entry make_partition_table(const struct flash_partition_entry *p) {
841 struct image_partition_entry entry = alloc_image_partition("partition-table", 0x800);
842
843 char *s = (char *)entry.data, *end = (char *)(s+entry.size);
844
845 *(s++) = 0x00;
846 *(s++) = 0x04;
847 *(s++) = 0x00;
848 *(s++) = 0x00;
849
850 size_t i;
851 for (i = 0; p[i].name; i++) {
852 size_t len = end-s;
853 size_t w = snprintf(s, len, "partition %s base 0x%05x size 0x%05x\n", p[i].name, p[i].base, p[i].size);
854
855 if (w > len-1)
856 error(1, 0, "flash partition table overflow?");
857
858 s += w;
859 }
860
861 s++;
862
863 memset(s, 0xff, end-s);
864
865 return entry;
866 }
867
868
869 /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
870 static inline uint8_t bcd(uint8_t v) {
871 return 0x10 * (v/10) + v%10;
872 }
873
874
875 /** Generates the soft-version partition */
876 static struct image_partition_entry make_soft_version(uint32_t rev) {
877 struct image_partition_entry entry = alloc_image_partition("soft-version", sizeof(struct soft_version));
878 struct soft_version *s = (struct soft_version *)entry.data;
879
880 time_t t;
881
882 if (source_date_epoch != -1)
883 t = source_date_epoch;
884 else if (time(&t) == (time_t)(-1))
885 error(1, errno, "time");
886
887 struct tm *tm = localtime(&t);
888
889 s->magic = htonl(0x0000000c);
890 s->zero = 0;
891 s->pad1 = 0xff;
892
893 s->version_major = 0;
894 s->version_minor = 0;
895 s->version_patch = 0;
896
897 s->year_hi = bcd((1900+tm->tm_year)/100);
898 s->year_lo = bcd(tm->tm_year%100);
899 s->month = bcd(tm->tm_mon+1);
900 s->day = bcd(tm->tm_mday);
901 s->rev = htonl(rev);
902
903 s->pad2 = 0xff;
904
905 return entry;
906 }
907
908 static struct image_partition_entry make_soft_version_from_string(const char *soft_ver) {
909 /** String length _including_ the terminating zero byte */
910 uint32_t ver_len = strlen(soft_ver) + 1;
911 /** Partition contains 64 bit header, the version string, and one additional null byte */
912 size_t partition_len = 2*sizeof(uint32_t) + ver_len + 1;
913 struct image_partition_entry entry = alloc_image_partition("soft-version", partition_len);
914
915 uint32_t *len = (uint32_t *)entry.data;
916 len[0] = htonl(ver_len);
917 len[1] = 0;
918 memcpy(&len[2], soft_ver, ver_len);
919
920 entry.data[partition_len - 1] = 0;
921
922 return entry;
923 }
924
925 /** Generates the support-list partition */
926 static struct image_partition_entry make_support_list(const struct device_info *info) {
927 size_t len = strlen(info->support_list);
928 struct image_partition_entry entry = alloc_image_partition("support-list", len + 9);
929
930 put32(entry.data, len);
931 memset(entry.data+4, 0, 4);
932 memcpy(entry.data+8, info->support_list, len);
933 entry.data[len+8] = info->support_trail;
934
935 return entry;
936 }
937
938 /** Creates a new image partition with an arbitrary name from a file */
939 static struct image_partition_entry read_file(const char *part_name, const char *filename, bool add_jffs2_eof) {
940 struct stat statbuf;
941
942 if (stat(filename, &statbuf) < 0)
943 error(1, errno, "unable to stat file `%s'", filename);
944
945 size_t len = statbuf.st_size;
946
947 if (add_jffs2_eof)
948 len = ALIGN(len, 0x10000) + sizeof(jffs2_eof_mark);
949
950 struct image_partition_entry entry = alloc_image_partition(part_name, len);
951
952 FILE *file = fopen(filename, "rb");
953 if (!file)
954 error(1, errno, "unable to open file `%s'", filename);
955
956 if (fread(entry.data, statbuf.st_size, 1, file) != 1)
957 error(1, errno, "unable to read file `%s'", filename);
958
959 if (add_jffs2_eof) {
960 uint8_t *eof = entry.data + statbuf.st_size, *end = entry.data+entry.size;
961
962 memset(eof, 0xff, end - eof - sizeof(jffs2_eof_mark));
963 memcpy(end - sizeof(jffs2_eof_mark), jffs2_eof_mark, sizeof(jffs2_eof_mark));
964 }
965
966 fclose(file);
967
968 return entry;
969 }
970
971 /** Creates a new image partition from arbitrary data */
972 static struct image_partition_entry put_data(const char *part_name, const char *datain, size_t len) {
973
974 struct image_partition_entry entry = alloc_image_partition(part_name, len);
975
976 memcpy(entry.data, datain, len);
977
978 return entry;
979 }
980
981 /**
982 Copies a list of image partitions into an image buffer and generates the image partition table while doing so
983
984 Example image partition table:
985
986 fwup-ptn partition-table base 0x00800 size 0x00800
987 fwup-ptn os-image base 0x01000 size 0x113b45
988 fwup-ptn file-system base 0x114b45 size 0x1d0004
989 fwup-ptn support-list base 0x2e4b49 size 0x000d1
990
991 Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
992 the end of the partition table is marked with a zero byte.
993
994 The firmware image must contain at least the partition-table and support-list partitions
995 to be accepted. There aren't any alignment constraints for the image partitions.
996
997 The partition-table partition contains the actual flash layout; partitions
998 from the image partition table are mapped to the corresponding flash partitions during
999 the firmware upgrade. The support-list partition contains a list of devices supported by
1000 the firmware image.
1001
1002 The base offsets in the firmware partition table are relative to the end
1003 of the vendor information block, so the partition-table partition will
1004 actually start at offset 0x1814 of the image.
1005
1006 I think partition-table must be the first partition in the firmware image.
1007 */
1008 static void put_partitions(uint8_t *buffer, const struct flash_partition_entry *flash_parts, const struct image_partition_entry *parts) {
1009 size_t i, j;
1010 char *image_pt = (char *)buffer, *end = image_pt + 0x800;
1011
1012 size_t base = 0x800;
1013 for (i = 0; parts[i].name; i++) {
1014 for (j = 0; flash_parts[j].name; j++) {
1015 if (!strcmp(flash_parts[j].name, parts[i].name)) {
1016 if (parts[i].size > flash_parts[j].size)
1017 error(1, 0, "%s partition too big (more than %u bytes)", flash_parts[j].name, (unsigned)flash_parts[j].size);
1018 break;
1019 }
1020 }
1021
1022 assert(flash_parts[j].name);
1023
1024 memcpy(buffer + base, parts[i].data, parts[i].size);
1025
1026 size_t len = end-image_pt;
1027 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);
1028
1029 if (w > len-1)
1030 error(1, 0, "image partition table overflow?");
1031
1032 image_pt += w;
1033
1034 base += parts[i].size;
1035 }
1036 }
1037
1038 /** Generates and writes the image MD5 checksum */
1039 static void put_md5(uint8_t *md5, uint8_t *buffer, unsigned int len) {
1040 MD5_CTX ctx;
1041
1042 MD5_Init(&ctx);
1043 MD5_Update(&ctx, md5_salt, (unsigned int)sizeof(md5_salt));
1044 MD5_Update(&ctx, buffer, len);
1045 MD5_Final(md5, &ctx);
1046 }
1047
1048
1049 /**
1050 Generates the firmware image in factory format
1051
1052 Image format:
1053
1054 Bytes (hex) Usage
1055 ----------- -----
1056 0000-0003 Image size (4 bytes, big endian)
1057 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
1058 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
1059 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
1060 (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
1061 1014-1813 Image partition table (2048 bytes, padded with 0xff)
1062 1814-xxxx Firmware partitions
1063 */
1064 static void * generate_factory_image(const struct device_info *info, const struct image_partition_entry *parts, size_t *len) {
1065 *len = 0x1814;
1066
1067 size_t i;
1068 for (i = 0; parts[i].name; i++)
1069 *len += parts[i].size;
1070
1071 uint8_t *image = malloc(*len);
1072 if (!image)
1073 error(1, errno, "malloc");
1074
1075 memset(image, 0xff, *len);
1076 put32(image, *len);
1077
1078 if (info->vendor) {
1079 size_t vendor_len = strlen(info->vendor);
1080 put32(image+0x14, vendor_len);
1081 memcpy(image+0x18, info->vendor, vendor_len);
1082 }
1083
1084 put_partitions(image + 0x1014, info->partitions, parts);
1085 put_md5(image+0x04, image+0x14, *len-0x14);
1086
1087 return image;
1088 }
1089
1090 /**
1091 Generates the firmware image in sysupgrade format
1092
1093 This makes some assumptions about the provided flash and image partition tables and
1094 should be generalized when TP-LINK starts building its safeloader into hardware with
1095 different flash layouts.
1096 */
1097 static void * generate_sysupgrade_image(const struct device_info *info, const struct image_partition_entry *image_parts, size_t *len) {
1098 size_t i, j;
1099 size_t flash_first_partition_index = 0;
1100 size_t flash_last_partition_index = 0;
1101 const struct flash_partition_entry *flash_first_partition = NULL;
1102 const struct flash_partition_entry *flash_last_partition = NULL;
1103 const struct image_partition_entry *image_last_partition = NULL;
1104
1105 /** Find first and last partitions */
1106 for (i = 0; info->partitions[i].name; i++) {
1107 if (!strcmp(info->partitions[i].name, info->first_sysupgrade_partition)) {
1108 flash_first_partition = &info->partitions[i];
1109 flash_first_partition_index = i;
1110 } else if (!strcmp(info->partitions[i].name, info->last_sysupgrade_partition)) {
1111 flash_last_partition = &info->partitions[i];
1112 flash_last_partition_index = i;
1113 }
1114 }
1115
1116 assert(flash_first_partition && flash_last_partition);
1117 assert(flash_first_partition_index < flash_last_partition_index);
1118
1119 /** Find last partition from image to calculate needed size */
1120 for (i = 0; image_parts[i].name; i++) {
1121 if (!strcmp(image_parts[i].name, info->last_sysupgrade_partition)) {
1122 image_last_partition = &image_parts[i];
1123 break;
1124 }
1125 }
1126
1127 assert(image_last_partition);
1128
1129 *len = flash_last_partition->base - flash_first_partition->base + image_last_partition->size;
1130
1131 uint8_t *image = malloc(*len);
1132 if (!image)
1133 error(1, errno, "malloc");
1134
1135 memset(image, 0xff, *len);
1136
1137 for (i = flash_first_partition_index; i <= flash_last_partition_index; i++) {
1138 for (j = 0; image_parts[j].name; j++) {
1139 if (!strcmp(info->partitions[i].name, image_parts[j].name)) {
1140 if (image_parts[j].size > info->partitions[i].size)
1141 error(1, 0, "%s partition too big (more than %u bytes)", info->partitions[i].name, (unsigned)info->partitions[i].size);
1142 memcpy(image + info->partitions[i].base - flash_first_partition->base, image_parts[j].data, image_parts[j].size);
1143 break;
1144 }
1145
1146 assert(image_parts[j].name);
1147 }
1148 }
1149
1150 return image;
1151 }
1152
1153 /** Generates an image according to a given layout and writes it to a file */
1154 static void build_image(const char *output,
1155 const char *kernel_image,
1156 const char *rootfs_image,
1157 uint32_t rev,
1158 bool add_jffs2_eof,
1159 bool sysupgrade,
1160 const struct device_info *info) {
1161
1162 struct image_partition_entry parts[7] = {};
1163
1164 parts[0] = make_partition_table(info->partitions);
1165 if (info->soft_ver)
1166 parts[1] = make_soft_version_from_string(info->soft_ver);
1167 else
1168 parts[1] = make_soft_version(rev);
1169
1170 parts[2] = make_support_list(info);
1171 parts[3] = read_file("os-image", kernel_image, false);
1172 parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof);
1173
1174 if (strcasecmp(info->id, "ARCHER-C25-V1") == 0) {
1175 const char mdat[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
1176 parts[5] = put_data("extra-para", mdat, 11);
1177 }
1178
1179 size_t len;
1180 void *image;
1181 if (sysupgrade)
1182 image = generate_sysupgrade_image(info, parts, &len);
1183 else
1184 image = generate_factory_image(info, parts, &len);
1185
1186 FILE *file = fopen(output, "wb");
1187 if (!file)
1188 error(1, errno, "unable to open output file");
1189
1190 if (fwrite(image, len, 1, file) != 1)
1191 error(1, 0, "unable to write output file");
1192
1193 fclose(file);
1194
1195 free(image);
1196
1197 size_t i;
1198 for (i = 0; parts[i].name; i++)
1199 free_image_partition(parts[i]);
1200 }
1201
1202 /** Usage output */
1203 static void usage(const char *argv0) {
1204 fprintf(stderr,
1205 "Usage: %s [OPTIONS...]\n"
1206 "\n"
1207 "Options:\n"
1208 " -B <board> create image for the board specified with <board>\n"
1209 " -k <file> read kernel image from the file <file>\n"
1210 " -r <file> read rootfs image from the file <file>\n"
1211 " -o <file> write output to the file <file>\n"
1212 " -V <rev> sets the revision number to <rev>\n"
1213 " -j add jffs2 end-of-filesystem markers\n"
1214 " -S create sysupgrade instead of factory image\n"
1215 " -h show this help\n",
1216 argv0
1217 );
1218 };
1219
1220
1221 static const struct device_info *find_board(const char *id)
1222 {
1223 struct device_info *board = NULL;
1224
1225 for (board = boards; board->id != NULL; board++)
1226 if (strcasecmp(id, board->id) == 0)
1227 return board;
1228
1229 return NULL;
1230 }
1231
1232 int main(int argc, char *argv[]) {
1233 const char *board = NULL, *kernel_image = NULL, *rootfs_image = NULL, *output = NULL;
1234 bool add_jffs2_eof = false, sysupgrade = false;
1235 unsigned rev = 0;
1236 const struct device_info *info;
1237 set_source_date_epoch();
1238
1239 while (true) {
1240 int c;
1241
1242 c = getopt(argc, argv, "B:k:r:o:V:jSh");
1243 if (c == -1)
1244 break;
1245
1246 switch (c) {
1247 case 'B':
1248 board = optarg;
1249 break;
1250
1251 case 'k':
1252 kernel_image = optarg;
1253 break;
1254
1255 case 'r':
1256 rootfs_image = optarg;
1257 break;
1258
1259 case 'o':
1260 output = optarg;
1261 break;
1262
1263 case 'V':
1264 sscanf(optarg, "r%u", &rev);
1265 break;
1266
1267 case 'j':
1268 add_jffs2_eof = true;
1269 break;
1270
1271 case 'S':
1272 sysupgrade = true;
1273 break;
1274
1275 case 'h':
1276 usage(argv[0]);
1277 return 0;
1278
1279 default:
1280 usage(argv[0]);
1281 return 1;
1282 }
1283 }
1284
1285 if (!board)
1286 error(1, 0, "no board has been specified");
1287 if (!kernel_image)
1288 error(1, 0, "no kernel image has been specified");
1289 if (!rootfs_image)
1290 error(1, 0, "no rootfs image has been specified");
1291 if (!output)
1292 error(1, 0, "no output filename has been specified");
1293
1294 info = find_board(board);
1295
1296 if (info == NULL)
1297 error(1, 0, "unsupported board %s", board);
1298
1299 build_image(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, info);
1300
1301 return 0;
1302 }