blob: 062c76badeccff3f4a871f13a23bf2eeed896b18 [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
110ulong env_get_bootm_low(void)
111{
112 char *s = env_get("bootm_low");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600113
Simon Glass41506ff2021-09-25 07:03:15 -0600114 if (s) {
115 ulong tmp = hextoul(s, NULL);
116 return tmp;
117 }
118
Tom Riniaa6e94d2022-11-16 13:10:37 -0500119#if defined(CFG_SYS_SDRAM_BASE)
120 return CFG_SYS_SDRAM_BASE;
Simon Glass41506ff2021-09-25 07:03:15 -0600121#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
122 return gd->bd->bi_dram[0].start;
123#else
124 return 0;
125#endif
126}
127
128phys_size_t env_get_bootm_size(void)
129{
130 phys_size_t tmp, size;
131 phys_addr_t start;
132 char *s = env_get("bootm_size");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600133
Simon Glass41506ff2021-09-25 07:03:15 -0600134 if (s) {
135 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
136 return tmp;
137 }
138
139 start = gd->ram_base;
140 size = gd->ram_size;
141
142 if (start + size > gd->ram_top)
143 size = gd->ram_top - start;
144
145 s = env_get("bootm_low");
146 if (s)
147 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
148 else
149 tmp = start;
150
151 return size - (tmp - start);
152}
153
154phys_size_t env_get_bootm_mapsize(void)
155{
156 phys_size_t tmp;
157 char *s = env_get("bootm_mapsize");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600158
Simon Glass41506ff2021-09-25 07:03:15 -0600159 if (s) {
160 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
161 return tmp;
162 }
163
Tom Rini65cc0e22022-11-16 13:10:41 -0500164#if defined(CFG_SYS_BOOTMAPSZ)
165 return CFG_SYS_BOOTMAPSZ;
Simon Glass41506ff2021-09-25 07:03:15 -0600166#else
167 return env_get_bootm_size();
168#endif
169}
170
171void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
172{
173 if (to == from)
174 return;
175
Simon Glass9c2e9122022-08-28 12:32:53 -0600176 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600177 if (to > from) {
Simon Glass9c2e9122022-08-28 12:32:53 -0600178 from += len;
179 to += len;
Simon Glass41506ff2021-09-25 07:03:15 -0600180 }
Simon Glass9c2e9122022-08-28 12:32:53 -0600181 while (len > 0) {
182 size_t tail = (len > chunksz) ? chunksz : len;
183
Stefan Roese29caf932022-09-02 14:10:46 +0200184 schedule();
Simon Glass9c2e9122022-08-28 12:32:53 -0600185 if (to > from) {
186 to -= tail;
187 from -= tail;
188 }
189 memmove(to, from, tail);
190 if (to < from) {
191 to += tail;
192 from += tail;
193 }
194 len -= tail;
Simon Glass41506ff2021-09-25 07:03:15 -0600195 }
Simon Glass9c2e9122022-08-28 12:32:53 -0600196 } else {
197 memmove(to, from, len);
Simon Glass41506ff2021-09-25 07:03:15 -0600198 }
Simon Glass41506ff2021-09-25 07:03:15 -0600199}
200
Simon Glass530cc472023-11-18 14:04:57 -0700201ulong genimg_get_kernel_addr_fit(const char *const img_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600202 const char **fit_uname_config,
203 const char **fit_uname_kernel)
Simon Glass41506ff2021-09-25 07:03:15 -0600204{
205 ulong kernel_addr;
206
207 /* find out kernel image address */
208 if (!img_addr) {
209 kernel_addr = image_load_addr;
210 debug("* kernel: default image load address = 0x%08lx\n",
211 image_load_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600212 } else if (CONFIG_IS_ENABLED(FIT) &&
213 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass41506ff2021-09-25 07:03:15 -0600214 fit_uname_config)) {
215 debug("* kernel: config '%s' from image at 0x%08lx\n",
216 *fit_uname_config, kernel_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600217 } else if (CONFIG_IS_ENABLED(FIT) &&
218 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
219 fit_uname_kernel)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600220 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
221 *fit_uname_kernel, kernel_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600222 } else {
223 kernel_addr = hextoul(img_addr, NULL);
224 debug("* kernel: cmdline image address = 0x%08lx\n",
225 kernel_addr);
226 }
227
228 return kernel_addr;
229}
230
231/**
232 * genimg_get_kernel_addr() is the simple version of
233 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
234 */
235ulong genimg_get_kernel_addr(char * const img_addr)
236{
237 const char *fit_uname_config = NULL;
238 const char *fit_uname_kernel = NULL;
239
240 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
241 &fit_uname_kernel);
242}
243
244/**
245 * genimg_get_format - get image format type
246 * @img_addr: image start address
247 *
248 * genimg_get_format() checks whether provided address points to a valid
249 * legacy or FIT image.
250 *
251 * New uImage format and FDT blob are based on a libfdt. FDT blob
252 * may be passed directly or embedded in a FIT image. In both situations
253 * genimg_get_format() must be able to dectect libfdt header.
254 *
255 * returns:
256 * image format type or IMAGE_FORMAT_INVALID if no image is present
257 */
258int genimg_get_format(const void *img_addr)
259{
Simon Glass1df654a2021-09-25 19:43:35 -0600260 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassf3543e62022-09-06 20:26:52 -0600261 const struct legacy_img_hdr *hdr;
Simon Glass41506ff2021-09-25 07:03:15 -0600262
Simon Glassf3543e62022-09-06 20:26:52 -0600263 hdr = (const struct legacy_img_hdr *)img_addr;
Simon Glass1df654a2021-09-25 19:43:35 -0600264 if (image_check_magic(hdr))
265 return IMAGE_FORMAT_LEGACY;
266 }
267 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
268 if (!fdt_check_header(img_addr))
269 return IMAGE_FORMAT_FIT;
270 }
271 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
Safae Ouajih734cb472023-02-06 00:50:05 +0100272 is_android_boot_image_header(img_addr))
Simon Glass41506ff2021-09-25 07:03:15 -0600273 return IMAGE_FORMAT_ANDROID;
Simon Glass41506ff2021-09-25 07:03:15 -0600274
275 return IMAGE_FORMAT_INVALID;
276}
277
278/**
279 * fit_has_config - check if there is a valid FIT configuration
280 * @images: pointer to the bootm command headers structure
281 *
282 * fit_has_config() checks if there is a FIT configuration in use
283 * (if FTI support is present).
284 *
285 * returns:
286 * 0, no FIT support or no configuration found
287 * 1, configuration found
288 */
Simon Glassd9d7c202022-09-06 20:26:50 -0600289int genimg_has_config(struct bootm_headers *images)
Simon Glass41506ff2021-09-25 07:03:15 -0600290{
Simon Glass1df654a2021-09-25 19:43:35 -0600291 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass41506ff2021-09-25 07:03:15 -0600292 return 1;
Simon Glass1df654a2021-09-25 19:43:35 -0600293
Simon Glass41506ff2021-09-25 07:03:15 -0600294 return 0;
295}
296
297/**
Simon Glasse4c92872021-09-25 19:43:37 -0600298 * select_ramdisk() - Select and locate the ramdisk to use
299 *
Simon Glass41506ff2021-09-25 07:03:15 -0600300 * @images: pointer to the bootm images structure
Simon Glass4f2d9412022-08-28 12:32:47 -0600301 * @select: name of ramdisk to select, or hex address, NULL for any
Simon Glass41506ff2021-09-25 07:03:15 -0600302 * @arch: expected ramdisk architecture
Simon Glasse4c92872021-09-25 19:43:37 -0600303 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
304 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100305 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glasse4c92872021-09-25 19:43:37 -0600306 * other -ve value on other error
Simon Glass41506ff2021-09-25 07:03:15 -0600307 */
Simon Glassd9d7c202022-09-06 20:26:50 -0600308static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
Simon Glasse4c92872021-09-25 19:43:37 -0600309 ulong *rd_datap, ulong *rd_lenp)
Simon Glass41506ff2021-09-25 07:03:15 -0600310{
Simon Glassf7659f62022-08-28 12:32:49 -0600311 const char *fit_uname_config;
312 const char *fit_uname_ramdisk;
Simon Glassca788832022-08-28 12:32:51 -0600313 bool done_select = !select;
Simon Glass4f2d9412022-08-28 12:32:47 -0600314 bool done = false;
Simon Glassf7659f62022-08-28 12:32:49 -0600315 int rd_noffset;
Tom Rini51a765b2023-04-05 19:48:56 -0400316 ulong rd_addr = 0;
Simon Glasse4c92872021-09-25 19:43:37 -0600317 char *buf;
Simon Glass78f88792021-09-25 19:43:36 -0600318
Simon Glassca788832022-08-28 12:32:51 -0600319 if (CONFIG_IS_ENABLED(FIT)) {
Simon Glassf7659f62022-08-28 12:32:49 -0600320 fit_uname_config = images->fit_uname_cfg;
321 fit_uname_ramdisk = NULL;
Simon Glass78f88792021-09-25 19:43:36 -0600322
Tom Rini621158d2021-12-20 09:36:32 -0500323 if (select) {
324 ulong default_addr;
Simon Glass41506ff2021-09-25 07:03:15 -0600325 /*
326 * If the init ramdisk comes from the FIT image and
327 * the FIT image address is omitted in the command
328 * line argument, try to use os FIT image address or
329 * default load address.
330 */
331 if (images->fit_uname_os)
332 default_addr = (ulong)images->fit_hdr_os;
333 else
334 default_addr = image_load_addr;
335
Simon Glass20f5d832022-08-28 12:32:52 -0600336 if (fit_parse_conf(select, default_addr, &rd_addr,
337 &fit_uname_config)) {
Simon Glass3d2a47f2021-09-25 07:03:16 -0600338 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
339 fit_uname_config, rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600340 done_select = true;
Simon Glass41506ff2021-09-25 07:03:15 -0600341 } else if (fit_parse_subimage(select, default_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600342 &rd_addr,
343 &fit_uname_ramdisk)) {
344 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
345 fit_uname_ramdisk, rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600346 done_select = true;
347 }
348 }
349 }
350 if (!done_select) {
Simon Glass20f5d832022-08-28 12:32:52 -0600351 rd_addr = hextoul(select, NULL);
352 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600353 }
Simon Glass20f5d832022-08-28 12:32:52 -0600354 if (CONFIG_IS_ENABLED(FIT) && !select) {
355 /* use FIT configuration provided in first bootm
356 * command argument. If the property is not defined,
357 * quit silently (with -ENOPKG)
Tom Rini621158d2021-12-20 09:36:32 -0500358 */
Simon Glass20f5d832022-08-28 12:32:52 -0600359 rd_addr = map_to_sysmem(images->fit_hdr_os);
360 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
361 rd_addr);
362 if (rd_noffset == -ENOENT)
363 return -ENOPKG;
364 else if (rd_noffset < 0)
365 return rd_noffset;
366 }
Simon Glass78f88792021-09-25 19:43:36 -0600367
Simon Glass20f5d832022-08-28 12:32:52 -0600368 /*
369 * Check if there is an initrd image at the
370 * address provided in the second bootm argument
371 * check image type, for FIT images get FIT node.
372 */
373 buf = map_sysmem(rd_addr, 0);
374 switch (genimg_get_format(buf)) {
375 case IMAGE_FORMAT_LEGACY:
376 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassf3543e62022-09-06 20:26:52 -0600377 const struct legacy_img_hdr *rd_hdr;
Simon Glass41506ff2021-09-25 07:03:15 -0600378
Simon Glass20f5d832022-08-28 12:32:52 -0600379 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
380 rd_addr);
Tom Rini621158d2021-12-20 09:36:32 -0500381
Simon Glass20f5d832022-08-28 12:32:52 -0600382 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
383 rd_hdr = image_get_ramdisk(rd_addr, arch,
384 images->verify);
Simon Glass41506ff2021-09-25 07:03:15 -0600385
Simon Glass20f5d832022-08-28 12:32:52 -0600386 if (!rd_hdr)
387 return -ENOENT;
Simon Glass41506ff2021-09-25 07:03:15 -0600388
Simon Glass20f5d832022-08-28 12:32:52 -0600389 *rd_datap = image_get_data(rd_hdr);
390 *rd_lenp = image_get_data_size(rd_hdr);
391 done = true;
Simon Glass4f2d9412022-08-28 12:32:47 -0600392 }
Simon Glass20f5d832022-08-28 12:32:52 -0600393 break;
394 case IMAGE_FORMAT_FIT:
395 if (CONFIG_IS_ENABLED(FIT)) {
396 rd_noffset = fit_image_load(images, rd_addr,
397 &fit_uname_ramdisk,
398 &fit_uname_config,
399 arch, IH_TYPE_RAMDISK,
400 BOOTSTAGE_ID_FIT_RD_START,
401 FIT_LOAD_OPTIONAL_NON_ZERO,
402 rd_datap, rd_lenp);
403 if (rd_noffset < 0)
404 return rd_noffset;
405
406 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
407 images->fit_uname_rd = fit_uname_ramdisk;
408 images->fit_noffset_rd = rd_noffset;
409 done = true;
410 }
411 break;
412 case IMAGE_FORMAT_ANDROID:
413 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass20f5d832022-08-28 12:32:52 -0600414 int ret;
Safae Ouajih636da202023-02-06 00:50:17 +0100415 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
416 void *boot_img = map_sysmem(get_abootimg_addr(), 0);
417 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
418
419 ret = android_image_get_ramdisk(boot_img, vendor_boot_img,
420 rd_datap, rd_lenp);
421 unmap_sysmem(vendor_boot_img);
422 unmap_sysmem(boot_img);
423 } else {
424 void *ptr = map_sysmem(images->os.start, 0);
425
426 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp);
427 unmap_sysmem(ptr);
428 }
429
Simon Glass20f5d832022-08-28 12:32:52 -0600430 if (ret)
431 return ret;
432 done = true;
433 }
434 break;
435 }
Simon Glass1df654a2021-09-25 19:43:35 -0600436
Simon Glass4f2d9412022-08-28 12:32:47 -0600437 if (!done) {
438 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
439 char *end = NULL;
440
441 if (select)
442 end = strchr(select, ':');
443 if (end) {
444 *rd_lenp = hextoul(++end, NULL);
445 *rd_datap = rd_addr;
446 done = true;
Simon Glass41506ff2021-09-25 07:03:15 -0600447 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600448 }
449
450 if (!done) {
Simon Glass1df654a2021-09-25 19:43:35 -0600451 puts("Wrong Ramdisk Image Format\n");
Simon Glasse4c92872021-09-25 19:43:37 -0600452 return -EINVAL;
Simon Glass41506ff2021-09-25 07:03:15 -0600453 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600454 }
Simon Glasse4c92872021-09-25 19:43:37 -0600455
456 return 0;
457}
458
459/**
460 * boot_get_ramdisk - main ramdisk handling routine
461 * @argc: command argument count
462 * @argv: command argument list
463 * @images: pointer to the bootm images structure
464 * @arch: expected ramdisk architecture
465 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
466 * @rd_end: pointer to a ulong variable, will hold ramdisk end
467 *
468 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
469 * Currently supported are the following ramdisk sources:
470 * - multicomponent kernel/ramdisk image,
471 * - commandline provided address of decicated ramdisk image.
472 *
473 * returns:
474 * 0, if ramdisk image was found and valid, or skiped
475 * rd_start and rd_end are set to ramdisk start/end addresses if
476 * ramdisk image is found and valid
477 *
478 * 1, if ramdisk image is found but corrupted, or invalid
479 * rd_start and rd_end are set to 0 if no ramdisk exists
480 */
Simon Glassd9d7c202022-09-06 20:26:50 -0600481int boot_get_ramdisk(int argc, char *const argv[], struct bootm_headers *images,
Simon Glasse4c92872021-09-25 19:43:37 -0600482 u8 arch, ulong *rd_start, ulong *rd_end)
483{
484 ulong rd_data, rd_len;
485 const char *select = NULL;
486
487 *rd_start = 0;
488 *rd_end = 0;
489
490 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
491 char *buf;
492
493 /* Look for an Android boot image */
494 buf = map_sysmem(images->os.start, 0);
495 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
496 select = (argc == 0) ? env_get("loadaddr") : argv[0];
497 }
498
499 if (argc >= 2)
500 select = argv[1];
501
502 /*
503 * Look for a '-' which indicates to ignore the
504 * ramdisk argument
505 */
506 if (select && strcmp(select, "-") == 0) {
507 debug("## Skipping init Ramdisk\n");
508 rd_len = 0;
509 rd_data = 0;
510 } else if (select || genimg_has_config(images)) {
511 int ret;
512
513 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
514 if (ret == -ENOPKG)
515 return 0;
516 else if (ret)
517 return ret;
Simon Glass41506ff2021-09-25 07:03:15 -0600518 } else if (images->legacy_hdr_valid &&
519 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600520 IH_TYPE_MULTI)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600521 /*
522 * Now check if we have a legacy mult-component image,
523 * get second entry data start address and len.
524 */
525 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600526 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
527 (ulong)images->legacy_hdr_os);
Simon Glass41506ff2021-09-25 07:03:15 -0600528
529 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
530 } else {
531 /*
532 * no initrd image
533 */
534 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600535 rd_len = 0;
536 rd_data = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600537 }
538
539 if (!rd_data) {
540 debug("## No init Ramdisk\n");
541 } else {
542 *rd_start = rd_data;
543 *rd_end = rd_data + rd_len;
544 }
545 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600546 *rd_start, *rd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600547
548 return 0;
549}
550
Simon Glass41506ff2021-09-25 07:03:15 -0600551/**
552 * boot_ramdisk_high - relocate init ramdisk
553 * @lmb: pointer to lmb handle, will be used for memory mgmt
554 * @rd_data: ramdisk data start address
555 * @rd_len: ramdisk data length
556 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
557 * start address (after possible relocation)
558 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
559 * end address (after possible relocation)
560 *
561 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
562 * variable and if requested ramdisk data is moved to a specified location.
563 *
564 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
565 * start/end addresses if ramdisk image start and len were provided,
566 * otherwise set initrd_start and initrd_end set to zeros.
567 *
568 * returns:
569 * 0 - success
570 * -1 - failure
571 */
572int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600573 ulong *initrd_start, ulong *initrd_end)
Simon Glass41506ff2021-09-25 07:03:15 -0600574{
575 char *s;
576 ulong initrd_high;
577 int initrd_copy_to_ram = 1;
578
579 s = env_get("initrd_high");
580 if (s) {
581 /* a value of "no" or a similar string will act like 0,
582 * turning the "load high" feature off. This is intentional.
583 */
584 initrd_high = hextoul(s, NULL);
585 if (initrd_high == ~0)
586 initrd_copy_to_ram = 0;
587 } else {
588 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
589 }
590
Simon Glass41506ff2021-09-25 07:03:15 -0600591 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600592 initrd_high, initrd_copy_to_ram);
Simon Glass41506ff2021-09-25 07:03:15 -0600593
594 if (rd_data) {
595 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
596 debug(" in-place initrd\n");
597 *initrd_start = rd_data;
598 *initrd_end = rd_data + rd_len;
599 lmb_reserve(lmb, rd_data, rd_len);
600 } else {
601 if (initrd_high)
602 *initrd_start = (ulong)lmb_alloc_base(lmb,
603 rd_len, 0x1000, initrd_high);
604 else
605 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
606 0x1000);
607
608 if (*initrd_start == 0) {
609 puts("ramdisk - allocation error\n");
610 goto error;
611 }
612 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
613
614 *initrd_end = *initrd_start + rd_len;
615 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600616 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600617
618 memmove_wd((void *)*initrd_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600619 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass41506ff2021-09-25 07:03:15 -0600620
Simon Glass41506ff2021-09-25 07:03:15 -0600621 /*
622 * Ensure the image is flushed to memory to handle
623 * AMP boot scenarios in which we might not be
624 * HW cache coherent
625 */
Simon Glass1df654a2021-09-25 19:43:35 -0600626 if (IS_ENABLED(CONFIG_MP)) {
627 flush_cache((unsigned long)*initrd_start,
628 ALIGN(rd_len, ARCH_DMA_MINALIGN));
629 }
Simon Glass41506ff2021-09-25 07:03:15 -0600630 puts("OK\n");
631 }
632 } else {
633 *initrd_start = 0;
634 *initrd_end = 0;
635 }
636 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600637 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600638
639 return 0;
640
641error:
642 return -1;
643}
Simon Glass41506ff2021-09-25 07:03:15 -0600644
Simon Glassd9d7c202022-09-06 20:26:50 -0600645int boot_get_setup(struct bootm_headers *images, u8 arch,
Simon Glass41506ff2021-09-25 07:03:15 -0600646 ulong *setup_start, ulong *setup_len)
647{
Simon Glass1df654a2021-09-25 19:43:35 -0600648 if (!CONFIG_IS_ENABLED(FIT))
649 return -ENOENT;
650
Simon Glass41506ff2021-09-25 07:03:15 -0600651 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600652}
653
Simon Glassd9d7c202022-09-06 20:26:50 -0600654int boot_get_fpga(int argc, char *const argv[], struct bootm_headers *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600655 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600656{
657 ulong tmp_img_addr, img_data, img_len;
658 void *buf;
659 int conf_noffset;
660 int fit_img_result;
661 const char *uname, *name;
662 int err;
663 int devnum = 0; /* TODO support multi fpga platforms */
664
Simon Glass1df654a2021-09-25 19:43:35 -0600665 if (!IS_ENABLED(CONFIG_FPGA))
666 return -ENOSYS;
667
Simon Glass41506ff2021-09-25 07:03:15 -0600668 /* Check to see if the images struct has a FIT configuration */
669 if (!genimg_has_config(images)) {
670 debug("## FIT configuration was not specified\n");
671 return 0;
672 }
673
674 /*
675 * Obtain the os FIT header from the images struct
676 */
677 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
678 buf = map_sysmem(tmp_img_addr, 0);
679 /*
680 * Check image type. For FIT images get FIT node
681 * and attempt to locate a generic binary.
682 */
683 switch (genimg_get_format(buf)) {
684 case IMAGE_FORMAT_FIT:
685 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
686
687 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
688 NULL);
689 if (!uname) {
690 debug("## FPGA image is not specified\n");
691 return 0;
692 }
693 fit_img_result = fit_image_load(images,
694 tmp_img_addr,
695 (const char **)&uname,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600696 &images->fit_uname_cfg,
Simon Glass41506ff2021-09-25 07:03:15 -0600697 arch,
698 IH_TYPE_FPGA,
699 BOOTSTAGE_ID_FPGA_INIT,
700 FIT_LOAD_OPTIONAL_NON_ZERO,
701 &img_data, &img_len);
702
703 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
704 uname, img_data, img_len);
705
706 if (fit_img_result < 0) {
707 /* Something went wrong! */
708 return fit_img_result;
709 }
710
711 if (!fpga_is_partial_data(devnum, img_len)) {
712 name = "full";
713 err = fpga_loadbitstream(devnum, (char *)img_data,
714 img_len, BIT_FULL);
715 if (err)
716 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300717 img_len, BIT_FULL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600718 } else {
719 name = "partial";
720 err = fpga_loadbitstream(devnum, (char *)img_data,
721 img_len, BIT_PARTIAL);
722 if (err)
723 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300724 img_len, BIT_PARTIAL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600725 }
726
727 if (err)
728 return err;
729
730 printf(" Programming %s bitstream... OK\n", name);
731 break;
732 default:
733 printf("The given image format is not supported (corrupt?)\n");
734 return 1;
735 }
736
737 return 0;
738}
Simon Glass41506ff2021-09-25 07:03:15 -0600739
Simon Glass3d2a47f2021-09-25 07:03:16 -0600740static void fit_loadable_process(u8 img_type,
Simon Glass41506ff2021-09-25 07:03:15 -0600741 ulong img_data,
742 ulong img_len)
743{
744 int i;
745 const unsigned int count =
746 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
747 struct fit_loadable_tbl *fit_loadable_handler =
748 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
749 /* For each loadable handler */
750 for (i = 0; i < count; i++, fit_loadable_handler++)
751 /* matching this type */
752 if (fit_loadable_handler->type == img_type)
753 /* call that handler with this image data */
754 fit_loadable_handler->handler(img_data, img_len);
755}
756
Simon Glassd9d7c202022-09-06 20:26:50 -0600757int boot_get_loadable(int argc, char *const argv[], struct bootm_headers *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600758 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600759{
760 /*
761 * These variables are used to hold the current image location
762 * in system memory.
763 */
764 ulong tmp_img_addr;
765 /*
766 * These two variables are requirements for fit_image_load, but
767 * their values are not used
768 */
769 ulong img_data, img_len;
770 void *buf;
771 int loadables_index;
772 int conf_noffset;
773 int fit_img_result;
774 const char *uname;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600775 u8 img_type;
Simon Glass41506ff2021-09-25 07:03:15 -0600776
777 /* Check to see if the images struct has a FIT configuration */
778 if (!genimg_has_config(images)) {
779 debug("## FIT configuration was not specified\n");
780 return 0;
781 }
782
783 /*
784 * Obtain the os FIT header from the images struct
785 */
786 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
787 buf = map_sysmem(tmp_img_addr, 0);
788 /*
789 * Check image type. For FIT images get FIT node
790 * and attempt to locate a generic binary.
791 */
792 switch (genimg_get_format(buf)) {
793 case IMAGE_FORMAT_FIT:
794 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
795
796 for (loadables_index = 0;
797 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600798 FIT_LOADABLE_PROP,
799 loadables_index, NULL), uname;
800 loadables_index++) {
801 fit_img_result = fit_image_load(images, tmp_img_addr,
802 &uname,
803 &images->fit_uname_cfg,
804 arch, IH_TYPE_LOADABLE,
805 BOOTSTAGE_ID_FIT_LOADABLE_START,
806 FIT_LOAD_OPTIONAL_NON_ZERO,
807 &img_data, &img_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600808 if (fit_img_result < 0) {
809 /* Something went wrong! */
810 return fit_img_result;
811 }
812
813 fit_img_result = fit_image_get_node(buf, uname);
814 if (fit_img_result < 0) {
815 /* Something went wrong! */
816 return fit_img_result;
817 }
818 fit_img_result = fit_image_get_type(buf,
819 fit_img_result,
820 &img_type);
821 if (fit_img_result < 0) {
822 /* Something went wrong! */
823 return fit_img_result;
824 }
825
826 fit_loadable_process(img_type, img_data, img_len);
827 }
828 break;
829 default:
830 printf("The given image format is not supported (corrupt?)\n");
831 return 1;
832 }
833
834 return 0;
835}
Simon Glass41506ff2021-09-25 07:03:15 -0600836
Simon Glass41506ff2021-09-25 07:03:15 -0600837/**
838 * boot_get_cmdline - allocate and initialize kernel cmdline
839 * @lmb: pointer to lmb handle, will be used for memory mgmt
840 * @cmd_start: pointer to a ulong variable, will hold cmdline start
841 * @cmd_end: pointer to a ulong variable, will hold cmdline end
842 *
Simon Glass9c2e9122022-08-28 12:32:53 -0600843 * This allocates space for kernel command line below
Simon Glass41506ff2021-09-25 07:03:15 -0600844 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
845 * variable is present its contents is copied to allocated kernel
846 * command line.
847 *
848 * returns:
849 * 0 - success
850 * -1 - failure
851 */
852int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
853{
Simon Glass9c2e9122022-08-28 12:32:53 -0600854 int barg;
Simon Glass41506ff2021-09-25 07:03:15 -0600855 char *cmdline;
856 char *s;
857
Simon Glass9c2e9122022-08-28 12:32:53 -0600858 /*
859 * Help the compiler detect that this function is only called when
860 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
861 */
862 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
863 return 0;
864
865 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
866 cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
Simon Glass41506ff2021-09-25 07:03:15 -0600867 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass3d2a47f2021-09-25 07:03:16 -0600868 if (!cmdline)
Simon Glass41506ff2021-09-25 07:03:15 -0600869 return -1;
870
871 s = env_get("bootargs");
872 if (!s)
873 s = "";
874
875 strcpy(cmdline, s);
876
Simon Glass3d2a47f2021-09-25 07:03:16 -0600877 *cmd_start = (ulong)cmdline;
Simon Glass41506ff2021-09-25 07:03:15 -0600878 *cmd_end = *cmd_start + strlen(cmdline);
879
880 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
881
882 return 0;
883}
Simon Glass41506ff2021-09-25 07:03:15 -0600884
Simon Glass41506ff2021-09-25 07:03:15 -0600885/**
886 * boot_get_kbd - allocate and initialize kernel copy of board info
887 * @lmb: pointer to lmb handle, will be used for memory mgmt
888 * @kbd: double pointer to board info data
889 *
890 * boot_get_kbd() allocates space for kernel copy of board info data below
891 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
892 * with the current u-boot board info data.
893 *
894 * returns:
895 * 0 - success
896 * -1 - failure
897 */
898int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
899{
900 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
901 sizeof(struct bd_info),
902 0xf,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600903 env_get_bootm_mapsize() +
904 env_get_bootm_low());
905 if (!*kbd)
Simon Glass41506ff2021-09-25 07:03:15 -0600906 return -1;
907
Simon Glass3d2a47f2021-09-25 07:03:16 -0600908 **kbd = *gd->bd;
Simon Glass41506ff2021-09-25 07:03:15 -0600909
910 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
911
Simon Glass9c2e9122022-08-28 12:32:53 -0600912 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
Simon Glass4ed37ab2021-09-25 07:03:20 -0600913 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass41506ff2021-09-25 07:03:15 -0600914
915 return 0;
916}
Simon Glass41506ff2021-09-25 07:03:15 -0600917
Simon Glassd9d7c202022-09-06 20:26:50 -0600918int image_setup_linux(struct bootm_headers *images)
Simon Glass41506ff2021-09-25 07:03:15 -0600919{
920 ulong of_size = images->ft_len;
921 char **of_flat_tree = &images->ft_addr;
Simon Glass9c2e9122022-08-28 12:32:53 -0600922 struct lmb *lmb = images_lmb(images);
Simon Glass41506ff2021-09-25 07:03:15 -0600923 int ret;
924
Simon Glass9c2e9122022-08-28 12:32:53 -0600925 /* This function cannot be called without lmb support */
Simon Glass210af542023-02-05 15:40:13 -0700926 if (!IS_ENABLED(CONFIG_LMB))
Simon Glass9c2e9122022-08-28 12:32:53 -0600927 return -EFAULT;
Simon Glass0c303f92021-09-25 19:43:21 -0600928 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass41506ff2021-09-25 07:03:15 -0600929 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
930
Simon Glass806d1ff2021-09-25 19:43:25 -0600931 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600932 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600933 &images->cmdline_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600934 if (ret) {
935 puts("ERROR with allocation of cmdline\n");
936 return ret;
937 }
938 }
939
Simon Glass0c303f92021-09-25 19:43:21 -0600940 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600941 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
942 if (ret)
943 return ret;
944 }
945
Simon Glass0c303f92021-09-25 19:43:21 -0600946 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass41506ff2021-09-25 07:03:15 -0600947 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
948 if (ret)
949 return ret;
950 }
951
952 return 0;
953}
Simon Glass5d3248a2021-09-25 07:03:17 -0600954
955void genimg_print_size(uint32_t size)
956{
957 printf("%d Bytes = ", size);
958 print_size(size, "\n");
959}
960
961void genimg_print_time(time_t timestamp)
962{
963 struct rtc_time tm;
964
965 rtc_to_tm(timestamp, &tm);
966 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
967 tm.tm_year, tm.tm_mon, tm.tm_mday,
968 tm.tm_hour, tm.tm_min, tm.tm_sec);
969}
Simon Glass30f33332023-01-06 08:52:28 -0600970
971/**
972 * get_default_image() - Return default property from /images
973 *
974 * Return: Pointer to value of default property (or NULL)
975 */
976static const char *get_default_image(const void *fit)
977{
978 int images_noffset;
979
980 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
981 if (images_noffset < 0)
982 return NULL;
983
984 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
985}
986
987int image_locate_script(void *buf, int size, const char *fit_uname,
988 const char *confname, char **datap, uint *lenp)
989{
990 const struct legacy_img_hdr *hdr;
991 const void *fit_data;
992 const void *fit_hdr;
993 size_t fit_len;
994 int noffset;
995 int verify;
996 ulong len;
997 u32 *data;
998
999 verify = env_get_yesno("verify");
1000
1001 switch (genimg_get_format(buf)) {
1002 case IMAGE_FORMAT_LEGACY:
Marek Vasut771cb4d2023-02-27 20:56:31 +01001003 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
1004 goto exit_image_format;
1005 } else {
Simon Glass30f33332023-01-06 08:52:28 -06001006 hdr = buf;
1007
1008 if (!image_check_magic(hdr)) {
1009 puts("Bad magic number\n");
1010 return 1;
1011 }
1012
1013 if (!image_check_hcrc(hdr)) {
1014 puts("Bad header crc\n");
1015 return 1;
1016 }
1017
1018 if (verify) {
1019 if (!image_check_dcrc(hdr)) {
1020 puts("Bad data crc\n");
1021 return 1;
1022 }
1023 }
1024
1025 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
1026 puts("Bad image type\n");
1027 return 1;
1028 }
1029
1030 /* get length of script */
1031 data = (u32 *)image_get_data(hdr);
1032
1033 len = uimage_to_cpu(*data);
1034 if (!len) {
1035 puts("Empty Script\n");
1036 return 1;
1037 }
1038
1039 /*
1040 * scripts are just multi-image files with one
1041 * component, so seek past the zero-terminated sequence
1042 * of image lengths to get to the actual image data
1043 */
1044 while (*data++);
1045 }
1046 break;
1047 case IMAGE_FORMAT_FIT:
Marek Vasut771cb4d2023-02-27 20:56:31 +01001048 if (!IS_ENABLED(CONFIG_FIT)) {
1049 goto exit_image_format;
1050 } else {
Simon Glass30f33332023-01-06 08:52:28 -06001051 fit_hdr = buf;
1052 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1053 puts("Bad FIT image format\n");
1054 return 1;
1055 }
1056
1057 if (!fit_uname) {
1058 /* If confname is empty, use the default */
1059 if (confname && *confname)
1060 noffset = fit_conf_get_node(fit_hdr, confname);
1061 else
1062 noffset = fit_conf_get_node(fit_hdr, NULL);
1063 if (noffset < 0) {
1064 if (!confname)
1065 goto fallback;
1066 printf("Could not find config %s\n", confname);
1067 return 1;
1068 }
1069
1070 if (verify && fit_config_verify(fit_hdr, noffset))
1071 return 1;
1072
1073 noffset = fit_conf_get_prop_node(fit_hdr,
1074 noffset,
1075 FIT_SCRIPT_PROP,
1076 IH_PHASE_NONE);
1077 if (noffset < 0) {
1078 if (!confname)
1079 goto fallback;
1080 printf("Could not find script in %s\n", confname);
1081 return 1;
1082 }
1083 } else {
1084fallback:
1085 if (!fit_uname || !*fit_uname)
1086 fit_uname = get_default_image(fit_hdr);
1087 if (!fit_uname) {
1088 puts("No FIT subimage unit name\n");
1089 return 1;
1090 }
1091
1092 /* get script component image node offset */
1093 noffset = fit_image_get_node(fit_hdr, fit_uname);
1094 if (noffset < 0) {
1095 printf("Can't find '%s' FIT subimage\n",
1096 fit_uname);
1097 return 1;
1098 }
1099 }
1100
1101 if (!fit_image_check_type(fit_hdr, noffset,
1102 IH_TYPE_SCRIPT)) {
1103 puts("Not a image image\n");
1104 return 1;
1105 }
1106
1107 /* verify integrity */
1108 if (verify && !fit_image_verify(fit_hdr, noffset)) {
1109 puts("Bad Data Hash\n");
1110 return 1;
1111 }
1112
1113 /* get script subimage data address and length */
Tobias Waldekranze45bba52023-02-16 16:33:47 +01001114 if (fit_image_get_data_and_size(fit_hdr, noffset,
1115 &fit_data, &fit_len)) {
Simon Glass30f33332023-01-06 08:52:28 -06001116 puts("Could not find script subimage data\n");
1117 return 1;
1118 }
1119
1120 data = (u32 *)fit_data;
1121 len = (ulong)fit_len;
1122 }
1123 break;
1124 default:
Marek Vasut771cb4d2023-02-27 20:56:31 +01001125 goto exit_image_format;
Simon Glass30f33332023-01-06 08:52:28 -06001126 }
1127
1128 *datap = (char *)data;
1129 *lenp = len;
1130
1131 return 0;
Marek Vasut771cb4d2023-02-27 20:56:31 +01001132
1133exit_image_format:
1134 puts("Wrong image format for \"source\" command\n");
1135 return -EPERM;
Simon Glass30f33332023-01-06 08:52:28 -06001136}