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
35 #define ALIGN_IMX(x, a) __ALIGN_MASK_IMX((x), (__typeof__(x))(a) - 1, a)
36 #define __ALIGN_MASK_IMX(x, mask, mask2) (((x) + (mask)) / (mask2) * (mask2))
38 static uint32_t get_cfg_value(char *token, char *name, int linenr)
44 value = strtoul(token, &endptr, 16);
45 if (errno || token == endptr) {
46 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
53 int imx8mimage_check_params(struct image_tool_params *params)
58 static void imx8mimage_set_header(void *ptr, struct stat *sbuf, int ifd,
59 struct image_tool_params *params)
63 static void imx8mimage_print_header(const void *ptr)
67 static int imx8mimage_check_image_types(uint8_t type)
69 return (type == IH_TYPE_IMX8MIMAGE) ? EXIT_SUCCESS : EXIT_FAILURE;
72 static table_entry_t imx8mimage_cmds[] = {
73 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
74 {CMD_FIT, "FIT", "fit image", },
75 {CMD_SIGNED_HDMI, "SIGNED_HDMI", "signed hdmi image", },
76 {CMD_LOADER, "LOADER", "loader image", },
77 {CMD_SECOND_LOADER, "SECOND_LOADER", "2nd loader image", },
78 {CMD_DDR_FW, "DDR_FW", "ddr firmware", },
79 {CMD_ROM_VERSION, "ROM_VERSION", "rom version", },
83 static table_entry_t imx8mimage_ivt_offset[] = {
84 {0x400, "sd", "sd/emmc",},
85 {0x400, "emmc_fastboot", "emmc fastboot",},
86 {0x1000, "fspi", "flexspi", },
87 {-1, "", "Invalid", },
90 static void parse_cfg_cmd(int32_t cmd, char *token, char *name, int lineno)
94 ivt_offset = get_table_entry_id(imx8mimage_ivt_offset,
95 "imx8mimage ivt offset",
97 if (!strncmp(token, "sd", 2))
98 rom_image_offset = 0x8000;
100 if (rom_version == ROM_V2)
106 case CMD_SECOND_LOADER:
109 case CMD_SIGNED_HDMI:
115 case CMD_ROM_VERSION:
116 if (!strncmp(token, "v2", 2)) {
117 rom_version = ROM_V2;
119 } else if (!strncmp(token, "v1", 2)) {
120 rom_version = ROM_V1;
126 static void parse_cfg_fld(int32_t *cmd, char *token,
127 char *name, int lineno, int fld)
131 *cmd = get_table_entry_id(imx8mimage_cmds,
132 "imx8mimage commands", token);
134 fprintf(stderr, "Error: %s[%d] - Invalid command" "(%s)\n",
135 name, lineno, token);
145 parse_cfg_cmd(*cmd, token, name, lineno);
147 case CFG_REG_ADDRESS:
150 ap_start_addr = get_cfg_value(token, name, lineno);
152 case CMD_SECOND_LOADER:
153 sld_start_addr = get_cfg_value(token, name, lineno);
159 case CMD_SECOND_LOADER:
160 sld_src_off = get_cfg_value(token, name, lineno);
168 static uint32_t parse_cfg_file(char *name)
172 char *token, *saveptr1, *saveptr2;
178 fd = fopen(name, "r");
180 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
185 * Very simple parsing, line starting with # are comments
188 while ((getline(&line, &len, fd)) > 0) {
191 token = strtok_r(line, "\r\n", &saveptr1);
195 /* Check inside the single line */
196 for (fld = CFG_COMMAND, cmd = CFG_INVALID,
197 line = token; ; line = NULL, fld++) {
198 token = strtok_r(line, " \t", &saveptr2);
202 /* Drop all text starting with '#' as comments */
206 parse_cfg_fld(&cmd, token, name, lineno, fld);
213 static void fill_zero(int ifd, int size, int offset)
219 memset(zeros, 0, sizeof(zeros));
221 ret = lseek(ifd, offset, SEEK_SET);
223 fprintf(stderr, "%s seek: %s\n", __func__, strerror(errno));
233 if (write(ifd, (char *)&zeros, fill_size) != fill_size) {
234 fprintf(stderr, "Write error: %s\n",
243 static void copy_file(int ifd, const char *datafile, int pad, int offset,
254 memset(zeros, 0, sizeof(zeros));
256 dfd = open(datafile, O_RDONLY | O_BINARY);
258 fprintf(stderr, "Can't open %s: %s\n",
259 datafile, strerror(errno));
263 if (fstat(dfd, &sbuf) < 0) {
264 fprintf(stderr, "Can't stat %s: %s\n",
265 datafile, strerror(errno));
269 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
270 if (ptr == MAP_FAILED) {
271 fprintf(stderr, "Can't read %s: %s\n",
272 datafile, strerror(errno));
276 size = sbuf.st_size - datafile_offset;
277 ret = lseek(ifd, offset, SEEK_SET);
279 fprintf(stderr, "lseek ifd fail\n");
283 if (write(ifd, ptr + datafile_offset, size) != size) {
284 fprintf(stderr, "Write error %s\n",
291 if (pad == 1 && tail != 0) {
292 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
293 fprintf(stderr, "Write error on %s\n",
297 } else if (pad > 1) {
299 int todo = sizeof(zeros);
303 if (write(ifd, (char *)&zeros, todo) != todo) {
304 fprintf(stderr, "Write error: %s\n",
312 munmap((void *)ptr, sbuf.st_size);
317 /* Return this IVT offset in the final output file */
318 static int generate_ivt_for_fit(int fd, int fit_offset, uint32_t ep,
319 uint32_t *fit_load_addr)
321 struct legacy_img_hdr image_header;
324 uint32_t fit_size, load_addr;
325 int align_len = 64 - 1; /* 64 is cacheline size */
327 ret = lseek(fd, fit_offset, SEEK_SET);
329 fprintf(stderr, "lseek fd fail for fit\n");
333 if (read(fd, (char *)&image_header, sizeof(struct legacy_img_hdr)) !=
334 sizeof(struct legacy_img_hdr)) {
335 fprintf(stderr, "generate_ivt_for_fit read failed: %s\n",
340 if (be32_to_cpu(image_header.ih_magic) != FDT_MAGIC) {
341 fprintf(stderr, "%s error: not a FIT file\n", __func__);
345 fit_size = fdt_totalsize(&image_header);
347 fit_size = ALIGN_IMX(fit_size, ALIGN_SIZE);
349 ret = lseek(fd, fit_offset + fit_size, SEEK_SET);
351 fprintf(stderr, "lseek fd fail for fit\n");
356 * ep is the u-boot entry. SPL loads the FIT before the u-boot
357 * address. 0x2000 is for CSF_SIZE
359 load_addr = (ep - (fit_size + CSF_SIZE) - 512 - align_len) &
362 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
364 (load_addr + fit_size),
365 (load_addr + fit_size + 0x20),
368 if (write(fd, &ivt_header, sizeof(flash_header_v2_t)) !=
369 sizeof(flash_header_v2_t)) {
370 fprintf(stderr, "IVT writing error on fit image\n");
374 *fit_load_addr = load_addr;
376 return fit_offset + fit_size;
379 static void dump_header_v2(imx_header_v3_t *imx_header, int index)
381 const char *ivt_name[2] = {"HDMI FW", "LOADER IMAGE"};
383 fprintf(stdout, "========= IVT HEADER [%s] =========\n",
385 fprintf(stdout, "header.tag: \t\t0x%x\n",
386 imx_header[index].fhdr.header.tag);
387 fprintf(stdout, "header.length: \t\t0x%x\n",
388 imx_header[index].fhdr.header.length);
389 fprintf(stdout, "header.version: \t0x%x\n",
390 imx_header[index].fhdr.header.version);
391 fprintf(stdout, "entry: \t\t\t0x%x\n",
392 imx_header[index].fhdr.entry);
393 fprintf(stdout, "reserved1: \t\t0x%x\n",
394 imx_header[index].fhdr.reserved1);
395 fprintf(stdout, "dcd_ptr: \t\t0x%x\n",
396 imx_header[index].fhdr.dcd_ptr);
397 fprintf(stdout, "boot_data_ptr: \t\t0x%x\n",
398 imx_header[index].fhdr.boot_data_ptr);
399 fprintf(stdout, "self: \t\t\t0x%x\n",
400 imx_header[index].fhdr.self);
401 fprintf(stdout, "csf: \t\t\t0x%x\n",
402 imx_header[index].fhdr.csf);
403 fprintf(stdout, "reserved2: \t\t0x%x\n",
404 imx_header[index].fhdr.reserved2);
406 fprintf(stdout, "boot_data.start: \t0x%x\n",
407 imx_header[index].boot_data.start);
408 fprintf(stdout, "boot_data.size: \t0x%x\n",
409 imx_header[index].boot_data.size);
410 fprintf(stdout, "boot_data.plugin: \t0x%x\n",
411 imx_header[index].boot_data.plugin);
414 #ifdef CONFIG_FSPI_CONF_HEADER
415 static int generate_fspi_header (int ifd)
419 char lut_str[] = CONFIG_LUT_SEQUENCE;
421 fspi_conf fspi_conf_data = {
422 .tag = {0x46, 0x43, 0x46, 0x42},
423 .version = {0x00, 0x00, 0x01, 0x56},
424 .reserved_1 = {0x00, 0x00, 0x00, 0x00},
425 .read_sample = CONFIG_READ_CLK_SOURCE,
428 .coladdrwidth = 0x00,
429 .devcfgenable = 0x00,
430 .reserved_2 = {0x00, 0x00, 0x00},
431 .devmodeseq = {0x00, 0x00, 0x00, 0x00},
432 .devmodearg = {0x00, 0x00, 0x00, 0x00},
434 .reserved_3 = {0x00},
435 .cmd_seq = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
436 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
437 .cmd_arg = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
438 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
439 .controllermisc = {0x00, 0x00, 0x00, 0x00},
440 .dev_type = CONFIG_DEVICE_TYPE,
441 .sflash_pad = CONFIG_FLASH_PAD_TYPE,
442 .serial_clk = CONFIG_SERIAL_CLK_FREQUENCY,
443 .lut_custom = CONFIG_LUT_CUSTOM_SEQUENCE,
444 .reserved_4 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
445 .sflashA1 = {0x00, 0x00, 0x00, 0x10},
446 .sflashA2 = {0x00, 0x00, 0x00, 0x00},
447 .sflashB1 = {0x00, 0x00, 0x00, 0x00},
448 .sflashB2 = {0x00, 0x00, 0x00, 0x00},
449 .cspadover = {0x00, 0x00, 0x00, 0x00},
450 .sclkpadover = {0x00, 0x00, 0x00, 0x00},
451 .datapadover = {0x00, 0x00, 0x00, 0x00},
452 .dqspadover = {0x00, 0x00, 0x00, 0x00},
453 .timeout = {0x00, 0x00, 0x00, 0x00},
454 .commandInt = {0x00, 0x00, 0x00, 0x00},
455 .datavalid = {0x00, 0x00, 0x00, 0x00},
456 .busyoffset = {0x00, 0x00},
457 .busybitpolarity = {0x00, 0x00},
460 for (val = strtok(lut_str, ","); val; val = strtok(NULL, ",")) {
461 fspi_conf_data.lut[i++] = strtoul(val, NULL, 16);
464 ret = lseek(ifd, 0, SEEK_CUR);
465 if (write(ifd, &fspi_conf_data, sizeof(fspi_conf_data)) == -1)
468 ret = lseek(ifd, sizeof(fspi_conf_data), SEEK_CUR);
474 void build_image(int ofd)
476 int file_off, header_hdmi_off = 0, header_image_off;
478 #ifdef CONFIG_FSPI_CONF_HEADER
479 int fspi_off, fspi_fd;
483 int hdmi_fd, ap_fd, sld_fd;
484 uint32_t sld_load_addr = 0;
485 uint32_t csf_off, sld_csf_off = 0;
490 fprintf(stderr, "No LOADER image specificed\n");
497 header_hdmi_off = file_off + ivt_offset;
499 hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY);
501 fprintf(stderr, "%s: Can't open: %s\n",
502 signed_hdmi, strerror(errno));
506 if (fstat(hdmi_fd, &sbuf) < 0) {
507 fprintf(stderr, "%s: Can't stat: %s\n",
508 signed_hdmi, strerror(errno));
514 * Aligned to 104KB = 92KB FW image + 0x8000
515 * (IVT and alignment) + 0x4000 (second IVT + CSF)
517 file_off += ALIGN_IMX(sbuf.st_size,
518 HDMI_FW_SIZE + 0x2000 + 0x1000);
521 header_image_off = file_off + ivt_offset;
523 #ifdef CONFIG_FSPI_CONF_HEADER
524 fspi = CONFIG_FSPI_CONF_FILE;
525 fspi_fd = open(fspi, O_RDWR | O_CREAT, S_IRWXU);
527 fprintf(stderr, "Can't open %s: %s\n",
528 fspi, strerror(errno));
532 fspi_off = generate_fspi_header(fspi_fd);
533 file_off = header_image_off + fspi_off;
537 ap_fd = open(ap_img, O_RDONLY | O_BINARY);
539 fprintf(stderr, "%s: Can't open: %s\n",
540 ap_img, strerror(errno));
543 if (fstat(ap_fd, &sbuf) < 0) {
544 fprintf(stderr, "%s: Can't stat: %s\n",
545 ap_img, strerror(errno));
550 imx_header[IMAGE_IVT_ID].fhdr.header.tag = IVT_HEADER_TAG; /* 0xD1 */
551 imx_header[IMAGE_IVT_ID].fhdr.header.length =
552 cpu_to_be16(sizeof(flash_header_v2_t));
553 imx_header[IMAGE_IVT_ID].fhdr.header.version = IVT_VERSION_V3; /* 0x41 */
554 imx_header[IMAGE_IVT_ID].fhdr.entry = ap_start_addr;
555 imx_header[IMAGE_IVT_ID].fhdr.self = ap_start_addr -
556 sizeof(imx_header_v3_t);
557 imx_header[IMAGE_IVT_ID].fhdr.dcd_ptr = 0;
558 imx_header[IMAGE_IVT_ID].fhdr.boot_data_ptr =
559 imx_header[IMAGE_IVT_ID].fhdr.self +
560 offsetof(imx_header_v3_t, boot_data);
561 imx_header[IMAGE_IVT_ID].boot_data.start =
562 imx_header[IMAGE_IVT_ID].fhdr.self - ivt_offset;
563 imx_header[IMAGE_IVT_ID].boot_data.size =
564 ALIGN_IMX(sbuf.st_size + sizeof(imx_header_v3_t) + ivt_offset,
567 image_off = header_image_off + sizeof(imx_header_v3_t);
568 file_off += imx_header[IMAGE_IVT_ID].boot_data.size;
570 imx_header[IMAGE_IVT_ID].boot_data.plugin = 0;
571 imx_header[IMAGE_IVT_ID].fhdr.csf =
572 imx_header[IMAGE_IVT_ID].boot_data.start +
573 imx_header[IMAGE_IVT_ID].boot_data.size;
575 imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */
578 file_off += CSF_SIZE;
580 /* Second boot loader image */
583 fprintf(stderr, "Not support no fit\n");
586 sld_header_off = sld_src_off - rom_image_offset;
587 sld_fd = open(sld_img, O_RDONLY | O_BINARY);
589 fprintf(stderr, "%s: Can't open: %s\n",
590 sld_img, strerror(errno));
594 if (fstat(sld_fd, &sbuf) < 0) {
595 fprintf(stderr, "%s: Can't stat: %s\n",
596 sld_img, strerror(errno));
602 file_off = sld_header_off;
603 file_off += sbuf.st_size + sizeof(struct legacy_img_hdr);
608 header_hdmi_off -= ivt_offset;
609 ret = lseek(ofd, header_hdmi_off, SEEK_SET);
611 fprintf(stderr, "lseek ofd fail for hdmi\n");
615 /* The signed HDMI FW has 0x400 IVT offset, need remove it */
616 copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400);
620 header_image_off -= ivt_offset;
621 image_off -= ivt_offset;
622 ret = lseek(ofd, header_image_off, SEEK_SET);
624 fprintf(stderr, "lseek ofd fail\n");
628 /* Write image header */
629 if (write(ofd, &imx_header[IMAGE_IVT_ID], sizeof(imx_header_v3_t)) !=
630 sizeof(imx_header_v3_t)) {
631 fprintf(stderr, "error writing image hdr\n");
635 copy_file(ofd, ap_img, 0, image_off, 0);
637 csf_off -= ivt_offset;
638 fill_zero(ofd, CSF_SIZE, csf_off);
641 sld_header_off -= ivt_offset;
642 ret = lseek(ofd, sld_header_off, SEEK_SET);
644 fprintf(stderr, "lseek ofd fail for sld_img\n");
648 /* Write image header */
652 copy_file(ofd, sld_img, 0, sld_header_off, 0);
654 generate_ivt_for_fit(ofd, sld_header_off,
656 &sld_load_addr) + 0x20;
661 dump_header_v2(imx_header, 0);
662 dump_header_v2(imx_header, 1);
664 fprintf(stdout, "========= OFFSET dump =========");
666 fprintf(stdout, "\nSIGNED HDMI FW:\n");
667 fprintf(stdout, " header_hdmi_off \t0x%x\n",
671 fprintf(stdout, "\nLoader IMAGE:\n");
672 fprintf(stdout, " header_image_off \t0x%x\n image_off \t\t0x%x\n csf_off \t\t0x%x\n",
673 header_image_off, image_off, csf_off);
674 fprintf(stdout, " spl hab block: \t0x%x 0x%x 0x%x\n",
675 imx_header[IMAGE_IVT_ID].fhdr.self, header_image_off,
676 csf_off - header_image_off);
678 fprintf(stdout, "\nSecond Loader IMAGE:\n");
679 fprintf(stdout, " sld_header_off \t0x%x\n",
681 fprintf(stdout, " sld_csf_off \t\t0x%x\n",
683 fprintf(stdout, " sld hab block: \t0x%x 0x%x 0x%x\n",
684 sld_load_addr, sld_header_off, sld_csf_off - sld_header_off);
687 int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams)
690 * SECO FW is a container image, this is to calculate the
691 * 2nd container offset.
693 fprintf(stdout, "parsing %s\n", mparams->imagename);
694 parse_cfg_file(mparams->imagename);
702 * imx8mimage parameters
706 "NXP i.MX8M Boot Image support",
709 imx8mimage_check_params,
711 imx8mimage_print_header,
712 imx8mimage_set_header,
714 imx8mimage_check_image_types,