blob: b726bd6b30358a4c676c35a18bd18032027c26f3 [file] [log] [blame]
Simon Glass4d7237b2021-09-25 07:03:15 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Image code used by boards (and not host tools)
4 *
5 * (C) Copyright 2008 Semihalf
6 *
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 */
10
Tom Rinidec7ea02024-05-20 13:35:03 -060011#include <config.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060012#include <bootstage.h>
13#include <cpu_func.h>
Simon Glass1ab16922022-07-31 12:28:48 -060014#include <display_options.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060015#include <env.h>
16#include <fpga.h>
17#include <image.h>
Andy Shevchenkoe3b3ad92021-11-08 21:03:38 +030018#include <init.h>
Simon Glass7ae16bb2022-08-28 12:32:53 -060019#include <log.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060020#include <mapmem.h>
Simon Glass4a8a8a12021-09-25 07:03:17 -060021#include <rtc.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060022#include <watchdog.h>
23#include <asm/cache.h>
24#include <asm/global_data.h>
25
Simon Glass4d7237b2021-09-25 07:03:15 -060026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass4d7237b2021-09-25 07:03:15 -060028/**
29 * image_get_ramdisk - get and verify ramdisk image
30 * @rd_addr: ramdisk image start address
31 * @arch: expected ramdisk architecture
32 * @verify: checksum verification flag
33 *
34 * image_get_ramdisk() returns a pointer to the verified ramdisk image
35 * header. Routine receives image start address and expected architecture
36 * flag. Verification done covers data and header integrity and os/type/arch
37 * fields checking.
38 *
39 * returns:
40 * pointer to a ramdisk image header, if image was found and valid
41 * otherwise, return NULL
42 */
Simon Glassbb7d3bb2022-09-06 20:26:52 -060043static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
44 int verify)
Simon Glass4d7237b2021-09-25 07:03:15 -060045{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060046 const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -060047
48 if (!image_check_magic(rd_hdr)) {
49 puts("Bad Magic Number\n");
50 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
51 return NULL;
52 }
53
54 if (!image_check_hcrc(rd_hdr)) {
55 puts("Bad Header Checksum\n");
56 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
57 return NULL;
58 }
59
60 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
61 image_print_contents(rd_hdr);
62
63 if (verify) {
64 puts(" Verifying Checksum ... ");
65 if (!image_check_dcrc(rd_hdr)) {
66 puts("Bad Data CRC\n");
67 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
68 return NULL;
69 }
70 puts("OK\n");
71 }
72
73 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
74
75 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
76 !image_check_arch(rd_hdr, arch) ||
77 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
78 printf("No Linux %s Ramdisk Image\n",
Simon Glass60d71542021-09-25 07:03:16 -060079 genimg_get_arch_name(arch));
Simon Glass4d7237b2021-09-25 07:03:15 -060080 bootstage_error(BOOTSTAGE_ID_RAMDISK);
81 return NULL;
82 }
83
84 return rd_hdr;
85}
Simon Glass4d7237b2021-09-25 07:03:15 -060086
87/*****************************************************************************/
88/* Shared dual-format routines */
89/*****************************************************************************/
90ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
91ulong image_save_addr; /* Default Save Address */
92ulong image_save_size; /* Default Save Size (in bytes) */
93
94static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass60d71542021-09-25 07:03:16 -060095 int flags)
Simon Glass4d7237b2021-09-25 07:03:15 -060096{
97 switch (op) {
98 case env_op_create:
99 case env_op_overwrite:
100 image_load_addr = hextoul(value, NULL);
101 break;
102 default:
103 break;
104 }
105
106 return 0;
107}
108U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
Marek Vasutf816c422024-03-26 23:13:11 +0100110phys_addr_t env_get_bootm_low(void)
Simon Glass4d7237b2021-09-25 07:03:15 -0600111{
112 char *s = env_get("bootm_low");
Simon Glass60d71542021-09-25 07:03:16 -0600113
Marek Vasutf816c422024-03-26 23:13:11 +0100114 if (s)
115 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600116
Tom Rinibb4dd962022-11-16 13:10:37 -0500117#if defined(CFG_SYS_SDRAM_BASE)
118 return CFG_SYS_SDRAM_BASE;
Simon Glass4d7237b2021-09-25 07:03:15 -0600119#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
120 return gd->bd->bi_dram[0].start;
121#else
122 return 0;
123#endif
124}
125
126phys_size_t env_get_bootm_size(void)
127{
Marek Vasutb140bbd2024-03-26 23:13:13 +0100128 phys_addr_t start, low;
129 phys_size_t size;
Simon Glass4d7237b2021-09-25 07:03:15 -0600130 char *s = env_get("bootm_size");
Simon Glass60d71542021-09-25 07:03:16 -0600131
Marek Vasut1d02ed72024-03-26 23:13:12 +0100132 if (s)
133 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600134
135 start = gd->ram_base;
136 size = gd->ram_size;
137
138 if (start + size > gd->ram_top)
139 size = gd->ram_top - start;
140
141 s = env_get("bootm_low");
142 if (s)
Marek Vasutb140bbd2024-03-26 23:13:13 +0100143 low = simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600144 else
Marek Vasutb140bbd2024-03-26 23:13:13 +0100145 low = start;
Simon Glass4d7237b2021-09-25 07:03:15 -0600146
Marek Vasutb140bbd2024-03-26 23:13:13 +0100147 return size - (low - start);
Simon Glass4d7237b2021-09-25 07:03:15 -0600148}
149
150phys_size_t env_get_bootm_mapsize(void)
151{
Simon Glass4d7237b2021-09-25 07:03:15 -0600152 char *s = env_get("bootm_mapsize");
Simon Glass60d71542021-09-25 07:03:16 -0600153
Marek Vasutddf73a82024-03-26 23:13:14 +0100154 if (s)
155 return simple_strtoull(s, NULL, 16);
Simon Glass4d7237b2021-09-25 07:03:15 -0600156
Tom Rini6a5dccc2022-11-16 13:10:41 -0500157#if defined(CFG_SYS_BOOTMAPSZ)
158 return CFG_SYS_BOOTMAPSZ;
Simon Glass4d7237b2021-09-25 07:03:15 -0600159#else
160 return env_get_bootm_size();
161#endif
162}
163
164void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
165{
166 if (to == from)
167 return;
168
Simon Glass7ae16bb2022-08-28 12:32:53 -0600169 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600170 if (to > from) {
Simon Glass7ae16bb2022-08-28 12:32:53 -0600171 from += len;
172 to += len;
Simon Glass4d7237b2021-09-25 07:03:15 -0600173 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600174 while (len > 0) {
175 size_t tail = (len > chunksz) ? chunksz : len;
176
Stefan Roese80877fa2022-09-02 14:10:46 +0200177 schedule();
Simon Glass7ae16bb2022-08-28 12:32:53 -0600178 if (to > from) {
179 to -= tail;
180 from -= tail;
181 }
182 memmove(to, from, tail);
183 if (to < from) {
184 to += tail;
185 from += tail;
186 }
187 len -= tail;
Simon Glass4d7237b2021-09-25 07:03:15 -0600188 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600189 } else {
190 memmove(to, from, len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600191 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600192}
193
Simon Glass4e8ec472023-11-18 14:04:57 -0700194ulong genimg_get_kernel_addr_fit(const char *const img_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600195 const char **fit_uname_config,
196 const char **fit_uname_kernel)
Simon Glass4d7237b2021-09-25 07:03:15 -0600197{
198 ulong kernel_addr;
199
200 /* find out kernel image address */
201 if (!img_addr) {
202 kernel_addr = image_load_addr;
203 debug("* kernel: default image load address = 0x%08lx\n",
204 image_load_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600205 } else if (CONFIG_IS_ENABLED(FIT) &&
206 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass4d7237b2021-09-25 07:03:15 -0600207 fit_uname_config)) {
208 debug("* kernel: config '%s' from image at 0x%08lx\n",
209 *fit_uname_config, kernel_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600210 } else if (CONFIG_IS_ENABLED(FIT) &&
211 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
212 fit_uname_kernel)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600213 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
214 *fit_uname_kernel, kernel_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600215 } else {
216 kernel_addr = hextoul(img_addr, NULL);
217 debug("* kernel: cmdline image address = 0x%08lx\n",
218 kernel_addr);
219 }
220
221 return kernel_addr;
222}
223
224/**
225 * genimg_get_kernel_addr() is the simple version of
226 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
227 */
228ulong genimg_get_kernel_addr(char * const img_addr)
229{
230 const char *fit_uname_config = NULL;
231 const char *fit_uname_kernel = NULL;
232
233 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
234 &fit_uname_kernel);
235}
236
237/**
238 * genimg_get_format - get image format type
239 * @img_addr: image start address
240 *
241 * genimg_get_format() checks whether provided address points to a valid
242 * legacy or FIT image.
243 *
244 * New uImage format and FDT blob are based on a libfdt. FDT blob
245 * may be passed directly or embedded in a FIT image. In both situations
246 * genimg_get_format() must be able to dectect libfdt header.
247 *
248 * returns:
249 * image format type or IMAGE_FORMAT_INVALID if no image is present
250 */
251int genimg_get_format(const void *img_addr)
252{
Simon Glass0f781382021-09-25 19:43:35 -0600253 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600254 const struct legacy_img_hdr *hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600255
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600256 hdr = (const struct legacy_img_hdr *)img_addr;
Simon Glass0f781382021-09-25 19:43:35 -0600257 if (image_check_magic(hdr))
258 return IMAGE_FORMAT_LEGACY;
259 }
260 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
261 if (!fdt_check_header(img_addr))
262 return IMAGE_FORMAT_FIT;
263 }
264 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
Safae Ouajih88ad0c12023-02-06 00:50:05 +0100265 is_android_boot_image_header(img_addr))
Simon Glass4d7237b2021-09-25 07:03:15 -0600266 return IMAGE_FORMAT_ANDROID;
Simon Glass4d7237b2021-09-25 07:03:15 -0600267
268 return IMAGE_FORMAT_INVALID;
269}
270
271/**
272 * fit_has_config - check if there is a valid FIT configuration
273 * @images: pointer to the bootm command headers structure
274 *
275 * fit_has_config() checks if there is a FIT configuration in use
276 * (if FTI support is present).
277 *
278 * returns:
279 * 0, no FIT support or no configuration found
280 * 1, configuration found
281 */
Simon Glassdf00afa2022-09-06 20:26:50 -0600282int genimg_has_config(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600283{
Simon Glass0f781382021-09-25 19:43:35 -0600284 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass4d7237b2021-09-25 07:03:15 -0600285 return 1;
Simon Glass0f781382021-09-25 19:43:35 -0600286
Simon Glass4d7237b2021-09-25 07:03:15 -0600287 return 0;
288}
289
290/**
Simon Glassccda2fa2021-09-25 19:43:37 -0600291 * select_ramdisk() - Select and locate the ramdisk to use
292 *
Simon Glass4d7237b2021-09-25 07:03:15 -0600293 * @images: pointer to the bootm images structure
Simon Glass664696f2022-08-28 12:32:47 -0600294 * @select: name of ramdisk to select, or hex address, NULL for any
Simon Glass4d7237b2021-09-25 07:03:15 -0600295 * @arch: expected ramdisk architecture
Simon Glassccda2fa2021-09-25 19:43:37 -0600296 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
297 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100298 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glassccda2fa2021-09-25 19:43:37 -0600299 * other -ve value on other error
Simon Glass4d7237b2021-09-25 07:03:15 -0600300 */
Simon Glassdf00afa2022-09-06 20:26:50 -0600301static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
Simon Glassccda2fa2021-09-25 19:43:37 -0600302 ulong *rd_datap, ulong *rd_lenp)
Simon Glass4d7237b2021-09-25 07:03:15 -0600303{
Simon Glassf2d23de2022-08-28 12:32:49 -0600304 const char *fit_uname_config;
305 const char *fit_uname_ramdisk;
Simon Glass973b8932022-08-28 12:32:51 -0600306 bool done_select = !select;
Simon Glass664696f2022-08-28 12:32:47 -0600307 bool done = false;
Simon Glassf2d23de2022-08-28 12:32:49 -0600308 int rd_noffset;
Tom Rini1ca3c642023-04-05 19:48:56 -0400309 ulong rd_addr = 0;
Simon Glassccda2fa2021-09-25 19:43:37 -0600310 char *buf;
Simon Glass2de5b012021-09-25 19:43:36 -0600311
Simon Glass973b8932022-08-28 12:32:51 -0600312 if (CONFIG_IS_ENABLED(FIT)) {
Simon Glassf2d23de2022-08-28 12:32:49 -0600313 fit_uname_config = images->fit_uname_cfg;
314 fit_uname_ramdisk = NULL;
Simon Glass2de5b012021-09-25 19:43:36 -0600315
Tom Rinibc7d9d22021-12-20 09:36:32 -0500316 if (select) {
317 ulong default_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600318 /*
319 * If the init ramdisk comes from the FIT image and
320 * the FIT image address is omitted in the command
321 * line argument, try to use os FIT image address or
322 * default load address.
323 */
324 if (images->fit_uname_os)
325 default_addr = (ulong)images->fit_hdr_os;
326 else
327 default_addr = image_load_addr;
328
Simon Glass92c586f2022-08-28 12:32:52 -0600329 if (fit_parse_conf(select, default_addr, &rd_addr,
330 &fit_uname_config)) {
Simon Glass60d71542021-09-25 07:03:16 -0600331 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
332 fit_uname_config, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600333 done_select = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600334 } else if (fit_parse_subimage(select, default_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600335 &rd_addr,
336 &fit_uname_ramdisk)) {
337 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
338 fit_uname_ramdisk, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600339 done_select = true;
340 }
341 }
342 }
343 if (!done_select) {
Simon Glass92c586f2022-08-28 12:32:52 -0600344 rd_addr = hextoul(select, NULL);
345 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600346 }
Simon Glass92c586f2022-08-28 12:32:52 -0600347 if (CONFIG_IS_ENABLED(FIT) && !select) {
348 /* use FIT configuration provided in first bootm
349 * command argument. If the property is not defined,
350 * quit silently (with -ENOPKG)
Tom Rinibc7d9d22021-12-20 09:36:32 -0500351 */
Simon Glass92c586f2022-08-28 12:32:52 -0600352 rd_addr = map_to_sysmem(images->fit_hdr_os);
353 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
354 rd_addr);
355 if (rd_noffset == -ENOENT)
356 return -ENOPKG;
357 else if (rd_noffset < 0)
358 return rd_noffset;
359 }
Simon Glass2de5b012021-09-25 19:43:36 -0600360
Simon Glass92c586f2022-08-28 12:32:52 -0600361 /*
362 * Check if there is an initrd image at the
363 * address provided in the second bootm argument
364 * check image type, for FIT images get FIT node.
365 */
366 buf = map_sysmem(rd_addr, 0);
367 switch (genimg_get_format(buf)) {
368 case IMAGE_FORMAT_LEGACY:
369 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600370 const struct legacy_img_hdr *rd_hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600371
Simon Glass92c586f2022-08-28 12:32:52 -0600372 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
373 rd_addr);
Tom Rinibc7d9d22021-12-20 09:36:32 -0500374
Simon Glass92c586f2022-08-28 12:32:52 -0600375 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
376 rd_hdr = image_get_ramdisk(rd_addr, arch,
377 images->verify);
Simon Glass4d7237b2021-09-25 07:03:15 -0600378
Simon Glass92c586f2022-08-28 12:32:52 -0600379 if (!rd_hdr)
380 return -ENOENT;
Simon Glass4d7237b2021-09-25 07:03:15 -0600381
Simon Glass92c586f2022-08-28 12:32:52 -0600382 *rd_datap = image_get_data(rd_hdr);
383 *rd_lenp = image_get_data_size(rd_hdr);
384 done = true;
385 }
386 break;
387 case IMAGE_FORMAT_FIT:
388 if (CONFIG_IS_ENABLED(FIT)) {
389 rd_noffset = fit_image_load(images, rd_addr,
390 &fit_uname_ramdisk,
391 &fit_uname_config,
392 arch, IH_TYPE_RAMDISK,
393 BOOTSTAGE_ID_FIT_RD_START,
394 FIT_LOAD_OPTIONAL_NON_ZERO,
395 rd_datap, rd_lenp);
396 if (rd_noffset < 0)
397 return rd_noffset;
Simon Glassdc0aa042022-08-28 12:32:46 -0600398
Simon Glass92c586f2022-08-28 12:32:52 -0600399 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
400 images->fit_uname_rd = fit_uname_ramdisk;
401 images->fit_noffset_rd = rd_noffset;
402 done = true;
Simon Glass664696f2022-08-28 12:32:47 -0600403 }
Simon Glass92c586f2022-08-28 12:32:52 -0600404 break;
405 case IMAGE_FORMAT_ANDROID:
406 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass92c586f2022-08-28 12:32:52 -0600407 int ret;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100408 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000409 ulong boot_img = get_abootimg_addr();
410 ulong init_boot_img = get_ainit_bootimg_addr();
Safae Ouajih51c981b2023-02-06 00:50:17 +0100411 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000412 void *ramdisk_img;
Safae Ouajih51c981b2023-02-06 00:50:17 +0100413
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000414 if (init_boot_img == -1)
415 ramdisk_img = map_sysmem(boot_img, 0);
416 else
417 ramdisk_img = map_sysmem(init_boot_img, 0);
418
419 ret = android_image_get_ramdisk(ramdisk_img, vendor_boot_img,
Safae Ouajih51c981b2023-02-06 00:50:17 +0100420 rd_datap, rd_lenp);
421 unmap_sysmem(vendor_boot_img);
Roman Stratiienko826e0be2024-05-23 07:06:07 +0000422 unmap_sysmem(ramdisk_img);
Safae Ouajih51c981b2023-02-06 00:50:17 +0100423 } 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
Michael Walle955415b2024-07-29 23:36:57 +0200430 if (ret == -ENOENT)
431 return -ENOPKG;
432 else if (ret)
Simon Glass92c586f2022-08-28 12:32:52 -0600433 return ret;
434 done = true;
435 }
436 break;
437 }
Simon Glass664696f2022-08-28 12:32:47 -0600438
439 if (!done) {
440 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
441 char *end = NULL;
Simon Glass0f781382021-09-25 19:43:35 -0600442
Simon Glass664696f2022-08-28 12:32:47 -0600443 if (select)
444 end = strchr(select, ':');
445 if (end) {
446 *rd_lenp = hextoul(++end, NULL);
447 *rd_datap = rd_addr;
448 done = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600449 }
Simon Glass664696f2022-08-28 12:32:47 -0600450 }
451
452 if (!done) {
Simon Glass0f781382021-09-25 19:43:35 -0600453 puts("Wrong Ramdisk Image Format\n");
Simon Glassccda2fa2021-09-25 19:43:37 -0600454 return -EINVAL;
Simon Glass4d7237b2021-09-25 07:03:15 -0600455 }
Simon Glass664696f2022-08-28 12:32:47 -0600456 }
Simon Glassccda2fa2021-09-25 19:43:37 -0600457
458 return 0;
459}
460
Simon Glass8eef77e2023-11-18 14:05:06 -0700461int boot_get_ramdisk(char const *select, struct bootm_headers *images,
462 uint arch, ulong *rd_start, ulong *rd_end)
Simon Glassccda2fa2021-09-25 19:43:37 -0600463{
464 ulong rd_data, rd_len;
Simon Glassccda2fa2021-09-25 19:43:37 -0600465
466 *rd_start = 0;
467 *rd_end = 0;
468
Simon Glassccda2fa2021-09-25 19:43:37 -0600469 /*
470 * Look for a '-' which indicates to ignore the
471 * ramdisk argument
472 */
473 if (select && strcmp(select, "-") == 0) {
474 debug("## Skipping init Ramdisk\n");
475 rd_len = 0;
476 rd_data = 0;
477 } else if (select || genimg_has_config(images)) {
478 int ret;
479
480 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
481 if (ret == -ENOPKG)
482 return 0;
483 else if (ret)
484 return ret;
Simon Glass4d7237b2021-09-25 07:03:15 -0600485 } else if (images->legacy_hdr_valid &&
486 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass60d71542021-09-25 07:03:16 -0600487 IH_TYPE_MULTI)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600488 /*
489 * Now check if we have a legacy mult-component image,
490 * get second entry data start address and len.
491 */
492 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600493 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
494 (ulong)images->legacy_hdr_os);
Simon Glass4d7237b2021-09-25 07:03:15 -0600495
496 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
497 } else {
498 /*
499 * no initrd image
500 */
501 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600502 rd_len = 0;
503 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600504 }
505
506 if (!rd_data) {
507 debug("## No init Ramdisk\n");
508 } else {
509 *rd_start = rd_data;
510 *rd_end = rd_data + rd_len;
511 }
512 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600513 *rd_start, *rd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600514
515 return 0;
516}
517
Simon Glass4d7237b2021-09-25 07:03:15 -0600518/**
519 * boot_ramdisk_high - relocate init ramdisk
Simon Glass4d7237b2021-09-25 07:03:15 -0600520 * @rd_data: ramdisk data start address
521 * @rd_len: ramdisk data length
522 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
523 * start address (after possible relocation)
524 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
525 * end address (after possible relocation)
526 *
527 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
528 * variable and if requested ramdisk data is moved to a specified location.
529 *
530 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
531 * start/end addresses if ramdisk image start and len were provided,
532 * otherwise set initrd_start and initrd_end set to zeros.
533 *
534 * returns:
535 * 0 - success
536 * -1 - failure
537 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530538int boot_ramdisk_high(ulong rd_data, ulong rd_len, ulong *initrd_start,
539 ulong *initrd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600540{
541 char *s;
Marek Vasutf816c422024-03-26 23:13:11 +0100542 phys_addr_t initrd_high;
Simon Glass4d7237b2021-09-25 07:03:15 -0600543 int initrd_copy_to_ram = 1;
544
545 s = env_get("initrd_high");
546 if (s) {
547 /* a value of "no" or a similar string will act like 0,
548 * turning the "load high" feature off. This is intentional.
549 */
550 initrd_high = hextoul(s, NULL);
551 if (initrd_high == ~0)
552 initrd_copy_to_ram = 0;
553 } else {
554 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
555 }
556
Marek Vasutf816c422024-03-26 23:13:11 +0100557 debug("## initrd_high = 0x%llx, copy_to_ram = %d\n",
558 (u64)initrd_high, initrd_copy_to_ram);
Simon Glass4d7237b2021-09-25 07:03:15 -0600559
560 if (rd_data) {
561 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
562 debug(" in-place initrd\n");
563 *initrd_start = rd_data;
564 *initrd_end = rd_data + rd_len;
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530565 lmb_reserve(rd_data, rd_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600566 } else {
567 if (initrd_high)
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530568 *initrd_start = (ulong)lmb_alloc_base(rd_len,
569 0x1000,
570 initrd_high);
Simon Glass4d7237b2021-09-25 07:03:15 -0600571 else
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530572 *initrd_start = (ulong)lmb_alloc(rd_len,
Simon Glass4d7237b2021-09-25 07:03:15 -0600573 0x1000);
574
575 if (*initrd_start == 0) {
576 puts("ramdisk - allocation error\n");
577 goto error;
578 }
579 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
580
581 *initrd_end = *initrd_start + rd_len;
582 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass60d71542021-09-25 07:03:16 -0600583 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600584
585 memmove_wd((void *)*initrd_start,
Simon Glass60d71542021-09-25 07:03:16 -0600586 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass4d7237b2021-09-25 07:03:15 -0600587
Simon Glass4d7237b2021-09-25 07:03:15 -0600588 /*
589 * Ensure the image is flushed to memory to handle
590 * AMP boot scenarios in which we might not be
591 * HW cache coherent
592 */
Simon Glass0f781382021-09-25 19:43:35 -0600593 if (IS_ENABLED(CONFIG_MP)) {
594 flush_cache((unsigned long)*initrd_start,
595 ALIGN(rd_len, ARCH_DMA_MINALIGN));
596 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600597 puts("OK\n");
598 }
599 } else {
600 *initrd_start = 0;
601 *initrd_end = 0;
602 }
603 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600604 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600605
606 return 0;
607
608error:
609 return -1;
610}
Simon Glass4d7237b2021-09-25 07:03:15 -0600611
Simon Glassdf00afa2022-09-06 20:26:50 -0600612int boot_get_setup(struct bootm_headers *images, u8 arch,
Simon Glass4d7237b2021-09-25 07:03:15 -0600613 ulong *setup_start, ulong *setup_len)
614{
Simon Glass0f781382021-09-25 19:43:35 -0600615 if (!CONFIG_IS_ENABLED(FIT))
616 return -ENOENT;
617
Simon Glass4d7237b2021-09-25 07:03:15 -0600618 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600619}
620
Simon Glass01080ab2023-11-18 14:05:11 -0700621int boot_get_fpga(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600622{
623 ulong tmp_img_addr, img_data, img_len;
624 void *buf;
625 int conf_noffset;
626 int fit_img_result;
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100627 const char *uname, *name, *compatible;
Simon Glass4d7237b2021-09-25 07:03:15 -0600628 int err;
629 int devnum = 0; /* TODO support multi fpga platforms */
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100630 int flags = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600631
Simon Glass0f781382021-09-25 19:43:35 -0600632 if (!IS_ENABLED(CONFIG_FPGA))
633 return -ENOSYS;
634
Simon Glass4d7237b2021-09-25 07:03:15 -0600635 /* Check to see if the images struct has a FIT configuration */
636 if (!genimg_has_config(images)) {
637 debug("## FIT configuration was not specified\n");
638 return 0;
639 }
640
641 /*
642 * Obtain the os FIT header from the images struct
643 */
644 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
645 buf = map_sysmem(tmp_img_addr, 0);
646 /*
647 * Check image type. For FIT images get FIT node
648 * and attempt to locate a generic binary.
649 */
650 switch (genimg_get_format(buf)) {
651 case IMAGE_FORMAT_FIT:
652 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
653
654 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
655 NULL);
656 if (!uname) {
657 debug("## FPGA image is not specified\n");
658 return 0;
659 }
660 fit_img_result = fit_image_load(images,
661 tmp_img_addr,
662 (const char **)&uname,
Simon Glass60d71542021-09-25 07:03:16 -0600663 &images->fit_uname_cfg,
Simon Glass01080ab2023-11-18 14:05:11 -0700664 IH_ARCH_DEFAULT,
Simon Glass4d7237b2021-09-25 07:03:15 -0600665 IH_TYPE_FPGA,
666 BOOTSTAGE_ID_FPGA_INIT,
667 FIT_LOAD_OPTIONAL_NON_ZERO,
668 &img_data, &img_len);
669
670 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
671 uname, img_data, img_len);
672
673 if (fit_img_result < 0) {
674 /* Something went wrong! */
675 return fit_img_result;
676 }
677
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100678 conf_noffset = fit_image_get_node(buf, uname);
679 compatible = fdt_getprop(buf, conf_noffset, "compatible", NULL);
680 if (!compatible) {
681 printf("'fpga' image without 'compatible' property\n");
682 } else {
683 if (CONFIG_IS_ENABLED(FPGA_LOAD_SECURE))
684 flags = fpga_compatible2flag(devnum, compatible);
685 }
686
Simon Glass4d7237b2021-09-25 07:03:15 -0600687 if (!fpga_is_partial_data(devnum, img_len)) {
688 name = "full";
689 err = fpga_loadbitstream(devnum, (char *)img_data,
690 img_len, BIT_FULL);
691 if (err)
692 err = fpga_load(devnum, (const void *)img_data,
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100693 img_len, BIT_FULL, flags);
Simon Glass4d7237b2021-09-25 07:03:15 -0600694 } else {
695 name = "partial";
696 err = fpga_loadbitstream(devnum, (char *)img_data,
697 img_len, BIT_PARTIAL);
698 if (err)
699 err = fpga_load(devnum, (const void *)img_data,
Peter Korsgaard8620fc02024-11-05 17:21:36 +0100700 img_len, BIT_PARTIAL, flags);
Simon Glass4d7237b2021-09-25 07:03:15 -0600701 }
702
703 if (err)
704 return err;
705
706 printf(" Programming %s bitstream... OK\n", name);
707 break;
708 default:
709 printf("The given image format is not supported (corrupt?)\n");
710 return 1;
711 }
712
713 return 0;
714}
Simon Glass4d7237b2021-09-25 07:03:15 -0600715
Simon Glass60d71542021-09-25 07:03:16 -0600716static void fit_loadable_process(u8 img_type,
Simon Glass4d7237b2021-09-25 07:03:15 -0600717 ulong img_data,
718 ulong img_len)
719{
720 int i;
721 const unsigned int count =
722 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
723 struct fit_loadable_tbl *fit_loadable_handler =
724 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
725 /* For each loadable handler */
726 for (i = 0; i < count; i++, fit_loadable_handler++)
727 /* matching this type */
728 if (fit_loadable_handler->type == img_type)
729 /* call that handler with this image data */
730 fit_loadable_handler->handler(img_data, img_len);
731}
732
Simon Glass4fdc5592023-11-18 14:05:12 -0700733int boot_get_loadable(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600734{
735 /*
736 * These variables are used to hold the current image location
737 * in system memory.
738 */
739 ulong tmp_img_addr;
740 /*
741 * These two variables are requirements for fit_image_load, but
742 * their values are not used
743 */
744 ulong img_data, img_len;
745 void *buf;
746 int loadables_index;
747 int conf_noffset;
748 int fit_img_result;
749 const char *uname;
Simon Glass60d71542021-09-25 07:03:16 -0600750 u8 img_type;
Simon Glass4d7237b2021-09-25 07:03:15 -0600751
752 /* Check to see if the images struct has a FIT configuration */
753 if (!genimg_has_config(images)) {
754 debug("## FIT configuration was not specified\n");
755 return 0;
756 }
757
758 /*
759 * Obtain the os FIT header from the images struct
760 */
761 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
762 buf = map_sysmem(tmp_img_addr, 0);
763 /*
764 * Check image type. For FIT images get FIT node
765 * and attempt to locate a generic binary.
766 */
767 switch (genimg_get_format(buf)) {
768 case IMAGE_FORMAT_FIT:
769 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
770
771 for (loadables_index = 0;
772 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass60d71542021-09-25 07:03:16 -0600773 FIT_LOADABLE_PROP,
774 loadables_index, NULL), uname;
775 loadables_index++) {
776 fit_img_result = fit_image_load(images, tmp_img_addr,
777 &uname,
778 &images->fit_uname_cfg,
Simon Glass4fdc5592023-11-18 14:05:12 -0700779 IH_ARCH_DEFAULT,
780 IH_TYPE_LOADABLE,
Simon Glass60d71542021-09-25 07:03:16 -0600781 BOOTSTAGE_ID_FIT_LOADABLE_START,
782 FIT_LOAD_OPTIONAL_NON_ZERO,
783 &img_data, &img_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600784 if (fit_img_result < 0) {
785 /* Something went wrong! */
786 return fit_img_result;
787 }
788
789 fit_img_result = fit_image_get_node(buf, uname);
790 if (fit_img_result < 0) {
791 /* Something went wrong! */
792 return fit_img_result;
793 }
794 fit_img_result = fit_image_get_type(buf,
795 fit_img_result,
796 &img_type);
797 if (fit_img_result < 0) {
798 /* Something went wrong! */
799 return fit_img_result;
800 }
801
802 fit_loadable_process(img_type, img_data, img_len);
803 }
804 break;
805 default:
806 printf("The given image format is not supported (corrupt?)\n");
807 return 1;
808 }
809
810 return 0;
811}
Simon Glass4d7237b2021-09-25 07:03:15 -0600812
Simon Glass4d7237b2021-09-25 07:03:15 -0600813/**
814 * boot_get_cmdline - allocate and initialize kernel cmdline
Simon Glass4d7237b2021-09-25 07:03:15 -0600815 * @cmd_start: pointer to a ulong variable, will hold cmdline start
816 * @cmd_end: pointer to a ulong variable, will hold cmdline end
817 *
Simon Glass7ae16bb2022-08-28 12:32:53 -0600818 * This allocates space for kernel command line below
Simon Glass4d7237b2021-09-25 07:03:15 -0600819 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
820 * variable is present its contents is copied to allocated kernel
821 * command line.
822 *
823 * returns:
824 * 0 - success
825 * -1 - failure
826 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530827int boot_get_cmdline(ulong *cmd_start, ulong *cmd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600828{
Simon Glass7ae16bb2022-08-28 12:32:53 -0600829 int barg;
Simon Glass4d7237b2021-09-25 07:03:15 -0600830 char *cmdline;
831 char *s;
832
Simon Glass7ae16bb2022-08-28 12:32:53 -0600833 /*
834 * Help the compiler detect that this function is only called when
835 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
836 */
837 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
838 return 0;
839
840 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530841 cmdline = (char *)(ulong)lmb_alloc_base(barg, 0xf,
Simon Glass4d7237b2021-09-25 07:03:15 -0600842 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass60d71542021-09-25 07:03:16 -0600843 if (!cmdline)
Simon Glass4d7237b2021-09-25 07:03:15 -0600844 return -1;
845
846 s = env_get("bootargs");
847 if (!s)
848 s = "";
849
850 strcpy(cmdline, s);
851
Simon Glass60d71542021-09-25 07:03:16 -0600852 *cmd_start = (ulong)cmdline;
Simon Glass4d7237b2021-09-25 07:03:15 -0600853 *cmd_end = *cmd_start + strlen(cmdline);
854
855 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
856
857 return 0;
858}
Simon Glass4d7237b2021-09-25 07:03:15 -0600859
Simon Glass4d7237b2021-09-25 07:03:15 -0600860/**
861 * boot_get_kbd - allocate and initialize kernel copy of board info
Simon Glass4d7237b2021-09-25 07:03:15 -0600862 * @kbd: double pointer to board info data
863 *
864 * boot_get_kbd() allocates space for kernel copy of board info data below
865 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
866 * with the current u-boot board info data.
867 *
868 * returns:
869 * 0 - success
870 * -1 - failure
871 */
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530872int boot_get_kbd(struct bd_info **kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600873{
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530874 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(sizeof(struct bd_info),
Simon Glass4d7237b2021-09-25 07:03:15 -0600875 0xf,
Simon Glass60d71542021-09-25 07:03:16 -0600876 env_get_bootm_mapsize() +
877 env_get_bootm_low());
878 if (!*kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600879 return -1;
880
Simon Glass60d71542021-09-25 07:03:16 -0600881 **kbd = *gd->bd;
Simon Glass4d7237b2021-09-25 07:03:15 -0600882
883 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
884
Simon Glass7ae16bb2022-08-28 12:32:53 -0600885 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
Simon Glassbc0c4db2021-09-25 07:03:20 -0600886 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass4d7237b2021-09-25 07:03:15 -0600887
888 return 0;
889}
Simon Glass4d7237b2021-09-25 07:03:15 -0600890
Simon Glassdf00afa2022-09-06 20:26:50 -0600891int image_setup_linux(struct bootm_headers *images)
Simon Glass4d7237b2021-09-25 07:03:15 -0600892{
893 ulong of_size = images->ft_len;
894 char **of_flat_tree = &images->ft_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600895 int ret;
896
Simon Glass7ae16bb2022-08-28 12:32:53 -0600897 /* This function cannot be called without lmb support */
Sughosh Ganu0d733f02024-08-26 17:29:22 +0530898 if (!CONFIG_IS_ENABLED(LMB))
Simon Glass7ae16bb2022-08-28 12:32:53 -0600899 return -EFAULT;
Simon Glass85c057e2021-09-25 19:43:21 -0600900 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530901 boot_fdt_add_mem_rsv_regions(*of_flat_tree);
Simon Glass4d7237b2021-09-25 07:03:15 -0600902
Simon Glassb8eb1fe2021-09-25 19:43:25 -0600903 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530904 ret = boot_get_cmdline(&images->cmdline_start,
Simon Glass60d71542021-09-25 07:03:16 -0600905 &images->cmdline_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600906 if (ret) {
907 puts("ERROR with allocation of cmdline\n");
908 return ret;
909 }
910 }
911
Simon Glass85c057e2021-09-25 19:43:21 -0600912 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530913 ret = boot_relocate_fdt(of_flat_tree, &of_size);
Simon Glass4d7237b2021-09-25 07:03:15 -0600914 if (ret)
915 return ret;
916 }
917
Simon Glass85c057e2021-09-25 19:43:21 -0600918 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530919 ret = image_setup_libfdt(images, *of_flat_tree, true);
Simon Glass4d7237b2021-09-25 07:03:15 -0600920 if (ret)
921 return ret;
922 }
923
924 return 0;
925}
Simon Glass4a8a8a12021-09-25 07:03:17 -0600926
927void genimg_print_size(uint32_t size)
928{
929 printf("%d Bytes = ", size);
930 print_size(size, "\n");
931}
932
933void genimg_print_time(time_t timestamp)
934{
935 struct rtc_time tm;
936
937 rtc_to_tm(timestamp, &tm);
938 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
939 tm.tm_year, tm.tm_mon, tm.tm_mday,
940 tm.tm_hour, tm.tm_min, tm.tm_sec);
941}
Simon Glassdaee3ba2023-01-06 08:52:28 -0600942
943/**
944 * get_default_image() - Return default property from /images
945 *
946 * Return: Pointer to value of default property (or NULL)
947 */
948static const char *get_default_image(const void *fit)
949{
950 int images_noffset;
951
952 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
953 if (images_noffset < 0)
954 return NULL;
955
956 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
957}
958
959int image_locate_script(void *buf, int size, const char *fit_uname,
960 const char *confname, char **datap, uint *lenp)
961{
962 const struct legacy_img_hdr *hdr;
963 const void *fit_data;
964 const void *fit_hdr;
965 size_t fit_len;
966 int noffset;
967 int verify;
968 ulong len;
969 u32 *data;
970
971 verify = env_get_yesno("verify");
972
973 switch (genimg_get_format(buf)) {
974 case IMAGE_FORMAT_LEGACY:
Marek Vasutfb59eca2023-02-27 20:56:31 +0100975 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
976 goto exit_image_format;
977 } else {
Simon Glassdaee3ba2023-01-06 08:52:28 -0600978 hdr = buf;
979
980 if (!image_check_magic(hdr)) {
981 puts("Bad magic number\n");
982 return 1;
983 }
984
985 if (!image_check_hcrc(hdr)) {
986 puts("Bad header crc\n");
987 return 1;
988 }
989
990 if (verify) {
991 if (!image_check_dcrc(hdr)) {
992 puts("Bad data crc\n");
993 return 1;
994 }
995 }
996
997 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
998 puts("Bad image type\n");
999 return 1;
1000 }
1001
1002 /* get length of script */
1003 data = (u32 *)image_get_data(hdr);
1004
1005 len = uimage_to_cpu(*data);
1006 if (!len) {
1007 puts("Empty Script\n");
1008 return 1;
1009 }
1010
1011 /*
1012 * scripts are just multi-image files with one
1013 * component, so seek past the zero-terminated sequence
1014 * of image lengths to get to the actual image data
1015 */
1016 while (*data++);
1017 }
1018 break;
1019 case IMAGE_FORMAT_FIT:
Marek Vasutfb59eca2023-02-27 20:56:31 +01001020 if (!IS_ENABLED(CONFIG_FIT)) {
1021 goto exit_image_format;
1022 } else {
Simon Glassdaee3ba2023-01-06 08:52:28 -06001023 fit_hdr = buf;
1024 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1025 puts("Bad FIT image format\n");
1026 return 1;
1027 }
1028
1029 if (!fit_uname) {
1030 /* If confname is empty, use the default */
1031 if (confname && *confname)
1032 noffset = fit_conf_get_node(fit_hdr, confname);
1033 else
1034 noffset = fit_conf_get_node(fit_hdr, NULL);
1035 if (noffset < 0) {
1036 if (!confname)
1037 goto fallback;
1038 printf("Could not find config %s\n", confname);
1039 return 1;
1040 }
1041
1042 if (verify && fit_config_verify(fit_hdr, noffset))
1043 return 1;
1044
1045 noffset = fit_conf_get_prop_node(fit_hdr,
1046 noffset,
1047 FIT_SCRIPT_PROP,
1048 IH_PHASE_NONE);
1049 if (noffset < 0) {
1050 if (!confname)
1051 goto fallback;
1052 printf("Could not find script in %s\n", confname);
1053 return 1;
1054 }
1055 } else {
1056fallback:
1057 if (!fit_uname || !*fit_uname)
1058 fit_uname = get_default_image(fit_hdr);
1059 if (!fit_uname) {
1060 puts("No FIT subimage unit name\n");
1061 return 1;
1062 }
1063
1064 /* get script component image node offset */
1065 noffset = fit_image_get_node(fit_hdr, fit_uname);
1066 if (noffset < 0) {
1067 printf("Can't find '%s' FIT subimage\n",
1068 fit_uname);
1069 return 1;
1070 }
1071 }
1072
1073 if (!fit_image_check_type(fit_hdr, noffset,
1074 IH_TYPE_SCRIPT)) {
1075 puts("Not a image image\n");
1076 return 1;
1077 }
1078
1079 /* verify integrity */
1080 if (verify && !fit_image_verify(fit_hdr, noffset)) {
1081 puts("Bad Data Hash\n");
1082 return 1;
1083 }
1084
1085 /* get script subimage data address and length */
Tobias Waldekranz317dbfc2023-02-16 16:33:47 +01001086 if (fit_image_get_data_and_size(fit_hdr, noffset,
1087 &fit_data, &fit_len)) {
Simon Glassdaee3ba2023-01-06 08:52:28 -06001088 puts("Could not find script subimage data\n");
1089 return 1;
1090 }
1091
1092 data = (u32 *)fit_data;
1093 len = (ulong)fit_len;
1094 }
1095 break;
1096 default:
Marek Vasutfb59eca2023-02-27 20:56:31 +01001097 goto exit_image_format;
Simon Glassdaee3ba2023-01-06 08:52:28 -06001098 }
1099
1100 *datap = (char *)data;
1101 *lenp = len;
1102
1103 return 0;
Marek Vasutfb59eca2023-02-27 20:56:31 +01001104
1105exit_image_format:
1106 puts("Wrong image format for \"source\" command\n");
1107 return -EPERM;
Simon Glassdaee3ba2023-01-06 08:52:28 -06001108}