Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 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 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 12 | #include "imagetool.h" |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 13 | #include <image.h> |
| 14 | #include "imximage.h" |
| 15 | |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 16 | #define UNDEFINED 0xFFFFFFFF |
| 17 | |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 18 | /* |
| 19 | * Supported commands for configuration file |
| 20 | */ |
| 21 | static table_entry_t imximage_cmds[] = { |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 22 | {CMD_BOOT_FROM, "BOOT_FROM", "boot command", }, |
Marek Vasut | d45fd73 | 2013-04-25 10:16:02 +0000 | [diff] [blame] | 23 | {CMD_BOOT_OFFSET, "BOOT_OFFSET", "Boot offset", }, |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 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", }, |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 28 | {CMD_CSF, "CSF", "Command Sequence File", }, |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 29 | {CMD_IMAGE_VERSION, "IMAGE_VERSION", "image version", }, |
| 30 | {-1, "", "", }, |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | /* |
| 34 | * Supported Boot options for configuration file |
| 35 | * this is needed to set the correct flash offset |
| 36 | */ |
Stefano Babic | dc39a3e | 2013-06-26 23:50:06 +0200 | [diff] [blame] | 37 | static table_entry_t imximage_boot_offset[] = { |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 38 | {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",}, |
Dirk Behme | 40a9c46 | 2012-01-11 23:28:32 +0000 | [diff] [blame] | 39 | {FLASH_OFFSET_NAND, "nand", "NAND Flash", }, |
Dirk Behme | dfbf6ce | 2012-01-11 23:28:31 +0000 | [diff] [blame] | 40 | {FLASH_OFFSET_NOR, "nor", "NOR Flash", }, |
| 41 | {FLASH_OFFSET_SATA, "sata", "SATA Disk", }, |
Dirk Behme | 40a9c46 | 2012-01-11 23:28:32 +0000 | [diff] [blame] | 42 | {FLASH_OFFSET_SD, "sd", "SD Card", }, |
| 43 | {FLASH_OFFSET_SPI, "spi", "SPI Flash", }, |
Ye.Li | f16cde0 | 2015-01-13 15:53:06 +0800 | [diff] [blame] | 44 | {FLASH_OFFSET_QSPI, "qspi", "QSPI NOR Flash",}, |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 45 | {-1, "", "Invalid", }, |
| 46 | }; |
| 47 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 48 | /* |
Stefano Babic | dc39a3e | 2013-06-26 23:50:06 +0200 | [diff] [blame] | 49 | * Supported Boot options for configuration file |
| 50 | * this is needed to determine the initial load size |
| 51 | */ |
| 52 | static table_entry_t imximage_boot_loadsize[] = { |
| 53 | {FLASH_LOADSIZE_ONENAND, "onenand", "OneNAND Flash",}, |
| 54 | {FLASH_LOADSIZE_NAND, "nand", "NAND Flash", }, |
| 55 | {FLASH_LOADSIZE_NOR, "nor", "NOR Flash", }, |
| 56 | {FLASH_LOADSIZE_SATA, "sata", "SATA Disk", }, |
| 57 | {FLASH_LOADSIZE_SD, "sd", "SD Card", }, |
| 58 | {FLASH_LOADSIZE_SPI, "spi", "SPI Flash", }, |
Ye.Li | f16cde0 | 2015-01-13 15:53:06 +0800 | [diff] [blame] | 59 | {FLASH_LOADSIZE_QSPI, "qspi", "QSPI NOR Flash",}, |
Stefano Babic | dc39a3e | 2013-06-26 23:50:06 +0200 | [diff] [blame] | 60 | {-1, "", "Invalid", }, |
| 61 | }; |
| 62 | |
| 63 | /* |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 64 | * IMXIMAGE version definition for i.MX chips |
| 65 | */ |
| 66 | static table_entry_t imximage_versions[] = { |
| 67 | {IMXIMAGE_V1, "", " (i.MX25/35/51 compatible)", }, |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 68 | {IMXIMAGE_V2, "", " (i.MX53/6/7 compatible)", }, |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 69 | {-1, "", " (Invalid)", }, |
| 70 | }; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 71 | |
| 72 | static struct imx_header imximage_header; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 73 | static uint32_t imximage_version; |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 74 | /* |
| 75 | * Image Vector Table Offset |
| 76 | * Initialized to a wrong not 4-bytes aligned address to |
| 77 | * check if it is was set by the cfg file. |
| 78 | */ |
| 79 | static uint32_t imximage_ivt_offset = UNDEFINED; |
| 80 | static uint32_t imximage_csf_size = UNDEFINED; |
Stefano Babic | dc39a3e | 2013-06-26 23:50:06 +0200 | [diff] [blame] | 81 | /* Initial Load Region Size */ |
| 82 | static uint32_t imximage_init_loadsize; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 83 | |
| 84 | static set_dcd_val_t set_dcd_val; |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 85 | static set_dcd_param_t set_dcd_param; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 86 | static set_dcd_rst_t set_dcd_rst; |
| 87 | static set_imx_hdr_t set_imx_hdr; |
Troy Kisky | 9bd5b85 | 2012-10-03 15:47:03 +0000 | [diff] [blame] | 88 | static uint32_t max_dcd_entries; |
Troy Kisky | 7b922cb | 2012-10-03 15:47:07 +0000 | [diff] [blame] | 89 | static uint32_t *header_size_ptr; |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 90 | static uint32_t *csf_ptr; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 91 | |
| 92 | static uint32_t get_cfg_value(char *token, char *name, int linenr) |
| 93 | { |
| 94 | char *endptr; |
| 95 | uint32_t value; |
| 96 | |
| 97 | errno = 0; |
| 98 | value = strtoul(token, &endptr, 16); |
| 99 | if (errno || (token == endptr)) { |
| 100 | fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n", |
| 101 | name, linenr, token); |
| 102 | exit(EXIT_FAILURE); |
| 103 | } |
| 104 | return value; |
| 105 | } |
| 106 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 107 | static uint32_t detect_imximage_version(struct imx_header *imx_hdr) |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 108 | { |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 109 | imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1; |
| 110 | imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2; |
| 111 | flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr; |
| 112 | flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr; |
| 113 | |
| 114 | /* Try to detect V1 */ |
| 115 | if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) && |
| 116 | (hdr_v1->dcd_table.preamble.barker == DCD_BARKER)) |
| 117 | return IMXIMAGE_V1; |
| 118 | |
| 119 | /* Try to detect V2 */ |
| 120 | if ((fhdr_v2->header.tag == IVT_HEADER_TAG) && |
| 121 | (hdr_v2->dcd_table.header.tag == DCD_HEADER_TAG)) |
| 122 | return IMXIMAGE_V2; |
| 123 | |
| 124 | return IMXIMAGE_VER_INVALID; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 127 | static void err_imximage_version(int version) |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 128 | { |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 129 | fprintf(stderr, |
| 130 | "Error: Unsupported imximage version:%d\n", version); |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 131 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 132 | exit(EXIT_FAILURE); |
| 133 | } |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 134 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 135 | static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno, |
| 136 | int fld, uint32_t value, uint32_t off) |
| 137 | { |
| 138 | dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 139 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 140 | switch (fld) { |
| 141 | case CFG_REG_SIZE: |
| 142 | /* Byte, halfword, word */ |
| 143 | if ((value != 1) && (value != 2) && (value != 4)) { |
| 144 | fprintf(stderr, "Error: %s[%d] - " |
| 145 | "Invalid register size " "(%d)\n", |
| 146 | name, lineno, value); |
| 147 | exit(EXIT_FAILURE); |
| 148 | } |
| 149 | dcd_v1->addr_data[off].type = value; |
| 150 | break; |
| 151 | case CFG_REG_ADDRESS: |
| 152 | dcd_v1->addr_data[off].addr = value; |
| 153 | break; |
| 154 | case CFG_REG_VALUE: |
| 155 | dcd_v1->addr_data[off].value = value; |
| 156 | break; |
| 157 | default: |
| 158 | break; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 159 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 160 | } |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 163 | static void set_dcd_param_v2(struct imx_header *imxhdr, uint32_t dcd_len, |
| 164 | int32_t cmd) |
| 165 | { |
| 166 | dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table; |
| 167 | |
| 168 | switch (cmd) { |
| 169 | case CMD_WRITE_DATA: |
| 170 | dcd_v2->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG; |
| 171 | dcd_v2->write_dcd_command.length = cpu_to_be16( |
| 172 | dcd_len * sizeof(dcd_addr_data_t) + 4); |
| 173 | dcd_v2->write_dcd_command.param = DCD_WRITE_DATA_PARAM; |
| 174 | break; |
| 175 | case CMD_WRITE_CLR_BIT: |
| 176 | dcd_v2->write_dcd_command.tag = DCD_WRITE_DATA_COMMAND_TAG; |
| 177 | dcd_v2->write_dcd_command.length = cpu_to_be16( |
| 178 | dcd_len * sizeof(dcd_addr_data_t) + 4); |
| 179 | dcd_v2->write_dcd_command.param = DCD_WRITE_CLR_BIT_PARAM; |
| 180 | break; |
| 181 | /* |
| 182 | * Check data command only supports one entry, |
| 183 | * so use 0xC = size(address + value + command). |
| 184 | */ |
| 185 | case CMD_CHECK_BITS_SET: |
| 186 | dcd_v2->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG; |
| 187 | dcd_v2->write_dcd_command.length = cpu_to_be16(0xC); |
| 188 | dcd_v2->write_dcd_command.param = DCD_CHECK_BITS_SET_PARAM; |
| 189 | break; |
| 190 | case CMD_CHECK_BITS_CLR: |
| 191 | dcd_v2->write_dcd_command.tag = DCD_CHECK_DATA_COMMAND_TAG; |
| 192 | dcd_v2->write_dcd_command.length = cpu_to_be16(0xC); |
| 193 | dcd_v2->write_dcd_command.param = DCD_CHECK_BITS_SET_PARAM; |
| 194 | break; |
| 195 | default: |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 200 | static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno, |
| 201 | int fld, uint32_t value, uint32_t off) |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 202 | { |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 203 | dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table; |
| 204 | |
| 205 | switch (fld) { |
| 206 | case CFG_REG_ADDRESS: |
| 207 | dcd_v2->addr_data[off].addr = cpu_to_be32(value); |
| 208 | break; |
| 209 | case CFG_REG_VALUE: |
| 210 | dcd_v2->addr_data[off].value = cpu_to_be32(value); |
| 211 | break; |
| 212 | default: |
| 213 | break; |
| 214 | |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Complete setting up the rest field of DCD of V1 |
| 220 | * such as barker code and DCD data length. |
| 221 | */ |
| 222 | static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len, |
| 223 | char *name, int lineno) |
| 224 | { |
| 225 | dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table; |
| 226 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 227 | dcd_v1->preamble.barker = DCD_BARKER; |
| 228 | dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t); |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Complete setting up the reset field of DCD of V2 |
| 233 | * such as DCD tag, version, length, etc. |
| 234 | */ |
| 235 | static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len, |
| 236 | char *name, int lineno) |
| 237 | { |
| 238 | dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table; |
| 239 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 240 | dcd_v2->header.tag = DCD_HEADER_TAG; |
| 241 | dcd_v2->header.length = cpu_to_be16( |
| 242 | dcd_len * sizeof(dcd_addr_data_t) + 8); |
| 243 | dcd_v2->header.version = DCD_VERSION; |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 244 | set_dcd_param_v2(imxhdr, dcd_len, CMD_WRITE_DATA); |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len, |
Troy Kisky | 7bb9220 | 2012-10-03 15:47:08 +0000 | [diff] [blame] | 248 | uint32_t entry_point, uint32_t flash_offset) |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 249 | { |
| 250 | imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1; |
| 251 | flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr; |
| 252 | dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table; |
Troy Kisky | 2d7c29f | 2012-10-03 15:47:09 +0000 | [diff] [blame] | 253 | uint32_t hdr_base; |
Troy Kisky | 7b922cb | 2012-10-03 15:47:07 +0000 | [diff] [blame] | 254 | uint32_t header_length = (((char *)&dcd_v1->addr_data[dcd_len].addr) |
| 255 | - ((char *)imxhdr)); |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 256 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 257 | /* Set magic number */ |
| 258 | fhdr_v1->app_code_barker = APP_CODE_BARKER; |
| 259 | |
Stefano Babic | dc39a3e | 2013-06-26 23:50:06 +0200 | [diff] [blame] | 260 | /* TODO: check i.MX image V1 handling, for now use 'old' style */ |
| 261 | hdr_base = entry_point - 4096; |
Troy Kisky | 2d7c29f | 2012-10-03 15:47:09 +0000 | [diff] [blame] | 262 | fhdr_v1->app_dest_ptr = hdr_base - flash_offset; |
Troy Kisky | 7bb9220 | 2012-10-03 15:47:08 +0000 | [diff] [blame] | 263 | fhdr_v1->app_code_jump_vector = entry_point; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 264 | |
Troy Kisky | 2d7c29f | 2012-10-03 15:47:09 +0000 | [diff] [blame] | 265 | fhdr_v1->dcd_ptr_ptr = hdr_base + offsetof(flash_header_v1_t, dcd_ptr); |
| 266 | fhdr_v1->dcd_ptr = hdr_base + offsetof(imx_header_v1_t, dcd_table); |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 267 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 268 | /* Security feature are not supported */ |
| 269 | fhdr_v1->app_code_csf = 0; |
| 270 | fhdr_v1->super_root_key = 0; |
Troy Kisky | 7b922cb | 2012-10-03 15:47:07 +0000 | [diff] [blame] | 271 | header_size_ptr = (uint32_t *)(((char *)imxhdr) + header_length - 4); |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len, |
Troy Kisky | 7bb9220 | 2012-10-03 15:47:08 +0000 | [diff] [blame] | 275 | uint32_t entry_point, uint32_t flash_offset) |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 276 | { |
| 277 | imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2; |
| 278 | flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr; |
Troy Kisky | 2d7c29f | 2012-10-03 15:47:09 +0000 | [diff] [blame] | 279 | uint32_t hdr_base; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 280 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 281 | /* Set magic number */ |
| 282 | fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */ |
| 283 | fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t)); |
| 284 | fhdr_v2->header.version = IVT_VERSION; /* 0x40 */ |
| 285 | |
Troy Kisky | 7bb9220 | 2012-10-03 15:47:08 +0000 | [diff] [blame] | 286 | fhdr_v2->entry = entry_point; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 287 | fhdr_v2->reserved1 = fhdr_v2->reserved2 = 0; |
Stefano Babic | dc39a3e | 2013-06-26 23:50:06 +0200 | [diff] [blame] | 288 | hdr_base = entry_point - imximage_init_loadsize + |
| 289 | flash_offset; |
| 290 | fhdr_v2->self = hdr_base; |
Troy Kisky | 2d7c29f | 2012-10-03 15:47:09 +0000 | [diff] [blame] | 291 | fhdr_v2->dcd_ptr = hdr_base + offsetof(imx_header_v2_t, dcd_table); |
| 292 | fhdr_v2->boot_data_ptr = hdr_base |
| 293 | + offsetof(imx_header_v2_t, boot_data); |
Stefano Babic | dc39a3e | 2013-06-26 23:50:06 +0200 | [diff] [blame] | 294 | hdr_v2->boot_data.start = entry_point - imximage_init_loadsize; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 295 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 296 | fhdr_v2->csf = 0; |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 297 | |
Troy Kisky | 7b922cb | 2012-10-03 15:47:07 +0000 | [diff] [blame] | 298 | header_size_ptr = &hdr_v2->boot_data.size; |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 299 | csf_ptr = &fhdr_v2->csf; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 300 | } |
| 301 | |
York Sun | d061d95 | 2013-09-20 12:24:44 -0700 | [diff] [blame] | 302 | static void set_hdr_func(void) |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 303 | { |
| 304 | switch (imximage_version) { |
| 305 | case IMXIMAGE_V1: |
| 306 | set_dcd_val = set_dcd_val_v1; |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 307 | set_dcd_param = NULL; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 308 | set_dcd_rst = set_dcd_rst_v1; |
| 309 | set_imx_hdr = set_imx_hdr_v1; |
Troy Kisky | 9bd5b85 | 2012-10-03 15:47:03 +0000 | [diff] [blame] | 310 | max_dcd_entries = MAX_HW_CFG_SIZE_V1; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 311 | break; |
| 312 | case IMXIMAGE_V2: |
| 313 | set_dcd_val = set_dcd_val_v2; |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 314 | set_dcd_param = set_dcd_param_v2; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 315 | set_dcd_rst = set_dcd_rst_v2; |
| 316 | set_imx_hdr = set_imx_hdr_v2; |
Troy Kisky | 9bd5b85 | 2012-10-03 15:47:03 +0000 | [diff] [blame] | 317 | max_dcd_entries = MAX_HW_CFG_SIZE_V2; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 318 | break; |
| 319 | default: |
| 320 | err_imximage_version(imximage_version); |
| 321 | break; |
| 322 | } |
| 323 | } |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 324 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 325 | static void print_hdr_v1(struct imx_header *imx_hdr) |
| 326 | { |
| 327 | imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1; |
| 328 | flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr; |
| 329 | dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table; |
| 330 | uint32_t size, length, ver; |
| 331 | |
| 332 | size = dcd_v1->preamble.length; |
| 333 | if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) { |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 334 | fprintf(stderr, |
| 335 | "Error: Image corrupt DCD size %d exceed maximum %d\n", |
Stefano Babic | 5cdde80 | 2010-02-05 15:16:02 +0100 | [diff] [blame] | 336 | (uint32_t)(size / sizeof(dcd_type_addr_data_t)), |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 337 | MAX_HW_CFG_SIZE_V1); |
| 338 | exit(EXIT_FAILURE); |
| 339 | } |
| 340 | |
| 341 | length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t); |
| 342 | ver = detect_imximage_version(imx_hdr); |
| 343 | |
| 344 | printf("Image Type: Freescale IMX Boot Image\n"); |
| 345 | printf("Image Ver: %x", ver); |
| 346 | printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver)); |
| 347 | printf("Data Size: "); |
| 348 | genimg_print_size(dcd_v1->addr_data[length].type); |
| 349 | printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr); |
| 350 | printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector); |
| 351 | } |
| 352 | |
| 353 | static void print_hdr_v2(struct imx_header *imx_hdr) |
| 354 | { |
| 355 | imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2; |
| 356 | flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr; |
| 357 | dcd_v2_t *dcd_v2 = &hdr_v2->dcd_table; |
| 358 | uint32_t size, version; |
| 359 | |
| 360 | size = be16_to_cpu(dcd_v2->header.length) - 8; |
| 361 | if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) { |
| 362 | fprintf(stderr, |
| 363 | "Error: Image corrupt DCD size %d exceed maximum %d\n", |
| 364 | (uint32_t)(size / sizeof(dcd_addr_data_t)), |
| 365 | MAX_HW_CFG_SIZE_V2); |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 366 | exit(EXIT_FAILURE); |
| 367 | } |
| 368 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 369 | version = detect_imximage_version(imx_hdr); |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 370 | |
| 371 | printf("Image Type: Freescale IMX Boot Image\n"); |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 372 | printf("Image Ver: %x", version); |
| 373 | printf("%s\n", get_table_entry_name(imximage_versions, NULL, version)); |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 374 | printf("Data Size: "); |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 375 | genimg_print_size(hdr_v2->boot_data.size); |
| 376 | printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr); |
| 377 | printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry); |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 378 | if (fhdr_v2->csf && (imximage_ivt_offset != UNDEFINED) && |
| 379 | (imximage_csf_size != UNDEFINED)) { |
| 380 | printf("HAB Blocks: %08x %08x %08x\n", |
| 381 | (uint32_t)fhdr_v2->self, 0, |
| 382 | hdr_v2->boot_data.size - imximage_ivt_offset - |
| 383 | imximage_csf_size); |
| 384 | } |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 387 | static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token, |
| 388 | char *name, int lineno, int fld, int dcd_len) |
| 389 | { |
| 390 | int value; |
| 391 | static int cmd_ver_first = ~0; |
| 392 | |
| 393 | switch (cmd) { |
| 394 | case CMD_IMAGE_VERSION: |
| 395 | imximage_version = get_cfg_value(token, name, lineno); |
| 396 | if (cmd_ver_first == 0) { |
| 397 | fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION " |
| 398 | "command need be the first before other " |
| 399 | "valid command in the file\n", name, lineno); |
| 400 | exit(EXIT_FAILURE); |
| 401 | } |
| 402 | cmd_ver_first = 1; |
York Sun | d061d95 | 2013-09-20 12:24:44 -0700 | [diff] [blame] | 403 | set_hdr_func(); |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 404 | break; |
| 405 | case CMD_BOOT_FROM: |
Stefano Babic | dc39a3e | 2013-06-26 23:50:06 +0200 | [diff] [blame] | 406 | imximage_ivt_offset = get_table_entry_id(imximage_boot_offset, |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 407 | "imximage boot option", token); |
Stefano Babic | e74e779 | 2013-06-26 18:08:37 +0200 | [diff] [blame] | 408 | if (imximage_ivt_offset == -1) { |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 409 | fprintf(stderr, "Error: %s[%d] -Invalid boot device" |
| 410 | "(%s)\n", name, lineno, token); |
| 411 | exit(EXIT_FAILURE); |
| 412 | } |
Stefano Babic | dc39a3e | 2013-06-26 23:50:06 +0200 | [diff] [blame] | 413 | |
| 414 | imximage_init_loadsize = |
| 415 | get_table_entry_id(imximage_boot_loadsize, |
| 416 | "imximage boot option", token); |
| 417 | |
| 418 | if (imximage_init_loadsize == -1) { |
| 419 | fprintf(stderr, |
| 420 | "Error: %s[%d] -Invalid boot device(%s)\n", |
| 421 | name, lineno, token); |
| 422 | exit(EXIT_FAILURE); |
| 423 | } |
Stefano Babic | 832eda8 | 2013-08-19 19:03:20 +0200 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * The SOC loads from the storage starting at address 0 |
| 427 | * then ensures that the load size contains the offset |
| 428 | */ |
| 429 | if (imximage_init_loadsize < imximage_ivt_offset) |
| 430 | imximage_init_loadsize = imximage_ivt_offset; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 431 | if (unlikely(cmd_ver_first != 1)) |
| 432 | cmd_ver_first = 0; |
| 433 | break; |
Marek Vasut | d45fd73 | 2013-04-25 10:16:02 +0000 | [diff] [blame] | 434 | case CMD_BOOT_OFFSET: |
Stefano Babic | e74e779 | 2013-06-26 18:08:37 +0200 | [diff] [blame] | 435 | imximage_ivt_offset = get_cfg_value(token, name, lineno); |
Marek Vasut | d45fd73 | 2013-04-25 10:16:02 +0000 | [diff] [blame] | 436 | if (unlikely(cmd_ver_first != 1)) |
| 437 | cmd_ver_first = 0; |
| 438 | break; |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 439 | case CMD_WRITE_DATA: |
| 440 | case CMD_WRITE_CLR_BIT: |
| 441 | case CMD_CHECK_BITS_SET: |
| 442 | case CMD_CHECK_BITS_CLR: |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 443 | value = get_cfg_value(token, name, lineno); |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 444 | if (set_dcd_param) |
| 445 | (*set_dcd_param)(imxhdr, dcd_len, cmd); |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 446 | (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len); |
| 447 | if (unlikely(cmd_ver_first != 1)) |
| 448 | cmd_ver_first = 0; |
| 449 | break; |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 450 | case CMD_CSF: |
| 451 | if (imximage_version != 2) { |
| 452 | fprintf(stderr, |
| 453 | "Error: %s[%d] - CSF only supported for VERSION 2(%s)\n", |
| 454 | name, lineno, token); |
| 455 | exit(EXIT_FAILURE); |
| 456 | } |
| 457 | imximage_csf_size = get_cfg_value(token, name, lineno); |
| 458 | if (unlikely(cmd_ver_first != 1)) |
| 459 | cmd_ver_first = 0; |
| 460 | break; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd, |
| 465 | char *token, char *name, int lineno, int fld, int *dcd_len) |
| 466 | { |
| 467 | int value; |
| 468 | |
| 469 | switch (fld) { |
| 470 | case CFG_COMMAND: |
| 471 | *cmd = get_table_entry_id(imximage_cmds, |
| 472 | "imximage commands", token); |
| 473 | if (*cmd < 0) { |
| 474 | fprintf(stderr, "Error: %s[%d] - Invalid command" |
| 475 | "(%s)\n", name, lineno, token); |
| 476 | exit(EXIT_FAILURE); |
| 477 | } |
| 478 | break; |
| 479 | case CFG_REG_SIZE: |
| 480 | parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len); |
| 481 | break; |
| 482 | case CFG_REG_ADDRESS: |
| 483 | case CFG_REG_VALUE: |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 484 | switch(*cmd) { |
| 485 | case CMD_WRITE_DATA: |
| 486 | case CMD_WRITE_CLR_BIT: |
| 487 | case CMD_CHECK_BITS_SET: |
| 488 | case CMD_CHECK_BITS_CLR: |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 489 | |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 490 | value = get_cfg_value(token, name, lineno); |
| 491 | if (set_dcd_param) |
| 492 | (*set_dcd_param)(imxhdr, *dcd_len, *cmd); |
| 493 | (*set_dcd_val)(imxhdr, name, lineno, fld, value, |
| 494 | *dcd_len); |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 495 | |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 496 | if (fld == CFG_REG_VALUE) { |
| 497 | (*dcd_len)++; |
| 498 | if (*dcd_len > max_dcd_entries) { |
| 499 | fprintf(stderr, "Error: %s[%d] -" |
| 500 | "DCD table exceeds maximum size(%d)\n", |
| 501 | name, lineno, max_dcd_entries); |
| 502 | exit(EXIT_FAILURE); |
| 503 | } |
Troy Kisky | 9bd5b85 | 2012-10-03 15:47:03 +0000 | [diff] [blame] | 504 | } |
Adrian Alonso | 73e5732 | 2015-07-20 19:04:55 -0500 | [diff] [blame] | 505 | break; |
| 506 | default: |
| 507 | break; |
Troy Kisky | 9bd5b85 | 2012-10-03 15:47:03 +0000 | [diff] [blame] | 508 | } |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 509 | break; |
| 510 | default: |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name) |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 515 | { |
| 516 | FILE *fd = NULL; |
| 517 | char *line = NULL; |
| 518 | char *token, *saveptr1, *saveptr2; |
| 519 | int lineno = 0; |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 520 | int fld; |
Kim Phillips | 1c0c250 | 2010-02-22 19:37:56 -0600 | [diff] [blame] | 521 | size_t len; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 522 | int dcd_len = 0; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 523 | int32_t cmd; |
| 524 | |
| 525 | fd = fopen(name, "r"); |
| 526 | if (fd == 0) { |
| 527 | fprintf(stderr, "Error: %s - Can't open DCD file\n", name); |
| 528 | exit(EXIT_FAILURE); |
| 529 | } |
| 530 | |
Stefano Babic | 832eda8 | 2013-08-19 19:03:20 +0200 | [diff] [blame] | 531 | /* |
| 532 | * Very simple parsing, line starting with # are comments |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 533 | * and are dropped |
| 534 | */ |
| 535 | while ((getline(&line, &len, fd)) > 0) { |
| 536 | lineno++; |
| 537 | |
| 538 | token = strtok_r(line, "\r\n", &saveptr1); |
| 539 | if (token == NULL) |
| 540 | continue; |
| 541 | |
| 542 | /* Check inside the single line */ |
| 543 | for (fld = CFG_COMMAND, cmd = CMD_INVALID, |
| 544 | line = token; ; line = NULL, fld++) { |
| 545 | token = strtok_r(line, " \t", &saveptr2); |
| 546 | if (token == NULL) |
| 547 | break; |
| 548 | |
| 549 | /* Drop all text starting with '#' as comments */ |
| 550 | if (token[0] == '#') |
| 551 | break; |
| 552 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 553 | parse_cfg_fld(imxhdr, &cmd, token, name, |
| 554 | lineno, fld, &dcd_len); |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 557 | } |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 558 | |
| 559 | (*set_dcd_rst)(imxhdr, dcd_len, name, lineno); |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 560 | fclose(fd); |
| 561 | |
Troy Kisky | e55eaf9 | 2012-10-03 15:47:05 +0000 | [diff] [blame] | 562 | /* Exit if there is no BOOT_FROM field specifying the flash_offset */ |
Stefano Babic | e74e779 | 2013-06-26 18:08:37 +0200 | [diff] [blame] | 563 | if (imximage_ivt_offset == FLASH_OFFSET_UNDEFINED) { |
Troy Kisky | e55eaf9 | 2012-10-03 15:47:05 +0000 | [diff] [blame] | 564 | fprintf(stderr, "Error: No BOOT_FROM tag in %s\n", name); |
| 565 | exit(EXIT_FAILURE); |
| 566 | } |
Stefano Babic | 5cdde80 | 2010-02-05 15:16:02 +0100 | [diff] [blame] | 567 | return dcd_len; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 568 | } |
| 569 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 570 | |
| 571 | static int imximage_check_image_types(uint8_t type) |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 572 | { |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 573 | if (type == IH_TYPE_IMXIMAGE) |
| 574 | return EXIT_SUCCESS; |
| 575 | else |
| 576 | return EXIT_FAILURE; |
| 577 | } |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 578 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 579 | static int imximage_verify_header(unsigned char *ptr, int image_size, |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 580 | struct image_tool_params *params) |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 581 | { |
| 582 | struct imx_header *imx_hdr = (struct imx_header *) ptr; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 583 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 584 | if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID) |
| 585 | return -FDT_ERR_BADSTRUCTURE; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 586 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 587 | return 0; |
| 588 | } |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 589 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 590 | static void imximage_print_header(const void *ptr) |
| 591 | { |
| 592 | struct imx_header *imx_hdr = (struct imx_header *) ptr; |
| 593 | uint32_t version = detect_imximage_version(imx_hdr); |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 594 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 595 | switch (version) { |
| 596 | case IMXIMAGE_V1: |
| 597 | print_hdr_v1(imx_hdr); |
| 598 | break; |
| 599 | case IMXIMAGE_V2: |
| 600 | print_hdr_v2(imx_hdr); |
| 601 | break; |
| 602 | default: |
| 603 | err_imximage_version(version); |
| 604 | break; |
| 605 | } |
| 606 | } |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 607 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 608 | static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd, |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 609 | struct image_tool_params *params) |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 610 | { |
| 611 | struct imx_header *imxhdr = (struct imx_header *)ptr; |
| 612 | uint32_t dcd_len; |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 613 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 614 | /* |
| 615 | * In order to not change the old imx cfg file |
| 616 | * by adding VERSION command into it, here need |
| 617 | * set up function ptr group to V1 by default. |
| 618 | */ |
| 619 | imximage_version = IMXIMAGE_V1; |
Dirk Behme | 14a98cd | 2012-02-22 22:50:19 +0000 | [diff] [blame] | 620 | /* Be able to detect if the cfg file has no BOOT_FROM tag */ |
Stefano Babic | e74e779 | 2013-06-26 18:08:37 +0200 | [diff] [blame] | 621 | imximage_ivt_offset = FLASH_OFFSET_UNDEFINED; |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 622 | imximage_csf_size = 0; |
York Sun | d061d95 | 2013-09-20 12:24:44 -0700 | [diff] [blame] | 623 | set_hdr_func(); |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 624 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 625 | /* Parse dcd configuration file */ |
| 626 | dcd_len = parse_cfg_file(imxhdr, params->imagename); |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 627 | |
Ye.Li | f406351 | 2014-08-20 16:55:32 +0800 | [diff] [blame] | 628 | if (imximage_version == IMXIMAGE_V2) { |
| 629 | if (imximage_init_loadsize < imximage_ivt_offset + |
| 630 | sizeof(imx_header_v2_t)) |
| 631 | imximage_init_loadsize = imximage_ivt_offset + |
| 632 | sizeof(imx_header_v2_t); |
| 633 | } |
| 634 | |
Liu Hui-R64343 | 4aa360a | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 635 | /* Set the imx header */ |
Stefano Babic | e74e779 | 2013-06-26 18:08:37 +0200 | [diff] [blame] | 636 | (*set_imx_hdr)(imxhdr, dcd_len, params->ep, imximage_ivt_offset); |
Fabio Estevam | 5fd7c5d | 2013-01-03 08:24:33 +0000 | [diff] [blame] | 637 | |
| 638 | /* |
| 639 | * ROM bug alert |
Marek Vasut | 3d1acc6 | 2013-04-21 05:52:22 +0000 | [diff] [blame] | 640 | * |
| 641 | * MX53 only loads 512 byte multiples in case of SD boot. |
| 642 | * MX53 only loads NAND page multiples in case of NAND boot and |
| 643 | * supports up to 4096 byte large pages, thus align to 4096. |
| 644 | * |
| 645 | * The remaining fraction of a block bytes would not be loaded! |
Fabio Estevam | 5fd7c5d | 2013-01-03 08:24:33 +0000 | [diff] [blame] | 646 | */ |
Ye.Li | 49e036f | 2014-10-30 17:54:08 +0800 | [diff] [blame] | 647 | *header_size_ptr = ROUND((sbuf->st_size + imximage_ivt_offset), 4096); |
Stefano Babic | 4aa9749 | 2013-06-27 11:42:38 +0200 | [diff] [blame] | 648 | |
| 649 | if (csf_ptr && imximage_csf_size) { |
| 650 | *csf_ptr = params->ep - imximage_init_loadsize + |
| 651 | *header_size_ptr; |
| 652 | *header_size_ptr += imximage_csf_size; |
| 653 | } |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 654 | } |
| 655 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 656 | int imximage_check_params(struct image_tool_params *params) |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 657 | { |
| 658 | if (!params) |
| 659 | return CFG_INVALID; |
| 660 | if (!strlen(params->imagename)) { |
| 661 | fprintf(stderr, "Error: %s - Configuration file not specified, " |
| 662 | "it is needed for imximage generation\n", |
| 663 | params->cmdname); |
| 664 | return CFG_INVALID; |
| 665 | } |
| 666 | /* |
| 667 | * Check parameters: |
| 668 | * XIP is not allowed and verify that incompatible |
| 669 | * parameters are not sent at the same time |
| 670 | * For example, if list is required a data image must not be provided |
| 671 | */ |
| 672 | return (params->dflag && (params->fflag || params->lflag)) || |
| 673 | (params->fflag && (params->dflag || params->lflag)) || |
| 674 | (params->lflag && (params->dflag || params->fflag)) || |
| 675 | (params->xflag) || !(strlen(params->imagename)); |
| 676 | } |
| 677 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 678 | static int imximage_generate(struct image_tool_params *params, |
Stefano Babic | 832eda8 | 2013-08-19 19:03:20 +0200 | [diff] [blame] | 679 | struct image_type_params *tparams) |
| 680 | { |
| 681 | struct imx_header *imxhdr; |
| 682 | size_t alloc_len; |
| 683 | struct stat sbuf; |
| 684 | char *datafile = params->datafile; |
| 685 | uint32_t pad_len; |
| 686 | |
| 687 | memset(&imximage_header, 0, sizeof(imximage_header)); |
| 688 | |
| 689 | /* |
| 690 | * In order to not change the old imx cfg file |
| 691 | * by adding VERSION command into it, here need |
| 692 | * set up function ptr group to V1 by default. |
| 693 | */ |
| 694 | imximage_version = IMXIMAGE_V1; |
| 695 | /* Be able to detect if the cfg file has no BOOT_FROM tag */ |
| 696 | imximage_ivt_offset = FLASH_OFFSET_UNDEFINED; |
| 697 | imximage_csf_size = 0; |
York Sun | d061d95 | 2013-09-20 12:24:44 -0700 | [diff] [blame] | 698 | set_hdr_func(); |
Stefano Babic | 832eda8 | 2013-08-19 19:03:20 +0200 | [diff] [blame] | 699 | |
| 700 | /* Parse dcd configuration file */ |
| 701 | parse_cfg_file(&imximage_header, params->imagename); |
| 702 | |
| 703 | /* TODO: check i.MX image V1 handling, for now use 'old' style */ |
| 704 | if (imximage_version == IMXIMAGE_V1) { |
| 705 | alloc_len = 4096; |
| 706 | } else { |
| 707 | if (imximage_init_loadsize < imximage_ivt_offset + |
| 708 | sizeof(imx_header_v2_t)) |
| 709 | imximage_init_loadsize = imximage_ivt_offset + |
| 710 | sizeof(imx_header_v2_t); |
| 711 | alloc_len = imximage_init_loadsize - imximage_ivt_offset; |
| 712 | } |
| 713 | |
| 714 | if (alloc_len < sizeof(struct imx_header)) { |
| 715 | fprintf(stderr, "%s: header error\n", |
| 716 | params->cmdname); |
| 717 | exit(EXIT_FAILURE); |
| 718 | } |
| 719 | |
| 720 | imxhdr = malloc(alloc_len); |
| 721 | |
| 722 | if (!imxhdr) { |
| 723 | fprintf(stderr, "%s: malloc return failure: %s\n", |
| 724 | params->cmdname, strerror(errno)); |
| 725 | exit(EXIT_FAILURE); |
| 726 | } |
| 727 | |
| 728 | memset(imxhdr, 0, alloc_len); |
| 729 | |
| 730 | tparams->header_size = alloc_len; |
| 731 | tparams->hdr = imxhdr; |
| 732 | |
| 733 | /* determine data image file length */ |
| 734 | |
| 735 | if (stat(datafile, &sbuf) < 0) { |
| 736 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 737 | params->cmdname, datafile, strerror(errno)); |
| 738 | exit(EXIT_FAILURE); |
| 739 | } |
| 740 | |
| 741 | pad_len = ROUND(sbuf.st_size, 4096) - sbuf.st_size; |
| 742 | |
| 743 | /* TODO: check i.MX image V1 handling, for now use 'old' style */ |
| 744 | if (imximage_version == IMXIMAGE_V1) |
| 745 | return 0; |
| 746 | else |
| 747 | return pad_len; |
| 748 | } |
| 749 | |
| 750 | |
Stefano Babic | 7b07f09 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 751 | /* |
| 752 | * imximage parameters |
| 753 | */ |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 754 | U_BOOT_IMAGE_TYPE( |
| 755 | imximage, |
| 756 | "Freescale i.MX Boot Image support", |
| 757 | 0, |
| 758 | NULL, |
| 759 | imximage_check_params, |
| 760 | imximage_verify_header, |
| 761 | imximage_print_header, |
| 762 | imximage_set_header, |
| 763 | NULL, |
| 764 | imximage_check_image_types, |
| 765 | NULL, |
| 766 | imximage_generate |
| 767 | ); |