Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1 | /* |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 2 | * Image manipulator for Marvell SoCs |
| 3 | * supports Kirkwood, Dove, Armada 370, and Armada XP |
| 4 | * |
| 5 | * (C) Copyright 2013 Thomas Petazzoni |
| 6 | * <thomas.petazzoni@free-electrons.com> |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 7 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 9 | * |
| 10 | * Not implemented: support for the register headers and secure |
| 11 | * headers in v1 images |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 12 | */ |
| 13 | |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 14 | #include "imagetool.h" |
Andreas Bießmann | 7abec5b | 2014-10-24 23:39:11 +0200 | [diff] [blame] | 15 | #include <limits.h> |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 16 | #include <image.h> |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 17 | #include <stdint.h> |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 18 | #include "kwbimage.h" |
| 19 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 20 | static struct image_cfg_element *image_cfg; |
| 21 | static int cfgn; |
| 22 | |
| 23 | struct boot_mode { |
| 24 | unsigned int id; |
| 25 | const char *name; |
| 26 | }; |
| 27 | |
| 28 | struct boot_mode boot_modes[] = { |
| 29 | { 0x4D, "i2c" }, |
| 30 | { 0x5A, "spi" }, |
| 31 | { 0x8B, "nand" }, |
| 32 | { 0x78, "sata" }, |
| 33 | { 0x9C, "pex" }, |
| 34 | { 0x69, "uart" }, |
Stefan Roese | 539fe4a | 2015-07-20 11:20:37 +0200 | [diff] [blame] | 35 | { 0xAE, "sdio" }, |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 36 | {}, |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 37 | }; |
| 38 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 39 | struct nand_ecc_mode { |
| 40 | unsigned int id; |
| 41 | const char *name; |
| 42 | }; |
| 43 | |
| 44 | struct nand_ecc_mode nand_ecc_modes[] = { |
| 45 | { 0x00, "default" }, |
| 46 | { 0x01, "hamming" }, |
| 47 | { 0x02, "rs" }, |
| 48 | { 0x03, "disabled" }, |
| 49 | {}, |
| 50 | }; |
| 51 | |
| 52 | /* Used to identify an undefined execution or destination address */ |
| 53 | #define ADDR_INVALID ((uint32_t)-1) |
| 54 | |
| 55 | #define BINARY_MAX_ARGS 8 |
| 56 | |
| 57 | /* In-memory representation of a line of the configuration file */ |
| 58 | struct image_cfg_element { |
| 59 | enum { |
| 60 | IMAGE_CFG_VERSION = 0x1, |
| 61 | IMAGE_CFG_BOOT_FROM, |
| 62 | IMAGE_CFG_DEST_ADDR, |
| 63 | IMAGE_CFG_EXEC_ADDR, |
| 64 | IMAGE_CFG_NAND_BLKSZ, |
| 65 | IMAGE_CFG_NAND_BADBLK_LOCATION, |
| 66 | IMAGE_CFG_NAND_ECC_MODE, |
| 67 | IMAGE_CFG_NAND_PAGESZ, |
| 68 | IMAGE_CFG_BINARY, |
| 69 | IMAGE_CFG_PAYLOAD, |
| 70 | IMAGE_CFG_DATA, |
Chris Packham | 883bf45 | 2016-11-09 22:07:45 +1300 | [diff] [blame] | 71 | IMAGE_CFG_BAUDRATE, |
Chris Packham | 1e0728a | 2016-11-09 22:21:45 +1300 | [diff] [blame] | 72 | IMAGE_CFG_DEBUG, |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 73 | } type; |
| 74 | union { |
| 75 | unsigned int version; |
| 76 | unsigned int bootfrom; |
| 77 | struct { |
| 78 | const char *file; |
| 79 | unsigned int args[BINARY_MAX_ARGS]; |
| 80 | unsigned int nargs; |
| 81 | } binary; |
| 82 | const char *payload; |
| 83 | unsigned int dstaddr; |
| 84 | unsigned int execaddr; |
| 85 | unsigned int nandblksz; |
| 86 | unsigned int nandbadblklocation; |
| 87 | unsigned int nandeccmode; |
| 88 | unsigned int nandpagesz; |
| 89 | struct ext_hdr_v0_reg regdata; |
Chris Packham | 883bf45 | 2016-11-09 22:07:45 +1300 | [diff] [blame] | 90 | unsigned int baudrate; |
Chris Packham | 1e0728a | 2016-11-09 22:21:45 +1300 | [diff] [blame] | 91 | unsigned int debug; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 92 | }; |
| 93 | }; |
| 94 | |
| 95 | #define IMAGE_CFG_ELEMENT_MAX 256 |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 96 | |
| 97 | /* |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 98 | * Utility functions to manipulate boot mode and ecc modes (convert |
| 99 | * them back and forth between description strings and the |
| 100 | * corresponding numerical identifiers). |
| 101 | */ |
| 102 | |
| 103 | static const char *image_boot_mode_name(unsigned int id) |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 104 | { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 105 | int i; |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 106 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 107 | for (i = 0; boot_modes[i].name; i++) |
| 108 | if (boot_modes[i].id == id) |
| 109 | return boot_modes[i].name; |
| 110 | return NULL; |
| 111 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 112 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 113 | int image_boot_mode_id(const char *boot_mode_name) |
| 114 | { |
| 115 | int i; |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 116 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 117 | for (i = 0; boot_modes[i].name; i++) |
| 118 | if (!strcmp(boot_modes[i].name, boot_mode_name)) |
| 119 | return boot_modes[i].id; |
| 120 | |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | int image_nand_ecc_mode_id(const char *nand_ecc_mode_name) |
| 125 | { |
| 126 | int i; |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 127 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 128 | for (i = 0; nand_ecc_modes[i].name; i++) |
| 129 | if (!strcmp(nand_ecc_modes[i].name, nand_ecc_mode_name)) |
| 130 | return nand_ecc_modes[i].id; |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | static struct image_cfg_element * |
| 135 | image_find_option(unsigned int optiontype) |
| 136 | { |
| 137 | int i; |
| 138 | |
| 139 | for (i = 0; i < cfgn; i++) { |
| 140 | if (image_cfg[i].type == optiontype) |
| 141 | return &image_cfg[i]; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 142 | } |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 143 | |
| 144 | return NULL; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 145 | } |
| 146 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 147 | static unsigned int |
| 148 | image_count_options(unsigned int optiontype) |
| 149 | { |
| 150 | int i; |
| 151 | unsigned int count = 0; |
| 152 | |
| 153 | for (i = 0; i < cfgn; i++) |
| 154 | if (image_cfg[i].type == optiontype) |
| 155 | count++; |
| 156 | |
| 157 | return count; |
| 158 | } |
| 159 | |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 160 | /* |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 161 | * Compute a 8-bit checksum of a memory area. This algorithm follows |
| 162 | * the requirements of the Marvell SoC BootROM specifications. |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 163 | */ |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 164 | static uint8_t image_checksum8(void *start, uint32_t len) |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 165 | { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 166 | uint8_t csum = 0; |
| 167 | uint8_t *p = start; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 168 | |
| 169 | /* check len and return zero checksum if invalid */ |
| 170 | if (!len) |
| 171 | return 0; |
| 172 | |
| 173 | do { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 174 | csum += *p; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 175 | p++; |
| 176 | } while (--len); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 177 | |
| 178 | return csum; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 179 | } |
| 180 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 181 | static uint32_t image_checksum32(void *start, uint32_t len) |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 182 | { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 183 | uint32_t csum = 0; |
| 184 | uint32_t *p = start; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 185 | |
| 186 | /* check len and return zero checksum if invalid */ |
| 187 | if (!len) |
| 188 | return 0; |
| 189 | |
| 190 | if (len % sizeof(uint32_t)) { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 191 | fprintf(stderr, "Length %d is not in multiple of %zu\n", |
| 192 | len, sizeof(uint32_t)); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | do { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 197 | csum += *p; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 198 | p++; |
| 199 | len -= sizeof(uint32_t); |
| 200 | } while (len > 0); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 201 | |
| 202 | return csum; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 203 | } |
| 204 | |
Chris Packham | 883bf45 | 2016-11-09 22:07:45 +1300 | [diff] [blame] | 205 | static uint8_t baudrate_to_option(unsigned int baudrate) |
| 206 | { |
| 207 | switch (baudrate) { |
| 208 | case 2400: |
| 209 | return MAIN_HDR_V1_OPT_BAUD_2400; |
| 210 | case 4800: |
| 211 | return MAIN_HDR_V1_OPT_BAUD_4800; |
| 212 | case 9600: |
| 213 | return MAIN_HDR_V1_OPT_BAUD_9600; |
| 214 | case 19200: |
| 215 | return MAIN_HDR_V1_OPT_BAUD_19200; |
| 216 | case 38400: |
| 217 | return MAIN_HDR_V1_OPT_BAUD_38400; |
| 218 | case 57600: |
| 219 | return MAIN_HDR_V1_OPT_BAUD_57600; |
| 220 | case 115200: |
| 221 | return MAIN_HDR_V1_OPT_BAUD_115200; |
| 222 | default: |
| 223 | return MAIN_HDR_V1_OPT_BAUD_DEFAULT; |
| 224 | } |
| 225 | } |
| 226 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 227 | static void *image_create_v0(size_t *imagesz, struct image_tool_params *params, |
| 228 | int payloadsz) |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 229 | { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 230 | struct image_cfg_element *e; |
| 231 | size_t headersz; |
| 232 | struct main_hdr_v0 *main_hdr; |
| 233 | struct ext_hdr_v0 *ext_hdr; |
Mario Six | 7497cd6 | 2017-01-11 16:00:55 +0100 | [diff] [blame^] | 234 | uint8_t *image; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 235 | int has_ext = 0; |
| 236 | |
| 237 | /* |
| 238 | * Calculate the size of the header and the size of the |
| 239 | * payload |
| 240 | */ |
| 241 | headersz = sizeof(struct main_hdr_v0); |
| 242 | |
| 243 | if (image_count_options(IMAGE_CFG_DATA) > 0) { |
| 244 | has_ext = 1; |
| 245 | headersz += sizeof(struct ext_hdr_v0); |
| 246 | } |
| 247 | |
| 248 | if (image_count_options(IMAGE_CFG_PAYLOAD) > 1) { |
| 249 | fprintf(stderr, "More than one payload, not possible\n"); |
| 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | image = malloc(headersz); |
| 254 | if (!image) { |
| 255 | fprintf(stderr, "Cannot allocate memory for image\n"); |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | memset(image, 0, headersz); |
| 260 | |
Mario Six | 7497cd6 | 2017-01-11 16:00:55 +0100 | [diff] [blame^] | 261 | main_hdr = (struct main_hdr_v0 *)image; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 262 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 263 | /* Fill in the main header */ |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 264 | main_hdr->blocksize = |
| 265 | cpu_to_le32(payloadsz + sizeof(uint32_t) - headersz); |
| 266 | main_hdr->srcaddr = cpu_to_le32(headersz); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 267 | main_hdr->ext = has_ext; |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 268 | main_hdr->destaddr = cpu_to_le32(params->addr); |
| 269 | main_hdr->execaddr = cpu_to_le32(params->ep); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 270 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 271 | e = image_find_option(IMAGE_CFG_BOOT_FROM); |
| 272 | if (e) |
| 273 | main_hdr->blockid = e->bootfrom; |
| 274 | e = image_find_option(IMAGE_CFG_NAND_ECC_MODE); |
| 275 | if (e) |
| 276 | main_hdr->nandeccmode = e->nandeccmode; |
| 277 | e = image_find_option(IMAGE_CFG_NAND_PAGESZ); |
| 278 | if (e) |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 279 | main_hdr->nandpagesize = cpu_to_le16(e->nandpagesz); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 280 | main_hdr->checksum = image_checksum8(image, |
| 281 | sizeof(struct main_hdr_v0)); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 282 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 283 | /* Generate the ext header */ |
| 284 | if (has_ext) { |
| 285 | int cfgi, datai; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 286 | |
Mario Six | 7497cd6 | 2017-01-11 16:00:55 +0100 | [diff] [blame^] | 287 | ext_hdr = (struct ext_hdr_v0 *) |
| 288 | (image + sizeof(struct main_hdr_v0)); |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 289 | ext_hdr->offset = cpu_to_le32(0x40); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 290 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 291 | for (cfgi = 0, datai = 0; cfgi < cfgn; cfgi++) { |
| 292 | e = &image_cfg[cfgi]; |
| 293 | if (e->type != IMAGE_CFG_DATA) |
| 294 | continue; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 295 | |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 296 | ext_hdr->rcfg[datai].raddr = |
| 297 | cpu_to_le32(e->regdata.raddr); |
| 298 | ext_hdr->rcfg[datai].rdata = |
| 299 | cpu_to_le32(e->regdata.rdata); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 300 | datai++; |
| 301 | } |
| 302 | |
| 303 | ext_hdr->checksum = image_checksum8(ext_hdr, |
| 304 | sizeof(struct ext_hdr_v0)); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 305 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 306 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 307 | *imagesz = headersz; |
| 308 | return image; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 309 | } |
| 310 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 311 | static size_t image_headersz_v1(struct image_tool_params *params, |
| 312 | int *hasext) |
| 313 | { |
| 314 | struct image_cfg_element *binarye; |
| 315 | size_t headersz; |
| 316 | int ret; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 317 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 318 | /* |
| 319 | * Calculate the size of the header and the size of the |
| 320 | * payload |
| 321 | */ |
| 322 | headersz = sizeof(struct main_hdr_v1); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 323 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 324 | if (image_count_options(IMAGE_CFG_BINARY) > 1) { |
| 325 | fprintf(stderr, "More than one binary blob, not supported\n"); |
| 326 | return 0; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 327 | } |
| 328 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 329 | if (image_count_options(IMAGE_CFG_PAYLOAD) > 1) { |
| 330 | fprintf(stderr, "More than one payload, not possible\n"); |
| 331 | return 0; |
| 332 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 333 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 334 | binarye = image_find_option(IMAGE_CFG_BINARY); |
| 335 | if (binarye) { |
| 336 | struct stat s; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 337 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 338 | ret = stat(binarye->binary.file, &s); |
| 339 | if (ret < 0) { |
Andreas Bießmann | 7abec5b | 2014-10-24 23:39:11 +0200 | [diff] [blame] | 340 | char cwd[PATH_MAX]; |
| 341 | char *dir = cwd; |
| 342 | |
| 343 | memset(cwd, 0, sizeof(cwd)); |
| 344 | if (!getcwd(cwd, sizeof(cwd))) { |
| 345 | dir = "current working directory"; |
| 346 | perror("getcwd() failed"); |
| 347 | } |
| 348 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 349 | fprintf(stderr, |
| 350 | "Didn't find the file '%s' in '%s' which is mandatory to generate the image\n" |
| 351 | "This file generally contains the DDR3 training code, and should be extracted from an existing bootable\n" |
| 352 | "image for your board. See 'kwbimage -x' to extract it from an existing image.\n", |
Andreas Bießmann | 7abec5b | 2014-10-24 23:39:11 +0200 | [diff] [blame] | 353 | binarye->binary.file, dir); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 354 | return 0; |
| 355 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 356 | |
Reinhard Pfau | 5286c0d | 2015-11-29 15:52:14 +0100 | [diff] [blame] | 357 | headersz += sizeof(struct opt_hdr_v1) + |
| 358 | s.st_size + |
| 359 | (binarye->binary.nargs + 2) * sizeof(uint32_t); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 360 | if (hasext) |
| 361 | *hasext = 1; |
| 362 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 363 | |
Stefan Roese | 69e9dde | 2015-07-20 11:20:38 +0200 | [diff] [blame] | 364 | #if defined(CONFIG_SYS_U_BOOT_OFFS) |
| 365 | if (headersz > CONFIG_SYS_U_BOOT_OFFS) { |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 366 | fprintf(stderr, |
| 367 | "Error: Image header (incl. SPL image) too big!\n"); |
Stefan Roese | 69e9dde | 2015-07-20 11:20:38 +0200 | [diff] [blame] | 368 | fprintf(stderr, "header=0x%x CONFIG_SYS_U_BOOT_OFFS=0x%x!\n", |
| 369 | (int)headersz, CONFIG_SYS_U_BOOT_OFFS); |
| 370 | fprintf(stderr, "Increase CONFIG_SYS_U_BOOT_OFFS!\n"); |
Kevin Smith | 8880949 | 2015-03-16 14:58:21 +0000 | [diff] [blame] | 371 | return 0; |
Kevin Smith | 8880949 | 2015-03-16 14:58:21 +0000 | [diff] [blame] | 372 | } |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 373 | headersz = CONFIG_SYS_U_BOOT_OFFS; |
Kevin Smith | 8880949 | 2015-03-16 14:58:21 +0000 | [diff] [blame] | 374 | #endif |
| 375 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 376 | /* |
| 377 | * The payload should be aligned on some reasonable |
| 378 | * boundary |
| 379 | */ |
| 380 | return ALIGN_SUP(headersz, 4096); |
| 381 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 382 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 383 | static void *image_create_v1(size_t *imagesz, struct image_tool_params *params, |
| 384 | int payloadsz) |
| 385 | { |
| 386 | struct image_cfg_element *e, *binarye; |
| 387 | struct main_hdr_v1 *main_hdr; |
| 388 | size_t headersz; |
Mario Six | 7497cd6 | 2017-01-11 16:00:55 +0100 | [diff] [blame^] | 389 | uint8_t *image, *cur; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 390 | int hasext = 0; |
| 391 | int ret; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 392 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 393 | /* |
| 394 | * Calculate the size of the header and the size of the |
| 395 | * payload |
| 396 | */ |
| 397 | headersz = image_headersz_v1(params, &hasext); |
| 398 | if (headersz == 0) |
| 399 | return NULL; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 400 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 401 | image = malloc(headersz); |
| 402 | if (!image) { |
| 403 | fprintf(stderr, "Cannot allocate memory for image\n"); |
| 404 | return NULL; |
| 405 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 406 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 407 | memset(image, 0, headersz); |
| 408 | |
Mario Six | 7497cd6 | 2017-01-11 16:00:55 +0100 | [diff] [blame^] | 409 | main_hdr = (struct main_hdr_v1 *)image; |
| 410 | cur = image + sizeof(struct main_hdr_v1); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 411 | |
| 412 | /* Fill the main header */ |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 413 | main_hdr->blocksize = |
| 414 | cpu_to_le32(payloadsz - headersz + sizeof(uint32_t)); |
| 415 | main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 416 | main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16; |
Mario Six | 7e40763 | 2017-01-11 16:00:53 +0100 | [diff] [blame] | 417 | main_hdr->destaddr = cpu_to_le32(params->addr) |
| 418 | - sizeof(image_header_t); |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 419 | main_hdr->execaddr = cpu_to_le32(params->ep); |
| 420 | main_hdr->srcaddr = cpu_to_le32(headersz); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 421 | main_hdr->ext = hasext; |
| 422 | main_hdr->version = 1; |
| 423 | e = image_find_option(IMAGE_CFG_BOOT_FROM); |
| 424 | if (e) |
| 425 | main_hdr->blockid = e->bootfrom; |
| 426 | e = image_find_option(IMAGE_CFG_NAND_BLKSZ); |
| 427 | if (e) |
| 428 | main_hdr->nandblocksize = e->nandblksz / (64 * 1024); |
| 429 | e = image_find_option(IMAGE_CFG_NAND_BADBLK_LOCATION); |
| 430 | if (e) |
| 431 | main_hdr->nandbadblklocation = e->nandbadblklocation; |
Chris Packham | 883bf45 | 2016-11-09 22:07:45 +1300 | [diff] [blame] | 432 | e = image_find_option(IMAGE_CFG_BAUDRATE); |
| 433 | if (e) |
| 434 | main_hdr->options = baudrate_to_option(e->baudrate); |
Chris Packham | 1e0728a | 2016-11-09 22:21:45 +1300 | [diff] [blame] | 435 | e = image_find_option(IMAGE_CFG_DEBUG); |
| 436 | if (e) |
| 437 | main_hdr->flags = e->debug ? 0x1 : 0; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 438 | |
| 439 | binarye = image_find_option(IMAGE_CFG_BINARY); |
| 440 | if (binarye) { |
Mario Six | 7497cd6 | 2017-01-11 16:00:55 +0100 | [diff] [blame^] | 441 | struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)cur; |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 442 | uint32_t *args; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 443 | size_t binhdrsz; |
| 444 | struct stat s; |
| 445 | int argi; |
| 446 | FILE *bin; |
| 447 | |
| 448 | hdr->headertype = OPT_HDR_V1_BINARY_TYPE; |
| 449 | |
| 450 | bin = fopen(binarye->binary.file, "r"); |
| 451 | if (!bin) { |
| 452 | fprintf(stderr, "Cannot open binary file %s\n", |
| 453 | binarye->binary.file); |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | fstat(fileno(bin), &s); |
| 458 | |
| 459 | binhdrsz = sizeof(struct opt_hdr_v1) + |
Reinhard Pfau | 5286c0d | 2015-11-29 15:52:14 +0100 | [diff] [blame] | 460 | (binarye->binary.nargs + 2) * sizeof(uint32_t) + |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 461 | s.st_size; |
Stefan Roese | a4279a5 | 2015-10-28 07:53:58 +0100 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * The size includes the binary image size, rounded |
| 465 | * up to a 4-byte boundary. Plus 4 bytes for the |
| 466 | * next-header byte and 3-byte alignment at the end. |
| 467 | */ |
| 468 | binhdrsz = ALIGN_SUP(binhdrsz, 4) + 4; |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 469 | hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 470 | hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16; |
| 471 | |
| 472 | cur += sizeof(struct opt_hdr_v1); |
| 473 | |
Mario Six | 7497cd6 | 2017-01-11 16:00:55 +0100 | [diff] [blame^] | 474 | args = (uint32_t *)cur; |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 475 | *args = cpu_to_le32(binarye->binary.nargs); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 476 | args++; |
| 477 | for (argi = 0; argi < binarye->binary.nargs; argi++) |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 478 | args[argi] = cpu_to_le32(binarye->binary.args[argi]); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 479 | |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 480 | cur += (binarye->binary.nargs + 1) * sizeof(uint32_t); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 481 | |
| 482 | ret = fread(cur, s.st_size, 1, bin); |
| 483 | if (ret != 1) { |
| 484 | fprintf(stderr, |
| 485 | "Could not read binary image %s\n", |
| 486 | binarye->binary.file); |
| 487 | return NULL; |
| 488 | } |
| 489 | |
| 490 | fclose(bin); |
| 491 | |
Stefan Roese | a4279a5 | 2015-10-28 07:53:58 +0100 | [diff] [blame] | 492 | cur += ALIGN_SUP(s.st_size, 4); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | * For now, we don't support more than one binary |
| 496 | * header, and no other header types are |
| 497 | * supported. So, the binary header is necessarily the |
| 498 | * last one |
| 499 | */ |
Stefan Roese | a4279a5 | 2015-10-28 07:53:58 +0100 | [diff] [blame] | 500 | *((uint32_t *)cur) = 0x00000000; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 501 | |
| 502 | cur += sizeof(uint32_t); |
| 503 | } |
| 504 | |
| 505 | /* Calculate and set the header checksum */ |
| 506 | main_hdr->checksum = image_checksum8(main_hdr, headersz); |
| 507 | |
| 508 | *imagesz = headersz; |
| 509 | return image; |
| 510 | } |
| 511 | |
| 512 | static int image_create_config_parse_oneline(char *line, |
| 513 | struct image_cfg_element *el) |
| 514 | { |
| 515 | char *keyword, *saveptr; |
| 516 | char deliminiters[] = " \t"; |
| 517 | |
| 518 | keyword = strtok_r(line, deliminiters, &saveptr); |
| 519 | if (!strcmp(keyword, "VERSION")) { |
| 520 | char *value = strtok_r(NULL, deliminiters, &saveptr); |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 521 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 522 | el->type = IMAGE_CFG_VERSION; |
| 523 | el->version = atoi(value); |
| 524 | } else if (!strcmp(keyword, "BOOT_FROM")) { |
| 525 | char *value = strtok_r(NULL, deliminiters, &saveptr); |
Andreas Bießmann | 4c40e35 | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 526 | int ret = image_boot_mode_id(value); |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 527 | |
Andreas Bießmann | 4c40e35 | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 528 | if (ret < 0) { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 529 | fprintf(stderr, |
| 530 | "Invalid boot media '%s'\n", value); |
| 531 | return -1; |
| 532 | } |
Andreas Bießmann | 4c40e35 | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 533 | el->type = IMAGE_CFG_BOOT_FROM; |
| 534 | el->bootfrom = ret; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 535 | } else if (!strcmp(keyword, "NAND_BLKSZ")) { |
| 536 | char *value = strtok_r(NULL, deliminiters, &saveptr); |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 537 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 538 | el->type = IMAGE_CFG_NAND_BLKSZ; |
| 539 | el->nandblksz = strtoul(value, NULL, 16); |
| 540 | } else if (!strcmp(keyword, "NAND_BADBLK_LOCATION")) { |
| 541 | char *value = strtok_r(NULL, deliminiters, &saveptr); |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 542 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 543 | el->type = IMAGE_CFG_NAND_BADBLK_LOCATION; |
| 544 | el->nandbadblklocation = |
| 545 | strtoul(value, NULL, 16); |
| 546 | } else if (!strcmp(keyword, "NAND_ECC_MODE")) { |
| 547 | char *value = strtok_r(NULL, deliminiters, &saveptr); |
Andreas Bießmann | 4c40e35 | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 548 | int ret = image_nand_ecc_mode_id(value); |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 549 | |
Andreas Bießmann | 4c40e35 | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 550 | if (ret < 0) { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 551 | fprintf(stderr, |
| 552 | "Invalid NAND ECC mode '%s'\n", value); |
| 553 | return -1; |
| 554 | } |
Andreas Bießmann | 4c40e35 | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 555 | el->type = IMAGE_CFG_NAND_ECC_MODE; |
| 556 | el->nandeccmode = ret; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 557 | } else if (!strcmp(keyword, "NAND_PAGE_SIZE")) { |
| 558 | char *value = strtok_r(NULL, deliminiters, &saveptr); |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 559 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 560 | el->type = IMAGE_CFG_NAND_PAGESZ; |
| 561 | el->nandpagesz = strtoul(value, NULL, 16); |
| 562 | } else if (!strcmp(keyword, "BINARY")) { |
| 563 | char *value = strtok_r(NULL, deliminiters, &saveptr); |
| 564 | int argi = 0; |
| 565 | |
| 566 | el->type = IMAGE_CFG_BINARY; |
| 567 | el->binary.file = strdup(value); |
| 568 | while (1) { |
| 569 | value = strtok_r(NULL, deliminiters, &saveptr); |
| 570 | if (!value) |
| 571 | break; |
| 572 | el->binary.args[argi] = strtoul(value, NULL, 16); |
| 573 | argi++; |
| 574 | if (argi >= BINARY_MAX_ARGS) { |
| 575 | fprintf(stderr, |
| 576 | "Too many argument for binary\n"); |
| 577 | return -1; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 578 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 579 | } |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 580 | el->binary.nargs = argi; |
| 581 | } else if (!strcmp(keyword, "DATA")) { |
| 582 | char *value1 = strtok_r(NULL, deliminiters, &saveptr); |
| 583 | char *value2 = strtok_r(NULL, deliminiters, &saveptr); |
| 584 | |
| 585 | if (!value1 || !value2) { |
| 586 | fprintf(stderr, |
| 587 | "Invalid number of arguments for DATA\n"); |
| 588 | return -1; |
| 589 | } |
| 590 | |
| 591 | el->type = IMAGE_CFG_DATA; |
| 592 | el->regdata.raddr = strtoul(value1, NULL, 16); |
| 593 | el->regdata.rdata = strtoul(value2, NULL, 16); |
Chris Packham | 883bf45 | 2016-11-09 22:07:45 +1300 | [diff] [blame] | 594 | } else if (!strcmp(keyword, "BAUDRATE")) { |
| 595 | char *value = strtok_r(NULL, deliminiters, &saveptr); |
| 596 | el->type = IMAGE_CFG_BAUDRATE; |
| 597 | el->baudrate = strtoul(value, NULL, 10); |
Chris Packham | 1e0728a | 2016-11-09 22:21:45 +1300 | [diff] [blame] | 598 | } else if (!strcmp(keyword, "DEBUG")) { |
| 599 | char *value = strtok_r(NULL, deliminiters, &saveptr); |
| 600 | el->type = IMAGE_CFG_DEBUG; |
| 601 | el->debug = strtoul(value, NULL, 10); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 602 | } else { |
| 603 | fprintf(stderr, "Ignoring unknown line '%s'\n", line); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 604 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 605 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 606 | return 0; |
| 607 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 608 | |
| 609 | /* |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 610 | * Parse the configuration file 'fcfg' into the array of configuration |
| 611 | * elements 'image_cfg', and return the number of configuration |
| 612 | * elements in 'cfgn'. |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 613 | */ |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 614 | static int image_create_config_parse(FILE *fcfg) |
| 615 | { |
| 616 | int ret; |
| 617 | int cfgi = 0; |
| 618 | |
| 619 | /* Parse the configuration file */ |
| 620 | while (!feof(fcfg)) { |
| 621 | char *line; |
| 622 | char buf[256]; |
| 623 | |
| 624 | /* Read the current line */ |
| 625 | memset(buf, 0, sizeof(buf)); |
| 626 | line = fgets(buf, sizeof(buf), fcfg); |
| 627 | if (!line) |
| 628 | break; |
| 629 | |
| 630 | /* Ignore useless lines */ |
| 631 | if (line[0] == '\n' || line[0] == '#') |
| 632 | continue; |
| 633 | |
| 634 | /* Strip final newline */ |
| 635 | if (line[strlen(line) - 1] == '\n') |
| 636 | line[strlen(line) - 1] = 0; |
| 637 | |
| 638 | /* Parse the current line */ |
| 639 | ret = image_create_config_parse_oneline(line, |
| 640 | &image_cfg[cfgi]); |
| 641 | if (ret) |
| 642 | return ret; |
| 643 | |
| 644 | cfgi++; |
| 645 | |
| 646 | if (cfgi >= IMAGE_CFG_ELEMENT_MAX) { |
| 647 | fprintf(stderr, |
| 648 | "Too many configuration elements in .cfg file\n"); |
| 649 | return -1; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | cfgn = cfgi; |
| 654 | return 0; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 655 | } |
| 656 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 657 | static int image_get_version(void) |
| 658 | { |
| 659 | struct image_cfg_element *e; |
| 660 | |
| 661 | e = image_find_option(IMAGE_CFG_VERSION); |
| 662 | if (!e) |
| 663 | return -1; |
| 664 | |
| 665 | return e->version; |
| 666 | } |
| 667 | |
| 668 | static int image_version_file(const char *input) |
| 669 | { |
| 670 | FILE *fcfg; |
| 671 | int version; |
| 672 | int ret; |
| 673 | |
| 674 | fcfg = fopen(input, "r"); |
| 675 | if (!fcfg) { |
| 676 | fprintf(stderr, "Could not open input file %s\n", input); |
| 677 | return -1; |
| 678 | } |
| 679 | |
| 680 | image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX * |
| 681 | sizeof(struct image_cfg_element)); |
| 682 | if (!image_cfg) { |
| 683 | fprintf(stderr, "Cannot allocate memory\n"); |
| 684 | fclose(fcfg); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | memset(image_cfg, 0, |
| 689 | IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element)); |
| 690 | rewind(fcfg); |
| 691 | |
| 692 | ret = image_create_config_parse(fcfg); |
| 693 | fclose(fcfg); |
| 694 | if (ret) { |
| 695 | free(image_cfg); |
| 696 | return -1; |
| 697 | } |
| 698 | |
| 699 | version = image_get_version(); |
| 700 | /* Fallback to version 0 is no version is provided in the cfg file */ |
| 701 | if (version == -1) |
| 702 | version = 0; |
| 703 | |
| 704 | free(image_cfg); |
| 705 | |
| 706 | return version; |
| 707 | } |
| 708 | |
| 709 | static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd, |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 710 | struct image_tool_params *params) |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 711 | { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 712 | FILE *fcfg; |
| 713 | void *image = NULL; |
| 714 | int version; |
Łukasz Majewski | f04dab4 | 2014-11-21 09:22:43 +0100 | [diff] [blame] | 715 | size_t headersz = 0; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 716 | uint32_t checksum; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 717 | int ret; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 718 | int size; |
| 719 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 720 | fcfg = fopen(params->imagename, "r"); |
| 721 | if (!fcfg) { |
| 722 | fprintf(stderr, "Could not open input file %s\n", |
| 723 | params->imagename); |
| 724 | exit(EXIT_FAILURE); |
| 725 | } |
| 726 | |
| 727 | image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX * |
| 728 | sizeof(struct image_cfg_element)); |
| 729 | if (!image_cfg) { |
| 730 | fprintf(stderr, "Cannot allocate memory\n"); |
| 731 | fclose(fcfg); |
| 732 | exit(EXIT_FAILURE); |
| 733 | } |
| 734 | |
| 735 | memset(image_cfg, 0, |
| 736 | IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element)); |
| 737 | rewind(fcfg); |
| 738 | |
| 739 | ret = image_create_config_parse(fcfg); |
| 740 | fclose(fcfg); |
| 741 | if (ret) { |
| 742 | free(image_cfg); |
| 743 | exit(EXIT_FAILURE); |
| 744 | } |
| 745 | |
Stefan Roese | 48676a3 | 2015-09-01 13:46:35 +0200 | [diff] [blame] | 746 | /* The MVEBU BootROM does not allow non word aligned payloads */ |
| 747 | sbuf->st_size = ALIGN_SUP(sbuf->st_size, 4); |
| 748 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 749 | version = image_get_version(); |
Stefan Roese | 933918c | 2014-10-28 11:32:24 +0100 | [diff] [blame] | 750 | switch (version) { |
| 751 | /* |
| 752 | * Fallback to version 0 if no version is provided in the |
| 753 | * cfg file |
| 754 | */ |
| 755 | case -1: |
| 756 | case 0: |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 757 | image = image_create_v0(&headersz, params, sbuf->st_size); |
Stefan Roese | 933918c | 2014-10-28 11:32:24 +0100 | [diff] [blame] | 758 | break; |
| 759 | |
| 760 | case 1: |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 761 | image = image_create_v1(&headersz, params, sbuf->st_size); |
Stefan Roese | 933918c | 2014-10-28 11:32:24 +0100 | [diff] [blame] | 762 | break; |
| 763 | |
| 764 | default: |
| 765 | fprintf(stderr, "Unsupported version %d\n", version); |
| 766 | free(image_cfg); |
| 767 | exit(EXIT_FAILURE); |
| 768 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 769 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 770 | if (!image) { |
| 771 | fprintf(stderr, "Could not create image\n"); |
| 772 | free(image_cfg); |
| 773 | exit(EXIT_FAILURE); |
| 774 | } |
| 775 | |
| 776 | free(image_cfg); |
| 777 | |
| 778 | /* Build and add image checksum header */ |
Reinhard Pfau | 3efeaae | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 779 | checksum = |
| 780 | cpu_to_le32(image_checksum32((uint32_t *)ptr, sbuf->st_size)); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 781 | size = write(ifd, &checksum, sizeof(uint32_t)); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 782 | if (size != sizeof(uint32_t)) { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 783 | fprintf(stderr, "Error:%s - Checksum write %d bytes %s\n", |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 784 | params->cmdname, size, params->imagefile); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 785 | exit(EXIT_FAILURE); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | sbuf->st_size += sizeof(uint32_t); |
| 789 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 790 | /* Finally copy the header into the image area */ |
| 791 | memcpy(ptr, image, headersz); |
| 792 | |
| 793 | free(image); |
| 794 | } |
| 795 | |
| 796 | static void kwbimage_print_header(const void *ptr) |
| 797 | { |
| 798 | struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr; |
| 799 | |
| 800 | printf("Image Type: MVEBU Boot from %s Image\n", |
| 801 | image_boot_mode_name(mhdr->blockid)); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 802 | printf("Image version:%d\n", image_version((void *)ptr)); |
Gerald Kerma | 8f9e583 | 2014-10-31 01:03:27 +0100 | [diff] [blame] | 803 | printf("Data Size: "); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 804 | genimg_print_size(mhdr->blocksize - sizeof(uint32_t)); |
| 805 | printf("Load Address: %08x\n", mhdr->destaddr); |
| 806 | printf("Entry Point: %08x\n", mhdr->execaddr); |
| 807 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 808 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 809 | static int kwbimage_check_image_types(uint8_t type) |
| 810 | { |
| 811 | if (type == IH_TYPE_KWBIMAGE) |
| 812 | return EXIT_SUCCESS; |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 813 | |
| 814 | return EXIT_FAILURE; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 815 | } |
| 816 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 817 | static int kwbimage_verify_header(unsigned char *ptr, int image_size, |
| 818 | struct image_tool_params *params) |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 819 | { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 820 | struct main_hdr_v0 *main_hdr; |
| 821 | struct ext_hdr_v0 *ext_hdr; |
| 822 | uint8_t checksum; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 823 | |
Mario Six | 7497cd6 | 2017-01-11 16:00:55 +0100 | [diff] [blame^] | 824 | main_hdr = (struct main_hdr_v0 *)ptr; |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 825 | checksum = image_checksum8(ptr, |
Gerald Kerma | 8f9e583 | 2014-10-31 01:03:27 +0100 | [diff] [blame] | 826 | sizeof(struct main_hdr_v0) |
| 827 | - sizeof(uint8_t)); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 828 | if (checksum != main_hdr->checksum) |
| 829 | return -FDT_ERR_BADSTRUCTURE; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 830 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 831 | /* Only version 0 extended header has checksum */ |
| 832 | if (image_version((void *)ptr) == 0) { |
Mario Six | 7497cd6 | 2017-01-11 16:00:55 +0100 | [diff] [blame^] | 833 | ext_hdr = (struct ext_hdr_v0 *) |
| 834 | (ptr + sizeof(struct main_hdr_v0)); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 835 | checksum = image_checksum8(ext_hdr, |
Gerald Kerma | 8f9e583 | 2014-10-31 01:03:27 +0100 | [diff] [blame] | 836 | sizeof(struct ext_hdr_v0) |
| 837 | - sizeof(uint8_t)); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 838 | if (checksum != ext_hdr->checksum) |
| 839 | return -FDT_ERR_BADSTRUCTURE; |
| 840 | } |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 845 | static int kwbimage_generate(struct image_tool_params *params, |
| 846 | struct image_type_params *tparams) |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 847 | { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 848 | int alloc_len; |
| 849 | void *hdr; |
| 850 | int version = 0; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 851 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 852 | version = image_version_file(params->imagename); |
| 853 | if (version == 0) { |
| 854 | alloc_len = sizeof(struct main_hdr_v0) + |
| 855 | sizeof(struct ext_hdr_v0); |
| 856 | } else { |
| 857 | alloc_len = image_headersz_v1(params, NULL); |
| 858 | } |
| 859 | |
| 860 | hdr = malloc(alloc_len); |
| 861 | if (!hdr) { |
| 862 | fprintf(stderr, "%s: malloc return failure: %s\n", |
| 863 | params->cmdname, strerror(errno)); |
| 864 | exit(EXIT_FAILURE); |
| 865 | } |
| 866 | |
| 867 | memset(hdr, 0, alloc_len); |
| 868 | tparams->header_size = alloc_len; |
| 869 | tparams->hdr = hdr; |
| 870 | |
Stefan Roese | da43fd3 | 2015-11-24 09:14:59 +0100 | [diff] [blame] | 871 | /* |
| 872 | * The resulting image needs to be 4-byte aligned. At least |
| 873 | * the Marvell hdrparser tool complains if its unaligned. |
| 874 | * By returning 1 here in this function, called via |
| 875 | * tparams->vrec_header() in mkimage.c, mkimage will |
| 876 | * automatically pad the the resulting image to a 4-byte |
| 877 | * size if necessary. |
| 878 | */ |
| 879 | return 1; |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 880 | } |
| 881 | |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 882 | /* |
| 883 | * Report Error if xflag is set in addition to default |
| 884 | */ |
| 885 | static int kwbimage_check_params(struct image_tool_params *params) |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 886 | { |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 887 | if (!strlen(params->imagename)) { |
Mario Six | d6009d7 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 888 | char *msg = "Configuration file for kwbimage creation omitted"; |
| 889 | |
| 890 | fprintf(stderr, "Error:%s - %s\n", params->cmdname, msg); |
Stefan Roese | 3b8b19d | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 891 | return CFG_INVALID; |
| 892 | } |
| 893 | |
| 894 | return (params->dflag && (params->fflag || params->lflag)) || |
| 895 | (params->fflag && (params->dflag || params->lflag)) || |
| 896 | (params->lflag && (params->dflag || params->fflag)) || |
| 897 | (params->xflag) || !(strlen(params->imagename)); |
Prafulla Wadaskar | 0732941 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* |
| 901 | * kwbimage type parameters definition |
| 902 | */ |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 903 | U_BOOT_IMAGE_TYPE( |
| 904 | kwbimage, |
| 905 | "Marvell MVEBU Boot Image support", |
| 906 | 0, |
| 907 | NULL, |
| 908 | kwbimage_check_params, |
| 909 | kwbimage_verify_header, |
| 910 | kwbimage_print_header, |
| 911 | kwbimage_set_header, |
| 912 | NULL, |
| 913 | kwbimage_check_image_types, |
| 914 | NULL, |
| 915 | kwbimage_generate |
| 916 | ); |