blob: 9985b95a98ab1ba502d0525efa1a22910c08e45f [file] [log] [blame]
Peng Fan31c51da2018-11-20 10:19:36 +00001// 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
14static uint32_t ap_start_addr, sld_start_addr, sld_src_off;
15static char *ap_img, *sld_img, *signed_hdmi;
16static imx_header_v3_t imx_header[2]; /* At most there are 3 IVT headers */
17static uint32_t rom_image_offset;
18static uint32_t sector_size = 0x200;
19static uint32_t image_off;
20static uint32_t sld_header_off;
21static uint32_t ivt_offset;
22static uint32_t using_fit;
23
Peng Fan7be85a72019-09-16 03:09:39 +000024#define ROM_V1 1
25#define ROM_V2 2
26
27static uint32_t rom_version = ROM_V1;
28
Peng Fan31c51da2018-11-20 10:19:36 +000029#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
Fabio Estevam452b0252021-02-22 13:16:43 -030035#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))
Peng Fan31c51da2018-11-20 10:19:36 +000037
38static 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
53int imx8mimage_check_params(struct image_tool_params *params)
54{
55 return 0;
56}
57
58static void imx8mimage_set_header(void *ptr, struct stat *sbuf, int ifd,
59 struct image_tool_params *params)
60{
61}
62
63static void imx8mimage_print_header(const void *ptr)
64{
65}
66
67static int imx8mimage_check_image_types(uint8_t type)
68{
69 return (type == IH_TYPE_IMX8MIMAGE) ? EXIT_SUCCESS : EXIT_FAILURE;
70}
71
72static 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", },
Peng Fan7be85a72019-09-16 03:09:39 +000079 {CMD_ROM_VERSION, "ROM_VERSION", "rom version", },
Peng Fan31c51da2018-11-20 10:19:36 +000080 {-1, "", "", },
81};
82
83static 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
90static 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;
Peng Fan7be85a72019-09-16 03:09:39 +000099
100 if (rom_version == ROM_V2)
101 ivt_offset = 0;
Peng Fan31c51da2018-11-20 10:19:36 +0000102 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;
Peng Fan31c51da2018-11-20 10:19:36 +0000111 break;
112 case CMD_DDR_FW:
113 /* Do nothing */
114 break;
Peng Fan7be85a72019-09-16 03:09:39 +0000115 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
Peng Fan31c51da2018-11-20 10:19:36 +0000124 }
125}
126
127static void parse_cfg_fld(int32_t *cmd, char *token,
128 char *name, int lineno, int fld)
129{
130 switch (fld) {
131 case CFG_COMMAND:
132 *cmd = get_table_entry_id(imx8mimage_cmds,
133 "imx8mimage commands", token);
134 if (*cmd < 0) {
135 fprintf(stderr, "Error: %s[%d] - Invalid command" "(%s)\n",
136 name, lineno, token);
137 exit(EXIT_FAILURE);
138 }
Peng Fan3ae9e1b2019-08-06 10:07:26 +0000139 switch (*cmd) {
140 case CMD_FIT:
141 using_fit = 1;
142 break;
143 }
Peng Fan31c51da2018-11-20 10:19:36 +0000144 break;
145 case CFG_REG_SIZE:
146 parse_cfg_cmd(*cmd, token, name, lineno);
147 break;
148 case CFG_REG_ADDRESS:
149 switch (*cmd) {
150 case CMD_LOADER:
151 ap_start_addr = get_cfg_value(token, name, lineno);
152 break;
153 case CMD_SECOND_LOADER:
154 sld_start_addr = get_cfg_value(token, name, lineno);
155 break;
156 }
157 break;
158 case CFG_REG_VALUE:
159 switch (*cmd) {
160 case CMD_SECOND_LOADER:
161 sld_src_off = get_cfg_value(token, name, lineno);
162 break;
163 }
164 default:
165 break;
166 }
167}
168
169static uint32_t parse_cfg_file(char *name)
170{
171 FILE *fd = NULL;
172 char *line = NULL;
173 char *token, *saveptr1, *saveptr2;
174 int lineno = 0;
175 int fld;
176 size_t len;
177 int32_t cmd;
178
179 fd = fopen(name, "r");
180 if (fd == 0) {
181 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
182 exit(EXIT_FAILURE);
183 }
184
185 /*
186 * Very simple parsing, line starting with # are comments
187 * and are dropped
188 */
189 while ((getline(&line, &len, fd)) > 0) {
190 lineno++;
191
192 token = strtok_r(line, "\r\n", &saveptr1);
193 if (!token)
194 continue;
195
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);
200 if (!token)
201 break;
202
203 /* Drop all text starting with '#' as comments */
204 if (token[0] == '#')
205 break;
206
207 parse_cfg_fld(&cmd, token, name, lineno, fld);
208 }
209 }
210
211 return 0;
212}
213
214static void fill_zero(int ifd, int size, int offset)
215{
216 int fill_size;
217 uint8_t zeros[4096];
218 int ret;
219
220 memset(zeros, 0, sizeof(zeros));
221
222 ret = lseek(ifd, offset, SEEK_SET);
223 if (ret < 0) {
224 fprintf(stderr, "%s seek: %s\n", __func__, strerror(errno));
225 exit(EXIT_FAILURE);
226 }
227
228 while (size) {
229 if (size > 4096)
230 fill_size = 4096;
231 else
232 fill_size = size;
233
234 if (write(ifd, (char *)&zeros, fill_size) != fill_size) {
235 fprintf(stderr, "Write error: %s\n",
236 strerror(errno));
237 exit(EXIT_FAILURE);
238 }
239
240 size -= fill_size;
241 };
242}
243
244static void copy_file(int ifd, const char *datafile, int pad, int offset,
245 int datafile_offset)
246{
247 int dfd;
248 struct stat sbuf;
249 unsigned char *ptr;
250 int tail;
251 int zero = 0;
252 uint8_t zeros[4096];
253 int size, ret;
254
255 memset(zeros, 0, sizeof(zeros));
256
257 dfd = open(datafile, O_RDONLY | O_BINARY);
258 if (dfd < 0) {
259 fprintf(stderr, "Can't open %s: %s\n",
260 datafile, strerror(errno));
261 exit(EXIT_FAILURE);
262 }
263
264 if (fstat(dfd, &sbuf) < 0) {
265 fprintf(stderr, "Can't stat %s: %s\n",
266 datafile, strerror(errno));
267 exit(EXIT_FAILURE);
268 }
269
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));
274 exit(EXIT_FAILURE);
275 }
276
277 size = sbuf.st_size - datafile_offset;
278 ret = lseek(ifd, offset, SEEK_SET);
279 if (ret < 0) {
280 fprintf(stderr, "lseek ifd fail\n");
281 exit(EXIT_FAILURE);
282 }
283
284 if (write(ifd, ptr + datafile_offset, size) != size) {
285 fprintf(stderr, "Write error %s\n",
286 strerror(errno));
287 exit(EXIT_FAILURE);
288 }
289
290 tail = size % 4;
291 pad = pad - size;
292 if (pad == 1 && tail != 0) {
293 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
294 fprintf(stderr, "Write error on %s\n",
295 strerror(errno));
296 exit(EXIT_FAILURE);
297 }
298 } else if (pad > 1) {
299 while (pad > 0) {
300 int todo = sizeof(zeros);
301
302 if (todo > pad)
303 todo = pad;
304 if (write(ifd, (char *)&zeros, todo) != todo) {
305 fprintf(stderr, "Write error: %s\n",
306 strerror(errno));
307 exit(EXIT_FAILURE);
308 }
309 pad -= todo;
310 }
311 }
312
313 munmap((void *)ptr, sbuf.st_size);
314 close(dfd);
315}
316
317/* Return this IVT offset in the final output file */
318static int generate_ivt_for_fit(int fd, int fit_offset, uint32_t ep,
319 uint32_t *fit_load_addr)
320{
321 image_header_t 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(image_header_t)) !=
334 sizeof(image_header_t)) {
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);
Peng Fan31c51da2018-11-20 10:19:36 +0000346
Fabio Estevam452b0252021-02-22 13:16:43 -0300347 fit_size = ALIGN_IMX(fit_size, ALIGN_SIZE);
Peng Fan31c51da2018-11-20 10:19:36 +0000348
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
379static 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
414void build_image(int ofd)
415{
416 int file_off, header_hdmi_off = 0, header_image_off;
417 int hdmi_fd, ap_fd, sld_fd;
418 uint32_t sld_load_addr = 0;
419 uint32_t csf_off, sld_csf_off = 0;
420 int ret;
421 struct stat sbuf;
422
423 if (!ap_img) {
424 fprintf(stderr, "No LOADER image specificed\n");
425 exit(EXIT_FAILURE);
426 }
427
428 file_off = 0;
429
430 if (signed_hdmi) {
431 header_hdmi_off = file_off + ivt_offset;
432
433 hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY);
434 if (hdmi_fd < 0) {
435 fprintf(stderr, "%s: Can't open: %s\n",
436 signed_hdmi, strerror(errno));
437 exit(EXIT_FAILURE);
438 }
439
440 if (fstat(hdmi_fd, &sbuf) < 0) {
441 fprintf(stderr, "%s: Can't stat: %s\n",
442 signed_hdmi, strerror(errno));
443 exit(EXIT_FAILURE);
444 }
445 close(hdmi_fd);
446
447 /*
448 * Aligned to 104KB = 92KB FW image + 0x8000
449 * (IVT and alignment) + 0x4000 (second IVT + CSF)
450 */
Fabio Estevam452b0252021-02-22 13:16:43 -0300451 file_off += ALIGN_IMX(sbuf.st_size,
Peng Fan31c51da2018-11-20 10:19:36 +0000452 HDMI_FW_SIZE + 0x2000 + 0x1000);
453 }
454
455 header_image_off = file_off + ivt_offset;
456
457 ap_fd = open(ap_img, O_RDONLY | O_BINARY);
458 if (ap_fd < 0) {
459 fprintf(stderr, "%s: Can't open: %s\n",
460 ap_img, strerror(errno));
461 exit(EXIT_FAILURE);
462 }
463 if (fstat(ap_fd, &sbuf) < 0) {
464 fprintf(stderr, "%s: Can't stat: %s\n",
465 ap_img, strerror(errno));
466 exit(EXIT_FAILURE);
467 }
468 close(ap_fd);
469
470 imx_header[IMAGE_IVT_ID].fhdr.header.tag = IVT_HEADER_TAG; /* 0xD1 */
471 imx_header[IMAGE_IVT_ID].fhdr.header.length =
472 cpu_to_be16(sizeof(flash_header_v2_t));
473 imx_header[IMAGE_IVT_ID].fhdr.header.version = IVT_VERSION_V3; /* 0x41 */
474 imx_header[IMAGE_IVT_ID].fhdr.entry = ap_start_addr;
475 imx_header[IMAGE_IVT_ID].fhdr.self = ap_start_addr -
476 sizeof(imx_header_v3_t);
477 imx_header[IMAGE_IVT_ID].fhdr.dcd_ptr = 0;
478 imx_header[IMAGE_IVT_ID].fhdr.boot_data_ptr =
479 imx_header[IMAGE_IVT_ID].fhdr.self +
480 offsetof(imx_header_v3_t, boot_data);
481 imx_header[IMAGE_IVT_ID].boot_data.start =
482 imx_header[IMAGE_IVT_ID].fhdr.self - ivt_offset;
483 imx_header[IMAGE_IVT_ID].boot_data.size =
Fabio Estevam452b0252021-02-22 13:16:43 -0300484 ALIGN_IMX(sbuf.st_size + sizeof(imx_header_v3_t) + ivt_offset,
Peng Fan31c51da2018-11-20 10:19:36 +0000485 sector_size);
486
487 image_off = header_image_off + sizeof(imx_header_v3_t);
488 file_off += imx_header[IMAGE_IVT_ID].boot_data.size;
489
490 imx_header[IMAGE_IVT_ID].boot_data.plugin = 0;
491 imx_header[IMAGE_IVT_ID].fhdr.csf =
492 imx_header[IMAGE_IVT_ID].boot_data.start +
493 imx_header[IMAGE_IVT_ID].boot_data.size;
494
495 imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */
496
497 csf_off = file_off;
498 file_off += CSF_SIZE;
499
500 /* Second boot loader image */
501 if (sld_img) {
502 if (!using_fit) {
503 fprintf(stderr, "Not support no fit\n");
504 exit(EXIT_FAILURE);
505 } else {
506 sld_header_off = sld_src_off - rom_image_offset;
507 /*
508 * Record the second bootloader relative offset in
509 * image's IVT reserved1
510 */
Peng Fan7be85a72019-09-16 03:09:39 +0000511 if (rom_version == ROM_V1) {
512 imx_header[IMAGE_IVT_ID].fhdr.reserved1 =
513 sld_header_off - header_image_off;
514 }
Peng Fan31c51da2018-11-20 10:19:36 +0000515 sld_fd = open(sld_img, O_RDONLY | O_BINARY);
516 if (sld_fd < 0) {
517 fprintf(stderr, "%s: Can't open: %s\n",
518 sld_img, strerror(errno));
519 exit(EXIT_FAILURE);
520 }
521
522 if (fstat(sld_fd, &sbuf) < 0) {
523 fprintf(stderr, "%s: Can't stat: %s\n",
524 sld_img, strerror(errno));
525 exit(EXIT_FAILURE);
526 }
527
528 close(sld_fd);
529
530 file_off = sld_header_off;
531 file_off += sbuf.st_size + sizeof(image_header_t);
532 }
533 }
534
535 if (signed_hdmi) {
536 header_hdmi_off -= ivt_offset;
537 ret = lseek(ofd, header_hdmi_off, SEEK_SET);
538 if (ret < 0) {
539 fprintf(stderr, "lseek ofd fail for hdmi\n");
540 exit(EXIT_FAILURE);
541 }
542
543 /* The signed HDMI FW has 0x400 IVT offset, need remove it */
544 copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400);
545 }
546
547 /* Main Image */
548 header_image_off -= ivt_offset;
549 image_off -= ivt_offset;
550 ret = lseek(ofd, header_image_off, SEEK_SET);
551 if (ret < 0) {
552 fprintf(stderr, "lseek ofd fail\n");
553 exit(EXIT_FAILURE);
554 }
555
556 /* Write image header */
557 if (write(ofd, &imx_header[IMAGE_IVT_ID], sizeof(imx_header_v3_t)) !=
558 sizeof(imx_header_v3_t)) {
559 fprintf(stderr, "error writing image hdr\n");
560 exit(1);
561 }
562
563 copy_file(ofd, ap_img, 0, image_off, 0);
564
565 csf_off -= ivt_offset;
566 fill_zero(ofd, CSF_SIZE, csf_off);
567
568 if (sld_img) {
569 sld_header_off -= ivt_offset;
570 ret = lseek(ofd, sld_header_off, SEEK_SET);
571 if (ret < 0) {
572 fprintf(stderr, "lseek ofd fail for sld_img\n");
573 exit(EXIT_FAILURE);
574 }
575
576 /* Write image header */
577 if (!using_fit) {
578 /* TODO */
579 } else {
580 copy_file(ofd, sld_img, 0, sld_header_off, 0);
581 sld_csf_off =
582 generate_ivt_for_fit(ofd, sld_header_off,
583 sld_start_addr,
584 &sld_load_addr) + 0x20;
585 }
586 }
587
588 if (!signed_hdmi)
589 dump_header_v2(imx_header, 0);
590 dump_header_v2(imx_header, 1);
591
592 fprintf(stdout, "========= OFFSET dump =========");
593 if (signed_hdmi) {
594 fprintf(stdout, "\nSIGNED HDMI FW:\n");
595 fprintf(stdout, " header_hdmi_off \t0x%x\n",
596 header_hdmi_off);
597 }
598
599 fprintf(stdout, "\nLoader IMAGE:\n");
600 fprintf(stdout, " header_image_off \t0x%x\n image_off \t\t0x%x\n csf_off \t\t0x%x\n",
601 header_image_off, image_off, csf_off);
602 fprintf(stdout, " spl hab block: \t0x%x 0x%x 0x%x\n",
603 imx_header[IMAGE_IVT_ID].fhdr.self, header_image_off,
604 csf_off - header_image_off);
605
606 fprintf(stdout, "\nSecond Loader IMAGE:\n");
607 fprintf(stdout, " sld_header_off \t0x%x\n",
608 sld_header_off);
609 fprintf(stdout, " sld_csf_off \t\t0x%x\n",
610 sld_csf_off);
611 fprintf(stdout, " sld hab block: \t0x%x 0x%x 0x%x\n",
612 sld_load_addr, sld_header_off, sld_csf_off - sld_header_off);
613}
614
615int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams)
616{
617 /*
618 * SECO FW is a container image, this is to calculate the
619 * 2nd container offset.
620 */
621 fprintf(stdout, "parsing %s\n", mparams->imagename);
622 parse_cfg_file(mparams->imagename);
623
624 build_image(outfd);
625
626 return 0;
627}
628
629/*
630 * imx8mimage parameters
631 */
632U_BOOT_IMAGE_TYPE(
633 imx8mimage,
634 "NXP i.MX8M Boot Image support",
635 0,
636 NULL,
637 imx8mimage_check_params,
638 NULL,
639 imx8mimage_print_header,
640 imx8mimage_set_header,
641 NULL,
642 imx8mimage_check_image_types,
643 NULL,
644 NULL
645);