kwbimage.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /*
  2. * Image manipulator for Marvell SoCs
  3. * supports Kirkwood, Dove, Armada 370, and Armada XP
  4. *
  5. * (C) Copyright 2013 Thomas Petazzoni
  6. * <thomas.petazzoni@free-electrons.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. *
  10. * Not implemented: support for the register headers and secure
  11. * headers in v1 images
  12. */
  13. #include "imagetool.h"
  14. #include <limits.h>
  15. #include <image.h>
  16. #include <stdint.h>
  17. #include "kwbimage.h"
  18. static struct image_cfg_element *image_cfg;
  19. static int cfgn;
  20. struct boot_mode {
  21. unsigned int id;
  22. const char *name;
  23. };
  24. struct boot_mode boot_modes[] = {
  25. { 0x4D, "i2c" },
  26. { 0x5A, "spi" },
  27. { 0x8B, "nand" },
  28. { 0x78, "sata" },
  29. { 0x9C, "pex" },
  30. { 0x69, "uart" },
  31. { 0xAE, "sdio" },
  32. {},
  33. };
  34. struct nand_ecc_mode {
  35. unsigned int id;
  36. const char *name;
  37. };
  38. struct nand_ecc_mode nand_ecc_modes[] = {
  39. { 0x00, "default" },
  40. { 0x01, "hamming" },
  41. { 0x02, "rs" },
  42. { 0x03, "disabled" },
  43. {},
  44. };
  45. /* Used to identify an undefined execution or destination address */
  46. #define ADDR_INVALID ((uint32_t)-1)
  47. #define BINARY_MAX_ARGS 8
  48. /* In-memory representation of a line of the configuration file */
  49. enum image_cfg_type {
  50. IMAGE_CFG_VERSION = 0x1,
  51. IMAGE_CFG_BOOT_FROM,
  52. IMAGE_CFG_DEST_ADDR,
  53. IMAGE_CFG_EXEC_ADDR,
  54. IMAGE_CFG_NAND_BLKSZ,
  55. IMAGE_CFG_NAND_BADBLK_LOCATION,
  56. IMAGE_CFG_NAND_ECC_MODE,
  57. IMAGE_CFG_NAND_PAGESZ,
  58. IMAGE_CFG_BINARY,
  59. IMAGE_CFG_PAYLOAD,
  60. IMAGE_CFG_DATA,
  61. IMAGE_CFG_BAUDRATE,
  62. IMAGE_CFG_DEBUG,
  63. IMAGE_CFG_COUNT
  64. } type;
  65. static const char * const id_strs[] = {
  66. [IMAGE_CFG_VERSION] = "VERSION",
  67. [IMAGE_CFG_BOOT_FROM] = "BOOT_FROM",
  68. [IMAGE_CFG_DEST_ADDR] = "DEST_ADDR",
  69. [IMAGE_CFG_EXEC_ADDR] = "EXEC_ADDR",
  70. [IMAGE_CFG_NAND_BLKSZ] = "NAND_BLKSZ",
  71. [IMAGE_CFG_NAND_BADBLK_LOCATION] = "NAND_BADBLK_LOCATION",
  72. [IMAGE_CFG_NAND_ECC_MODE] = "NAND_ECC_MODE",
  73. [IMAGE_CFG_NAND_PAGESZ] = "NAND_PAGE_SIZE",
  74. [IMAGE_CFG_BINARY] = "BINARY",
  75. [IMAGE_CFG_PAYLOAD] = "PAYLOAD",
  76. [IMAGE_CFG_DATA] = "DATA",
  77. [IMAGE_CFG_BAUDRATE] = "BAUDRATE",
  78. [IMAGE_CFG_DEBUG] = "DEBUG",
  79. };
  80. struct image_cfg_element {
  81. enum image_cfg_type type;
  82. union {
  83. unsigned int version;
  84. unsigned int bootfrom;
  85. struct {
  86. const char *file;
  87. unsigned int args[BINARY_MAX_ARGS];
  88. unsigned int nargs;
  89. } binary;
  90. const char *payload;
  91. unsigned int dstaddr;
  92. unsigned int execaddr;
  93. unsigned int nandblksz;
  94. unsigned int nandbadblklocation;
  95. unsigned int nandeccmode;
  96. unsigned int nandpagesz;
  97. struct ext_hdr_v0_reg regdata;
  98. unsigned int baudrate;
  99. unsigned int debug;
  100. };
  101. };
  102. #define IMAGE_CFG_ELEMENT_MAX 256
  103. /*
  104. * Utility functions to manipulate boot mode and ecc modes (convert
  105. * them back and forth between description strings and the
  106. * corresponding numerical identifiers).
  107. */
  108. static const char *image_boot_mode_name(unsigned int id)
  109. {
  110. int i;
  111. for (i = 0; boot_modes[i].name; i++)
  112. if (boot_modes[i].id == id)
  113. return boot_modes[i].name;
  114. return NULL;
  115. }
  116. int image_boot_mode_id(const char *boot_mode_name)
  117. {
  118. int i;
  119. for (i = 0; boot_modes[i].name; i++)
  120. if (!strcmp(boot_modes[i].name, boot_mode_name))
  121. return boot_modes[i].id;
  122. return -1;
  123. }
  124. int image_nand_ecc_mode_id(const char *nand_ecc_mode_name)
  125. {
  126. int i;
  127. for (i = 0; nand_ecc_modes[i].name; i++)
  128. if (!strcmp(nand_ecc_modes[i].name, nand_ecc_mode_name))
  129. return nand_ecc_modes[i].id;
  130. return -1;
  131. }
  132. static struct image_cfg_element *
  133. image_find_option(unsigned int optiontype)
  134. {
  135. int i;
  136. for (i = 0; i < cfgn; i++) {
  137. if (image_cfg[i].type == optiontype)
  138. return &image_cfg[i];
  139. }
  140. return NULL;
  141. }
  142. static unsigned int
  143. image_count_options(unsigned int optiontype)
  144. {
  145. int i;
  146. unsigned int count = 0;
  147. for (i = 0; i < cfgn; i++)
  148. if (image_cfg[i].type == optiontype)
  149. count++;
  150. return count;
  151. }
  152. /*
  153. * Compute a 8-bit checksum of a memory area. This algorithm follows
  154. * the requirements of the Marvell SoC BootROM specifications.
  155. */
  156. static uint8_t image_checksum8(void *start, uint32_t len)
  157. {
  158. uint8_t csum = 0;
  159. uint8_t *p = start;
  160. /* check len and return zero checksum if invalid */
  161. if (!len)
  162. return 0;
  163. do {
  164. csum += *p;
  165. p++;
  166. } while (--len);
  167. return csum;
  168. }
  169. static uint32_t image_checksum32(void *start, uint32_t len)
  170. {
  171. uint32_t csum = 0;
  172. uint32_t *p = start;
  173. /* check len and return zero checksum if invalid */
  174. if (!len)
  175. return 0;
  176. if (len % sizeof(uint32_t)) {
  177. fprintf(stderr, "Length %d is not in multiple of %zu\n",
  178. len, sizeof(uint32_t));
  179. return 0;
  180. }
  181. do {
  182. csum += *p;
  183. p++;
  184. len -= sizeof(uint32_t);
  185. } while (len > 0);
  186. return csum;
  187. }
  188. static uint8_t baudrate_to_option(unsigned int baudrate)
  189. {
  190. switch (baudrate) {
  191. case 2400:
  192. return MAIN_HDR_V1_OPT_BAUD_2400;
  193. case 4800:
  194. return MAIN_HDR_V1_OPT_BAUD_4800;
  195. case 9600:
  196. return MAIN_HDR_V1_OPT_BAUD_9600;
  197. case 19200:
  198. return MAIN_HDR_V1_OPT_BAUD_19200;
  199. case 38400:
  200. return MAIN_HDR_V1_OPT_BAUD_38400;
  201. case 57600:
  202. return MAIN_HDR_V1_OPT_BAUD_57600;
  203. case 115200:
  204. return MAIN_HDR_V1_OPT_BAUD_115200;
  205. default:
  206. return MAIN_HDR_V1_OPT_BAUD_DEFAULT;
  207. }
  208. }
  209. static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
  210. int payloadsz)
  211. {
  212. struct image_cfg_element *e;
  213. size_t headersz;
  214. struct main_hdr_v0 *main_hdr;
  215. uint8_t *image;
  216. int has_ext = 0;
  217. /*
  218. * Calculate the size of the header and the size of the
  219. * payload
  220. */
  221. headersz = sizeof(struct main_hdr_v0);
  222. if (image_count_options(IMAGE_CFG_DATA) > 0) {
  223. has_ext = 1;
  224. headersz += sizeof(struct ext_hdr_v0);
  225. }
  226. if (image_count_options(IMAGE_CFG_PAYLOAD) > 1) {
  227. fprintf(stderr, "More than one payload, not possible\n");
  228. return NULL;
  229. }
  230. image = malloc(headersz);
  231. if (!image) {
  232. fprintf(stderr, "Cannot allocate memory for image\n");
  233. return NULL;
  234. }
  235. memset(image, 0, headersz);
  236. main_hdr = (struct main_hdr_v0 *)image;
  237. /* Fill in the main header */
  238. main_hdr->blocksize =
  239. cpu_to_le32(payloadsz + sizeof(uint32_t) - headersz);
  240. main_hdr->srcaddr = cpu_to_le32(headersz);
  241. main_hdr->ext = has_ext;
  242. main_hdr->destaddr = cpu_to_le32(params->addr);
  243. main_hdr->execaddr = cpu_to_le32(params->ep);
  244. e = image_find_option(IMAGE_CFG_BOOT_FROM);
  245. if (e)
  246. main_hdr->blockid = e->bootfrom;
  247. e = image_find_option(IMAGE_CFG_NAND_ECC_MODE);
  248. if (e)
  249. main_hdr->nandeccmode = e->nandeccmode;
  250. e = image_find_option(IMAGE_CFG_NAND_PAGESZ);
  251. if (e)
  252. main_hdr->nandpagesize = cpu_to_le16(e->nandpagesz);
  253. main_hdr->checksum = image_checksum8(image,
  254. sizeof(struct main_hdr_v0));
  255. /* Generate the ext header */
  256. if (has_ext) {
  257. struct ext_hdr_v0 *ext_hdr;
  258. int cfgi, datai;
  259. ext_hdr = (struct ext_hdr_v0 *)
  260. (image + sizeof(struct main_hdr_v0));
  261. ext_hdr->offset = cpu_to_le32(0x40);
  262. for (cfgi = 0, datai = 0; cfgi < cfgn; cfgi++) {
  263. e = &image_cfg[cfgi];
  264. if (e->type != IMAGE_CFG_DATA)
  265. continue;
  266. ext_hdr->rcfg[datai].raddr =
  267. cpu_to_le32(e->regdata.raddr);
  268. ext_hdr->rcfg[datai].rdata =
  269. cpu_to_le32(e->regdata.rdata);
  270. datai++;
  271. }
  272. ext_hdr->checksum = image_checksum8(ext_hdr,
  273. sizeof(struct ext_hdr_v0));
  274. }
  275. *imagesz = headersz;
  276. return image;
  277. }
  278. static size_t image_headersz_v1(int *hasext)
  279. {
  280. struct image_cfg_element *binarye;
  281. size_t headersz;
  282. /*
  283. * Calculate the size of the header and the size of the
  284. * payload
  285. */
  286. headersz = sizeof(struct main_hdr_v1);
  287. if (image_count_options(IMAGE_CFG_BINARY) > 1) {
  288. fprintf(stderr, "More than one binary blob, not supported\n");
  289. return 0;
  290. }
  291. if (image_count_options(IMAGE_CFG_PAYLOAD) > 1) {
  292. fprintf(stderr, "More than one payload, not possible\n");
  293. return 0;
  294. }
  295. binarye = image_find_option(IMAGE_CFG_BINARY);
  296. if (binarye) {
  297. int ret;
  298. struct stat s;
  299. ret = stat(binarye->binary.file, &s);
  300. if (ret < 0) {
  301. char cwd[PATH_MAX];
  302. char *dir = cwd;
  303. memset(cwd, 0, sizeof(cwd));
  304. if (!getcwd(cwd, sizeof(cwd))) {
  305. dir = "current working directory";
  306. perror("getcwd() failed");
  307. }
  308. fprintf(stderr,
  309. "Didn't find the file '%s' in '%s' which is mandatory to generate the image\n"
  310. "This file generally contains the DDR3 training code, and should be extracted from an existing bootable\n"
  311. "image for your board. See 'kwbimage -x' to extract it from an existing image.\n",
  312. binarye->binary.file, dir);
  313. return 0;
  314. }
  315. headersz += sizeof(struct opt_hdr_v1) +
  316. s.st_size +
  317. (binarye->binary.nargs + 2) * sizeof(uint32_t);
  318. if (hasext)
  319. *hasext = 1;
  320. }
  321. #if defined(CONFIG_SYS_U_BOOT_OFFS)
  322. if (headersz > CONFIG_SYS_U_BOOT_OFFS) {
  323. fprintf(stderr,
  324. "Error: Image header (incl. SPL image) too big!\n");
  325. fprintf(stderr, "header=0x%x CONFIG_SYS_U_BOOT_OFFS=0x%x!\n",
  326. (int)headersz, CONFIG_SYS_U_BOOT_OFFS);
  327. fprintf(stderr, "Increase CONFIG_SYS_U_BOOT_OFFS!\n");
  328. return 0;
  329. }
  330. headersz = CONFIG_SYS_U_BOOT_OFFS;
  331. #endif
  332. /*
  333. * The payload should be aligned on some reasonable
  334. * boundary
  335. */
  336. return ALIGN_SUP(headersz, 4096);
  337. }
  338. int add_binary_header_v1(uint8_t *cur)
  339. {
  340. struct image_cfg_element *binarye;
  341. struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)cur;
  342. uint32_t *args;
  343. size_t binhdrsz;
  344. struct stat s;
  345. int argi;
  346. FILE *bin;
  347. int ret;
  348. binarye = image_find_option(IMAGE_CFG_BINARY);
  349. if (!binarye)
  350. return 0;
  351. hdr->headertype = OPT_HDR_V1_BINARY_TYPE;
  352. bin = fopen(binarye->binary.file, "r");
  353. if (!bin) {
  354. fprintf(stderr, "Cannot open binary file %s\n",
  355. binarye->binary.file);
  356. return -1;
  357. }
  358. fstat(fileno(bin), &s);
  359. binhdrsz = sizeof(struct opt_hdr_v1) +
  360. (binarye->binary.nargs + 2) * sizeof(uint32_t) +
  361. s.st_size;
  362. /*
  363. * The size includes the binary image size, rounded
  364. * up to a 4-byte boundary. Plus 4 bytes for the
  365. * next-header byte and 3-byte alignment at the end.
  366. */
  367. binhdrsz = ALIGN_SUP(binhdrsz, 4) + 4;
  368. hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF);
  369. hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16;
  370. cur += sizeof(struct opt_hdr_v1);
  371. args = (uint32_t *)cur;
  372. *args = cpu_to_le32(binarye->binary.nargs);
  373. args++;
  374. for (argi = 0; argi < binarye->binary.nargs; argi++)
  375. args[argi] = cpu_to_le32(binarye->binary.args[argi]);
  376. cur += (binarye->binary.nargs + 1) * sizeof(uint32_t);
  377. ret = fread(cur, s.st_size, 1, bin);
  378. if (ret != 1) {
  379. fprintf(stderr,
  380. "Could not read binary image %s\n",
  381. binarye->binary.file);
  382. return -1;
  383. }
  384. fclose(bin);
  385. cur += ALIGN_SUP(s.st_size, 4);
  386. /*
  387. * For now, we don't support more than one binary
  388. * header, and no other header types are
  389. * supported. So, the binary header is necessarily the
  390. * last one
  391. */
  392. *((uint32_t *)cur) = 0x00000000;
  393. cur += sizeof(uint32_t);
  394. return 0;
  395. }
  396. static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
  397. int payloadsz)
  398. {
  399. struct image_cfg_element *e;
  400. struct main_hdr_v1 *main_hdr;
  401. size_t headersz;
  402. uint8_t *image, *cur;
  403. int hasext = 0;
  404. /*
  405. * Calculate the size of the header and the size of the
  406. * payload
  407. */
  408. headersz = image_headersz_v1(&hasext);
  409. if (headersz == 0)
  410. return NULL;
  411. image = malloc(headersz);
  412. if (!image) {
  413. fprintf(stderr, "Cannot allocate memory for image\n");
  414. return NULL;
  415. }
  416. memset(image, 0, headersz);
  417. main_hdr = (struct main_hdr_v1 *)image;
  418. cur = image + sizeof(struct main_hdr_v1);
  419. /* Fill the main header */
  420. main_hdr->blocksize =
  421. cpu_to_le32(payloadsz - headersz + sizeof(uint32_t));
  422. main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF);
  423. main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16;
  424. main_hdr->destaddr = cpu_to_le32(params->addr)
  425. - sizeof(image_header_t);
  426. main_hdr->execaddr = cpu_to_le32(params->ep);
  427. main_hdr->srcaddr = cpu_to_le32(headersz);
  428. main_hdr->ext = hasext;
  429. main_hdr->version = 1;
  430. e = image_find_option(IMAGE_CFG_BOOT_FROM);
  431. if (e)
  432. main_hdr->blockid = e->bootfrom;
  433. e = image_find_option(IMAGE_CFG_NAND_BLKSZ);
  434. if (e)
  435. main_hdr->nandblocksize = e->nandblksz / (64 * 1024);
  436. e = image_find_option(IMAGE_CFG_NAND_BADBLK_LOCATION);
  437. if (e)
  438. main_hdr->nandbadblklocation = e->nandbadblklocation;
  439. e = image_find_option(IMAGE_CFG_BAUDRATE);
  440. if (e)
  441. main_hdr->options = baudrate_to_option(e->baudrate);
  442. e = image_find_option(IMAGE_CFG_DEBUG);
  443. if (e)
  444. main_hdr->flags = e->debug ? 0x1 : 0;
  445. if (add_binary_header_v1(cur))
  446. return NULL;
  447. /* Calculate and set the header checksum */
  448. main_hdr->checksum = image_checksum8(main_hdr, headersz);
  449. *imagesz = headersz;
  450. return image;
  451. }
  452. int recognize_keyword(char *keyword)
  453. {
  454. int kw_id;
  455. for (kw_id = 1; kw_id < IMAGE_CFG_COUNT; ++kw_id)
  456. if (!strcmp(keyword, id_strs[kw_id]))
  457. return kw_id;
  458. return 0;
  459. }
  460. static int image_create_config_parse_oneline(char *line,
  461. struct image_cfg_element *el)
  462. {
  463. char *keyword, *saveptr, *value1, *value2;
  464. char delimiters[] = " \t";
  465. int keyword_id, ret, argi;
  466. char *unknown_msg = "Ignoring unknown line '%s'\n";
  467. keyword = strtok_r(line, delimiters, &saveptr);
  468. keyword_id = recognize_keyword(keyword);
  469. if (!keyword_id) {
  470. fprintf(stderr, unknown_msg, line);
  471. return 0;
  472. }
  473. el->type = keyword_id;
  474. value1 = strtok_r(NULL, delimiters, &saveptr);
  475. if (!value1) {
  476. fprintf(stderr, "Parameter missing in line '%s'\n", line);
  477. return -1;
  478. }
  479. switch (keyword_id) {
  480. case IMAGE_CFG_VERSION:
  481. el->version = atoi(value1);
  482. break;
  483. case IMAGE_CFG_BOOT_FROM:
  484. ret = image_boot_mode_id(value1);
  485. if (ret < 0) {
  486. fprintf(stderr, "Invalid boot media '%s'\n", value1);
  487. return -1;
  488. }
  489. el->bootfrom = ret;
  490. break;
  491. case IMAGE_CFG_NAND_BLKSZ:
  492. el->nandblksz = strtoul(value1, NULL, 16);
  493. break;
  494. case IMAGE_CFG_NAND_BADBLK_LOCATION:
  495. el->nandbadblklocation = strtoul(value1, NULL, 16);
  496. break;
  497. case IMAGE_CFG_NAND_ECC_MODE:
  498. ret = image_nand_ecc_mode_id(value1);
  499. if (ret < 0) {
  500. fprintf(stderr, "Invalid NAND ECC mode '%s'\n", value1);
  501. return -1;
  502. }
  503. el->nandeccmode = ret;
  504. break;
  505. case IMAGE_CFG_NAND_PAGESZ:
  506. el->nandpagesz = strtoul(value1, NULL, 16);
  507. break;
  508. case IMAGE_CFG_BINARY:
  509. argi = 0;
  510. el->binary.file = strdup(value1);
  511. while (1) {
  512. char *value = strtok_r(NULL, delimiters, &saveptr);
  513. if (!value)
  514. break;
  515. el->binary.args[argi] = strtoul(value, NULL, 16);
  516. argi++;
  517. if (argi >= BINARY_MAX_ARGS) {
  518. fprintf(stderr,
  519. "Too many arguments for BINARY\n");
  520. return -1;
  521. }
  522. }
  523. el->binary.nargs = argi;
  524. break;
  525. case IMAGE_CFG_DATA:
  526. value2 = strtok_r(NULL, delimiters, &saveptr);
  527. if (!value1 || !value2) {
  528. fprintf(stderr,
  529. "Invalid number of arguments for DATA\n");
  530. return -1;
  531. }
  532. el->regdata.raddr = strtoul(value1, NULL, 16);
  533. el->regdata.rdata = strtoul(value2, NULL, 16);
  534. break;
  535. case IMAGE_CFG_BAUDRATE:
  536. el->baudrate = strtoul(value1, NULL, 10);
  537. break;
  538. case IMAGE_CFG_DEBUG:
  539. el->debug = strtoul(value1, NULL, 10);
  540. break;
  541. default:
  542. fprintf(stderr, unknown_msg, line);
  543. }
  544. return 0;
  545. }
  546. /*
  547. * Parse the configuration file 'fcfg' into the array of configuration
  548. * elements 'image_cfg', and return the number of configuration
  549. * elements in 'cfgn'.
  550. */
  551. static int image_create_config_parse(FILE *fcfg)
  552. {
  553. int ret;
  554. int cfgi = 0;
  555. /* Parse the configuration file */
  556. while (!feof(fcfg)) {
  557. char *line;
  558. char buf[256];
  559. /* Read the current line */
  560. memset(buf, 0, sizeof(buf));
  561. line = fgets(buf, sizeof(buf), fcfg);
  562. if (!line)
  563. break;
  564. /* Ignore useless lines */
  565. if (line[0] == '\n' || line[0] == '#')
  566. continue;
  567. /* Strip final newline */
  568. if (line[strlen(line) - 1] == '\n')
  569. line[strlen(line) - 1] = 0;
  570. /* Parse the current line */
  571. ret = image_create_config_parse_oneline(line,
  572. &image_cfg[cfgi]);
  573. if (ret)
  574. return ret;
  575. cfgi++;
  576. if (cfgi >= IMAGE_CFG_ELEMENT_MAX) {
  577. fprintf(stderr,
  578. "Too many configuration elements in .cfg file\n");
  579. return -1;
  580. }
  581. }
  582. cfgn = cfgi;
  583. return 0;
  584. }
  585. static int image_get_version(void)
  586. {
  587. struct image_cfg_element *e;
  588. e = image_find_option(IMAGE_CFG_VERSION);
  589. if (!e)
  590. return -1;
  591. return e->version;
  592. }
  593. static int image_version_file(const char *input)
  594. {
  595. FILE *fcfg;
  596. int version;
  597. int ret;
  598. fcfg = fopen(input, "r");
  599. if (!fcfg) {
  600. fprintf(stderr, "Could not open input file %s\n", input);
  601. return -1;
  602. }
  603. image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
  604. sizeof(struct image_cfg_element));
  605. if (!image_cfg) {
  606. fprintf(stderr, "Cannot allocate memory\n");
  607. fclose(fcfg);
  608. return -1;
  609. }
  610. memset(image_cfg, 0,
  611. IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element));
  612. rewind(fcfg);
  613. ret = image_create_config_parse(fcfg);
  614. fclose(fcfg);
  615. if (ret) {
  616. free(image_cfg);
  617. return -1;
  618. }
  619. version = image_get_version();
  620. /* Fallback to version 0 is no version is provided in the cfg file */
  621. if (version == -1)
  622. version = 0;
  623. free(image_cfg);
  624. return version;
  625. }
  626. static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd,
  627. struct image_tool_params *params)
  628. {
  629. FILE *fcfg;
  630. void *image = NULL;
  631. int version;
  632. size_t headersz = 0;
  633. uint32_t checksum;
  634. int ret;
  635. int size;
  636. fcfg = fopen(params->imagename, "r");
  637. if (!fcfg) {
  638. fprintf(stderr, "Could not open input file %s\n",
  639. params->imagename);
  640. exit(EXIT_FAILURE);
  641. }
  642. image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX *
  643. sizeof(struct image_cfg_element));
  644. if (!image_cfg) {
  645. fprintf(stderr, "Cannot allocate memory\n");
  646. fclose(fcfg);
  647. exit(EXIT_FAILURE);
  648. }
  649. memset(image_cfg, 0,
  650. IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element));
  651. rewind(fcfg);
  652. ret = image_create_config_parse(fcfg);
  653. fclose(fcfg);
  654. if (ret) {
  655. free(image_cfg);
  656. exit(EXIT_FAILURE);
  657. }
  658. /* The MVEBU BootROM does not allow non word aligned payloads */
  659. sbuf->st_size = ALIGN_SUP(sbuf->st_size, 4);
  660. version = image_get_version();
  661. switch (version) {
  662. /*
  663. * Fallback to version 0 if no version is provided in the
  664. * cfg file
  665. */
  666. case -1:
  667. case 0:
  668. image = image_create_v0(&headersz, params, sbuf->st_size);
  669. break;
  670. case 1:
  671. image = image_create_v1(&headersz, params, sbuf->st_size);
  672. break;
  673. default:
  674. fprintf(stderr, "Unsupported version %d\n", version);
  675. free(image_cfg);
  676. exit(EXIT_FAILURE);
  677. }
  678. if (!image) {
  679. fprintf(stderr, "Could not create image\n");
  680. free(image_cfg);
  681. exit(EXIT_FAILURE);
  682. }
  683. free(image_cfg);
  684. /* Build and add image checksum header */
  685. checksum =
  686. cpu_to_le32(image_checksum32((uint32_t *)ptr, sbuf->st_size));
  687. size = write(ifd, &checksum, sizeof(uint32_t));
  688. if (size != sizeof(uint32_t)) {
  689. fprintf(stderr, "Error:%s - Checksum write %d bytes %s\n",
  690. params->cmdname, size, params->imagefile);
  691. exit(EXIT_FAILURE);
  692. }
  693. sbuf->st_size += sizeof(uint32_t);
  694. /* Finally copy the header into the image area */
  695. memcpy(ptr, image, headersz);
  696. free(image);
  697. }
  698. static void kwbimage_print_header(const void *ptr)
  699. {
  700. struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr;
  701. printf("Image Type: MVEBU Boot from %s Image\n",
  702. image_boot_mode_name(mhdr->blockid));
  703. printf("Image version:%d\n", image_version((void *)ptr));
  704. printf("Data Size: ");
  705. genimg_print_size(mhdr->blocksize - sizeof(uint32_t));
  706. printf("Load Address: %08x\n", mhdr->destaddr);
  707. printf("Entry Point: %08x\n", mhdr->execaddr);
  708. }
  709. static int kwbimage_check_image_types(uint8_t type)
  710. {
  711. if (type == IH_TYPE_KWBIMAGE)
  712. return EXIT_SUCCESS;
  713. return EXIT_FAILURE;
  714. }
  715. static int kwbimage_verify_header(unsigned char *ptr, int image_size,
  716. struct image_tool_params *params)
  717. {
  718. struct main_hdr_v0 *main_hdr;
  719. uint8_t checksum;
  720. main_hdr = (struct main_hdr_v0 *)ptr;
  721. checksum = image_checksum8(ptr,
  722. sizeof(struct main_hdr_v0)
  723. - sizeof(uint8_t));
  724. if (checksum != main_hdr->checksum)
  725. return -FDT_ERR_BADSTRUCTURE;
  726. /* Only version 0 extended header has checksum */
  727. if (image_version((void *)ptr) == 0) {
  728. struct ext_hdr_v0 *ext_hdr;
  729. ext_hdr = (struct ext_hdr_v0 *)
  730. (ptr + sizeof(struct main_hdr_v0));
  731. checksum = image_checksum8(ext_hdr,
  732. sizeof(struct ext_hdr_v0)
  733. - sizeof(uint8_t));
  734. if (checksum != ext_hdr->checksum)
  735. return -FDT_ERR_BADSTRUCTURE;
  736. }
  737. return 0;
  738. }
  739. static int kwbimage_generate(struct image_tool_params *params,
  740. struct image_type_params *tparams)
  741. {
  742. int alloc_len;
  743. void *hdr;
  744. int version = 0;
  745. version = image_version_file(params->imagename);
  746. if (version == 0) {
  747. alloc_len = sizeof(struct main_hdr_v0) +
  748. sizeof(struct ext_hdr_v0);
  749. } else {
  750. alloc_len = image_headersz_v1(NULL);
  751. }
  752. hdr = malloc(alloc_len);
  753. if (!hdr) {
  754. fprintf(stderr, "%s: malloc return failure: %s\n",
  755. params->cmdname, strerror(errno));
  756. exit(EXIT_FAILURE);
  757. }
  758. memset(hdr, 0, alloc_len);
  759. tparams->header_size = alloc_len;
  760. tparams->hdr = hdr;
  761. /*
  762. * The resulting image needs to be 4-byte aligned. At least
  763. * the Marvell hdrparser tool complains if its unaligned.
  764. * By returning 1 here in this function, called via
  765. * tparams->vrec_header() in mkimage.c, mkimage will
  766. * automatically pad the the resulting image to a 4-byte
  767. * size if necessary.
  768. */
  769. return 1;
  770. }
  771. /*
  772. * Report Error if xflag is set in addition to default
  773. */
  774. static int kwbimage_check_params(struct image_tool_params *params)
  775. {
  776. if (!strlen(params->imagename)) {
  777. char *msg = "Configuration file for kwbimage creation omitted";
  778. fprintf(stderr, "Error:%s - %s\n", params->cmdname, msg);
  779. return CFG_INVALID;
  780. }
  781. return (params->dflag && (params->fflag || params->lflag)) ||
  782. (params->fflag && (params->dflag || params->lflag)) ||
  783. (params->lflag && (params->dflag || params->fflag)) ||
  784. (params->xflag) || !(strlen(params->imagename));
  785. }
  786. /*
  787. * kwbimage type parameters definition
  788. */
  789. U_BOOT_IMAGE_TYPE(
  790. kwbimage,
  791. "Marvell MVEBU Boot Image support",
  792. 0,
  793. NULL,
  794. kwbimage_check_params,
  795. kwbimage_verify_header,
  796. kwbimage_print_header,
  797. kwbimage_set_header,
  798. NULL,
  799. kwbimage_check_image_types,
  800. NULL,
  801. kwbimage_generate
  802. );