blob: 01e14869114f1c008060cb38621622d178fab6a9 [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
282 return 0;
283}
284
285static void check_file(struct stat *sbuf, char *filename)
286{
287 int tmp_fd = open(filename, O_RDONLY | O_BINARY);
288
289 if (tmp_fd < 0) {
290 fprintf(stderr, "%s: Can't open: %s\n",
291 filename, strerror(errno));
292 exit(EXIT_FAILURE);
293 }
294
295 if (fstat(tmp_fd, sbuf) < 0) {
296 fprintf(stderr, "%s: Can't stat: %s\n",
297 filename, strerror(errno));
298 exit(EXIT_FAILURE);
299 }
300
301 close(tmp_fd);
302}
303
304static void copy_file_aligned(int ifd, const char *datafile, int offset,
305 int align)
306{
307 int dfd;
308 struct stat sbuf;
309 unsigned char *ptr;
310 uint8_t zeros[0x4000];
311 int size;
Peng Fan00c2dd52018-11-05 09:53:22 +0000312 int ret;
Peng Fan60a56072018-10-16 04:50:30 +0000313
314 if (align > 0x4000) {
315 fprintf(stderr, "Wrong alignment requested %d\n", align);
316 exit(EXIT_FAILURE);
317 }
318
319 memset(zeros, 0, sizeof(zeros));
320
321 dfd = open(datafile, O_RDONLY | O_BINARY);
322 if (dfd < 0) {
323 fprintf(stderr, "Can't open %s: %s\n",
324 datafile, strerror(errno));
325 exit(EXIT_FAILURE);
326 }
327
328 if (fstat(dfd, &sbuf) < 0) {
329 fprintf(stderr, "Can't stat %s: %s\n",
330 datafile, strerror(errno));
331 exit(EXIT_FAILURE);
332 }
333
334 if (sbuf.st_size == 0)
335 goto close;
336
337 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
338 if (ptr == MAP_FAILED) {
339 fprintf(stderr, "Can't read %s: %s\n",
340 datafile, strerror(errno));
341 exit(EXIT_FAILURE);
342 }
343
344 size = sbuf.st_size;
Peng Fan00c2dd52018-11-05 09:53:22 +0000345 ret = lseek(ifd, offset, SEEK_SET);
346 if (ret < 0) {
347 fprintf(stderr, "%s: lseek error %s\n",
348 __func__, strerror(errno));
349 exit(EXIT_FAILURE);
350 }
351
Peng Fan60a56072018-10-16 04:50:30 +0000352 if (write(ifd, ptr, size) != size) {
353 fprintf(stderr, "Write error %s\n", strerror(errno));
354 exit(EXIT_FAILURE);
355 }
356
357 align = ALIGN(size, align) - size;
358
359 if (write(ifd, (char *)&zeros, align) != align) {
360 fprintf(stderr, "Write error: %s\n", strerror(errno));
361 exit(EXIT_FAILURE);
362 }
363
364 munmap((void *)ptr, sbuf.st_size);
365close:
366 close(dfd);
367}
368
369static void copy_file (int ifd, const char *datafile, int pad, int offset)
370{
371 int dfd;
372 struct stat sbuf;
373 unsigned char *ptr;
374 int tail;
Peng Fane3ed0cc2021-03-19 15:56:53 +0800375 uint64_t zero = 0;
Peng Fan60a56072018-10-16 04:50:30 +0000376 uint8_t zeros[4096];
Peng Fan00c2dd52018-11-05 09:53:22 +0000377 int size, ret;
Peng Fan60a56072018-10-16 04:50:30 +0000378
379 memset(zeros, 0, sizeof(zeros));
380
381 dfd = open(datafile, O_RDONLY | O_BINARY);
382 if (dfd < 0) {
383 fprintf(stderr, "Can't open %s: %s\n",
384 datafile, strerror(errno));
385 exit(EXIT_FAILURE);
386 }
387
388 if (fstat(dfd, &sbuf) < 0) {
389 fprintf(stderr, "Can't stat %s: %s\n",
390 datafile, strerror(errno));
391 exit(EXIT_FAILURE);
392 }
393
394 if (sbuf.st_size == 0)
395 goto close;
396
397 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
398 if (ptr == MAP_FAILED) {
399 fprintf(stderr, "Can't read %s: %s\n",
400 datafile, strerror(errno));
401 exit(EXIT_FAILURE);
402 }
403
404 size = sbuf.st_size;
Peng Fan00c2dd52018-11-05 09:53:22 +0000405 ret = lseek(ifd, offset, SEEK_SET);
406 if (ret < 0) {
407 fprintf(stderr, "%s: lseek error %s\n",
408 __func__, strerror(errno));
409 exit(EXIT_FAILURE);
410 }
411
Peng Fan60a56072018-10-16 04:50:30 +0000412 if (write(ifd, ptr, size) != size) {
413 fprintf(stderr, "Write error %s\n",
414 strerror(errno));
415 exit(EXIT_FAILURE);
416 }
417
418 tail = size % 4;
419 pad = pad - size;
420 if (pad == 1 && tail != 0) {
421 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
422 fprintf(stderr, "Write error on %s\n",
423 strerror(errno));
424 exit(EXIT_FAILURE);
425 }
426 } else if (pad > 1) {
427 while (pad > 0) {
428 int todo = sizeof(zeros);
429
430 if (todo > pad)
431 todo = pad;
432 if (write(ifd, (char *)&zeros, todo) != todo) {
433 fprintf(stderr, "Write error: %s\n",
434 strerror(errno));
435 exit(EXIT_FAILURE);
436 }
437 pad -= todo;
438 }
439 }
440
441 munmap((void *)ptr, sbuf.st_size);
442close:
443 close(dfd);
444}
445
446uint64_t read_dcd_offset(char *filename)
447{
448 int dfd;
449 struct stat sbuf;
450 uint8_t *ptr;
451 uint64_t offset = 0;
452
453 dfd = open(filename, O_RDONLY | O_BINARY);
454 if (dfd < 0) {
455 fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno));
456 exit(EXIT_FAILURE);
457 }
458
459 if (fstat(dfd, &sbuf) < 0) {
460 fprintf(stderr, "Can't stat %s: %s\n", filename, strerror(errno));
461 exit(EXIT_FAILURE);
462 }
463
464 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
465 if (ptr == MAP_FAILED) {
466 fprintf(stderr, "Can't read %s: %s\n", filename, strerror(errno));
467 exit(EXIT_FAILURE);
468 }
469
470 offset = *(uint32_t *)(ptr + DCD_ENTRY_ADDR_IN_SCFW);
471
472 munmap((void *)ptr, sbuf.st_size);
473 close(dfd);
474
475 return offset;
476}
477
478static void set_image_hash(boot_img_t *img, char *filename, uint32_t hash_type)
479{
480 FILE *fp = NULL;
481 char sha_command[512];
482 char hash[2 * HASH_MAX_LEN + 1];
483 int i, ret;
484
485 if (img->size == 0)
486 sprintf(sha_command, "sha%dsum /dev/null", hash_type);
487 else
488 sprintf(sha_command, "dd if=/dev/zero of=tmp_pad bs=%d count=1;\
489 dd if=\'%s\' of=tmp_pad conv=notrunc;\
490 sha%dsum tmp_pad; rm -f tmp_pad",
491 img->size, filename, hash_type);
492
493 switch (hash_type) {
494 case HASH_TYPE_SHA_256:
495 img->hab_flags |= IMG_FLAG_HASH_SHA256;
496 break;
497 case HASH_TYPE_SHA_384:
498 img->hab_flags |= IMG_FLAG_HASH_SHA384;
499 break;
500 case HASH_TYPE_SHA_512:
501 img->hab_flags |= IMG_FLAG_HASH_SHA512;
502 break;
503 default:
504 fprintf(stderr, "Wrong hash type selected (%d) !!!\n\n",
505 hash_type);
506 exit(EXIT_FAILURE);
507 break;
508 }
509 memset(img->hash, 0, HASH_MAX_LEN);
510
511 fp = popen(sha_command, "r");
512 if (!fp) {
513 fprintf(stderr, "Failed to run command hash\n");
514 exit(EXIT_FAILURE);
515 }
516
517 if (!fgets(hash, hash_type / 4 + 1, fp)) {
518 fprintf(stderr, "Failed to hash file: %s\n", filename);
519 exit(EXIT_FAILURE);
520 }
521
522 for (i = 0; i < strlen(hash) / 2; i++) {
523 ret = sscanf(hash + 2 * i, "%02hhx", &img->hash[i]);
524 if (ret < 0) {
525 fprintf(stderr, "Failed sscanf hash: %d\n", ret);
526 exit(EXIT_FAILURE);
527 }
528 }
529
530 pclose(fp);
531}
532
533static void set_image_array_entry(flash_header_v3_t *container,
534 soc_type_t soc, const image_t *image_stack,
535 uint32_t offset, uint32_t size,
536 char *tmp_filename, bool dcd_skip)
537{
538 uint64_t entry = image_stack->entry;
539 uint64_t core = image_stack->ext;
540 uint32_t meta;
541 char *tmp_name = "";
542 option_type_t type = image_stack->option;
543 boot_img_t *img = &container->img[container->num_images];
544
545 img->offset = offset; /* Is re-adjusted later */
546 img->size = size;
547
548 set_image_hash(img, tmp_filename, IMAGE_HASH_ALGO_DEFAULT);
549
550 switch (type) {
551 case SECO:
552 img->hab_flags |= IMG_TYPE_SECO;
553 img->hab_flags |= CORE_SECO << BOOT_IMG_FLAGS_CORE_SHIFT;
554 tmp_name = "SECO";
555 img->dst = 0x20C00000;
556 img->entry = 0x20000000;
557 break;
Peng Faneeb23bf2022-07-26 16:41:19 +0800558 case SENTINEL:
559 if (container->num_images > 0) {
560 fprintf(stderr, "Error: SENTINEL container only allows 1 image\n");
561 return;
562 }
563
564 img->hab_flags |= IMG_TYPE_SENTINEL;
565 img->hab_flags |= CORE_ULP_SENTINEL << BOOT_IMG_FLAGS_CORE_SHIFT;
566 tmp_name = "SENTINEL";
567 img->dst = 0xe4000000; /* S400 IRAM base */
568 img->entry = 0xe4000000;
569 break;
Peng Fan60a56072018-10-16 04:50:30 +0000570 case AP:
571 if (soc == QX && core == CORE_CA35) {
572 meta = IMAGE_A35_DEFAULT_META(custom_partition);
573 } else if (soc == QM && core == CORE_CA53) {
574 meta = IMAGE_A53_DEFAULT_META(custom_partition);
575 } else if (soc == QM && core == CORE_CA72) {
576 meta = IMAGE_A72_DEFAULT_META(custom_partition);
Peng Faneeb23bf2022-07-26 16:41:19 +0800577 } else if (((soc == ULP) || (soc == IMX9)) && core == CORE_CA35) {
578 meta = 0;
Peng Fan60a56072018-10-16 04:50:30 +0000579 } else {
Heinrich Schuchardt819abf82018-12-17 10:22:21 +0100580 fprintf(stderr,
581 "Error: invalid AP core id: %" PRIu64 "\n",
Peng Fan60a56072018-10-16 04:50:30 +0000582 core);
583 exit(EXIT_FAILURE);
584 }
585 img->hab_flags |= IMG_TYPE_EXEC;
Peng Faneeb23bf2022-07-26 16:41:19 +0800586 if ((soc == ULP) || (soc == IMX9))
587 img->hab_flags |= CORE_ULP_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
588 else
589 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 +0000590 tmp_name = "AP";
591 img->dst = entry;
592 img->entry = entry;
593 img->meta = meta;
594 custom_partition = 0;
595 break;
596 case M40:
597 case M41:
Peng Faneeb23bf2022-07-26 16:41:19 +0800598 if ((soc == ULP) || (soc == IMX9)) {
599 core = CORE_ULP_CM33;
600 meta = 0;
Peng Fan60a56072018-10-16 04:50:30 +0000601 } else {
Peng Faneeb23bf2022-07-26 16:41:19 +0800602 if (core == 0) {
603 core = CORE_CM4_0;
604 meta = IMAGE_M4_0_DEFAULT_META(custom_partition);
605 } else if (core == 1) {
606 core = CORE_CM4_1;
607 meta = IMAGE_M4_1_DEFAULT_META(custom_partition);
608 } else {
609 fprintf(stderr,
610 "Error: invalid m4 core id: %" PRIu64 "\n",
611 core);
612 exit(EXIT_FAILURE);
613 }
Peng Fan60a56072018-10-16 04:50:30 +0000614 }
615 img->hab_flags |= IMG_TYPE_EXEC;
616 img->hab_flags |= core << BOOT_IMG_FLAGS_CORE_SHIFT;
617 tmp_name = "M4";
618 if ((entry & 0x7) != 0) {
619 fprintf(stderr, "\n\nWarning: M4 Destination address is not 8 byte aligned\n\n");
620 exit(EXIT_FAILURE);
621 }
622 img->dst = entry;
623 img->entry = entry;
624 img->meta = meta;
625 custom_partition = 0;
626 break;
627 case DATA:
628 img->hab_flags |= IMG_TYPE_DATA;
Peng Faneeb23bf2022-07-26 16:41:19 +0800629 if ((soc == ULP) || (soc == IMX9)) {
630 if (core == CORE_CM4_0)
631 img->hab_flags |= CORE_ULP_CM33 << BOOT_IMG_FLAGS_CORE_SHIFT;
632 else
633 img->hab_flags |= CORE_ULP_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
634 } else {
635 img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
636 }
Peng Fan60a56072018-10-16 04:50:30 +0000637 tmp_name = "DATA";
638 img->dst = entry;
639 break;
640 case MSG_BLOCK:
641 img->hab_flags |= IMG_TYPE_DATA;
642 img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
643 img->meta = core << BOOT_IMG_META_MU_RID_SHIFT;
644 tmp_name = "MSG_BLOCK";
645 img->dst = entry;
646 break;
647 case SCFW:
648 img->hab_flags |= scfw_flags & 0xFFFF0000;
649 img->hab_flags |= IMG_TYPE_EXEC;
650 img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
651 tmp_name = "SCFW";
652 img->dst = 0x1FFE0000;
653 img->entry = 0x1FFE0000;
654
655 /* Lets add the DCD now */
656 if (!dcd_skip) {
657 container->num_images++;
658 img = &container->img[container->num_images];
659 img->hab_flags |= IMG_TYPE_DCD_DDR;
660 img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
661 set_image_hash(img, "/dev/null",
662 IMAGE_HASH_ALGO_DEFAULT);
663 img->offset = offset + img->size;
664 img->entry = read_dcd_offset(tmp_filename);
665 img->dst = img->entry - 1;
666 }
667 break;
Peng Faneeb23bf2022-07-26 16:41:19 +0800668 case UPOWER:
669 if (soc == ULP) {
670 img->hab_flags |= IMG_TYPE_EXEC;
671 img->hab_flags |= CORE_ULP_UPOWER << BOOT_IMG_FLAGS_CORE_SHIFT;
672 tmp_name = "UPOWER";
673 img->dst = 0x28300200; /* UPOWER code RAM */
674 img->entry = 0x28300200;
675 }
676 break;
Peng Fan60a56072018-10-16 04:50:30 +0000677 default:
678 fprintf(stderr, "unrecognized image type (%d)\n", type);
679 exit(EXIT_FAILURE);
680 }
681
682 fprintf(stdout, "%s file_offset = 0x%x size = 0x%x\n", tmp_name, offset, size);
683
684 container->num_images++;
685}
686
687void set_container(flash_header_v3_t *container, uint16_t sw_version,
688 uint32_t alignment, uint32_t flags, uint16_t fuse_version)
689{
690 container->sig_blk_hdr.tag = 0x90;
691 container->sig_blk_hdr.length = sizeof(sig_blk_hdr_t);
692 container->sw_version = sw_version;
693 container->padding = alignment;
694 container->fuse_version = fuse_version;
695 container->flags = flags;
696 fprintf(stdout, "container flags: 0x%x\n", container->flags);
697}
698
699static int get_container_image_start_pos(image_t *image_stack, uint32_t align)
700{
701 image_t *img_sp = image_stack;
702 /*8K total container header*/
703 int file_off = CONTAINER_IMAGE_ARRAY_START_OFFSET;
704 FILE *fd = NULL;
705 flash_header_v3_t header;
706 int ret;
707
708 while (img_sp->option != NO_IMG) {
709 if (img_sp->option == APPEND) {
710 fd = fopen(img_sp->filename, "r");
711 if (!fd) {
712 fprintf(stderr, "Fail open first container file %s\n", img_sp->filename);
713 exit(EXIT_FAILURE);
714 }
715
716 ret = fread(&header, sizeof(header), 1, fd);
Peng Fand65dcfd2018-11-05 09:53:25 +0000717 if (ret != 1) {
Peng Fan60a56072018-10-16 04:50:30 +0000718 printf("Failure Read header %d\n", ret);
Peng Fand65dcfd2018-11-05 09:53:25 +0000719 exit(EXIT_FAILURE);
720 }
Peng Fan60a56072018-10-16 04:50:30 +0000721
722 fclose(fd);
723
724 if (header.tag != IVT_HEADER_TAG_B0) {
Vagrant Cascadian31860f12019-01-08 13:10:23 -0800725 fprintf(stderr, "header tag mismatched \n");
Peng Fan60a56072018-10-16 04:50:30 +0000726 exit(EXIT_FAILURE);
727 } else {
728 file_off +=
729 header.img[header.num_images - 1].size;
730 file_off = ALIGN(file_off, align);
731 }
732 }
733
734 img_sp++;
735 }
736
737 return file_off;
738}
739
740static void set_imx_hdr_v3(imx_header_v3_t *imxhdr, uint32_t cont_id)
741{
742 flash_header_v3_t *fhdr_v3 = &imxhdr->fhdr[cont_id];
743
744 /* Set magic number, Only >= B0 supported */
745 fhdr_v3->tag = IVT_HEADER_TAG_B0;
746 fhdr_v3->version = IVT_VERSION_B0;
747}
748
749static uint8_t *flatten_container_header(imx_header_v3_t *imx_header,
750 uint8_t containers_count,
751 uint32_t *size_out,
752 uint32_t file_offset)
753{
754 uint8_t *flat = NULL;
755 uint8_t *ptr = NULL;
756 uint16_t size = 0;
757 int i, j;
758
759 /* Compute size of all container headers */
760 for (i = 0; i < containers_count; i++) {
761 flash_header_v3_t *container = &imx_header->fhdr[i];
762
763 container->sig_blk_offset = HEADER_IMG_ARRAY_OFFSET +
764 container->num_images * IMG_ARRAY_ENTRY_SIZE;
765
766 container->length = HEADER_IMG_ARRAY_OFFSET +
767 (IMG_ARRAY_ENTRY_SIZE * container->num_images) +
768 sizeof(sig_blk_hdr_t);
769
770 /* Print info needed by CST to sign the container header */
771 fprintf(stdout, "CST: CONTAINER %d offset: 0x%x\n",
772 i, file_offset + size);
773 fprintf(stdout, "CST: CONTAINER %d: Signature Block: offset is at 0x%x\n", i,
774 file_offset + size + container->length -
775 SIGNATURE_BLOCK_HEADER_LENGTH);
776
777 size += ALIGN(container->length, container->padding);
778 }
779
780 flat = calloc(size, sizeof(uint8_t));
781 if (!flat) {
782 fprintf(stderr, "Failed to allocate memory (%d)\n", size);
783 exit(EXIT_FAILURE);
784 }
785
786 ptr = flat;
787 *size_out = size;
788
789 for (i = 0; i < containers_count; i++) {
790 flash_header_v3_t *container = &imx_header->fhdr[i];
791 uint32_t container_start_offset = ptr - flat;
792
793 /* Append container header */
794 append(ptr, container, HEADER_IMG_ARRAY_OFFSET);
795
796 /* Adjust images offset to start from container headers start */
797 for (j = 0; j < container->num_images; j++) {
798 container->img[j].offset -=
799 container_start_offset + file_offset;
800 }
801 /* Append each image array entry */
802 for (j = 0; j < container->num_images; j++)
803 append(ptr, &container->img[j], sizeof(boot_img_t));
804
805 append(ptr, &container->sig_blk_hdr, sizeof(sig_blk_hdr_t));
806
807 /* Padding for container (if necessary) */
808 ptr += ALIGN(container->length, container->padding) -
809 container->length;
810 }
811
812 return flat;
813}
814
815static int build_container(soc_type_t soc, uint32_t sector_size,
816 bool emmc_fastboot, image_t *image_stack,
817 bool dcd_skip, uint8_t fuse_version,
818 uint16_t sw_version, int ofd)
819{
820 static imx_header_v3_t imx_header;
821 image_t *img_sp = image_stack;
822 int file_off;
823 uint8_t *tmp;
824 struct stat sbuf;
825 char *tmp_filename = NULL;
826 uint32_t size = 0;
827 uint32_t file_padding = 0;
Peng Fan00c2dd52018-11-05 09:53:22 +0000828 int ret;
Peng Fan60a56072018-10-16 04:50:30 +0000829
830 int container = -1;
831 int cont_img_count = 0; /* indexes to arrange the container */
832
833 memset((char *)&imx_header, 0, sizeof(imx_header_v3_t));
834
835 if (!image_stack) {
836 fprintf(stderr, "Empty image stack ");
837 exit(EXIT_FAILURE);
838 }
839
840 if (soc == QX)
841 fprintf(stdout, "Platform:\ti.MX8QXP B0\n");
842 else if (soc == QM)
843 fprintf(stdout, "Platform:\ti.MX8QM B0\n");
Peng Faneeb23bf2022-07-26 16:41:19 +0800844 else if (soc == ULP)
845 fprintf(stdout, "Platform:\ti.MX8ULP A0\n");
846 else if (soc == IMX9)
847 fprintf(stdout, "Platform:\ti.MX9\n");
Peng Fan60a56072018-10-16 04:50:30 +0000848
849 set_imx_hdr_v3(&imx_header, 0);
850 set_imx_hdr_v3(&imx_header, 1);
851
852 file_off = get_container_image_start_pos(image_stack, sector_size);
853 fprintf(stdout, "container image offset (aligned):%x\n", file_off);
854
855 /* step through image stack and generate the header */
856 img_sp = image_stack;
857
858 /* stop once we reach null terminator */
859 while (img_sp->option != NO_IMG) {
860 switch (img_sp->option) {
861 case AP:
862 case M40:
863 case M41:
864 case SCFW:
865 case DATA:
Peng Faneeb23bf2022-07-26 16:41:19 +0800866 case UPOWER:
Peng Fan60a56072018-10-16 04:50:30 +0000867 case MSG_BLOCK:
Peng Fanf107b0f2018-11-05 09:53:28 +0000868 if (container < 0) {
869 fprintf(stderr, "No container found\n");
870 exit(EXIT_FAILURE);
871 }
Peng Fan60a56072018-10-16 04:50:30 +0000872 check_file(&sbuf, img_sp->filename);
873 tmp_filename = img_sp->filename;
874 set_image_array_entry(&imx_header.fhdr[container],
875 soc, img_sp, file_off,
876 ALIGN(sbuf.st_size, sector_size),
877 tmp_filename, dcd_skip);
878 img_sp->src = file_off;
879
880 file_off += ALIGN(sbuf.st_size, sector_size);
881 cont_img_count++;
882 break;
883
884 case SECO:
Peng Faneeb23bf2022-07-26 16:41:19 +0800885 case SENTINEL:
Peng Fanf107b0f2018-11-05 09:53:28 +0000886 if (container < 0) {
887 fprintf(stderr, "No container found\n");
888 exit(EXIT_FAILURE);
889 }
Peng Fan60a56072018-10-16 04:50:30 +0000890 check_file(&sbuf, img_sp->filename);
891 tmp_filename = img_sp->filename;
892 set_image_array_entry(&imx_header.fhdr[container],
893 soc,
894 img_sp,
895 file_off,
896 sbuf.st_size,
897 tmp_filename, dcd_skip);
898 img_sp->src = file_off;
899
900 file_off += sbuf.st_size;
901 cont_img_count++;
902 break;
903
904 case NEW_CONTAINER:
905 container++;
906 set_container(&imx_header.fhdr[container], sw_version,
907 CONTAINER_ALIGNMENT,
908 CONTAINER_FLAGS_DEFAULT,
909 fuse_version);
910 /* reset img count when moving to new container */
911 cont_img_count = 0;
912 scfw_flags = 0;
913 break;
914
915 case APPEND:
916 /*
917 * nothing to do here, the container is appended
918 * in the output
919 */
920 break;
921 case FLAG:
922 /*
923 * override the flags for scfw in current container
924 * mask off bottom 16 bits.
925 */
926 scfw_flags = img_sp->entry & 0xFFFF0000;
927 break;
928 case FILEOFF:
929 if (file_off > img_sp->dst) {
930 fprintf(stderr, "FILEOFF address less than current file offset!!!\n");
931 exit(EXIT_FAILURE);
932 }
933 if (img_sp->dst != ALIGN(img_sp->dst, sector_size)) {
934 fprintf(stderr, "FILEOFF address is not aligned to sector size!!!\n");
935 exit(EXIT_FAILURE);
936 }
937 file_off = img_sp->dst;
938 break;
939 case PARTITION:
940 /*
941 * keep custom partition until next executable image
942 * use a global var for default behaviour
943 */
944 custom_partition = img_sp->entry;
945 break;
946 default:
947 fprintf(stderr, "unrecognized option in input stack (%d)\n", img_sp->option);
948 exit(EXIT_FAILURE);
949 }
950 img_sp++; /* advance index */
951 }
952
953 /* Append container (if specified) */
954 img_sp = image_stack;
955 do {
956 if (img_sp->option == APPEND) {
957 copy_file(ofd, img_sp->filename, 0, 0);
958 file_padding += FIRST_CONTAINER_HEADER_LENGTH;
959 }
960 img_sp++;
961 } while (img_sp->option != NO_IMG);
962
963 /* Add padding or skip appended container */
Peng Fan00c2dd52018-11-05 09:53:22 +0000964 ret = lseek(ofd, file_padding, SEEK_SET);
965 if (ret < 0) {
966 fprintf(stderr, "%s: lseek error %s\n",
967 __func__, strerror(errno));
968 exit(EXIT_FAILURE);
969 }
Peng Fan60a56072018-10-16 04:50:30 +0000970
Peng Fan18864852018-11-05 09:53:31 +0000971 if (container >= 0) {
972 /* Note: Image offset are not contained in the image */
973 tmp = flatten_container_header(&imx_header, container + 1,
974 &size, file_padding);
975 /* Write image header */
976 if (write(ofd, tmp, size) != size) {
977 fprintf(stderr, "error writing image hdr\n");
978 exit(EXIT_FAILURE);
979 }
Peng Fan60a56072018-10-16 04:50:30 +0000980
Peng Fan18864852018-11-05 09:53:31 +0000981 /* Clean-up memory used by the headers */
982 free(tmp);
983 }
Peng Fan60a56072018-10-16 04:50:30 +0000984
985 /*
986 * step through the image stack again this time copying
987 * images to final bin, stop once we reach null terminator.
988 */
989 img_sp = image_stack;
990 while (img_sp->option != NO_IMG) {
991 if (img_sp->option == M40 || img_sp->option == M41 ||
992 img_sp->option == AP || img_sp->option == DATA ||
993 img_sp->option == SCD || img_sp->option == SCFW ||
Peng Faneeb23bf2022-07-26 16:41:19 +0800994 img_sp->option == SECO || img_sp->option == MSG_BLOCK ||
995 img_sp->option == UPOWER || img_sp->option == SENTINEL) {
Peng Fan60a56072018-10-16 04:50:30 +0000996 copy_file_aligned(ofd, img_sp->filename, img_sp->src,
997 sector_size);
998 }
999 img_sp++;
1000 }
1001
1002 return 0;
1003}
1004
1005int imx8image_copy_image(int outfd, struct image_tool_params *mparams)
1006{
1007 image_t *img_sp = param_stack;
1008
1009 /*
1010 * SECO FW is a container image, this is to calculate the
1011 * 2nd container offset.
1012 */
1013 fprintf(stdout, "parsing %s\n", mparams->imagename);
1014 parse_cfg_file(img_sp, mparams->imagename);
1015
1016 if (sector_size == 0) {
1017 fprintf(stderr, "Wrong sector size\n");
1018 exit(EXIT_FAILURE);
1019 }
1020
1021 fprintf(stdout, "CONTAINER Sector size:\t%08x\n", sector_size);
1022 fprintf(stdout, "CONTAINER FUSE VERSION:\t0x%02x\n", fuse_version);
1023 fprintf(stdout, "CONTAINER SW VERSION:\t0x%04x\n", sw_version);
1024
1025 build_container(soc, sector_size, emmc_fastboot,
Fabio Estevam21958e42019-01-18 11:00:11 -02001026 img_sp, false, fuse_version, sw_version, outfd);
Peng Fan60a56072018-10-16 04:50:30 +00001027
1028 return 0;
1029}
1030
1031/*
1032 * imx8image parameters
1033 */
1034U_BOOT_IMAGE_TYPE(
1035 imx8image,
1036 "NXP i.MX8 Boot Image support",
1037 0,
1038 NULL,
1039 imx8image_check_params,
1040 NULL,
1041 imx8image_print_header,
1042 imx8image_set_header,
1043 NULL,
1044 imx8image_check_image_types,
1045 NULL,
1046 NULL
1047);