Simon Glass | 4d7237b | 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 | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 11 | #include <config.h> |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 12 | #include <bootstage.h> |
| 13 | #include <cpu_func.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 14 | #include <display_options.h> |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 15 | #include <env.h> |
| 16 | #include <fpga.h> |
| 17 | #include <image.h> |
Andy Shevchenko | e3b3ad9 | 2021-11-08 21:03:38 +0300 | [diff] [blame] | 18 | #include <init.h> |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 19 | #include <log.h> |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 20 | #include <mapmem.h> |
Simon Glass | 4a8a8a1 | 2021-09-25 07:03:17 -0600 | [diff] [blame] | 21 | #include <rtc.h> |
Simon Glass | 4d7237b | 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 | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Simon Glass | 4d7237b | 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 | bb7d3bb | 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 | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 45 | { |
Simon Glass | bb7d3bb | 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 | 4d7237b | 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 | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 79 | genimg_get_arch_name(arch)); |
Simon Glass | 4d7237b | 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 | 4d7237b | 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 | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 95 | int flags) |
Simon Glass | 4d7237b | 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 | f816c42 | 2024-03-26 23:13:11 +0100 | [diff] [blame] | 110 | phys_addr_t env_get_bootm_low(void) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 111 | { |
| 112 | char *s = env_get("bootm_low"); |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 113 | |
Marek Vasut | f816c42 | 2024-03-26 23:13:11 +0100 | [diff] [blame] | 114 | if (s) |
| 115 | return simple_strtoull(s, NULL, 16); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 116 | |
Tom Rini | bb4dd96 | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 117 | #if defined(CFG_SYS_SDRAM_BASE) |
| 118 | return CFG_SYS_SDRAM_BASE; |
Simon Glass | 4d7237b | 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 | b140bbd | 2024-03-26 23:13:13 +0100 | [diff] [blame] | 128 | phys_addr_t start, low; |
| 129 | phys_size_t size; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 130 | char *s = env_get("bootm_size"); |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 131 | |
Marek Vasut | 1d02ed7 | 2024-03-26 23:13:12 +0100 | [diff] [blame] | 132 | if (s) |
| 133 | return simple_strtoull(s, NULL, 16); |
Simon Glass | 4d7237b | 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 | b140bbd | 2024-03-26 23:13:13 +0100 | [diff] [blame] | 143 | low = simple_strtoull(s, NULL, 16); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 144 | else |
Marek Vasut | b140bbd | 2024-03-26 23:13:13 +0100 | [diff] [blame] | 145 | low = start; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 146 | |
Marek Vasut | b140bbd | 2024-03-26 23:13:13 +0100 | [diff] [blame] | 147 | return size - (low - start); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | phys_size_t env_get_bootm_mapsize(void) |
| 151 | { |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 152 | char *s = env_get("bootm_mapsize"); |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 153 | |
Marek Vasut | ddf73a8 | 2024-03-26 23:13:14 +0100 | [diff] [blame] | 154 | if (s) |
| 155 | return simple_strtoull(s, NULL, 16); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 156 | |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 157 | #if defined(CFG_SYS_BOOTMAPSZ) |
| 158 | return CFG_SYS_BOOTMAPSZ; |
Simon Glass | 4d7237b | 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 | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 169 | if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) { |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 170 | if (to > from) { |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 171 | from += len; |
| 172 | to += len; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 173 | } |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 174 | while (len > 0) { |
| 175 | size_t tail = (len > chunksz) ? chunksz : len; |
| 176 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 177 | schedule(); |
Simon Glass | 7ae16bb | 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 | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 188 | } |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 189 | } else { |
| 190 | memmove(to, from, len); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 191 | } |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 192 | } |
| 193 | |
Simon Glass | 4e8ec47 | 2023-11-18 14:04:57 -0700 | [diff] [blame] | 194 | ulong genimg_get_kernel_addr_fit(const char *const img_addr, |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 195 | const char **fit_uname_config, |
| 196 | const char **fit_uname_kernel) |
Simon Glass | 4d7237b | 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 | 0f78138 | 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 | 4d7237b | 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 | 0f78138 | 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 | 4d7237b | 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 | 4d7237b | 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 | 0f78138 | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 253 | if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) { |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 254 | const struct legacy_img_hdr *hdr; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 255 | |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 256 | hdr = (const struct legacy_img_hdr *)img_addr; |
Simon Glass | 0f78138 | 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 | 88ad0c1 | 2023-02-06 00:50:05 +0100 | [diff] [blame] | 265 | is_android_boot_image_header(img_addr)) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 266 | return IMAGE_FORMAT_ANDROID; |
Simon Glass | 4d7237b | 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 | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 282 | int genimg_has_config(struct bootm_headers *images) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 283 | { |
Simon Glass | 0f78138 | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 284 | if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 285 | return 1; |
Simon Glass | 0f78138 | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 286 | |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /** |
Simon Glass | ccda2fa | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 291 | * select_ramdisk() - Select and locate the ramdisk to use |
| 292 | * |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 293 | * @images: pointer to the bootm images structure |
Simon Glass | 664696f | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 294 | * @select: name of ramdisk to select, or hex address, NULL for any |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 295 | * @arch: expected ramdisk architecture |
Simon Glass | ccda2fa | 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 | 47b4c02 | 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 | ccda2fa | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 299 | * other -ve value on other error |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 300 | */ |
Simon Glass | df00afa | 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 | ccda2fa | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 302 | ulong *rd_datap, ulong *rd_lenp) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 303 | { |
Simon Glass | f2d23de | 2022-08-28 12:32:49 -0600 | [diff] [blame] | 304 | const char *fit_uname_config; |
| 305 | const char *fit_uname_ramdisk; |
Simon Glass | 973b893 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 306 | bool done_select = !select; |
Simon Glass | 664696f | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 307 | bool done = false; |
Simon Glass | f2d23de | 2022-08-28 12:32:49 -0600 | [diff] [blame] | 308 | int rd_noffset; |
Tom Rini | 1ca3c64 | 2023-04-05 19:48:56 -0400 | [diff] [blame] | 309 | ulong rd_addr = 0; |
Simon Glass | ccda2fa | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 310 | char *buf; |
Simon Glass | 2de5b01 | 2021-09-25 19:43:36 -0600 | [diff] [blame] | 311 | |
Simon Glass | 973b893 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 312 | if (CONFIG_IS_ENABLED(FIT)) { |
Simon Glass | f2d23de | 2022-08-28 12:32:49 -0600 | [diff] [blame] | 313 | fit_uname_config = images->fit_uname_cfg; |
| 314 | fit_uname_ramdisk = NULL; |
Simon Glass | 2de5b01 | 2021-09-25 19:43:36 -0600 | [diff] [blame] | 315 | |
Tom Rini | bc7d9d2 | 2021-12-20 09:36:32 -0500 | [diff] [blame] | 316 | if (select) { |
| 317 | ulong default_addr; |
Simon Glass | 4d7237b | 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 | 92c586f | 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 | 60d7154 | 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 | 973b893 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 333 | done_select = true; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 334 | } else if (fit_parse_subimage(select, default_addr, |
Simon Glass | 60d7154 | 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 | 973b893 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 339 | done_select = true; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | if (!done_select) { |
Simon Glass | 92c586f | 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 | 973b893 | 2022-08-28 12:32:51 -0600 | [diff] [blame] | 346 | } |
Simon Glass | 92c586f | 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 | bc7d9d2 | 2021-12-20 09:36:32 -0500 | [diff] [blame] | 351 | */ |
Simon Glass | 92c586f | 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 | 2de5b01 | 2021-09-25 19:43:36 -0600 | [diff] [blame] | 360 | |
Simon Glass | 92c586f | 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 | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 370 | const struct legacy_img_hdr *rd_hdr; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 371 | |
Simon Glass | 92c586f | 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 | bc7d9d2 | 2021-12-20 09:36:32 -0500 | [diff] [blame] | 374 | |
Simon Glass | 92c586f | 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 | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 378 | |
Simon Glass | 92c586f | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 379 | if (!rd_hdr) |
| 380 | return -ENOENT; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 381 | |
Simon Glass | 92c586f | 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; |
| 385 | } |
| 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; |
Simon Glass | dc0aa04 | 2022-08-28 12:32:46 -0600 | [diff] [blame] | 398 | |
Simon Glass | 92c586f | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 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; |
Simon Glass | 664696f | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 403 | } |
Simon Glass | 92c586f | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 404 | break; |
| 405 | case IMAGE_FORMAT_ANDROID: |
| 406 | if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) { |
Simon Glass | 92c586f | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 407 | int ret; |
Safae Ouajih | 51c981b | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 408 | if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) { |
Roman Stratiienko | 826e0be | 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 | 51c981b | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 411 | void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0); |
Roman Stratiienko | 826e0be | 2024-05-23 07:06:07 +0000 | [diff] [blame] | 412 | void *ramdisk_img; |
Safae Ouajih | 51c981b | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 413 | |
Roman Stratiienko | 826e0be | 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 | 51c981b | 2023-02-06 00:50:17 +0100 | [diff] [blame] | 420 | rd_datap, rd_lenp); |
| 421 | unmap_sysmem(vendor_boot_img); |
Roman Stratiienko | 826e0be | 2024-05-23 07:06:07 +0000 | [diff] [blame] | 422 | unmap_sysmem(ramdisk_img); |
Safae Ouajih | 51c981b | 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 | |
Simon Glass | 92c586f | 2022-08-28 12:32:52 -0600 | [diff] [blame] | 430 | if (ret) |
| 431 | return ret; |
| 432 | done = true; |
| 433 | } |
| 434 | break; |
| 435 | } |
Simon Glass | 664696f | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 436 | |
| 437 | if (!done) { |
| 438 | if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) { |
| 439 | char *end = NULL; |
Simon Glass | 0f78138 | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 440 | |
Simon Glass | 664696f | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 441 | if (select) |
| 442 | end = strchr(select, ':'); |
| 443 | if (end) { |
| 444 | *rd_lenp = hextoul(++end, NULL); |
| 445 | *rd_datap = rd_addr; |
| 446 | done = true; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 447 | } |
Simon Glass | 664696f | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | if (!done) { |
Simon Glass | 0f78138 | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 451 | puts("Wrong Ramdisk Image Format\n"); |
Simon Glass | ccda2fa | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 452 | return -EINVAL; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 453 | } |
Simon Glass | 664696f | 2022-08-28 12:32:47 -0600 | [diff] [blame] | 454 | } |
Simon Glass | ccda2fa | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Simon Glass | 8eef77e | 2023-11-18 14:05:06 -0700 | [diff] [blame] | 459 | int boot_get_ramdisk(char const *select, struct bootm_headers *images, |
| 460 | uint arch, ulong *rd_start, ulong *rd_end) |
Simon Glass | ccda2fa | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 461 | { |
| 462 | ulong rd_data, rd_len; |
Simon Glass | ccda2fa | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 463 | |
| 464 | *rd_start = 0; |
| 465 | *rd_end = 0; |
| 466 | |
Simon Glass | ccda2fa | 2021-09-25 19:43:37 -0600 | [diff] [blame] | 467 | /* |
| 468 | * Look for a '-' which indicates to ignore the |
| 469 | * ramdisk argument |
| 470 | */ |
| 471 | if (select && strcmp(select, "-") == 0) { |
| 472 | debug("## Skipping init Ramdisk\n"); |
| 473 | rd_len = 0; |
| 474 | rd_data = 0; |
| 475 | } else if (select || genimg_has_config(images)) { |
| 476 | int ret; |
| 477 | |
| 478 | ret = select_ramdisk(images, select, arch, &rd_data, &rd_len); |
| 479 | if (ret == -ENOPKG) |
| 480 | return 0; |
| 481 | else if (ret) |
| 482 | return ret; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 483 | } else if (images->legacy_hdr_valid && |
| 484 | image_check_type(&images->legacy_hdr_os_copy, |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 485 | IH_TYPE_MULTI)) { |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 486 | /* |
| 487 | * Now check if we have a legacy mult-component image, |
| 488 | * get second entry data start address and len. |
| 489 | */ |
| 490 | bootstage_mark(BOOTSTAGE_ID_RAMDISK); |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 491 | printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n", |
| 492 | (ulong)images->legacy_hdr_os); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 493 | |
| 494 | image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len); |
| 495 | } else { |
| 496 | /* |
| 497 | * no initrd image |
| 498 | */ |
| 499 | bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK); |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 500 | rd_len = 0; |
| 501 | rd_data = 0; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | if (!rd_data) { |
| 505 | debug("## No init Ramdisk\n"); |
| 506 | } else { |
| 507 | *rd_start = rd_data; |
| 508 | *rd_end = rd_data + rd_len; |
| 509 | } |
| 510 | debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n", |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 511 | *rd_start, *rd_end); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 516 | /** |
| 517 | * boot_ramdisk_high - relocate init ramdisk |
| 518 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
| 519 | * @rd_data: ramdisk data start address |
| 520 | * @rd_len: ramdisk data length |
| 521 | * @initrd_start: pointer to a ulong variable, will hold final init ramdisk |
| 522 | * start address (after possible relocation) |
| 523 | * @initrd_end: pointer to a ulong variable, will hold final init ramdisk |
| 524 | * end address (after possible relocation) |
| 525 | * |
| 526 | * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment |
| 527 | * variable and if requested ramdisk data is moved to a specified location. |
| 528 | * |
| 529 | * Initrd_start and initrd_end are set to final (after relocation) ramdisk |
| 530 | * start/end addresses if ramdisk image start and len were provided, |
| 531 | * otherwise set initrd_start and initrd_end set to zeros. |
| 532 | * |
| 533 | * returns: |
| 534 | * 0 - success |
| 535 | * -1 - failure |
| 536 | */ |
| 537 | int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len, |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 538 | ulong *initrd_start, ulong *initrd_end) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 539 | { |
| 540 | char *s; |
Marek Vasut | f816c42 | 2024-03-26 23:13:11 +0100 | [diff] [blame] | 541 | phys_addr_t initrd_high; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 542 | int initrd_copy_to_ram = 1; |
| 543 | |
| 544 | s = env_get("initrd_high"); |
| 545 | if (s) { |
| 546 | /* a value of "no" or a similar string will act like 0, |
| 547 | * turning the "load high" feature off. This is intentional. |
| 548 | */ |
| 549 | initrd_high = hextoul(s, NULL); |
| 550 | if (initrd_high == ~0) |
| 551 | initrd_copy_to_ram = 0; |
| 552 | } else { |
| 553 | initrd_high = env_get_bootm_mapsize() + env_get_bootm_low(); |
| 554 | } |
| 555 | |
Marek Vasut | f816c42 | 2024-03-26 23:13:11 +0100 | [diff] [blame] | 556 | debug("## initrd_high = 0x%llx, copy_to_ram = %d\n", |
| 557 | (u64)initrd_high, initrd_copy_to_ram); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 558 | |
| 559 | if (rd_data) { |
| 560 | if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */ |
| 561 | debug(" in-place initrd\n"); |
| 562 | *initrd_start = rd_data; |
| 563 | *initrd_end = rd_data + rd_len; |
| 564 | lmb_reserve(lmb, rd_data, rd_len); |
| 565 | } else { |
| 566 | if (initrd_high) |
| 567 | *initrd_start = (ulong)lmb_alloc_base(lmb, |
| 568 | rd_len, 0x1000, initrd_high); |
| 569 | else |
| 570 | *initrd_start = (ulong)lmb_alloc(lmb, rd_len, |
| 571 | 0x1000); |
| 572 | |
| 573 | if (*initrd_start == 0) { |
| 574 | puts("ramdisk - allocation error\n"); |
| 575 | goto error; |
| 576 | } |
| 577 | bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK); |
| 578 | |
| 579 | *initrd_end = *initrd_start + rd_len; |
| 580 | printf(" Loading Ramdisk to %08lx, end %08lx ... ", |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 581 | *initrd_start, *initrd_end); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 582 | |
| 583 | memmove_wd((void *)*initrd_start, |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 584 | (void *)rd_data, rd_len, CHUNKSZ); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 585 | |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 586 | /* |
| 587 | * Ensure the image is flushed to memory to handle |
| 588 | * AMP boot scenarios in which we might not be |
| 589 | * HW cache coherent |
| 590 | */ |
Simon Glass | 0f78138 | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 591 | if (IS_ENABLED(CONFIG_MP)) { |
| 592 | flush_cache((unsigned long)*initrd_start, |
| 593 | ALIGN(rd_len, ARCH_DMA_MINALIGN)); |
| 594 | } |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 595 | puts("OK\n"); |
| 596 | } |
| 597 | } else { |
| 598 | *initrd_start = 0; |
| 599 | *initrd_end = 0; |
| 600 | } |
| 601 | debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n", |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 602 | *initrd_start, *initrd_end); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 603 | |
| 604 | return 0; |
| 605 | |
| 606 | error: |
| 607 | return -1; |
| 608 | } |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 609 | |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 610 | int boot_get_setup(struct bootm_headers *images, u8 arch, |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 611 | ulong *setup_start, ulong *setup_len) |
| 612 | { |
Simon Glass | 0f78138 | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 613 | if (!CONFIG_IS_ENABLED(FIT)) |
| 614 | return -ENOENT; |
| 615 | |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 616 | return boot_get_setup_fit(images, arch, setup_start, setup_len); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 617 | } |
| 618 | |
Simon Glass | 01080ab | 2023-11-18 14:05:11 -0700 | [diff] [blame] | 619 | int boot_get_fpga(struct bootm_headers *images) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 620 | { |
| 621 | ulong tmp_img_addr, img_data, img_len; |
| 622 | void *buf; |
| 623 | int conf_noffset; |
| 624 | int fit_img_result; |
| 625 | const char *uname, *name; |
| 626 | int err; |
| 627 | int devnum = 0; /* TODO support multi fpga platforms */ |
| 628 | |
Simon Glass | 0f78138 | 2021-09-25 19:43:35 -0600 | [diff] [blame] | 629 | if (!IS_ENABLED(CONFIG_FPGA)) |
| 630 | return -ENOSYS; |
| 631 | |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 632 | /* Check to see if the images struct has a FIT configuration */ |
| 633 | if (!genimg_has_config(images)) { |
| 634 | debug("## FIT configuration was not specified\n"); |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Obtain the os FIT header from the images struct |
| 640 | */ |
| 641 | tmp_img_addr = map_to_sysmem(images->fit_hdr_os); |
| 642 | buf = map_sysmem(tmp_img_addr, 0); |
| 643 | /* |
| 644 | * Check image type. For FIT images get FIT node |
| 645 | * and attempt to locate a generic binary. |
| 646 | */ |
| 647 | switch (genimg_get_format(buf)) { |
| 648 | case IMAGE_FORMAT_FIT: |
| 649 | conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg); |
| 650 | |
| 651 | uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0, |
| 652 | NULL); |
| 653 | if (!uname) { |
| 654 | debug("## FPGA image is not specified\n"); |
| 655 | return 0; |
| 656 | } |
| 657 | fit_img_result = fit_image_load(images, |
| 658 | tmp_img_addr, |
| 659 | (const char **)&uname, |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 660 | &images->fit_uname_cfg, |
Simon Glass | 01080ab | 2023-11-18 14:05:11 -0700 | [diff] [blame] | 661 | IH_ARCH_DEFAULT, |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 662 | IH_TYPE_FPGA, |
| 663 | BOOTSTAGE_ID_FPGA_INIT, |
| 664 | FIT_LOAD_OPTIONAL_NON_ZERO, |
| 665 | &img_data, &img_len); |
| 666 | |
| 667 | debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n", |
| 668 | uname, img_data, img_len); |
| 669 | |
| 670 | if (fit_img_result < 0) { |
| 671 | /* Something went wrong! */ |
| 672 | return fit_img_result; |
| 673 | } |
| 674 | |
| 675 | if (!fpga_is_partial_data(devnum, img_len)) { |
| 676 | name = "full"; |
| 677 | err = fpga_loadbitstream(devnum, (char *)img_data, |
| 678 | img_len, BIT_FULL); |
| 679 | if (err) |
| 680 | err = fpga_load(devnum, (const void *)img_data, |
Oleksandr Suvorov | 4ff163d | 2022-07-22 17:16:07 +0300 | [diff] [blame] | 681 | img_len, BIT_FULL, 0); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 682 | } else { |
| 683 | name = "partial"; |
| 684 | err = fpga_loadbitstream(devnum, (char *)img_data, |
| 685 | img_len, BIT_PARTIAL); |
| 686 | if (err) |
| 687 | err = fpga_load(devnum, (const void *)img_data, |
Oleksandr Suvorov | 4ff163d | 2022-07-22 17:16:07 +0300 | [diff] [blame] | 688 | img_len, BIT_PARTIAL, 0); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | if (err) |
| 692 | return err; |
| 693 | |
| 694 | printf(" Programming %s bitstream... OK\n", name); |
| 695 | break; |
| 696 | default: |
| 697 | printf("The given image format is not supported (corrupt?)\n"); |
| 698 | return 1; |
| 699 | } |
| 700 | |
| 701 | return 0; |
| 702 | } |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 703 | |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 704 | static void fit_loadable_process(u8 img_type, |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 705 | ulong img_data, |
| 706 | ulong img_len) |
| 707 | { |
| 708 | int i; |
| 709 | const unsigned int count = |
| 710 | ll_entry_count(struct fit_loadable_tbl, fit_loadable); |
| 711 | struct fit_loadable_tbl *fit_loadable_handler = |
| 712 | ll_entry_start(struct fit_loadable_tbl, fit_loadable); |
| 713 | /* For each loadable handler */ |
| 714 | for (i = 0; i < count; i++, fit_loadable_handler++) |
| 715 | /* matching this type */ |
| 716 | if (fit_loadable_handler->type == img_type) |
| 717 | /* call that handler with this image data */ |
| 718 | fit_loadable_handler->handler(img_data, img_len); |
| 719 | } |
| 720 | |
Simon Glass | 4fdc559 | 2023-11-18 14:05:12 -0700 | [diff] [blame] | 721 | int boot_get_loadable(struct bootm_headers *images) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 722 | { |
| 723 | /* |
| 724 | * These variables are used to hold the current image location |
| 725 | * in system memory. |
| 726 | */ |
| 727 | ulong tmp_img_addr; |
| 728 | /* |
| 729 | * These two variables are requirements for fit_image_load, but |
| 730 | * their values are not used |
| 731 | */ |
| 732 | ulong img_data, img_len; |
| 733 | void *buf; |
| 734 | int loadables_index; |
| 735 | int conf_noffset; |
| 736 | int fit_img_result; |
| 737 | const char *uname; |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 738 | u8 img_type; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 739 | |
| 740 | /* Check to see if the images struct has a FIT configuration */ |
| 741 | if (!genimg_has_config(images)) { |
| 742 | debug("## FIT configuration was not specified\n"); |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Obtain the os FIT header from the images struct |
| 748 | */ |
| 749 | tmp_img_addr = map_to_sysmem(images->fit_hdr_os); |
| 750 | buf = map_sysmem(tmp_img_addr, 0); |
| 751 | /* |
| 752 | * Check image type. For FIT images get FIT node |
| 753 | * and attempt to locate a generic binary. |
| 754 | */ |
| 755 | switch (genimg_get_format(buf)) { |
| 756 | case IMAGE_FORMAT_FIT: |
| 757 | conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg); |
| 758 | |
| 759 | for (loadables_index = 0; |
| 760 | uname = fdt_stringlist_get(buf, conf_noffset, |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 761 | FIT_LOADABLE_PROP, |
| 762 | loadables_index, NULL), uname; |
| 763 | loadables_index++) { |
| 764 | fit_img_result = fit_image_load(images, tmp_img_addr, |
| 765 | &uname, |
| 766 | &images->fit_uname_cfg, |
Simon Glass | 4fdc559 | 2023-11-18 14:05:12 -0700 | [diff] [blame] | 767 | IH_ARCH_DEFAULT, |
| 768 | IH_TYPE_LOADABLE, |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 769 | BOOTSTAGE_ID_FIT_LOADABLE_START, |
| 770 | FIT_LOAD_OPTIONAL_NON_ZERO, |
| 771 | &img_data, &img_len); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 772 | if (fit_img_result < 0) { |
| 773 | /* Something went wrong! */ |
| 774 | return fit_img_result; |
| 775 | } |
| 776 | |
| 777 | fit_img_result = fit_image_get_node(buf, uname); |
| 778 | if (fit_img_result < 0) { |
| 779 | /* Something went wrong! */ |
| 780 | return fit_img_result; |
| 781 | } |
| 782 | fit_img_result = fit_image_get_type(buf, |
| 783 | fit_img_result, |
| 784 | &img_type); |
| 785 | if (fit_img_result < 0) { |
| 786 | /* Something went wrong! */ |
| 787 | return fit_img_result; |
| 788 | } |
| 789 | |
| 790 | fit_loadable_process(img_type, img_data, img_len); |
| 791 | } |
| 792 | break; |
| 793 | default: |
| 794 | printf("The given image format is not supported (corrupt?)\n"); |
| 795 | return 1; |
| 796 | } |
| 797 | |
| 798 | return 0; |
| 799 | } |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 800 | |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 801 | /** |
| 802 | * boot_get_cmdline - allocate and initialize kernel cmdline |
| 803 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
| 804 | * @cmd_start: pointer to a ulong variable, will hold cmdline start |
| 805 | * @cmd_end: pointer to a ulong variable, will hold cmdline end |
| 806 | * |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 807 | * This allocates space for kernel command line below |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 808 | * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment |
| 809 | * variable is present its contents is copied to allocated kernel |
| 810 | * command line. |
| 811 | * |
| 812 | * returns: |
| 813 | * 0 - success |
| 814 | * -1 - failure |
| 815 | */ |
| 816 | int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end) |
| 817 | { |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 818 | int barg; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 819 | char *cmdline; |
| 820 | char *s; |
| 821 | |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 822 | /* |
| 823 | * Help the compiler detect that this function is only called when |
| 824 | * CONFIG_SYS_BOOT_GET_CMDLINE is enabled |
| 825 | */ |
| 826 | if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) |
| 827 | return 0; |
| 828 | |
| 829 | barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE); |
| 830 | cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf, |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 831 | env_get_bootm_mapsize() + env_get_bootm_low()); |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 832 | if (!cmdline) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 833 | return -1; |
| 834 | |
| 835 | s = env_get("bootargs"); |
| 836 | if (!s) |
| 837 | s = ""; |
| 838 | |
| 839 | strcpy(cmdline, s); |
| 840 | |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 841 | *cmd_start = (ulong)cmdline; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 842 | *cmd_end = *cmd_start + strlen(cmdline); |
| 843 | |
| 844 | debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end); |
| 845 | |
| 846 | return 0; |
| 847 | } |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 848 | |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 849 | /** |
| 850 | * boot_get_kbd - allocate and initialize kernel copy of board info |
| 851 | * @lmb: pointer to lmb handle, will be used for memory mgmt |
| 852 | * @kbd: double pointer to board info data |
| 853 | * |
| 854 | * boot_get_kbd() allocates space for kernel copy of board info data below |
| 855 | * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized |
| 856 | * with the current u-boot board info data. |
| 857 | * |
| 858 | * returns: |
| 859 | * 0 - success |
| 860 | * -1 - failure |
| 861 | */ |
| 862 | int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd) |
| 863 | { |
| 864 | *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb, |
| 865 | sizeof(struct bd_info), |
| 866 | 0xf, |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 867 | env_get_bootm_mapsize() + |
| 868 | env_get_bootm_low()); |
| 869 | if (!*kbd) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 870 | return -1; |
| 871 | |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 872 | **kbd = *gd->bd; |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 873 | |
| 874 | debug("## kernel board info at 0x%08lx\n", (ulong)*kbd); |
| 875 | |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 876 | if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI)) |
Simon Glass | bc0c4db | 2021-09-25 07:03:20 -0600 | [diff] [blame] | 877 | do_bdinfo(NULL, 0, 0, NULL); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 878 | |
| 879 | return 0; |
| 880 | } |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 881 | |
Simon Glass | df00afa | 2022-09-06 20:26:50 -0600 | [diff] [blame] | 882 | int image_setup_linux(struct bootm_headers *images) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 883 | { |
| 884 | ulong of_size = images->ft_len; |
| 885 | char **of_flat_tree = &images->ft_addr; |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 886 | struct lmb *lmb = images_lmb(images); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 887 | int ret; |
| 888 | |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 889 | /* This function cannot be called without lmb support */ |
Simon Glass | ae7ed57 | 2023-02-05 15:40:13 -0700 | [diff] [blame] | 890 | if (!IS_ENABLED(CONFIG_LMB)) |
Simon Glass | 7ae16bb | 2022-08-28 12:32:53 -0600 | [diff] [blame] | 891 | return -EFAULT; |
Simon Glass | 85c057e | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 892 | if (CONFIG_IS_ENABLED(OF_LIBFDT)) |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 893 | boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree); |
| 894 | |
Simon Glass | b8eb1fe | 2021-09-25 19:43:25 -0600 | [diff] [blame] | 895 | if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) { |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 896 | ret = boot_get_cmdline(lmb, &images->cmdline_start, |
Simon Glass | 60d7154 | 2021-09-25 07:03:16 -0600 | [diff] [blame] | 897 | &images->cmdline_end); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 898 | if (ret) { |
| 899 | puts("ERROR with allocation of cmdline\n"); |
| 900 | return ret; |
| 901 | } |
| 902 | } |
| 903 | |
Simon Glass | 85c057e | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 904 | if (CONFIG_IS_ENABLED(OF_LIBFDT)) { |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 905 | ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size); |
| 906 | if (ret) |
| 907 | return ret; |
| 908 | } |
| 909 | |
Simon Glass | 85c057e | 2021-09-25 19:43:21 -0600 | [diff] [blame] | 910 | if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) { |
Simon Glass | 30762dd | 2023-11-12 08:27:44 -0700 | [diff] [blame] | 911 | ret = image_setup_libfdt(images, *of_flat_tree, lmb); |
Simon Glass | 4d7237b | 2021-09-25 07:03:15 -0600 | [diff] [blame] | 912 | if (ret) |
| 913 | return ret; |
| 914 | } |
| 915 | |
| 916 | return 0; |
| 917 | } |
Simon Glass | 4a8a8a1 | 2021-09-25 07:03:17 -0600 | [diff] [blame] | 918 | |
| 919 | void genimg_print_size(uint32_t size) |
| 920 | { |
| 921 | printf("%d Bytes = ", size); |
| 922 | print_size(size, "\n"); |
| 923 | } |
| 924 | |
| 925 | void genimg_print_time(time_t timestamp) |
| 926 | { |
| 927 | struct rtc_time tm; |
| 928 | |
| 929 | rtc_to_tm(timestamp, &tm); |
| 930 | printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n", |
| 931 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 932 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 933 | } |
Simon Glass | daee3ba | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 934 | |
| 935 | /** |
| 936 | * get_default_image() - Return default property from /images |
| 937 | * |
| 938 | * Return: Pointer to value of default property (or NULL) |
| 939 | */ |
| 940 | static const char *get_default_image(const void *fit) |
| 941 | { |
| 942 | int images_noffset; |
| 943 | |
| 944 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 945 | if (images_noffset < 0) |
| 946 | return NULL; |
| 947 | |
| 948 | return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL); |
| 949 | } |
| 950 | |
| 951 | int image_locate_script(void *buf, int size, const char *fit_uname, |
| 952 | const char *confname, char **datap, uint *lenp) |
| 953 | { |
| 954 | const struct legacy_img_hdr *hdr; |
| 955 | const void *fit_data; |
| 956 | const void *fit_hdr; |
| 957 | size_t fit_len; |
| 958 | int noffset; |
| 959 | int verify; |
| 960 | ulong len; |
| 961 | u32 *data; |
| 962 | |
| 963 | verify = env_get_yesno("verify"); |
| 964 | |
| 965 | switch (genimg_get_format(buf)) { |
| 966 | case IMAGE_FORMAT_LEGACY: |
Marek Vasut | fb59eca | 2023-02-27 20:56:31 +0100 | [diff] [blame] | 967 | if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) { |
| 968 | goto exit_image_format; |
| 969 | } else { |
Simon Glass | daee3ba | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 970 | hdr = buf; |
| 971 | |
| 972 | if (!image_check_magic(hdr)) { |
| 973 | puts("Bad magic number\n"); |
| 974 | return 1; |
| 975 | } |
| 976 | |
| 977 | if (!image_check_hcrc(hdr)) { |
| 978 | puts("Bad header crc\n"); |
| 979 | return 1; |
| 980 | } |
| 981 | |
| 982 | if (verify) { |
| 983 | if (!image_check_dcrc(hdr)) { |
| 984 | puts("Bad data crc\n"); |
| 985 | return 1; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | if (!image_check_type(hdr, IH_TYPE_SCRIPT)) { |
| 990 | puts("Bad image type\n"); |
| 991 | return 1; |
| 992 | } |
| 993 | |
| 994 | /* get length of script */ |
| 995 | data = (u32 *)image_get_data(hdr); |
| 996 | |
| 997 | len = uimage_to_cpu(*data); |
| 998 | if (!len) { |
| 999 | puts("Empty Script\n"); |
| 1000 | return 1; |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | * scripts are just multi-image files with one |
| 1005 | * component, so seek past the zero-terminated sequence |
| 1006 | * of image lengths to get to the actual image data |
| 1007 | */ |
| 1008 | while (*data++); |
| 1009 | } |
| 1010 | break; |
| 1011 | case IMAGE_FORMAT_FIT: |
Marek Vasut | fb59eca | 2023-02-27 20:56:31 +0100 | [diff] [blame] | 1012 | if (!IS_ENABLED(CONFIG_FIT)) { |
| 1013 | goto exit_image_format; |
| 1014 | } else { |
Simon Glass | daee3ba | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 1015 | fit_hdr = buf; |
| 1016 | if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { |
| 1017 | puts("Bad FIT image format\n"); |
| 1018 | return 1; |
| 1019 | } |
| 1020 | |
| 1021 | if (!fit_uname) { |
| 1022 | /* If confname is empty, use the default */ |
| 1023 | if (confname && *confname) |
| 1024 | noffset = fit_conf_get_node(fit_hdr, confname); |
| 1025 | else |
| 1026 | noffset = fit_conf_get_node(fit_hdr, NULL); |
| 1027 | if (noffset < 0) { |
| 1028 | if (!confname) |
| 1029 | goto fallback; |
| 1030 | printf("Could not find config %s\n", confname); |
| 1031 | return 1; |
| 1032 | } |
| 1033 | |
| 1034 | if (verify && fit_config_verify(fit_hdr, noffset)) |
| 1035 | return 1; |
| 1036 | |
| 1037 | noffset = fit_conf_get_prop_node(fit_hdr, |
| 1038 | noffset, |
| 1039 | FIT_SCRIPT_PROP, |
| 1040 | IH_PHASE_NONE); |
| 1041 | if (noffset < 0) { |
| 1042 | if (!confname) |
| 1043 | goto fallback; |
| 1044 | printf("Could not find script in %s\n", confname); |
| 1045 | return 1; |
| 1046 | } |
| 1047 | } else { |
| 1048 | fallback: |
| 1049 | if (!fit_uname || !*fit_uname) |
| 1050 | fit_uname = get_default_image(fit_hdr); |
| 1051 | if (!fit_uname) { |
| 1052 | puts("No FIT subimage unit name\n"); |
| 1053 | return 1; |
| 1054 | } |
| 1055 | |
| 1056 | /* get script component image node offset */ |
| 1057 | noffset = fit_image_get_node(fit_hdr, fit_uname); |
| 1058 | if (noffset < 0) { |
| 1059 | printf("Can't find '%s' FIT subimage\n", |
| 1060 | fit_uname); |
| 1061 | return 1; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | if (!fit_image_check_type(fit_hdr, noffset, |
| 1066 | IH_TYPE_SCRIPT)) { |
| 1067 | puts("Not a image image\n"); |
| 1068 | return 1; |
| 1069 | } |
| 1070 | |
| 1071 | /* verify integrity */ |
| 1072 | if (verify && !fit_image_verify(fit_hdr, noffset)) { |
| 1073 | puts("Bad Data Hash\n"); |
| 1074 | return 1; |
| 1075 | } |
| 1076 | |
| 1077 | /* get script subimage data address and length */ |
Tobias Waldekranz | 317dbfc | 2023-02-16 16:33:47 +0100 | [diff] [blame] | 1078 | if (fit_image_get_data_and_size(fit_hdr, noffset, |
| 1079 | &fit_data, &fit_len)) { |
Simon Glass | daee3ba | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 1080 | puts("Could not find script subimage data\n"); |
| 1081 | return 1; |
| 1082 | } |
| 1083 | |
| 1084 | data = (u32 *)fit_data; |
| 1085 | len = (ulong)fit_len; |
| 1086 | } |
| 1087 | break; |
| 1088 | default: |
Marek Vasut | fb59eca | 2023-02-27 20:56:31 +0100 | [diff] [blame] | 1089 | goto exit_image_format; |
Simon Glass | daee3ba | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | *datap = (char *)data; |
| 1093 | *lenp = len; |
| 1094 | |
| 1095 | return 0; |
Marek Vasut | fb59eca | 2023-02-27 20:56:31 +0100 | [diff] [blame] | 1096 | |
| 1097 | exit_image_format: |
| 1098 | puts("Wrong image format for \"source\" command\n"); |
| 1099 | return -EPERM; |
Simon Glass | daee3ba | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 1100 | } |