1
2
3
4
5
6
7
8#include "imx8image.h"
9#include <image.h>
10
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", },
63 {CFG_A53, "A53", "A53 core", },
64 {CFG_A72, "A72", "A72 core", },
65 {-1, "", "", },
66};
67
68static table_entry_t imx8image_sector_size[] = {
69 {0x400, "sd", "sd/emmc",},
70 {0x400, "emmc_fastboot", "emmc fastboot",},
71 {0x400, "fspi", "flexspi", },
72 {0x1000, "nand_4k", "nand 4K", },
73 {0x2000, "nand_8k", "nand 8K", },
74 {0x4000, "nand_16k", "nand 16K", },
75 {-1, "", "Invalid", },
76};
77
78static void parse_cfg_cmd(image_t *param_stack, int32_t cmd, char *token,
79 char *name, int lineno)
80{
81 switch (cmd) {
82 case CMD_BOOT_FROM:
83 sector_size = get_table_entry_id(imx8image_sector_size,
84 "imximage boot option",
85 token);
86 if (!strncmp("emmc_fastboot", token, 13))
87 emmc_fastboot = true;
88 break;
89 case CMD_FUSE_VERSION:
90 fuse_version = (uint8_t)(strtoll(token, NULL, 0) & 0xFF);
91 break;
92 case CMD_SW_VERSION:
93 sw_version = (uint8_t)(strtoll(token, NULL, 0) & 0xFFFF);
94 break;
95 case CMD_FILEOFF:
96 param_stack[p_idx].option = FILEOFF;
97 param_stack[p_idx++].dst = (uint32_t)strtoll(token, NULL, 0);
98 break;
99 case CMD_MSG_BLOCK:
100 param_stack[p_idx].option = MSG_BLOCK;
101 param_stack[p_idx].filename = token;
102 break;
103 case CMD_FLAG:
104 param_stack[p_idx].option = FLAG;
105 param_stack[p_idx++].entry = (uint32_t)strtoll(token, NULL, 0);
106 break;
107 case CMD_APPEND:
108 param_stack[p_idx].option = APPEND;
109 param_stack[p_idx++].filename = token;
110 break;
111 case CMD_PARTITION:
112 param_stack[p_idx].option = PARTITION;
113 param_stack[p_idx++].entry = (uint32_t)strtoll(token, NULL, 0);
114 break;
115 case CMD_SOC_TYPE:
116 if (!strncmp(token, "IMX8QX", 6)) {
117 soc = QX;
118 } else if (!strncmp(token, "IMX8QM", 6)) {
119 soc = QM;
120 } else {
121 fprintf(stderr, "Unknown CMD_SOC_TYPE");
122 exit(EXIT_FAILURE);
123 }
124 break;
125 case CMD_IMAGE:
126 case CMD_DATA:
127 core_type = get_table_entry_id(imx8image_core_entries,
128 "imx8image core entries",
129 token);
130 if (core_type < 0) {
131 fprintf(stderr, "Wrong IMAGE core_type %s\n", token);
132 exit(EXIT_FAILURE);
133 }
134 break;
135 default:
136 break;
137 }
138}
139
140static void parse_cfg_fld(image_t *param_stack, int32_t *cmd, char *token,
141 char *name, int lineno, int fld)
142{
143 switch (fld) {
144 case CFG_COMMAND:
145 *cmd = get_table_entry_id(imx8image_cmds, "imx8image cmds",
146 token);
147 if (*cmd < 0) {
148 fprintf(stderr, "Error: %s[%d] - Invalid command (%s)\n", name, lineno, token);
149 exit(EXIT_FAILURE);
150 }
151
152 if (*cmd == CMD_CONTAINER) {
153 fprintf(stdout, "New Container: \t%d\n", ++container);
154 param_stack[p_idx++].option = NEW_CONTAINER;
155 }
156 break;
157 case CFG_CORE_TYPE:
158 parse_cfg_cmd(param_stack, *cmd, token, name, lineno);
159 break;
160 case CFG_IMAGE_NAME:
161 if (*cmd == CMD_MSG_BLOCK) {
162 if (!strncmp(token, "fuse", 4)) {
163 param_stack[p_idx].ext = SC_R_OTP;
164 } else if (!strncmp(token, "debug", 5)) {
165 param_stack[p_idx].ext = SC_R_DEBUG;
166 } else if (!strncmp(token, "field", 5)) {
167 param_stack[p_idx].ext = SC_R_ROM_0;
168 } else {
169 fprintf(stderr, "MSG type not found %s\n", token);
170 exit(EXIT_FAILURE);
171 }
172 break;
173 }
174 switch (core_type) {
175 case CFG_SCU:
176 param_stack[p_idx].option = SCFW;
177 param_stack[p_idx++].filename = token;
178 break;
179 case CFG_M40:
180 param_stack[p_idx].option = M40;
181 param_stack[p_idx].ext = 0;
182 param_stack[p_idx].filename = token;
183 break;
184 case CFG_M41:
185 param_stack[p_idx].option = M41;
186 param_stack[p_idx].ext = 1;
187 param_stack[p_idx].filename = token;
188 break;
189 case CFG_A35:
190 param_stack[p_idx].ext = CORE_CA35;
191 param_stack[p_idx].option =
192 (*cmd == CMD_DATA) ? DATA : AP;
193 param_stack[p_idx].filename = token;
194 break;
195 case CFG_A53:
196 param_stack[p_idx].ext = CORE_CA53;
197 param_stack[p_idx].option =
198 (*cmd == CMD_DATA) ? DATA : AP;
199 param_stack[p_idx].filename = token;
200 break;
201 case CFG_A72:
202 param_stack[p_idx].ext = CORE_CA72;
203 param_stack[p_idx].option =
204 (*cmd == CMD_DATA) ? DATA : AP;
205 param_stack[p_idx].filename = token;
206 break;
207 }
208 break;
209 case CFG_LOAD_ADDR:
210 if (*cmd == CMD_MSG_BLOCK) {
211 param_stack[p_idx++].entry =
212 (uint32_t)strtoll(token, NULL, 0);
213 break;
214 }
215 switch (core_type) {
216 case CFG_SCU:
217 break;
218 case CFG_M40:
219 case CFG_M41:
220 case CFG_A35:
221 case CFG_A53:
222 case CFG_A72:
223 param_stack[p_idx++].entry =
224 (uint32_t)strtoll(token, NULL, 0);
225 break;
226 }
227 default:
228 break;
229 }
230}
231
232static uint32_t parse_cfg_file(image_t *param_stack, char *name)
233{
234 FILE *fd = NULL;
235 char *line = NULL;
236 char *token, *saveptr1, *saveptr2;
237 int lineno = 0;
238 int fld;
239 size_t len;
240 int32_t cmd;
241
242 fd = fopen(name, "r");
243 if (fd == 0) {
244 fprintf(stderr, "Error: %s - Can't open cfg file\n", name);
245 exit(EXIT_FAILURE);
246 }
247
248
249
250
251
252 while ((getline(&line, &len, fd)) > 0) {
253 lineno++;
254
255 token = strtok_r(line, "\r\n", &saveptr1);
256 if (!token)
257 continue;
258
259
260 for (fld = CFG_COMMAND, cmd = CFG_INVALID,
261 line = token; ; line = NULL, fld++) {
262 token = strtok_r(line, " \t", &saveptr2);
263 if (!token)
264 break;
265
266
267 if (token[0] == '#')
268 break;
269
270 parse_cfg_fld(param_stack, &cmd, token, name, lineno,
271 fld);
272 }
273 }
274
275 return 0;
276}
277
278static void check_file(struct stat *sbuf, char *filename)
279{
280 int tmp_fd = open(filename, O_RDONLY | O_BINARY);
281
282 if (tmp_fd < 0) {
283 fprintf(stderr, "%s: Can't open: %s\n",
284 filename, strerror(errno));
285 exit(EXIT_FAILURE);
286 }
287
288 if (fstat(tmp_fd, sbuf) < 0) {
289 fprintf(stderr, "%s: Can't stat: %s\n",
290 filename, strerror(errno));
291 exit(EXIT_FAILURE);
292 }
293
294 close(tmp_fd);
295}
296
297static void copy_file_aligned(int ifd, const char *datafile, int offset,
298 int align)
299{
300 int dfd;
301 struct stat sbuf;
302 unsigned char *ptr;
303 uint8_t zeros[0x4000];
304 int size;
305 int ret;
306
307 if (align > 0x4000) {
308 fprintf(stderr, "Wrong alignment requested %d\n", align);
309 exit(EXIT_FAILURE);
310 }
311
312 memset(zeros, 0, sizeof(zeros));
313
314 dfd = open(datafile, O_RDONLY | O_BINARY);
315 if (dfd < 0) {
316 fprintf(stderr, "Can't open %s: %s\n",
317 datafile, strerror(errno));
318 exit(EXIT_FAILURE);
319 }
320
321 if (fstat(dfd, &sbuf) < 0) {
322 fprintf(stderr, "Can't stat %s: %s\n",
323 datafile, strerror(errno));
324 exit(EXIT_FAILURE);
325 }
326
327 if (sbuf.st_size == 0)
328 goto close;
329
330 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
331 if (ptr == MAP_FAILED) {
332 fprintf(stderr, "Can't read %s: %s\n",
333 datafile, strerror(errno));
334 exit(EXIT_FAILURE);
335 }
336
337 size = sbuf.st_size;
338 ret = lseek(ifd, offset, SEEK_SET);
339 if (ret < 0) {
340 fprintf(stderr, "%s: lseek error %s\n",
341 __func__, strerror(errno));
342 exit(EXIT_FAILURE);
343 }
344
345 if (write(ifd, ptr, size) != size) {
346 fprintf(stderr, "Write error %s\n", strerror(errno));
347 exit(EXIT_FAILURE);
348 }
349
350 align = ALIGN(size, align) - size;
351
352 if (write(ifd, (char *)&zeros, align) != align) {
353 fprintf(stderr, "Write error: %s\n", strerror(errno));
354 exit(EXIT_FAILURE);
355 }
356
357 munmap((void *)ptr, sbuf.st_size);
358close:
359 close(dfd);
360}
361
362static void copy_file (int ifd, const char *datafile, int pad, int offset)
363{
364 int dfd;
365 struct stat sbuf;
366 unsigned char *ptr;
367 int tail;
368 uint64_t zero = 0;
369 uint8_t zeros[4096];
370 int size, ret;
371
372 memset(zeros, 0, sizeof(zeros));
373
374 dfd = open(datafile, O_RDONLY | O_BINARY);
375 if (dfd < 0) {
376 fprintf(stderr, "Can't open %s: %s\n",
377 datafile, strerror(errno));
378 exit(EXIT_FAILURE);
379 }
380
381 if (fstat(dfd, &sbuf) < 0) {
382 fprintf(stderr, "Can't stat %s: %s\n",
383 datafile, strerror(errno));
384 exit(EXIT_FAILURE);
385 }
386
387 if (sbuf.st_size == 0)
388 goto close;
389
390 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
391 if (ptr == MAP_FAILED) {
392 fprintf(stderr, "Can't read %s: %s\n",
393 datafile, strerror(errno));
394 exit(EXIT_FAILURE);
395 }
396
397 size = sbuf.st_size;
398 ret = lseek(ifd, offset, SEEK_SET);
399 if (ret < 0) {
400 fprintf(stderr, "%s: lseek error %s\n",
401 __func__, strerror(errno));
402 exit(EXIT_FAILURE);
403 }
404
405 if (write(ifd, ptr, size) != size) {
406 fprintf(stderr, "Write error %s\n",
407 strerror(errno));
408 exit(EXIT_FAILURE);
409 }
410
411 tail = size % 4;
412 pad = pad - size;
413 if (pad == 1 && tail != 0) {
414 if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) {
415 fprintf(stderr, "Write error on %s\n",
416 strerror(errno));
417 exit(EXIT_FAILURE);
418 }
419 } else if (pad > 1) {
420 while (pad > 0) {
421 int todo = sizeof(zeros);
422
423 if (todo > pad)
424 todo = pad;
425 if (write(ifd, (char *)&zeros, todo) != todo) {
426 fprintf(stderr, "Write error: %s\n",
427 strerror(errno));
428 exit(EXIT_FAILURE);
429 }
430 pad -= todo;
431 }
432 }
433
434 munmap((void *)ptr, sbuf.st_size);
435close:
436 close(dfd);
437}
438
439uint64_t read_dcd_offset(char *filename)
440{
441 int dfd;
442 struct stat sbuf;
443 uint8_t *ptr;
444 uint64_t offset = 0;
445
446 dfd = open(filename, O_RDONLY | O_BINARY);
447 if (dfd < 0) {
448 fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno));
449 exit(EXIT_FAILURE);
450 }
451
452 if (fstat(dfd, &sbuf) < 0) {
453 fprintf(stderr, "Can't stat %s: %s\n", filename, strerror(errno));
454 exit(EXIT_FAILURE);
455 }
456
457 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
458 if (ptr == MAP_FAILED) {
459 fprintf(stderr, "Can't read %s: %s\n", filename, strerror(errno));
460 exit(EXIT_FAILURE);
461 }
462
463 offset = *(uint32_t *)(ptr + DCD_ENTRY_ADDR_IN_SCFW);
464
465 munmap((void *)ptr, sbuf.st_size);
466 close(dfd);
467
468 return offset;
469}
470
471static void set_image_hash(boot_img_t *img, char *filename, uint32_t hash_type)
472{
473 FILE *fp = NULL;
474 char sha_command[512];
475 char hash[2 * HASH_MAX_LEN + 1];
476 int i, ret;
477
478 if (img->size == 0)
479 sprintf(sha_command, "sha%dsum /dev/null", hash_type);
480 else
481 sprintf(sha_command, "dd if=/dev/zero of=tmp_pad bs=%d count=1;\
482 dd if=\'%s\' of=tmp_pad conv=notrunc;\
483 sha%dsum tmp_pad; rm -f tmp_pad",
484 img->size, filename, hash_type);
485
486 switch (hash_type) {
487 case HASH_TYPE_SHA_256:
488 img->hab_flags |= IMG_FLAG_HASH_SHA256;
489 break;
490 case HASH_TYPE_SHA_384:
491 img->hab_flags |= IMG_FLAG_HASH_SHA384;
492 break;
493 case HASH_TYPE_SHA_512:
494 img->hab_flags |= IMG_FLAG_HASH_SHA512;
495 break;
496 default:
497 fprintf(stderr, "Wrong hash type selected (%d) !!!\n\n",
498 hash_type);
499 exit(EXIT_FAILURE);
500 break;
501 }
502 memset(img->hash, 0, HASH_MAX_LEN);
503
504 fp = popen(sha_command, "r");
505 if (!fp) {
506 fprintf(stderr, "Failed to run command hash\n");
507 exit(EXIT_FAILURE);
508 }
509
510 if (!fgets(hash, hash_type / 4 + 1, fp)) {
511 fprintf(stderr, "Failed to hash file: %s\n", filename);
512 exit(EXIT_FAILURE);
513 }
514
515 for (i = 0; i < strlen(hash) / 2; i++) {
516 ret = sscanf(hash + 2 * i, "%02hhx", &img->hash[i]);
517 if (ret < 0) {
518 fprintf(stderr, "Failed sscanf hash: %d\n", ret);
519 exit(EXIT_FAILURE);
520 }
521 }
522
523 pclose(fp);
524}
525
526static void set_image_array_entry(flash_header_v3_t *container,
527 soc_type_t soc, const image_t *image_stack,
528 uint32_t offset, uint32_t size,
529 char *tmp_filename, bool dcd_skip)
530{
531 uint64_t entry = image_stack->entry;
532 uint64_t core = image_stack->ext;
533 uint32_t meta;
534 char *tmp_name = "";
535 option_type_t type = image_stack->option;
536 boot_img_t *img = &container->img[container->num_images];
537
538 img->offset = offset;
539 img->size = size;
540
541 set_image_hash(img, tmp_filename, IMAGE_HASH_ALGO_DEFAULT);
542
543 switch (type) {
544 case SECO:
545 img->hab_flags |= IMG_TYPE_SECO;
546 img->hab_flags |= CORE_SECO << BOOT_IMG_FLAGS_CORE_SHIFT;
547 tmp_name = "SECO";
548 img->dst = 0x20C00000;
549 img->entry = 0x20000000;
550 break;
551 case AP:
552 if (soc == QX && core == CORE_CA35) {
553 meta = IMAGE_A35_DEFAULT_META(custom_partition);
554 } else if (soc == QM && core == CORE_CA53) {
555 meta = IMAGE_A53_DEFAULT_META(custom_partition);
556 } else if (soc == QM && core == CORE_CA72) {
557 meta = IMAGE_A72_DEFAULT_META(custom_partition);
558 } else {
559 fprintf(stderr,
560 "Error: invalid AP core id: %" PRIu64 "\n",
561 core);
562 exit(EXIT_FAILURE);
563 }
564 img->hab_flags |= IMG_TYPE_EXEC;
565
566 img->hab_flags |= CORE_CA53 << BOOT_IMG_FLAGS_CORE_SHIFT;
567 tmp_name = "AP";
568 img->dst = entry;
569 img->entry = entry;
570 img->meta = meta;
571 custom_partition = 0;
572 break;
573 case M40:
574 case M41:
575 if (core == 0) {
576 core = CORE_CM4_0;
577 meta = IMAGE_M4_0_DEFAULT_META(custom_partition);
578 } else if (core == 1) {
579 core = CORE_CM4_1;
580 meta = IMAGE_M4_1_DEFAULT_META(custom_partition);
581 } else {
582 fprintf(stderr,
583 "Error: invalid m4 core id: %" PRIu64 "\n",
584 core);
585 exit(EXIT_FAILURE);
586 }
587 img->hab_flags |= IMG_TYPE_EXEC;
588 img->hab_flags |= core << BOOT_IMG_FLAGS_CORE_SHIFT;
589 tmp_name = "M4";
590 if ((entry & 0x7) != 0) {
591 fprintf(stderr, "\n\nWarning: M4 Destination address is not 8 byte aligned\n\n");
592 exit(EXIT_FAILURE);
593 }
594 img->dst = entry;
595 img->entry = entry;
596 img->meta = meta;
597 custom_partition = 0;
598 break;
599 case DATA:
600 img->hab_flags |= IMG_TYPE_DATA;
601 img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
602 tmp_name = "DATA";
603 img->dst = entry;
604 break;
605 case MSG_BLOCK:
606 img->hab_flags |= IMG_TYPE_DATA;
607 img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT;
608 img->meta = core << BOOT_IMG_META_MU_RID_SHIFT;
609 tmp_name = "MSG_BLOCK";
610 img->dst = entry;
611 break;
612 case SCFW:
613 img->hab_flags |= scfw_flags & 0xFFFF0000;
614 img->hab_flags |= IMG_TYPE_EXEC;
615 img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
616 tmp_name = "SCFW";
617 img->dst = 0x1FFE0000;
618 img->entry = 0x1FFE0000;
619
620
621 if (!dcd_skip) {
622 container->num_images++;
623 img = &container->img[container->num_images];
624 img->hab_flags |= IMG_TYPE_DCD_DDR;
625 img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT;
626 set_image_hash(img, "/dev/null",
627 IMAGE_HASH_ALGO_DEFAULT);
628 img->offset = offset + img->size;
629 img->entry = read_dcd_offset(tmp_filename);
630 img->dst = img->entry - 1;
631 }
632 break;
633 default:
634 fprintf(stderr, "unrecognized image type (%d)\n", type);
635 exit(EXIT_FAILURE);
636 }
637
638 fprintf(stdout, "%s file_offset = 0x%x size = 0x%x\n", tmp_name, offset, size);
639
640 container->num_images++;
641}
642
643void set_container(flash_header_v3_t *container, uint16_t sw_version,
644 uint32_t alignment, uint32_t flags, uint16_t fuse_version)
645{
646 container->sig_blk_hdr.tag = 0x90;
647 container->sig_blk_hdr.length = sizeof(sig_blk_hdr_t);
648 container->sw_version = sw_version;
649 container->padding = alignment;
650 container->fuse_version = fuse_version;
651 container->flags = flags;
652 fprintf(stdout, "container flags: 0x%x\n", container->flags);
653}
654
655static int get_container_image_start_pos(image_t *image_stack, uint32_t align)
656{
657 image_t *img_sp = image_stack;
658
659 int file_off = CONTAINER_IMAGE_ARRAY_START_OFFSET;
660 FILE *fd = NULL;
661 flash_header_v3_t header;
662 int ret;
663
664 while (img_sp->option != NO_IMG) {
665 if (img_sp->option == APPEND) {
666 fd = fopen(img_sp->filename, "r");
667 if (!fd) {
668 fprintf(stderr, "Fail open first container file %s\n", img_sp->filename);
669 exit(EXIT_FAILURE);
670 }
671
672 ret = fread(&header, sizeof(header), 1, fd);
673 if (ret != 1) {
674 printf("Failure Read header %d\n", ret);
675 exit(EXIT_FAILURE);
676 }
677
678 fclose(fd);
679
680 if (header.tag != IVT_HEADER_TAG_B0) {
681 fprintf(stderr, "header tag mismatched \n");
682 exit(EXIT_FAILURE);
683 } else {
684 file_off +=
685 header.img[header.num_images - 1].size;
686 file_off = ALIGN(file_off, align);
687 }
688 }
689
690 img_sp++;
691 }
692
693 return file_off;
694}
695
696static void set_imx_hdr_v3(imx_header_v3_t *imxhdr, uint32_t cont_id)
697{
698 flash_header_v3_t *fhdr_v3 = &imxhdr->fhdr[cont_id];
699
700
701 fhdr_v3->tag = IVT_HEADER_TAG_B0;
702 fhdr_v3->version = IVT_VERSION_B0;
703}
704
705static uint8_t *flatten_container_header(imx_header_v3_t *imx_header,
706 uint8_t containers_count,
707 uint32_t *size_out,
708 uint32_t file_offset)
709{
710 uint8_t *flat = NULL;
711 uint8_t *ptr = NULL;
712 uint16_t size = 0;
713 int i, j;
714
715
716 for (i = 0; i < containers_count; i++) {
717 flash_header_v3_t *container = &imx_header->fhdr[i];
718
719 container->sig_blk_offset = HEADER_IMG_ARRAY_OFFSET +
720 container->num_images * IMG_ARRAY_ENTRY_SIZE;
721
722 container->length = HEADER_IMG_ARRAY_OFFSET +
723 (IMG_ARRAY_ENTRY_SIZE * container->num_images) +
724 sizeof(sig_blk_hdr_t);
725
726
727 fprintf(stdout, "CST: CONTAINER %d offset: 0x%x\n",
728 i, file_offset + size);
729 fprintf(stdout, "CST: CONTAINER %d: Signature Block: offset is at 0x%x\n", i,
730 file_offset + size + container->length -
731 SIGNATURE_BLOCK_HEADER_LENGTH);
732
733 size += ALIGN(container->length, container->padding);
734 }
735
736 flat = calloc(size, sizeof(uint8_t));
737 if (!flat) {
738 fprintf(stderr, "Failed to allocate memory (%d)\n", size);
739 exit(EXIT_FAILURE);
740 }
741
742 ptr = flat;
743 *size_out = size;
744
745 for (i = 0; i < containers_count; i++) {
746 flash_header_v3_t *container = &imx_header->fhdr[i];
747 uint32_t container_start_offset = ptr - flat;
748
749
750 append(ptr, container, HEADER_IMG_ARRAY_OFFSET);
751
752
753 for (j = 0; j < container->num_images; j++) {
754 container->img[j].offset -=
755 container_start_offset + file_offset;
756 }
757
758 for (j = 0; j < container->num_images; j++)
759 append(ptr, &container->img[j], sizeof(boot_img_t));
760
761 append(ptr, &container->sig_blk_hdr, sizeof(sig_blk_hdr_t));
762
763
764 ptr += ALIGN(container->length, container->padding) -
765 container->length;
766 }
767
768 return flat;
769}
770
771static int build_container(soc_type_t soc, uint32_t sector_size,
772 bool emmc_fastboot, image_t *image_stack,
773 bool dcd_skip, uint8_t fuse_version,
774 uint16_t sw_version, int ofd)
775{
776 static imx_header_v3_t imx_header;
777 image_t *img_sp = image_stack;
778 int file_off;
779 uint8_t *tmp;
780 struct stat sbuf;
781 char *tmp_filename = NULL;
782 uint32_t size = 0;
783 uint32_t file_padding = 0;
784 int ret;
785
786 int container = -1;
787 int cont_img_count = 0;
788
789 memset((char *)&imx_header, 0, sizeof(imx_header_v3_t));
790
791 if (!image_stack) {
792 fprintf(stderr, "Empty image stack ");
793 exit(EXIT_FAILURE);
794 }
795
796 if (soc == QX)
797 fprintf(stdout, "Platform:\ti.MX8QXP B0\n");
798 else if (soc == QM)
799 fprintf(stdout, "Platform:\ti.MX8QM B0\n");
800
801 set_imx_hdr_v3(&imx_header, 0);
802 set_imx_hdr_v3(&imx_header, 1);
803
804 file_off = get_container_image_start_pos(image_stack, sector_size);
805 fprintf(stdout, "container image offset (aligned):%x\n", file_off);
806
807
808 img_sp = image_stack;
809
810
811 while (img_sp->option != NO_IMG) {
812 switch (img_sp->option) {
813 case AP:
814 case M40:
815 case M41:
816 case SCFW:
817 case DATA:
818 case MSG_BLOCK:
819 if (container < 0) {
820 fprintf(stderr, "No container found\n");
821 exit(EXIT_FAILURE);
822 }
823 check_file(&sbuf, img_sp->filename);
824 tmp_filename = img_sp->filename;
825 set_image_array_entry(&imx_header.fhdr[container],
826 soc, img_sp, file_off,
827 ALIGN(sbuf.st_size, sector_size),
828 tmp_filename, dcd_skip);
829 img_sp->src = file_off;
830
831 file_off += ALIGN(sbuf.st_size, sector_size);
832 cont_img_count++;
833 break;
834
835 case SECO:
836 if (container < 0) {
837 fprintf(stderr, "No container found\n");
838 exit(EXIT_FAILURE);
839 }
840 check_file(&sbuf, img_sp->filename);
841 tmp_filename = img_sp->filename;
842 set_image_array_entry(&imx_header.fhdr[container],
843 soc,
844 img_sp,
845 file_off,
846 sbuf.st_size,
847 tmp_filename, dcd_skip);
848 img_sp->src = file_off;
849
850 file_off += sbuf.st_size;
851 cont_img_count++;
852 break;
853
854 case NEW_CONTAINER:
855 container++;
856 set_container(&imx_header.fhdr[container], sw_version,
857 CONTAINER_ALIGNMENT,
858 CONTAINER_FLAGS_DEFAULT,
859 fuse_version);
860
861 cont_img_count = 0;
862 scfw_flags = 0;
863 break;
864
865 case APPEND:
866
867
868
869
870 break;
871 case FLAG:
872
873
874
875
876 scfw_flags = img_sp->entry & 0xFFFF0000;
877 break;
878 case FILEOFF:
879 if (file_off > img_sp->dst) {
880 fprintf(stderr, "FILEOFF address less than current file offset!!!\n");
881 exit(EXIT_FAILURE);
882 }
883 if (img_sp->dst != ALIGN(img_sp->dst, sector_size)) {
884 fprintf(stderr, "FILEOFF address is not aligned to sector size!!!\n");
885 exit(EXIT_FAILURE);
886 }
887 file_off = img_sp->dst;
888 break;
889 case PARTITION:
890
891
892
893
894 custom_partition = img_sp->entry;
895 break;
896 default:
897 fprintf(stderr, "unrecognized option in input stack (%d)\n", img_sp->option);
898 exit(EXIT_FAILURE);
899 }
900 img_sp++;
901 }
902
903
904 img_sp = image_stack;
905 do {
906 if (img_sp->option == APPEND) {
907 copy_file(ofd, img_sp->filename, 0, 0);
908 file_padding += FIRST_CONTAINER_HEADER_LENGTH;
909 }
910 img_sp++;
911 } while (img_sp->option != NO_IMG);
912
913
914 ret = lseek(ofd, file_padding, SEEK_SET);
915 if (ret < 0) {
916 fprintf(stderr, "%s: lseek error %s\n",
917 __func__, strerror(errno));
918 exit(EXIT_FAILURE);
919 }
920
921 if (container >= 0) {
922
923 tmp = flatten_container_header(&imx_header, container + 1,
924 &size, file_padding);
925
926 if (write(ofd, tmp, size) != size) {
927 fprintf(stderr, "error writing image hdr\n");
928 exit(EXIT_FAILURE);
929 }
930
931
932 free(tmp);
933 }
934
935
936
937
938
939 img_sp = image_stack;
940 while (img_sp->option != NO_IMG) {
941 if (img_sp->option == M40 || img_sp->option == M41 ||
942 img_sp->option == AP || img_sp->option == DATA ||
943 img_sp->option == SCD || img_sp->option == SCFW ||
944 img_sp->option == SECO || img_sp->option == MSG_BLOCK) {
945 copy_file_aligned(ofd, img_sp->filename, img_sp->src,
946 sector_size);
947 }
948 img_sp++;
949 }
950
951 return 0;
952}
953
954int imx8image_copy_image(int outfd, struct image_tool_params *mparams)
955{
956 image_t *img_sp = param_stack;
957
958
959
960
961
962 fprintf(stdout, "parsing %s\n", mparams->imagename);
963 parse_cfg_file(img_sp, mparams->imagename);
964
965 if (sector_size == 0) {
966 fprintf(stderr, "Wrong sector size\n");
967 exit(EXIT_FAILURE);
968 }
969
970 fprintf(stdout, "CONTAINER Sector size:\t%08x\n", sector_size);
971 fprintf(stdout, "CONTAINER FUSE VERSION:\t0x%02x\n", fuse_version);
972 fprintf(stdout, "CONTAINER SW VERSION:\t0x%04x\n", sw_version);
973
974 build_container(soc, sector_size, emmc_fastboot,
975 img_sp, false, fuse_version, sw_version, outfd);
976
977 return 0;
978}
979
980
981
982
983U_BOOT_IMAGE_TYPE(
984 imx8image,
985 "NXP i.MX8 Boot Image support",
986 0,
987 NULL,
988 imx8image_check_params,
989 NULL,
990 imx8image_print_header,
991 imx8image_set_header,
992 NULL,
993 imx8image_check_image_types,
994 NULL,
995 NULL
996);
997