tools: imx8mimage: fix HDMI/FIT parsing
[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 CSF_SIZE 0x2000
25 #define HDMI_IVT_ID 0
26 #define IMAGE_IVT_ID 1
27
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))
32
33 static uint32_t get_cfg_value(char *token, char *name,  int linenr)
34 {
35         char *endptr;
36         uint32_t value;
37
38         errno = 0;
39         value = strtoul(token, &endptr, 16);
40         if (errno || token == endptr) {
41                 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
42                         name,  linenr, token);
43                 exit(EXIT_FAILURE);
44         }
45         return value;
46 }
47
48 int imx8mimage_check_params(struct image_tool_params *params)
49 {
50         return 0;
51 }
52
53 static void imx8mimage_set_header(void *ptr, struct stat *sbuf, int ifd,
54                                   struct image_tool_params *params)
55 {
56 }
57
58 static void imx8mimage_print_header(const void *ptr)
59 {
60 }
61
62 static int imx8mimage_check_image_types(uint8_t type)
63 {
64         return (type == IH_TYPE_IMX8MIMAGE) ? EXIT_SUCCESS : EXIT_FAILURE;
65 }
66
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",       },
74         {-1,                    "",                     "",                   },
75 };
76
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",      },
82 };
83
84 static void parse_cfg_cmd(int32_t cmd, char *token, char *name, int lineno)
85 {
86         switch (cmd) {
87         case CMD_BOOT_FROM:
88                 ivt_offset = get_table_entry_id(imx8mimage_ivt_offset,
89                                                 "imx8mimage ivt offset",
90                                                 token);
91                 if (!strncmp(token, "sd", 2))
92                         rom_image_offset = 0x8000;
93                 break;
94         case CMD_LOADER:
95                 ap_img = token;
96                 break;
97         case CMD_SECOND_LOADER:
98                 sld_img = token;
99                 break;
100         case CMD_SIGNED_HDMI:
101                 signed_hdmi = token;
102                 break;
103         case CMD_DDR_FW:
104                 /* Do nothing */
105                 break;
106         }
107 }
108
109 static void parse_cfg_fld(int32_t *cmd, char *token,
110                           char *name, int lineno, int fld)
111 {
112         switch (fld) {
113         case CFG_COMMAND:
114                 *cmd = get_table_entry_id(imx8mimage_cmds,
115                                           "imx8mimage commands", token);
116                 if (*cmd < 0) {
117                         fprintf(stderr, "Error: %s[%d] - Invalid command" "(%s)\n",
118                                 name, lineno, token);
119                         exit(EXIT_FAILURE);
120                 }
121                 switch (*cmd) {
122                 case CMD_FIT:
123                         using_fit = 1;
124                         break;
125                 }
126                 break;
127         case CFG_REG_SIZE:
128                 parse_cfg_cmd(*cmd, token, name, lineno);
129                 break;
130         case CFG_REG_ADDRESS:
131                 switch (*cmd) {
132                 case CMD_LOADER:
133                         ap_start_addr = get_cfg_value(token, name, lineno);
134                         break;
135                 case CMD_SECOND_LOADER:
136                         sld_start_addr = get_cfg_value(token, name, lineno);
137                         break;
138                 }
139                 break;
140         case CFG_REG_VALUE:
141                 switch (*cmd) {
142                 case CMD_SECOND_LOADER:
143                         sld_src_off = get_cfg_value(token, name, lineno);
144                         break;
145                 }
146         default:
147                 break;
148         }
149 }
150
151 static uint32_t parse_cfg_file(char *name)
152 {
153         FILE *fd = NULL;
154         char *line = NULL;
155         char *token, *saveptr1, *saveptr2;
156         int lineno = 0;
157         int fld;
158         size_t len;
159         int32_t cmd;
160
161         fd = fopen(name, "r");
162         if (fd == 0) {
163                 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
164                 exit(EXIT_FAILURE);
165         }
166
167         /*
168          * Very simple parsing, line starting with # are comments
169          * and are dropped
170          */
171         while ((getline(&line, &len, fd)) > 0) {
172                 lineno++;
173
174                 token = strtok_r(line, "\r\n", &saveptr1);
175                 if (!token)
176                         continue;
177
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);
182                         if (!token)
183                                 break;
184
185                         /* Drop all text starting with '#' as comments */
186                         if (token[0] == '#')
187                                 break;
188
189                         parse_cfg_fld(&cmd, token, name, lineno, fld);
190                 }
191         }
192
193         return 0;
194 }
195
196 static void fill_zero(int ifd, int size, int offset)
197 {
198         int fill_size;
199         uint8_t zeros[4096];
200         int ret;
201
202         memset(zeros, 0, sizeof(zeros));
203
204         ret = lseek(ifd, offset, SEEK_SET);
205         if (ret < 0) {
206                 fprintf(stderr, "%s seek: %s\n", __func__, strerror(errno));
207                 exit(EXIT_FAILURE);
208         }
209
210         while (size) {
211                 if (size > 4096)
212                         fill_size = 4096;
213                 else
214                         fill_size = size;
215
216                 if (write(ifd, (char *)&zeros, fill_size) != fill_size) {
217                         fprintf(stderr, "Write error: %s\n",
218                                 strerror(errno));
219                         exit(EXIT_FAILURE);
220                 }
221
222                 size -= fill_size;
223         };
224 }
225
226 static void copy_file(int ifd, const char *datafile, int pad, int offset,
227                       int datafile_offset)
228 {
229         int dfd;
230         struct stat sbuf;
231         unsigned char *ptr;
232         int tail;
233         int zero = 0;
234         uint8_t zeros[4096];
235         int size, ret;
236
237         memset(zeros, 0, sizeof(zeros));
238
239         dfd = open(datafile, O_RDONLY | O_BINARY);
240         if (dfd < 0) {
241                 fprintf(stderr, "Can't open %s: %s\n",
242                         datafile, strerror(errno));
243                 exit(EXIT_FAILURE);
244         }
245
246         if (fstat(dfd, &sbuf) < 0) {
247                 fprintf(stderr, "Can't stat %s: %s\n",
248                         datafile, strerror(errno));
249                 exit(EXIT_FAILURE);
250         }
251
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));
256                 exit(EXIT_FAILURE);
257         }
258
259         size = sbuf.st_size - datafile_offset;
260         ret = lseek(ifd, offset, SEEK_SET);
261         if (ret < 0) {
262                 fprintf(stderr, "lseek ifd fail\n");
263                 exit(EXIT_FAILURE);
264         }
265
266         if (write(ifd, ptr + datafile_offset, size) != size) {
267                 fprintf(stderr, "Write error %s\n",
268                         strerror(errno));
269                 exit(EXIT_FAILURE);
270         }
271
272         tail = size % 4;
273         pad = pad - size;
274         if (pad == 1 && tail != 0) {
275                 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
276                         fprintf(stderr, "Write error on %s\n",
277                                 strerror(errno));
278                         exit(EXIT_FAILURE);
279                 }
280         } else if (pad > 1) {
281                 while (pad > 0) {
282                         int todo = sizeof(zeros);
283
284                         if (todo > pad)
285                                 todo = pad;
286                         if (write(ifd, (char *)&zeros, todo) != todo) {
287                                 fprintf(stderr, "Write error: %s\n",
288                                         strerror(errno));
289                                 exit(EXIT_FAILURE);
290                         }
291                         pad -= todo;
292                 }
293         }
294
295         munmap((void *)ptr, sbuf.st_size);
296         close(dfd);
297 }
298
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)
302 {
303         image_header_t image_header;
304         int ret;
305
306         uint32_t fit_size, load_addr;
307         int align_len = 64 - 1; /* 64 is cacheline size */
308
309         ret = lseek(fd, fit_offset, SEEK_SET);
310         if (ret < 0) {
311                 fprintf(stderr, "lseek fd fail for fit\n");
312                 exit(EXIT_FAILURE);
313         }
314
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",
318                         strerror(errno));
319                 exit(EXIT_FAILURE);
320         }
321
322         if (be32_to_cpu(image_header.ih_magic) != FDT_MAGIC) {
323                 fprintf(stderr, "%s error: not a FIT file\n", __func__);
324                 exit(EXIT_FAILURE);
325         }
326
327         fit_size = fdt_totalsize(&image_header);
328         fit_size = (fit_size + 3) & ~3;
329
330         fit_size = ALIGN(fit_size, ALIGN_SIZE);
331
332         ret = lseek(fd, fit_offset + fit_size, SEEK_SET);
333         if (ret < 0) {
334                 fprintf(stderr, "lseek fd fail for fit\n");
335                 exit(EXIT_FAILURE);
336         }
337
338         /*
339          * ep is the u-boot entry. SPL loads the FIT before the u-boot
340          * address. 0x2000 is for CSF_SIZE
341          */
342         load_addr = (ep - (fit_size + CSF_SIZE) - 512 - align_len) &
343                 ~align_len;
344
345         flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
346                 load_addr, 0, 0, 0,
347                 (load_addr + fit_size),
348                 (load_addr + fit_size + 0x20),
349                 0 };
350
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");
354                 exit(EXIT_FAILURE);
355         }
356
357         *fit_load_addr = load_addr;
358
359         return fit_offset + fit_size;
360 }
361
362 static void dump_header_v2(imx_header_v3_t *imx_header, int index)
363 {
364         const char *ivt_name[2] = {"HDMI FW", "LOADER IMAGE"};
365
366         fprintf(stdout, "========= IVT HEADER [%s] =========\n",
367                 ivt_name[index]);
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);
388
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);
395 }
396
397 void build_image(int ofd)
398 {
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;
403         int ret;
404         struct stat sbuf;
405
406         if (!ap_img) {
407                 fprintf(stderr, "No LOADER image specificed\n");
408                 exit(EXIT_FAILURE);
409         }
410
411         file_off = 0;
412
413         if (signed_hdmi) {
414                 header_hdmi_off = file_off + ivt_offset;
415
416                 hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY);
417                 if (hdmi_fd < 0) {
418                         fprintf(stderr, "%s: Can't open: %s\n",
419                                 signed_hdmi, strerror(errno));
420                         exit(EXIT_FAILURE);
421                 }
422
423                 if (fstat(hdmi_fd, &sbuf) < 0) {
424                         fprintf(stderr, "%s: Can't stat: %s\n",
425                                 signed_hdmi, strerror(errno));
426                         exit(EXIT_FAILURE);
427                 }
428                 close(hdmi_fd);
429
430                 /*
431                  * Aligned to 104KB = 92KB FW image + 0x8000
432                  * (IVT and alignment) + 0x4000 (second IVT + CSF)
433                  */
434                 file_off += ALIGN(sbuf.st_size,
435                                   HDMI_FW_SIZE + 0x2000 + 0x1000);
436         }
437
438         header_image_off = file_off + ivt_offset;
439
440         ap_fd = open(ap_img, O_RDONLY | O_BINARY);
441         if (ap_fd < 0) {
442                 fprintf(stderr, "%s: Can't open: %s\n",
443                         ap_img, strerror(errno));
444                 exit(EXIT_FAILURE);
445         }
446         if (fstat(ap_fd, &sbuf) < 0) {
447                 fprintf(stderr, "%s: Can't stat: %s\n",
448                         ap_img, strerror(errno));
449                 exit(EXIT_FAILURE);
450         }
451         close(ap_fd);
452
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,
468                       sector_size);
469
470         image_off = header_image_off + sizeof(imx_header_v3_t);
471         file_off +=  imx_header[IMAGE_IVT_ID].boot_data.size;
472
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;
477
478         imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */
479
480         csf_off = file_off;
481         file_off += CSF_SIZE;
482
483         /* Second boot loader image */
484         if (sld_img) {
485                 if (!using_fit) {
486                         fprintf(stderr, "Not support no fit\n");
487                         exit(EXIT_FAILURE);
488                 } else {
489                         sld_header_off = sld_src_off - rom_image_offset;
490                         /*
491                          * Record the second bootloader relative offset in
492                          * image's IVT reserved1
493                          */
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);
497                         if (sld_fd < 0) {
498                                 fprintf(stderr, "%s: Can't open: %s\n",
499                                         sld_img, strerror(errno));
500                                 exit(EXIT_FAILURE);
501                         }
502
503                         if (fstat(sld_fd, &sbuf) < 0) {
504                                 fprintf(stderr, "%s: Can't stat: %s\n",
505                                         sld_img, strerror(errno));
506                                 exit(EXIT_FAILURE);
507                         }
508
509                         close(sld_fd);
510
511                         file_off = sld_header_off;
512                         file_off += sbuf.st_size + sizeof(image_header_t);
513                 }
514         }
515
516         if (signed_hdmi) {
517                 header_hdmi_off -= ivt_offset;
518                 ret = lseek(ofd, header_hdmi_off, SEEK_SET);
519                 if (ret < 0) {
520                         fprintf(stderr, "lseek ofd fail for hdmi\n");
521                         exit(EXIT_FAILURE);
522                 }
523
524                 /* The signed HDMI FW has 0x400 IVT offset, need remove it */
525                 copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400);
526         }
527
528         /* Main Image */
529         header_image_off -= ivt_offset;
530         image_off -= ivt_offset;
531         ret = lseek(ofd, header_image_off, SEEK_SET);
532         if (ret < 0) {
533                 fprintf(stderr, "lseek ofd fail\n");
534                 exit(EXIT_FAILURE);
535         }
536
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");
541                 exit(1);
542         }
543
544         copy_file(ofd, ap_img, 0, image_off, 0);
545
546         csf_off -= ivt_offset;
547         fill_zero(ofd, CSF_SIZE, csf_off);
548
549         if (sld_img) {
550                 sld_header_off -= ivt_offset;
551                 ret = lseek(ofd, sld_header_off, SEEK_SET);
552                 if (ret < 0) {
553                         fprintf(stderr, "lseek ofd fail for sld_img\n");
554                         exit(EXIT_FAILURE);
555                 }
556
557                 /* Write image header */
558                 if (!using_fit) {
559                         /* TODO */
560                 } else {
561                         copy_file(ofd, sld_img, 0, sld_header_off, 0);
562                         sld_csf_off =
563                                 generate_ivt_for_fit(ofd, sld_header_off,
564                                                      sld_start_addr,
565                                                      &sld_load_addr) + 0x20;
566                 }
567         }
568
569         if (!signed_hdmi)
570                 dump_header_v2(imx_header, 0);
571         dump_header_v2(imx_header, 1);
572
573         fprintf(stdout, "========= OFFSET dump =========");
574         if (signed_hdmi) {
575                 fprintf(stdout, "\nSIGNED HDMI FW:\n");
576                 fprintf(stdout, " header_hdmi_off \t0x%x\n",
577                         header_hdmi_off);
578         }
579
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);
586
587         fprintf(stdout, "\nSecond Loader IMAGE:\n");
588         fprintf(stdout, " sld_header_off \t0x%x\n",
589                 sld_header_off);
590         fprintf(stdout, " sld_csf_off \t\t0x%x\n",
591                 sld_csf_off);
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);
594 }
595
596 int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams)
597 {
598         /*
599          * SECO FW is a container image, this is to calculate the
600          * 2nd container offset.
601          */
602         fprintf(stdout, "parsing %s\n", mparams->imagename);
603         parse_cfg_file(mparams->imagename);
604
605         build_image(outfd);
606
607         return 0;
608 }
609
610 /*
611  * imx8mimage parameters
612  */
613 U_BOOT_IMAGE_TYPE(
614         imx8mimage,
615         "NXP i.MX8M Boot Image support",
616         0,
617         NULL,
618         imx8mimage_check_params,
619         NULL,
620         imx8mimage_print_header,
621         imx8mimage_set_header,
622         NULL,
623         imx8mimage_check_image_types,
624         NULL,
625         NULL
626 );