blob: d500da1b4b918288a1f3f006028ddd4ef8717f6d [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
201/**
202 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
203 * FIT strings
204 * @img_addr: a string might contain real image address
205 * @fit_uname_config: double pointer to a char, will hold pointer to a
206 * configuration unit name
207 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
208 * name
209 *
210 * genimg_get_kernel_addr_fit get the real kernel start address from a string
211 * which is normally the first argv of bootm/bootz
212 *
213 * returns:
214 * kernel start address
215 */
216ulong genimg_get_kernel_addr_fit(char * const img_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600217 const char **fit_uname_config,
218 const char **fit_uname_kernel)
Simon Glass41506ff2021-09-25 07:03:15 -0600219{
220 ulong kernel_addr;
221
222 /* find out kernel image address */
223 if (!img_addr) {
224 kernel_addr = image_load_addr;
225 debug("* kernel: default image load address = 0x%08lx\n",
226 image_load_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600227 } else if (CONFIG_IS_ENABLED(FIT) &&
228 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass41506ff2021-09-25 07:03:15 -0600229 fit_uname_config)) {
230 debug("* kernel: config '%s' from image at 0x%08lx\n",
231 *fit_uname_config, kernel_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600232 } else if (CONFIG_IS_ENABLED(FIT) &&
233 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
234 fit_uname_kernel)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600235 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
236 *fit_uname_kernel, kernel_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600237 } else {
238 kernel_addr = hextoul(img_addr, NULL);
239 debug("* kernel: cmdline image address = 0x%08lx\n",
240 kernel_addr);
241 }
242
243 return kernel_addr;
244}
245
246/**
247 * genimg_get_kernel_addr() is the simple version of
248 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
249 */
250ulong genimg_get_kernel_addr(char * const img_addr)
251{
252 const char *fit_uname_config = NULL;
253 const char *fit_uname_kernel = NULL;
254
255 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
256 &fit_uname_kernel);
257}
258
259/**
260 * genimg_get_format - get image format type
261 * @img_addr: image start address
262 *
263 * genimg_get_format() checks whether provided address points to a valid
264 * legacy or FIT image.
265 *
266 * New uImage format and FDT blob are based on a libfdt. FDT blob
267 * may be passed directly or embedded in a FIT image. In both situations
268 * genimg_get_format() must be able to dectect libfdt header.
269 *
270 * returns:
271 * image format type or IMAGE_FORMAT_INVALID if no image is present
272 */
273int genimg_get_format(const void *img_addr)
274{
Simon Glass1df654a2021-09-25 19:43:35 -0600275 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassf3543e62022-09-06 20:26:52 -0600276 const struct legacy_img_hdr *hdr;
Simon Glass41506ff2021-09-25 07:03:15 -0600277
Simon Glassf3543e62022-09-06 20:26:52 -0600278 hdr = (const struct legacy_img_hdr *)img_addr;
Simon Glass1df654a2021-09-25 19:43:35 -0600279 if (image_check_magic(hdr))
280 return IMAGE_FORMAT_LEGACY;
281 }
282 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
283 if (!fdt_check_header(img_addr))
284 return IMAGE_FORMAT_FIT;
285 }
286 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
Safae Ouajih734cb472023-02-06 00:50:05 +0100287 is_android_boot_image_header(img_addr))
Simon Glass41506ff2021-09-25 07:03:15 -0600288 return IMAGE_FORMAT_ANDROID;
Simon Glass41506ff2021-09-25 07:03:15 -0600289
290 return IMAGE_FORMAT_INVALID;
291}
292
293/**
294 * fit_has_config - check if there is a valid FIT configuration
295 * @images: pointer to the bootm command headers structure
296 *
297 * fit_has_config() checks if there is a FIT configuration in use
298 * (if FTI support is present).
299 *
300 * returns:
301 * 0, no FIT support or no configuration found
302 * 1, configuration found
303 */
Simon Glassd9d7c202022-09-06 20:26:50 -0600304int genimg_has_config(struct bootm_headers *images)
Simon Glass41506ff2021-09-25 07:03:15 -0600305{
Simon Glass1df654a2021-09-25 19:43:35 -0600306 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass41506ff2021-09-25 07:03:15 -0600307 return 1;
Simon Glass1df654a2021-09-25 19:43:35 -0600308
Simon Glass41506ff2021-09-25 07:03:15 -0600309 return 0;
310}
311
312/**
Simon Glasse4c92872021-09-25 19:43:37 -0600313 * select_ramdisk() - Select and locate the ramdisk to use
314 *
Simon Glass41506ff2021-09-25 07:03:15 -0600315 * @images: pointer to the bootm images structure
Simon Glass4f2d9412022-08-28 12:32:47 -0600316 * @select: name of ramdisk to select, or hex address, NULL for any
Simon Glass41506ff2021-09-25 07:03:15 -0600317 * @arch: expected ramdisk architecture
Simon Glasse4c92872021-09-25 19:43:37 -0600318 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
319 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100320 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glasse4c92872021-09-25 19:43:37 -0600321 * other -ve value on other error
Simon Glass41506ff2021-09-25 07:03:15 -0600322 */
Simon Glassd9d7c202022-09-06 20:26:50 -0600323static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
Simon Glasse4c92872021-09-25 19:43:37 -0600324 ulong *rd_datap, ulong *rd_lenp)
Simon Glass41506ff2021-09-25 07:03:15 -0600325{
Simon Glassf7659f62022-08-28 12:32:49 -0600326 const char *fit_uname_config;
327 const char *fit_uname_ramdisk;
Simon Glassca788832022-08-28 12:32:51 -0600328 bool done_select = !select;
Simon Glass4f2d9412022-08-28 12:32:47 -0600329 bool done = false;
Simon Glassf7659f62022-08-28 12:32:49 -0600330 int rd_noffset;
Tom Rini51a765b2023-04-05 19:48:56 -0400331 ulong rd_addr = 0;
Simon Glasse4c92872021-09-25 19:43:37 -0600332 char *buf;
Simon Glass78f88792021-09-25 19:43:36 -0600333
Simon Glassca788832022-08-28 12:32:51 -0600334 if (CONFIG_IS_ENABLED(FIT)) {
Simon Glassf7659f62022-08-28 12:32:49 -0600335 fit_uname_config = images->fit_uname_cfg;
336 fit_uname_ramdisk = NULL;
Simon Glass78f88792021-09-25 19:43:36 -0600337
Tom Rini621158d2021-12-20 09:36:32 -0500338 if (select) {
339 ulong default_addr;
Simon Glass41506ff2021-09-25 07:03:15 -0600340 /*
341 * If the init ramdisk comes from the FIT image and
342 * the FIT image address is omitted in the command
343 * line argument, try to use os FIT image address or
344 * default load address.
345 */
346 if (images->fit_uname_os)
347 default_addr = (ulong)images->fit_hdr_os;
348 else
349 default_addr = image_load_addr;
350
Simon Glass20f5d832022-08-28 12:32:52 -0600351 if (fit_parse_conf(select, default_addr, &rd_addr,
352 &fit_uname_config)) {
Simon Glass3d2a47f2021-09-25 07:03:16 -0600353 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
354 fit_uname_config, rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600355 done_select = true;
Simon Glass41506ff2021-09-25 07:03:15 -0600356 } else if (fit_parse_subimage(select, default_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600357 &rd_addr,
358 &fit_uname_ramdisk)) {
359 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
360 fit_uname_ramdisk, rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600361 done_select = true;
362 }
363 }
364 }
365 if (!done_select) {
Simon Glass20f5d832022-08-28 12:32:52 -0600366 rd_addr = hextoul(select, NULL);
367 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600368 }
Simon Glass20f5d832022-08-28 12:32:52 -0600369 if (CONFIG_IS_ENABLED(FIT) && !select) {
370 /* use FIT configuration provided in first bootm
371 * command argument. If the property is not defined,
372 * quit silently (with -ENOPKG)
Tom Rini621158d2021-12-20 09:36:32 -0500373 */
Simon Glass20f5d832022-08-28 12:32:52 -0600374 rd_addr = map_to_sysmem(images->fit_hdr_os);
375 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
376 rd_addr);
377 if (rd_noffset == -ENOENT)
378 return -ENOPKG;
379 else if (rd_noffset < 0)
380 return rd_noffset;
381 }
Simon Glass78f88792021-09-25 19:43:36 -0600382
Simon Glass20f5d832022-08-28 12:32:52 -0600383 /*
384 * Check if there is an initrd image at the
385 * address provided in the second bootm argument
386 * check image type, for FIT images get FIT node.
387 */
388 buf = map_sysmem(rd_addr, 0);
389 switch (genimg_get_format(buf)) {
390 case IMAGE_FORMAT_LEGACY:
391 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassf3543e62022-09-06 20:26:52 -0600392 const struct legacy_img_hdr *rd_hdr;
Simon Glass41506ff2021-09-25 07:03:15 -0600393
Simon Glass20f5d832022-08-28 12:32:52 -0600394 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
395 rd_addr);
Tom Rini621158d2021-12-20 09:36:32 -0500396
Simon Glass20f5d832022-08-28 12:32:52 -0600397 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
398 rd_hdr = image_get_ramdisk(rd_addr, arch,
399 images->verify);
Simon Glass41506ff2021-09-25 07:03:15 -0600400
Simon Glass20f5d832022-08-28 12:32:52 -0600401 if (!rd_hdr)
402 return -ENOENT;
Simon Glass41506ff2021-09-25 07:03:15 -0600403
Simon Glass20f5d832022-08-28 12:32:52 -0600404 *rd_datap = image_get_data(rd_hdr);
405 *rd_lenp = image_get_data_size(rd_hdr);
406 done = true;
Simon Glass4f2d9412022-08-28 12:32:47 -0600407 }
Simon Glass20f5d832022-08-28 12:32:52 -0600408 break;
409 case IMAGE_FORMAT_FIT:
410 if (CONFIG_IS_ENABLED(FIT)) {
411 rd_noffset = fit_image_load(images, rd_addr,
412 &fit_uname_ramdisk,
413 &fit_uname_config,
414 arch, IH_TYPE_RAMDISK,
415 BOOTSTAGE_ID_FIT_RD_START,
416 FIT_LOAD_OPTIONAL_NON_ZERO,
417 rd_datap, rd_lenp);
418 if (rd_noffset < 0)
419 return rd_noffset;
420
421 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
422 images->fit_uname_rd = fit_uname_ramdisk;
423 images->fit_noffset_rd = rd_noffset;
424 done = true;
425 }
426 break;
427 case IMAGE_FORMAT_ANDROID:
428 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass20f5d832022-08-28 12:32:52 -0600429 int ret;
Safae Ouajih636da202023-02-06 00:50:17 +0100430 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
431 void *boot_img = map_sysmem(get_abootimg_addr(), 0);
432 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
433
434 ret = android_image_get_ramdisk(boot_img, vendor_boot_img,
435 rd_datap, rd_lenp);
436 unmap_sysmem(vendor_boot_img);
437 unmap_sysmem(boot_img);
438 } else {
439 void *ptr = map_sysmem(images->os.start, 0);
440
441 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp);
442 unmap_sysmem(ptr);
443 }
444
Simon Glass20f5d832022-08-28 12:32:52 -0600445 if (ret)
446 return ret;
447 done = true;
448 }
449 break;
450 }
Simon Glass1df654a2021-09-25 19:43:35 -0600451
Simon Glass4f2d9412022-08-28 12:32:47 -0600452 if (!done) {
453 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
454 char *end = NULL;
455
456 if (select)
457 end = strchr(select, ':');
458 if (end) {
459 *rd_lenp = hextoul(++end, NULL);
460 *rd_datap = rd_addr;
461 done = true;
Simon Glass41506ff2021-09-25 07:03:15 -0600462 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600463 }
464
465 if (!done) {
Simon Glass1df654a2021-09-25 19:43:35 -0600466 puts("Wrong Ramdisk Image Format\n");
Simon Glasse4c92872021-09-25 19:43:37 -0600467 return -EINVAL;
Simon Glass41506ff2021-09-25 07:03:15 -0600468 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600469 }
Simon Glasse4c92872021-09-25 19:43:37 -0600470
471 return 0;
472}
473
474/**
475 * boot_get_ramdisk - main ramdisk handling routine
476 * @argc: command argument count
477 * @argv: command argument list
478 * @images: pointer to the bootm images structure
479 * @arch: expected ramdisk architecture
480 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
481 * @rd_end: pointer to a ulong variable, will hold ramdisk end
482 *
483 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
484 * Currently supported are the following ramdisk sources:
485 * - multicomponent kernel/ramdisk image,
486 * - commandline provided address of decicated ramdisk image.
487 *
488 * returns:
489 * 0, if ramdisk image was found and valid, or skiped
490 * rd_start and rd_end are set to ramdisk start/end addresses if
491 * ramdisk image is found and valid
492 *
493 * 1, if ramdisk image is found but corrupted, or invalid
494 * rd_start and rd_end are set to 0 if no ramdisk exists
495 */
Simon Glassd9d7c202022-09-06 20:26:50 -0600496int boot_get_ramdisk(int argc, char *const argv[], struct bootm_headers *images,
Simon Glasse4c92872021-09-25 19:43:37 -0600497 u8 arch, ulong *rd_start, ulong *rd_end)
498{
499 ulong rd_data, rd_len;
500 const char *select = NULL;
501
502 *rd_start = 0;
503 *rd_end = 0;
504
505 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
506 char *buf;
507
508 /* Look for an Android boot image */
509 buf = map_sysmem(images->os.start, 0);
510 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
511 select = (argc == 0) ? env_get("loadaddr") : argv[0];
512 }
513
514 if (argc >= 2)
515 select = argv[1];
516
517 /*
518 * Look for a '-' which indicates to ignore the
519 * ramdisk argument
520 */
521 if (select && strcmp(select, "-") == 0) {
522 debug("## Skipping init Ramdisk\n");
523 rd_len = 0;
524 rd_data = 0;
525 } else if (select || genimg_has_config(images)) {
526 int ret;
527
528 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
529 if (ret == -ENOPKG)
530 return 0;
531 else if (ret)
532 return ret;
Simon Glass41506ff2021-09-25 07:03:15 -0600533 } else if (images->legacy_hdr_valid &&
534 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600535 IH_TYPE_MULTI)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600536 /*
537 * Now check if we have a legacy mult-component image,
538 * get second entry data start address and len.
539 */
540 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600541 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
542 (ulong)images->legacy_hdr_os);
Simon Glass41506ff2021-09-25 07:03:15 -0600543
544 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
545 } else {
546 /*
547 * no initrd image
548 */
549 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600550 rd_len = 0;
551 rd_data = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600552 }
553
554 if (!rd_data) {
555 debug("## No init Ramdisk\n");
556 } else {
557 *rd_start = rd_data;
558 *rd_end = rd_data + rd_len;
559 }
560 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600561 *rd_start, *rd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600562
563 return 0;
564}
565
Simon Glass41506ff2021-09-25 07:03:15 -0600566/**
567 * boot_ramdisk_high - relocate init ramdisk
568 * @lmb: pointer to lmb handle, will be used for memory mgmt
569 * @rd_data: ramdisk data start address
570 * @rd_len: ramdisk data length
571 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
572 * start address (after possible relocation)
573 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
574 * end address (after possible relocation)
575 *
576 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
577 * variable and if requested ramdisk data is moved to a specified location.
578 *
579 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
580 * start/end addresses if ramdisk image start and len were provided,
581 * otherwise set initrd_start and initrd_end set to zeros.
582 *
583 * returns:
584 * 0 - success
585 * -1 - failure
586 */
587int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600588 ulong *initrd_start, ulong *initrd_end)
Simon Glass41506ff2021-09-25 07:03:15 -0600589{
590 char *s;
591 ulong initrd_high;
592 int initrd_copy_to_ram = 1;
593
594 s = env_get("initrd_high");
595 if (s) {
596 /* a value of "no" or a similar string will act like 0,
597 * turning the "load high" feature off. This is intentional.
598 */
599 initrd_high = hextoul(s, NULL);
600 if (initrd_high == ~0)
601 initrd_copy_to_ram = 0;
602 } else {
603 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
604 }
605
Simon Glass41506ff2021-09-25 07:03:15 -0600606 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600607 initrd_high, initrd_copy_to_ram);
Simon Glass41506ff2021-09-25 07:03:15 -0600608
609 if (rd_data) {
610 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
611 debug(" in-place initrd\n");
612 *initrd_start = rd_data;
613 *initrd_end = rd_data + rd_len;
614 lmb_reserve(lmb, rd_data, rd_len);
615 } else {
616 if (initrd_high)
617 *initrd_start = (ulong)lmb_alloc_base(lmb,
618 rd_len, 0x1000, initrd_high);
619 else
620 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
621 0x1000);
622
623 if (*initrd_start == 0) {
624 puts("ramdisk - allocation error\n");
625 goto error;
626 }
627 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
628
629 *initrd_end = *initrd_start + rd_len;
630 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600631 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600632
633 memmove_wd((void *)*initrd_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600634 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass41506ff2021-09-25 07:03:15 -0600635
Simon Glass41506ff2021-09-25 07:03:15 -0600636 /*
637 * Ensure the image is flushed to memory to handle
638 * AMP boot scenarios in which we might not be
639 * HW cache coherent
640 */
Simon Glass1df654a2021-09-25 19:43:35 -0600641 if (IS_ENABLED(CONFIG_MP)) {
642 flush_cache((unsigned long)*initrd_start,
643 ALIGN(rd_len, ARCH_DMA_MINALIGN));
644 }
Simon Glass41506ff2021-09-25 07:03:15 -0600645 puts("OK\n");
646 }
647 } else {
648 *initrd_start = 0;
649 *initrd_end = 0;
650 }
651 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600652 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600653
654 return 0;
655
656error:
657 return -1;
658}
Simon Glass41506ff2021-09-25 07:03:15 -0600659
Simon Glassd9d7c202022-09-06 20:26:50 -0600660int boot_get_setup(struct bootm_headers *images, u8 arch,
Simon Glass41506ff2021-09-25 07:03:15 -0600661 ulong *setup_start, ulong *setup_len)
662{
Simon Glass1df654a2021-09-25 19:43:35 -0600663 if (!CONFIG_IS_ENABLED(FIT))
664 return -ENOENT;
665
Simon Glass41506ff2021-09-25 07:03:15 -0600666 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600667}
668
Simon Glassd9d7c202022-09-06 20:26:50 -0600669int boot_get_fpga(int argc, char *const argv[], struct bootm_headers *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600670 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600671{
672 ulong tmp_img_addr, img_data, img_len;
673 void *buf;
674 int conf_noffset;
675 int fit_img_result;
676 const char *uname, *name;
677 int err;
678 int devnum = 0; /* TODO support multi fpga platforms */
679
Simon Glass1df654a2021-09-25 19:43:35 -0600680 if (!IS_ENABLED(CONFIG_FPGA))
681 return -ENOSYS;
682
Simon Glass41506ff2021-09-25 07:03:15 -0600683 /* Check to see if the images struct has a FIT configuration */
684 if (!genimg_has_config(images)) {
685 debug("## FIT configuration was not specified\n");
686 return 0;
687 }
688
689 /*
690 * Obtain the os FIT header from the images struct
691 */
692 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
693 buf = map_sysmem(tmp_img_addr, 0);
694 /*
695 * Check image type. For FIT images get FIT node
696 * and attempt to locate a generic binary.
697 */
698 switch (genimg_get_format(buf)) {
699 case IMAGE_FORMAT_FIT:
700 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
701
702 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
703 NULL);
704 if (!uname) {
705 debug("## FPGA image is not specified\n");
706 return 0;
707 }
708 fit_img_result = fit_image_load(images,
709 tmp_img_addr,
710 (const char **)&uname,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600711 &images->fit_uname_cfg,
Simon Glass41506ff2021-09-25 07:03:15 -0600712 arch,
713 IH_TYPE_FPGA,
714 BOOTSTAGE_ID_FPGA_INIT,
715 FIT_LOAD_OPTIONAL_NON_ZERO,
716 &img_data, &img_len);
717
718 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
719 uname, img_data, img_len);
720
721 if (fit_img_result < 0) {
722 /* Something went wrong! */
723 return fit_img_result;
724 }
725
726 if (!fpga_is_partial_data(devnum, img_len)) {
727 name = "full";
728 err = fpga_loadbitstream(devnum, (char *)img_data,
729 img_len, BIT_FULL);
730 if (err)
731 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300732 img_len, BIT_FULL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600733 } else {
734 name = "partial";
735 err = fpga_loadbitstream(devnum, (char *)img_data,
736 img_len, BIT_PARTIAL);
737 if (err)
738 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300739 img_len, BIT_PARTIAL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600740 }
741
742 if (err)
743 return err;
744
745 printf(" Programming %s bitstream... OK\n", name);
746 break;
747 default:
748 printf("The given image format is not supported (corrupt?)\n");
749 return 1;
750 }
751
752 return 0;
753}
Simon Glass41506ff2021-09-25 07:03:15 -0600754
Simon Glass3d2a47f2021-09-25 07:03:16 -0600755static void fit_loadable_process(u8 img_type,
Simon Glass41506ff2021-09-25 07:03:15 -0600756 ulong img_data,
757 ulong img_len)
758{
759 int i;
760 const unsigned int count =
761 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
762 struct fit_loadable_tbl *fit_loadable_handler =
763 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
764 /* For each loadable handler */
765 for (i = 0; i < count; i++, fit_loadable_handler++)
766 /* matching this type */
767 if (fit_loadable_handler->type == img_type)
768 /* call that handler with this image data */
769 fit_loadable_handler->handler(img_data, img_len);
770}
771
Simon Glassd9d7c202022-09-06 20:26:50 -0600772int boot_get_loadable(int argc, char *const argv[], struct bootm_headers *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600773 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600774{
775 /*
776 * These variables are used to hold the current image location
777 * in system memory.
778 */
779 ulong tmp_img_addr;
780 /*
781 * These two variables are requirements for fit_image_load, but
782 * their values are not used
783 */
784 ulong img_data, img_len;
785 void *buf;
786 int loadables_index;
787 int conf_noffset;
788 int fit_img_result;
789 const char *uname;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600790 u8 img_type;
Simon Glass41506ff2021-09-25 07:03:15 -0600791
792 /* Check to see if the images struct has a FIT configuration */
793 if (!genimg_has_config(images)) {
794 debug("## FIT configuration was not specified\n");
795 return 0;
796 }
797
798 /*
799 * Obtain the os FIT header from the images struct
800 */
801 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
802 buf = map_sysmem(tmp_img_addr, 0);
803 /*
804 * Check image type. For FIT images get FIT node
805 * and attempt to locate a generic binary.
806 */
807 switch (genimg_get_format(buf)) {
808 case IMAGE_FORMAT_FIT:
809 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
810
811 for (loadables_index = 0;
812 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600813 FIT_LOADABLE_PROP,
814 loadables_index, NULL), uname;
815 loadables_index++) {
816 fit_img_result = fit_image_load(images, tmp_img_addr,
817 &uname,
818 &images->fit_uname_cfg,
819 arch, IH_TYPE_LOADABLE,
820 BOOTSTAGE_ID_FIT_LOADABLE_START,
821 FIT_LOAD_OPTIONAL_NON_ZERO,
822 &img_data, &img_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600823 if (fit_img_result < 0) {
824 /* Something went wrong! */
825 return fit_img_result;
826 }
827
828 fit_img_result = fit_image_get_node(buf, uname);
829 if (fit_img_result < 0) {
830 /* Something went wrong! */
831 return fit_img_result;
832 }
833 fit_img_result = fit_image_get_type(buf,
834 fit_img_result,
835 &img_type);
836 if (fit_img_result < 0) {
837 /* Something went wrong! */
838 return fit_img_result;
839 }
840
841 fit_loadable_process(img_type, img_data, img_len);
842 }
843 break;
844 default:
845 printf("The given image format is not supported (corrupt?)\n");
846 return 1;
847 }
848
849 return 0;
850}
Simon Glass41506ff2021-09-25 07:03:15 -0600851
Simon Glass41506ff2021-09-25 07:03:15 -0600852/**
853 * boot_get_cmdline - allocate and initialize kernel cmdline
854 * @lmb: pointer to lmb handle, will be used for memory mgmt
855 * @cmd_start: pointer to a ulong variable, will hold cmdline start
856 * @cmd_end: pointer to a ulong variable, will hold cmdline end
857 *
Simon Glass9c2e9122022-08-28 12:32:53 -0600858 * This allocates space for kernel command line below
Simon Glass41506ff2021-09-25 07:03:15 -0600859 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
860 * variable is present its contents is copied to allocated kernel
861 * command line.
862 *
863 * returns:
864 * 0 - success
865 * -1 - failure
866 */
867int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
868{
Simon Glass9c2e9122022-08-28 12:32:53 -0600869 int barg;
Simon Glass41506ff2021-09-25 07:03:15 -0600870 char *cmdline;
871 char *s;
872
Simon Glass9c2e9122022-08-28 12:32:53 -0600873 /*
874 * Help the compiler detect that this function is only called when
875 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
876 */
877 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
878 return 0;
879
880 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
881 cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
Simon Glass41506ff2021-09-25 07:03:15 -0600882 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass3d2a47f2021-09-25 07:03:16 -0600883 if (!cmdline)
Simon Glass41506ff2021-09-25 07:03:15 -0600884 return -1;
885
886 s = env_get("bootargs");
887 if (!s)
888 s = "";
889
890 strcpy(cmdline, s);
891
Simon Glass3d2a47f2021-09-25 07:03:16 -0600892 *cmd_start = (ulong)cmdline;
Simon Glass41506ff2021-09-25 07:03:15 -0600893 *cmd_end = *cmd_start + strlen(cmdline);
894
895 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
896
897 return 0;
898}
Simon Glass41506ff2021-09-25 07:03:15 -0600899
Simon Glass41506ff2021-09-25 07:03:15 -0600900/**
901 * boot_get_kbd - allocate and initialize kernel copy of board info
902 * @lmb: pointer to lmb handle, will be used for memory mgmt
903 * @kbd: double pointer to board info data
904 *
905 * boot_get_kbd() allocates space for kernel copy of board info data below
906 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
907 * with the current u-boot board info data.
908 *
909 * returns:
910 * 0 - success
911 * -1 - failure
912 */
913int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
914{
915 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
916 sizeof(struct bd_info),
917 0xf,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600918 env_get_bootm_mapsize() +
919 env_get_bootm_low());
920 if (!*kbd)
Simon Glass41506ff2021-09-25 07:03:15 -0600921 return -1;
922
Simon Glass3d2a47f2021-09-25 07:03:16 -0600923 **kbd = *gd->bd;
Simon Glass41506ff2021-09-25 07:03:15 -0600924
925 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
926
Simon Glass9c2e9122022-08-28 12:32:53 -0600927 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
Simon Glass4ed37ab2021-09-25 07:03:20 -0600928 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass41506ff2021-09-25 07:03:15 -0600929
930 return 0;
931}
Simon Glass41506ff2021-09-25 07:03:15 -0600932
Simon Glassd9d7c202022-09-06 20:26:50 -0600933int image_setup_linux(struct bootm_headers *images)
Simon Glass41506ff2021-09-25 07:03:15 -0600934{
935 ulong of_size = images->ft_len;
936 char **of_flat_tree = &images->ft_addr;
Simon Glass9c2e9122022-08-28 12:32:53 -0600937 struct lmb *lmb = images_lmb(images);
Simon Glass41506ff2021-09-25 07:03:15 -0600938 int ret;
939
Simon Glass9c2e9122022-08-28 12:32:53 -0600940 /* This function cannot be called without lmb support */
Simon Glass210af542023-02-05 15:40:13 -0700941 if (!IS_ENABLED(CONFIG_LMB))
Simon Glass9c2e9122022-08-28 12:32:53 -0600942 return -EFAULT;
Simon Glass0c303f92021-09-25 19:43:21 -0600943 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass41506ff2021-09-25 07:03:15 -0600944 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
945
Simon Glass806d1ff2021-09-25 19:43:25 -0600946 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600947 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600948 &images->cmdline_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600949 if (ret) {
950 puts("ERROR with allocation of cmdline\n");
951 return ret;
952 }
953 }
954
Simon Glass0c303f92021-09-25 19:43:21 -0600955 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600956 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
957 if (ret)
958 return ret;
959 }
960
Simon Glass0c303f92021-09-25 19:43:21 -0600961 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass41506ff2021-09-25 07:03:15 -0600962 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
963 if (ret)
964 return ret;
965 }
966
967 return 0;
968}
Simon Glass5d3248a2021-09-25 07:03:17 -0600969
970void genimg_print_size(uint32_t size)
971{
972 printf("%d Bytes = ", size);
973 print_size(size, "\n");
974}
975
976void genimg_print_time(time_t timestamp)
977{
978 struct rtc_time tm;
979
980 rtc_to_tm(timestamp, &tm);
981 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
982 tm.tm_year, tm.tm_mon, tm.tm_mday,
983 tm.tm_hour, tm.tm_min, tm.tm_sec);
984}
Simon Glass30f33332023-01-06 08:52:28 -0600985
986/**
987 * get_default_image() - Return default property from /images
988 *
989 * Return: Pointer to value of default property (or NULL)
990 */
991static const char *get_default_image(const void *fit)
992{
993 int images_noffset;
994
995 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
996 if (images_noffset < 0)
997 return NULL;
998
999 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
1000}
1001
1002int image_locate_script(void *buf, int size, const char *fit_uname,
1003 const char *confname, char **datap, uint *lenp)
1004{
1005 const struct legacy_img_hdr *hdr;
1006 const void *fit_data;
1007 const void *fit_hdr;
1008 size_t fit_len;
1009 int noffset;
1010 int verify;
1011 ulong len;
1012 u32 *data;
1013
1014 verify = env_get_yesno("verify");
1015
1016 switch (genimg_get_format(buf)) {
1017 case IMAGE_FORMAT_LEGACY:
Marek Vasut771cb4d2023-02-27 20:56:31 +01001018 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
1019 goto exit_image_format;
1020 } else {
Simon Glass30f33332023-01-06 08:52:28 -06001021 hdr = buf;
1022
1023 if (!image_check_magic(hdr)) {
1024 puts("Bad magic number\n");
1025 return 1;
1026 }
1027
1028 if (!image_check_hcrc(hdr)) {
1029 puts("Bad header crc\n");
1030 return 1;
1031 }
1032
1033 if (verify) {
1034 if (!image_check_dcrc(hdr)) {
1035 puts("Bad data crc\n");
1036 return 1;
1037 }
1038 }
1039
1040 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
1041 puts("Bad image type\n");
1042 return 1;
1043 }
1044
1045 /* get length of script */
1046 data = (u32 *)image_get_data(hdr);
1047
1048 len = uimage_to_cpu(*data);
1049 if (!len) {
1050 puts("Empty Script\n");
1051 return 1;
1052 }
1053
1054 /*
1055 * scripts are just multi-image files with one
1056 * component, so seek past the zero-terminated sequence
1057 * of image lengths to get to the actual image data
1058 */
1059 while (*data++);
1060 }
1061 break;
1062 case IMAGE_FORMAT_FIT:
Marek Vasut771cb4d2023-02-27 20:56:31 +01001063 if (!IS_ENABLED(CONFIG_FIT)) {
1064 goto exit_image_format;
1065 } else {
Simon Glass30f33332023-01-06 08:52:28 -06001066 fit_hdr = buf;
1067 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1068 puts("Bad FIT image format\n");
1069 return 1;
1070 }
1071
1072 if (!fit_uname) {
1073 /* If confname is empty, use the default */
1074 if (confname && *confname)
1075 noffset = fit_conf_get_node(fit_hdr, confname);
1076 else
1077 noffset = fit_conf_get_node(fit_hdr, NULL);
1078 if (noffset < 0) {
1079 if (!confname)
1080 goto fallback;
1081 printf("Could not find config %s\n", confname);
1082 return 1;
1083 }
1084
1085 if (verify && fit_config_verify(fit_hdr, noffset))
1086 return 1;
1087
1088 noffset = fit_conf_get_prop_node(fit_hdr,
1089 noffset,
1090 FIT_SCRIPT_PROP,
1091 IH_PHASE_NONE);
1092 if (noffset < 0) {
1093 if (!confname)
1094 goto fallback;
1095 printf("Could not find script in %s\n", confname);
1096 return 1;
1097 }
1098 } else {
1099fallback:
1100 if (!fit_uname || !*fit_uname)
1101 fit_uname = get_default_image(fit_hdr);
1102 if (!fit_uname) {
1103 puts("No FIT subimage unit name\n");
1104 return 1;
1105 }
1106
1107 /* get script component image node offset */
1108 noffset = fit_image_get_node(fit_hdr, fit_uname);
1109 if (noffset < 0) {
1110 printf("Can't find '%s' FIT subimage\n",
1111 fit_uname);
1112 return 1;
1113 }
1114 }
1115
1116 if (!fit_image_check_type(fit_hdr, noffset,
1117 IH_TYPE_SCRIPT)) {
1118 puts("Not a image image\n");
1119 return 1;
1120 }
1121
1122 /* verify integrity */
1123 if (verify && !fit_image_verify(fit_hdr, noffset)) {
1124 puts("Bad Data Hash\n");
1125 return 1;
1126 }
1127
1128 /* get script subimage data address and length */
Tobias Waldekranze45bba52023-02-16 16:33:47 +01001129 if (fit_image_get_data_and_size(fit_hdr, noffset,
1130 &fit_data, &fit_len)) {
Simon Glass30f33332023-01-06 08:52:28 -06001131 puts("Could not find script subimage data\n");
1132 return 1;
1133 }
1134
1135 data = (u32 *)fit_data;
1136 len = (ulong)fit_len;
1137 }
1138 break;
1139 default:
Marek Vasut771cb4d2023-02-27 20:56:31 +01001140 goto exit_image_format;
Simon Glass30f33332023-01-06 08:52:28 -06001141 }
1142
1143 *datap = (char *)data;
1144 *lenp = len;
1145
1146 return 0;
Marek Vasut771cb4d2023-02-27 20:56:31 +01001147
1148exit_image_format:
1149 puts("Wrong image format for \"source\" command\n");
1150 return -EPERM;
Simon Glass30f33332023-01-06 08:52:28 -06001151}