kwbimage.c 21 KB

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