mkmylofw.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /*
  2. * Copyright (C) 2006-2008 Gabor Juhos <juhosg@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the
  16. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <unistd.h> /* for unlink() */
  25. #include <libgen.h>
  26. #include <getopt.h> /* for getopt() */
  27. #include <stdarg.h>
  28. #include <errno.h>
  29. #include <sys/stat.h>
  30. #include <endian.h> /* for __BYTE_ORDER */
  31. #if defined(__CYGWIN__)
  32. # include <byteswap.h>
  33. #endif
  34. #if (__BYTE_ORDER == __LITTLE_ENDIAN)
  35. # define HOST_TO_LE16(x) (x)
  36. # define HOST_TO_LE32(x) (x)
  37. #else
  38. # define HOST_TO_LE16(x) bswap_16(x)
  39. # define HOST_TO_LE32(x) bswap_32(x)
  40. #endif
  41. #include "myloader.h"
  42. #define MAX_FW_BLOCKS 32
  43. #define MAX_ARG_COUNT 32
  44. #define MAX_ARG_LEN 1024
  45. #define FILE_BUF_LEN (16*1024)
  46. #define PART_NAME_LEN 32
  47. struct fw_block {
  48. uint32_t addr;
  49. uint32_t blocklen; /* length of the block */
  50. uint32_t flags;
  51. char *name; /* name of the file */
  52. uint32_t size; /* length of the file */
  53. uint32_t crc; /* crc value of the file */
  54. };
  55. struct fw_part {
  56. struct mylo_partition mylo;
  57. char name[PART_NAME_LEN];
  58. };
  59. #define BLOCK_FLAG_HAVEHDR 0x0001
  60. struct cpx_board {
  61. char *model; /* model number*/
  62. char *name; /* model name*/
  63. char *desc; /* description */
  64. uint16_t vid; /* vendor id */
  65. uint16_t did; /* device id */
  66. uint16_t svid; /* sub vendor id */
  67. uint16_t sdid; /* sub device id */
  68. uint32_t flash_size; /* size of flash */
  69. uint32_t part_offset; /* offset of the partition_table */
  70. uint32_t part_size; /* size of the partition_table */
  71. };
  72. #define BOARD(_vid, _did, _svid, _sdid, _flash, _mod, _name, _desc, _po, _ps) { \
  73. .model = _mod, .name = _name, .desc = _desc, \
  74. .vid = _vid, .did = _did, .svid = _svid, .sdid = _sdid, \
  75. .flash_size = (_flash << 20), \
  76. .part_offset = _po, .part_size = _ps }
  77. #define CPX_BOARD(_did, _flash, _mod, _name, _desc, _po, _ps) \
  78. BOARD(VENID_COMPEX, _did, VENID_COMPEX, _did, _flash, _mod, _name, _desc, _po, _ps)
  79. #define CPX_BOARD_ADM(_did, _flash, _mod, _name, _desc) \
  80. CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
  81. #define CPX_BOARD_AR71XX(_did, _flash, _mod, _name, _desc) \
  82. CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x20000, 0x8000)
  83. #define CPX_BOARD_AR23XX(_did, _flash, _mod, _name, _desc) \
  84. CPX_BOARD(_did, _flash, _mod, _name, _desc, 0x10000, 0x10000)
  85. #define ALIGN(x,y) ((x)+((y)-1)) & ~((y)-1)
  86. char *progname;
  87. char *ofname = NULL;
  88. uint32_t flash_size = 0;
  89. int fw_num_partitions = 0;
  90. int fw_num_blocks = 0;
  91. int verblevel = 0;
  92. struct mylo_fw_header fw_header;
  93. struct fw_part fw_parts[MYLO_MAX_PARTITIONS];
  94. struct fw_block fw_blocks[MAX_FW_BLOCKS];
  95. struct cpx_board *board;
  96. struct cpx_board boards[] = {
  97. CPX_BOARD_ADM(DEVID_COMPEX_NP18A, 4,
  98. "NP18A", "Compex NetPassage 18A",
  99. "Dualband Wireless A+G Internet Gateway"),
  100. CPX_BOARD_ADM(DEVID_COMPEX_NP26G8M, 2,
  101. "NP26G8M", "Compex NetPassage 26G (8M)",
  102. "Wireless-G Broadband Multimedia Gateway"),
  103. CPX_BOARD_ADM(DEVID_COMPEX_NP26G16M, 4,
  104. "NP26G16M", "Compex NetPassage 26G (16M)",
  105. "Wireless-G Broadband Multimedia Gateway"),
  106. CPX_BOARD_ADM(DEVID_COMPEX_NP27G, 4,
  107. "NP27G", "Compex NetPassage 27G",
  108. "Wireless-G 54Mbps eXtended Range Router"),
  109. CPX_BOARD_ADM(DEVID_COMPEX_NP28G, 4,
  110. "NP28G", "Compex NetPassage 28G",
  111. "Wireless 108Mbps Super-G XR Multimedia Router with 4 USB Ports"),
  112. CPX_BOARD_ADM(DEVID_COMPEX_NP28GHS, 4,
  113. "NP28GHS", "Compex NetPassage 28G (HotSpot)",
  114. "HotSpot Solution"),
  115. CPX_BOARD_ADM(DEVID_COMPEX_WP18, 4,
  116. "WP18", "Compex NetPassage WP18",
  117. "Wireless-G 54Mbps A+G Dualband Access Point"),
  118. CPX_BOARD_ADM(DEVID_COMPEX_WP54G, 4,
  119. "WP54G", "Compex WP54G",
  120. "Wireless-G 54Mbps XR Access Point"),
  121. CPX_BOARD_ADM(DEVID_COMPEX_WP54Gv1C, 2,
  122. "WP54Gv1C", "Compex WP54G rev.1C",
  123. "Wireless-G 54Mbps XR Access Point"),
  124. CPX_BOARD_ADM(DEVID_COMPEX_WP54AG, 4,
  125. "WP54AG", "Compex WP54AG",
  126. "Wireless-AG 54Mbps XR Access Point"),
  127. CPX_BOARD_ADM(DEVID_COMPEX_WPP54G, 4,
  128. "WPP54G", "Compex WPP54G",
  129. "Outdoor Access Point"),
  130. CPX_BOARD_ADM(DEVID_COMPEX_WPP54AG, 4,
  131. "WPP54AG", "Compex WPP54AG",
  132. "Outdoor Access Point"),
  133. CPX_BOARD_AR71XX(DEVID_COMPEX_WP543, 2,
  134. "WP543", "Compex WP543",
  135. "BareBoard"),
  136. CPX_BOARD_AR71XX(DEVID_COMPEX_WPE72, 8,
  137. "WPE72", "Compex WPE72",
  138. "BareBoard"),
  139. CPX_BOARD_AR23XX(DEVID_COMPEX_NP25G, 4,
  140. "NP25G", "Compex NetPassage 25G",
  141. "Wireless 54Mbps XR Router"),
  142. CPX_BOARD_AR23XX(DEVID_COMPEX_WPE53G, 4,
  143. "WPE53G", "Compex NetPassage 25G",
  144. "Wireless 54Mbps XR Access Point"),
  145. {.model = NULL}
  146. };
  147. void
  148. errmsgv(int syserr, const char *fmt, va_list arg_ptr)
  149. {
  150. int save = errno;
  151. fflush(0);
  152. fprintf(stderr, "[%s] Error: ", progname);
  153. vfprintf(stderr, fmt, arg_ptr);
  154. if (syserr != 0) {
  155. fprintf(stderr, ": %s", strerror(save));
  156. }
  157. fprintf(stderr, "\n");
  158. }
  159. void
  160. errmsg(int syserr, const char *fmt, ...)
  161. {
  162. va_list arg_ptr;
  163. va_start(arg_ptr, fmt);
  164. errmsgv(syserr, fmt, arg_ptr);
  165. va_end(arg_ptr);
  166. }
  167. void
  168. dbgmsg(int level, const char *fmt, ...)
  169. {
  170. va_list arg_ptr;
  171. if (verblevel >= level) {
  172. fflush(0);
  173. va_start(arg_ptr, fmt);
  174. vfprintf(stderr, fmt, arg_ptr);
  175. fprintf(stderr, "\n");
  176. va_end(arg_ptr);
  177. }
  178. }
  179. void
  180. usage(int status)
  181. {
  182. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  183. struct cpx_board *board;
  184. fprintf(stream, "Usage: %s [OPTION...] <file>\n", progname);
  185. fprintf(stream,
  186. "\n"
  187. " <file> write output to the <file>\n"
  188. "\n"
  189. "Options:\n"
  190. " -B <board> create firmware for the board specified with <board>.\n"
  191. " This option set vendor id, device id, subvendor id,\n"
  192. " subdevice id, and flash size options to the right value.\n"
  193. " valid <board> values:\n");
  194. for (board = boards; board->model != NULL; board++){
  195. fprintf(stream,
  196. " %-12s: %s\n",
  197. board->model, board->name);
  198. };
  199. fprintf(stream,
  200. " -i <vid>:<did>[:<svid>[:<sdid>]]\n"
  201. " create firmware for board with vendor id <vid>, device\n"
  202. " id <did>, subvendor id <svid> and subdevice id <sdid>.\n"
  203. " -r <rev> set board revision to <rev>.\n"
  204. " -s <size> set flash size to <size>\n"
  205. " -b <addr>:<len>[:[<flags>]:<file>]\n"
  206. " define block at <addr> with length of <len>.\n"
  207. " valid <flag> values:\n"
  208. " h : add crc header before the file data.\n"
  209. " -p <addr>:<len>[:<flags>[:<param>[:<name>[:<file>]]]]\n"
  210. " add partition at <addr>, with size of <len> to the\n"
  211. " partition table, set partition name to <name>, partition \n"
  212. " flags to <flags> and partition parameter to <param>.\n"
  213. " If the <file> is specified content of the file will be \n"
  214. " added to the firmware image.\n"
  215. " valid <flag> values:\n"
  216. " a: this is the active partition. The bootloader loads\n"
  217. " the firmware from this partition.\n"
  218. " h: the partition data have a header.\n"
  219. " l: the partition data uses LZMA compression.\n"
  220. " p: the bootloader loads data from this partition to\n"
  221. " the RAM before decompress it.\n"
  222. " -h show this screen\n"
  223. );
  224. exit(status);
  225. }
  226. /*
  227. * Code to compute the CRC-32 table. Borrowed from
  228. * gzip-1.0.3/makecrc.c.
  229. */
  230. static uint32_t crc_32_tab[256];
  231. void
  232. init_crc_table(void)
  233. {
  234. /* Not copyrighted 1990 Mark Adler */
  235. uint32_t c; /* crc shift register */
  236. uint32_t e; /* polynomial exclusive-or pattern */
  237. int i; /* counter for all possible eight bit values */
  238. int k; /* byte being shifted into crc apparatus */
  239. /* terms of polynomial defining this crc (except x^32): */
  240. static const int p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
  241. /* Make exclusive-or pattern from polynomial */
  242. e = 0;
  243. for (i = 0; i < sizeof(p)/sizeof(int); i++)
  244. e |= 1L << (31 - p[i]);
  245. crc_32_tab[0] = 0;
  246. for (i = 1; i < 256; i++) {
  247. c = 0;
  248. for (k = i | 256; k != 1; k >>= 1) {
  249. c = c & 1 ? (c >> 1) ^ e : c >> 1;
  250. if (k & 1)
  251. c ^= e;
  252. }
  253. crc_32_tab[i] = c;
  254. }
  255. }
  256. void
  257. update_crc(uint8_t *p, uint32_t len, uint32_t *crc)
  258. {
  259. uint32_t t;
  260. t = *crc ^ 0xFFFFFFFFUL;
  261. while (len--) {
  262. t = crc_32_tab[(t ^ *p++) & 0xff] ^ (t >> 8);
  263. }
  264. *crc = t ^ 0xFFFFFFFFUL;
  265. }
  266. uint32_t
  267. get_crc(uint8_t *p, uint32_t len)
  268. {
  269. uint32_t crc;
  270. crc = 0;
  271. update_crc(p ,len , &crc);
  272. return crc;
  273. }
  274. int
  275. str2u32(char *arg, uint32_t *val)
  276. {
  277. char *err = NULL;
  278. uint32_t t;
  279. errno=0;
  280. t = strtoul(arg, &err, 0);
  281. if (errno || (err==arg) || ((err != NULL) && *err)) {
  282. return -1;
  283. }
  284. *val = t;
  285. return 0;
  286. }
  287. int
  288. str2u16(char *arg, uint16_t *val)
  289. {
  290. char *err = NULL;
  291. uint32_t t;
  292. errno=0;
  293. t = strtoul(arg, &err, 0);
  294. if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
  295. return -1;
  296. }
  297. *val = t & 0xFFFF;
  298. return 0;
  299. }
  300. struct cpx_board *
  301. find_board(char *model){
  302. struct cpx_board *board;
  303. struct cpx_board *tmp;
  304. board = NULL;
  305. for (tmp = boards; tmp->model != NULL; tmp++){
  306. if (strcasecmp(model, tmp->model) == 0) {
  307. board = tmp;
  308. break;
  309. }
  310. };
  311. return board;
  312. }
  313. int
  314. get_file_crc(struct fw_block *ff)
  315. {
  316. FILE *f;
  317. uint8_t buf[FILE_BUF_LEN];
  318. uint32_t readlen = sizeof(buf);
  319. int res = -1;
  320. size_t len;
  321. if ((ff->flags & BLOCK_FLAG_HAVEHDR) == 0) {
  322. res = 0;
  323. goto out;
  324. }
  325. errno = 0;
  326. f = fopen(ff->name,"r");
  327. if (errno) {
  328. errmsg(1,"unable to open file %s", ff->name);
  329. goto out;
  330. }
  331. ff->crc = 0;
  332. len = ff->size;
  333. while (len > 0) {
  334. if (len < readlen)
  335. readlen = len;
  336. errno = 0;
  337. fread(buf, readlen, 1, f);
  338. if (errno) {
  339. errmsg(1,"unable to read from file %s", ff->name);
  340. goto out_close;
  341. }
  342. update_crc(buf, readlen, &ff->crc);
  343. len -= readlen;
  344. }
  345. res = 0;
  346. out_close:
  347. fclose(f);
  348. out:
  349. return res;
  350. }
  351. int
  352. process_files(void)
  353. {
  354. struct fw_block *b;
  355. struct stat st;
  356. int i;
  357. for (i = 0; i < fw_num_blocks; i++) {
  358. b = &fw_blocks[i];
  359. if ((b->addr + b->blocklen) > flash_size) {
  360. errmsg(0, "block at 0x%08X is too big", b->addr);
  361. return -1;
  362. }
  363. if (b->name == NULL)
  364. continue;
  365. if (stat(b->name, &st) < 0) {
  366. errmsg(0, "stat failed on %s",b->name);
  367. return -1;
  368. }
  369. if (b->blocklen == 0) {
  370. b->blocklen = flash_size - b->addr;
  371. }
  372. if (st.st_size > b->blocklen) {
  373. errmsg(0,"file %s is too big",b->name);
  374. return -1;
  375. }
  376. b->size = st.st_size;
  377. }
  378. return 0;
  379. }
  380. int
  381. process_partitions(void)
  382. {
  383. struct mylo_partition *part;
  384. int i;
  385. for (i = 0; i < fw_num_partitions; i++) {
  386. part = &fw_parts[i].mylo;
  387. if (part->addr > flash_size) {
  388. errmsg(0, "invalid partition at 0x%08X", part->addr);
  389. return -1;
  390. }
  391. if ((part->addr + part->size) > flash_size) {
  392. errmsg(0, "partition at 0x%08X is too big", part->addr);
  393. return -1;
  394. }
  395. }
  396. return 0;
  397. }
  398. /*
  399. * routines to write data to the output file
  400. */
  401. int
  402. write_out_data(FILE *outfile, uint8_t *data, size_t len, uint32_t *crc)
  403. {
  404. errno = 0;
  405. fwrite(data, len, 1, outfile);
  406. if (errno) {
  407. errmsg(1,"unable to write output file");
  408. return -1;
  409. }
  410. if (crc) {
  411. update_crc(data, len, crc);
  412. }
  413. return 0;
  414. }
  415. int
  416. write_out_desc(FILE *outfile, struct mylo_fw_blockdesc *desc, uint32_t *crc)
  417. {
  418. return write_out_data(outfile, (uint8_t *)desc,
  419. sizeof(*desc), crc);
  420. }
  421. int
  422. write_out_padding(FILE *outfile, size_t len, uint8_t padc, uint32_t *crc)
  423. {
  424. uint8_t buff[512];
  425. size_t buflen = sizeof(buff);
  426. memset(buff, padc, buflen);
  427. while (len > 0) {
  428. if (len < buflen)
  429. buflen = len;
  430. if (write_out_data(outfile, buff, buflen, crc))
  431. return -1;
  432. len -= buflen;
  433. }
  434. return 0;
  435. }
  436. int
  437. write_out_file(FILE *outfile, struct fw_block *block, uint32_t *crc)
  438. {
  439. char buff[FILE_BUF_LEN];
  440. size_t buflen = sizeof(buff);
  441. FILE *f;
  442. size_t len;
  443. errno = 0;
  444. if (block->name == NULL) {
  445. return 0;
  446. }
  447. if ((block->flags & BLOCK_FLAG_HAVEHDR) != 0) {
  448. struct mylo_partition_header ph;
  449. if (get_file_crc(block) != 0)
  450. return -1;
  451. ph.crc = HOST_TO_LE32(block->crc);
  452. ph.len = HOST_TO_LE32(block->size);
  453. if (write_out_data(outfile, (uint8_t *)&ph, sizeof(ph), crc) != 0)
  454. return -1;
  455. }
  456. f = fopen(block->name,"r");
  457. if (errno) {
  458. errmsg(1,"unable to open file: %s", block->name);
  459. return -1;
  460. }
  461. len = block->size;
  462. while (len > 0) {
  463. if (len < buflen)
  464. buflen = len;
  465. /* read data from source file */
  466. errno = 0;
  467. fread(buff, buflen, 1, f);
  468. if (errno != 0) {
  469. errmsg(1,"unable to read from file: %s",block->name);
  470. return -1;
  471. }
  472. if (write_out_data(outfile, buff, buflen, crc) != 0)
  473. return -1;
  474. len -= buflen;
  475. }
  476. fclose(f);
  477. /* align next block on a 4 byte boundary */
  478. len = ALIGN(len,4) - block->size;
  479. if (write_out_padding(outfile, len, 0xFF, crc))
  480. return -1;
  481. dbgmsg(1,"file %s written out", block->name);
  482. return 0;
  483. }
  484. int
  485. write_out_header(FILE *outfile, uint32_t *crc)
  486. {
  487. struct mylo_fw_header hdr;
  488. memset(&hdr, 0, sizeof(hdr));
  489. hdr.magic = HOST_TO_LE32(MYLO_MAGIC_FIRMWARE);
  490. hdr.crc = HOST_TO_LE32(fw_header.crc);
  491. hdr.vid = HOST_TO_LE16(fw_header.vid);
  492. hdr.did = HOST_TO_LE16(fw_header.did);
  493. hdr.svid = HOST_TO_LE16(fw_header.svid);
  494. hdr.sdid = HOST_TO_LE16(fw_header.sdid);
  495. hdr.rev = HOST_TO_LE32(fw_header.rev);
  496. hdr.fwhi = HOST_TO_LE32(fw_header.fwhi);
  497. hdr.fwlo = HOST_TO_LE32(fw_header.fwlo);
  498. hdr.flags = HOST_TO_LE32(fw_header.flags);
  499. if (fseek(outfile, 0, SEEK_SET) != 0) {
  500. errmsg(1,"fseek failed on output file");
  501. return -1;
  502. }
  503. return write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr), crc);
  504. }
  505. int
  506. write_out_partitions(FILE *outfile, uint32_t *crc)
  507. {
  508. struct mylo_partition_table p;
  509. char part_names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
  510. int ret;
  511. int i;
  512. if (fw_num_partitions == 0)
  513. return 0;
  514. memset(&p, 0, sizeof(p));
  515. memset(part_names, 0, sizeof(part_names));
  516. p.magic = HOST_TO_LE32(MYLO_MAGIC_PARTITIONS);
  517. for (i = 0; i < fw_num_partitions; i++) {
  518. struct mylo_partition *mp;
  519. struct fw_part *fp;
  520. mp = &p.partitions[i];
  521. fp = &fw_parts[i];
  522. mp->flags = HOST_TO_LE16(fp->mylo.flags);
  523. mp->type = HOST_TO_LE16(PARTITION_TYPE_USED);
  524. mp->addr = HOST_TO_LE32(fp->mylo.addr);
  525. mp->size = HOST_TO_LE32(fp->mylo.size);
  526. mp->param = HOST_TO_LE32(fp->mylo.param);
  527. memcpy(part_names[i], fp->name, PART_NAME_LEN);
  528. }
  529. ret = write_out_data(outfile, (uint8_t *)&p, sizeof(p), crc);
  530. if (ret)
  531. return ret;
  532. ret = write_out_data(outfile, (uint8_t *)part_names, sizeof(part_names),
  533. crc);
  534. return ret;
  535. }
  536. int
  537. write_out_blocks(FILE *outfile, uint32_t *crc)
  538. {
  539. struct mylo_fw_blockdesc desc;
  540. struct fw_block *b;
  541. uint32_t dlen;
  542. int i;
  543. /*
  544. * if at least one partition specified, write out block descriptor
  545. * for the partition table
  546. */
  547. if (fw_num_partitions > 0) {
  548. desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
  549. desc.addr = HOST_TO_LE32(board->part_offset);
  550. desc.dlen = HOST_TO_LE32(sizeof(struct mylo_partition_table) +
  551. (MYLO_MAX_PARTITIONS * PART_NAME_LEN));
  552. desc.blen = HOST_TO_LE32(board->part_size);
  553. if (write_out_desc(outfile, &desc, crc) != 0)
  554. return -1;
  555. }
  556. /*
  557. * write out block descriptors for each files
  558. */
  559. for (i = 0; i < fw_num_blocks; i++) {
  560. b = &fw_blocks[i];
  561. /* detect block size */
  562. dlen = b->size;
  563. if ((b->flags & BLOCK_FLAG_HAVEHDR) != 0) {
  564. dlen += sizeof(struct mylo_partition_header);
  565. }
  566. /* round up to 4 bytes */
  567. dlen = ALIGN(dlen, 4);
  568. /* setup the descriptor */
  569. desc.type = HOST_TO_LE32(FW_DESC_TYPE_USED);
  570. desc.addr = HOST_TO_LE32(b->addr);
  571. desc.dlen = HOST_TO_LE32(dlen);
  572. desc.blen = HOST_TO_LE32(b->blocklen);
  573. if (write_out_desc(outfile, &desc, crc) != 0)
  574. return -1;
  575. }
  576. /*
  577. * write out the null block descriptor
  578. */
  579. memset(&desc, 0, sizeof(desc));
  580. if (write_out_desc(outfile, &desc, crc) != 0)
  581. return -1;
  582. if (write_out_partitions(outfile, crc) != 0)
  583. return -1;
  584. /*
  585. * write out data for each blocks
  586. */
  587. for (i = 0; i < fw_num_blocks; i++) {
  588. b = &fw_blocks[i];
  589. if (write_out_file(outfile, b, crc) != 0)
  590. return -1;
  591. }
  592. return 0;
  593. }
  594. /*
  595. * argument parsing
  596. */
  597. int
  598. parse_arg(char *arg, char *buf, char *argv[])
  599. {
  600. int res = 0;
  601. size_t argl;
  602. char *tok;
  603. char **ap = &buf;
  604. int i;
  605. if ((arg == NULL)) {
  606. /* invalid argument string */
  607. return -1;
  608. }
  609. argl = strlen(arg);
  610. if (argl == 0) {
  611. /* no arguments */
  612. return res;
  613. }
  614. if (argl >= MAX_ARG_LEN) {
  615. /* argument is too long */
  616. argl = MAX_ARG_LEN-1;
  617. }
  618. memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
  619. memcpy(buf, arg, argl);
  620. buf[argl] = '\0';
  621. for (i = 0; i < MAX_ARG_COUNT; i++) {
  622. tok = strsep(ap, ":");
  623. if (tok == NULL) {
  624. break;
  625. }
  626. #if 0
  627. else if (tok[0] == '\0') {
  628. break;
  629. }
  630. #endif
  631. argv[i] = tok;
  632. res++;
  633. }
  634. return res;
  635. }
  636. int
  637. required_arg(char c, char *arg)
  638. {
  639. if ((optarg != NULL) && (*arg == '-')){
  640. errmsg(0,"option %c requires an argument\n", c);
  641. return -1;
  642. }
  643. return 0;
  644. }
  645. int
  646. is_empty_arg(char *arg)
  647. {
  648. int ret = 1;
  649. if (arg != NULL) {
  650. if (*arg) ret = 0;
  651. };
  652. return ret;
  653. }
  654. int
  655. parse_opt_flags(char ch, char *arg)
  656. {
  657. if (required_arg(ch, arg)) {
  658. goto err_out;
  659. }
  660. if (str2u32(arg, &fw_header.flags) != 0) {
  661. errmsg(0,"invalid firmware flags: %s", arg);
  662. goto err_out;
  663. }
  664. dbgmsg(1, "firmware flags set to %X bytes", fw_header.flags);
  665. return 0;
  666. err_out:
  667. return -1;
  668. }
  669. int
  670. parse_opt_size(char ch, char *arg)
  671. {
  672. if (required_arg(ch, arg)) {
  673. goto err_out;
  674. }
  675. if (str2u32(arg, &flash_size) != 0) {
  676. errmsg(0,"invalid flash size: %s", arg);
  677. goto err_out;
  678. }
  679. dbgmsg(1, "flash size set to %d bytes", flash_size);
  680. return 0;
  681. err_out:
  682. return -1;
  683. }
  684. int
  685. parse_opt_id(char ch, char *arg)
  686. {
  687. char buf[MAX_ARG_LEN];
  688. char *argv[MAX_ARG_COUNT];
  689. int argc;
  690. char *p;
  691. if (required_arg(ch, arg)) {
  692. goto err_out;
  693. }
  694. argc = parse_arg(arg, buf, argv);
  695. /* processing vendor ID*/
  696. p = argv[0];
  697. if (is_empty_arg(p)) {
  698. errmsg(0,"vendor id is missing from -%c %s",ch, arg);
  699. goto err_out;
  700. } else if (str2u16(p, &fw_header.vid) != 0) {
  701. errmsg(0,"invalid vendor id: %s", p);
  702. goto err_out;
  703. }
  704. dbgmsg(1, "vendor id is set to 0x%04X", fw_header.vid);
  705. /* processing device ID*/
  706. p = argv[1];
  707. if (is_empty_arg(p)) {
  708. errmsg(0,"device id is missing from -%c %s",ch, arg);
  709. goto err_out;
  710. } else if (str2u16(p, &fw_header.did) != 0) {
  711. errmsg(0,"invalid device id: %s", p);
  712. goto err_out;
  713. }
  714. dbgmsg(1, "device id is set to 0x%04X", fw_header.did);
  715. /* processing sub vendor ID*/
  716. p = argv[2];
  717. if (is_empty_arg(p)) {
  718. fw_header.svid = fw_header.vid;
  719. } else if (str2u16(p, &fw_header.svid) != 0) {
  720. errmsg(0,"invalid sub vendor id: %s", p);
  721. goto err_out;
  722. }
  723. dbgmsg(1, "sub vendor id is set to 0x%04X", fw_header.svid);
  724. /* processing device ID*/
  725. p = argv[3];
  726. if (is_empty_arg(p)) {
  727. fw_header.sdid = fw_header.did;
  728. } else if (str2u16(p, &fw_header.sdid) != 0) {
  729. errmsg(0,"invalid sub device id: %s", p);
  730. goto err_out;
  731. }
  732. dbgmsg(1, "sub device id is set to 0x%04X", fw_header.sdid);
  733. /* processing revision */
  734. p = argv[4];
  735. if (is_empty_arg(p)) {
  736. fw_header.rev = 0;
  737. } else if (str2u32(arg, &fw_header.rev) != 0) {
  738. errmsg(0,"invalid revision number: %s", p);
  739. goto err_out;
  740. }
  741. dbgmsg(1, "board revision is set to 0x%08X", fw_header.rev);
  742. return 0;
  743. err_out:
  744. return -1;
  745. }
  746. int
  747. parse_opt_block(char ch, char *arg)
  748. {
  749. char buf[MAX_ARG_LEN];
  750. char *argv[MAX_ARG_COUNT];
  751. int argc;
  752. struct fw_block *b;
  753. char *p;
  754. if (required_arg(ch, arg)) {
  755. goto err_out;
  756. }
  757. if (fw_num_blocks >= MAX_FW_BLOCKS) {
  758. errmsg(0,"too many blocks specified");
  759. goto err_out;
  760. }
  761. argc = parse_arg(arg, buf, argv);
  762. dbgmsg(1,"processing block option %s, count %d", arg, argc);
  763. b = &fw_blocks[fw_num_blocks++];
  764. /* processing block address */
  765. p = argv[0];
  766. if (is_empty_arg(p)) {
  767. errmsg(0,"no block address specified in %s", arg);
  768. goto err_out;
  769. } else if (str2u32(p, &b->addr) != 0) {
  770. errmsg(0,"invalid block address: %s", p);
  771. goto err_out;
  772. }
  773. /* processing block length */
  774. p = argv[1];
  775. if (is_empty_arg(p)) {
  776. errmsg(0,"no block length specified in %s", arg);
  777. goto err_out;
  778. } else if (str2u32(p, &b->blocklen) != 0) {
  779. errmsg(0,"invalid block length: %s", p);
  780. goto err_out;
  781. }
  782. if (argc < 3) {
  783. dbgmsg(1,"empty block %s", arg);
  784. goto success;
  785. }
  786. /* processing flags */
  787. p = argv[2];
  788. if (is_empty_arg(p) == 0) {
  789. for ( ; *p != '\0'; p++) {
  790. switch (*p) {
  791. case 'h':
  792. b->flags |= BLOCK_FLAG_HAVEHDR;
  793. break;
  794. default:
  795. errmsg(0, "invalid block flag \"%c\"", *p);
  796. goto err_out;
  797. }
  798. }
  799. }
  800. /* processing file name */
  801. p = argv[3];
  802. if (is_empty_arg(p)) {
  803. errmsg(0,"file name missing in %s", arg);
  804. goto err_out;
  805. }
  806. b->name = strdup(p);
  807. if (b->name == NULL) {
  808. errmsg(0,"not enough memory");
  809. goto err_out;
  810. }
  811. success:
  812. return 0;
  813. err_out:
  814. return -1;
  815. }
  816. int
  817. parse_opt_partition(char ch, char *arg)
  818. {
  819. char buf[MAX_ARG_LEN];
  820. char *argv[MAX_ARG_COUNT];
  821. int argc;
  822. char *p;
  823. struct mylo_partition *part;
  824. struct fw_part *fp;
  825. if (required_arg(ch, arg)) {
  826. goto err_out;
  827. }
  828. if (fw_num_partitions >= MYLO_MAX_PARTITIONS) {
  829. errmsg(0, "too many partitions specified");
  830. goto err_out;
  831. }
  832. fp = &fw_parts[fw_num_partitions++];
  833. part = &fp->mylo;
  834. argc = parse_arg(arg, buf, argv);
  835. /* processing partition address */
  836. p = argv[0];
  837. if (is_empty_arg(p)) {
  838. errmsg(0,"partition address missing in -%c %s",ch, arg);
  839. goto err_out;
  840. } else if (str2u32(p, &part->addr) != 0) {
  841. errmsg(0,"invalid partition address: %s", p);
  842. goto err_out;
  843. }
  844. /* processing partition size */
  845. p = argv[1];
  846. if (is_empty_arg(p)) {
  847. errmsg(0,"partition size missing in -%c %s",ch, arg);
  848. goto err_out;
  849. } else if (str2u32(p, &part->size) != 0) {
  850. errmsg(0,"invalid partition size: %s", p);
  851. goto err_out;
  852. }
  853. /* processing partition flags */
  854. p = argv[2];
  855. if (is_empty_arg(p) == 0) {
  856. for ( ; *p != '\0'; p++) {
  857. switch (*p) {
  858. case 'a':
  859. part->flags |= PARTITION_FLAG_ACTIVE;
  860. break;
  861. case 'p':
  862. part->flags |= PARTITION_FLAG_PRELOAD;
  863. break;
  864. case 'l':
  865. part->flags |= PARTITION_FLAG_LZMA;
  866. break;
  867. case 'h':
  868. part->flags |= PARTITION_FLAG_HAVEHDR;
  869. break;
  870. default:
  871. errmsg(0, "invalid partition flag \"%c\"", *p);
  872. goto err_out;
  873. }
  874. }
  875. }
  876. /* processing partition parameter */
  877. p = argv[3];
  878. if (is_empty_arg(p)) {
  879. /* set default partition parameter */
  880. part->param = 0;
  881. } else if (str2u32(p, &part->param) != 0) {
  882. errmsg(0,"invalid partition parameter: %s", p);
  883. goto err_out;
  884. }
  885. p = argv[4];
  886. if (is_empty_arg(p)) {
  887. /* set default partition parameter */
  888. fp->name[0] = '\0';
  889. } else {
  890. strncpy(fp->name, p, PART_NAME_LEN);
  891. }
  892. #if 1
  893. if (part->size == 0) {
  894. part->size = flash_size - part->addr;
  895. }
  896. /* processing file parameter */
  897. p = argv[5];
  898. if (is_empty_arg(p) == 0) {
  899. struct fw_block *b;
  900. if (fw_num_blocks == MAX_FW_BLOCKS) {
  901. errmsg(0,"too many blocks specified", p);
  902. goto err_out;
  903. }
  904. b = &fw_blocks[fw_num_blocks++];
  905. b->name = strdup(p);
  906. b->addr = part->addr;
  907. b->blocklen = part->size;
  908. if (part->flags & PARTITION_FLAG_HAVEHDR) {
  909. b->flags |= BLOCK_FLAG_HAVEHDR;
  910. }
  911. }
  912. #endif
  913. return 0;
  914. err_out:
  915. return -1;
  916. }
  917. int
  918. parse_opt_board(char ch, char *arg)
  919. {
  920. if (required_arg(ch, arg)) {
  921. goto err_out;
  922. }
  923. board = find_board(arg);
  924. if (board == NULL){
  925. errmsg(0,"invalid/unknown board specified: %s", arg);
  926. goto err_out;
  927. }
  928. fw_header.vid = board->vid;
  929. fw_header.did = board->did;
  930. fw_header.svid = board->svid;
  931. fw_header.sdid = board->sdid;
  932. flash_size = board->flash_size;
  933. return 0;
  934. err_out:
  935. return -1;
  936. }
  937. int
  938. parse_opt_rev(char ch, char *arg)
  939. {
  940. if (required_arg(ch, arg)) {
  941. return -1;
  942. }
  943. if (str2u32(arg, &fw_header.rev) != 0) {
  944. errmsg(0,"invalid revision number: %s", arg);
  945. return -1;
  946. }
  947. return 0;
  948. }
  949. /*
  950. * main
  951. */
  952. int
  953. main(int argc, char *argv[])
  954. {
  955. int optinvalid = 0; /* flag for invalid option */
  956. int c;
  957. int res = EXIT_FAILURE;
  958. FILE *outfile;
  959. uint32_t crc;
  960. progname=basename(argv[0]);
  961. memset(&fw_header, 0, sizeof(fw_header));
  962. /* init header defaults */
  963. fw_header.vid = VENID_COMPEX;
  964. fw_header.did = DEVID_COMPEX_WP54G;
  965. fw_header.svid = VENID_COMPEX;
  966. fw_header.sdid = DEVID_COMPEX_WP54G;
  967. fw_header.fwhi = 0x20000;
  968. fw_header.fwlo = 0x20000;
  969. fw_header.flags = 0;
  970. opterr = 0; /* could not print standard getopt error messages */
  971. while ((c = getopt(argc, argv, "b:B:f:hi:p:r:s:v")) != -1) {
  972. optinvalid = 0;
  973. switch (c) {
  974. case 'b':
  975. optinvalid = parse_opt_block(c,optarg);
  976. break;
  977. case 'B':
  978. optinvalid = parse_opt_board(c,optarg);
  979. break;
  980. case 'f':
  981. optinvalid = parse_opt_flags(c,optarg);
  982. break;
  983. case 'h':
  984. usage(EXIT_SUCCESS);
  985. break;
  986. case 'i':
  987. optinvalid = parse_opt_id(c,optarg);
  988. break;
  989. case 'p':
  990. optinvalid = parse_opt_partition(c,optarg);
  991. break;
  992. case 'r':
  993. optinvalid = parse_opt_rev(c,optarg);
  994. break;
  995. case 's':
  996. optinvalid = parse_opt_size(c,optarg);
  997. break;
  998. case 'v':
  999. verblevel++;
  1000. break;
  1001. default:
  1002. optinvalid = 1;
  1003. break;
  1004. }
  1005. if (optinvalid != 0 ){
  1006. errmsg(0, "invalid option: -%c", optopt);
  1007. goto out;
  1008. }
  1009. }
  1010. if (optind == argc) {
  1011. errmsg(0, "no output file specified");
  1012. goto out;
  1013. }
  1014. ofname = argv[optind++];
  1015. if (optind < argc) {
  1016. errmsg(0, "invalid option: %s", argv[optind]);
  1017. goto out;
  1018. }
  1019. if (!board) {
  1020. errmsg(0, "no board specified");
  1021. goto out;
  1022. }
  1023. if (flash_size == 0) {
  1024. errmsg(0, "no flash size specified");
  1025. goto out;
  1026. }
  1027. if (process_files() != 0) {
  1028. goto out;
  1029. }
  1030. if (process_partitions() != 0) {
  1031. goto out;
  1032. }
  1033. outfile = fopen(ofname, "w");
  1034. if (outfile == NULL) {
  1035. errmsg(1, "could not open \"%s\" for writing", ofname);
  1036. goto out;
  1037. }
  1038. crc = 0;
  1039. init_crc_table();
  1040. if (write_out_header(outfile, &crc) != 0)
  1041. goto out_flush;
  1042. if (write_out_blocks(outfile, &crc) != 0)
  1043. goto out_flush;
  1044. fw_header.crc = crc;
  1045. if (write_out_header(outfile, NULL) != 0)
  1046. goto out_flush;
  1047. dbgmsg(1,"Firmware file %s completed.", ofname);
  1048. res = EXIT_SUCCESS;
  1049. out_flush:
  1050. fflush(outfile);
  1051. fclose(outfile);
  1052. if (res != EXIT_SUCCESS) {
  1053. unlink(ofname);
  1054. }
  1055. out:
  1056. return res;
  1057. }