mkzynfw.c 22 KB

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