tplink-safeloader.c 68 KB

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