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