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