Peng Fan | 6609c26 | 2018-11-20 10:19:36 +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 | |
| 9 | #include "imagetool.h" |
| 10 | #include <image.h> |
| 11 | #include "imximage.h" |
| 12 | #include "compiler.h" |
| 13 | |
| 14 | static uint32_t ap_start_addr, sld_start_addr, sld_src_off; |
| 15 | static char *ap_img, *sld_img, *signed_hdmi; |
| 16 | static imx_header_v3_t imx_header[2]; /* At most there are 3 IVT headers */ |
| 17 | static uint32_t rom_image_offset; |
| 18 | static uint32_t sector_size = 0x200; |
| 19 | static uint32_t image_off; |
| 20 | static uint32_t sld_header_off; |
| 21 | static uint32_t ivt_offset; |
| 22 | static uint32_t using_fit; |
| 23 | |
Peng Fan | b8f1683 | 2019-09-16 03:09:39 +0000 | [diff] [blame] | 24 | #define ROM_V1 1 |
| 25 | #define ROM_V2 2 |
| 26 | |
| 27 | static uint32_t rom_version = ROM_V1; |
| 28 | |
Peng Fan | 6609c26 | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 29 | #define CSF_SIZE 0x2000 |
| 30 | #define HDMI_IVT_ID 0 |
| 31 | #define IMAGE_IVT_ID 1 |
| 32 | |
| 33 | #define HDMI_FW_SIZE 0x17000 /* Use Last 0x1000 for IVT and CSF */ |
| 34 | #define ALIGN_SIZE 0x1000 |
Peng Fan | 6609c26 | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 35 | |
| 36 | static uint32_t get_cfg_value(char *token, char *name, int linenr) |
| 37 | { |
| 38 | char *endptr; |
| 39 | uint32_t value; |
| 40 | |
| 41 | errno = 0; |
| 42 | value = strtoul(token, &endptr, 16); |
| 43 | if (errno || token == endptr) { |
| 44 | fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n", |
| 45 | name, linenr, token); |
| 46 | exit(EXIT_FAILURE); |
| 47 | } |
| 48 | return value; |
| 49 | } |
| 50 | |
| 51 | int imx8mimage_check_params(struct image_tool_params *params) |
| 52 | { |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static void imx8mimage_set_header(void *ptr, struct stat *sbuf, int ifd, |
| 57 | struct image_tool_params *params) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | static void imx8mimage_print_header(const void *ptr) |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | static int imx8mimage_check_image_types(uint8_t type) |
| 66 | { |
| 67 | return (type == IH_TYPE_IMX8MIMAGE) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 68 | } |
| 69 | |
| 70 | static table_entry_t imx8mimage_cmds[] = { |
| 71 | {CMD_BOOT_FROM, "BOOT_FROM", "boot command", }, |
| 72 | {CMD_FIT, "FIT", "fit image", }, |
| 73 | {CMD_SIGNED_HDMI, "SIGNED_HDMI", "signed hdmi image", }, |
| 74 | {CMD_LOADER, "LOADER", "loader image", }, |
| 75 | {CMD_SECOND_LOADER, "SECOND_LOADER", "2nd loader image", }, |
| 76 | {CMD_DDR_FW, "DDR_FW", "ddr firmware", }, |
Peng Fan | b8f1683 | 2019-09-16 03:09:39 +0000 | [diff] [blame] | 77 | {CMD_ROM_VERSION, "ROM_VERSION", "rom version", }, |
Peng Fan | 6609c26 | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 78 | {-1, "", "", }, |
| 79 | }; |
| 80 | |
| 81 | static table_entry_t imx8mimage_ivt_offset[] = { |
| 82 | {0x400, "sd", "sd/emmc",}, |
| 83 | {0x400, "emmc_fastboot", "emmc fastboot",}, |
| 84 | {0x1000, "fspi", "flexspi", }, |
| 85 | {-1, "", "Invalid", }, |
| 86 | }; |
| 87 | |
| 88 | static void parse_cfg_cmd(int32_t cmd, char *token, char *name, int lineno) |
| 89 | { |
| 90 | switch (cmd) { |
| 91 | case CMD_BOOT_FROM: |
| 92 | ivt_offset = get_table_entry_id(imx8mimage_ivt_offset, |
| 93 | "imx8mimage ivt offset", |
| 94 | token); |
| 95 | if (!strncmp(token, "sd", 2)) |
| 96 | rom_image_offset = 0x8000; |
Peng Fan | b8f1683 | 2019-09-16 03:09:39 +0000 | [diff] [blame] | 97 | |
| 98 | if (rom_version == ROM_V2) |
| 99 | ivt_offset = 0; |
Peng Fan | 6609c26 | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 100 | break; |
| 101 | case CMD_LOADER: |
| 102 | ap_img = token; |
| 103 | break; |
| 104 | case CMD_SECOND_LOADER: |
| 105 | sld_img = token; |
| 106 | break; |
| 107 | case CMD_SIGNED_HDMI: |
| 108 | signed_hdmi = token; |
Peng Fan | 6609c26 | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 109 | break; |
| 110 | case CMD_DDR_FW: |
| 111 | /* Do nothing */ |
| 112 | break; |
Peng Fan | b8f1683 | 2019-09-16 03:09:39 +0000 | [diff] [blame] | 113 | case CMD_ROM_VERSION: |
| 114 | if (!strncmp(token, "v2", 2)) { |
| 115 | rom_version = ROM_V2; |
| 116 | ivt_offset = 0; |
| 117 | } else if (!strncmp(token, "v1", 2)) { |
| 118 | rom_version = ROM_V1; |
| 119 | } |
| 120 | break; |
| 121 | |
Peng Fan | 6609c26 | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | static void parse_cfg_fld(int32_t *cmd, char *token, |
| 126 | char *name, int lineno, int fld) |
| 127 | { |
| 128 | switch (fld) { |
| 129 | case CFG_COMMAND: |
| 130 | *cmd = get_table_entry_id(imx8mimage_cmds, |
| 131 | "imx8mimage commands", token); |
| 132 | if (*cmd < 0) { |
| 133 | fprintf(stderr, "Error: %s[%d] - Invalid command" "(%s)\n", |
| 134 | name, lineno, token); |
| 135 | exit(EXIT_FAILURE); |
| 136 | } |
Peng Fan | 4d655bf | 2019-08-06 10:07:26 +0000 | [diff] [blame] | 137 | switch (*cmd) { |
| 138 | case CMD_FIT: |
| 139 | using_fit = 1; |
| 140 | break; |
| 141 | } |
Peng Fan | 6609c26 | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 142 | break; |
| 143 | case CFG_REG_SIZE: |
| 144 | parse_cfg_cmd(*cmd, token, name, lineno); |
| 145 | break; |
| 146 | case CFG_REG_ADDRESS: |
| 147 | switch (*cmd) { |
| 148 | case CMD_LOADER: |
| 149 | ap_start_addr = get_cfg_value(token, name, lineno); |
| 150 | break; |
| 151 | case CMD_SECOND_LOADER: |
| 152 | sld_start_addr = get_cfg_value(token, name, lineno); |
| 153 | break; |
| 154 | } |
| 155 | break; |
| 156 | case CFG_REG_VALUE: |
| 157 | switch (*cmd) { |
| 158 | case CMD_SECOND_LOADER: |
| 159 | sld_src_off = get_cfg_value(token, name, lineno); |
| 160 | break; |
| 161 | } |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static uint32_t parse_cfg_file(char *name) |
| 168 | { |
| 169 | FILE *fd = NULL; |
| 170 | char *line = NULL; |
| 171 | char *token, *saveptr1, *saveptr2; |
| 172 | int lineno = 0; |
| 173 | int fld; |
| 174 | size_t len; |
| 175 | int32_t cmd; |
| 176 | |
| 177 | fd = fopen(name, "r"); |
| 178 | if (fd == 0) { |
| 179 | fprintf(stderr, "Error: %s - Can't open cfg file\n", name); |
| 180 | exit(EXIT_FAILURE); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Very simple parsing, line starting with # are comments |
| 185 | * and are dropped |
| 186 | */ |
| 187 | while ((getline(&line, &len, fd)) > 0) { |
| 188 | lineno++; |
| 189 | |
| 190 | token = strtok_r(line, "\r\n", &saveptr1); |
| 191 | if (!token) |
| 192 | continue; |
| 193 | |
| 194 | /* Check inside the single line */ |
| 195 | for (fld = CFG_COMMAND, cmd = CFG_INVALID, |
| 196 | line = token; ; line = NULL, fld++) { |
| 197 | token = strtok_r(line, " \t", &saveptr2); |
| 198 | if (!token) |
| 199 | break; |
| 200 | |
| 201 | /* Drop all text starting with '#' as comments */ |
| 202 | if (token[0] == '#') |
| 203 | break; |
| 204 | |
| 205 | parse_cfg_fld(&cmd, token, name, lineno, fld); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static void fill_zero(int ifd, int size, int offset) |
| 213 | { |
| 214 | int fill_size; |
| 215 | uint8_t zeros[4096]; |
| 216 | int ret; |
| 217 | |
| 218 | memset(zeros, 0, sizeof(zeros)); |
| 219 | |
| 220 | ret = lseek(ifd, offset, SEEK_SET); |
| 221 | if (ret < 0) { |
| 222 | fprintf(stderr, "%s seek: %s\n", __func__, strerror(errno)); |
| 223 | exit(EXIT_FAILURE); |
| 224 | } |
| 225 | |
| 226 | while (size) { |
| 227 | if (size > 4096) |
| 228 | fill_size = 4096; |
| 229 | else |
| 230 | fill_size = size; |
| 231 | |
| 232 | if (write(ifd, (char *)&zeros, fill_size) != fill_size) { |
| 233 | fprintf(stderr, "Write error: %s\n", |
| 234 | strerror(errno)); |
| 235 | exit(EXIT_FAILURE); |
| 236 | } |
| 237 | |
| 238 | size -= fill_size; |
| 239 | }; |
| 240 | } |
| 241 | |
| 242 | static void copy_file(int ifd, const char *datafile, int pad, int offset, |
| 243 | int datafile_offset) |
| 244 | { |
| 245 | int dfd; |
| 246 | struct stat sbuf; |
| 247 | unsigned char *ptr; |
| 248 | int tail; |
| 249 | int zero = 0; |
| 250 | uint8_t zeros[4096]; |
| 251 | int size, ret; |
| 252 | |
| 253 | memset(zeros, 0, sizeof(zeros)); |
| 254 | |
| 255 | dfd = open(datafile, O_RDONLY | O_BINARY); |
| 256 | if (dfd < 0) { |
| 257 | fprintf(stderr, "Can't open %s: %s\n", |
| 258 | datafile, strerror(errno)); |
| 259 | exit(EXIT_FAILURE); |
| 260 | } |
| 261 | |
| 262 | if (fstat(dfd, &sbuf) < 0) { |
| 263 | fprintf(stderr, "Can't stat %s: %s\n", |
| 264 | datafile, strerror(errno)); |
| 265 | exit(EXIT_FAILURE); |
| 266 | } |
| 267 | |
| 268 | ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); |
| 269 | if (ptr == MAP_FAILED) { |
| 270 | fprintf(stderr, "Can't read %s: %s\n", |
| 271 | datafile, strerror(errno)); |
| 272 | exit(EXIT_FAILURE); |
| 273 | } |
| 274 | |
| 275 | size = sbuf.st_size - datafile_offset; |
| 276 | ret = lseek(ifd, offset, SEEK_SET); |
| 277 | if (ret < 0) { |
| 278 | fprintf(stderr, "lseek ifd fail\n"); |
| 279 | exit(EXIT_FAILURE); |
| 280 | } |
| 281 | |
| 282 | if (write(ifd, ptr + datafile_offset, size) != size) { |
| 283 | fprintf(stderr, "Write error %s\n", |
| 284 | strerror(errno)); |
| 285 | exit(EXIT_FAILURE); |
| 286 | } |
| 287 | |
| 288 | tail = size % 4; |
| 289 | pad = pad - size; |
| 290 | if (pad == 1 && tail != 0) { |
| 291 | if (write(ifd, (char *)&zero, 4 - tail) != 4 - tail) { |
| 292 | fprintf(stderr, "Write error on %s\n", |
| 293 | strerror(errno)); |
| 294 | exit(EXIT_FAILURE); |
| 295 | } |
| 296 | } else if (pad > 1) { |
| 297 | while (pad > 0) { |
| 298 | int todo = sizeof(zeros); |
| 299 | |
| 300 | if (todo > pad) |
| 301 | todo = pad; |
| 302 | if (write(ifd, (char *)&zeros, todo) != todo) { |
| 303 | fprintf(stderr, "Write error: %s\n", |
| 304 | strerror(errno)); |
| 305 | exit(EXIT_FAILURE); |
| 306 | } |
| 307 | pad -= todo; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | munmap((void *)ptr, sbuf.st_size); |
| 312 | close(dfd); |
| 313 | } |
| 314 | |
| 315 | /* Return this IVT offset in the final output file */ |
| 316 | static int generate_ivt_for_fit(int fd, int fit_offset, uint32_t ep, |
| 317 | uint32_t *fit_load_addr) |
| 318 | { |
| 319 | image_header_t image_header; |
| 320 | int ret; |
| 321 | |
| 322 | uint32_t fit_size, load_addr; |
| 323 | int align_len = 64 - 1; /* 64 is cacheline size */ |
| 324 | |
| 325 | ret = lseek(fd, fit_offset, SEEK_SET); |
| 326 | if (ret < 0) { |
| 327 | fprintf(stderr, "lseek fd fail for fit\n"); |
| 328 | exit(EXIT_FAILURE); |
| 329 | } |
| 330 | |
| 331 | if (read(fd, (char *)&image_header, sizeof(image_header_t)) != |
| 332 | sizeof(image_header_t)) { |
| 333 | fprintf(stderr, "generate_ivt_for_fit read failed: %s\n", |
| 334 | strerror(errno)); |
| 335 | exit(EXIT_FAILURE); |
| 336 | } |
| 337 | |
| 338 | if (be32_to_cpu(image_header.ih_magic) != FDT_MAGIC) { |
| 339 | fprintf(stderr, "%s error: not a FIT file\n", __func__); |
| 340 | exit(EXIT_FAILURE); |
| 341 | } |
| 342 | |
| 343 | fit_size = fdt_totalsize(&image_header); |
Peng Fan | 6609c26 | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 344 | |
| 345 | fit_size = ALIGN(fit_size, ALIGN_SIZE); |
| 346 | |
| 347 | ret = lseek(fd, fit_offset + fit_size, SEEK_SET); |
| 348 | if (ret < 0) { |
| 349 | fprintf(stderr, "lseek fd fail for fit\n"); |
| 350 | exit(EXIT_FAILURE); |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * ep is the u-boot entry. SPL loads the FIT before the u-boot |
| 355 | * address. 0x2000 is for CSF_SIZE |
| 356 | */ |
| 357 | load_addr = (ep - (fit_size + CSF_SIZE) - 512 - align_len) & |
| 358 | ~align_len; |
| 359 | |
| 360 | flash_header_v2_t ivt_header = { { 0xd1, 0x2000, 0x40 }, |
| 361 | load_addr, 0, 0, 0, |
| 362 | (load_addr + fit_size), |
| 363 | (load_addr + fit_size + 0x20), |
| 364 | 0 }; |
| 365 | |
| 366 | if (write(fd, &ivt_header, sizeof(flash_header_v2_t)) != |
| 367 | sizeof(flash_header_v2_t)) { |
| 368 | fprintf(stderr, "IVT writing error on fit image\n"); |
| 369 | exit(EXIT_FAILURE); |
| 370 | } |
| 371 | |
| 372 | *fit_load_addr = load_addr; |
| 373 | |
| 374 | return fit_offset + fit_size; |
| 375 | } |
| 376 | |
| 377 | static void dump_header_v2(imx_header_v3_t *imx_header, int index) |
| 378 | { |
| 379 | const char *ivt_name[2] = {"HDMI FW", "LOADER IMAGE"}; |
| 380 | |
| 381 | fprintf(stdout, "========= IVT HEADER [%s] =========\n", |
| 382 | ivt_name[index]); |
| 383 | fprintf(stdout, "header.tag: \t\t0x%x\n", |
| 384 | imx_header[index].fhdr.header.tag); |
| 385 | fprintf(stdout, "header.length: \t\t0x%x\n", |
| 386 | imx_header[index].fhdr.header.length); |
| 387 | fprintf(stdout, "header.version: \t0x%x\n", |
| 388 | imx_header[index].fhdr.header.version); |
| 389 | fprintf(stdout, "entry: \t\t\t0x%x\n", |
| 390 | imx_header[index].fhdr.entry); |
| 391 | fprintf(stdout, "reserved1: \t\t0x%x\n", |
| 392 | imx_header[index].fhdr.reserved1); |
| 393 | fprintf(stdout, "dcd_ptr: \t\t0x%x\n", |
| 394 | imx_header[index].fhdr.dcd_ptr); |
| 395 | fprintf(stdout, "boot_data_ptr: \t\t0x%x\n", |
| 396 | imx_header[index].fhdr.boot_data_ptr); |
| 397 | fprintf(stdout, "self: \t\t\t0x%x\n", |
| 398 | imx_header[index].fhdr.self); |
| 399 | fprintf(stdout, "csf: \t\t\t0x%x\n", |
| 400 | imx_header[index].fhdr.csf); |
| 401 | fprintf(stdout, "reserved2: \t\t0x%x\n", |
| 402 | imx_header[index].fhdr.reserved2); |
| 403 | |
| 404 | fprintf(stdout, "boot_data.start: \t0x%x\n", |
| 405 | imx_header[index].boot_data.start); |
| 406 | fprintf(stdout, "boot_data.size: \t0x%x\n", |
| 407 | imx_header[index].boot_data.size); |
| 408 | fprintf(stdout, "boot_data.plugin: \t0x%x\n", |
| 409 | imx_header[index].boot_data.plugin); |
| 410 | } |
| 411 | |
| 412 | void build_image(int ofd) |
| 413 | { |
| 414 | int file_off, header_hdmi_off = 0, header_image_off; |
| 415 | int hdmi_fd, ap_fd, sld_fd; |
| 416 | uint32_t sld_load_addr = 0; |
| 417 | uint32_t csf_off, sld_csf_off = 0; |
| 418 | int ret; |
| 419 | struct stat sbuf; |
| 420 | |
| 421 | if (!ap_img) { |
| 422 | fprintf(stderr, "No LOADER image specificed\n"); |
| 423 | exit(EXIT_FAILURE); |
| 424 | } |
| 425 | |
| 426 | file_off = 0; |
| 427 | |
| 428 | if (signed_hdmi) { |
| 429 | header_hdmi_off = file_off + ivt_offset; |
| 430 | |
| 431 | hdmi_fd = open(signed_hdmi, O_RDONLY | O_BINARY); |
| 432 | if (hdmi_fd < 0) { |
| 433 | fprintf(stderr, "%s: Can't open: %s\n", |
| 434 | signed_hdmi, strerror(errno)); |
| 435 | exit(EXIT_FAILURE); |
| 436 | } |
| 437 | |
| 438 | if (fstat(hdmi_fd, &sbuf) < 0) { |
| 439 | fprintf(stderr, "%s: Can't stat: %s\n", |
| 440 | signed_hdmi, strerror(errno)); |
| 441 | exit(EXIT_FAILURE); |
| 442 | } |
| 443 | close(hdmi_fd); |
| 444 | |
| 445 | /* |
| 446 | * Aligned to 104KB = 92KB FW image + 0x8000 |
| 447 | * (IVT and alignment) + 0x4000 (second IVT + CSF) |
| 448 | */ |
| 449 | file_off += ALIGN(sbuf.st_size, |
| 450 | HDMI_FW_SIZE + 0x2000 + 0x1000); |
| 451 | } |
| 452 | |
| 453 | header_image_off = file_off + ivt_offset; |
| 454 | |
| 455 | ap_fd = open(ap_img, O_RDONLY | O_BINARY); |
| 456 | if (ap_fd < 0) { |
| 457 | fprintf(stderr, "%s: Can't open: %s\n", |
| 458 | ap_img, strerror(errno)); |
| 459 | exit(EXIT_FAILURE); |
| 460 | } |
| 461 | if (fstat(ap_fd, &sbuf) < 0) { |
| 462 | fprintf(stderr, "%s: Can't stat: %s\n", |
| 463 | ap_img, strerror(errno)); |
| 464 | exit(EXIT_FAILURE); |
| 465 | } |
| 466 | close(ap_fd); |
| 467 | |
| 468 | imx_header[IMAGE_IVT_ID].fhdr.header.tag = IVT_HEADER_TAG; /* 0xD1 */ |
| 469 | imx_header[IMAGE_IVT_ID].fhdr.header.length = |
| 470 | cpu_to_be16(sizeof(flash_header_v2_t)); |
| 471 | imx_header[IMAGE_IVT_ID].fhdr.header.version = IVT_VERSION_V3; /* 0x41 */ |
| 472 | imx_header[IMAGE_IVT_ID].fhdr.entry = ap_start_addr; |
| 473 | imx_header[IMAGE_IVT_ID].fhdr.self = ap_start_addr - |
| 474 | sizeof(imx_header_v3_t); |
| 475 | imx_header[IMAGE_IVT_ID].fhdr.dcd_ptr = 0; |
| 476 | imx_header[IMAGE_IVT_ID].fhdr.boot_data_ptr = |
| 477 | imx_header[IMAGE_IVT_ID].fhdr.self + |
| 478 | offsetof(imx_header_v3_t, boot_data); |
| 479 | imx_header[IMAGE_IVT_ID].boot_data.start = |
| 480 | imx_header[IMAGE_IVT_ID].fhdr.self - ivt_offset; |
| 481 | imx_header[IMAGE_IVT_ID].boot_data.size = |
| 482 | ALIGN(sbuf.st_size + sizeof(imx_header_v3_t) + ivt_offset, |
| 483 | sector_size); |
| 484 | |
| 485 | image_off = header_image_off + sizeof(imx_header_v3_t); |
| 486 | file_off += imx_header[IMAGE_IVT_ID].boot_data.size; |
| 487 | |
| 488 | imx_header[IMAGE_IVT_ID].boot_data.plugin = 0; |
| 489 | imx_header[IMAGE_IVT_ID].fhdr.csf = |
| 490 | imx_header[IMAGE_IVT_ID].boot_data.start + |
| 491 | imx_header[IMAGE_IVT_ID].boot_data.size; |
| 492 | |
| 493 | imx_header[IMAGE_IVT_ID].boot_data.size += CSF_SIZE; /* 8K region dummy CSF */ |
| 494 | |
| 495 | csf_off = file_off; |
| 496 | file_off += CSF_SIZE; |
| 497 | |
| 498 | /* Second boot loader image */ |
| 499 | if (sld_img) { |
| 500 | if (!using_fit) { |
| 501 | fprintf(stderr, "Not support no fit\n"); |
| 502 | exit(EXIT_FAILURE); |
| 503 | } else { |
| 504 | sld_header_off = sld_src_off - rom_image_offset; |
| 505 | /* |
| 506 | * Record the second bootloader relative offset in |
| 507 | * image's IVT reserved1 |
| 508 | */ |
Peng Fan | b8f1683 | 2019-09-16 03:09:39 +0000 | [diff] [blame] | 509 | if (rom_version == ROM_V1) { |
| 510 | imx_header[IMAGE_IVT_ID].fhdr.reserved1 = |
| 511 | sld_header_off - header_image_off; |
| 512 | } |
Peng Fan | 6609c26 | 2018-11-20 10:19:36 +0000 | [diff] [blame] | 513 | sld_fd = open(sld_img, O_RDONLY | O_BINARY); |
| 514 | if (sld_fd < 0) { |
| 515 | fprintf(stderr, "%s: Can't open: %s\n", |
| 516 | sld_img, strerror(errno)); |
| 517 | exit(EXIT_FAILURE); |
| 518 | } |
| 519 | |
| 520 | if (fstat(sld_fd, &sbuf) < 0) { |
| 521 | fprintf(stderr, "%s: Can't stat: %s\n", |
| 522 | sld_img, strerror(errno)); |
| 523 | exit(EXIT_FAILURE); |
| 524 | } |
| 525 | |
| 526 | close(sld_fd); |
| 527 | |
| 528 | file_off = sld_header_off; |
| 529 | file_off += sbuf.st_size + sizeof(image_header_t); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | if (signed_hdmi) { |
| 534 | header_hdmi_off -= ivt_offset; |
| 535 | ret = lseek(ofd, header_hdmi_off, SEEK_SET); |
| 536 | if (ret < 0) { |
| 537 | fprintf(stderr, "lseek ofd fail for hdmi\n"); |
| 538 | exit(EXIT_FAILURE); |
| 539 | } |
| 540 | |
| 541 | /* The signed HDMI FW has 0x400 IVT offset, need remove it */ |
| 542 | copy_file(ofd, signed_hdmi, 0, header_hdmi_off, 0x400); |
| 543 | } |
| 544 | |
| 545 | /* Main Image */ |
| 546 | header_image_off -= ivt_offset; |
| 547 | image_off -= ivt_offset; |
| 548 | ret = lseek(ofd, header_image_off, SEEK_SET); |
| 549 | if (ret < 0) { |
| 550 | fprintf(stderr, "lseek ofd fail\n"); |
| 551 | exit(EXIT_FAILURE); |
| 552 | } |
| 553 | |
| 554 | /* Write image header */ |
| 555 | if (write(ofd, &imx_header[IMAGE_IVT_ID], sizeof(imx_header_v3_t)) != |
| 556 | sizeof(imx_header_v3_t)) { |
| 557 | fprintf(stderr, "error writing image hdr\n"); |
| 558 | exit(1); |
| 559 | } |
| 560 | |
| 561 | copy_file(ofd, ap_img, 0, image_off, 0); |
| 562 | |
| 563 | csf_off -= ivt_offset; |
| 564 | fill_zero(ofd, CSF_SIZE, csf_off); |
| 565 | |
| 566 | if (sld_img) { |
| 567 | sld_header_off -= ivt_offset; |
| 568 | ret = lseek(ofd, sld_header_off, SEEK_SET); |
| 569 | if (ret < 0) { |
| 570 | fprintf(stderr, "lseek ofd fail for sld_img\n"); |
| 571 | exit(EXIT_FAILURE); |
| 572 | } |
| 573 | |
| 574 | /* Write image header */ |
| 575 | if (!using_fit) { |
| 576 | /* TODO */ |
| 577 | } else { |
| 578 | copy_file(ofd, sld_img, 0, sld_header_off, 0); |
| 579 | sld_csf_off = |
| 580 | generate_ivt_for_fit(ofd, sld_header_off, |
| 581 | sld_start_addr, |
| 582 | &sld_load_addr) + 0x20; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if (!signed_hdmi) |
| 587 | dump_header_v2(imx_header, 0); |
| 588 | dump_header_v2(imx_header, 1); |
| 589 | |
| 590 | fprintf(stdout, "========= OFFSET dump ========="); |
| 591 | if (signed_hdmi) { |
| 592 | fprintf(stdout, "\nSIGNED HDMI FW:\n"); |
| 593 | fprintf(stdout, " header_hdmi_off \t0x%x\n", |
| 594 | header_hdmi_off); |
| 595 | } |
| 596 | |
| 597 | fprintf(stdout, "\nLoader IMAGE:\n"); |
| 598 | fprintf(stdout, " header_image_off \t0x%x\n image_off \t\t0x%x\n csf_off \t\t0x%x\n", |
| 599 | header_image_off, image_off, csf_off); |
| 600 | fprintf(stdout, " spl hab block: \t0x%x 0x%x 0x%x\n", |
| 601 | imx_header[IMAGE_IVT_ID].fhdr.self, header_image_off, |
| 602 | csf_off - header_image_off); |
| 603 | |
| 604 | fprintf(stdout, "\nSecond Loader IMAGE:\n"); |
| 605 | fprintf(stdout, " sld_header_off \t0x%x\n", |
| 606 | sld_header_off); |
| 607 | fprintf(stdout, " sld_csf_off \t\t0x%x\n", |
| 608 | sld_csf_off); |
| 609 | fprintf(stdout, " sld hab block: \t0x%x 0x%x 0x%x\n", |
| 610 | sld_load_addr, sld_header_off, sld_csf_off - sld_header_off); |
| 611 | } |
| 612 | |
| 613 | int imx8mimage_copy_image(int outfd, struct image_tool_params *mparams) |
| 614 | { |
| 615 | /* |
| 616 | * SECO FW is a container image, this is to calculate the |
| 617 | * 2nd container offset. |
| 618 | */ |
| 619 | fprintf(stdout, "parsing %s\n", mparams->imagename); |
| 620 | parse_cfg_file(mparams->imagename); |
| 621 | |
| 622 | build_image(outfd); |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * imx8mimage parameters |
| 629 | */ |
| 630 | U_BOOT_IMAGE_TYPE( |
| 631 | imx8mimage, |
| 632 | "NXP i.MX8M Boot Image support", |
| 633 | 0, |
| 634 | NULL, |
| 635 | imx8mimage_check_params, |
| 636 | NULL, |
| 637 | imx8mimage_print_header, |
| 638 | imx8mimage_set_header, |
| 639 | NULL, |
| 640 | imx8mimage_check_image_types, |
| 641 | NULL, |
| 642 | NULL |
| 643 | ); |