1
0

mkcsysimg.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. *
  3. * Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
  4. *
  5. * This program was based on the code found in various Linux
  6. * source tarballs released by Edimax for it's devices.
  7. * Original author: David Hsu <davidhsu@realtek.com.tw>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301, USA.
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include <unistd.h> /* for unlink() */
  29. #include <libgen.h>
  30. #include <getopt.h> /* for getopt() */
  31. #include <stdarg.h>
  32. #include <errno.h>
  33. #include <sys/stat.h>
  34. #include <endian.h> /* for __BYTE_ORDER */
  35. #if defined(__CYGWIN__)
  36. # include <byteswap.h>
  37. #endif
  38. #include "csysimg.h"
  39. #if (__BYTE_ORDER == __LITTLE_ENDIAN)
  40. # define HOST_TO_LE16(x) (x)
  41. # define HOST_TO_LE32(x) (x)
  42. # define LE16_TO_HOST(x) (x)
  43. # define LE32_TO_HOST(x) (x)
  44. #else
  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 MAX_NUM_BLOCKS 8
  51. #define MAX_ARG_COUNT 32
  52. #define MAX_ARG_LEN 1024
  53. #define FILE_BUF_LEN (16*1024)
  54. #define CSYS_PADC 0xFF
  55. #define BLOCK_TYPE_BOOT 0
  56. #define BLOCK_TYPE_CONF 1
  57. #define BLOCK_TYPE_WEBP 2
  58. #define BLOCK_TYPE_CODE 3
  59. #define BLOCK_TYPE_XTRA 4
  60. #define DEFAULT_BLOCK_ALIGN 0x10000U
  61. #define CSUM_SIZE_NONE 0
  62. #define CSUM_SIZE_8 1
  63. #define CSUM_SIZE_16 2
  64. struct csum_state{
  65. int size;
  66. uint16_t val;
  67. uint16_t tmp;
  68. int odd;
  69. };
  70. struct csys_block {
  71. int type; /* type of the block */
  72. int need_file;
  73. char *file_name; /* name of the file */
  74. uint32_t file_size; /* length of the file */
  75. unsigned char sig[SIG_LEN];
  76. uint32_t addr;
  77. int addr_set;
  78. uint32_t align;
  79. int align_set;
  80. uint8_t padc;
  81. uint32_t size;
  82. uint32_t size_hdr;
  83. uint32_t size_csum;
  84. uint32_t size_avail;
  85. struct csum_state *css;
  86. };
  87. struct board_info {
  88. char *model;
  89. char *name;
  90. uint32_t flash_size;
  91. char sig_boot[SIG_LEN];
  92. char sig_conf[SIG_LEN];
  93. char sig_webp[SIG_LEN];
  94. uint32_t boot_size;
  95. uint32_t conf_size;
  96. uint32_t webp_size;
  97. uint32_t webp_size_max;
  98. uint32_t code_size;
  99. uint32_t addr_code;
  100. uint32_t addr_webp;
  101. };
  102. #define BOARD(m, n, f, sigb, sigw, bs, cs, ws, ac, aw) {\
  103. .model = m, .name = n, .flash_size = f<<20, \
  104. .sig_boot = sigb, .sig_conf = SIG_CONF, .sig_webp = sigw, \
  105. .boot_size = bs, .conf_size = cs, \
  106. .webp_size = ws, .webp_size_max = 3*0x10000, \
  107. .addr_code = ac, .addr_webp = aw \
  108. }
  109. #define BOARD_ADM(m,n,f, sigw) BOARD(m,n,f, ADM_BOOT_SIG, sigw, \
  110. ADM_BOOT_SIZE, ADM_CONF_SIZE, ADM_WEBP_SIZE, \
  111. ADM_CODE_ADDR, ADM_WEBP_ADDR)
  112. /*
  113. * Globals
  114. */
  115. char *progname;
  116. char *ofname = NULL;
  117. int verblevel = 0;
  118. int invalid_causes_error = 1;
  119. int keep_invalid_images = 0;
  120. struct board_info *board = NULL;
  121. struct csys_block *boot_block = NULL;
  122. struct csys_block *conf_block = NULL;
  123. struct csys_block *webp_block = NULL;
  124. struct csys_block *code_block = NULL;
  125. struct csys_block blocks[MAX_NUM_BLOCKS];
  126. int num_blocks = 0;
  127. static struct board_info boards[] = {
  128. /* The original Edimax products */
  129. BOARD_ADM("BR-6104K", "Edimax BR-6104K", 2, SIG_BR6104K),
  130. BOARD_ADM("BR-6104KP", "Edimax BR-6104KP", 2, SIG_BR6104KP),
  131. BOARD_ADM("BR-6104Wg", "Edimax BR-6104Wg", 2, SIG_BR6104Wg),
  132. BOARD_ADM("BR-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG),
  133. BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K),
  134. BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP),
  135. BOARD_ADM("BR-6524N", "Edimax BR-6524N", 2, SIG_BR6524N),
  136. BOARD_ADM("BR-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG),
  137. BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP),
  138. BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K),
  139. BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP),
  140. BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP),
  141. BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg),
  142. BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg),
  143. BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U),
  144. BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg),
  145. /* Hawking products */
  146. BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4),
  147. BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G),
  148. /* Planet products */
  149. BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D),
  150. BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D),
  151. /* Conceptronic products */
  152. BOARD_ADM("C54BSR4", "Conceptronic C54BSR4", 2, SIG_C54BSR4),
  153. /* OSBRiDGE products */
  154. BOARD_ADM("5GXi", "OSBDRiDGE 5GXi", 2, SIG_5GXI),
  155. {.model = NULL}
  156. };
  157. /*
  158. * Message macros
  159. */
  160. #define ERR(fmt, ...) do { \
  161. fflush(0); \
  162. fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
  163. } while (0)
  164. #define ERRS(fmt, ...) do { \
  165. int save = errno; \
  166. fflush(0); \
  167. fprintf(stderr, "[%s] *** error: " fmt ": %s\n", progname, ## __VA_ARGS__ \
  168. , strerror(save)); \
  169. } while (0)
  170. #define WARN(fmt, ...) do { \
  171. fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
  172. } while (0)
  173. #define DBG(lev, fmt, ...) do { \
  174. if (verblevel < lev) \
  175. break;\
  176. fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
  177. } while (0)
  178. #define ERR_FATAL -1
  179. #define ERR_INVALID_IMAGE -2
  180. void
  181. usage(int status)
  182. {
  183. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  184. struct board_info *board;
  185. fprintf(stream, "Usage: %s [OPTIONS...] <file>\n", progname);
  186. fprintf(stream,
  187. "\n"
  188. "Options:\n"
  189. " -B <board> create image for the board specified with <board>.\n"
  190. " valid <board> values:\n"
  191. );
  192. for (board = boards; board->model != NULL; board++){
  193. fprintf(stream,
  194. " %-12s: %s\n",
  195. board->model, board->name);
  196. };
  197. fprintf(stream,
  198. " -d don't throw error on invalid images\n"
  199. " -k keep invalid images\n"
  200. " -b <file>[:<align>[:<padc>]]\n"
  201. " add boot code to the image\n"
  202. " -c <file>[:<align>[:<padc>]]\n"
  203. " add configuration settings to the image\n"
  204. " -r <file>:[<addr>][:<align>[:<padc>]]\n"
  205. " add runtime code to the image\n"
  206. " -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
  207. " add webpages to the image\n"
  208. " -x <file>[:<align>[:<padc>]]\n"
  209. " add extra data at the end of the image\n"
  210. " -h show this screen\n"
  211. "Parameters:\n"
  212. " <file> write output to the file <file>\n"
  213. );
  214. exit(status);
  215. }
  216. static inline uint32_t align(uint32_t base, uint32_t alignment)
  217. {
  218. uint32_t ret;
  219. if (alignment) {
  220. ret = (base + alignment - 1);
  221. ret &= ~(alignment-1);
  222. } else {
  223. ret = base;
  224. }
  225. return ret;
  226. }
  227. /*
  228. * argument parsing
  229. */
  230. int
  231. str2u32(char *arg, uint32_t *val)
  232. {
  233. char *err = NULL;
  234. uint32_t t;
  235. errno=0;
  236. t = strtoul(arg, &err, 0);
  237. if (errno || (err==arg) || ((err != NULL) && *err)) {
  238. return -1;
  239. }
  240. *val = t;
  241. return 0;
  242. }
  243. int
  244. str2u16(char *arg, uint16_t *val)
  245. {
  246. char *err = NULL;
  247. uint32_t t;
  248. errno=0;
  249. t = strtoul(arg, &err, 0);
  250. if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
  251. return -1;
  252. }
  253. *val = t & 0xFFFF;
  254. return 0;
  255. }
  256. int
  257. str2u8(char *arg, uint8_t *val)
  258. {
  259. char *err = NULL;
  260. uint32_t t;
  261. errno=0;
  262. t = strtoul(arg, &err, 0);
  263. if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x100)) {
  264. return -1;
  265. }
  266. *val = t & 0xFF;
  267. return 0;
  268. }
  269. int
  270. str2sig(char *arg, uint32_t *sig)
  271. {
  272. if (strlen(arg) != 4)
  273. return -1;
  274. *sig = arg[0] | (arg[1] << 8) | (arg[2] << 16) | (arg[3] << 24);
  275. return 0;
  276. }
  277. int
  278. parse_arg(char *arg, char *buf, char *argv[])
  279. {
  280. int res = 0;
  281. size_t argl;
  282. char *tok;
  283. char **ap = &buf;
  284. int i;
  285. memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
  286. if ((arg == NULL)) {
  287. /* no arguments */
  288. return 0;
  289. }
  290. argl = strlen(arg);
  291. if (argl == 0) {
  292. /* no arguments */
  293. return 0;
  294. }
  295. if (argl >= MAX_ARG_LEN) {
  296. /* argument is too long */
  297. argl = MAX_ARG_LEN-1;
  298. }
  299. memcpy(buf, arg, argl);
  300. buf[argl] = '\0';
  301. for (i = 0; i < MAX_ARG_COUNT; i++) {
  302. tok = strsep(ap, ":");
  303. if (tok == NULL) {
  304. break;
  305. }
  306. #if 0
  307. else if (tok[0] == '\0') {
  308. break;
  309. }
  310. #endif
  311. argv[i] = tok;
  312. res++;
  313. }
  314. return res;
  315. }
  316. int
  317. required_arg(char c, char *arg)
  318. {
  319. if (arg == NULL || *arg != '-')
  320. return 0;
  321. ERR("option -%c requires an argument\n", c);
  322. return ERR_FATAL;
  323. }
  324. int
  325. is_empty_arg(char *arg)
  326. {
  327. int ret = 1;
  328. if (arg != NULL) {
  329. if (*arg) ret = 0;
  330. };
  331. return ret;
  332. }
  333. void
  334. csum8_update(uint8_t *p, uint32_t len, struct csum_state *css)
  335. {
  336. for ( ; len > 0; len --) {
  337. css->val += *p++;
  338. }
  339. }
  340. uint16_t
  341. csum8_get(struct csum_state *css)
  342. {
  343. uint8_t t;
  344. t = css->val;
  345. return ~t + 1;
  346. }
  347. void
  348. csum16_update(uint8_t *p, uint32_t len, struct csum_state *css)
  349. {
  350. uint16_t t;
  351. if (css->odd) {
  352. t = css->tmp + (p[0]<<8);
  353. css->val += LE16_TO_HOST(t);
  354. css->odd = 0;
  355. len--;
  356. p++;
  357. }
  358. for ( ; len > 1; len -= 2, p +=2 ) {
  359. t = p[0] + (p[1] << 8);
  360. css->val += LE16_TO_HOST(t);
  361. }
  362. if (len == 1) {
  363. css->tmp = p[0];
  364. css->odd = 1;
  365. }
  366. }
  367. uint16_t
  368. csum16_get(struct csum_state *css)
  369. {
  370. char pad = 0;
  371. csum16_update(&pad, 1, css);
  372. return ~css->val + 1;
  373. }
  374. void
  375. csum_init(struct csum_state *css, int size)
  376. {
  377. css->val = 0;
  378. css->tmp = 0;
  379. css->odd = 0;
  380. css->size = size;
  381. }
  382. void
  383. csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
  384. {
  385. switch (css->size) {
  386. case CSUM_SIZE_8:
  387. csum8_update(p,len,css);
  388. break;
  389. case CSUM_SIZE_16:
  390. csum16_update(p,len,css);
  391. break;
  392. }
  393. }
  394. uint16_t
  395. csum_get(struct csum_state *css)
  396. {
  397. uint16_t ret;
  398. switch (css->size) {
  399. case CSUM_SIZE_8:
  400. ret = csum8_get(css);
  401. break;
  402. case CSUM_SIZE_16:
  403. ret = csum16_get(css);
  404. break;
  405. }
  406. return ret;
  407. }
  408. /*
  409. * routines to write data to the output file
  410. */
  411. int
  412. write_out_data(FILE *outfile, uint8_t *data, size_t len,
  413. struct csum_state *css)
  414. {
  415. errno = 0;
  416. fwrite(data, len, 1, outfile);
  417. if (errno) {
  418. ERRS("unable to write output file");
  419. return ERR_FATAL;
  420. }
  421. if (css) {
  422. csum_update(data, len, css);
  423. }
  424. return 0;
  425. }
  426. int
  427. write_out_padding(FILE *outfile, size_t len, uint8_t padc,
  428. struct csum_state *css)
  429. {
  430. uint8_t buf[512];
  431. size_t buflen = sizeof(buf);
  432. int err;
  433. memset(buf, padc, buflen);
  434. while (len > 0) {
  435. if (len < buflen)
  436. buflen = len;
  437. err = write_out_data(outfile, buf, buflen, css);
  438. if (err)
  439. return err;
  440. len -= buflen;
  441. }
  442. return 0;
  443. }
  444. int
  445. block_stat_file(struct csys_block *block)
  446. {
  447. struct stat st;
  448. int err;
  449. if (block->file_name == NULL)
  450. return 0;
  451. err = stat(block->file_name, &st);
  452. if (err){
  453. ERRS("stat failed on %s", block->file_name);
  454. return ERR_FATAL;
  455. }
  456. block->file_size = st.st_size;
  457. return 0;
  458. }
  459. int
  460. block_writeout_hdr(FILE *outfile, struct csys_block *block)
  461. {
  462. struct csys_header hdr;
  463. int res;
  464. if (block->size_hdr == 0)
  465. return 0;
  466. /* setup header fields */
  467. memcpy(hdr.sig, block->sig, 4);
  468. hdr.addr = HOST_TO_LE32(block->addr);
  469. hdr.size = HOST_TO_LE32(block->size - block->size_hdr - block->size_csum);
  470. DBG(1,"writing header for block");
  471. res = write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr),NULL);
  472. return res;
  473. }
  474. int
  475. block_writeout_file(FILE *outfile, struct csys_block *block)
  476. {
  477. char buf[FILE_BUF_LEN];
  478. size_t buflen = sizeof(buf);
  479. FILE *f;
  480. size_t len;
  481. int res;
  482. if (block->file_name == NULL)
  483. return 0;
  484. if (block->file_size == 0)
  485. return 0;
  486. errno = 0;
  487. f = fopen(block->file_name,"r");
  488. if (errno) {
  489. ERRS("unable to open file: %s", block->file_name);
  490. return ERR_FATAL;
  491. }
  492. len = block->file_size;
  493. while (len > 0) {
  494. if (len < buflen)
  495. buflen = len;
  496. /* read data from source file */
  497. errno = 0;
  498. fread(buf, buflen, 1, f);
  499. if (errno != 0) {
  500. ERRS("unable to read from file: %s", block->file_name);
  501. res = ERR_FATAL;
  502. break;
  503. }
  504. res = write_out_data(outfile, buf, buflen, block->css);
  505. if (res)
  506. break;
  507. len -= buflen;
  508. }
  509. fclose(f);
  510. return res;
  511. }
  512. int
  513. block_writeout_data(FILE *outfile, struct csys_block *block)
  514. {
  515. int res;
  516. size_t padlen;
  517. res = block_writeout_file(outfile, block);
  518. if (res)
  519. return res;
  520. /* write padding data if neccesary */
  521. padlen = block->size_avail - block->file_size;
  522. DBG(1,"padding block, length=%zu", padlen);
  523. res = write_out_padding(outfile, padlen, block->padc, block->css);
  524. return res;
  525. }
  526. int
  527. block_writeout_csum(FILE *outfile, struct csys_block *block)
  528. {
  529. uint16_t csum;
  530. int res;
  531. if (block->size_csum == 0)
  532. return 0;
  533. DBG(1,"writing checksum for block");
  534. csum = HOST_TO_LE16(csum_get(block->css));
  535. res = write_out_data(outfile, (uint8_t *)&csum, block->size_csum, NULL);
  536. return res;
  537. }
  538. int
  539. block_writeout(FILE *outfile, struct csys_block *block)
  540. {
  541. int res;
  542. struct csum_state css;
  543. res = 0;
  544. if (block == NULL)
  545. return res;
  546. block->css = NULL;
  547. DBG(2, "writing block, file=%s, file_size=%d, space=%d",
  548. block->file_name, block->file_size, block->size_avail);
  549. res = block_writeout_hdr(outfile, block);
  550. if (res)
  551. return res;
  552. if (block->size_csum != 0) {
  553. block->css = &css;
  554. csum_init(&css, block->size_csum);
  555. }
  556. res = block_writeout_data(outfile, block);
  557. if (res)
  558. return res;
  559. res = block_writeout_csum(outfile, block);
  560. if (res)
  561. return res;
  562. return res;
  563. }
  564. int
  565. write_out_blocks(FILE *outfile)
  566. {
  567. struct csys_block *block;
  568. int i, res;
  569. res = block_writeout(outfile, boot_block);
  570. if (res)
  571. return res;
  572. res = block_writeout(outfile, conf_block);
  573. if (res)
  574. return res;
  575. res = block_writeout(outfile, webp_block);
  576. if (res)
  577. return res;
  578. res = block_writeout(outfile, code_block);
  579. if (res)
  580. return res;
  581. res = 0;
  582. for (i=0; i < num_blocks; i++) {
  583. block = &blocks[i];
  584. if (block->type != BLOCK_TYPE_XTRA)
  585. continue;
  586. res = block_writeout(outfile, block);
  587. if (res)
  588. break;
  589. }
  590. return res;
  591. }
  592. struct board_info *
  593. find_board(char *model)
  594. {
  595. struct board_info *ret;
  596. struct board_info *board;
  597. ret = NULL;
  598. for (board = boards; board->model != NULL; board++){
  599. if (strcasecmp(model, board->model) == 0) {
  600. ret = board;
  601. break;
  602. }
  603. };
  604. return ret;
  605. }
  606. int
  607. parse_opt_board(char ch, char *arg)
  608. {
  609. DBG(1,"parsing board option: -%c %s", ch, arg);
  610. if (board != NULL) {
  611. ERR("only one board option allowed");
  612. return ERR_FATAL;
  613. }
  614. if (required_arg(ch, arg))
  615. return ERR_FATAL;
  616. board = find_board(arg);
  617. if (board == NULL){
  618. ERR("invalid/unknown board specified: %s", arg);
  619. return ERR_FATAL;
  620. }
  621. return 0;
  622. }
  623. int
  624. parse_opt_block(char ch, char *arg)
  625. {
  626. char buf[MAX_ARG_LEN];
  627. char *argv[MAX_ARG_COUNT];
  628. int argc;
  629. char *p;
  630. struct csys_block *block;
  631. int i;
  632. if ( num_blocks > MAX_NUM_BLOCKS ) {
  633. ERR("too many blocks specified");
  634. return ERR_FATAL;
  635. }
  636. block = &blocks[num_blocks];
  637. /* setup default field values */
  638. block->need_file = 1;
  639. block->padc = 0xFF;
  640. switch (ch) {
  641. case 'b':
  642. if (boot_block) {
  643. WARN("only one boot block allowed");
  644. break;
  645. }
  646. block->type = BLOCK_TYPE_BOOT;
  647. boot_block = block;
  648. break;
  649. case 'c':
  650. if (conf_block) {
  651. WARN("only one config block allowed");
  652. break;
  653. }
  654. block->type = BLOCK_TYPE_CONF;
  655. conf_block = block;
  656. break;
  657. case 'w':
  658. if (webp_block) {
  659. WARN("only one web block allowed");
  660. break;
  661. }
  662. block->type = BLOCK_TYPE_WEBP;
  663. block->size_hdr = sizeof(struct csys_header);
  664. block->size_csum = CSUM_SIZE_8;
  665. block->need_file = 0;
  666. webp_block = block;
  667. break;
  668. case 'r':
  669. if (code_block) {
  670. WARN("only one runtime block allowed");
  671. break;
  672. }
  673. block->type = BLOCK_TYPE_CODE;
  674. block->size_hdr = sizeof(struct csys_header);
  675. block->size_csum = CSUM_SIZE_16;
  676. code_block = block;
  677. break;
  678. case 'x':
  679. block->type = BLOCK_TYPE_XTRA;
  680. break;
  681. default:
  682. ERR("unknown block type \"%c\"", ch);
  683. return ERR_FATAL;
  684. }
  685. argc = parse_arg(arg, buf, argv);
  686. i = 0;
  687. p = argv[i++];
  688. if (!is_empty_arg(p)) {
  689. block->file_name = strdup(p);
  690. if (block->file_name == NULL) {
  691. ERR("not enough memory");
  692. return ERR_FATAL;
  693. }
  694. } else if (block->need_file){
  695. ERR("no file specified in %s", arg);
  696. return ERR_FATAL;
  697. }
  698. if (block->size_hdr) {
  699. p = argv[i++];
  700. if (!is_empty_arg(p)) {
  701. if (str2u32(p, &block->addr) != 0) {
  702. ERR("invalid start address in %s", arg);
  703. return ERR_FATAL;
  704. }
  705. block->addr_set = 1;
  706. }
  707. }
  708. p = argv[i++];
  709. if (!is_empty_arg(p)) {
  710. if (str2u32(p, &block->align) != 0) {
  711. ERR("invalid alignment value in %s", arg);
  712. return ERR_FATAL;
  713. }
  714. block->align_set = 1;
  715. }
  716. p = argv[i++];
  717. if (!is_empty_arg(p) && (str2u8(p, &block->padc) != 0)) {
  718. ERR("invalid paddig character in %s", arg);
  719. return ERR_FATAL;
  720. }
  721. num_blocks++;
  722. return 0;
  723. }
  724. int
  725. process_blocks(void)
  726. {
  727. struct csys_block *block;
  728. uint32_t offs = 0;
  729. int i;
  730. int res;
  731. res = 0;
  732. /* collecting stats */
  733. for (i = 0; i < num_blocks; i++) {
  734. block = &blocks[i];
  735. res = block_stat_file(block);
  736. if (res)
  737. return res;
  738. }
  739. /* bootloader */
  740. block = boot_block;
  741. if (block) {
  742. block->size = board->boot_size;
  743. if (block->file_size > board->boot_size) {
  744. WARN("boot block is too big");
  745. res = ERR_INVALID_IMAGE;
  746. }
  747. }
  748. offs += board->boot_size;
  749. /* configuration data */
  750. block = conf_block;
  751. if (block) {
  752. block->size = board->conf_size;
  753. if (block->file_size > board->conf_size) {
  754. WARN("config block is too big");
  755. res = ERR_INVALID_IMAGE;
  756. }
  757. }
  758. offs += board->conf_size;
  759. /* webpages */
  760. block = webp_block;
  761. if (block) {
  762. memcpy(block->sig, board->sig_webp, 4);
  763. if (block->addr_set == 0)
  764. block->addr = board->addr_webp;
  765. if (block->align_set == 0)
  766. block->align = DEFAULT_BLOCK_ALIGN;
  767. block->size = align(offs + block->file_size + block->size_hdr +
  768. block->size_csum, block->align) - offs;
  769. if (block->size > board->webp_size_max) {
  770. WARN("webpages block is too big");
  771. res = ERR_INVALID_IMAGE;
  772. }
  773. DBG(2,"webpages start at %08x, size=%08x", offs,
  774. block->size);
  775. offs += block->size;
  776. if (offs > board->flash_size) {
  777. WARN("webp block is too big");
  778. res = ERR_INVALID_IMAGE;
  779. }
  780. }
  781. /* runtime code */
  782. block = code_block;
  783. if (block) {
  784. memcpy(code_block->sig, SIG_CSYS, 4);
  785. if (block->addr_set == 0)
  786. block->addr = board->addr_code;
  787. if (block->align_set == 0)
  788. block->align = DEFAULT_BLOCK_ALIGN;
  789. block->size = align(offs + block->file_size +
  790. block->size_hdr + block->size_csum,
  791. block->align) - offs;
  792. DBG(2,"code block start at %08x, size=%08x", offs,
  793. block->size);
  794. offs += block->size;
  795. if (offs > board->flash_size) {
  796. WARN("code block is too big");
  797. res = ERR_INVALID_IMAGE;
  798. }
  799. }
  800. for (i = 0; i < num_blocks; i++) {
  801. block = &blocks[i];
  802. if (block->type != BLOCK_TYPE_XTRA)
  803. continue;
  804. if (block->align_set == 0)
  805. block->align = DEFAULT_BLOCK_ALIGN;
  806. block->size = align(offs + block->file_size,
  807. block->align) - offs;
  808. DBG(2,"file %s start at %08x, size=%08x, align=%08x",
  809. block->file_name, offs, block->size, block->align);
  810. offs += block->size;
  811. if (offs > board->flash_size) {
  812. WARN("file %s is too big, size=%d, avail=%d",
  813. block->file_name, block->file_size,
  814. board->flash_size - offs);
  815. res = ERR_INVALID_IMAGE;
  816. }
  817. }
  818. for (i = 0; i < num_blocks; i++) {
  819. block = &blocks[i];
  820. block->size_avail = block->size - block->size_hdr -
  821. block->size_csum;
  822. if (block->size_avail < block->file_size) {
  823. WARN("file %s is too big, size=%d, avail=%d",
  824. block->file_name, block->file_size,
  825. block->size_avail);
  826. res = ERR_INVALID_IMAGE;
  827. }
  828. }
  829. return res;
  830. }
  831. int
  832. main(int argc, char *argv[])
  833. {
  834. int optinvalid = 0; /* flag for invalid option */
  835. int c;
  836. int res = ERR_FATAL;
  837. FILE *outfile;
  838. progname=basename(argv[0]);
  839. opterr = 0; /* could not print standard getopt error messages */
  840. while ( 1 ) {
  841. optinvalid = 0;
  842. c = getopt(argc, argv, "b:B:c:dhkr:vw:x:");
  843. if (c == -1)
  844. break;
  845. switch (c) {
  846. case 'b':
  847. case 'c':
  848. case 'r':
  849. case 'x':
  850. optinvalid = parse_opt_block(c,optarg);
  851. break;
  852. case 'w':
  853. if (optarg != NULL && *optarg == '-') {
  854. /* rollback */
  855. optind--;
  856. optarg = NULL;
  857. }
  858. optinvalid = parse_opt_block(c,optarg);
  859. break;
  860. case 'd':
  861. invalid_causes_error = 0;
  862. break;
  863. case 'k':
  864. keep_invalid_images = 1;
  865. break;
  866. case 'B':
  867. optinvalid = parse_opt_board(c,optarg);
  868. break;
  869. case 'v':
  870. verblevel++;
  871. break;
  872. case 'h':
  873. usage(EXIT_SUCCESS);
  874. break;
  875. default:
  876. optinvalid = 1;
  877. break;
  878. }
  879. if (optinvalid != 0 ){
  880. ERR("invalid option: -%c", optopt);
  881. goto out;
  882. }
  883. }
  884. if (board == NULL) {
  885. ERR("no board specified");
  886. goto out;
  887. }
  888. if (optind == argc) {
  889. ERR("no output file specified");
  890. goto out;
  891. }
  892. ofname = argv[optind++];
  893. if (optind < argc) {
  894. ERR("invalid option: %s", argv[optind]);
  895. goto out;
  896. }
  897. res = process_blocks();
  898. if (res == ERR_FATAL)
  899. goto out;
  900. if (res == ERR_INVALID_IMAGE) {
  901. if (invalid_causes_error)
  902. res = ERR_FATAL;
  903. if (keep_invalid_images == 0) {
  904. WARN("generation of invalid images \"%s\" disabled", ofname);
  905. goto out;
  906. }
  907. WARN("generating invalid image: \"%s\"", ofname);
  908. }
  909. outfile = fopen(ofname, "w");
  910. if (outfile == NULL) {
  911. ERRS("could not open \"%s\" for writing", ofname);
  912. res = ERR_FATAL;
  913. goto out;
  914. }
  915. if (write_out_blocks(outfile) != 0) {
  916. res = ERR_FATAL;
  917. goto out_flush;
  918. }
  919. DBG(1,"Image file %s completed.", ofname);
  920. out_flush:
  921. fflush(outfile);
  922. fclose(outfile);
  923. if (res == ERR_FATAL) {
  924. unlink(ofname);
  925. }
  926. out:
  927. if (res == ERR_FATAL)
  928. return EXIT_FAILURE;
  929. return EXIT_SUCCESS;
  930. }