1 // SPDX-License-Identifier: GPL-2.0+
5 * Peng Fan <peng.fan@nxp.com>
14 static uint32_t ap_start_addr, sld_start_addr, sld_src_off;
15 static char *ap_img, *sld_img, *signed_hdmi;
16 static imx_header_v3_t imx_header[2]; /* At most there are 3 IVT headers */
17 static uint32_t rom_image_offset;
18 static uint32_t sector_size = 0x200;
19 static uint32_t image_off;
20 static uint32_t sld_header_off;
21 static uint32_t ivt_offset;
22 static uint32_t using_fit;
27 static uint32_t rom_version = ROM_V1;
29 #define CSF_SIZE 0x2000
31 #define IMAGE_IVT_ID 1
33 #define HDMI_FW_SIZE 0x17000 /* Use Last 0x1000 for IVT and CSF */
34 #define ALIGN_SIZE 0x1000
36 static uint32_t get_cfg_value(char *token, char *name, int linenr)
42 value = strtoul(token, &endptr, 16);
43 if (errno || token == endptr) {
44 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
51 int imx8mimage_check_params(struct image_tool_params *params)
56 static void imx8mimage_set_header(void *ptr, struct stat *sbuf, int ifd,
57 struct image_tool_params *params)
61 static void imx8mimage_print_header(const void *ptr)
65 static int imx8mimage_check_image_types(uint8_t type)
67 return (type == IH_TYPE_IMX8MIMAGE) ? EXIT_SUCCESS : EXIT_FAILURE;
70 static table_entry_t imx8mimage_cmds[] = {
71 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
72 {CMD_FIT, "FIT", "fit image", },
73 {CMD_SIGNED_HDMI, "SIGNED_HDMI", "signed hdmi image", },
74 {CMD_LOADER, "LOADER", "loader image", },
75 {CMD_SECOND_LOADER, "SECOND_LOADER", "2nd loader image", },
76 {CMD_DDR_FW, "DDR_FW", "ddr firmware", },
77 {CMD_ROM_VERSION, "ROM_VERSION", "rom version", },
81 static table_entry_t imx8mimage_ivt_offset[] = {
82 {0x400, "sd", "sd/emmc",},
83 {0x400, "emmc_fastboot", "emmc fastboot",},
84 {0x1000, "fspi", "flexspi", },
85 {-1, "", "Invalid", },
88 static void parse_cfg_cmd(int32_t cmd, char *token, char *name, int lineno)
92 ivt_offset = get_table_entry_id(imx8mimage_ivt_offset,
93 "imx8mimage ivt offset",
95 if (!strncmp(token, "sd", 2))
96 rom_image_offset = 0x8000;
98 if (rom_version == ROM_V2)
104 case CMD_SECOND_LOADER:
107 case CMD_SIGNED_HDMI:
113 case CMD_ROM_VERSION:
114 if (!strncmp(token, "v2", 2)) {
115 rom_version = ROM_V2;
117 } else if (!strncmp(token, "v1", 2)) {
118 rom_version = ROM_V1;
125 static void parse_cfg_fld(int32_t *cmd, char *token,
126 char *name, int lineno, int fld)
130 *cmd = get_table_entry_id(imx8mimage_cmds,
131 "imx8mimage commands", token);
133 fprintf(stderr, "Error: %s[%d] - Invalid command" "(%s)\n",
134 name, lineno, token);
144 parse_cfg_cmd(*cmd, token, name, lineno);
146 case CFG_REG_ADDRESS:
149 ap_start_addr = get_cfg_value(token, name, lineno);
151 case CMD_SECOND_LOADER:
152 sld_start_addr = get_cfg_value(token, name, lineno);
158 case CMD_SECOND_LOADER:
159 sld_src_off = get_cfg_value(token, name, lineno);
167 static uint32_t parse_cfg_file(char *name)
171 char *token, *saveptr1, *saveptr2;
177 fd = fopen(name, "r");
179 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
184 * Very simple parsing, line starting with # are comments
187 while ((getline(&line, &len, fd)) > 0) {
190 token = strtok_r(line, "\r\n", &saveptr1);
194 /* Check inside the single line */
195 for (fld = CFG_COMMAND, cmd = CFG_INVALID,
196 line = token; ; line = NULL, fld++) {
197 token = strtok_r(line, " \t", &saveptr2);
201 /* Drop all text starting with '#' as comments */
205 parse_cfg_fld(&cmd, token, name, lineno, fld);
212 static void fill_zero(int ifd, int size, int offset)
218 memset(zeros, 0, sizeof(zeros));
220 ret = lseek(ifd, offset, SEEK_SET);
222 fprintf(stderr, "%s seek: %s\n", __func__, strerror(errno));
232 if (write(ifd, (char *)&zeros, fill_size) != fill_size) {
233 fprintf(stderr, "Write error: %s\n",
242 static void copy_file(int ifd, const char *datafile, int pad, int offset,
253 memset(zeros, 0, sizeof(zeros));
255 dfd = open(datafile, O_RDONLY | O_BINARY);
257 fprintf(stderr, "Can't open %s: %s\n",
258 datafile, strerror(errno));
262 if (fstat(dfd, &sbuf) < 0) {
263 fprintf(stderr, "Can't stat %s: %s\n",
264 datafile, strerror(errno));
268 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
269 if (ptr == MAP_FAILED) {
270 fprintf(stderr, "Can't read %s: %s\n",
271 datafile, strerror(errno));
275 size = sbuf.st_size - datafile_offset;
276 ret = lseek(ifd, offset, SEEK_SET);
278 fprintf(stderr, "lseek ifd fail\n");
282 if (write(ifd, ptr + datafile_offset, size) != size) {
283 fprintf(stderr, "Write error %s\n",
290 if (pad == 1 && tail != 0) {
291 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
292 fprintf(stderr, "Write error on %s\n",
296 } else if (pad > 1) {
298 int todo = sizeof(zeros);
302 if (write(ifd, (char *)&zeros, todo) != todo) {
303 fprintf(stderr, "Write error: %s\n",
311 munmap((void *)ptr, sbuf.st_size);
315 /* Return this IVT offset in the final output file */
316 static int generate_ivt_for_fit(int fd, int fit_offset, uint32_t ep,
317 uint32_t *fit_load_addr)
319 image_header_t image_header;
322 uint32_t fit_size, load_addr;
323 int align_len = 64 - 1; /* 64 is cacheline size */
325 ret = lseek(fd, fit_offset, SEEK_SET);
327 fprintf(stderr, "lseek fd fail for fit\n");
331 if (read(fd, (char *)&image_header, sizeof(image_header_t)) !=
332 sizeof(image_header_t)) {
333 fprintf(stderr, "generate_ivt_for_fit read failed: %s\n",
338 if (be32_to_cpu(image_header.ih_magic) != FDT_MAGIC) {
339 fprintf(stderr, "%s error: not a FIT file\n", __func__);
343 fit_size = fdt_totalsize(&image_header);
345 fit_size = ALIGN(fit_size, ALIGN_SIZE);
347 ret = lseek(fd, fit_offset + fit_size, SEEK_SET);
349 fprintf(stderr, "lseek fd fail for fit\n");
354 * ep is the u-boot entry. SPL loads the FIT before the u-boot
355 * address. 0x2000 is for CSF_SIZE
357 load_addr = (ep - (fit_size + CSF_SIZE) - 512 - align_len) &
360 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
362 (load_addr + fit_size),
363 (load_addr + fit_size + 0x20),
366 if (write(fd, &ivt_header, sizeof(flash_header_v2_t)) !=
367 sizeof(flash_header_v2_t)) {
368 fprintf(stderr, "IVT writing error on fit image\n");
372 *fit_load_addr = load_addr;
374 return fit_offset + fit_size;
377 static void dump_header_v2(imx_header_v3_t *imx_header, int index)
379 const char *ivt_name[2] = {"HDMI FW", "LOADER IMAGE"};
381 fprintf(stdout, "========= IVT HEADER [%s] =========\n",
383 fprintf(stdout, "header.tag: \t\t0x%x\n",
384 imx_header[index].fhdr.header.tag);
385 fprintf(stdout, "header.length: \t\t0x%x\n",
386 imx_header[index].fhdr.header.length);
387 fprintf(stdout, "header.version: \t0x%x\n",
388 imx_header[index].fhdr.header.version);
389 fprintf(stdout, "entry: \t\t\t0x%x\n",
390 imx_header[index].fhdr.entry);
391 fprintf(stdout, "reserved1: \t\t0x%x\n",
392 imx_header[index].fhdr.reserved1);
393 fprintf(stdout, "dcd_ptr: \t\t0x%x\n",
394 imx_header[index].fhdr.dcd_ptr);
395 fprintf(stdout, "boot_data_ptr: \t\t0x%x\n",
396 imx_header[index].fhdr.boot_data_ptr);
397 fprintf(stdout, "self: \t\t\t0x%x\n",
398 imx_header[index].fhdr.self);
399 fprintf(stdout, "csf: \t\t\t0x%x\n",
400 imx_header[index].fhdr.csf);
401 fprintf(stdout, "reserved2: \t\t0x%x\n",
402 imx_header[index].fhdr.reserved2);
404 fprintf(stdout, "boot_data.start: \t0x%x\n",
405 imx_header[index].boot_data.start);
406 fprintf(stdout, "boot_data.size: \t0x%x\n",
407 imx_header[index].boot_data.size);
408 fprintf(stdout, "boot_data.plugin: \t0x%x\n",
409 imx_header[index].boot_data.plugin);
412 void build_image(int ofd)
414 int file_off, header_hdmi_off = 0, header_image_off;
415 int hdmi_fd, ap_fd, sld_fd;
416 uint32_t sld_load_addr = 0;
417 uint32_t csf_off, sld_csf_off = 0;
422 fprintf(stderr, "No LOADER image specificed\n");
429 header_hdmi_off = file_off + ivt_offset;
431 hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY);
433 fprintf(stderr, "%s: Can't open: %s\n",
434 signed_hdmi, strerror(errno));
438 if (fstat(hdmi_fd, &sbuf) < 0) {
439 fprintf(stderr, "%s: Can't stat: %s\n",
440 signed_hdmi, strerror(errno));
446 * Aligned to 104KB = 92KB FW image + 0x8000
447 * (IVT and alignment) + 0x4000 (second IVT + CSF)
449 file_off += ALIGN(sbuf.st_size,
450 HDMI_FW_SIZE + 0x2000 + 0x1000);
453 header_image_off = file_off + ivt_offset;
455 ap_fd = open(ap_img, O_RDONLY | O_BINARY);
457 fprintf(stderr, "%s: Can't open: %s\n",
458 ap_img, strerror(errno));
461 if (fstat(ap_fd, &sbuf) < 0) {
462 fprintf(stderr, "%s: Can't stat: %s\n",
463 ap_img, strerror(errno));
468 imx_header[IMAGE_IVT_ID].fhdr.header.tag = IVT_HEADER_TAG; /* 0xD1 */
469 imx_header[IMAGE_IVT_ID].fhdr.header.length =
470 cpu_to_be16(sizeof(flash_header_v2_t));
471 imx_header[IMAGE_IVT_ID].fhdr.header.version = IVT_VERSION_V3; /* 0x41 */
472 imx_header[IMAGE_IVT_ID].fhdr.entry = ap_start_addr;
473 imx_header[IMAGE_IVT_ID].fhdr.self = ap_start_addr -
474 sizeof(imx_header_v3_t);
475 imx_header[IMAGE_IVT_ID].fhdr.dcd_ptr = 0;
476 imx_header[IMAGE_IVT_ID].fhdr.boot_data_ptr =
477 imx_header[IMAGE_IVT_ID].fhdr.self +
478 offsetof(imx_header_v3_t, boot_data);
479 imx_header[IMAGE_IVT_ID].boot_data.start =
480 imx_header[IMAGE_IVT_ID].fhdr.self - ivt_offset;
481 imx_header[IMAGE_IVT_ID].boot_data.size =
482 ALIGN(sbuf.st_size + sizeof(imx_header_v3_t) + ivt_offset,
485 image_off = header_image_off + sizeof(imx_header_v3_t);
486 file_off += imx_header[IMAGE_IVT_ID].boot_data.size;
488 imx_header[IMAGE_IVT_ID].boot_data.plugin = 0;
489 imx_header[IMAGE_IVT_ID].fhdr.csf =
490 imx_header[IMAGE_IVT_ID].boot_data.start +
491 imx_header[IMAGE_IVT_ID].boot_data.size;
493 imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */
496 file_off += CSF_SIZE;
498 /* Second boot loader image */
501 fprintf(stderr, "Not support no fit\n");
504 sld_header_off = sld_src_off - rom_image_offset;
506 * Record the second bootloader relative offset in
507 * image's IVT reserved1
509 if (rom_version == ROM_V1) {
510 imx_header[IMAGE_IVT_ID].fhdr.reserved1 =
511 sld_header_off - header_image_off;
513 sld_fd = open(sld_img, O_RDONLY | O_BINARY);
515 fprintf(stderr, "%s: Can't open: %s\n",
516 sld_img, strerror(errno));
520 if (fstat(sld_fd, &sbuf) < 0) {
521 fprintf(stderr, "%s: Can't stat: %s\n",
522 sld_img, strerror(errno));
528 file_off = sld_header_off;
529 file_off += sbuf.st_size + sizeof(image_header_t);
534 header_hdmi_off -= ivt_offset;
535 ret = lseek(ofd, header_hdmi_off, SEEK_SET);
537 fprintf(stderr, "lseek ofd fail for hdmi\n");
541 /* The signed HDMI FW has 0x400 IVT offset, need remove it */
542 copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400);
546 header_image_off -= ivt_offset;
547 image_off -= ivt_offset;
548 ret = lseek(ofd, header_image_off, SEEK_SET);
550 fprintf(stderr, "lseek ofd fail\n");
554 /* Write image header */
555 if (write(ofd, &imx_header[IMAGE_IVT_ID], sizeof(imx_header_v3_t)) !=
556 sizeof(imx_header_v3_t)) {
557 fprintf(stderr, "error writing image hdr\n");
561 copy_file(ofd, ap_img, 0, image_off, 0);
563 csf_off -= ivt_offset;
564 fill_zero(ofd, CSF_SIZE, csf_off);
567 sld_header_off -= ivt_offset;
568 ret = lseek(ofd, sld_header_off, SEEK_SET);
570 fprintf(stderr, "lseek ofd fail for sld_img\n");
574 /* Write image header */
578 copy_file(ofd, sld_img, 0, sld_header_off, 0);
580 generate_ivt_for_fit(ofd, sld_header_off,
582 &sld_load_addr) + 0x20;
587 dump_header_v2(imx_header, 0);
588 dump_header_v2(imx_header, 1);
590 fprintf(stdout, "========= OFFSET dump =========");
592 fprintf(stdout, "\nSIGNED HDMI FW:\n");
593 fprintf(stdout, " header_hdmi_off \t0x%x\n",
597 fprintf(stdout, "\nLoader IMAGE:\n");
598 fprintf(stdout, " header_image_off \t0x%x\n image_off \t\t0x%x\n csf_off \t\t0x%x\n",
599 header_image_off, image_off, csf_off);
600 fprintf(stdout, " spl hab block: \t0x%x 0x%x 0x%x\n",
601 imx_header[IMAGE_IVT_ID].fhdr.self, header_image_off,
602 csf_off - header_image_off);
604 fprintf(stdout, "\nSecond Loader IMAGE:\n");
605 fprintf(stdout, " sld_header_off \t0x%x\n",
607 fprintf(stdout, " sld_csf_off \t\t0x%x\n",
609 fprintf(stdout, " sld hab block: \t0x%x 0x%x 0x%x\n",
610 sld_load_addr, sld_header_off, sld_csf_off - sld_header_off);
613 int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams)
616 * SECO FW is a container image, this is to calculate the
617 * 2nd container offset.
619 fprintf(stdout, "parsing %s\n", mparams->imagename);
620 parse_cfg_file(mparams->imagename);
628 * imx8mimage parameters
632 "NXP i.MX8M Boot Image support",
635 imx8mimage_check_params,
637 imx8mimage_print_header,
638 imx8mimage_set_header,
640 imx8mimage_check_image_types,