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