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