Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 2 | /* |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 3 | * Image manipulator for Marvell SoCs |
Pali Rohár | 8010f4f | 2021-09-24 23:07:02 +0200 | [diff] [blame] | 4 | * supports Kirkwood, Dove, Armada 370, Armada XP, Armada 375, Armada 38x and |
| 5 | * Armada 39x |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 6 | * |
| 7 | * (C) Copyright 2013 Thomas Petazzoni |
| 8 | * <thomas.petazzoni@free-electrons.com> |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 9 | */ |
| 10 | |
Heinrich Schuchardt | 3a8b919 | 2021-12-18 11:25:12 +0100 | [diff] [blame] | 11 | #define OPENSSL_API_COMPAT 0x10101000L |
| 12 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 13 | #include "imagetool.h" |
Andreas Bießmann | e5f1a58 | 2014-10-24 23:39:11 +0200 | [diff] [blame] | 14 | #include <limits.h> |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 15 | #include <image.h> |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 16 | #include <stdarg.h> |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 17 | #include <stdint.h> |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 18 | #include "kwbimage.h" |
| 19 | |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 20 | #include <openssl/bn.h> |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 21 | #include <openssl/rsa.h> |
| 22 | #include <openssl/pem.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/evp.h> |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 25 | |
Jonathan Gray | a2d5efd | 2018-02-21 02:59:01 +1100 | [diff] [blame] | 26 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 27 | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL) |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 28 | static void RSA_get0_key(const RSA *r, |
| 29 | const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) |
| 30 | { |
| 31 | if (n != NULL) |
| 32 | *n = r->n; |
| 33 | if (e != NULL) |
| 34 | *e = r->e; |
| 35 | if (d != NULL) |
| 36 | *d = r->d; |
| 37 | } |
| 38 | |
Jonathan Gray | a2d5efd | 2018-02-21 02:59:01 +1100 | [diff] [blame] | 39 | #elif !defined(LIBRESSL_VERSION_NUMBER) |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 40 | void EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) |
| 41 | { |
| 42 | EVP_MD_CTX_reset(ctx); |
| 43 | } |
| 44 | #endif |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 45 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 46 | static struct image_cfg_element *image_cfg; |
| 47 | static int cfgn; |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 48 | static int verbose_mode; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 49 | |
| 50 | struct boot_mode { |
| 51 | unsigned int id; |
| 52 | const char *name; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 53 | }; |
| 54 | |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 55 | /* |
| 56 | * SHA2-256 hash |
| 57 | */ |
| 58 | struct hash_v1 { |
| 59 | uint8_t hash[32]; |
| 60 | }; |
| 61 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 62 | struct boot_mode boot_modes[] = { |
Pali Rohár | e515a33 | 2021-08-11 10:14:17 +0200 | [diff] [blame] | 63 | { IBR_HDR_I2C_ID, "i2c" }, |
| 64 | { IBR_HDR_SPI_ID, "spi" }, |
| 65 | { IBR_HDR_NAND_ID, "nand" }, |
| 66 | { IBR_HDR_SATA_ID, "sata" }, |
| 67 | { IBR_HDR_PEX_ID, "pex" }, |
| 68 | { IBR_HDR_UART_ID, "uart" }, |
| 69 | { IBR_HDR_SDIO_ID, "sdio" }, |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 70 | {}, |
| 71 | }; |
| 72 | |
| 73 | struct nand_ecc_mode { |
| 74 | unsigned int id; |
| 75 | const char *name; |
| 76 | }; |
| 77 | |
| 78 | struct nand_ecc_mode nand_ecc_modes[] = { |
Pali Rohár | e515a33 | 2021-08-11 10:14:17 +0200 | [diff] [blame] | 79 | { IBR_HDR_ECC_DEFAULT, "default" }, |
| 80 | { IBR_HDR_ECC_FORCED_HAMMING, "hamming" }, |
| 81 | { IBR_HDR_ECC_FORCED_RS, "rs" }, |
| 82 | { IBR_HDR_ECC_DISABLED, "disabled" }, |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 83 | {}, |
| 84 | }; |
| 85 | |
| 86 | /* Used to identify an undefined execution or destination address */ |
| 87 | #define ADDR_INVALID ((uint32_t)-1) |
| 88 | |
Pali Rohár | 6c7f152 | 2021-07-23 11:14:07 +0200 | [diff] [blame] | 89 | #define BINARY_MAX_ARGS 255 |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 90 | |
| 91 | /* In-memory representation of a line of the configuration file */ |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 92 | |
| 93 | enum image_cfg_type { |
| 94 | IMAGE_CFG_VERSION = 0x1, |
| 95 | IMAGE_CFG_BOOT_FROM, |
| 96 | IMAGE_CFG_DEST_ADDR, |
| 97 | IMAGE_CFG_EXEC_ADDR, |
| 98 | IMAGE_CFG_NAND_BLKSZ, |
| 99 | IMAGE_CFG_NAND_BADBLK_LOCATION, |
| 100 | IMAGE_CFG_NAND_ECC_MODE, |
| 101 | IMAGE_CFG_NAND_PAGESZ, |
Pali Rohár | af49605 | 2022-01-12 18:20:40 +0100 | [diff] [blame] | 102 | IMAGE_CFG_CPU, |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 103 | IMAGE_CFG_BINARY, |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 104 | IMAGE_CFG_DATA, |
Pali Rohár | f63c583 | 2021-07-23 11:14:12 +0200 | [diff] [blame] | 105 | IMAGE_CFG_DATA_DELAY, |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 106 | IMAGE_CFG_BAUDRATE, |
Pali Rohár | 12f2c03 | 2021-11-08 18:12:41 +0100 | [diff] [blame] | 107 | IMAGE_CFG_UART_PORT, |
| 108 | IMAGE_CFG_UART_MPP, |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 109 | IMAGE_CFG_DEBUG, |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 110 | IMAGE_CFG_KAK, |
| 111 | IMAGE_CFG_CSK, |
| 112 | IMAGE_CFG_CSK_INDEX, |
| 113 | IMAGE_CFG_JTAG_DELAY, |
| 114 | IMAGE_CFG_BOX_ID, |
| 115 | IMAGE_CFG_FLASH_ID, |
| 116 | IMAGE_CFG_SEC_COMMON_IMG, |
| 117 | IMAGE_CFG_SEC_SPECIALIZED_IMG, |
| 118 | IMAGE_CFG_SEC_BOOT_DEV, |
| 119 | IMAGE_CFG_SEC_FUSE_DUMP, |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 120 | |
| 121 | IMAGE_CFG_COUNT |
| 122 | } type; |
| 123 | |
| 124 | static const char * const id_strs[] = { |
| 125 | [IMAGE_CFG_VERSION] = "VERSION", |
| 126 | [IMAGE_CFG_BOOT_FROM] = "BOOT_FROM", |
| 127 | [IMAGE_CFG_DEST_ADDR] = "DEST_ADDR", |
| 128 | [IMAGE_CFG_EXEC_ADDR] = "EXEC_ADDR", |
| 129 | [IMAGE_CFG_NAND_BLKSZ] = "NAND_BLKSZ", |
| 130 | [IMAGE_CFG_NAND_BADBLK_LOCATION] = "NAND_BADBLK_LOCATION", |
| 131 | [IMAGE_CFG_NAND_ECC_MODE] = "NAND_ECC_MODE", |
| 132 | [IMAGE_CFG_NAND_PAGESZ] = "NAND_PAGE_SIZE", |
Pali Rohár | af49605 | 2022-01-12 18:20:40 +0100 | [diff] [blame] | 133 | [IMAGE_CFG_CPU] = "CPU", |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 134 | [IMAGE_CFG_BINARY] = "BINARY", |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 135 | [IMAGE_CFG_DATA] = "DATA", |
Pali Rohár | f63c583 | 2021-07-23 11:14:12 +0200 | [diff] [blame] | 136 | [IMAGE_CFG_DATA_DELAY] = "DATA_DELAY", |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 137 | [IMAGE_CFG_BAUDRATE] = "BAUDRATE", |
Pali Rohár | 12f2c03 | 2021-11-08 18:12:41 +0100 | [diff] [blame] | 138 | [IMAGE_CFG_UART_PORT] = "UART_PORT", |
| 139 | [IMAGE_CFG_UART_MPP] = "UART_MPP", |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 140 | [IMAGE_CFG_DEBUG] = "DEBUG", |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 141 | [IMAGE_CFG_KAK] = "KAK", |
| 142 | [IMAGE_CFG_CSK] = "CSK", |
| 143 | [IMAGE_CFG_CSK_INDEX] = "CSK_INDEX", |
| 144 | [IMAGE_CFG_JTAG_DELAY] = "JTAG_DELAY", |
| 145 | [IMAGE_CFG_BOX_ID] = "BOX_ID", |
| 146 | [IMAGE_CFG_FLASH_ID] = "FLASH_ID", |
| 147 | [IMAGE_CFG_SEC_COMMON_IMG] = "SEC_COMMON_IMG", |
| 148 | [IMAGE_CFG_SEC_SPECIALIZED_IMG] = "SEC_SPECIALIZED_IMG", |
| 149 | [IMAGE_CFG_SEC_BOOT_DEV] = "SEC_BOOT_DEV", |
| 150 | [IMAGE_CFG_SEC_FUSE_DUMP] = "SEC_FUSE_DUMP" |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 151 | }; |
| 152 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 153 | struct image_cfg_element { |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 154 | enum image_cfg_type type; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 155 | union { |
| 156 | unsigned int version; |
Pali Rohár | af49605 | 2022-01-12 18:20:40 +0100 | [diff] [blame] | 157 | unsigned int cpu_sheeva; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 158 | unsigned int bootfrom; |
| 159 | struct { |
| 160 | const char *file; |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 161 | unsigned int loadaddr; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 162 | unsigned int args[BINARY_MAX_ARGS]; |
| 163 | unsigned int nargs; |
| 164 | } binary; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 165 | unsigned int dstaddr; |
| 166 | unsigned int execaddr; |
| 167 | unsigned int nandblksz; |
| 168 | unsigned int nandbadblklocation; |
| 169 | unsigned int nandeccmode; |
| 170 | unsigned int nandpagesz; |
| 171 | struct ext_hdr_v0_reg regdata; |
Pali Rohár | f63c583 | 2021-07-23 11:14:12 +0200 | [diff] [blame] | 172 | unsigned int regdata_delay; |
Chris Packham | 4bdb547 | 2016-11-09 22:07:45 +1300 | [diff] [blame] | 173 | unsigned int baudrate; |
Pali Rohár | 12f2c03 | 2021-11-08 18:12:41 +0100 | [diff] [blame] | 174 | unsigned int uart_port; |
| 175 | unsigned int uart_mpp; |
Chris Packham | 2611c05 | 2016-11-09 22:21:45 +1300 | [diff] [blame] | 176 | unsigned int debug; |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 177 | const char *key_name; |
| 178 | int csk_idx; |
| 179 | uint8_t jtag_delay; |
| 180 | uint32_t boxid; |
| 181 | uint32_t flashid; |
| 182 | bool sec_specialized_img; |
| 183 | unsigned int sec_boot_dev; |
| 184 | const char *name; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 185 | }; |
| 186 | }; |
| 187 | |
| 188 | #define IMAGE_CFG_ELEMENT_MAX 256 |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 189 | |
| 190 | /* |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 191 | * Utility functions to manipulate boot mode and ecc modes (convert |
| 192 | * them back and forth between description strings and the |
| 193 | * corresponding numerical identifiers). |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 194 | */ |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 195 | |
| 196 | static const char *image_boot_mode_name(unsigned int id) |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 197 | { |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 198 | int i; |
Mario Six | 94490a4 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 199 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 200 | for (i = 0; boot_modes[i].name; i++) |
| 201 | if (boot_modes[i].id == id) |
| 202 | return boot_modes[i].name; |
| 203 | return NULL; |
| 204 | } |
| 205 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 206 | static int image_boot_mode_id(const char *boot_mode_name) |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 207 | { |
| 208 | int i; |
Mario Six | 94490a4 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 209 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 210 | for (i = 0; boot_modes[i].name; i++) |
| 211 | if (!strcmp(boot_modes[i].name, boot_mode_name)) |
| 212 | return boot_modes[i].id; |
| 213 | |
| 214 | return -1; |
| 215 | } |
| 216 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 217 | static int image_nand_ecc_mode_id(const char *nand_ecc_mode_name) |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 218 | { |
| 219 | int i; |
Mario Six | 94490a4 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 220 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 221 | for (i = 0; nand_ecc_modes[i].name; i++) |
| 222 | if (!strcmp(nand_ecc_modes[i].name, nand_ecc_mode_name)) |
| 223 | return nand_ecc_modes[i].id; |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | static struct image_cfg_element * |
| 228 | image_find_option(unsigned int optiontype) |
| 229 | { |
| 230 | int i; |
| 231 | |
| 232 | for (i = 0; i < cfgn; i++) { |
| 233 | if (image_cfg[i].type == optiontype) |
| 234 | return &image_cfg[i]; |
| 235 | } |
| 236 | |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | static unsigned int |
| 241 | image_count_options(unsigned int optiontype) |
| 242 | { |
| 243 | int i; |
| 244 | unsigned int count = 0; |
| 245 | |
| 246 | for (i = 0; i < cfgn; i++) |
| 247 | if (image_cfg[i].type == optiontype) |
| 248 | count++; |
| 249 | |
| 250 | return count; |
| 251 | } |
| 252 | |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 253 | static int image_get_csk_index(void) |
| 254 | { |
| 255 | struct image_cfg_element *e; |
| 256 | |
| 257 | e = image_find_option(IMAGE_CFG_CSK_INDEX); |
| 258 | if (!e) |
| 259 | return -1; |
| 260 | |
| 261 | return e->csk_idx; |
| 262 | } |
| 263 | |
| 264 | static bool image_get_spezialized_img(void) |
| 265 | { |
| 266 | struct image_cfg_element *e; |
| 267 | |
| 268 | e = image_find_option(IMAGE_CFG_SEC_SPECIALIZED_IMG); |
| 269 | if (!e) |
| 270 | return false; |
| 271 | |
| 272 | return e->sec_specialized_img; |
| 273 | } |
| 274 | |
Pali Rohár | d1547b3 | 2021-11-08 18:12:43 +0100 | [diff] [blame] | 275 | static int image_get_bootfrom(void) |
| 276 | { |
| 277 | struct image_cfg_element *e; |
| 278 | |
| 279 | e = image_find_option(IMAGE_CFG_BOOT_FROM); |
| 280 | if (!e) |
| 281 | /* fallback to SPI if no BOOT_FROM is not provided */ |
| 282 | return IBR_HDR_SPI_ID; |
| 283 | |
| 284 | return e->bootfrom; |
| 285 | } |
| 286 | |
Pali Rohár | af49605 | 2022-01-12 18:20:40 +0100 | [diff] [blame] | 287 | static int image_is_cpu_sheeva(void) |
| 288 | { |
| 289 | struct image_cfg_element *e; |
| 290 | |
| 291 | e = image_find_option(IMAGE_CFG_CPU); |
| 292 | if (!e) |
| 293 | return 0; |
| 294 | |
| 295 | return e->cpu_sheeva; |
| 296 | } |
| 297 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 298 | /* |
| 299 | * Compute a 8-bit checksum of a memory area. This algorithm follows |
| 300 | * the requirements of the Marvell SoC BootROM specifications. |
| 301 | */ |
| 302 | static uint8_t image_checksum8(void *start, uint32_t len) |
| 303 | { |
| 304 | uint8_t csum = 0; |
| 305 | uint8_t *p = start; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 306 | |
| 307 | /* check len and return zero checksum if invalid */ |
| 308 | if (!len) |
| 309 | return 0; |
| 310 | |
| 311 | do { |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 312 | csum += *p; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 313 | p++; |
| 314 | } while (--len); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 315 | |
| 316 | return csum; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 317 | } |
| 318 | |
Baruch Siach | db7cd4ed | 2017-07-04 20:23:40 +0300 | [diff] [blame] | 319 | /* |
| 320 | * Verify checksum over a complete header that includes the checksum field. |
| 321 | * Return 1 when OK, otherwise 0. |
| 322 | */ |
| 323 | static int main_hdr_checksum_ok(void *hdr) |
| 324 | { |
| 325 | /* Offsets of checksum in v0 and v1 headers are the same */ |
| 326 | struct main_hdr_v0 *main_hdr = (struct main_hdr_v0 *)hdr; |
| 327 | uint8_t checksum; |
| 328 | |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 329 | checksum = image_checksum8(hdr, kwbheader_size_for_csum(hdr)); |
Baruch Siach | db7cd4ed | 2017-07-04 20:23:40 +0300 | [diff] [blame] | 330 | /* Calculated checksum includes the header checksum field. Compensate |
| 331 | * for that. |
| 332 | */ |
| 333 | checksum -= main_hdr->checksum; |
| 334 | |
| 335 | return checksum == main_hdr->checksum; |
| 336 | } |
| 337 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 338 | static uint32_t image_checksum32(void *start, uint32_t len) |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 339 | { |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 340 | uint32_t csum = 0; |
| 341 | uint32_t *p = start; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 342 | |
| 343 | /* check len and return zero checksum if invalid */ |
| 344 | if (!len) |
| 345 | return 0; |
| 346 | |
| 347 | if (len % sizeof(uint32_t)) { |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 348 | fprintf(stderr, "Length %d is not in multiple of %zu\n", |
| 349 | len, sizeof(uint32_t)); |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | do { |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 354 | csum += *p; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 355 | p++; |
| 356 | len -= sizeof(uint32_t); |
| 357 | } while (len > 0); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 358 | |
| 359 | return csum; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 360 | } |
| 361 | |
Chris Packham | 4bdb547 | 2016-11-09 22:07:45 +1300 | [diff] [blame] | 362 | static uint8_t baudrate_to_option(unsigned int baudrate) |
| 363 | { |
| 364 | switch (baudrate) { |
| 365 | case 2400: |
| 366 | return MAIN_HDR_V1_OPT_BAUD_2400; |
| 367 | case 4800: |
| 368 | return MAIN_HDR_V1_OPT_BAUD_4800; |
| 369 | case 9600: |
| 370 | return MAIN_HDR_V1_OPT_BAUD_9600; |
| 371 | case 19200: |
| 372 | return MAIN_HDR_V1_OPT_BAUD_19200; |
| 373 | case 38400: |
| 374 | return MAIN_HDR_V1_OPT_BAUD_38400; |
| 375 | case 57600: |
| 376 | return MAIN_HDR_V1_OPT_BAUD_57600; |
| 377 | case 115200: |
| 378 | return MAIN_HDR_V1_OPT_BAUD_115200; |
| 379 | default: |
| 380 | return MAIN_HDR_V1_OPT_BAUD_DEFAULT; |
| 381 | } |
| 382 | } |
| 383 | |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 384 | static void kwb_msg(const char *fmt, ...) |
| 385 | { |
| 386 | if (verbose_mode) { |
| 387 | va_list ap; |
| 388 | |
| 389 | va_start(ap, fmt); |
| 390 | vfprintf(stdout, fmt, ap); |
| 391 | va_end(ap); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | static int openssl_err(const char *msg) |
| 396 | { |
| 397 | unsigned long ssl_err = ERR_get_error(); |
| 398 | |
| 399 | fprintf(stderr, "%s", msg); |
| 400 | fprintf(stderr, ": %s\n", |
| 401 | ERR_error_string(ssl_err, 0)); |
| 402 | |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | static int kwb_load_rsa_key(const char *keydir, const char *name, RSA **p_rsa) |
| 407 | { |
| 408 | char path[PATH_MAX]; |
| 409 | RSA *rsa; |
| 410 | FILE *f; |
| 411 | |
| 412 | if (!keydir) |
| 413 | keydir = "."; |
| 414 | |
| 415 | snprintf(path, sizeof(path), "%s/%s.key", keydir, name); |
| 416 | f = fopen(path, "r"); |
| 417 | if (!f) { |
| 418 | fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n", |
| 419 | path, strerror(errno)); |
| 420 | return -ENOENT; |
| 421 | } |
| 422 | |
| 423 | rsa = PEM_read_RSAPrivateKey(f, 0, NULL, ""); |
| 424 | if (!rsa) { |
| 425 | openssl_err("Failure reading private key"); |
| 426 | fclose(f); |
| 427 | return -EPROTO; |
| 428 | } |
| 429 | fclose(f); |
| 430 | *p_rsa = rsa; |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int kwb_load_cfg_key(struct image_tool_params *params, |
| 436 | unsigned int cfg_option, const char *key_name, |
| 437 | RSA **p_key) |
| 438 | { |
| 439 | struct image_cfg_element *e_key; |
| 440 | RSA *key; |
| 441 | int res; |
| 442 | |
| 443 | *p_key = NULL; |
| 444 | |
| 445 | e_key = image_find_option(cfg_option); |
| 446 | if (!e_key) { |
| 447 | fprintf(stderr, "%s not configured\n", key_name); |
| 448 | return -ENOENT; |
| 449 | } |
| 450 | |
| 451 | res = kwb_load_rsa_key(params->keydir, e_key->key_name, &key); |
| 452 | if (res < 0) { |
| 453 | fprintf(stderr, "Failed to load %s\n", key_name); |
| 454 | return -ENOENT; |
| 455 | } |
| 456 | |
| 457 | *p_key = key; |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int kwb_load_kak(struct image_tool_params *params, RSA **p_kak) |
| 463 | { |
| 464 | return kwb_load_cfg_key(params, IMAGE_CFG_KAK, "KAK", p_kak); |
| 465 | } |
| 466 | |
| 467 | static int kwb_load_csk(struct image_tool_params *params, RSA **p_csk) |
| 468 | { |
| 469 | return kwb_load_cfg_key(params, IMAGE_CFG_CSK, "CSK", p_csk); |
| 470 | } |
| 471 | |
| 472 | static int kwb_compute_pubkey_hash(struct pubkey_der_v1 *pk, |
| 473 | struct hash_v1 *hash) |
| 474 | { |
| 475 | EVP_MD_CTX *ctx; |
| 476 | unsigned int key_size; |
| 477 | unsigned int hash_size; |
| 478 | int ret = 0; |
| 479 | |
| 480 | if (!pk || !hash || pk->key[0] != 0x30 || pk->key[1] != 0x82) |
| 481 | return -EINVAL; |
| 482 | |
| 483 | key_size = (pk->key[2] << 8) + pk->key[3] + 4; |
| 484 | |
| 485 | ctx = EVP_MD_CTX_create(); |
| 486 | if (!ctx) |
| 487 | return openssl_err("EVP context creation failed"); |
| 488 | |
| 489 | EVP_MD_CTX_init(ctx); |
| 490 | if (!EVP_DigestInit(ctx, EVP_sha256())) { |
| 491 | ret = openssl_err("Digest setup failed"); |
| 492 | goto hash_err_ctx; |
| 493 | } |
| 494 | |
| 495 | if (!EVP_DigestUpdate(ctx, pk->key, key_size)) { |
| 496 | ret = openssl_err("Hashing data failed"); |
| 497 | goto hash_err_ctx; |
| 498 | } |
| 499 | |
| 500 | if (!EVP_DigestFinal(ctx, hash->hash, &hash_size)) { |
| 501 | ret = openssl_err("Could not obtain hash"); |
| 502 | goto hash_err_ctx; |
| 503 | } |
| 504 | |
| 505 | EVP_MD_CTX_cleanup(ctx); |
| 506 | |
| 507 | hash_err_ctx: |
| 508 | EVP_MD_CTX_destroy(ctx); |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | static int kwb_import_pubkey(RSA **key, struct pubkey_der_v1 *src, char *keyname) |
| 513 | { |
| 514 | RSA *rsa; |
| 515 | const unsigned char *ptr; |
| 516 | |
| 517 | if (!key || !src) |
| 518 | goto fail; |
| 519 | |
| 520 | ptr = src->key; |
| 521 | rsa = d2i_RSAPublicKey(key, &ptr, sizeof(src->key)); |
| 522 | if (!rsa) { |
| 523 | openssl_err("error decoding public key"); |
| 524 | goto fail; |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | fail: |
| 529 | fprintf(stderr, "Failed to decode %s pubkey\n", keyname); |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | |
| 533 | static int kwb_export_pubkey(RSA *key, struct pubkey_der_v1 *dst, FILE *hashf, |
| 534 | char *keyname) |
| 535 | { |
| 536 | int size_exp, size_mod, size_seq; |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 537 | const BIGNUM *key_e, *key_n; |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 538 | uint8_t *cur; |
| 539 | char *errmsg = "Failed to encode %s\n"; |
| 540 | |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 541 | RSA_get0_key(key, NULL, &key_e, NULL); |
| 542 | RSA_get0_key(key, &key_n, NULL, NULL); |
| 543 | |
| 544 | if (!key || !key_e || !key_n || !dst) { |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 545 | fprintf(stderr, "export pk failed: (%p, %p, %p, %p)", |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 546 | key, key_e, key_n, dst); |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 547 | fprintf(stderr, errmsg, keyname); |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * According to the specs, the key should be PKCS#1 DER encoded. |
| 553 | * But unfortunately the really required encoding seems to be different; |
| 554 | * it violates DER...! (But it still conformes to BER.) |
| 555 | * (Length always in long form w/ 2 byte length code; no leading zero |
| 556 | * when MSB of first byte is set...) |
| 557 | * So we cannot use the encoding func provided by OpenSSL and have to |
| 558 | * do the encoding manually. |
| 559 | */ |
| 560 | |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 561 | size_exp = BN_num_bytes(key_e); |
| 562 | size_mod = BN_num_bytes(key_n); |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 563 | size_seq = 4 + size_mod + 4 + size_exp; |
| 564 | |
| 565 | if (size_mod > 256) { |
| 566 | fprintf(stderr, "export pk failed: wrong mod size: %d\n", |
| 567 | size_mod); |
| 568 | fprintf(stderr, errmsg, keyname); |
| 569 | return -EINVAL; |
| 570 | } |
| 571 | |
| 572 | if (4 + size_seq > sizeof(dst->key)) { |
Marek Behún | 3b5da64 | 2021-09-24 23:06:38 +0200 | [diff] [blame] | 573 | fprintf(stderr, "export pk failed: seq too large (%d, %zu)\n", |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 574 | 4 + size_seq, sizeof(dst->key)); |
| 575 | fprintf(stderr, errmsg, keyname); |
| 576 | return -ENOBUFS; |
| 577 | } |
| 578 | |
| 579 | cur = dst->key; |
| 580 | |
| 581 | /* PKCS#1 (RFC3447) RSAPublicKey structure */ |
| 582 | *cur++ = 0x30; /* SEQUENCE */ |
| 583 | *cur++ = 0x82; |
| 584 | *cur++ = (size_seq >> 8) & 0xFF; |
| 585 | *cur++ = size_seq & 0xFF; |
| 586 | /* Modulus */ |
| 587 | *cur++ = 0x02; /* INTEGER */ |
| 588 | *cur++ = 0x82; |
| 589 | *cur++ = (size_mod >> 8) & 0xFF; |
| 590 | *cur++ = size_mod & 0xFF; |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 591 | BN_bn2bin(key_n, cur); |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 592 | cur += size_mod; |
| 593 | /* Exponent */ |
| 594 | *cur++ = 0x02; /* INTEGER */ |
| 595 | *cur++ = 0x82; |
| 596 | *cur++ = (size_exp >> 8) & 0xFF; |
| 597 | *cur++ = size_exp & 0xFF; |
Jelle van der Waa | e15843b | 2017-05-08 21:31:20 +0200 | [diff] [blame] | 598 | BN_bn2bin(key_e, cur); |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 599 | |
| 600 | if (hashf) { |
| 601 | struct hash_v1 pk_hash; |
| 602 | int i; |
| 603 | int ret = 0; |
| 604 | |
| 605 | ret = kwb_compute_pubkey_hash(dst, &pk_hash); |
| 606 | if (ret < 0) { |
| 607 | fprintf(stderr, errmsg, keyname); |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | fprintf(hashf, "SHA256 = "); |
| 612 | for (i = 0 ; i < sizeof(pk_hash.hash); ++i) |
| 613 | fprintf(hashf, "%02X", pk_hash.hash[i]); |
| 614 | fprintf(hashf, "\n"); |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 620 | static int kwb_sign(RSA *key, void *data, int datasz, struct sig_v1 *sig, |
| 621 | char *signame) |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 622 | { |
| 623 | EVP_PKEY *evp_key; |
| 624 | EVP_MD_CTX *ctx; |
| 625 | unsigned int sig_size; |
| 626 | int size; |
| 627 | int ret = 0; |
| 628 | |
| 629 | evp_key = EVP_PKEY_new(); |
| 630 | if (!evp_key) |
| 631 | return openssl_err("EVP_PKEY object creation failed"); |
| 632 | |
| 633 | if (!EVP_PKEY_set1_RSA(evp_key, key)) { |
| 634 | ret = openssl_err("EVP key setup failed"); |
| 635 | goto err_key; |
| 636 | } |
| 637 | |
| 638 | size = EVP_PKEY_size(evp_key); |
| 639 | if (size > sizeof(sig->sig)) { |
| 640 | fprintf(stderr, "Buffer to small for signature (%d bytes)\n", |
| 641 | size); |
| 642 | ret = -ENOBUFS; |
| 643 | goto err_key; |
| 644 | } |
| 645 | |
| 646 | ctx = EVP_MD_CTX_create(); |
| 647 | if (!ctx) { |
| 648 | ret = openssl_err("EVP context creation failed"); |
| 649 | goto err_key; |
| 650 | } |
| 651 | EVP_MD_CTX_init(ctx); |
| 652 | if (!EVP_SignInit(ctx, EVP_sha256())) { |
| 653 | ret = openssl_err("Signer setup failed"); |
| 654 | goto err_ctx; |
| 655 | } |
| 656 | |
| 657 | if (!EVP_SignUpdate(ctx, data, datasz)) { |
| 658 | ret = openssl_err("Signing data failed"); |
| 659 | goto err_ctx; |
| 660 | } |
| 661 | |
| 662 | if (!EVP_SignFinal(ctx, sig->sig, &sig_size, evp_key)) { |
| 663 | ret = openssl_err("Could not obtain signature"); |
| 664 | goto err_ctx; |
| 665 | } |
| 666 | |
| 667 | EVP_MD_CTX_cleanup(ctx); |
| 668 | EVP_MD_CTX_destroy(ctx); |
| 669 | EVP_PKEY_free(evp_key); |
| 670 | |
| 671 | return 0; |
| 672 | |
| 673 | err_ctx: |
| 674 | EVP_MD_CTX_destroy(ctx); |
| 675 | err_key: |
| 676 | EVP_PKEY_free(evp_key); |
| 677 | fprintf(stderr, "Failed to create %s signature\n", signame); |
| 678 | return ret; |
| 679 | } |
| 680 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 681 | static int kwb_verify(RSA *key, void *data, int datasz, struct sig_v1 *sig, |
| 682 | char *signame) |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 683 | { |
| 684 | EVP_PKEY *evp_key; |
| 685 | EVP_MD_CTX *ctx; |
| 686 | int size; |
| 687 | int ret = 0; |
| 688 | |
| 689 | evp_key = EVP_PKEY_new(); |
| 690 | if (!evp_key) |
| 691 | return openssl_err("EVP_PKEY object creation failed"); |
| 692 | |
| 693 | if (!EVP_PKEY_set1_RSA(evp_key, key)) { |
| 694 | ret = openssl_err("EVP key setup failed"); |
| 695 | goto err_key; |
| 696 | } |
| 697 | |
| 698 | size = EVP_PKEY_size(evp_key); |
| 699 | if (size > sizeof(sig->sig)) { |
| 700 | fprintf(stderr, "Invalid signature size (%d bytes)\n", |
| 701 | size); |
| 702 | ret = -EINVAL; |
| 703 | goto err_key; |
| 704 | } |
| 705 | |
| 706 | ctx = EVP_MD_CTX_create(); |
| 707 | if (!ctx) { |
| 708 | ret = openssl_err("EVP context creation failed"); |
| 709 | goto err_key; |
| 710 | } |
| 711 | EVP_MD_CTX_init(ctx); |
| 712 | if (!EVP_VerifyInit(ctx, EVP_sha256())) { |
| 713 | ret = openssl_err("Verifier setup failed"); |
| 714 | goto err_ctx; |
| 715 | } |
| 716 | |
| 717 | if (!EVP_VerifyUpdate(ctx, data, datasz)) { |
| 718 | ret = openssl_err("Hashing data failed"); |
| 719 | goto err_ctx; |
| 720 | } |
| 721 | |
Young Xiao | 2251512 | 2019-04-17 17:20:24 +0800 | [diff] [blame] | 722 | if (EVP_VerifyFinal(ctx, sig->sig, sizeof(sig->sig), evp_key) != 1) { |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 723 | ret = openssl_err("Could not verify signature"); |
| 724 | goto err_ctx; |
| 725 | } |
| 726 | |
| 727 | EVP_MD_CTX_cleanup(ctx); |
| 728 | EVP_MD_CTX_destroy(ctx); |
| 729 | EVP_PKEY_free(evp_key); |
| 730 | |
| 731 | return 0; |
| 732 | |
| 733 | err_ctx: |
| 734 | EVP_MD_CTX_destroy(ctx); |
| 735 | err_key: |
| 736 | EVP_PKEY_free(evp_key); |
| 737 | fprintf(stderr, "Failed to verify %s signature\n", signame); |
| 738 | return ret; |
| 739 | } |
| 740 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 741 | static int kwb_sign_and_verify(RSA *key, void *data, int datasz, |
| 742 | struct sig_v1 *sig, char *signame) |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 743 | { |
| 744 | if (kwb_sign(key, data, datasz, sig, signame) < 0) |
| 745 | return -1; |
| 746 | |
| 747 | if (kwb_verify(key, data, datasz, sig, signame) < 0) |
| 748 | return -1; |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 754 | static int kwb_dump_fuse_cmds_38x(FILE *out, struct secure_hdr_v1 *sec_hdr) |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 755 | { |
| 756 | struct hash_v1 kak_pub_hash; |
| 757 | struct image_cfg_element *e; |
| 758 | unsigned int fuse_line; |
| 759 | int i, idx; |
| 760 | uint8_t *ptr; |
| 761 | uint32_t val; |
| 762 | int ret = 0; |
| 763 | |
| 764 | if (!out || !sec_hdr) |
| 765 | return -EINVAL; |
| 766 | |
| 767 | ret = kwb_compute_pubkey_hash(&sec_hdr->kak, &kak_pub_hash); |
| 768 | if (ret < 0) |
| 769 | goto done; |
| 770 | |
| 771 | fprintf(out, "# burn KAK pub key hash\n"); |
| 772 | ptr = kak_pub_hash.hash; |
| 773 | for (fuse_line = 26; fuse_line <= 30; ++fuse_line) { |
| 774 | fprintf(out, "fuse prog -y %u 0 ", fuse_line); |
| 775 | |
| 776 | for (i = 4; i-- > 0;) |
| 777 | fprintf(out, "%02hx", (ushort)ptr[i]); |
| 778 | ptr += 4; |
| 779 | fprintf(out, " 00"); |
| 780 | |
| 781 | if (fuse_line < 30) { |
| 782 | for (i = 3; i-- > 0;) |
| 783 | fprintf(out, "%02hx", (ushort)ptr[i]); |
| 784 | ptr += 3; |
| 785 | } else { |
| 786 | fprintf(out, "000000"); |
| 787 | } |
| 788 | |
| 789 | fprintf(out, " 1\n"); |
| 790 | } |
| 791 | |
| 792 | fprintf(out, "# burn CSK selection\n"); |
| 793 | |
| 794 | idx = image_get_csk_index(); |
| 795 | if (idx < 0 || idx > 15) { |
| 796 | ret = -EINVAL; |
| 797 | goto done; |
| 798 | } |
| 799 | if (idx > 0) { |
| 800 | for (fuse_line = 31; fuse_line < 31 + idx; ++fuse_line) |
| 801 | fprintf(out, "fuse prog -y %u 0 00000001 00000000 1\n", |
| 802 | fuse_line); |
| 803 | } else { |
| 804 | fprintf(out, "# CSK index is 0; no mods needed\n"); |
| 805 | } |
| 806 | |
| 807 | e = image_find_option(IMAGE_CFG_BOX_ID); |
| 808 | if (e) { |
| 809 | fprintf(out, "# set box ID\n"); |
| 810 | fprintf(out, "fuse prog -y 48 0 %08x 00000000 1\n", e->boxid); |
| 811 | } |
| 812 | |
| 813 | e = image_find_option(IMAGE_CFG_FLASH_ID); |
| 814 | if (e) { |
| 815 | fprintf(out, "# set flash ID\n"); |
| 816 | fprintf(out, "fuse prog -y 47 0 %08x 00000000 1\n", e->flashid); |
| 817 | } |
| 818 | |
| 819 | fprintf(out, "# enable secure mode "); |
| 820 | fprintf(out, "(must be the last fuse line written)\n"); |
| 821 | |
| 822 | val = 1; |
| 823 | e = image_find_option(IMAGE_CFG_SEC_BOOT_DEV); |
| 824 | if (!e) { |
| 825 | fprintf(stderr, "ERROR: secured mode boot device not given\n"); |
| 826 | ret = -EINVAL; |
| 827 | goto done; |
| 828 | } |
| 829 | |
| 830 | if (e->sec_boot_dev > 0xff) { |
| 831 | fprintf(stderr, "ERROR: secured mode boot device invalid\n"); |
| 832 | ret = -EINVAL; |
| 833 | goto done; |
| 834 | } |
| 835 | |
| 836 | val |= (e->sec_boot_dev << 8); |
| 837 | |
| 838 | fprintf(out, "fuse prog -y 24 0 %08x 0103e0a9 1\n", val); |
| 839 | |
| 840 | fprintf(out, "# lock (unused) fuse lines (0-23)s\n"); |
| 841 | for (fuse_line = 0; fuse_line < 24; ++fuse_line) |
| 842 | fprintf(out, "fuse prog -y %u 2 1\n", fuse_line); |
| 843 | |
| 844 | fprintf(out, "# OK, that's all :-)\n"); |
| 845 | |
| 846 | done: |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | static int kwb_dump_fuse_cmds(struct secure_hdr_v1 *sec_hdr) |
| 851 | { |
| 852 | int ret = 0; |
| 853 | struct image_cfg_element *e; |
| 854 | |
| 855 | e = image_find_option(IMAGE_CFG_SEC_FUSE_DUMP); |
| 856 | if (!e) |
| 857 | return 0; |
| 858 | |
| 859 | if (!strcmp(e->name, "a38x")) { |
| 860 | FILE *out = fopen("kwb_fuses_a38x.txt", "w+"); |
| 861 | |
Heinrich Schuchardt | f858bb2 | 2021-08-17 07:03:20 +0200 | [diff] [blame] | 862 | if (!out) { |
| 863 | fprintf(stderr, "Couldn't open eFuse settings: '%s': %s\n", |
| 864 | "kwb_fuses_a38x.txt", strerror(errno)); |
| 865 | return -ENOENT; |
| 866 | } |
| 867 | |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 868 | kwb_dump_fuse_cmds_38x(out, sec_hdr); |
| 869 | fclose(out); |
| 870 | goto done; |
| 871 | } |
| 872 | |
| 873 | ret = -ENOSYS; |
| 874 | |
| 875 | done: |
| 876 | return ret; |
| 877 | } |
| 878 | |
Pali Rohár | 5cad2e6 | 2021-11-08 18:12:48 +0100 | [diff] [blame] | 879 | static size_t image_headersz_align(size_t headersz, uint8_t blockid) |
| 880 | { |
| 881 | /* |
| 882 | * Header needs to be 4-byte aligned, which is already ensured by code |
| 883 | * above. Moreover UART images must have header aligned to 128 bytes |
| 884 | * (xmodem block size), NAND images to 256 bytes (ECC calculation), |
| 885 | * and SATA and SDIO images to 512 bytes (storage block size). |
| 886 | * Note that SPI images do not have to have header size aligned |
| 887 | * to 256 bytes because it is possible to read from SPI storage from |
| 888 | * any offset (read offset does not have to be aligned to block size). |
| 889 | */ |
| 890 | if (blockid == IBR_HDR_UART_ID) |
| 891 | return ALIGN(headersz, 128); |
| 892 | else if (blockid == IBR_HDR_NAND_ID) |
| 893 | return ALIGN(headersz, 256); |
| 894 | else if (blockid == IBR_HDR_SATA_ID || blockid == IBR_HDR_SDIO_ID) |
| 895 | return ALIGN(headersz, 512); |
| 896 | else |
| 897 | return headersz; |
| 898 | } |
| 899 | |
Pali Rohár | 851114b | 2021-11-08 18:12:50 +0100 | [diff] [blame] | 900 | static size_t image_headersz_v0(int *hasext) |
| 901 | { |
| 902 | size_t headersz; |
| 903 | |
| 904 | headersz = sizeof(struct main_hdr_v0); |
| 905 | if (image_count_options(IMAGE_CFG_DATA) > 0) { |
| 906 | headersz += sizeof(struct ext_hdr_v0); |
| 907 | if (hasext) |
| 908 | *hasext = 1; |
| 909 | } |
| 910 | |
| 911 | return image_headersz_align(headersz, image_get_bootfrom()); |
| 912 | } |
| 913 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 914 | static void *image_create_v0(size_t *imagesz, struct image_tool_params *params, |
| 915 | int payloadsz) |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 916 | { |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 917 | struct image_cfg_element *e; |
| 918 | size_t headersz; |
| 919 | struct main_hdr_v0 *main_hdr; |
Mario Six | 885fba1 | 2017-01-11 16:00:55 +0100 | [diff] [blame] | 920 | uint8_t *image; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 921 | int has_ext = 0; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 922 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 923 | /* |
| 924 | * Calculate the size of the header and the size of the |
| 925 | * payload |
| 926 | */ |
Pali Rohár | 851114b | 2021-11-08 18:12:50 +0100 | [diff] [blame] | 927 | headersz = image_headersz_v0(&has_ext); |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 928 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 929 | image = malloc(headersz); |
| 930 | if (!image) { |
| 931 | fprintf(stderr, "Cannot allocate memory for image\n"); |
| 932 | return NULL; |
| 933 | } |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 934 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 935 | memset(image, 0, headersz); |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 936 | |
Mario Six | 885fba1 | 2017-01-11 16:00:55 +0100 | [diff] [blame] | 937 | main_hdr = (struct main_hdr_v0 *)image; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 938 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 939 | /* Fill in the main header */ |
Reinhard Pfau | a8840dc | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 940 | main_hdr->blocksize = |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 941 | cpu_to_le32(payloadsz); |
Reinhard Pfau | a8840dc | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 942 | main_hdr->srcaddr = cpu_to_le32(headersz); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 943 | main_hdr->ext = has_ext; |
Pali Rohár | 01bdac6 | 2021-11-08 18:12:42 +0100 | [diff] [blame] | 944 | main_hdr->version = 0; |
Reinhard Pfau | a8840dc | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 945 | main_hdr->destaddr = cpu_to_le32(params->addr); |
| 946 | main_hdr->execaddr = cpu_to_le32(params->ep); |
Pali Rohár | d1547b3 | 2021-11-08 18:12:43 +0100 | [diff] [blame] | 947 | main_hdr->blockid = image_get_bootfrom(); |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 948 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 949 | e = image_find_option(IMAGE_CFG_NAND_ECC_MODE); |
| 950 | if (e) |
| 951 | main_hdr->nandeccmode = e->nandeccmode; |
| 952 | e = image_find_option(IMAGE_CFG_NAND_PAGESZ); |
| 953 | if (e) |
Reinhard Pfau | a8840dc | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 954 | main_hdr->nandpagesize = cpu_to_le16(e->nandpagesz); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 955 | main_hdr->checksum = image_checksum8(image, |
| 956 | sizeof(struct main_hdr_v0)); |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 957 | |
Pali Rohár | 5c61710 | 2021-11-08 18:12:51 +0100 | [diff] [blame] | 958 | /* |
| 959 | * For SATA srcaddr is specified in number of sectors starting from |
| 960 | * sector 0. The main header is stored at sector number 1. |
| 961 | * This expects the sector size to be 512 bytes. |
| 962 | * Header size is already aligned. |
| 963 | */ |
| 964 | if (main_hdr->blockid == IBR_HDR_SATA_ID) |
| 965 | main_hdr->srcaddr = cpu_to_le32(headersz / 512 + 1); |
| 966 | |
| 967 | /* |
| 968 | * For SDIO srcaddr is specified in number of sectors starting from |
| 969 | * sector 0. The main header is stored at sector number 0. |
| 970 | * This expects sector size to be 512 bytes. |
| 971 | * Header size is already aligned. |
| 972 | */ |
| 973 | if (main_hdr->blockid == IBR_HDR_SDIO_ID) |
| 974 | main_hdr->srcaddr = cpu_to_le32(headersz / 512); |
| 975 | |
| 976 | /* For PCIe srcaddr is not used and must be set to 0xFFFFFFFF. */ |
| 977 | if (main_hdr->blockid == IBR_HDR_PEX_ID) |
| 978 | main_hdr->srcaddr = cpu_to_le32(0xFFFFFFFF); |
| 979 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 980 | /* Generate the ext header */ |
| 981 | if (has_ext) { |
Mario Six | e89016c | 2017-01-11 16:00:56 +0100 | [diff] [blame] | 982 | struct ext_hdr_v0 *ext_hdr; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 983 | int cfgi, datai; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 984 | |
Mario Six | 885fba1 | 2017-01-11 16:00:55 +0100 | [diff] [blame] | 985 | ext_hdr = (struct ext_hdr_v0 *) |
| 986 | (image + sizeof(struct main_hdr_v0)); |
Reinhard Pfau | a8840dc | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 987 | ext_hdr->offset = cpu_to_le32(0x40); |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 988 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 989 | for (cfgi = 0, datai = 0; cfgi < cfgn; cfgi++) { |
| 990 | e = &image_cfg[cfgi]; |
| 991 | if (e->type != IMAGE_CFG_DATA) |
| 992 | continue; |
| 993 | |
Reinhard Pfau | a8840dc | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 994 | ext_hdr->rcfg[datai].raddr = |
| 995 | cpu_to_le32(e->regdata.raddr); |
| 996 | ext_hdr->rcfg[datai].rdata = |
| 997 | cpu_to_le32(e->regdata.rdata); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 998 | datai++; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 999 | } |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1000 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1001 | ext_hdr->checksum = image_checksum8(ext_hdr, |
| 1002 | sizeof(struct ext_hdr_v0)); |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1003 | } |
| 1004 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1005 | *imagesz = headersz; |
| 1006 | return image; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1007 | } |
| 1008 | |
Mario Six | e93cf53 | 2017-01-11 16:00:57 +0100 | [diff] [blame] | 1009 | static size_t image_headersz_v1(int *hasext) |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1010 | { |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1011 | struct image_cfg_element *e; |
Pali Rohár | 02ba70a | 2021-07-23 11:14:11 +0200 | [diff] [blame] | 1012 | unsigned int count; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1013 | size_t headersz; |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1014 | int cpu_sheeva; |
| 1015 | struct stat s; |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1016 | int cfgi; |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1017 | int ret; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1018 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1019 | /* |
| 1020 | * Calculate the size of the header and the size of the |
| 1021 | * payload |
| 1022 | */ |
| 1023 | headersz = sizeof(struct main_hdr_v1); |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1024 | |
Pali Rohár | e58f08b | 2021-10-21 16:46:07 +0200 | [diff] [blame] | 1025 | if (image_get_csk_index() >= 0) { |
| 1026 | headersz += sizeof(struct secure_hdr_v1); |
| 1027 | if (hasext) |
| 1028 | *hasext = 1; |
| 1029 | } |
| 1030 | |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1031 | cpu_sheeva = image_is_cpu_sheeva(); |
| 1032 | |
Pali Rohár | d737d5d | 2022-01-12 18:20:37 +0100 | [diff] [blame] | 1033 | count = 0; |
| 1034 | for (cfgi = 0; cfgi < cfgn; cfgi++) { |
| 1035 | e = &image_cfg[cfgi]; |
| 1036 | |
| 1037 | if (e->type == IMAGE_CFG_DATA) |
| 1038 | count++; |
| 1039 | |
Pali Rohár | 3db9c41 | 2022-01-12 18:20:38 +0100 | [diff] [blame] | 1040 | if (e->type == IMAGE_CFG_DATA_DELAY || |
| 1041 | (e->type == IMAGE_CFG_BINARY && count > 0)) { |
Pali Rohár | d737d5d | 2022-01-12 18:20:37 +0100 | [diff] [blame] | 1042 | headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4; |
| 1043 | count = 0; |
| 1044 | } |
Pali Rohár | 02ba70a | 2021-07-23 11:14:11 +0200 | [diff] [blame] | 1045 | |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1046 | if (e->type != IMAGE_CFG_BINARY) |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1047 | continue; |
| 1048 | |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1049 | ret = stat(e->binary.file, &s); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1050 | if (ret < 0) { |
Andreas Bießmann | e5f1a58 | 2014-10-24 23:39:11 +0200 | [diff] [blame] | 1051 | char cwd[PATH_MAX]; |
| 1052 | char *dir = cwd; |
| 1053 | |
| 1054 | memset(cwd, 0, sizeof(cwd)); |
| 1055 | if (!getcwd(cwd, sizeof(cwd))) { |
| 1056 | dir = "current working directory"; |
| 1057 | perror("getcwd() failed"); |
| 1058 | } |
| 1059 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1060 | fprintf(stderr, |
| 1061 | "Didn't find the file '%s' in '%s' which is mandatory to generate the image\n" |
| 1062 | "This file generally contains the DDR3 training code, and should be extracted from an existing bootable\n" |
Pali Rohár | 3d7b93d | 2021-07-23 11:14:35 +0200 | [diff] [blame] | 1063 | "image for your board. Use 'dumpimage -T kwbimage -p 0' to extract it from an existing image.\n", |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1064 | e->binary.file, dir); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1065 | return 0; |
| 1066 | } |
| 1067 | |
Pali Rohár | e58f08b | 2021-10-21 16:46:07 +0200 | [diff] [blame] | 1068 | headersz += sizeof(struct opt_hdr_v1) + sizeof(uint32_t) + |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1069 | (e->binary.nargs) * sizeof(uint32_t); |
| 1070 | |
| 1071 | if (e->binary.loadaddr) { |
| 1072 | /* |
| 1073 | * BootROM loads kwbimage header (in which the |
| 1074 | * executable code is also stored) to address |
| 1075 | * 0x40004000 or 0x40000000. Thus there is |
| 1076 | * restriction for the load address of the N-th |
| 1077 | * BINARY image. |
| 1078 | */ |
| 1079 | unsigned int base_addr, low_addr, high_addr; |
| 1080 | |
| 1081 | base_addr = cpu_sheeva ? 0x40004000 : 0x40000000; |
| 1082 | low_addr = base_addr + headersz; |
| 1083 | high_addr = low_addr + |
| 1084 | (BINARY_MAX_ARGS - e->binary.nargs) * sizeof(uint32_t); |
| 1085 | |
| 1086 | if (cpu_sheeva && e->binary.loadaddr % 16) { |
| 1087 | fprintf(stderr, |
| 1088 | "Invalid LOAD_ADDRESS 0x%08x for BINARY %s with %d args.\n" |
| 1089 | "Address for CPU SHEEVA must be 16-byte aligned.\n", |
| 1090 | e->binary.loadaddr, e->binary.file, e->binary.nargs); |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | if (e->binary.loadaddr % 4 || e->binary.loadaddr < low_addr || |
| 1095 | e->binary.loadaddr > high_addr) { |
| 1096 | fprintf(stderr, |
| 1097 | "Invalid LOAD_ADDRESS 0x%08x for BINARY %s with %d args.\n" |
| 1098 | "Address must be 4-byte aligned and in range 0x%08x-0x%08x.\n", |
| 1099 | e->binary.loadaddr, e->binary.file, |
| 1100 | e->binary.nargs, low_addr, high_addr); |
| 1101 | return 0; |
| 1102 | } |
| 1103 | headersz = e->binary.loadaddr - base_addr; |
Pali Rohár | bdf8c9f | 2022-01-12 18:20:46 +0100 | [diff] [blame] | 1104 | } else if (cpu_sheeva) { |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1105 | headersz = ALIGN(headersz, 16); |
Pali Rohár | bdf8c9f | 2022-01-12 18:20:46 +0100 | [diff] [blame] | 1106 | } else { |
| 1107 | headersz = ALIGN(headersz, 4); |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1108 | } |
| 1109 | |
Pali Rohár | e58f08b | 2021-10-21 16:46:07 +0200 | [diff] [blame] | 1110 | headersz += ALIGN(s.st_size, 4) + sizeof(uint32_t); |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1111 | if (hasext) |
| 1112 | *hasext = 1; |
| 1113 | } |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1114 | |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1115 | if (count > 0) |
| 1116 | headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4; |
| 1117 | |
Pali Rohár | 5cad2e6 | 2021-11-08 18:12:48 +0100 | [diff] [blame] | 1118 | return image_headersz_align(headersz, image_get_bootfrom()); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1119 | } |
| 1120 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 1121 | static int add_binary_header_v1(uint8_t **cur, uint8_t **next_ext, |
| 1122 | struct image_cfg_element *binarye, |
| 1123 | struct main_hdr_v1 *main_hdr) |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1124 | { |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1125 | struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)*cur; |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1126 | uint32_t base_addr; |
Pali Rohár | e58f08b | 2021-10-21 16:46:07 +0200 | [diff] [blame] | 1127 | uint32_t add_args; |
| 1128 | uint32_t offset; |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1129 | uint32_t *args; |
| 1130 | size_t binhdrsz; |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1131 | int cpu_sheeva; |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1132 | struct stat s; |
| 1133 | int argi; |
| 1134 | FILE *bin; |
| 1135 | int ret; |
| 1136 | |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1137 | hdr->headertype = OPT_HDR_V1_BINARY_TYPE; |
| 1138 | |
| 1139 | bin = fopen(binarye->binary.file, "r"); |
| 1140 | if (!bin) { |
| 1141 | fprintf(stderr, "Cannot open binary file %s\n", |
| 1142 | binarye->binary.file); |
| 1143 | return -1; |
| 1144 | } |
| 1145 | |
Mario Six | 1f6c8a5 | 2017-02-13 10:11:55 +0100 | [diff] [blame] | 1146 | if (fstat(fileno(bin), &s)) { |
| 1147 | fprintf(stderr, "Cannot stat binary file %s\n", |
| 1148 | binarye->binary.file); |
| 1149 | goto err_close; |
| 1150 | } |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1151 | |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1152 | *cur += sizeof(struct opt_hdr_v1); |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1153 | |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1154 | args = (uint32_t *)*cur; |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1155 | *args = cpu_to_le32(binarye->binary.nargs); |
| 1156 | args++; |
| 1157 | for (argi = 0; argi < binarye->binary.nargs; argi++) |
| 1158 | args[argi] = cpu_to_le32(binarye->binary.args[argi]); |
| 1159 | |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1160 | *cur += (binarye->binary.nargs + 1) * sizeof(uint32_t); |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1161 | |
Pali Rohár | e58f08b | 2021-10-21 16:46:07 +0200 | [diff] [blame] | 1162 | /* |
Pali Rohár | bdf8c9f | 2022-01-12 18:20:46 +0100 | [diff] [blame] | 1163 | * ARM executable code inside the BIN header on platforms with Sheeva |
| 1164 | * CPU (A370 and AXP) must always be aligned with the 128-bit boundary. |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1165 | * In the case when this code is not position independent (e.g. ARM |
| 1166 | * SPL), it must be placed at fixed load and execute address. |
Pali Rohár | e58f08b | 2021-10-21 16:46:07 +0200 | [diff] [blame] | 1167 | * This requirement can be met by inserting dummy arguments into |
| 1168 | * BIN header, if needed. |
| 1169 | */ |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1170 | cpu_sheeva = image_is_cpu_sheeva(); |
| 1171 | base_addr = cpu_sheeva ? 0x40004000 : 0x40000000; |
Pali Rohár | e58f08b | 2021-10-21 16:46:07 +0200 | [diff] [blame] | 1172 | offset = *cur - (uint8_t *)main_hdr; |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1173 | if (binarye->binary.loadaddr) |
| 1174 | add_args = (binarye->binary.loadaddr - base_addr - offset) / sizeof(uint32_t); |
Pali Rohár | bdf8c9f | 2022-01-12 18:20:46 +0100 | [diff] [blame] | 1175 | else if (cpu_sheeva) |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1176 | add_args = ((16 - offset % 16) % 16) / sizeof(uint32_t); |
Pali Rohár | bdf8c9f | 2022-01-12 18:20:46 +0100 | [diff] [blame] | 1177 | else |
| 1178 | add_args = 0; |
Pali Rohár | e58f08b | 2021-10-21 16:46:07 +0200 | [diff] [blame] | 1179 | if (add_args) { |
| 1180 | *(args - 1) = cpu_to_le32(binarye->binary.nargs + add_args); |
| 1181 | *cur += add_args * sizeof(uint32_t); |
| 1182 | } |
| 1183 | |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1184 | ret = fread(*cur, s.st_size, 1, bin); |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1185 | if (ret != 1) { |
| 1186 | fprintf(stderr, |
| 1187 | "Could not read binary image %s\n", |
| 1188 | binarye->binary.file); |
Mario Six | 1f6c8a5 | 2017-02-13 10:11:55 +0100 | [diff] [blame] | 1189 | goto err_close; |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | fclose(bin); |
| 1193 | |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1194 | *cur += ALIGN(s.st_size, 4); |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1195 | |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1196 | *((uint32_t *)*cur) = 0x00000000; |
| 1197 | **next_ext = 1; |
| 1198 | *next_ext = *cur; |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1199 | |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1200 | *cur += sizeof(uint32_t); |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1201 | |
Pali Rohár | e58f08b | 2021-10-21 16:46:07 +0200 | [diff] [blame] | 1202 | binhdrsz = sizeof(struct opt_hdr_v1) + |
| 1203 | (binarye->binary.nargs + add_args + 2) * sizeof(uint32_t) + |
| 1204 | ALIGN(s.st_size, 4); |
| 1205 | hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF); |
| 1206 | hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16; |
| 1207 | |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1208 | return 0; |
Mario Six | 1f6c8a5 | 2017-02-13 10:11:55 +0100 | [diff] [blame] | 1209 | |
| 1210 | err_close: |
| 1211 | fclose(bin); |
| 1212 | |
| 1213 | return -1; |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 1216 | static int export_pub_kak_hash(RSA *kak, struct secure_hdr_v1 *secure_hdr) |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1217 | { |
| 1218 | FILE *hashf; |
| 1219 | int res; |
| 1220 | |
| 1221 | hashf = fopen("pub_kak_hash.txt", "w"); |
Heinrich Schuchardt | f858bb2 | 2021-08-17 07:03:20 +0200 | [diff] [blame] | 1222 | if (!hashf) { |
| 1223 | fprintf(stderr, "Couldn't open hash file: '%s': %s\n", |
| 1224 | "pub_kak_hash.txt", strerror(errno)); |
| 1225 | return 1; |
| 1226 | } |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1227 | |
| 1228 | res = kwb_export_pubkey(kak, &secure_hdr->kak, hashf, "KAK"); |
| 1229 | |
| 1230 | fclose(hashf); |
| 1231 | |
| 1232 | return res < 0 ? 1 : 0; |
| 1233 | } |
| 1234 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 1235 | static int kwb_sign_csk_with_kak(struct image_tool_params *params, |
| 1236 | struct secure_hdr_v1 *secure_hdr, RSA *csk) |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1237 | { |
| 1238 | RSA *kak = NULL; |
| 1239 | RSA *kak_pub = NULL; |
| 1240 | int csk_idx = image_get_csk_index(); |
| 1241 | struct sig_v1 tmp_sig; |
| 1242 | |
Heinrich Schuchardt | f0317d7 | 2021-08-17 07:11:58 +0200 | [diff] [blame] | 1243 | if (csk_idx < 0 || csk_idx > 15) { |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1244 | fprintf(stderr, "Invalid CSK index %d\n", csk_idx); |
| 1245 | return 1; |
| 1246 | } |
| 1247 | |
| 1248 | if (kwb_load_kak(params, &kak) < 0) |
| 1249 | return 1; |
| 1250 | |
| 1251 | if (export_pub_kak_hash(kak, secure_hdr)) |
| 1252 | return 1; |
| 1253 | |
| 1254 | if (kwb_import_pubkey(&kak_pub, &secure_hdr->kak, "KAK") < 0) |
| 1255 | return 1; |
| 1256 | |
| 1257 | if (kwb_export_pubkey(csk, &secure_hdr->csk[csk_idx], NULL, "CSK") < 0) |
| 1258 | return 1; |
| 1259 | |
| 1260 | if (kwb_sign_and_verify(kak, &secure_hdr->csk, |
| 1261 | sizeof(secure_hdr->csk) + |
| 1262 | sizeof(secure_hdr->csksig), |
| 1263 | &tmp_sig, "CSK") < 0) |
| 1264 | return 1; |
| 1265 | |
| 1266 | if (kwb_verify(kak_pub, &secure_hdr->csk, |
| 1267 | sizeof(secure_hdr->csk) + |
| 1268 | sizeof(secure_hdr->csksig), |
| 1269 | &tmp_sig, "CSK (2)") < 0) |
| 1270 | return 1; |
| 1271 | |
| 1272 | secure_hdr->csksig = tmp_sig; |
| 1273 | |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 1277 | static int add_secure_header_v1(struct image_tool_params *params, uint8_t *ptr, |
| 1278 | int payloadsz, size_t headersz, uint8_t *image, |
| 1279 | struct secure_hdr_v1 *secure_hdr) |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1280 | { |
| 1281 | struct image_cfg_element *e_jtagdelay; |
| 1282 | struct image_cfg_element *e_boxid; |
| 1283 | struct image_cfg_element *e_flashid; |
| 1284 | RSA *csk = NULL; |
| 1285 | unsigned char *image_ptr; |
| 1286 | size_t image_size; |
| 1287 | struct sig_v1 tmp_sig; |
| 1288 | bool specialized_img = image_get_spezialized_img(); |
| 1289 | |
| 1290 | kwb_msg("Create secure header content\n"); |
| 1291 | |
| 1292 | e_jtagdelay = image_find_option(IMAGE_CFG_JTAG_DELAY); |
| 1293 | e_boxid = image_find_option(IMAGE_CFG_BOX_ID); |
| 1294 | e_flashid = image_find_option(IMAGE_CFG_FLASH_ID); |
| 1295 | |
| 1296 | if (kwb_load_csk(params, &csk) < 0) |
| 1297 | return 1; |
| 1298 | |
| 1299 | secure_hdr->headertype = OPT_HDR_V1_SECURE_TYPE; |
| 1300 | secure_hdr->headersz_msb = 0; |
| 1301 | secure_hdr->headersz_lsb = cpu_to_le16(sizeof(struct secure_hdr_v1)); |
| 1302 | if (e_jtagdelay) |
| 1303 | secure_hdr->jtag_delay = e_jtagdelay->jtag_delay; |
| 1304 | if (e_boxid && specialized_img) |
| 1305 | secure_hdr->boxid = cpu_to_le32(e_boxid->boxid); |
| 1306 | if (e_flashid && specialized_img) |
| 1307 | secure_hdr->flashid = cpu_to_le32(e_flashid->flashid); |
| 1308 | |
| 1309 | if (kwb_sign_csk_with_kak(params, secure_hdr, csk)) |
| 1310 | return 1; |
| 1311 | |
| 1312 | image_ptr = ptr + headersz; |
| 1313 | image_size = payloadsz - headersz; |
| 1314 | |
| 1315 | if (kwb_sign_and_verify(csk, image_ptr, image_size, |
| 1316 | &secure_hdr->imgsig, "image") < 0) |
| 1317 | return 1; |
| 1318 | |
| 1319 | if (kwb_sign_and_verify(csk, image, headersz, &tmp_sig, "header") < 0) |
| 1320 | return 1; |
| 1321 | |
| 1322 | secure_hdr->hdrsig = tmp_sig; |
| 1323 | |
| 1324 | kwb_dump_fuse_cmds(secure_hdr); |
| 1325 | |
| 1326 | return 0; |
| 1327 | } |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1328 | |
Pali Rohár | 9ac1def | 2022-01-12 18:20:36 +0100 | [diff] [blame] | 1329 | static void finish_register_set_header_v1(uint8_t **cur, uint8_t **next_ext, |
| 1330 | struct register_set_hdr_v1 *register_set_hdr, |
| 1331 | int *datai, uint8_t delay) |
| 1332 | { |
| 1333 | int size = sizeof(struct register_set_hdr_v1) + 8 * (*datai) + 4; |
| 1334 | |
| 1335 | register_set_hdr->headertype = OPT_HDR_V1_REGISTER_TYPE; |
| 1336 | register_set_hdr->headersz_lsb = cpu_to_le16(size & 0xFFFF); |
| 1337 | register_set_hdr->headersz_msb = size >> 16; |
| 1338 | register_set_hdr->data[*datai].last_entry.delay = delay; |
| 1339 | *cur += size; |
| 1340 | **next_ext = 1; |
| 1341 | *next_ext = ®ister_set_hdr->data[*datai].last_entry.next; |
| 1342 | *datai = 0; |
| 1343 | } |
| 1344 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1345 | static void *image_create_v1(size_t *imagesz, struct image_tool_params *params, |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1346 | uint8_t *ptr, int payloadsz) |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1347 | { |
Mario Six | 79066ef | 2017-01-11 16:00:58 +0100 | [diff] [blame] | 1348 | struct image_cfg_element *e; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1349 | struct main_hdr_v1 *main_hdr; |
Pali Rohár | 2b0980c | 2021-11-08 18:12:49 +0100 | [diff] [blame] | 1350 | struct opt_hdr_v1 *ohdr; |
Pali Rohár | 02ba70a | 2021-07-23 11:14:11 +0200 | [diff] [blame] | 1351 | struct register_set_hdr_v1 *register_set_hdr; |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1352 | struct secure_hdr_v1 *secure_hdr = NULL; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1353 | size_t headersz; |
Mario Six | 885fba1 | 2017-01-11 16:00:55 +0100 | [diff] [blame] | 1354 | uint8_t *image, *cur; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1355 | int hasext = 0; |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1356 | uint8_t *next_ext = NULL; |
Pali Rohár | 9ac1def | 2022-01-12 18:20:36 +0100 | [diff] [blame] | 1357 | int cfgi, datai; |
Pali Rohár | 3db9c41 | 2022-01-12 18:20:38 +0100 | [diff] [blame] | 1358 | uint8_t delay; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1359 | |
| 1360 | /* |
| 1361 | * Calculate the size of the header and the size of the |
| 1362 | * payload |
| 1363 | */ |
Mario Six | e93cf53 | 2017-01-11 16:00:57 +0100 | [diff] [blame] | 1364 | headersz = image_headersz_v1(&hasext); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1365 | if (headersz == 0) |
| 1366 | return NULL; |
| 1367 | |
| 1368 | image = malloc(headersz); |
| 1369 | if (!image) { |
| 1370 | fprintf(stderr, "Cannot allocate memory for image\n"); |
| 1371 | return NULL; |
| 1372 | } |
| 1373 | |
| 1374 | memset(image, 0, headersz); |
| 1375 | |
Mario Six | 885fba1 | 2017-01-11 16:00:55 +0100 | [diff] [blame] | 1376 | main_hdr = (struct main_hdr_v1 *)image; |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1377 | cur = image; |
| 1378 | cur += sizeof(struct main_hdr_v1); |
| 1379 | next_ext = &main_hdr->ext; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1380 | |
| 1381 | /* Fill the main header */ |
Reinhard Pfau | a8840dc | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 1382 | main_hdr->blocksize = |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 1383 | cpu_to_le32(payloadsz); |
Reinhard Pfau | a8840dc | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 1384 | main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1385 | main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16; |
Pali Rohár | cc3443f | 2021-07-23 11:14:06 +0200 | [diff] [blame] | 1386 | main_hdr->destaddr = cpu_to_le32(params->addr); |
Reinhard Pfau | a8840dc | 2015-11-29 15:48:25 +0100 | [diff] [blame] | 1387 | main_hdr->execaddr = cpu_to_le32(params->ep); |
| 1388 | main_hdr->srcaddr = cpu_to_le32(headersz); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1389 | main_hdr->ext = hasext; |
| 1390 | main_hdr->version = 1; |
Pali Rohár | d1547b3 | 2021-11-08 18:12:43 +0100 | [diff] [blame] | 1391 | main_hdr->blockid = image_get_bootfrom(); |
| 1392 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1393 | e = image_find_option(IMAGE_CFG_NAND_BLKSZ); |
| 1394 | if (e) |
| 1395 | main_hdr->nandblocksize = e->nandblksz / (64 * 1024); |
Pali Rohár | 2fdba4f | 2021-10-22 12:37:46 +0200 | [diff] [blame] | 1396 | e = image_find_option(IMAGE_CFG_NAND_PAGESZ); |
| 1397 | if (e) |
| 1398 | main_hdr->nandpagesize = cpu_to_le16(e->nandpagesz); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1399 | e = image_find_option(IMAGE_CFG_NAND_BADBLK_LOCATION); |
| 1400 | if (e) |
| 1401 | main_hdr->nandbadblklocation = e->nandbadblklocation; |
Chris Packham | 4bdb547 | 2016-11-09 22:07:45 +1300 | [diff] [blame] | 1402 | e = image_find_option(IMAGE_CFG_BAUDRATE); |
| 1403 | if (e) |
Pali Rohár | 12f2c03 | 2021-11-08 18:12:41 +0100 | [diff] [blame] | 1404 | main_hdr->options |= baudrate_to_option(e->baudrate); |
| 1405 | e = image_find_option(IMAGE_CFG_UART_PORT); |
| 1406 | if (e) |
| 1407 | main_hdr->options |= (e->uart_port & 3) << 3; |
| 1408 | e = image_find_option(IMAGE_CFG_UART_MPP); |
| 1409 | if (e) |
| 1410 | main_hdr->options |= (e->uart_mpp & 7) << 5; |
Chris Packham | 2611c05 | 2016-11-09 22:21:45 +1300 | [diff] [blame] | 1411 | e = image_find_option(IMAGE_CFG_DEBUG); |
| 1412 | if (e) |
| 1413 | main_hdr->flags = e->debug ? 0x1 : 0; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1414 | |
Pali Rohár | 501a54a | 2021-07-23 11:13:59 +0200 | [diff] [blame] | 1415 | /* |
| 1416 | * For SATA srcaddr is specified in number of sectors starting from |
| 1417 | * sector 0. The main header is stored at sector number 1. |
| 1418 | * This expects the sector size to be 512 bytes. |
| 1419 | * Header size is already aligned. |
| 1420 | */ |
| 1421 | if (main_hdr->blockid == IBR_HDR_SATA_ID) |
| 1422 | main_hdr->srcaddr = cpu_to_le32(headersz / 512 + 1); |
| 1423 | |
| 1424 | /* |
| 1425 | * For SDIO srcaddr is specified in number of sectors starting from |
| 1426 | * sector 0. The main header is stored at sector number 0. |
| 1427 | * This expects sector size to be 512 bytes. |
| 1428 | * Header size is already aligned. |
| 1429 | */ |
| 1430 | if (main_hdr->blockid == IBR_HDR_SDIO_ID) |
| 1431 | main_hdr->srcaddr = cpu_to_le32(headersz / 512); |
| 1432 | |
| 1433 | /* For PCIe srcaddr is not used and must be set to 0xFFFFFFFF. */ |
| 1434 | if (main_hdr->blockid == IBR_HDR_PEX_ID) |
| 1435 | main_hdr->srcaddr = cpu_to_le32(0xFFFFFFFF); |
| 1436 | |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1437 | if (image_get_csk_index() >= 0) { |
| 1438 | /* |
| 1439 | * only reserve the space here; we fill the header later since |
| 1440 | * we need the header to be complete to compute the signatures |
| 1441 | */ |
| 1442 | secure_hdr = (struct secure_hdr_v1 *)cur; |
| 1443 | cur += sizeof(struct secure_hdr_v1); |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1444 | *next_ext = 1; |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1445 | next_ext = &secure_hdr->next; |
| 1446 | } |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1447 | |
Pali Rohár | 02ba70a | 2021-07-23 11:14:11 +0200 | [diff] [blame] | 1448 | datai = 0; |
Pali Rohár | 02ba70a | 2021-07-23 11:14:11 +0200 | [diff] [blame] | 1449 | for (cfgi = 0; cfgi < cfgn; cfgi++) { |
| 1450 | e = &image_cfg[cfgi]; |
Pali Rohár | f63c583 | 2021-07-23 11:14:12 +0200 | [diff] [blame] | 1451 | if (e->type != IMAGE_CFG_DATA && |
Pali Rohár | 3db9c41 | 2022-01-12 18:20:38 +0100 | [diff] [blame] | 1452 | e->type != IMAGE_CFG_DATA_DELAY && |
| 1453 | e->type != IMAGE_CFG_BINARY) |
Pali Rohár | 02ba70a | 2021-07-23 11:14:11 +0200 | [diff] [blame] | 1454 | continue; |
Pali Rohár | 3db9c41 | 2022-01-12 18:20:38 +0100 | [diff] [blame] | 1455 | |
Pali Rohár | d737d5d | 2022-01-12 18:20:37 +0100 | [diff] [blame] | 1456 | if (datai == 0) |
| 1457 | register_set_hdr = (struct register_set_hdr_v1 *)cur; |
Pali Rohár | 3db9c41 | 2022-01-12 18:20:38 +0100 | [diff] [blame] | 1458 | |
| 1459 | /* If delay is not specified, use the smallest possible value. */ |
| 1460 | if (e->type == IMAGE_CFG_DATA_DELAY) |
| 1461 | delay = e->regdata_delay; |
| 1462 | else |
| 1463 | delay = REGISTER_SET_HDR_OPT_DELAY_MS(0); |
| 1464 | |
| 1465 | /* |
| 1466 | * DATA_DELAY command is the last entry in the register set |
| 1467 | * header and BINARY command inserts new binary header. |
| 1468 | * Therefore BINARY command requires to finish register set |
| 1469 | * header if some DATA command was specified. And DATA_DELAY |
| 1470 | * command automatically finish register set header even when |
| 1471 | * there was no DATA command. |
| 1472 | */ |
| 1473 | if (e->type == IMAGE_CFG_DATA_DELAY || |
| 1474 | (e->type == IMAGE_CFG_BINARY && datai != 0)) |
Pali Rohár | 9ac1def | 2022-01-12 18:20:36 +0100 | [diff] [blame] | 1475 | finish_register_set_header_v1(&cur, &next_ext, register_set_hdr, |
Pali Rohár | 3db9c41 | 2022-01-12 18:20:38 +0100 | [diff] [blame] | 1476 | &datai, delay); |
| 1477 | |
| 1478 | if (e->type == IMAGE_CFG_DATA) { |
| 1479 | register_set_hdr->data[datai].entry.address = |
| 1480 | cpu_to_le32(e->regdata.raddr); |
| 1481 | register_set_hdr->data[datai].entry.value = |
| 1482 | cpu_to_le32(e->regdata.rdata); |
| 1483 | datai++; |
Pali Rohár | f63c583 | 2021-07-23 11:14:12 +0200 | [diff] [blame] | 1484 | } |
Pali Rohár | 3db9c41 | 2022-01-12 18:20:38 +0100 | [diff] [blame] | 1485 | |
| 1486 | if (e->type == IMAGE_CFG_BINARY) { |
| 1487 | if (add_binary_header_v1(&cur, &next_ext, e, main_hdr)) |
| 1488 | return NULL; |
| 1489 | } |
Pali Rohár | 02ba70a | 2021-07-23 11:14:11 +0200 | [diff] [blame] | 1490 | } |
| 1491 | if (datai != 0) { |
Pali Rohár | 9ac1def | 2022-01-12 18:20:36 +0100 | [diff] [blame] | 1492 | /* Set delay to the smallest possible value. */ |
Pali Rohár | 3db9c41 | 2022-01-12 18:20:38 +0100 | [diff] [blame] | 1493 | delay = REGISTER_SET_HDR_OPT_DELAY_MS(0); |
Pali Rohár | 9ac1def | 2022-01-12 18:20:36 +0100 | [diff] [blame] | 1494 | finish_register_set_header_v1(&cur, &next_ext, register_set_hdr, |
Pali Rohár | 3db9c41 | 2022-01-12 18:20:38 +0100 | [diff] [blame] | 1495 | &datai, delay); |
Pali Rohár | d9fb82c | 2021-07-23 11:14:09 +0200 | [diff] [blame] | 1496 | } |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1497 | |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 1498 | if (secure_hdr && add_secure_header_v1(params, ptr, payloadsz + headersz, |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1499 | headersz, image, secure_hdr)) |
| 1500 | return NULL; |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1501 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1502 | *imagesz = headersz; |
Pali Rohár | 2b0980c | 2021-11-08 18:12:49 +0100 | [diff] [blame] | 1503 | |
| 1504 | /* Fill the real header size without padding into the main header */ |
| 1505 | headersz = sizeof(*main_hdr); |
| 1506 | for_each_opt_hdr_v1 (ohdr, main_hdr) |
| 1507 | headersz += opt_hdr_v1_size(ohdr); |
| 1508 | main_hdr->headersz_lsb = cpu_to_le16(headersz & 0xFFFF); |
| 1509 | main_hdr->headersz_msb = (headersz & 0xFFFF0000) >> 16; |
| 1510 | |
Pierre Bourdon | 9203c73 | 2021-12-25 20:50:19 +0100 | [diff] [blame] | 1511 | /* Calculate and set the header checksum */ |
| 1512 | main_hdr->checksum = image_checksum8(main_hdr, headersz); |
| 1513 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1514 | return image; |
| 1515 | } |
| 1516 | |
Pali Rohár | 6eb20bb | 2022-01-12 18:20:35 +0100 | [diff] [blame] | 1517 | static int recognize_keyword(char *keyword) |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1518 | { |
| 1519 | int kw_id; |
| 1520 | |
| 1521 | for (kw_id = 1; kw_id < IMAGE_CFG_COUNT; ++kw_id) |
| 1522 | if (!strcmp(keyword, id_strs[kw_id])) |
| 1523 | return kw_id; |
| 1524 | |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1528 | static int image_create_config_parse_oneline(char *line, |
| 1529 | struct image_cfg_element *el) |
| 1530 | { |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1531 | char *keyword, *saveptr, *value1, *value2; |
| 1532 | char delimiters[] = " \t"; |
| 1533 | int keyword_id, ret, argi; |
| 1534 | char *unknown_msg = "Ignoring unknown line '%s'\n"; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1535 | |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1536 | keyword = strtok_r(line, delimiters, &saveptr); |
| 1537 | keyword_id = recognize_keyword(keyword); |
Mario Six | 94490a4 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 1538 | |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1539 | if (!keyword_id) { |
| 1540 | fprintf(stderr, unknown_msg, line); |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
| 1544 | el->type = keyword_id; |
| 1545 | |
| 1546 | value1 = strtok_r(NULL, delimiters, &saveptr); |
| 1547 | |
| 1548 | if (!value1) { |
| 1549 | fprintf(stderr, "Parameter missing in line '%s'\n", line); |
| 1550 | return -1; |
| 1551 | } |
| 1552 | |
| 1553 | switch (keyword_id) { |
| 1554 | case IMAGE_CFG_VERSION: |
| 1555 | el->version = atoi(value1); |
| 1556 | break; |
Pali Rohár | af49605 | 2022-01-12 18:20:40 +0100 | [diff] [blame] | 1557 | case IMAGE_CFG_CPU: |
| 1558 | if (strcmp(value1, "FEROCEON") == 0) |
| 1559 | el->cpu_sheeva = 0; |
| 1560 | else if (strcmp(value1, "SHEEVA") == 0) |
| 1561 | el->cpu_sheeva = 1; |
| 1562 | else if (strcmp(value1, "A9") == 0) |
| 1563 | el->cpu_sheeva = 0; |
| 1564 | else { |
| 1565 | fprintf(stderr, "Invalid CPU %s\n", value1); |
| 1566 | return -1; |
| 1567 | } |
| 1568 | break; |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1569 | case IMAGE_CFG_BOOT_FROM: |
| 1570 | ret = image_boot_mode_id(value1); |
Mario Six | 94490a4 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 1571 | |
Andreas Bießmann | f411b8f | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 1572 | if (ret < 0) { |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1573 | fprintf(stderr, "Invalid boot media '%s'\n", value1); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1574 | return -1; |
| 1575 | } |
Andreas Bießmann | f411b8f | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 1576 | el->bootfrom = ret; |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1577 | break; |
| 1578 | case IMAGE_CFG_NAND_BLKSZ: |
| 1579 | el->nandblksz = strtoul(value1, NULL, 16); |
| 1580 | break; |
| 1581 | case IMAGE_CFG_NAND_BADBLK_LOCATION: |
| 1582 | el->nandbadblklocation = strtoul(value1, NULL, 16); |
| 1583 | break; |
| 1584 | case IMAGE_CFG_NAND_ECC_MODE: |
| 1585 | ret = image_nand_ecc_mode_id(value1); |
Mario Six | 94490a4 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 1586 | |
Andreas Bießmann | f411b8f | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 1587 | if (ret < 0) { |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1588 | fprintf(stderr, "Invalid NAND ECC mode '%s'\n", value1); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1589 | return -1; |
| 1590 | } |
Andreas Bießmann | f411b8f | 2014-10-24 23:25:52 +0200 | [diff] [blame] | 1591 | el->nandeccmode = ret; |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1592 | break; |
| 1593 | case IMAGE_CFG_NAND_PAGESZ: |
| 1594 | el->nandpagesz = strtoul(value1, NULL, 16); |
| 1595 | break; |
| 1596 | case IMAGE_CFG_BINARY: |
| 1597 | argi = 0; |
Mario Six | 94490a4 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 1598 | |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1599 | el->binary.file = strdup(value1); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1600 | while (1) { |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1601 | char *value = strtok_r(NULL, delimiters, &saveptr); |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1602 | char *endptr; |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1603 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1604 | if (!value) |
| 1605 | break; |
Pali Rohár | 0aca27e | 2022-01-12 18:20:41 +0100 | [diff] [blame] | 1606 | |
| 1607 | if (!strcmp(value, "LOAD_ADDRESS")) { |
| 1608 | value = strtok_r(NULL, delimiters, &saveptr); |
| 1609 | if (!value) { |
| 1610 | fprintf(stderr, |
| 1611 | "Missing address argument for BINARY LOAD_ADDRESS\n"); |
| 1612 | return -1; |
| 1613 | } |
| 1614 | el->binary.loadaddr = strtoul(value, &endptr, 16); |
| 1615 | if (*endptr) { |
| 1616 | fprintf(stderr, |
| 1617 | "Invalid argument '%s' for BINARY LOAD_ADDRESS\n", |
| 1618 | value); |
| 1619 | return -1; |
| 1620 | } |
| 1621 | value = strtok_r(NULL, delimiters, &saveptr); |
| 1622 | if (value) { |
| 1623 | fprintf(stderr, |
| 1624 | "Unexpected argument '%s' after BINARY LOAD_ADDRESS\n", |
| 1625 | value); |
| 1626 | return -1; |
| 1627 | } |
| 1628 | break; |
| 1629 | } |
| 1630 | |
| 1631 | el->binary.args[argi] = strtoul(value, &endptr, 16); |
| 1632 | if (*endptr) { |
| 1633 | fprintf(stderr, "Invalid argument '%s' for BINARY\n", value); |
| 1634 | return -1; |
| 1635 | } |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1636 | argi++; |
| 1637 | if (argi >= BINARY_MAX_ARGS) { |
| 1638 | fprintf(stderr, |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1639 | "Too many arguments for BINARY\n"); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1640 | return -1; |
| 1641 | } |
| 1642 | } |
| 1643 | el->binary.nargs = argi; |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1644 | break; |
| 1645 | case IMAGE_CFG_DATA: |
| 1646 | value2 = strtok_r(NULL, delimiters, &saveptr); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1647 | |
| 1648 | if (!value1 || !value2) { |
| 1649 | fprintf(stderr, |
| 1650 | "Invalid number of arguments for DATA\n"); |
| 1651 | return -1; |
| 1652 | } |
| 1653 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1654 | el->regdata.raddr = strtoul(value1, NULL, 16); |
| 1655 | el->regdata.rdata = strtoul(value2, NULL, 16); |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1656 | break; |
Pali Rohár | f63c583 | 2021-07-23 11:14:12 +0200 | [diff] [blame] | 1657 | case IMAGE_CFG_DATA_DELAY: |
| 1658 | if (!strcmp(value1, "SDRAM_SETUP")) |
| 1659 | el->regdata_delay = REGISTER_SET_HDR_OPT_DELAY_SDRAM_SETUP; |
| 1660 | else |
| 1661 | el->regdata_delay = REGISTER_SET_HDR_OPT_DELAY_MS(strtoul(value1, NULL, 10)); |
Pali Rohár | fdcae26 | 2022-01-12 18:20:48 +0100 | [diff] [blame^] | 1662 | if (el->regdata_delay > 255) { |
| 1663 | fprintf(stderr, "Maximal DATA_DELAY is 255\n"); |
| 1664 | return -1; |
| 1665 | } |
Pali Rohár | f63c583 | 2021-07-23 11:14:12 +0200 | [diff] [blame] | 1666 | break; |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1667 | case IMAGE_CFG_BAUDRATE: |
| 1668 | el->baudrate = strtoul(value1, NULL, 10); |
| 1669 | break; |
Pali Rohár | 12f2c03 | 2021-11-08 18:12:41 +0100 | [diff] [blame] | 1670 | case IMAGE_CFG_UART_PORT: |
| 1671 | el->uart_port = strtoul(value1, NULL, 16); |
| 1672 | break; |
| 1673 | case IMAGE_CFG_UART_MPP: |
| 1674 | el->uart_mpp = strtoul(value1, NULL, 16); |
| 1675 | break; |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1676 | case IMAGE_CFG_DEBUG: |
| 1677 | el->debug = strtoul(value1, NULL, 10); |
| 1678 | break; |
Mario Six | a1b6b0a | 2017-01-11 16:01:00 +0100 | [diff] [blame] | 1679 | case IMAGE_CFG_KAK: |
| 1680 | el->key_name = strdup(value1); |
| 1681 | break; |
| 1682 | case IMAGE_CFG_CSK: |
| 1683 | el->key_name = strdup(value1); |
| 1684 | break; |
| 1685 | case IMAGE_CFG_CSK_INDEX: |
| 1686 | el->csk_idx = strtol(value1, NULL, 0); |
| 1687 | break; |
| 1688 | case IMAGE_CFG_JTAG_DELAY: |
| 1689 | el->jtag_delay = strtoul(value1, NULL, 0); |
| 1690 | break; |
| 1691 | case IMAGE_CFG_BOX_ID: |
| 1692 | el->boxid = strtoul(value1, NULL, 0); |
| 1693 | break; |
| 1694 | case IMAGE_CFG_FLASH_ID: |
| 1695 | el->flashid = strtoul(value1, NULL, 0); |
| 1696 | break; |
| 1697 | case IMAGE_CFG_SEC_SPECIALIZED_IMG: |
| 1698 | el->sec_specialized_img = true; |
| 1699 | break; |
| 1700 | case IMAGE_CFG_SEC_COMMON_IMG: |
| 1701 | el->sec_specialized_img = false; |
| 1702 | break; |
| 1703 | case IMAGE_CFG_SEC_BOOT_DEV: |
| 1704 | el->sec_boot_dev = strtoul(value1, NULL, 0); |
| 1705 | break; |
| 1706 | case IMAGE_CFG_SEC_FUSE_DUMP: |
| 1707 | el->name = strdup(value1); |
| 1708 | break; |
Mario Six | 4991b4f | 2017-01-11 16:00:59 +0100 | [diff] [blame] | 1709 | default: |
| 1710 | fprintf(stderr, unknown_msg, line); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1711 | } |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1712 | |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1716 | /* |
| 1717 | * Parse the configuration file 'fcfg' into the array of configuration |
| 1718 | * elements 'image_cfg', and return the number of configuration |
| 1719 | * elements in 'cfgn'. |
| 1720 | */ |
| 1721 | static int image_create_config_parse(FILE *fcfg) |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1722 | { |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1723 | int ret; |
| 1724 | int cfgi = 0; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1725 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1726 | /* Parse the configuration file */ |
| 1727 | while (!feof(fcfg)) { |
| 1728 | char *line; |
| 1729 | char buf[256]; |
| 1730 | |
| 1731 | /* Read the current line */ |
| 1732 | memset(buf, 0, sizeof(buf)); |
| 1733 | line = fgets(buf, sizeof(buf), fcfg); |
| 1734 | if (!line) |
| 1735 | break; |
| 1736 | |
| 1737 | /* Ignore useless lines */ |
| 1738 | if (line[0] == '\n' || line[0] == '#') |
| 1739 | continue; |
| 1740 | |
| 1741 | /* Strip final newline */ |
| 1742 | if (line[strlen(line) - 1] == '\n') |
| 1743 | line[strlen(line) - 1] = 0; |
| 1744 | |
| 1745 | /* Parse the current line */ |
| 1746 | ret = image_create_config_parse_oneline(line, |
| 1747 | &image_cfg[cfgi]); |
| 1748 | if (ret) |
| 1749 | return ret; |
| 1750 | |
| 1751 | cfgi++; |
| 1752 | |
| 1753 | if (cfgi >= IMAGE_CFG_ELEMENT_MAX) { |
| 1754 | fprintf(stderr, |
| 1755 | "Too many configuration elements in .cfg file\n"); |
| 1756 | return -1; |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | cfgn = cfgi; |
| 1761 | return 0; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1762 | } |
| 1763 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1764 | static int image_get_version(void) |
| 1765 | { |
| 1766 | struct image_cfg_element *e; |
| 1767 | |
| 1768 | e = image_find_option(IMAGE_CFG_VERSION); |
| 1769 | if (!e) |
| 1770 | return -1; |
| 1771 | |
| 1772 | return e->version; |
| 1773 | } |
| 1774 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1775 | static void kwbimage_set_header(void *ptr, struct stat *sbuf, int ifd, |
| 1776 | struct image_tool_params *params) |
| 1777 | { |
| 1778 | FILE *fcfg; |
| 1779 | void *image = NULL; |
| 1780 | int version; |
Łukasz Majewski | 93e9371 | 2014-11-21 09:22:43 +0100 | [diff] [blame] | 1781 | size_t headersz = 0; |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 1782 | size_t datasz; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1783 | uint32_t checksum; |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 1784 | struct stat s; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1785 | int ret; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1786 | |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 1787 | /* |
| 1788 | * Do not use sbuf->st_size as it contains size with padding. |
| 1789 | * We need original image data size, so stat original file. |
| 1790 | */ |
| 1791 | if (stat(params->datafile, &s)) { |
| 1792 | fprintf(stderr, "Could not stat data file %s: %s\n", |
| 1793 | params->datafile, strerror(errno)); |
| 1794 | exit(EXIT_FAILURE); |
| 1795 | } |
| 1796 | datasz = ALIGN(s.st_size, 4); |
| 1797 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1798 | fcfg = fopen(params->imagename, "r"); |
| 1799 | if (!fcfg) { |
| 1800 | fprintf(stderr, "Could not open input file %s\n", |
| 1801 | params->imagename); |
| 1802 | exit(EXIT_FAILURE); |
| 1803 | } |
| 1804 | |
| 1805 | image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX * |
| 1806 | sizeof(struct image_cfg_element)); |
| 1807 | if (!image_cfg) { |
| 1808 | fprintf(stderr, "Cannot allocate memory\n"); |
| 1809 | fclose(fcfg); |
| 1810 | exit(EXIT_FAILURE); |
| 1811 | } |
| 1812 | |
| 1813 | memset(image_cfg, 0, |
| 1814 | IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element)); |
| 1815 | rewind(fcfg); |
| 1816 | |
| 1817 | ret = image_create_config_parse(fcfg); |
| 1818 | fclose(fcfg); |
| 1819 | if (ret) { |
| 1820 | free(image_cfg); |
| 1821 | exit(EXIT_FAILURE); |
| 1822 | } |
| 1823 | |
| 1824 | version = image_get_version(); |
Stefan Roese | 934a529 | 2014-10-28 11:32:24 +0100 | [diff] [blame] | 1825 | switch (version) { |
| 1826 | /* |
| 1827 | * Fallback to version 0 if no version is provided in the |
| 1828 | * cfg file |
| 1829 | */ |
| 1830 | case -1: |
| 1831 | case 0: |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 1832 | image = image_create_v0(&headersz, params, datasz + 4); |
Stefan Roese | 934a529 | 2014-10-28 11:32:24 +0100 | [diff] [blame] | 1833 | break; |
| 1834 | |
| 1835 | case 1: |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 1836 | image = image_create_v1(&headersz, params, ptr, datasz + 4); |
Stefan Roese | 934a529 | 2014-10-28 11:32:24 +0100 | [diff] [blame] | 1837 | break; |
| 1838 | |
| 1839 | default: |
| 1840 | fprintf(stderr, "Unsupported version %d\n", version); |
| 1841 | free(image_cfg); |
| 1842 | exit(EXIT_FAILURE); |
| 1843 | } |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1844 | |
| 1845 | if (!image) { |
| 1846 | fprintf(stderr, "Could not create image\n"); |
| 1847 | free(image_cfg); |
| 1848 | exit(EXIT_FAILURE); |
| 1849 | } |
| 1850 | |
| 1851 | free(image_cfg); |
| 1852 | |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 1853 | /* Build and add image data checksum */ |
Pali Rohár | 37cb9c1 | 2021-07-23 11:13:56 +0200 | [diff] [blame] | 1854 | checksum = cpu_to_le32(image_checksum32((uint8_t *)ptr + headersz, |
Pali Rohár | e23ad5d | 2021-11-08 18:12:47 +0100 | [diff] [blame] | 1855 | datasz)); |
| 1856 | memcpy((uint8_t *)ptr + headersz + datasz, &checksum, sizeof(uint32_t)); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1857 | |
| 1858 | /* Finally copy the header into the image area */ |
| 1859 | memcpy(ptr, image, headersz); |
| 1860 | |
| 1861 | free(image); |
| 1862 | } |
| 1863 | |
| 1864 | static void kwbimage_print_header(const void *ptr) |
| 1865 | { |
| 1866 | struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr; |
Marek Behún | 732c930 | 2021-08-18 00:59:15 +0200 | [diff] [blame] | 1867 | struct opt_hdr_v1 *ohdr; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1868 | |
| 1869 | printf("Image Type: MVEBU Boot from %s Image\n", |
| 1870 | image_boot_mode_name(mhdr->blockid)); |
Marek Behún | acb0b38 | 2021-09-24 23:07:00 +0200 | [diff] [blame] | 1871 | printf("Image version:%d\n", kwbimage_version(ptr)); |
Pali Rohár | 34dcf95 | 2021-07-23 11:14:04 +0200 | [diff] [blame] | 1872 | |
Marek Behún | 732c930 | 2021-08-18 00:59:15 +0200 | [diff] [blame] | 1873 | for_each_opt_hdr_v1 (ohdr, mhdr) { |
| 1874 | if (ohdr->headertype == OPT_HDR_V1_BINARY_TYPE) { |
| 1875 | printf("BIN Hdr Size: "); |
| 1876 | genimg_print_size(opt_hdr_v1_size(ohdr) - 12 - |
| 1877 | 4 * ohdr->data[0]); |
Pali Rohár | 34dcf95 | 2021-07-23 11:14:04 +0200 | [diff] [blame] | 1878 | } |
| 1879 | } |
Marek Behún | 732c930 | 2021-08-18 00:59:15 +0200 | [diff] [blame] | 1880 | |
Gerald Kerma | 26f195c | 2014-10-31 01:03:27 +0100 | [diff] [blame] | 1881 | printf("Data Size: "); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1882 | genimg_print_size(mhdr->blocksize - sizeof(uint32_t)); |
| 1883 | printf("Load Address: %08x\n", mhdr->destaddr); |
| 1884 | printf("Entry Point: %08x\n", mhdr->execaddr); |
| 1885 | } |
| 1886 | |
| 1887 | static int kwbimage_check_image_types(uint8_t type) |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1888 | { |
| 1889 | if (type == IH_TYPE_KWBIMAGE) |
| 1890 | return EXIT_SUCCESS; |
Mario Six | 94490a4 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 1891 | |
| 1892 | return EXIT_FAILURE; |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 1893 | } |
| 1894 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1895 | static int kwbimage_verify_header(unsigned char *ptr, int image_size, |
| 1896 | struct image_tool_params *params) |
| 1897 | { |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1898 | size_t header_size = kwbheader_size(ptr); |
Pali Rohár | 700ea98 | 2021-11-08 18:12:44 +0100 | [diff] [blame] | 1899 | uint8_t blockid; |
| 1900 | uint32_t offset; |
| 1901 | uint32_t size; |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1902 | uint8_t csum; |
Alexander Graf | 6cd5678 | 2018-03-15 11:14:19 +0100 | [diff] [blame] | 1903 | |
| 1904 | if (header_size > image_size) |
| 1905 | return -FDT_ERR_BADSTRUCTURE; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1906 | |
Baruch Siach | db7cd4ed | 2017-07-04 20:23:40 +0300 | [diff] [blame] | 1907 | if (!main_hdr_checksum_ok(ptr)) |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1908 | return -FDT_ERR_BADSTRUCTURE; |
| 1909 | |
| 1910 | /* Only version 0 extended header has checksum */ |
Marek Behún | acb0b38 | 2021-09-24 23:07:00 +0200 | [diff] [blame] | 1911 | if (kwbimage_version(ptr) == 0) { |
Pali Rohár | fe2c0e2 | 2021-07-23 11:14:01 +0200 | [diff] [blame] | 1912 | struct main_hdr_v0 *mhdr = (struct main_hdr_v0 *)ptr; |
Mario Six | e89016c | 2017-01-11 16:00:56 +0100 | [diff] [blame] | 1913 | |
Pali Rohár | fe2c0e2 | 2021-07-23 11:14:01 +0200 | [diff] [blame] | 1914 | if (mhdr->ext & 0x1) { |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1915 | struct ext_hdr_v0 *ext_hdr = (void *)(mhdr + 1); |
Pali Rohár | fe2c0e2 | 2021-07-23 11:14:01 +0200 | [diff] [blame] | 1916 | |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 1917 | csum = image_checksum8(ext_hdr, sizeof(*ext_hdr) - 1); |
| 1918 | if (csum != ext_hdr->checksum) |
Pali Rohár | fe2c0e2 | 2021-07-23 11:14:01 +0200 | [diff] [blame] | 1919 | return -FDT_ERR_BADSTRUCTURE; |
| 1920 | } |
Pali Rohár | 700ea98 | 2021-11-08 18:12:44 +0100 | [diff] [blame] | 1921 | |
| 1922 | blockid = mhdr->blockid; |
| 1923 | offset = le32_to_cpu(mhdr->srcaddr); |
| 1924 | size = le32_to_cpu(mhdr->blocksize); |
Marek Behún | acb0b38 | 2021-09-24 23:07:00 +0200 | [diff] [blame] | 1925 | } else if (kwbimage_version(ptr) == 1) { |
Pali Rohár | 9380445 | 2021-07-23 11:14:02 +0200 | [diff] [blame] | 1926 | struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr; |
Marek Behún | 732c930 | 2021-08-18 00:59:15 +0200 | [diff] [blame] | 1927 | const uint8_t *mhdr_end; |
| 1928 | struct opt_hdr_v1 *ohdr; |
Pali Rohár | 9380445 | 2021-07-23 11:14:02 +0200 | [diff] [blame] | 1929 | |
Marek Behún | 732c930 | 2021-08-18 00:59:15 +0200 | [diff] [blame] | 1930 | mhdr_end = (uint8_t *)mhdr + header_size; |
| 1931 | for_each_opt_hdr_v1 (ohdr, ptr) |
| 1932 | if (!opt_hdr_v1_valid_size(ohdr, mhdr_end)) |
| 1933 | return -FDT_ERR_BADSTRUCTURE; |
Pali Rohár | e0c243c | 2021-07-23 11:14:03 +0200 | [diff] [blame] | 1934 | |
Pali Rohár | 700ea98 | 2021-11-08 18:12:44 +0100 | [diff] [blame] | 1935 | blockid = mhdr->blockid; |
Pali Rohár | e0c243c | 2021-07-23 11:14:03 +0200 | [diff] [blame] | 1936 | offset = le32_to_cpu(mhdr->srcaddr); |
Pali Rohár | e0c243c | 2021-07-23 11:14:03 +0200 | [diff] [blame] | 1937 | size = le32_to_cpu(mhdr->blocksize); |
Pali Rohár | b984056 | 2021-08-11 10:14:14 +0200 | [diff] [blame] | 1938 | } else { |
| 1939 | return -FDT_ERR_BADSTRUCTURE; |
Pali Rohár | 9380445 | 2021-07-23 11:14:02 +0200 | [diff] [blame] | 1940 | } |
| 1941 | |
Pali Rohár | 700ea98 | 2021-11-08 18:12:44 +0100 | [diff] [blame] | 1942 | /* |
| 1943 | * For SATA srcaddr is specified in number of sectors. |
| 1944 | * The main header is must be stored at sector number 1. |
| 1945 | * This expects that sector size is 512 bytes and recalculates |
| 1946 | * data offset to bytes relative to the main header. |
| 1947 | */ |
| 1948 | if (blockid == IBR_HDR_SATA_ID) { |
| 1949 | if (offset < 1) |
| 1950 | return -FDT_ERR_BADSTRUCTURE; |
| 1951 | offset -= 1; |
| 1952 | offset *= 512; |
| 1953 | } |
| 1954 | |
| 1955 | /* |
| 1956 | * For SDIO srcaddr is specified in number of sectors. |
| 1957 | * This expects that sector size is 512 bytes and recalculates |
| 1958 | * data offset to bytes. |
| 1959 | */ |
| 1960 | if (blockid == IBR_HDR_SDIO_ID) |
| 1961 | offset *= 512; |
| 1962 | |
| 1963 | /* |
| 1964 | * For PCIe srcaddr is always set to 0xFFFFFFFF. |
| 1965 | * This expects that data starts after all headers. |
| 1966 | */ |
| 1967 | if (blockid == IBR_HDR_PEX_ID && offset == 0xFFFFFFFF) |
| 1968 | offset = header_size; |
| 1969 | |
| 1970 | if (offset > image_size || offset % 4 != 0) |
| 1971 | return -FDT_ERR_BADSTRUCTURE; |
| 1972 | |
| 1973 | if (size < 4 || offset + size > image_size || size % 4 != 0) |
| 1974 | return -FDT_ERR_BADSTRUCTURE; |
| 1975 | |
| 1976 | if (image_checksum32(ptr + offset, size - 4) != |
| 1977 | *(uint32_t *)(ptr + offset + size - 4)) |
| 1978 | return -FDT_ERR_BADSTRUCTURE; |
| 1979 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1980 | return 0; |
| 1981 | } |
| 1982 | |
| 1983 | static int kwbimage_generate(struct image_tool_params *params, |
| 1984 | struct image_type_params *tparams) |
| 1985 | { |
Patrick Wildt | 6cbf7ed | 2017-05-10 22:18:54 +0200 | [diff] [blame] | 1986 | FILE *fcfg; |
Pali Rohár | 37cb9c1 | 2021-07-23 11:13:56 +0200 | [diff] [blame] | 1987 | struct stat s; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1988 | int alloc_len; |
Pali Rohár | c934aad | 2021-07-23 11:13:57 +0200 | [diff] [blame] | 1989 | int bootfrom; |
Patrick Wildt | 6cbf7ed | 2017-05-10 22:18:54 +0200 | [diff] [blame] | 1990 | int version; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1991 | void *hdr; |
Patrick Wildt | 6cbf7ed | 2017-05-10 22:18:54 +0200 | [diff] [blame] | 1992 | int ret; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 1993 | |
Patrick Wildt | 6cbf7ed | 2017-05-10 22:18:54 +0200 | [diff] [blame] | 1994 | fcfg = fopen(params->imagename, "r"); |
| 1995 | if (!fcfg) { |
| 1996 | fprintf(stderr, "Could not open input file %s\n", |
| 1997 | params->imagename); |
| 1998 | exit(EXIT_FAILURE); |
| 1999 | } |
| 2000 | |
Pali Rohár | 37cb9c1 | 2021-07-23 11:13:56 +0200 | [diff] [blame] | 2001 | if (stat(params->datafile, &s)) { |
| 2002 | fprintf(stderr, "Could not stat data file %s: %s\n", |
| 2003 | params->datafile, strerror(errno)); |
| 2004 | exit(EXIT_FAILURE); |
| 2005 | } |
| 2006 | |
Patrick Wildt | 6cbf7ed | 2017-05-10 22:18:54 +0200 | [diff] [blame] | 2007 | image_cfg = malloc(IMAGE_CFG_ELEMENT_MAX * |
| 2008 | sizeof(struct image_cfg_element)); |
| 2009 | if (!image_cfg) { |
| 2010 | fprintf(stderr, "Cannot allocate memory\n"); |
| 2011 | fclose(fcfg); |
| 2012 | exit(EXIT_FAILURE); |
| 2013 | } |
| 2014 | |
| 2015 | memset(image_cfg, 0, |
| 2016 | IMAGE_CFG_ELEMENT_MAX * sizeof(struct image_cfg_element)); |
| 2017 | rewind(fcfg); |
| 2018 | |
| 2019 | ret = image_create_config_parse(fcfg); |
| 2020 | fclose(fcfg); |
| 2021 | if (ret) { |
| 2022 | free(image_cfg); |
| 2023 | exit(EXIT_FAILURE); |
| 2024 | } |
| 2025 | |
Pali Rohár | c934aad | 2021-07-23 11:13:57 +0200 | [diff] [blame] | 2026 | bootfrom = image_get_bootfrom(); |
Patrick Wildt | 6cbf7ed | 2017-05-10 22:18:54 +0200 | [diff] [blame] | 2027 | version = image_get_version(); |
| 2028 | switch (version) { |
| 2029 | /* |
| 2030 | * Fallback to version 0 if no version is provided in the |
| 2031 | * cfg file |
| 2032 | */ |
| 2033 | case -1: |
| 2034 | case 0: |
Pali Rohár | 851114b | 2021-11-08 18:12:50 +0100 | [diff] [blame] | 2035 | alloc_len = image_headersz_v0(NULL); |
Patrick Wildt | 6cbf7ed | 2017-05-10 22:18:54 +0200 | [diff] [blame] | 2036 | break; |
| 2037 | |
| 2038 | case 1: |
Mario Six | e93cf53 | 2017-01-11 16:00:57 +0100 | [diff] [blame] | 2039 | alloc_len = image_headersz_v1(NULL); |
Pali Rohár | 252e7c3 | 2022-01-12 18:20:42 +0100 | [diff] [blame] | 2040 | if (!alloc_len) { |
| 2041 | free(image_cfg); |
| 2042 | exit(EXIT_FAILURE); |
| 2043 | } |
Pali Rohár | 78d997f | 2022-01-12 18:20:43 +0100 | [diff] [blame] | 2044 | if (alloc_len > 192*1024) { |
| 2045 | fprintf(stderr, "Header is too big (%u bytes), maximal kwbimage header size is %u bytes\n", alloc_len, 192*1024); |
| 2046 | free(image_cfg); |
| 2047 | exit(EXIT_FAILURE); |
| 2048 | } |
Patrick Wildt | 6cbf7ed | 2017-05-10 22:18:54 +0200 | [diff] [blame] | 2049 | break; |
| 2050 | |
| 2051 | default: |
| 2052 | fprintf(stderr, "Unsupported version %d\n", version); |
| 2053 | free(image_cfg); |
| 2054 | exit(EXIT_FAILURE); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 2055 | } |
| 2056 | |
Patrick Wildt | 6cbf7ed | 2017-05-10 22:18:54 +0200 | [diff] [blame] | 2057 | free(image_cfg); |
| 2058 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 2059 | hdr = malloc(alloc_len); |
| 2060 | if (!hdr) { |
| 2061 | fprintf(stderr, "%s: malloc return failure: %s\n", |
| 2062 | params->cmdname, strerror(errno)); |
| 2063 | exit(EXIT_FAILURE); |
| 2064 | } |
| 2065 | |
| 2066 | memset(hdr, 0, alloc_len); |
| 2067 | tparams->header_size = alloc_len; |
| 2068 | tparams->hdr = hdr; |
| 2069 | |
Stefan Roese | 7772085 | 2015-11-24 09:14:59 +0100 | [diff] [blame] | 2070 | /* |
| 2071 | * The resulting image needs to be 4-byte aligned. At least |
| 2072 | * the Marvell hdrparser tool complains if its unaligned. |
Pali Rohár | 37cb9c1 | 2021-07-23 11:13:56 +0200 | [diff] [blame] | 2073 | * After the image data is stored 4-byte checksum. |
Pali Rohár | 188099e | 2021-11-08 18:12:46 +0100 | [diff] [blame] | 2074 | * Final UART image must be aligned to 128 bytes. |
Pali Rohár | c934aad | 2021-07-23 11:13:57 +0200 | [diff] [blame] | 2075 | * Final SPI and NAND images must be aligned to 256 bytes. |
Pali Rohár | 501a54a | 2021-07-23 11:13:59 +0200 | [diff] [blame] | 2076 | * Final SATA and SDIO images must be aligned to 512 bytes. |
Stefan Roese | 7772085 | 2015-11-24 09:14:59 +0100 | [diff] [blame] | 2077 | */ |
Pali Rohár | c934aad | 2021-07-23 11:13:57 +0200 | [diff] [blame] | 2078 | if (bootfrom == IBR_HDR_SPI_ID || bootfrom == IBR_HDR_NAND_ID) |
| 2079 | return 4 + (256 - (alloc_len + s.st_size + 4) % 256) % 256; |
Pali Rohár | 501a54a | 2021-07-23 11:13:59 +0200 | [diff] [blame] | 2080 | else if (bootfrom == IBR_HDR_SATA_ID || bootfrom == IBR_HDR_SDIO_ID) |
| 2081 | return 4 + (512 - (alloc_len + s.st_size + 4) % 512) % 512; |
Pali Rohár | 188099e | 2021-11-08 18:12:46 +0100 | [diff] [blame] | 2082 | else if (bootfrom == IBR_HDR_UART_ID) |
| 2083 | return 4 + (128 - (alloc_len + s.st_size + 4) % 128) % 128; |
Pali Rohár | c934aad | 2021-07-23 11:13:57 +0200 | [diff] [blame] | 2084 | else |
| 2085 | return 4 + (4 - s.st_size % 4) % 4; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
Pali Rohár | aa6943c | 2021-07-23 11:14:34 +0200 | [diff] [blame] | 2088 | static int kwbimage_extract_subimage(void *ptr, struct image_tool_params *params) |
| 2089 | { |
| 2090 | struct main_hdr_v1 *mhdr = (struct main_hdr_v1 *)ptr; |
Marek Behún | fe2fd73 | 2021-09-24 23:07:01 +0200 | [diff] [blame] | 2091 | size_t header_size = kwbheader_size(ptr); |
Marek Behún | 732c930 | 2021-08-18 00:59:15 +0200 | [diff] [blame] | 2092 | struct opt_hdr_v1 *ohdr; |
Pali Rohár | aa6943c | 2021-07-23 11:14:34 +0200 | [diff] [blame] | 2093 | int idx = params->pflag; |
| 2094 | int cur_idx = 0; |
| 2095 | uint32_t offset; |
| 2096 | ulong image; |
| 2097 | ulong size; |
| 2098 | |
Marek Behún | 732c930 | 2021-08-18 00:59:15 +0200 | [diff] [blame] | 2099 | for_each_opt_hdr_v1 (ohdr, ptr) { |
| 2100 | if (ohdr->headertype != OPT_HDR_V1_BINARY_TYPE) |
| 2101 | continue; |
Pali Rohár | aa6943c | 2021-07-23 11:14:34 +0200 | [diff] [blame] | 2102 | |
Marek Behún | 732c930 | 2021-08-18 00:59:15 +0200 | [diff] [blame] | 2103 | if (idx == cur_idx) { |
| 2104 | image = (ulong)&ohdr->data[4 + 4 * ohdr->data[0]]; |
| 2105 | size = opt_hdr_v1_size(ohdr) - 12 - 4 * ohdr->data[0]; |
| 2106 | goto extract; |
Pali Rohár | aa6943c | 2021-07-23 11:14:34 +0200 | [diff] [blame] | 2107 | } |
Marek Behún | 732c930 | 2021-08-18 00:59:15 +0200 | [diff] [blame] | 2108 | |
| 2109 | ++cur_idx; |
Pali Rohár | aa6943c | 2021-07-23 11:14:34 +0200 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | if (idx != cur_idx) { |
| 2113 | printf("Image %d is not present\n", idx); |
| 2114 | return -1; |
| 2115 | } |
| 2116 | |
| 2117 | offset = le32_to_cpu(mhdr->srcaddr); |
| 2118 | |
| 2119 | if (mhdr->blockid == IBR_HDR_SATA_ID) { |
| 2120 | offset -= 1; |
| 2121 | offset *= 512; |
| 2122 | } |
| 2123 | |
| 2124 | if (mhdr->blockid == IBR_HDR_SDIO_ID) |
| 2125 | offset *= 512; |
| 2126 | |
| 2127 | if (mhdr->blockid == IBR_HDR_PEX_ID && offset == 0xFFFFFFFF) |
| 2128 | offset = header_size; |
| 2129 | |
| 2130 | image = (ulong)((uint8_t *)ptr + offset); |
| 2131 | size = le32_to_cpu(mhdr->blocksize) - 4; |
| 2132 | |
| 2133 | extract: |
| 2134 | return imagetool_save_subimage(params->outfile, image, size); |
| 2135 | } |
| 2136 | |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 2137 | /* |
| 2138 | * Report Error if xflag is set in addition to default |
| 2139 | */ |
| 2140 | static int kwbimage_check_params(struct image_tool_params *params) |
| 2141 | { |
Pali Rohár | aa6943c | 2021-07-23 11:14:34 +0200 | [diff] [blame] | 2142 | if (!params->iflag && (!params->imagename || !strlen(params->imagename))) { |
Mario Six | 94490a4 | 2017-01-11 16:00:54 +0100 | [diff] [blame] | 2143 | char *msg = "Configuration file for kwbimage creation omitted"; |
| 2144 | |
| 2145 | fprintf(stderr, "Error:%s - %s\n", params->cmdname, msg); |
Pali Rohár | 56087c1 | 2021-11-08 18:12:45 +0100 | [diff] [blame] | 2146 | return 1; |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | return (params->dflag && (params->fflag || params->lflag)) || |
| 2150 | (params->fflag && (params->dflag || params->lflag)) || |
| 2151 | (params->lflag && (params->dflag || params->fflag)) || |
Pali Rohár | aa6943c | 2021-07-23 11:14:34 +0200 | [diff] [blame] | 2152 | (params->xflag); |
Stefan Roese | 4acd2d2 | 2014-10-22 12:13:23 +0200 | [diff] [blame] | 2153 | } |
| 2154 | |
Prafulla Wadaskar | aa0c7a8 | 2009-09-07 15:05:02 +0530 | [diff] [blame] | 2155 | /* |
| 2156 | * kwbimage type parameters definition |
| 2157 | */ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 2158 | U_BOOT_IMAGE_TYPE( |
| 2159 | kwbimage, |
| 2160 | "Marvell MVEBU Boot Image support", |
| 2161 | 0, |
| 2162 | NULL, |
| 2163 | kwbimage_check_params, |
| 2164 | kwbimage_verify_header, |
| 2165 | kwbimage_print_header, |
| 2166 | kwbimage_set_header, |
Pali Rohár | aa6943c | 2021-07-23 11:14:34 +0200 | [diff] [blame] | 2167 | kwbimage_extract_subimage, |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 2168 | kwbimage_check_image_types, |
| 2169 | NULL, |
| 2170 | kwbimage_generate |
| 2171 | ); |