blob: 21075c23799a6ac94a6a68bb6c13334b2110b287 [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
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 Fanb8f16832019-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 Fan6609c262018-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 Estevam325bb402021-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 Fan6609c262018-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
Pali Rohár2972d7d2023-03-29 21:25:54 +020063static void imx8mimage_print_header(const void *ptr, struct image_tool_params *params)
Peng Fan6609c262018-11-20 10:19:36 +000064{
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 Fanb8f16832019-09-16 03:09:39 +000079 {CMD_ROM_VERSION, "ROM_VERSION", "rom version", },
Peng Fan6609c262018-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 Fanb8f16832019-09-16 03:09:39 +000099
100 if (rom_version == ROM_V2)
101 ivt_offset = 0;
Peng Fan6609c262018-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 Fan6609c262018-11-20 10:19:36 +0000111 break;
112 case CMD_DDR_FW:
113 /* Do nothing */
114 break;
Peng Fanb8f16832019-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;
Peng Fan6609c262018-11-20 10:19:36 +0000123 }
124}
125
126static 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 }
Peng Fan4d655bf2019-08-06 10:07:26 +0000138 switch (*cmd) {
139 case CMD_FIT:
140 using_fit = 1;
141 break;
142 }
Peng Fan6609c262018-11-20 10:19:36 +0000143 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
168static 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
Mikhail Ilin90177852022-11-23 13:48:44 +0300210 fclose(fd);
Peng Fan6609c262018-11-20 10:19:36 +0000211 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;
Peng Fan16841a62021-03-19 15:56:53 +0800251 uint64_t zero = 0;
Peng Fan6609c262018-11-20 10:19:36 +0000252 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));
Peng Fanc7b87112021-08-16 11:48:41 +0800274 goto err_mmap;
Peng Fan6609c262018-11-20 10:19:36 +0000275 }
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);
Peng Fanc7b87112021-08-16 11:48:41 +0800314err_mmap:
Peng Fan6609c262018-11-20 10:19:36 +0000315 close(dfd);
316}
317
318/* Return this IVT offset in the final output file */
319static int generate_ivt_for_fit(int fd, int fit_offset, uint32_t ep,
320 uint32_t *fit_load_addr)
321{
Simon Glassf3543e62022-09-06 20:26:52 -0600322 struct legacy_img_hdr image_header;
Peng Fan6609c262018-11-20 10:19:36 +0000323 int ret;
324
325 uint32_t fit_size, load_addr;
326 int align_len = 64 - 1; /* 64 is cacheline size */
327
328 ret = lseek(fd, fit_offset, SEEK_SET);
329 if (ret < 0) {
330 fprintf(stderr, "lseek fd fail for fit\n");
331 exit(EXIT_FAILURE);
332 }
333
Simon Glassf3543e62022-09-06 20:26:52 -0600334 if (read(fd, (char *)&image_header, sizeof(struct legacy_img_hdr)) !=
335 sizeof(struct legacy_img_hdr)) {
Peng Fan6609c262018-11-20 10:19:36 +0000336 fprintf(stderr, "generate_ivt_for_fit read failed: %s\n",
337 strerror(errno));
338 exit(EXIT_FAILURE);
339 }
340
341 if (be32_to_cpu(image_header.ih_magic) != FDT_MAGIC) {
342 fprintf(stderr, "%s error: not a FIT file\n", __func__);
343 exit(EXIT_FAILURE);
344 }
345
346 fit_size = fdt_totalsize(&image_header);
Peng Fan6609c262018-11-20 10:19:36 +0000347
Fabio Estevam325bb402021-02-22 13:16:43 -0300348 fit_size = ALIGN_IMX(fit_size, ALIGN_SIZE);
Peng Fan6609c262018-11-20 10:19:36 +0000349
350 ret = lseek(fd, fit_offset + fit_size, SEEK_SET);
351 if (ret < 0) {
352 fprintf(stderr, "lseek fd fail for fit\n");
353 exit(EXIT_FAILURE);
354 }
355
356 /*
357 * ep is the u-boot entry. SPL loads the FIT before the u-boot
358 * address. 0x2000 is for CSF_SIZE
359 */
360 load_addr = (ep - (fit_size + CSF_SIZE) - 512 - align_len) &
361 ~align_len;
362
363 flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 },
364 load_addr, 0, 0, 0,
365 (load_addr + fit_size),
366 (load_addr + fit_size + 0x20),
367 0 };
368
369 if (write(fd, &ivt_header, sizeof(flash_header_v2_t)) !=
370 sizeof(flash_header_v2_t)) {
371 fprintf(stderr, "IVT writing error on fit image\n");
372 exit(EXIT_FAILURE);
373 }
374
375 *fit_load_addr = load_addr;
376
377 return fit_offset + fit_size;
378}
379
380static void dump_header_v2(imx_header_v3_t *imx_header, int index)
381{
382 const char *ivt_name[2] = {"HDMI FW", "LOADER IMAGE"};
383
384 fprintf(stdout, "========= IVT HEADER [%s] =========\n",
385 ivt_name[index]);
386 fprintf(stdout, "header.tag: \t\t0x%x\n",
387 imx_header[index].fhdr.header.tag);
388 fprintf(stdout, "header.length: \t\t0x%x\n",
389 imx_header[index].fhdr.header.length);
390 fprintf(stdout, "header.version: \t0x%x\n",
391 imx_header[index].fhdr.header.version);
392 fprintf(stdout, "entry: \t\t\t0x%x\n",
393 imx_header[index].fhdr.entry);
394 fprintf(stdout, "reserved1: \t\t0x%x\n",
395 imx_header[index].fhdr.reserved1);
396 fprintf(stdout, "dcd_ptr: \t\t0x%x\n",
397 imx_header[index].fhdr.dcd_ptr);
398 fprintf(stdout, "boot_data_ptr: \t\t0x%x\n",
399 imx_header[index].fhdr.boot_data_ptr);
400 fprintf(stdout, "self: \t\t\t0x%x\n",
401 imx_header[index].fhdr.self);
402 fprintf(stdout, "csf: \t\t\t0x%x\n",
403 imx_header[index].fhdr.csf);
404 fprintf(stdout, "reserved2: \t\t0x%x\n",
405 imx_header[index].fhdr.reserved2);
406
407 fprintf(stdout, "boot_data.start: \t0x%x\n",
408 imx_header[index].boot_data.start);
409 fprintf(stdout, "boot_data.size: \t0x%x\n",
410 imx_header[index].boot_data.size);
411 fprintf(stdout, "boot_data.plugin: \t0x%x\n",
412 imx_header[index].boot_data.plugin);
413}
414
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000415#ifdef CONFIG_FSPI_CONF_HEADER
416static int generate_fspi_header (int ifd)
417{
418 int ret, i = 0;
419 char *val;
420 char lut_str[] = CONFIG_LUT_SEQUENCE;
421
422 fspi_conf fspi_conf_data = {
423 .tag = {0x46, 0x43, 0x46, 0x42},
424 .version = {0x00, 0x00, 0x01, 0x56},
425 .reserved_1 = {0x00, 0x00, 0x00, 0x00},
426 .read_sample = CONFIG_READ_CLK_SOURCE,
427 .datahold = 0x03,
428 .datasetup = 0x03,
429 .coladdrwidth = 0x00,
430 .devcfgenable = 0x00,
431 .reserved_2 = {0x00, 0x00, 0x00},
432 .devmodeseq = {0x00, 0x00, 0x00, 0x00},
433 .devmodearg = {0x00, 0x00, 0x00, 0x00},
434 .cmd_enable = 0x00,
435 .reserved_3 = {0x00},
436 .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},
440 .controllermisc = {0x00, 0x00, 0x00, 0x00},
441 .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,
445 .reserved_4 = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
446 .sflashA1 = {0x00, 0x00, 0x00, 0x10},
447 .sflashA2 = {0x00, 0x00, 0x00, 0x00},
448 .sflashB1 = {0x00, 0x00, 0x00, 0x00},
449 .sflashB2 = {0x00, 0x00, 0x00, 0x00},
450 .cspadover = {0x00, 0x00, 0x00, 0x00},
451 .sclkpadover = {0x00, 0x00, 0x00, 0x00},
452 .datapadover = {0x00, 0x00, 0x00, 0x00},
453 .dqspadover = {0x00, 0x00, 0x00, 0x00},
454 .timeout = {0x00, 0x00, 0x00, 0x00},
455 .commandInt = {0x00, 0x00, 0x00, 0x00},
456 .datavalid = {0x00, 0x00, 0x00, 0x00},
457 .busyoffset = {0x00, 0x00},
458 .busybitpolarity = {0x00, 0x00},
459 };
460
461 for (val = strtok(lut_str, ","); val; val = strtok(NULL, ",")) {
462 fspi_conf_data.lut[i++] = strtoul(val, NULL, 16);
463 }
464
465 ret = lseek(ifd, 0, SEEK_CUR);
466 if (write(ifd, &fspi_conf_data, sizeof(fspi_conf_data)) == -1)
467 exit(EXIT_FAILURE);
468
469 ret = lseek(ifd, sizeof(fspi_conf_data), SEEK_CUR);
470
471 return ret;
472}
473#endif
474
Peng Fan6609c262018-11-20 10:19:36 +0000475void build_image(int ofd)
476{
477 int file_off, header_hdmi_off = 0, header_image_off;
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000478
479#ifdef CONFIG_FSPI_CONF_HEADER
480 int fspi_off, fspi_fd;
481 char *fspi;
482#endif
483
Peng Fan6609c262018-11-20 10:19:36 +0000484 int hdmi_fd, ap_fd, sld_fd;
485 uint32_t sld_load_addr = 0;
486 uint32_t csf_off, sld_csf_off = 0;
487 int ret;
488 struct stat sbuf;
489
490 if (!ap_img) {
491 fprintf(stderr, "No LOADER image specificed\n");
492 exit(EXIT_FAILURE);
493 }
494
495 file_off = 0;
496
497 if (signed_hdmi) {
498 header_hdmi_off = file_off + ivt_offset;
499
500 hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY);
501 if (hdmi_fd < 0) {
502 fprintf(stderr, "%s: Can't open: %s\n",
503 signed_hdmi, strerror(errno));
504 exit(EXIT_FAILURE);
505 }
506
507 if (fstat(hdmi_fd, &sbuf) < 0) {
508 fprintf(stderr, "%s: Can't stat: %s\n",
509 signed_hdmi, strerror(errno));
510 exit(EXIT_FAILURE);
511 }
512 close(hdmi_fd);
513
514 /*
515 * Aligned to 104KB = 92KB FW image + 0x8000
516 * (IVT and alignment) + 0x4000 (second IVT + CSF)
517 */
Fabio Estevam325bb402021-02-22 13:16:43 -0300518 file_off += ALIGN_IMX(sbuf.st_size,
Peng Fan6609c262018-11-20 10:19:36 +0000519 HDMI_FW_SIZE + 0x2000 + 0x1000);
520 }
521
522 header_image_off = file_off + ivt_offset;
523
Mamta Shukla5fe1d4b2022-07-12 14:36:17 +0000524#ifdef CONFIG_FSPI_CONF_HEADER
525 fspi = CONFIG_FSPI_CONF_FILE;
526 fspi_fd = open(fspi, O_RDWR | O_CREAT, S_IRWXU);
527 if (fspi_fd < 0) {
528 fprintf(stderr, "Can't open %s: %s\n",
529 fspi, strerror(errno));
530 exit(EXIT_FAILURE);
531 }
532
533 fspi_off = generate_fspi_header(fspi_fd);
534 file_off = header_image_off + fspi_off;
535 close(fspi_fd);
536
537#endif
Peng Fan6609c262018-11-20 10:19:36 +0000538 ap_fd = open(ap_img, O_RDONLY | O_BINARY);
539 if (ap_fd < 0) {
540 fprintf(stderr, "%s: Can't open: %s\n",
541 ap_img, strerror(errno));
542 exit(EXIT_FAILURE);
543 }
544 if (fstat(ap_fd, &sbuf) < 0) {
545 fprintf(stderr, "%s: Can't stat: %s\n",
546 ap_img, strerror(errno));
547 exit(EXIT_FAILURE);
548 }
549 close(ap_fd);
550
551 imx_header[IMAGE_IVT_ID].fhdr.header.tag = IVT_HEADER_TAG; /* 0xD1 */
552 imx_header[IMAGE_IVT_ID].fhdr.header.length =
553 cpu_to_be16(sizeof(flash_header_v2_t));
554 imx_header[IMAGE_IVT_ID].fhdr.header.version = IVT_VERSION_V3; /* 0x41 */
555 imx_header[IMAGE_IVT_ID].fhdr.entry = ap_start_addr;
556 imx_header[IMAGE_IVT_ID].fhdr.self = ap_start_addr -
557 sizeof(imx_header_v3_t);
558 imx_header[IMAGE_IVT_ID].fhdr.dcd_ptr = 0;
559 imx_header[IMAGE_IVT_ID].fhdr.boot_data_ptr =
560 imx_header[IMAGE_IVT_ID].fhdr.self +
561 offsetof(imx_header_v3_t, boot_data);
562 imx_header[IMAGE_IVT_ID].boot_data.start =
563 imx_header[IMAGE_IVT_ID].fhdr.self - ivt_offset;
564 imx_header[IMAGE_IVT_ID].boot_data.size =
Fabio Estevam325bb402021-02-22 13:16:43 -0300565 ALIGN_IMX(sbuf.st_size + sizeof(imx_header_v3_t) + ivt_offset,
Peng Fan6609c262018-11-20 10:19:36 +0000566 sector_size);
567
568 image_off = header_image_off + sizeof(imx_header_v3_t);
569 file_off += imx_header[IMAGE_IVT_ID].boot_data.size;
570
571 imx_header[IMAGE_IVT_ID].boot_data.plugin = 0;
572 imx_header[IMAGE_IVT_ID].fhdr.csf =
573 imx_header[IMAGE_IVT_ID].boot_data.start +
574 imx_header[IMAGE_IVT_ID].boot_data.size;
575
576 imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */
577
578 csf_off = file_off;
579 file_off += CSF_SIZE;
580
581 /* Second boot loader image */
582 if (sld_img) {
583 if (!using_fit) {
584 fprintf(stderr, "Not support no fit\n");
585 exit(EXIT_FAILURE);
586 } else {
587 sld_header_off = sld_src_off - rom_image_offset;
Peng Fan6609c262018-11-20 10:19:36 +0000588 sld_fd = open(sld_img, O_RDONLY | O_BINARY);
589 if (sld_fd < 0) {
590 fprintf(stderr, "%s: Can't open: %s\n",
591 sld_img, strerror(errno));
592 exit(EXIT_FAILURE);
593 }
594
595 if (fstat(sld_fd, &sbuf) < 0) {
596 fprintf(stderr, "%s: Can't stat: %s\n",
597 sld_img, strerror(errno));
598 exit(EXIT_FAILURE);
599 }
600
601 close(sld_fd);
602
603 file_off = sld_header_off;
Simon Glassf3543e62022-09-06 20:26:52 -0600604 file_off += sbuf.st_size + sizeof(struct legacy_img_hdr);
Peng Fan6609c262018-11-20 10:19:36 +0000605 }
606 }
607
608 if (signed_hdmi) {
609 header_hdmi_off -= ivt_offset;
610 ret = lseek(ofd, header_hdmi_off, SEEK_SET);
611 if (ret < 0) {
612 fprintf(stderr, "lseek ofd fail for hdmi\n");
613 exit(EXIT_FAILURE);
614 }
615
616 /* The signed HDMI FW has 0x400 IVT offset, need remove it */
617 copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400);
618 }
619
620 /* Main Image */
621 header_image_off -= ivt_offset;
622 image_off -= ivt_offset;
623 ret = lseek(ofd, header_image_off, SEEK_SET);
624 if (ret < 0) {
625 fprintf(stderr, "lseek ofd fail\n");
626 exit(EXIT_FAILURE);
627 }
628
629 /* Write image header */
630 if (write(ofd, &imx_header[IMAGE_IVT_ID], sizeof(imx_header_v3_t)) !=
631 sizeof(imx_header_v3_t)) {
632 fprintf(stderr, "error writing image hdr\n");
633 exit(1);
634 }
635
636 copy_file(ofd, ap_img, 0, image_off, 0);
637
638 csf_off -= ivt_offset;
639 fill_zero(ofd, CSF_SIZE, csf_off);
640
641 if (sld_img) {
642 sld_header_off -= ivt_offset;
643 ret = lseek(ofd, sld_header_off, SEEK_SET);
644 if (ret < 0) {
645 fprintf(stderr, "lseek ofd fail for sld_img\n");
646 exit(EXIT_FAILURE);
647 }
648
649 /* Write image header */
650 if (!using_fit) {
651 /* TODO */
652 } else {
653 copy_file(ofd, sld_img, 0, sld_header_off, 0);
654 sld_csf_off =
655 generate_ivt_for_fit(ofd, sld_header_off,
656 sld_start_addr,
657 &sld_load_addr) + 0x20;
658 }
659 }
660
661 if (!signed_hdmi)
662 dump_header_v2(imx_header, 0);
663 dump_header_v2(imx_header, 1);
664
665 fprintf(stdout, "========= OFFSET dump =========");
666 if (signed_hdmi) {
667 fprintf(stdout, "\nSIGNED HDMI FW:\n");
668 fprintf(stdout, " header_hdmi_off \t0x%x\n",
669 header_hdmi_off);
670 }
671
672 fprintf(stdout, "\nLoader IMAGE:\n");
673 fprintf(stdout, " header_image_off \t0x%x\n image_off \t\t0x%x\n csf_off \t\t0x%x\n",
674 header_image_off, image_off, csf_off);
675 fprintf(stdout, " spl hab block: \t0x%x 0x%x 0x%x\n",
676 imx_header[IMAGE_IVT_ID].fhdr.self, header_image_off,
677 csf_off - header_image_off);
678
679 fprintf(stdout, "\nSecond Loader IMAGE:\n");
680 fprintf(stdout, " sld_header_off \t0x%x\n",
681 sld_header_off);
682 fprintf(stdout, " sld_csf_off \t\t0x%x\n",
683 sld_csf_off);
684 fprintf(stdout, " sld hab block: \t0x%x 0x%x 0x%x\n",
685 sld_load_addr, sld_header_off, sld_csf_off - sld_header_off);
686}
687
688int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams)
689{
690 /*
691 * SECO FW is a container image, this is to calculate the
692 * 2nd container offset.
693 */
694 fprintf(stdout, "parsing %s\n", mparams->imagename);
695 parse_cfg_file(mparams->imagename);
696
697 build_image(outfd);
698
699 return 0;
700}
701
702/*
703 * imx8mimage parameters
704 */
705U_BOOT_IMAGE_TYPE(
706 imx8mimage,
707 "NXP i.MX8M Boot Image support",
708 0,
709 NULL,
710 imx8mimage_check_params,
711 NULL,
712 imx8mimage_print_header,
713 imx8mimage_set_header,
714 NULL,
715 imx8mimage_check_image_types,
716 NULL,
717 NULL
718);