0064-firmware-utils-backport-tplink-safeloader-from-LEDE-05abcf518d5e2b7d6526fd7a87a88a268030694a.patch 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. From: Matthias Schiffer <mschiffer@universe-factory.net>
  2. Date: Wed, 21 Dec 2016 19:22:56 +0100
  3. Subject: firmware-utils: backport tplink-safeloader from LEDE 05abcf518d5e2b7d6526fd7a87a88a268030694a
  4. Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
  5. diff --git a/tools/firmware-utils/src/tplink-safeloader.c b/tools/firmware-utils/src/tplink-safeloader.c
  6. index 77a894b237281f6211dd1123ead4b5b0f2f58370..016c118787f3b5332b76f65c85930065a569d6b8 100644
  7. --- a/tools/firmware-utils/src/tplink-safeloader.c
  8. +++ b/tools/firmware-utils/src/tplink-safeloader.c
  9. @@ -53,6 +53,8 @@
  10. #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
  11. +#define MAX_PARTITIONS 32
  12. +
  13. /** An image partition table entry */
  14. struct image_partition_entry {
  15. const char *name;
  16. @@ -67,6 +69,16 @@ struct flash_partition_entry {
  17. uint32_t size;
  18. };
  19. +/** Firmware layout description */
  20. +struct device_info {
  21. + const char *id;
  22. + const char *vendor;
  23. + const char *support_list;
  24. + char support_trail;
  25. + const struct flash_partition_entry partitions[MAX_PARTITIONS+1];
  26. + const char *first_sysupgrade_partition;
  27. + const char *last_sysupgrade_partition;
  28. +};
  29. /** The content of the soft-version structure */
  30. struct __attribute__((__packed__)) soft_version {
  31. @@ -102,45 +114,225 @@ static const uint8_t md5_salt[16] = {
  32. };
  33. -/** Vendor information for CPE210/220/510/520 */
  34. -static const char cpe510_vendor[] = "CPE510(TP-LINK|UN|N300-5):1.0\r\n";
  35. -
  36. -
  37. -/**
  38. - The flash partition table for CPE210/220/510/520;
  39. - it is the same as the one used by the stock images.
  40. -*/
  41. -static const struct flash_partition_entry cpe510_partitions[] = {
  42. - {"fs-uboot", 0x00000, 0x20000},
  43. - {"partition-table", 0x20000, 0x02000},
  44. - {"default-mac", 0x30000, 0x00020},
  45. - {"product-info", 0x31100, 0x00100},
  46. - {"signature", 0x32000, 0x00400},
  47. - {"os-image", 0x40000, 0x170000},
  48. - {"soft-version", 0x1b0000, 0x00100},
  49. - {"support-list", 0x1b1000, 0x00400},
  50. - {"file-system", 0x1c0000, 0x600000},
  51. - {"user-config", 0x7c0000, 0x10000},
  52. - {"default-config", 0x7d0000, 0x10000},
  53. - {"log", 0x7e0000, 0x10000},
  54. - {"radio", 0x7f0000, 0x10000},
  55. - {NULL, 0, 0}
  56. +/** Firmware layout table */
  57. +static struct device_info boards[] = {
  58. + /** Firmware layout for the CPE210/220 */
  59. + {
  60. + .id = "CPE210",
  61. + .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
  62. + .support_list =
  63. + "SupportList:\r\n"
  64. + "CPE210(TP-LINK|UN|N300-2):1.0\r\n"
  65. + "CPE210(TP-LINK|UN|N300-2):1.1\r\n"
  66. + "CPE210(TP-LINK|US|N300-2):1.1\r\n"
  67. + "CPE210(TP-LINK|EU|N300-2):1.1\r\n"
  68. + "CPE220(TP-LINK|UN|N300-2):1.1\r\n"
  69. + "CPE220(TP-LINK|US|N300-2):1.1\r\n"
  70. + "CPE220(TP-LINK|EU|N300-2):1.1\r\n",
  71. + .support_trail = '\xff',
  72. +
  73. + .partitions = {
  74. + {"fs-uboot", 0x00000, 0x20000},
  75. + {"partition-table", 0x20000, 0x02000},
  76. + {"default-mac", 0x30000, 0x00020},
  77. + {"product-info", 0x31100, 0x00100},
  78. + {"signature", 0x32000, 0x00400},
  79. + {"os-image", 0x40000, 0x170000},
  80. + {"soft-version", 0x1b0000, 0x00100},
  81. + {"support-list", 0x1b1000, 0x00400},
  82. + {"file-system", 0x1c0000, 0x600000},
  83. + {"user-config", 0x7c0000, 0x10000},
  84. + {"default-config", 0x7d0000, 0x10000},
  85. + {"log", 0x7e0000, 0x10000},
  86. + {"radio", 0x7f0000, 0x10000},
  87. + {NULL, 0, 0}
  88. + },
  89. +
  90. + .first_sysupgrade_partition = "os-image",
  91. + .last_sysupgrade_partition = "file-system",
  92. + },
  93. +
  94. + /** Firmware layout for the CPE510/520 */
  95. + {
  96. + .id = "CPE510",
  97. + .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
  98. + .support_list =
  99. + "SupportList:\r\n"
  100. + "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
  101. + "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
  102. + "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
  103. + "CPE510(TP-LINK|US|N300-5):1.1\r\n"
  104. + "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
  105. + "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
  106. + "CPE520(TP-LINK|US|N300-5):1.1\r\n"
  107. + "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
  108. + .support_trail = '\xff',
  109. +
  110. + .partitions = {
  111. + {"fs-uboot", 0x00000, 0x20000},
  112. + {"partition-table", 0x20000, 0x02000},
  113. + {"default-mac", 0x30000, 0x00020},
  114. + {"product-info", 0x31100, 0x00100},
  115. + {"signature", 0x32000, 0x00400},
  116. + {"os-image", 0x40000, 0x170000},
  117. + {"soft-version", 0x1b0000, 0x00100},
  118. + {"support-list", 0x1b1000, 0x00400},
  119. + {"file-system", 0x1c0000, 0x600000},
  120. + {"user-config", 0x7c0000, 0x10000},
  121. + {"default-config", 0x7d0000, 0x10000},
  122. + {"log", 0x7e0000, 0x10000},
  123. + {"radio", 0x7f0000, 0x10000},
  124. + {NULL, 0, 0}
  125. + },
  126. +
  127. + .first_sysupgrade_partition = "os-image",
  128. + .last_sysupgrade_partition = "file-system",
  129. + },
  130. +
  131. + /** Firmware layout for the C2600 */
  132. + {
  133. + .id = "C2600",
  134. + .vendor = "",
  135. + .support_list =
  136. + "SupportList:\r\n"
  137. + "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
  138. + .support_trail = '\x00',
  139. +
  140. + .partitions = {
  141. + {"SBL1", 0x00000, 0x20000},
  142. + {"MIBIB", 0x20000, 0x20000},
  143. + {"SBL2", 0x40000, 0x20000},
  144. + {"SBL3", 0x60000, 0x30000},
  145. + {"DDRCONFIG", 0x90000, 0x10000},
  146. + {"SSD", 0xa0000, 0x10000},
  147. + {"TZ", 0xb0000, 0x30000},
  148. + {"RPM", 0xe0000, 0x20000},
  149. + {"fs-uboot", 0x100000, 0x70000},
  150. + {"uboot-env", 0x170000, 0x40000},
  151. + {"radio", 0x1b0000, 0x40000},
  152. + {"os-image", 0x1f0000, 0x200000},
  153. + {"file-system", 0x3f0000, 0x1b00000},
  154. + {"default-mac", 0x1ef0000, 0x00200},
  155. + {"pin", 0x1ef0200, 0x00200},
  156. + {"product-info", 0x1ef0400, 0x0fc00},
  157. + {"partition-table", 0x1f00000, 0x10000},
  158. + {"soft-version", 0x1f10000, 0x10000},
  159. + {"support-list", 0x1f20000, 0x10000},
  160. + {"profile", 0x1f30000, 0x10000},
  161. + {"default-config", 0x1f40000, 0x10000},
  162. + {"user-config", 0x1f50000, 0x40000},
  163. + {"qos-db", 0x1f90000, 0x40000},
  164. + {"usb-config", 0x1fd0000, 0x10000},
  165. + {"log", 0x1fe0000, 0x20000},
  166. + {NULL, 0, 0}
  167. + },
  168. +
  169. + .first_sysupgrade_partition = "os-image",
  170. + .last_sysupgrade_partition = "file-system"
  171. + },
  172. +
  173. + /** Firmware layout for the C9 */
  174. + {
  175. + .id = "ARCHERC9",
  176. + .vendor = "",
  177. + .support_list =
  178. + "SupportList:\n"
  179. + "{product_name:ArcherC9,"
  180. + "product_ver:1.0.0,"
  181. + "special_id:00000000}\n",
  182. + .support_trail = '\x00',
  183. +
  184. + .partitions = {
  185. + {"fs-uboot", 0x00000, 0x40000},
  186. + {"os-image", 0x40000, 0x200000},
  187. + {"file-system", 0x240000, 0xc00000},
  188. + {"default-mac", 0xe40000, 0x00200},
  189. + {"pin", 0xe40200, 0x00200},
  190. + {"product-info", 0xe40400, 0x00200},
  191. + {"partition-table", 0xe50000, 0x10000},
  192. + {"soft-version", 0xe60000, 0x00200},
  193. + {"support-list", 0xe61000, 0x0f000},
  194. + {"profile", 0xe70000, 0x10000},
  195. + {"default-config", 0xe80000, 0x10000},
  196. + {"user-config", 0xe90000, 0x50000},
  197. + {"log", 0xee0000, 0x100000},
  198. + {"radio_bk", 0xfe0000, 0x10000},
  199. + {"radio", 0xff0000, 0x10000},
  200. + {NULL, 0, 0}
  201. + },
  202. +
  203. + .first_sysupgrade_partition = "os-image",
  204. + .last_sysupgrade_partition = "file-system"
  205. + },
  206. +
  207. + /** Firmware layout for the EAP120 */
  208. + {
  209. + .id = "EAP120",
  210. + .vendor = "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
  211. + .support_list =
  212. + "SupportList:\r\n"
  213. + "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
  214. + .support_trail = '\xff',
  215. +
  216. + .partitions = {
  217. + {"fs-uboot", 0x00000, 0x20000},
  218. + {"partition-table", 0x20000, 0x02000},
  219. + {"default-mac", 0x30000, 0x00020},
  220. + {"support-list", 0x31000, 0x00100},
  221. + {"product-info", 0x31100, 0x00100},
  222. + {"soft-version", 0x32000, 0x00100},
  223. + {"os-image", 0x40000, 0x180000},
  224. + {"file-system", 0x1c0000, 0x600000},
  225. + {"user-config", 0x7c0000, 0x10000},
  226. + {"backup-config", 0x7d0000, 0x10000},
  227. + {"log", 0x7e0000, 0x10000},
  228. + {"radio", 0x7f0000, 0x10000},
  229. + {NULL, 0, 0}
  230. + },
  231. +
  232. + .first_sysupgrade_partition = "os-image",
  233. + .last_sysupgrade_partition = "file-system"
  234. + },
  235. +
  236. + /** Firmware layout for the TL-WR1043 v4 */
  237. + {
  238. + .id = "TLWR1043NDV4",
  239. + .vendor = "",
  240. + .support_list =
  241. + "SupportList:\n"
  242. + "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
  243. + .support_trail = '\x00',
  244. +
  245. + /**
  246. + We use a bigger os-image partition than the stock images (and thus
  247. + smaller file-system), as our kernel doesn't fit in the stock firmware's
  248. + 1MB os-image.
  249. + */
  250. + .partitions = {
  251. + {"fs-uboot", 0x00000, 0x20000},
  252. + {"os-image", 0x20000, 0x180000},
  253. + {"file-system", 0x1a0000, 0xdb0000},
  254. + {"default-mac", 0xf50000, 0x00200},
  255. + {"pin", 0xf50200, 0x00200},
  256. + {"product-info", 0xf50400, 0x0fc00},
  257. + {"soft-version", 0xf60000, 0x0b000},
  258. + {"support-list", 0xf6b000, 0x04000},
  259. + {"profile", 0xf70000, 0x04000},
  260. + {"default-config", 0xf74000, 0x0b000},
  261. + {"user-config", 0xf80000, 0x40000},
  262. + {"partition-table", 0xfc0000, 0x10000},
  263. + {"log", 0xfd0000, 0x20000},
  264. + {"radio", 0xff0000, 0x10000},
  265. + {NULL, 0, 0}
  266. + },
  267. +
  268. + .first_sysupgrade_partition = "os-image",
  269. + .last_sysupgrade_partition = "file-system"
  270. + },
  271. +
  272. + {}
  273. };
  274. -/**
  275. - The support list for CPE210/220/510/520
  276. -*/
  277. -static const char cpe510_support_list[] =
  278. - "SupportList:\r\n"
  279. - "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
  280. - "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
  281. - "CPE520(TP-LINK|UN|N300-5):1.0\r\n"
  282. - "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
  283. - "CPE210(TP-LINK|UN|N300-2):1.0\r\n"
  284. - "CPE210(TP-LINK|UN|N300-2):1.1\r\n"
  285. - "CPE220(TP-LINK|UN|N300-2):1.0\r\n"
  286. - "CPE220(TP-LINK|UN|N300-2):1.1\r\n";
  287. -
  288. #define error(_ret, _errno, _str, ...) \
  289. do { \
  290. fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
  291. @@ -240,14 +432,14 @@ static struct image_partition_entry make_soft_version(uint32_t rev) {
  292. }
  293. /** Generates the support-list partition */
  294. -static struct image_partition_entry make_support_list(const char *support_list) {
  295. - size_t len = strlen(support_list);
  296. +static struct image_partition_entry make_support_list(const struct device_info *info) {
  297. + size_t len = strlen(info->support_list);
  298. struct image_partition_entry entry = alloc_image_partition("support-list", len + 9);
  299. put32(entry.data, len);
  300. memset(entry.data+4, 0, 4);
  301. - memcpy(entry.data+8, support_list, len);
  302. - entry.data[len+8] = '\xff';
  303. + memcpy(entry.data+8, info->support_list, len);
  304. + entry.data[len+8] = info->support_trail;
  305. return entry;
  306. }
  307. @@ -313,12 +505,22 @@ static struct image_partition_entry read_file(const char *part_name, const char
  308. I think partition-table must be the first partition in the firmware image.
  309. */
  310. -static void put_partitions(uint8_t *buffer, const struct image_partition_entry *parts) {
  311. - size_t i;
  312. +static void put_partitions(uint8_t *buffer, const struct flash_partition_entry *flash_parts, const struct image_partition_entry *parts) {
  313. + size_t i, j;
  314. char *image_pt = (char *)buffer, *end = image_pt + 0x800;
  315. size_t base = 0x800;
  316. for (i = 0; parts[i].name; i++) {
  317. + for (j = 0; flash_parts[j].name; j++) {
  318. + if (!strcmp(flash_parts[j].name, parts[i].name)) {
  319. + if (parts[i].size > flash_parts[j].size)
  320. + error(1, 0, "%s partition too big (more than %u bytes)", flash_parts[j].name, (unsigned)flash_parts[j].size);
  321. + break;
  322. + }
  323. + }
  324. +
  325. + assert(flash_parts[j].name);
  326. +
  327. memcpy(buffer + base, parts[i].data, parts[i].size);
  328. size_t len = end-image_pt;
  329. @@ -331,10 +533,6 @@ static void put_partitions(uint8_t *buffer, const struct image_partition_entry *
  330. base += parts[i].size;
  331. }
  332. -
  333. - image_pt++;
  334. -
  335. - memset(image_pt, 0xff, end-image_pt);
  336. }
  337. /** Generates and writes the image MD5 checksum */
  338. @@ -363,7 +561,7 @@ static void put_md5(uint8_t *md5, uint8_t *buffer, unsigned int len) {
  339. 1014-1813 Image partition table (2048 bytes, padded with 0xff)
  340. 1814-xxxx Firmware partitions
  341. */
  342. -static void * generate_factory_image(const char *vendor, const struct image_partition_entry *parts, size_t *len) {
  343. +static void * generate_factory_image(const struct device_info *info, const struct image_partition_entry *parts, size_t *len) {
  344. *len = 0x1814;
  345. size_t i;
  346. @@ -374,14 +572,16 @@ static void * generate_factory_image(const char *vendor, const struct image_part
  347. if (!image)
  348. error(1, errno, "malloc");
  349. + memset(image, 0xff, *len);
  350. put32(image, *len);
  351. - size_t vendor_len = strlen(vendor);
  352. - put32(image+0x14, vendor_len);
  353. - memcpy(image+0x18, vendor, vendor_len);
  354. - memset(image+0x18+vendor_len, 0xff, 4092-vendor_len);
  355. + if (info->vendor) {
  356. + size_t vendor_len = strlen(info->vendor);
  357. + put32(image+0x14, vendor_len);
  358. + memcpy(image+0x18, info->vendor, vendor_len);
  359. + }
  360. - put_partitions(image + 0x1014, parts);
  361. + put_partitions(image + 0x1014, info->partitions, parts);
  362. put_md5(image+0x04, image+0x14, *len-0x14);
  363. return image;
  364. @@ -394,33 +594,39 @@ static void * generate_factory_image(const char *vendor, const struct image_part
  365. should be generalized when TP-LINK starts building its safeloader into hardware with
  366. different flash layouts.
  367. */
  368. -static void * generate_sysupgrade_image(const struct flash_partition_entry *flash_parts, const struct image_partition_entry *image_parts, size_t *len) {
  369. - const struct flash_partition_entry *flash_os_image = &flash_parts[5];
  370. - const struct flash_partition_entry *flash_soft_version = &flash_parts[6];
  371. - const struct flash_partition_entry *flash_support_list = &flash_parts[7];
  372. - const struct flash_partition_entry *flash_file_system = &flash_parts[8];
  373. -
  374. - const struct image_partition_entry *image_os_image = &image_parts[3];
  375. - const struct image_partition_entry *image_soft_version = &image_parts[1];
  376. - const struct image_partition_entry *image_support_list = &image_parts[2];
  377. - const struct image_partition_entry *image_file_system = &image_parts[4];
  378. -
  379. - assert(strcmp(flash_os_image->name, "os-image") == 0);
  380. - assert(strcmp(flash_soft_version->name, "soft-version") == 0);
  381. - assert(strcmp(flash_support_list->name, "support-list") == 0);
  382. - assert(strcmp(flash_file_system->name, "file-system") == 0);
  383. -
  384. - assert(strcmp(image_os_image->name, "os-image") == 0);
  385. - assert(strcmp(image_soft_version->name, "soft-version") == 0);
  386. - assert(strcmp(image_support_list->name, "support-list") == 0);
  387. - assert(strcmp(image_file_system->name, "file-system") == 0);
  388. -
  389. - if (image_os_image->size > flash_os_image->size)
  390. - error(1, 0, "kernel image too big (more than %u bytes)", (unsigned)flash_os_image->size);
  391. - if (image_file_system->size > flash_file_system->size)
  392. - error(1, 0, "rootfs image too big (more than %u bytes)", (unsigned)flash_file_system->size);
  393. -
  394. - *len = flash_file_system->base - flash_os_image->base + image_file_system->size;
  395. +static void * generate_sysupgrade_image(const struct device_info *info, const struct image_partition_entry *image_parts, size_t *len) {
  396. + size_t i, j;
  397. + size_t flash_first_partition_index = 0;
  398. + size_t flash_last_partition_index = 0;
  399. + const struct flash_partition_entry *flash_first_partition = NULL;
  400. + const struct flash_partition_entry *flash_last_partition = NULL;
  401. + const struct image_partition_entry *image_last_partition = NULL;
  402. +
  403. + /** Find first and last partitions */
  404. + for (i = 0; info->partitions[i].name; i++) {
  405. + if (!strcmp(info->partitions[i].name, info->first_sysupgrade_partition)) {
  406. + flash_first_partition = &info->partitions[i];
  407. + flash_first_partition_index = i;
  408. + } else if (!strcmp(info->partitions[i].name, info->last_sysupgrade_partition)) {
  409. + flash_last_partition = &info->partitions[i];
  410. + flash_last_partition_index = i;
  411. + }
  412. + }
  413. +
  414. + assert(flash_first_partition && flash_last_partition);
  415. + assert(flash_first_partition_index < flash_last_partition_index);
  416. +
  417. + /** Find last partition from image to calculate needed size */
  418. + for (i = 0; image_parts[i].name; i++) {
  419. + if (!strcmp(image_parts[i].name, info->last_sysupgrade_partition)) {
  420. + image_last_partition = &image_parts[i];
  421. + break;
  422. + }
  423. + }
  424. +
  425. + assert(image_last_partition);
  426. +
  427. + *len = flash_last_partition->base - flash_first_partition->base + image_last_partition->size;
  428. uint8_t *image = malloc(*len);
  429. if (!image)
  430. @@ -428,31 +634,44 @@ static void * generate_sysupgrade_image(const struct flash_partition_entry *flas
  431. memset(image, 0xff, *len);
  432. - memcpy(image, image_os_image->data, image_os_image->size);
  433. - memcpy(image + flash_soft_version->base - flash_os_image->base, image_soft_version->data, image_soft_version->size);
  434. - memcpy(image + flash_support_list->base - flash_os_image->base, image_support_list->data, image_support_list->size);
  435. - memcpy(image + flash_file_system->base - flash_os_image->base, image_file_system->data, image_file_system->size);
  436. + for (i = flash_first_partition_index; i <= flash_last_partition_index; i++) {
  437. + for (j = 0; image_parts[j].name; j++) {
  438. + if (!strcmp(info->partitions[i].name, image_parts[j].name)) {
  439. + if (image_parts[j].size > info->partitions[i].size)
  440. + error(1, 0, "%s partition too big (more than %u bytes)", info->partitions[i].name, (unsigned)info->partitions[i].size);
  441. + memcpy(image + info->partitions[i].base - flash_first_partition->base, image_parts[j].data, image_parts[j].size);
  442. + break;
  443. + }
  444. +
  445. + assert(image_parts[j].name);
  446. + }
  447. + }
  448. return image;
  449. }
  450. -
  451. -/** Generates an image for CPE210/220/510/520 and writes it to a file */
  452. -static void do_cpe510(const char *output, const char *kernel_image, const char *rootfs_image, uint32_t rev, bool add_jffs2_eof, bool sysupgrade) {
  453. +/** Generates an image according to a given layout and writes it to a file */
  454. +static void build_image(const char *output,
  455. + const char *kernel_image,
  456. + const char *rootfs_image,
  457. + uint32_t rev,
  458. + bool add_jffs2_eof,
  459. + bool sysupgrade,
  460. + const struct device_info *info) {
  461. struct image_partition_entry parts[6] = {};
  462. - parts[0] = make_partition_table(cpe510_partitions);
  463. + parts[0] = make_partition_table(info->partitions);
  464. parts[1] = make_soft_version(rev);
  465. - parts[2] = make_support_list(cpe510_support_list);
  466. + parts[2] = make_support_list(info);
  467. parts[3] = read_file("os-image", kernel_image, false);
  468. parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof);
  469. size_t len;
  470. void *image;
  471. if (sysupgrade)
  472. - image = generate_sysupgrade_image(cpe510_partitions, parts, &len);
  473. + image = generate_sysupgrade_image(info, parts, &len);
  474. else
  475. - image = generate_factory_image(cpe510_vendor, parts, &len);
  476. + image = generate_factory_image(info, parts, &len);
  477. FILE *file = fopen(output, "wb");
  478. if (!file)
  479. @@ -470,7 +689,6 @@ static void do_cpe510(const char *output, const char *kernel_image, const char *
  480. free_image_partition(parts[i]);
  481. }
  482. -
  483. /** Usage output */
  484. static void usage(const char *argv0) {
  485. fprintf(stderr,
  486. @@ -490,10 +708,22 @@ static void usage(const char *argv0) {
  487. };
  488. +static const struct device_info *find_board(const char *id)
  489. +{
  490. + struct device_info *board = NULL;
  491. +
  492. + for (board = boards; board->id != NULL; board++)
  493. + if (strcasecmp(id, board->id) == 0)
  494. + return board;
  495. +
  496. + return NULL;
  497. +}
  498. +
  499. int main(int argc, char *argv[]) {
  500. const char *board = NULL, *kernel_image = NULL, *rootfs_image = NULL, *output = NULL;
  501. bool add_jffs2_eof = false, sysupgrade = false;
  502. unsigned rev = 0;
  503. + const struct device_info *info;
  504. while (true) {
  505. int c;
  506. @@ -550,10 +780,12 @@ int main(int argc, char *argv[]) {
  507. if (!output)
  508. error(1, 0, "no output filename has been specified");
  509. - if (strcmp(board, "CPE510") == 0)
  510. - do_cpe510(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade);
  511. - else
  512. + info = find_board(board);
  513. +
  514. + if (info == NULL)
  515. error(1, 0, "unsupported board %s", board);
  516. + build_image(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, info);
  517. +
  518. return 0;
  519. }