Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Marvell International Ltd. |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 4 | * https://spdx.org/licenses |
| 5 | */ |
| 6 | |
| 7 | #include <config.h> |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 10 | #include <env.h> |
Simon Glass | 85f1378 | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 11 | #include <image.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 12 | #include <net.h> |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 13 | #include <vsprintf.h> |
| 14 | #include <errno.h> |
| 15 | #include <dm.h> |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 16 | #include <fuse.h> |
| 17 | #include <mach/efuse.h> |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 18 | |
| 19 | #include <spi_flash.h> |
| 20 | #include <spi.h> |
| 21 | #include <nand.h> |
| 22 | #include <usb.h> |
| 23 | #include <fs.h> |
| 24 | #include <mmc.h> |
Konstantin Porotchkin | a89c055 | 2017-01-08 16:52:06 +0200 | [diff] [blame] | 25 | #ifdef CONFIG_BLK |
| 26 | #include <blk.h> |
| 27 | #endif |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 28 | #include <u-boot/sha1.h> |
| 29 | #include <u-boot/sha256.h> |
Pali Rohár | a4bbb2c | 2022-07-26 16:11:58 +0200 | [diff] [blame] | 30 | #include <u-boot/sha512.h> |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 31 | |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 32 | #if defined(CONFIG_ARMADA_8K) |
| 33 | #define MAIN_HDR_MAGIC 0xB105B002 |
| 34 | |
| 35 | struct mvebu_image_header { |
| 36 | u32 magic; /* 0-3 */ |
| 37 | u32 prolog_size; /* 4-7 */ |
| 38 | u32 prolog_checksum; /* 8-11 */ |
| 39 | u32 boot_image_size; /* 12-15 */ |
| 40 | u32 boot_image_checksum; /* 16-19 */ |
| 41 | u32 rsrvd0; /* 20-23 */ |
| 42 | u32 load_addr; /* 24-27 */ |
| 43 | u32 exec_addr; /* 28-31 */ |
| 44 | u8 uart_cfg; /* 32 */ |
| 45 | u8 baudrate; /* 33 */ |
| 46 | u8 ext_count; /* 34 */ |
| 47 | u8 aux_flags; /* 35 */ |
| 48 | u32 io_arg_0; /* 36-39 */ |
| 49 | u32 io_arg_1; /* 40-43 */ |
| 50 | u32 io_arg_2; /* 43-47 */ |
| 51 | u32 io_arg_3; /* 48-51 */ |
| 52 | u32 rsrvd1; /* 52-55 */ |
| 53 | u32 rsrvd2; /* 56-59 */ |
| 54 | u32 rsrvd3; /* 60-63 */ |
| 55 | }; |
| 56 | #elif defined(CONFIG_ARMADA_3700) /* A3700 */ |
| 57 | #define HASH_SUM_LEN 16 |
| 58 | #define IMAGE_VERSION_3_6_0 0x030600 |
| 59 | #define IMAGE_VERSION_3_5_0 0x030500 |
| 60 | |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 61 | struct tim_boot_flash_sign { |
| 62 | unsigned int id; |
| 63 | const char *name; |
| 64 | }; |
| 65 | |
| 66 | struct tim_boot_flash_sign tim_boot_flash_signs[] = { |
| 67 | { 0x454d4d08, "mmc" }, |
| 68 | { 0x454d4d0b, "mmc" }, |
| 69 | { 0x5350490a, "spi" }, |
| 70 | { 0x5350491a, "nand" }, |
| 71 | { 0x55415223, "uart" }, |
| 72 | { 0x53415432, "sata" }, |
| 73 | {}, |
| 74 | }; |
| 75 | |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 76 | struct common_tim_data { |
| 77 | u32 version; |
| 78 | u32 identifier; |
| 79 | u32 trusted; |
| 80 | u32 issue_date; |
| 81 | u32 oem_unique_id; |
| 82 | u32 reserved[5]; /* Reserve 20 bytes */ |
| 83 | u32 boot_flash_sign; |
| 84 | u32 num_images; |
| 85 | u32 num_keys; |
| 86 | u32 size_of_reserved; |
| 87 | }; |
| 88 | |
| 89 | struct mvebu_image_info { |
| 90 | u32 image_id; |
| 91 | u32 next_image_id; |
| 92 | u32 flash_entry_addr; |
| 93 | u32 load_addr; |
| 94 | u32 image_size; |
| 95 | u32 image_size_to_hash; |
| 96 | u32 hash_algorithm_id; |
| 97 | u32 hash[HASH_SUM_LEN]; /* Reserve 512 bits for the hash */ |
| 98 | u32 partition_number; |
| 99 | u32 enc_algorithm_id; |
| 100 | u32 encrypt_start_offset; |
| 101 | u32 encrypt_size; |
| 102 | }; |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 103 | #elif defined(CONFIG_ARMADA_32BIT) |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 104 | |
Pali Rohár | 0f7df22 | 2021-10-22 12:41:10 +0200 | [diff] [blame] | 105 | /* Structure of the main header, version 1 (Armada 370/XP/375/38x/39x) */ |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 106 | struct a38x_main_hdr_v1 { |
| 107 | u8 blockid; /* 0x0 */ |
| 108 | u8 flags; /* 0x1 */ |
Pali Rohár | 7fea884 | 2021-10-22 12:37:48 +0200 | [diff] [blame] | 109 | u16 nandpagesize; /* 0x2-0x3 */ |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 110 | u32 blocksize; /* 0x4-0x7 */ |
| 111 | u8 version; /* 0x8 */ |
| 112 | u8 headersz_msb; /* 0x9 */ |
| 113 | u16 headersz_lsb; /* 0xA-0xB */ |
| 114 | u32 srcaddr; /* 0xC-0xF */ |
| 115 | u32 destaddr; /* 0x10-0x13 */ |
| 116 | u32 execaddr; /* 0x14-0x17 */ |
| 117 | u8 options; /* 0x18 */ |
| 118 | u8 nandblocksize; /* 0x19 */ |
| 119 | u8 nandbadblklocation; /* 0x1A */ |
| 120 | u8 reserved4; /* 0x1B */ |
| 121 | u16 reserved5; /* 0x1C-0x1D */ |
| 122 | u8 ext; /* 0x1E */ |
| 123 | u8 checksum; /* 0x1F */ |
| 124 | }; |
Joel Johnson | ab5dd30 | 2020-04-17 09:38:06 -0600 | [diff] [blame] | 125 | |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 126 | /* |
| 127 | * Header for the optional headers, version 1 (Armada 370/XP/375/38x/39x) |
| 128 | */ |
| 129 | struct a38x_opt_hdr_v1 { |
| 130 | u8 headertype; |
| 131 | u8 headersz_msb; |
| 132 | u16 headersz_lsb; |
| 133 | u8 data[0]; |
| 134 | }; |
| 135 | #define A38X_OPT_HDR_V1_SECURE_TYPE 0x1 |
| 136 | |
Joel Johnson | ab5dd30 | 2020-04-17 09:38:06 -0600 | [diff] [blame] | 137 | struct a38x_boot_mode { |
| 138 | unsigned int id; |
| 139 | const char *name; |
| 140 | }; |
| 141 | |
| 142 | /* The blockid header field values used to indicate boot device of image */ |
| 143 | struct a38x_boot_mode a38x_boot_modes[] = { |
| 144 | { 0x4D, "i2c" }, |
| 145 | { 0x5A, "spi" }, |
| 146 | { 0x69, "uart" }, |
| 147 | { 0x78, "sata" }, |
| 148 | { 0x8B, "nand" }, |
| 149 | { 0x9C, "pex" }, |
| 150 | { 0xAE, "mmc" }, |
| 151 | {}, |
| 152 | }; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 153 | |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 154 | #endif |
| 155 | |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 156 | struct bubt_dev { |
| 157 | char name[8]; |
| 158 | size_t (*read)(const char *file_name); |
| 159 | int (*write)(size_t image_size); |
| 160 | int (*active)(void); |
| 161 | }; |
| 162 | |
| 163 | static ulong get_load_addr(void) |
| 164 | { |
| 165 | const char *addr_str; |
| 166 | unsigned long addr; |
| 167 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 168 | addr_str = env_get("loadaddr"); |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 169 | if (addr_str) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 170 | addr = hextoul(addr_str, NULL); |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 171 | else |
| 172 | addr = CONFIG_SYS_LOAD_ADDR; |
| 173 | |
| 174 | return addr; |
| 175 | } |
| 176 | |
| 177 | /******************************************************************** |
| 178 | * eMMC services |
| 179 | ********************************************************************/ |
Jean-Jacques Hiblot | d053167 | 2018-01-04 15:23:32 +0100 | [diff] [blame] | 180 | #if CONFIG_IS_ENABLED(DM_MMC) && CONFIG_IS_ENABLED(MMC_WRITE) |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 181 | static int mmc_burn_image(size_t image_size) |
| 182 | { |
| 183 | struct mmc *mmc; |
| 184 | lbaint_t start_lba; |
| 185 | lbaint_t blk_count; |
| 186 | ulong blk_written; |
| 187 | int err; |
| 188 | const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV; |
Konstantin Porotchkin | a89c055 | 2017-01-08 16:52:06 +0200 | [diff] [blame] | 189 | #ifdef CONFIG_BLK |
| 190 | struct blk_desc *blk_desc; |
| 191 | #endif |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 192 | mmc = find_mmc_device(mmc_dev_num); |
| 193 | if (!mmc) { |
| 194 | printf("No SD/MMC/eMMC card found\n"); |
| 195 | return -ENOMEDIUM; |
| 196 | } |
| 197 | |
| 198 | err = mmc_init(mmc); |
| 199 | if (err) { |
| 200 | printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC", |
| 201 | mmc_dev_num); |
| 202 | return err; |
| 203 | } |
| 204 | |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 205 | /* SD reserves LBA-0 for MBR and boots from LBA-1, |
| 206 | * MMC/eMMC boots from LBA-0 |
| 207 | */ |
| 208 | start_lba = IS_SD(mmc) ? 1 : 0; |
Konstantin Porotchkin | a89c055 | 2017-01-08 16:52:06 +0200 | [diff] [blame] | 209 | #ifdef CONFIG_BLK |
| 210 | blk_count = image_size / mmc->write_bl_len; |
| 211 | if (image_size % mmc->write_bl_len) |
| 212 | blk_count += 1; |
| 213 | |
| 214 | blk_desc = mmc_get_blk_desc(mmc); |
| 215 | if (!blk_desc) { |
| 216 | printf("Error - failed to obtain block descriptor\n"); |
| 217 | return -ENODEV; |
| 218 | } |
| 219 | blk_written = blk_dwrite(blk_desc, start_lba, blk_count, |
| 220 | (void *)get_load_addr()); |
| 221 | #else |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 222 | blk_count = image_size / mmc->block_dev.blksz; |
| 223 | if (image_size % mmc->block_dev.blksz) |
| 224 | blk_count += 1; |
| 225 | |
| 226 | blk_written = mmc->block_dev.block_write(mmc_dev_num, |
Konstantin Porotchkin | a89c055 | 2017-01-08 16:52:06 +0200 | [diff] [blame] | 227 | start_lba, blk_count, |
| 228 | (void *)get_load_addr()); |
| 229 | #endif /* CONFIG_BLK */ |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 230 | if (blk_written != blk_count) { |
| 231 | printf("Error - written %#lx blocks\n", blk_written); |
| 232 | return -ENOSPC; |
| 233 | } |
| 234 | printf("Done!\n"); |
| 235 | |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static size_t mmc_read_file(const char *file_name) |
| 240 | { |
| 241 | loff_t act_read = 0; |
| 242 | int rc; |
| 243 | struct mmc *mmc; |
| 244 | const u8 mmc_dev_num = CONFIG_SYS_MMC_ENV_DEV; |
| 245 | |
| 246 | mmc = find_mmc_device(mmc_dev_num); |
| 247 | if (!mmc) { |
| 248 | printf("No SD/MMC/eMMC card found\n"); |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | if (mmc_init(mmc)) { |
| 253 | printf("%s(%d) init failed\n", IS_SD(mmc) ? "SD" : "MMC", |
| 254 | mmc_dev_num); |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | /* Load from data partition (0) */ |
| 259 | if (fs_set_blk_dev("mmc", "0", FS_TYPE_ANY)) { |
| 260 | printf("Error: MMC 0 not found\n"); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | /* Perfrom file read */ |
| 265 | rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read); |
| 266 | if (rc) |
| 267 | return 0; |
| 268 | |
| 269 | return act_read; |
| 270 | } |
| 271 | |
| 272 | static int is_mmc_active(void) |
| 273 | { |
| 274 | return 1; |
| 275 | } |
| 276 | #else /* CONFIG_DM_MMC */ |
| 277 | static int mmc_burn_image(size_t image_size) |
| 278 | { |
| 279 | return -ENODEV; |
| 280 | } |
| 281 | |
| 282 | static size_t mmc_read_file(const char *file_name) |
| 283 | { |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int is_mmc_active(void) |
| 288 | { |
| 289 | return 0; |
| 290 | } |
| 291 | #endif /* CONFIG_DM_MMC */ |
| 292 | |
| 293 | /******************************************************************** |
| 294 | * SPI services |
| 295 | ********************************************************************/ |
| 296 | #ifdef CONFIG_SPI_FLASH |
| 297 | static int spi_burn_image(size_t image_size) |
| 298 | { |
| 299 | int ret; |
| 300 | struct spi_flash *flash; |
| 301 | u32 erase_bytes; |
| 302 | |
| 303 | /* Probe the SPI bus to get the flash device */ |
Tom Rini | 119d2fb | 2021-12-11 14:55:48 -0500 | [diff] [blame] | 304 | flash = spi_flash_probe(CONFIG_SF_DEFAULT_BUS, |
| 305 | CONFIG_SF_DEFAULT_CS, |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 306 | CONFIG_SF_DEFAULT_SPEED, |
| 307 | CONFIG_SF_DEFAULT_MODE); |
| 308 | if (!flash) { |
| 309 | printf("Failed to probe SPI Flash\n"); |
| 310 | return -ENOMEDIUM; |
| 311 | } |
| 312 | |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 313 | erase_bytes = image_size + |
| 314 | (flash->erase_size - image_size % flash->erase_size); |
| 315 | printf("Erasing %d bytes (%d blocks) at offset 0 ...", |
| 316 | erase_bytes, erase_bytes / flash->erase_size); |
| 317 | ret = spi_flash_erase(flash, 0, erase_bytes); |
| 318 | if (ret) |
| 319 | printf("Error!\n"); |
| 320 | else |
| 321 | printf("Done!\n"); |
| 322 | |
| 323 | printf("Writing %d bytes from 0x%lx to offset 0 ...", |
| 324 | (int)image_size, get_load_addr()); |
| 325 | ret = spi_flash_write(flash, 0, image_size, (void *)get_load_addr()); |
| 326 | if (ret) |
| 327 | printf("Error!\n"); |
| 328 | else |
| 329 | printf("Done!\n"); |
| 330 | |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static int is_spi_active(void) |
| 335 | { |
| 336 | return 1; |
| 337 | } |
| 338 | |
| 339 | #else /* CONFIG_SPI_FLASH */ |
| 340 | static int spi_burn_image(size_t image_size) |
| 341 | { |
| 342 | return -ENODEV; |
| 343 | } |
| 344 | |
| 345 | static int is_spi_active(void) |
| 346 | { |
| 347 | return 0; |
| 348 | } |
| 349 | #endif /* CONFIG_SPI_FLASH */ |
| 350 | |
| 351 | /******************************************************************** |
| 352 | * NAND services |
| 353 | ********************************************************************/ |
| 354 | #ifdef CONFIG_CMD_NAND |
| 355 | static int nand_burn_image(size_t image_size) |
| 356 | { |
Konstantin Porotchkin | c6db27b | 2017-03-28 18:16:56 +0300 | [diff] [blame] | 357 | int ret; |
| 358 | uint32_t block_size; |
Grygorii Strashko | 4a240bc | 2017-06-26 19:13:02 -0500 | [diff] [blame] | 359 | struct mtd_info *mtd; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 360 | |
Grygorii Strashko | 4a240bc | 2017-06-26 19:13:02 -0500 | [diff] [blame] | 361 | mtd = get_nand_dev_by_index(nand_curr_device); |
| 362 | if (!mtd) { |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 363 | puts("\nno devices available\n"); |
| 364 | return -ENOMEDIUM; |
| 365 | } |
Grygorii Strashko | 4a240bc | 2017-06-26 19:13:02 -0500 | [diff] [blame] | 366 | block_size = mtd->erasesize; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 367 | |
| 368 | /* Align U-Boot size to currently used blocksize */ |
| 369 | image_size = ((image_size + (block_size - 1)) & (~(block_size - 1))); |
| 370 | |
Joel Johnson | 22d78e4 | 2020-04-17 09:38:05 -0600 | [diff] [blame] | 371 | /* Erase the U-Boot image space */ |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 372 | printf("Erasing 0x%x - 0x%x:...", 0, (int)image_size); |
Grygorii Strashko | 4a240bc | 2017-06-26 19:13:02 -0500 | [diff] [blame] | 373 | ret = nand_erase(mtd, 0, image_size); |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 374 | if (ret) { |
| 375 | printf("Error!\n"); |
| 376 | goto error; |
| 377 | } |
| 378 | printf("Done!\n"); |
| 379 | |
| 380 | /* Write the image to flash */ |
Konstantin Porotchkin | c6db27b | 2017-03-28 18:16:56 +0300 | [diff] [blame] | 381 | printf("Writing %d bytes from 0x%lx to offset 0 ... ", |
| 382 | (int)image_size, get_load_addr()); |
Grygorii Strashko | 4a240bc | 2017-06-26 19:13:02 -0500 | [diff] [blame] | 383 | ret = nand_write(mtd, 0, &image_size, (void *)get_load_addr()); |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 384 | if (ret) |
| 385 | printf("Error!\n"); |
| 386 | else |
| 387 | printf("Done!\n"); |
| 388 | |
| 389 | error: |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | static int is_nand_active(void) |
| 394 | { |
| 395 | return 1; |
| 396 | } |
| 397 | |
| 398 | #else /* CONFIG_CMD_NAND */ |
| 399 | static int nand_burn_image(size_t image_size) |
| 400 | { |
| 401 | return -ENODEV; |
| 402 | } |
| 403 | |
| 404 | static int is_nand_active(void) |
| 405 | { |
| 406 | return 0; |
| 407 | } |
| 408 | #endif /* CONFIG_CMD_NAND */ |
| 409 | |
| 410 | /******************************************************************** |
| 411 | * USB services |
| 412 | ********************************************************************/ |
| 413 | #if defined(CONFIG_USB_STORAGE) && defined(CONFIG_BLK) |
| 414 | static size_t usb_read_file(const char *file_name) |
| 415 | { |
| 416 | loff_t act_read = 0; |
| 417 | struct udevice *dev; |
| 418 | int rc; |
| 419 | |
| 420 | usb_stop(); |
| 421 | |
| 422 | if (usb_init() < 0) { |
| 423 | printf("Error: usb_init failed\n"); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* Try to recognize storage devices immediately */ |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 428 | blk_first_device(UCLASS_USB, &dev); |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 429 | if (!dev) { |
| 430 | printf("Error: USB storage device not found\n"); |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | /* Always load from usb 0 */ |
| 435 | if (fs_set_blk_dev("usb", "0", FS_TYPE_ANY)) { |
| 436 | printf("Error: USB 0 not found\n"); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /* Perfrom file read */ |
| 441 | rc = fs_read(file_name, get_load_addr(), 0, 0, &act_read); |
| 442 | if (rc) |
| 443 | return 0; |
| 444 | |
| 445 | return act_read; |
| 446 | } |
| 447 | |
| 448 | static int is_usb_active(void) |
| 449 | { |
| 450 | return 1; |
| 451 | } |
| 452 | |
| 453 | #else /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */ |
| 454 | static size_t usb_read_file(const char *file_name) |
| 455 | { |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int is_usb_active(void) |
| 460 | { |
| 461 | return 0; |
| 462 | } |
| 463 | #endif /* defined(CONFIG_USB_STORAGE) && defined (CONFIG_BLK) */ |
| 464 | |
| 465 | /******************************************************************** |
| 466 | * Network services |
| 467 | ********************************************************************/ |
| 468 | #ifdef CONFIG_CMD_NET |
| 469 | static size_t tftp_read_file(const char *file_name) |
| 470 | { |
Pali Rohár | 2c96d02 | 2022-07-26 16:11:57 +0200 | [diff] [blame] | 471 | int ret; |
| 472 | |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 473 | /* |
| 474 | * update global variable image_load_addr before tftp file from network |
| 475 | */ |
| 476 | image_load_addr = get_load_addr(); |
Pali Rohár | 2c96d02 | 2022-07-26 16:11:57 +0200 | [diff] [blame] | 477 | ret = net_loop(TFTPGET); |
| 478 | return ret > 0 ? ret : 0; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static int is_tftp_active(void) |
| 482 | { |
| 483 | return 1; |
| 484 | } |
| 485 | |
| 486 | #else |
| 487 | static size_t tftp_read_file(const char *file_name) |
| 488 | { |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static int is_tftp_active(void) |
| 493 | { |
| 494 | return 0; |
| 495 | } |
| 496 | #endif /* CONFIG_CMD_NET */ |
| 497 | |
| 498 | enum bubt_devices { |
| 499 | BUBT_DEV_NET = 0, |
| 500 | BUBT_DEV_USB, |
| 501 | BUBT_DEV_MMC, |
| 502 | BUBT_DEV_SPI, |
| 503 | BUBT_DEV_NAND, |
| 504 | |
| 505 | BUBT_MAX_DEV |
| 506 | }; |
| 507 | |
| 508 | struct bubt_dev bubt_devs[BUBT_MAX_DEV] = { |
| 509 | {"tftp", tftp_read_file, NULL, is_tftp_active}, |
| 510 | {"usb", usb_read_file, NULL, is_usb_active}, |
| 511 | {"mmc", mmc_read_file, mmc_burn_image, is_mmc_active}, |
| 512 | {"spi", NULL, spi_burn_image, is_spi_active}, |
| 513 | {"nand", NULL, nand_burn_image, is_nand_active}, |
| 514 | }; |
| 515 | |
| 516 | static int bubt_write_file(struct bubt_dev *dst, size_t image_size) |
| 517 | { |
| 518 | if (!dst->write) { |
| 519 | printf("Error: Write not supported on device %s\n", dst->name); |
| 520 | return -ENOTSUPP; |
| 521 | } |
| 522 | |
| 523 | return dst->write(image_size); |
| 524 | } |
| 525 | |
| 526 | #if defined(CONFIG_ARMADA_8K) |
| 527 | u32 do_checksum32(u32 *start, int32_t len) |
| 528 | { |
| 529 | u32 sum = 0; |
| 530 | u32 *startp = start; |
| 531 | |
| 532 | do { |
| 533 | sum += *startp; |
| 534 | startp++; |
| 535 | len -= 4; |
| 536 | } while (len > 0); |
| 537 | |
| 538 | return sum; |
| 539 | } |
| 540 | |
| 541 | static int check_image_header(void) |
| 542 | { |
| 543 | struct mvebu_image_header *hdr = |
| 544 | (struct mvebu_image_header *)get_load_addr(); |
| 545 | u32 header_len = hdr->prolog_size; |
| 546 | u32 checksum; |
| 547 | u32 checksum_ref = hdr->prolog_checksum; |
| 548 | |
| 549 | /* |
| 550 | * For now compare checksum, and magic. Later we can |
| 551 | * verify more stuff on the header like interface type, etc |
| 552 | */ |
| 553 | if (hdr->magic != MAIN_HDR_MAGIC) { |
| 554 | printf("ERROR: Bad MAGIC 0x%08x != 0x%08x\n", |
| 555 | hdr->magic, MAIN_HDR_MAGIC); |
| 556 | return -ENOEXEC; |
| 557 | } |
| 558 | |
| 559 | /* The checksum value is discarded from checksum calculation */ |
| 560 | hdr->prolog_checksum = 0; |
| 561 | |
| 562 | checksum = do_checksum32((u32 *)hdr, header_len); |
| 563 | if (checksum != checksum_ref) { |
| 564 | printf("Error: Bad Image checksum. 0x%x != 0x%x\n", |
| 565 | checksum, checksum_ref); |
| 566 | return -ENOEXEC; |
| 567 | } |
| 568 | |
| 569 | /* Restore the checksum before writing */ |
| 570 | hdr->prolog_checksum = checksum_ref; |
| 571 | printf("Image checksum...OK!\n"); |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | #elif defined(CONFIG_ARMADA_3700) /* Armada 3700 */ |
| 576 | static int check_image_header(void) |
| 577 | { |
| 578 | struct common_tim_data *hdr = (struct common_tim_data *)get_load_addr(); |
| 579 | int image_num; |
| 580 | u8 hash_160_output[SHA1_SUM_LEN]; |
| 581 | u8 hash_256_output[SHA256_SUM_LEN]; |
Pali Rohár | a4bbb2c | 2022-07-26 16:11:58 +0200 | [diff] [blame] | 582 | u8 hash_512_output[SHA512_SUM_LEN]; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 583 | sha1_context hash1_text; |
| 584 | sha256_context hash256_text; |
Pali Rohár | a4bbb2c | 2022-07-26 16:11:58 +0200 | [diff] [blame] | 585 | sha512_context hash512_text; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 586 | u8 *hash_output; |
| 587 | u32 hash_algorithm_id; |
| 588 | u32 image_size_to_hash; |
| 589 | u32 flash_entry_addr; |
| 590 | u32 *hash_value; |
| 591 | u32 internal_hash[HASH_SUM_LEN]; |
| 592 | const u8 *buff; |
| 593 | u32 num_of_image = hdr->num_images; |
| 594 | u32 version = hdr->version; |
| 595 | u32 trusted = hdr->trusted; |
| 596 | |
| 597 | /* bubt checksum validation only supports nontrusted images */ |
| 598 | if (trusted == 1) { |
| 599 | printf("bypass image validation, "); |
| 600 | printf("only untrusted image is supported now\n"); |
| 601 | return 0; |
| 602 | } |
| 603 | /* only supports image version 3.5 and 3.6 */ |
| 604 | if (version != IMAGE_VERSION_3_5_0 && version != IMAGE_VERSION_3_6_0) { |
| 605 | printf("Error: Unsupported Image version = 0x%08x\n", version); |
| 606 | return -ENOEXEC; |
| 607 | } |
| 608 | /* validate images hash value */ |
| 609 | for (image_num = 0; image_num < num_of_image; image_num++) { |
| 610 | struct mvebu_image_info *info = |
| 611 | (struct mvebu_image_info *)(get_load_addr() + |
| 612 | sizeof(struct common_tim_data) + |
| 613 | image_num * sizeof(struct mvebu_image_info)); |
| 614 | hash_algorithm_id = info->hash_algorithm_id; |
| 615 | image_size_to_hash = info->image_size_to_hash; |
| 616 | flash_entry_addr = info->flash_entry_addr; |
| 617 | hash_value = info->hash; |
| 618 | buff = (const u8 *)(get_load_addr() + flash_entry_addr); |
| 619 | |
| 620 | if (image_num == 0) { |
| 621 | /* |
| 622 | * The first image includes hash values in its content. |
| 623 | * For hash calculation, we need to save the original |
| 624 | * hash values to a local variable that will be |
| 625 | * copied back for comparsion and set all zeros to |
| 626 | * the orignal hash values for calculating new value. |
| 627 | * First image original format : |
| 628 | * x...x (datum1) x...x(orig. hash values) x...x(datum2) |
| 629 | * Replaced first image format : |
| 630 | * x...x (datum1) 0...0(hash values) x...x(datum2) |
| 631 | */ |
| 632 | memcpy(internal_hash, hash_value, |
| 633 | sizeof(internal_hash)); |
| 634 | memset(hash_value, 0, sizeof(internal_hash)); |
| 635 | } |
| 636 | if (image_size_to_hash == 0) { |
| 637 | printf("Warning: Image_%d hash checksum is disabled, ", |
| 638 | image_num); |
| 639 | printf("skip the image validation.\n"); |
| 640 | continue; |
| 641 | } |
| 642 | switch (hash_algorithm_id) { |
| 643 | case SHA1_SUM_LEN: |
| 644 | sha1_starts(&hash1_text); |
| 645 | sha1_update(&hash1_text, buff, image_size_to_hash); |
| 646 | sha1_finish(&hash1_text, hash_160_output); |
| 647 | hash_output = hash_160_output; |
| 648 | break; |
| 649 | case SHA256_SUM_LEN: |
| 650 | sha256_starts(&hash256_text); |
| 651 | sha256_update(&hash256_text, buff, image_size_to_hash); |
| 652 | sha256_finish(&hash256_text, hash_256_output); |
| 653 | hash_output = hash_256_output; |
| 654 | break; |
Pali Rohár | a4bbb2c | 2022-07-26 16:11:58 +0200 | [diff] [blame] | 655 | case SHA512_SUM_LEN: |
| 656 | sha512_starts(&hash512_text); |
| 657 | sha512_update(&hash512_text, buff, image_size_to_hash); |
| 658 | sha512_finish(&hash512_text, hash_512_output); |
| 659 | hash_output = hash_512_output; |
| 660 | break; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 661 | default: |
| 662 | printf("Error: Unsupported hash_algorithm_id = %d\n", |
| 663 | hash_algorithm_id); |
| 664 | return -ENOEXEC; |
| 665 | } |
| 666 | if (image_num == 0) |
| 667 | memcpy(hash_value, internal_hash, |
| 668 | sizeof(internal_hash)); |
| 669 | if (memcmp(hash_value, hash_output, hash_algorithm_id) != 0) { |
| 670 | printf("Error: Image_%d checksum is not correct\n", |
| 671 | image_num); |
| 672 | return -ENOEXEC; |
| 673 | } |
| 674 | } |
| 675 | printf("Image checksum...OK!\n"); |
| 676 | |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 677 | return 0; |
| 678 | } |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 679 | #elif defined(CONFIG_ARMADA_32BIT) |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 680 | static size_t a38x_header_size(const struct a38x_main_hdr_v1 *h) |
| 681 | { |
| 682 | if (h->version == 1) |
| 683 | return (h->headersz_msb << 16) | le16_to_cpu(h->headersz_lsb); |
| 684 | |
| 685 | printf("Error: Invalid A38x image (header version 0x%x unknown)!\n", |
| 686 | h->version); |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 687 | return 0; |
| 688 | } |
| 689 | |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 690 | static uint8_t image_checksum8(const void *start, size_t len) |
| 691 | { |
| 692 | u8 csum = 0; |
| 693 | const u8 *p = start; |
| 694 | |
| 695 | while (len) { |
| 696 | csum += *p; |
| 697 | ++p; |
| 698 | --len; |
| 699 | } |
| 700 | |
| 701 | return csum; |
| 702 | } |
| 703 | |
Pali Rohár | 05781ff | 2022-08-23 14:52:23 +0200 | [diff] [blame] | 704 | static uint32_t image_checksum32(const void *start, size_t len) |
| 705 | { |
| 706 | u32 csum = 0; |
| 707 | const u32 *p = start; |
| 708 | |
| 709 | while (len) { |
| 710 | csum += *p; |
| 711 | ++p; |
| 712 | len -= sizeof(u32); |
| 713 | } |
| 714 | |
| 715 | return csum; |
| 716 | } |
| 717 | |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 718 | static int check_image_header(void) |
| 719 | { |
| 720 | u8 checksum; |
Pali Rohár | 05781ff | 2022-08-23 14:52:23 +0200 | [diff] [blame] | 721 | u32 checksum32, exp_checksum32; |
| 722 | u32 offset, size; |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 723 | const struct a38x_main_hdr_v1 *hdr = |
| 724 | (struct a38x_main_hdr_v1 *)get_load_addr(); |
| 725 | const size_t image_size = a38x_header_size(hdr); |
| 726 | |
| 727 | if (!image_size) |
| 728 | return -ENOEXEC; |
| 729 | |
| 730 | checksum = image_checksum8(hdr, image_size); |
| 731 | checksum -= hdr->checksum; |
| 732 | if (checksum != hdr->checksum) { |
Pali Rohár | 05781ff | 2022-08-23 14:52:23 +0200 | [diff] [blame] | 733 | printf("Error: Bad A38x image header checksum. 0x%x != 0x%x\n", |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 734 | checksum, hdr->checksum); |
| 735 | return -ENOEXEC; |
| 736 | } |
| 737 | |
Pali Rohár | 05781ff | 2022-08-23 14:52:23 +0200 | [diff] [blame] | 738 | offset = le32_to_cpu(hdr->srcaddr); |
| 739 | size = le32_to_cpu(hdr->blocksize); |
| 740 | |
| 741 | if (hdr->blockid == 0x78) { /* SATA id */ |
| 742 | if (offset < 1) { |
| 743 | printf("Error: Bad A38x image srcaddr.\n"); |
| 744 | return -ENOEXEC; |
| 745 | } |
| 746 | offset -= 1; |
| 747 | offset *= 512; |
| 748 | } |
| 749 | |
| 750 | if (hdr->blockid == 0xAE) /* SDIO id */ |
| 751 | offset *= 512; |
| 752 | |
| 753 | if (offset % 4 != 0 || size < 4 || size % 4 != 0) { |
| 754 | printf("Error: Bad A38x image blocksize.\n"); |
| 755 | return -ENOEXEC; |
| 756 | } |
| 757 | |
| 758 | checksum32 = image_checksum32((u8 *)hdr + offset, size - 4); |
| 759 | exp_checksum32 = *(u32 *)((u8 *)hdr + offset + size - 4); |
| 760 | if (checksum32 != exp_checksum32) { |
| 761 | printf("Error: Bad A38x image data checksum. 0x%08x != 0x%08x\n", |
| 762 | checksum32, exp_checksum32); |
| 763 | return -ENOEXEC; |
| 764 | } |
| 765 | |
Joel Johnson | 37b9c12 | 2020-04-17 09:38:04 -0600 | [diff] [blame] | 766 | printf("Image checksum...OK!\n"); |
| 767 | return 0; |
| 768 | } |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 769 | |
| 770 | #if defined(CONFIG_ARMADA_38X) |
| 771 | static int a38x_image_is_secure(const struct a38x_main_hdr_v1 *hdr) |
| 772 | { |
| 773 | u32 image_size = a38x_header_size(hdr); |
| 774 | struct a38x_opt_hdr_v1 *ohdr; |
| 775 | u32 ohdr_size; |
| 776 | |
| 777 | if (hdr->version != 1) |
| 778 | return 0; |
| 779 | |
| 780 | if (!hdr->ext) |
| 781 | return 0; |
| 782 | |
| 783 | ohdr = (struct a38x_opt_hdr_v1 *)(hdr + 1); |
| 784 | do { |
| 785 | if (ohdr->headertype == A38X_OPT_HDR_V1_SECURE_TYPE) |
| 786 | return 1; |
| 787 | |
| 788 | ohdr_size = (ohdr->headersz_msb << 16) | le16_to_cpu(ohdr->headersz_lsb); |
| 789 | |
| 790 | if (!*((u8 *)ohdr + ohdr_size - 4)) |
| 791 | break; |
| 792 | |
| 793 | ohdr = (struct a38x_opt_hdr_v1 *)((u8 *)ohdr + ohdr_size); |
| 794 | if ((u8 *)ohdr >= (u8 *)hdr + image_size) |
| 795 | break; |
| 796 | } while (1); |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | #endif |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 801 | #else /* Not ARMADA? */ |
| 802 | static int check_image_header(void) |
| 803 | { |
| 804 | printf("bubt cmd does not support this SoC device or family!\n"); |
| 805 | return -ENOEXEC; |
| 806 | } |
| 807 | #endif |
| 808 | |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 809 | #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT) |
| 810 | static u64 fuse_read_u64(u32 bank) |
| 811 | { |
| 812 | u32 val[2]; |
| 813 | int ret; |
| 814 | |
| 815 | ret = fuse_read(bank, 0, &val[0]); |
| 816 | if (ret < 0) |
| 817 | return -1; |
| 818 | |
| 819 | ret = fuse_read(bank, 1, &val[1]); |
| 820 | if (ret < 0) |
| 821 | return -1; |
| 822 | |
| 823 | return ((u64)val[1] << 32) | val[0]; |
| 824 | } |
| 825 | #endif |
| 826 | |
| 827 | #if defined(CONFIG_ARMADA_3700) |
| 828 | static inline u8 maj3(u8 val) |
| 829 | { |
| 830 | /* return majority vote of 3 bits */ |
| 831 | return ((val & 0x7) == 3 || (val & 0x7) > 4) ? 1 : 0; |
| 832 | } |
| 833 | #endif |
| 834 | |
Joel Johnson | ab5dd30 | 2020-04-17 09:38:06 -0600 | [diff] [blame] | 835 | static int bubt_check_boot_mode(const struct bubt_dev *dst) |
| 836 | { |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 837 | #if defined(CONFIG_ARMADA_3700) || defined(CONFIG_ARMADA_32BIT) |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 838 | int mode, secure_mode; |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 839 | #if defined(CONFIG_ARMADA_3700) |
| 840 | const struct tim_boot_flash_sign *boot_modes = tim_boot_flash_signs; |
| 841 | const struct common_tim_data *hdr = |
| 842 | (struct common_tim_data *)get_load_addr(); |
| 843 | u32 id = hdr->boot_flash_sign; |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 844 | int is_secure = hdr->trusted != 0; |
| 845 | u64 otp_secure_bits = fuse_read_u64(1); |
| 846 | int otp_secure_boot = ((maj3(otp_secure_bits >> 0) << 0) | |
| 847 | (maj3(otp_secure_bits >> 4) << 1)) == 2; |
| 848 | unsigned int otp_boot_device = (maj3(otp_secure_bits >> 48) << 0) | |
| 849 | (maj3(otp_secure_bits >> 52) << 1) | |
| 850 | (maj3(otp_secure_bits >> 56) << 2) | |
| 851 | (maj3(otp_secure_bits >> 60) << 3); |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 852 | #elif defined(CONFIG_ARMADA_32BIT) |
| 853 | const struct a38x_boot_mode *boot_modes = a38x_boot_modes; |
| 854 | const struct a38x_main_hdr_v1 *hdr = |
| 855 | (struct a38x_main_hdr_v1 *)get_load_addr(); |
| 856 | u32 id = hdr->blockid; |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 857 | #if defined(CONFIG_ARMADA_38X) |
| 858 | int is_secure = a38x_image_is_secure(hdr); |
| 859 | u64 otp_secure_bits = fuse_read_u64(EFUSE_LINE_SECURE_BOOT); |
| 860 | int otp_secure_boot = otp_secure_bits & 0x1; |
| 861 | unsigned int otp_boot_device = (otp_secure_bits >> 8) & 0x7; |
| 862 | #endif |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 863 | #endif |
Joel Johnson | ab5dd30 | 2020-04-17 09:38:06 -0600 | [diff] [blame] | 864 | |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 865 | for (mode = 0; boot_modes[mode].name; mode++) { |
| 866 | if (boot_modes[mode].id == id) |
| 867 | break; |
| 868 | } |
Joel Johnson | 27e9d07 | 2020-04-17 09:38:08 -0600 | [diff] [blame] | 869 | |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 870 | if (!boot_modes[mode].name) { |
| 871 | printf("Error: unknown boot device in image header: 0x%x\n", id); |
Joel Johnson | ab5dd30 | 2020-04-17 09:38:06 -0600 | [diff] [blame] | 872 | return -ENOEXEC; |
Joel Johnson | ab5dd30 | 2020-04-17 09:38:06 -0600 | [diff] [blame] | 873 | } |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 874 | |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 875 | if (strcmp(boot_modes[mode].name, dst->name) != 0) { |
| 876 | printf("Error: image meant to be booted from \"%s\", not \"%s\"!\n", |
| 877 | boot_modes[mode].name, dst->name); |
| 878 | return -ENOEXEC; |
| 879 | } |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 880 | |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 881 | #if defined(CONFIG_ARMADA_38X) || defined(CONFIG_ARMADA_3700) |
| 882 | if (otp_secure_bits == (u64)-1) { |
| 883 | printf("Error: cannot read OTP secure bits\n"); |
| 884 | return -ENOEXEC; |
| 885 | } else { |
| 886 | if (otp_secure_boot && !is_secure) { |
| 887 | printf("Error: secure boot is enabled in OTP but image does not have secure boot header!\n"); |
| 888 | return -ENOEXEC; |
| 889 | } else if (!otp_secure_boot && is_secure) { |
| 890 | #if defined(CONFIG_ARMADA_3700) |
| 891 | /* |
| 892 | * Armada 3700 BootROM rejects trusted image when secure boot is not enabled. |
| 893 | * Armada 385 BootROM accepts image with secure boot header also when secure boot is not enabled. |
| 894 | */ |
| 895 | printf("Error: secure boot is disabled in OTP but image has secure boot header!\n"); |
| 896 | return -ENOEXEC; |
Pali Rohár | 1ce57b8 | 2022-07-26 16:11:56 +0200 | [diff] [blame] | 897 | #endif |
Pali Rohár | d83e04a | 2022-08-23 14:52:24 +0200 | [diff] [blame] | 898 | } else if (otp_boot_device && otp_boot_device != id) { |
| 899 | for (secure_mode = 0; boot_modes[secure_mode].name; secure_mode++) { |
| 900 | if (boot_modes[secure_mode].id == otp_boot_device) |
| 901 | break; |
| 902 | } |
| 903 | printf("Error: boot source is set to \"%s\" in OTP but image is for \"%s\"!\n", |
| 904 | boot_modes[secure_mode].name ?: "unknown", dst->name); |
| 905 | return -ENOEXEC; |
| 906 | } |
| 907 | } |
| 908 | #endif |
| 909 | #endif |
| 910 | return 0; |
Joel Johnson | ab5dd30 | 2020-04-17 09:38:06 -0600 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | static int bubt_verify(const struct bubt_dev *dst) |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 914 | { |
| 915 | int err; |
| 916 | |
| 917 | /* Check a correct image header exists */ |
| 918 | err = check_image_header(); |
| 919 | if (err) { |
| 920 | printf("Error: Image header verification failed\n"); |
| 921 | return err; |
| 922 | } |
| 923 | |
Joel Johnson | ab5dd30 | 2020-04-17 09:38:06 -0600 | [diff] [blame] | 924 | err = bubt_check_boot_mode(dst); |
| 925 | if (err) { |
| 926 | printf("Error: Image boot mode verification failed\n"); |
| 927 | return err; |
| 928 | } |
| 929 | |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static int bubt_read_file(struct bubt_dev *src) |
| 934 | { |
| 935 | size_t image_size; |
| 936 | |
| 937 | if (!src->read) { |
| 938 | printf("Error: Read not supported on device \"%s\"\n", |
| 939 | src->name); |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | image_size = src->read(net_boot_file_name); |
| 944 | if (image_size <= 0) { |
| 945 | printf("Error: Failed to read file %s from %s\n", |
| 946 | net_boot_file_name, src->name); |
| 947 | return 0; |
| 948 | } |
| 949 | |
| 950 | return image_size; |
| 951 | } |
| 952 | |
| 953 | static int bubt_is_dev_active(struct bubt_dev *dev) |
| 954 | { |
| 955 | if (!dev->active) { |
Joel Johnson | 22d78e4 | 2020-04-17 09:38:05 -0600 | [diff] [blame] | 956 | printf("Device \"%s\" not supported by U-Boot image\n", |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 957 | dev->name); |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | if (!dev->active()) { |
| 962 | printf("Device \"%s\" is inactive\n", dev->name); |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | return 1; |
| 967 | } |
| 968 | |
| 969 | struct bubt_dev *find_bubt_dev(char *dev_name) |
| 970 | { |
| 971 | int dev; |
| 972 | |
| 973 | for (dev = 0; dev < BUBT_MAX_DEV; dev++) { |
| 974 | if (strcmp(bubt_devs[dev].name, dev_name) == 0) |
| 975 | return &bubt_devs[dev]; |
| 976 | } |
| 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | #define DEFAULT_BUBT_SRC "tftp" |
| 982 | |
| 983 | #ifndef DEFAULT_BUBT_DST |
| 984 | #ifdef CONFIG_MVEBU_SPI_BOOT |
| 985 | #define DEFAULT_BUBT_DST "spi" |
| 986 | #elif defined(CONFIG_MVEBU_NAND_BOOT) |
| 987 | #define DEFAULT_BUBT_DST "nand" |
| 988 | #elif defined(CONFIG_MVEBU_MMC_BOOT) |
| 989 | #define DEFAULT_BUBT_DST "mmc" |
Konstantin Porotchkin | e78a389 | 2021-03-17 18:53:43 +0200 | [diff] [blame] | 990 | #else |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 991 | #define DEFAULT_BUBT_DST "error" |
| 992 | #endif |
| 993 | #endif /* DEFAULT_BUBT_DST */ |
| 994 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 995 | int do_bubt_cmd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 996 | { |
| 997 | struct bubt_dev *src, *dst; |
| 998 | size_t image_size; |
| 999 | char src_dev_name[8]; |
| 1000 | char dst_dev_name[8]; |
| 1001 | char *name; |
| 1002 | int err; |
| 1003 | |
| 1004 | if (argc < 2) |
| 1005 | copy_filename(net_boot_file_name, |
| 1006 | CONFIG_MVEBU_UBOOT_DFLT_NAME, |
| 1007 | sizeof(net_boot_file_name)); |
| 1008 | else |
| 1009 | copy_filename(net_boot_file_name, argv[1], |
| 1010 | sizeof(net_boot_file_name)); |
| 1011 | |
| 1012 | if (argc >= 3) { |
| 1013 | strncpy(dst_dev_name, argv[2], 8); |
| 1014 | } else { |
| 1015 | name = DEFAULT_BUBT_DST; |
| 1016 | strncpy(dst_dev_name, name, 8); |
| 1017 | } |
| 1018 | |
| 1019 | if (argc >= 4) |
| 1020 | strncpy(src_dev_name, argv[3], 8); |
| 1021 | else |
| 1022 | strncpy(src_dev_name, DEFAULT_BUBT_SRC, 8); |
| 1023 | |
| 1024 | /* Figure out the destination device */ |
| 1025 | dst = find_bubt_dev(dst_dev_name); |
| 1026 | if (!dst) { |
| 1027 | printf("Error: Unknown destination \"%s\"\n", dst_dev_name); |
Pali Rohár | 873fc6a | 2022-07-26 16:11:59 +0200 | [diff] [blame] | 1028 | return 1; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | if (!bubt_is_dev_active(dst)) |
Pali Rohár | 873fc6a | 2022-07-26 16:11:59 +0200 | [diff] [blame] | 1032 | return 1; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 1033 | |
| 1034 | /* Figure out the source device */ |
| 1035 | src = find_bubt_dev(src_dev_name); |
| 1036 | if (!src) { |
| 1037 | printf("Error: Unknown source \"%s\"\n", src_dev_name); |
| 1038 | return 1; |
| 1039 | } |
| 1040 | |
| 1041 | if (!bubt_is_dev_active(src)) |
| 1042 | return -ENODEV; |
| 1043 | |
Joel Johnson | 22d78e4 | 2020-04-17 09:38:05 -0600 | [diff] [blame] | 1044 | printf("Burning U-Boot image \"%s\" from \"%s\" to \"%s\"\n", |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 1045 | net_boot_file_name, src->name, dst->name); |
| 1046 | |
| 1047 | image_size = bubt_read_file(src); |
| 1048 | if (!image_size) |
Pali Rohár | 873fc6a | 2022-07-26 16:11:59 +0200 | [diff] [blame] | 1049 | return 1; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 1050 | |
Joel Johnson | ab5dd30 | 2020-04-17 09:38:06 -0600 | [diff] [blame] | 1051 | err = bubt_verify(dst); |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 1052 | if (err) |
Pali Rohár | 873fc6a | 2022-07-26 16:11:59 +0200 | [diff] [blame] | 1053 | return 1; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 1054 | |
| 1055 | err = bubt_write_file(dst, image_size); |
| 1056 | if (err) |
Pali Rohár | 873fc6a | 2022-07-26 16:11:59 +0200 | [diff] [blame] | 1057 | return 1; |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
| 1062 | U_BOOT_CMD( |
| 1063 | bubt, 4, 0, do_bubt_cmd, |
| 1064 | "Burn a u-boot image to flash", |
| 1065 | "[file-name] [destination [source]]\n" |
Pali Rohár | b418820 | 2021-01-27 11:56:02 +0100 | [diff] [blame] | 1066 | "\t-file-name The image file name to burn. Default = " CONFIG_MVEBU_UBOOT_DFLT_NAME "\n" |
| 1067 | "\t-destination Flash to burn to [spi, nand, mmc]. Default = " DEFAULT_BUBT_DST "\n" |
| 1068 | "\t-source The source to load image from [tftp, usb, mmc]. Default = " DEFAULT_BUBT_SRC "\n" |
Konstantin Porotchkin | 97d2678 | 2016-12-08 12:22:28 +0200 | [diff] [blame] | 1069 | "Examples:\n" |
| 1070 | "\tbubt - Burn flash-image.bin from tftp to active boot device\n" |
| 1071 | "\tbubt flash-image-new.bin nand - Burn flash-image-new.bin from tftp to NAND flash\n" |
| 1072 | "\tbubt backup-flash-image.bin mmc usb - Burn backup-flash-image.bin from usb to MMC\n" |
| 1073 | |
| 1074 | ); |