imx8image.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018 NXP
  4. *
  5. * Peng Fan <peng.fan@nxp.com>
  6. */
  7. #include "imx8image.h"
  8. #include <image.h>
  9. static int p_idx;
  10. static int sector_size;
  11. static soc_type_t soc;
  12. static int container = -1;
  13. static int32_t core_type = CFG_CORE_INVALID;
  14. static bool emmc_fastboot;
  15. static image_t param_stack[IMG_STACK_SIZE];
  16. static uint8_t fuse_version;
  17. static uint16_t sw_version;
  18. static uint32_t custom_partition;
  19. static uint32_t scfw_flags;
  20. int imx8image_check_params(struct image_tool_params *params)
  21. {
  22. return 0;
  23. }
  24. static void imx8image_set_header(void *ptr, struct stat *sbuf, int ifd,
  25. struct image_tool_params *params)
  26. {
  27. }
  28. static void imx8image_print_header(const void *ptr)
  29. {
  30. }
  31. static int imx8image_check_image_types(uint8_t type)
  32. {
  33. return (type == IH_TYPE_IMX8IMAGE) ? EXIT_SUCCESS : EXIT_FAILURE;
  34. }
  35. static table_entry_t imx8image_cmds[] = {
  36. {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
  37. {CMD_FUSE_VERSION, "FUSE_VERSION", "fuse version", },
  38. {CMD_SW_VERSION, "SW_VERSION", "sw version", },
  39. {CMD_MSG_BLOCK, "MSG_BLOCK", "msg block", },
  40. {CMD_FILEOFF, "FILEOFF", "fileoff", },
  41. {CMD_FLAG, "FLAG", "flag", },
  42. {CMD_APPEND, "APPEND", "append a container", },
  43. {CMD_PARTITION, "PARTITION", "new partition", },
  44. {CMD_SOC_TYPE, "SOC_TYPE", "soc type", },
  45. {CMD_CONTAINER, "CONTAINER", "new container", },
  46. {CMD_IMAGE, "IMAGE", "new image", },
  47. {CMD_DATA, "DATA", "new data", },
  48. {-1, "", "", },
  49. };
  50. static table_entry_t imx8image_core_entries[] = {
  51. {CFG_SCU, "SCU", "scu core", },
  52. {CFG_M40, "M40", "M4 core 0", },
  53. {CFG_M41, "M41", "M4 core 1", },
  54. {CFG_A35, "A35", "A35 core", },
  55. {CFG_A53, "A53", "A53 core", },
  56. {CFG_A72, "A72", "A72 core", },
  57. {-1, "", "", },
  58. };
  59. static table_entry_t imx8image_sector_size[] = {
  60. {0x400, "sd", "sd/emmc",},
  61. {0x400, "emmc_fastboot", "emmc fastboot",},
  62. {0x400, "fspi", "flexspi", },
  63. {0x1000, "nand_4k", "nand 4K", },
  64. {0x2000, "nand_8k", "nand 8K", },
  65. {0x4000, "nand_16k", "nand 16K", },
  66. {-1, "", "Invalid", },
  67. };
  68. static void parse_cfg_cmd(image_t *param_stack, int32_t cmd, char *token,
  69. char *name, int lineno)
  70. {
  71. switch (cmd) {
  72. case CMD_BOOT_FROM:
  73. sector_size = get_table_entry_id(imx8image_sector_size,
  74. "imximage boot option",
  75. token);
  76. if (!strncmp("emmc_fastboot", token, 13))
  77. emmc_fastboot = true;
  78. break;
  79. case CMD_FUSE_VERSION:
  80. fuse_version = (uint8_t)(strtoll(token, NULL, 0) & 0xFF);
  81. break;
  82. case CMD_SW_VERSION:
  83. sw_version = (uint8_t)(strtoll(token, NULL, 0) & 0xFFFF);
  84. break;
  85. case CMD_FILEOFF:
  86. param_stack[p_idx].option = FILEOFF;
  87. param_stack[p_idx++].dst = (uint32_t)strtoll(token, NULL, 0);
  88. break;
  89. case CMD_MSG_BLOCK:
  90. param_stack[p_idx].option = MSG_BLOCK;
  91. param_stack[p_idx].filename = token;
  92. break;
  93. case CMD_FLAG:
  94. param_stack[p_idx].option = FLAG;
  95. param_stack[p_idx++].entry = (uint32_t)strtoll(token, NULL, 0);
  96. break;
  97. case CMD_APPEND:
  98. param_stack[p_idx].option = APPEND;
  99. param_stack[p_idx++].filename = token;
  100. break;
  101. case CMD_PARTITION:
  102. param_stack[p_idx].option = PARTITION;
  103. param_stack[p_idx++].entry = (uint32_t)strtoll(token, NULL, 0);
  104. break;
  105. case CMD_SOC_TYPE:
  106. if (!strncmp(token, "IMX8QX", 6)) {
  107. soc = QX;
  108. } else if (!strncmp(token, "IMX8QM", 6)) {
  109. soc = QM;
  110. } else {
  111. fprintf(stderr, "Unknown CMD_SOC_TYPE");
  112. exit(EXIT_FAILURE);
  113. }
  114. break;
  115. case CMD_IMAGE:
  116. case CMD_DATA:
  117. core_type = get_table_entry_id(imx8image_core_entries,
  118. "imx8image core entries",
  119. token);
  120. if (core_type < 0) {
  121. fprintf(stderr, "Wrong IMAGE core_type %s\n", token);
  122. exit(EXIT_FAILURE);
  123. }
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. static void parse_cfg_fld(image_t *param_stack, int32_t *cmd, char *token,
  130. char *name, int lineno, int fld)
  131. {
  132. switch (fld) {
  133. case CFG_COMMAND:
  134. *cmd = get_table_entry_id(imx8image_cmds, "imx8image cmds",
  135. token);
  136. if (*cmd < 0) {
  137. fprintf(stderr, "Error: %s[%d] - Invalid command (%s)\n", name, lineno, token);
  138. exit(EXIT_FAILURE);
  139. }
  140. if (*cmd == CMD_CONTAINER) {
  141. fprintf(stdout, "New Container: \t%d\n", ++container);
  142. param_stack[p_idx++].option = NEW_CONTAINER;
  143. }
  144. break;
  145. case CFG_CORE_TYPE:
  146. parse_cfg_cmd(param_stack, *cmd, token, name, lineno);
  147. break;
  148. case CFG_IMAGE_NAME:
  149. if (*cmd == CMD_MSG_BLOCK) {
  150. if (!strncmp(token, "fuse", 4)) {
  151. param_stack[p_idx].ext = SC_R_OTP;
  152. } else if (!strncmp(token, "debug", 5)) {
  153. param_stack[p_idx].ext = SC_R_DEBUG;
  154. } else if (!strncmp(token, "field", 5)) {
  155. param_stack[p_idx].ext = SC_R_ROM_0;
  156. } else {
  157. fprintf(stderr, "MSG type not found %s\n", token);
  158. exit(EXIT_FAILURE);
  159. }
  160. break;
  161. }
  162. switch (core_type) {
  163. case CFG_SCU:
  164. param_stack[p_idx].option = SCFW;
  165. param_stack[p_idx++].filename = token;
  166. break;
  167. case CFG_M40:
  168. param_stack[p_idx].option = M40;
  169. param_stack[p_idx].ext = 0;
  170. param_stack[p_idx].filename = token;
  171. break;
  172. case CFG_M41:
  173. param_stack[p_idx].option = M41;
  174. param_stack[p_idx].ext = 1;
  175. param_stack[p_idx].filename = token;
  176. break;
  177. case CFG_A35:
  178. param_stack[p_idx].ext = CORE_CA35;
  179. param_stack[p_idx].option =
  180. (*cmd == CMD_DATA) ? DATA : AP;
  181. param_stack[p_idx].filename = token;
  182. break;
  183. case CFG_A53:
  184. param_stack[p_idx].ext = CORE_CA53;
  185. param_stack[p_idx].option =
  186. (*cmd == CMD_DATA) ? DATA : AP;
  187. param_stack[p_idx].filename = token;
  188. break;
  189. case CFG_A72:
  190. param_stack[p_idx].ext = CORE_CA72;
  191. param_stack[p_idx].option =
  192. (*cmd == CMD_DATA) ? DATA : AP;
  193. param_stack[p_idx].filename = token;
  194. break;
  195. }
  196. break;
  197. case CFG_LOAD_ADDR:
  198. if (*cmd == CMD_MSG_BLOCK) {
  199. param_stack[p_idx++].entry =
  200. (uint32_t)strtoll(token, NULL, 0);
  201. break;
  202. }
  203. switch (core_type) {
  204. case CFG_SCU:
  205. break;
  206. case CFG_M40:
  207. case CFG_M41:
  208. case CFG_A35:
  209. case CFG_A53:
  210. case CFG_A72:
  211. param_stack[p_idx++].entry =
  212. (uint32_t)strtoll(token, NULL, 0);
  213. break;
  214. }
  215. default:
  216. break;
  217. }
  218. }
  219. static uint32_t parse_cfg_file(image_t *param_stack, char *name)
  220. {
  221. FILE *fd = NULL;
  222. char *line = NULL;
  223. char *token, *saveptr1, *saveptr2;
  224. int lineno = 0;
  225. int fld;
  226. size_t len;
  227. int32_t cmd;
  228. fd = fopen(name, "r");
  229. if (fd == 0) {
  230. fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
  231. exit(EXIT_FAILURE);
  232. }
  233. /*
  234. * Very simple parsing, line starting with # are comments
  235. * and are dropped
  236. */
  237. while ((getline(&line, &len, fd)) > 0) {
  238. lineno++;
  239. token = strtok_r(line, "\r\n", &saveptr1);
  240. if (!token)
  241. continue;
  242. /* Check inside the single line */
  243. for (fld = CFG_COMMAND, cmd = CFG_INVALID,
  244. line = token; ; line = NULL, fld++) {
  245. token = strtok_r(line, " \t", &saveptr2);
  246. if (!token)
  247. break;
  248. /* Drop all text starting with '#' as comments */
  249. if (token[0] == '#')
  250. break;
  251. parse_cfg_fld(param_stack, &cmd, token, name, lineno,
  252. fld);
  253. }
  254. }
  255. return 0;
  256. }
  257. static void check_file(struct stat *sbuf, char *filename)
  258. {
  259. int tmp_fd = open(filename, O_RDONLY | O_BINARY);
  260. if (tmp_fd < 0) {
  261. fprintf(stderr, "%s: Can't open: %s\n",
  262. filename, strerror(errno));
  263. exit(EXIT_FAILURE);
  264. }
  265. if (fstat(tmp_fd, sbuf) < 0) {
  266. fprintf(stderr, "%s: Can't stat: %s\n",
  267. filename, strerror(errno));
  268. exit(EXIT_FAILURE);
  269. }
  270. close(tmp_fd);
  271. }
  272. static void copy_file_aligned(int ifd, const char *datafile, int offset,
  273. int align)
  274. {
  275. int dfd;
  276. struct stat sbuf;
  277. unsigned char *ptr;
  278. uint8_t zeros[0x4000];
  279. int size;
  280. int ret;
  281. if (align > 0x4000) {
  282. fprintf(stderr, "Wrong alignment requested %d\n", align);
  283. exit(EXIT_FAILURE);
  284. }
  285. memset(zeros, 0, sizeof(zeros));
  286. dfd = open(datafile, O_RDONLY | O_BINARY);
  287. if (dfd < 0) {
  288. fprintf(stderr, "Can't open %s: %s\n",
  289. datafile, strerror(errno));
  290. exit(EXIT_FAILURE);
  291. }
  292. if (fstat(dfd, &sbuf) < 0) {
  293. fprintf(stderr, "Can't stat %s: %s\n",
  294. datafile, strerror(errno));
  295. exit(EXIT_FAILURE);
  296. }
  297. if (sbuf.st_size == 0)
  298. goto close;
  299. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  300. if (ptr == MAP_FAILED) {
  301. fprintf(stderr, "Can't read %s: %s\n",
  302. datafile, strerror(errno));
  303. exit(EXIT_FAILURE);
  304. }
  305. size = sbuf.st_size;
  306. ret = lseek(ifd, offset, SEEK_SET);
  307. if (ret < 0) {
  308. fprintf(stderr, "%s: lseek error %s\n",
  309. __func__, strerror(errno));
  310. exit(EXIT_FAILURE);
  311. }
  312. if (write(ifd, ptr, size) != size) {
  313. fprintf(stderr, "Write error %s\n", strerror(errno));
  314. exit(EXIT_FAILURE);
  315. }
  316. align = ALIGN(size, align) - size;
  317. if (write(ifd, (char *)&zeros, align) != align) {
  318. fprintf(stderr, "Write error: %s\n", strerror(errno));
  319. exit(EXIT_FAILURE);
  320. }
  321. munmap((void *)ptr, sbuf.st_size);
  322. close:
  323. close(dfd);
  324. }
  325. static void copy_file (int ifd, const char *datafile, int pad, int offset)
  326. {
  327. int dfd;
  328. struct stat sbuf;
  329. unsigned char *ptr;
  330. int tail;
  331. uint64_t zero = 0;
  332. uint8_t zeros[4096];
  333. int size, ret;
  334. memset(zeros, 0, sizeof(zeros));
  335. dfd = open(datafile, O_RDONLY | O_BINARY);
  336. if (dfd < 0) {
  337. fprintf(stderr, "Can't open %s: %s\n",
  338. datafile, strerror(errno));
  339. exit(EXIT_FAILURE);
  340. }
  341. if (fstat(dfd, &sbuf) < 0) {
  342. fprintf(stderr, "Can't stat %s: %s\n",
  343. datafile, strerror(errno));
  344. exit(EXIT_FAILURE);
  345. }
  346. if (sbuf.st_size == 0)
  347. goto close;
  348. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  349. if (ptr == MAP_FAILED) {
  350. fprintf(stderr, "Can't read %s: %s\n",
  351. datafile, strerror(errno));
  352. exit(EXIT_FAILURE);
  353. }
  354. size = sbuf.st_size;
  355. ret = lseek(ifd, offset, SEEK_SET);
  356. if (ret < 0) {
  357. fprintf(stderr, "%s: lseek error %s\n",
  358. __func__, strerror(errno));
  359. exit(EXIT_FAILURE);
  360. }
  361. if (write(ifd, ptr, size) != size) {
  362. fprintf(stderr, "Write error %s\n",
  363. strerror(errno));
  364. exit(EXIT_FAILURE);
  365. }
  366. tail = size % 4;
  367. pad = pad - size;
  368. if (pad == 1 && tail != 0) {
  369. if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
  370. fprintf(stderr, "Write error on %s\n",
  371. strerror(errno));
  372. exit(EXIT_FAILURE);
  373. }
  374. } else if (pad > 1) {
  375. while (pad > 0) {
  376. int todo = sizeof(zeros);
  377. if (todo > pad)
  378. todo = pad;
  379. if (write(ifd, (char *)&zeros, todo) != todo) {
  380. fprintf(stderr, "Write error: %s\n",
  381. strerror(errno));
  382. exit(EXIT_FAILURE);
  383. }
  384. pad -= todo;
  385. }
  386. }
  387. munmap((void *)ptr, sbuf.st_size);
  388. close:
  389. close(dfd);
  390. }
  391. uint64_t read_dcd_offset(char *filename)
  392. {
  393. int dfd;
  394. struct stat sbuf;
  395. uint8_t *ptr;
  396. uint64_t offset = 0;
  397. dfd = open(filename, O_RDONLY | O_BINARY);
  398. if (dfd < 0) {
  399. fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno));
  400. exit(EXIT_FAILURE);
  401. }
  402. if (fstat(dfd, &sbuf) < 0) {
  403. fprintf(stderr, "Can't stat %s: %s\n", filename, strerror(errno));
  404. exit(EXIT_FAILURE);
  405. }
  406. ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
  407. if (ptr == MAP_FAILED) {
  408. fprintf(stderr, "Can't read %s: %s\n", filename, strerror(errno));
  409. exit(EXIT_FAILURE);
  410. }
  411. offset = *(uint32_t *)(ptr + DCD_ENTRY_ADDR_IN_SCFW);
  412. munmap((void *)ptr, sbuf.st_size);
  413. close(dfd);
  414. return offset;
  415. }
  416. static void set_image_hash(boot_img_t *img, char *filename, uint32_t hash_type)
  417. {
  418. FILE *fp = NULL;
  419. char sha_command[512];
  420. char hash[2 * HASH_MAX_LEN + 1];
  421. int i, ret;
  422. if (img->size == 0)
  423. sprintf(sha_command, "sha%dsum /dev/null", hash_type);
  424. else
  425. sprintf(sha_command, "dd if=/dev/zero of=tmp_pad bs=%d count=1;\
  426. dd if=\'%s\' of=tmp_pad conv=notrunc;\
  427. sha%dsum tmp_pad; rm -f tmp_pad",
  428. img->size, filename, hash_type);
  429. switch (hash_type) {
  430. case HASH_TYPE_SHA_256:
  431. img->hab_flags |= IMG_FLAG_HASH_SHA256;
  432. break;
  433. case HASH_TYPE_SHA_384:
  434. img->hab_flags |= IMG_FLAG_HASH_SHA384;
  435. break;
  436. case HASH_TYPE_SHA_512:
  437. img->hab_flags |= IMG_FLAG_HASH_SHA512;
  438. break;
  439. default:
  440. fprintf(stderr, "Wrong hash type selected (%d) !!!\n\n",
  441. hash_type);
  442. exit(EXIT_FAILURE);
  443. break;
  444. }
  445. memset(img->hash, 0, HASH_MAX_LEN);
  446. fp = popen(sha_command, "r");
  447. if (!fp) {
  448. fprintf(stderr, "Failed to run command hash\n");
  449. exit(EXIT_FAILURE);
  450. }
  451. if (!fgets(hash, hash_type / 4 + 1, fp)) {
  452. fprintf(stderr, "Failed to hash file: %s\n", filename);
  453. exit(EXIT_FAILURE);
  454. }
  455. for (i = 0; i < strlen(hash) / 2; i++) {
  456. ret = sscanf(hash + 2 * i, "%02hhx", &img->hash[i]);
  457. if (ret < 0) {
  458. fprintf(stderr, "Failed sscanf hash: %d\n", ret);
  459. exit(EXIT_FAILURE);
  460. }
  461. }
  462. pclose(fp);
  463. }
  464. static void set_image_array_entry(flash_header_v3_t *container,
  465. soc_type_t soc, const image_t *image_stack,
  466. uint32_t offset, uint32_t size,
  467. char *tmp_filename, bool dcd_skip)
  468. {
  469. uint64_t entry = image_stack->entry;
  470. uint64_t core = image_stack->ext;
  471. uint32_t meta;
  472. char *tmp_name = "";
  473. option_type_t type = image_stack->option;
  474. boot_img_t *img = &container->img[container->num_images];
  475. img->offset = offset; /* Is re-adjusted later */
  476. img->size = size;
  477. set_image_hash(img, tmp_filename, IMAGE_HASH_ALGO_DEFAULT);
  478. switch (type) {
  479. case SECO:
  480. img->hab_flags |= IMG_TYPE_SECO;
  481. img->hab_flags |= CORE_SECO << BOOT_IMG_FLAGS_CORE_SHIFT;
  482. tmp_name = "SECO";
  483. img->dst = 0x20C00000;
  484. img->entry = 0x20000000;
  485. break;
  486. case AP:
  487. if (soc == QX && core == CORE_CA35) {
  488. meta = IMAGE_A35_DEFAULT_META(custom_partition);
  489. } else if (soc == QM && core == CORE_CA53) {
  490. meta = IMAGE_A53_DEFAULT_META(custom_partition);
  491. } else if (soc == QM && core == CORE_CA72) {
  492. meta = IMAGE_A72_DEFAULT_META(custom_partition);
  493. } else {
  494. fprintf(stderr,
  495. "Error: invalid AP core id: %" PRIu64 "\n",
  496. core);
  497. exit(EXIT_FAILURE);
  498. }
  499. img->hab_flags |= IMG_TYPE_EXEC;
  500. /* On B0, only core id = 4 is valid */
  501. img->hab_flags |= CORE_CA53 << BOOT_IMG_FLAGS_CORE_SHIFT;
  502. tmp_name = "AP";
  503. img->dst = entry;
  504. img->entry = entry;
  505. img->meta = meta;
  506. custom_partition = 0;
  507. break;
  508. case M40:
  509. case M41:
  510. if (core == 0) {
  511. core = CORE_CM4_0;
  512. meta = IMAGE_M4_0_DEFAULT_META(custom_partition);
  513. } else if (core == 1) {
  514. core = CORE_CM4_1;
  515. meta = IMAGE_M4_1_DEFAULT_META(custom_partition);
  516. } else {
  517. fprintf(stderr,
  518. "Error: invalid m4 core id: %" PRIu64 "\n",
  519. core);
  520. exit(EXIT_FAILURE);
  521. }
  522. img->hab_flags |= IMG_TYPE_EXEC;
  523. img->hab_flags |= core << BOOT_IMG_FLAGS_CORE_SHIFT;
  524. tmp_name = "M4";
  525. if ((entry & 0x7) != 0) {
  526. fprintf(stderr, "\n\nWarning: M4 Destination address is not 8 byte aligned\n\n");
  527. exit(EXIT_FAILURE);
  528. }
  529. img->dst = entry;
  530. img->entry = entry;
  531. img->meta = meta;
  532. custom_partition = 0;
  533. break;
  534. case DATA:
  535. img->hab_flags |= IMG_TYPE_DATA;
  536. img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
  537. tmp_name = "DATA";
  538. img->dst = entry;
  539. break;
  540. case MSG_BLOCK:
  541. img->hab_flags |= IMG_TYPE_DATA;
  542. img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
  543. img->meta = core << BOOT_IMG_META_MU_RID_SHIFT;
  544. tmp_name = "MSG_BLOCK";
  545. img->dst = entry;
  546. break;
  547. case SCFW:
  548. img->hab_flags |= scfw_flags & 0xFFFF0000;
  549. img->hab_flags |= IMG_TYPE_EXEC;
  550. img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
  551. tmp_name = "SCFW";
  552. img->dst = 0x1FFE0000;
  553. img->entry = 0x1FFE0000;
  554. /* Lets add the DCD now */
  555. if (!dcd_skip) {
  556. container->num_images++;
  557. img = &container->img[container->num_images];
  558. img->hab_flags |= IMG_TYPE_DCD_DDR;
  559. img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
  560. set_image_hash(img, "/dev/null",
  561. IMAGE_HASH_ALGO_DEFAULT);
  562. img->offset = offset + img->size;
  563. img->entry = read_dcd_offset(tmp_filename);
  564. img->dst = img->entry - 1;
  565. }
  566. break;
  567. default:
  568. fprintf(stderr, "unrecognized image type (%d)\n", type);
  569. exit(EXIT_FAILURE);
  570. }
  571. fprintf(stdout, "%s file_offset = 0x%x size = 0x%x\n", tmp_name, offset, size);
  572. container->num_images++;
  573. }
  574. void set_container(flash_header_v3_t *container, uint16_t sw_version,
  575. uint32_t alignment, uint32_t flags, uint16_t fuse_version)
  576. {
  577. container->sig_blk_hdr.tag = 0x90;
  578. container->sig_blk_hdr.length = sizeof(sig_blk_hdr_t);
  579. container->sw_version = sw_version;
  580. container->padding = alignment;
  581. container->fuse_version = fuse_version;
  582. container->flags = flags;
  583. fprintf(stdout, "container flags: 0x%x\n", container->flags);
  584. }
  585. static int get_container_image_start_pos(image_t *image_stack, uint32_t align)
  586. {
  587. image_t *img_sp = image_stack;
  588. /*8K total container header*/
  589. int file_off = CONTAINER_IMAGE_ARRAY_START_OFFSET;
  590. FILE *fd = NULL;
  591. flash_header_v3_t header;
  592. int ret;
  593. while (img_sp->option != NO_IMG) {
  594. if (img_sp->option == APPEND) {
  595. fd = fopen(img_sp->filename, "r");
  596. if (!fd) {
  597. fprintf(stderr, "Fail open first container file %s\n", img_sp->filename);
  598. exit(EXIT_FAILURE);
  599. }
  600. ret = fread(&header, sizeof(header), 1, fd);
  601. if (ret != 1) {
  602. printf("Failure Read header %d\n", ret);
  603. exit(EXIT_FAILURE);
  604. }
  605. fclose(fd);
  606. if (header.tag != IVT_HEADER_TAG_B0) {
  607. fprintf(stderr, "header tag mismatched \n");
  608. exit(EXIT_FAILURE);
  609. } else {
  610. file_off +=
  611. header.img[header.num_images - 1].size;
  612. file_off = ALIGN(file_off, align);
  613. }
  614. }
  615. img_sp++;
  616. }
  617. return file_off;
  618. }
  619. static void set_imx_hdr_v3(imx_header_v3_t *imxhdr, uint32_t cont_id)
  620. {
  621. flash_header_v3_t *fhdr_v3 = &imxhdr->fhdr[cont_id];
  622. /* Set magic number, Only >= B0 supported */
  623. fhdr_v3->tag = IVT_HEADER_TAG_B0;
  624. fhdr_v3->version = IVT_VERSION_B0;
  625. }
  626. static uint8_t *flatten_container_header(imx_header_v3_t *imx_header,
  627. uint8_t containers_count,
  628. uint32_t *size_out,
  629. uint32_t file_offset)
  630. {
  631. uint8_t *flat = NULL;
  632. uint8_t *ptr = NULL;
  633. uint16_t size = 0;
  634. int i, j;
  635. /* Compute size of all container headers */
  636. for (i = 0; i < containers_count; i++) {
  637. flash_header_v3_t *container = &imx_header->fhdr[i];
  638. container->sig_blk_offset = HEADER_IMG_ARRAY_OFFSET +
  639. container->num_images * IMG_ARRAY_ENTRY_SIZE;
  640. container->length = HEADER_IMG_ARRAY_OFFSET +
  641. (IMG_ARRAY_ENTRY_SIZE * container->num_images) +
  642. sizeof(sig_blk_hdr_t);
  643. /* Print info needed by CST to sign the container header */
  644. fprintf(stdout, "CST: CONTAINER %d offset: 0x%x\n",
  645. i, file_offset + size);
  646. fprintf(stdout, "CST: CONTAINER %d: Signature Block: offset is at 0x%x\n", i,
  647. file_offset + size + container->length -
  648. SIGNATURE_BLOCK_HEADER_LENGTH);
  649. size += ALIGN(container->length, container->padding);
  650. }
  651. flat = calloc(size, sizeof(uint8_t));
  652. if (!flat) {
  653. fprintf(stderr, "Failed to allocate memory (%d)\n", size);
  654. exit(EXIT_FAILURE);
  655. }
  656. ptr = flat;
  657. *size_out = size;
  658. for (i = 0; i < containers_count; i++) {
  659. flash_header_v3_t *container = &imx_header->fhdr[i];
  660. uint32_t container_start_offset = ptr - flat;
  661. /* Append container header */
  662. append(ptr, container, HEADER_IMG_ARRAY_OFFSET);
  663. /* Adjust images offset to start from container headers start */
  664. for (j = 0; j < container->num_images; j++) {
  665. container->img[j].offset -=
  666. container_start_offset + file_offset;
  667. }
  668. /* Append each image array entry */
  669. for (j = 0; j < container->num_images; j++)
  670. append(ptr, &container->img[j], sizeof(boot_img_t));
  671. append(ptr, &container->sig_blk_hdr, sizeof(sig_blk_hdr_t));
  672. /* Padding for container (if necessary) */
  673. ptr += ALIGN(container->length, container->padding) -
  674. container->length;
  675. }
  676. return flat;
  677. }
  678. static int build_container(soc_type_t soc, uint32_t sector_size,
  679. bool emmc_fastboot, image_t *image_stack,
  680. bool dcd_skip, uint8_t fuse_version,
  681. uint16_t sw_version, int ofd)
  682. {
  683. static imx_header_v3_t imx_header;
  684. image_t *img_sp = image_stack;
  685. int file_off;
  686. uint8_t *tmp;
  687. struct stat sbuf;
  688. char *tmp_filename = NULL;
  689. uint32_t size = 0;
  690. uint32_t file_padding = 0;
  691. int ret;
  692. int container = -1;
  693. int cont_img_count = 0; /* indexes to arrange the container */
  694. memset((char *)&imx_header, 0, sizeof(imx_header_v3_t));
  695. if (!image_stack) {
  696. fprintf(stderr, "Empty image stack ");
  697. exit(EXIT_FAILURE);
  698. }
  699. if (soc == QX)
  700. fprintf(stdout, "Platform:\ti.MX8QXP B0\n");
  701. else if (soc == QM)
  702. fprintf(stdout, "Platform:\ti.MX8QM B0\n");
  703. set_imx_hdr_v3(&imx_header, 0);
  704. set_imx_hdr_v3(&imx_header, 1);
  705. file_off = get_container_image_start_pos(image_stack, sector_size);
  706. fprintf(stdout, "container image offset (aligned):%x\n", file_off);
  707. /* step through image stack and generate the header */
  708. img_sp = image_stack;
  709. /* stop once we reach null terminator */
  710. while (img_sp->option != NO_IMG) {
  711. switch (img_sp->option) {
  712. case AP:
  713. case M40:
  714. case M41:
  715. case SCFW:
  716. case DATA:
  717. case MSG_BLOCK:
  718. if (container < 0) {
  719. fprintf(stderr, "No container found\n");
  720. exit(EXIT_FAILURE);
  721. }
  722. check_file(&sbuf, img_sp->filename);
  723. tmp_filename = img_sp->filename;
  724. set_image_array_entry(&imx_header.fhdr[container],
  725. soc, img_sp, file_off,
  726. ALIGN(sbuf.st_size, sector_size),
  727. tmp_filename, dcd_skip);
  728. img_sp->src = file_off;
  729. file_off += ALIGN(sbuf.st_size, sector_size);
  730. cont_img_count++;
  731. break;
  732. case SECO:
  733. if (container < 0) {
  734. fprintf(stderr, "No container found\n");
  735. exit(EXIT_FAILURE);
  736. }
  737. check_file(&sbuf, img_sp->filename);
  738. tmp_filename = img_sp->filename;
  739. set_image_array_entry(&imx_header.fhdr[container],
  740. soc,
  741. img_sp,
  742. file_off,
  743. sbuf.st_size,
  744. tmp_filename, dcd_skip);
  745. img_sp->src = file_off;
  746. file_off += sbuf.st_size;
  747. cont_img_count++;
  748. break;
  749. case NEW_CONTAINER:
  750. container++;
  751. set_container(&imx_header.fhdr[container], sw_version,
  752. CONTAINER_ALIGNMENT,
  753. CONTAINER_FLAGS_DEFAULT,
  754. fuse_version);
  755. /* reset img count when moving to new container */
  756. cont_img_count = 0;
  757. scfw_flags = 0;
  758. break;
  759. case APPEND:
  760. /*
  761. * nothing to do here, the container is appended
  762. * in the output
  763. */
  764. break;
  765. case FLAG:
  766. /*
  767. * override the flags for scfw in current container
  768. * mask off bottom 16 bits.
  769. */
  770. scfw_flags = img_sp->entry & 0xFFFF0000;
  771. break;
  772. case FILEOFF:
  773. if (file_off > img_sp->dst) {
  774. fprintf(stderr, "FILEOFF address less than current file offset!!!\n");
  775. exit(EXIT_FAILURE);
  776. }
  777. if (img_sp->dst != ALIGN(img_sp->dst, sector_size)) {
  778. fprintf(stderr, "FILEOFF address is not aligned to sector size!!!\n");
  779. exit(EXIT_FAILURE);
  780. }
  781. file_off = img_sp->dst;
  782. break;
  783. case PARTITION:
  784. /*
  785. * keep custom partition until next executable image
  786. * use a global var for default behaviour
  787. */
  788. custom_partition = img_sp->entry;
  789. break;
  790. default:
  791. fprintf(stderr, "unrecognized option in input stack (%d)\n", img_sp->option);
  792. exit(EXIT_FAILURE);
  793. }
  794. img_sp++; /* advance index */
  795. }
  796. /* Append container (if specified) */
  797. img_sp = image_stack;
  798. do {
  799. if (img_sp->option == APPEND) {
  800. copy_file(ofd, img_sp->filename, 0, 0);
  801. file_padding += FIRST_CONTAINER_HEADER_LENGTH;
  802. }
  803. img_sp++;
  804. } while (img_sp->option != NO_IMG);
  805. /* Add padding or skip appended container */
  806. ret = lseek(ofd, file_padding, SEEK_SET);
  807. if (ret < 0) {
  808. fprintf(stderr, "%s: lseek error %s\n",
  809. __func__, strerror(errno));
  810. exit(EXIT_FAILURE);
  811. }
  812. if (container >= 0) {
  813. /* Note: Image offset are not contained in the image */
  814. tmp = flatten_container_header(&imx_header, container + 1,
  815. &size, file_padding);
  816. /* Write image header */
  817. if (write(ofd, tmp, size) != size) {
  818. fprintf(stderr, "error writing image hdr\n");
  819. exit(EXIT_FAILURE);
  820. }
  821. /* Clean-up memory used by the headers */
  822. free(tmp);
  823. }
  824. /*
  825. * step through the image stack again this time copying
  826. * images to final bin, stop once we reach null terminator.
  827. */
  828. img_sp = image_stack;
  829. while (img_sp->option != NO_IMG) {
  830. if (img_sp->option == M40 || img_sp->option == M41 ||
  831. img_sp->option == AP || img_sp->option == DATA ||
  832. img_sp->option == SCD || img_sp->option == SCFW ||
  833. img_sp->option == SECO || img_sp->option == MSG_BLOCK) {
  834. copy_file_aligned(ofd, img_sp->filename, img_sp->src,
  835. sector_size);
  836. }
  837. img_sp++;
  838. }
  839. return 0;
  840. }
  841. int imx8image_copy_image(int outfd, struct image_tool_params *mparams)
  842. {
  843. image_t *img_sp = param_stack;
  844. /*
  845. * SECO FW is a container image, this is to calculate the
  846. * 2nd container offset.
  847. */
  848. fprintf(stdout, "parsing %s\n", mparams->imagename);
  849. parse_cfg_file(img_sp, mparams->imagename);
  850. if (sector_size == 0) {
  851. fprintf(stderr, "Wrong sector size\n");
  852. exit(EXIT_FAILURE);
  853. }
  854. fprintf(stdout, "CONTAINER Sector size:\t%08x\n", sector_size);
  855. fprintf(stdout, "CONTAINER FUSE VERSION:\t0x%02x\n", fuse_version);
  856. fprintf(stdout, "CONTAINER SW VERSION:\t0x%04x\n", sw_version);
  857. build_container(soc, sector_size, emmc_fastboot,
  858. img_sp, false, fuse_version, sw_version, outfd);
  859. return 0;
  860. }
  861. /*
  862. * imx8image parameters
  863. */
  864. U_BOOT_IMAGE_TYPE(
  865. imx8image,
  866. "NXP i.MX8 Boot Image support",
  867. 0,
  868. NULL,
  869. imx8image_check_params,
  870. NULL,
  871. imx8image_print_header,
  872. imx8image_set_header,
  873. NULL,
  874. imx8image_check_image_types,
  875. NULL,
  876. NULL
  877. );