blob: e3d63745299393b7d228a85a94190c8db53f5700 [file] [log] [blame]
Simon Glass4d7237b2021-09-25 07:03:15 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Image code used by boards (and not host tools)
4 *
5 * (C) Copyright 2008 Semihalf
6 *
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 */
10
11#include <common.h>
12#include <bootstage.h>
13#include <cpu_func.h>
Simon Glass1ab16922022-07-31 12:28:48 -060014#include <display_options.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060015#include <env.h>
16#include <fpga.h>
17#include <image.h>
Andy Shevchenkoe3b3ad92021-11-08 21:03:38 +030018#include <init.h>
Simon Glass7ae16bb2022-08-28 12:32:53 -060019#include <log.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060020#include <mapmem.h>
Simon Glass4a8a8a12021-09-25 07:03:17 -060021#include <rtc.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060022#include <watchdog.h>
23#include <asm/cache.h>
24#include <asm/global_data.h>
25
Simon Glass4d7237b2021-09-25 07:03:15 -060026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass4d7237b2021-09-25 07:03:15 -060028/**
29 * image_get_ramdisk - get and verify ramdisk image
30 * @rd_addr: ramdisk image start address
31 * @arch: expected ramdisk architecture
32 * @verify: checksum verification flag
33 *
34 * image_get_ramdisk() returns a pointer to the verified ramdisk image
35 * header. Routine receives image start address and expected architecture
36 * flag. Verification done covers data and header integrity and os/type/arch
37 * fields checking.
38 *
39 * returns:
40 * pointer to a ramdisk image header, if image was found and valid
41 * otherwise, return NULL
42 */
Simon Glassbb7d3bb2022-09-06 20:26:52 -060043static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
44 int verify)
Simon Glass4d7237b2021-09-25 07:03:15 -060045{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060046 const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -060047
48 if (!image_check_magic(rd_hdr)) {
49 puts("Bad Magic Number\n");
50 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
51 return NULL;
52 }
53
54 if (!image_check_hcrc(rd_hdr)) {
55 puts("Bad Header Checksum\n");
56 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
57 return NULL;
58 }
59
60 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
61 image_print_contents(rd_hdr);
62
63 if (verify) {
64 puts(" Verifying Checksum ... ");
65 if (!image_check_dcrc(rd_hdr)) {
66 puts("Bad Data CRC\n");
67 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
68 return NULL;
69 }
70 puts("OK\n");
71 }
72
73 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
74
75 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
76 !image_check_arch(rd_hdr, arch) ||
77 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
78 printf("No Linux %s Ramdisk Image\n",
Simon Glass60d71542021-09-25 07:03:16 -060079 genimg_get_arch_name(arch));
Simon Glass4d7237b2021-09-25 07:03:15 -060080 bootstage_error(BOOTSTAGE_ID_RAMDISK);
81 return NULL;
82 }
83
84 return rd_hdr;
85}
Simon Glass4d7237b2021-09-25 07:03:15 -060086
87/*****************************************************************************/
88/* Shared dual-format routines */
89/*****************************************************************************/
90ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
91ulong image_save_addr; /* Default Save Address */
92ulong image_save_size; /* Default Save Size (in bytes) */
93
94static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass60d71542021-09-25 07:03:16 -060095 int flags)
Simon Glass4d7237b2021-09-25 07:03:15 -060096{
97 switch (op) {
98 case env_op_create:
99 case env_op_overwrite:
100 image_load_addr = hextoul(value, NULL);
101 break;
102 default:
103 break;
104 }
105
106 return 0;
107}
108U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
Marek Vasutf816c422024-03-26 23:13:11 +0100110phys_addr_t env_get_bootm_low(void)
Simon Glass4d7237b2021-09-25 07:03:15 -0600111{
112 char *s = env_get("bootm_low");
Simon Glass60d71542021-09-25 07:03:16 -0600113
Marek Vasutf816c422024-03-26 23:13:11 +0100114 if (s)
115 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600116
Tom Rinibb4dd962022-11-16 13:10:37 -0500117#if defined(CFG_SYS_SDRAM_BASE)
118 return CFG_SYS_SDRAM_BASE;
Simon Glass4d7237b2021-09-25 07:03:15 -0600119#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
120 return gd->bd->bi_dram[0].start;
121#else
122 return 0;
123#endif
124}
125
126phys_size_t env_get_bootm_size(void)
127{
128 phys_size_t tmp, size;
129 phys_addr_t start;
130 char *s = env_get("bootm_size");
Simon Glass60d71542021-09-25 07:03:16 -0600131
Marek Vasut1d02ed72024-03-26 23:13:12 +0100132 if (s)
133 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600134
135 start = gd->ram_base;
136 size = gd->ram_size;
137
138 if (start + size > gd->ram_top)
139 size = gd->ram_top - start;
140
141 s = env_get("bootm_low");
142 if (s)
Marek Vasut1d02ed72024-03-26 23:13:12 +0100143 tmp = simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600144 else
145 tmp = start;
146
147 return size - (tmp - start);
148}
149
150phys_size_t env_get_bootm_mapsize(void)
151{
152 phys_size_t tmp;
153 char *s = env_get("bootm_mapsize");
Simon Glass60d71542021-09-25 07:03:16 -0600154
Simon Glass4d7237b2021-09-25 07:03:15 -0600155 if (s) {
156 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
157 return tmp;
158 }
159
Tom Rini6a5dccc2022-11-16 13:10:41 -0500160#if defined(CFG_SYS_BOOTMAPSZ)
161 return CFG_SYS_BOOTMAPSZ;
Simon Glass4d7237b2021-09-25 07:03:15 -0600162#else
163 return env_get_bootm_size();
164#endif
165}
166
167void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
168{
169 if (to == from)
170 return;
171
Simon Glass7ae16bb2022-08-28 12:32:53 -0600172 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600173 if (to > from) {
Simon Glass7ae16bb2022-08-28 12:32:53 -0600174 from += len;
175 to += len;
Simon Glass4d7237b2021-09-25 07:03:15 -0600176 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600177 while (len > 0) {
178 size_t tail = (len > chunksz) ? chunksz : len;
179
Stefan Roese80877fa2022-09-02 14:10:46 +0200180 schedule();
Simon Glass7ae16bb2022-08-28 12:32:53 -0600181 if (to > from) {
182 to -= tail;
183 from -= tail;
184 }
185 memmove(to, from, tail);
186 if (to < from) {
187 to += tail;
188 from += tail;
189 }
190 len -= tail;
Simon Glass4d7237b2021-09-25 07:03:15 -0600191 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600192 } else {
193 memmove(to, from, len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600194 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600195}
196
Simon Glass4e8ec472023-11-18 14:04:57 -0700197ulong genimg_get_kernel_addr_fit(const char *const img_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600198 const char **fit_uname_config,
199 const char **fit_uname_kernel)
Simon Glass4d7237b2021-09-25 07:03:15 -0600200{
201 ulong kernel_addr;
202
203 /* find out kernel image address */
204 if (!img_addr) {
205 kernel_addr = image_load_addr;
206 debug("* kernel: default image load address = 0x%08lx\n",
207 image_load_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600208 } else if (CONFIG_IS_ENABLED(FIT) &&
209 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass4d7237b2021-09-25 07:03:15 -0600210 fit_uname_config)) {
211 debug("* kernel: config '%s' from image at 0x%08lx\n",
212 *fit_uname_config, kernel_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600213 } else if (CONFIG_IS_ENABLED(FIT) &&
214 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
215 fit_uname_kernel)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600216 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
217 *fit_uname_kernel, kernel_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600218 } else {
219 kernel_addr = hextoul(img_addr, NULL);
220 debug("* kernel: cmdline image address = 0x%08lx\n",
221 kernel_addr);
222 }
223
224 return kernel_addr;
225}
226
227/**
228 * genimg_get_kernel_addr() is the simple version of
229 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
230 */
231ulong genimg_get_kernel_addr(char * const img_addr)
232{
233 const char *fit_uname_config = NULL;
234 const char *fit_uname_kernel = NULL;
235
236 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
237 &fit_uname_kernel);
238}
239
240/**
241 * genimg_get_format - get image format type
242 * @img_addr: image start address
243 *
244 * genimg_get_format() checks whether provided address points to a valid
245 * legacy or FIT image.
246 *
247 * New uImage format and FDT blob are based on a libfdt. FDT blob
248 * may be passed directly or embedded in a FIT image. In both situations
249 * genimg_get_format() must be able to dectect libfdt header.
250 *
251 * returns:
252 * image format type or IMAGE_FORMAT_INVALID if no image is present
253 */
254int genimg_get_format(const void *img_addr)
255{
Simon Glass0f781382021-09-25 19:43:35 -0600256 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600257 const struct legacy_img_hdr *hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600258
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600259 hdr = (const struct legacy_img_hdr *)img_addr;
Simon Glass0f781382021-09-25 19:43:35 -0600260 if (image_check_magic(hdr))
261 return IMAGE_FORMAT_LEGACY;
262 }
263 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
264 if (!fdt_check_header(img_addr))
265 return IMAGE_FORMAT_FIT;
266 }
267 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100268 is_android_boot_image_header(img_addr))
Simon Glass4d7237b2021-09-25 07:03:15 -0600269 return IMAGE_FORMAT_ANDROID;
Simon Glass4d7237b2021-09-25 07:03:15 -0600270
271 return IMAGE_FORMAT_INVALID;
272}
273
274/**
275 * fit_has_config - check if there is a valid FIT configuration
276 * @images: pointer to the bootm command headers structure
277 *
278 * fit_has_config() checks if there is a FIT configuration in use
279 * (if FTI support is present).
280 *
281 * returns:
282 * 0, no FIT support or no configuration found
283 * 1, configuration found
284 */
Simon Glassdf00afa2022-09-06 20:26:50 -0600285int genimg_has_config(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600286{
Simon Glass0f781382021-09-25 19:43:35 -0600287 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass4d7237b2021-09-25 07:03:15 -0600288 return 1;
Simon Glass0f781382021-09-25 19:43:35 -0600289
Simon Glass4d7237b2021-09-25 07:03:15 -0600290 return 0;
291}
292
293/**
Simon Glassccda2fa2021-09-25 19:43:37 -0600294 * select_ramdisk() - Select and locate the ramdisk to use
295 *
Simon Glass4d7237b2021-09-25 07:03:15 -0600296 * @images: pointer to the bootm images structure
Simon Glass664696f2022-08-28 12:32:47 -0600297 * @select: name of ramdisk to select, or hex address, NULL for any
Simon Glass4d7237b2021-09-25 07:03:15 -0600298 * @arch: expected ramdisk architecture
Simon Glassccda2fa2021-09-25 19:43:37 -0600299 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
300 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100301 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glassccda2fa2021-09-25 19:43:37 -0600302 * other -ve value on other error
Simon Glass4d7237b2021-09-25 07:03:15 -0600303 */
Simon Glassdf00afa2022-09-06 20:26:50 -0600304static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
Simon Glassccda2fa2021-09-25 19:43:37 -0600305 ulong *rd_datap, ulong *rd_lenp)
Simon Glass4d7237b2021-09-25 07:03:15 -0600306{
Simon Glassf2d23de2022-08-28 12:32:49 -0600307 const char *fit_uname_config;
308 const char *fit_uname_ramdisk;
Simon Glass973b8932022-08-28 12:32:51 -0600309 bool done_select = !select;
Simon Glass664696f2022-08-28 12:32:47 -0600310 bool done = false;
Simon Glassf2d23de2022-08-28 12:32:49 -0600311 int rd_noffset;
Tom Rini1ca3c642023-04-05 19:48:56 -0400312 ulong rd_addr = 0;
Simon Glassccda2fa2021-09-25 19:43:37 -0600313 char *buf;
Simon Glass2de5b012021-09-25 19:43:36 -0600314
Simon Glass973b8932022-08-28 12:32:51 -0600315 if (CONFIG_IS_ENABLED(FIT)) {
Simon Glassf2d23de2022-08-28 12:32:49 -0600316 fit_uname_config = images->fit_uname_cfg;
317 fit_uname_ramdisk = NULL;
Simon Glass2de5b012021-09-25 19:43:36 -0600318
Tom Rinibc7d9d22021-12-20 09:36:32 -0500319 if (select) {
320 ulong default_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600321 /*
322 * If the init ramdisk comes from the FIT image and
323 * the FIT image address is omitted in the command
324 * line argument, try to use os FIT image address or
325 * default load address.
326 */
327 if (images->fit_uname_os)
328 default_addr = (ulong)images->fit_hdr_os;
329 else
330 default_addr = image_load_addr;
331
Simon Glass92c586f2022-08-28 12:32:52 -0600332 if (fit_parse_conf(select, default_addr, &rd_addr,
333 &fit_uname_config)) {
Simon Glass60d71542021-09-25 07:03:16 -0600334 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
335 fit_uname_config, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600336 done_select = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600337 } else if (fit_parse_subimage(select, default_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600338 &rd_addr,
339 &fit_uname_ramdisk)) {
340 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
341 fit_uname_ramdisk, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600342 done_select = true;
343 }
344 }
345 }
346 if (!done_select) {
Simon Glass92c586f2022-08-28 12:32:52 -0600347 rd_addr = hextoul(select, NULL);
348 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600349 }
Simon Glass92c586f2022-08-28 12:32:52 -0600350 if (CONFIG_IS_ENABLED(FIT) && !select) {
351 /* use FIT configuration provided in first bootm
352 * command argument. If the property is not defined,
353 * quit silently (with -ENOPKG)
Tom Rinibc7d9d22021-12-20 09:36:32 -0500354 */
Simon Glass92c586f2022-08-28 12:32:52 -0600355 rd_addr = map_to_sysmem(images->fit_hdr_os);
356 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
357 rd_addr);
358 if (rd_noffset == -ENOENT)
359 return -ENOPKG;
360 else if (rd_noffset < 0)
361 return rd_noffset;
362 }
Simon Glass2de5b012021-09-25 19:43:36 -0600363
Simon Glass92c586f2022-08-28 12:32:52 -0600364 /*
365 * Check if there is an initrd image at the
366 * address provided in the second bootm argument
367 * check image type, for FIT images get FIT node.
368 */
369 buf = map_sysmem(rd_addr, 0);
370 switch (genimg_get_format(buf)) {
371 case IMAGE_FORMAT_LEGACY:
372 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600373 const struct legacy_img_hdr *rd_hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600374
Simon Glass92c586f2022-08-28 12:32:52 -0600375 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
376 rd_addr);
Tom Rinibc7d9d22021-12-20 09:36:32 -0500377
Simon Glass92c586f2022-08-28 12:32:52 -0600378 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
379 rd_hdr = image_get_ramdisk(rd_addr, arch,
380 images->verify);
Simon Glass4d7237b2021-09-25 07:03:15 -0600381
Simon Glass92c586f2022-08-28 12:32:52 -0600382 if (!rd_hdr)
383 return -ENOENT;
Simon Glass4d7237b2021-09-25 07:03:15 -0600384
Simon Glass92c586f2022-08-28 12:32:52 -0600385 *rd_datap = image_get_data(rd_hdr);
386 *rd_lenp = image_get_data_size(rd_hdr);
387 done = true;
388 }
389 break;
390 case IMAGE_FORMAT_FIT:
391 if (CONFIG_IS_ENABLED(FIT)) {
392 rd_noffset = fit_image_load(images, rd_addr,
393 &fit_uname_ramdisk,
394 &fit_uname_config,
395 arch, IH_TYPE_RAMDISK,
396 BOOTSTAGE_ID_FIT_RD_START,
397 FIT_LOAD_OPTIONAL_NON_ZERO,
398 rd_datap, rd_lenp);
399 if (rd_noffset < 0)
400 return rd_noffset;
Simon Glassdc0aa042022-08-28 12:32:46 -0600401
Simon Glass92c586f2022-08-28 12:32:52 -0600402 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
403 images->fit_uname_rd = fit_uname_ramdisk;
404 images->fit_noffset_rd = rd_noffset;
405 done = true;
Simon Glass664696f2022-08-28 12:32:47 -0600406 }
Simon Glass92c586f2022-08-28 12:32:52 -0600407 break;
408 case IMAGE_FORMAT_ANDROID:
409 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass92c586f2022-08-28 12:32:52 -0600410 int ret;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100411 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
412 void *boot_img = map_sysmem(get_abootimg_addr(), 0);
413 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
414
415 ret = android_image_get_ramdisk(boot_img, vendor_boot_img,
416 rd_datap, rd_lenp);
417 unmap_sysmem(vendor_boot_img);
418 unmap_sysmem(boot_img);
419 } else {
420 void *ptr = map_sysmem(images->os.start, 0);
421
422 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp);
423 unmap_sysmem(ptr);
424 }
425
Simon Glass92c586f2022-08-28 12:32:52 -0600426 if (ret)
427 return ret;
428 done = true;
429 }
430 break;
431 }
Simon Glass664696f2022-08-28 12:32:47 -0600432
433 if (!done) {
434 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
435 char *end = NULL;
Simon Glass0f781382021-09-25 19:43:35 -0600436
Simon Glass664696f2022-08-28 12:32:47 -0600437 if (select)
438 end = strchr(select, ':');
439 if (end) {
440 *rd_lenp = hextoul(++end, NULL);
441 *rd_datap = rd_addr;
442 done = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600443 }
Simon Glass664696f2022-08-28 12:32:47 -0600444 }
445
446 if (!done) {
Simon Glass0f781382021-09-25 19:43:35 -0600447 puts("Wrong Ramdisk Image Format\n");
Simon Glassccda2fa2021-09-25 19:43:37 -0600448 return -EINVAL;
Simon Glass4d7237b2021-09-25 07:03:15 -0600449 }
Simon Glass664696f2022-08-28 12:32:47 -0600450 }
Simon Glassccda2fa2021-09-25 19:43:37 -0600451
452 return 0;
453}
454
Simon Glass8eef77e2023-11-18 14:05:06 -0700455int boot_get_ramdisk(char const *select, struct bootm_headers *images,
456 uint arch, ulong *rd_start, ulong *rd_end)
Simon Glassccda2fa2021-09-25 19:43:37 -0600457{
458 ulong rd_data, rd_len;
Simon Glassccda2fa2021-09-25 19:43:37 -0600459
460 *rd_start = 0;
461 *rd_end = 0;
462
Simon Glassccda2fa2021-09-25 19:43:37 -0600463 /*
464 * Look for a '-' which indicates to ignore the
465 * ramdisk argument
466 */
467 if (select && strcmp(select, "-") == 0) {
468 debug("## Skipping init Ramdisk\n");
469 rd_len = 0;
470 rd_data = 0;
471 } else if (select || genimg_has_config(images)) {
472 int ret;
473
474 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
475 if (ret == -ENOPKG)
476 return 0;
477 else if (ret)
478 return ret;
Simon Glass4d7237b2021-09-25 07:03:15 -0600479 } else if (images->legacy_hdr_valid &&
480 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass60d71542021-09-25 07:03:16 -0600481 IH_TYPE_MULTI)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600482 /*
483 * Now check if we have a legacy mult-component image,
484 * get second entry data start address and len.
485 */
486 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600487 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
488 (ulong)images->legacy_hdr_os);
Simon Glass4d7237b2021-09-25 07:03:15 -0600489
490 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
491 } else {
492 /*
493 * no initrd image
494 */
495 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600496 rd_len = 0;
497 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600498 }
499
500 if (!rd_data) {
501 debug("## No init Ramdisk\n");
502 } else {
503 *rd_start = rd_data;
504 *rd_end = rd_data + rd_len;
505 }
506 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600507 *rd_start, *rd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600508
509 return 0;
510}
511
Simon Glass4d7237b2021-09-25 07:03:15 -0600512/**
513 * boot_ramdisk_high - relocate init ramdisk
514 * @lmb: pointer to lmb handle, will be used for memory mgmt
515 * @rd_data: ramdisk data start address
516 * @rd_len: ramdisk data length
517 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
518 * start address (after possible relocation)
519 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
520 * end address (after possible relocation)
521 *
522 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
523 * variable and if requested ramdisk data is moved to a specified location.
524 *
525 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
526 * start/end addresses if ramdisk image start and len were provided,
527 * otherwise set initrd_start and initrd_end set to zeros.
528 *
529 * returns:
530 * 0 - success
531 * -1 - failure
532 */
533int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass60d71542021-09-25 07:03:16 -0600534 ulong *initrd_start, ulong *initrd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600535{
536 char *s;
Marek Vasutf816c422024-03-26 23:13:11 +0100537 phys_addr_t initrd_high;
Simon Glass4d7237b2021-09-25 07:03:15 -0600538 int initrd_copy_to_ram = 1;
539
540 s = env_get("initrd_high");
541 if (s) {
542 /* a value of "no" or a similar string will act like 0,
543 * turning the "load high" feature off. This is intentional.
544 */
545 initrd_high = hextoul(s, NULL);
546 if (initrd_high == ~0)
547 initrd_copy_to_ram = 0;
548 } else {
549 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
550 }
551
Marek Vasutf816c422024-03-26 23:13:11 +0100552 debug("## initrd_high = 0x%llx, copy_to_ram = %d\n",
553 (u64)initrd_high, initrd_copy_to_ram);
Simon Glass4d7237b2021-09-25 07:03:15 -0600554
555 if (rd_data) {
556 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
557 debug(" in-place initrd\n");
558 *initrd_start = rd_data;
559 *initrd_end = rd_data + rd_len;
560 lmb_reserve(lmb, rd_data, rd_len);
561 } else {
562 if (initrd_high)
563 *initrd_start = (ulong)lmb_alloc_base(lmb,
564 rd_len, 0x1000, initrd_high);
565 else
566 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
567 0x1000);
568
569 if (*initrd_start == 0) {
570 puts("ramdisk - allocation error\n");
571 goto error;
572 }
573 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
574
575 *initrd_end = *initrd_start + rd_len;
576 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass60d71542021-09-25 07:03:16 -0600577 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600578
579 memmove_wd((void *)*initrd_start,
Simon Glass60d71542021-09-25 07:03:16 -0600580 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass4d7237b2021-09-25 07:03:15 -0600581
Simon Glass4d7237b2021-09-25 07:03:15 -0600582 /*
583 * Ensure the image is flushed to memory to handle
584 * AMP boot scenarios in which we might not be
585 * HW cache coherent
586 */
Simon Glass0f781382021-09-25 19:43:35 -0600587 if (IS_ENABLED(CONFIG_MP)) {
588 flush_cache((unsigned long)*initrd_start,
589 ALIGN(rd_len, ARCH_DMA_MINALIGN));
590 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600591 puts("OK\n");
592 }
593 } else {
594 *initrd_start = 0;
595 *initrd_end = 0;
596 }
597 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600598 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600599
600 return 0;
601
602error:
603 return -1;
604}
Simon Glass4d7237b2021-09-25 07:03:15 -0600605
Simon Glassdf00afa2022-09-06 20:26:50 -0600606int boot_get_setup(struct bootm_headers *images, u8 arch,
Simon Glass4d7237b2021-09-25 07:03:15 -0600607 ulong *setup_start, ulong *setup_len)
608{
Simon Glass0f781382021-09-25 19:43:35 -0600609 if (!CONFIG_IS_ENABLED(FIT))
610 return -ENOENT;
611
Simon Glass4d7237b2021-09-25 07:03:15 -0600612 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600613}
614
Simon Glass01080ab2023-11-18 14:05:11 -0700615int boot_get_fpga(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600616{
617 ulong tmp_img_addr, img_data, img_len;
618 void *buf;
619 int conf_noffset;
620 int fit_img_result;
621 const char *uname, *name;
622 int err;
623 int devnum = 0; /* TODO support multi fpga platforms */
624
Simon Glass0f781382021-09-25 19:43:35 -0600625 if (!IS_ENABLED(CONFIG_FPGA))
626 return -ENOSYS;
627
Simon Glass4d7237b2021-09-25 07:03:15 -0600628 /* Check to see if the images struct has a FIT configuration */
629 if (!genimg_has_config(images)) {
630 debug("## FIT configuration was not specified\n");
631 return 0;
632 }
633
634 /*
635 * Obtain the os FIT header from the images struct
636 */
637 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
638 buf = map_sysmem(tmp_img_addr, 0);
639 /*
640 * Check image type. For FIT images get FIT node
641 * and attempt to locate a generic binary.
642 */
643 switch (genimg_get_format(buf)) {
644 case IMAGE_FORMAT_FIT:
645 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
646
647 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
648 NULL);
649 if (!uname) {
650 debug("## FPGA image is not specified\n");
651 return 0;
652 }
653 fit_img_result = fit_image_load(images,
654 tmp_img_addr,
655 (const char **)&uname,
Simon Glass60d71542021-09-25 07:03:16 -0600656 &images->fit_uname_cfg,
Simon Glass01080ab2023-11-18 14:05:11 -0700657 IH_ARCH_DEFAULT,
Simon Glass4d7237b2021-09-25 07:03:15 -0600658 IH_TYPE_FPGA,
659 BOOTSTAGE_ID_FPGA_INIT,
660 FIT_LOAD_OPTIONAL_NON_ZERO,
661 &img_data, &img_len);
662
663 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
664 uname, img_data, img_len);
665
666 if (fit_img_result < 0) {
667 /* Something went wrong! */
668 return fit_img_result;
669 }
670
671 if (!fpga_is_partial_data(devnum, img_len)) {
672 name = "full";
673 err = fpga_loadbitstream(devnum, (char *)img_data,
674 img_len, BIT_FULL);
675 if (err)
676 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300677 img_len, BIT_FULL, 0);
Simon Glass4d7237b2021-09-25 07:03:15 -0600678 } else {
679 name = "partial";
680 err = fpga_loadbitstream(devnum, (char *)img_data,
681 img_len, BIT_PARTIAL);
682 if (err)
683 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300684 img_len, BIT_PARTIAL, 0);
Simon Glass4d7237b2021-09-25 07:03:15 -0600685 }
686
687 if (err)
688 return err;
689
690 printf(" Programming %s bitstream... OK\n", name);
691 break;
692 default:
693 printf("The given image format is not supported (corrupt?)\n");
694 return 1;
695 }
696
697 return 0;
698}
Simon Glass4d7237b2021-09-25 07:03:15 -0600699
Simon Glass60d71542021-09-25 07:03:16 -0600700static void fit_loadable_process(u8 img_type,
Simon Glass4d7237b2021-09-25 07:03:15 -0600701 ulong img_data,
702 ulong img_len)
703{
704 int i;
705 const unsigned int count =
706 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
707 struct fit_loadable_tbl *fit_loadable_handler =
708 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
709 /* For each loadable handler */
710 for (i = 0; i < count; i++, fit_loadable_handler++)
711 /* matching this type */
712 if (fit_loadable_handler->type == img_type)
713 /* call that handler with this image data */
714 fit_loadable_handler->handler(img_data, img_len);
715}
716
Simon Glass4fdc5592023-11-18 14:05:12 -0700717int boot_get_loadable(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600718{
719 /*
720 * These variables are used to hold the current image location
721 * in system memory.
722 */
723 ulong tmp_img_addr;
724 /*
725 * These two variables are requirements for fit_image_load, but
726 * their values are not used
727 */
728 ulong img_data, img_len;
729 void *buf;
730 int loadables_index;
731 int conf_noffset;
732 int fit_img_result;
733 const char *uname;
Simon Glass60d71542021-09-25 07:03:16 -0600734 u8 img_type;
Simon Glass4d7237b2021-09-25 07:03:15 -0600735
736 /* Check to see if the images struct has a FIT configuration */
737 if (!genimg_has_config(images)) {
738 debug("## FIT configuration was not specified\n");
739 return 0;
740 }
741
742 /*
743 * Obtain the os FIT header from the images struct
744 */
745 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
746 buf = map_sysmem(tmp_img_addr, 0);
747 /*
748 * Check image type. For FIT images get FIT node
749 * and attempt to locate a generic binary.
750 */
751 switch (genimg_get_format(buf)) {
752 case IMAGE_FORMAT_FIT:
753 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
754
755 for (loadables_index = 0;
756 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass60d71542021-09-25 07:03:16 -0600757 FIT_LOADABLE_PROP,
758 loadables_index, NULL), uname;
759 loadables_index++) {
760 fit_img_result = fit_image_load(images, tmp_img_addr,
761 &uname,
762 &images->fit_uname_cfg,
Simon Glass4fdc5592023-11-18 14:05:12 -0700763 IH_ARCH_DEFAULT,
764 IH_TYPE_LOADABLE,
Simon Glass60d71542021-09-25 07:03:16 -0600765 BOOTSTAGE_ID_FIT_LOADABLE_START,
766 FIT_LOAD_OPTIONAL_NON_ZERO,
767 &img_data, &img_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600768 if (fit_img_result < 0) {
769 /* Something went wrong! */
770 return fit_img_result;
771 }
772
773 fit_img_result = fit_image_get_node(buf, uname);
774 if (fit_img_result < 0) {
775 /* Something went wrong! */
776 return fit_img_result;
777 }
778 fit_img_result = fit_image_get_type(buf,
779 fit_img_result,
780 &img_type);
781 if (fit_img_result < 0) {
782 /* Something went wrong! */
783 return fit_img_result;
784 }
785
786 fit_loadable_process(img_type, img_data, img_len);
787 }
788 break;
789 default:
790 printf("The given image format is not supported (corrupt?)\n");
791 return 1;
792 }
793
794 return 0;
795}
Simon Glass4d7237b2021-09-25 07:03:15 -0600796
Simon Glass4d7237b2021-09-25 07:03:15 -0600797/**
798 * boot_get_cmdline - allocate and initialize kernel cmdline
799 * @lmb: pointer to lmb handle, will be used for memory mgmt
800 * @cmd_start: pointer to a ulong variable, will hold cmdline start
801 * @cmd_end: pointer to a ulong variable, will hold cmdline end
802 *
Simon Glass7ae16bb2022-08-28 12:32:53 -0600803 * This allocates space for kernel command line below
Simon Glass4d7237b2021-09-25 07:03:15 -0600804 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
805 * variable is present its contents is copied to allocated kernel
806 * command line.
807 *
808 * returns:
809 * 0 - success
810 * -1 - failure
811 */
812int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
813{
Simon Glass7ae16bb2022-08-28 12:32:53 -0600814 int barg;
Simon Glass4d7237b2021-09-25 07:03:15 -0600815 char *cmdline;
816 char *s;
817
Simon Glass7ae16bb2022-08-28 12:32:53 -0600818 /*
819 * Help the compiler detect that this function is only called when
820 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
821 */
822 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
823 return 0;
824
825 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
826 cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
Simon Glass4d7237b2021-09-25 07:03:15 -0600827 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass60d71542021-09-25 07:03:16 -0600828 if (!cmdline)
Simon Glass4d7237b2021-09-25 07:03:15 -0600829 return -1;
830
831 s = env_get("bootargs");
832 if (!s)
833 s = "";
834
835 strcpy(cmdline, s);
836
Simon Glass60d71542021-09-25 07:03:16 -0600837 *cmd_start = (ulong)cmdline;
Simon Glass4d7237b2021-09-25 07:03:15 -0600838 *cmd_end = *cmd_start + strlen(cmdline);
839
840 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
841
842 return 0;
843}
Simon Glass4d7237b2021-09-25 07:03:15 -0600844
Simon Glass4d7237b2021-09-25 07:03:15 -0600845/**
846 * boot_get_kbd - allocate and initialize kernel copy of board info
847 * @lmb: pointer to lmb handle, will be used for memory mgmt
848 * @kbd: double pointer to board info data
849 *
850 * boot_get_kbd() allocates space for kernel copy of board info data below
851 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
852 * with the current u-boot board info data.
853 *
854 * returns:
855 * 0 - success
856 * -1 - failure
857 */
858int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
859{
860 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
861 sizeof(struct bd_info),
862 0xf,
Simon Glass60d71542021-09-25 07:03:16 -0600863 env_get_bootm_mapsize() +
864 env_get_bootm_low());
865 if (!*kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600866 return -1;
867
Simon Glass60d71542021-09-25 07:03:16 -0600868 **kbd = *gd->bd;
Simon Glass4d7237b2021-09-25 07:03:15 -0600869
870 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
871
Simon Glass7ae16bb2022-08-28 12:32:53 -0600872 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
Simon Glassbc0c4db2021-09-25 07:03:20 -0600873 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass4d7237b2021-09-25 07:03:15 -0600874
875 return 0;
876}
Simon Glass4d7237b2021-09-25 07:03:15 -0600877
Simon Glassdf00afa2022-09-06 20:26:50 -0600878int image_setup_linux(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600879{
880 ulong of_size = images->ft_len;
881 char **of_flat_tree = &images->ft_addr;
Simon Glass7ae16bb2022-08-28 12:32:53 -0600882 struct lmb *lmb = images_lmb(images);
Simon Glass4d7237b2021-09-25 07:03:15 -0600883 int ret;
884
Simon Glass7ae16bb2022-08-28 12:32:53 -0600885 /* This function cannot be called without lmb support */
Simon Glassae7ed572023-02-05 15:40:13 -0700886 if (!IS_ENABLED(CONFIG_LMB))
Simon Glass7ae16bb2022-08-28 12:32:53 -0600887 return -EFAULT;
Simon Glass85c057e2021-09-25 19:43:21 -0600888 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass4d7237b2021-09-25 07:03:15 -0600889 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
890
Simon Glassb8eb1fe2021-09-25 19:43:25 -0600891 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600892 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass60d71542021-09-25 07:03:16 -0600893 &images->cmdline_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600894 if (ret) {
895 puts("ERROR with allocation of cmdline\n");
896 return ret;
897 }
898 }
899
Simon Glass85c057e2021-09-25 19:43:21 -0600900 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600901 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
902 if (ret)
903 return ret;
904 }
905
Simon Glass85c057e2021-09-25 19:43:21 -0600906 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass30762dd2023-11-12 08:27:44 -0700907 ret = image_setup_libfdt(images, *of_flat_tree, lmb);
Simon Glass4d7237b2021-09-25 07:03:15 -0600908 if (ret)
909 return ret;
910 }
911
912 return 0;
913}
Simon Glass4a8a8a12021-09-25 07:03:17 -0600914
915void genimg_print_size(uint32_t size)
916{
917 printf("%d Bytes = ", size);
918 print_size(size, "\n");
919}
920
921void genimg_print_time(time_t timestamp)
922{
923 struct rtc_time tm;
924
925 rtc_to_tm(timestamp, &tm);
926 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
927 tm.tm_year, tm.tm_mon, tm.tm_mday,
928 tm.tm_hour, tm.tm_min, tm.tm_sec);
929}
Simon Glassdaee3ba2023-01-06 08:52:28 -0600930
931/**
932 * get_default_image() - Return default property from /images
933 *
934 * Return: Pointer to value of default property (or NULL)
935 */
936static const char *get_default_image(const void *fit)
937{
938 int images_noffset;
939
940 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
941 if (images_noffset < 0)
942 return NULL;
943
944 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
945}
946
947int image_locate_script(void *buf, int size, const char *fit_uname,
948 const char *confname, char **datap, uint *lenp)
949{
950 const struct legacy_img_hdr *hdr;
951 const void *fit_data;
952 const void *fit_hdr;
953 size_t fit_len;
954 int noffset;
955 int verify;
956 ulong len;
957 u32 *data;
958
959 verify = env_get_yesno("verify");
960
961 switch (genimg_get_format(buf)) {
962 case IMAGE_FORMAT_LEGACY:
Marek Vasutfb59eca2023-02-27 20:56:31 +0100963 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
964 goto exit_image_format;
965 } else {
Simon Glassdaee3ba2023-01-06 08:52:28 -0600966 hdr = buf;
967
968 if (!image_check_magic(hdr)) {
969 puts("Bad magic number\n");
970 return 1;
971 }
972
973 if (!image_check_hcrc(hdr)) {
974 puts("Bad header crc\n");
975 return 1;
976 }
977
978 if (verify) {
979 if (!image_check_dcrc(hdr)) {
980 puts("Bad data crc\n");
981 return 1;
982 }
983 }
984
985 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
986 puts("Bad image type\n");
987 return 1;
988 }
989
990 /* get length of script */
991 data = (u32 *)image_get_data(hdr);
992
993 len = uimage_to_cpu(*data);
994 if (!len) {
995 puts("Empty Script\n");
996 return 1;
997 }
998
999 /*
1000 * scripts are just multi-image files with one
1001 * component, so seek past the zero-terminated sequence
1002 * of image lengths to get to the actual image data
1003 */
1004 while (*data++);
1005 }
1006 break;
1007 case IMAGE_FORMAT_FIT:
Marek Vasutfb59eca2023-02-27 20:56:31 +01001008 if (!IS_ENABLED(CONFIG_FIT)) {
1009 goto exit_image_format;
1010 } else {
Simon Glassdaee3ba2023-01-06 08:52:28 -06001011 fit_hdr = buf;
1012 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1013 puts("Bad FIT image format\n");
1014 return 1;
1015 }
1016
1017 if (!fit_uname) {
1018 /* If confname is empty, use the default */
1019 if (confname && *confname)
1020 noffset = fit_conf_get_node(fit_hdr, confname);
1021 else
1022 noffset = fit_conf_get_node(fit_hdr, NULL);
1023 if (noffset < 0) {
1024 if (!confname)
1025 goto fallback;
1026 printf("Could not find config %s\n", confname);
1027 return 1;
1028 }
1029
1030 if (verify && fit_config_verify(fit_hdr, noffset))
1031 return 1;
1032
1033 noffset = fit_conf_get_prop_node(fit_hdr,
1034 noffset,
1035 FIT_SCRIPT_PROP,
1036 IH_PHASE_NONE);
1037 if (noffset < 0) {
1038 if (!confname)
1039 goto fallback;
1040 printf("Could not find script in %s\n", confname);
1041 return 1;
1042 }
1043 } else {
1044fallback:
1045 if (!fit_uname || !*fit_uname)
1046 fit_uname = get_default_image(fit_hdr);
1047 if (!fit_uname) {
1048 puts("No FIT subimage unit name\n");
1049 return 1;
1050 }
1051
1052 /* get script component image node offset */
1053 noffset = fit_image_get_node(fit_hdr, fit_uname);
1054 if (noffset < 0) {
1055 printf("Can't find '%s' FIT subimage\n",
1056 fit_uname);
1057 return 1;
1058 }
1059 }
1060
1061 if (!fit_image_check_type(fit_hdr, noffset,
1062 IH_TYPE_SCRIPT)) {
1063 puts("Not a image image\n");
1064 return 1;
1065 }
1066
1067 /* verify integrity */
1068 if (verify && !fit_image_verify(fit_hdr, noffset)) {
1069 puts("Bad Data Hash\n");
1070 return 1;
1071 }
1072
1073 /* get script subimage data address and length */
Tobias Waldekranz317dbfc2023-02-16 16:33:47 +01001074 if (fit_image_get_data_and_size(fit_hdr, noffset,
1075 &fit_data, &fit_len)) {
Simon Glassdaee3ba2023-01-06 08:52:28 -06001076 puts("Could not find script subimage data\n");
1077 return 1;
1078 }
1079
1080 data = (u32 *)fit_data;
1081 len = (ulong)fit_len;
1082 }
1083 break;
1084 default:
Marek Vasutfb59eca2023-02-27 20:56:31 +01001085 goto exit_image_format;
Simon Glassdaee3ba2023-01-06 08:52:28 -06001086 }
1087
1088 *datap = (char *)data;
1089 *lenp = len;
1090
1091 return 0;
Marek Vasutfb59eca2023-02-27 20:56:31 +01001092
1093exit_image_format:
1094 puts("Wrong image format for \"source\" command\n");
1095 return -EPERM;
Simon Glassdaee3ba2023-01-06 08:52:28 -06001096}