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;
127 static void parse_cfg_fld(int32_t *cmd, char *token,
128 char *name, int lineno, int fld)
132 *cmd = get_table_entry_id(imx8mimage_cmds,
133 "imx8mimage commands", token);
135 fprintf(stderr, "Error: %s[%d] - Invalid command" "(%s)\n",
136 name, lineno, token);
146 parse_cfg_cmd(*cmd, token, name, lineno);
148 case CFG_REG_ADDRESS:
151 ap_start_addr = get_cfg_value(token, name, lineno);
153 case CMD_SECOND_LOADER:
154 sld_start_addr = get_cfg_value(token, name, lineno);
160 case CMD_SECOND_LOADER:
161 sld_src_off = get_cfg_value(token, name, lineno);
169 static uint32_t parse_cfg_file(char *name)
173 char *token, *saveptr1, *saveptr2;
179 fd = fopen(name, "r");
181 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
186 * Very simple parsing, line starting with # are comments
189 while ((getline(&line, &len, fd)) > 0) {
192 token = strtok_r(line, "\r\n", &saveptr1);
196 /* Check inside the single line */
197 for (fld = CFG_COMMAND, cmd = CFG_INVALID,
198 line = token; ; line = NULL, fld++) {
199 token = strtok_r(line, " \t", &saveptr2);
203 /* Drop all text starting with '#' as comments */
207 parse_cfg_fld(&cmd, token, name, lineno, fld);
214 static void fill_zero(int ifd, int size, int offset)
220 memset(zeros, 0, sizeof(zeros));
222 ret = lseek(ifd, offset, SEEK_SET);
224 fprintf(stderr, "%s seek: %s\n", __func__, strerror(errno));
234 if (write(ifd, (char *)&zeros, fill_size) != fill_size) {
235 fprintf(stderr, "Write error: %s\n",
244 static void copy_file(int ifd, const char *datafile, int pad, int offset,
255 memset(zeros, 0, sizeof(zeros));
257 dfd = open(datafile, O_RDONLY | O_BINARY);
259 fprintf(stderr, "Can't open %s: %s\n",
260 datafile, strerror(errno));
264 if (fstat(dfd, &sbuf) < 0) {
265 fprintf(stderr, "Can't stat %s: %s\n",
266 datafile, strerror(errno));
270 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
271 if (ptr == MAP_FAILED) {
272 fprintf(stderr, "Can't read %s: %s\n",
273 datafile, strerror(errno));
277 size = sbuf.st_size - datafile_offset;
278 ret = lseek(ifd, offset, SEEK_SET);
280 fprintf(stderr, "lseek ifd fail\n");
284 if (write(ifd, ptr + datafile_offset, size) != size) {
285 fprintf(stderr, "Write error %s\n",
292 if (pad == 1 && tail != 0) {
293 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
294 fprintf(stderr, "Write error on %s\n",
298 } else if (pad > 1) {
300 int todo = sizeof(zeros);
304 if (write(ifd, (char *)&zeros, todo) != todo) {
305 fprintf(stderr, "Write error: %s\n",
313 munmap((void *)ptr, sbuf.st_size);
318 /* Return this IVT offset in the final output file */
319 static int generate_ivt_for_fit(int fd, int fit_offset, uint32_t ep,
320 uint32_t *fit_load_addr)
322 image_header_t image_header;
325 uint32_t fit_size, load_addr;
326 int align_len = 64 - 1; /* 64 is cacheline size */
328 ret = lseek(fd, fit_offset, SEEK_SET);
330 fprintf(stderr, "lseek fd fail for fit\n");
334 if (read(fd, (char *)&image_header, sizeof(image_header_t)) !=
335 sizeof(image_header_t)) {
336 fprintf(stderr, "generate_ivt_for_fit read failed: %s\n",
341 if (be32_to_cpu(image_header.ih_magic) != FDT_MAGIC) {
342 fprintf(stderr, "%s error: not a FIT file\n", __func__);
346 fit_size = fdt_totalsize(&image_header);
348 fit_size = ALIGN_IMX(fit_size, ALIGN_SIZE);
350 ret = lseek(fd, fit_offset + fit_size, SEEK_SET);
352 fprintf(stderr, "lseek fd fail for fit\n");
357 * ep is the u-boot entry. SPL loads the FIT before the u-boot
358 * address. 0x2000 is for CSF_SIZE
360 load_addr = (ep - (fit_size + CSF_SIZE) - 512 - align_len) &
363 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
365 (load_addr + fit_size),
366 (load_addr + fit_size + 0x20),
369 if (write(fd, &ivt_header, sizeof(flash_header_v2_t)) !=
370 sizeof(flash_header_v2_t)) {
371 fprintf(stderr, "IVT writing error on fit image\n");
375 *fit_load_addr = load_addr;
377 return fit_offset + fit_size;
380 static void dump_header_v2(imx_header_v3_t *imx_header, int index)
382 const char *ivt_name[2] = {"HDMI FW", "LOADER IMAGE"};
384 fprintf(stdout, "========= IVT HEADER [%s] =========\n",
386 fprintf(stdout, "header.tag: \t\t0x%x\n",
387 imx_header[index].fhdr.header.tag);
388 fprintf(stdout, "header.length: \t\t0x%x\n",
389 imx_header[index].fhdr.header.length);
390 fprintf(stdout, "header.version: \t0x%x\n",
391 imx_header[index].fhdr.header.version);
392 fprintf(stdout, "entry: \t\t\t0x%x\n",
393 imx_header[index].fhdr.entry);
394 fprintf(stdout, "reserved1: \t\t0x%x\n",
395 imx_header[index].fhdr.reserved1);
396 fprintf(stdout, "dcd_ptr: \t\t0x%x\n",
397 imx_header[index].fhdr.dcd_ptr);
398 fprintf(stdout, "boot_data_ptr: \t\t0x%x\n",
399 imx_header[index].fhdr.boot_data_ptr);
400 fprintf(stdout, "self: \t\t\t0x%x\n",
401 imx_header[index].fhdr.self);
402 fprintf(stdout, "csf: \t\t\t0x%x\n",
403 imx_header[index].fhdr.csf);
404 fprintf(stdout, "reserved2: \t\t0x%x\n",
405 imx_header[index].fhdr.reserved2);
407 fprintf(stdout, "boot_data.start: \t0x%x\n",
408 imx_header[index].boot_data.start);
409 fprintf(stdout, "boot_data.size: \t0x%x\n",
410 imx_header[index].boot_data.size);
411 fprintf(stdout, "boot_data.plugin: \t0x%x\n",
412 imx_header[index].boot_data.plugin);
415 void build_image(int ofd)
417 int file_off, header_hdmi_off = 0, header_image_off;
418 int hdmi_fd, ap_fd, sld_fd;
419 uint32_t sld_load_addr = 0;
420 uint32_t csf_off, sld_csf_off = 0;
425 fprintf(stderr, "No LOADER image specificed\n");
432 header_hdmi_off = file_off + ivt_offset;
434 hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY);
436 fprintf(stderr, "%s: Can't open: %s\n",
437 signed_hdmi, strerror(errno));
441 if (fstat(hdmi_fd, &sbuf) < 0) {
442 fprintf(stderr, "%s: Can't stat: %s\n",
443 signed_hdmi, strerror(errno));
449 * Aligned to 104KB = 92KB FW image + 0x8000
450 * (IVT and alignment) + 0x4000 (second IVT + CSF)
452 file_off += ALIGN_IMX(sbuf.st_size,
453 HDMI_FW_SIZE + 0x2000 + 0x1000);
456 header_image_off = file_off + ivt_offset;
458 ap_fd = open(ap_img, O_RDONLY | O_BINARY);
460 fprintf(stderr, "%s: Can't open: %s\n",
461 ap_img, strerror(errno));
464 if (fstat(ap_fd, &sbuf) < 0) {
465 fprintf(stderr, "%s: Can't stat: %s\n",
466 ap_img, strerror(errno));
471 imx_header[IMAGE_IVT_ID].fhdr.header.tag = IVT_HEADER_TAG; /* 0xD1 */
472 imx_header[IMAGE_IVT_ID].fhdr.header.length =
473 cpu_to_be16(sizeof(flash_header_v2_t));
474 imx_header[IMAGE_IVT_ID].fhdr.header.version = IVT_VERSION_V3; /* 0x41 */
475 imx_header[IMAGE_IVT_ID].fhdr.entry = ap_start_addr;
476 imx_header[IMAGE_IVT_ID].fhdr.self = ap_start_addr -
477 sizeof(imx_header_v3_t);
478 imx_header[IMAGE_IVT_ID].fhdr.dcd_ptr = 0;
479 imx_header[IMAGE_IVT_ID].fhdr.boot_data_ptr =
480 imx_header[IMAGE_IVT_ID].fhdr.self +
481 offsetof(imx_header_v3_t, boot_data);
482 imx_header[IMAGE_IVT_ID].boot_data.start =
483 imx_header[IMAGE_IVT_ID].fhdr.self - ivt_offset;
484 imx_header[IMAGE_IVT_ID].boot_data.size =
485 ALIGN_IMX(sbuf.st_size + sizeof(imx_header_v3_t) + ivt_offset,
488 image_off = header_image_off + sizeof(imx_header_v3_t);
489 file_off += imx_header[IMAGE_IVT_ID].boot_data.size;
491 imx_header[IMAGE_IVT_ID].boot_data.plugin = 0;
492 imx_header[IMAGE_IVT_ID].fhdr.csf =
493 imx_header[IMAGE_IVT_ID].boot_data.start +
494 imx_header[IMAGE_IVT_ID].boot_data.size;
496 imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */
499 file_off += CSF_SIZE;
501 /* Second boot loader image */
504 fprintf(stderr, "Not support no fit\n");
507 sld_header_off = sld_src_off - rom_image_offset;
509 * Record the second bootloader relative offset in
510 * image's IVT reserved1
512 if (rom_version == ROM_V1) {
513 imx_header[IMAGE_IVT_ID].fhdr.reserved1 =
514 sld_header_off - header_image_off;
516 sld_fd = open(sld_img, O_RDONLY | O_BINARY);
518 fprintf(stderr, "%s: Can't open: %s\n",
519 sld_img, strerror(errno));
523 if (fstat(sld_fd, &sbuf) < 0) {
524 fprintf(stderr, "%s: Can't stat: %s\n",
525 sld_img, strerror(errno));
531 file_off = sld_header_off;
532 file_off += sbuf.st_size + sizeof(image_header_t);
537 header_hdmi_off -= ivt_offset;
538 ret = lseek(ofd, header_hdmi_off, SEEK_SET);
540 fprintf(stderr, "lseek ofd fail for hdmi\n");
544 /* The signed HDMI FW has 0x400 IVT offset, need remove it */
545 copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400);
549 header_image_off -= ivt_offset;
550 image_off -= ivt_offset;
551 ret = lseek(ofd, header_image_off, SEEK_SET);
553 fprintf(stderr, "lseek ofd fail\n");
557 /* Write image header */
558 if (write(ofd, &imx_header[IMAGE_IVT_ID], sizeof(imx_header_v3_t)) !=
559 sizeof(imx_header_v3_t)) {
560 fprintf(stderr, "error writing image hdr\n");
564 copy_file(ofd, ap_img, 0, image_off, 0);
566 csf_off -= ivt_offset;
567 fill_zero(ofd, CSF_SIZE, csf_off);
570 sld_header_off -= ivt_offset;
571 ret = lseek(ofd, sld_header_off, SEEK_SET);
573 fprintf(stderr, "lseek ofd fail for sld_img\n");
577 /* Write image header */
581 copy_file(ofd, sld_img, 0, sld_header_off, 0);
583 generate_ivt_for_fit(ofd, sld_header_off,
585 &sld_load_addr) + 0x20;
590 dump_header_v2(imx_header, 0);
591 dump_header_v2(imx_header, 1);
593 fprintf(stdout, "========= OFFSET dump =========");
595 fprintf(stdout, "\nSIGNED HDMI FW:\n");
596 fprintf(stdout, " header_hdmi_off \t0x%x\n",
600 fprintf(stdout, "\nLoader IMAGE:\n");
601 fprintf(stdout, " header_image_off \t0x%x\n image_off \t\t0x%x\n csf_off \t\t0x%x\n",
602 header_image_off, image_off, csf_off);
603 fprintf(stdout, " spl hab block: \t0x%x 0x%x 0x%x\n",
604 imx_header[IMAGE_IVT_ID].fhdr.self, header_image_off,
605 csf_off - header_image_off);
607 fprintf(stdout, "\nSecond Loader IMAGE:\n");
608 fprintf(stdout, " sld_header_off \t0x%x\n",
610 fprintf(stdout, " sld_csf_off \t\t0x%x\n",
612 fprintf(stdout, " sld hab block: \t0x%x 0x%x 0x%x\n",
613 sld_load_addr, sld_header_off, sld_csf_off - sld_header_off);
616 int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams)
619 * SECO FW is a container image, this is to calculate the
620 * 2nd container offset.
622 fprintf(stdout, "parsing %s\n", mparams->imagename);
623 parse_cfg_file(mparams->imagename);
631 * imx8mimage parameters
635 "NXP i.MX8M Boot Image support",
638 imx8mimage_check_params,
640 imx8mimage_print_header,
641 imx8mimage_set_header,
643 imx8mimage_check_image_types,