imximage.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /*
  2. * (C) Copyright 2009
  3. * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
  4. *
  5. * (C) Copyright 2008
  6. * Marvell Semiconductor <www.marvell.com>
  7. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include "imagetool.h"
  12. #include <image.h>
  13. #include "imximage.h"
  14. #define UNDEFINED 0xFFFFFFFF
  15. /*
  16. * Supported commands for configuration file
  17. */
  18. static table_entry_t imximage_cmds[] = {
  19. {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
  20. {CMD_BOOT_OFFSET, "BOOT_OFFSET", "Boot offset", },
  21. {CMD_WRITE_DATA, "DATA", "Reg Write Data", },
  22. {CMD_WRITE_CLR_BIT, "CLR_BIT", "Reg clear bit", },
  23. {CMD_CHECK_BITS_SET, "CHECK_BITS_SET", "Reg Check bits set", },
  24. {CMD_CHECK_BITS_CLR, "CHECK_BITS_CLR", "Reg Check bits clr", },
  25. {CMD_CSF, "CSF", "Command Sequence File", },
  26. {CMD_IMAGE_VERSION, "IMAGE_VERSION", "image version", },
  27. {CMD_PLUGIN, "PLUGIN", "file plugin_addr", },
  28. {-1, "", "", },
  29. };
  30. /*
  31. * Supported Boot options for configuration file
  32. * this is needed to set the correct flash offset
  33. */
  34. static table_entry_t imximage_boot_offset[] = {
  35. {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",},
  36. {FLASH_OFFSET_NAND, "nand", "NAND Flash", },
  37. {FLASH_OFFSET_NOR, "nor", "NOR Flash", },
  38. {FLASH_OFFSET_SATA, "sata", "SATA Disk", },
  39. {FLASH_OFFSET_SD, "sd", "SD Card", },
  40. {FLASH_OFFSET_SPI, "spi", "SPI Flash", },
  41. {FLASH_OFFSET_QSPI, "qspi", "QSPI NOR Flash",},
  42. {-1, "", "Invalid", },
  43. };
  44. /*
  45. * Supported Boot options for configuration file
  46. * this is needed to determine the initial load size
  47. */
  48. static table_entry_t imximage_boot_loadsize[] = {
  49. {FLASH_LOADSIZE_ONENAND, "onenand", "OneNAND Flash",},
  50. {FLASH_LOADSIZE_NAND, "nand", "NAND Flash", },
  51. {FLASH_LOADSIZE_NOR, "nor", "NOR Flash", },
  52. {FLASH_LOADSIZE_SATA, "sata", "SATA Disk", },
  53. {FLASH_LOADSIZE_SD, "sd", "SD Card", },
  54. {FLASH_LOADSIZE_SPI, "spi", "SPI Flash", },
  55. {FLASH_LOADSIZE_QSPI, "qspi", "QSPI NOR Flash",},
  56. {-1, "", "Invalid", },
  57. };
  58. /*
  59. * IMXIMAGE version definition for i.MX chips
  60. */
  61. static table_entry_t imximage_versions[] = {
  62. {IMXIMAGE_V1, "", " (i.MX25/35/51 compatible)", },
  63. {IMXIMAGE_V2, "", " (i.MX53/6/7 compatible)", },
  64. {-1, "", " (Invalid)", },
  65. };
  66. static struct imx_header imximage_header;
  67. static uint32_t imximage_version;
  68. /*
  69. * Image Vector Table Offset
  70. * Initialized to a wrong not 4-bytes aligned address to
  71. * check if it is was set by the cfg file.
  72. */
  73. static uint32_t imximage_ivt_offset = UNDEFINED;
  74. static uint32_t imximage_csf_size = UNDEFINED;
  75. /* Initial Load Region Size */
  76. static uint32_t imximage_init_loadsize;
  77. static uint32_t imximage_iram_free_start;
  78. static uint32_t imximage_plugin_size;
  79. static uint32_t plugin_image;
  80. static set_dcd_val_t set_dcd_val;
  81. static set_dcd_param_t set_dcd_param;
  82. static set_dcd_rst_t set_dcd_rst;
  83. static set_imx_hdr_t set_imx_hdr;
  84. static uint32_t max_dcd_entries;
  85. static uint32_t *header_size_ptr;
  86. static uint32_t *csf_ptr;
  87. static uint32_t get_cfg_value(char *token, char *name, int linenr)
  88. {
  89. char *endptr;
  90. uint32_t value;
  91. errno = 0;
  92. value = strtoul(token, &endptr, 16);
  93. if (errno || (token == endptr)) {
  94. fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
  95. name, linenr, token);
  96. exit(EXIT_FAILURE);
  97. }
  98. return value;
  99. }
  100. static uint32_t detect_imximage_version(struct imx_header *imx_hdr)
  101. {
  102. imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
  103. imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
  104. flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
  105. flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
  106. /* Try to detect V1 */
  107. if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) &&
  108. (hdr_v1->dcd_table.preamble.barker == DCD_BARKER))
  109. return IMXIMAGE_V1;
  110. /* Try to detect V2 */
  111. if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
  112. (hdr_v2->data.dcd_table.header.tag == DCD_HEADER_TAG))
  113. return IMXIMAGE_V2;
  114. if ((fhdr_v2->header.tag == IVT_HEADER_TAG) &&
  115. hdr_v2->boot_data.plugin)
  116. return IMXIMAGE_V2;
  117. return IMXIMAGE_VER_INVALID;
  118. }
  119. static void err_imximage_version(int version)
  120. {
  121. fprintf(stderr,
  122. "Error: Unsupported imximage version:%d\n", version);
  123. exit(EXIT_FAILURE);
  124. }
  125. static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno,
  126. int fld, uint32_t value, uint32_t off)
  127. {
  128. dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
  129. switch (fld) {
  130. case CFG_REG_SIZE:
  131. /* Byte, halfword, word */
  132. if ((value != 1) && (value != 2) && (value != 4)) {
  133. fprintf(stderr, "Error: %s[%d] - "
  134. "Invalid register size " "(%d)\n",
  135. name, lineno, value);
  136. exit(EXIT_FAILURE);
  137. }
  138. dcd_v1->addr_data[off].type = value;
  139. break;
  140. case CFG_REG_ADDRESS:
  141. dcd_v1->addr_data[off].addr = value;
  142. break;
  143. case CFG_REG_VALUE:
  144. dcd_v1->addr_data[off].value = value;
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. static struct dcd_v2_cmd *gd_last_cmd;
  151. static void set_dcd_param_v2(struct imx_header *imxhdr, uint32_t dcd_len,
  152. int32_t cmd)
  153. {
  154. dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.data.dcd_table;
  155. struct dcd_v2_cmd *d = gd_last_cmd;
  156. struct dcd_v2_cmd *d2;
  157. int len;
  158. if (!d)
  159. d = &dcd_v2->dcd_cmd;
  160. d2 = d;
  161. len = be16_to_cpu(d->write_dcd_command.length);
  162. if (len > 4)
  163. d2 = (struct dcd_v2_cmd *)(((char *)d) + len);
  164. switch (cmd) {
  165. case CMD_WRITE_DATA:
  166. if ((d->write_dcd_command.tag == DCD_WRITE_DATA_COMMAND_TAG) &&
  167. (d->write_dcd_command.param == DCD_WRITE_DATA_PARAM))
  168. break;
  169. d = d2;
  170. d->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG;
  171. d->write_dcd_command.length = cpu_to_be16(4);
  172. d->write_dcd_command.param = DCD_WRITE_DATA_PARAM;
  173. break;
  174. case CMD_WRITE_CLR_BIT:
  175. if ((d->write_dcd_command.tag == DCD_WRITE_DATA_COMMAND_TAG) &&
  176. (d->write_dcd_command.param == DCD_WRITE_CLR_BIT_PARAM))
  177. break;
  178. d = d2;
  179. d->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG;
  180. d->write_dcd_command.length = cpu_to_be16(4);
  181. d->write_dcd_command.param = DCD_WRITE_CLR_BIT_PARAM;
  182. break;
  183. /*
  184. * Check data command only supports one entry,
  185. */
  186. case CMD_CHECK_BITS_SET:
  187. d = d2;
  188. d->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG;
  189. d->write_dcd_command.length = cpu_to_be16(4);
  190. d->write_dcd_command.param = DCD_CHECK_BITS_SET_PARAM;
  191. break;
  192. case CMD_CHECK_BITS_CLR:
  193. d = d2;
  194. d->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG;
  195. d->write_dcd_command.length = cpu_to_be16(4);
  196. d->write_dcd_command.param = DCD_CHECK_BITS_CLR_PARAM;
  197. break;
  198. default:
  199. break;
  200. }
  201. gd_last_cmd = d;
  202. }
  203. static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno,
  204. int fld, uint32_t value, uint32_t off)
  205. {
  206. struct dcd_v2_cmd *d = gd_last_cmd;
  207. int len;
  208. len = be16_to_cpu(d->write_dcd_command.length);
  209. off = (len - 4) >> 3;
  210. switch (fld) {
  211. case CFG_REG_ADDRESS:
  212. d->addr_data[off].addr = cpu_to_be32(value);
  213. break;
  214. case CFG_REG_VALUE:
  215. d->addr_data[off].value = cpu_to_be32(value);
  216. off++;
  217. d->write_dcd_command.length = cpu_to_be16((off << 3) + 4);
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. /*
  224. * Complete setting up the rest field of DCD of V1
  225. * such as barker code and DCD data length.
  226. */
  227. static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len,
  228. char *name, int lineno)
  229. {
  230. dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table;
  231. dcd_v1->preamble.barker = DCD_BARKER;
  232. dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t);
  233. }
  234. /*
  235. * Complete setting up the reset field of DCD of V2
  236. * such as DCD tag, version, length, etc.
  237. */
  238. static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len,
  239. char *name, int lineno)
  240. {
  241. if (!imxhdr->header.hdr_v2.boot_data.plugin) {
  242. dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.data.dcd_table;
  243. struct dcd_v2_cmd *d = gd_last_cmd;
  244. int len;
  245. if (!d)
  246. d = &dcd_v2->dcd_cmd;
  247. len = be16_to_cpu(d->write_dcd_command.length);
  248. if (len > 4)
  249. d = (struct dcd_v2_cmd *)(((char *)d) + len);
  250. len = (char *)d - (char *)&dcd_v2->header;
  251. dcd_v2->header.tag = DCD_HEADER_TAG;
  252. dcd_v2->header.length = cpu_to_be16(len);
  253. dcd_v2->header.version = DCD_VERSION;
  254. }
  255. }
  256. static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len,
  257. uint32_t entry_point, uint32_t flash_offset)
  258. {
  259. imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1;
  260. flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
  261. dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
  262. uint32_t hdr_base;
  263. uint32_t header_length = (((char *)&dcd_v1->addr_data[dcd_len].addr)
  264. - ((char *)imxhdr));
  265. /* Set magic number */
  266. fhdr_v1->app_code_barker = APP_CODE_BARKER;
  267. /* TODO: check i.MX image V1 handling, for now use 'old' style */
  268. hdr_base = entry_point - 4096;
  269. fhdr_v1->app_dest_ptr = hdr_base - flash_offset;
  270. fhdr_v1->app_code_jump_vector = entry_point;
  271. fhdr_v1->dcd_ptr_ptr = hdr_base + offsetof(flash_header_v1_t, dcd_ptr);
  272. fhdr_v1->dcd_ptr = hdr_base + offsetof(imx_header_v1_t, dcd_table);
  273. /* Security feature are not supported */
  274. fhdr_v1->app_code_csf = 0;
  275. fhdr_v1->super_root_key = 0;
  276. header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4);
  277. }
  278. static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
  279. uint32_t entry_point, uint32_t flash_offset)
  280. {
  281. imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
  282. flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
  283. uint32_t hdr_base;
  284. /* Set magic number */
  285. fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
  286. fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
  287. fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
  288. if (!hdr_v2->boot_data.plugin) {
  289. fhdr_v2->entry = entry_point;
  290. fhdr_v2->reserved1 = 0;
  291. fhdr_v2->reserved1 = 0;
  292. hdr_base = entry_point - imximage_init_loadsize +
  293. flash_offset;
  294. fhdr_v2->self = hdr_base;
  295. if (dcd_len > 0)
  296. fhdr_v2->dcd_ptr = hdr_base +
  297. offsetof(imx_header_v2_t, data);
  298. else
  299. fhdr_v2->dcd_ptr = 0;
  300. fhdr_v2->boot_data_ptr = hdr_base
  301. + offsetof(imx_header_v2_t, boot_data);
  302. hdr_v2->boot_data.start = entry_point - imximage_init_loadsize;
  303. fhdr_v2->csf = 0;
  304. header_size_ptr = &hdr_v2->boot_data.size;
  305. csf_ptr = &fhdr_v2->csf;
  306. } else {
  307. imx_header_v2_t *next_hdr_v2;
  308. flash_header_v2_t *next_fhdr_v2;
  309. if (imximage_csf_size != 0) {
  310. fprintf(stderr, "Error: Header v2: SECURE_BOOT is only supported in DCD mode!");
  311. exit(EXIT_FAILURE);
  312. }
  313. fhdr_v2->entry = imximage_iram_free_start +
  314. flash_offset + sizeof(flash_header_v2_t) +
  315. sizeof(boot_data_t);
  316. fhdr_v2->reserved1 = 0;
  317. fhdr_v2->reserved2 = 0;
  318. fhdr_v2->self = imximage_iram_free_start + flash_offset;
  319. fhdr_v2->dcd_ptr = 0;
  320. fhdr_v2->boot_data_ptr = fhdr_v2->self +
  321. offsetof(imx_header_v2_t, boot_data);
  322. hdr_v2->boot_data.start = imximage_iram_free_start;
  323. /*
  324. * The actural size of plugin image is "imximage_plugin_size +
  325. * sizeof(flash_header_v2_t) + sizeof(boot_data_t)", plus the
  326. * flash_offset space.The ROM code only need to copy this size
  327. * to run the plugin code. However, later when copy the whole
  328. * U-Boot image to DDR, the ROM code use memcpy to copy the
  329. * first part of the image, and use the storage read function
  330. * to get the remaining part. This requires the dividing point
  331. * must be multiple of storage sector size. Here we set the
  332. * first section to be MAX_PLUGIN_CODE_SIZE(64KB) for this
  333. * purpose.
  334. */
  335. hdr_v2->boot_data.size = MAX_PLUGIN_CODE_SIZE;
  336. /* Security feature are not supported */
  337. fhdr_v2->csf = 0;
  338. next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 +
  339. imximage_plugin_size);
  340. next_fhdr_v2 = &next_hdr_v2->fhdr;
  341. next_fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
  342. next_fhdr_v2->header.length =
  343. cpu_to_be16(sizeof(flash_header_v2_t));
  344. next_fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
  345. next_fhdr_v2->entry = entry_point;
  346. hdr_base = entry_point - sizeof(struct imx_header);
  347. next_fhdr_v2->reserved1 = 0;
  348. next_fhdr_v2->reserved2 = 0;
  349. next_fhdr_v2->self = hdr_base + imximage_plugin_size;
  350. next_fhdr_v2->dcd_ptr = 0;
  351. next_fhdr_v2->boot_data_ptr = next_fhdr_v2->self +
  352. offsetof(imx_header_v2_t, boot_data);
  353. next_hdr_v2->boot_data.start = hdr_base - flash_offset;
  354. header_size_ptr = &next_hdr_v2->boot_data.size;
  355. next_hdr_v2->boot_data.plugin = 0;
  356. next_fhdr_v2->csf = 0;
  357. }
  358. }
  359. static void set_hdr_func(void)
  360. {
  361. switch (imximage_version) {
  362. case IMXIMAGE_V1:
  363. set_dcd_val = set_dcd_val_v1;
  364. set_dcd_param = NULL;
  365. set_dcd_rst = set_dcd_rst_v1;
  366. set_imx_hdr = set_imx_hdr_v1;
  367. max_dcd_entries = MAX_HW_CFG_SIZE_V1;
  368. break;
  369. case IMXIMAGE_V2:
  370. gd_last_cmd = NULL;
  371. set_dcd_val = set_dcd_val_v2;
  372. set_dcd_param = set_dcd_param_v2;
  373. set_dcd_rst = set_dcd_rst_v2;
  374. set_imx_hdr = set_imx_hdr_v2;
  375. max_dcd_entries = MAX_HW_CFG_SIZE_V2;
  376. break;
  377. default:
  378. err_imximage_version(imximage_version);
  379. break;
  380. }
  381. }
  382. static void print_hdr_v1(struct imx_header *imx_hdr)
  383. {
  384. imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
  385. flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
  386. dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
  387. uint32_t size, length, ver;
  388. size = dcd_v1->preamble.length;
  389. if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
  390. fprintf(stderr,
  391. "Error: Image corrupt DCD size %d exceed maximum %d\n",
  392. (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
  393. MAX_HW_CFG_SIZE_V1);
  394. exit(EXIT_FAILURE);
  395. }
  396. length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
  397. ver = detect_imximage_version(imx_hdr);
  398. printf("Image Type: Freescale IMX Boot Image\n");
  399. printf("Image Ver: %x", ver);
  400. printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
  401. printf("Data Size: ");
  402. genimg_print_size(dcd_v1->addr_data[length].type);
  403. printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
  404. printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
  405. }
  406. static void print_hdr_v2(struct imx_header *imx_hdr)
  407. {
  408. imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
  409. flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
  410. dcd_v2_t *dcd_v2 = &hdr_v2->data.dcd_table;
  411. uint32_t size, version, plugin;
  412. plugin = hdr_v2->boot_data.plugin;
  413. if (!plugin) {
  414. size = be16_to_cpu(dcd_v2->header.length);
  415. if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
  416. fprintf(stderr,
  417. "Error: Image corrupt DCD size %d exceed maximum %d\n",
  418. (uint32_t)(size / sizeof(dcd_addr_data_t)),
  419. MAX_HW_CFG_SIZE_V2);
  420. exit(EXIT_FAILURE);
  421. }
  422. }
  423. version = detect_imximage_version(imx_hdr);
  424. printf("Image Type: Freescale IMX Boot Image\n");
  425. printf("Image Ver: %x", version);
  426. printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
  427. printf("Mode: %s\n", plugin ? "PLUGIN" : "DCD");
  428. if (!plugin) {
  429. printf("Data Size: ");
  430. genimg_print_size(hdr_v2->boot_data.size);
  431. printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
  432. printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
  433. if (fhdr_v2->csf && (imximage_ivt_offset != UNDEFINED) &&
  434. (imximage_csf_size != UNDEFINED)) {
  435. uint16_t dcdlen;
  436. int offs;
  437. dcdlen = hdr_v2->data.dcd_table.header.length;
  438. offs = (char *)&hdr_v2->data.dcd_table
  439. - (char *)hdr_v2;
  440. printf("HAB Blocks: %08x %08x %08x\n",
  441. (uint32_t)fhdr_v2->self, 0,
  442. hdr_v2->boot_data.size - imximage_ivt_offset -
  443. imximage_csf_size);
  444. printf("DCD Blocks: 00910000 %08x %08x\n",
  445. offs, be16_to_cpu(dcdlen));
  446. }
  447. } else {
  448. imx_header_v2_t *next_hdr_v2;
  449. flash_header_v2_t *next_fhdr_v2;
  450. /*First Header*/
  451. printf("Plugin Data Size: ");
  452. genimg_print_size(hdr_v2->boot_data.size);
  453. printf("Plugin Code Size: ");
  454. genimg_print_size(imximage_plugin_size);
  455. printf("Plugin Load Address: %08x\n", hdr_v2->boot_data.start);
  456. printf("Plugin Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
  457. /*Second Header*/
  458. next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 +
  459. imximage_plugin_size);
  460. next_fhdr_v2 = &next_hdr_v2->fhdr;
  461. printf("U-Boot Data Size: ");
  462. genimg_print_size(next_hdr_v2->boot_data.size);
  463. printf("U-Boot Load Address: %08x\n",
  464. next_hdr_v2->boot_data.start);
  465. printf("U-Boot Entry Point: %08x\n",
  466. (uint32_t)next_fhdr_v2->entry);
  467. }
  468. }
  469. static void copy_plugin_code(struct imx_header *imxhdr, char *plugin_file)
  470. {
  471. int ifd;
  472. struct stat sbuf;
  473. char *plugin_buf = imxhdr->header.hdr_v2.data.plugin_code;
  474. char *ptr;
  475. ifd = open(plugin_file, O_RDONLY|O_BINARY);
  476. if (ifd < 0) {
  477. fprintf(stderr, "Can't open %s: %s\n",
  478. plugin_file,
  479. strerror(errno));
  480. exit(EXIT_FAILURE);
  481. }
  482. if (fstat(ifd, &sbuf) < 0) {
  483. fprintf(stderr, "Can't stat %s: %s\n",
  484. plugin_file,
  485. strerror(errno));
  486. exit(EXIT_FAILURE);
  487. }
  488. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  489. if (ptr == MAP_FAILED) {
  490. fprintf(stderr, "Can't read %s: %s\n",
  491. plugin_file,
  492. strerror(errno));
  493. exit(EXIT_FAILURE);
  494. }
  495. if (sbuf.st_size > MAX_PLUGIN_CODE_SIZE) {
  496. printf("plugin binary size too large\n");
  497. exit(EXIT_FAILURE);
  498. }
  499. memcpy(plugin_buf, ptr, sbuf.st_size);
  500. imximage_plugin_size = sbuf.st_size;
  501. (void) munmap((void *)ptr, sbuf.st_size);
  502. (void) close(ifd);
  503. imxhdr->header.hdr_v2.boot_data.plugin = 1;
  504. }
  505. static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
  506. char *name, int lineno, int fld, int dcd_len)
  507. {
  508. int value;
  509. static int cmd_ver_first = ~0;
  510. switch (cmd) {
  511. case CMD_IMAGE_VERSION:
  512. imximage_version = get_cfg_value(token, name, lineno);
  513. if (cmd_ver_first == 0) {
  514. fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
  515. "command need be the first before other "
  516. "valid command in the file\n", name, lineno);
  517. exit(EXIT_FAILURE);
  518. }
  519. cmd_ver_first = 1;
  520. set_hdr_func();
  521. break;
  522. case CMD_BOOT_FROM:
  523. imximage_ivt_offset = get_table_entry_id(imximage_boot_offset,
  524. "imximage boot option", token);
  525. if (imximage_ivt_offset == -1) {
  526. fprintf(stderr, "Error: %s[%d] -Invalid boot device"
  527. "(%s)\n", name, lineno, token);
  528. exit(EXIT_FAILURE);
  529. }
  530. imximage_init_loadsize =
  531. get_table_entry_id(imximage_boot_loadsize,
  532. "imximage boot option", token);
  533. if (imximage_init_loadsize == -1) {
  534. fprintf(stderr,
  535. "Error: %s[%d] -Invalid boot device(%s)\n",
  536. name, lineno, token);
  537. exit(EXIT_FAILURE);
  538. }
  539. /*
  540. * The SOC loads from the storage starting at address 0
  541. * then ensures that the load size contains the offset
  542. */
  543. if (imximage_init_loadsize < imximage_ivt_offset)
  544. imximage_init_loadsize = imximage_ivt_offset;
  545. if (unlikely(cmd_ver_first != 1))
  546. cmd_ver_first = 0;
  547. break;
  548. case CMD_BOOT_OFFSET:
  549. imximage_ivt_offset = get_cfg_value(token, name, lineno);
  550. if (unlikely(cmd_ver_first != 1))
  551. cmd_ver_first = 0;
  552. break;
  553. case CMD_WRITE_DATA:
  554. case CMD_WRITE_CLR_BIT:
  555. case CMD_CHECK_BITS_SET:
  556. case CMD_CHECK_BITS_CLR:
  557. value = get_cfg_value(token, name, lineno);
  558. if (set_dcd_param)
  559. (*set_dcd_param)(imxhdr, dcd_len, cmd);
  560. (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
  561. if (unlikely(cmd_ver_first != 1))
  562. cmd_ver_first = 0;
  563. break;
  564. case CMD_CSF:
  565. if (imximage_version != 2) {
  566. fprintf(stderr,
  567. "Error: %s[%d] - CSF only supported for VERSION 2(%s)\n",
  568. name, lineno, token);
  569. exit(EXIT_FAILURE);
  570. }
  571. imximage_csf_size = get_cfg_value(token, name, lineno);
  572. if (unlikely(cmd_ver_first != 1))
  573. cmd_ver_first = 0;
  574. break;
  575. case CMD_PLUGIN:
  576. plugin_image = 1;
  577. copy_plugin_code(imxhdr, token);
  578. break;
  579. }
  580. }
  581. static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
  582. char *token, char *name, int lineno, int fld, int *dcd_len)
  583. {
  584. int value;
  585. switch (fld) {
  586. case CFG_COMMAND:
  587. *cmd = get_table_entry_id(imximage_cmds,
  588. "imximage commands", token);
  589. if (*cmd < 0) {
  590. fprintf(stderr, "Error: %s[%d] - Invalid command"
  591. "(%s)\n", name, lineno, token);
  592. exit(EXIT_FAILURE);
  593. }
  594. break;
  595. case CFG_REG_SIZE:
  596. parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
  597. break;
  598. case CFG_REG_ADDRESS:
  599. case CFG_REG_VALUE:
  600. switch(*cmd) {
  601. case CMD_WRITE_DATA:
  602. case CMD_WRITE_CLR_BIT:
  603. case CMD_CHECK_BITS_SET:
  604. case CMD_CHECK_BITS_CLR:
  605. value = get_cfg_value(token, name, lineno);
  606. if (set_dcd_param)
  607. (*set_dcd_param)(imxhdr, *dcd_len, *cmd);
  608. (*set_dcd_val)(imxhdr, name, lineno, fld, value,
  609. *dcd_len);
  610. if (fld == CFG_REG_VALUE) {
  611. (*dcd_len)++;
  612. if (*dcd_len > max_dcd_entries) {
  613. fprintf(stderr, "Error: %s[%d] -"
  614. "DCD table exceeds maximum size(%d)\n",
  615. name, lineno, max_dcd_entries);
  616. exit(EXIT_FAILURE);
  617. }
  618. }
  619. break;
  620. case CMD_PLUGIN:
  621. value = get_cfg_value(token, name, lineno);
  622. imximage_iram_free_start = value;
  623. break;
  624. default:
  625. break;
  626. }
  627. break;
  628. default:
  629. break;
  630. }
  631. }
  632. static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
  633. {
  634. FILE *fd = NULL;
  635. char *line = NULL;
  636. char *token, *saveptr1, *saveptr2;
  637. int lineno = 0;
  638. int fld;
  639. size_t len;
  640. int dcd_len = 0;
  641. int32_t cmd;
  642. fd = fopen(name, "r");
  643. if (fd == 0) {
  644. fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
  645. exit(EXIT_FAILURE);
  646. }
  647. /*
  648. * Very simple parsing, line starting with # are comments
  649. * and are dropped
  650. */
  651. while ((getline(&line, &len, fd)) > 0) {
  652. lineno++;
  653. token = strtok_r(line, "\r\n", &saveptr1);
  654. if (token == NULL)
  655. continue;
  656. /* Check inside the single line */
  657. for (fld = CFG_COMMAND, cmd = CMD_INVALID,
  658. line = token; ; line = NULL, fld++) {
  659. token = strtok_r(line, " \t", &saveptr2);
  660. if (token == NULL)
  661. break;
  662. /* Drop all text starting with '#' as comments */
  663. if (token[0] == '#')
  664. break;
  665. parse_cfg_fld(imxhdr, &cmd, token, name,
  666. lineno, fld, &dcd_len);
  667. }
  668. }
  669. (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
  670. fclose(fd);
  671. /* Exit if there is no BOOT_FROM field specifying the flash_offset */
  672. if (imximage_ivt_offset == FLASH_OFFSET_UNDEFINED) {
  673. fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name);
  674. exit(EXIT_FAILURE);
  675. }
  676. return dcd_len;
  677. }
  678. static int imximage_check_image_types(uint8_t type)
  679. {
  680. if (type == IH_TYPE_IMXIMAGE)
  681. return EXIT_SUCCESS;
  682. else
  683. return EXIT_FAILURE;
  684. }
  685. static int imximage_verify_header(unsigned char *ptr, int image_size,
  686. struct image_tool_params *params)
  687. {
  688. struct imx_header *imx_hdr = (struct imx_header *) ptr;
  689. if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
  690. return -FDT_ERR_BADSTRUCTURE;
  691. return 0;
  692. }
  693. static void imximage_print_header(const void *ptr)
  694. {
  695. struct imx_header *imx_hdr = (struct imx_header *) ptr;
  696. uint32_t version = detect_imximage_version(imx_hdr);
  697. switch (version) {
  698. case IMXIMAGE_V1:
  699. print_hdr_v1(imx_hdr);
  700. break;
  701. case IMXIMAGE_V2:
  702. print_hdr_v2(imx_hdr);
  703. break;
  704. default:
  705. err_imximage_version(version);
  706. break;
  707. }
  708. }
  709. static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
  710. struct image_tool_params *params)
  711. {
  712. struct imx_header *imxhdr = (struct imx_header *)ptr;
  713. uint32_t dcd_len;
  714. uint32_t header_size;
  715. /*
  716. * In order to not change the old imx cfg file
  717. * by adding VERSION command into it, here need
  718. * set up function ptr group to V1 by default.
  719. */
  720. imximage_version = IMXIMAGE_V1;
  721. /* Be able to detect if the cfg file has no BOOT_FROM tag */
  722. imximage_ivt_offset = FLASH_OFFSET_UNDEFINED;
  723. imximage_csf_size = 0;
  724. set_hdr_func();
  725. /* Parse dcd configuration file */
  726. dcd_len = parse_cfg_file(imxhdr, params->imagename);
  727. if (imximage_version == IMXIMAGE_V2) {
  728. header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t);
  729. if (!plugin_image)
  730. header_size += sizeof(dcd_v2_t);
  731. else
  732. header_size += MAX_PLUGIN_CODE_SIZE;
  733. if (imximage_init_loadsize < imximage_ivt_offset + header_size)
  734. imximage_init_loadsize = imximage_ivt_offset +
  735. header_size;
  736. }
  737. /* Set the imx header */
  738. (*set_imx_hdr)(imxhdr, dcd_len, params->ep, imximage_ivt_offset);
  739. /*
  740. * ROM bug alert
  741. *
  742. * MX53 only loads 512 byte multiples in case of SD boot.
  743. * MX53 only loads NAND page multiples in case of NAND boot and
  744. * supports up to 4096 byte large pages, thus align to 4096.
  745. *
  746. * The remaining fraction of a block bytes would not be loaded!
  747. */
  748. *header_size_ptr = ROUND((sbuf->st_size + imximage_ivt_offset), 4096);
  749. if (csf_ptr && imximage_csf_size) {
  750. *csf_ptr = params->ep - imximage_init_loadsize +
  751. *header_size_ptr;
  752. *header_size_ptr += imximage_csf_size;
  753. }
  754. }
  755. int imximage_check_params(struct image_tool_params *params)
  756. {
  757. if (!params)
  758. return CFG_INVALID;
  759. if (!strlen(params->imagename)) {
  760. fprintf(stderr, "Error: %s - Configuration file not specified, "
  761. "it is needed for imximage generation\n",
  762. params->cmdname);
  763. return CFG_INVALID;
  764. }
  765. /*
  766. * Check parameters:
  767. * XIP is not allowed and verify that incompatible
  768. * parameters are not sent at the same time
  769. * For example, if list is required a data image must not be provided
  770. */
  771. return (params->dflag && (params->fflag || params->lflag)) ||
  772. (params->fflag && (params->dflag || params->lflag)) ||
  773. (params->lflag && (params->dflag || params->fflag)) ||
  774. (params->xflag) || !(strlen(params->imagename));
  775. }
  776. static int imximage_generate(struct image_tool_params *params,
  777. struct image_type_params *tparams)
  778. {
  779. struct imx_header *imxhdr;
  780. size_t alloc_len;
  781. struct stat sbuf;
  782. char *datafile = params->datafile;
  783. uint32_t pad_len, header_size;
  784. memset(&imximage_header, 0, sizeof(imximage_header));
  785. /*
  786. * In order to not change the old imx cfg file
  787. * by adding VERSION command into it, here need
  788. * set up function ptr group to V1 by default.
  789. */
  790. imximage_version = IMXIMAGE_V1;
  791. /* Be able to detect if the cfg file has no BOOT_FROM tag */
  792. imximage_ivt_offset = FLASH_OFFSET_UNDEFINED;
  793. imximage_csf_size = 0;
  794. set_hdr_func();
  795. /* Parse dcd configuration file */
  796. parse_cfg_file(&imximage_header, params->imagename);
  797. /* TODO: check i.MX image V1 handling, for now use 'old' style */
  798. if (imximage_version == IMXIMAGE_V1) {
  799. alloc_len = 4096;
  800. header_size = 4096;
  801. } else {
  802. header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t);
  803. if (!plugin_image)
  804. header_size += sizeof(dcd_v2_t);
  805. else
  806. header_size += MAX_PLUGIN_CODE_SIZE;
  807. if (imximage_init_loadsize < imximage_ivt_offset + header_size)
  808. imximage_init_loadsize = imximage_ivt_offset +
  809. header_size;
  810. alloc_len = imximage_init_loadsize - imximage_ivt_offset;
  811. }
  812. if (alloc_len < header_size) {
  813. fprintf(stderr, "%s: header error\n",
  814. params->cmdname);
  815. exit(EXIT_FAILURE);
  816. }
  817. imxhdr = malloc(alloc_len);
  818. if (!imxhdr) {
  819. fprintf(stderr, "%s: malloc return failure: %s\n",
  820. params->cmdname, strerror(errno));
  821. exit(EXIT_FAILURE);
  822. }
  823. memset(imxhdr, 0, alloc_len);
  824. tparams->header_size = alloc_len;
  825. tparams->hdr = imxhdr;
  826. /* determine data image file length */
  827. if (stat(datafile, &sbuf) < 0) {
  828. fprintf(stderr, "%s: Can't stat %s: %s\n",
  829. params->cmdname, datafile, strerror(errno));
  830. exit(EXIT_FAILURE);
  831. }
  832. pad_len = ROUND(sbuf.st_size, 4096) - sbuf.st_size;
  833. /* TODO: check i.MX image V1 handling, for now use 'old' style */
  834. if (imximage_version == IMXIMAGE_V1)
  835. return 0;
  836. else
  837. return pad_len;
  838. }
  839. /*
  840. * imximage parameters
  841. */
  842. U_BOOT_IMAGE_TYPE(
  843. imximage,
  844. "Freescale i.MX Boot Image support",
  845. 0,
  846. NULL,
  847. imximage_check_params,
  848. imximage_verify_header,
  849. imximage_print_header,
  850. imximage_set_header,
  851. NULL,
  852. imximage_check_image_types,
  853. NULL,
  854. imximage_generate
  855. );