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

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