mkzynfw.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /*
  2. *
  3. * Copyright (C) 2007-2008 OpenWrt.org
  4. * Copyright (C) 2007-2008 Gabor Juhos <juhosg at openwrt.org>
  5. *
  6. * This code was based on the information of the ZyXEL's firmware
  7. * image format written by Kolja Waschk, can be found at:
  8. * http://www.ixo.de/info/zyxel_uclinux
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <unistd.h> /* for unlink() */
  20. #include <libgen.h>
  21. #include <getopt.h> /* for getopt() */
  22. #include <stdarg.h>
  23. #include <errno.h>
  24. #include <sys/stat.h>
  25. #include <endian.h> /* for __BYTE_ORDER */
  26. #if defined(__CYGWIN__)
  27. # include <byteswap.h>
  28. #endif
  29. #include <inttypes.h>
  30. #include "zynos.h"
  31. #if (__BYTE_ORDER == __LITTLE_ENDIAN)
  32. # define HOST_TO_LE16(x) (x)
  33. # define HOST_TO_LE32(x) (x)
  34. # define LE16_TO_HOST(x) (x)
  35. # define LE32_TO_HOST(x) (x)
  36. # define HOST_TO_BE16(x) bswap_16(x)
  37. # define HOST_TO_BE32(x) bswap_32(x)
  38. # define BE16_TO_HOST(x) bswap_16(x)
  39. # define BE32_TO_HOST(x) bswap_32(x)
  40. #else
  41. # define HOST_TO_BE16(x) (x)
  42. # define HOST_TO_BE32(x) (x)
  43. # define BE16_TO_HOST(x) (x)
  44. # define BE32_TO_HOST(x) (x)
  45. # define HOST_TO_LE16(x) bswap_16(x)
  46. # define HOST_TO_LE32(x) bswap_32(x)
  47. # define LE16_TO_HOST(x) bswap_16(x)
  48. # define LE32_TO_HOST(x) bswap_32(x)
  49. #endif
  50. #define ALIGN(x,y) (((x)+((y)-1)) & ~((y)-1))
  51. #define MAX_NUM_BLOCKS 8
  52. #define MAX_ARG_COUNT 32
  53. #define MAX_ARG_LEN 1024
  54. #define FILE_BUF_LEN (16*1024)
  55. struct csum_state{
  56. int odd;
  57. uint32_t sum;
  58. uint32_t tmp;
  59. };
  60. struct fw_block {
  61. uint32_t align; /* alignment of this block */
  62. char *file_name; /* name of the file */
  63. uint32_t file_size; /* length of the file */
  64. char *mmap_name; /* name in the MMAP table */
  65. int type; /* block type */
  66. uint32_t padlen;
  67. uint8_t padc;
  68. };
  69. #define BLOCK_TYPE_BOOTEXT 0
  70. #define BLOCK_TYPE_RAW 1
  71. struct fw_mmap {
  72. uint32_t addr;
  73. uint32_t size;
  74. uint32_t user_addr;
  75. uint32_t user_size;
  76. };
  77. #define MMAP_DATA_SIZE 1024
  78. #define MMAP_ALIGN 16
  79. struct board_info {
  80. char *name; /* model name */
  81. char *desc; /* description */
  82. uint16_t vendor; /* vendor id */
  83. uint16_t model; /* model id */
  84. uint32_t flash_base; /* flash base address */
  85. uint32_t flash_size; /* board flash size */
  86. uint32_t code_start; /* code start address */
  87. uint32_t romio_offs; /* offset of the firmware within the flash */
  88. uint32_t bootext_size; /* maximum size of bootext block */
  89. };
  90. /*
  91. * Globals
  92. */
  93. char *progname;
  94. char *ofname = NULL;
  95. int verblevel = 0;
  96. struct board_info *board = NULL;
  97. struct fw_block blocks[MAX_NUM_BLOCKS];
  98. struct fw_block *bootext_block = NULL;
  99. int num_blocks = 0;
  100. #define ADM5120_FLASH_BASE 0xBFC00000
  101. #define ADM5120_CODE_START 0x80008000
  102. /* TODO: check values for AR7 */
  103. #define AR7_FLASH_BASE 0xB0000000
  104. #define AR7_CODE_START 0x94008000
  105. #define ATHEROS_FLASH_BASE 0xBFC00000
  106. #define ATHEROS_CODE_START 0x80e00000
  107. #define AR71XX_FLASH_BASE 0xBFC00000
  108. #define AR71XX_CODE_START 0x81E00000
  109. #define BOARD(n, d, v, m, fb, fs, cs, fo) { \
  110. .name = (n), .desc=(d), \
  111. .vendor = (v), .model = (m), \
  112. .flash_base = (fb), .flash_size = (fs)<<20, \
  113. .code_start = (cs), .romio_offs = (fo), \
  114. .bootext_size = BOOTEXT_DEF_SIZE \
  115. }
  116. #define ADMBOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
  117. ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x8000)
  118. #define ADMBOARD2(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
  119. ADM5120_FLASH_BASE, fs, ADM5120_CODE_START, 0x10000)
  120. #define AR7BOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
  121. AR7_FLASH_BASE, fs, AR7_CODE_START, 0x8000)
  122. #define ATHEROSBOARD1(n, d, m, fs) BOARD(n, d, ZYNOS_VENDOR_ID_ZYXEL, m, \
  123. ATHEROS_FLASH_BASE, fs, ATHEROS_CODE_START, 0x30000)
  124. #define AR71XXBOARD1(n, d, m, fs) { \
  125. .name = (n), .desc=(d), \
  126. .vendor = (ZYNOS_VENDOR_ID_ZYXEL), .model = (m), \
  127. .flash_base = (AR71XX_FLASH_BASE), .flash_size = (fs)<<20, \
  128. .code_start = (AR71XX_CODE_START), .romio_offs = (0x40000), \
  129. .bootext_size = 0x30000 \
  130. }
  131. static struct board_info boards[] = {
  132. /*
  133. * Infineon/ADMtek ADM5120 based boards
  134. */
  135. ADMBOARD2("ES-2024A", "ZyXEL ES-2024A", ZYNOS_MODEL_ES_2024A, 4),
  136. ADMBOARD2("ES-2024PWR", "ZyXEL ES-2024PWR", ZYNOS_MODEL_ES_2024PWR, 4),
  137. ADMBOARD2("ES-2108", "ZyXEL ES-2108", ZYNOS_MODEL_ES_2108, 4),
  138. ADMBOARD2("ES-2108-F", "ZyXEL ES-2108-F", ZYNOS_MODEL_ES_2108_F, 4),
  139. ADMBOARD2("ES-2108-G", "ZyXEL ES-2108-G", ZYNOS_MODEL_ES_2108_G, 4),
  140. ADMBOARD2("ES-2108-LC", "ZyXEL ES-2108-LC", ZYNOS_MODEL_ES_2108_LC, 4),
  141. ADMBOARD2("ES-2108PWR", "ZyXEL ES-2108PWR", ZYNOS_MODEL_ES_2108PWR, 4),
  142. ADMBOARD1("HS-100", "ZyXEL HomeSafe 100", ZYNOS_MODEL_HS_100, 2),
  143. ADMBOARD1("HS-100W", "ZyXEL HomeSafe 100W", ZYNOS_MODEL_HS_100W, 2),
  144. ADMBOARD1("P-334", "ZyXEL Prestige 334", ZYNOS_MODEL_P_334, 2),
  145. ADMBOARD1("P-334U", "ZyXEL Prestige 334U", ZYNOS_MODEL_P_334U, 4),
  146. ADMBOARD1("P-334W", "ZyXEL Prestige 334W", ZYNOS_MODEL_P_334W, 2),
  147. ADMBOARD1("P-334WH", "ZyXEL Prestige 334WH", ZYNOS_MODEL_P_334WH, 4),
  148. ADMBOARD1("P-334WHD", "ZyXEL Prestige 334WHD", ZYNOS_MODEL_P_334WHD, 4),
  149. ADMBOARD1("P-334WT", "ZyXEL Prestige 334WT", ZYNOS_MODEL_P_334WT, 4),
  150. ADMBOARD1("P-335", "ZyXEL Prestige 335", ZYNOS_MODEL_P_335, 4),
  151. ADMBOARD1("P-335Plus", "ZyXEL Prestige 335Plus", ZYNOS_MODEL_P_335PLUS, 4),
  152. ADMBOARD1("P-335U", "ZyXEL Prestige 335U", ZYNOS_MODEL_P_335U, 4),
  153. ADMBOARD1("P-335WT", "ZyXEL Prestige 335WT", ZYNOS_MODEL_P_335WT, 4),
  154. {
  155. .name = "P-2602HW-D1A",
  156. .desc = "ZyXEL P-2602HW-D1A",
  157. .vendor = ZYNOS_VENDOR_ID_ZYXEL,
  158. .model = ZYNOS_MODEL_P_2602HW_D1A,
  159. .flash_base = AR7_FLASH_BASE,
  160. .flash_size = 4*1024*1024,
  161. .code_start = 0x94008000,
  162. .romio_offs = 0x20000,
  163. .bootext_size = BOOTEXT_DEF_SIZE,
  164. },
  165. #if 0
  166. /*
  167. * Texas Instruments AR7 based boards
  168. */
  169. AR7BOARD1("P-660H-61", "ZyXEL P-660H-61", ZYNOS_MODEL_P_660H_61, 2),
  170. AR7BOARD1("P-660H-63", "ZyXEL P-660H-63", ZYNOS_MODEL_P_660H_63, 2),
  171. AR7BOARD1("P-660H-D1", "ZyXEL P-660H-D1", ZYNOS_MODEL_P_660H_D1, 2),
  172. AR7BOARD1("P-660H-D3", "ZyXEL P-660H-D3", ZYNOS_MODEL_P_660H_D3, 2),
  173. AR7BOARD1("P-660HW-61", "ZyXEL P-660HW-61", ZYNOS_MODEL_P_660HW_61, 2),
  174. AR7BOARD1("P-660HW-63", "ZyXEL P-660HW-63", ZYNOS_MODEL_P_660HW_63, 2),
  175. AR7BOARD1("P-660HW-67", "ZyXEL P-660HW-67", ZYNOS_MODEL_P_660HW_67, 2),
  176. AR7BOARD1("P-660HW-D1", "ZyXEL P-660HW-D1", ZYNOS_MODEL_P_660HW_D1, 2),
  177. AR7BOARD1("P-660HW-D3", "ZyXEL P-660HW-D3", ZYNOS_MODEL_P_660HW_D3, 2),
  178. AR7BOARD1("P-660R-61", "ZyXEL P-660R-61", ZYNOS_MODEL_P_660R_61, 2),
  179. AR7BOARD1("P-660R-61C", "ZyXEL P-660R-61C", ZYNOS_MODEL_P_660R_61C, 2),
  180. AR7BOARD1("P-660R-63", "ZyXEL P-660R-63", ZYNOS_MODEL_P_660R_63, 2),
  181. AR7BOARD1("P-660R-63C", "ZyXEL P-660R-63C", ZYNOS_MODEL_P_660R_63C, 2),
  182. AR7BOARD1("P-660R-67", "ZyXEL P-660R-67", ZYNOS_MODEL_P_660R_67, 2),
  183. AR7BOARD1("P-660R-D1", "ZyXEL P-660R-D1", ZYNOS_MODEL_P_660R_D1, 2),
  184. AR7BOARD1("P-660R-D3", "ZyXEL P-660R-D3", ZYNOS_MODEL_P_660R_D3, 2),
  185. #endif
  186. {
  187. .name = "O2SURF",
  188. .desc = "O2 DSL Surf & Phone",
  189. .vendor = ZYNOS_VENDOR_ID_O2,
  190. .model = ZYNOS_MODEL_O2SURF,
  191. .flash_base = AR7_FLASH_BASE,
  192. .flash_size = 8*1024*1024,
  193. .code_start = 0x94014000,
  194. .romio_offs = 0x40000,
  195. .bootext_size = BOOTEXT_DEF_SIZE,
  196. },
  197. /*
  198. :x
  199. */
  200. ATHEROSBOARD1("NBG-318S", "ZyXEL NBG-318S", ZYNOS_MODEL_NBG_318S, 4),
  201. /*
  202. * Atheros ar71xx based boards
  203. */
  204. AR71XXBOARD1("NBG-460N", "ZyXEL NBG-460N", ZYNOS_MODEL_NBG_460N, 4),
  205. {.name = NULL}
  206. };
  207. /*
  208. * Message macros
  209. */
  210. #define ERR(fmt, ...) do { \
  211. fflush(0); \
  212. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  213. progname, ## __VA_ARGS__ ); \
  214. } while (0)
  215. #define ERRS(fmt, ...) do { \
  216. int save = errno; \
  217. fflush(0); \
  218. fprintf(stderr, "[%s] *** error: " fmt ", %s\n", \
  219. progname, ## __VA_ARGS__, strerror(save)); \
  220. } while (0)
  221. #define WARN(fmt, ...) do { \
  222. fprintf(stderr, "[%s] *** warning: " fmt "\n", \
  223. progname, ## __VA_ARGS__ ); \
  224. } while (0)
  225. #define DBG(lev, fmt, ...) do { \
  226. if (verblevel < lev) \
  227. break;\
  228. fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
  229. } while (0)
  230. #define ERR_FATAL -1
  231. #define ERR_INVALID_IMAGE -2
  232. /*
  233. * Helper routines
  234. */
  235. void
  236. usage(int status)
  237. {
  238. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  239. struct board_info *board;
  240. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  241. fprintf(stream,
  242. "\n"
  243. "Options:\n"
  244. " -B <board> create image for the board specified with <board>.\n"
  245. " valid <board> values:\n"
  246. );
  247. for (board = boards; board->name != NULL; board++){
  248. fprintf(stream,
  249. " %-12s= %s\n",
  250. board->name, board->desc);
  251. };
  252. fprintf(stream,
  253. " -b <file>[:<align>]\n"
  254. " add boot extension block to the image\n"
  255. " -r <file>[:<align>]\n"
  256. " add raw block to the image\n"
  257. " -o <file> write output to the file <file>\n"
  258. " -h show this screen\n"
  259. );
  260. exit(status);
  261. }
  262. /*
  263. * argument parsing
  264. */
  265. int
  266. str2u32(char *arg, uint32_t *val)
  267. {
  268. char *err = NULL;
  269. uint32_t t;
  270. errno=0;
  271. t = strtoul(arg, &err, 0);
  272. if (errno || (err==arg) || ((err != NULL) && *err)) {
  273. return -1;
  274. }
  275. *val = t;
  276. return 0;
  277. }
  278. int
  279. str2u16(char *arg, uint16_t *val)
  280. {
  281. char *err = NULL;
  282. uint32_t t;
  283. errno=0;
  284. t = strtoul(arg, &err, 0);
  285. if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
  286. return -1;
  287. }
  288. *val = t & 0xFFFF;
  289. return 0;
  290. }
  291. int
  292. str2u8(char *arg, uint8_t *val)
  293. {
  294. char *err = NULL;
  295. uint32_t t;
  296. errno=0;
  297. t = strtoul(arg, &err, 0);
  298. if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x100)) {
  299. return -1;
  300. }
  301. *val = t & 0xFF;
  302. return 0;
  303. }
  304. int
  305. str2sig(char *arg, uint32_t *sig)
  306. {
  307. if (strlen(arg) != 4)
  308. return -1;
  309. *sig = arg[0] | (arg[1] << 8) | (arg[2] << 16) | (arg[3] << 24);
  310. return 0;
  311. }
  312. int
  313. parse_arg(char *arg, char *buf, char *argv[])
  314. {
  315. int res = 0;
  316. size_t argl;
  317. char *tok;
  318. char **ap = &buf;
  319. int i;
  320. memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
  321. if ((arg == NULL)) {
  322. /* no arguments */
  323. return 0;
  324. }
  325. argl = strlen(arg);
  326. if (argl == 0) {
  327. /* no arguments */
  328. return 0;
  329. }
  330. if (argl >= MAX_ARG_LEN) {
  331. /* argument is too long */
  332. argl = MAX_ARG_LEN-1;
  333. }
  334. memcpy(buf, arg, argl);
  335. buf[argl] = '\0';
  336. for (i = 0; i < MAX_ARG_COUNT; i++) {
  337. tok = strsep(ap, ":");
  338. if (tok == NULL) {
  339. break;
  340. }
  341. #if 0
  342. else if (tok[0] == '\0') {
  343. break;
  344. }
  345. #endif
  346. argv[i] = tok;
  347. res++;
  348. }
  349. return res;
  350. }
  351. int
  352. required_arg(char c, char *arg)
  353. {
  354. if (arg == NULL || *arg != '-')
  355. return 0;
  356. ERR("option -%c requires an argument\n", c);
  357. return -1;
  358. }
  359. int
  360. is_empty_arg(char *arg)
  361. {
  362. int ret = 1;
  363. if (arg != NULL) {
  364. if (*arg) ret = 0;
  365. };
  366. return ret;
  367. }
  368. void
  369. csum_init(struct csum_state *css)
  370. {
  371. css->odd = 0;
  372. css->sum = 0;
  373. css->tmp = 0;
  374. }
  375. void
  376. csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
  377. {
  378. if (len == 0)
  379. return;
  380. if (css->odd) {
  381. css->sum += (css->tmp << 8) + p[0];
  382. if (css->sum > 0xFFFF) {
  383. css->sum += 1;
  384. css->sum &= 0xFFFF;
  385. }
  386. css->odd = 0;
  387. len--;
  388. p++;
  389. }
  390. for ( ; len > 1; len -= 2, p +=2 ) {
  391. css->sum += (p[0] << 8) + p[1];
  392. if (css->sum > 0xFFFF) {
  393. css->sum += 1;
  394. css->sum &= 0xFFFF;
  395. }
  396. }
  397. if (len == 1){
  398. css->tmp = p[0];
  399. css->odd = 1;
  400. }
  401. }
  402. uint16_t
  403. csum_get(struct csum_state *css)
  404. {
  405. char pad = 0;
  406. csum_update(&pad, 1, css);
  407. return css->sum;
  408. }
  409. uint16_t
  410. csum_buf(uint8_t *p, uint32_t len)
  411. {
  412. struct csum_state css;
  413. csum_init(&css);
  414. csum_update(p, len, &css);
  415. return csum_get(&css);
  416. }
  417. /*
  418. * routines to write data to the output file
  419. */
  420. int
  421. write_out_data(FILE *outfile, uint8_t *data, size_t len,
  422. struct csum_state *css)
  423. {
  424. errno = 0;
  425. fwrite(data, len, 1, outfile);
  426. if (errno) {
  427. ERR("unable to write output file");
  428. return -1;
  429. }
  430. if (css) {
  431. csum_update(data, len, css);
  432. }
  433. return 0;
  434. }
  435. int
  436. write_out_padding(FILE *outfile, size_t len, uint8_t padc,
  437. struct csum_state *css)
  438. {
  439. uint8_t buf[512];
  440. size_t buflen = sizeof(buf);
  441. memset(buf, padc, buflen);
  442. while (len > 0) {
  443. if (len < buflen)
  444. buflen = len;
  445. if (write_out_data(outfile, buf, buflen, css))
  446. return -1;
  447. len -= buflen;
  448. }
  449. return 0;
  450. }
  451. int
  452. write_out_data_align(FILE *outfile, uint8_t *data, size_t len, size_t align,
  453. struct csum_state *css)
  454. {
  455. size_t padlen;
  456. int res;
  457. res = write_out_data(outfile, data, len, css);
  458. if (res)
  459. return res;
  460. padlen = ALIGN(len,align) - len;
  461. res = write_out_padding(outfile, padlen, 0xFF, css);
  462. return res;
  463. }
  464. int
  465. write_out_header(FILE *outfile, struct zyn_rombin_hdr *hdr)
  466. {
  467. struct zyn_rombin_hdr t;
  468. errno = 0;
  469. if (fseek(outfile, 0, SEEK_SET) != 0) {
  470. ERRS("fseek failed on output file");
  471. return -1;
  472. }
  473. /* setup temporary header fields */
  474. memset(&t, 0, sizeof(t));
  475. t.addr = HOST_TO_BE32(hdr->addr);
  476. memcpy(&t.sig, ROMBIN_SIGNATURE, ROMBIN_SIG_LEN);
  477. t.type = hdr->type;
  478. t.flags = hdr->flags;
  479. t.osize = HOST_TO_BE32(hdr->osize);
  480. t.csize = HOST_TO_BE32(hdr->csize);
  481. t.ocsum = HOST_TO_BE16(hdr->ocsum);
  482. t.ccsum = HOST_TO_BE16(hdr->ccsum);
  483. t.mmap_addr = HOST_TO_BE32(hdr->mmap_addr);
  484. DBG(2, "hdr.addr = 0x%08x", hdr->addr);
  485. DBG(2, "hdr.type = 0x%02x", hdr->type);
  486. DBG(2, "hdr.osize = 0x%08x", hdr->osize);
  487. DBG(2, "hdr.csize = 0x%08x", hdr->csize);
  488. DBG(2, "hdr.flags = 0x%02x", hdr->flags);
  489. DBG(2, "hdr.ocsum = 0x%04x", hdr->ocsum);
  490. DBG(2, "hdr.ccsum = 0x%04x", hdr->ccsum);
  491. DBG(2, "hdr.mmap_addr = 0x%08x", hdr->mmap_addr);
  492. return write_out_data(outfile, (uint8_t *)&t, sizeof(t), NULL);
  493. }
  494. int
  495. write_out_mmap(FILE *outfile, struct fw_mmap *mmap, struct csum_state *css)
  496. {
  497. struct zyn_mmt_hdr *mh;
  498. uint8_t buf[MMAP_DATA_SIZE];
  499. uint32_t user_size;
  500. char *data;
  501. int res;
  502. memset(buf, 0, sizeof(buf));
  503. mh = (struct zyn_mmt_hdr *)buf;
  504. /* TODO: needs to recreate the memory map too? */
  505. mh->count=0;
  506. /* Build user data section */
  507. data = buf+sizeof(*mh);
  508. data += sprintf(data, "Vendor 1 %d", board->vendor);
  509. *data++ = '\0';
  510. data += sprintf(data, "Model 1 %d", BE16_TO_HOST(board->model));
  511. *data++ = '\0';
  512. /* TODO: make hardware version configurable? */
  513. data += sprintf(data, "HwVerRange 2 %d %d", 0, 0);
  514. *data++ = '\0';
  515. user_size = (uint8_t *)data - buf;
  516. mh->user_start= HOST_TO_BE32(mmap->addr+sizeof(*mh));
  517. mh->user_end= HOST_TO_BE32(mmap->addr+user_size);
  518. mh->csum = HOST_TO_BE16(csum_buf(buf+sizeof(*mh), user_size));
  519. res = write_out_data(outfile, buf, sizeof(buf), css);
  520. return res;
  521. }
  522. int
  523. block_stat_file(struct fw_block *block)
  524. {
  525. struct stat st;
  526. int res;
  527. if (block->file_name == NULL)
  528. return 0;
  529. res = stat(block->file_name, &st);
  530. if (res){
  531. ERRS("stat failed on %s", block->file_name);
  532. return res;
  533. }
  534. block->file_size = st.st_size;
  535. return 0;
  536. }
  537. int
  538. read_magic(uint16_t *magic)
  539. {
  540. FILE *f;
  541. int res;
  542. errno = 0;
  543. f = fopen(bootext_block->file_name,"r");
  544. if (errno) {
  545. ERRS("unable to open file: %s", bootext_block->file_name);
  546. return -1;
  547. }
  548. errno = 0;
  549. fread(magic, 2, 1, f);
  550. if (errno != 0) {
  551. ERRS("unable to read from file: %s", bootext_block->file_name);
  552. res = -1;
  553. goto err;
  554. }
  555. res = 0;
  556. err:
  557. fclose(f);
  558. return res;
  559. }
  560. int
  561. write_out_file(FILE *outfile, char *name, size_t len, struct csum_state *css)
  562. {
  563. char buf[FILE_BUF_LEN];
  564. size_t buflen = sizeof(buf);
  565. FILE *f;
  566. int res;
  567. DBG(2, "writing out file, name=%s, len=%zu",
  568. name, len);
  569. errno = 0;
  570. f = fopen(name,"r");
  571. if (errno) {
  572. ERRS("unable to open file: %s", name);
  573. return -1;
  574. }
  575. while (len > 0) {
  576. if (len < buflen)
  577. buflen = len;
  578. /* read data from source file */
  579. errno = 0;
  580. fread(buf, buflen, 1, f);
  581. if (errno != 0) {
  582. ERRS("unable to read from file: %s",name);
  583. res = -1;
  584. break;
  585. }
  586. res = write_out_data(outfile, buf, buflen, css);
  587. if (res)
  588. break;
  589. len -= buflen;
  590. }
  591. fclose(f);
  592. return res;
  593. }
  594. int
  595. write_out_block(FILE *outfile, struct fw_block *block, struct csum_state *css)
  596. {
  597. int res;
  598. if (block == NULL)
  599. return 0;
  600. if (block->file_name == NULL)
  601. return 0;
  602. if (block->file_size == 0)
  603. return 0;
  604. res = write_out_file(outfile, block->file_name,
  605. block->file_size, css);
  606. return res;
  607. }
  608. int
  609. write_out_image(FILE *outfile)
  610. {
  611. struct fw_block *block;
  612. struct fw_mmap mmap;
  613. struct zyn_rombin_hdr hdr;
  614. struct csum_state css;
  615. int i, res;
  616. uint32_t offset;
  617. uint32_t padlen;
  618. uint16_t csum;
  619. uint16_t t;
  620. /* setup header fields */
  621. memset(&hdr, 0, sizeof(hdr));
  622. hdr.addr = board->code_start;
  623. hdr.type = OBJECT_TYPE_BOOTEXT;
  624. hdr.flags = ROMBIN_FLAG_OCSUM;
  625. offset = board->romio_offs;
  626. res = write_out_header(outfile, &hdr);
  627. if (res)
  628. return res;
  629. offset += sizeof(hdr);
  630. csum_init(&css);
  631. res = write_out_block(outfile, bootext_block, &css);
  632. if (res)
  633. return res;
  634. offset += bootext_block->file_size;
  635. if (offset > (board->romio_offs + board->bootext_size)) {
  636. ERR("bootext file '%s' is too big", bootext_block->file_name);
  637. return -1;
  638. }
  639. padlen = ALIGN(offset, MMAP_ALIGN) - offset;
  640. res = write_out_padding(outfile, padlen, 0xFF, &css);
  641. if (res)
  642. return res;
  643. offset += padlen;
  644. mmap.addr = board->flash_base + offset;
  645. res = write_out_mmap(outfile, &mmap, &css);
  646. if (res)
  647. return res;
  648. offset += MMAP_DATA_SIZE;
  649. if ((offset - board->romio_offs) < board->bootext_size) {
  650. padlen = board->romio_offs + board->bootext_size - offset;
  651. res = write_out_padding(outfile, padlen, 0xFF, &css);
  652. if (res)
  653. return res;
  654. offset += padlen;
  655. DBG(2, "bootext end at %08x", offset);
  656. }
  657. for (i = 0; i < num_blocks; i++) {
  658. block = &blocks[i];
  659. if (block->type == BLOCK_TYPE_BOOTEXT)
  660. continue;
  661. padlen = ALIGN(offset, block->align) - offset;
  662. res = write_out_padding(outfile, padlen, 0xFF, &css);
  663. if (res)
  664. return res;
  665. offset += padlen;
  666. res = write_out_block(outfile, block, &css);
  667. if (res)
  668. return res;
  669. offset += block->file_size;
  670. }
  671. padlen = ALIGN(offset, 4) - offset;
  672. res = write_out_padding(outfile, padlen, 0xFF, &css);
  673. if (res)
  674. return res;
  675. offset += padlen;
  676. csum = csum_get(&css);
  677. hdr.mmap_addr = mmap.addr;
  678. hdr.osize = 2;
  679. res = read_magic(&hdr.ocsum);
  680. if (res)
  681. return res;
  682. hdr.ocsum = BE16_TO_HOST(hdr.ocsum);
  683. if (csum <= hdr.ocsum)
  684. t = hdr.ocsum - csum;
  685. else
  686. t = hdr.ocsum - csum - 1;
  687. DBG(2, "ocsum=%04x, csum=%04x, fix=%04x", hdr.ocsum, csum, t);
  688. t = HOST_TO_BE16(t);
  689. res = write_out_data(outfile, (uint8_t *)&t, 2, NULL);
  690. if (res)
  691. return res;
  692. res = write_out_header(outfile, &hdr);
  693. return res;
  694. }
  695. struct board_info *
  696. find_board(char *name)
  697. {
  698. struct board_info *ret;
  699. struct board_info *board;
  700. ret = NULL;
  701. for (board = boards; board->name != NULL; board++){
  702. if (strcasecmp(name, board->name) == 0) {
  703. ret = board;
  704. break;
  705. }
  706. };
  707. return ret;
  708. }
  709. int
  710. parse_opt_board(char ch, char *arg)
  711. {
  712. DBG(1,"parsing board option: -%c %s", ch, arg);
  713. if (board != NULL) {
  714. ERR("only one board option allowed");
  715. return -1;
  716. }
  717. if (required_arg(ch, arg))
  718. return -1;
  719. board = find_board(arg);
  720. if (board == NULL){
  721. ERR("invalid/unknown board specified: %s", arg);
  722. return -1;
  723. }
  724. return 0;
  725. }
  726. int
  727. parse_opt_ofname(char ch, char *arg)
  728. {
  729. if (ofname != NULL) {
  730. ERR("only one output file allowed");
  731. return -1;
  732. }
  733. if (required_arg(ch, arg))
  734. return -1;
  735. ofname = arg;
  736. return 0;
  737. }
  738. int
  739. parse_opt_block(char ch, char *arg)
  740. {
  741. char buf[MAX_ARG_LEN];
  742. char *argv[MAX_ARG_COUNT];
  743. int argc;
  744. char *p;
  745. struct fw_block *block;
  746. int i;
  747. if ( num_blocks >= MAX_NUM_BLOCKS ) {
  748. ERR("too many blocks specified");
  749. return -1;
  750. }
  751. block = &blocks[num_blocks++];
  752. /* setup default field values */
  753. block->padc = 0xFF;
  754. switch(ch) {
  755. case 'b':
  756. if (bootext_block) {
  757. ERR("only one boot extension block allowed");
  758. break;
  759. }
  760. block->type = BLOCK_TYPE_BOOTEXT;
  761. bootext_block = block;
  762. break;
  763. case 'r':
  764. block->type = BLOCK_TYPE_RAW;
  765. break;
  766. }
  767. argc = parse_arg(arg, buf, argv);
  768. i = 0;
  769. p = argv[i++];
  770. if (is_empty_arg(p)) {
  771. ERR("no file specified in %s", arg);
  772. return -1;
  773. } else {
  774. block->file_name = strdup(p);
  775. if (block->file_name == NULL) {
  776. ERR("not enough memory");
  777. return -1;
  778. }
  779. }
  780. if (block->type == BLOCK_TYPE_BOOTEXT)
  781. return 0;
  782. p = argv[i++];
  783. if (!is_empty_arg(p)) {
  784. if (str2u32(p, &block->align) != 0) {
  785. ERR("invalid block align in %s", arg);
  786. return -1;
  787. }
  788. }
  789. return 0;
  790. }
  791. int
  792. calc_block_offsets(int type, uint32_t *offset)
  793. {
  794. struct fw_block *block;
  795. uint32_t next_offs;
  796. uint32_t avail;
  797. int i, res;
  798. DBG(1,"calculating block offsets, starting with %" PRIu32,
  799. *offset);
  800. res = 0;
  801. for (i = 0; i < num_blocks; i++) {
  802. block = &blocks[i];
  803. if (block->type != type)
  804. continue;
  805. next_offs = ALIGN(*offset, block->align);
  806. avail = board->flash_size - next_offs;
  807. if (block->file_size > avail) {
  808. ERR("file %s is too big, offset = %u, size=%u,"
  809. " avail = %u, align = %u", block->file_name,
  810. (unsigned)next_offs,
  811. (unsigned)block->file_size,
  812. (unsigned)avail,
  813. (unsigned)block->align);
  814. res = -1;
  815. break;
  816. }
  817. block->padlen = next_offs - *offset;
  818. *offset += block->file_size;
  819. }
  820. return res;
  821. }
  822. int
  823. process_blocks(void)
  824. {
  825. struct fw_block *block;
  826. uint32_t offset;
  827. int i;
  828. int res;
  829. /* collecting file stats */
  830. for (i = 0; i < num_blocks; i++) {
  831. block = &blocks[i];
  832. res = block_stat_file(block);
  833. if (res)
  834. return res;
  835. }
  836. offset = board->romio_offs + bootext_block->file_size;
  837. res = calc_block_offsets(BLOCK_TYPE_RAW, &offset);
  838. return res;
  839. }
  840. int
  841. main(int argc, char *argv[])
  842. {
  843. int optinvalid = 0; /* flag for invalid option */
  844. int c;
  845. int res = EXIT_FAILURE;
  846. FILE *outfile;
  847. progname=basename(argv[0]);
  848. opterr = 0; /* could not print standard getopt error messages */
  849. while ( 1 ) {
  850. optinvalid = 0;
  851. c = getopt(argc, argv, "b:B:ho:r:v");
  852. if (c == -1)
  853. break;
  854. switch (c) {
  855. case 'b':
  856. case 'r':
  857. optinvalid = parse_opt_block(c,optarg);
  858. break;
  859. case 'B':
  860. optinvalid = parse_opt_board(c,optarg);
  861. break;
  862. case 'o':
  863. optinvalid = parse_opt_ofname(c,optarg);
  864. break;
  865. case 'v':
  866. verblevel++;
  867. break;
  868. case 'h':
  869. usage(EXIT_SUCCESS);
  870. break;
  871. default:
  872. optinvalid = 1;
  873. break;
  874. }
  875. if (optinvalid != 0 ) {
  876. ERR("invalid option: -%c", optopt);
  877. goto out;
  878. }
  879. }
  880. if (board == NULL) {
  881. ERR("no board specified");
  882. goto out;
  883. }
  884. if (ofname == NULL) {
  885. ERR("no output file specified");
  886. goto out;
  887. }
  888. if (optind < argc) {
  889. ERR("invalid option: %s", argv[optind]);
  890. goto out;
  891. }
  892. if (process_blocks() != 0) {
  893. goto out;
  894. }
  895. outfile = fopen(ofname, "w");
  896. if (outfile == NULL) {
  897. ERRS("could not open \"%s\" for writing", ofname);
  898. goto out;
  899. }
  900. if (write_out_image(outfile) != 0)
  901. goto out_flush;
  902. DBG(1,"Image file %s completed.", ofname);
  903. res = EXIT_SUCCESS;
  904. out_flush:
  905. fflush(outfile);
  906. fclose(outfile);
  907. if (res != EXIT_SUCCESS) {
  908. unlink(ofname);
  909. }
  910. out:
  911. return res;
  912. }