Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 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" |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 9 | #include <image.h> |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 10 | |
| 11 | static int p_idx; |
| 12 | static int sector_size; |
| 13 | static soc_type_t soc; |
| 14 | static int container = -1; |
| 15 | static int32_t core_type = CFG_CORE_INVALID; |
| 16 | static bool emmc_fastboot; |
| 17 | static image_t param_stack[IMG_STACK_SIZE]; |
| 18 | static uint8_t fuse_version; |
| 19 | static uint16_t sw_version; |
| 20 | static uint32_t custom_partition; |
| 21 | static uint32_t scfw_flags; |
| 22 | |
| 23 | int imx8image_check_params(struct image_tool_params *params) |
| 24 | { |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | static void imx8image_set_header(void *ptr, struct stat *sbuf, int ifd, |
| 29 | struct image_tool_params *params) |
| 30 | { |
| 31 | } |
| 32 | |
Pali Rohár | 2972d7d | 2023-03-29 21:25:54 +0200 | [diff] [blame] | 33 | static void imx8image_print_header(const void *ptr, struct image_tool_params *params) |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
| 37 | static int imx8image_check_image_types(uint8_t type) |
| 38 | { |
| 39 | return (type == IH_TYPE_IMX8IMAGE) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 40 | } |
| 41 | |
| 42 | static 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 | |
| 58 | static 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 Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 63 | {CFG_A55, "A55", "A55 core", }, |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 64 | {CFG_A53, "A53", "A53 core", }, |
| 65 | {CFG_A72, "A72", "A72 core", }, |
| 66 | {-1, "", "", }, |
| 67 | }; |
| 68 | |
| 69 | static 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 | |
| 79 | static 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 Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 121 | } else if (!strncmp(token, "ULP", 3)) { |
| 122 | soc = IMX9; |
| 123 | } else if (!strncmp(token, "IMX9", 4)) { |
| 124 | soc = IMX9; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 125 | } 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 | |
| 145 | static 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 Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 195 | case CFG_A55: |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 196 | 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 Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 228 | case CFG_A55: |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 229 | 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 | |
| 239 | static uint32_t parse_cfg_file(image_t *param_stack, char *name) |
| 240 | { |
| 241 | FILE *fd = NULL; |
| 242 | char *line = NULL; |
| 243 | char *token, *saveptr1, *saveptr2; |
| 244 | int lineno = 0; |
| 245 | int fld; |
| 246 | size_t len; |
| 247 | int32_t cmd; |
| 248 | |
| 249 | fd = fopen(name, "r"); |
| 250 | if (fd == 0) { |
| 251 | fprintf(stderr, "Error: %s - Can't open cfg file\n", name); |
| 252 | exit(EXIT_FAILURE); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Very simple parsing, line starting with # are comments |
| 257 | * and are dropped |
| 258 | */ |
| 259 | while ((getline(&line, &len, fd)) > 0) { |
| 260 | lineno++; |
| 261 | |
| 262 | token = strtok_r(line, "\r\n", &saveptr1); |
| 263 | if (!token) |
| 264 | continue; |
| 265 | |
| 266 | /* Check inside the single line */ |
| 267 | for (fld = CFG_COMMAND, cmd = CFG_INVALID, |
| 268 | line = token; ; line = NULL, fld++) { |
| 269 | token = strtok_r(line, " \t", &saveptr2); |
| 270 | if (!token) |
| 271 | break; |
| 272 | |
| 273 | /* Drop all text starting with '#' as comments */ |
| 274 | if (token[0] == '#') |
| 275 | break; |
| 276 | |
| 277 | parse_cfg_fld(param_stack, &cmd, token, name, lineno, |
| 278 | fld); |
| 279 | } |
| 280 | } |
| 281 | |
Mikhail Ilin | f75e92f | 2022-11-23 13:59:49 +0300 | [diff] [blame] | 282 | fclose(fd); |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static void check_file(struct stat *sbuf, char *filename) |
| 287 | { |
| 288 | int tmp_fd = open(filename, O_RDONLY | O_BINARY); |
| 289 | |
| 290 | if (tmp_fd < 0) { |
| 291 | fprintf(stderr, "%s: Can't open: %s\n", |
| 292 | filename, strerror(errno)); |
| 293 | exit(EXIT_FAILURE); |
| 294 | } |
| 295 | |
| 296 | if (fstat(tmp_fd, sbuf) < 0) { |
| 297 | fprintf(stderr, "%s: Can't stat: %s\n", |
| 298 | filename, strerror(errno)); |
| 299 | exit(EXIT_FAILURE); |
| 300 | } |
| 301 | |
| 302 | close(tmp_fd); |
| 303 | } |
| 304 | |
| 305 | static void copy_file_aligned(int ifd, const char *datafile, int offset, |
| 306 | int align) |
| 307 | { |
| 308 | int dfd; |
| 309 | struct stat sbuf; |
| 310 | unsigned char *ptr; |
| 311 | uint8_t zeros[0x4000]; |
| 312 | int size; |
Peng Fan | fc61cc2 | 2018-11-05 09:53:22 +0000 | [diff] [blame] | 313 | int ret; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 314 | |
| 315 | if (align > 0x4000) { |
| 316 | fprintf(stderr, "Wrong alignment requested %d\n", align); |
| 317 | exit(EXIT_FAILURE); |
| 318 | } |
| 319 | |
| 320 | memset(zeros, 0, sizeof(zeros)); |
| 321 | |
| 322 | dfd = open(datafile, O_RDONLY | O_BINARY); |
| 323 | if (dfd < 0) { |
| 324 | fprintf(stderr, "Can't open %s: %s\n", |
| 325 | datafile, strerror(errno)); |
| 326 | exit(EXIT_FAILURE); |
| 327 | } |
| 328 | |
| 329 | if (fstat(dfd, &sbuf) < 0) { |
| 330 | fprintf(stderr, "Can't stat %s: %s\n", |
| 331 | datafile, strerror(errno)); |
| 332 | exit(EXIT_FAILURE); |
| 333 | } |
| 334 | |
| 335 | if (sbuf.st_size == 0) |
| 336 | goto close; |
| 337 | |
| 338 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 339 | if (ptr == MAP_FAILED) { |
| 340 | fprintf(stderr, "Can't read %s: %s\n", |
| 341 | datafile, strerror(errno)); |
| 342 | exit(EXIT_FAILURE); |
| 343 | } |
| 344 | |
| 345 | size = sbuf.st_size; |
Peng Fan | fc61cc2 | 2018-11-05 09:53:22 +0000 | [diff] [blame] | 346 | ret = lseek(ifd, offset, SEEK_SET); |
| 347 | if (ret < 0) { |
| 348 | fprintf(stderr, "%s: lseek error %s\n", |
| 349 | __func__, strerror(errno)); |
| 350 | exit(EXIT_FAILURE); |
| 351 | } |
| 352 | |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 353 | if (write(ifd, ptr, size) != size) { |
| 354 | fprintf(stderr, "Write error %s\n", strerror(errno)); |
| 355 | exit(EXIT_FAILURE); |
| 356 | } |
| 357 | |
| 358 | align = ALIGN(size, align) - size; |
| 359 | |
| 360 | if (write(ifd, (char *)&zeros, align) != align) { |
| 361 | fprintf(stderr, "Write error: %s\n", strerror(errno)); |
| 362 | exit(EXIT_FAILURE); |
| 363 | } |
| 364 | |
| 365 | munmap((void *)ptr, sbuf.st_size); |
| 366 | close: |
| 367 | close(dfd); |
| 368 | } |
| 369 | |
| 370 | static void copy_file (int ifd, const char *datafile, int pad, int offset) |
| 371 | { |
| 372 | int dfd; |
| 373 | struct stat sbuf; |
| 374 | unsigned char *ptr; |
| 375 | int tail; |
Peng Fan | 16841a6 | 2021-03-19 15:56:53 +0800 | [diff] [blame] | 376 | uint64_t zero = 0; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 377 | uint8_t zeros[4096]; |
Peng Fan | fc61cc2 | 2018-11-05 09:53:22 +0000 | [diff] [blame] | 378 | int size, ret; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 379 | |
| 380 | memset(zeros, 0, sizeof(zeros)); |
| 381 | |
| 382 | dfd = open(datafile, O_RDONLY | O_BINARY); |
| 383 | if (dfd < 0) { |
| 384 | fprintf(stderr, "Can't open %s: %s\n", |
| 385 | datafile, strerror(errno)); |
| 386 | exit(EXIT_FAILURE); |
| 387 | } |
| 388 | |
| 389 | if (fstat(dfd, &sbuf) < 0) { |
| 390 | fprintf(stderr, "Can't stat %s: %s\n", |
| 391 | datafile, strerror(errno)); |
| 392 | exit(EXIT_FAILURE); |
| 393 | } |
| 394 | |
| 395 | if (sbuf.st_size == 0) |
| 396 | goto close; |
| 397 | |
| 398 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 399 | if (ptr == MAP_FAILED) { |
| 400 | fprintf(stderr, "Can't read %s: %s\n", |
| 401 | datafile, strerror(errno)); |
| 402 | exit(EXIT_FAILURE); |
| 403 | } |
| 404 | |
| 405 | size = sbuf.st_size; |
Peng Fan | fc61cc2 | 2018-11-05 09:53:22 +0000 | [diff] [blame] | 406 | ret = lseek(ifd, offset, SEEK_SET); |
| 407 | if (ret < 0) { |
| 408 | fprintf(stderr, "%s: lseek error %s\n", |
| 409 | __func__, strerror(errno)); |
| 410 | exit(EXIT_FAILURE); |
| 411 | } |
| 412 | |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 413 | if (write(ifd, ptr, size) != size) { |
| 414 | fprintf(stderr, "Write error %s\n", |
| 415 | strerror(errno)); |
| 416 | exit(EXIT_FAILURE); |
| 417 | } |
| 418 | |
| 419 | tail = size % 4; |
| 420 | pad = pad - size; |
| 421 | if (pad == 1 && tail != 0) { |
| 422 | if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) { |
| 423 | fprintf(stderr, "Write error on %s\n", |
| 424 | strerror(errno)); |
| 425 | exit(EXIT_FAILURE); |
| 426 | } |
| 427 | } else if (pad > 1) { |
| 428 | while (pad > 0) { |
| 429 | int todo = sizeof(zeros); |
| 430 | |
| 431 | if (todo > pad) |
| 432 | todo = pad; |
| 433 | if (write(ifd, (char *)&zeros, todo) != todo) { |
| 434 | fprintf(stderr, "Write error: %s\n", |
| 435 | strerror(errno)); |
| 436 | exit(EXIT_FAILURE); |
| 437 | } |
| 438 | pad -= todo; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | munmap((void *)ptr, sbuf.st_size); |
| 443 | close: |
| 444 | close(dfd); |
| 445 | } |
| 446 | |
| 447 | uint64_t read_dcd_offset(char *filename) |
| 448 | { |
| 449 | int dfd; |
| 450 | struct stat sbuf; |
| 451 | uint8_t *ptr; |
| 452 | uint64_t offset = 0; |
| 453 | |
| 454 | dfd = open(filename, O_RDONLY | O_BINARY); |
| 455 | if (dfd < 0) { |
| 456 | fprintf(stderr, "Can't open %s: %s\n", filename, strerror(errno)); |
| 457 | exit(EXIT_FAILURE); |
| 458 | } |
| 459 | |
| 460 | if (fstat(dfd, &sbuf) < 0) { |
| 461 | fprintf(stderr, "Can't stat %s: %s\n", filename, strerror(errno)); |
| 462 | exit(EXIT_FAILURE); |
| 463 | } |
| 464 | |
| 465 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 466 | if (ptr == MAP_FAILED) { |
| 467 | fprintf(stderr, "Can't read %s: %s\n", filename, strerror(errno)); |
| 468 | exit(EXIT_FAILURE); |
| 469 | } |
| 470 | |
| 471 | offset = *(uint32_t *)(ptr + DCD_ENTRY_ADDR_IN_SCFW); |
| 472 | |
| 473 | munmap((void *)ptr, sbuf.st_size); |
| 474 | close(dfd); |
| 475 | |
| 476 | return offset; |
| 477 | } |
| 478 | |
| 479 | static void set_image_hash(boot_img_t *img, char *filename, uint32_t hash_type) |
| 480 | { |
| 481 | FILE *fp = NULL; |
| 482 | char sha_command[512]; |
| 483 | char hash[2 * HASH_MAX_LEN + 1]; |
| 484 | int i, ret; |
| 485 | |
| 486 | if (img->size == 0) |
| 487 | sprintf(sha_command, "sha%dsum /dev/null", hash_type); |
| 488 | else |
| 489 | sprintf(sha_command, "dd if=/dev/zero of=tmp_pad bs=%d count=1;\ |
| 490 | dd if=\'%s\' of=tmp_pad conv=notrunc;\ |
| 491 | sha%dsum tmp_pad; rm -f tmp_pad", |
| 492 | img->size, filename, hash_type); |
| 493 | |
| 494 | switch (hash_type) { |
| 495 | case HASH_TYPE_SHA_256: |
| 496 | img->hab_flags |= IMG_FLAG_HASH_SHA256; |
| 497 | break; |
| 498 | case HASH_TYPE_SHA_384: |
| 499 | img->hab_flags |= IMG_FLAG_HASH_SHA384; |
| 500 | break; |
| 501 | case HASH_TYPE_SHA_512: |
| 502 | img->hab_flags |= IMG_FLAG_HASH_SHA512; |
| 503 | break; |
| 504 | default: |
| 505 | fprintf(stderr, "Wrong hash type selected (%d) !!!\n\n", |
| 506 | hash_type); |
| 507 | exit(EXIT_FAILURE); |
| 508 | break; |
| 509 | } |
| 510 | memset(img->hash, 0, HASH_MAX_LEN); |
| 511 | |
| 512 | fp = popen(sha_command, "r"); |
| 513 | if (!fp) { |
| 514 | fprintf(stderr, "Failed to run command hash\n"); |
| 515 | exit(EXIT_FAILURE); |
| 516 | } |
| 517 | |
| 518 | if (!fgets(hash, hash_type / 4 + 1, fp)) { |
| 519 | fprintf(stderr, "Failed to hash file: %s\n", filename); |
| 520 | exit(EXIT_FAILURE); |
| 521 | } |
| 522 | |
| 523 | for (i = 0; i < strlen(hash) / 2; i++) { |
| 524 | ret = sscanf(hash + 2 * i, "%02hhx", &img->hash[i]); |
| 525 | if (ret < 0) { |
| 526 | fprintf(stderr, "Failed sscanf hash: %d\n", ret); |
| 527 | exit(EXIT_FAILURE); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | pclose(fp); |
| 532 | } |
| 533 | |
| 534 | static void set_image_array_entry(flash_header_v3_t *container, |
| 535 | soc_type_t soc, const image_t *image_stack, |
| 536 | uint32_t offset, uint32_t size, |
| 537 | char *tmp_filename, bool dcd_skip) |
| 538 | { |
| 539 | uint64_t entry = image_stack->entry; |
| 540 | uint64_t core = image_stack->ext; |
| 541 | uint32_t meta; |
| 542 | char *tmp_name = ""; |
| 543 | option_type_t type = image_stack->option; |
| 544 | boot_img_t *img = &container->img[container->num_images]; |
| 545 | |
| 546 | img->offset = offset; /* Is re-adjusted later */ |
| 547 | img->size = size; |
| 548 | |
| 549 | set_image_hash(img, tmp_filename, IMAGE_HASH_ALGO_DEFAULT); |
| 550 | |
| 551 | switch (type) { |
| 552 | case SECO: |
| 553 | img->hab_flags |= IMG_TYPE_SECO; |
| 554 | img->hab_flags |= CORE_SECO << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 555 | tmp_name = "SECO"; |
| 556 | img->dst = 0x20C00000; |
| 557 | img->entry = 0x20000000; |
| 558 | break; |
Peng Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 559 | case SENTINEL: |
| 560 | if (container->num_images > 0) { |
| 561 | fprintf(stderr, "Error: SENTINEL container only allows 1 image\n"); |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | img->hab_flags |= IMG_TYPE_SENTINEL; |
| 566 | img->hab_flags |= CORE_ULP_SENTINEL << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 567 | tmp_name = "SENTINEL"; |
| 568 | img->dst = 0xe4000000; /* S400 IRAM base */ |
| 569 | img->entry = 0xe4000000; |
| 570 | break; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 571 | case AP: |
| 572 | if (soc == QX && core == CORE_CA35) { |
| 573 | meta = IMAGE_A35_DEFAULT_META(custom_partition); |
| 574 | } else if (soc == QM && core == CORE_CA53) { |
| 575 | meta = IMAGE_A53_DEFAULT_META(custom_partition); |
| 576 | } else if (soc == QM && core == CORE_CA72) { |
| 577 | meta = IMAGE_A72_DEFAULT_META(custom_partition); |
Peng Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 578 | } else if (((soc == ULP) || (soc == IMX9)) && core == CORE_CA35) { |
| 579 | meta = 0; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 580 | } else { |
Heinrich Schuchardt | eb7f908 | 2018-12-17 10:22:21 +0100 | [diff] [blame] | 581 | fprintf(stderr, |
| 582 | "Error: invalid AP core id: %" PRIu64 "\n", |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 583 | core); |
| 584 | exit(EXIT_FAILURE); |
| 585 | } |
| 586 | img->hab_flags |= IMG_TYPE_EXEC; |
Peng Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 587 | if ((soc == ULP) || (soc == IMX9)) |
| 588 | img->hab_flags |= CORE_ULP_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 589 | else |
| 590 | img->hab_flags |= CORE_CA53 << BOOT_IMG_FLAGS_CORE_SHIFT; /* On B0, only core id = 4 is valid */ |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 591 | tmp_name = "AP"; |
| 592 | img->dst = entry; |
| 593 | img->entry = entry; |
| 594 | img->meta = meta; |
| 595 | custom_partition = 0; |
| 596 | break; |
| 597 | case M40: |
| 598 | case M41: |
Peng Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 599 | if ((soc == ULP) || (soc == IMX9)) { |
| 600 | core = CORE_ULP_CM33; |
| 601 | meta = 0; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 602 | } else { |
Peng Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 603 | if (core == 0) { |
| 604 | core = CORE_CM4_0; |
| 605 | meta = IMAGE_M4_0_DEFAULT_META(custom_partition); |
| 606 | } else if (core == 1) { |
| 607 | core = CORE_CM4_1; |
| 608 | meta = IMAGE_M4_1_DEFAULT_META(custom_partition); |
| 609 | } else { |
| 610 | fprintf(stderr, |
| 611 | "Error: invalid m4 core id: %" PRIu64 "\n", |
| 612 | core); |
| 613 | exit(EXIT_FAILURE); |
| 614 | } |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 615 | } |
| 616 | img->hab_flags |= IMG_TYPE_EXEC; |
| 617 | img->hab_flags |= core << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 618 | tmp_name = "M4"; |
| 619 | if ((entry & 0x7) != 0) { |
| 620 | fprintf(stderr, "\n\nWarning: M4 Destination address is not 8 byte aligned\n\n"); |
| 621 | exit(EXIT_FAILURE); |
| 622 | } |
| 623 | img->dst = entry; |
| 624 | img->entry = entry; |
| 625 | img->meta = meta; |
| 626 | custom_partition = 0; |
| 627 | break; |
| 628 | case DATA: |
| 629 | img->hab_flags |= IMG_TYPE_DATA; |
Peng Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 630 | if ((soc == ULP) || (soc == IMX9)) { |
| 631 | if (core == CORE_CM4_0) |
| 632 | img->hab_flags |= CORE_ULP_CM33 << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 633 | else |
| 634 | img->hab_flags |= CORE_ULP_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 635 | } else { |
| 636 | img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 637 | } |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 638 | tmp_name = "DATA"; |
| 639 | img->dst = entry; |
| 640 | break; |
| 641 | case MSG_BLOCK: |
| 642 | img->hab_flags |= IMG_TYPE_DATA; |
| 643 | img->hab_flags |= CORE_CA35 << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 644 | img->meta = core << BOOT_IMG_META_MU_RID_SHIFT; |
| 645 | tmp_name = "MSG_BLOCK"; |
| 646 | img->dst = entry; |
| 647 | break; |
| 648 | case SCFW: |
| 649 | img->hab_flags |= scfw_flags & 0xFFFF0000; |
| 650 | img->hab_flags |= IMG_TYPE_EXEC; |
| 651 | img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 652 | tmp_name = "SCFW"; |
| 653 | img->dst = 0x1FFE0000; |
| 654 | img->entry = 0x1FFE0000; |
| 655 | |
| 656 | /* Lets add the DCD now */ |
| 657 | if (!dcd_skip) { |
| 658 | container->num_images++; |
| 659 | img = &container->img[container->num_images]; |
| 660 | img->hab_flags |= IMG_TYPE_DCD_DDR; |
| 661 | img->hab_flags |= CORE_SC << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 662 | set_image_hash(img, "/dev/null", |
| 663 | IMAGE_HASH_ALGO_DEFAULT); |
| 664 | img->offset = offset + img->size; |
| 665 | img->entry = read_dcd_offset(tmp_filename); |
| 666 | img->dst = img->entry - 1; |
| 667 | } |
| 668 | break; |
Peng Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 669 | case UPOWER: |
| 670 | if (soc == ULP) { |
| 671 | img->hab_flags |= IMG_TYPE_EXEC; |
| 672 | img->hab_flags |= CORE_ULP_UPOWER << BOOT_IMG_FLAGS_CORE_SHIFT; |
| 673 | tmp_name = "UPOWER"; |
| 674 | img->dst = 0x28300200; /* UPOWER code RAM */ |
| 675 | img->entry = 0x28300200; |
| 676 | } |
| 677 | break; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 678 | default: |
| 679 | fprintf(stderr, "unrecognized image type (%d)\n", type); |
| 680 | exit(EXIT_FAILURE); |
| 681 | } |
| 682 | |
| 683 | fprintf(stdout, "%s file_offset = 0x%x size = 0x%x\n", tmp_name, offset, size); |
| 684 | |
| 685 | container->num_images++; |
| 686 | } |
| 687 | |
| 688 | void set_container(flash_header_v3_t *container, uint16_t sw_version, |
| 689 | uint32_t alignment, uint32_t flags, uint16_t fuse_version) |
| 690 | { |
| 691 | container->sig_blk_hdr.tag = 0x90; |
| 692 | container->sig_blk_hdr.length = sizeof(sig_blk_hdr_t); |
| 693 | container->sw_version = sw_version; |
| 694 | container->padding = alignment; |
| 695 | container->fuse_version = fuse_version; |
| 696 | container->flags = flags; |
| 697 | fprintf(stdout, "container flags: 0x%x\n", container->flags); |
| 698 | } |
| 699 | |
| 700 | static int get_container_image_start_pos(image_t *image_stack, uint32_t align) |
| 701 | { |
| 702 | image_t *img_sp = image_stack; |
| 703 | /*8K total container header*/ |
| 704 | int file_off = CONTAINER_IMAGE_ARRAY_START_OFFSET; |
| 705 | FILE *fd = NULL; |
| 706 | flash_header_v3_t header; |
| 707 | int ret; |
| 708 | |
| 709 | while (img_sp->option != NO_IMG) { |
| 710 | if (img_sp->option == APPEND) { |
| 711 | fd = fopen(img_sp->filename, "r"); |
| 712 | if (!fd) { |
| 713 | fprintf(stderr, "Fail open first container file %s\n", img_sp->filename); |
| 714 | exit(EXIT_FAILURE); |
| 715 | } |
| 716 | |
| 717 | ret = fread(&header, sizeof(header), 1, fd); |
Peng Fan | df439e9 | 2018-11-05 09:53:25 +0000 | [diff] [blame] | 718 | if (ret != 1) { |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 719 | printf("Failure Read header %d\n", ret); |
Peng Fan | df439e9 | 2018-11-05 09:53:25 +0000 | [diff] [blame] | 720 | exit(EXIT_FAILURE); |
| 721 | } |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 722 | |
| 723 | fclose(fd); |
| 724 | |
| 725 | if (header.tag != IVT_HEADER_TAG_B0) { |
Vagrant Cascadian | bb5835b | 2019-01-08 13:10:23 -0800 | [diff] [blame] | 726 | fprintf(stderr, "header tag mismatched \n"); |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 727 | exit(EXIT_FAILURE); |
| 728 | } else { |
| 729 | file_off += |
| 730 | header.img[header.num_images - 1].size; |
| 731 | file_off = ALIGN(file_off, align); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | img_sp++; |
| 736 | } |
| 737 | |
| 738 | return file_off; |
| 739 | } |
| 740 | |
| 741 | static void set_imx_hdr_v3(imx_header_v3_t *imxhdr, uint32_t cont_id) |
| 742 | { |
| 743 | flash_header_v3_t *fhdr_v3 = &imxhdr->fhdr[cont_id]; |
| 744 | |
| 745 | /* Set magic number, Only >= B0 supported */ |
| 746 | fhdr_v3->tag = IVT_HEADER_TAG_B0; |
| 747 | fhdr_v3->version = IVT_VERSION_B0; |
| 748 | } |
| 749 | |
| 750 | static uint8_t *flatten_container_header(imx_header_v3_t *imx_header, |
| 751 | uint8_t containers_count, |
| 752 | uint32_t *size_out, |
| 753 | uint32_t file_offset) |
| 754 | { |
| 755 | uint8_t *flat = NULL; |
| 756 | uint8_t *ptr = NULL; |
| 757 | uint16_t size = 0; |
| 758 | int i, j; |
| 759 | |
| 760 | /* Compute size of all container headers */ |
| 761 | for (i = 0; i < containers_count; i++) { |
| 762 | flash_header_v3_t *container = &imx_header->fhdr[i]; |
| 763 | |
| 764 | container->sig_blk_offset = HEADER_IMG_ARRAY_OFFSET + |
| 765 | container->num_images * IMG_ARRAY_ENTRY_SIZE; |
| 766 | |
| 767 | container->length = HEADER_IMG_ARRAY_OFFSET + |
| 768 | (IMG_ARRAY_ENTRY_SIZE * container->num_images) + |
| 769 | sizeof(sig_blk_hdr_t); |
| 770 | |
| 771 | /* Print info needed by CST to sign the container header */ |
| 772 | fprintf(stdout, "CST: CONTAINER %d offset: 0x%x\n", |
| 773 | i, file_offset + size); |
| 774 | fprintf(stdout, "CST: CONTAINER %d: Signature Block: offset is at 0x%x\n", i, |
| 775 | file_offset + size + container->length - |
| 776 | SIGNATURE_BLOCK_HEADER_LENGTH); |
| 777 | |
| 778 | size += ALIGN(container->length, container->padding); |
| 779 | } |
| 780 | |
| 781 | flat = calloc(size, sizeof(uint8_t)); |
| 782 | if (!flat) { |
| 783 | fprintf(stderr, "Failed to allocate memory (%d)\n", size); |
| 784 | exit(EXIT_FAILURE); |
| 785 | } |
| 786 | |
| 787 | ptr = flat; |
| 788 | *size_out = size; |
| 789 | |
| 790 | for (i = 0; i < containers_count; i++) { |
| 791 | flash_header_v3_t *container = &imx_header->fhdr[i]; |
| 792 | uint32_t container_start_offset = ptr - flat; |
| 793 | |
| 794 | /* Append container header */ |
| 795 | append(ptr, container, HEADER_IMG_ARRAY_OFFSET); |
| 796 | |
| 797 | /* Adjust images offset to start from container headers start */ |
| 798 | for (j = 0; j < container->num_images; j++) { |
| 799 | container->img[j].offset -= |
| 800 | container_start_offset + file_offset; |
| 801 | } |
| 802 | /* Append each image array entry */ |
| 803 | for (j = 0; j < container->num_images; j++) |
| 804 | append(ptr, &container->img[j], sizeof(boot_img_t)); |
| 805 | |
| 806 | append(ptr, &container->sig_blk_hdr, sizeof(sig_blk_hdr_t)); |
| 807 | |
| 808 | /* Padding for container (if necessary) */ |
| 809 | ptr += ALIGN(container->length, container->padding) - |
| 810 | container->length; |
| 811 | } |
| 812 | |
| 813 | return flat; |
| 814 | } |
| 815 | |
| 816 | static int build_container(soc_type_t soc, uint32_t sector_size, |
| 817 | bool emmc_fastboot, image_t *image_stack, |
| 818 | bool dcd_skip, uint8_t fuse_version, |
| 819 | uint16_t sw_version, int ofd) |
| 820 | { |
| 821 | static imx_header_v3_t imx_header; |
| 822 | image_t *img_sp = image_stack; |
| 823 | int file_off; |
| 824 | uint8_t *tmp; |
| 825 | struct stat sbuf; |
| 826 | char *tmp_filename = NULL; |
| 827 | uint32_t size = 0; |
| 828 | uint32_t file_padding = 0; |
Peng Fan | fc61cc2 | 2018-11-05 09:53:22 +0000 | [diff] [blame] | 829 | int ret; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 830 | |
| 831 | int container = -1; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 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 Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 844 | 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 Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 848 | |
| 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 Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 866 | case UPOWER: |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 867 | case MSG_BLOCK: |
Peng Fan | a9f7f1c | 2018-11-05 09:53:28 +0000 | [diff] [blame] | 868 | if (container < 0) { |
| 869 | fprintf(stderr, "No container found\n"); |
| 870 | exit(EXIT_FAILURE); |
| 871 | } |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 872 | 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); |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 881 | break; |
| 882 | |
| 883 | case SECO: |
Peng Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 884 | case SENTINEL: |
Peng Fan | a9f7f1c | 2018-11-05 09:53:28 +0000 | [diff] [blame] | 885 | if (container < 0) { |
| 886 | fprintf(stderr, "No container found\n"); |
| 887 | exit(EXIT_FAILURE); |
| 888 | } |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 889 | check_file(&sbuf, img_sp->filename); |
| 890 | tmp_filename = img_sp->filename; |
| 891 | set_image_array_entry(&imx_header.fhdr[container], |
| 892 | soc, |
| 893 | img_sp, |
| 894 | file_off, |
| 895 | sbuf.st_size, |
| 896 | tmp_filename, dcd_skip); |
| 897 | img_sp->src = file_off; |
| 898 | |
| 899 | file_off += sbuf.st_size; |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 900 | break; |
| 901 | |
| 902 | case NEW_CONTAINER: |
| 903 | container++; |
| 904 | set_container(&imx_header.fhdr[container], sw_version, |
| 905 | CONTAINER_ALIGNMENT, |
| 906 | CONTAINER_FLAGS_DEFAULT, |
| 907 | fuse_version); |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 908 | scfw_flags = 0; |
| 909 | break; |
| 910 | |
| 911 | case APPEND: |
| 912 | /* |
| 913 | * nothing to do here, the container is appended |
| 914 | * in the output |
| 915 | */ |
| 916 | break; |
| 917 | case FLAG: |
| 918 | /* |
| 919 | * override the flags for scfw in current container |
| 920 | * mask off bottom 16 bits. |
| 921 | */ |
| 922 | scfw_flags = img_sp->entry & 0xFFFF0000; |
| 923 | break; |
| 924 | case FILEOFF: |
| 925 | if (file_off > img_sp->dst) { |
| 926 | fprintf(stderr, "FILEOFF address less than current file offset!!!\n"); |
| 927 | exit(EXIT_FAILURE); |
| 928 | } |
| 929 | if (img_sp->dst != ALIGN(img_sp->dst, sector_size)) { |
| 930 | fprintf(stderr, "FILEOFF address is not aligned to sector size!!!\n"); |
| 931 | exit(EXIT_FAILURE); |
| 932 | } |
| 933 | file_off = img_sp->dst; |
| 934 | break; |
| 935 | case PARTITION: |
| 936 | /* |
| 937 | * keep custom partition until next executable image |
| 938 | * use a global var for default behaviour |
| 939 | */ |
| 940 | custom_partition = img_sp->entry; |
| 941 | break; |
| 942 | default: |
| 943 | fprintf(stderr, "unrecognized option in input stack (%d)\n", img_sp->option); |
| 944 | exit(EXIT_FAILURE); |
| 945 | } |
| 946 | img_sp++; /* advance index */ |
| 947 | } |
| 948 | |
| 949 | /* Append container (if specified) */ |
| 950 | img_sp = image_stack; |
| 951 | do { |
| 952 | if (img_sp->option == APPEND) { |
| 953 | copy_file(ofd, img_sp->filename, 0, 0); |
| 954 | file_padding += FIRST_CONTAINER_HEADER_LENGTH; |
| 955 | } |
| 956 | img_sp++; |
| 957 | } while (img_sp->option != NO_IMG); |
| 958 | |
| 959 | /* Add padding or skip appended container */ |
Peng Fan | fc61cc2 | 2018-11-05 09:53:22 +0000 | [diff] [blame] | 960 | ret = lseek(ofd, file_padding, SEEK_SET); |
| 961 | if (ret < 0) { |
| 962 | fprintf(stderr, "%s: lseek error %s\n", |
| 963 | __func__, strerror(errno)); |
| 964 | exit(EXIT_FAILURE); |
| 965 | } |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 966 | |
Peng Fan | 47f7a9d | 2018-11-05 09:53:31 +0000 | [diff] [blame] | 967 | if (container >= 0) { |
| 968 | /* Note: Image offset are not contained in the image */ |
| 969 | tmp = flatten_container_header(&imx_header, container + 1, |
| 970 | &size, file_padding); |
| 971 | /* Write image header */ |
| 972 | if (write(ofd, tmp, size) != size) { |
| 973 | fprintf(stderr, "error writing image hdr\n"); |
| 974 | exit(EXIT_FAILURE); |
| 975 | } |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 976 | |
Peng Fan | 47f7a9d | 2018-11-05 09:53:31 +0000 | [diff] [blame] | 977 | /* Clean-up memory used by the headers */ |
| 978 | free(tmp); |
| 979 | } |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 980 | |
| 981 | /* |
| 982 | * step through the image stack again this time copying |
| 983 | * images to final bin, stop once we reach null terminator. |
| 984 | */ |
| 985 | img_sp = image_stack; |
| 986 | while (img_sp->option != NO_IMG) { |
| 987 | if (img_sp->option == M40 || img_sp->option == M41 || |
| 988 | img_sp->option == AP || img_sp->option == DATA || |
| 989 | img_sp->option == SCD || img_sp->option == SCFW || |
Peng Fan | 6ec65c8 | 2022-07-26 16:41:19 +0800 | [diff] [blame] | 990 | img_sp->option == SECO || img_sp->option == MSG_BLOCK || |
| 991 | img_sp->option == UPOWER || img_sp->option == SENTINEL) { |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 992 | copy_file_aligned(ofd, img_sp->filename, img_sp->src, |
| 993 | sector_size); |
| 994 | } |
| 995 | img_sp++; |
| 996 | } |
| 997 | |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | int imx8image_copy_image(int outfd, struct image_tool_params *mparams) |
| 1002 | { |
| 1003 | image_t *img_sp = param_stack; |
| 1004 | |
| 1005 | /* |
| 1006 | * SECO FW is a container image, this is to calculate the |
| 1007 | * 2nd container offset. |
| 1008 | */ |
| 1009 | fprintf(stdout, "parsing %s\n", mparams->imagename); |
| 1010 | parse_cfg_file(img_sp, mparams->imagename); |
| 1011 | |
| 1012 | if (sector_size == 0) { |
| 1013 | fprintf(stderr, "Wrong sector size\n"); |
| 1014 | exit(EXIT_FAILURE); |
| 1015 | } |
| 1016 | |
| 1017 | fprintf(stdout, "CONTAINER Sector size:\t%08x\n", sector_size); |
| 1018 | fprintf(stdout, "CONTAINER FUSE VERSION:\t0x%02x\n", fuse_version); |
| 1019 | fprintf(stdout, "CONTAINER SW VERSION:\t0x%04x\n", sw_version); |
| 1020 | |
| 1021 | build_container(soc, sector_size, emmc_fastboot, |
Fabio Estevam | 9ec1791 | 2019-01-18 11:00:11 -0200 | [diff] [blame] | 1022 | img_sp, false, fuse_version, sw_version, outfd); |
Peng Fan | a2b96ec | 2018-10-16 04:50:30 +0000 | [diff] [blame] | 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * imx8image parameters |
| 1029 | */ |
| 1030 | U_BOOT_IMAGE_TYPE( |
| 1031 | imx8image, |
| 1032 | "NXP i.MX8 Boot Image support", |
| 1033 | 0, |
| 1034 | NULL, |
| 1035 | imx8image_check_params, |
| 1036 | NULL, |
| 1037 | imx8image_print_header, |
| 1038 | imx8image_set_header, |
| 1039 | NULL, |
| 1040 | imx8image_check_image_types, |
| 1041 | NULL, |
| 1042 | NULL |
| 1043 | ); |