binman: Use an exit code when blobs are missing
[platform/kernel/u-boot.git] / tools / imx8mimage.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2018 NXP
4  *
5  * Peng Fan <peng.fan@nxp.com>
6  */
7
8
9 #include "imagetool.h"
10 #include <image.h>
11 #include "imximage.h"
12 #include "compiler.h"
13
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;
23
24 #define ROM_V1 1
25 #define ROM_V2 2
26
27 static uint32_t rom_version = ROM_V1;
28
29 #define CSF_SIZE 0x2000
30 #define HDMI_IVT_ID 0
31 #define IMAGE_IVT_ID 1
32
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))
37
38 static uint32_t get_cfg_value(char *token, char *name,  int linenr)
39 {
40         char *endptr;
41         uint32_t value;
42
43         errno = 0;
44         value = strtoul(token, &endptr, 16);
45         if (errno || token == endptr) {
46                 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
47                         name,  linenr, token);
48                 exit(EXIT_FAILURE);
49         }
50         return value;
51 }
52
53 int imx8mimage_check_params(struct image_tool_params *params)
54 {
55         return 0;
56 }
57
58 static void imx8mimage_set_header(void *ptr, struct stat *sbuf, int ifd,
59                                   struct image_tool_params *params)
60 {
61 }
62
63 static void imx8mimage_print_header(const void *ptr)
64 {
65 }
66
67 static int imx8mimage_check_image_types(uint8_t type)
68 {
69         return (type == IH_TYPE_IMX8MIMAGE) ? EXIT_SUCCESS : EXIT_FAILURE;
70 }
71
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",        },
80         {-1,                    "",                     "",                   },
81 };
82
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",      },
88 };
89
90 static void parse_cfg_cmd(int32_t cmd, char *token, char *name, int lineno)
91 {
92         switch (cmd) {
93         case CMD_BOOT_FROM:
94                 ivt_offset = get_table_entry_id(imx8mimage_ivt_offset,
95                                                 "imx8mimage ivt offset",
96                                                 token);
97                 if (!strncmp(token, "sd", 2))
98                         rom_image_offset = 0x8000;
99
100                 if (rom_version == ROM_V2)
101                         ivt_offset = 0;
102                 break;
103         case CMD_LOADER:
104                 ap_img = token;
105                 break;
106         case CMD_SECOND_LOADER:
107                 sld_img = token;
108                 break;
109         case CMD_SIGNED_HDMI:
110                 signed_hdmi = token;
111                 break;
112         case CMD_DDR_FW:
113                 /* Do nothing */
114                 break;
115         case CMD_ROM_VERSION:
116                 if (!strncmp(token, "v2", 2)) {
117                         rom_version = ROM_V2;
118                         ivt_offset = 0;
119                 } else if (!strncmp(token, "v1", 2)) {
120                         rom_version = ROM_V1;
121                 }
122                 break;
123         }
124 }
125
126 static void parse_cfg_fld(int32_t *cmd, char *token,
127                           char *name, int lineno, int fld)
128 {
129         switch (fld) {
130         case CFG_COMMAND:
131                 *cmd = get_table_entry_id(imx8mimage_cmds,
132                                           "imx8mimage commands", token);
133                 if (*cmd < 0) {
134                         fprintf(stderr, "Error: %s[%d] - Invalid command" "(%s)\n",
135                                 name, lineno, token);
136                         exit(EXIT_FAILURE);
137                 }
138                 switch (*cmd) {
139                 case CMD_FIT:
140                         using_fit = 1;
141                         break;
142                 }
143                 break;
144         case CFG_REG_SIZE:
145                 parse_cfg_cmd(*cmd, token, name, lineno);
146                 break;
147         case CFG_REG_ADDRESS:
148                 switch (*cmd) {
149                 case CMD_LOADER:
150                         ap_start_addr = get_cfg_value(token, name, lineno);
151                         break;
152                 case CMD_SECOND_LOADER:
153                         sld_start_addr = get_cfg_value(token, name, lineno);
154                         break;
155                 }
156                 break;
157         case CFG_REG_VALUE:
158                 switch (*cmd) {
159                 case CMD_SECOND_LOADER:
160                         sld_src_off = get_cfg_value(token, name, lineno);
161                         break;
162                 }
163         default:
164                 break;
165         }
166 }
167
168 static uint32_t parse_cfg_file(char *name)
169 {
170         FILE *fd = NULL;
171         char *line = NULL;
172         char *token, *saveptr1, *saveptr2;
173         int lineno = 0;
174         int fld;
175         size_t len;
176         int32_t cmd;
177
178         fd = fopen(name, "r");
179         if (fd == 0) {
180                 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
181                 exit(EXIT_FAILURE);
182         }
183
184         /*
185          * Very simple parsing, line starting with # are comments
186          * and are dropped
187          */
188         while ((getline(&line, &len, fd)) > 0) {
189                 lineno++;
190
191                 token = strtok_r(line, "\r\n", &saveptr1);
192                 if (!token)
193                         continue;
194
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);
199                         if (!token)
200                                 break;
201
202                         /* Drop all text starting with '#' as comments */
203                         if (token[0] == '#')
204                                 break;
205
206                         parse_cfg_fld(&cmd, token, name, lineno, fld);
207                 }
208         }
209
210         return 0;
211 }
212
213 static void fill_zero(int ifd, int size, int offset)
214 {
215         int fill_size;
216         uint8_t zeros[4096];
217         int ret;
218
219         memset(zeros, 0, sizeof(zeros));
220
221         ret = lseek(ifd, offset, SEEK_SET);
222         if (ret < 0) {
223                 fprintf(stderr, "%s seek: %s\n", __func__, strerror(errno));
224                 exit(EXIT_FAILURE);
225         }
226
227         while (size) {
228                 if (size > 4096)
229                         fill_size = 4096;
230                 else
231                         fill_size = size;
232
233                 if (write(ifd, (char *)&zeros, fill_size) != fill_size) {
234                         fprintf(stderr, "Write error: %s\n",
235                                 strerror(errno));
236                         exit(EXIT_FAILURE);
237                 }
238
239                 size -= fill_size;
240         };
241 }
242
243 static void copy_file(int ifd, const char *datafile, int pad, int offset,
244                       int datafile_offset)
245 {
246         int dfd;
247         struct stat sbuf;
248         unsigned char *ptr;
249         int tail;
250         uint64_t zero = 0;
251         uint8_t zeros[4096];
252         int size, ret;
253
254         memset(zeros, 0, sizeof(zeros));
255
256         dfd = open(datafile, O_RDONLY | O_BINARY);
257         if (dfd < 0) {
258                 fprintf(stderr, "Can't open %s: %s\n",
259                         datafile, strerror(errno));
260                 exit(EXIT_FAILURE);
261         }
262
263         if (fstat(dfd, &sbuf) < 0) {
264                 fprintf(stderr, "Can't stat %s: %s\n",
265                         datafile, strerror(errno));
266                 exit(EXIT_FAILURE);
267         }
268
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));
273                 goto err_mmap;
274         }
275
276         size = sbuf.st_size - datafile_offset;
277         ret = lseek(ifd, offset, SEEK_SET);
278         if (ret < 0) {
279                 fprintf(stderr, "lseek ifd fail\n");
280                 exit(EXIT_FAILURE);
281         }
282
283         if (write(ifd, ptr + datafile_offset, size) != size) {
284                 fprintf(stderr, "Write error %s\n",
285                         strerror(errno));
286                 exit(EXIT_FAILURE);
287         }
288
289         tail = size % 4;
290         pad = pad - size;
291         if (pad == 1 && tail != 0) {
292                 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
293                         fprintf(stderr, "Write error on %s\n",
294                                 strerror(errno));
295                         exit(EXIT_FAILURE);
296                 }
297         } else if (pad > 1) {
298                 while (pad > 0) {
299                         int todo = sizeof(zeros);
300
301                         if (todo > pad)
302                                 todo = pad;
303                         if (write(ifd, (char *)&zeros, todo) != todo) {
304                                 fprintf(stderr, "Write error: %s\n",
305                                         strerror(errno));
306                                 exit(EXIT_FAILURE);
307                         }
308                         pad -= todo;
309                 }
310         }
311
312         munmap((void *)ptr, sbuf.st_size);
313 err_mmap:
314         close(dfd);
315 }
316
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)
320 {
321         struct legacy_img_hdr image_header;
322         int ret;
323
324         uint32_t fit_size, load_addr;
325         int align_len = 64 - 1; /* 64 is cacheline size */
326
327         ret = lseek(fd, fit_offset, SEEK_SET);
328         if (ret < 0) {
329                 fprintf(stderr, "lseek fd fail for fit\n");
330                 exit(EXIT_FAILURE);
331         }
332
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",
336                         strerror(errno));
337                 exit(EXIT_FAILURE);
338         }
339
340         if (be32_to_cpu(image_header.ih_magic) != FDT_MAGIC) {
341                 fprintf(stderr, "%s error: not a FIT file\n", __func__);
342                 exit(EXIT_FAILURE);
343         }
344
345         fit_size = fdt_totalsize(&image_header);
346
347         fit_size = ALIGN_IMX(fit_size, ALIGN_SIZE);
348
349         ret = lseek(fd, fit_offset + fit_size, SEEK_SET);
350         if (ret < 0) {
351                 fprintf(stderr, "lseek fd fail for fit\n");
352                 exit(EXIT_FAILURE);
353         }
354
355         /*
356          * ep is the u-boot entry. SPL loads the FIT before the u-boot
357          * address. 0x2000 is for CSF_SIZE
358          */
359         load_addr = (ep - (fit_size + CSF_SIZE) - 512 - align_len) &
360                 ~align_len;
361
362         flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
363                 load_addr, 0, 0, 0,
364                 (load_addr + fit_size),
365                 (load_addr + fit_size + 0x20),
366                 0 };
367
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");
371                 exit(EXIT_FAILURE);
372         }
373
374         *fit_load_addr = load_addr;
375
376         return fit_offset + fit_size;
377 }
378
379 static void dump_header_v2(imx_header_v3_t *imx_header, int index)
380 {
381         const char *ivt_name[2] = {"HDMI FW", "LOADER IMAGE"};
382
383         fprintf(stdout, "========= IVT HEADER [%s] =========\n",
384                 ivt_name[index]);
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);
405
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);
412 }
413
414 #ifdef CONFIG_FSPI_CONF_HEADER
415 static int generate_fspi_header (int ifd)
416 {
417         int ret, i = 0;
418         char *val;
419         char lut_str[] = CONFIG_LUT_SEQUENCE;
420
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,
426         .datahold =  0x03,
427         .datasetup = 0x03,
428         .coladdrwidth = 0x00,
429         .devcfgenable = 0x00,
430         .reserved_2 = {0x00, 0x00, 0x00},
431         .devmodeseq =  {0x00, 0x00, 0x00, 0x00},
432         .devmodearg =  {0x00, 0x00, 0x00, 0x00},
433         .cmd_enable =  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},
458         };
459
460         for (val = strtok(lut_str, ","); val; val = strtok(NULL, ",")) {
461                 fspi_conf_data.lut[i++] = strtoul(val, NULL, 16);
462         }
463
464         ret = lseek(ifd, 0, SEEK_CUR);
465         if (write(ifd, &fspi_conf_data, sizeof(fspi_conf_data)) == -1)
466                 exit(EXIT_FAILURE);
467
468         ret = lseek(ifd, sizeof(fspi_conf_data), SEEK_CUR);
469
470         return ret;
471 }
472 #endif
473
474 void build_image(int ofd)
475 {
476         int file_off, header_hdmi_off = 0, header_image_off;
477
478 #ifdef CONFIG_FSPI_CONF_HEADER
479         int fspi_off, fspi_fd;
480         char *fspi;
481 #endif
482
483         int hdmi_fd, ap_fd, sld_fd;
484         uint32_t sld_load_addr = 0;
485         uint32_t csf_off, sld_csf_off = 0;
486         int ret;
487         struct stat sbuf;
488
489         if (!ap_img) {
490                 fprintf(stderr, "No LOADER image specificed\n");
491                 exit(EXIT_FAILURE);
492         }
493
494         file_off = 0;
495
496         if (signed_hdmi) {
497                 header_hdmi_off = file_off + ivt_offset;
498
499                 hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY);
500                 if (hdmi_fd < 0) {
501                         fprintf(stderr, "%s: Can't open: %s\n",
502                                 signed_hdmi, strerror(errno));
503                         exit(EXIT_FAILURE);
504                 }
505
506                 if (fstat(hdmi_fd, &sbuf) < 0) {
507                         fprintf(stderr, "%s: Can't stat: %s\n",
508                                 signed_hdmi, strerror(errno));
509                         exit(EXIT_FAILURE);
510                 }
511                 close(hdmi_fd);
512
513                 /*
514                  * Aligned to 104KB = 92KB FW image + 0x8000
515                  * (IVT and alignment) + 0x4000 (second IVT + CSF)
516                  */
517                 file_off += ALIGN_IMX(sbuf.st_size,
518                                   HDMI_FW_SIZE + 0x2000 + 0x1000);
519         }
520
521         header_image_off = file_off + ivt_offset;
522
523 #ifdef CONFIG_FSPI_CONF_HEADER
524         fspi = CONFIG_FSPI_CONF_FILE;
525         fspi_fd = open(fspi, O_RDWR | O_CREAT, S_IRWXU);
526         if (fspi_fd < 0) {
527                 fprintf(stderr, "Can't open %s: %s\n",
528                         fspi, strerror(errno));
529                 exit(EXIT_FAILURE);
530         }
531
532         fspi_off = generate_fspi_header(fspi_fd);
533         file_off = header_image_off + fspi_off;
534         close(fspi_fd);
535
536 #endif
537         ap_fd = open(ap_img, O_RDONLY | O_BINARY);
538         if (ap_fd < 0) {
539                 fprintf(stderr, "%s: Can't open: %s\n",
540                         ap_img, strerror(errno));
541                 exit(EXIT_FAILURE);
542         }
543         if (fstat(ap_fd, &sbuf) < 0) {
544                 fprintf(stderr, "%s: Can't stat: %s\n",
545                         ap_img, strerror(errno));
546                 exit(EXIT_FAILURE);
547         }
548         close(ap_fd);
549
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,
565                       sector_size);
566
567         image_off = header_image_off + sizeof(imx_header_v3_t);
568         file_off +=  imx_header[IMAGE_IVT_ID].boot_data.size;
569
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;
574
575         imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */
576
577         csf_off = file_off;
578         file_off += CSF_SIZE;
579
580         /* Second boot loader image */
581         if (sld_img) {
582                 if (!using_fit) {
583                         fprintf(stderr, "Not support no fit\n");
584                         exit(EXIT_FAILURE);
585                 } else {
586                         sld_header_off = sld_src_off - rom_image_offset;
587                         sld_fd = open(sld_img, O_RDONLY | O_BINARY);
588                         if (sld_fd < 0) {
589                                 fprintf(stderr, "%s: Can't open: %s\n",
590                                         sld_img, strerror(errno));
591                                 exit(EXIT_FAILURE);
592                         }
593
594                         if (fstat(sld_fd, &sbuf) < 0) {
595                                 fprintf(stderr, "%s: Can't stat: %s\n",
596                                         sld_img, strerror(errno));
597                                 exit(EXIT_FAILURE);
598                         }
599
600                         close(sld_fd);
601
602                         file_off = sld_header_off;
603                         file_off += sbuf.st_size + sizeof(struct legacy_img_hdr);
604                 }
605         }
606
607         if (signed_hdmi) {
608                 header_hdmi_off -= ivt_offset;
609                 ret = lseek(ofd, header_hdmi_off, SEEK_SET);
610                 if (ret < 0) {
611                         fprintf(stderr, "lseek ofd fail for hdmi\n");
612                         exit(EXIT_FAILURE);
613                 }
614
615                 /* The signed HDMI FW has 0x400 IVT offset, need remove it */
616                 copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400);
617         }
618
619         /* Main Image */
620         header_image_off -= ivt_offset;
621         image_off -= ivt_offset;
622         ret = lseek(ofd, header_image_off, SEEK_SET);
623         if (ret < 0) {
624                 fprintf(stderr, "lseek ofd fail\n");
625                 exit(EXIT_FAILURE);
626         }
627
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");
632                 exit(1);
633         }
634
635         copy_file(ofd, ap_img, 0, image_off, 0);
636
637         csf_off -= ivt_offset;
638         fill_zero(ofd, CSF_SIZE, csf_off);
639
640         if (sld_img) {
641                 sld_header_off -= ivt_offset;
642                 ret = lseek(ofd, sld_header_off, SEEK_SET);
643                 if (ret < 0) {
644                         fprintf(stderr, "lseek ofd fail for sld_img\n");
645                         exit(EXIT_FAILURE);
646                 }
647
648                 /* Write image header */
649                 if (!using_fit) {
650                         /* TODO */
651                 } else {
652                         copy_file(ofd, sld_img, 0, sld_header_off, 0);
653                         sld_csf_off =
654                                 generate_ivt_for_fit(ofd, sld_header_off,
655                                                      sld_start_addr,
656                                                      &sld_load_addr) + 0x20;
657                 }
658         }
659
660         if (!signed_hdmi)
661                 dump_header_v2(imx_header, 0);
662         dump_header_v2(imx_header, 1);
663
664         fprintf(stdout, "========= OFFSET dump =========");
665         if (signed_hdmi) {
666                 fprintf(stdout, "\nSIGNED HDMI FW:\n");
667                 fprintf(stdout, " header_hdmi_off \t0x%x\n",
668                         header_hdmi_off);
669         }
670
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);
677
678         fprintf(stdout, "\nSecond Loader IMAGE:\n");
679         fprintf(stdout, " sld_header_off \t0x%x\n",
680                 sld_header_off);
681         fprintf(stdout, " sld_csf_off \t\t0x%x\n",
682                 sld_csf_off);
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);
685 }
686
687 int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams)
688 {
689         /*
690          * SECO FW is a container image, this is to calculate the
691          * 2nd container offset.
692          */
693         fprintf(stdout, "parsing %s\n", mparams->imagename);
694         parse_cfg_file(mparams->imagename);
695
696         build_image(outfd);
697
698         return 0;
699 }
700
701 /*
702  * imx8mimage parameters
703  */
704 U_BOOT_IMAGE_TYPE(
705         imx8mimage,
706         "NXP i.MX8M Boot Image support",
707         0,
708         NULL,
709         imx8mimage_check_params,
710         NULL,
711         imx8mimage_print_header,
712         imx8mimage_set_header,
713         NULL,
714         imx8mimage_check_image_types,
715         NULL,
716         NULL
717 );