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;
24 #define CSF_SIZE 0x2000
26 #define IMAGE_IVT_ID 1
28 #define HDMI_FW_SIZE 0x17000 /* Use Last 0x1000 for IVT and CSF */
29 #define ALIGN_SIZE 0x1000
30 #define ALIGN(x,a) __ALIGN_MASK((x), (__typeof__(x))(a) - 1, a)
31 #define __ALIGN_MASK(x,mask,mask2) (((x) + (mask)) / (mask2) * (mask2))
33 static uint32_t get_cfg_value(char *token, char *name, int linenr)
39 value = strtoul(token, &endptr, 16);
40 if (errno || token == endptr) {
41 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
48 int imx8mimage_check_params(struct image_tool_params *params)
53 static void imx8mimage_set_header(void *ptr, struct stat *sbuf, int ifd,
54 struct image_tool_params *params)
58 static void imx8mimage_print_header(const void *ptr)
62 static int imx8mimage_check_image_types(uint8_t type)
64 return (type == IH_TYPE_IMX8MIMAGE) ? EXIT_SUCCESS : EXIT_FAILURE;
67 static table_entry_t imx8mimage_cmds[] = {
68 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
69 {CMD_FIT, "FIT", "fit image", },
70 {CMD_SIGNED_HDMI, "SIGNED_HDMI", "signed hdmi image", },
71 {CMD_LOADER, "LOADER", "loader image", },
72 {CMD_SECOND_LOADER, "SECOND_LOADER", "2nd loader image", },
73 {CMD_DDR_FW, "DDR_FW", "ddr firmware", },
77 static table_entry_t imx8mimage_ivt_offset[] = {
78 {0x400, "sd", "sd/emmc",},
79 {0x400, "emmc_fastboot", "emmc fastboot",},
80 {0x1000, "fspi", "flexspi", },
81 {-1, "", "Invalid", },
84 static void parse_cfg_cmd(int32_t cmd, char *token, char *name, int lineno)
88 ivt_offset = get_table_entry_id(imx8mimage_ivt_offset,
89 "imx8mimage ivt offset",
91 if (!strncmp(token, "sd", 2))
92 rom_image_offset = 0x8000;
97 case CMD_SECOND_LOADER:
100 case CMD_SIGNED_HDMI:
109 static void parse_cfg_fld(int32_t *cmd, char *token,
110 char *name, int lineno, int fld)
114 *cmd = get_table_entry_id(imx8mimage_cmds,
115 "imx8mimage commands", token);
117 fprintf(stderr, "Error: %s[%d] - Invalid command" "(%s)\n",
118 name, lineno, token);
128 parse_cfg_cmd(*cmd, token, name, lineno);
130 case CFG_REG_ADDRESS:
133 ap_start_addr = get_cfg_value(token, name, lineno);
135 case CMD_SECOND_LOADER:
136 sld_start_addr = get_cfg_value(token, name, lineno);
142 case CMD_SECOND_LOADER:
143 sld_src_off = get_cfg_value(token, name, lineno);
151 static uint32_t parse_cfg_file(char *name)
155 char *token, *saveptr1, *saveptr2;
161 fd = fopen(name, "r");
163 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
168 * Very simple parsing, line starting with # are comments
171 while ((getline(&line, &len, fd)) > 0) {
174 token = strtok_r(line, "\r\n", &saveptr1);
178 /* Check inside the single line */
179 for (fld = CFG_COMMAND, cmd = CFG_INVALID,
180 line = token; ; line = NULL, fld++) {
181 token = strtok_r(line, " \t", &saveptr2);
185 /* Drop all text starting with '#' as comments */
189 parse_cfg_fld(&cmd, token, name, lineno, fld);
196 static void fill_zero(int ifd, int size, int offset)
202 memset(zeros, 0, sizeof(zeros));
204 ret = lseek(ifd, offset, SEEK_SET);
206 fprintf(stderr, "%s seek: %s\n", __func__, strerror(errno));
216 if (write(ifd, (char *)&zeros, fill_size) != fill_size) {
217 fprintf(stderr, "Write error: %s\n",
226 static void copy_file(int ifd, const char *datafile, int pad, int offset,
237 memset(zeros, 0, sizeof(zeros));
239 dfd = open(datafile, O_RDONLY | O_BINARY);
241 fprintf(stderr, "Can't open %s: %s\n",
242 datafile, strerror(errno));
246 if (fstat(dfd, &sbuf) < 0) {
247 fprintf(stderr, "Can't stat %s: %s\n",
248 datafile, strerror(errno));
252 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
253 if (ptr == MAP_FAILED) {
254 fprintf(stderr, "Can't read %s: %s\n",
255 datafile, strerror(errno));
259 size = sbuf.st_size - datafile_offset;
260 ret = lseek(ifd, offset, SEEK_SET);
262 fprintf(stderr, "lseek ifd fail\n");
266 if (write(ifd, ptr + datafile_offset, size) != size) {
267 fprintf(stderr, "Write error %s\n",
274 if (pad == 1 && tail != 0) {
275 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
276 fprintf(stderr, "Write error on %s\n",
280 } else if (pad > 1) {
282 int todo = sizeof(zeros);
286 if (write(ifd, (char *)&zeros, todo) != todo) {
287 fprintf(stderr, "Write error: %s\n",
295 munmap((void *)ptr, sbuf.st_size);
299 /* Return this IVT offset in the final output file */
300 static int generate_ivt_for_fit(int fd, int fit_offset, uint32_t ep,
301 uint32_t *fit_load_addr)
303 image_header_t image_header;
306 uint32_t fit_size, load_addr;
307 int align_len = 64 - 1; /* 64 is cacheline size */
309 ret = lseek(fd, fit_offset, SEEK_SET);
311 fprintf(stderr, "lseek fd fail for fit\n");
315 if (read(fd, (char *)&image_header, sizeof(image_header_t)) !=
316 sizeof(image_header_t)) {
317 fprintf(stderr, "generate_ivt_for_fit read failed: %s\n",
322 if (be32_to_cpu(image_header.ih_magic) != FDT_MAGIC) {
323 fprintf(stderr, "%s error: not a FIT file\n", __func__);
327 fit_size = fdt_totalsize(&image_header);
328 fit_size = (fit_size + 3) & ~3;
330 fit_size = ALIGN(fit_size, ALIGN_SIZE);
332 ret = lseek(fd, fit_offset + fit_size, SEEK_SET);
334 fprintf(stderr, "lseek fd fail for fit\n");
339 * ep is the u-boot entry. SPL loads the FIT before the u-boot
340 * address. 0x2000 is for CSF_SIZE
342 load_addr = (ep - (fit_size + CSF_SIZE) - 512 - align_len) &
345 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
347 (load_addr + fit_size),
348 (load_addr + fit_size + 0x20),
351 if (write(fd, &ivt_header, sizeof(flash_header_v2_t)) !=
352 sizeof(flash_header_v2_t)) {
353 fprintf(stderr, "IVT writing error on fit image\n");
357 *fit_load_addr = load_addr;
359 return fit_offset + fit_size;
362 static void dump_header_v2(imx_header_v3_t *imx_header, int index)
364 const char *ivt_name[2] = {"HDMI FW", "LOADER IMAGE"};
366 fprintf(stdout, "========= IVT HEADER [%s] =========\n",
368 fprintf(stdout, "header.tag: \t\t0x%x\n",
369 imx_header[index].fhdr.header.tag);
370 fprintf(stdout, "header.length: \t\t0x%x\n",
371 imx_header[index].fhdr.header.length);
372 fprintf(stdout, "header.version: \t0x%x\n",
373 imx_header[index].fhdr.header.version);
374 fprintf(stdout, "entry: \t\t\t0x%x\n",
375 imx_header[index].fhdr.entry);
376 fprintf(stdout, "reserved1: \t\t0x%x\n",
377 imx_header[index].fhdr.reserved1);
378 fprintf(stdout, "dcd_ptr: \t\t0x%x\n",
379 imx_header[index].fhdr.dcd_ptr);
380 fprintf(stdout, "boot_data_ptr: \t\t0x%x\n",
381 imx_header[index].fhdr.boot_data_ptr);
382 fprintf(stdout, "self: \t\t\t0x%x\n",
383 imx_header[index].fhdr.self);
384 fprintf(stdout, "csf: \t\t\t0x%x\n",
385 imx_header[index].fhdr.csf);
386 fprintf(stdout, "reserved2: \t\t0x%x\n",
387 imx_header[index].fhdr.reserved2);
389 fprintf(stdout, "boot_data.start: \t0x%x\n",
390 imx_header[index].boot_data.start);
391 fprintf(stdout, "boot_data.size: \t0x%x\n",
392 imx_header[index].boot_data.size);
393 fprintf(stdout, "boot_data.plugin: \t0x%x\n",
394 imx_header[index].boot_data.plugin);
397 void build_image(int ofd)
399 int file_off, header_hdmi_off = 0, header_image_off;
400 int hdmi_fd, ap_fd, sld_fd;
401 uint32_t sld_load_addr = 0;
402 uint32_t csf_off, sld_csf_off = 0;
407 fprintf(stderr, "No LOADER image specificed\n");
414 header_hdmi_off = file_off + ivt_offset;
416 hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY);
418 fprintf(stderr, "%s: Can't open: %s\n",
419 signed_hdmi, strerror(errno));
423 if (fstat(hdmi_fd, &sbuf) < 0) {
424 fprintf(stderr, "%s: Can't stat: %s\n",
425 signed_hdmi, strerror(errno));
431 * Aligned to 104KB = 92KB FW image + 0x8000
432 * (IVT and alignment) + 0x4000 (second IVT + CSF)
434 file_off += ALIGN(sbuf.st_size,
435 HDMI_FW_SIZE + 0x2000 + 0x1000);
438 header_image_off = file_off + ivt_offset;
440 ap_fd = open(ap_img, O_RDONLY | O_BINARY);
442 fprintf(stderr, "%s: Can't open: %s\n",
443 ap_img, strerror(errno));
446 if (fstat(ap_fd, &sbuf) < 0) {
447 fprintf(stderr, "%s: Can't stat: %s\n",
448 ap_img, strerror(errno));
453 imx_header[IMAGE_IVT_ID].fhdr.header.tag = IVT_HEADER_TAG; /* 0xD1 */
454 imx_header[IMAGE_IVT_ID].fhdr.header.length =
455 cpu_to_be16(sizeof(flash_header_v2_t));
456 imx_header[IMAGE_IVT_ID].fhdr.header.version = IVT_VERSION_V3; /* 0x41 */
457 imx_header[IMAGE_IVT_ID].fhdr.entry = ap_start_addr;
458 imx_header[IMAGE_IVT_ID].fhdr.self = ap_start_addr -
459 sizeof(imx_header_v3_t);
460 imx_header[IMAGE_IVT_ID].fhdr.dcd_ptr = 0;
461 imx_header[IMAGE_IVT_ID].fhdr.boot_data_ptr =
462 imx_header[IMAGE_IVT_ID].fhdr.self +
463 offsetof(imx_header_v3_t, boot_data);
464 imx_header[IMAGE_IVT_ID].boot_data.start =
465 imx_header[IMAGE_IVT_ID].fhdr.self - ivt_offset;
466 imx_header[IMAGE_IVT_ID].boot_data.size =
467 ALIGN(sbuf.st_size + sizeof(imx_header_v3_t) + ivt_offset,
470 image_off = header_image_off + sizeof(imx_header_v3_t);
471 file_off += imx_header[IMAGE_IVT_ID].boot_data.size;
473 imx_header[IMAGE_IVT_ID].boot_data.plugin = 0;
474 imx_header[IMAGE_IVT_ID].fhdr.csf =
475 imx_header[IMAGE_IVT_ID].boot_data.start +
476 imx_header[IMAGE_IVT_ID].boot_data.size;
478 imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */
481 file_off += CSF_SIZE;
483 /* Second boot loader image */
486 fprintf(stderr, "Not support no fit\n");
489 sld_header_off = sld_src_off - rom_image_offset;
491 * Record the second bootloader relative offset in
492 * image's IVT reserved1
494 imx_header[IMAGE_IVT_ID].fhdr.reserved1 =
495 sld_header_off - header_image_off;
496 sld_fd = open(sld_img, O_RDONLY | O_BINARY);
498 fprintf(stderr, "%s: Can't open: %s\n",
499 sld_img, strerror(errno));
503 if (fstat(sld_fd, &sbuf) < 0) {
504 fprintf(stderr, "%s: Can't stat: %s\n",
505 sld_img, strerror(errno));
511 file_off = sld_header_off;
512 file_off += sbuf.st_size + sizeof(image_header_t);
517 header_hdmi_off -= ivt_offset;
518 ret = lseek(ofd, header_hdmi_off, SEEK_SET);
520 fprintf(stderr, "lseek ofd fail for hdmi\n");
524 /* The signed HDMI FW has 0x400 IVT offset, need remove it */
525 copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400);
529 header_image_off -= ivt_offset;
530 image_off -= ivt_offset;
531 ret = lseek(ofd, header_image_off, SEEK_SET);
533 fprintf(stderr, "lseek ofd fail\n");
537 /* Write image header */
538 if (write(ofd, &imx_header[IMAGE_IVT_ID], sizeof(imx_header_v3_t)) !=
539 sizeof(imx_header_v3_t)) {
540 fprintf(stderr, "error writing image hdr\n");
544 copy_file(ofd, ap_img, 0, image_off, 0);
546 csf_off -= ivt_offset;
547 fill_zero(ofd, CSF_SIZE, csf_off);
550 sld_header_off -= ivt_offset;
551 ret = lseek(ofd, sld_header_off, SEEK_SET);
553 fprintf(stderr, "lseek ofd fail for sld_img\n");
557 /* Write image header */
561 copy_file(ofd, sld_img, 0, sld_header_off, 0);
563 generate_ivt_for_fit(ofd, sld_header_off,
565 &sld_load_addr) + 0x20;
570 dump_header_v2(imx_header, 0);
571 dump_header_v2(imx_header, 1);
573 fprintf(stdout, "========= OFFSET dump =========");
575 fprintf(stdout, "\nSIGNED HDMI FW:\n");
576 fprintf(stdout, " header_hdmi_off \t0x%x\n",
580 fprintf(stdout, "\nLoader IMAGE:\n");
581 fprintf(stdout, " header_image_off \t0x%x\n image_off \t\t0x%x\n csf_off \t\t0x%x\n",
582 header_image_off, image_off, csf_off);
583 fprintf(stdout, " spl hab block: \t0x%x 0x%x 0x%x\n",
584 imx_header[IMAGE_IVT_ID].fhdr.self, header_image_off,
585 csf_off - header_image_off);
587 fprintf(stdout, "\nSecond Loader IMAGE:\n");
588 fprintf(stdout, " sld_header_off \t0x%x\n",
590 fprintf(stdout, " sld_csf_off \t\t0x%x\n",
592 fprintf(stdout, " sld hab block: \t0x%x 0x%x 0x%x\n",
593 sld_load_addr, sld_header_off, sld_csf_off - sld_header_off);
596 int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams)
599 * SECO FW is a container image, this is to calculate the
600 * 2nd container offset.
602 fprintf(stdout, "parsing %s\n", mparams->imagename);
603 parse_cfg_file(mparams->imagename);
611 * imx8mimage parameters
615 "NXP i.MX8M Boot Image support",
618 imx8mimage_check_params,
620 imx8mimage_print_header,
621 imx8mimage_set_header,
623 imx8mimage_check_image_types,