imx8image.c 25 KB

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