blob: d60d293e649beba2d528b04de5d303da84fadaf8 [file] [log] [blame]
Peng Fan6609c262018-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
Peng Fan6609c262018-11-20 10:19:36 +00008#include "imagetool.h"
9#include <image.h>
10#include "imximage.h"
11#include "compiler.h"
12
13static uint32_t ap_start_addr, sld_start_addr, sld_src_off;
14static char *ap_img, *sld_img, *signed_hdmi;
15static imx_header_v3_t imx_header[2]; /* At most there are 3 IVT headers */
16static uint32_t rom_image_offset;
17static uint32_t sector_size = 0x200;
18static uint32_t image_off;
19static uint32_t sld_header_off;
20static uint32_t ivt_offset;
21static uint32_t using_fit;
22
Peng Fanb8f16832019-09-16 03:09:39 +000023#define ROM_V1 1
24#define ROM_V2 2
25
26static uint32_t rom_version = ROM_V1;
27
Peng Fan6609c262018-11-20 10:19:36 +000028#define CSF_SIZE 0x2000
29#define HDMI_IVT_ID 0
30#define IMAGE_IVT_ID 1
31
32#define HDMI_FW_SIZE 0x17000 /* Use Last 0x1000 for IVT and CSF */
33#define ALIGN_SIZE 0x1000
Fabio Estevam325bb402021-02-22 13:16:43 -030034#define ALIGN_IMX(x, a) __ALIGN_MASK_IMX((x), (__typeof__(x))(a) - 1, a)
35#define __ALIGN_MASK_IMX(x, mask, mask2) (((x) + (mask)) / (mask2) * (mask2))
Peng Fan6609c262018-11-20 10:19:36 +000036
37static uint32_t get_cfg_value(char *token, char *name, int linenr)
38{
39 char *endptr;
40 uint32_t value;
41
42 errno = 0;
43 value = strtoul(token, &endptr, 16);
44 if (errno || token == endptr) {
45 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
46 name, linenr, token);
47 exit(EXIT_FAILURE);
48 }
49 return value;
50}
51
52int imx8mimage_check_params(struct image_tool_params *params)
53{
54 return 0;
55}
56
57static void imx8mimage_set_header(void *ptr, struct stat *sbuf, int ifd,
58 struct image_tool_params *params)
59{
60}
61
Pali Rohár2972d7d2023-03-29 21:25:54 +020062static void imx8mimage_print_header(const void *ptr, struct image_tool_params *params)
Peng Fan6609c262018-11-20 10:19:36 +000063{
64}
65
66static int imx8mimage_check_image_types(uint8_t type)
67{
68 return (type == IH_TYPE_IMX8MIMAGE) ? EXIT_SUCCESS : EXIT_FAILURE;
69}
70
71static table_entry_t imx8mimage_cmds[] = {
72 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
73 {CMD_FIT, "FIT", "fit image", },
74 {CMD_SIGNED_HDMI, "SIGNED_HDMI", "signed hdmi image", },
75 {CMD_LOADER, "LOADER", "loader image", },
76 {CMD_SECOND_LOADER, "SECOND_LOADER", "2nd loader image", },
77 {CMD_DDR_FW, "DDR_FW", "ddr firmware", },
Peng Fanb8f16832019-09-16 03:09:39 +000078 {CMD_ROM_VERSION, "ROM_VERSION", "rom version", },
Peng Fan6609c262018-11-20 10:19:36 +000079 {-1, "", "", },
80};
81
82static table_entry_t imx8mimage_ivt_offset[] = {
83 {0x400, "sd", "sd/emmc",},
84 {0x400, "emmc_fastboot", "emmc fastboot",},
85 {0x1000, "fspi", "flexspi", },
86 {-1, "", "Invalid", },
87};
88
89static void parse_cfg_cmd(int32_t cmd, char *token, char *name, int lineno)
90{
91 switch (cmd) {
92 case CMD_BOOT_FROM:
93 ivt_offset = get_table_entry_id(imx8mimage_ivt_offset,
94 "imx8mimage ivt offset",
95 token);
96 if (!strncmp(token, "sd", 2))
97 rom_image_offset = 0x8000;
Peng Fanb8f16832019-09-16 03:09:39 +000098
99 if (rom_version == ROM_V2)
100 ivt_offset = 0;
Peng Fan6609c262018-11-20 10:19:36 +0000101 break;
102 case CMD_LOADER:
103 ap_img = token;
104 break;
105 case CMD_SECOND_LOADER:
106 sld_img = token;
107 break;
108 case CMD_SIGNED_HDMI:
109 signed_hdmi = token;
Peng Fan6609c262018-11-20 10:19:36 +0000110 break;
111 case CMD_DDR_FW:
112 /* Do nothing */
113 break;
Peng Fanb8f16832019-09-16 03:09:39 +0000114 case CMD_ROM_VERSION:
115 if (!strncmp(token, "v2", 2)) {
116 rom_version = ROM_V2;
117 ivt_offset = 0;
118 } else if (!strncmp(token, "v1", 2)) {
119 rom_version = ROM_V1;
120 }
121 break;
Peng Fan6609c262018-11-20 10:19:36 +0000122 }
123}
124
125static void parse_cfg_fld(int32_t *cmd, char *token,
126 char *name, int lineno, int fld)
127{
128 switch (fld) {
129 case CFG_COMMAND:
130 *cmd = get_table_entry_id(imx8mimage_cmds,
131 "imx8mimage commands", token);
132 if (*cmd < 0) {
133 fprintf(stderr, "Error: %s[%d] - Invalid command" "(%s)\n",
134 name, lineno, token);
135 exit(EXIT_FAILURE);
136 }
Peng Fan4d655bf2019-08-06 10:07:26 +0000137 switch (*cmd) {
138 case CMD_FIT:
139 using_fit = 1;
140 break;
141 }
Peng Fan6609c262018-11-20 10:19:36 +0000142 break;
143 case CFG_REG_SIZE:
144 parse_cfg_cmd(*cmd, token, name, lineno);
145 break;
146 case CFG_REG_ADDRESS:
147 switch (*cmd) {
148 case CMD_LOADER:
149 ap_start_addr = get_cfg_value(token, name, lineno);
150 break;
151 case CMD_SECOND_LOADER:
152 sld_start_addr = get_cfg_value(token, name, lineno);
153 break;
154 }
155 break;
156 case CFG_REG_VALUE:
157 switch (*cmd) {
158 case CMD_SECOND_LOADER:
159 sld_src_off = get_cfg_value(token, name, lineno);
160 break;
161 }
162 default:
163 break;
164 }
165}
166
167static uint32_t parse_cfg_file(char *name)
168{
169 FILE *fd = NULL;
170 char *line = NULL;
171 char *token, *saveptr1, *saveptr2;
172 int lineno = 0;
173 int fld;
174 size_t len;
175 int32_t cmd;
176
177 fd = fopen(name, "r");
178 if (fd == 0) {
179 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
180 exit(EXIT_FAILURE);
181 }
182
183 /*
184 * Very simple parsing, line starting with # are comments
185 * and are dropped
186 */
187 while ((getline(&line, &len, fd)) > 0) {
188 lineno++;
189
190 token = strtok_r(line, "\r\n", &saveptr1);
191 if (!token)
192 continue;
193
194 /* Check inside the single line */
195 for (fld = CFG_COMMAND, cmd = CFG_INVALID,
196 line = token; ; line = NULL, fld++) {
197 token = strtok_r(line, " \t", &saveptr2);
198 if (!token)
199 break;
200
201 /* Drop all text starting with '#' as comments */
202 if (token[0] == '#')
203 break;
204
205 parse_cfg_fld(&cmd, token, name, lineno, fld);
206 }
207 }
208
Mikhail Ilin90177852022-11-23 13:48:44 +0300209 fclose(fd);
Peng Fan6609c262018-11-20 10:19:36 +0000210 return 0;
211}
212
213static 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
243static 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;
Peng Fan16841a62021-03-19 15:56:53 +0800250 uint64_t zero = 0;
Peng Fan6609c262018-11-20 10:19:36 +0000251 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));
Peng Fanc7b87112021-08-16 11:48:41 +0800273 goto err_mmap;
Peng Fan6609c262018-11-20 10:19:36 +0000274 }
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);
Peng Fanc7b87112021-08-16 11:48:41 +0800313err_mmap:
Peng Fan6609c262018-11-20 10:19:36 +0000314 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{
Simon Glassf3543e62022-09-06 20:26:52 -0600321 struct legacy_img_hdr image_header;
Peng Fan6609c262018-11-20 10:19:36 +0000322 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
Simon Glassf3543e62022-09-06 20:26:52 -0600333 if (read(fd, (char *)&image_header, sizeof(struct legacy_img_hdr)) !=
334 sizeof(struct legacy_img_hdr)) {
Peng Fan6609c262018-11-20 10:19:36 +0000335 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 Fan6609c262018-11-20 10:19:36 +0000346
Fabio Estevam325bb402021-02-22 13:16:43 -0300347 fit_size = ALIGN_IMX(fit_size, ALIGN_SIZE);
Peng Fan6609c262018-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
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000414#ifdef CONFIG_FSPI_CONF_HEADER
415static 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,
Jesse Taubeb0adbd62024-01-23 21:15:16 -0500428 .coladdrwidth = CONFIG_FSPI_COL_ADDR_W,
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000429 .devcfgenable = 0x00,
Jesse Taubeb0adbd62024-01-23 21:15:16 -0500430 .deviceModeType = 0x00,
431 .waitTimeCfgCommands = 0x0000,
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000432 .devmodeseq = {0x00, 0x00, 0x00, 0x00},
Jesse Taubeb0adbd62024-01-23 21:15:16 -0500433 .devmodearg = 0x00000000,
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000434 .cmd_enable = 0x00,
Jesse Taubeb0adbd62024-01-23 21:15:16 -0500435 .configModeType = {0x00},
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000436 .cmd_seq = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
437 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
438 .cmd_arg = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
439 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
Jesse Taubeb0adbd62024-01-23 21:15:16 -0500440 .controllermisc = cpu_to_le32(CONFIG_FSPI_CONTROLLER_MISC),
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000441 .dev_type = CONFIG_DEVICE_TYPE,
442 .sflash_pad = CONFIG_FLASH_PAD_TYPE,
443 .serial_clk = CONFIG_SERIAL_CLK_FREQUENCY,
444 .lut_custom = CONFIG_LUT_CUSTOM_SEQUENCE,
Jesse Taubeb0adbd62024-01-23 21:15:16 -0500445 .reserved_2 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
446 .sflashA1 = cpu_to_le32(CONFIG_FSPI_FLASH_A1_SIZE),
447 .sflashA2 = 0x00000000,
448 .sflashB1 = 0x00000000,
449 .sflashB2 = 0x00000000,
450 .cspadover = 0x00000000,
451 .sclkpadover = 0x00000000,
452 .datapadover = 0x00000000,
453 .dqspadover = 0x00000000,
454 .timeout = 0x00000000,
455 .commandInt = 0x00000000,
456 .datavalid = {0x0000, 0x0000},
457 .busyoffset = 0x0000,
458 .busybitpolarity = 0x0000,
459 .lutCustomSeq = {0x00},
460 .reserved_3 = {0x00}
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000461 };
462
463 for (val = strtok(lut_str, ","); val; val = strtok(NULL, ",")) {
464 fspi_conf_data.lut[i++] = strtoul(val, NULL, 16);
465 }
466
467 ret = lseek(ifd, 0, SEEK_CUR);
468 if (write(ifd, &fspi_conf_data, sizeof(fspi_conf_data)) == -1)
469 exit(EXIT_FAILURE);
470
471 ret = lseek(ifd, sizeof(fspi_conf_data), SEEK_CUR);
472
473 return ret;
474}
475#endif
476
Peng Fan6609c262018-11-20 10:19:36 +0000477void build_image(int ofd)
478{
479 int file_off, header_hdmi_off = 0, header_image_off;
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000480
481#ifdef CONFIG_FSPI_CONF_HEADER
482 int fspi_off, fspi_fd;
483 char *fspi;
484#endif
485
Peng Fan6609c262018-11-20 10:19:36 +0000486 int hdmi_fd, ap_fd, sld_fd;
487 uint32_t sld_load_addr = 0;
488 uint32_t csf_off, sld_csf_off = 0;
489 int ret;
490 struct stat sbuf;
491
492 if (!ap_img) {
493 fprintf(stderr, "No LOADER image specificed\n");
494 exit(EXIT_FAILURE);
495 }
496
497 file_off = 0;
498
499 if (signed_hdmi) {
500 header_hdmi_off = file_off + ivt_offset;
501
502 hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY);
503 if (hdmi_fd < 0) {
504 fprintf(stderr, "%s: Can't open: %s\n",
505 signed_hdmi, strerror(errno));
506 exit(EXIT_FAILURE);
507 }
508
509 if (fstat(hdmi_fd, &sbuf) < 0) {
510 fprintf(stderr, "%s: Can't stat: %s\n",
511 signed_hdmi, strerror(errno));
512 exit(EXIT_FAILURE);
513 }
514 close(hdmi_fd);
515
516 /*
517 * Aligned to 104KB = 92KB FW image + 0x8000
518 * (IVT and alignment) + 0x4000 (second IVT + CSF)
519 */
Fabio Estevam325bb402021-02-22 13:16:43 -0300520 file_off += ALIGN_IMX(sbuf.st_size,
Peng Fan6609c262018-11-20 10:19:36 +0000521 HDMI_FW_SIZE + 0x2000 + 0x1000);
522 }
523
524 header_image_off = file_off + ivt_offset;
525
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000526#ifdef CONFIG_FSPI_CONF_HEADER
527 fspi = CONFIG_FSPI_CONF_FILE;
528 fspi_fd = open(fspi, O_RDWR | O_CREAT, S_IRWXU);
529 if (fspi_fd < 0) {
530 fprintf(stderr, "Can't open %s: %s\n",
531 fspi, strerror(errno));
532 exit(EXIT_FAILURE);
533 }
534
535 fspi_off = generate_fspi_header(fspi_fd);
536 file_off = header_image_off + fspi_off;
537 close(fspi_fd);
538
539#endif
Peng Fan6609c262018-11-20 10:19:36 +0000540 ap_fd = open(ap_img, O_RDONLY | O_BINARY);
541 if (ap_fd < 0) {
542 fprintf(stderr, "%s: Can't open: %s\n",
543 ap_img, strerror(errno));
544 exit(EXIT_FAILURE);
545 }
546 if (fstat(ap_fd, &sbuf) < 0) {
547 fprintf(stderr, "%s: Can't stat: %s\n",
548 ap_img, strerror(errno));
549 exit(EXIT_FAILURE);
550 }
551 close(ap_fd);
552
553 imx_header[IMAGE_IVT_ID].fhdr.header.tag = IVT_HEADER_TAG; /* 0xD1 */
554 imx_header[IMAGE_IVT_ID].fhdr.header.length =
555 cpu_to_be16(sizeof(flash_header_v2_t));
556 imx_header[IMAGE_IVT_ID].fhdr.header.version = IVT_VERSION_V3; /* 0x41 */
557 imx_header[IMAGE_IVT_ID].fhdr.entry = ap_start_addr;
558 imx_header[IMAGE_IVT_ID].fhdr.self = ap_start_addr -
559 sizeof(imx_header_v3_t);
560 imx_header[IMAGE_IVT_ID].fhdr.dcd_ptr = 0;
561 imx_header[IMAGE_IVT_ID].fhdr.boot_data_ptr =
562 imx_header[IMAGE_IVT_ID].fhdr.self +
563 offsetof(imx_header_v3_t, boot_data);
564 imx_header[IMAGE_IVT_ID].boot_data.start =
565 imx_header[IMAGE_IVT_ID].fhdr.self - ivt_offset;
566 imx_header[IMAGE_IVT_ID].boot_data.size =
Fabio Estevam325bb402021-02-22 13:16:43 -0300567 ALIGN_IMX(sbuf.st_size + sizeof(imx_header_v3_t) + ivt_offset,
Peng Fan6609c262018-11-20 10:19:36 +0000568 sector_size);
569
570 image_off = header_image_off + sizeof(imx_header_v3_t);
571 file_off += imx_header[IMAGE_IVT_ID].boot_data.size;
572
573 imx_header[IMAGE_IVT_ID].boot_data.plugin = 0;
574 imx_header[IMAGE_IVT_ID].fhdr.csf =
575 imx_header[IMAGE_IVT_ID].boot_data.start +
576 imx_header[IMAGE_IVT_ID].boot_data.size;
577
578 imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */
579
580 csf_off = file_off;
581 file_off += CSF_SIZE;
582
583 /* Second boot loader image */
584 if (sld_img) {
585 if (!using_fit) {
586 fprintf(stderr, "Not support no fit\n");
587 exit(EXIT_FAILURE);
588 } else {
589 sld_header_off = sld_src_off - rom_image_offset;
Peng Fan6609c262018-11-20 10:19:36 +0000590 sld_fd = open(sld_img, O_RDONLY | O_BINARY);
591 if (sld_fd < 0) {
592 fprintf(stderr, "%s: Can't open: %s\n",
593 sld_img, strerror(errno));
594 exit(EXIT_FAILURE);
595 }
596
597 if (fstat(sld_fd, &sbuf) < 0) {
598 fprintf(stderr, "%s: Can't stat: %s\n",
599 sld_img, strerror(errno));
600 exit(EXIT_FAILURE);
601 }
602
603 close(sld_fd);
604
605 file_off = sld_header_off;
Simon Glassf3543e62022-09-06 20:26:52 -0600606 file_off += sbuf.st_size + sizeof(struct legacy_img_hdr);
Peng Fan6609c262018-11-20 10:19:36 +0000607 }
608 }
609
610 if (signed_hdmi) {
611 header_hdmi_off -= ivt_offset;
612 ret = lseek(ofd, header_hdmi_off, SEEK_SET);
613 if (ret < 0) {
614 fprintf(stderr, "lseek ofd fail for hdmi\n");
615 exit(EXIT_FAILURE);
616 }
617
618 /* The signed HDMI FW has 0x400 IVT offset, need remove it */
619 copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400);
620 }
621
622 /* Main Image */
623 header_image_off -= ivt_offset;
624 image_off -= ivt_offset;
625 ret = lseek(ofd, header_image_off, SEEK_SET);
626 if (ret < 0) {
627 fprintf(stderr, "lseek ofd fail\n");
628 exit(EXIT_FAILURE);
629 }
630
631 /* Write image header */
632 if (write(ofd, &imx_header[IMAGE_IVT_ID], sizeof(imx_header_v3_t)) !=
633 sizeof(imx_header_v3_t)) {
634 fprintf(stderr, "error writing image hdr\n");
635 exit(1);
636 }
637
638 copy_file(ofd, ap_img, 0, image_off, 0);
639
640 csf_off -= ivt_offset;
641 fill_zero(ofd, CSF_SIZE, csf_off);
642
643 if (sld_img) {
644 sld_header_off -= ivt_offset;
645 ret = lseek(ofd, sld_header_off, SEEK_SET);
646 if (ret < 0) {
647 fprintf(stderr, "lseek ofd fail for sld_img\n");
648 exit(EXIT_FAILURE);
649 }
650
651 /* Write image header */
652 if (!using_fit) {
653 /* TODO */
654 } else {
655 copy_file(ofd, sld_img, 0, sld_header_off, 0);
656 sld_csf_off =
657 generate_ivt_for_fit(ofd, sld_header_off,
658 sld_start_addr,
659 &sld_load_addr) + 0x20;
660 }
661 }
662
663 if (!signed_hdmi)
664 dump_header_v2(imx_header, 0);
665 dump_header_v2(imx_header, 1);
666
667 fprintf(stdout, "========= OFFSET dump =========");
668 if (signed_hdmi) {
669 fprintf(stdout, "\nSIGNED HDMI FW:\n");
670 fprintf(stdout, " header_hdmi_off \t0x%x\n",
671 header_hdmi_off);
672 }
673
674 fprintf(stdout, "\nLoader IMAGE:\n");
675 fprintf(stdout, " header_image_off \t0x%x\n image_off \t\t0x%x\n csf_off \t\t0x%x\n",
676 header_image_off, image_off, csf_off);
677 fprintf(stdout, " spl hab block: \t0x%x 0x%x 0x%x\n",
678 imx_header[IMAGE_IVT_ID].fhdr.self, header_image_off,
679 csf_off - header_image_off);
680
681 fprintf(stdout, "\nSecond Loader IMAGE:\n");
682 fprintf(stdout, " sld_header_off \t0x%x\n",
683 sld_header_off);
684 fprintf(stdout, " sld_csf_off \t\t0x%x\n",
685 sld_csf_off);
686 fprintf(stdout, " sld hab block: \t0x%x 0x%x 0x%x\n",
687 sld_load_addr, sld_header_off, sld_csf_off - sld_header_off);
688}
689
690int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams)
691{
692 /*
693 * SECO FW is a container image, this is to calculate the
694 * 2nd container offset.
695 */
696 fprintf(stdout, "parsing %s\n", mparams->imagename);
697 parse_cfg_file(mparams->imagename);
698
699 build_image(outfd);
700
701 return 0;
702}
703
704/*
705 * imx8mimage parameters
706 */
707U_BOOT_IMAGE_TYPE(
708 imx8mimage,
709 "NXP i.MX8M Boot Image support",
710 0,
711 NULL,
712 imx8mimage_check_params,
713 NULL,
714 imx8mimage_print_header,
715 imx8mimage_set_header,
716 NULL,
717 imx8mimage_check_image_types,
718 NULL,
719 NULL
720);