Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Image code used by boards (and not host tools) |
| 4 | * |
| 5 | * (C) Copyright 2008 Semihalf |
| 6 | * |
| 7 | * (C) Copyright 2000-2006 |
| 8 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 9 | */ |
| 10 | |
Tom Rini | 03de305 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 11 | #include <config.h> |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 12 | #include <bootstage.h> |
| 13 | #include <cpu_func.h> |
Simon Glass | 4e4bf94 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 14 | #include <display_options.h> |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 15 | #include <env.h> |
| 16 | #include <fpga.h> |
| 17 | #include <image.h> |
Andy Shevchenko | 4b32531 | 2021-11-08 21:03:38 +0300 | [diff] [blame] | 18 | #include <init.h> |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 19 | #include <log.h> |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 20 | #include <mapmem.h> |
Simon Glass | 5d3248a | 2021-09-25 07:03:17 -0600 | [diff] [blame] | 21 | #include <rtc.h> |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 22 | #include <watchdog.h> |
| 23 | #include <asm/cache.h> |
| 24 | #include <asm/global_data.h> |
| 25 | |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 28 | /** |
| 29 | * image_get_ramdisk - get and verify ramdisk image |
| 30 | * @rd_addr: ramdisk image start address |
| 31 | * @arch: expected ramdisk architecture |
| 32 | * @verify: checksum verification flag |
| 33 | * |
| 34 | * image_get_ramdisk() returns a pointer to the verified ramdisk image |
| 35 | * header. Routine receives image start address and expected architecture |
| 36 | * flag. Verification done covers data and header integrity and os/type/arch |
| 37 | * fields checking. |
| 38 | * |
| 39 | * returns: |
| 40 | * pointer to a ramdisk image header, if image was found and valid |
| 41 | * otherwise, return NULL |
| 42 | */ |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 43 | static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch, |
| 44 | int verify) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 45 | { |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 46 | const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 47 | |
| 48 | if (!image_check_magic(rd_hdr)) { |
| 49 | puts("Bad Magic Number\n"); |
| 50 | bootstage_error(BOOTSTAGE_ID_RD_MAGIC); |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | if (!image_check_hcrc(rd_hdr)) { |
| 55 | puts("Bad Header Checksum\n"); |
| 56 | bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | bootstage_mark(BOOTSTAGE_ID_RD_MAGIC); |
| 61 | image_print_contents(rd_hdr); |
| 62 | |
| 63 | if (verify) { |
| 64 | puts(" Verifying Checksum ... "); |
| 65 | if (!image_check_dcrc(rd_hdr)) { |
| 66 | puts("Bad Data CRC\n"); |
| 67 | bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM); |
| 68 | return NULL; |
| 69 | } |
| 70 | puts("OK\n"); |
| 71 | } |
| 72 | |
| 73 | bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM); |
| 74 | |
| 75 | if (!image_check_os(rd_hdr, IH_OS_LINUX) || |
| 76 | !image_check_arch(rd_hdr, arch) || |
| 77 | !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) { |
| 78 | printf("No Linux %s Ramdisk Image\n", |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 79 | genimg_get_arch_name(arch)); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 80 | bootstage_error(BOOTSTAGE_ID_RAMDISK); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | return rd_hdr; |
| 85 | } |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 86 | |
| 87 | /*****************************************************************************/ |
| 88 | /* Shared dual-format routines */ |
| 89 | /*****************************************************************************/ |
| 90 | ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */ |
| 91 | ulong image_save_addr; /* Default Save Address */ |
| 92 | ulong image_save_size; /* Default Save Size (in bytes) */ |
| 93 | |
| 94 | static int on_loadaddr(const char *name, const char *value, enum env_op op, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 95 | int flags) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 96 | { |
| 97 | switch (op) { |
| 98 | case env_op_create: |
| 99 | case env_op_overwrite: |
| 100 | image_load_addr = hextoul(value, NULL); |
| 101 | break; |
| 102 | default: |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr); |
| 109 | |
Marek Vasut | a4df06e | 2024-03-26 23:13:11 +0100 | [diff] [blame] | 110 | phys_addr_t env_get_bootm_low(void) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 111 | { |
| 112 | char *s = env_get("bootm_low"); |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 113 | |
Marek Vasut | a4df06e | 2024-03-26 23:13:11 +0100 | [diff] [blame] | 114 | if (s) |
| 115 | return simple_strtoull(s, NULL, 16); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 116 | |
Tom Rini | aa6e94d | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 117 | #if defined(CFG_SYS_SDRAM_BASE) |
| 118 | return CFG_SYS_SDRAM_BASE; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 119 | #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV) |
| 120 | return gd->bd->bi_dram[0].start; |
| 121 | #else |
| 122 | return 0; |
| 123 | #endif |
| 124 | } |
| 125 | |
| 126 | phys_size_t env_get_bootm_size(void) |
| 127 | { |
Marek Vasut | 5fb569b | 2024-03-26 23:13:13 +0100 | [diff] [blame] | 128 | phys_addr_t start, low; |
| 129 | phys_size_t size; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 130 | char *s = env_get("bootm_size"); |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 131 | |
Marek Vasut | 98e68ec | 2024-03-26 23:13:12 +0100 | [diff] [blame] | 132 | if (s) |
| 133 | return simple_strtoull(s, NULL, 16); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 134 | |
| 135 | start = gd->ram_base; |
| 136 | size = gd->ram_size; |
| 137 | |
| 138 | if (start + size > gd->ram_top) |
| 139 | size = gd->ram_top - start; |
| 140 | |
| 141 | s = env_get("bootm_low"); |
| 142 | if (s) |
Marek Vasut | 5fb569b | 2024-03-26 23:13:13 +0100 | [diff] [blame] | 143 | low = simple_strtoull(s, NULL, 16); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 144 | else |
Marek Vasut | 5fb569b | 2024-03-26 23:13:13 +0100 | [diff] [blame] | 145 | low = start; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 146 | |
Marek Vasut | 5fb569b | 2024-03-26 23:13:13 +0100 | [diff] [blame] | 147 | return size - (low - start); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | phys_size_t env_get_bootm_mapsize(void) |
| 151 | { |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 152 | char *s = env_get("bootm_mapsize"); |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 153 | |
Marek Vasut | ca8d4df | 2024-03-26 23:13:14 +0100 | [diff] [blame] | 154 | if (s) |
| 155 | return simple_strtoull(s, NULL, 16); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 156 | |
Tom Rini | 65cc0e2 | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 157 | #if defined(CFG_SYS_BOOTMAPSZ) |
| 158 | return CFG_SYS_BOOTMAPSZ; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 159 | #else |
| 160 | return env_get_bootm_size(); |
| 161 | #endif |
| 162 | } |
| 163 | |
| 164 | void memmove_wd(void *to, void *from, size_t len, ulong chunksz) |
| 165 | { |
| 166 | if (to == from) |
| 167 | return; |
| 168 | |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 169 | if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) { |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 170 | if (to > from) { |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 171 | from += len; |
| 172 | to += len; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 173 | } |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 174 | while (len > 0) { |
| 175 | size_t tail = (len > chunksz) ? chunksz : len; |
| 176 | |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 177 | schedule(); |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 178 | if (to > from) { |
| 179 | to -= tail; |
| 180 | from -= tail; |
| 181 | } |
| 182 | memmove(to, from, tail); |
| 183 | if (to < from) { |
| 184 | to += tail; |
| 185 | from += tail; |
| 186 | } |
| 187 | len -= tail; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 188 | } |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 189 | } else { |
| 190 | memmove(to, from, len); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 191 | } |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 192 | } |
| 193 | |
Simon Glass | 530cc47 | 2023-11-18 14:04:57 -0700 | [diff] [blame] | 194 | ulong genimg_get_kernel_addr_fit(const char *const img_addr, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 195 | const char **fit_uname_config, |
| 196 | const char **fit_uname_kernel) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 197 | { |
| 198 | ulong kernel_addr; |
| 199 | |
| 200 | /* find out kernel image address */ |
| 201 | if (!img_addr) { |
| 202 | kernel_addr = image_load_addr; |
| 203 | debug("* kernel: default image load address = 0x%08lx\n", |
| 204 | image_load_addr); |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 205 | } else if (CONFIG_IS_ENABLED(FIT) && |
| 206 | fit_parse_conf(img_addr, image_load_addr, &kernel_addr, |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 207 | fit_uname_config)) { |
| 208 | debug("* kernel: config '%s' from image at 0x%08lx\n", |
| 209 | *fit_uname_config, kernel_addr); |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 210 | } else if (CONFIG_IS_ENABLED(FIT) && |
| 211 | fit_parse_subimage(img_addr, image_load_addr, &kernel_addr, |
| 212 | fit_uname_kernel)) { |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 213 | debug("* kernel: subimage '%s' from image at 0x%08lx\n", |
| 214 | *fit_uname_kernel, kernel_addr); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 215 | } else { |
| 216 | kernel_addr = hextoul(img_addr, NULL); |
| 217 | debug("* kernel: cmdline image address = 0x%08lx\n", |
| 218 | kernel_addr); |
| 219 | } |
| 220 | |
| 221 | return kernel_addr; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * genimg_get_kernel_addr() is the simple version of |
| 226 | * genimg_get_kernel_addr_fit(). It ignores those return FIT strings |
| 227 | */ |
| 228 | ulong genimg_get_kernel_addr(char * const img_addr) |
| 229 | { |
| 230 | const char *fit_uname_config = NULL; |
| 231 | const char *fit_uname_kernel = NULL; |
| 232 | |
| 233 | return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config, |
| 234 | &fit_uname_kernel); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * genimg_get_format - get image format type |
| 239 | * @img_addr: image start address |
| 240 | * |
| 241 | * genimg_get_format() checks whether provided address points to a valid |
| 242 | * legacy or FIT image. |
| 243 | * |
| 244 | * New uImage format and FDT blob are based on a libfdt. FDT blob |
| 245 | * may be passed directly or embedded in a FIT image. In both situations |
| 246 | * genimg_get_format() must be able to dectect libfdt header. |
| 247 | * |
| 248 | * returns: |
| 249 | * image format type or IMAGE_FORMAT_INVALID if no image is present |
| 250 | */ |
| 251 | int genimg_get_format(const void *img_addr) |
| 252 | { |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 253 | if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) { |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 254 | const struct legacy_img_hdr *hdr; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 255 | |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 256 | hdr = (const struct legacy_img_hdr *)img_addr; |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 257 | if (image_check_magic(hdr)) |
| 258 | return IMAGE_FORMAT_LEGACY; |
| 259 | } |
| 260 | if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) { |
| 261 | if (!fdt_check_header(img_addr)) |
| 262 | return IMAGE_FORMAT_FIT; |
| 263 | } |
| 264 | if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) && |
Safae Ouajih | 734cb47 | 2023-02-06 00:50:05 +0100 | [diff] [blame] | 265 | is_android_boot_image_header(img_addr)) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 266 | return IMAGE_FORMAT_ANDROID; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 267 | |
| 268 | return IMAGE_FORMAT_INVALID; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * fit_has_config - check if there is a valid FIT configuration |
| 273 | * @images: pointer to the bootm command headers structure |
| 274 | * |
| 275 | * fit_has_config() checks if there is a FIT configuration in use |
| 276 | * (if FTI support is present). |
| 277 | * |
| 278 | * returns: |
| 279 | * 0, no FIT support or no configuration found |
| 280 | * 1, configuration found |
| 281 | */ |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 282 | int genimg_has_config(struct bootm_headers *images) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 283 | { |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 284 | if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 285 | return 1; |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 286 | |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /** |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 291 | * select_ramdisk() - Select and locate the ramdisk to use |
| 292 | * |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 293 | * @images: pointer to the bootm images structure |
Simon Glass | 4f2d941 | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 294 | * @select: name of ramdisk to select, or hex address, NULL for any |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 295 | * @arch: expected ramdisk architecture |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 296 | * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer |
| 297 | * @rd_lenp: pointer to a ulong variable, will hold ramdisk length |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 298 | * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported), |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 299 | * other -ve value on other error |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 300 | */ |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 301 | static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch, |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 302 | ulong *rd_datap, ulong *rd_lenp) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 303 | { |
Simon Glass | f7659f6 | 2022-08-28 12:32:49 -0600 | [diff] [blame] | 304 | const char *fit_uname_config; |
| 305 | const char *fit_uname_ramdisk; |
Simon Glass | ca78883 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 306 | bool done_select = !select; |
Simon Glass | 4f2d941 | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 307 | bool done = false; |
Simon Glass | f7659f6 | 2022-08-28 12:32:49 -0600 | [diff] [blame] | 308 | int rd_noffset; |
Tom Rini | 51a765b | 2023-04-05 19:48:56 -0400 | [diff] [blame] | 309 | ulong rd_addr = 0; |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 310 | char *buf; |
Simon Glass | 78f8879 | 2021-09-25 19:43:36 -0600 | [diff] [blame] | 311 | |
Simon Glass | ca78883 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 312 | if (CONFIG_IS_ENABLED(FIT)) { |
Simon Glass | f7659f6 | 2022-08-28 12:32:49 -0600 | [diff] [blame] | 313 | fit_uname_config = images->fit_uname_cfg; |
| 314 | fit_uname_ramdisk = NULL; |
Simon Glass | 78f8879 | 2021-09-25 19:43:36 -0600 | [diff] [blame] | 315 | |
Tom Rini | 621158d | 2021-12-20 09:36:32 -0500 | [diff] [blame] | 316 | if (select) { |
| 317 | ulong default_addr; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 318 | /* |
| 319 | * If the init ramdisk comes from the FIT image and |
| 320 | * the FIT image address is omitted in the command |
| 321 | * line argument, try to use os FIT image address or |
| 322 | * default load address. |
| 323 | */ |
| 324 | if (images->fit_uname_os) |
| 325 | default_addr = (ulong)images->fit_hdr_os; |
| 326 | else |
| 327 | default_addr = image_load_addr; |
| 328 | |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 329 | if (fit_parse_conf(select, default_addr, &rd_addr, |
| 330 | &fit_uname_config)) { |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 331 | debug("* ramdisk: config '%s' from image at 0x%08lx\n", |
| 332 | fit_uname_config, rd_addr); |
Simon Glass | ca78883 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 333 | done_select = true; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 334 | } else if (fit_parse_subimage(select, default_addr, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 335 | &rd_addr, |
| 336 | &fit_uname_ramdisk)) { |
| 337 | debug("* ramdisk: subimage '%s' from image at 0x%08lx\n", |
| 338 | fit_uname_ramdisk, rd_addr); |
Simon Glass | ca78883 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 339 | done_select = true; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | if (!done_select) { |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 344 | rd_addr = hextoul(select, NULL); |
| 345 | debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr); |
Simon Glass | ca78883 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 346 | } |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 347 | if (CONFIG_IS_ENABLED(FIT) && !select) { |
| 348 | /* use FIT configuration provided in first bootm |
| 349 | * command argument. If the property is not defined, |
| 350 | * quit silently (with -ENOPKG) |
Tom Rini | 621158d | 2021-12-20 09:36:32 -0500 | [diff] [blame] | 351 | */ |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 352 | rd_addr = map_to_sysmem(images->fit_hdr_os); |
| 353 | rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP, |
| 354 | rd_addr); |
| 355 | if (rd_noffset == -ENOENT) |
| 356 | return -ENOPKG; |
| 357 | else if (rd_noffset < 0) |
| 358 | return rd_noffset; |
| 359 | } |
Simon Glass | 78f8879 | 2021-09-25 19:43:36 -0600 | [diff] [blame] | 360 | |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 361 | /* |
| 362 | * Check if there is an initrd image at the |
| 363 | * address provided in the second bootm argument |
| 364 | * check image type, for FIT images get FIT node. |
| 365 | */ |
| 366 | buf = map_sysmem(rd_addr, 0); |
| 367 | switch (genimg_get_format(buf)) { |
| 368 | case IMAGE_FORMAT_LEGACY: |
| 369 | if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) { |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 370 | const struct legacy_img_hdr *rd_hdr; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 371 | |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 372 | printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n", |
| 373 | rd_addr); |
Tom Rini | 621158d | 2021-12-20 09:36:32 -0500 | [diff] [blame] | 374 | |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 375 | bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK); |
| 376 | rd_hdr = image_get_ramdisk(rd_addr, arch, |
| 377 | images->verify); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 378 | |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 379 | if (!rd_hdr) |
| 380 | return -ENOENT; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 381 | |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 382 | *rd_datap = image_get_data(rd_hdr); |
| 383 | *rd_lenp = image_get_data_size(rd_hdr); |
| 384 | done = true; |
Simon Glass | 4f2d941 | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 385 | } |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 386 | break; |
| 387 | case IMAGE_FORMAT_FIT: |
| 388 | if (CONFIG_IS_ENABLED(FIT)) { |
| 389 | rd_noffset = fit_image_load(images, rd_addr, |
| 390 | &fit_uname_ramdisk, |
| 391 | &fit_uname_config, |
| 392 | arch, IH_TYPE_RAMDISK, |
| 393 | BOOTSTAGE_ID_FIT_RD_START, |
| 394 | FIT_LOAD_OPTIONAL_NON_ZERO, |
| 395 | rd_datap, rd_lenp); |
| 396 | if (rd_noffset < 0) |
| 397 | return rd_noffset; |
| 398 | |
| 399 | images->fit_hdr_rd = map_sysmem(rd_addr, 0); |
| 400 | images->fit_uname_rd = fit_uname_ramdisk; |
| 401 | images->fit_noffset_rd = rd_noffset; |
| 402 | done = true; |
| 403 | } |
| 404 | break; |
| 405 | case IMAGE_FORMAT_ANDROID: |
| 406 | if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) { |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 407 | int ret; |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 408 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { |
Roman Stratiienko | 17b1656 | 2024-05-23 07:06:07 +0000 | [diff] [blame] | 409 | ulong boot_img = get_abootimg_addr(); |
| 410 | ulong init_boot_img = get_ainit_bootimg_addr(); |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 411 | void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0); |
Roman Stratiienko | 17b1656 | 2024-05-23 07:06:07 +0000 | [diff] [blame] | 412 | void *ramdisk_img; |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 413 | |
Roman Stratiienko | 17b1656 | 2024-05-23 07:06:07 +0000 | [diff] [blame] | 414 | if (init_boot_img == -1) |
| 415 | ramdisk_img = map_sysmem(boot_img, 0); |
| 416 | else |
| 417 | ramdisk_img = map_sysmem(init_boot_img, 0); |
| 418 | |
| 419 | ret = android_image_get_ramdisk(ramdisk_img, vendor_boot_img, |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 420 | rd_datap, rd_lenp); |
| 421 | unmap_sysmem(vendor_boot_img); |
Roman Stratiienko | 17b1656 | 2024-05-23 07:06:07 +0000 | [diff] [blame] | 422 | unmap_sysmem(ramdisk_img); |
Safae Ouajih | 636da20 | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 423 | } else { |
| 424 | void *ptr = map_sysmem(images->os.start, 0); |
| 425 | |
| 426 | ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp); |
| 427 | unmap_sysmem(ptr); |
| 428 | } |
| 429 | |
Michael Walle | 6509c3f | 2024-07-29 23:36:57 +0200 | [diff] [blame] | 430 | if (ret == -ENOENT) |
| 431 | return -ENOPKG; |
| 432 | else if (ret) |
Simon Glass | 20f5d83 | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 433 | return ret; |
| 434 | done = true; |
| 435 | } |
| 436 | break; |
| 437 | } |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 438 | |
Simon Glass | 4f2d941 | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 439 | if (!done) { |
| 440 | if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) { |
| 441 | char *end = NULL; |
| 442 | |
| 443 | if (select) |
| 444 | end = strchr(select, ':'); |
| 445 | if (end) { |
| 446 | *rd_lenp = hextoul(++end, NULL); |
| 447 | *rd_datap = rd_addr; |
| 448 | done = true; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 449 | } |
Simon Glass | 4f2d941 | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if (!done) { |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 453 | puts("Wrong Ramdisk Image Format\n"); |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 454 | return -EINVAL; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 455 | } |
Simon Glass | 4f2d941 | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 456 | } |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Simon Glass | 8eda15b | 2023-11-18 14:05:06 -0700 | [diff] [blame] | 461 | int boot_get_ramdisk(char const *select, struct bootm_headers *images, |
| 462 | uint arch, ulong *rd_start, ulong *rd_end) |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 463 | { |
| 464 | ulong rd_data, rd_len; |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 465 | |
| 466 | *rd_start = 0; |
| 467 | *rd_end = 0; |
| 468 | |
Simon Glass | e4c9287 | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 469 | /* |
| 470 | * Look for a '-' which indicates to ignore the |
| 471 | * ramdisk argument |
| 472 | */ |
| 473 | if (select && strcmp(select, "-") == 0) { |
| 474 | debug("## Skipping init Ramdisk\n"); |
| 475 | rd_len = 0; |
| 476 | rd_data = 0; |
| 477 | } else if (select || genimg_has_config(images)) { |
| 478 | int ret; |
| 479 | |
| 480 | ret = select_ramdisk(images, select, arch, &rd_data, &rd_len); |
| 481 | if (ret == -ENOPKG) |
| 482 | return 0; |
| 483 | else if (ret) |
| 484 | return ret; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 485 | } else if (images->legacy_hdr_valid && |
| 486 | image_check_type(&images->legacy_hdr_os_copy, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 487 | IH_TYPE_MULTI)) { |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 488 | /* |
| 489 | * Now check if we have a legacy mult-component image, |
| 490 | * get second entry data start address and len. |
| 491 | */ |
| 492 | bootstage_mark(BOOTSTAGE_ID_RAMDISK); |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 493 | printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n", |
| 494 | (ulong)images->legacy_hdr_os); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 495 | |
| 496 | image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len); |
| 497 | } else { |
| 498 | /* |
| 499 | * no initrd image |
| 500 | */ |
| 501 | bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK); |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 502 | rd_len = 0; |
| 503 | rd_data = 0; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | if (!rd_data) { |
| 507 | debug("## No init Ramdisk\n"); |
| 508 | } else { |
| 509 | *rd_start = rd_data; |
| 510 | *rd_end = rd_data + rd_len; |
| 511 | } |
| 512 | debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n", |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 513 | *rd_start, *rd_end); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 518 | /** |
| 519 | * boot_ramdisk_high - relocate init ramdisk |
| 520 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
| 521 | * @rd_data: ramdisk data start address |
| 522 | * @rd_len: ramdisk data length |
| 523 | * @initrd_start: pointer to a ulong variable, will hold final init ramdisk |
| 524 | * start address (after possible relocation) |
| 525 | * @initrd_end: pointer to a ulong variable, will hold final init ramdisk |
| 526 | * end address (after possible relocation) |
| 527 | * |
| 528 | * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment |
| 529 | * variable and if requested ramdisk data is moved to a specified location. |
| 530 | * |
| 531 | * Initrd_start and initrd_end are set to final (after relocation) ramdisk |
| 532 | * start/end addresses if ramdisk image start and len were provided, |
| 533 | * otherwise set initrd_start and initrd_end set to zeros. |
| 534 | * |
| 535 | * returns: |
| 536 | * 0 - success |
| 537 | * -1 - failure |
| 538 | */ |
| 539 | int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 540 | ulong *initrd_start, ulong *initrd_end) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 541 | { |
| 542 | char *s; |
Marek Vasut | a4df06e | 2024-03-26 23:13:11 +0100 | [diff] [blame] | 543 | phys_addr_t initrd_high; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 544 | int initrd_copy_to_ram = 1; |
| 545 | |
| 546 | s = env_get("initrd_high"); |
| 547 | if (s) { |
| 548 | /* a value of "no" or a similar string will act like 0, |
| 549 | * turning the "load high" feature off. This is intentional. |
| 550 | */ |
| 551 | initrd_high = hextoul(s, NULL); |
| 552 | if (initrd_high == ~0) |
| 553 | initrd_copy_to_ram = 0; |
| 554 | } else { |
| 555 | initrd_high = env_get_bootm_mapsize() + env_get_bootm_low(); |
| 556 | } |
| 557 | |
Marek Vasut | a4df06e | 2024-03-26 23:13:11 +0100 | [diff] [blame] | 558 | debug("## initrd_high = 0x%llx, copy_to_ram = %d\n", |
| 559 | (u64)initrd_high, initrd_copy_to_ram); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 560 | |
| 561 | if (rd_data) { |
| 562 | if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */ |
| 563 | debug(" in-place initrd\n"); |
| 564 | *initrd_start = rd_data; |
| 565 | *initrd_end = rd_data + rd_len; |
| 566 | lmb_reserve(lmb, rd_data, rd_len); |
| 567 | } else { |
| 568 | if (initrd_high) |
| 569 | *initrd_start = (ulong)lmb_alloc_base(lmb, |
| 570 | rd_len, 0x1000, initrd_high); |
| 571 | else |
| 572 | *initrd_start = (ulong)lmb_alloc(lmb, rd_len, |
| 573 | 0x1000); |
| 574 | |
| 575 | if (*initrd_start == 0) { |
| 576 | puts("ramdisk - allocation error\n"); |
| 577 | goto error; |
| 578 | } |
| 579 | bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK); |
| 580 | |
| 581 | *initrd_end = *initrd_start + rd_len; |
| 582 | printf(" Loading Ramdisk to %08lx, end %08lx ... ", |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 583 | *initrd_start, *initrd_end); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 584 | |
| 585 | memmove_wd((void *)*initrd_start, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 586 | (void *)rd_data, rd_len, CHUNKSZ); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 587 | |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 588 | /* |
| 589 | * Ensure the image is flushed to memory to handle |
| 590 | * AMP boot scenarios in which we might not be |
| 591 | * HW cache coherent |
| 592 | */ |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 593 | if (IS_ENABLED(CONFIG_MP)) { |
| 594 | flush_cache((unsigned long)*initrd_start, |
| 595 | ALIGN(rd_len, ARCH_DMA_MINALIGN)); |
| 596 | } |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 597 | puts("OK\n"); |
| 598 | } |
| 599 | } else { |
| 600 | *initrd_start = 0; |
| 601 | *initrd_end = 0; |
| 602 | } |
| 603 | debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n", |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 604 | *initrd_start, *initrd_end); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 605 | |
| 606 | return 0; |
| 607 | |
| 608 | error: |
| 609 | return -1; |
| 610 | } |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 611 | |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 612 | int boot_get_setup(struct bootm_headers *images, u8 arch, |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 613 | ulong *setup_start, ulong *setup_len) |
| 614 | { |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 615 | if (!CONFIG_IS_ENABLED(FIT)) |
| 616 | return -ENOENT; |
| 617 | |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 618 | return boot_get_setup_fit(images, arch, setup_start, setup_len); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 619 | } |
| 620 | |
Simon Glass | 745367b | 2023-11-18 14:05:11 -0700 | [diff] [blame] | 621 | int boot_get_fpga(struct bootm_headers *images) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 622 | { |
| 623 | ulong tmp_img_addr, img_data, img_len; |
| 624 | void *buf; |
| 625 | int conf_noffset; |
| 626 | int fit_img_result; |
| 627 | const char *uname, *name; |
| 628 | int err; |
| 629 | int devnum = 0; /* TODO support multi fpga platforms */ |
| 630 | |
Simon Glass | 1df654a | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 631 | if (!IS_ENABLED(CONFIG_FPGA)) |
| 632 | return -ENOSYS; |
| 633 | |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 634 | /* Check to see if the images struct has a FIT configuration */ |
| 635 | if (!genimg_has_config(images)) { |
| 636 | debug("## FIT configuration was not specified\n"); |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * Obtain the os FIT header from the images struct |
| 642 | */ |
| 643 | tmp_img_addr = map_to_sysmem(images->fit_hdr_os); |
| 644 | buf = map_sysmem(tmp_img_addr, 0); |
| 645 | /* |
| 646 | * Check image type. For FIT images get FIT node |
| 647 | * and attempt to locate a generic binary. |
| 648 | */ |
| 649 | switch (genimg_get_format(buf)) { |
| 650 | case IMAGE_FORMAT_FIT: |
| 651 | conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg); |
| 652 | |
| 653 | uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0, |
| 654 | NULL); |
| 655 | if (!uname) { |
| 656 | debug("## FPGA image is not specified\n"); |
| 657 | return 0; |
| 658 | } |
| 659 | fit_img_result = fit_image_load(images, |
| 660 | tmp_img_addr, |
| 661 | (const char **)&uname, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 662 | &images->fit_uname_cfg, |
Simon Glass | 745367b | 2023-11-18 14:05:11 -0700 | [diff] [blame] | 663 | IH_ARCH_DEFAULT, |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 664 | IH_TYPE_FPGA, |
| 665 | BOOTSTAGE_ID_FPGA_INIT, |
| 666 | FIT_LOAD_OPTIONAL_NON_ZERO, |
| 667 | &img_data, &img_len); |
| 668 | |
| 669 | debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n", |
| 670 | uname, img_data, img_len); |
| 671 | |
| 672 | if (fit_img_result < 0) { |
| 673 | /* Something went wrong! */ |
| 674 | return fit_img_result; |
| 675 | } |
| 676 | |
| 677 | if (!fpga_is_partial_data(devnum, img_len)) { |
| 678 | name = "full"; |
| 679 | err = fpga_loadbitstream(devnum, (char *)img_data, |
| 680 | img_len, BIT_FULL); |
| 681 | if (err) |
| 682 | err = fpga_load(devnum, (const void *)img_data, |
Oleksandr Suvorov | 282eed5 | 2022-07-22 17:16:07 +0300 | [diff] [blame] | 683 | img_len, BIT_FULL, 0); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 684 | } else { |
| 685 | name = "partial"; |
| 686 | err = fpga_loadbitstream(devnum, (char *)img_data, |
| 687 | img_len, BIT_PARTIAL); |
| 688 | if (err) |
| 689 | err = fpga_load(devnum, (const void *)img_data, |
Oleksandr Suvorov | 282eed5 | 2022-07-22 17:16:07 +0300 | [diff] [blame] | 690 | img_len, BIT_PARTIAL, 0); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (err) |
| 694 | return err; |
| 695 | |
| 696 | printf(" Programming %s bitstream... OK\n", name); |
| 697 | break; |
| 698 | default: |
| 699 | printf("The given image format is not supported (corrupt?)\n"); |
| 700 | return 1; |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 705 | |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 706 | static void fit_loadable_process(u8 img_type, |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 707 | ulong img_data, |
| 708 | ulong img_len) |
| 709 | { |
| 710 | int i; |
| 711 | const unsigned int count = |
| 712 | ll_entry_count(struct fit_loadable_tbl, fit_loadable); |
| 713 | struct fit_loadable_tbl *fit_loadable_handler = |
| 714 | ll_entry_start(struct fit_loadable_tbl, fit_loadable); |
| 715 | /* For each loadable handler */ |
| 716 | for (i = 0; i < count; i++, fit_loadable_handler++) |
| 717 | /* matching this type */ |
| 718 | if (fit_loadable_handler->type == img_type) |
| 719 | /* call that handler with this image data */ |
| 720 | fit_loadable_handler->handler(img_data, img_len); |
| 721 | } |
| 722 | |
Simon Glass | 9645628 | 2023-11-18 14:05:12 -0700 | [diff] [blame] | 723 | int boot_get_loadable(struct bootm_headers *images) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 724 | { |
| 725 | /* |
| 726 | * These variables are used to hold the current image location |
| 727 | * in system memory. |
| 728 | */ |
| 729 | ulong tmp_img_addr; |
| 730 | /* |
| 731 | * These two variables are requirements for fit_image_load, but |
| 732 | * their values are not used |
| 733 | */ |
| 734 | ulong img_data, img_len; |
| 735 | void *buf; |
| 736 | int loadables_index; |
| 737 | int conf_noffset; |
| 738 | int fit_img_result; |
| 739 | const char *uname; |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 740 | u8 img_type; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 741 | |
| 742 | /* Check to see if the images struct has a FIT configuration */ |
| 743 | if (!genimg_has_config(images)) { |
| 744 | debug("## FIT configuration was not specified\n"); |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Obtain the os FIT header from the images struct |
| 750 | */ |
| 751 | tmp_img_addr = map_to_sysmem(images->fit_hdr_os); |
| 752 | buf = map_sysmem(tmp_img_addr, 0); |
| 753 | /* |
| 754 | * Check image type. For FIT images get FIT node |
| 755 | * and attempt to locate a generic binary. |
| 756 | */ |
| 757 | switch (genimg_get_format(buf)) { |
| 758 | case IMAGE_FORMAT_FIT: |
| 759 | conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg); |
| 760 | |
| 761 | for (loadables_index = 0; |
| 762 | uname = fdt_stringlist_get(buf, conf_noffset, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 763 | FIT_LOADABLE_PROP, |
| 764 | loadables_index, NULL), uname; |
| 765 | loadables_index++) { |
| 766 | fit_img_result = fit_image_load(images, tmp_img_addr, |
| 767 | &uname, |
| 768 | &images->fit_uname_cfg, |
Simon Glass | 9645628 | 2023-11-18 14:05:12 -0700 | [diff] [blame] | 769 | IH_ARCH_DEFAULT, |
| 770 | IH_TYPE_LOADABLE, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 771 | BOOTSTAGE_ID_FIT_LOADABLE_START, |
| 772 | FIT_LOAD_OPTIONAL_NON_ZERO, |
| 773 | &img_data, &img_len); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 774 | if (fit_img_result < 0) { |
| 775 | /* Something went wrong! */ |
| 776 | return fit_img_result; |
| 777 | } |
| 778 | |
| 779 | fit_img_result = fit_image_get_node(buf, uname); |
| 780 | if (fit_img_result < 0) { |
| 781 | /* Something went wrong! */ |
| 782 | return fit_img_result; |
| 783 | } |
| 784 | fit_img_result = fit_image_get_type(buf, |
| 785 | fit_img_result, |
| 786 | &img_type); |
| 787 | if (fit_img_result < 0) { |
| 788 | /* Something went wrong! */ |
| 789 | return fit_img_result; |
| 790 | } |
| 791 | |
| 792 | fit_loadable_process(img_type, img_data, img_len); |
| 793 | } |
| 794 | break; |
| 795 | default: |
| 796 | printf("The given image format is not supported (corrupt?)\n"); |
| 797 | return 1; |
| 798 | } |
| 799 | |
| 800 | return 0; |
| 801 | } |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 802 | |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 803 | /** |
| 804 | * boot_get_cmdline - allocate and initialize kernel cmdline |
| 805 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
| 806 | * @cmd_start: pointer to a ulong variable, will hold cmdline start |
| 807 | * @cmd_end: pointer to a ulong variable, will hold cmdline end |
| 808 | * |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 809 | * This allocates space for kernel command line below |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 810 | * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment |
| 811 | * variable is present its contents is copied to allocated kernel |
| 812 | * command line. |
| 813 | * |
| 814 | * returns: |
| 815 | * 0 - success |
| 816 | * -1 - failure |
| 817 | */ |
| 818 | int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end) |
| 819 | { |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 820 | int barg; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 821 | char *cmdline; |
| 822 | char *s; |
| 823 | |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 824 | /* |
| 825 | * Help the compiler detect that this function is only called when |
| 826 | * CONFIG_SYS_BOOT_GET_CMDLINE is enabled |
| 827 | */ |
| 828 | if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) |
| 829 | return 0; |
| 830 | |
| 831 | barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE); |
| 832 | cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf, |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 833 | env_get_bootm_mapsize() + env_get_bootm_low()); |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 834 | if (!cmdline) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 835 | return -1; |
| 836 | |
| 837 | s = env_get("bootargs"); |
| 838 | if (!s) |
| 839 | s = ""; |
| 840 | |
| 841 | strcpy(cmdline, s); |
| 842 | |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 843 | *cmd_start = (ulong)cmdline; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 844 | *cmd_end = *cmd_start + strlen(cmdline); |
| 845 | |
| 846 | debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end); |
| 847 | |
| 848 | return 0; |
| 849 | } |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 850 | |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 851 | /** |
| 852 | * boot_get_kbd - allocate and initialize kernel copy of board info |
| 853 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
| 854 | * @kbd: double pointer to board info data |
| 855 | * |
| 856 | * boot_get_kbd() allocates space for kernel copy of board info data below |
| 857 | * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized |
| 858 | * with the current u-boot board info data. |
| 859 | * |
| 860 | * returns: |
| 861 | * 0 - success |
| 862 | * -1 - failure |
| 863 | */ |
| 864 | int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd) |
| 865 | { |
| 866 | *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb, |
| 867 | sizeof(struct bd_info), |
| 868 | 0xf, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 869 | env_get_bootm_mapsize() + |
| 870 | env_get_bootm_low()); |
| 871 | if (!*kbd) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 872 | return -1; |
| 873 | |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 874 | **kbd = *gd->bd; |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 875 | |
| 876 | debug("## kernel board info at 0x%08lx\n", (ulong)*kbd); |
| 877 | |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 878 | if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI)) |
Simon Glass | 4ed37ab | 2021-09-25 07:03:20 -0600 | [diff] [blame] | 879 | do_bdinfo(NULL, 0, 0, NULL); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 880 | |
| 881 | return 0; |
| 882 | } |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 883 | |
Simon Glass | d9d7c20 | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 884 | int image_setup_linux(struct bootm_headers *images) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 885 | { |
| 886 | ulong of_size = images->ft_len; |
| 887 | char **of_flat_tree = &images->ft_addr; |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 888 | struct lmb *lmb = images_lmb(images); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 889 | int ret; |
| 890 | |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 891 | /* This function cannot be called without lmb support */ |
Simon Glass | 210af54 | 2023-02-05 15:40:13 -0700 | [diff] [blame] | 892 | if (!IS_ENABLED(CONFIG_LMB)) |
Simon Glass | 9c2e912 | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 893 | return -EFAULT; |
Simon Glass | 0c303f9 | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 894 | if (CONFIG_IS_ENABLED(OF_LIBFDT)) |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 895 | boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree); |
| 896 | |
Simon Glass | 806d1ff | 2021-09-25 19:43:25 -0600 | [diff] [blame] | 897 | if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) { |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 898 | ret = boot_get_cmdline(lmb, &images->cmdline_start, |
Simon Glass | 3d2a47f | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 899 | &images->cmdline_end); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 900 | if (ret) { |
| 901 | puts("ERROR with allocation of cmdline\n"); |
| 902 | return ret; |
| 903 | } |
| 904 | } |
| 905 | |
Simon Glass | 0c303f9 | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 906 | if (CONFIG_IS_ENABLED(OF_LIBFDT)) { |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 907 | ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size); |
| 908 | if (ret) |
| 909 | return ret; |
| 910 | } |
| 911 | |
Simon Glass | 0c303f9 | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 912 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) { |
Simon Glass | 1de1a03 | 2023-11-12 08:27:44 -0700 | [diff] [blame] | 913 | ret = image_setup_libfdt(images, *of_flat_tree, lmb); |
Simon Glass | 41506ff | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 914 | if (ret) |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | return 0; |
| 919 | } |
Simon Glass | 5d3248a | 2021-09-25 07:03:17 -0600 | [diff] [blame] | 920 | |
| 921 | void genimg_print_size(uint32_t size) |
| 922 | { |
| 923 | printf("%d Bytes = ", size); |
| 924 | print_size(size, "\n"); |
| 925 | } |
| 926 | |
| 927 | void genimg_print_time(time_t timestamp) |
| 928 | { |
| 929 | struct rtc_time tm; |
| 930 | |
| 931 | rtc_to_tm(timestamp, &tm); |
| 932 | printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n", |
| 933 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 934 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 935 | } |
Simon Glass | 30f3333 | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 936 | |
| 937 | /** |
| 938 | * get_default_image() - Return default property from /images |
| 939 | * |
| 940 | * Return: Pointer to value of default property (or NULL) |
| 941 | */ |
| 942 | static const char *get_default_image(const void *fit) |
| 943 | { |
| 944 | int images_noffset; |
| 945 | |
| 946 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 947 | if (images_noffset < 0) |
| 948 | return NULL; |
| 949 | |
| 950 | return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL); |
| 951 | } |
| 952 | |
| 953 | int image_locate_script(void *buf, int size, const char *fit_uname, |
| 954 | const char *confname, char **datap, uint *lenp) |
| 955 | { |
| 956 | const struct legacy_img_hdr *hdr; |
| 957 | const void *fit_data; |
| 958 | const void *fit_hdr; |
| 959 | size_t fit_len; |
| 960 | int noffset; |
| 961 | int verify; |
| 962 | ulong len; |
| 963 | u32 *data; |
| 964 | |
| 965 | verify = env_get_yesno("verify"); |
| 966 | |
| 967 | switch (genimg_get_format(buf)) { |
| 968 | case IMAGE_FORMAT_LEGACY: |
Marek Vasut | 771cb4d | 2023-02-27 20:56:31 +0100 | [diff] [blame] | 969 | if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) { |
| 970 | goto exit_image_format; |
| 971 | } else { |
Simon Glass | 30f3333 | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 972 | hdr = buf; |
| 973 | |
| 974 | if (!image_check_magic(hdr)) { |
| 975 | puts("Bad magic number\n"); |
| 976 | return 1; |
| 977 | } |
| 978 | |
| 979 | if (!image_check_hcrc(hdr)) { |
| 980 | puts("Bad header crc\n"); |
| 981 | return 1; |
| 982 | } |
| 983 | |
| 984 | if (verify) { |
| 985 | if (!image_check_dcrc(hdr)) { |
| 986 | puts("Bad data crc\n"); |
| 987 | return 1; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | if (!image_check_type(hdr, IH_TYPE_SCRIPT)) { |
| 992 | puts("Bad image type\n"); |
| 993 | return 1; |
| 994 | } |
| 995 | |
| 996 | /* get length of script */ |
| 997 | data = (u32 *)image_get_data(hdr); |
| 998 | |
| 999 | len = uimage_to_cpu(*data); |
| 1000 | if (!len) { |
| 1001 | puts("Empty Script\n"); |
| 1002 | return 1; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * scripts are just multi-image files with one |
| 1007 | * component, so seek past the zero-terminated sequence |
| 1008 | * of image lengths to get to the actual image data |
| 1009 | */ |
| 1010 | while (*data++); |
| 1011 | } |
| 1012 | break; |
| 1013 | case IMAGE_FORMAT_FIT: |
Marek Vasut | 771cb4d | 2023-02-27 20:56:31 +0100 | [diff] [blame] | 1014 | if (!IS_ENABLED(CONFIG_FIT)) { |
| 1015 | goto exit_image_format; |
| 1016 | } else { |
Simon Glass | 30f3333 | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 1017 | fit_hdr = buf; |
| 1018 | if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { |
| 1019 | puts("Bad FIT image format\n"); |
| 1020 | return 1; |
| 1021 | } |
| 1022 | |
| 1023 | if (!fit_uname) { |
| 1024 | /* If confname is empty, use the default */ |
| 1025 | if (confname && *confname) |
| 1026 | noffset = fit_conf_get_node(fit_hdr, confname); |
| 1027 | else |
| 1028 | noffset = fit_conf_get_node(fit_hdr, NULL); |
| 1029 | if (noffset < 0) { |
| 1030 | if (!confname) |
| 1031 | goto fallback; |
| 1032 | printf("Could not find config %s\n", confname); |
| 1033 | return 1; |
| 1034 | } |
| 1035 | |
| 1036 | if (verify && fit_config_verify(fit_hdr, noffset)) |
| 1037 | return 1; |
| 1038 | |
| 1039 | noffset = fit_conf_get_prop_node(fit_hdr, |
| 1040 | noffset, |
| 1041 | FIT_SCRIPT_PROP, |
| 1042 | IH_PHASE_NONE); |
| 1043 | if (noffset < 0) { |
| 1044 | if (!confname) |
| 1045 | goto fallback; |
| 1046 | printf("Could not find script in %s\n", confname); |
| 1047 | return 1; |
| 1048 | } |
| 1049 | } else { |
| 1050 | fallback: |
| 1051 | if (!fit_uname || !*fit_uname) |
| 1052 | fit_uname = get_default_image(fit_hdr); |
| 1053 | if (!fit_uname) { |
| 1054 | puts("No FIT subimage unit name\n"); |
| 1055 | return 1; |
| 1056 | } |
| 1057 | |
| 1058 | /* get script component image node offset */ |
| 1059 | noffset = fit_image_get_node(fit_hdr, fit_uname); |
| 1060 | if (noffset < 0) { |
| 1061 | printf("Can't find '%s' FIT subimage\n", |
| 1062 | fit_uname); |
| 1063 | return 1; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | if (!fit_image_check_type(fit_hdr, noffset, |
| 1068 | IH_TYPE_SCRIPT)) { |
| 1069 | puts("Not a image image\n"); |
| 1070 | return 1; |
| 1071 | } |
| 1072 | |
| 1073 | /* verify integrity */ |
| 1074 | if (verify && !fit_image_verify(fit_hdr, noffset)) { |
| 1075 | puts("Bad Data Hash\n"); |
| 1076 | return 1; |
| 1077 | } |
| 1078 | |
| 1079 | /* get script subimage data address and length */ |
Tobias Waldekranz | e45bba5 | 2023-02-16 16:33:47 +0100 | [diff] [blame] | 1080 | if (fit_image_get_data_and_size(fit_hdr, noffset, |
| 1081 | &fit_data, &fit_len)) { |
Simon Glass | 30f3333 | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 1082 | puts("Could not find script subimage data\n"); |
| 1083 | return 1; |
| 1084 | } |
| 1085 | |
| 1086 | data = (u32 *)fit_data; |
| 1087 | len = (ulong)fit_len; |
| 1088 | } |
| 1089 | break; |
| 1090 | default: |
Marek Vasut | 771cb4d | 2023-02-27 20:56:31 +0100 | [diff] [blame] | 1091 | goto exit_image_format; |
Simon Glass | 30f3333 | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | *datap = (char *)data; |
| 1095 | *lenp = len; |
| 1096 | |
| 1097 | return 0; |
Marek Vasut | 771cb4d | 2023-02-27 20:56:31 +0100 | [diff] [blame] | 1098 | |
| 1099 | exit_image_format: |
| 1100 | puts("Wrong image format for \"source\" command\n"); |
| 1101 | return -EPERM; |
Simon Glass | 30f3333 | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 1102 | } |