blob: 005d60caf5c9e1850d94abf2be1a5bc95c715b4e [file] [log] [blame]
Simon Glass4d7237b2021-09-25 07:03:15 -06001// 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 Rinidec7ea02024-05-20 13:35:03 -060011#include <config.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060012#include <bootstage.h>
13#include <cpu_func.h>
Simon Glass1ab16922022-07-31 12:28:48 -060014#include <display_options.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060015#include <env.h>
16#include <fpga.h>
17#include <image.h>
Andy Shevchenkoe3b3ad92021-11-08 21:03:38 +030018#include <init.h>
Sughosh Ganu7bdfe122025-06-17 16:13:41 +053019#include <lmb.h>
Simon Glass7ae16bb2022-08-28 12:32:53 -060020#include <log.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060021#include <mapmem.h>
Simon Glass4a8a8a12021-09-25 07:03:17 -060022#include <rtc.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060023#include <watchdog.h>
24#include <asm/cache.h>
25#include <asm/global_data.h>
26
Simon Glass4d7237b2021-09-25 07:03:15 -060027DECLARE_GLOBAL_DATA_PTR;
28
Simon Glass4d7237b2021-09-25 07:03:15 -060029/**
30 * image_get_ramdisk - get and verify ramdisk image
31 * @rd_addr: ramdisk image start address
32 * @arch: expected ramdisk architecture
33 * @verify: checksum verification flag
34 *
35 * image_get_ramdisk() returns a pointer to the verified ramdisk image
36 * header. Routine receives image start address and expected architecture
37 * flag. Verification done covers data and header integrity and os/type/arch
38 * fields checking.
39 *
40 * returns:
41 * pointer to a ramdisk image header, if image was found and valid
42 * otherwise, return NULL
43 */
Simon Glassbb7d3bb2022-09-06 20:26:52 -060044static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
45 int verify)
Simon Glass4d7237b2021-09-25 07:03:15 -060046{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060047 const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -060048
49 if (!image_check_magic(rd_hdr)) {
50 puts("Bad Magic Number\n");
51 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
52 return NULL;
53 }
54
55 if (!image_check_hcrc(rd_hdr)) {
56 puts("Bad Header Checksum\n");
57 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
58 return NULL;
59 }
60
61 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
62 image_print_contents(rd_hdr);
63
64 if (verify) {
65 puts(" Verifying Checksum ... ");
66 if (!image_check_dcrc(rd_hdr)) {
67 puts("Bad Data CRC\n");
68 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
69 return NULL;
70 }
71 puts("OK\n");
72 }
73
74 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
75
76 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
77 !image_check_arch(rd_hdr, arch) ||
78 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
79 printf("No Linux %s Ramdisk Image\n",
Simon Glass60d71542021-09-25 07:03:16 -060080 genimg_get_arch_name(arch));
Simon Glass4d7237b2021-09-25 07:03:15 -060081 bootstage_error(BOOTSTAGE_ID_RAMDISK);
82 return NULL;
83 }
84
85 return rd_hdr;
86}
Simon Glass4d7237b2021-09-25 07:03:15 -060087
88/*****************************************************************************/
89/* Shared dual-format routines */
90/*****************************************************************************/
91ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
92ulong image_save_addr; /* Default Save Address */
93ulong image_save_size; /* Default Save Size (in bytes) */
94
95static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass60d71542021-09-25 07:03:16 -060096 int flags)
Simon Glass4d7237b2021-09-25 07:03:15 -060097{
98 switch (op) {
99 case env_op_create:
100 case env_op_overwrite:
101 image_load_addr = hextoul(value, NULL);
102 break;
103 default:
104 break;
105 }
106
107 return 0;
108}
109U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
110
Marek Vasutf816c422024-03-26 23:13:11 +0100111phys_addr_t env_get_bootm_low(void)
Simon Glass4d7237b2021-09-25 07:03:15 -0600112{
113 char *s = env_get("bootm_low");
Simon Glass60d71542021-09-25 07:03:16 -0600114
Marek Vasutf816c422024-03-26 23:13:11 +0100115 if (s)
116 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600117
Tom Rinibb4dd962022-11-16 13:10:37 -0500118#if defined(CFG_SYS_SDRAM_BASE)
119 return CFG_SYS_SDRAM_BASE;
Simon Glass4d7237b2021-09-25 07:03:15 -0600120#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
121 return gd->bd->bi_dram[0].start;
122#else
123 return 0;
124#endif
125}
126
127phys_size_t env_get_bootm_size(void)
128{
Marek Vasutb140bbd2024-03-26 23:13:13 +0100129 phys_addr_t start, low;
130 phys_size_t size;
Simon Glass4d7237b2021-09-25 07:03:15 -0600131 char *s = env_get("bootm_size");
Simon Glass60d71542021-09-25 07:03:16 -0600132
Marek Vasut1d02ed72024-03-26 23:13:12 +0100133 if (s)
134 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600135
136 start = gd->ram_base;
137 size = gd->ram_size;
138
139 if (start + size > gd->ram_top)
140 size = gd->ram_top - start;
141
142 s = env_get("bootm_low");
143 if (s)
Marek Vasutb140bbd2024-03-26 23:13:13 +0100144 low = simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600145 else
Marek Vasutb140bbd2024-03-26 23:13:13 +0100146 low = start;
Simon Glass4d7237b2021-09-25 07:03:15 -0600147
Marek Vasutb140bbd2024-03-26 23:13:13 +0100148 return size - (low - start);
Simon Glass4d7237b2021-09-25 07:03:15 -0600149}
150
151phys_size_t env_get_bootm_mapsize(void)
152{
Simon Glass4d7237b2021-09-25 07:03:15 -0600153 char *s = env_get("bootm_mapsize");
Simon Glass60d71542021-09-25 07:03:16 -0600154
Marek Vasutddf73a82024-03-26 23:13:14 +0100155 if (s)
156 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600157
Tom Rini6a5dccc2022-11-16 13:10:41 -0500158#if defined(CFG_SYS_BOOTMAPSZ)
159 return CFG_SYS_BOOTMAPSZ;
Simon Glass4d7237b2021-09-25 07:03:15 -0600160#else
161 return env_get_bootm_size();
162#endif
163}
164
165void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
166{
167 if (to == from)
168 return;
169
Simon Glass7ae16bb2022-08-28 12:32:53 -0600170 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600171 if (to > from) {
Simon Glass7ae16bb2022-08-28 12:32:53 -0600172 from += len;
173 to += len;
Simon Glass4d7237b2021-09-25 07:03:15 -0600174 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600175 while (len > 0) {
176 size_t tail = (len > chunksz) ? chunksz : len;
177
Stefan Roese80877fa2022-09-02 14:10:46 +0200178 schedule();
Simon Glass7ae16bb2022-08-28 12:32:53 -0600179 if (to > from) {
180 to -= tail;
181 from -= tail;
182 }
183 memmove(to, from, tail);
184 if (to < from) {
185 to += tail;
186 from += tail;
187 }
188 len -= tail;
Simon Glass4d7237b2021-09-25 07:03:15 -0600189 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600190 } else {
191 memmove(to, from, len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600192 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600193}
194
Simon Glass4e8ec472023-11-18 14:04:57 -0700195ulong genimg_get_kernel_addr_fit(const char *const img_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600196 const char **fit_uname_config,
197 const char **fit_uname_kernel)
Simon Glass4d7237b2021-09-25 07:03:15 -0600198{
199 ulong kernel_addr;
200
201 /* find out kernel image address */
202 if (!img_addr) {
203 kernel_addr = image_load_addr;
204 debug("* kernel: default image load address = 0x%08lx\n",
205 image_load_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600206 } else if (CONFIG_IS_ENABLED(FIT) &&
207 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass4d7237b2021-09-25 07:03:15 -0600208 fit_uname_config)) {
209 debug("* kernel: config '%s' from image at 0x%08lx\n",
210 *fit_uname_config, kernel_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600211 } else if (CONFIG_IS_ENABLED(FIT) &&
212 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
213 fit_uname_kernel)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600214 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
215 *fit_uname_kernel, kernel_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600216 } else {
217 kernel_addr = hextoul(img_addr, NULL);
218 debug("* kernel: cmdline image address = 0x%08lx\n",
219 kernel_addr);
220 }
221
222 return kernel_addr;
223}
224
225/**
226 * genimg_get_kernel_addr() is the simple version of
227 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
228 */
229ulong genimg_get_kernel_addr(char * const img_addr)
230{
231 const char *fit_uname_config = NULL;
232 const char *fit_uname_kernel = NULL;
233
234 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
235 &fit_uname_kernel);
236}
237
238/**
239 * genimg_get_format - get image format type
240 * @img_addr: image start address
241 *
242 * genimg_get_format() checks whether provided address points to a valid
243 * legacy or FIT image.
244 *
245 * New uImage format and FDT blob are based on a libfdt. FDT blob
246 * may be passed directly or embedded in a FIT image. In both situations
247 * genimg_get_format() must be able to dectect libfdt header.
248 *
249 * returns:
250 * image format type or IMAGE_FORMAT_INVALID if no image is present
251 */
252int genimg_get_format(const void *img_addr)
253{
Simon Glass0f781382021-09-25 19:43:35 -0600254 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600255 const struct legacy_img_hdr *hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600256
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600257 hdr = (const struct legacy_img_hdr *)img_addr;
Simon Glass0f781382021-09-25 19:43:35 -0600258 if (image_check_magic(hdr))
259 return IMAGE_FORMAT_LEGACY;
260 }
261 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
262 if (!fdt_check_header(img_addr))
263 return IMAGE_FORMAT_FIT;
264 }
265 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100266 is_android_boot_image_header(img_addr))
Simon Glass4d7237b2021-09-25 07:03:15 -0600267 return IMAGE_FORMAT_ANDROID;
Simon Glass4d7237b2021-09-25 07:03:15 -0600268
269 return IMAGE_FORMAT_INVALID;
270}
271
272/**
273 * fit_has_config - check if there is a valid FIT configuration
274 * @images: pointer to the bootm command headers structure
275 *
276 * fit_has_config() checks if there is a FIT configuration in use
277 * (if FTI support is present).
278 *
279 * returns:
280 * 0, no FIT support or no configuration found
281 * 1, configuration found
282 */
Simon Glassdf00afa2022-09-06 20:26:50 -0600283int genimg_has_config(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600284{
Simon Glass0f781382021-09-25 19:43:35 -0600285 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass4d7237b2021-09-25 07:03:15 -0600286 return 1;
Simon Glass0f781382021-09-25 19:43:35 -0600287
Simon Glass4d7237b2021-09-25 07:03:15 -0600288 return 0;
289}
290
291/**
Simon Glassccda2fa2021-09-25 19:43:37 -0600292 * select_ramdisk() - Select and locate the ramdisk to use
293 *
Simon Glass4d7237b2021-09-25 07:03:15 -0600294 * @images: pointer to the bootm images structure
Simon Glass664696f2022-08-28 12:32:47 -0600295 * @select: name of ramdisk to select, or hex address, NULL for any
Simon Glass4d7237b2021-09-25 07:03:15 -0600296 * @arch: expected ramdisk architecture
Simon Glassccda2fa2021-09-25 19:43:37 -0600297 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
298 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100299 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glassccda2fa2021-09-25 19:43:37 -0600300 * other -ve value on other error
Simon Glass4d7237b2021-09-25 07:03:15 -0600301 */
Simon Glassdf00afa2022-09-06 20:26:50 -0600302static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
Simon Glassccda2fa2021-09-25 19:43:37 -0600303 ulong *rd_datap, ulong *rd_lenp)
Simon Glass4d7237b2021-09-25 07:03:15 -0600304{
Simon Glassf2d23de2022-08-28 12:32:49 -0600305 const char *fit_uname_config;
306 const char *fit_uname_ramdisk;
Simon Glass973b8932022-08-28 12:32:51 -0600307 bool done_select = !select;
Simon Glass664696f2022-08-28 12:32:47 -0600308 bool done = false;
Simon Glassf2d23de2022-08-28 12:32:49 -0600309 int rd_noffset;
Tom Rini1ca3c642023-04-05 19:48:56 -0400310 ulong rd_addr = 0;
Simon Glassccda2fa2021-09-25 19:43:37 -0600311 char *buf;
Simon Glass2de5b012021-09-25 19:43:36 -0600312
Simon Glass973b8932022-08-28 12:32:51 -0600313 if (CONFIG_IS_ENABLED(FIT)) {
Simon Glassf2d23de2022-08-28 12:32:49 -0600314 fit_uname_config = images->fit_uname_cfg;
315 fit_uname_ramdisk = NULL;
Simon Glass2de5b012021-09-25 19:43:36 -0600316
Tom Rinibc7d9d22021-12-20 09:36:32 -0500317 if (select) {
318 ulong default_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600319 /*
320 * If the init ramdisk comes from the FIT image and
321 * the FIT image address is omitted in the command
322 * line argument, try to use os FIT image address or
323 * default load address.
324 */
325 if (images->fit_uname_os)
326 default_addr = (ulong)images->fit_hdr_os;
327 else
328 default_addr = image_load_addr;
329
Simon Glass92c586f2022-08-28 12:32:52 -0600330 if (fit_parse_conf(select, default_addr, &rd_addr,
331 &fit_uname_config)) {
Simon Glass60d71542021-09-25 07:03:16 -0600332 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
333 fit_uname_config, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600334 done_select = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600335 } else if (fit_parse_subimage(select, default_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600336 &rd_addr,
337 &fit_uname_ramdisk)) {
338 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
339 fit_uname_ramdisk, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600340 done_select = true;
341 }
342 }
343 }
344 if (!done_select) {
Simon Glass92c586f2022-08-28 12:32:52 -0600345 rd_addr = hextoul(select, NULL);
346 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600347 }
Simon Glass92c586f2022-08-28 12:32:52 -0600348 if (CONFIG_IS_ENABLED(FIT) && !select) {
349 /* use FIT configuration provided in first bootm
350 * command argument. If the property is not defined,
351 * quit silently (with -ENOPKG)
Tom Rinibc7d9d22021-12-20 09:36:32 -0500352 */
Simon Glass92c586f2022-08-28 12:32:52 -0600353 rd_addr = map_to_sysmem(images->fit_hdr_os);
354 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
355 rd_addr);
356 if (rd_noffset == -ENOENT)
357 return -ENOPKG;
358 else if (rd_noffset < 0)
359 return rd_noffset;
360 }
Simon Glass2de5b012021-09-25 19:43:36 -0600361
Simon Glass92c586f2022-08-28 12:32:52 -0600362 /*
363 * Check if there is an initrd image at the
364 * address provided in the second bootm argument
365 * check image type, for FIT images get FIT node.
366 */
367 buf = map_sysmem(rd_addr, 0);
368 switch (genimg_get_format(buf)) {
369 case IMAGE_FORMAT_LEGACY:
370 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600371 const struct legacy_img_hdr *rd_hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600372
Simon Glass92c586f2022-08-28 12:32:52 -0600373 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
374 rd_addr);
Tom Rinibc7d9d22021-12-20 09:36:32 -0500375
Simon Glass92c586f2022-08-28 12:32:52 -0600376 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
377 rd_hdr = image_get_ramdisk(rd_addr, arch,
378 images->verify);
Simon Glass4d7237b2021-09-25 07:03:15 -0600379
Simon Glass92c586f2022-08-28 12:32:52 -0600380 if (!rd_hdr)
381 return -ENOENT;
Simon Glass4d7237b2021-09-25 07:03:15 -0600382
Simon Glass92c586f2022-08-28 12:32:52 -0600383 *rd_datap = image_get_data(rd_hdr);
384 *rd_lenp = image_get_data_size(rd_hdr);
385 done = true;
386 }
387 break;
388 case IMAGE_FORMAT_FIT:
389 if (CONFIG_IS_ENABLED(FIT)) {
390 rd_noffset = fit_image_load(images, rd_addr,
391 &fit_uname_ramdisk,
392 &fit_uname_config,
393 arch, IH_TYPE_RAMDISK,
394 BOOTSTAGE_ID_FIT_RD_START,
395 FIT_LOAD_OPTIONAL_NON_ZERO,
396 rd_datap, rd_lenp);
397 if (rd_noffset < 0)
398 return rd_noffset;
Simon Glassdc0aa042022-08-28 12:32:46 -0600399
Simon Glass92c586f2022-08-28 12:32:52 -0600400 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
401 images->fit_uname_rd = fit_uname_ramdisk;
402 images->fit_noffset_rd = rd_noffset;
403 done = true;
Simon Glass664696f2022-08-28 12:32:47 -0600404 }
Simon Glass92c586f2022-08-28 12:32:52 -0600405 break;
406 case IMAGE_FORMAT_ANDROID:
407 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass92c586f2022-08-28 12:32:52 -0600408 int ret;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100409 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000410 ulong boot_img = get_abootimg_addr();
411 ulong init_boot_img = get_ainit_bootimg_addr();
Safae Ouajih51c981b2023-02-06 00:50:17 +0100412 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000413 void *ramdisk_img;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100414
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000415 if (init_boot_img == -1)
416 ramdisk_img = map_sysmem(boot_img, 0);
417 else
418 ramdisk_img = map_sysmem(init_boot_img, 0);
419
420 ret = android_image_get_ramdisk(ramdisk_img, vendor_boot_img,
Safae Ouajih51c981b2023-02-06 00:50:17 +0100421 rd_datap, rd_lenp);
422 unmap_sysmem(vendor_boot_img);
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000423 unmap_sysmem(ramdisk_img);
Safae Ouajih51c981b2023-02-06 00:50:17 +0100424 } else {
425 void *ptr = map_sysmem(images->os.start, 0);
426
427 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp);
428 unmap_sysmem(ptr);
429 }
430
Michael Walle955415b2024-07-29 23:36:57 +0200431 if (ret == -ENOENT)
432 return -ENOPKG;
433 else if (ret)
Simon Glass92c586f2022-08-28 12:32:52 -0600434 return ret;
435 done = true;
436 }
437 break;
438 }
Simon Glass664696f2022-08-28 12:32:47 -0600439
440 if (!done) {
441 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
442 char *end = NULL;
Simon Glass0f781382021-09-25 19:43:35 -0600443
Simon Glass664696f2022-08-28 12:32:47 -0600444 if (select)
445 end = strchr(select, ':');
446 if (end) {
447 *rd_lenp = hextoul(++end, NULL);
448 *rd_datap = rd_addr;
449 done = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600450 }
Simon Glass664696f2022-08-28 12:32:47 -0600451 }
452
453 if (!done) {
Simon Glass0f781382021-09-25 19:43:35 -0600454 puts("Wrong Ramdisk Image Format\n");
Simon Glassccda2fa2021-09-25 19:43:37 -0600455 return -EINVAL;
Simon Glass4d7237b2021-09-25 07:03:15 -0600456 }
Simon Glass664696f2022-08-28 12:32:47 -0600457 }
Simon Glassccda2fa2021-09-25 19:43:37 -0600458
459 return 0;
460}
461
Simon Glass8eef77e2023-11-18 14:05:06 -0700462int boot_get_ramdisk(char const *select, struct bootm_headers *images,
463 uint arch, ulong *rd_start, ulong *rd_end)
Simon Glassccda2fa2021-09-25 19:43:37 -0600464{
465 ulong rd_data, rd_len;
Simon Glassccda2fa2021-09-25 19:43:37 -0600466
467 *rd_start = 0;
468 *rd_end = 0;
469
Simon Glassccda2fa2021-09-25 19:43:37 -0600470 /*
471 * Look for a '-' which indicates to ignore the
472 * ramdisk argument
473 */
474 if (select && strcmp(select, "-") == 0) {
475 debug("## Skipping init Ramdisk\n");
476 rd_len = 0;
477 rd_data = 0;
478 } else if (select || genimg_has_config(images)) {
479 int ret;
480
481 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
482 if (ret == -ENOPKG)
483 return 0;
484 else if (ret)
485 return ret;
Simon Glass4d7237b2021-09-25 07:03:15 -0600486 } else if (images->legacy_hdr_valid &&
487 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass60d71542021-09-25 07:03:16 -0600488 IH_TYPE_MULTI)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600489 /*
490 * Now check if we have a legacy mult-component image,
491 * get second entry data start address and len.
492 */
493 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600494 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
495 (ulong)images->legacy_hdr_os);
Simon Glass4d7237b2021-09-25 07:03:15 -0600496
497 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
498 } else {
499 /*
500 * no initrd image
501 */
502 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600503 rd_len = 0;
504 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600505 }
506
507 if (!rd_data) {
508 debug("## No init Ramdisk\n");
509 } else {
510 *rd_start = rd_data;
511 *rd_end = rd_data + rd_len;
512 }
513 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600514 *rd_start, *rd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600515
516 return 0;
517}
518
Simon Glass4d7237b2021-09-25 07:03:15 -0600519/**
520 * boot_ramdisk_high - relocate init ramdisk
Simon Glass4d7237b2021-09-25 07:03:15 -0600521 * @rd_data: ramdisk data start address
522 * @rd_len: ramdisk data length
523 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
524 * start address (after possible relocation)
525 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
526 * end address (after possible relocation)
527 *
528 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
529 * variable and if requested ramdisk data is moved to a specified location.
530 *
531 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
532 * start/end addresses if ramdisk image start and len were provided,
533 * otherwise set initrd_start and initrd_end set to zeros.
534 *
535 * returns:
536 * 0 - success
537 * -1 - failure
538 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530539int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start,
540 ulong *initrd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600541{
Sughosh Ganu9b0765a2025-06-17 16:13:40 +0530542 int err;
Simon Glass4d7237b2021-09-25 07:03:15 -0600543 char *s;
Marek Vasutf816c422024-03-26 23:13:11 +0100544 phys_addr_t initrd_high;
Simon Glass4d7237b2021-09-25 07:03:15 -0600545 int initrd_copy_to_ram = 1;
546
547 s = env_get("initrd_high");
548 if (s) {
549 /* a value of "no" or a similar string will act like 0,
550 * turning the "load high" feature off. This is intentional.
551 */
552 initrd_high = hextoul(s, NULL);
553 if (initrd_high == ~0)
554 initrd_copy_to_ram = 0;
555 } else {
556 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
557 }
558
Marek Vasutf816c422024-03-26 23:13:11 +0100559 debug("## initrd_high = 0x%llx, copy_to_ram = %d\n",
560 (u64)initrd_high, initrd_copy_to_ram);
Simon Glass4d7237b2021-09-25 07:03:15 -0600561
562 if (rd_data) {
563 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
Sughosh Ganu9b0765a2025-06-17 16:13:40 +0530564 phys_addr_t initrd_addr;
565
Simon Glass4d7237b2021-09-25 07:03:15 -0600566 debug(" in-place initrd\n");
567 *initrd_start = rd_data;
568 *initrd_end = rd_data + rd_len;
Sughosh Ganu9b0765a2025-06-17 16:13:40 +0530569 initrd_addr = (phys_addr_t)rd_data;
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530570 err = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &initrd_addr,
571 rd_len, LMB_NONE);
Sughosh Ganu9b0765a2025-06-17 16:13:40 +0530572 if (err) {
573 puts("in-place initrd alloc failed\n");
574 goto error;
575 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600576 } else {
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530577 enum lmb_mem_type type = initrd_high ?
578 LMB_MEM_ALLOC_MAX : LMB_MEM_ALLOC_ANY;
Simon Glass4d7237b2021-09-25 07:03:15 -0600579
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530580 err = lmb_alloc_mem(type, 0x1000, &initrd_high, rd_len,
581 LMB_NONE);
582 if (err) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600583 puts("ramdisk - allocation error\n");
584 goto error;
585 }
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530586
587 *initrd_start = (ulong)initrd_high;
Simon Glass4d7237b2021-09-25 07:03:15 -0600588 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
589
590 *initrd_end = *initrd_start + rd_len;
591 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass60d71542021-09-25 07:03:16 -0600592 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600593
594 memmove_wd((void *)*initrd_start,
Simon Glass60d71542021-09-25 07:03:16 -0600595 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass4d7237b2021-09-25 07:03:15 -0600596
Simon Glass4d7237b2021-09-25 07:03:15 -0600597 /*
598 * Ensure the image is flushed to memory to handle
599 * AMP boot scenarios in which we might not be
600 * HW cache coherent
601 */
Simon Glass0f781382021-09-25 19:43:35 -0600602 if (IS_ENABLED(CONFIG_MP)) {
603 flush_cache((unsigned long)*initrd_start,
604 ALIGN(rd_len, ARCH_DMA_MINALIGN));
605 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600606 puts("OK\n");
607 }
608 } else {
609 *initrd_start = 0;
610 *initrd_end = 0;
611 }
612 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600613 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600614
615 return 0;
616
617error:
618 return -1;
619}
Simon Glass4d7237b2021-09-25 07:03:15 -0600620
Simon Glassdf00afa2022-09-06 20:26:50 -0600621int boot_get_setup(struct bootm_headers *images, u8 arch,
Simon Glass4d7237b2021-09-25 07:03:15 -0600622 ulong *setup_start, ulong *setup_len)
623{
Simon Glass0f781382021-09-25 19:43:35 -0600624 if (!CONFIG_IS_ENABLED(FIT))
625 return -ENOENT;
626
Simon Glass4d7237b2021-09-25 07:03:15 -0600627 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600628}
629
Simon Glass01080ab2023-11-18 14:05:11 -0700630int boot_get_fpga(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600631{
632 ulong tmp_img_addr, img_data, img_len;
633 void *buf;
634 int conf_noffset;
635 int fit_img_result;
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100636 const char *uname, *name, *compatible;
Simon Glass4d7237b2021-09-25 07:03:15 -0600637 int err;
638 int devnum = 0; /* TODO support multi fpga platforms */
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100639 int flags = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600640
Simon Glass0f781382021-09-25 19:43:35 -0600641 if (!IS_ENABLED(CONFIG_FPGA))
642 return -ENOSYS;
643
Simon Glass4d7237b2021-09-25 07:03:15 -0600644 /* Check to see if the images struct has a FIT configuration */
645 if (!genimg_has_config(images)) {
646 debug("## FIT configuration was not specified\n");
647 return 0;
648 }
649
650 /*
651 * Obtain the os FIT header from the images struct
652 */
653 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
654 buf = map_sysmem(tmp_img_addr, 0);
655 /*
656 * Check image type. For FIT images get FIT node
657 * and attempt to locate a generic binary.
658 */
659 switch (genimg_get_format(buf)) {
660 case IMAGE_FORMAT_FIT:
661 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
662
663 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
664 NULL);
665 if (!uname) {
666 debug("## FPGA image is not specified\n");
667 return 0;
668 }
669 fit_img_result = fit_image_load(images,
670 tmp_img_addr,
671 (const char **)&uname,
Simon Glass60d71542021-09-25 07:03:16 -0600672 &images->fit_uname_cfg,
Simon Glass01080ab2023-11-18 14:05:11 -0700673 IH_ARCH_DEFAULT,
Simon Glass4d7237b2021-09-25 07:03:15 -0600674 IH_TYPE_FPGA,
675 BOOTSTAGE_ID_FPGA_INIT,
676 FIT_LOAD_OPTIONAL_NON_ZERO,
677 &img_data, &img_len);
678
679 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
680 uname, img_data, img_len);
681
682 if (fit_img_result < 0) {
683 /* Something went wrong! */
684 return fit_img_result;
685 }
686
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100687 conf_noffset = fit_image_get_node(buf, uname);
688 compatible = fdt_getprop(buf, conf_noffset, "compatible", NULL);
689 if (!compatible) {
690 printf("'fpga' image without 'compatible' property\n");
691 } else {
692 if (CONFIG_IS_ENABLED(FPGA_LOAD_SECURE))
693 flags = fpga_compatible2flag(devnum, compatible);
694 }
695
Simon Glass4d7237b2021-09-25 07:03:15 -0600696 if (!fpga_is_partial_data(devnum, img_len)) {
697 name = "full";
698 err = fpga_loadbitstream(devnum, (char *)img_data,
699 img_len, BIT_FULL);
700 if (err)
701 err = fpga_load(devnum, (const void *)img_data,
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100702 img_len, BIT_FULL, flags);
Simon Glass4d7237b2021-09-25 07:03:15 -0600703 } else {
704 name = "partial";
705 err = fpga_loadbitstream(devnum, (char *)img_data,
706 img_len, BIT_PARTIAL);
707 if (err)
708 err = fpga_load(devnum, (const void *)img_data,
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100709 img_len, BIT_PARTIAL, flags);
Simon Glass4d7237b2021-09-25 07:03:15 -0600710 }
711
712 if (err)
713 return err;
714
715 printf(" Programming %s bitstream... OK\n", name);
716 break;
717 default:
718 printf("The given image format is not supported (corrupt?)\n");
719 return 1;
720 }
721
722 return 0;
723}
Simon Glass4d7237b2021-09-25 07:03:15 -0600724
Simon Glass60d71542021-09-25 07:03:16 -0600725static void fit_loadable_process(u8 img_type,
Simon Glass4d7237b2021-09-25 07:03:15 -0600726 ulong img_data,
727 ulong img_len)
728{
729 int i;
730 const unsigned int count =
731 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
732 struct fit_loadable_tbl *fit_loadable_handler =
733 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
734 /* For each loadable handler */
735 for (i = 0; i < count; i++, fit_loadable_handler++)
736 /* matching this type */
737 if (fit_loadable_handler->type == img_type)
738 /* call that handler with this image data */
739 fit_loadable_handler->handler(img_data, img_len);
740}
741
Simon Glass4fdc5592023-11-18 14:05:12 -0700742int boot_get_loadable(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600743{
744 /*
745 * These variables are used to hold the current image location
746 * in system memory.
747 */
748 ulong tmp_img_addr;
749 /*
750 * These two variables are requirements for fit_image_load, but
751 * their values are not used
752 */
753 ulong img_data, img_len;
754 void *buf;
755 int loadables_index;
756 int conf_noffset;
757 int fit_img_result;
758 const char *uname;
Simon Glass60d71542021-09-25 07:03:16 -0600759 u8 img_type;
Simon Glass4d7237b2021-09-25 07:03:15 -0600760
761 /* Check to see if the images struct has a FIT configuration */
762 if (!genimg_has_config(images)) {
763 debug("## FIT configuration was not specified\n");
764 return 0;
765 }
766
767 /*
768 * Obtain the os FIT header from the images struct
769 */
770 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
771 buf = map_sysmem(tmp_img_addr, 0);
772 /*
773 * Check image type. For FIT images get FIT node
774 * and attempt to locate a generic binary.
775 */
776 switch (genimg_get_format(buf)) {
777 case IMAGE_FORMAT_FIT:
778 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
779
780 for (loadables_index = 0;
781 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass60d71542021-09-25 07:03:16 -0600782 FIT_LOADABLE_PROP,
783 loadables_index, NULL), uname;
784 loadables_index++) {
785 fit_img_result = fit_image_load(images, tmp_img_addr,
786 &uname,
787 &images->fit_uname_cfg,
Simon Glass4fdc5592023-11-18 14:05:12 -0700788 IH_ARCH_DEFAULT,
789 IH_TYPE_LOADABLE,
Simon Glass60d71542021-09-25 07:03:16 -0600790 BOOTSTAGE_ID_FIT_LOADABLE_START,
791 FIT_LOAD_OPTIONAL_NON_ZERO,
792 &img_data, &img_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600793 if (fit_img_result < 0) {
794 /* Something went wrong! */
795 return fit_img_result;
796 }
797
798 fit_img_result = fit_image_get_node(buf, uname);
799 if (fit_img_result < 0) {
800 /* Something went wrong! */
801 return fit_img_result;
802 }
803 fit_img_result = fit_image_get_type(buf,
804 fit_img_result,
805 &img_type);
806 if (fit_img_result < 0) {
807 /* Something went wrong! */
808 return fit_img_result;
809 }
810
811 fit_loadable_process(img_type, img_data, img_len);
812 }
813 break;
814 default:
815 printf("The given image format is not supported (corrupt?)\n");
816 return 1;
817 }
818
819 return 0;
820}
Simon Glass4d7237b2021-09-25 07:03:15 -0600821
Simon Glass4d7237b2021-09-25 07:03:15 -0600822/**
823 * boot_get_cmdline - allocate and initialize kernel cmdline
Simon Glass4d7237b2021-09-25 07:03:15 -0600824 * @cmd_start: pointer to a ulong variable, will hold cmdline start
825 * @cmd_end: pointer to a ulong variable, will hold cmdline end
826 *
Simon Glass7ae16bb2022-08-28 12:32:53 -0600827 * This allocates space for kernel command line below
Simon Glass4d7237b2021-09-25 07:03:15 -0600828 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
829 * variable is present its contents is copied to allocated kernel
830 * command line.
831 *
832 * returns:
833 * 0 - success
834 * -1 - failure
835 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530836int boot_get_cmdline(ulong *cmd_start, ulong *cmd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600837{
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530838 int barg, err;
Simon Glass4d7237b2021-09-25 07:03:15 -0600839 char *cmdline;
840 char *s;
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530841 phys_addr_t addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600842
Simon Glass7ae16bb2022-08-28 12:32:53 -0600843 /*
844 * Help the compiler detect that this function is only called when
845 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
846 */
847 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
848 return 0;
849
850 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530851 addr = env_get_bootm_mapsize() + env_get_bootm_low();
852
853 err = lmb_alloc_mem(LMB_MEM_ALLOC_MAX, 0xf, &addr, barg, LMB_NONE);
854 if (err)
Simon Glass4d7237b2021-09-25 07:03:15 -0600855 return -1;
856
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530857 cmdline = (char *)(uintptr_t)addr;
858
Simon Glass4d7237b2021-09-25 07:03:15 -0600859 s = env_get("bootargs");
860 if (!s)
861 s = "";
862
863 strcpy(cmdline, s);
864
Simon Glass60d71542021-09-25 07:03:16 -0600865 *cmd_start = (ulong)cmdline;
Simon Glass4d7237b2021-09-25 07:03:15 -0600866 *cmd_end = *cmd_start + strlen(cmdline);
867
868 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
869
870 return 0;
871}
Simon Glass4d7237b2021-09-25 07:03:15 -0600872
Simon Glass4d7237b2021-09-25 07:03:15 -0600873/**
874 * boot_get_kbd - allocate and initialize kernel copy of board info
Simon Glass4d7237b2021-09-25 07:03:15 -0600875 * @kbd: double pointer to board info data
876 *
877 * boot_get_kbd() allocates space for kernel copy of board info data below
878 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
879 * with the current u-boot board info data.
880 *
881 * returns:
882 * 0 - success
883 * -1 - failure
884 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530885int boot_get_kbd(struct bd_info **kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600886{
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530887 int err;
888 phys_addr_t addr;
889
890 addr = env_get_bootm_mapsize() + env_get_bootm_low();
891 err = lmb_alloc_mem(LMB_MEM_ALLOC_MAX, 0xf, &addr,
892 sizeof(struct bd_info), LMB_NONE);
893 if (err)
Simon Glass4d7237b2021-09-25 07:03:15 -0600894 return -1;
895
Sughosh Ganu7bdfe122025-06-17 16:13:41 +0530896 *kbd = (struct bd_info *)(uintptr_t)addr;
Simon Glass60d71542021-09-25 07:03:16 -0600897 **kbd = *gd->bd;
Simon Glass4d7237b2021-09-25 07:03:15 -0600898
899 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
900
Simon Glass7ae16bb2022-08-28 12:32:53 -0600901 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
Simon Glassbc0c4db2021-09-25 07:03:20 -0600902 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass4d7237b2021-09-25 07:03:15 -0600903
904 return 0;
905}
Simon Glass4d7237b2021-09-25 07:03:15 -0600906
Simon Glassdf00afa2022-09-06 20:26:50 -0600907int image_setup_linux(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600908{
909 ulong of_size = images->ft_len;
910 char **of_flat_tree = &images->ft_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600911 int ret;
912
Simon Glass7ae16bb2022-08-28 12:32:53 -0600913 /* This function cannot be called without lmb support */
Sughosh Ganu0d733f02024-08-26 17:29:22 +0530914 if (!CONFIG_IS_ENABLED(LMB))
Simon Glass7ae16bb2022-08-28 12:32:53 -0600915 return -EFAULT;
Simon Glass85c057e2021-09-25 19:43:21 -0600916 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530917 boot_fdt_add_mem_rsv_regions(*of_flat_tree);
Simon Glass4d7237b2021-09-25 07:03:15 -0600918
Simon Glassb8eb1fe2021-09-25 19:43:25 -0600919 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530920 ret = boot_get_cmdline(&images->cmdline_start,
Simon Glass60d71542021-09-25 07:03:16 -0600921 &images->cmdline_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600922 if (ret) {
923 puts("ERROR with allocation of cmdline\n");
924 return ret;
925 }
926 }
927
Simon Glass85c057e2021-09-25 19:43:21 -0600928 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530929 ret = boot_relocate_fdt(of_flat_tree, &of_size);
Simon Glass4d7237b2021-09-25 07:03:15 -0600930 if (ret)
931 return ret;
932 }
933
Simon Glass85c057e2021-09-25 19:43:21 -0600934 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530935 ret = image_setup_libfdt(images, *of_flat_tree, true);
Simon Glass4d7237b2021-09-25 07:03:15 -0600936 if (ret)
937 return ret;
938 }
939
940 return 0;
941}
Simon Glass4a8a8a12021-09-25 07:03:17 -0600942
943void genimg_print_size(uint32_t size)
944{
945 printf("%d Bytes = ", size);
946 print_size(size, "\n");
947}
948
949void genimg_print_time(time_t timestamp)
950{
951 struct rtc_time tm;
952
953 rtc_to_tm(timestamp, &tm);
954 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
955 tm.tm_year, tm.tm_mon, tm.tm_mday,
956 tm.tm_hour, tm.tm_min, tm.tm_sec);
957}
Simon Glassdaee3ba2023-01-06 08:52:28 -0600958
959/**
960 * get_default_image() - Return default property from /images
961 *
962 * Return: Pointer to value of default property (or NULL)
963 */
964static const char *get_default_image(const void *fit)
965{
966 int images_noffset;
967
968 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
969 if (images_noffset < 0)
970 return NULL;
971
972 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
973}
974
975int image_locate_script(void *buf, int size, const char *fit_uname,
976 const char *confname, char **datap, uint *lenp)
977{
978 const struct legacy_img_hdr *hdr;
979 const void *fit_data;
980 const void *fit_hdr;
981 size_t fit_len;
982 int noffset;
983 int verify;
984 ulong len;
985 u32 *data;
986
987 verify = env_get_yesno("verify");
988
989 switch (genimg_get_format(buf)) {
990 case IMAGE_FORMAT_LEGACY:
Marek Vasutfb59eca2023-02-27 20:56:31 +0100991 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
992 goto exit_image_format;
993 } else {
Simon Glassdaee3ba2023-01-06 08:52:28 -0600994 hdr = buf;
995
996 if (!image_check_magic(hdr)) {
997 puts("Bad magic number\n");
998 return 1;
999 }
1000
1001 if (!image_check_hcrc(hdr)) {
1002 puts("Bad header crc\n");
1003 return 1;
1004 }
1005
1006 if (verify) {
1007 if (!image_check_dcrc(hdr)) {
1008 puts("Bad data crc\n");
1009 return 1;
1010 }
1011 }
1012
1013 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
1014 puts("Bad image type\n");
1015 return 1;
1016 }
1017
1018 /* get length of script */
1019 data = (u32 *)image_get_data(hdr);
1020
1021 len = uimage_to_cpu(*data);
1022 if (!len) {
1023 puts("Empty Script\n");
1024 return 1;
1025 }
1026
1027 /*
1028 * scripts are just multi-image files with one
1029 * component, so seek past the zero-terminated sequence
1030 * of image lengths to get to the actual image data
1031 */
1032 while (*data++);
1033 }
1034 break;
1035 case IMAGE_FORMAT_FIT:
Marek Vasutfb59eca2023-02-27 20:56:31 +01001036 if (!IS_ENABLED(CONFIG_FIT)) {
1037 goto exit_image_format;
1038 } else {
Simon Glassdaee3ba2023-01-06 08:52:28 -06001039 fit_hdr = buf;
1040 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1041 puts("Bad FIT image format\n");
1042 return 1;
1043 }
1044
1045 if (!fit_uname) {
1046 /* If confname is empty, use the default */
1047 if (confname && *confname)
1048 noffset = fit_conf_get_node(fit_hdr, confname);
1049 else
1050 noffset = fit_conf_get_node(fit_hdr, NULL);
1051 if (noffset < 0) {
1052 if (!confname)
1053 goto fallback;
1054 printf("Could not find config %s\n", confname);
1055 return 1;
1056 }
1057
1058 if (verify && fit_config_verify(fit_hdr, noffset))
1059 return 1;
1060
1061 noffset = fit_conf_get_prop_node(fit_hdr,
1062 noffset,
1063 FIT_SCRIPT_PROP,
1064 IH_PHASE_NONE);
1065 if (noffset < 0) {
1066 if (!confname)
1067 goto fallback;
1068 printf("Could not find script in %s\n", confname);
1069 return 1;
1070 }
1071 } else {
1072fallback:
1073 if (!fit_uname || !*fit_uname)
1074 fit_uname = get_default_image(fit_hdr);
1075 if (!fit_uname) {
1076 puts("No FIT subimage unit name\n");
1077 return 1;
1078 }
1079
1080 /* get script component image node offset */
1081 noffset = fit_image_get_node(fit_hdr, fit_uname);
1082 if (noffset < 0) {
1083 printf("Can't find '%s' FIT subimage\n",
1084 fit_uname);
1085 return 1;
1086 }
1087 }
1088
1089 if (!fit_image_check_type(fit_hdr, noffset,
1090 IH_TYPE_SCRIPT)) {
1091 puts("Not a image image\n");
1092 return 1;
1093 }
1094
1095 /* verify integrity */
1096 if (verify && !fit_image_verify(fit_hdr, noffset)) {
1097 puts("Bad Data Hash\n");
1098 return 1;
1099 }
1100
1101 /* get script subimage data address and length */
Simon Glass833445d2025-01-10 17:00:13 -07001102 if (fit_image_get_data(fit_hdr, noffset, &fit_data,
1103 &fit_len)) {
Simon Glassdaee3ba2023-01-06 08:52:28 -06001104 puts("Could not find script subimage data\n");
1105 return 1;
1106 }
1107
1108 data = (u32 *)fit_data;
1109 len = (ulong)fit_len;
1110 }
1111 break;
1112 default:
Marek Vasutfb59eca2023-02-27 20:56:31 +01001113 goto exit_image_format;
Simon Glassdaee3ba2023-01-06 08:52:28 -06001114 }
1115
1116 *datap = (char *)data;
1117 *lenp = len;
1118
1119 return 0;
Marek Vasutfb59eca2023-02-27 20:56:31 +01001120
1121exit_image_format:
1122 puts("Wrong image format for \"source\" command\n");
1123 return -EPERM;
Simon Glassdaee3ba2023-01-06 08:52:28 -06001124}