0121-firmware-utils-mktplinkfw-backport-from-LEDE-a4fc62bc0ea4010ddbfbd738453c9db70988a57c.patch 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. From: Matthias Schiffer <mschiffer@universe-factory.net>
  2. Date: Sun, 28 Aug 2016 20:20:35 +0200
  3. Subject: firmware-utils: mktplinkfw: backport from LEDE a4fc62bc0ea4010ddbfbd738453c9db70988a57c
  4. diff --git a/tools/firmware-utils/Makefile b/tools/firmware-utils/Makefile
  5. index 0397845..aee8e87 100644
  6. --- a/tools/firmware-utils/Makefile
  7. +++ b/tools/firmware-utils/Makefile
  8. @@ -40,7 +40,7 @@ define Host/Compile
  9. $(call cc,encode_crc)
  10. $(call cc,nand_ecc)
  11. $(call cc,mkplanexfw sha1)
  12. - $(call cc,mktplinkfw md5)
  13. + $(call cc,mktplinkfw md5, -Wall)
  14. $(call cc,mktplinkfw2 md5)
  15. $(call cc,tplink-safeloader md5, -Wall)
  16. $(call cc,pc1crypt)
  17. diff --git a/tools/firmware-utils/src/mktplinkfw.c b/tools/firmware-utils/src/mktplinkfw.c
  18. index 9785a3f..34e6546 100644
  19. --- a/tools/firmware-utils/src/mktplinkfw.c
  20. +++ b/tools/firmware-utils/src/mktplinkfw.c
  21. @@ -28,53 +28,10 @@
  22. #include "md5.h"
  23. #define ALIGN(x,a) ({ typeof(a) __a = (a); (((x) + __a - 1) & ~(__a - 1)); })
  24. +#define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
  25. #define HEADER_VERSION_V1 0x01000000
  26. -#define HWID_ANTMINER_S1 0x04440101
  27. -#define HWID_ANTMINER_S3 0x04440301
  28. -#define HWID_GL_INET_V1 0x08000001
  29. -#define HWID_GS_OOLITE_V1 0x3C000101
  30. -#define HWID_ONION_OMEGA 0x04700001
  31. -#define HWID_TL_MR10U_V1 0x00100101
  32. -#define HWID_TL_MR13U_V1 0x00130101
  33. -#define HWID_TL_MR3020_V1 0x30200001
  34. -#define HWID_TL_MR3220_V1 0x32200001
  35. -#define HWID_TL_MR3220_V2 0x32200002
  36. -#define HWID_TL_MR3420_V1 0x34200001
  37. -#define HWID_TL_MR3420_V2 0x34200002
  38. -#define HWID_TL_WA701N_V1 0x07010001
  39. -#define HWID_TL_WA701N_V2 0x07010002
  40. -#define HWID_TL_WA7210N_V2 0x72100002
  41. -#define HWID_TL_WA7510N_V1 0x75100001
  42. -#define HWID_TL_WA801ND_V1 0x08010001
  43. -#define HWID_TL_WA830RE_V1 0x08300010
  44. -#define HWID_TL_WA830RE_V2 0x08300002
  45. -#define HWID_TL_WA801ND_V2 0x08010002
  46. -#define HWID_TL_WA801ND_V3 0x08010003
  47. -#define HWID_TL_WA901ND_V1 0x09010001
  48. -#define HWID_TL_WA901ND_V2 0x09010002
  49. -#define HWID_TL_WA901ND_V4 0x09010004
  50. -#define HWID_TL_WDR4300_V1_IL 0x43008001
  51. -#define HWID_TL_WDR4900_V1 0x49000001
  52. -#define HWID_TL_WR703N_V1 0x07030101
  53. -#define HWID_TL_WR720N_V3 0x07200103
  54. -#define HWID_TL_WR720N_V4 0x07200104
  55. -#define HWID_TL_WR741ND_V1 0x07410001
  56. -#define HWID_TL_WR741ND_V4 0x07410004
  57. -#define HWID_TL_WR740N_V1 0x07400001
  58. -#define HWID_TL_WR740N_V3 0x07400003
  59. -#define HWID_TL_WR743ND_V1 0x07430001
  60. -#define HWID_TL_WR743ND_V2 0x07430002
  61. -#define HWID_TL_WR841N_V1_5 0x08410002
  62. -#define HWID_TL_WR841ND_V3 0x08410003
  63. -#define HWID_TL_WR841ND_V5 0x08410005
  64. -#define HWID_TL_WR841ND_V7 0x08410007
  65. -#define HWID_TL_WR941ND_V2 0x09410002
  66. -#define HWID_TL_WR941ND_V4 0x09410004
  67. -#define HWID_TL_WR1043ND_V1 0x10430001
  68. -#define HWID_TL_WR1043ND_V2 0x10430002
  69. -#define HWID_TL_WR1041N_V2 0x10410002
  70. -#define HWID_TL_WR2543N_V1 0x25430001
  71. +#define HEADER_VERSION_V2 0x02000000
  72. #define MD5SUM_LEN 16
  73. @@ -89,7 +46,7 @@ struct fw_header {
  74. char fw_version[36];
  75. uint32_t hw_id; /* hardware id */
  76. uint32_t hw_rev; /* hardware revision */
  77. - uint32_t unk1;
  78. + uint32_t region_code; /* region code */
  79. uint8_t md5sum1[MD5SUM_LEN];
  80. uint32_t unk2;
  81. uint8_t md5sum2[MD5SUM_LEN];
  82. @@ -106,7 +63,10 @@ struct fw_header {
  83. uint16_t ver_hi;
  84. uint16_t ver_mid;
  85. uint16_t ver_lo;
  86. - uint8_t pad[354];
  87. + uint8_t pad[130];
  88. + char region_str1[32];
  89. + char region_str2[32];
  90. + uint8_t pad2[160];
  91. } __attribute__ ((packed));
  92. struct flash_layout {
  93. @@ -117,13 +77,12 @@ struct flash_layout {
  94. uint32_t rootfs_ofs;
  95. };
  96. -struct board_info {
  97. - char *id;
  98. - uint32_t hw_id;
  99. - uint32_t hw_rev;
  100. - char *layout_id;
  101. +struct fw_region {
  102. + char name[4];
  103. + uint32_t code;
  104. };
  105. +
  106. /*
  107. * Globals
  108. */
  109. @@ -132,15 +91,17 @@ static char *progname;
  110. static char *vendor = "TP-LINK Technologies";
  111. static char *version = "ver. 1.0";
  112. static char *fw_ver = "0.0.0";
  113. +static uint32_t hdr_ver = HEADER_VERSION_V1;
  114. -static char *board_id;
  115. -static struct board_info *board;
  116. static char *layout_id;
  117. static struct flash_layout *layout;
  118. static char *opt_hw_id;
  119. static uint32_t hw_id;
  120. static char *opt_hw_rev;
  121. static uint32_t hw_rev;
  122. +static uint32_t opt_hdr_ver = 1;
  123. +static char *country;
  124. +static const struct fw_region *region;
  125. static int fw_ver_lo;
  126. static int fw_ver_mid;
  127. static int fw_ver_hi;
  128. @@ -163,12 +124,12 @@ static uint32_t reserved_space;
  129. static struct file_info inspect_info;
  130. static int extract = 0;
  131. -char md5salt_normal[MD5SUM_LEN] = {
  132. +static const char md5salt_normal[MD5SUM_LEN] = {
  133. 0xdc, 0xd7, 0x3a, 0xa5, 0xc3, 0x95, 0x98, 0xfb,
  134. 0xdd, 0xf9, 0xe7, 0xf4, 0x0e, 0xae, 0x47, 0x38,
  135. };
  136. -char md5salt_boot[MD5SUM_LEN] = {
  137. +static const char md5salt_boot[MD5SUM_LEN] = {
  138. 0x8c, 0xef, 0x33, 0x5b, 0xd5, 0xc5, 0xce, 0xfa,
  139. 0xa7, 0x9c, 0x28, 0xda, 0xb2, 0xe9, 0x0f, 0x42,
  140. };
  141. @@ -213,7 +174,7 @@ static struct flash_layout layouts[] = {
  142. }, {
  143. .id = "16Mppc",
  144. .fw_max_len = 0xf80000,
  145. - .kernel_la = 0x00000000,
  146. + .kernel_la = 0x00000000 ,
  147. .kernel_ep = 0xc0000000,
  148. .rootfs_ofs = 0x2a0000,
  149. }, {
  150. @@ -221,235 +182,10 @@ static struct flash_layout layouts[] = {
  151. }
  152. };
  153. -static struct board_info boards[] = {
  154. - {
  155. - .id = "TL-MR10Uv1",
  156. - .hw_id = HWID_TL_MR10U_V1,
  157. - .hw_rev = 1,
  158. - .layout_id = "4Mlzma",
  159. - }, {
  160. - .id = "TL-MR13Uv1",
  161. - .hw_id = HWID_TL_MR13U_V1,
  162. - .hw_rev = 1,
  163. - .layout_id = "4Mlzma",
  164. - }, {
  165. - .id = "TL-MR3020v1",
  166. - .hw_id = HWID_TL_MR3020_V1,
  167. - .hw_rev = 1,
  168. - .layout_id = "4Mlzma",
  169. - }, {
  170. - .id = "TL-MR3220v1",
  171. - .hw_id = HWID_TL_MR3220_V1,
  172. - .hw_rev = 1,
  173. - .layout_id = "4M",
  174. - }, {
  175. - .id = "TL-MR3220v2",
  176. - .hw_id = HWID_TL_MR3220_V2,
  177. - .hw_rev = 1,
  178. - .layout_id = "4Mlzma",
  179. - }, {
  180. - .id = "TL-MR3420v1",
  181. - .hw_id = HWID_TL_MR3420_V1,
  182. - .hw_rev = 1,
  183. - .layout_id = "4M",
  184. - }, {
  185. - .id = "TL-MR3420v2",
  186. - .hw_id = HWID_TL_MR3420_V2,
  187. - .hw_rev = 1,
  188. - .layout_id = "4Mlzma",
  189. - }, {
  190. - .id = "TL-WA701Nv1",
  191. - .hw_id = HWID_TL_WA701N_V1,
  192. - .hw_rev = 1,
  193. - .layout_id = "4M",
  194. - }, {
  195. - .id = "TL-WA701Nv2",
  196. - .hw_id = HWID_TL_WA701N_V2,
  197. - .hw_rev = 1,
  198. - .layout_id = "4Mlzma",
  199. - }, {
  200. - .id = "TL-WA7210N",
  201. - .hw_id = HWID_TL_WA7210N_V2,
  202. - .hw_rev = 2,
  203. - .layout_id = "4Mlzma",
  204. - }, {
  205. - .id = "TL-WA7510N",
  206. - .hw_id = HWID_TL_WA7510N_V1,
  207. - .hw_rev = 1,
  208. - .layout_id = "4M",
  209. - }, {
  210. - .id = "TL-WA801NDv1",
  211. - .hw_id = HWID_TL_WA801ND_V1,
  212. - .hw_rev = 1,
  213. - .layout_id = "4M",
  214. - }, {
  215. - .id = "TL-WA830REv1",
  216. - .hw_id = HWID_TL_WA830RE_V1,
  217. - .hw_rev = 1,
  218. - .layout_id = "4M",
  219. - }, {
  220. - .id = "TL-WA830REv2",
  221. - .hw_id = HWID_TL_WA830RE_V2,
  222. - .hw_rev = 1,
  223. - .layout_id = "4M",
  224. - }, {
  225. - .id = "TL-WA801NDv2",
  226. - .hw_id = HWID_TL_WA801ND_V2,
  227. - .hw_rev = 1,
  228. - .layout_id = "4Mlzma",
  229. - },{
  230. - .id = "TL-WA801NDv3",
  231. - .hw_id = HWID_TL_WA801ND_V3,
  232. - .hw_rev = 1,
  233. - .layout_id = "4Mlzma",
  234. - }, {
  235. - .id = "TL-WA901NDv1",
  236. - .hw_id = HWID_TL_WA901ND_V1,
  237. - .hw_rev = 1,
  238. - .layout_id = "4M",
  239. - }, {
  240. - .id = "TL-WA901NDv2",
  241. - .hw_id = HWID_TL_WA901ND_V2,
  242. - .hw_rev = 1,
  243. - .layout_id = "4M",
  244. - }, {
  245. - .id = "TL-WA901NDv4",
  246. - .hw_id = HWID_TL_WA901ND_V4,
  247. - .hw_rev = 1,
  248. - .layout_id = "4Mlzma",
  249. - }, {
  250. - .id = "TL-WDR4300v1",
  251. - .hw_id = HWID_TL_WDR4300_V1_IL,
  252. - .hw_rev = 1,
  253. - .layout_id = "8Mlzma",
  254. - }, {
  255. - .id = "TL-WDR4900v1",
  256. - .hw_id = HWID_TL_WDR4900_V1,
  257. - .hw_rev = 1,
  258. - .layout_id = "16Mppc",
  259. - }, {
  260. - .id = "TL-WR741NDv1",
  261. - .hw_id = HWID_TL_WR741ND_V1,
  262. - .hw_rev = 1,
  263. - .layout_id = "4M",
  264. - }, {
  265. - .id = "TL-WR741NDv4",
  266. - .hw_id = HWID_TL_WR741ND_V4,
  267. - .hw_rev = 1,
  268. - .layout_id = "4Mlzma",
  269. - }, {
  270. - .id = "TL-WR740Nv1",
  271. - .hw_id = HWID_TL_WR740N_V1,
  272. - .hw_rev = 1,
  273. - .layout_id = "4M",
  274. - }, {
  275. - .id = "TL-WR740Nv3",
  276. - .hw_id = HWID_TL_WR740N_V3,
  277. - .hw_rev = 1,
  278. - .layout_id = "4M",
  279. - }, {
  280. - .id = "TL-WR743NDv1",
  281. - .hw_id = HWID_TL_WR743ND_V1,
  282. - .hw_rev = 1,
  283. - .layout_id = "4M",
  284. - }, {
  285. - .id = "TL-WR743NDv2",
  286. - .hw_id = HWID_TL_WR743ND_V2,
  287. - .hw_rev = 1,
  288. - .layout_id = "4Mlzma",
  289. - }, {
  290. - .id = "TL-WR841Nv1.5",
  291. - .hw_id = HWID_TL_WR841N_V1_5,
  292. - .hw_rev = 2,
  293. - .layout_id = "4M",
  294. - }, {
  295. - .id = "TL-WR841NDv3",
  296. - .hw_id = HWID_TL_WR841ND_V3,
  297. - .hw_rev = 3,
  298. - .layout_id = "4M",
  299. - }, {
  300. - .id = "TL-WR841NDv5",
  301. - .hw_id = HWID_TL_WR841ND_V5,
  302. - .hw_rev = 1,
  303. - .layout_id = "4M",
  304. - }, {
  305. - .id = "TL-WR841NDv7",
  306. - .hw_id = HWID_TL_WR841ND_V7,
  307. - .hw_rev = 1,
  308. - .layout_id = "4M",
  309. - }, {
  310. - .id = "TL-WR941NDv2",
  311. - .hw_id = HWID_TL_WR941ND_V2,
  312. - .hw_rev = 2,
  313. - .layout_id = "4M",
  314. - }, {
  315. - .id = "TL-WR941NDv4",
  316. - .hw_id = HWID_TL_WR941ND_V4,
  317. - .hw_rev = 1,
  318. - .layout_id = "4M",
  319. - }, {
  320. - .id = "TL-WR1041Nv2",
  321. - .hw_id = HWID_TL_WR1041N_V2,
  322. - .hw_rev = 1,
  323. - .layout_id = "4Mlzma",
  324. - }, {
  325. - .id = "TL-WR1043NDv1",
  326. - .hw_id = HWID_TL_WR1043ND_V1,
  327. - .hw_rev = 1,
  328. - .layout_id = "8M",
  329. - }, {
  330. - .id = "TL-WR1043NDv2",
  331. - .hw_id = HWID_TL_WR1043ND_V2,
  332. - .hw_rev = 1,
  333. - .layout_id = "8Mlzma",
  334. - }, {
  335. - .id = "TL-WR2543Nv1",
  336. - .hw_id = HWID_TL_WR2543N_V1,
  337. - .hw_rev = 1,
  338. - .layout_id = "8Mlzma",
  339. - }, {
  340. - .id = "TL-WR703Nv1",
  341. - .hw_id = HWID_TL_WR703N_V1,
  342. - .hw_rev = 1,
  343. - .layout_id = "4Mlzma",
  344. - }, {
  345. - .id = "TL-WR720Nv3",
  346. - .hw_id = HWID_TL_WR720N_V3,
  347. - .hw_rev = 1,
  348. - .layout_id = "4Mlzma",
  349. - }, {
  350. - .id = "TL-WR720Nv4",
  351. - .hw_id = HWID_TL_WR720N_V4,
  352. - .hw_rev = 1,
  353. - .layout_id = "4Mlzma",
  354. - }, {
  355. - .id = "GL-INETv1",
  356. - .hw_id = HWID_GL_INET_V1,
  357. - .hw_rev = 1,
  358. - .layout_id = "8Mlzma",
  359. - }, {
  360. - .id = "GS-OOLITEv1",
  361. - .hw_id = HWID_GS_OOLITE_V1,
  362. - .hw_rev = 1,
  363. - .layout_id = "16Mlzma",
  364. - }, {
  365. - .id = "ONION-OMEGA",
  366. - .hw_id = HWID_ONION_OMEGA,
  367. - .hw_rev = 1,
  368. - .layout_id = "16Mlzma",
  369. - }, {
  370. - .id = "ANTMINER-S1",
  371. - .hw_id = HWID_ANTMINER_S1,
  372. - .hw_rev = 1,
  373. - .layout_id = "8Mlzma",
  374. - }, {
  375. - .id = "ANTMINER-S3",
  376. - .hw_id = HWID_ANTMINER_S3,
  377. - .hw_rev = 1,
  378. - .layout_id = "8Mlzma",
  379. - }, {
  380. - /* terminating entry */
  381. - }
  382. +static const struct fw_region regions[] = {
  383. + /* Default region (universal) uses code 0 as well */
  384. + {"US", 1},
  385. + {"EU", 0},
  386. };
  387. /*
  388. @@ -464,7 +200,7 @@ static struct board_info boards[] = {
  389. #define ERRS(fmt, ...) do { \
  390. int save = errno; \
  391. fflush(0); \
  392. - fprintf(stderr, "[%s] *** error: " fmt "\n", \
  393. + fprintf(stderr, "[%s] *** error: " fmt ": %s\n", \
  394. progname, ## __VA_ARGS__, strerror(save)); \
  395. } while (0)
  396. @@ -472,35 +208,7 @@ static struct board_info boards[] = {
  397. fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
  398. } while (0)
  399. -static struct board_info *find_board(char *id)
  400. -{
  401. - struct board_info *ret;
  402. - struct board_info *board;
  403. -
  404. - ret = NULL;
  405. - for (board = boards; board->id != NULL; board++){
  406. - if (strcasecmp(id, board->id) == 0) {
  407. - ret = board;
  408. - break;
  409. - }
  410. - };
  411. -
  412. - return ret;
  413. -}
  414. -
  415. -static struct board_info *find_board_by_hwid(uint32_t hw_id)
  416. -{
  417. - struct board_info *board;
  418. -
  419. - for (board = boards; board->id != NULL; board++) {
  420. - if (hw_id == board->hw_id)
  421. - return board;
  422. - };
  423. -
  424. - return NULL;
  425. -}
  426. -
  427. -static struct flash_layout *find_layout(char *id)
  428. +static struct flash_layout *find_layout(const char *id)
  429. {
  430. struct flash_layout *ret;
  431. struct flash_layout *l;
  432. @@ -516,21 +224,29 @@ static struct flash_layout *find_layout(char *id)
  433. return ret;
  434. }
  435. +static const struct fw_region * find_region(const char *country) {
  436. + size_t i;
  437. +
  438. + for (i = 0; i < ARRAY_SIZE(regions); i++) {
  439. + if (strcasecmp(regions[i].name, country) == 0)
  440. + return &regions[i];
  441. + }
  442. +
  443. + return NULL;
  444. +}
  445. +
  446. static void usage(int status)
  447. {
  448. - FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  449. - struct board_info *board;
  450. -
  451. - fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  452. - fprintf(stream,
  453. + fprintf(stderr, "Usage: %s [OPTIONS...]\n", progname);
  454. + fprintf(stderr,
  455. "\n"
  456. "Options:\n"
  457. -" -B <board> create image for the board specified with <board>\n"
  458. " -c use combined kernel image\n"
  459. " -E <ep> overwrite kernel entry point with <ep> (hexval prefixed with 0x)\n"
  460. " -L <la> overwrite kernel load address with <la> (hexval prefixed with 0x)\n"
  461. " -H <hwid> use hardware id specified with <hwid>\n"
  462. " -W <hwrev> use hardware revision specified with <hwrev>\n"
  463. +" -C <country> set region code to <country>\n"
  464. " -F <id> use flash layout specified with <id>\n"
  465. " -k <file> read kernel image from the file <file>\n"
  466. " -r <file> read rootfs image from the file <file>\n"
  467. @@ -543,6 +259,7 @@ static void usage(int status)
  468. " -N <vendor> set image vendor to <vendor>\n"
  469. " -V <version> set image version to <version>\n"
  470. " -v <version> set firmware version to <version>\n"
  471. +" -m <version> set header version to <version>\n"
  472. " -i <file> inspect given firmware file <file>\n"
  473. " -x extract kernel and rootfs while inspecting (requires -i)\n"
  474. " -X <size> reserve <size> bytes in the firmware image (hexval prefixed with 0x)\n"
  475. @@ -552,7 +269,7 @@ static void usage(int status)
  476. exit(status);
  477. }
  478. -static int get_md5(char *data, int size, char *md5)
  479. +static void get_md5(const char *data, int size, uint8_t *md5)
  480. {
  481. MD5_CTX ctx;
  482. @@ -579,7 +296,7 @@ static int get_file_stat(struct file_info *fdata)
  483. return 0;
  484. }
  485. -static int read_to_buf(struct file_info *fdata, char *buf)
  486. +static int read_to_buf(const struct file_info *fdata, char *buf)
  487. {
  488. FILE *f;
  489. int ret = EXIT_FAILURE;
  490. @@ -608,6 +325,7 @@ static int read_to_buf(struct file_info *fdata, char *buf)
  491. static int check_options(void)
  492. {
  493. int ret;
  494. + int exceed_bytes;
  495. if (inspect_info.file_name) {
  496. ret = get_file_stat(&inspect_info);
  497. @@ -620,33 +338,28 @@ static int check_options(void)
  498. return -1;
  499. }
  500. - if (board_id == NULL && opt_hw_id == NULL) {
  501. - ERR("either board or hardware id must be specified");
  502. + if (opt_hw_id == NULL) {
  503. + ERR("hardware id not specified");
  504. return -1;
  505. }
  506. + hw_id = strtoul(opt_hw_id, NULL, 0);
  507. - if (board_id) {
  508. - board = find_board(board_id);
  509. - if (board == NULL) {
  510. - ERR("unknown/unsupported board id \"%s\"", board_id);
  511. - return -1;
  512. - }
  513. - if (layout_id == NULL)
  514. - layout_id = board->layout_id;
  515. + if (layout_id == NULL) {
  516. + ERR("flash layout is not specified");
  517. + return -1;
  518. + }
  519. - hw_id = board->hw_id;
  520. - hw_rev = board->hw_rev;
  521. - } else {
  522. - if (layout_id == NULL) {
  523. - ERR("flash layout is not specified");
  524. + if (opt_hw_rev)
  525. + hw_rev = strtoul(opt_hw_rev, NULL, 0);
  526. + else
  527. + hw_rev = 1;
  528. +
  529. + if (country) {
  530. + region = find_region(country);
  531. + if (!region) {
  532. + ERR("unknown region code \"%s\"", country);
  533. return -1;
  534. }
  535. - hw_id = strtoul(opt_hw_id, NULL, 0);
  536. -
  537. - if (opt_hw_rev)
  538. - hw_rev = strtoul(opt_hw_rev, NULL, 0);
  539. - else
  540. - hw_rev = 1;
  541. }
  542. layout = find_layout(layout_id);
  543. @@ -681,10 +394,10 @@ static int check_options(void)
  544. kernel_len = kernel_info.file_size;
  545. if (combined) {
  546. - if (kernel_info.file_size >
  547. - fw_max_len - sizeof(struct fw_header)) {
  548. + exceed_bytes = kernel_info.file_size - (fw_max_len - sizeof(struct fw_header));
  549. + if (exceed_bytes > 0) {
  550. if (!ignore_size) {
  551. - ERR("kernel image is too big");
  552. + ERR("kernel image is too big by %i bytes", exceed_bytes);
  553. return -1;
  554. }
  555. layout->fw_max_len = sizeof(struct fw_header) +
  556. @@ -708,21 +421,21 @@ static int check_options(void)
  557. DBG("kernel length aligned to %u", kernel_len);
  558. - if (kernel_len + rootfs_info.file_size >
  559. - fw_max_len - sizeof(struct fw_header)) {
  560. - ERR("images are too big");
  561. + exceed_bytes = kernel_len + rootfs_info.file_size - (fw_max_len - sizeof(struct fw_header));
  562. + if (exceed_bytes > 0) {
  563. + ERR("images are too big by %i bytes", exceed_bytes);
  564. return -1;
  565. }
  566. } else {
  567. - if (kernel_info.file_size >
  568. - rootfs_ofs - sizeof(struct fw_header)) {
  569. - ERR("kernel image is too big");
  570. + exceed_bytes = kernel_info.file_size - (rootfs_ofs - sizeof(struct fw_header));
  571. + if (exceed_bytes > 0) {
  572. + ERR("kernel image is too big by %i bytes", exceed_bytes);
  573. return -1;
  574. }
  575. - if (rootfs_info.file_size >
  576. - (fw_max_len - rootfs_ofs)) {
  577. - ERR("rootfs image is too big");
  578. + exceed_bytes = rootfs_info.file_size - (fw_max_len - rootfs_ofs);
  579. + if (exceed_bytes > 0) {
  580. + ERR("rootfs image is too big by %i bytes", exceed_bytes);
  581. return -1;
  582. }
  583. }
  584. @@ -739,6 +452,15 @@ static int check_options(void)
  585. return -1;
  586. }
  587. + if (opt_hdr_ver == 1) {
  588. + hdr_ver = HEADER_VERSION_V1;
  589. + } else if (opt_hdr_ver == 2) {
  590. + hdr_ver = HEADER_VERSION_V2;
  591. + } else {
  592. + ERR("invalid header version '%u'", opt_hdr_ver);
  593. + return -1;
  594. + }
  595. +
  596. return 0;
  597. }
  598. @@ -748,7 +470,7 @@ static void fill_header(char *buf, int len)
  599. memset(hdr, 0, sizeof(struct fw_header));
  600. - hdr->version = htonl(HEADER_VERSION_V1);
  601. + hdr->version = htonl(hdr_ver);
  602. strncpy(hdr->vendor_name, vendor, sizeof(hdr->vendor_name));
  603. strncpy(hdr->fw_version, version, sizeof(hdr->fw_version));
  604. hdr->hw_id = htonl(hw_id);
  605. @@ -773,6 +495,18 @@ static void fill_header(char *buf, int len)
  606. hdr->ver_mid = htons(fw_ver_mid);
  607. hdr->ver_lo = htons(fw_ver_lo);
  608. + if (region) {
  609. + hdr->region_code = htonl(region->code);
  610. + snprintf(
  611. + hdr->region_str1, sizeof(hdr->region_str1), "00000000;%02X%02X%02X%02X;",
  612. + region->name[0], region->name[1], region->name[2], region->name[3]
  613. + );
  614. + snprintf(
  615. + hdr->region_str2, sizeof(hdr->region_str2), "%02X%02X%02X%02X",
  616. + region->name[0], region->name[1], region->name[2], region->name[3]
  617. + );
  618. + }
  619. +
  620. get_md5(buf, len, hdr->md5sum1);
  621. }
  622. @@ -810,7 +544,7 @@ static int pad_jffs2(char *buf, int currlen)
  623. return len;
  624. }
  625. -static int write_fw(char *data, int len)
  626. +static int write_fw(const char *data, int len)
  627. {
  628. FILE *f;
  629. int ret = EXIT_FAILURE;
  630. @@ -902,61 +636,22 @@ static int build_fw(void)
  631. }
  632. /* Helper functions to inspect_fw() representing different output formats */
  633. -static inline void inspect_fw_pstr(char *label, char *str)
  634. +static inline void inspect_fw_pstr(const char *label, const char *str)
  635. {
  636. printf("%-23s: %s\n", label, str);
  637. }
  638. -static inline void inspect_fw_phex(char *label, uint32_t val)
  639. +static inline void inspect_fw_phex(const char *label, uint32_t val)
  640. {
  641. printf("%-23s: 0x%08x\n", label, val);
  642. }
  643. -static inline void inspect_fw_phexpost(char *label,
  644. - uint32_t val, char *post)
  645. -{
  646. - printf("%-23s: 0x%08x (%s)\n", label, val, post);
  647. -}
  648. -
  649. -static inline void inspect_fw_phexdef(char *label,
  650. - uint32_t val, uint32_t defval)
  651. -{
  652. - printf("%-23s: 0x%08x ", label, val);
  653. -
  654. - if (val == defval)
  655. - printf("(== OpenWrt default)\n");
  656. - else
  657. - printf("(OpenWrt default: 0x%08x)\n", defval);
  658. -}
  659. -
  660. -static inline void inspect_fw_phexexp(char *label,
  661. - uint32_t val, uint32_t expval)
  662. -{
  663. - printf("%-23s: 0x%08x ", label, val);
  664. -
  665. - if (val == expval)
  666. - printf("(ok)\n");
  667. - else
  668. - printf("(expected: 0x%08x)\n", expval);
  669. -}
  670. -
  671. -static inline void inspect_fw_phexdec(char *label, uint32_t val)
  672. +static inline void inspect_fw_phexdec(const char *label, uint32_t val)
  673. {
  674. printf("%-23s: 0x%08x / %8u bytes\n", label, val, val);
  675. }
  676. -static inline void inspect_fw_phexdecdef(char *label,
  677. - uint32_t val, uint32_t defval)
  678. -{
  679. - printf("%-23s: 0x%08x / %8u bytes ", label, val, val);
  680. -
  681. - if (val == defval)
  682. - printf("(== OpenWrt default)\n");
  683. - else
  684. - printf("(OpenWrt default: 0x%08x)\n", defval);
  685. -}
  686. -
  687. -static inline void inspect_fw_pmd5sum(char *label, uint8_t *val, char *text)
  688. +static inline void inspect_fw_pmd5sum(const char *label, const uint8_t *val, const char *text)
  689. {
  690. int i;
  691. @@ -971,7 +666,6 @@ static int inspect_fw(void)
  692. char *buf;
  693. struct fw_header *hdr;
  694. uint8_t md5sum[MD5SUM_LEN];
  695. - struct board_info *board;
  696. int ret = EXIT_FAILURE;
  697. buf = malloc(inspect_info.file_size);
  698. @@ -988,16 +682,14 @@ static int inspect_fw(void)
  699. inspect_fw_pstr("File name", inspect_info.file_name);
  700. inspect_fw_phexdec("File size", inspect_info.file_size);
  701. - if (ntohl(hdr->version) != HEADER_VERSION_V1) {
  702. - ERR("file does not seem to have V1 header!\n");
  703. + if ((ntohl(hdr->version) != HEADER_VERSION_V1) &&
  704. + (ntohl(hdr->version) != HEADER_VERSION_V2)) {
  705. + ERR("file does not seem to have V1/V2 header!\n");
  706. goto out_free_buf;
  707. }
  708. inspect_fw_phexdec("Version 1 Header size", sizeof(struct fw_header));
  709. - if (ntohl(hdr->unk1) != 0)
  710. - inspect_fw_phexdec("Unknown value 1", hdr->unk1);
  711. -
  712. memcpy(md5sum, hdr->md5sum1, sizeof(md5sum));
  713. if (ntohl(hdr->boot_len) == 0)
  714. memcpy(hdr->md5sum1, md5salt_normal, sizeof(md5sum));
  715. @@ -1022,19 +714,9 @@ static int inspect_fw(void)
  716. inspect_fw_pstr("Vendor name", hdr->vendor_name);
  717. inspect_fw_pstr("Firmware version", hdr->fw_version);
  718. - board = find_board_by_hwid(ntohl(hdr->hw_id));
  719. - if (board) {
  720. - layout = find_layout(board->layout_id);
  721. - inspect_fw_phexpost("Hardware ID",
  722. - ntohl(hdr->hw_id), board->id);
  723. - inspect_fw_phexexp("Hardware Revision",
  724. - ntohl(hdr->hw_rev), board->hw_rev);
  725. - } else {
  726. - inspect_fw_phexpost("Hardware ID",
  727. - ntohl(hdr->hw_id), "unknown");
  728. - inspect_fw_phex("Hardware Revision",
  729. - ntohl(hdr->hw_rev));
  730. - }
  731. + inspect_fw_phex("Hardware ID", ntohl(hdr->hw_id));
  732. + inspect_fw_phex("Hardware Revision", ntohl(hdr->hw_rev));
  733. + inspect_fw_phex("Region code", ntohl(hdr->region_code));
  734. printf("\n");
  735. @@ -1042,24 +724,12 @@ static int inspect_fw(void)
  736. ntohl(hdr->kernel_ofs));
  737. inspect_fw_phexdec("Kernel data length",
  738. ntohl(hdr->kernel_len));
  739. - if (board) {
  740. - inspect_fw_phexdef("Kernel load address",
  741. - ntohl(hdr->kernel_la),
  742. - layout ? layout->kernel_la : 0xffffffff);
  743. - inspect_fw_phexdef("Kernel entry point",
  744. - ntohl(hdr->kernel_ep),
  745. - layout ? layout->kernel_ep : 0xffffffff);
  746. - inspect_fw_phexdecdef("Rootfs data offset",
  747. - ntohl(hdr->rootfs_ofs),
  748. - layout ? layout->rootfs_ofs : 0xffffffff);
  749. - } else {
  750. - inspect_fw_phex("Kernel load address",
  751. - ntohl(hdr->kernel_la));
  752. - inspect_fw_phex("Kernel entry point",
  753. - ntohl(hdr->kernel_ep));
  754. - inspect_fw_phexdec("Rootfs data offset",
  755. - ntohl(hdr->rootfs_ofs));
  756. - }
  757. + inspect_fw_phex("Kernel load address",
  758. + ntohl(hdr->kernel_la));
  759. + inspect_fw_phex("Kernel entry point",
  760. + ntohl(hdr->kernel_ep));
  761. + inspect_fw_phexdec("Rootfs data offset",
  762. + ntohl(hdr->rootfs_ofs));
  763. inspect_fw_phexdec("Rootfs data length",
  764. ntohl(hdr->rootfs_len));
  765. inspect_fw_phexdec("Boot loader data offset",
  766. @@ -1115,16 +785,13 @@ static int inspect_fw(void)
  767. int main(int argc, char *argv[])
  768. {
  769. int ret = EXIT_FAILURE;
  770. - int err;
  771. -
  772. - FILE *outfile;
  773. progname = basename(argv[0]);
  774. while ( 1 ) {
  775. int c;
  776. - c = getopt(argc, argv, "a:B:H:E:F:L:V:N:W:ci:k:r:R:o:xX:hsSjv:");
  777. + c = getopt(argc, argv, "a:H:E:F:L:m:V:N:W:C:ci:k:r:R:o:xX:hsSjv:");
  778. if (c == -1)
  779. break;
  780. @@ -1132,9 +799,6 @@ int main(int argc, char *argv[])
  781. case 'a':
  782. sscanf(optarg, "0x%x", &rootfs_align);
  783. break;
  784. - case 'B':
  785. - board_id = optarg;
  786. - break;
  787. case 'H':
  788. opt_hw_id = optarg;
  789. break;
  790. @@ -1147,9 +811,15 @@ int main(int argc, char *argv[])
  791. case 'W':
  792. opt_hw_rev = optarg;
  793. break;
  794. + case 'C':
  795. + country = optarg;
  796. + break;
  797. case 'L':
  798. sscanf(optarg, "0x%x", &kernel_la);
  799. break;
  800. + case 'm':
  801. + sscanf(optarg, "%u", &opt_hdr_ver);
  802. + break;
  803. case 'V':
  804. version = optarg;
  805. break;