imximage.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. hdr_base = entry_point - imximage_init_loadsize + flash_offset;
  268. fhdr_v1->app_dest_ptr = hdr_base - flash_offset;
  269. fhdr_v1->app_code_jump_vector = entry_point;
  270. fhdr_v1->dcd_ptr_ptr = hdr_base + offsetof(flash_header_v1_t, dcd_ptr);
  271. fhdr_v1->dcd_ptr = hdr_base + offsetof(imx_header_v1_t, dcd_table);
  272. /* Security feature are not supported */
  273. fhdr_v1->app_code_csf = 0;
  274. fhdr_v1->super_root_key = 0;
  275. header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4);
  276. }
  277. static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len,
  278. uint32_t entry_point, uint32_t flash_offset)
  279. {
  280. imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2;
  281. flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
  282. uint32_t hdr_base;
  283. /* Set magic number */
  284. fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
  285. fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t));
  286. fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
  287. if (!hdr_v2->boot_data.plugin) {
  288. fhdr_v2->entry = entry_point;
  289. fhdr_v2->reserved1 = 0;
  290. fhdr_v2->reserved1 = 0;
  291. hdr_base = entry_point - imximage_init_loadsize +
  292. flash_offset;
  293. fhdr_v2->self = hdr_base;
  294. if (dcd_len > 0)
  295. fhdr_v2->dcd_ptr = hdr_base +
  296. offsetof(imx_header_v2_t, data);
  297. else
  298. fhdr_v2->dcd_ptr = 0;
  299. fhdr_v2->boot_data_ptr = hdr_base
  300. + offsetof(imx_header_v2_t, boot_data);
  301. hdr_v2->boot_data.start = entry_point - imximage_init_loadsize;
  302. fhdr_v2->csf = 0;
  303. header_size_ptr = &hdr_v2->boot_data.size;
  304. csf_ptr = &fhdr_v2->csf;
  305. } else {
  306. imx_header_v2_t *next_hdr_v2;
  307. flash_header_v2_t *next_fhdr_v2;
  308. if (imximage_csf_size != 0) {
  309. fprintf(stderr, "Error: Header v2: SECURE_BOOT is only supported in DCD mode!");
  310. exit(EXIT_FAILURE);
  311. }
  312. fhdr_v2->entry = imximage_iram_free_start +
  313. flash_offset + sizeof(flash_header_v2_t) +
  314. sizeof(boot_data_t);
  315. fhdr_v2->reserved1 = 0;
  316. fhdr_v2->reserved2 = 0;
  317. fhdr_v2->self = imximage_iram_free_start + flash_offset;
  318. fhdr_v2->dcd_ptr = 0;
  319. fhdr_v2->boot_data_ptr = fhdr_v2->self +
  320. offsetof(imx_header_v2_t, boot_data);
  321. hdr_v2->boot_data.start = imximage_iram_free_start;
  322. /*
  323. * The actural size of plugin image is "imximage_plugin_size +
  324. * sizeof(flash_header_v2_t) + sizeof(boot_data_t)", plus the
  325. * flash_offset space.The ROM code only need to copy this size
  326. * to run the plugin code. However, later when copy the whole
  327. * U-Boot image to DDR, the ROM code use memcpy to copy the
  328. * first part of the image, and use the storage read function
  329. * to get the remaining part. This requires the dividing point
  330. * must be multiple of storage sector size. Here we set the
  331. * first section to be MAX_PLUGIN_CODE_SIZE(64KB) for this
  332. * purpose.
  333. */
  334. hdr_v2->boot_data.size = MAX_PLUGIN_CODE_SIZE;
  335. /* Security feature are not supported */
  336. fhdr_v2->csf = 0;
  337. next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 +
  338. imximage_plugin_size);
  339. next_fhdr_v2 = &next_hdr_v2->fhdr;
  340. next_fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */
  341. next_fhdr_v2->header.length =
  342. cpu_to_be16(sizeof(flash_header_v2_t));
  343. next_fhdr_v2->header.version = IVT_VERSION; /* 0x40 */
  344. next_fhdr_v2->entry = entry_point;
  345. hdr_base = entry_point - sizeof(struct imx_header);
  346. next_fhdr_v2->reserved1 = 0;
  347. next_fhdr_v2->reserved2 = 0;
  348. next_fhdr_v2->self = hdr_base + imximage_plugin_size;
  349. next_fhdr_v2->dcd_ptr = 0;
  350. next_fhdr_v2->boot_data_ptr = next_fhdr_v2->self +
  351. offsetof(imx_header_v2_t, boot_data);
  352. next_hdr_v2->boot_data.start = hdr_base - flash_offset;
  353. header_size_ptr = &next_hdr_v2->boot_data.size;
  354. next_hdr_v2->boot_data.plugin = 0;
  355. next_fhdr_v2->csf = 0;
  356. }
  357. }
  358. static void set_hdr_func(void)
  359. {
  360. switch (imximage_version) {
  361. case IMXIMAGE_V1:
  362. set_dcd_val = set_dcd_val_v1;
  363. set_dcd_param = NULL;
  364. set_dcd_rst = set_dcd_rst_v1;
  365. set_imx_hdr = set_imx_hdr_v1;
  366. max_dcd_entries = MAX_HW_CFG_SIZE_V1;
  367. break;
  368. case IMXIMAGE_V2:
  369. gd_last_cmd = NULL;
  370. set_dcd_val = set_dcd_val_v2;
  371. set_dcd_param = set_dcd_param_v2;
  372. set_dcd_rst = set_dcd_rst_v2;
  373. set_imx_hdr = set_imx_hdr_v2;
  374. max_dcd_entries = MAX_HW_CFG_SIZE_V2;
  375. break;
  376. default:
  377. err_imximage_version(imximage_version);
  378. break;
  379. }
  380. }
  381. static void print_hdr_v1(struct imx_header *imx_hdr)
  382. {
  383. imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1;
  384. flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr;
  385. dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table;
  386. uint32_t size, length, ver;
  387. size = dcd_v1->preamble.length;
  388. if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) {
  389. fprintf(stderr,
  390. "Error: Image corrupt DCD size %d exceed maximum %d\n",
  391. (uint32_t)(size / sizeof(dcd_type_addr_data_t)),
  392. MAX_HW_CFG_SIZE_V1);
  393. exit(EXIT_FAILURE);
  394. }
  395. length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t);
  396. ver = detect_imximage_version(imx_hdr);
  397. printf("Image Type: Freescale IMX Boot Image\n");
  398. printf("Image Ver: %x", ver);
  399. printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver));
  400. printf("Data Size: ");
  401. genimg_print_size(dcd_v1->addr_data[length].type);
  402. printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr);
  403. printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector);
  404. }
  405. static void print_hdr_v2(struct imx_header *imx_hdr)
  406. {
  407. imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2;
  408. flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr;
  409. dcd_v2_t *dcd_v2 = &hdr_v2->data.dcd_table;
  410. uint32_t size, version, plugin;
  411. plugin = hdr_v2->boot_data.plugin;
  412. if (!plugin) {
  413. size = be16_to_cpu(dcd_v2->header.length);
  414. if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) {
  415. fprintf(stderr,
  416. "Error: Image corrupt DCD size %d exceed maximum %d\n",
  417. (uint32_t)(size / sizeof(dcd_addr_data_t)),
  418. MAX_HW_CFG_SIZE_V2);
  419. exit(EXIT_FAILURE);
  420. }
  421. }
  422. version = detect_imximage_version(imx_hdr);
  423. printf("Image Type: Freescale IMX Boot Image\n");
  424. printf("Image Ver: %x", version);
  425. printf("%s\n", get_table_entry_name(imximage_versions, NULL, version));
  426. printf("Mode: %s\n", plugin ? "PLUGIN" : "DCD");
  427. if (!plugin) {
  428. printf("Data Size: ");
  429. genimg_print_size(hdr_v2->boot_data.size);
  430. printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr);
  431. printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
  432. if (fhdr_v2->csf && (imximage_ivt_offset != UNDEFINED) &&
  433. (imximage_csf_size != UNDEFINED)) {
  434. uint16_t dcdlen;
  435. int offs;
  436. dcdlen = hdr_v2->data.dcd_table.header.length;
  437. offs = (char *)&hdr_v2->data.dcd_table
  438. - (char *)hdr_v2;
  439. printf("HAB Blocks: %08x %08x %08x\n",
  440. (uint32_t)fhdr_v2->self, 0,
  441. hdr_v2->boot_data.size - imximage_ivt_offset -
  442. imximage_csf_size);
  443. printf("DCD Blocks: 00910000 %08x %08x\n",
  444. offs, be16_to_cpu(dcdlen));
  445. }
  446. } else {
  447. imx_header_v2_t *next_hdr_v2;
  448. flash_header_v2_t *next_fhdr_v2;
  449. /*First Header*/
  450. printf("Plugin Data Size: ");
  451. genimg_print_size(hdr_v2->boot_data.size);
  452. printf("Plugin Code Size: ");
  453. genimg_print_size(imximage_plugin_size);
  454. printf("Plugin Load Address: %08x\n", hdr_v2->boot_data.start);
  455. printf("Plugin Entry Point: %08x\n", (uint32_t)fhdr_v2->entry);
  456. /*Second Header*/
  457. next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 +
  458. imximage_plugin_size);
  459. next_fhdr_v2 = &next_hdr_v2->fhdr;
  460. printf("U-Boot Data Size: ");
  461. genimg_print_size(next_hdr_v2->boot_data.size);
  462. printf("U-Boot Load Address: %08x\n",
  463. next_hdr_v2->boot_data.start);
  464. printf("U-Boot Entry Point: %08x\n",
  465. (uint32_t)next_fhdr_v2->entry);
  466. }
  467. }
  468. static void copy_plugin_code(struct imx_header *imxhdr, char *plugin_file)
  469. {
  470. int ifd;
  471. struct stat sbuf;
  472. char *plugin_buf = imxhdr->header.hdr_v2.data.plugin_code;
  473. char *ptr;
  474. ifd = open(plugin_file, O_RDONLY|O_BINARY);
  475. if (ifd < 0) {
  476. fprintf(stderr, "Can't open %s: %s\n",
  477. plugin_file,
  478. strerror(errno));
  479. exit(EXIT_FAILURE);
  480. }
  481. if (fstat(ifd, &sbuf) < 0) {
  482. fprintf(stderr, "Can't stat %s: %s\n",
  483. plugin_file,
  484. strerror(errno));
  485. exit(EXIT_FAILURE);
  486. }
  487. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0);
  488. if (ptr == MAP_FAILED) {
  489. fprintf(stderr, "Can't read %s: %s\n",
  490. plugin_file,
  491. strerror(errno));
  492. exit(EXIT_FAILURE);
  493. }
  494. if (sbuf.st_size > MAX_PLUGIN_CODE_SIZE) {
  495. printf("plugin binary size too large\n");
  496. exit(EXIT_FAILURE);
  497. }
  498. memcpy(plugin_buf, ptr, sbuf.st_size);
  499. imximage_plugin_size = sbuf.st_size;
  500. (void) munmap((void *)ptr, sbuf.st_size);
  501. (void) close(ifd);
  502. imxhdr->header.hdr_v2.boot_data.plugin = 1;
  503. }
  504. static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token,
  505. char *name, int lineno, int fld, int dcd_len)
  506. {
  507. int value;
  508. static int cmd_ver_first = ~0;
  509. switch (cmd) {
  510. case CMD_IMAGE_VERSION:
  511. imximage_version = get_cfg_value(token, name, lineno);
  512. if (cmd_ver_first == 0) {
  513. fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION "
  514. "command need be the first before other "
  515. "valid command in the file\n", name, lineno);
  516. exit(EXIT_FAILURE);
  517. }
  518. cmd_ver_first = 1;
  519. set_hdr_func();
  520. break;
  521. case CMD_BOOT_FROM:
  522. imximage_ivt_offset = get_table_entry_id(imximage_boot_offset,
  523. "imximage boot option", token);
  524. if (imximage_ivt_offset == -1) {
  525. fprintf(stderr, "Error: %s[%d] -Invalid boot device"
  526. "(%s)\n", name, lineno, token);
  527. exit(EXIT_FAILURE);
  528. }
  529. imximage_init_loadsize =
  530. get_table_entry_id(imximage_boot_loadsize,
  531. "imximage boot option", token);
  532. if (imximage_init_loadsize == -1) {
  533. fprintf(stderr,
  534. "Error: %s[%d] -Invalid boot device(%s)\n",
  535. name, lineno, token);
  536. exit(EXIT_FAILURE);
  537. }
  538. /*
  539. * The SOC loads from the storage starting at address 0
  540. * then ensures that the load size contains the offset
  541. */
  542. if (imximage_init_loadsize < imximage_ivt_offset)
  543. imximage_init_loadsize = imximage_ivt_offset;
  544. if (unlikely(cmd_ver_first != 1))
  545. cmd_ver_first = 0;
  546. break;
  547. case CMD_BOOT_OFFSET:
  548. imximage_ivt_offset = get_cfg_value(token, name, lineno);
  549. if (unlikely(cmd_ver_first != 1))
  550. cmd_ver_first = 0;
  551. break;
  552. case CMD_WRITE_DATA:
  553. case CMD_WRITE_CLR_BIT:
  554. case CMD_CHECK_BITS_SET:
  555. case CMD_CHECK_BITS_CLR:
  556. value = get_cfg_value(token, name, lineno);
  557. if (set_dcd_param)
  558. (*set_dcd_param)(imxhdr, dcd_len, cmd);
  559. (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len);
  560. if (unlikely(cmd_ver_first != 1))
  561. cmd_ver_first = 0;
  562. break;
  563. case CMD_CSF:
  564. if (imximage_version != 2) {
  565. fprintf(stderr,
  566. "Error: %s[%d] - CSF only supported for VERSION 2(%s)\n",
  567. name, lineno, token);
  568. exit(EXIT_FAILURE);
  569. }
  570. imximage_csf_size = get_cfg_value(token, name, lineno);
  571. if (unlikely(cmd_ver_first != 1))
  572. cmd_ver_first = 0;
  573. break;
  574. case CMD_PLUGIN:
  575. plugin_image = 1;
  576. copy_plugin_code(imxhdr, token);
  577. break;
  578. }
  579. }
  580. static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd,
  581. char *token, char *name, int lineno, int fld, int *dcd_len)
  582. {
  583. int value;
  584. switch (fld) {
  585. case CFG_COMMAND:
  586. *cmd = get_table_entry_id(imximage_cmds,
  587. "imximage commands", token);
  588. if (*cmd < 0) {
  589. fprintf(stderr, "Error: %s[%d] - Invalid command"
  590. "(%s)\n", name, lineno, token);
  591. exit(EXIT_FAILURE);
  592. }
  593. break;
  594. case CFG_REG_SIZE:
  595. parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len);
  596. break;
  597. case CFG_REG_ADDRESS:
  598. case CFG_REG_VALUE:
  599. switch(*cmd) {
  600. case CMD_WRITE_DATA:
  601. case CMD_WRITE_CLR_BIT:
  602. case CMD_CHECK_BITS_SET:
  603. case CMD_CHECK_BITS_CLR:
  604. value = get_cfg_value(token, name, lineno);
  605. if (set_dcd_param)
  606. (*set_dcd_param)(imxhdr, *dcd_len, *cmd);
  607. (*set_dcd_val)(imxhdr, name, lineno, fld, value,
  608. *dcd_len);
  609. if (fld == CFG_REG_VALUE) {
  610. (*dcd_len)++;
  611. if (*dcd_len > max_dcd_entries) {
  612. fprintf(stderr, "Error: %s[%d] -"
  613. "DCD table exceeds maximum size(%d)\n",
  614. name, lineno, max_dcd_entries);
  615. exit(EXIT_FAILURE);
  616. }
  617. }
  618. break;
  619. case CMD_PLUGIN:
  620. value = get_cfg_value(token, name, lineno);
  621. imximage_iram_free_start = value;
  622. break;
  623. default:
  624. break;
  625. }
  626. break;
  627. default:
  628. break;
  629. }
  630. }
  631. static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name)
  632. {
  633. FILE *fd = NULL;
  634. char *line = NULL;
  635. char *token, *saveptr1, *saveptr2;
  636. int lineno = 0;
  637. int fld;
  638. size_t len;
  639. int dcd_len = 0;
  640. int32_t cmd;
  641. fd = fopen(name, "r");
  642. if (fd == 0) {
  643. fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
  644. exit(EXIT_FAILURE);
  645. }
  646. /*
  647. * Very simple parsing, line starting with # are comments
  648. * and are dropped
  649. */
  650. while ((getline(&line, &len, fd)) > 0) {
  651. lineno++;
  652. token = strtok_r(line, "\r\n", &saveptr1);
  653. if (token == NULL)
  654. continue;
  655. /* Check inside the single line */
  656. for (fld = CFG_COMMAND, cmd = CMD_INVALID,
  657. line = token; ; line = NULL, fld++) {
  658. token = strtok_r(line, " \t", &saveptr2);
  659. if (token == NULL)
  660. break;
  661. /* Drop all text starting with '#' as comments */
  662. if (token[0] == '#')
  663. break;
  664. parse_cfg_fld(imxhdr, &cmd, token, name,
  665. lineno, fld, &dcd_len);
  666. }
  667. }
  668. (*set_dcd_rst)(imxhdr, dcd_len, name, lineno);
  669. fclose(fd);
  670. /* Exit if there is no BOOT_FROM field specifying the flash_offset */
  671. if (imximage_ivt_offset == FLASH_OFFSET_UNDEFINED) {
  672. fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name);
  673. exit(EXIT_FAILURE);
  674. }
  675. return dcd_len;
  676. }
  677. static int imximage_check_image_types(uint8_t type)
  678. {
  679. if (type == IH_TYPE_IMXIMAGE)
  680. return EXIT_SUCCESS;
  681. else
  682. return EXIT_FAILURE;
  683. }
  684. static int imximage_verify_header(unsigned char *ptr, int image_size,
  685. struct image_tool_params *params)
  686. {
  687. struct imx_header *imx_hdr = (struct imx_header *) ptr;
  688. if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID)
  689. return -FDT_ERR_BADSTRUCTURE;
  690. return 0;
  691. }
  692. static void imximage_print_header(const void *ptr)
  693. {
  694. struct imx_header *imx_hdr = (struct imx_header *) ptr;
  695. uint32_t version = detect_imximage_version(imx_hdr);
  696. switch (version) {
  697. case IMXIMAGE_V1:
  698. print_hdr_v1(imx_hdr);
  699. break;
  700. case IMXIMAGE_V2:
  701. print_hdr_v2(imx_hdr);
  702. break;
  703. default:
  704. err_imximage_version(version);
  705. break;
  706. }
  707. }
  708. static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd,
  709. struct image_tool_params *params)
  710. {
  711. struct imx_header *imxhdr = (struct imx_header *)ptr;
  712. uint32_t dcd_len;
  713. uint32_t header_size;
  714. /*
  715. * In order to not change the old imx cfg file
  716. * by adding VERSION command into it, here need
  717. * set up function ptr group to V1 by default.
  718. */
  719. imximage_version = IMXIMAGE_V1;
  720. /* Be able to detect if the cfg file has no BOOT_FROM tag */
  721. imximage_ivt_offset = FLASH_OFFSET_UNDEFINED;
  722. imximage_csf_size = 0;
  723. set_hdr_func();
  724. /* Parse dcd configuration file */
  725. dcd_len = parse_cfg_file(imxhdr, params->imagename);
  726. if (imximage_version == IMXIMAGE_V1)
  727. header_size = sizeof(flash_header_v1_t);
  728. else {
  729. header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t);
  730. if (!plugin_image)
  731. header_size += sizeof(dcd_v2_t);
  732. else
  733. header_size += MAX_PLUGIN_CODE_SIZE;
  734. }
  735. if (imximage_init_loadsize < imximage_ivt_offset + header_size)
  736. imximage_init_loadsize = imximage_ivt_offset + header_size;
  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. if (imximage_version == IMXIMAGE_V1)
  798. header_size = sizeof(imx_header_v1_t);
  799. else {
  800. header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t);
  801. if (!plugin_image)
  802. header_size += sizeof(dcd_v2_t);
  803. else
  804. header_size += MAX_PLUGIN_CODE_SIZE;
  805. }
  806. if (imximage_init_loadsize < imximage_ivt_offset + header_size)
  807. imximage_init_loadsize = imximage_ivt_offset + header_size;
  808. alloc_len = imximage_init_loadsize - imximage_ivt_offset;
  809. if (alloc_len < header_size) {
  810. fprintf(stderr, "%s: header error\n",
  811. params->cmdname);
  812. exit(EXIT_FAILURE);
  813. }
  814. imxhdr = malloc(alloc_len);
  815. if (!imxhdr) {
  816. fprintf(stderr, "%s: malloc return failure: %s\n",
  817. params->cmdname, strerror(errno));
  818. exit(EXIT_FAILURE);
  819. }
  820. memset(imxhdr, 0, alloc_len);
  821. tparams->header_size = alloc_len;
  822. tparams->hdr = imxhdr;
  823. /* determine data image file length */
  824. if (stat(datafile, &sbuf) < 0) {
  825. fprintf(stderr, "%s: Can't stat %s: %s\n",
  826. params->cmdname, datafile, strerror(errno));
  827. exit(EXIT_FAILURE);
  828. }
  829. pad_len = ROUND(sbuf.st_size, 4096) - sbuf.st_size;
  830. return pad_len;
  831. }
  832. /*
  833. * imximage parameters
  834. */
  835. U_BOOT_IMAGE_TYPE(
  836. imximage,
  837. "Freescale i.MX Boot Image support",
  838. 0,
  839. NULL,
  840. imximage_check_params,
  841. imximage_verify_header,
  842. imximage_print_header,
  843. imximage_set_header,
  844. NULL,
  845. imximage_check_image_types,
  846. NULL,
  847. imximage_generate
  848. );