1 /* 2 * (C) Copyright 2009 3 * Stefano Babic, DENX Software Engineering, sbabic@denx.de. 4 * 5 * (C) Copyright 2008 6 * Marvell Semiconductor <www.marvell.com> 7 * Written-by: Prafulla Wadaskar <prafulla@marvell.com> 8 * 9 * SPDX-License-Identifier: GPL-2.0+ 10 */ 11 12 #include "imagetool.h" 13 #include <image.h> 14 #include "imximage.h" 15 16 #define UNDEFINED 0xFFFFFFFF 17 18 /* 19 * Supported commands for configuration file 20 */ 21 static table_entry_t imximage_cmds[] = { 22 {CMD_BOOT_FROM, "BOOT_FROM", "boot command", }, 23 {CMD_BOOT_OFFSET, "BOOT_OFFSET", "Boot offset", }, 24 {CMD_WRITE_DATA, "DATA", "Reg Write Data", }, 25 {CMD_WRITE_CLR_BIT, "CLR_BIT", "Reg clear bit", }, 26 {CMD_CHECK_BITS_SET, "CHECK_BITS_SET", "Reg Check bits set", }, 27 {CMD_CHECK_BITS_CLR, "CHECK_BITS_CLR", "Reg Check bits clr", }, 28 {CMD_CSF, "CSF", "Command Sequence File", }, 29 {CMD_IMAGE_VERSION, "IMAGE_VERSION", "image version", }, 30 {CMD_PLUGIN, "PLUGIN", "file plugin_addr", }, 31 {-1, "", "", }, 32 }; 33 34 /* 35 * Supported Boot options for configuration file 36 * this is needed to set the correct flash offset 37 */ 38 static table_entry_t imximage_boot_offset[] = { 39 {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",}, 40 {FLASH_OFFSET_NAND, "nand", "NAND Flash", }, 41 {FLASH_OFFSET_NOR, "nor", "NOR Flash", }, 42 {FLASH_OFFSET_SATA, "sata", "SATA Disk", }, 43 {FLASH_OFFSET_SD, "sd", "SD Card", }, 44 {FLASH_OFFSET_SPI, "spi", "SPI Flash", }, 45 {FLASH_OFFSET_QSPI, "qspi", "QSPI NOR Flash",}, 46 {-1, "", "Invalid", }, 47 }; 48 49 /* 50 * Supported Boot options for configuration file 51 * this is needed to determine the initial load size 52 */ 53 static table_entry_t imximage_boot_loadsize[] = { 54 {FLASH_LOADSIZE_ONENAND, "onenand", "OneNAND Flash",}, 55 {FLASH_LOADSIZE_NAND, "nand", "NAND Flash", }, 56 {FLASH_LOADSIZE_NOR, "nor", "NOR Flash", }, 57 {FLASH_LOADSIZE_SATA, "sata", "SATA Disk", }, 58 {FLASH_LOADSIZE_SD, "sd", "SD Card", }, 59 {FLASH_LOADSIZE_SPI, "spi", "SPI Flash", }, 60 {FLASH_LOADSIZE_QSPI, "qspi", "QSPI NOR Flash",}, 61 {-1, "", "Invalid", }, 62 }; 63 64 /* 65 * IMXIMAGE version definition for i.MX chips 66 */ 67 static table_entry_t imximage_versions[] = { 68 {IMXIMAGE_V1, "", " (i.MX25/35/51 compatible)", }, 69 {IMXIMAGE_V2, "", " (i.MX53/6/7 compatible)", }, 70 {-1, "", " (Invalid)", }, 71 }; 72 73 static struct imx_header imximage_header; 74 static uint32_t imximage_version; 75 /* 76 * Image Vector Table Offset 77 * Initialized to a wrong not 4-bytes aligned address to 78 * check if it is was set by the cfg file. 79 */ 80 static uint32_t imximage_ivt_offset = UNDEFINED; 81 static uint32_t imximage_csf_size = UNDEFINED; 82 /* Initial Load Region Size */ 83 static uint32_t imximage_init_loadsize; 84 static uint32_t imximage_iram_free_start; 85 static uint32_t imximage_plugin_size; 86 static uint32_t plugin_image; 87 88 static set_dcd_val_t set_dcd_val; 89 static set_dcd_param_t set_dcd_param; 90 static set_dcd_rst_t set_dcd_rst; 91 static set_imx_hdr_t set_imx_hdr; 92 static uint32_t max_dcd_entries; 93 static uint32_t *header_size_ptr; 94 static uint32_t *csf_ptr; 95 96 static uint32_t get_cfg_value(char *token, char *name, int linenr) 97 { 98 char *endptr; 99 uint32_t value; 100 101 errno = 0; 102 value = strtoul(token, &endptr, 16); 103 if (errno || (token == endptr)) { 104 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n", 105 name, linenr, token); 106 exit(EXIT_FAILURE); 107 } 108 return value; 109 } 110 111 static uint32_t detect_imximage_version(struct imx_header *imx_hdr) 112 { 113 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1; 114 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2; 115 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr; 116 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr; 117 118 /* Try to detect V1 */ 119 if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) && 120 (hdr_v1->dcd_table.preamble.barker == DCD_BARKER)) 121 return IMXIMAGE_V1; 122 123 /* Try to detect V2 */ 124 if ((fhdr_v2->header.tag == IVT_HEADER_TAG) && 125 (hdr_v2->data.dcd_table.header.tag == DCD_HEADER_TAG)) 126 return IMXIMAGE_V2; 127 128 if ((fhdr_v2->header.tag == IVT_HEADER_TAG) && 129 hdr_v2->boot_data.plugin) 130 return IMXIMAGE_V2; 131 132 return IMXIMAGE_VER_INVALID; 133 } 134 135 static void err_imximage_version(int version) 136 { 137 fprintf(stderr, 138 "Error: Unsupported imximage version:%d\n", version); 139 140 exit(EXIT_FAILURE); 141 } 142 143 static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno, 144 int fld, uint32_t value, uint32_t off) 145 { 146 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table; 147 148 switch (fld) { 149 case CFG_REG_SIZE: 150 /* Byte, halfword, word */ 151 if ((value != 1) && (value != 2) && (value != 4)) { 152 fprintf(stderr, "Error: %s[%d] - " 153 "Invalid register size " "(%d)\n", 154 name, lineno, value); 155 exit(EXIT_FAILURE); 156 } 157 dcd_v1->addr_data[off].type = value; 158 break; 159 case CFG_REG_ADDRESS: 160 dcd_v1->addr_data[off].addr = value; 161 break; 162 case CFG_REG_VALUE: 163 dcd_v1->addr_data[off].value = value; 164 break; 165 default: 166 break; 167 168 } 169 } 170 171 static struct dcd_v2_cmd *gd_last_cmd; 172 173 static void set_dcd_param_v2(struct imx_header *imxhdr, uint32_t dcd_len, 174 int32_t cmd) 175 { 176 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.data.dcd_table; 177 struct dcd_v2_cmd *d = gd_last_cmd; 178 struct dcd_v2_cmd *d2; 179 int len; 180 181 if (!d) 182 d = &dcd_v2->dcd_cmd; 183 d2 = d; 184 len = be16_to_cpu(d->write_dcd_command.length); 185 if (len > 4) 186 d2 = (struct dcd_v2_cmd *)(((char *)d) + len); 187 188 switch (cmd) { 189 case CMD_WRITE_DATA: 190 if ((d->write_dcd_command.tag == DCD_WRITE_DATA_COMMAND_TAG) && 191 (d->write_dcd_command.param == DCD_WRITE_DATA_PARAM)) 192 break; 193 d = d2; 194 d->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG; 195 d->write_dcd_command.length = cpu_to_be16(4); 196 d->write_dcd_command.param = DCD_WRITE_DATA_PARAM; 197 break; 198 case CMD_WRITE_CLR_BIT: 199 if ((d->write_dcd_command.tag == DCD_WRITE_DATA_COMMAND_TAG) && 200 (d->write_dcd_command.param == DCD_WRITE_CLR_BIT_PARAM)) 201 break; 202 d = d2; 203 d->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG; 204 d->write_dcd_command.length = cpu_to_be16(4); 205 d->write_dcd_command.param = DCD_WRITE_CLR_BIT_PARAM; 206 break; 207 /* 208 * Check data command only supports one entry, 209 */ 210 case CMD_CHECK_BITS_SET: 211 d = d2; 212 d->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG; 213 d->write_dcd_command.length = cpu_to_be16(4); 214 d->write_dcd_command.param = DCD_CHECK_BITS_SET_PARAM; 215 break; 216 case CMD_CHECK_BITS_CLR: 217 d = d2; 218 d->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG; 219 d->write_dcd_command.length = cpu_to_be16(4); 220 d->write_dcd_command.param = DCD_CHECK_BITS_CLR_PARAM; 221 break; 222 default: 223 break; 224 } 225 gd_last_cmd = d; 226 } 227 228 static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno, 229 int fld, uint32_t value, uint32_t off) 230 { 231 struct dcd_v2_cmd *d = gd_last_cmd; 232 int len; 233 234 len = be16_to_cpu(d->write_dcd_command.length); 235 off = (len - 4) >> 3; 236 237 switch (fld) { 238 case CFG_REG_ADDRESS: 239 d->addr_data[off].addr = cpu_to_be32(value); 240 break; 241 case CFG_REG_VALUE: 242 d->addr_data[off].value = cpu_to_be32(value); 243 off++; 244 d->write_dcd_command.length = cpu_to_be16((off << 3) + 4); 245 break; 246 default: 247 break; 248 249 } 250 } 251 252 /* 253 * Complete setting up the rest field of DCD of V1 254 * such as barker code and DCD data length. 255 */ 256 static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len, 257 char *name, int lineno) 258 { 259 dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table; 260 261 dcd_v1->preamble.barker = DCD_BARKER; 262 dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t); 263 } 264 265 /* 266 * Complete setting up the reset field of DCD of V2 267 * such as DCD tag, version, length, etc. 268 */ 269 static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len, 270 char *name, int lineno) 271 { 272 if (!imxhdr->header.hdr_v2.boot_data.plugin) { 273 dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.data.dcd_table; 274 struct dcd_v2_cmd *d = gd_last_cmd; 275 int len; 276 277 if (!d) 278 d = &dcd_v2->dcd_cmd; 279 len = be16_to_cpu(d->write_dcd_command.length); 280 if (len > 4) 281 d = (struct dcd_v2_cmd *)(((char *)d) + len); 282 283 len = (char *)d - (char *)&dcd_v2->header; 284 285 dcd_v2->header.tag = DCD_HEADER_TAG; 286 dcd_v2->header.length = cpu_to_be16(len); 287 dcd_v2->header.version = DCD_VERSION; 288 } 289 } 290 291 static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len, 292 uint32_t entry_point, uint32_t flash_offset) 293 { 294 imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1; 295 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr; 296 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table; 297 uint32_t hdr_base; 298 uint32_t header_length = (((char *)&dcd_v1->addr_data[dcd_len].addr) 299 - ((char *)imxhdr)); 300 301 /* Set magic number */ 302 fhdr_v1->app_code_barker = APP_CODE_BARKER; 303 304 /* TODO: check i.MX image V1 handling, for now use 'old' style */ 305 hdr_base = entry_point - 4096; 306 fhdr_v1->app_dest_ptr = hdr_base - flash_offset; 307 fhdr_v1->app_code_jump_vector = entry_point; 308 309 fhdr_v1->dcd_ptr_ptr = hdr_base + offsetof(flash_header_v1_t, dcd_ptr); 310 fhdr_v1->dcd_ptr = hdr_base + offsetof(imx_header_v1_t, dcd_table); 311 312 /* Security feature are not supported */ 313 fhdr_v1->app_code_csf = 0; 314 fhdr_v1->super_root_key = 0; 315 header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4); 316 } 317 318 static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len, 319 uint32_t entry_point, uint32_t flash_offset) 320 { 321 imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2; 322 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr; 323 uint32_t hdr_base; 324 325 /* Set magic number */ 326 fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */ 327 fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t)); 328 fhdr_v2->header.version = IVT_VERSION; /* 0x40 */ 329 330 if (!hdr_v2->boot_data.plugin) { 331 fhdr_v2->entry = entry_point; 332 fhdr_v2->reserved1 = 0; 333 fhdr_v2->reserved1 = 0; 334 hdr_base = entry_point - imximage_init_loadsize + 335 flash_offset; 336 fhdr_v2->self = hdr_base; 337 if (dcd_len > 0) 338 fhdr_v2->dcd_ptr = hdr_base + 339 offsetof(imx_header_v2_t, data); 340 else 341 fhdr_v2->dcd_ptr = 0; 342 fhdr_v2->boot_data_ptr = hdr_base 343 + offsetof(imx_header_v2_t, boot_data); 344 hdr_v2->boot_data.start = entry_point - imximage_init_loadsize; 345 346 fhdr_v2->csf = 0; 347 348 header_size_ptr = &hdr_v2->boot_data.size; 349 csf_ptr = &fhdr_v2->csf; 350 } else { 351 imx_header_v2_t *next_hdr_v2; 352 flash_header_v2_t *next_fhdr_v2; 353 354 if (imximage_csf_size != 0) { 355 fprintf(stderr, "Error: Header v2: SECURE_BOOT is only supported in DCD mode!"); 356 exit(EXIT_FAILURE); 357 } 358 359 fhdr_v2->entry = imximage_iram_free_start + 360 flash_offset + sizeof(flash_header_v2_t) + 361 sizeof(boot_data_t); 362 363 fhdr_v2->reserved1 = 0; 364 fhdr_v2->reserved2 = 0; 365 fhdr_v2->self = imximage_iram_free_start + flash_offset; 366 367 fhdr_v2->dcd_ptr = 0; 368 369 fhdr_v2->boot_data_ptr = fhdr_v2->self + 370 offsetof(imx_header_v2_t, boot_data); 371 372 hdr_v2->boot_data.start = imximage_iram_free_start; 373 /* 374 * The actural size of plugin image is "imximage_plugin_size + 375 * sizeof(flash_header_v2_t) + sizeof(boot_data_t)", plus the 376 * flash_offset space.The ROM code only need to copy this size 377 * to run the plugin code. However, later when copy the whole 378 * U-Boot image to DDR, the ROM code use memcpy to copy the 379 * first part of the image, and use the storage read function 380 * to get the remaining part. This requires the dividing point 381 * must be multiple of storage sector size. Here we set the 382 * first section to be MAX_PLUGIN_CODE_SIZE(64KB) for this 383 * purpose. 384 */ 385 hdr_v2->boot_data.size = MAX_PLUGIN_CODE_SIZE; 386 387 /* Security feature are not supported */ 388 fhdr_v2->csf = 0; 389 390 next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 + 391 imximage_plugin_size); 392 393 next_fhdr_v2 = &next_hdr_v2->fhdr; 394 395 next_fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */ 396 next_fhdr_v2->header.length = 397 cpu_to_be16(sizeof(flash_header_v2_t)); 398 next_fhdr_v2->header.version = IVT_VERSION; /* 0x40 */ 399 400 next_fhdr_v2->entry = entry_point; 401 hdr_base = entry_point - sizeof(struct imx_header); 402 next_fhdr_v2->reserved1 = 0; 403 next_fhdr_v2->reserved2 = 0; 404 next_fhdr_v2->self = hdr_base + imximage_plugin_size; 405 406 next_fhdr_v2->dcd_ptr = 0; 407 next_fhdr_v2->boot_data_ptr = next_fhdr_v2->self + 408 offsetof(imx_header_v2_t, boot_data); 409 410 next_hdr_v2->boot_data.start = hdr_base - flash_offset; 411 412 header_size_ptr = &next_hdr_v2->boot_data.size; 413 414 next_hdr_v2->boot_data.plugin = 0; 415 416 next_fhdr_v2->csf = 0; 417 } 418 } 419 420 static void set_hdr_func(void) 421 { 422 switch (imximage_version) { 423 case IMXIMAGE_V1: 424 set_dcd_val = set_dcd_val_v1; 425 set_dcd_param = NULL; 426 set_dcd_rst = set_dcd_rst_v1; 427 set_imx_hdr = set_imx_hdr_v1; 428 max_dcd_entries = MAX_HW_CFG_SIZE_V1; 429 break; 430 case IMXIMAGE_V2: 431 gd_last_cmd = NULL; 432 set_dcd_val = set_dcd_val_v2; 433 set_dcd_param = set_dcd_param_v2; 434 set_dcd_rst = set_dcd_rst_v2; 435 set_imx_hdr = set_imx_hdr_v2; 436 max_dcd_entries = MAX_HW_CFG_SIZE_V2; 437 break; 438 default: 439 err_imximage_version(imximage_version); 440 break; 441 } 442 } 443 444 static void print_hdr_v1(struct imx_header *imx_hdr) 445 { 446 imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1; 447 flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr; 448 dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table; 449 uint32_t size, length, ver; 450 451 size = dcd_v1->preamble.length; 452 if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) { 453 fprintf(stderr, 454 "Error: Image corrupt DCD size %d exceed maximum %d\n", 455 (uint32_t)(size / sizeof(dcd_type_addr_data_t)), 456 MAX_HW_CFG_SIZE_V1); 457 exit(EXIT_FAILURE); 458 } 459 460 length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t); 461 ver = detect_imximage_version(imx_hdr); 462 463 printf("Image Type: Freescale IMX Boot Image\n"); 464 printf("Image Ver: %x", ver); 465 printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver)); 466 printf("Data Size: "); 467 genimg_print_size(dcd_v1->addr_data[length].type); 468 printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr); 469 printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector); 470 } 471 472 static void print_hdr_v2(struct imx_header *imx_hdr) 473 { 474 imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2; 475 flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr; 476 dcd_v2_t *dcd_v2 = &hdr_v2->data.dcd_table; 477 uint32_t size, version, plugin; 478 479 plugin = hdr_v2->boot_data.plugin; 480 if (!plugin) { 481 size = be16_to_cpu(dcd_v2->header.length); 482 if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) { 483 fprintf(stderr, 484 "Error: Image corrupt DCD size %d exceed maximum %d\n", 485 (uint32_t)(size / sizeof(dcd_addr_data_t)), 486 MAX_HW_CFG_SIZE_V2); 487 exit(EXIT_FAILURE); 488 } 489 } 490 491 version = detect_imximage_version(imx_hdr); 492 493 printf("Image Type: Freescale IMX Boot Image\n"); 494 printf("Image Ver: %x", version); 495 printf("%s\n", get_table_entry_name(imximage_versions, NULL, version)); 496 printf("Mode: %s\n", plugin ? "PLUGIN" : "DCD"); 497 if (!plugin) { 498 printf("Data Size: "); 499 genimg_print_size(hdr_v2->boot_data.size); 500 printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr); 501 printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry); 502 if (fhdr_v2->csf && (imximage_ivt_offset != UNDEFINED) && 503 (imximage_csf_size != UNDEFINED)) { 504 printf("HAB Blocks: %08x %08x %08x\n", 505 (uint32_t)fhdr_v2->self, 0, 506 hdr_v2->boot_data.size - imximage_ivt_offset - 507 imximage_csf_size); 508 } 509 } else { 510 imx_header_v2_t *next_hdr_v2; 511 flash_header_v2_t *next_fhdr_v2; 512 513 /*First Header*/ 514 printf("Plugin Data Size: "); 515 genimg_print_size(hdr_v2->boot_data.size); 516 printf("Plugin Code Size: "); 517 genimg_print_size(imximage_plugin_size); 518 printf("Plugin Load Address: %08x\n", hdr_v2->boot_data.start); 519 printf("Plugin Entry Point: %08x\n", (uint32_t)fhdr_v2->entry); 520 521 /*Second Header*/ 522 next_hdr_v2 = (imx_header_v2_t *)((char *)hdr_v2 + 523 imximage_plugin_size); 524 next_fhdr_v2 = &next_hdr_v2->fhdr; 525 printf("U-Boot Data Size: "); 526 genimg_print_size(next_hdr_v2->boot_data.size); 527 printf("U-Boot Load Address: %08x\n", 528 next_hdr_v2->boot_data.start); 529 printf("U-Boot Entry Point: %08x\n", 530 (uint32_t)next_fhdr_v2->entry); 531 } 532 } 533 534 static void copy_plugin_code(struct imx_header *imxhdr, char *plugin_file) 535 { 536 int ifd = -1; 537 struct stat sbuf; 538 char *plugin_buf = imxhdr->header.hdr_v2.data.plugin_code; 539 char *ptr; 540 541 ifd = open(plugin_file, O_RDONLY|O_BINARY); 542 if (fstat(ifd, &sbuf) < 0) { 543 fprintf(stderr, "Can't stat %s: %s\n", 544 plugin_file, 545 strerror(errno)); 546 exit(EXIT_FAILURE); 547 } 548 549 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, ifd, 0); 550 if (ptr == MAP_FAILED) { 551 fprintf(stderr, "Can't read %s: %s\n", 552 plugin_file, 553 strerror(errno)); 554 exit(EXIT_FAILURE); 555 } 556 557 if (sbuf.st_size > MAX_PLUGIN_CODE_SIZE) { 558 printf("plugin binary size too large\n"); 559 exit(EXIT_FAILURE); 560 } 561 562 memcpy(plugin_buf, ptr, sbuf.st_size); 563 imximage_plugin_size = sbuf.st_size; 564 565 (void) munmap((void *)ptr, sbuf.st_size); 566 (void) close(ifd); 567 568 imxhdr->header.hdr_v2.boot_data.plugin = 1; 569 } 570 571 static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token, 572 char *name, int lineno, int fld, int dcd_len) 573 { 574 int value; 575 static int cmd_ver_first = ~0; 576 577 switch (cmd) { 578 case CMD_IMAGE_VERSION: 579 imximage_version = get_cfg_value(token, name, lineno); 580 if (cmd_ver_first == 0) { 581 fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION " 582 "command need be the first before other " 583 "valid command in the file\n", name, lineno); 584 exit(EXIT_FAILURE); 585 } 586 cmd_ver_first = 1; 587 set_hdr_func(); 588 break; 589 case CMD_BOOT_FROM: 590 imximage_ivt_offset = get_table_entry_id(imximage_boot_offset, 591 "imximage boot option", token); 592 if (imximage_ivt_offset == -1) { 593 fprintf(stderr, "Error: %s[%d] -Invalid boot device" 594 "(%s)\n", name, lineno, token); 595 exit(EXIT_FAILURE); 596 } 597 598 imximage_init_loadsize = 599 get_table_entry_id(imximage_boot_loadsize, 600 "imximage boot option", token); 601 602 if (imximage_init_loadsize == -1) { 603 fprintf(stderr, 604 "Error: %s[%d] -Invalid boot device(%s)\n", 605 name, lineno, token); 606 exit(EXIT_FAILURE); 607 } 608 609 /* 610 * The SOC loads from the storage starting at address 0 611 * then ensures that the load size contains the offset 612 */ 613 if (imximage_init_loadsize < imximage_ivt_offset) 614 imximage_init_loadsize = imximage_ivt_offset; 615 if (unlikely(cmd_ver_first != 1)) 616 cmd_ver_first = 0; 617 break; 618 case CMD_BOOT_OFFSET: 619 imximage_ivt_offset = get_cfg_value(token, name, lineno); 620 if (unlikely(cmd_ver_first != 1)) 621 cmd_ver_first = 0; 622 break; 623 case CMD_WRITE_DATA: 624 case CMD_WRITE_CLR_BIT: 625 case CMD_CHECK_BITS_SET: 626 case CMD_CHECK_BITS_CLR: 627 value = get_cfg_value(token, name, lineno); 628 if (set_dcd_param) 629 (*set_dcd_param)(imxhdr, dcd_len, cmd); 630 (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len); 631 if (unlikely(cmd_ver_first != 1)) 632 cmd_ver_first = 0; 633 break; 634 case CMD_CSF: 635 if (imximage_version != 2) { 636 fprintf(stderr, 637 "Error: %s[%d] - CSF only supported for VERSION 2(%s)\n", 638 name, lineno, token); 639 exit(EXIT_FAILURE); 640 } 641 imximage_csf_size = get_cfg_value(token, name, lineno); 642 if (unlikely(cmd_ver_first != 1)) 643 cmd_ver_first = 0; 644 break; 645 case CMD_PLUGIN: 646 plugin_image = 1; 647 copy_plugin_code(imxhdr, token); 648 break; 649 } 650 } 651 652 static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd, 653 char *token, char *name, int lineno, int fld, int *dcd_len) 654 { 655 int value; 656 657 switch (fld) { 658 case CFG_COMMAND: 659 *cmd = get_table_entry_id(imximage_cmds, 660 "imximage commands", token); 661 if (*cmd < 0) { 662 fprintf(stderr, "Error: %s[%d] - Invalid command" 663 "(%s)\n", name, lineno, token); 664 exit(EXIT_FAILURE); 665 } 666 break; 667 case CFG_REG_SIZE: 668 parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len); 669 break; 670 case CFG_REG_ADDRESS: 671 case CFG_REG_VALUE: 672 switch(*cmd) { 673 case CMD_WRITE_DATA: 674 case CMD_WRITE_CLR_BIT: 675 case CMD_CHECK_BITS_SET: 676 case CMD_CHECK_BITS_CLR: 677 678 value = get_cfg_value(token, name, lineno); 679 if (set_dcd_param) 680 (*set_dcd_param)(imxhdr, *dcd_len, *cmd); 681 (*set_dcd_val)(imxhdr, name, lineno, fld, value, 682 *dcd_len); 683 684 if (fld == CFG_REG_VALUE) { 685 (*dcd_len)++; 686 if (*dcd_len > max_dcd_entries) { 687 fprintf(stderr, "Error: %s[%d] -" 688 "DCD table exceeds maximum size(%d)\n", 689 name, lineno, max_dcd_entries); 690 exit(EXIT_FAILURE); 691 } 692 } 693 break; 694 case CMD_PLUGIN: 695 value = get_cfg_value(token, name, lineno); 696 imximage_iram_free_start = value; 697 break; 698 default: 699 break; 700 } 701 break; 702 default: 703 break; 704 } 705 } 706 static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name) 707 { 708 FILE *fd = NULL; 709 char *line = NULL; 710 char *token, *saveptr1, *saveptr2; 711 int lineno = 0; 712 int fld; 713 size_t len; 714 int dcd_len = 0; 715 int32_t cmd; 716 717 fd = fopen(name, "r"); 718 if (fd == 0) { 719 fprintf(stderr, "Error: %s - Can't open DCD file\n", name); 720 exit(EXIT_FAILURE); 721 } 722 723 /* 724 * Very simple parsing, line starting with # are comments 725 * and are dropped 726 */ 727 while ((getline(&line, &len, fd)) > 0) { 728 lineno++; 729 730 token = strtok_r(line, "\r\n", &saveptr1); 731 if (token == NULL) 732 continue; 733 734 /* Check inside the single line */ 735 for (fld = CFG_COMMAND, cmd = CMD_INVALID, 736 line = token; ; line = NULL, fld++) { 737 token = strtok_r(line, " \t", &saveptr2); 738 if (token == NULL) 739 break; 740 741 /* Drop all text starting with '#' as comments */ 742 if (token[0] == '#') 743 break; 744 745 parse_cfg_fld(imxhdr, &cmd, token, name, 746 lineno, fld, &dcd_len); 747 } 748 749 } 750 751 (*set_dcd_rst)(imxhdr, dcd_len, name, lineno); 752 fclose(fd); 753 754 /* Exit if there is no BOOT_FROM field specifying the flash_offset */ 755 if (imximage_ivt_offset == FLASH_OFFSET_UNDEFINED) { 756 fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name); 757 exit(EXIT_FAILURE); 758 } 759 return dcd_len; 760 } 761 762 763 static int imximage_check_image_types(uint8_t type) 764 { 765 if (type == IH_TYPE_IMXIMAGE) 766 return EXIT_SUCCESS; 767 else 768 return EXIT_FAILURE; 769 } 770 771 static int imximage_verify_header(unsigned char *ptr, int image_size, 772 struct image_tool_params *params) 773 { 774 struct imx_header *imx_hdr = (struct imx_header *) ptr; 775 776 if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID) 777 return -FDT_ERR_BADSTRUCTURE; 778 779 return 0; 780 } 781 782 static void imximage_print_header(const void *ptr) 783 { 784 struct imx_header *imx_hdr = (struct imx_header *) ptr; 785 uint32_t version = detect_imximage_version(imx_hdr); 786 787 switch (version) { 788 case IMXIMAGE_V1: 789 print_hdr_v1(imx_hdr); 790 break; 791 case IMXIMAGE_V2: 792 print_hdr_v2(imx_hdr); 793 break; 794 default: 795 err_imximage_version(version); 796 break; 797 } 798 } 799 800 static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd, 801 struct image_tool_params *params) 802 { 803 struct imx_header *imxhdr = (struct imx_header *)ptr; 804 uint32_t dcd_len; 805 uint32_t header_size; 806 807 /* 808 * In order to not change the old imx cfg file 809 * by adding VERSION command into it, here need 810 * set up function ptr group to V1 by default. 811 */ 812 imximage_version = IMXIMAGE_V1; 813 /* Be able to detect if the cfg file has no BOOT_FROM tag */ 814 imximage_ivt_offset = FLASH_OFFSET_UNDEFINED; 815 imximage_csf_size = 0; 816 set_hdr_func(); 817 818 /* Parse dcd configuration file */ 819 dcd_len = parse_cfg_file(imxhdr, params->imagename); 820 821 if (imximage_version == IMXIMAGE_V2) { 822 header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t); 823 if (!plugin_image) 824 header_size += sizeof(dcd_v2_t); 825 else 826 header_size += MAX_PLUGIN_CODE_SIZE; 827 828 if (imximage_init_loadsize < imximage_ivt_offset + header_size) 829 imximage_init_loadsize = imximage_ivt_offset + 830 header_size; 831 } 832 833 /* Set the imx header */ 834 (*set_imx_hdr)(imxhdr, dcd_len, params->ep, imximage_ivt_offset); 835 836 /* 837 * ROM bug alert 838 * 839 * MX53 only loads 512 byte multiples in case of SD boot. 840 * MX53 only loads NAND page multiples in case of NAND boot and 841 * supports up to 4096 byte large pages, thus align to 4096. 842 * 843 * The remaining fraction of a block bytes would not be loaded! 844 */ 845 *header_size_ptr = ROUND((sbuf->st_size + imximage_ivt_offset), 4096); 846 847 if (csf_ptr && imximage_csf_size) { 848 *csf_ptr = params->ep - imximage_init_loadsize + 849 *header_size_ptr; 850 *header_size_ptr += imximage_csf_size; 851 } 852 } 853 854 int imximage_check_params(struct image_tool_params *params) 855 { 856 if (!params) 857 return CFG_INVALID; 858 if (!strlen(params->imagename)) { 859 fprintf(stderr, "Error: %s - Configuration file not specified, " 860 "it is needed for imximage generation\n", 861 params->cmdname); 862 return CFG_INVALID; 863 } 864 /* 865 * Check parameters: 866 * XIP is not allowed and verify that incompatible 867 * parameters are not sent at the same time 868 * For example, if list is required a data image must not be provided 869 */ 870 return (params->dflag && (params->fflag || params->lflag)) || 871 (params->fflag && (params->dflag || params->lflag)) || 872 (params->lflag && (params->dflag || params->fflag)) || 873 (params->xflag) || !(strlen(params->imagename)); 874 } 875 876 static int imximage_generate(struct image_tool_params *params, 877 struct image_type_params *tparams) 878 { 879 struct imx_header *imxhdr; 880 size_t alloc_len; 881 struct stat sbuf; 882 char *datafile = params->datafile; 883 uint32_t pad_len, header_size; 884 885 memset(&imximage_header, 0, sizeof(imximage_header)); 886 887 /* 888 * In order to not change the old imx cfg file 889 * by adding VERSION command into it, here need 890 * set up function ptr group to V1 by default. 891 */ 892 imximage_version = IMXIMAGE_V1; 893 /* Be able to detect if the cfg file has no BOOT_FROM tag */ 894 imximage_ivt_offset = FLASH_OFFSET_UNDEFINED; 895 imximage_csf_size = 0; 896 set_hdr_func(); 897 898 /* Parse dcd configuration file */ 899 parse_cfg_file(&imximage_header, params->imagename); 900 901 /* TODO: check i.MX image V1 handling, for now use 'old' style */ 902 if (imximage_version == IMXIMAGE_V1) { 903 alloc_len = 4096; 904 header_size = 4096; 905 } else { 906 header_size = sizeof(flash_header_v2_t) + sizeof(boot_data_t); 907 if (!plugin_image) 908 header_size += sizeof(dcd_v2_t); 909 else 910 header_size += MAX_PLUGIN_CODE_SIZE; 911 912 if (imximage_init_loadsize < imximage_ivt_offset + header_size) 913 imximage_init_loadsize = imximage_ivt_offset + 914 header_size; 915 alloc_len = imximage_init_loadsize - imximage_ivt_offset; 916 } 917 918 if (alloc_len < header_size) { 919 fprintf(stderr, "%s: header error\n", 920 params->cmdname); 921 exit(EXIT_FAILURE); 922 } 923 924 imxhdr = malloc(alloc_len); 925 926 if (!imxhdr) { 927 fprintf(stderr, "%s: malloc return failure: %s\n", 928 params->cmdname, strerror(errno)); 929 exit(EXIT_FAILURE); 930 } 931 932 memset(imxhdr, 0, alloc_len); 933 934 tparams->header_size = alloc_len; 935 tparams->hdr = imxhdr; 936 937 /* determine data image file length */ 938 939 if (stat(datafile, &sbuf) < 0) { 940 fprintf(stderr, "%s: Can't stat %s: %s\n", 941 params->cmdname, datafile, strerror(errno)); 942 exit(EXIT_FAILURE); 943 } 944 945 pad_len = ROUND(sbuf.st_size, 4096) - sbuf.st_size; 946 947 /* TODO: check i.MX image V1 handling, for now use 'old' style */ 948 if (imximage_version == IMXIMAGE_V1) 949 return 0; 950 else 951 return pad_len; 952 } 953 954 955 /* 956 * imximage parameters 957 */ 958 U_BOOT_IMAGE_TYPE( 959 imximage, 960 "Freescale i.MX Boot Image support", 961 0, 962 NULL, 963 imximage_check_params, 964 imximage_verify_header, 965 imximage_print_header, 966 imximage_set_header, 967 NULL, 968 imximage_check_image_types, 969 NULL, 970 imximage_generate 971 ); 972