blob: 3263497a1d5ff18423e6f090d0b2dbae7695bb44 [file] [log] [blame]
Simon Glass41506ff2021-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
11#include <common.h>
12#include <bootstage.h>
13#include <cpu_func.h>
Simon Glass4e4bf942022-07-31 12:28:48 -060014#include <display_options.h>
Simon Glass41506ff2021-09-25 07:03:15 -060015#include <env.h>
16#include <fpga.h>
17#include <image.h>
Andy Shevchenko4b325312021-11-08 21:03:38 +030018#include <init.h>
Simon Glass9c2e9122022-08-28 12:32:53 -060019#include <log.h>
Simon Glass41506ff2021-09-25 07:03:15 -060020#include <mapmem.h>
Simon Glass5d3248a2021-09-25 07:03:17 -060021#include <rtc.h>
Simon Glass41506ff2021-09-25 07:03:15 -060022#include <watchdog.h>
23#include <asm/cache.h>
24#include <asm/global_data.h>
25
Simon Glass41506ff2021-09-25 07:03:15 -060026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass41506ff2021-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 Glassf3543e62022-09-06 20:26:52 -060043static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
44 int verify)
Simon Glass41506ff2021-09-25 07:03:15 -060045{
Simon Glassf3543e62022-09-06 20:26:52 -060046 const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr;
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -060079 genimg_get_arch_name(arch));
Simon Glass41506ff2021-09-25 07:03:15 -060080 bootstage_error(BOOTSTAGE_ID_RAMDISK);
81 return NULL;
82 }
83
84 return rd_hdr;
85}
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -060095 int flags)
Simon Glass41506ff2021-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 Vasuta4df06e2024-03-26 23:13:11 +0100110phys_addr_t env_get_bootm_low(void)
Simon Glass41506ff2021-09-25 07:03:15 -0600111{
112 char *s = env_get("bootm_low");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600113
Marek Vasuta4df06e2024-03-26 23:13:11 +0100114 if (s)
115 return simple_strtoull(s, NULL, 16);
Simon Glass41506ff2021-09-25 07:03:15 -0600116
Tom Riniaa6e94d2022-11-16 13:10:37 -0500117#if defined(CFG_SYS_SDRAM_BASE)
118 return CFG_SYS_SDRAM_BASE;
Simon Glass41506ff2021-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{
128 phys_size_t tmp, size;
129 phys_addr_t start;
130 char *s = env_get("bootm_size");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600131
Simon Glass41506ff2021-09-25 07:03:15 -0600132 if (s) {
133 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
134 return tmp;
135 }
136
137 start = gd->ram_base;
138 size = gd->ram_size;
139
140 if (start + size > gd->ram_top)
141 size = gd->ram_top - start;
142
143 s = env_get("bootm_low");
144 if (s)
145 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
146 else
147 tmp = start;
148
149 return size - (tmp - start);
150}
151
152phys_size_t env_get_bootm_mapsize(void)
153{
154 phys_size_t tmp;
155 char *s = env_get("bootm_mapsize");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600156
Simon Glass41506ff2021-09-25 07:03:15 -0600157 if (s) {
158 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
159 return tmp;
160 }
161
Tom Rini65cc0e22022-11-16 13:10:41 -0500162#if defined(CFG_SYS_BOOTMAPSZ)
163 return CFG_SYS_BOOTMAPSZ;
Simon Glass41506ff2021-09-25 07:03:15 -0600164#else
165 return env_get_bootm_size();
166#endif
167}
168
169void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
170{
171 if (to == from)
172 return;
173
Simon Glass9c2e9122022-08-28 12:32:53 -0600174 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600175 if (to > from) {
Simon Glass9c2e9122022-08-28 12:32:53 -0600176 from += len;
177 to += len;
Simon Glass41506ff2021-09-25 07:03:15 -0600178 }
Simon Glass9c2e9122022-08-28 12:32:53 -0600179 while (len > 0) {
180 size_t tail = (len > chunksz) ? chunksz : len;
181
Stefan Roese29caf932022-09-02 14:10:46 +0200182 schedule();
Simon Glass9c2e9122022-08-28 12:32:53 -0600183 if (to > from) {
184 to -= tail;
185 from -= tail;
186 }
187 memmove(to, from, tail);
188 if (to < from) {
189 to += tail;
190 from += tail;
191 }
192 len -= tail;
Simon Glass41506ff2021-09-25 07:03:15 -0600193 }
Simon Glass9c2e9122022-08-28 12:32:53 -0600194 } else {
195 memmove(to, from, len);
Simon Glass41506ff2021-09-25 07:03:15 -0600196 }
Simon Glass41506ff2021-09-25 07:03:15 -0600197}
198
Simon Glass530cc472023-11-18 14:04:57 -0700199ulong genimg_get_kernel_addr_fit(const char *const img_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600200 const char **fit_uname_config,
201 const char **fit_uname_kernel)
Simon Glass41506ff2021-09-25 07:03:15 -0600202{
203 ulong kernel_addr;
204
205 /* find out kernel image address */
206 if (!img_addr) {
207 kernel_addr = image_load_addr;
208 debug("* kernel: default image load address = 0x%08lx\n",
209 image_load_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600210 } else if (CONFIG_IS_ENABLED(FIT) &&
211 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass41506ff2021-09-25 07:03:15 -0600212 fit_uname_config)) {
213 debug("* kernel: config '%s' from image at 0x%08lx\n",
214 *fit_uname_config, kernel_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600215 } else if (CONFIG_IS_ENABLED(FIT) &&
216 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
217 fit_uname_kernel)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600218 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
219 *fit_uname_kernel, kernel_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600220 } else {
221 kernel_addr = hextoul(img_addr, NULL);
222 debug("* kernel: cmdline image address = 0x%08lx\n",
223 kernel_addr);
224 }
225
226 return kernel_addr;
227}
228
229/**
230 * genimg_get_kernel_addr() is the simple version of
231 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
232 */
233ulong genimg_get_kernel_addr(char * const img_addr)
234{
235 const char *fit_uname_config = NULL;
236 const char *fit_uname_kernel = NULL;
237
238 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
239 &fit_uname_kernel);
240}
241
242/**
243 * genimg_get_format - get image format type
244 * @img_addr: image start address
245 *
246 * genimg_get_format() checks whether provided address points to a valid
247 * legacy or FIT image.
248 *
249 * New uImage format and FDT blob are based on a libfdt. FDT blob
250 * may be passed directly or embedded in a FIT image. In both situations
251 * genimg_get_format() must be able to dectect libfdt header.
252 *
253 * returns:
254 * image format type or IMAGE_FORMAT_INVALID if no image is present
255 */
256int genimg_get_format(const void *img_addr)
257{
Simon Glass1df654a2021-09-25 19:43:35 -0600258 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassf3543e62022-09-06 20:26:52 -0600259 const struct legacy_img_hdr *hdr;
Simon Glass41506ff2021-09-25 07:03:15 -0600260
Simon Glassf3543e62022-09-06 20:26:52 -0600261 hdr = (const struct legacy_img_hdr *)img_addr;
Simon Glass1df654a2021-09-25 19:43:35 -0600262 if (image_check_magic(hdr))
263 return IMAGE_FORMAT_LEGACY;
264 }
265 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
266 if (!fdt_check_header(img_addr))
267 return IMAGE_FORMAT_FIT;
268 }
269 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
Safae Ouajih734cb472023-02-06 00:50:05 +0100270 is_android_boot_image_header(img_addr))
Simon Glass41506ff2021-09-25 07:03:15 -0600271 return IMAGE_FORMAT_ANDROID;
Simon Glass41506ff2021-09-25 07:03:15 -0600272
273 return IMAGE_FORMAT_INVALID;
274}
275
276/**
277 * fit_has_config - check if there is a valid FIT configuration
278 * @images: pointer to the bootm command headers structure
279 *
280 * fit_has_config() checks if there is a FIT configuration in use
281 * (if FTI support is present).
282 *
283 * returns:
284 * 0, no FIT support or no configuration found
285 * 1, configuration found
286 */
Simon Glassd9d7c202022-09-06 20:26:50 -0600287int genimg_has_config(struct bootm_headers *images)
Simon Glass41506ff2021-09-25 07:03:15 -0600288{
Simon Glass1df654a2021-09-25 19:43:35 -0600289 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass41506ff2021-09-25 07:03:15 -0600290 return 1;
Simon Glass1df654a2021-09-25 19:43:35 -0600291
Simon Glass41506ff2021-09-25 07:03:15 -0600292 return 0;
293}
294
295/**
Simon Glasse4c92872021-09-25 19:43:37 -0600296 * select_ramdisk() - Select and locate the ramdisk to use
297 *
Simon Glass41506ff2021-09-25 07:03:15 -0600298 * @images: pointer to the bootm images structure
Simon Glass4f2d9412022-08-28 12:32:47 -0600299 * @select: name of ramdisk to select, or hex address, NULL for any
Simon Glass41506ff2021-09-25 07:03:15 -0600300 * @arch: expected ramdisk architecture
Simon Glasse4c92872021-09-25 19:43:37 -0600301 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
302 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100303 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glasse4c92872021-09-25 19:43:37 -0600304 * other -ve value on other error
Simon Glass41506ff2021-09-25 07:03:15 -0600305 */
Simon Glassd9d7c202022-09-06 20:26:50 -0600306static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
Simon Glasse4c92872021-09-25 19:43:37 -0600307 ulong *rd_datap, ulong *rd_lenp)
Simon Glass41506ff2021-09-25 07:03:15 -0600308{
Simon Glassf7659f62022-08-28 12:32:49 -0600309 const char *fit_uname_config;
310 const char *fit_uname_ramdisk;
Simon Glassca788832022-08-28 12:32:51 -0600311 bool done_select = !select;
Simon Glass4f2d9412022-08-28 12:32:47 -0600312 bool done = false;
Simon Glassf7659f62022-08-28 12:32:49 -0600313 int rd_noffset;
Tom Rini51a765b2023-04-05 19:48:56 -0400314 ulong rd_addr = 0;
Simon Glasse4c92872021-09-25 19:43:37 -0600315 char *buf;
Simon Glass78f88792021-09-25 19:43:36 -0600316
Simon Glassca788832022-08-28 12:32:51 -0600317 if (CONFIG_IS_ENABLED(FIT)) {
Simon Glassf7659f62022-08-28 12:32:49 -0600318 fit_uname_config = images->fit_uname_cfg;
319 fit_uname_ramdisk = NULL;
Simon Glass78f88792021-09-25 19:43:36 -0600320
Tom Rini621158d2021-12-20 09:36:32 -0500321 if (select) {
322 ulong default_addr;
Simon Glass41506ff2021-09-25 07:03:15 -0600323 /*
324 * If the init ramdisk comes from the FIT image and
325 * the FIT image address is omitted in the command
326 * line argument, try to use os FIT image address or
327 * default load address.
328 */
329 if (images->fit_uname_os)
330 default_addr = (ulong)images->fit_hdr_os;
331 else
332 default_addr = image_load_addr;
333
Simon Glass20f5d832022-08-28 12:32:52 -0600334 if (fit_parse_conf(select, default_addr, &rd_addr,
335 &fit_uname_config)) {
Simon Glass3d2a47f2021-09-25 07:03:16 -0600336 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
337 fit_uname_config, rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600338 done_select = true;
Simon Glass41506ff2021-09-25 07:03:15 -0600339 } else if (fit_parse_subimage(select, default_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600340 &rd_addr,
341 &fit_uname_ramdisk)) {
342 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
343 fit_uname_ramdisk, rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600344 done_select = true;
345 }
346 }
347 }
348 if (!done_select) {
Simon Glass20f5d832022-08-28 12:32:52 -0600349 rd_addr = hextoul(select, NULL);
350 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600351 }
Simon Glass20f5d832022-08-28 12:32:52 -0600352 if (CONFIG_IS_ENABLED(FIT) && !select) {
353 /* use FIT configuration provided in first bootm
354 * command argument. If the property is not defined,
355 * quit silently (with -ENOPKG)
Tom Rini621158d2021-12-20 09:36:32 -0500356 */
Simon Glass20f5d832022-08-28 12:32:52 -0600357 rd_addr = map_to_sysmem(images->fit_hdr_os);
358 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
359 rd_addr);
360 if (rd_noffset == -ENOENT)
361 return -ENOPKG;
362 else if (rd_noffset < 0)
363 return rd_noffset;
364 }
Simon Glass78f88792021-09-25 19:43:36 -0600365
Simon Glass20f5d832022-08-28 12:32:52 -0600366 /*
367 * Check if there is an initrd image at the
368 * address provided in the second bootm argument
369 * check image type, for FIT images get FIT node.
370 */
371 buf = map_sysmem(rd_addr, 0);
372 switch (genimg_get_format(buf)) {
373 case IMAGE_FORMAT_LEGACY:
374 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassf3543e62022-09-06 20:26:52 -0600375 const struct legacy_img_hdr *rd_hdr;
Simon Glass41506ff2021-09-25 07:03:15 -0600376
Simon Glass20f5d832022-08-28 12:32:52 -0600377 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
378 rd_addr);
Tom Rini621158d2021-12-20 09:36:32 -0500379
Simon Glass20f5d832022-08-28 12:32:52 -0600380 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
381 rd_hdr = image_get_ramdisk(rd_addr, arch,
382 images->verify);
Simon Glass41506ff2021-09-25 07:03:15 -0600383
Simon Glass20f5d832022-08-28 12:32:52 -0600384 if (!rd_hdr)
385 return -ENOENT;
Simon Glass41506ff2021-09-25 07:03:15 -0600386
Simon Glass20f5d832022-08-28 12:32:52 -0600387 *rd_datap = image_get_data(rd_hdr);
388 *rd_lenp = image_get_data_size(rd_hdr);
389 done = true;
Simon Glass4f2d9412022-08-28 12:32:47 -0600390 }
Simon Glass20f5d832022-08-28 12:32:52 -0600391 break;
392 case IMAGE_FORMAT_FIT:
393 if (CONFIG_IS_ENABLED(FIT)) {
394 rd_noffset = fit_image_load(images, rd_addr,
395 &fit_uname_ramdisk,
396 &fit_uname_config,
397 arch, IH_TYPE_RAMDISK,
398 BOOTSTAGE_ID_FIT_RD_START,
399 FIT_LOAD_OPTIONAL_NON_ZERO,
400 rd_datap, rd_lenp);
401 if (rd_noffset < 0)
402 return rd_noffset;
403
404 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
405 images->fit_uname_rd = fit_uname_ramdisk;
406 images->fit_noffset_rd = rd_noffset;
407 done = true;
408 }
409 break;
410 case IMAGE_FORMAT_ANDROID:
411 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass20f5d832022-08-28 12:32:52 -0600412 int ret;
Safae Ouajih636da202023-02-06 00:50:17 +0100413 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
414 void *boot_img = map_sysmem(get_abootimg_addr(), 0);
415 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
416
417 ret = android_image_get_ramdisk(boot_img, vendor_boot_img,
418 rd_datap, rd_lenp);
419 unmap_sysmem(vendor_boot_img);
420 unmap_sysmem(boot_img);
421 } else {
422 void *ptr = map_sysmem(images->os.start, 0);
423
424 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp);
425 unmap_sysmem(ptr);
426 }
427
Simon Glass20f5d832022-08-28 12:32:52 -0600428 if (ret)
429 return ret;
430 done = true;
431 }
432 break;
433 }
Simon Glass1df654a2021-09-25 19:43:35 -0600434
Simon Glass4f2d9412022-08-28 12:32:47 -0600435 if (!done) {
436 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
437 char *end = NULL;
438
439 if (select)
440 end = strchr(select, ':');
441 if (end) {
442 *rd_lenp = hextoul(++end, NULL);
443 *rd_datap = rd_addr;
444 done = true;
Simon Glass41506ff2021-09-25 07:03:15 -0600445 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600446 }
447
448 if (!done) {
Simon Glass1df654a2021-09-25 19:43:35 -0600449 puts("Wrong Ramdisk Image Format\n");
Simon Glasse4c92872021-09-25 19:43:37 -0600450 return -EINVAL;
Simon Glass41506ff2021-09-25 07:03:15 -0600451 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600452 }
Simon Glasse4c92872021-09-25 19:43:37 -0600453
454 return 0;
455}
456
Simon Glass8eda15b2023-11-18 14:05:06 -0700457int boot_get_ramdisk(char const *select, struct bootm_headers *images,
458 uint arch, ulong *rd_start, ulong *rd_end)
Simon Glasse4c92872021-09-25 19:43:37 -0600459{
460 ulong rd_data, rd_len;
Simon Glasse4c92872021-09-25 19:43:37 -0600461
462 *rd_start = 0;
463 *rd_end = 0;
464
Simon Glasse4c92872021-09-25 19:43:37 -0600465 /*
466 * Look for a '-' which indicates to ignore the
467 * ramdisk argument
468 */
469 if (select && strcmp(select, "-") == 0) {
470 debug("## Skipping init Ramdisk\n");
471 rd_len = 0;
472 rd_data = 0;
473 } else if (select || genimg_has_config(images)) {
474 int ret;
475
476 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
477 if (ret == -ENOPKG)
478 return 0;
479 else if (ret)
480 return ret;
Simon Glass41506ff2021-09-25 07:03:15 -0600481 } else if (images->legacy_hdr_valid &&
482 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600483 IH_TYPE_MULTI)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600484 /*
485 * Now check if we have a legacy mult-component image,
486 * get second entry data start address and len.
487 */
488 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600489 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
490 (ulong)images->legacy_hdr_os);
Simon Glass41506ff2021-09-25 07:03:15 -0600491
492 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
493 } else {
494 /*
495 * no initrd image
496 */
497 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600498 rd_len = 0;
499 rd_data = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600500 }
501
502 if (!rd_data) {
503 debug("## No init Ramdisk\n");
504 } else {
505 *rd_start = rd_data;
506 *rd_end = rd_data + rd_len;
507 }
508 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600509 *rd_start, *rd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600510
511 return 0;
512}
513
Simon Glass41506ff2021-09-25 07:03:15 -0600514/**
515 * boot_ramdisk_high - relocate init ramdisk
516 * @lmb: pointer to lmb handle, will be used for memory mgmt
517 * @rd_data: ramdisk data start address
518 * @rd_len: ramdisk data length
519 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
520 * start address (after possible relocation)
521 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
522 * end address (after possible relocation)
523 *
524 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
525 * variable and if requested ramdisk data is moved to a specified location.
526 *
527 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
528 * start/end addresses if ramdisk image start and len were provided,
529 * otherwise set initrd_start and initrd_end set to zeros.
530 *
531 * returns:
532 * 0 - success
533 * -1 - failure
534 */
535int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600536 ulong *initrd_start, ulong *initrd_end)
Simon Glass41506ff2021-09-25 07:03:15 -0600537{
538 char *s;
Marek Vasuta4df06e2024-03-26 23:13:11 +0100539 phys_addr_t initrd_high;
Simon Glass41506ff2021-09-25 07:03:15 -0600540 int initrd_copy_to_ram = 1;
541
542 s = env_get("initrd_high");
543 if (s) {
544 /* a value of "no" or a similar string will act like 0,
545 * turning the "load high" feature off. This is intentional.
546 */
547 initrd_high = hextoul(s, NULL);
548 if (initrd_high == ~0)
549 initrd_copy_to_ram = 0;
550 } else {
551 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
552 }
553
Marek Vasuta4df06e2024-03-26 23:13:11 +0100554 debug("## initrd_high = 0x%llx, copy_to_ram = %d\n",
555 (u64)initrd_high, initrd_copy_to_ram);
Simon Glass41506ff2021-09-25 07:03:15 -0600556
557 if (rd_data) {
558 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
559 debug(" in-place initrd\n");
560 *initrd_start = rd_data;
561 *initrd_end = rd_data + rd_len;
562 lmb_reserve(lmb, rd_data, rd_len);
563 } else {
564 if (initrd_high)
565 *initrd_start = (ulong)lmb_alloc_base(lmb,
566 rd_len, 0x1000, initrd_high);
567 else
568 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
569 0x1000);
570
571 if (*initrd_start == 0) {
572 puts("ramdisk - allocation error\n");
573 goto error;
574 }
575 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
576
577 *initrd_end = *initrd_start + rd_len;
578 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600579 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600580
581 memmove_wd((void *)*initrd_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600582 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass41506ff2021-09-25 07:03:15 -0600583
Simon Glass41506ff2021-09-25 07:03:15 -0600584 /*
585 * Ensure the image is flushed to memory to handle
586 * AMP boot scenarios in which we might not be
587 * HW cache coherent
588 */
Simon Glass1df654a2021-09-25 19:43:35 -0600589 if (IS_ENABLED(CONFIG_MP)) {
590 flush_cache((unsigned long)*initrd_start,
591 ALIGN(rd_len, ARCH_DMA_MINALIGN));
592 }
Simon Glass41506ff2021-09-25 07:03:15 -0600593 puts("OK\n");
594 }
595 } else {
596 *initrd_start = 0;
597 *initrd_end = 0;
598 }
599 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600600 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600601
602 return 0;
603
604error:
605 return -1;
606}
Simon Glass41506ff2021-09-25 07:03:15 -0600607
Simon Glassd9d7c202022-09-06 20:26:50 -0600608int boot_get_setup(struct bootm_headers *images, u8 arch,
Simon Glass41506ff2021-09-25 07:03:15 -0600609 ulong *setup_start, ulong *setup_len)
610{
Simon Glass1df654a2021-09-25 19:43:35 -0600611 if (!CONFIG_IS_ENABLED(FIT))
612 return -ENOENT;
613
Simon Glass41506ff2021-09-25 07:03:15 -0600614 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600615}
616
Simon Glass745367b2023-11-18 14:05:11 -0700617int boot_get_fpga(struct bootm_headers *images)
Simon Glass41506ff2021-09-25 07:03:15 -0600618{
619 ulong tmp_img_addr, img_data, img_len;
620 void *buf;
621 int conf_noffset;
622 int fit_img_result;
623 const char *uname, *name;
624 int err;
625 int devnum = 0; /* TODO support multi fpga platforms */
626
Simon Glass1df654a2021-09-25 19:43:35 -0600627 if (!IS_ENABLED(CONFIG_FPGA))
628 return -ENOSYS;
629
Simon Glass41506ff2021-09-25 07:03:15 -0600630 /* Check to see if the images struct has a FIT configuration */
631 if (!genimg_has_config(images)) {
632 debug("## FIT configuration was not specified\n");
633 return 0;
634 }
635
636 /*
637 * Obtain the os FIT header from the images struct
638 */
639 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
640 buf = map_sysmem(tmp_img_addr, 0);
641 /*
642 * Check image type. For FIT images get FIT node
643 * and attempt to locate a generic binary.
644 */
645 switch (genimg_get_format(buf)) {
646 case IMAGE_FORMAT_FIT:
647 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
648
649 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
650 NULL);
651 if (!uname) {
652 debug("## FPGA image is not specified\n");
653 return 0;
654 }
655 fit_img_result = fit_image_load(images,
656 tmp_img_addr,
657 (const char **)&uname,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600658 &images->fit_uname_cfg,
Simon Glass745367b2023-11-18 14:05:11 -0700659 IH_ARCH_DEFAULT,
Simon Glass41506ff2021-09-25 07:03:15 -0600660 IH_TYPE_FPGA,
661 BOOTSTAGE_ID_FPGA_INIT,
662 FIT_LOAD_OPTIONAL_NON_ZERO,
663 &img_data, &img_len);
664
665 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
666 uname, img_data, img_len);
667
668 if (fit_img_result < 0) {
669 /* Something went wrong! */
670 return fit_img_result;
671 }
672
673 if (!fpga_is_partial_data(devnum, img_len)) {
674 name = "full";
675 err = fpga_loadbitstream(devnum, (char *)img_data,
676 img_len, BIT_FULL);
677 if (err)
678 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300679 img_len, BIT_FULL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600680 } else {
681 name = "partial";
682 err = fpga_loadbitstream(devnum, (char *)img_data,
683 img_len, BIT_PARTIAL);
684 if (err)
685 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300686 img_len, BIT_PARTIAL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600687 }
688
689 if (err)
690 return err;
691
692 printf(" Programming %s bitstream... OK\n", name);
693 break;
694 default:
695 printf("The given image format is not supported (corrupt?)\n");
696 return 1;
697 }
698
699 return 0;
700}
Simon Glass41506ff2021-09-25 07:03:15 -0600701
Simon Glass3d2a47f2021-09-25 07:03:16 -0600702static void fit_loadable_process(u8 img_type,
Simon Glass41506ff2021-09-25 07:03:15 -0600703 ulong img_data,
704 ulong img_len)
705{
706 int i;
707 const unsigned int count =
708 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
709 struct fit_loadable_tbl *fit_loadable_handler =
710 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
711 /* For each loadable handler */
712 for (i = 0; i < count; i++, fit_loadable_handler++)
713 /* matching this type */
714 if (fit_loadable_handler->type == img_type)
715 /* call that handler with this image data */
716 fit_loadable_handler->handler(img_data, img_len);
717}
718
Simon Glass96456282023-11-18 14:05:12 -0700719int boot_get_loadable(struct bootm_headers *images)
Simon Glass41506ff2021-09-25 07:03:15 -0600720{
721 /*
722 * These variables are used to hold the current image location
723 * in system memory.
724 */
725 ulong tmp_img_addr;
726 /*
727 * These two variables are requirements for fit_image_load, but
728 * their values are not used
729 */
730 ulong img_data, img_len;
731 void *buf;
732 int loadables_index;
733 int conf_noffset;
734 int fit_img_result;
735 const char *uname;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600736 u8 img_type;
Simon Glass41506ff2021-09-25 07:03:15 -0600737
738 /* Check to see if the images struct has a FIT configuration */
739 if (!genimg_has_config(images)) {
740 debug("## FIT configuration was not specified\n");
741 return 0;
742 }
743
744 /*
745 * Obtain the os FIT header from the images struct
746 */
747 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
748 buf = map_sysmem(tmp_img_addr, 0);
749 /*
750 * Check image type. For FIT images get FIT node
751 * and attempt to locate a generic binary.
752 */
753 switch (genimg_get_format(buf)) {
754 case IMAGE_FORMAT_FIT:
755 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
756
757 for (loadables_index = 0;
758 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600759 FIT_LOADABLE_PROP,
760 loadables_index, NULL), uname;
761 loadables_index++) {
762 fit_img_result = fit_image_load(images, tmp_img_addr,
763 &uname,
764 &images->fit_uname_cfg,
Simon Glass96456282023-11-18 14:05:12 -0700765 IH_ARCH_DEFAULT,
766 IH_TYPE_LOADABLE,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600767 BOOTSTAGE_ID_FIT_LOADABLE_START,
768 FIT_LOAD_OPTIONAL_NON_ZERO,
769 &img_data, &img_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600770 if (fit_img_result < 0) {
771 /* Something went wrong! */
772 return fit_img_result;
773 }
774
775 fit_img_result = fit_image_get_node(buf, uname);
776 if (fit_img_result < 0) {
777 /* Something went wrong! */
778 return fit_img_result;
779 }
780 fit_img_result = fit_image_get_type(buf,
781 fit_img_result,
782 &img_type);
783 if (fit_img_result < 0) {
784 /* Something went wrong! */
785 return fit_img_result;
786 }
787
788 fit_loadable_process(img_type, img_data, img_len);
789 }
790 break;
791 default:
792 printf("The given image format is not supported (corrupt?)\n");
793 return 1;
794 }
795
796 return 0;
797}
Simon Glass41506ff2021-09-25 07:03:15 -0600798
Simon Glass41506ff2021-09-25 07:03:15 -0600799/**
800 * boot_get_cmdline - allocate and initialize kernel cmdline
801 * @lmb: pointer to lmb handle, will be used for memory mgmt
802 * @cmd_start: pointer to a ulong variable, will hold cmdline start
803 * @cmd_end: pointer to a ulong variable, will hold cmdline end
804 *
Simon Glass9c2e9122022-08-28 12:32:53 -0600805 * This allocates space for kernel command line below
Simon Glass41506ff2021-09-25 07:03:15 -0600806 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
807 * variable is present its contents is copied to allocated kernel
808 * command line.
809 *
810 * returns:
811 * 0 - success
812 * -1 - failure
813 */
814int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
815{
Simon Glass9c2e9122022-08-28 12:32:53 -0600816 int barg;
Simon Glass41506ff2021-09-25 07:03:15 -0600817 char *cmdline;
818 char *s;
819
Simon Glass9c2e9122022-08-28 12:32:53 -0600820 /*
821 * Help the compiler detect that this function is only called when
822 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
823 */
824 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
825 return 0;
826
827 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
828 cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
Simon Glass41506ff2021-09-25 07:03:15 -0600829 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass3d2a47f2021-09-25 07:03:16 -0600830 if (!cmdline)
Simon Glass41506ff2021-09-25 07:03:15 -0600831 return -1;
832
833 s = env_get("bootargs");
834 if (!s)
835 s = "";
836
837 strcpy(cmdline, s);
838
Simon Glass3d2a47f2021-09-25 07:03:16 -0600839 *cmd_start = (ulong)cmdline;
Simon Glass41506ff2021-09-25 07:03:15 -0600840 *cmd_end = *cmd_start + strlen(cmdline);
841
842 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
843
844 return 0;
845}
Simon Glass41506ff2021-09-25 07:03:15 -0600846
Simon Glass41506ff2021-09-25 07:03:15 -0600847/**
848 * boot_get_kbd - allocate and initialize kernel copy of board info
849 * @lmb: pointer to lmb handle, will be used for memory mgmt
850 * @kbd: double pointer to board info data
851 *
852 * boot_get_kbd() allocates space for kernel copy of board info data below
853 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
854 * with the current u-boot board info data.
855 *
856 * returns:
857 * 0 - success
858 * -1 - failure
859 */
860int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
861{
862 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
863 sizeof(struct bd_info),
864 0xf,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600865 env_get_bootm_mapsize() +
866 env_get_bootm_low());
867 if (!*kbd)
Simon Glass41506ff2021-09-25 07:03:15 -0600868 return -1;
869
Simon Glass3d2a47f2021-09-25 07:03:16 -0600870 **kbd = *gd->bd;
Simon Glass41506ff2021-09-25 07:03:15 -0600871
872 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
873
Simon Glass9c2e9122022-08-28 12:32:53 -0600874 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
Simon Glass4ed37ab2021-09-25 07:03:20 -0600875 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass41506ff2021-09-25 07:03:15 -0600876
877 return 0;
878}
Simon Glass41506ff2021-09-25 07:03:15 -0600879
Simon Glassd9d7c202022-09-06 20:26:50 -0600880int image_setup_linux(struct bootm_headers *images)
Simon Glass41506ff2021-09-25 07:03:15 -0600881{
882 ulong of_size = images->ft_len;
883 char **of_flat_tree = &images->ft_addr;
Simon Glass9c2e9122022-08-28 12:32:53 -0600884 struct lmb *lmb = images_lmb(images);
Simon Glass41506ff2021-09-25 07:03:15 -0600885 int ret;
886
Simon Glass9c2e9122022-08-28 12:32:53 -0600887 /* This function cannot be called without lmb support */
Simon Glass210af542023-02-05 15:40:13 -0700888 if (!IS_ENABLED(CONFIG_LMB))
Simon Glass9c2e9122022-08-28 12:32:53 -0600889 return -EFAULT;
Simon Glass0c303f92021-09-25 19:43:21 -0600890 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass41506ff2021-09-25 07:03:15 -0600891 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
892
Simon Glass806d1ff2021-09-25 19:43:25 -0600893 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600894 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600895 &images->cmdline_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600896 if (ret) {
897 puts("ERROR with allocation of cmdline\n");
898 return ret;
899 }
900 }
901
Simon Glass0c303f92021-09-25 19:43:21 -0600902 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600903 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
904 if (ret)
905 return ret;
906 }
907
Simon Glass0c303f92021-09-25 19:43:21 -0600908 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass1de1a032023-11-12 08:27:44 -0700909 ret = image_setup_libfdt(images, *of_flat_tree, lmb);
Simon Glass41506ff2021-09-25 07:03:15 -0600910 if (ret)
911 return ret;
912 }
913
914 return 0;
915}
Simon Glass5d3248a2021-09-25 07:03:17 -0600916
917void genimg_print_size(uint32_t size)
918{
919 printf("%d Bytes = ", size);
920 print_size(size, "\n");
921}
922
923void genimg_print_time(time_t timestamp)
924{
925 struct rtc_time tm;
926
927 rtc_to_tm(timestamp, &tm);
928 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
929 tm.tm_year, tm.tm_mon, tm.tm_mday,
930 tm.tm_hour, tm.tm_min, tm.tm_sec);
931}
Simon Glass30f33332023-01-06 08:52:28 -0600932
933/**
934 * get_default_image() - Return default property from /images
935 *
936 * Return: Pointer to value of default property (or NULL)
937 */
938static const char *get_default_image(const void *fit)
939{
940 int images_noffset;
941
942 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
943 if (images_noffset < 0)
944 return NULL;
945
946 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
947}
948
949int image_locate_script(void *buf, int size, const char *fit_uname,
950 const char *confname, char **datap, uint *lenp)
951{
952 const struct legacy_img_hdr *hdr;
953 const void *fit_data;
954 const void *fit_hdr;
955 size_t fit_len;
956 int noffset;
957 int verify;
958 ulong len;
959 u32 *data;
960
961 verify = env_get_yesno("verify");
962
963 switch (genimg_get_format(buf)) {
964 case IMAGE_FORMAT_LEGACY:
Marek Vasut771cb4d2023-02-27 20:56:31 +0100965 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
966 goto exit_image_format;
967 } else {
Simon Glass30f33332023-01-06 08:52:28 -0600968 hdr = buf;
969
970 if (!image_check_magic(hdr)) {
971 puts("Bad magic number\n");
972 return 1;
973 }
974
975 if (!image_check_hcrc(hdr)) {
976 puts("Bad header crc\n");
977 return 1;
978 }
979
980 if (verify) {
981 if (!image_check_dcrc(hdr)) {
982 puts("Bad data crc\n");
983 return 1;
984 }
985 }
986
987 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
988 puts("Bad image type\n");
989 return 1;
990 }
991
992 /* get length of script */
993 data = (u32 *)image_get_data(hdr);
994
995 len = uimage_to_cpu(*data);
996 if (!len) {
997 puts("Empty Script\n");
998 return 1;
999 }
1000
1001 /*
1002 * scripts are just multi-image files with one
1003 * component, so seek past the zero-terminated sequence
1004 * of image lengths to get to the actual image data
1005 */
1006 while (*data++);
1007 }
1008 break;
1009 case IMAGE_FORMAT_FIT:
Marek Vasut771cb4d2023-02-27 20:56:31 +01001010 if (!IS_ENABLED(CONFIG_FIT)) {
1011 goto exit_image_format;
1012 } else {
Simon Glass30f33332023-01-06 08:52:28 -06001013 fit_hdr = buf;
1014 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1015 puts("Bad FIT image format\n");
1016 return 1;
1017 }
1018
1019 if (!fit_uname) {
1020 /* If confname is empty, use the default */
1021 if (confname && *confname)
1022 noffset = fit_conf_get_node(fit_hdr, confname);
1023 else
1024 noffset = fit_conf_get_node(fit_hdr, NULL);
1025 if (noffset < 0) {
1026 if (!confname)
1027 goto fallback;
1028 printf("Could not find config %s\n", confname);
1029 return 1;
1030 }
1031
1032 if (verify && fit_config_verify(fit_hdr, noffset))
1033 return 1;
1034
1035 noffset = fit_conf_get_prop_node(fit_hdr,
1036 noffset,
1037 FIT_SCRIPT_PROP,
1038 IH_PHASE_NONE);
1039 if (noffset < 0) {
1040 if (!confname)
1041 goto fallback;
1042 printf("Could not find script in %s\n", confname);
1043 return 1;
1044 }
1045 } else {
1046fallback:
1047 if (!fit_uname || !*fit_uname)
1048 fit_uname = get_default_image(fit_hdr);
1049 if (!fit_uname) {
1050 puts("No FIT subimage unit name\n");
1051 return 1;
1052 }
1053
1054 /* get script component image node offset */
1055 noffset = fit_image_get_node(fit_hdr, fit_uname);
1056 if (noffset < 0) {
1057 printf("Can't find '%s' FIT subimage\n",
1058 fit_uname);
1059 return 1;
1060 }
1061 }
1062
1063 if (!fit_image_check_type(fit_hdr, noffset,
1064 IH_TYPE_SCRIPT)) {
1065 puts("Not a image image\n");
1066 return 1;
1067 }
1068
1069 /* verify integrity */
1070 if (verify && !fit_image_verify(fit_hdr, noffset)) {
1071 puts("Bad Data Hash\n");
1072 return 1;
1073 }
1074
1075 /* get script subimage data address and length */
Tobias Waldekranze45bba52023-02-16 16:33:47 +01001076 if (fit_image_get_data_and_size(fit_hdr, noffset,
1077 &fit_data, &fit_len)) {
Simon Glass30f33332023-01-06 08:52:28 -06001078 puts("Could not find script subimage data\n");
1079 return 1;
1080 }
1081
1082 data = (u32 *)fit_data;
1083 len = (ulong)fit_len;
1084 }
1085 break;
1086 default:
Marek Vasut771cb4d2023-02-27 20:56:31 +01001087 goto exit_image_format;
Simon Glass30f33332023-01-06 08:52:28 -06001088 }
1089
1090 *datap = (char *)data;
1091 *lenp = len;
1092
1093 return 0;
Marek Vasut771cb4d2023-02-27 20:56:31 +01001094
1095exit_image_format:
1096 puts("Wrong image format for \"source\" command\n");
1097 return -EPERM;
Simon Glass30f33332023-01-06 08:52:28 -06001098}