Stefano Babic | 8edcde5 | 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 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
Kumar Gala | 249d4de | 2010-01-27 10:16:56 -0600 | [diff] [blame] | 28 | /* Required to obtain the getline prototype from stdio.h */ |
| 29 | #define _GNU_SOURCE |
| 30 | |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 31 | #include "mkimage.h" |
| 32 | #include <image.h> |
| 33 | #include "imximage.h" |
| 34 | |
| 35 | /* |
| 36 | * Supported commands for configuration file |
| 37 | */ |
| 38 | static table_entry_t imximage_cmds[] = { |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 39 | {CMD_BOOT_FROM, "BOOT_FROM", "boot command", }, |
| 40 | {CMD_DATA, "DATA", "Reg Write Data", }, |
| 41 | {CMD_IMAGE_VERSION, "IMAGE_VERSION", "image version", }, |
| 42 | {-1, "", "", }, |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * Supported Boot options for configuration file |
| 47 | * this is needed to set the correct flash offset |
| 48 | */ |
| 49 | static table_entry_t imximage_bootops[] = { |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 50 | {FLASH_OFFSET_ONENAND, "onenand", "OneNAND Flash",}, |
Dirk Behme | bd25864 | 2012-01-11 23:28:32 +0000 | [diff] [blame] | 51 | {FLASH_OFFSET_NAND, "nand", "NAND Flash", }, |
Dirk Behme | 19b409c | 2012-01-11 23:28:31 +0000 | [diff] [blame] | 52 | {FLASH_OFFSET_NOR, "nor", "NOR Flash", }, |
| 53 | {FLASH_OFFSET_SATA, "sata", "SATA Disk", }, |
Dirk Behme | bd25864 | 2012-01-11 23:28:32 +0000 | [diff] [blame] | 54 | {FLASH_OFFSET_SD, "sd", "SD Card", }, |
| 55 | {FLASH_OFFSET_SPI, "spi", "SPI Flash", }, |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 56 | {-1, "", "Invalid", }, |
| 57 | }; |
| 58 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 59 | /* |
| 60 | * IMXIMAGE version definition for i.MX chips |
| 61 | */ |
| 62 | static table_entry_t imximage_versions[] = { |
| 63 | {IMXIMAGE_V1, "", " (i.MX25/35/51 compatible)", }, |
Dirk Behme | 19b409c | 2012-01-11 23:28:31 +0000 | [diff] [blame] | 64 | {IMXIMAGE_V2, "", " (i.MX53/6 compatible)", }, |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 65 | {-1, "", " (Invalid)", }, |
| 66 | }; |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 67 | |
| 68 | static struct imx_header imximage_header; |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 69 | static uint32_t imximage_version; |
| 70 | |
| 71 | static set_dcd_val_t set_dcd_val; |
| 72 | static set_dcd_rst_t set_dcd_rst; |
| 73 | static set_imx_hdr_t set_imx_hdr; |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 74 | |
| 75 | static uint32_t get_cfg_value(char *token, char *name, int linenr) |
| 76 | { |
| 77 | char *endptr; |
| 78 | uint32_t value; |
| 79 | |
| 80 | errno = 0; |
| 81 | value = strtoul(token, &endptr, 16); |
| 82 | if (errno || (token == endptr)) { |
| 83 | fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n", |
| 84 | name, linenr, token); |
| 85 | exit(EXIT_FAILURE); |
| 86 | } |
| 87 | return value; |
| 88 | } |
| 89 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 90 | static uint32_t detect_imximage_version(struct imx_header *imx_hdr) |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 91 | { |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 92 | imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1; |
| 93 | imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2; |
| 94 | flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr; |
| 95 | flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr; |
| 96 | |
| 97 | /* Try to detect V1 */ |
| 98 | if ((fhdr_v1->app_code_barker == APP_CODE_BARKER) && |
| 99 | (hdr_v1->dcd_table.preamble.barker == DCD_BARKER)) |
| 100 | return IMXIMAGE_V1; |
| 101 | |
| 102 | /* Try to detect V2 */ |
| 103 | if ((fhdr_v2->header.tag == IVT_HEADER_TAG) && |
| 104 | (hdr_v2->dcd_table.header.tag == DCD_HEADER_TAG)) |
| 105 | return IMXIMAGE_V2; |
| 106 | |
| 107 | return IMXIMAGE_VER_INVALID; |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 110 | static void err_imximage_version(int version) |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 111 | { |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 112 | fprintf(stderr, |
| 113 | "Error: Unsupported imximage version:%d\n", version); |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 114 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 115 | exit(EXIT_FAILURE); |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 118 | static void set_dcd_val_v1(struct imx_header *imxhdr, char *name, int lineno, |
| 119 | int fld, uint32_t value, uint32_t off) |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 120 | { |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 121 | dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table; |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 122 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 123 | switch (fld) { |
| 124 | case CFG_REG_SIZE: |
| 125 | /* Byte, halfword, word */ |
| 126 | if ((value != 1) && (value != 2) && (value != 4)) { |
| 127 | fprintf(stderr, "Error: %s[%d] - " |
| 128 | "Invalid register size " "(%d)\n", |
| 129 | name, lineno, value); |
| 130 | exit(EXIT_FAILURE); |
| 131 | } |
| 132 | dcd_v1->addr_data[off].type = value; |
| 133 | break; |
| 134 | case CFG_REG_ADDRESS: |
| 135 | dcd_v1->addr_data[off].addr = value; |
| 136 | break; |
| 137 | case CFG_REG_VALUE: |
| 138 | dcd_v1->addr_data[off].value = value; |
| 139 | break; |
| 140 | default: |
| 141 | break; |
| 142 | |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static void set_dcd_val_v2(struct imx_header *imxhdr, char *name, int lineno, |
| 147 | int fld, uint32_t value, uint32_t off) |
| 148 | { |
| 149 | dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table; |
| 150 | |
| 151 | switch (fld) { |
| 152 | case CFG_REG_ADDRESS: |
| 153 | dcd_v2->addr_data[off].addr = cpu_to_be32(value); |
| 154 | break; |
| 155 | case CFG_REG_VALUE: |
| 156 | dcd_v2->addr_data[off].value = cpu_to_be32(value); |
| 157 | break; |
| 158 | default: |
| 159 | break; |
| 160 | |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Complete setting up the rest field of DCD of V1 |
| 166 | * such as barker code and DCD data length. |
| 167 | */ |
| 168 | static void set_dcd_rst_v1(struct imx_header *imxhdr, uint32_t dcd_len, |
| 169 | char *name, int lineno) |
| 170 | { |
| 171 | dcd_v1_t *dcd_v1 = &imxhdr->header.hdr_v1.dcd_table; |
| 172 | |
| 173 | if (dcd_len > MAX_HW_CFG_SIZE_V1) { |
| 174 | fprintf(stderr, "Error: %s[%d] -" |
| 175 | "DCD table exceeds maximum size(%d)\n", |
| 176 | name, lineno, MAX_HW_CFG_SIZE_V1); |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 177 | exit(EXIT_FAILURE); |
| 178 | } |
| 179 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 180 | dcd_v1->preamble.barker = DCD_BARKER; |
| 181 | dcd_v1->preamble.length = dcd_len * sizeof(dcd_type_addr_data_t); |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 184 | /* |
| 185 | * Complete setting up the reset field of DCD of V2 |
| 186 | * such as DCD tag, version, length, etc. |
| 187 | */ |
| 188 | static void set_dcd_rst_v2(struct imx_header *imxhdr, uint32_t dcd_len, |
| 189 | char *name, int lineno) |
| 190 | { |
| 191 | dcd_v2_t *dcd_v2 = &imxhdr->header.hdr_v2.dcd_table; |
| 192 | |
| 193 | if (dcd_len > MAX_HW_CFG_SIZE_V2) { |
| 194 | fprintf(stderr, "Error: %s[%d] -" |
| 195 | "DCD table exceeds maximum size(%d)\n", |
| 196 | name, lineno, MAX_HW_CFG_SIZE_V2); |
| 197 | exit(EXIT_FAILURE); |
| 198 | } |
| 199 | |
| 200 | dcd_v2->header.tag = DCD_HEADER_TAG; |
| 201 | dcd_v2->header.length = cpu_to_be16( |
| 202 | dcd_len * sizeof(dcd_addr_data_t) + 8); |
| 203 | dcd_v2->header.version = DCD_VERSION; |
| 204 | dcd_v2->write_dcd_command.tag = DCD_COMMAND_TAG; |
| 205 | dcd_v2->write_dcd_command.length = cpu_to_be16( |
| 206 | dcd_len * sizeof(dcd_addr_data_t) + 4); |
| 207 | dcd_v2->write_dcd_command.param = DCD_COMMAND_PARAM; |
| 208 | } |
| 209 | |
| 210 | static void set_imx_hdr_v1(struct imx_header *imxhdr, uint32_t dcd_len, |
| 211 | struct stat *sbuf, |
| 212 | struct mkimage_params *params) |
| 213 | { |
| 214 | imx_header_v1_t *hdr_v1 = &imxhdr->header.hdr_v1; |
| 215 | flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr; |
| 216 | dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table; |
| 217 | uint32_t base_offset; |
| 218 | |
Dirk Behme | 49d3e27 | 2012-02-22 22:50:19 +0000 | [diff] [blame^] | 219 | /* Exit if there is no BOOT_FROM field specifying the flash_offset */ |
| 220 | if(imxhdr->flash_offset == FLASH_OFFSET_UNDEFINED) { |
| 221 | fprintf(stderr, "Error: Header v1: No BOOT_FROM tag in %s\n", |
| 222 | params->imagename); |
| 223 | exit(EXIT_FAILURE); |
| 224 | } |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 225 | |
| 226 | /* Set magic number */ |
| 227 | fhdr_v1->app_code_barker = APP_CODE_BARKER; |
| 228 | |
| 229 | fhdr_v1->app_dest_ptr = params->addr; |
| 230 | fhdr_v1->app_dest_ptr = params->ep - imxhdr->flash_offset - |
| 231 | sizeof(struct imx_header); |
| 232 | fhdr_v1->app_code_jump_vector = params->ep; |
| 233 | |
| 234 | base_offset = fhdr_v1->app_dest_ptr + imxhdr->flash_offset ; |
| 235 | fhdr_v1->dcd_ptr_ptr = |
| 236 | (uint32_t) (offsetof(flash_header_v1_t, dcd_ptr) - |
| 237 | offsetof(flash_header_v1_t, app_code_jump_vector) + |
| 238 | base_offset); |
| 239 | |
| 240 | fhdr_v1->dcd_ptr = base_offset + |
| 241 | offsetof(imx_header_v1_t, dcd_table); |
| 242 | |
| 243 | /* The external flash header must be at the end of the DCD table */ |
| 244 | dcd_v1->addr_data[dcd_len].type = sbuf->st_size + |
| 245 | imxhdr->flash_offset + |
| 246 | sizeof(struct imx_header); |
| 247 | |
| 248 | /* Security feature are not supported */ |
| 249 | fhdr_v1->app_code_csf = 0; |
| 250 | fhdr_v1->super_root_key = 0; |
| 251 | } |
| 252 | |
| 253 | static void set_imx_hdr_v2(struct imx_header *imxhdr, uint32_t dcd_len, |
| 254 | struct stat *sbuf, |
| 255 | struct mkimage_params *params) |
| 256 | { |
| 257 | imx_header_v2_t *hdr_v2 = &imxhdr->header.hdr_v2; |
| 258 | flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr; |
| 259 | |
Dirk Behme | 49d3e27 | 2012-02-22 22:50:19 +0000 | [diff] [blame^] | 260 | /* Exit if there is no BOOT_FROM field specifying the flash_offset */ |
| 261 | if(imxhdr->flash_offset == FLASH_OFFSET_UNDEFINED) { |
| 262 | fprintf(stderr, "Error: Header v2: No BOOT_FROM tag in %s\n", |
| 263 | params->imagename); |
| 264 | exit(EXIT_FAILURE); |
| 265 | } |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 266 | |
| 267 | /* Set magic number */ |
| 268 | fhdr_v2->header.tag = IVT_HEADER_TAG; /* 0xD1 */ |
| 269 | fhdr_v2->header.length = cpu_to_be16(sizeof(flash_header_v2_t)); |
| 270 | fhdr_v2->header.version = IVT_VERSION; /* 0x40 */ |
| 271 | |
| 272 | fhdr_v2->entry = params->ep; |
| 273 | fhdr_v2->reserved1 = fhdr_v2->reserved2 = 0; |
| 274 | fhdr_v2->self = params->ep - sizeof(struct imx_header); |
| 275 | |
| 276 | fhdr_v2->dcd_ptr = fhdr_v2->self + |
| 277 | offsetof(imx_header_v2_t, dcd_table); |
| 278 | |
| 279 | fhdr_v2->boot_data_ptr = fhdr_v2->self + |
| 280 | offsetof(imx_header_v2_t, boot_data); |
| 281 | |
| 282 | hdr_v2->boot_data.start = fhdr_v2->self - imxhdr->flash_offset; |
| 283 | hdr_v2->boot_data.size = sbuf->st_size + |
| 284 | imxhdr->flash_offset + |
| 285 | sizeof(struct imx_header); |
| 286 | |
| 287 | /* Security feature are not supported */ |
| 288 | fhdr_v2->csf = 0; |
| 289 | } |
| 290 | |
| 291 | static void set_hdr_func(struct imx_header *imxhdr) |
| 292 | { |
| 293 | switch (imximage_version) { |
| 294 | case IMXIMAGE_V1: |
| 295 | set_dcd_val = set_dcd_val_v1; |
| 296 | set_dcd_rst = set_dcd_rst_v1; |
| 297 | set_imx_hdr = set_imx_hdr_v1; |
| 298 | break; |
| 299 | case IMXIMAGE_V2: |
| 300 | set_dcd_val = set_dcd_val_v2; |
| 301 | set_dcd_rst = set_dcd_rst_v2; |
| 302 | set_imx_hdr = set_imx_hdr_v2; |
| 303 | break; |
| 304 | default: |
| 305 | err_imximage_version(imximage_version); |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | static void print_hdr_v1(struct imx_header *imx_hdr) |
| 311 | { |
| 312 | imx_header_v1_t *hdr_v1 = &imx_hdr->header.hdr_v1; |
| 313 | flash_header_v1_t *fhdr_v1 = &hdr_v1->fhdr; |
| 314 | dcd_v1_t *dcd_v1 = &hdr_v1->dcd_table; |
| 315 | uint32_t size, length, ver; |
| 316 | |
| 317 | size = dcd_v1->preamble.length; |
| 318 | if (size > (MAX_HW_CFG_SIZE_V1 * sizeof(dcd_type_addr_data_t))) { |
| 319 | fprintf(stderr, |
| 320 | "Error: Image corrupt DCD size %d exceed maximum %d\n", |
| 321 | (uint32_t)(size / sizeof(dcd_type_addr_data_t)), |
| 322 | MAX_HW_CFG_SIZE_V1); |
| 323 | exit(EXIT_FAILURE); |
| 324 | } |
| 325 | |
| 326 | length = dcd_v1->preamble.length / sizeof(dcd_type_addr_data_t); |
| 327 | ver = detect_imximage_version(imx_hdr); |
| 328 | |
| 329 | printf("Image Type: Freescale IMX Boot Image\n"); |
| 330 | printf("Image Ver: %x", ver); |
| 331 | printf("%s\n", get_table_entry_name(imximage_versions, NULL, ver)); |
| 332 | printf("Data Size: "); |
| 333 | genimg_print_size(dcd_v1->addr_data[length].type); |
| 334 | printf("Load Address: %08x\n", (uint32_t)fhdr_v1->app_dest_ptr); |
| 335 | printf("Entry Point: %08x\n", (uint32_t)fhdr_v1->app_code_jump_vector); |
| 336 | } |
| 337 | |
| 338 | static void print_hdr_v2(struct imx_header *imx_hdr) |
| 339 | { |
| 340 | imx_header_v2_t *hdr_v2 = &imx_hdr->header.hdr_v2; |
| 341 | flash_header_v2_t *fhdr_v2 = &hdr_v2->fhdr; |
| 342 | dcd_v2_t *dcd_v2 = &hdr_v2->dcd_table; |
| 343 | uint32_t size, version; |
| 344 | |
| 345 | size = be16_to_cpu(dcd_v2->header.length) - 8; |
| 346 | if (size > (MAX_HW_CFG_SIZE_V2 * sizeof(dcd_addr_data_t))) { |
| 347 | fprintf(stderr, |
| 348 | "Error: Image corrupt DCD size %d exceed maximum %d\n", |
| 349 | (uint32_t)(size / sizeof(dcd_addr_data_t)), |
| 350 | MAX_HW_CFG_SIZE_V2); |
| 351 | exit(EXIT_FAILURE); |
| 352 | } |
| 353 | |
| 354 | version = detect_imximage_version(imx_hdr); |
| 355 | |
| 356 | printf("Image Type: Freescale IMX Boot Image\n"); |
| 357 | printf("Image Ver: %x", version); |
| 358 | printf("%s\n", get_table_entry_name(imximage_versions, NULL, version)); |
| 359 | printf("Data Size: "); |
| 360 | genimg_print_size(hdr_v2->boot_data.size); |
| 361 | printf("Load Address: %08x\n", (uint32_t)fhdr_v2->boot_data_ptr); |
| 362 | printf("Entry Point: %08x\n", (uint32_t)fhdr_v2->entry); |
| 363 | } |
| 364 | |
| 365 | static void parse_cfg_cmd(struct imx_header *imxhdr, int32_t cmd, char *token, |
| 366 | char *name, int lineno, int fld, int dcd_len) |
| 367 | { |
| 368 | int value; |
| 369 | static int cmd_ver_first = ~0; |
| 370 | |
| 371 | switch (cmd) { |
| 372 | case CMD_IMAGE_VERSION: |
| 373 | imximage_version = get_cfg_value(token, name, lineno); |
| 374 | if (cmd_ver_first == 0) { |
| 375 | fprintf(stderr, "Error: %s[%d] - IMAGE_VERSION " |
| 376 | "command need be the first before other " |
| 377 | "valid command in the file\n", name, lineno); |
| 378 | exit(EXIT_FAILURE); |
| 379 | } |
| 380 | cmd_ver_first = 1; |
| 381 | set_hdr_func(imxhdr); |
| 382 | break; |
| 383 | case CMD_BOOT_FROM: |
| 384 | imxhdr->flash_offset = get_table_entry_id(imximage_bootops, |
| 385 | "imximage boot option", token); |
| 386 | if (imxhdr->flash_offset == -1) { |
| 387 | fprintf(stderr, "Error: %s[%d] -Invalid boot device" |
| 388 | "(%s)\n", name, lineno, token); |
| 389 | exit(EXIT_FAILURE); |
| 390 | } |
| 391 | if (unlikely(cmd_ver_first != 1)) |
| 392 | cmd_ver_first = 0; |
| 393 | break; |
| 394 | case CMD_DATA: |
| 395 | value = get_cfg_value(token, name, lineno); |
| 396 | (*set_dcd_val)(imxhdr, name, lineno, fld, value, dcd_len); |
| 397 | if (unlikely(cmd_ver_first != 1)) |
| 398 | cmd_ver_first = 0; |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | static void parse_cfg_fld(struct imx_header *imxhdr, int32_t *cmd, |
| 404 | char *token, char *name, int lineno, int fld, int *dcd_len) |
| 405 | { |
| 406 | int value; |
| 407 | |
| 408 | switch (fld) { |
| 409 | case CFG_COMMAND: |
| 410 | *cmd = get_table_entry_id(imximage_cmds, |
| 411 | "imximage commands", token); |
| 412 | if (*cmd < 0) { |
| 413 | fprintf(stderr, "Error: %s[%d] - Invalid command" |
| 414 | "(%s)\n", name, lineno, token); |
| 415 | exit(EXIT_FAILURE); |
| 416 | } |
| 417 | break; |
| 418 | case CFG_REG_SIZE: |
| 419 | parse_cfg_cmd(imxhdr, *cmd, token, name, lineno, fld, *dcd_len); |
| 420 | break; |
| 421 | case CFG_REG_ADDRESS: |
| 422 | case CFG_REG_VALUE: |
| 423 | if (*cmd != CMD_DATA) |
| 424 | return; |
| 425 | |
| 426 | value = get_cfg_value(token, name, lineno); |
| 427 | (*set_dcd_val)(imxhdr, name, lineno, fld, value, *dcd_len); |
| 428 | |
| 429 | if (fld == CFG_REG_VALUE) |
| 430 | (*dcd_len)++; |
| 431 | break; |
| 432 | default: |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | static uint32_t parse_cfg_file(struct imx_header *imxhdr, char *name) |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 437 | { |
| 438 | FILE *fd = NULL; |
| 439 | char *line = NULL; |
| 440 | char *token, *saveptr1, *saveptr2; |
| 441 | int lineno = 0; |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 442 | int fld; |
Kim Phillips | 0ad2270 | 2010-02-22 19:37:56 -0600 | [diff] [blame] | 443 | size_t len; |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 444 | int dcd_len = 0; |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 445 | int32_t cmd; |
| 446 | |
| 447 | fd = fopen(name, "r"); |
| 448 | if (fd == 0) { |
| 449 | fprintf(stderr, "Error: %s - Can't open DCD file\n", name); |
| 450 | exit(EXIT_FAILURE); |
| 451 | } |
| 452 | |
| 453 | /* Very simple parsing, line starting with # are comments |
| 454 | * and are dropped |
| 455 | */ |
| 456 | while ((getline(&line, &len, fd)) > 0) { |
| 457 | lineno++; |
| 458 | |
| 459 | token = strtok_r(line, "\r\n", &saveptr1); |
| 460 | if (token == NULL) |
| 461 | continue; |
| 462 | |
| 463 | /* Check inside the single line */ |
| 464 | for (fld = CFG_COMMAND, cmd = CMD_INVALID, |
| 465 | line = token; ; line = NULL, fld++) { |
| 466 | token = strtok_r(line, " \t", &saveptr2); |
| 467 | if (token == NULL) |
| 468 | break; |
| 469 | |
| 470 | /* Drop all text starting with '#' as comments */ |
| 471 | if (token[0] == '#') |
| 472 | break; |
| 473 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 474 | parse_cfg_fld(imxhdr, &cmd, token, name, |
| 475 | lineno, fld, &dcd_len); |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 478 | } |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 479 | |
| 480 | (*set_dcd_rst)(imxhdr, dcd_len, name, lineno); |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 481 | fclose(fd); |
| 482 | |
Stefano Babic | 5b28e91 | 2010-02-05 15:16:02 +0100 | [diff] [blame] | 483 | return dcd_len; |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 486 | |
| 487 | static int imximage_check_image_types(uint8_t type) |
| 488 | { |
| 489 | if (type == IH_TYPE_IMXIMAGE) |
| 490 | return EXIT_SUCCESS; |
| 491 | else |
| 492 | return EXIT_FAILURE; |
| 493 | } |
| 494 | |
| 495 | static int imximage_verify_header(unsigned char *ptr, int image_size, |
| 496 | struct mkimage_params *params) |
| 497 | { |
| 498 | struct imx_header *imx_hdr = (struct imx_header *) ptr; |
| 499 | |
| 500 | if (detect_imximage_version(imx_hdr) == IMXIMAGE_VER_INVALID) |
| 501 | return -FDT_ERR_BADSTRUCTURE; |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static void imximage_print_header(const void *ptr) |
| 507 | { |
| 508 | struct imx_header *imx_hdr = (struct imx_header *) ptr; |
| 509 | uint32_t version = detect_imximage_version(imx_hdr); |
| 510 | |
| 511 | switch (version) { |
| 512 | case IMXIMAGE_V1: |
| 513 | print_hdr_v1(imx_hdr); |
| 514 | break; |
| 515 | case IMXIMAGE_V2: |
| 516 | print_hdr_v2(imx_hdr); |
| 517 | break; |
| 518 | default: |
| 519 | err_imximage_version(version); |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 524 | static void imximage_set_header(void *ptr, struct stat *sbuf, int ifd, |
| 525 | struct mkimage_params *params) |
| 526 | { |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 527 | struct imx_header *imxhdr = (struct imx_header *)ptr; |
| 528 | uint32_t dcd_len; |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 529 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 530 | /* |
| 531 | * In order to not change the old imx cfg file |
| 532 | * by adding VERSION command into it, here need |
| 533 | * set up function ptr group to V1 by default. |
| 534 | */ |
| 535 | imximage_version = IMXIMAGE_V1; |
Dirk Behme | 49d3e27 | 2012-02-22 22:50:19 +0000 | [diff] [blame^] | 536 | /* Be able to detect if the cfg file has no BOOT_FROM tag */ |
| 537 | imxhdr->flash_offset = FLASH_OFFSET_UNDEFINED; |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 538 | set_hdr_func(imxhdr); |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 539 | |
| 540 | /* Parse dcd configuration file */ |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 541 | dcd_len = parse_cfg_file(imxhdr, params->imagename); |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 542 | |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 543 | /* Set the imx header */ |
| 544 | (*set_imx_hdr)(imxhdr, dcd_len, sbuf, params); |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | int imximage_check_params(struct mkimage_params *params) |
| 548 | { |
| 549 | if (!params) |
| 550 | return CFG_INVALID; |
| 551 | if (!strlen(params->imagename)) { |
| 552 | fprintf(stderr, "Error: %s - Configuration file not specified, " |
| 553 | "it is needed for imximage generation\n", |
| 554 | params->cmdname); |
| 555 | return CFG_INVALID; |
| 556 | } |
| 557 | /* |
| 558 | * Check parameters: |
| 559 | * XIP is not allowed and verify that incompatible |
| 560 | * parameters are not sent at the same time |
| 561 | * For example, if list is required a data image must not be provided |
| 562 | */ |
| 563 | return (params->dflag && (params->fflag || params->lflag)) || |
| 564 | (params->fflag && (params->dflag || params->lflag)) || |
| 565 | (params->lflag && (params->dflag || params->fflag)) || |
| 566 | (params->xflag) || !(strlen(params->imagename)); |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * imximage parameters |
| 571 | */ |
| 572 | static struct image_type_params imximage_params = { |
Liu Hui-R64343 | 8a1edd7 | 2011-01-19 09:40:26 +0000 | [diff] [blame] | 573 | .name = "Freescale i.MX 5x Boot Image support", |
Stefano Babic | 8edcde5 | 2010-01-20 18:19:10 +0100 | [diff] [blame] | 574 | .header_size = sizeof(struct imx_header), |
| 575 | .hdr = (void *)&imximage_header, |
| 576 | .check_image_type = imximage_check_image_types, |
| 577 | .verify_header = imximage_verify_header, |
| 578 | .print_header = imximage_print_header, |
| 579 | .set_header = imximage_set_header, |
| 580 | .check_params = imximage_check_params, |
| 581 | }; |
| 582 | |
| 583 | void init_imx_image_type(void) |
| 584 | { |
| 585 | mkimage_register(&imximage_params); |
| 586 | } |