blob: 395d5c64bdf005afe68bb10ee05d845cd261bcea [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
33static void imx8image_print_header(const void *ptr)
34{
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", },
60 {CFG_M40, "M40", "M4 core 0", },
61 {CFG_M41, "M41", "M4 core 1", },
62 {CFG_A35, "A35", "A35 core", },
Peng Faneeb23bf2022-07-26 16:41:19 +080063 {CFG_A55, "A55", "A55 core", },
Peng Fan60a56072018-10-16 04:50:30 +000064 {CFG_A53, "A53", "A53 core", },
65 {CFG_A72, "A72", "A72 core", },
66 {-1, "", "", },
67};
68
69static table_entry_t imx8image_sector_size[] = {
70 {0x400, "sd", "sd/emmc",},
71 {0x400, "emmc_fastboot", "emmc fastboot",},
72 {0x400, "fspi", "flexspi", },
73 {0x1000, "nand_4k", "nand 4K", },
74 {0x2000, "nand_8k", "nand 8K", },
75 {0x4000, "nand_16k", "nand 16K", },
76 {-1, "", "Invalid", },
77};
78
79static void parse_cfg_cmd(image_t *param_stack, int32_t cmd, char *token,
80 char *name, int lineno)
81{
82 switch (cmd) {
83 case CMD_BOOT_FROM:
84 sector_size = get_table_entry_id(imx8image_sector_size,
85 "imximage boot option",
86 token);
87 if (!strncmp("emmc_fastboot", token, 13))
88 emmc_fastboot = true;
89 break;
90 case CMD_FUSE_VERSION:
91 fuse_version = (uint8_t)(strtoll(token, NULL, 0) & 0xFF);
92 break;
93 case CMD_SW_VERSION:
94 sw_version = (uint8_t)(strtoll(token, NULL, 0) & 0xFFFF);
95 break;
96 case CMD_FILEOFF:
97 param_stack[p_idx].option = FILEOFF;
98 param_stack[p_idx++].dst = (uint32_t)strtoll(token, NULL, 0);
99 break;
100 case CMD_MSG_BLOCK:
101 param_stack[p_idx].option = MSG_BLOCK;
102 param_stack[p_idx].filename = token;
103 break;
104 case CMD_FLAG:
105 param_stack[p_idx].option = FLAG;
106 param_stack[p_idx++].entry = (uint32_t)strtoll(token, NULL, 0);
107 break;
108 case CMD_APPEND:
109 param_stack[p_idx].option = APPEND;
110 param_stack[p_idx++].filename = token;
111 break;
112 case CMD_PARTITION:
113 param_stack[p_idx].option = PARTITION;
114 param_stack[p_idx++].entry = (uint32_t)strtoll(token, NULL, 0);
115 break;
116 case CMD_SOC_TYPE:
117 if (!strncmp(token, "IMX8QX", 6)) {
118 soc = QX;
119 } else if (!strncmp(token, "IMX8QM", 6)) {
120 soc = QM;
Peng Faneeb23bf2022-07-26 16:41:19 +0800121 } else if (!strncmp(token, "ULP", 3)) {
122 soc = IMX9;
123 } else if (!strncmp(token, "IMX9", 4)) {
124 soc = IMX9;
Peng Fan60a56072018-10-16 04:50:30 +0000125 } else {
126 fprintf(stderr, "Unknown CMD_SOC_TYPE");
127 exit(EXIT_FAILURE);
128 }
129 break;
130 case CMD_IMAGE:
131 case CMD_DATA:
132 core_type = get_table_entry_id(imx8image_core_entries,
133 "imx8image core entries",
134 token);
135 if (core_type < 0) {
136 fprintf(stderr, "Wrong IMAGE core_type %s\n", token);
137 exit(EXIT_FAILURE);
138 }
139 break;
140 default:
141 break;
142 }
143}
144
145static void parse_cfg_fld(image_t *param_stack, int32_t *cmd, char *token,
146 char *name, int lineno, int fld)
147{
148 switch (fld) {
149 case CFG_COMMAND:
150 *cmd = get_table_entry_id(imx8image_cmds, "imx8image cmds",
151 token);
152 if (*cmd < 0) {
153 fprintf(stderr, "Error: %s[%d] - Invalid command (%s)\n", name, lineno, token);
154 exit(EXIT_FAILURE);
155 }
156
157 if (*cmd == CMD_CONTAINER) {
158 fprintf(stdout, "New Container: \t%d\n", ++container);
159 param_stack[p_idx++].option = NEW_CONTAINER;
160 }
161 break;
162 case CFG_CORE_TYPE:
163 parse_cfg_cmd(param_stack, *cmd, token, name, lineno);
164 break;
165 case CFG_IMAGE_NAME:
166 if (*cmd == CMD_MSG_BLOCK) {
167 if (!strncmp(token, "fuse", 4)) {
168 param_stack[p_idx].ext = SC_R_OTP;
169 } else if (!strncmp(token, "debug", 5)) {
170 param_stack[p_idx].ext = SC_R_DEBUG;
171 } else if (!strncmp(token, "field", 5)) {
172 param_stack[p_idx].ext = SC_R_ROM_0;
173 } else {
174 fprintf(stderr, "MSG type not found %s\n", token);
175 exit(EXIT_FAILURE);
176 }
177 break;
178 }
179 switch (core_type) {
180 case CFG_SCU:
181 param_stack[p_idx].option = SCFW;
182 param_stack[p_idx++].filename = token;
183 break;
184 case CFG_M40:
185 param_stack[p_idx].option = M40;
186 param_stack[p_idx].ext = 0;
187 param_stack[p_idx].filename = token;
188 break;
189 case CFG_M41:
190 param_stack[p_idx].option = M41;
191 param_stack[p_idx].ext = 1;
192 param_stack[p_idx].filename = token;
193 break;
194 case CFG_A35:
Peng Faneeb23bf2022-07-26 16:41:19 +0800195 case CFG_A55:
Peng Fan60a56072018-10-16 04:50:30 +0000196 param_stack[p_idx].ext = CORE_CA35;
197 param_stack[p_idx].option =
198 (*cmd == CMD_DATA) ? DATA : AP;
199 param_stack[p_idx].filename = token;
200 break;
201 case CFG_A53:
202 param_stack[p_idx].ext = CORE_CA53;
203 param_stack[p_idx].option =
204 (*cmd == CMD_DATA) ? DATA : AP;
205 param_stack[p_idx].filename = token;
206 break;
207 case CFG_A72:
208 param_stack[p_idx].ext = CORE_CA72;
209 param_stack[p_idx].option =
210 (*cmd == CMD_DATA) ? DATA : AP;
211 param_stack[p_idx].filename = token;
212 break;
213 }
214 break;
215 case CFG_LOAD_ADDR:
216 if (*cmd == CMD_MSG_BLOCK) {
217 param_stack[p_idx++].entry =
218 (uint32_t)strtoll(token, NULL, 0);
219 break;
220 }
221 switch (core_type) {
222 case CFG_SCU:
223 break;
224 case CFG_M40:
225 case CFG_M41:
226 case CFG_A35:
227 case CFG_A53:
Peng Faneeb23bf2022-07-26 16:41:19 +0800228 case CFG_A55:
Peng Fan60a56072018-10-16 04:50:30 +0000229 case CFG_A72:
230 param_stack[p_idx++].entry =
231 (uint32_t)strtoll(token, NULL, 0);
232 break;
233 }
234 default:
235 break;
236 }
237}
238
239static uint32_t parse_cfg_file(image_t *param_stack, char *name)
240{
241 FILE *fd = NULL;
242 char *line = NULL;
243 char *token, *saveptr1, *saveptr2;
244 int lineno = 0;
245 int fld;
246 size_t len;
247 int32_t cmd;
248
249 fd = fopen(name, "r");
250 if (fd == 0) {
251 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
252 exit(EXIT_FAILURE);
253 }
254
255 /*
256 * Very simple parsing, line starting with # are comments
257 * and are dropped
258 */
259 while ((getline(&line, &len, fd)) > 0) {
260 lineno++;
261
262 token = strtok_r(line, "\r\n", &saveptr1);
263 if (!token)
264 continue;
265
266 /* Check inside the single line */
267 for (fld = CFG_COMMAND, cmd = CFG_INVALID,
268 line = token; ; line = NULL, fld++) {
269 token = strtok_r(line, " \t", &saveptr2);
270 if (!token)
271 break;
272
273 /* Drop all text starting with '#' as comments */
274 if (token[0] == '#')
275 break;
276
277 parse_cfg_fld(param_stack, &cmd, token, name, lineno,
278 fld);
279 }
280 }
281
Mikhail Ilin7ed5b722022-11-23 13:59:49 +0300282 fclose(fd);
Peng Fan60a56072018-10-16 04:50:30 +0000283 return 0;
284}
285
286static void check_file(struct stat *sbuf, char *filename)
287{
288 int tmp_fd = open(filename, O_RDONLY | O_BINARY);
289
290 if (tmp_fd < 0) {
291 fprintf(stderr, "%s: Can't open: %s\n",
292 filename, strerror(errno));
293 exit(EXIT_FAILURE);
294 }
295
296 if (fstat(tmp_fd, sbuf) < 0) {
297 fprintf(stderr, "%s: Can't stat: %s\n",
298 filename, strerror(errno));
299 exit(EXIT_FAILURE);
300 }
301
302 close(tmp_fd);
303}
304
305static void copy_file_aligned(int ifd, const char *datafile, int offset,
306 int align)
307{
308 int dfd;
309 struct stat sbuf;
310 unsigned char *ptr;
311 uint8_t zeros[0x4000];
312 int size;
Peng Fan00c2dd52018-11-05 09:53:22 +0000313 int ret;
Peng Fan60a56072018-10-16 04:50:30 +0000314
315 if (align > 0x4000) {
316 fprintf(stderr, "Wrong alignment requested %d\n", align);
317 exit(EXIT_FAILURE);
318 }
319
320 memset(zeros, 0, sizeof(zeros));
321
322 dfd = open(datafile, O_RDONLY | O_BINARY);
323 if (dfd < 0) {
324 fprintf(stderr, "Can't open %s: %s\n",
325 datafile, strerror(errno));
326 exit(EXIT_FAILURE);
327 }
328
329 if (fstat(dfd, &sbuf) < 0) {
330 fprintf(stderr, "Can't stat %s: %s\n",
331 datafile, strerror(errno));
332 exit(EXIT_FAILURE);
333 }
334
335 if (sbuf.st_size == 0)
336 goto close;
337
338 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
339 if (ptr == MAP_FAILED) {
340 fprintf(stderr, "Can't read %s: %s\n",
341 datafile, strerror(errno));
342 exit(EXIT_FAILURE);
343 }
344
345 size = sbuf.st_size;
Peng Fan00c2dd52018-11-05 09:53:22 +0000346 ret = lseek(ifd, offset, SEEK_SET);
347 if (ret < 0) {
348 fprintf(stderr, "%s: lseek error %s\n",
349 __func__, strerror(errno));
350 exit(EXIT_FAILURE);
351 }
352
Peng Fan60a56072018-10-16 04:50:30 +0000353 if (write(ifd, ptr, size) != size) {
354 fprintf(stderr, "Write error %s\n", strerror(errno));
355 exit(EXIT_FAILURE);
356 }
357
358 align = ALIGN(size, align) - size;
359
360 if (write(ifd, (char *)&zeros, align) != align) {
361 fprintf(stderr, "Write error: %s\n", strerror(errno));
362 exit(EXIT_FAILURE);
363 }
364
365 munmap((void *)ptr, sbuf.st_size);
366close:
367 close(dfd);
368}
369
370static void copy_file (int ifd, const char *datafile, int pad, int offset)
371{
372 int dfd;
373 struct stat sbuf;
374 unsigned char *ptr;
375 int tail;
Peng Fane3ed0cc2021-03-19 15:56:53 +0800376 uint64_t zero = 0;
Peng Fan60a56072018-10-16 04:50:30 +0000377 uint8_t zeros[4096];
Peng Fan00c2dd52018-11-05 09:53:22 +0000378 int size, ret;
Peng Fan60a56072018-10-16 04:50:30 +0000379
380 memset(zeros, 0, sizeof(zeros));
381
382 dfd = open(datafile, O_RDONLY | O_BINARY);
383 if (dfd < 0) {
384 fprintf(stderr, "Can't open %s: %s\n",
385 datafile, strerror(errno));
386 exit(EXIT_FAILURE);
387 }
388
389 if (fstat(dfd, &sbuf) < 0) {
390 fprintf(stderr, "Can't stat %s: %s\n",
391 datafile, strerror(errno));
392 exit(EXIT_FAILURE);
393 }
394
395 if (sbuf.st_size == 0)
396 goto close;
397
398 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
399 if (ptr == MAP_FAILED) {
400 fprintf(stderr, "Can't read %s: %s\n",
401 datafile, strerror(errno));
402 exit(EXIT_FAILURE);
403 }
404
405 size = sbuf.st_size;
Peng Fan00c2dd52018-11-05 09:53:22 +0000406 ret = lseek(ifd, offset, SEEK_SET);
407 if (ret < 0) {
408 fprintf(stderr, "%s: lseek error %s\n",
409 __func__, strerror(errno));
410 exit(EXIT_FAILURE);
411 }
412
Peng Fan60a56072018-10-16 04:50:30 +0000413 if (write(ifd, ptr, size) != size) {
414 fprintf(stderr, "Write error %s\n",
415 strerror(errno));
416 exit(EXIT_FAILURE);
417 }
418
419 tail = size % 4;
420 pad = pad - size;
421 if (pad == 1 && tail != 0) {
422 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
423 fprintf(stderr, "Write error on %s\n",
424 strerror(errno));
425 exit(EXIT_FAILURE);
426 }
427 } else if (pad > 1) {
428 while (pad > 0) {
429 int todo = sizeof(zeros);
430
431 if (todo > pad)
432 todo = pad;
433 if (write(ifd, (char *)&zeros, todo) != todo) {
434 fprintf(stderr, "Write error: %s\n",
435 strerror(errno));
436 exit(EXIT_FAILURE);
437 }
438 pad -= todo;
439 }
440 }
441
442 munmap((void *)ptr, sbuf.st_size);
443close:
444 close(dfd);
445}
446
447uint64_t read_dcd_offset(char *filename)
448{
449 int dfd;
450 struct stat sbuf;
451 uint8_t *ptr;
452 uint64_t offset = 0;
453
454 dfd = open(filename, O_RDONLY | O_BINARY);
455 if (dfd < 0) {
456 fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno));
457 exit(EXIT_FAILURE);
458 }
459
460 if (fstat(dfd, &sbuf) < 0) {
461 fprintf(stderr, "Can't stat %s: %s\n", filename, strerror(errno));
462 exit(EXIT_FAILURE);
463 }
464
465 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
466 if (ptr == MAP_FAILED) {
467 fprintf(stderr, "Can't read %s: %s\n", filename, strerror(errno));
468 exit(EXIT_FAILURE);
469 }
470
471 offset = *(uint32_t *)(ptr + DCD_ENTRY_ADDR_IN_SCFW);
472
473 munmap((void *)ptr, sbuf.st_size);
474 close(dfd);
475
476 return offset;
477}
478
479static void set_image_hash(boot_img_t *img, char *filename, uint32_t hash_type)
480{
481 FILE *fp = NULL;
482 char sha_command[512];
483 char hash[2 * HASH_MAX_LEN + 1];
484 int i, ret;
485
486 if (img->size == 0)
487 sprintf(sha_command, "sha%dsum /dev/null", hash_type);
488 else
489 sprintf(sha_command, "dd if=/dev/zero of=tmp_pad bs=%d count=1;\
490 dd if=\'%s\' of=tmp_pad conv=notrunc;\
491 sha%dsum tmp_pad; rm -f tmp_pad",
492 img->size, filename, hash_type);
493
494 switch (hash_type) {
495 case HASH_TYPE_SHA_256:
496 img->hab_flags |= IMG_FLAG_HASH_SHA256;
497 break;
498 case HASH_TYPE_SHA_384:
499 img->hab_flags |= IMG_FLAG_HASH_SHA384;
500 break;
501 case HASH_TYPE_SHA_512:
502 img->hab_flags |= IMG_FLAG_HASH_SHA512;
503 break;
504 default:
505 fprintf(stderr, "Wrong hash type selected (%d) !!!\n\n",
506 hash_type);
507 exit(EXIT_FAILURE);
508 break;
509 }
510 memset(img->hash, 0, HASH_MAX_LEN);
511
512 fp = popen(sha_command, "r");
513 if (!fp) {
514 fprintf(stderr, "Failed to run command hash\n");
515 exit(EXIT_FAILURE);
516 }
517
518 if (!fgets(hash, hash_type / 4 + 1, fp)) {
519 fprintf(stderr, "Failed to hash file: %s\n", filename);
520 exit(EXIT_FAILURE);
521 }
522
523 for (i = 0; i < strlen(hash) / 2; i++) {
524 ret = sscanf(hash + 2 * i, "%02hhx", &img->hash[i]);
525 if (ret < 0) {
526 fprintf(stderr, "Failed sscanf hash: %d\n", ret);
527 exit(EXIT_FAILURE);
528 }
529 }
530
531 pclose(fp);
532}
533
534static void set_image_array_entry(flash_header_v3_t *container,
535 soc_type_t soc, const image_t *image_stack,
536 uint32_t offset, uint32_t size,
537 char *tmp_filename, bool dcd_skip)
538{
539 uint64_t entry = image_stack->entry;
540 uint64_t core = image_stack->ext;
541 uint32_t meta;
542 char *tmp_name = "";
543 option_type_t type = image_stack->option;
544 boot_img_t *img = &container->img[container->num_images];
545
546 img->offset = offset; /* Is re-adjusted later */
547 img->size = size;
548
549 set_image_hash(img, tmp_filename, IMAGE_HASH_ALGO_DEFAULT);
550
551 switch (type) {
552 case SECO:
553 img->hab_flags |= IMG_TYPE_SECO;
554 img->hab_flags |= CORE_SECO << BOOT_IMG_FLAGS_CORE_SHIFT;
555 tmp_name = "SECO";
556 img->dst = 0x20C00000;
557 img->entry = 0x20000000;
558 break;
Peng Faneeb23bf2022-07-26 16:41:19 +0800559 case SENTINEL:
560 if (container->num_images > 0) {
561 fprintf(stderr, "Error: SENTINEL container only allows 1 image\n");
562 return;
563 }
564
565 img->hab_flags |= IMG_TYPE_SENTINEL;
566 img->hab_flags |= CORE_ULP_SENTINEL << BOOT_IMG_FLAGS_CORE_SHIFT;
567 tmp_name = "SENTINEL";
568 img->dst = 0xe4000000; /* S400 IRAM base */
569 img->entry = 0xe4000000;
570 break;
Peng Fan60a56072018-10-16 04:50:30 +0000571 case AP:
572 if (soc == QX && core == CORE_CA35) {
573 meta = IMAGE_A35_DEFAULT_META(custom_partition);
574 } else if (soc == QM && core == CORE_CA53) {
575 meta = IMAGE_A53_DEFAULT_META(custom_partition);
576 } else if (soc == QM && core == CORE_CA72) {
577 meta = IMAGE_A72_DEFAULT_META(custom_partition);
Peng Faneeb23bf2022-07-26 16:41:19 +0800578 } else if (((soc == ULP) || (soc == IMX9)) && core == CORE_CA35) {
579 meta = 0;
Peng Fan60a56072018-10-16 04:50:30 +0000580 } else {
Heinrich Schuchardt819abf82018-12-17 10:22:21 +0100581 fprintf(stderr,
582 "Error: invalid AP core id: %" PRIu64 "\n",
Peng Fan60a56072018-10-16 04:50:30 +0000583 core);
584 exit(EXIT_FAILURE);
585 }
586 img->hab_flags |= IMG_TYPE_EXEC;
Peng Faneeb23bf2022-07-26 16:41:19 +0800587 if ((soc == ULP) || (soc == IMX9))
588 img->hab_flags |= CORE_ULP_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
589 else
590 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 +0000591 tmp_name = "AP";
592 img->dst = entry;
593 img->entry = entry;
594 img->meta = meta;
595 custom_partition = 0;
596 break;
597 case M40:
598 case M41:
Peng Faneeb23bf2022-07-26 16:41:19 +0800599 if ((soc == ULP) || (soc == IMX9)) {
600 core = CORE_ULP_CM33;
601 meta = 0;
Peng Fan60a56072018-10-16 04:50:30 +0000602 } else {
Peng Faneeb23bf2022-07-26 16:41:19 +0800603 if (core == 0) {
604 core = CORE_CM4_0;
605 meta = IMAGE_M4_0_DEFAULT_META(custom_partition);
606 } else if (core == 1) {
607 core = CORE_CM4_1;
608 meta = IMAGE_M4_1_DEFAULT_META(custom_partition);
609 } else {
610 fprintf(stderr,
611 "Error: invalid m4 core id: %" PRIu64 "\n",
612 core);
613 exit(EXIT_FAILURE);
614 }
Peng Fan60a56072018-10-16 04:50:30 +0000615 }
616 img->hab_flags |= IMG_TYPE_EXEC;
617 img->hab_flags |= core << BOOT_IMG_FLAGS_CORE_SHIFT;
618 tmp_name = "M4";
619 if ((entry & 0x7) != 0) {
620 fprintf(stderr, "\n\nWarning: M4 Destination address is not 8 byte aligned\n\n");
621 exit(EXIT_FAILURE);
622 }
623 img->dst = entry;
624 img->entry = entry;
625 img->meta = meta;
626 custom_partition = 0;
627 break;
628 case DATA:
629 img->hab_flags |= IMG_TYPE_DATA;
Peng Faneeb23bf2022-07-26 16:41:19 +0800630 if ((soc == ULP) || (soc == IMX9)) {
631 if (core == CORE_CM4_0)
632 img->hab_flags |= CORE_ULP_CM33 << BOOT_IMG_FLAGS_CORE_SHIFT;
633 else
634 img->hab_flags |= CORE_ULP_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
635 } else {
636 img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
637 }
Peng Fan60a56072018-10-16 04:50:30 +0000638 tmp_name = "DATA";
639 img->dst = entry;
640 break;
641 case MSG_BLOCK:
642 img->hab_flags |= IMG_TYPE_DATA;
643 img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
644 img->meta = core << BOOT_IMG_META_MU_RID_SHIFT;
645 tmp_name = "MSG_BLOCK";
646 img->dst = entry;
647 break;
648 case SCFW:
649 img->hab_flags |= scfw_flags & 0xFFFF0000;
650 img->hab_flags |= IMG_TYPE_EXEC;
651 img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
652 tmp_name = "SCFW";
653 img->dst = 0x1FFE0000;
654 img->entry = 0x1FFE0000;
655
656 /* Lets add the DCD now */
657 if (!dcd_skip) {
658 container->num_images++;
659 img = &container->img[container->num_images];
660 img->hab_flags |= IMG_TYPE_DCD_DDR;
661 img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
662 set_image_hash(img, "/dev/null",
663 IMAGE_HASH_ALGO_DEFAULT);
664 img->offset = offset + img->size;
665 img->entry = read_dcd_offset(tmp_filename);
666 img->dst = img->entry - 1;
667 }
668 break;
Peng Faneeb23bf2022-07-26 16:41:19 +0800669 case UPOWER:
670 if (soc == ULP) {
671 img->hab_flags |= IMG_TYPE_EXEC;
672 img->hab_flags |= CORE_ULP_UPOWER << BOOT_IMG_FLAGS_CORE_SHIFT;
673 tmp_name = "UPOWER";
674 img->dst = 0x28300200; /* UPOWER code RAM */
675 img->entry = 0x28300200;
676 }
677 break;
Peng Fan60a56072018-10-16 04:50:30 +0000678 default:
679 fprintf(stderr, "unrecognized image type (%d)\n", type);
680 exit(EXIT_FAILURE);
681 }
682
683 fprintf(stdout, "%s file_offset = 0x%x size = 0x%x\n", tmp_name, offset, size);
684
685 container->num_images++;
686}
687
688void set_container(flash_header_v3_t *container, uint16_t sw_version,
689 uint32_t alignment, uint32_t flags, uint16_t fuse_version)
690{
691 container->sig_blk_hdr.tag = 0x90;
692 container->sig_blk_hdr.length = sizeof(sig_blk_hdr_t);
693 container->sw_version = sw_version;
694 container->padding = alignment;
695 container->fuse_version = fuse_version;
696 container->flags = flags;
697 fprintf(stdout, "container flags: 0x%x\n", container->flags);
698}
699
700static int get_container_image_start_pos(image_t *image_stack, uint32_t align)
701{
702 image_t *img_sp = image_stack;
703 /*8K total container header*/
704 int file_off = CONTAINER_IMAGE_ARRAY_START_OFFSET;
705 FILE *fd = NULL;
706 flash_header_v3_t header;
707 int ret;
708
709 while (img_sp->option != NO_IMG) {
710 if (img_sp->option == APPEND) {
711 fd = fopen(img_sp->filename, "r");
712 if (!fd) {
713 fprintf(stderr, "Fail open first container file %s\n", img_sp->filename);
714 exit(EXIT_FAILURE);
715 }
716
717 ret = fread(&header, sizeof(header), 1, fd);
Peng Fand65dcfd2018-11-05 09:53:25 +0000718 if (ret != 1) {
Peng Fan60a56072018-10-16 04:50:30 +0000719 printf("Failure Read header %d\n", ret);
Peng Fand65dcfd2018-11-05 09:53:25 +0000720 exit(EXIT_FAILURE);
721 }
Peng Fan60a56072018-10-16 04:50:30 +0000722
723 fclose(fd);
724
725 if (header.tag != IVT_HEADER_TAG_B0) {
Vagrant Cascadian31860f12019-01-08 13:10:23 -0800726 fprintf(stderr, "header tag mismatched \n");
Peng Fan60a56072018-10-16 04:50:30 +0000727 exit(EXIT_FAILURE);
728 } else {
729 file_off +=
730 header.img[header.num_images - 1].size;
731 file_off = ALIGN(file_off, align);
732 }
733 }
734
735 img_sp++;
736 }
737
738 return file_off;
739}
740
741static void set_imx_hdr_v3(imx_header_v3_t *imxhdr, uint32_t cont_id)
742{
743 flash_header_v3_t *fhdr_v3 = &imxhdr->fhdr[cont_id];
744
745 /* Set magic number, Only >= B0 supported */
746 fhdr_v3->tag = IVT_HEADER_TAG_B0;
747 fhdr_v3->version = IVT_VERSION_B0;
748}
749
750static uint8_t *flatten_container_header(imx_header_v3_t *imx_header,
751 uint8_t containers_count,
752 uint32_t *size_out,
753 uint32_t file_offset)
754{
755 uint8_t *flat = NULL;
756 uint8_t *ptr = NULL;
757 uint16_t size = 0;
758 int i, j;
759
760 /* Compute size of all container headers */
761 for (i = 0; i < containers_count; i++) {
762 flash_header_v3_t *container = &imx_header->fhdr[i];
763
764 container->sig_blk_offset = HEADER_IMG_ARRAY_OFFSET +
765 container->num_images * IMG_ARRAY_ENTRY_SIZE;
766
767 container->length = HEADER_IMG_ARRAY_OFFSET +
768 (IMG_ARRAY_ENTRY_SIZE * container->num_images) +
769 sizeof(sig_blk_hdr_t);
770
771 /* Print info needed by CST to sign the container header */
772 fprintf(stdout, "CST: CONTAINER %d offset: 0x%x\n",
773 i, file_offset + size);
774 fprintf(stdout, "CST: CONTAINER %d: Signature Block: offset is at 0x%x\n", i,
775 file_offset + size + container->length -
776 SIGNATURE_BLOCK_HEADER_LENGTH);
777
778 size += ALIGN(container->length, container->padding);
779 }
780
781 flat = calloc(size, sizeof(uint8_t));
782 if (!flat) {
783 fprintf(stderr, "Failed to allocate memory (%d)\n", size);
784 exit(EXIT_FAILURE);
785 }
786
787 ptr = flat;
788 *size_out = size;
789
790 for (i = 0; i < containers_count; i++) {
791 flash_header_v3_t *container = &imx_header->fhdr[i];
792 uint32_t container_start_offset = ptr - flat;
793
794 /* Append container header */
795 append(ptr, container, HEADER_IMG_ARRAY_OFFSET);
796
797 /* Adjust images offset to start from container headers start */
798 for (j = 0; j < container->num_images; j++) {
799 container->img[j].offset -=
800 container_start_offset + file_offset;
801 }
802 /* Append each image array entry */
803 for (j = 0; j < container->num_images; j++)
804 append(ptr, &container->img[j], sizeof(boot_img_t));
805
806 append(ptr, &container->sig_blk_hdr, sizeof(sig_blk_hdr_t));
807
808 /* Padding for container (if necessary) */
809 ptr += ALIGN(container->length, container->padding) -
810 container->length;
811 }
812
813 return flat;
814}
815
816static int build_container(soc_type_t soc, uint32_t sector_size,
817 bool emmc_fastboot, image_t *image_stack,
818 bool dcd_skip, uint8_t fuse_version,
819 uint16_t sw_version, int ofd)
820{
821 static imx_header_v3_t imx_header;
822 image_t *img_sp = image_stack;
823 int file_off;
824 uint8_t *tmp;
825 struct stat sbuf;
826 char *tmp_filename = NULL;
827 uint32_t size = 0;
828 uint32_t file_padding = 0;
Peng Fan00c2dd52018-11-05 09:53:22 +0000829 int ret;
Peng Fan60a56072018-10-16 04:50:30 +0000830
831 int container = -1;
832 int cont_img_count = 0; /* indexes to arrange the container */
833
834 memset((char *)&imx_header, 0, sizeof(imx_header_v3_t));
835
836 if (!image_stack) {
837 fprintf(stderr, "Empty image stack ");
838 exit(EXIT_FAILURE);
839 }
840
841 if (soc == QX)
842 fprintf(stdout, "Platform:\ti.MX8QXP B0\n");
843 else if (soc == QM)
844 fprintf(stdout, "Platform:\ti.MX8QM B0\n");
Peng Faneeb23bf2022-07-26 16:41:19 +0800845 else if (soc == ULP)
846 fprintf(stdout, "Platform:\ti.MX8ULP A0\n");
847 else if (soc == IMX9)
848 fprintf(stdout, "Platform:\ti.MX9\n");
Peng Fan60a56072018-10-16 04:50:30 +0000849
850 set_imx_hdr_v3(&imx_header, 0);
851 set_imx_hdr_v3(&imx_header, 1);
852
853 file_off = get_container_image_start_pos(image_stack, sector_size);
854 fprintf(stdout, "container image offset (aligned):%x\n", file_off);
855
856 /* step through image stack and generate the header */
857 img_sp = image_stack;
858
859 /* stop once we reach null terminator */
860 while (img_sp->option != NO_IMG) {
861 switch (img_sp->option) {
862 case AP:
863 case M40:
864 case M41:
865 case SCFW:
866 case DATA:
Peng Faneeb23bf2022-07-26 16:41:19 +0800867 case UPOWER:
Peng Fan60a56072018-10-16 04:50:30 +0000868 case MSG_BLOCK:
Peng Fanf107b0f2018-11-05 09:53:28 +0000869 if (container < 0) {
870 fprintf(stderr, "No container found\n");
871 exit(EXIT_FAILURE);
872 }
Peng Fan60a56072018-10-16 04:50:30 +0000873 check_file(&sbuf, img_sp->filename);
874 tmp_filename = img_sp->filename;
875 set_image_array_entry(&imx_header.fhdr[container],
876 soc, img_sp, file_off,
877 ALIGN(sbuf.st_size, sector_size),
878 tmp_filename, dcd_skip);
879 img_sp->src = file_off;
880
881 file_off += ALIGN(sbuf.st_size, sector_size);
882 cont_img_count++;
883 break;
884
885 case SECO:
Peng Faneeb23bf2022-07-26 16:41:19 +0800886 case SENTINEL:
Peng Fanf107b0f2018-11-05 09:53:28 +0000887 if (container < 0) {
888 fprintf(stderr, "No container found\n");
889 exit(EXIT_FAILURE);
890 }
Peng Fan60a56072018-10-16 04:50:30 +0000891 check_file(&sbuf, img_sp->filename);
892 tmp_filename = img_sp->filename;
893 set_image_array_entry(&imx_header.fhdr[container],
894 soc,
895 img_sp,
896 file_off,
897 sbuf.st_size,
898 tmp_filename, dcd_skip);
899 img_sp->src = file_off;
900
901 file_off += sbuf.st_size;
902 cont_img_count++;
903 break;
904
905 case NEW_CONTAINER:
906 container++;
907 set_container(&imx_header.fhdr[container], sw_version,
908 CONTAINER_ALIGNMENT,
909 CONTAINER_FLAGS_DEFAULT,
910 fuse_version);
911 /* reset img count when moving to new container */
912 cont_img_count = 0;
913 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);