blob: 7a060811c7e2b47044de5c3860c2feca9ee6bc71 [file] [log] [blame]
Peng Fan60a56072018-10-16 04:50:30 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018 NXP
4 *
5 * Peng Fan <peng.fan@nxp.com>
6 */
7
8#include "imx8image.h"
Simon Glass2dc9c342020-05-10 11:40:01 -06009#include <image.h>
Peng Fan60a56072018-10-16 04:50:30 +000010
11static int p_idx;
12static int sector_size;
13static soc_type_t soc;
14static int container = -1;
15static int32_t core_type = CFG_CORE_INVALID;
16static bool emmc_fastboot;
Heiko Schocherfd61e712024-11-04 19:02:03 +010017static bool dcd_skip;
Peng Fan60a56072018-10-16 04:50:30 +000018static image_t param_stack[IMG_STACK_SIZE];
19static uint8_t fuse_version;
20static uint16_t sw_version;
21static uint32_t custom_partition;
22static uint32_t scfw_flags;
23
24int imx8image_check_params(struct image_tool_params *params)
25{
26 return 0;
27}
28
29static void imx8image_set_header(void *ptr, struct stat *sbuf, int ifd,
30 struct image_tool_params *params)
31{
32}
33
Pali Rohár0ed41e22023-03-29 21:25:54 +020034static void imx8image_print_header(const void *ptr, struct image_tool_params *params)
Peng Fan60a56072018-10-16 04:50:30 +000035{
36}
37
38static int imx8image_check_image_types(uint8_t type)
39{
40 return (type == IH_TYPE_IMX8IMAGE) ? EXIT_SUCCESS : EXIT_FAILURE;
41}
42
43static table_entry_t imx8image_cmds[] = {
44 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", },
Heiko Schocherfd61e712024-11-04 19:02:03 +010045 {CMD_DCD_SKIP, "DCD_SKIP", "skip DCD init", },
Peng Fan60a56072018-10-16 04:50:30 +000046 {CMD_FUSE_VERSION, "FUSE_VERSION", "fuse version", },
47 {CMD_SW_VERSION, "SW_VERSION", "sw version", },
48 {CMD_MSG_BLOCK, "MSG_BLOCK", "msg block", },
49 {CMD_FILEOFF, "FILEOFF", "fileoff", },
50 {CMD_FLAG, "FLAG", "flag", },
51 {CMD_APPEND, "APPEND", "append a container", },
52 {CMD_PARTITION, "PARTITION", "new partition", },
53 {CMD_SOC_TYPE, "SOC_TYPE", "soc type", },
54 {CMD_CONTAINER, "CONTAINER", "new container", },
55 {CMD_IMAGE, "IMAGE", "new image", },
56 {CMD_DATA, "DATA", "new data", },
57 {-1, "", "", },
58};
59
60static table_entry_t imx8image_core_entries[] = {
61 {CFG_SCU, "SCU", "scu core", },
Gary Bissona1e98d62024-08-05 23:25:08 +020062 {CFG_PWR, "PWR", "uPower core", },
Peng Fan60a56072018-10-16 04:50:30 +000063 {CFG_M40, "M40", "M4 core 0", },
64 {CFG_M41, "M41", "M4 core 1", },
65 {CFG_A35, "A35", "A35 core", },
Peng Faneeb23bf2022-07-26 16:41:19 +080066 {CFG_A55, "A55", "A55 core", },
Peng Fan60a56072018-10-16 04:50:30 +000067 {CFG_A53, "A53", "A53 core", },
68 {CFG_A72, "A72", "A72 core", },
69 {-1, "", "", },
70};
71
72static table_entry_t imx8image_sector_size[] = {
73 {0x400, "sd", "sd/emmc",},
74 {0x400, "emmc_fastboot", "emmc fastboot",},
75 {0x400, "fspi", "flexspi", },
76 {0x1000, "nand_4k", "nand 4K", },
77 {0x2000, "nand_8k", "nand 8K", },
78 {0x4000, "nand_16k", "nand 16K", },
79 {-1, "", "Invalid", },
80};
81
82static void parse_cfg_cmd(image_t *param_stack, int32_t cmd, char *token,
83 char *name, int lineno)
84{
85 switch (cmd) {
86 case CMD_BOOT_FROM:
87 sector_size = get_table_entry_id(imx8image_sector_size,
88 "imximage boot option",
89 token);
90 if (!strncmp("emmc_fastboot", token, 13))
91 emmc_fastboot = true;
92 break;
Heiko Schocherfd61e712024-11-04 19:02:03 +010093 case CMD_DCD_SKIP:
94 if (!strncmp("true", token, 4))
95 dcd_skip = true;
Fabio Estevama95e8322024-11-11 23:38:54 -030096 break;
Peng Fan60a56072018-10-16 04:50:30 +000097 case CMD_FUSE_VERSION:
98 fuse_version = (uint8_t)(strtoll(token, NULL, 0) & 0xFF);
99 break;
100 case CMD_SW_VERSION:
101 sw_version = (uint8_t)(strtoll(token, NULL, 0) & 0xFFFF);
102 break;
103 case CMD_FILEOFF:
104 param_stack[p_idx].option = FILEOFF;
105 param_stack[p_idx++].dst = (uint32_t)strtoll(token, NULL, 0);
106 break;
107 case CMD_MSG_BLOCK:
108 param_stack[p_idx].option = MSG_BLOCK;
109 param_stack[p_idx].filename = token;
110 break;
111 case CMD_FLAG:
112 param_stack[p_idx].option = FLAG;
113 param_stack[p_idx++].entry = (uint32_t)strtoll(token, NULL, 0);
114 break;
115 case CMD_APPEND:
116 param_stack[p_idx].option = APPEND;
117 param_stack[p_idx++].filename = token;
118 break;
119 case CMD_PARTITION:
120 param_stack[p_idx].option = PARTITION;
121 param_stack[p_idx++].entry = (uint32_t)strtoll(token, NULL, 0);
122 break;
123 case CMD_SOC_TYPE:
124 if (!strncmp(token, "IMX8QX", 6)) {
125 soc = QX;
126 } else if (!strncmp(token, "IMX8QM", 6)) {
127 soc = QM;
Peng Faneeb23bf2022-07-26 16:41:19 +0800128 } else if (!strncmp(token, "ULP", 3)) {
Gary Bisson70365802024-08-05 23:25:07 +0200129 soc = ULP;
Peng Faneeb23bf2022-07-26 16:41:19 +0800130 } else if (!strncmp(token, "IMX9", 4)) {
131 soc = IMX9;
Peng Fan60a56072018-10-16 04:50:30 +0000132 } else {
133 fprintf(stderr, "Unknown CMD_SOC_TYPE");
134 exit(EXIT_FAILURE);
135 }
136 break;
137 case CMD_IMAGE:
138 case CMD_DATA:
139 core_type = get_table_entry_id(imx8image_core_entries,
140 "imx8image core entries",
141 token);
142 if (core_type < 0) {
143 fprintf(stderr, "Wrong IMAGE core_type %s\n", token);
144 exit(EXIT_FAILURE);
145 }
146 break;
147 default:
148 break;
149 }
150}
151
152static void parse_cfg_fld(image_t *param_stack, int32_t *cmd, char *token,
153 char *name, int lineno, int fld)
154{
155 switch (fld) {
156 case CFG_COMMAND:
157 *cmd = get_table_entry_id(imx8image_cmds, "imx8image cmds",
158 token);
159 if (*cmd < 0) {
160 fprintf(stderr, "Error: %s[%d] - Invalid command (%s)\n", name, lineno, token);
161 exit(EXIT_FAILURE);
162 }
163
164 if (*cmd == CMD_CONTAINER) {
165 fprintf(stdout, "New Container: \t%d\n", ++container);
166 param_stack[p_idx++].option = NEW_CONTAINER;
167 }
168 break;
169 case CFG_CORE_TYPE:
170 parse_cfg_cmd(param_stack, *cmd, token, name, lineno);
171 break;
172 case CFG_IMAGE_NAME:
173 if (*cmd == CMD_MSG_BLOCK) {
174 if (!strncmp(token, "fuse", 4)) {
175 param_stack[p_idx].ext = SC_R_OTP;
176 } else if (!strncmp(token, "debug", 5)) {
177 param_stack[p_idx].ext = SC_R_DEBUG;
178 } else if (!strncmp(token, "field", 5)) {
179 param_stack[p_idx].ext = SC_R_ROM_0;
180 } else {
181 fprintf(stderr, "MSG type not found %s\n", token);
182 exit(EXIT_FAILURE);
183 }
184 break;
185 }
186 switch (core_type) {
187 case CFG_SCU:
188 param_stack[p_idx].option = SCFW;
189 param_stack[p_idx++].filename = token;
190 break;
Gary Bissona1e98d62024-08-05 23:25:08 +0200191 case CFG_PWR:
192 param_stack[p_idx].option = UPOWER;
193 param_stack[p_idx++].filename = token;
194 break;
Peng Fan60a56072018-10-16 04:50:30 +0000195 case CFG_M40:
196 param_stack[p_idx].option = M40;
197 param_stack[p_idx].ext = 0;
198 param_stack[p_idx].filename = token;
199 break;
200 case CFG_M41:
201 param_stack[p_idx].option = M41;
202 param_stack[p_idx].ext = 1;
203 param_stack[p_idx].filename = token;
204 break;
205 case CFG_A35:
Peng Faneeb23bf2022-07-26 16:41:19 +0800206 case CFG_A55:
Peng Fan60a56072018-10-16 04:50:30 +0000207 param_stack[p_idx].ext = CORE_CA35;
208 param_stack[p_idx].option =
209 (*cmd == CMD_DATA) ? DATA : AP;
210 param_stack[p_idx].filename = token;
211 break;
212 case CFG_A53:
213 param_stack[p_idx].ext = CORE_CA53;
214 param_stack[p_idx].option =
215 (*cmd == CMD_DATA) ? DATA : AP;
216 param_stack[p_idx].filename = token;
217 break;
218 case CFG_A72:
219 param_stack[p_idx].ext = CORE_CA72;
220 param_stack[p_idx].option =
221 (*cmd == CMD_DATA) ? DATA : AP;
222 param_stack[p_idx].filename = token;
223 break;
224 }
225 break;
226 case CFG_LOAD_ADDR:
227 if (*cmd == CMD_MSG_BLOCK) {
228 param_stack[p_idx++].entry =
229 (uint32_t)strtoll(token, NULL, 0);
230 break;
231 }
232 switch (core_type) {
233 case CFG_SCU:
234 break;
235 case CFG_M40:
236 case CFG_M41:
237 case CFG_A35:
238 case CFG_A53:
Peng Faneeb23bf2022-07-26 16:41:19 +0800239 case CFG_A55:
Peng Fan60a56072018-10-16 04:50:30 +0000240 case CFG_A72:
241 param_stack[p_idx++].entry =
242 (uint32_t)strtoll(token, NULL, 0);
243 break;
244 }
245 default:
246 break;
247 }
248}
249
250static uint32_t parse_cfg_file(image_t *param_stack, char *name)
251{
252 FILE *fd = NULL;
253 char *line = NULL;
254 char *token, *saveptr1, *saveptr2;
255 int lineno = 0;
256 int fld;
257 size_t len;
258 int32_t cmd;
259
260 fd = fopen(name, "r");
261 if (fd == 0) {
262 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
263 exit(EXIT_FAILURE);
264 }
265
266 /*
267 * Very simple parsing, line starting with # are comments
268 * and are dropped
269 */
270 while ((getline(&line, &len, fd)) > 0) {
271 lineno++;
272
273 token = strtok_r(line, "\r\n", &saveptr1);
274 if (!token)
275 continue;
276
277 /* Check inside the single line */
278 for (fld = CFG_COMMAND, cmd = CFG_INVALID,
279 line = token; ; line = NULL, fld++) {
280 token = strtok_r(line, " \t", &saveptr2);
281 if (!token)
282 break;
283
284 /* Drop all text starting with '#' as comments */
285 if (token[0] == '#')
286 break;
287
288 parse_cfg_fld(param_stack, &cmd, token, name, lineno,
289 fld);
290 }
291 }
292
Mikhail Ilin7ed5b722022-11-23 13:59:49 +0300293 fclose(fd);
Peng Fan60a56072018-10-16 04:50:30 +0000294 return 0;
295}
296
297static void check_file(struct stat *sbuf, char *filename)
298{
299 int tmp_fd = open(filename, O_RDONLY | O_BINARY);
300
301 if (tmp_fd < 0) {
302 fprintf(stderr, "%s: Can't open: %s\n",
303 filename, strerror(errno));
304 exit(EXIT_FAILURE);
305 }
306
307 if (fstat(tmp_fd, sbuf) < 0) {
308 fprintf(stderr, "%s: Can't stat: %s\n",
309 filename, strerror(errno));
310 exit(EXIT_FAILURE);
311 }
312
313 close(tmp_fd);
314}
315
316static void copy_file_aligned(int ifd, const char *datafile, int offset,
317 int align)
318{
319 int dfd;
320 struct stat sbuf;
321 unsigned char *ptr;
322 uint8_t zeros[0x4000];
323 int size;
Peng Fan00c2dd52018-11-05 09:53:22 +0000324 int ret;
Peng Fan60a56072018-10-16 04:50:30 +0000325
326 if (align > 0x4000) {
327 fprintf(stderr, "Wrong alignment requested %d\n", align);
328 exit(EXIT_FAILURE);
329 }
330
331 memset(zeros, 0, sizeof(zeros));
332
333 dfd = open(datafile, O_RDONLY | O_BINARY);
334 if (dfd < 0) {
335 fprintf(stderr, "Can't open %s: %s\n",
336 datafile, strerror(errno));
337 exit(EXIT_FAILURE);
338 }
339
340 if (fstat(dfd, &sbuf) < 0) {
341 fprintf(stderr, "Can't stat %s: %s\n",
342 datafile, strerror(errno));
343 exit(EXIT_FAILURE);
344 }
345
346 if (sbuf.st_size == 0)
347 goto close;
348
349 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
350 if (ptr == MAP_FAILED) {
351 fprintf(stderr, "Can't read %s: %s\n",
352 datafile, strerror(errno));
353 exit(EXIT_FAILURE);
354 }
355
356 size = sbuf.st_size;
Peng Fan00c2dd52018-11-05 09:53:22 +0000357 ret = lseek(ifd, offset, SEEK_SET);
358 if (ret < 0) {
359 fprintf(stderr, "%s: lseek error %s\n",
360 __func__, strerror(errno));
361 exit(EXIT_FAILURE);
362 }
363
Peng Fan60a56072018-10-16 04:50:30 +0000364 if (write(ifd, ptr, size) != size) {
365 fprintf(stderr, "Write error %s\n", strerror(errno));
366 exit(EXIT_FAILURE);
367 }
368
369 align = ALIGN(size, align) - size;
370
371 if (write(ifd, (char *)&zeros, align) != align) {
372 fprintf(stderr, "Write error: %s\n", strerror(errno));
373 exit(EXIT_FAILURE);
374 }
375
376 munmap((void *)ptr, sbuf.st_size);
377close:
378 close(dfd);
379}
380
381static void copy_file (int ifd, const char *datafile, int pad, int offset)
382{
383 int dfd;
384 struct stat sbuf;
385 unsigned char *ptr;
386 int tail;
Peng Fane3ed0cc2021-03-19 15:56:53 +0800387 uint64_t zero = 0;
Peng Fan60a56072018-10-16 04:50:30 +0000388 uint8_t zeros[4096];
Peng Fan00c2dd52018-11-05 09:53:22 +0000389 int size, ret;
Peng Fan60a56072018-10-16 04:50:30 +0000390
391 memset(zeros, 0, sizeof(zeros));
392
393 dfd = open(datafile, O_RDONLY | O_BINARY);
394 if (dfd < 0) {
395 fprintf(stderr, "Can't open %s: %s\n",
396 datafile, strerror(errno));
397 exit(EXIT_FAILURE);
398 }
399
400 if (fstat(dfd, &sbuf) < 0) {
401 fprintf(stderr, "Can't stat %s: %s\n",
402 datafile, strerror(errno));
403 exit(EXIT_FAILURE);
404 }
405
406 if (sbuf.st_size == 0)
407 goto close;
408
409 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
410 if (ptr == MAP_FAILED) {
411 fprintf(stderr, "Can't read %s: %s\n",
412 datafile, strerror(errno));
413 exit(EXIT_FAILURE);
414 }
415
416 size = sbuf.st_size;
Peng Fan00c2dd52018-11-05 09:53:22 +0000417 ret = lseek(ifd, offset, SEEK_SET);
418 if (ret < 0) {
419 fprintf(stderr, "%s: lseek error %s\n",
420 __func__, strerror(errno));
421 exit(EXIT_FAILURE);
422 }
423
Peng Fan60a56072018-10-16 04:50:30 +0000424 if (write(ifd, ptr, size) != size) {
425 fprintf(stderr, "Write error %s\n",
426 strerror(errno));
427 exit(EXIT_FAILURE);
428 }
429
430 tail = size % 4;
431 pad = pad - size;
432 if (pad == 1 && tail != 0) {
433 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
434 fprintf(stderr, "Write error on %s\n",
435 strerror(errno));
436 exit(EXIT_FAILURE);
437 }
438 } else if (pad > 1) {
439 while (pad > 0) {
440 int todo = sizeof(zeros);
441
442 if (todo > pad)
443 todo = pad;
444 if (write(ifd, (char *)&zeros, todo) != todo) {
445 fprintf(stderr, "Write error: %s\n",
446 strerror(errno));
447 exit(EXIT_FAILURE);
448 }
449 pad -= todo;
450 }
451 }
452
453 munmap((void *)ptr, sbuf.st_size);
454close:
455 close(dfd);
456}
457
458uint64_t read_dcd_offset(char *filename)
459{
460 int dfd;
461 struct stat sbuf;
462 uint8_t *ptr;
463 uint64_t offset = 0;
464
465 dfd = open(filename, O_RDONLY | O_BINARY);
466 if (dfd < 0) {
467 fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno));
468 exit(EXIT_FAILURE);
469 }
470
471 if (fstat(dfd, &sbuf) < 0) {
472 fprintf(stderr, "Can't stat %s: %s\n", filename, strerror(errno));
473 exit(EXIT_FAILURE);
474 }
475
476 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
477 if (ptr == MAP_FAILED) {
478 fprintf(stderr, "Can't read %s: %s\n", filename, strerror(errno));
479 exit(EXIT_FAILURE);
480 }
481
482 offset = *(uint32_t *)(ptr + DCD_ENTRY_ADDR_IN_SCFW);
483
484 munmap((void *)ptr, sbuf.st_size);
485 close(dfd);
486
487 return offset;
488}
489
490static void set_image_hash(boot_img_t *img, char *filename, uint32_t hash_type)
491{
492 FILE *fp = NULL;
493 char sha_command[512];
494 char hash[2 * HASH_MAX_LEN + 1];
495 int i, ret;
496
497 if (img->size == 0)
498 sprintf(sha_command, "sha%dsum /dev/null", hash_type);
499 else
500 sprintf(sha_command, "dd if=/dev/zero of=tmp_pad bs=%d count=1;\
501 dd if=\'%s\' of=tmp_pad conv=notrunc;\
502 sha%dsum tmp_pad; rm -f tmp_pad",
503 img->size, filename, hash_type);
504
505 switch (hash_type) {
506 case HASH_TYPE_SHA_256:
507 img->hab_flags |= IMG_FLAG_HASH_SHA256;
508 break;
509 case HASH_TYPE_SHA_384:
510 img->hab_flags |= IMG_FLAG_HASH_SHA384;
511 break;
512 case HASH_TYPE_SHA_512:
513 img->hab_flags |= IMG_FLAG_HASH_SHA512;
514 break;
515 default:
516 fprintf(stderr, "Wrong hash type selected (%d) !!!\n\n",
517 hash_type);
518 exit(EXIT_FAILURE);
519 break;
520 }
521 memset(img->hash, 0, HASH_MAX_LEN);
522
523 fp = popen(sha_command, "r");
524 if (!fp) {
525 fprintf(stderr, "Failed to run command hash\n");
526 exit(EXIT_FAILURE);
527 }
528
529 if (!fgets(hash, hash_type / 4 + 1, fp)) {
530 fprintf(stderr, "Failed to hash file: %s\n", filename);
531 exit(EXIT_FAILURE);
532 }
533
534 for (i = 0; i < strlen(hash) / 2; i++) {
535 ret = sscanf(hash + 2 * i, "%02hhx", &img->hash[i]);
536 if (ret < 0) {
537 fprintf(stderr, "Failed sscanf hash: %d\n", ret);
538 exit(EXIT_FAILURE);
539 }
540 }
541
542 pclose(fp);
543}
544
545static void set_image_array_entry(flash_header_v3_t *container,
546 soc_type_t soc, const image_t *image_stack,
547 uint32_t offset, uint32_t size,
548 char *tmp_filename, bool dcd_skip)
549{
550 uint64_t entry = image_stack->entry;
551 uint64_t core = image_stack->ext;
552 uint32_t meta;
553 char *tmp_name = "";
554 option_type_t type = image_stack->option;
555 boot_img_t *img = &container->img[container->num_images];
556
557 img->offset = offset; /* Is re-adjusted later */
558 img->size = size;
559
560 set_image_hash(img, tmp_filename, IMAGE_HASH_ALGO_DEFAULT);
561
562 switch (type) {
563 case SECO:
564 img->hab_flags |= IMG_TYPE_SECO;
565 img->hab_flags |= CORE_SECO << BOOT_IMG_FLAGS_CORE_SHIFT;
566 tmp_name = "SECO";
567 img->dst = 0x20C00000;
568 img->entry = 0x20000000;
569 break;
Peng Faneeb23bf2022-07-26 16:41:19 +0800570 case SENTINEL:
571 if (container->num_images > 0) {
572 fprintf(stderr, "Error: SENTINEL container only allows 1 image\n");
573 return;
574 }
575
576 img->hab_flags |= IMG_TYPE_SENTINEL;
577 img->hab_flags |= CORE_ULP_SENTINEL << BOOT_IMG_FLAGS_CORE_SHIFT;
578 tmp_name = "SENTINEL";
579 img->dst = 0xe4000000; /* S400 IRAM base */
580 img->entry = 0xe4000000;
581 break;
Peng Fan60a56072018-10-16 04:50:30 +0000582 case AP:
583 if (soc == QX && core == CORE_CA35) {
584 meta = IMAGE_A35_DEFAULT_META(custom_partition);
585 } else if (soc == QM && core == CORE_CA53) {
586 meta = IMAGE_A53_DEFAULT_META(custom_partition);
587 } else if (soc == QM && core == CORE_CA72) {
588 meta = IMAGE_A72_DEFAULT_META(custom_partition);
Peng Faneeb23bf2022-07-26 16:41:19 +0800589 } else if (((soc == ULP) || (soc == IMX9)) && core == CORE_CA35) {
590 meta = 0;
Peng Fan60a56072018-10-16 04:50:30 +0000591 } else {
Heinrich Schuchardt819abf82018-12-17 10:22:21 +0100592 fprintf(stderr,
593 "Error: invalid AP core id: %" PRIu64 "\n",
Peng Fan60a56072018-10-16 04:50:30 +0000594 core);
595 exit(EXIT_FAILURE);
596 }
597 img->hab_flags |= IMG_TYPE_EXEC;
Peng Faneeb23bf2022-07-26 16:41:19 +0800598 if ((soc == ULP) || (soc == IMX9))
599 img->hab_flags |= CORE_ULP_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
600 else
601 img->hab_flags |= CORE_CA53 << BOOT_IMG_FLAGS_CORE_SHIFT; /* On B0, only core id = 4 is valid */
Peng Fan60a56072018-10-16 04:50:30 +0000602 tmp_name = "AP";
603 img->dst = entry;
604 img->entry = entry;
605 img->meta = meta;
606 custom_partition = 0;
607 break;
608 case M40:
609 case M41:
Peng Faneeb23bf2022-07-26 16:41:19 +0800610 if ((soc == ULP) || (soc == IMX9)) {
611 core = CORE_ULP_CM33;
612 meta = 0;
Peng Fan60a56072018-10-16 04:50:30 +0000613 } else {
Peng Faneeb23bf2022-07-26 16:41:19 +0800614 if (core == 0) {
615 core = CORE_CM4_0;
616 meta = IMAGE_M4_0_DEFAULT_META(custom_partition);
617 } else if (core == 1) {
618 core = CORE_CM4_1;
619 meta = IMAGE_M4_1_DEFAULT_META(custom_partition);
620 } else {
621 fprintf(stderr,
622 "Error: invalid m4 core id: %" PRIu64 "\n",
623 core);
624 exit(EXIT_FAILURE);
625 }
Peng Fan60a56072018-10-16 04:50:30 +0000626 }
627 img->hab_flags |= IMG_TYPE_EXEC;
628 img->hab_flags |= core << BOOT_IMG_FLAGS_CORE_SHIFT;
629 tmp_name = "M4";
630 if ((entry & 0x7) != 0) {
631 fprintf(stderr, "\n\nWarning: M4 Destination address is not 8 byte aligned\n\n");
632 exit(EXIT_FAILURE);
633 }
634 img->dst = entry;
635 img->entry = entry;
636 img->meta = meta;
637 custom_partition = 0;
638 break;
639 case DATA:
640 img->hab_flags |= IMG_TYPE_DATA;
Peng Faneeb23bf2022-07-26 16:41:19 +0800641 if ((soc == ULP) || (soc == IMX9)) {
642 if (core == CORE_CM4_0)
643 img->hab_flags |= CORE_ULP_CM33 << BOOT_IMG_FLAGS_CORE_SHIFT;
644 else
645 img->hab_flags |= CORE_ULP_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
646 } else {
647 img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
648 }
Peng Fan60a56072018-10-16 04:50:30 +0000649 tmp_name = "DATA";
650 img->dst = entry;
651 break;
652 case MSG_BLOCK:
653 img->hab_flags |= IMG_TYPE_DATA;
654 img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
655 img->meta = core << BOOT_IMG_META_MU_RID_SHIFT;
656 tmp_name = "MSG_BLOCK";
657 img->dst = entry;
658 break;
659 case SCFW:
660 img->hab_flags |= scfw_flags & 0xFFFF0000;
661 img->hab_flags |= IMG_TYPE_EXEC;
662 img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
663 tmp_name = "SCFW";
664 img->dst = 0x1FFE0000;
665 img->entry = 0x1FFE0000;
666
667 /* Lets add the DCD now */
668 if (!dcd_skip) {
669 container->num_images++;
670 img = &container->img[container->num_images];
671 img->hab_flags |= IMG_TYPE_DCD_DDR;
672 img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
673 set_image_hash(img, "/dev/null",
674 IMAGE_HASH_ALGO_DEFAULT);
675 img->offset = offset + img->size;
676 img->entry = read_dcd_offset(tmp_filename);
677 img->dst = img->entry - 1;
678 }
679 break;
Peng Faneeb23bf2022-07-26 16:41:19 +0800680 case UPOWER:
681 if (soc == ULP) {
682 img->hab_flags |= IMG_TYPE_EXEC;
683 img->hab_flags |= CORE_ULP_UPOWER << BOOT_IMG_FLAGS_CORE_SHIFT;
684 tmp_name = "UPOWER";
685 img->dst = 0x28300200; /* UPOWER code RAM */
686 img->entry = 0x28300200;
687 }
688 break;
Peng Fan60a56072018-10-16 04:50:30 +0000689 default:
690 fprintf(stderr, "unrecognized image type (%d)\n", type);
691 exit(EXIT_FAILURE);
692 }
693
694 fprintf(stdout, "%s file_offset = 0x%x size = 0x%x\n", tmp_name, offset, size);
695
696 container->num_images++;
697}
698
699void set_container(flash_header_v3_t *container, uint16_t sw_version,
700 uint32_t alignment, uint32_t flags, uint16_t fuse_version)
701{
702 container->sig_blk_hdr.tag = 0x90;
703 container->sig_blk_hdr.length = sizeof(sig_blk_hdr_t);
704 container->sw_version = sw_version;
705 container->padding = alignment;
706 container->fuse_version = fuse_version;
707 container->flags = flags;
708 fprintf(stdout, "container flags: 0x%x\n", container->flags);
709}
710
711static int get_container_image_start_pos(image_t *image_stack, uint32_t align)
712{
713 image_t *img_sp = image_stack;
714 /*8K total container header*/
715 int file_off = CONTAINER_IMAGE_ARRAY_START_OFFSET;
716 FILE *fd = NULL;
717 flash_header_v3_t header;
718 int ret;
719
720 while (img_sp->option != NO_IMG) {
721 if (img_sp->option == APPEND) {
722 fd = fopen(img_sp->filename, "r");
723 if (!fd) {
724 fprintf(stderr, "Fail open first container file %s\n", img_sp->filename);
725 exit(EXIT_FAILURE);
726 }
727
728 ret = fread(&header, sizeof(header), 1, fd);
Peng Fand65dcfd2018-11-05 09:53:25 +0000729 if (ret != 1) {
Peng Fan60a56072018-10-16 04:50:30 +0000730 printf("Failure Read header %d\n", ret);
Peng Fand65dcfd2018-11-05 09:53:25 +0000731 exit(EXIT_FAILURE);
732 }
Peng Fan60a56072018-10-16 04:50:30 +0000733
734 fclose(fd);
735
736 if (header.tag != IVT_HEADER_TAG_B0) {
Vagrant Cascadian31860f12019-01-08 13:10:23 -0800737 fprintf(stderr, "header tag mismatched \n");
Peng Fan60a56072018-10-16 04:50:30 +0000738 exit(EXIT_FAILURE);
739 } else {
740 file_off +=
741 header.img[header.num_images - 1].size;
742 file_off = ALIGN(file_off, align);
743 }
744 }
745
746 img_sp++;
747 }
748
749 return file_off;
750}
751
752static void set_imx_hdr_v3(imx_header_v3_t *imxhdr, uint32_t cont_id)
753{
754 flash_header_v3_t *fhdr_v3 = &imxhdr->fhdr[cont_id];
755
756 /* Set magic number, Only >= B0 supported */
757 fhdr_v3->tag = IVT_HEADER_TAG_B0;
758 fhdr_v3->version = IVT_VERSION_B0;
759}
760
761static uint8_t *flatten_container_header(imx_header_v3_t *imx_header,
762 uint8_t containers_count,
763 uint32_t *size_out,
764 uint32_t file_offset)
765{
766 uint8_t *flat = NULL;
767 uint8_t *ptr = NULL;
768 uint16_t size = 0;
769 int i, j;
770
771 /* Compute size of all container headers */
772 for (i = 0; i < containers_count; i++) {
773 flash_header_v3_t *container = &imx_header->fhdr[i];
774
775 container->sig_blk_offset = HEADER_IMG_ARRAY_OFFSET +
776 container->num_images * IMG_ARRAY_ENTRY_SIZE;
777
778 container->length = HEADER_IMG_ARRAY_OFFSET +
779 (IMG_ARRAY_ENTRY_SIZE * container->num_images) +
780 sizeof(sig_blk_hdr_t);
781
782 /* Print info needed by CST to sign the container header */
783 fprintf(stdout, "CST: CONTAINER %d offset: 0x%x\n",
784 i, file_offset + size);
785 fprintf(stdout, "CST: CONTAINER %d: Signature Block: offset is at 0x%x\n", i,
786 file_offset + size + container->length -
787 SIGNATURE_BLOCK_HEADER_LENGTH);
788
789 size += ALIGN(container->length, container->padding);
790 }
791
792 flat = calloc(size, sizeof(uint8_t));
793 if (!flat) {
794 fprintf(stderr, "Failed to allocate memory (%d)\n", size);
795 exit(EXIT_FAILURE);
796 }
797
798 ptr = flat;
799 *size_out = size;
800
801 for (i = 0; i < containers_count; i++) {
802 flash_header_v3_t *container = &imx_header->fhdr[i];
803 uint32_t container_start_offset = ptr - flat;
804
805 /* Append container header */
806 append(ptr, container, HEADER_IMG_ARRAY_OFFSET);
807
808 /* Adjust images offset to start from container headers start */
809 for (j = 0; j < container->num_images; j++) {
810 container->img[j].offset -=
811 container_start_offset + file_offset;
812 }
813 /* Append each image array entry */
814 for (j = 0; j < container->num_images; j++)
815 append(ptr, &container->img[j], sizeof(boot_img_t));
816
817 append(ptr, &container->sig_blk_hdr, sizeof(sig_blk_hdr_t));
818
819 /* Padding for container (if necessary) */
820 ptr += ALIGN(container->length, container->padding) -
821 container->length;
822 }
823
824 return flat;
825}
826
827static int build_container(soc_type_t soc, uint32_t sector_size,
828 bool emmc_fastboot, image_t *image_stack,
829 bool dcd_skip, uint8_t fuse_version,
830 uint16_t sw_version, int ofd)
831{
832 static imx_header_v3_t imx_header;
833 image_t *img_sp = image_stack;
834 int file_off;
835 uint8_t *tmp;
836 struct stat sbuf;
837 char *tmp_filename = NULL;
838 uint32_t size = 0;
839 uint32_t file_padding = 0;
Peng Fan00c2dd52018-11-05 09:53:22 +0000840 int ret;
Peng Fan60a56072018-10-16 04:50:30 +0000841
842 int container = -1;
Peng Fan60a56072018-10-16 04:50:30 +0000843
844 memset((char *)&imx_header, 0, sizeof(imx_header_v3_t));
845
846 if (!image_stack) {
847 fprintf(stderr, "Empty image stack ");
848 exit(EXIT_FAILURE);
849 }
850
851 if (soc == QX)
852 fprintf(stdout, "Platform:\ti.MX8QXP B0\n");
853 else if (soc == QM)
854 fprintf(stdout, "Platform:\ti.MX8QM B0\n");
Peng Faneeb23bf2022-07-26 16:41:19 +0800855 else if (soc == ULP)
856 fprintf(stdout, "Platform:\ti.MX8ULP A0\n");
857 else if (soc == IMX9)
858 fprintf(stdout, "Platform:\ti.MX9\n");
Peng Fan60a56072018-10-16 04:50:30 +0000859
860 set_imx_hdr_v3(&imx_header, 0);
861 set_imx_hdr_v3(&imx_header, 1);
862
863 file_off = get_container_image_start_pos(image_stack, sector_size);
864 fprintf(stdout, "container image offset (aligned):%x\n", file_off);
865
866 /* step through image stack and generate the header */
867 img_sp = image_stack;
868
869 /* stop once we reach null terminator */
870 while (img_sp->option != NO_IMG) {
871 switch (img_sp->option) {
872 case AP:
873 case M40:
874 case M41:
875 case SCFW:
876 case DATA:
Peng Faneeb23bf2022-07-26 16:41:19 +0800877 case UPOWER:
Peng Fan60a56072018-10-16 04:50:30 +0000878 case MSG_BLOCK:
Peng Fanf107b0f2018-11-05 09:53:28 +0000879 if (container < 0) {
880 fprintf(stderr, "No container found\n");
881 exit(EXIT_FAILURE);
882 }
Peng Fan60a56072018-10-16 04:50:30 +0000883 check_file(&sbuf, img_sp->filename);
884 tmp_filename = img_sp->filename;
885 set_image_array_entry(&imx_header.fhdr[container],
886 soc, img_sp, file_off,
887 ALIGN(sbuf.st_size, sector_size),
888 tmp_filename, dcd_skip);
889 img_sp->src = file_off;
890
891 file_off += ALIGN(sbuf.st_size, sector_size);
Peng Fan60a56072018-10-16 04:50:30 +0000892 break;
893
894 case SECO:
Peng Faneeb23bf2022-07-26 16:41:19 +0800895 case SENTINEL:
Peng Fanf107b0f2018-11-05 09:53:28 +0000896 if (container < 0) {
897 fprintf(stderr, "No container found\n");
898 exit(EXIT_FAILURE);
899 }
Peng Fan60a56072018-10-16 04:50:30 +0000900 check_file(&sbuf, img_sp->filename);
901 tmp_filename = img_sp->filename;
902 set_image_array_entry(&imx_header.fhdr[container],
903 soc,
904 img_sp,
905 file_off,
906 sbuf.st_size,
907 tmp_filename, dcd_skip);
908 img_sp->src = file_off;
909
910 file_off += sbuf.st_size;
Peng Fan60a56072018-10-16 04:50:30 +0000911 break;
912
913 case NEW_CONTAINER:
914 container++;
915 set_container(&imx_header.fhdr[container], sw_version,
916 CONTAINER_ALIGNMENT,
917 CONTAINER_FLAGS_DEFAULT,
918 fuse_version);
Peng Fan60a56072018-10-16 04:50:30 +0000919 scfw_flags = 0;
920 break;
921
922 case APPEND:
923 /*
924 * nothing to do here, the container is appended
925 * in the output
926 */
927 break;
928 case FLAG:
929 /*
930 * override the flags for scfw in current container
931 * mask off bottom 16 bits.
932 */
933 scfw_flags = img_sp->entry & 0xFFFF0000;
934 break;
935 case FILEOFF:
936 if (file_off > img_sp->dst) {
937 fprintf(stderr, "FILEOFF address less than current file offset!!!\n");
938 exit(EXIT_FAILURE);
939 }
940 if (img_sp->dst != ALIGN(img_sp->dst, sector_size)) {
941 fprintf(stderr, "FILEOFF address is not aligned to sector size!!!\n");
942 exit(EXIT_FAILURE);
943 }
944 file_off = img_sp->dst;
945 break;
946 case PARTITION:
947 /*
948 * keep custom partition until next executable image
949 * use a global var for default behaviour
950 */
951 custom_partition = img_sp->entry;
952 break;
953 default:
954 fprintf(stderr, "unrecognized option in input stack (%d)\n", img_sp->option);
955 exit(EXIT_FAILURE);
956 }
957 img_sp++; /* advance index */
958 }
959
960 /* Append container (if specified) */
961 img_sp = image_stack;
962 do {
963 if (img_sp->option == APPEND) {
964 copy_file(ofd, img_sp->filename, 0, 0);
965 file_padding += FIRST_CONTAINER_HEADER_LENGTH;
966 }
967 img_sp++;
968 } while (img_sp->option != NO_IMG);
969
970 /* Add padding or skip appended container */
Peng Fan00c2dd52018-11-05 09:53:22 +0000971 ret = lseek(ofd, file_padding, SEEK_SET);
972 if (ret < 0) {
973 fprintf(stderr, "%s: lseek error %s\n",
974 __func__, strerror(errno));
975 exit(EXIT_FAILURE);
976 }
Peng Fan60a56072018-10-16 04:50:30 +0000977
Peng Fan18864852018-11-05 09:53:31 +0000978 if (container >= 0) {
979 /* Note: Image offset are not contained in the image */
980 tmp = flatten_container_header(&imx_header, container + 1,
981 &size, file_padding);
982 /* Write image header */
983 if (write(ofd, tmp, size) != size) {
984 fprintf(stderr, "error writing image hdr\n");
985 exit(EXIT_FAILURE);
986 }
Peng Fan60a56072018-10-16 04:50:30 +0000987
Peng Fan18864852018-11-05 09:53:31 +0000988 /* Clean-up memory used by the headers */
989 free(tmp);
990 }
Peng Fan60a56072018-10-16 04:50:30 +0000991
992 /*
993 * step through the image stack again this time copying
994 * images to final bin, stop once we reach null terminator.
995 */
996 img_sp = image_stack;
997 while (img_sp->option != NO_IMG) {
998 if (img_sp->option == M40 || img_sp->option == M41 ||
999 img_sp->option == AP || img_sp->option == DATA ||
1000 img_sp->option == SCD || img_sp->option == SCFW ||
Peng Faneeb23bf2022-07-26 16:41:19 +08001001 img_sp->option == SECO || img_sp->option == MSG_BLOCK ||
1002 img_sp->option == UPOWER || img_sp->option == SENTINEL) {
Peng Fan60a56072018-10-16 04:50:30 +00001003 copy_file_aligned(ofd, img_sp->filename, img_sp->src,
1004 sector_size);
1005 }
1006 img_sp++;
1007 }
1008
1009 return 0;
1010}
1011
1012int imx8image_copy_image(int outfd, struct image_tool_params *mparams)
1013{
1014 image_t *img_sp = param_stack;
1015
1016 /*
1017 * SECO FW is a container image, this is to calculate the
1018 * 2nd container offset.
1019 */
1020 fprintf(stdout, "parsing %s\n", mparams->imagename);
1021 parse_cfg_file(img_sp, mparams->imagename);
1022
1023 if (sector_size == 0) {
1024 fprintf(stderr, "Wrong sector size\n");
1025 exit(EXIT_FAILURE);
1026 }
1027
1028 fprintf(stdout, "CONTAINER Sector size:\t%08x\n", sector_size);
1029 fprintf(stdout, "CONTAINER FUSE VERSION:\t0x%02x\n", fuse_version);
1030 fprintf(stdout, "CONTAINER SW VERSION:\t0x%04x\n", sw_version);
1031
1032 build_container(soc, sector_size, emmc_fastboot,
Heiko Schocherfd61e712024-11-04 19:02:03 +01001033 img_sp, dcd_skip, fuse_version, sw_version, outfd);
Peng Fan60a56072018-10-16 04:50:30 +00001034
1035 return 0;
1036}
1037
1038/*
1039 * imx8image parameters
1040 */
1041U_BOOT_IMAGE_TYPE(
1042 imx8image,
1043 "NXP i.MX8 Boot Image support",
1044 0,
1045 NULL,
1046 imx8image_check_params,
1047 NULL,
1048 imx8image_print_header,
1049 imx8image_set_header,
1050 NULL,
1051 imx8image_check_image_types,
1052 NULL,
1053 NULL
1054);