blob: addccf87c808aab445b632e558b69ed83566bf7d [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>
Simon Glass7ae16bb2022-08-28 12:32:53 -060019#include <log.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060020#include <mapmem.h>
Simon Glass4a8a8a12021-09-25 07:03:17 -060021#include <rtc.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060022#include <watchdog.h>
23#include <asm/cache.h>
24#include <asm/global_data.h>
25
Simon Glass4d7237b2021-09-25 07:03:15 -060026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass4d7237b2021-09-25 07:03:15 -060028/**
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 Glassbb7d3bb2022-09-06 20:26:52 -060043static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
44 int verify)
Simon Glass4d7237b2021-09-25 07:03:15 -060045{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060046 const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -060047
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 Glass60d71542021-09-25 07:03:16 -060079 genimg_get_arch_name(arch));
Simon Glass4d7237b2021-09-25 07:03:15 -060080 bootstage_error(BOOTSTAGE_ID_RAMDISK);
81 return NULL;
82 }
83
84 return rd_hdr;
85}
Simon Glass4d7237b2021-09-25 07:03:15 -060086
87/*****************************************************************************/
88/* Shared dual-format routines */
89/*****************************************************************************/
90ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
91ulong image_save_addr; /* Default Save Address */
92ulong image_save_size; /* Default Save Size (in bytes) */
93
94static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass60d71542021-09-25 07:03:16 -060095 int flags)
Simon Glass4d7237b2021-09-25 07:03:15 -060096{
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}
108U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
Marek Vasutf816c422024-03-26 23:13:11 +0100110phys_addr_t env_get_bootm_low(void)
Simon Glass4d7237b2021-09-25 07:03:15 -0600111{
112 char *s = env_get("bootm_low");
Simon Glass60d71542021-09-25 07:03:16 -0600113
Marek Vasutf816c422024-03-26 23:13:11 +0100114 if (s)
115 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600116
Tom Rinibb4dd962022-11-16 13:10:37 -0500117#if defined(CFG_SYS_SDRAM_BASE)
118 return CFG_SYS_SDRAM_BASE;
Simon Glass4d7237b2021-09-25 07:03:15 -0600119#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
126phys_size_t env_get_bootm_size(void)
127{
Marek Vasutb140bbd2024-03-26 23:13:13 +0100128 phys_addr_t start, low;
129 phys_size_t size;
Simon Glass4d7237b2021-09-25 07:03:15 -0600130 char *s = env_get("bootm_size");
Simon Glass60d71542021-09-25 07:03:16 -0600131
Marek Vasut1d02ed72024-03-26 23:13:12 +0100132 if (s)
133 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600134
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 Vasutb140bbd2024-03-26 23:13:13 +0100143 low = simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600144 else
Marek Vasutb140bbd2024-03-26 23:13:13 +0100145 low = start;
Simon Glass4d7237b2021-09-25 07:03:15 -0600146
Marek Vasutb140bbd2024-03-26 23:13:13 +0100147 return size - (low - start);
Simon Glass4d7237b2021-09-25 07:03:15 -0600148}
149
150phys_size_t env_get_bootm_mapsize(void)
151{
Simon Glass4d7237b2021-09-25 07:03:15 -0600152 char *s = env_get("bootm_mapsize");
Simon Glass60d71542021-09-25 07:03:16 -0600153
Marek Vasutddf73a82024-03-26 23:13:14 +0100154 if (s)
155 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600156
Tom Rini6a5dccc2022-11-16 13:10:41 -0500157#if defined(CFG_SYS_BOOTMAPSZ)
158 return CFG_SYS_BOOTMAPSZ;
Simon Glass4d7237b2021-09-25 07:03:15 -0600159#else
160 return env_get_bootm_size();
161#endif
162}
163
164void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
165{
166 if (to == from)
167 return;
168
Simon Glass7ae16bb2022-08-28 12:32:53 -0600169 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600170 if (to > from) {
Simon Glass7ae16bb2022-08-28 12:32:53 -0600171 from += len;
172 to += len;
Simon Glass4d7237b2021-09-25 07:03:15 -0600173 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600174 while (len > 0) {
175 size_t tail = (len > chunksz) ? chunksz : len;
176
Stefan Roese80877fa2022-09-02 14:10:46 +0200177 schedule();
Simon Glass7ae16bb2022-08-28 12:32:53 -0600178 if (to > from) {
179 to -= tail;
180 from -= tail;
181 }
182 memmove(to, from, tail);
183 if (to < from) {
184 to += tail;
185 from += tail;
186 }
187 len -= tail;
Simon Glass4d7237b2021-09-25 07:03:15 -0600188 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600189 } else {
190 memmove(to, from, len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600191 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600192}
193
Simon Glass4e8ec472023-11-18 14:04:57 -0700194ulong genimg_get_kernel_addr_fit(const char *const img_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600195 const char **fit_uname_config,
196 const char **fit_uname_kernel)
Simon Glass4d7237b2021-09-25 07:03:15 -0600197{
198 ulong kernel_addr;
199
200 /* find out kernel image address */
201 if (!img_addr) {
202 kernel_addr = image_load_addr;
203 debug("* kernel: default image load address = 0x%08lx\n",
204 image_load_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600205 } else if (CONFIG_IS_ENABLED(FIT) &&
206 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass4d7237b2021-09-25 07:03:15 -0600207 fit_uname_config)) {
208 debug("* kernel: config '%s' from image at 0x%08lx\n",
209 *fit_uname_config, kernel_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600210 } else if (CONFIG_IS_ENABLED(FIT) &&
211 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
212 fit_uname_kernel)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600213 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
214 *fit_uname_kernel, kernel_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600215 } else {
216 kernel_addr = hextoul(img_addr, NULL);
217 debug("* kernel: cmdline image address = 0x%08lx\n",
218 kernel_addr);
219 }
220
221 return kernel_addr;
222}
223
224/**
225 * genimg_get_kernel_addr() is the simple version of
226 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
227 */
228ulong genimg_get_kernel_addr(char * const img_addr)
229{
230 const char *fit_uname_config = NULL;
231 const char *fit_uname_kernel = NULL;
232
233 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
234 &fit_uname_kernel);
235}
236
Simon Glass2f937672025-03-05 17:25:08 -0700237enum image_fmt_t genimg_get_format(const void *img_addr)
Simon Glass4d7237b2021-09-25 07:03:15 -0600238{
Simon Glass0f781382021-09-25 19:43:35 -0600239 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600240 const struct legacy_img_hdr *hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600241
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600242 hdr = (const struct legacy_img_hdr *)img_addr;
Simon Glass0f781382021-09-25 19:43:35 -0600243 if (image_check_magic(hdr))
244 return IMAGE_FORMAT_LEGACY;
245 }
246 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
247 if (!fdt_check_header(img_addr))
248 return IMAGE_FORMAT_FIT;
249 }
250 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100251 is_android_boot_image_header(img_addr))
Simon Glass4d7237b2021-09-25 07:03:15 -0600252 return IMAGE_FORMAT_ANDROID;
Simon Glass4d7237b2021-09-25 07:03:15 -0600253
254 return IMAGE_FORMAT_INVALID;
255}
256
257/**
258 * fit_has_config - check if there is a valid FIT configuration
259 * @images: pointer to the bootm command headers structure
260 *
261 * fit_has_config() checks if there is a FIT configuration in use
262 * (if FTI support is present).
263 *
264 * returns:
265 * 0, no FIT support or no configuration found
266 * 1, configuration found
267 */
Simon Glassdf00afa2022-09-06 20:26:50 -0600268int genimg_has_config(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600269{
Simon Glass0f781382021-09-25 19:43:35 -0600270 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass4d7237b2021-09-25 07:03:15 -0600271 return 1;
Simon Glass0f781382021-09-25 19:43:35 -0600272
Simon Glass4d7237b2021-09-25 07:03:15 -0600273 return 0;
274}
275
276/**
Simon Glassccda2fa2021-09-25 19:43:37 -0600277 * select_ramdisk() - Select and locate the ramdisk to use
278 *
Simon Glass4d7237b2021-09-25 07:03:15 -0600279 * @images: pointer to the bootm images structure
Simon Glass664696f2022-08-28 12:32:47 -0600280 * @select: name of ramdisk to select, or hex address, NULL for any
Simon Glass4d7237b2021-09-25 07:03:15 -0600281 * @arch: expected ramdisk architecture
Simon Glassccda2fa2021-09-25 19:43:37 -0600282 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
283 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100284 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glassccda2fa2021-09-25 19:43:37 -0600285 * other -ve value on other error
Simon Glass4d7237b2021-09-25 07:03:15 -0600286 */
Simon Glassdf00afa2022-09-06 20:26:50 -0600287static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
Simon Glassccda2fa2021-09-25 19:43:37 -0600288 ulong *rd_datap, ulong *rd_lenp)
Simon Glass4d7237b2021-09-25 07:03:15 -0600289{
Simon Glassf2d23de2022-08-28 12:32:49 -0600290 const char *fit_uname_config;
291 const char *fit_uname_ramdisk;
Simon Glass973b8932022-08-28 12:32:51 -0600292 bool done_select = !select;
Simon Glass664696f2022-08-28 12:32:47 -0600293 bool done = false;
Simon Glassf2d23de2022-08-28 12:32:49 -0600294 int rd_noffset;
Tom Rini1ca3c642023-04-05 19:48:56 -0400295 ulong rd_addr = 0;
Simon Glassccda2fa2021-09-25 19:43:37 -0600296 char *buf;
Simon Glass2de5b012021-09-25 19:43:36 -0600297
Simon Glass973b8932022-08-28 12:32:51 -0600298 if (CONFIG_IS_ENABLED(FIT)) {
Simon Glassf2d23de2022-08-28 12:32:49 -0600299 fit_uname_config = images->fit_uname_cfg;
300 fit_uname_ramdisk = NULL;
Simon Glass2de5b012021-09-25 19:43:36 -0600301
Tom Rinibc7d9d22021-12-20 09:36:32 -0500302 if (select) {
303 ulong default_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600304 /*
305 * If the init ramdisk comes from the FIT image and
306 * the FIT image address is omitted in the command
307 * line argument, try to use os FIT image address or
308 * default load address.
309 */
310 if (images->fit_uname_os)
311 default_addr = (ulong)images->fit_hdr_os;
312 else
313 default_addr = image_load_addr;
314
Simon Glass92c586f2022-08-28 12:32:52 -0600315 if (fit_parse_conf(select, default_addr, &rd_addr,
316 &fit_uname_config)) {
Simon Glass60d71542021-09-25 07:03:16 -0600317 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
318 fit_uname_config, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600319 done_select = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600320 } else if (fit_parse_subimage(select, default_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600321 &rd_addr,
322 &fit_uname_ramdisk)) {
323 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
324 fit_uname_ramdisk, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600325 done_select = true;
326 }
327 }
328 }
329 if (!done_select) {
Simon Glass92c586f2022-08-28 12:32:52 -0600330 rd_addr = hextoul(select, NULL);
331 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600332 }
Simon Glass92c586f2022-08-28 12:32:52 -0600333 if (CONFIG_IS_ENABLED(FIT) && !select) {
334 /* use FIT configuration provided in first bootm
335 * command argument. If the property is not defined,
336 * quit silently (with -ENOPKG)
Tom Rinibc7d9d22021-12-20 09:36:32 -0500337 */
Simon Glass92c586f2022-08-28 12:32:52 -0600338 rd_addr = map_to_sysmem(images->fit_hdr_os);
339 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
340 rd_addr);
341 if (rd_noffset == -ENOENT)
342 return -ENOPKG;
343 else if (rd_noffset < 0)
344 return rd_noffset;
345 }
Simon Glass2de5b012021-09-25 19:43:36 -0600346
Simon Glass92c586f2022-08-28 12:32:52 -0600347 /*
348 * Check if there is an initrd image at the
349 * address provided in the second bootm argument
350 * check image type, for FIT images get FIT node.
351 */
352 buf = map_sysmem(rd_addr, 0);
353 switch (genimg_get_format(buf)) {
354 case IMAGE_FORMAT_LEGACY:
355 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600356 const struct legacy_img_hdr *rd_hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600357
Simon Glass92c586f2022-08-28 12:32:52 -0600358 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
359 rd_addr);
Tom Rinibc7d9d22021-12-20 09:36:32 -0500360
Simon Glass92c586f2022-08-28 12:32:52 -0600361 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
362 rd_hdr = image_get_ramdisk(rd_addr, arch,
363 images->verify);
Simon Glass4d7237b2021-09-25 07:03:15 -0600364
Simon Glass92c586f2022-08-28 12:32:52 -0600365 if (!rd_hdr)
366 return -ENOENT;
Simon Glass4d7237b2021-09-25 07:03:15 -0600367
Simon Glass92c586f2022-08-28 12:32:52 -0600368 *rd_datap = image_get_data(rd_hdr);
369 *rd_lenp = image_get_data_size(rd_hdr);
370 done = true;
371 }
372 break;
373 case IMAGE_FORMAT_FIT:
374 if (CONFIG_IS_ENABLED(FIT)) {
375 rd_noffset = fit_image_load(images, rd_addr,
376 &fit_uname_ramdisk,
377 &fit_uname_config,
378 arch, IH_TYPE_RAMDISK,
379 BOOTSTAGE_ID_FIT_RD_START,
380 FIT_LOAD_OPTIONAL_NON_ZERO,
381 rd_datap, rd_lenp);
382 if (rd_noffset < 0)
383 return rd_noffset;
Simon Glassdc0aa042022-08-28 12:32:46 -0600384
Simon Glass92c586f2022-08-28 12:32:52 -0600385 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
386 images->fit_uname_rd = fit_uname_ramdisk;
387 images->fit_noffset_rd = rd_noffset;
388 done = true;
Simon Glass664696f2022-08-28 12:32:47 -0600389 }
Simon Glass92c586f2022-08-28 12:32:52 -0600390 break;
391 case IMAGE_FORMAT_ANDROID:
392 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass92c586f2022-08-28 12:32:52 -0600393 int ret;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100394 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000395 ulong boot_img = get_abootimg_addr();
396 ulong init_boot_img = get_ainit_bootimg_addr();
Safae Ouajih51c981b2023-02-06 00:50:17 +0100397 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000398 void *ramdisk_img;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100399
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000400 if (init_boot_img == -1)
401 ramdisk_img = map_sysmem(boot_img, 0);
402 else
403 ramdisk_img = map_sysmem(init_boot_img, 0);
404
405 ret = android_image_get_ramdisk(ramdisk_img, vendor_boot_img,
Safae Ouajih51c981b2023-02-06 00:50:17 +0100406 rd_datap, rd_lenp);
407 unmap_sysmem(vendor_boot_img);
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000408 unmap_sysmem(ramdisk_img);
Safae Ouajih51c981b2023-02-06 00:50:17 +0100409 } else {
410 void *ptr = map_sysmem(images->os.start, 0);
411
412 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp);
413 unmap_sysmem(ptr);
414 }
415
Michael Walle955415b2024-07-29 23:36:57 +0200416 if (ret == -ENOENT)
417 return -ENOPKG;
418 else if (ret)
Simon Glass92c586f2022-08-28 12:32:52 -0600419 return ret;
420 done = true;
421 }
422 break;
Simon Glass2f937672025-03-05 17:25:08 -0700423 case IMAGE_FORMAT_INVALID:
424 break;
Simon Glass92c586f2022-08-28 12:32:52 -0600425 }
Simon Glass664696f2022-08-28 12:32:47 -0600426
427 if (!done) {
428 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
429 char *end = NULL;
Simon Glass0f781382021-09-25 19:43:35 -0600430
Simon Glass664696f2022-08-28 12:32:47 -0600431 if (select)
432 end = strchr(select, ':');
433 if (end) {
434 *rd_lenp = hextoul(++end, NULL);
435 *rd_datap = rd_addr;
436 done = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600437 }
Simon Glass664696f2022-08-28 12:32:47 -0600438 }
439
440 if (!done) {
Simon Glass0f781382021-09-25 19:43:35 -0600441 puts("Wrong Ramdisk Image Format\n");
Simon Glassccda2fa2021-09-25 19:43:37 -0600442 return -EINVAL;
Simon Glass4d7237b2021-09-25 07:03:15 -0600443 }
Simon Glass664696f2022-08-28 12:32:47 -0600444 }
Simon Glassccda2fa2021-09-25 19:43:37 -0600445
446 return 0;
447}
448
Simon Glass8eef77e2023-11-18 14:05:06 -0700449int boot_get_ramdisk(char const *select, struct bootm_headers *images,
450 uint arch, ulong *rd_start, ulong *rd_end)
Simon Glassccda2fa2021-09-25 19:43:37 -0600451{
452 ulong rd_data, rd_len;
Simon Glassccda2fa2021-09-25 19:43:37 -0600453
454 *rd_start = 0;
455 *rd_end = 0;
456
Simon Glassccda2fa2021-09-25 19:43:37 -0600457 /*
458 * Look for a '-' which indicates to ignore the
459 * ramdisk argument
460 */
461 if (select && strcmp(select, "-") == 0) {
462 debug("## Skipping init Ramdisk\n");
463 rd_len = 0;
464 rd_data = 0;
465 } else if (select || genimg_has_config(images)) {
466 int ret;
467
468 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
469 if (ret == -ENOPKG)
470 return 0;
471 else if (ret)
472 return ret;
Simon Glass4d7237b2021-09-25 07:03:15 -0600473 } else if (images->legacy_hdr_valid &&
474 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass60d71542021-09-25 07:03:16 -0600475 IH_TYPE_MULTI)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600476 /*
477 * Now check if we have a legacy mult-component image,
478 * get second entry data start address and len.
479 */
480 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600481 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
482 (ulong)images->legacy_hdr_os);
Simon Glass4d7237b2021-09-25 07:03:15 -0600483
484 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
485 } else {
486 /*
487 * no initrd image
488 */
489 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600490 rd_len = 0;
491 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600492 }
493
494 if (!rd_data) {
495 debug("## No init Ramdisk\n");
496 } else {
497 *rd_start = rd_data;
498 *rd_end = rd_data + rd_len;
499 }
500 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600501 *rd_start, *rd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600502
503 return 0;
504}
505
Simon Glass4d7237b2021-09-25 07:03:15 -0600506/**
507 * boot_ramdisk_high - relocate init ramdisk
Simon Glass4d7237b2021-09-25 07:03:15 -0600508 * @rd_data: ramdisk data start address
509 * @rd_len: ramdisk data length
510 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
511 * start address (after possible relocation)
512 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
513 * end address (after possible relocation)
514 *
515 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
516 * variable and if requested ramdisk data is moved to a specified location.
517 *
518 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
519 * start/end addresses if ramdisk image start and len were provided,
520 * otherwise set initrd_start and initrd_end set to zeros.
521 *
522 * returns:
523 * 0 - success
524 * -1 - failure
525 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530526int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start,
527 ulong *initrd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600528{
529 char *s;
Marek Vasutf816c422024-03-26 23:13:11 +0100530 phys_addr_t initrd_high;
Simon Glass4d7237b2021-09-25 07:03:15 -0600531 int initrd_copy_to_ram = 1;
532
533 s = env_get("initrd_high");
534 if (s) {
535 /* a value of "no" or a similar string will act like 0,
536 * turning the "load high" feature off. This is intentional.
537 */
538 initrd_high = hextoul(s, NULL);
539 if (initrd_high == ~0)
540 initrd_copy_to_ram = 0;
541 } else {
542 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
543 }
544
Marek Vasutf816c422024-03-26 23:13:11 +0100545 debug("## initrd_high = 0x%llx, copy_to_ram = %d\n",
546 (u64)initrd_high, initrd_copy_to_ram);
Simon Glass4d7237b2021-09-25 07:03:15 -0600547
548 if (rd_data) {
549 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
550 debug(" in-place initrd\n");
551 *initrd_start = rd_data;
552 *initrd_end = rd_data + rd_len;
Ilias Apalodimasf72c55e2024-12-18 09:02:32 +0200553 lmb_reserve(rd_data, rd_len, LMB_NONE);
Simon Glass4d7237b2021-09-25 07:03:15 -0600554 } else {
555 if (initrd_high)
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200556 *initrd_start =
557 (ulong)lmb_alloc_base(rd_len,
558 0x1000,
559 initrd_high,
560 LMB_NONE);
Simon Glass4d7237b2021-09-25 07:03:15 -0600561 else
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530562 *initrd_start = (ulong)lmb_alloc(rd_len,
Simon Glass4d7237b2021-09-25 07:03:15 -0600563 0x1000);
564
565 if (*initrd_start == 0) {
566 puts("ramdisk - allocation error\n");
567 goto error;
568 }
569 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
570
571 *initrd_end = *initrd_start + rd_len;
572 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass60d71542021-09-25 07:03:16 -0600573 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600574
575 memmove_wd((void *)*initrd_start,
Simon Glass60d71542021-09-25 07:03:16 -0600576 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass4d7237b2021-09-25 07:03:15 -0600577
Simon Glass4d7237b2021-09-25 07:03:15 -0600578 /*
579 * Ensure the image is flushed to memory to handle
580 * AMP boot scenarios in which we might not be
581 * HW cache coherent
582 */
Simon Glass0f781382021-09-25 19:43:35 -0600583 if (IS_ENABLED(CONFIG_MP)) {
584 flush_cache((unsigned long)*initrd_start,
585 ALIGN(rd_len, ARCH_DMA_MINALIGN));
586 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600587 puts("OK\n");
588 }
589 } else {
590 *initrd_start = 0;
591 *initrd_end = 0;
592 }
593 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600594 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600595
596 return 0;
597
598error:
599 return -1;
600}
Simon Glass4d7237b2021-09-25 07:03:15 -0600601
Simon Glassdf00afa2022-09-06 20:26:50 -0600602int boot_get_setup(struct bootm_headers *images, u8 arch,
Simon Glass4d7237b2021-09-25 07:03:15 -0600603 ulong *setup_start, ulong *setup_len)
604{
Simon Glass0f781382021-09-25 19:43:35 -0600605 if (!CONFIG_IS_ENABLED(FIT))
606 return -ENOENT;
607
Simon Glass4d7237b2021-09-25 07:03:15 -0600608 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600609}
610
Simon Glass01080ab2023-11-18 14:05:11 -0700611int boot_get_fpga(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600612{
613 ulong tmp_img_addr, img_data, img_len;
614 void *buf;
615 int conf_noffset;
616 int fit_img_result;
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100617 const char *uname, *name, *compatible;
Simon Glass4d7237b2021-09-25 07:03:15 -0600618 int err;
619 int devnum = 0; /* TODO support multi fpga platforms */
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100620 int flags = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600621
Simon Glass0f781382021-09-25 19:43:35 -0600622 if (!IS_ENABLED(CONFIG_FPGA))
623 return -ENOSYS;
624
Simon Glass4d7237b2021-09-25 07:03:15 -0600625 /* Check to see if the images struct has a FIT configuration */
626 if (!genimg_has_config(images)) {
627 debug("## FIT configuration was not specified\n");
628 return 0;
629 }
630
631 /*
632 * Obtain the os FIT header from the images struct
633 */
634 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
635 buf = map_sysmem(tmp_img_addr, 0);
636 /*
637 * Check image type. For FIT images get FIT node
638 * and attempt to locate a generic binary.
639 */
640 switch (genimg_get_format(buf)) {
641 case IMAGE_FORMAT_FIT:
642 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
643
644 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
645 NULL);
646 if (!uname) {
647 debug("## FPGA image is not specified\n");
648 return 0;
649 }
650 fit_img_result = fit_image_load(images,
651 tmp_img_addr,
652 (const char **)&uname,
Simon Glass60d71542021-09-25 07:03:16 -0600653 &images->fit_uname_cfg,
Simon Glass01080ab2023-11-18 14:05:11 -0700654 IH_ARCH_DEFAULT,
Simon Glass4d7237b2021-09-25 07:03:15 -0600655 IH_TYPE_FPGA,
656 BOOTSTAGE_ID_FPGA_INIT,
657 FIT_LOAD_OPTIONAL_NON_ZERO,
658 &img_data, &img_len);
659
660 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
661 uname, img_data, img_len);
662
663 if (fit_img_result < 0) {
664 /* Something went wrong! */
665 return fit_img_result;
666 }
667
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100668 conf_noffset = fit_image_get_node(buf, uname);
669 compatible = fdt_getprop(buf, conf_noffset, "compatible", NULL);
670 if (!compatible) {
671 printf("'fpga' image without 'compatible' property\n");
672 } else {
673 if (CONFIG_IS_ENABLED(FPGA_LOAD_SECURE))
674 flags = fpga_compatible2flag(devnum, compatible);
675 }
676
Simon Glass4d7237b2021-09-25 07:03:15 -0600677 if (!fpga_is_partial_data(devnum, img_len)) {
678 name = "full";
679 err = fpga_loadbitstream(devnum, (char *)img_data,
680 img_len, BIT_FULL);
681 if (err)
682 err = fpga_load(devnum, (const void *)img_data,
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100683 img_len, BIT_FULL, flags);
Simon Glass4d7237b2021-09-25 07:03:15 -0600684 } else {
685 name = "partial";
686 err = fpga_loadbitstream(devnum, (char *)img_data,
687 img_len, BIT_PARTIAL);
688 if (err)
689 err = fpga_load(devnum, (const void *)img_data,
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100690 img_len, BIT_PARTIAL, flags);
Simon Glass4d7237b2021-09-25 07:03:15 -0600691 }
692
693 if (err)
694 return err;
695
696 printf(" Programming %s bitstream... OK\n", name);
697 break;
698 default:
699 printf("The given image format is not supported (corrupt?)\n");
700 return 1;
701 }
702
703 return 0;
704}
Simon Glass4d7237b2021-09-25 07:03:15 -0600705
Simon Glass60d71542021-09-25 07:03:16 -0600706static void fit_loadable_process(u8 img_type,
Simon Glass4d7237b2021-09-25 07:03:15 -0600707 ulong img_data,
708 ulong img_len)
709{
710 int i;
711 const unsigned int count =
712 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
713 struct fit_loadable_tbl *fit_loadable_handler =
714 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
715 /* For each loadable handler */
716 for (i = 0; i < count; i++, fit_loadable_handler++)
717 /* matching this type */
718 if (fit_loadable_handler->type == img_type)
719 /* call that handler with this image data */
720 fit_loadable_handler->handler(img_data, img_len);
721}
722
Simon Glass4fdc5592023-11-18 14:05:12 -0700723int boot_get_loadable(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600724{
725 /*
726 * These variables are used to hold the current image location
727 * in system memory.
728 */
729 ulong tmp_img_addr;
730 /*
731 * These two variables are requirements for fit_image_load, but
732 * their values are not used
733 */
734 ulong img_data, img_len;
735 void *buf;
736 int loadables_index;
737 int conf_noffset;
738 int fit_img_result;
739 const char *uname;
Simon Glass60d71542021-09-25 07:03:16 -0600740 u8 img_type;
Simon Glass4d7237b2021-09-25 07:03:15 -0600741
742 /* Check to see if the images struct has a FIT configuration */
743 if (!genimg_has_config(images)) {
744 debug("## FIT configuration was not specified\n");
745 return 0;
746 }
747
748 /*
749 * Obtain the os FIT header from the images struct
750 */
751 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
752 buf = map_sysmem(tmp_img_addr, 0);
753 /*
754 * Check image type. For FIT images get FIT node
755 * and attempt to locate a generic binary.
756 */
757 switch (genimg_get_format(buf)) {
758 case IMAGE_FORMAT_FIT:
759 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
760
761 for (loadables_index = 0;
762 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass60d71542021-09-25 07:03:16 -0600763 FIT_LOADABLE_PROP,
764 loadables_index, NULL), uname;
765 loadables_index++) {
766 fit_img_result = fit_image_load(images, tmp_img_addr,
767 &uname,
768 &images->fit_uname_cfg,
Simon Glass4fdc5592023-11-18 14:05:12 -0700769 IH_ARCH_DEFAULT,
770 IH_TYPE_LOADABLE,
Simon Glass60d71542021-09-25 07:03:16 -0600771 BOOTSTAGE_ID_FIT_LOADABLE_START,
772 FIT_LOAD_OPTIONAL_NON_ZERO,
773 &img_data, &img_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600774 if (fit_img_result < 0) {
775 /* Something went wrong! */
776 return fit_img_result;
777 }
778
779 fit_img_result = fit_image_get_node(buf, uname);
780 if (fit_img_result < 0) {
781 /* Something went wrong! */
782 return fit_img_result;
783 }
784 fit_img_result = fit_image_get_type(buf,
785 fit_img_result,
786 &img_type);
787 if (fit_img_result < 0) {
788 /* Something went wrong! */
789 return fit_img_result;
790 }
791
792 fit_loadable_process(img_type, img_data, img_len);
793 }
794 break;
795 default:
796 printf("The given image format is not supported (corrupt?)\n");
797 return 1;
798 }
799
800 return 0;
801}
Simon Glass4d7237b2021-09-25 07:03:15 -0600802
Simon Glass4d7237b2021-09-25 07:03:15 -0600803/**
804 * boot_get_cmdline - allocate and initialize kernel cmdline
Simon Glass4d7237b2021-09-25 07:03:15 -0600805 * @cmd_start: pointer to a ulong variable, will hold cmdline start
806 * @cmd_end: pointer to a ulong variable, will hold cmdline end
807 *
Simon Glass7ae16bb2022-08-28 12:32:53 -0600808 * This allocates space for kernel command line below
Simon Glass4d7237b2021-09-25 07:03:15 -0600809 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
810 * variable is present its contents is copied to allocated kernel
811 * command line.
812 *
813 * returns:
814 * 0 - success
815 * -1 - failure
816 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530817int boot_get_cmdline(ulong *cmd_start, ulong *cmd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600818{
Simon Glass7ae16bb2022-08-28 12:32:53 -0600819 int barg;
Simon Glass4d7237b2021-09-25 07:03:15 -0600820 char *cmdline;
821 char *s;
822
Simon Glass7ae16bb2022-08-28 12:32:53 -0600823 /*
824 * Help the compiler detect that this function is only called when
825 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
826 */
827 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
828 return 0;
829
830 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530831 cmdline = (char *)(ulong)lmb_alloc_base(barg, 0xf,
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200832 env_get_bootm_mapsize() + env_get_bootm_low(),
833 LMB_NONE);
Simon Glass60d71542021-09-25 07:03:16 -0600834 if (!cmdline)
Simon Glass4d7237b2021-09-25 07:03:15 -0600835 return -1;
836
837 s = env_get("bootargs");
838 if (!s)
839 s = "";
840
841 strcpy(cmdline, s);
842
Simon Glass60d71542021-09-25 07:03:16 -0600843 *cmd_start = (ulong)cmdline;
Simon Glass4d7237b2021-09-25 07:03:15 -0600844 *cmd_end = *cmd_start + strlen(cmdline);
845
846 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
847
848 return 0;
849}
Simon Glass4d7237b2021-09-25 07:03:15 -0600850
Simon Glass4d7237b2021-09-25 07:03:15 -0600851/**
852 * boot_get_kbd - allocate and initialize kernel copy of board info
Simon Glass4d7237b2021-09-25 07:03:15 -0600853 * @kbd: double pointer to board info data
854 *
855 * boot_get_kbd() allocates space for kernel copy of board info data below
856 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
857 * with the current u-boot board info data.
858 *
859 * returns:
860 * 0 - success
861 * -1 - failure
862 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530863int boot_get_kbd(struct bd_info **kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600864{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530865 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(sizeof(struct bd_info),
Ilias Apalodimasd1e9a262024-12-18 09:02:36 +0200866 0xf,
867 env_get_bootm_mapsize() +
868 env_get_bootm_low(),
869 LMB_NONE);
Simon Glass60d71542021-09-25 07:03:16 -0600870 if (!*kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600871 return -1;
872
Simon Glass60d71542021-09-25 07:03:16 -0600873 **kbd = *gd->bd;
Simon Glass4d7237b2021-09-25 07:03:15 -0600874
875 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
876
Simon Glass7ae16bb2022-08-28 12:32:53 -0600877 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
Simon Glassbc0c4db2021-09-25 07:03:20 -0600878 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass4d7237b2021-09-25 07:03:15 -0600879
880 return 0;
881}
Simon Glass4d7237b2021-09-25 07:03:15 -0600882
Simon Glassdf00afa2022-09-06 20:26:50 -0600883int image_setup_linux(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600884{
885 ulong of_size = images->ft_len;
886 char **of_flat_tree = &images->ft_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600887 int ret;
888
Simon Glass7ae16bb2022-08-28 12:32:53 -0600889 /* This function cannot be called without lmb support */
Sughosh Ganu0d733f02024-08-26 17:29:22 +0530890 if (!CONFIG_IS_ENABLED(LMB))
Simon Glass7ae16bb2022-08-28 12:32:53 -0600891 return -EFAULT;
Simon Glass85c057e2021-09-25 19:43:21 -0600892 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530893 boot_fdt_add_mem_rsv_regions(*of_flat_tree);
Simon Glass4d7237b2021-09-25 07:03:15 -0600894
Simon Glassb8eb1fe2021-09-25 19:43:25 -0600895 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530896 ret = boot_get_cmdline(&images->cmdline_start,
Simon Glass60d71542021-09-25 07:03:16 -0600897 &images->cmdline_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600898 if (ret) {
899 puts("ERROR with allocation of cmdline\n");
900 return ret;
901 }
902 }
903
Simon Glass85c057e2021-09-25 19:43:21 -0600904 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530905 ret = boot_relocate_fdt(of_flat_tree, &of_size);
Simon Glass4d7237b2021-09-25 07:03:15 -0600906 if (ret)
907 return ret;
908 }
909
Simon Glass85c057e2021-09-25 19:43:21 -0600910 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530911 ret = image_setup_libfdt(images, *of_flat_tree, true);
Simon Glass4d7237b2021-09-25 07:03:15 -0600912 if (ret)
913 return ret;
914 }
915
916 return 0;
917}
Simon Glass4a8a8a12021-09-25 07:03:17 -0600918
919void genimg_print_size(uint32_t size)
920{
921 printf("%d Bytes = ", size);
922 print_size(size, "\n");
923}
924
925void genimg_print_time(time_t timestamp)
926{
927 struct rtc_time tm;
928
929 rtc_to_tm(timestamp, &tm);
930 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
931 tm.tm_year, tm.tm_mon, tm.tm_mday,
932 tm.tm_hour, tm.tm_min, tm.tm_sec);
933}
Simon Glassdaee3ba2023-01-06 08:52:28 -0600934
935/**
936 * get_default_image() - Return default property from /images
937 *
938 * Return: Pointer to value of default property (or NULL)
939 */
940static const char *get_default_image(const void *fit)
941{
942 int images_noffset;
943
944 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
945 if (images_noffset < 0)
946 return NULL;
947
948 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
949}
950
951int image_locate_script(void *buf, int size, const char *fit_uname,
952 const char *confname, char **datap, uint *lenp)
953{
954 const struct legacy_img_hdr *hdr;
955 const void *fit_data;
956 const void *fit_hdr;
957 size_t fit_len;
958 int noffset;
959 int verify;
960 ulong len;
961 u32 *data;
962
963 verify = env_get_yesno("verify");
964
965 switch (genimg_get_format(buf)) {
966 case IMAGE_FORMAT_LEGACY:
Marek Vasutfb59eca2023-02-27 20:56:31 +0100967 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
968 goto exit_image_format;
969 } else {
Simon Glassdaee3ba2023-01-06 08:52:28 -0600970 hdr = buf;
971
972 if (!image_check_magic(hdr)) {
973 puts("Bad magic number\n");
974 return 1;
975 }
976
977 if (!image_check_hcrc(hdr)) {
978 puts("Bad header crc\n");
979 return 1;
980 }
981
982 if (verify) {
983 if (!image_check_dcrc(hdr)) {
984 puts("Bad data crc\n");
985 return 1;
986 }
987 }
988
989 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
990 puts("Bad image type\n");
991 return 1;
992 }
993
994 /* get length of script */
995 data = (u32 *)image_get_data(hdr);
996
997 len = uimage_to_cpu(*data);
998 if (!len) {
999 puts("Empty Script\n");
1000 return 1;
1001 }
1002
1003 /*
1004 * scripts are just multi-image files with one
1005 * component, so seek past the zero-terminated sequence
1006 * of image lengths to get to the actual image data
1007 */
1008 while (*data++);
1009 }
1010 break;
1011 case IMAGE_FORMAT_FIT:
Marek Vasutfb59eca2023-02-27 20:56:31 +01001012 if (!IS_ENABLED(CONFIG_FIT)) {
1013 goto exit_image_format;
1014 } else {
Simon Glassdaee3ba2023-01-06 08:52:28 -06001015 fit_hdr = buf;
1016 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1017 puts("Bad FIT image format\n");
1018 return 1;
1019 }
1020
1021 if (!fit_uname) {
1022 /* If confname is empty, use the default */
1023 if (confname && *confname)
1024 noffset = fit_conf_get_node(fit_hdr, confname);
1025 else
1026 noffset = fit_conf_get_node(fit_hdr, NULL);
1027 if (noffset < 0) {
1028 if (!confname)
1029 goto fallback;
1030 printf("Could not find config %s\n", confname);
1031 return 1;
1032 }
1033
1034 if (verify && fit_config_verify(fit_hdr, noffset))
1035 return 1;
1036
1037 noffset = fit_conf_get_prop_node(fit_hdr,
1038 noffset,
1039 FIT_SCRIPT_PROP,
1040 IH_PHASE_NONE);
1041 if (noffset < 0) {
1042 if (!confname)
1043 goto fallback;
1044 printf("Could not find script in %s\n", confname);
1045 return 1;
1046 }
1047 } else {
1048fallback:
1049 if (!fit_uname || !*fit_uname)
1050 fit_uname = get_default_image(fit_hdr);
1051 if (!fit_uname) {
1052 puts("No FIT subimage unit name\n");
1053 return 1;
1054 }
1055
1056 /* get script component image node offset */
1057 noffset = fit_image_get_node(fit_hdr, fit_uname);
1058 if (noffset < 0) {
1059 printf("Can't find '%s' FIT subimage\n",
1060 fit_uname);
1061 return 1;
1062 }
1063 }
1064
1065 if (!fit_image_check_type(fit_hdr, noffset,
1066 IH_TYPE_SCRIPT)) {
1067 puts("Not a image image\n");
1068 return 1;
1069 }
1070
1071 /* verify integrity */
1072 if (verify && !fit_image_verify(fit_hdr, noffset)) {
1073 puts("Bad Data Hash\n");
1074 return 1;
1075 }
1076
1077 /* get script subimage data address and length */
Simon Glass833445d2025-01-10 17:00:13 -07001078 if (fit_image_get_data(fit_hdr, noffset, &fit_data,
1079 &fit_len)) {
Simon Glassdaee3ba2023-01-06 08:52:28 -06001080 puts("Could not find script subimage data\n");
1081 return 1;
1082 }
1083
1084 data = (u32 *)fit_data;
1085 len = (ulong)fit_len;
1086 }
1087 break;
1088 default:
Marek Vasutfb59eca2023-02-27 20:56:31 +01001089 goto exit_image_format;
Simon Glassdaee3ba2023-01-06 08:52:28 -06001090 }
1091
1092 *datap = (char *)data;
1093 *lenp = len;
1094
1095 return 0;
Marek Vasutfb59eca2023-02-27 20:56:31 +01001096
1097exit_image_format:
1098 puts("Wrong image format for \"source\" command\n");
1099 return -EPERM;
Simon Glassdaee3ba2023-01-06 08:52:28 -06001100}