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