tplink-safeloader.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /*
  2. Copyright (c) 2014, Matthias Schiffer <[email protected]>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /*
  23. tplink-safeloader
  24. Image generation tool for the TP-LINK SafeLoader as seen on
  25. TP-LINK Pharos devices (CPE210/220/510/520)
  26. */
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <stdbool.h>
  30. #include <stdio.h>
  31. #include <stdint.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <time.h>
  35. #include <unistd.h>
  36. #include <arpa/inet.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include "md5.h"
  40. #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
  41. #define MAX_PARTITIONS 32
  42. /** An image partition table entry */
  43. struct image_partition_entry {
  44. const char *name;
  45. size_t size;
  46. uint8_t *data;
  47. };
  48. /** A flash partition table entry */
  49. struct flash_partition_entry {
  50. const char *name;
  51. uint32_t base;
  52. uint32_t size;
  53. };
  54. /** Firmware layout description */
  55. struct device_info {
  56. const char *id;
  57. const char *vendor;
  58. const char *support_list;
  59. char support_trail;
  60. const char *soft_ver;
  61. const struct flash_partition_entry partitions[MAX_PARTITIONS+1];
  62. const char *first_sysupgrade_partition;
  63. const char *last_sysupgrade_partition;
  64. };
  65. /** The content of the soft-version structure */
  66. struct __attribute__((__packed__)) soft_version {
  67. uint32_t magic;
  68. uint32_t zero;
  69. uint8_t pad1;
  70. uint8_t version_major;
  71. uint8_t version_minor;
  72. uint8_t version_patch;
  73. uint8_t year_hi;
  74. uint8_t year_lo;
  75. uint8_t month;
  76. uint8_t day;
  77. uint32_t rev;
  78. uint8_t pad2;
  79. };
  80. static const uint8_t jffs2_eof_mark[4] = {0xde, 0xad, 0xc0, 0xde};
  81. /**
  82. Salt for the MD5 hash
  83. Fortunately, TP-LINK seems to use the same salt for most devices which use
  84. the new image format.
  85. */
  86. static const uint8_t md5_salt[16] = {
  87. 0x7a, 0x2b, 0x15, 0xed,
  88. 0x9b, 0x98, 0x59, 0x6d,
  89. 0xe5, 0x04, 0xab, 0x44,
  90. 0xac, 0x2a, 0x9f, 0x4e,
  91. };
  92. /** Firmware layout table */
  93. static struct device_info boards[] = {
  94. /** Firmware layout for the CPE210/220 */
  95. {
  96. .id = "CPE210",
  97. .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
  98. .support_list =
  99. "SupportList:\r\n"
  100. "CPE210(TP-LINK|UN|N300-2):1.0\r\n"
  101. "CPE210(TP-LINK|UN|N300-2):1.1\r\n"
  102. "CPE210(TP-LINK|US|N300-2):1.1\r\n"
  103. "CPE210(TP-LINK|EU|N300-2):1.1\r\n"
  104. "CPE220(TP-LINK|UN|N300-2):1.1\r\n"
  105. "CPE220(TP-LINK|US|N300-2):1.1\r\n"
  106. "CPE220(TP-LINK|EU|N300-2):1.1\r\n",
  107. .support_trail = '\xff',
  108. .soft_ver = NULL,
  109. .partitions = {
  110. {"fs-uboot", 0x00000, 0x20000},
  111. {"partition-table", 0x20000, 0x02000},
  112. {"default-mac", 0x30000, 0x00020},
  113. {"product-info", 0x31100, 0x00100},
  114. {"signature", 0x32000, 0x00400},
  115. {"os-image", 0x40000, 0x170000},
  116. {"soft-version", 0x1b0000, 0x00100},
  117. {"support-list", 0x1b1000, 0x00400},
  118. {"file-system", 0x1c0000, 0x600000},
  119. {"user-config", 0x7c0000, 0x10000},
  120. {"default-config", 0x7d0000, 0x10000},
  121. {"log", 0x7e0000, 0x10000},
  122. {"radio", 0x7f0000, 0x10000},
  123. {NULL, 0, 0}
  124. },
  125. .first_sysupgrade_partition = "os-image",
  126. .last_sysupgrade_partition = "file-system",
  127. },
  128. /** Firmware layout for the CPE510/520 */
  129. {
  130. .id = "CPE510",
  131. .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
  132. .support_list =
  133. "SupportList:\r\n"
  134. "CPE510(TP-LINK|UN|N300-5):1.0\r\n"
  135. "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
  136. "CPE510(TP-LINK|UN|N300-5):1.1\r\n"
  137. "CPE510(TP-LINK|US|N300-5):1.1\r\n"
  138. "CPE510(TP-LINK|EU|N300-5):1.1\r\n"
  139. "CPE520(TP-LINK|UN|N300-5):1.1\r\n"
  140. "CPE520(TP-LINK|US|N300-5):1.1\r\n"
  141. "CPE520(TP-LINK|EU|N300-5):1.1\r\n",
  142. .support_trail = '\xff',
  143. .soft_ver = NULL,
  144. .partitions = {
  145. {"fs-uboot", 0x00000, 0x20000},
  146. {"partition-table", 0x20000, 0x02000},
  147. {"default-mac", 0x30000, 0x00020},
  148. {"product-info", 0x31100, 0x00100},
  149. {"signature", 0x32000, 0x00400},
  150. {"os-image", 0x40000, 0x170000},
  151. {"soft-version", 0x1b0000, 0x00100},
  152. {"support-list", 0x1b1000, 0x00400},
  153. {"file-system", 0x1c0000, 0x600000},
  154. {"user-config", 0x7c0000, 0x10000},
  155. {"default-config", 0x7d0000, 0x10000},
  156. {"log", 0x7e0000, 0x10000},
  157. {"radio", 0x7f0000, 0x10000},
  158. {NULL, 0, 0}
  159. },
  160. .first_sysupgrade_partition = "os-image",
  161. .last_sysupgrade_partition = "file-system",
  162. },
  163. {
  164. .id = "WBS210",
  165. .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
  166. .support_list =
  167. "SupportList:\r\n"
  168. "WBS210(TP-LINK|UN|N300-2):1.20\r\n"
  169. "WBS210(TP-LINK|US|N300-2):1.20\r\n"
  170. "WBS210(TP-LINK|EU|N300-2):1.20\r\n",
  171. .support_trail = '\xff',
  172. .soft_ver = NULL,
  173. .partitions = {
  174. {"fs-uboot", 0x00000, 0x20000},
  175. {"partition-table", 0x20000, 0x02000},
  176. {"default-mac", 0x30000, 0x00020},
  177. {"product-info", 0x31100, 0x00100},
  178. {"signature", 0x32000, 0x00400},
  179. {"os-image", 0x40000, 0x170000},
  180. {"soft-version", 0x1b0000, 0x00100},
  181. {"support-list", 0x1b1000, 0x00400},
  182. {"file-system", 0x1c0000, 0x600000},
  183. {"user-config", 0x7c0000, 0x10000},
  184. {"default-config", 0x7d0000, 0x10000},
  185. {"log", 0x7e0000, 0x10000},
  186. {"radio", 0x7f0000, 0x10000},
  187. {NULL, 0, 0}
  188. },
  189. .first_sysupgrade_partition = "os-image",
  190. .last_sysupgrade_partition = "file-system",
  191. },
  192. {
  193. .id = "WBS510",
  194. .vendor = "CPE510(TP-LINK|UN|N300-5):1.0\r\n",
  195. .support_list =
  196. "SupportList:\r\n"
  197. "WBS510(TP-LINK|UN|N300-5):1.20\r\n"
  198. "WBS510(TP-LINK|US|N300-5):1.20\r\n"
  199. "WBS510(TP-LINK|EU|N300-5):1.20\r\n",
  200. .support_trail = '\xff',
  201. .soft_ver = NULL,
  202. .partitions = {
  203. {"fs-uboot", 0x00000, 0x20000},
  204. {"partition-table", 0x20000, 0x02000},
  205. {"default-mac", 0x30000, 0x00020},
  206. {"product-info", 0x31100, 0x00100},
  207. {"signature", 0x32000, 0x00400},
  208. {"os-image", 0x40000, 0x170000},
  209. {"soft-version", 0x1b0000, 0x00100},
  210. {"support-list", 0x1b1000, 0x00400},
  211. {"file-system", 0x1c0000, 0x600000},
  212. {"user-config", 0x7c0000, 0x10000},
  213. {"default-config", 0x7d0000, 0x10000},
  214. {"log", 0x7e0000, 0x10000},
  215. {"radio", 0x7f0000, 0x10000},
  216. {NULL, 0, 0}
  217. },
  218. .first_sysupgrade_partition = "os-image",
  219. .last_sysupgrade_partition = "file-system",
  220. },
  221. /** Firmware layout for the C2600 */
  222. {
  223. .id = "C2600",
  224. .vendor = "",
  225. .support_list =
  226. "SupportList:\r\n"
  227. "{product_name:Archer C2600,product_ver:1.0.0,special_id:00000000}\r\n",
  228. .support_trail = '\x00',
  229. .soft_ver = NULL,
  230. .partitions = {
  231. {"SBL1", 0x00000, 0x20000},
  232. {"MIBIB", 0x20000, 0x20000},
  233. {"SBL2", 0x40000, 0x20000},
  234. {"SBL3", 0x60000, 0x30000},
  235. {"DDRCONFIG", 0x90000, 0x10000},
  236. {"SSD", 0xa0000, 0x10000},
  237. {"TZ", 0xb0000, 0x30000},
  238. {"RPM", 0xe0000, 0x20000},
  239. {"fs-uboot", 0x100000, 0x70000},
  240. {"uboot-env", 0x170000, 0x40000},
  241. {"radio", 0x1b0000, 0x40000},
  242. {"os-image", 0x1f0000, 0x200000},
  243. {"file-system", 0x3f0000, 0x1b00000},
  244. {"default-mac", 0x1ef0000, 0x00200},
  245. {"pin", 0x1ef0200, 0x00200},
  246. {"product-info", 0x1ef0400, 0x0fc00},
  247. {"partition-table", 0x1f00000, 0x10000},
  248. {"soft-version", 0x1f10000, 0x10000},
  249. {"support-list", 0x1f20000, 0x10000},
  250. {"profile", 0x1f30000, 0x10000},
  251. {"default-config", 0x1f40000, 0x10000},
  252. {"user-config", 0x1f50000, 0x40000},
  253. {"qos-db", 0x1f90000, 0x40000},
  254. {"usb-config", 0x1fd0000, 0x10000},
  255. {"log", 0x1fe0000, 0x20000},
  256. {NULL, 0, 0}
  257. },
  258. .first_sysupgrade_partition = "os-image",
  259. .last_sysupgrade_partition = "file-system"
  260. },
  261. /** Firmware layout for the C25v1 */
  262. {
  263. .id = "ARCHER-C25-V1",
  264. .support_list =
  265. "SupportList:\n"
  266. "{product_name:ArcherC25,product_ver:1.0.0,special_id:00000000}\n"
  267. "{product_name:ArcherC25,product_ver:1.0.0,special_id:55530000}\n"
  268. "{product_name:ArcherC25,product_ver:1.0.0,special_id:45550000}\n",
  269. .support_trail = '\x00',
  270. .soft_ver = "soft_ver:1.0.0\n",
  271. /**
  272. We use a bigger os-image partition than the stock images (and thus
  273. smaller file-system), as our kernel doesn't fit in the stock firmware's
  274. 1MB os-image.
  275. */
  276. .partitions = {
  277. {"factory-boot", 0x00000, 0x20000},
  278. {"fs-uboot", 0x20000, 0x10000},
  279. {"os-image", 0x30000, 0x180000}, /* Stock: base 0x30000 size 0x100000 */
  280. {"file-system", 0x1b0000, 0x620000}, /* Stock: base 0x130000 size 0x6a0000 */
  281. {"user-config", 0x7d0000, 0x04000},
  282. {"default-mac", 0x7e0000, 0x00100},
  283. {"device-id", 0x7e0100, 0x00100},
  284. {"extra-para", 0x7e0200, 0x00100},
  285. {"pin", 0x7e0300, 0x00100},
  286. {"support-list", 0x7e0400, 0x00400},
  287. {"soft-version", 0x7e0800, 0x00400},
  288. {"product-info", 0x7e0c00, 0x01400},
  289. {"partition-table", 0x7e2000, 0x01000},
  290. {"profile", 0x7e3000, 0x01000},
  291. {"default-config", 0x7e4000, 0x04000},
  292. {"merge-config", 0x7ec000, 0x02000},
  293. {"qos-db", 0x7ee000, 0x02000},
  294. {"radio", 0x7f0000, 0x10000},
  295. {NULL, 0, 0}
  296. },
  297. .first_sysupgrade_partition = "os-image",
  298. .last_sysupgrade_partition = "file-system",
  299. },
  300. /** Firmware layout for the C59v1 */
  301. {
  302. .id = "ARCHER-C59-V1",
  303. .vendor = "",
  304. .support_list =
  305. "SupportList:\r\n"
  306. "{product_name:Archer C59,product_ver:1.0.0,special_id:00000000}\r\n"
  307. "{product_name:Archer C59,product_ver:1.0.0,special_id:45550000}\r\n"
  308. "{product_name:Archer C59,product_ver:1.0.0,special_id:55530000}\r\n",
  309. .support_trail = '\x00',
  310. .soft_ver = "soft_ver:1.0.0\n",
  311. .partitions = {
  312. {"fs-uboot", 0x00000, 0x10000},
  313. {"default-mac", 0x10000, 0x00200},
  314. {"pin", 0x10200, 0x00200},
  315. {"device-id", 0x10400, 0x00100},
  316. {"product-info", 0x10500, 0x0fb00},
  317. {"os-image", 0x20000, 0x180000},
  318. {"file-system", 0x1a0000, 0xcb0000},
  319. {"partition-table", 0xe50000, 0x10000},
  320. {"soft-version", 0xe60000, 0x10000},
  321. {"support-list", 0xe70000, 0x10000},
  322. {"profile", 0xe80000, 0x10000},
  323. {"default-config", 0xe90000, 0x10000},
  324. {"user-config", 0xea0000, 0x40000},
  325. {"usb-config", 0xee0000, 0x10000},
  326. {"certificate", 0xef0000, 0x10000},
  327. {"qos-db", 0xf00000, 0x40000},
  328. {"log", 0xfe0000, 0x10000},
  329. {"radio", 0xff0000, 0x10000},
  330. {NULL, 0, 0}
  331. },
  332. .first_sysupgrade_partition = "os-image",
  333. .last_sysupgrade_partition = "file-system",
  334. },
  335. /** Firmware layout for the C60v1 */
  336. {
  337. .id = "ARCHER-C60-V1",
  338. .vendor = "",
  339. .support_list =
  340. "SupportList:\r\n"
  341. "{product_name:Archer C60,product_ver:1.0.0,special_id:00000000}\r\n"
  342. "{product_name:Archer C60,product_ver:1.0.0,special_id:45550000}\r\n"
  343. "{product_name:Archer C60,product_ver:1.0.0,special_id:55530000}\r\n",
  344. .support_trail = '\x00',
  345. .soft_ver = "soft_ver:1.0.0\n",
  346. .partitions = {
  347. {"fs-uboot", 0x00000, 0x10000},
  348. {"default-mac", 0x10000, 0x00200},
  349. {"pin", 0x10200, 0x00200},
  350. {"product-info", 0x10400, 0x00100},
  351. {"partition-table", 0x10500, 0x00800},
  352. {"soft-version", 0x11300, 0x00200},
  353. {"support-list", 0x11500, 0x00100},
  354. {"device-id", 0x11600, 0x00100},
  355. {"profile", 0x11700, 0x03900},
  356. {"default-config", 0x15000, 0x04000},
  357. {"user-config", 0x19000, 0x04000},
  358. {"os-image", 0x20000, 0x150000},
  359. {"file-system", 0x170000, 0x678000},
  360. {"certyficate", 0x7e8000, 0x08000},
  361. {"radio", 0x7f0000, 0x10000},
  362. {NULL, 0, 0}
  363. },
  364. .first_sysupgrade_partition = "os-image",
  365. .last_sysupgrade_partition = "file-system",
  366. },
  367. /** Firmware layout for the C5 */
  368. {
  369. .id = "ARCHER-C5-V2",
  370. .vendor = "",
  371. .support_list =
  372. "SupportList:\r\n"
  373. "{product_name:ArcherC5,"
  374. "product_ver:2.0.0,"
  375. "special_id:00000000}\r\n",
  376. .support_trail = '\x00',
  377. .soft_ver = NULL,
  378. .partitions = {
  379. {"fs-uboot", 0x00000, 0x40000},
  380. {"os-image", 0x40000, 0x200000},
  381. {"file-system", 0x240000, 0xc00000},
  382. {"default-mac", 0xe40000, 0x00200},
  383. {"pin", 0xe40200, 0x00200},
  384. {"product-info", 0xe40400, 0x00200},
  385. {"partition-table", 0xe50000, 0x10000},
  386. {"soft-version", 0xe60000, 0x00200},
  387. {"support-list", 0xe61000, 0x0f000},
  388. {"profile", 0xe70000, 0x10000},
  389. {"default-config", 0xe80000, 0x10000},
  390. {"user-config", 0xe90000, 0x50000},
  391. {"log", 0xee0000, 0x100000},
  392. {"radio_bk", 0xfe0000, 0x10000},
  393. {"radio", 0xff0000, 0x10000},
  394. {NULL, 0, 0}
  395. },
  396. .first_sysupgrade_partition = "os-image",
  397. .last_sysupgrade_partition = "file-system"
  398. },
  399. /** Firmware layout for the C9 */
  400. {
  401. .id = "ARCHERC9",
  402. .vendor = "",
  403. .support_list =
  404. "SupportList:\n"
  405. "{product_name:ArcherC9,"
  406. "product_ver:1.0.0,"
  407. "special_id:00000000}\n",
  408. .support_trail = '\x00',
  409. .soft_ver = NULL,
  410. .partitions = {
  411. {"fs-uboot", 0x00000, 0x40000},
  412. {"os-image", 0x40000, 0x200000},
  413. {"file-system", 0x240000, 0xc00000},
  414. {"default-mac", 0xe40000, 0x00200},
  415. {"pin", 0xe40200, 0x00200},
  416. {"product-info", 0xe40400, 0x00200},
  417. {"partition-table", 0xe50000, 0x10000},
  418. {"soft-version", 0xe60000, 0x00200},
  419. {"support-list", 0xe61000, 0x0f000},
  420. {"profile", 0xe70000, 0x10000},
  421. {"default-config", 0xe80000, 0x10000},
  422. {"user-config", 0xe90000, 0x50000},
  423. {"log", 0xee0000, 0x100000},
  424. {"radio_bk", 0xfe0000, 0x10000},
  425. {"radio", 0xff0000, 0x10000},
  426. {NULL, 0, 0}
  427. },
  428. .first_sysupgrade_partition = "os-image",
  429. .last_sysupgrade_partition = "file-system"
  430. },
  431. /** Firmware layout for the EAP120 */
  432. {
  433. .id = "EAP120",
  434. .vendor = "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
  435. .support_list =
  436. "SupportList:\r\n"
  437. "EAP120(TP-LINK|UN|N300-2):1.0\r\n",
  438. .support_trail = '\xff',
  439. .soft_ver = NULL,
  440. .partitions = {
  441. {"fs-uboot", 0x00000, 0x20000},
  442. {"partition-table", 0x20000, 0x02000},
  443. {"default-mac", 0x30000, 0x00020},
  444. {"support-list", 0x31000, 0x00100},
  445. {"product-info", 0x31100, 0x00100},
  446. {"soft-version", 0x32000, 0x00100},
  447. {"os-image", 0x40000, 0x180000},
  448. {"file-system", 0x1c0000, 0x600000},
  449. {"user-config", 0x7c0000, 0x10000},
  450. {"backup-config", 0x7d0000, 0x10000},
  451. {"log", 0x7e0000, 0x10000},
  452. {"radio", 0x7f0000, 0x10000},
  453. {NULL, 0, 0}
  454. },
  455. .first_sysupgrade_partition = "os-image",
  456. .last_sysupgrade_partition = "file-system"
  457. },
  458. /** Firmware layout for the TL-WA850RE v2 */
  459. {
  460. .id = "TLWA850REV2",
  461. .vendor = "",
  462. .support_list =
  463. "SupportList:\n"
  464. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55530000}\n"
  465. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:00000000}\n"
  466. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:55534100}\n"
  467. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:45550000}\n"
  468. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4B520000}\n"
  469. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:42520000}\n"
  470. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:4A500000}\n"
  471. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:43410000}\n"
  472. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:41550000}\n"
  473. "{product_name:TL-WA850RE,product_ver:2.0.0,special_id:52550000}\n",
  474. .support_trail = '\x00',
  475. .soft_ver = NULL,
  476. /**
  477. 576KB were moved from file-system to os-image
  478. in comparison to the stock image
  479. */
  480. .partitions = {
  481. {"fs-uboot", 0x00000, 0x20000},
  482. {"os-image", 0x20000, 0x150000},
  483. {"file-system", 0x170000, 0x240000},
  484. {"partition-table", 0x3b0000, 0x02000},
  485. {"default-mac", 0x3c0000, 0x00020},
  486. {"pin", 0x3c0100, 0x00020},
  487. {"product-info", 0x3c1000, 0x01000},
  488. {"soft-version", 0x3c2000, 0x00100},
  489. {"support-list", 0x3c3000, 0x01000},
  490. {"profile", 0x3c4000, 0x08000},
  491. {"user-config", 0x3d0000, 0x10000},
  492. {"default-config", 0x3e0000, 0x10000},
  493. {"radio", 0x3f0000, 0x10000},
  494. {NULL, 0, 0}
  495. },
  496. .first_sysupgrade_partition = "os-image",
  497. .last_sysupgrade_partition = "file-system"
  498. },
  499. /** Firmware layout for the TL-WA855RE v1 */
  500. {
  501. .id = "TLWA855REV1",
  502. .vendor = "",
  503. .support_list =
  504. "SupportList:\n"
  505. "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:00000000}\n"
  506. "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:55530000}\n"
  507. "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:45550000}\n"
  508. "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4B520000}\n"
  509. "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:42520000}\n"
  510. "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:4A500000}\n"
  511. "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:43410000}\n"
  512. "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:41550000}\n"
  513. "{product_name:TL-WA855RE,product_ver:1.0.0,special_id:52550000}\n",
  514. .support_trail = '\x00',
  515. .soft_ver = NULL,
  516. .partitions = {
  517. {"fs-uboot", 0x00000, 0x20000},
  518. {"os-image", 0x20000, 0x150000},
  519. {"file-system", 0x170000, 0x240000},
  520. {"partition-table", 0x3b0000, 0x02000},
  521. {"default-mac", 0x3c0000, 0x00020},
  522. {"pin", 0x3c0100, 0x00020},
  523. {"product-info", 0x3c1000, 0x01000},
  524. {"soft-version", 0x3c2000, 0x00100},
  525. {"support-list", 0x3c3000, 0x01000},
  526. {"profile", 0x3c4000, 0x08000},
  527. {"user-config", 0x3d0000, 0x10000},
  528. {"default-config", 0x3e0000, 0x10000},
  529. {"radio", 0x3f0000, 0x10000},
  530. {NULL, 0, 0}
  531. },
  532. .first_sysupgrade_partition = "os-image",
  533. .last_sysupgrade_partition = "file-system"
  534. },
  535. /** Firmware layout for the TL-WR1043 v4 */
  536. {
  537. .id = "TLWR1043NDV4",
  538. .vendor = "",
  539. .support_list =
  540. "SupportList:\n"
  541. "{product_name:TL-WR1043ND,product_ver:4.0.0,special_id:45550000}\n",
  542. .support_trail = '\x00',
  543. .soft_ver = NULL,
  544. /**
  545. We use a bigger os-image partition than the stock images (and thus
  546. smaller file-system), as our kernel doesn't fit in the stock firmware's
  547. 1MB os-image.
  548. */
  549. .partitions = {
  550. {"fs-uboot", 0x00000, 0x20000},
  551. {"os-image", 0x20000, 0x180000},
  552. {"file-system", 0x1a0000, 0xdb0000},
  553. {"default-mac", 0xf50000, 0x00200},
  554. {"pin", 0xf50200, 0x00200},
  555. {"product-info", 0xf50400, 0x0fc00},
  556. {"soft-version", 0xf60000, 0x0b000},
  557. {"support-list", 0xf6b000, 0x04000},
  558. {"profile", 0xf70000, 0x04000},
  559. {"default-config", 0xf74000, 0x0b000},
  560. {"user-config", 0xf80000, 0x40000},
  561. {"partition-table", 0xfc0000, 0x10000},
  562. {"log", 0xfd0000, 0x20000},
  563. {"radio", 0xff0000, 0x10000},
  564. {NULL, 0, 0}
  565. },
  566. .first_sysupgrade_partition = "os-image",
  567. .last_sysupgrade_partition = "file-system"
  568. },
  569. /** Firmware layout for the TL-WR942N V1 */
  570. {
  571. .id = "TLWR942NV1",
  572. .vendor = "",
  573. .support_list =
  574. "SupportList:\r\n"
  575. "{product_name:TL-WR942N,product_ver:1.0.0,special_id:00000000}\r\n"
  576. "{product_name:TL-WR942N,product_ver:1.0.0,special_id:52550000}\r\n",
  577. .support_trail = '\x00',
  578. .soft_ver = NULL,
  579. .partitions = {
  580. {"fs-uboot", 0x00000, 0x20000},
  581. {"os-image", 0x20000, 0x150000},
  582. {"file-system", 0x170000, 0xcd0000},
  583. {"default-mac", 0xe40000, 0x00200},
  584. {"pin", 0xe40200, 0x00200},
  585. {"product-info", 0xe40400, 0x0fc00},
  586. {"partition-table", 0xe50000, 0x10000},
  587. {"soft-version", 0xe60000, 0x10000},
  588. {"support-list", 0xe70000, 0x10000},
  589. {"profile", 0xe80000, 0x10000},
  590. {"default-config", 0xe90000, 0x10000},
  591. {"user-config", 0xea0000, 0x40000},
  592. {"qos-db", 0xee0000, 0x40000},
  593. {"certificate", 0xf20000, 0x10000},
  594. {"usb-config", 0xfb0000, 0x10000},
  595. {"log", 0xfc0000, 0x20000},
  596. {"radio-bk", 0xfe0000, 0x10000},
  597. {"radio", 0xff0000, 0x10000},
  598. {NULL, 0, 0}
  599. },
  600. .first_sysupgrade_partition = "os-image",
  601. .last_sysupgrade_partition = "file-system",
  602. },
  603. /** Firmware layout for the RE450 */
  604. {
  605. .id = "RE450",
  606. .vendor = "",
  607. .support_list =
  608. "SupportList:\r\n"
  609. "{product_name:RE450,product_ver:1.0.0,special_id:00000000}\r\n"
  610. "{product_name:RE450,product_ver:1.0.0,special_id:55530000}\r\n"
  611. "{product_name:RE450,product_ver:1.0.0,special_id:45550000}\r\n"
  612. "{product_name:RE450,product_ver:1.0.0,special_id:4A500000}\r\n"
  613. "{product_name:RE450,product_ver:1.0.0,special_id:43410000}\r\n"
  614. "{product_name:RE450,product_ver:1.0.0,special_id:41550000}\r\n"
  615. "{product_name:RE450,product_ver:1.0.0,special_id:4B520000}\r\n"
  616. "{product_name:RE450,product_ver:1.0.0,special_id:55534100}\r\n",
  617. .support_trail = '\x00',
  618. .soft_ver = NULL,
  619. /**
  620. The flash partition table for RE450;
  621. it is almost the same as the one used by the stock images,
  622. 576KB were moved from file-system to os-image.
  623. */
  624. .partitions = {
  625. {"fs-uboot", 0x00000, 0x20000},
  626. {"os-image", 0x20000, 0x150000},
  627. {"file-system", 0x170000, 0x4a0000},
  628. {"partition-table", 0x600000, 0x02000},
  629. {"default-mac", 0x610000, 0x00020},
  630. {"pin", 0x610100, 0x00020},
  631. {"product-info", 0x611100, 0x01000},
  632. {"soft-version", 0x620000, 0x01000},
  633. {"support-list", 0x621000, 0x01000},
  634. {"profile", 0x622000, 0x08000},
  635. {"user-config", 0x630000, 0x10000},
  636. {"default-config", 0x640000, 0x10000},
  637. {"radio", 0x7f0000, 0x10000},
  638. {NULL, 0, 0}
  639. },
  640. .first_sysupgrade_partition = "os-image",
  641. .last_sysupgrade_partition = "file-system"
  642. },
  643. {}
  644. };
  645. #define error(_ret, _errno, _str, ...) \
  646. do { \
  647. fprintf(stderr, _str ": %s\n", ## __VA_ARGS__, \
  648. strerror(_errno)); \
  649. if (_ret) \
  650. exit(_ret); \
  651. } while (0)
  652. /** Stores a uint32 as big endian */
  653. static inline void put32(uint8_t *buf, uint32_t val) {
  654. buf[0] = val >> 24;
  655. buf[1] = val >> 16;
  656. buf[2] = val >> 8;
  657. buf[3] = val;
  658. }
  659. /** Allocates a new image partition */
  660. static struct image_partition_entry alloc_image_partition(const char *name, size_t len) {
  661. struct image_partition_entry entry = {name, len, malloc(len)};
  662. if (!entry.data)
  663. error(1, errno, "malloc");
  664. return entry;
  665. }
  666. /** Frees an image partition */
  667. static void free_image_partition(struct image_partition_entry entry) {
  668. free(entry.data);
  669. }
  670. static time_t source_date_epoch = -1;
  671. static void set_source_date_epoch() {
  672. char *env = getenv("SOURCE_DATE_EPOCH");
  673. char *endptr = env;
  674. errno = 0;
  675. if (env && *env) {
  676. source_date_epoch = strtoull(env, &endptr, 10);
  677. if (errno || (endptr && *endptr != '\0')) {
  678. fprintf(stderr, "Invalid SOURCE_DATE_EPOCH");
  679. exit(1);
  680. }
  681. }
  682. }
  683. /** Generates the partition-table partition */
  684. static struct image_partition_entry make_partition_table(const struct flash_partition_entry *p) {
  685. struct image_partition_entry entry = alloc_image_partition("partition-table", 0x800);
  686. char *s = (char *)entry.data, *end = (char *)(s+entry.size);
  687. *(s++) = 0x00;
  688. *(s++) = 0x04;
  689. *(s++) = 0x00;
  690. *(s++) = 0x00;
  691. size_t i;
  692. for (i = 0; p[i].name; i++) {
  693. size_t len = end-s;
  694. size_t w = snprintf(s, len, "partition %s base 0x%05x size 0x%05x\n", p[i].name, p[i].base, p[i].size);
  695. if (w > len-1)
  696. error(1, 0, "flash partition table overflow?");
  697. s += w;
  698. }
  699. s++;
  700. memset(s, 0xff, end-s);
  701. return entry;
  702. }
  703. /** Generates a binary-coded decimal representation of an integer in the range [0, 99] */
  704. static inline uint8_t bcd(uint8_t v) {
  705. return 0x10 * (v/10) + v%10;
  706. }
  707. /** Generates the soft-version partition */
  708. static struct image_partition_entry make_soft_version(uint32_t rev) {
  709. struct image_partition_entry entry = alloc_image_partition("soft-version", sizeof(struct soft_version));
  710. struct soft_version *s = (struct soft_version *)entry.data;
  711. time_t t;
  712. if (source_date_epoch != -1)
  713. t = source_date_epoch;
  714. else if (time(&t) == (time_t)(-1))
  715. error(1, errno, "time");
  716. struct tm *tm = localtime(&t);
  717. s->magic = htonl(0x0000000c);
  718. s->zero = 0;
  719. s->pad1 = 0xff;
  720. s->version_major = 0;
  721. s->version_minor = 0;
  722. s->version_patch = 0;
  723. s->year_hi = bcd((1900+tm->tm_year)/100);
  724. s->year_lo = bcd(tm->tm_year%100);
  725. s->month = bcd(tm->tm_mon+1);
  726. s->day = bcd(tm->tm_mday);
  727. s->rev = htonl(rev);
  728. s->pad2 = 0xff;
  729. return entry;
  730. }
  731. static struct image_partition_entry make_soft_version_from_string(const char *soft_ver) {
  732. /** String length _including_ the terminating zero byte */
  733. uint32_t ver_len = strlen(soft_ver) + 1;
  734. /** Partition contains 64 bit header, the version string, and one additional null byte */
  735. size_t partition_len = 2*sizeof(uint32_t) + ver_len + 1;
  736. struct image_partition_entry entry = alloc_image_partition("soft-version", partition_len);
  737. uint32_t *len = (uint32_t *)entry.data;
  738. len[0] = htonl(ver_len);
  739. len[1] = 0;
  740. memcpy(&len[2], soft_ver, ver_len);
  741. entry.data[partition_len - 1] = 0;
  742. return entry;
  743. }
  744. /** Generates the support-list partition */
  745. static struct image_partition_entry make_support_list(const struct device_info *info) {
  746. size_t len = strlen(info->support_list);
  747. struct image_partition_entry entry = alloc_image_partition("support-list", len + 9);
  748. put32(entry.data, len);
  749. memset(entry.data+4, 0, 4);
  750. memcpy(entry.data+8, info->support_list, len);
  751. entry.data[len+8] = info->support_trail;
  752. return entry;
  753. }
  754. /** Creates a new image partition with an arbitrary name from a file */
  755. static struct image_partition_entry read_file(const char *part_name, const char *filename, bool add_jffs2_eof) {
  756. struct stat statbuf;
  757. if (stat(filename, &statbuf) < 0)
  758. error(1, errno, "unable to stat file `%s'", filename);
  759. size_t len = statbuf.st_size;
  760. if (add_jffs2_eof)
  761. len = ALIGN(len, 0x10000) + sizeof(jffs2_eof_mark);
  762. struct image_partition_entry entry = alloc_image_partition(part_name, len);
  763. FILE *file = fopen(filename, "rb");
  764. if (!file)
  765. error(1, errno, "unable to open file `%s'", filename);
  766. if (fread(entry.data, statbuf.st_size, 1, file) != 1)
  767. error(1, errno, "unable to read file `%s'", filename);
  768. if (add_jffs2_eof) {
  769. uint8_t *eof = entry.data + statbuf.st_size, *end = entry.data+entry.size;
  770. memset(eof, 0xff, end - eof - sizeof(jffs2_eof_mark));
  771. memcpy(end - sizeof(jffs2_eof_mark), jffs2_eof_mark, sizeof(jffs2_eof_mark));
  772. }
  773. fclose(file);
  774. return entry;
  775. }
  776. /** Creates a new image partition from arbitrary data */
  777. static struct image_partition_entry put_data(const char *part_name, const char *datain, size_t len) {
  778. struct image_partition_entry entry = alloc_image_partition(part_name, len);
  779. memcpy(entry.data, datain, len);
  780. return entry;
  781. }
  782. /**
  783. Copies a list of image partitions into an image buffer and generates the image partition table while doing so
  784. Example image partition table:
  785. fwup-ptn partition-table base 0x00800 size 0x00800
  786. fwup-ptn os-image base 0x01000 size 0x113b45
  787. fwup-ptn file-system base 0x114b45 size 0x1d0004
  788. fwup-ptn support-list base 0x2e4b49 size 0x000d1
  789. Each line of the partition table is terminated with the bytes 09 0d 0a ("\t\r\n"),
  790. the end of the partition table is marked with a zero byte.
  791. The firmware image must contain at least the partition-table and support-list partitions
  792. to be accepted. There aren't any alignment constraints for the image partitions.
  793. The partition-table partition contains the actual flash layout; partitions
  794. from the image partition table are mapped to the corresponding flash partitions during
  795. the firmware upgrade. The support-list partition contains a list of devices supported by
  796. the firmware image.
  797. The base offsets in the firmware partition table are relative to the end
  798. of the vendor information block, so the partition-table partition will
  799. actually start at offset 0x1814 of the image.
  800. I think partition-table must be the first partition in the firmware image.
  801. */
  802. static void put_partitions(uint8_t *buffer, const struct flash_partition_entry *flash_parts, const struct image_partition_entry *parts) {
  803. size_t i, j;
  804. char *image_pt = (char *)buffer, *end = image_pt + 0x800;
  805. size_t base = 0x800;
  806. for (i = 0; parts[i].name; i++) {
  807. for (j = 0; flash_parts[j].name; j++) {
  808. if (!strcmp(flash_parts[j].name, parts[i].name)) {
  809. if (parts[i].size > flash_parts[j].size)
  810. error(1, 0, "%s partition too big (more than %u bytes)", flash_parts[j].name, (unsigned)flash_parts[j].size);
  811. break;
  812. }
  813. }
  814. assert(flash_parts[j].name);
  815. memcpy(buffer + base, parts[i].data, parts[i].size);
  816. size_t len = end-image_pt;
  817. 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);
  818. if (w > len-1)
  819. error(1, 0, "image partition table overflow?");
  820. image_pt += w;
  821. base += parts[i].size;
  822. }
  823. }
  824. /** Generates and writes the image MD5 checksum */
  825. static void put_md5(uint8_t *md5, uint8_t *buffer, unsigned int len) {
  826. MD5_CTX ctx;
  827. MD5_Init(&ctx);
  828. MD5_Update(&ctx, md5_salt, (unsigned int)sizeof(md5_salt));
  829. MD5_Update(&ctx, buffer, len);
  830. MD5_Final(md5, &ctx);
  831. }
  832. /**
  833. Generates the firmware image in factory format
  834. Image format:
  835. Bytes (hex) Usage
  836. ----------- -----
  837. 0000-0003 Image size (4 bytes, big endian)
  838. 0004-0013 MD5 hash (hash of a 16 byte salt and the image data starting with byte 0x14)
  839. 0014-0017 Vendor information length (without padding) (4 bytes, big endian)
  840. 0018-1013 Vendor information (4092 bytes, padded with 0xff; there seem to be older
  841. (VxWorks-based) TP-LINK devices which use a smaller vendor information block)
  842. 1014-1813 Image partition table (2048 bytes, padded with 0xff)
  843. 1814-xxxx Firmware partitions
  844. */
  845. static void * generate_factory_image(const struct device_info *info, const struct image_partition_entry *parts, size_t *len) {
  846. *len = 0x1814;
  847. size_t i;
  848. for (i = 0; parts[i].name; i++)
  849. *len += parts[i].size;
  850. uint8_t *image = malloc(*len);
  851. if (!image)
  852. error(1, errno, "malloc");
  853. memset(image, 0xff, *len);
  854. put32(image, *len);
  855. if (info->vendor) {
  856. size_t vendor_len = strlen(info->vendor);
  857. put32(image+0x14, vendor_len);
  858. memcpy(image+0x18, info->vendor, vendor_len);
  859. }
  860. put_partitions(image + 0x1014, info->partitions, parts);
  861. put_md5(image+0x04, image+0x14, *len-0x14);
  862. return image;
  863. }
  864. /**
  865. Generates the firmware image in sysupgrade format
  866. This makes some assumptions about the provided flash and image partition tables and
  867. should be generalized when TP-LINK starts building its safeloader into hardware with
  868. different flash layouts.
  869. */
  870. static void * generate_sysupgrade_image(const struct device_info *info, const struct image_partition_entry *image_parts, size_t *len) {
  871. size_t i, j;
  872. size_t flash_first_partition_index = 0;
  873. size_t flash_last_partition_index = 0;
  874. const struct flash_partition_entry *flash_first_partition = NULL;
  875. const struct flash_partition_entry *flash_last_partition = NULL;
  876. const struct image_partition_entry *image_last_partition = NULL;
  877. /** Find first and last partitions */
  878. for (i = 0; info->partitions[i].name; i++) {
  879. if (!strcmp(info->partitions[i].name, info->first_sysupgrade_partition)) {
  880. flash_first_partition = &info->partitions[i];
  881. flash_first_partition_index = i;
  882. } else if (!strcmp(info->partitions[i].name, info->last_sysupgrade_partition)) {
  883. flash_last_partition = &info->partitions[i];
  884. flash_last_partition_index = i;
  885. }
  886. }
  887. assert(flash_first_partition && flash_last_partition);
  888. assert(flash_first_partition_index < flash_last_partition_index);
  889. /** Find last partition from image to calculate needed size */
  890. for (i = 0; image_parts[i].name; i++) {
  891. if (!strcmp(image_parts[i].name, info->last_sysupgrade_partition)) {
  892. image_last_partition = &image_parts[i];
  893. break;
  894. }
  895. }
  896. assert(image_last_partition);
  897. *len = flash_last_partition->base - flash_first_partition->base + image_last_partition->size;
  898. uint8_t *image = malloc(*len);
  899. if (!image)
  900. error(1, errno, "malloc");
  901. memset(image, 0xff, *len);
  902. for (i = flash_first_partition_index; i <= flash_last_partition_index; i++) {
  903. for (j = 0; image_parts[j].name; j++) {
  904. if (!strcmp(info->partitions[i].name, image_parts[j].name)) {
  905. if (image_parts[j].size > info->partitions[i].size)
  906. error(1, 0, "%s partition too big (more than %u bytes)", info->partitions[i].name, (unsigned)info->partitions[i].size);
  907. memcpy(image + info->partitions[i].base - flash_first_partition->base, image_parts[j].data, image_parts[j].size);
  908. break;
  909. }
  910. assert(image_parts[j].name);
  911. }
  912. }
  913. return image;
  914. }
  915. /** Generates an image according to a given layout and writes it to a file */
  916. static void build_image(const char *output,
  917. const char *kernel_image,
  918. const char *rootfs_image,
  919. uint32_t rev,
  920. bool add_jffs2_eof,
  921. bool sysupgrade,
  922. const struct device_info *info) {
  923. struct image_partition_entry parts[7] = {};
  924. parts[0] = make_partition_table(info->partitions);
  925. if (info->soft_ver)
  926. parts[1] = make_soft_version_from_string(info->soft_ver);
  927. else
  928. parts[1] = make_soft_version(rev);
  929. parts[2] = make_support_list(info);
  930. parts[3] = read_file("os-image", kernel_image, false);
  931. parts[4] = read_file("file-system", rootfs_image, add_jffs2_eof);
  932. if (strcasecmp(info->id, "ARCHER-C25-V1") == 0) {
  933. const char mdat[11] = {0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
  934. parts[5] = put_data("extra-para", mdat, 11);
  935. }
  936. size_t len;
  937. void *image;
  938. if (sysupgrade)
  939. image = generate_sysupgrade_image(info, parts, &len);
  940. else
  941. image = generate_factory_image(info, parts, &len);
  942. FILE *file = fopen(output, "wb");
  943. if (!file)
  944. error(1, errno, "unable to open output file");
  945. if (fwrite(image, len, 1, file) != 1)
  946. error(1, 0, "unable to write output file");
  947. fclose(file);
  948. free(image);
  949. size_t i;
  950. for (i = 0; parts[i].name; i++)
  951. free_image_partition(parts[i]);
  952. }
  953. /** Usage output */
  954. static void usage(const char *argv0) {
  955. fprintf(stderr,
  956. "Usage: %s [OPTIONS...]\n"
  957. "\n"
  958. "Options:\n"
  959. " -B <board> create image for the board specified with <board>\n"
  960. " -k <file> read kernel image from the file <file>\n"
  961. " -r <file> read rootfs image from the file <file>\n"
  962. " -o <file> write output to the file <file>\n"
  963. " -V <rev> sets the revision number to <rev>\n"
  964. " -j add jffs2 end-of-filesystem markers\n"
  965. " -S create sysupgrade instead of factory image\n"
  966. " -h show this help\n",
  967. argv0
  968. );
  969. };
  970. static const struct device_info *find_board(const char *id)
  971. {
  972. struct device_info *board = NULL;
  973. for (board = boards; board->id != NULL; board++)
  974. if (strcasecmp(id, board->id) == 0)
  975. return board;
  976. return NULL;
  977. }
  978. int main(int argc, char *argv[]) {
  979. const char *board = NULL, *kernel_image = NULL, *rootfs_image = NULL, *output = NULL;
  980. bool add_jffs2_eof = false, sysupgrade = false;
  981. unsigned rev = 0;
  982. const struct device_info *info;
  983. set_source_date_epoch();
  984. while (true) {
  985. int c;
  986. c = getopt(argc, argv, "B:k:r:o:V:jSh");
  987. if (c == -1)
  988. break;
  989. switch (c) {
  990. case 'B':
  991. board = optarg;
  992. break;
  993. case 'k':
  994. kernel_image = optarg;
  995. break;
  996. case 'r':
  997. rootfs_image = optarg;
  998. break;
  999. case 'o':
  1000. output = optarg;
  1001. break;
  1002. case 'V':
  1003. sscanf(optarg, "r%u", &rev);
  1004. break;
  1005. case 'j':
  1006. add_jffs2_eof = true;
  1007. break;
  1008. case 'S':
  1009. sysupgrade = true;
  1010. break;
  1011. case 'h':
  1012. usage(argv[0]);
  1013. return 0;
  1014. default:
  1015. usage(argv[0]);
  1016. return 1;
  1017. }
  1018. }
  1019. if (!board)
  1020. error(1, 0, "no board has been specified");
  1021. if (!kernel_image)
  1022. error(1, 0, "no kernel image has been specified");
  1023. if (!rootfs_image)
  1024. error(1, 0, "no rootfs image has been specified");
  1025. if (!output)
  1026. error(1, 0, "no output filename has been specified");
  1027. info = find_board(board);
  1028. if (info == NULL)
  1029. error(1, 0, "unsupported board %s", board);
  1030. build_image(output, kernel_image, rootfs_image, rev, add_jffs2_eof, sysupgrade, info);
  1031. return 0;
  1032. }