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