blob: 7c0948b592a7a1639e1fbd60bbcfb7db5febc2ad [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 Glass41506ff2021-09-25 07:03:15 -060019#include <mapmem.h>
Simon Glass5d3248a2021-09-25 07:03:17 -060020#include <rtc.h>
Simon Glass41506ff2021-09-25 07:03:15 -060021#include <watchdog.h>
22#include <asm/cache.h>
23#include <asm/global_data.h>
24
Simon Glass41506ff2021-09-25 07:03:15 -060025DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass41506ff2021-09-25 07:03:15 -060027/**
28 * image_get_ramdisk - get and verify ramdisk image
29 * @rd_addr: ramdisk image start address
30 * @arch: expected ramdisk architecture
31 * @verify: checksum verification flag
32 *
33 * image_get_ramdisk() returns a pointer to the verified ramdisk image
34 * header. Routine receives image start address and expected architecture
35 * flag. Verification done covers data and header integrity and os/type/arch
36 * fields checking.
37 *
38 * returns:
39 * pointer to a ramdisk image header, if image was found and valid
40 * otherwise, return NULL
41 */
Simon Glass3d2a47f2021-09-25 07:03:16 -060042static const image_header_t *image_get_ramdisk(ulong rd_addr, u8 arch,
43 int verify)
Simon Glass41506ff2021-09-25 07:03:15 -060044{
45 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
46
47 if (!image_check_magic(rd_hdr)) {
48 puts("Bad Magic Number\n");
49 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
50 return NULL;
51 }
52
53 if (!image_check_hcrc(rd_hdr)) {
54 puts("Bad Header Checksum\n");
55 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
56 return NULL;
57 }
58
59 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
60 image_print_contents(rd_hdr);
61
62 if (verify) {
63 puts(" Verifying Checksum ... ");
64 if (!image_check_dcrc(rd_hdr)) {
65 puts("Bad Data CRC\n");
66 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
67 return NULL;
68 }
69 puts("OK\n");
70 }
71
72 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
73
74 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
75 !image_check_arch(rd_hdr, arch) ||
76 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
77 printf("No Linux %s Ramdisk Image\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -060078 genimg_get_arch_name(arch));
Simon Glass41506ff2021-09-25 07:03:15 -060079 bootstage_error(BOOTSTAGE_ID_RAMDISK);
80 return NULL;
81 }
82
83 return rd_hdr;
84}
Simon Glass41506ff2021-09-25 07:03:15 -060085
86/*****************************************************************************/
87/* Shared dual-format routines */
88/*****************************************************************************/
89ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
90ulong image_save_addr; /* Default Save Address */
91ulong image_save_size; /* Default Save Size (in bytes) */
92
93static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass3d2a47f2021-09-25 07:03:16 -060094 int flags)
Simon Glass41506ff2021-09-25 07:03:15 -060095{
96 switch (op) {
97 case env_op_create:
98 case env_op_overwrite:
99 image_load_addr = hextoul(value, NULL);
100 break;
101 default:
102 break;
103 }
104
105 return 0;
106}
107U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
108
109ulong env_get_bootm_low(void)
110{
111 char *s = env_get("bootm_low");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600112
Simon Glass41506ff2021-09-25 07:03:15 -0600113 if (s) {
114 ulong tmp = hextoul(s, NULL);
115 return tmp;
116 }
117
118#if defined(CONFIG_SYS_SDRAM_BASE)
119 return CONFIG_SYS_SDRAM_BASE;
120#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
121 return gd->bd->bi_dram[0].start;
122#else
123 return 0;
124#endif
125}
126
127phys_size_t env_get_bootm_size(void)
128{
129 phys_size_t tmp, size;
130 phys_addr_t start;
131 char *s = env_get("bootm_size");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600132
Simon Glass41506ff2021-09-25 07:03:15 -0600133 if (s) {
134 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
135 return tmp;
136 }
137
138 start = gd->ram_base;
139 size = gd->ram_size;
140
141 if (start + size > gd->ram_top)
142 size = gd->ram_top - start;
143
144 s = env_get("bootm_low");
145 if (s)
146 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
147 else
148 tmp = start;
149
150 return size - (tmp - start);
151}
152
153phys_size_t env_get_bootm_mapsize(void)
154{
155 phys_size_t tmp;
156 char *s = env_get("bootm_mapsize");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600157
Simon Glass41506ff2021-09-25 07:03:15 -0600158 if (s) {
159 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
160 return tmp;
161 }
162
163#if defined(CONFIG_SYS_BOOTMAPSZ)
164 return CONFIG_SYS_BOOTMAPSZ;
165#else
166 return env_get_bootm_size();
167#endif
168}
169
170void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
171{
172 if (to == from)
173 return;
174
175#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
176 if (to > from) {
177 from += len;
178 to += len;
179 }
180 while (len > 0) {
181 size_t tail = (len > chunksz) ? chunksz : len;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600182
Simon Glass41506ff2021-09-25 07:03:15 -0600183 WATCHDOG_RESET();
184 if (to > from) {
185 to -= tail;
186 from -= tail;
187 }
188 memmove(to, from, tail);
189 if (to < from) {
190 to += tail;
191 from += tail;
192 }
193 len -= tail;
194 }
195#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
196 memmove(to, from, len);
197#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
198}
199
200/**
201 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
202 * FIT strings
203 * @img_addr: a string might contain real image address
204 * @fit_uname_config: double pointer to a char, will hold pointer to a
205 * configuration unit name
206 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
207 * name
208 *
209 * genimg_get_kernel_addr_fit get the real kernel start address from a string
210 * which is normally the first argv of bootm/bootz
211 *
212 * returns:
213 * kernel start address
214 */
215ulong genimg_get_kernel_addr_fit(char * const img_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600216 const char **fit_uname_config,
217 const char **fit_uname_kernel)
Simon Glass41506ff2021-09-25 07:03:15 -0600218{
219 ulong kernel_addr;
220
221 /* find out kernel image address */
222 if (!img_addr) {
223 kernel_addr = image_load_addr;
224 debug("* kernel: default image load address = 0x%08lx\n",
225 image_load_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600226 } else if (CONFIG_IS_ENABLED(FIT) &&
227 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass41506ff2021-09-25 07:03:15 -0600228 fit_uname_config)) {
229 debug("* kernel: config '%s' from image at 0x%08lx\n",
230 *fit_uname_config, kernel_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600231 } else if (CONFIG_IS_ENABLED(FIT) &&
232 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
233 fit_uname_kernel)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600234 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
235 *fit_uname_kernel, kernel_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600236 } else {
237 kernel_addr = hextoul(img_addr, NULL);
238 debug("* kernel: cmdline image address = 0x%08lx\n",
239 kernel_addr);
240 }
241
242 return kernel_addr;
243}
244
245/**
246 * genimg_get_kernel_addr() is the simple version of
247 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
248 */
249ulong genimg_get_kernel_addr(char * const img_addr)
250{
251 const char *fit_uname_config = NULL;
252 const char *fit_uname_kernel = NULL;
253
254 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
255 &fit_uname_kernel);
256}
257
258/**
259 * genimg_get_format - get image format type
260 * @img_addr: image start address
261 *
262 * genimg_get_format() checks whether provided address points to a valid
263 * legacy or FIT image.
264 *
265 * New uImage format and FDT blob are based on a libfdt. FDT blob
266 * may be passed directly or embedded in a FIT image. In both situations
267 * genimg_get_format() must be able to dectect libfdt header.
268 *
269 * returns:
270 * image format type or IMAGE_FORMAT_INVALID if no image is present
271 */
272int genimg_get_format(const void *img_addr)
273{
Simon Glass1df654a2021-09-25 19:43:35 -0600274 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
275 const image_header_t *hdr;
Simon Glass41506ff2021-09-25 07:03:15 -0600276
Simon Glass1df654a2021-09-25 19:43:35 -0600277 hdr = (const image_header_t *)img_addr;
278 if (image_check_magic(hdr))
279 return IMAGE_FORMAT_LEGACY;
280 }
281 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
282 if (!fdt_check_header(img_addr))
283 return IMAGE_FORMAT_FIT;
284 }
285 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
286 !android_image_check_header(img_addr))
Simon Glass41506ff2021-09-25 07:03:15 -0600287 return IMAGE_FORMAT_ANDROID;
Simon Glass41506ff2021-09-25 07:03:15 -0600288
289 return IMAGE_FORMAT_INVALID;
290}
291
292/**
293 * fit_has_config - check if there is a valid FIT configuration
294 * @images: pointer to the bootm command headers structure
295 *
296 * fit_has_config() checks if there is a FIT configuration in use
297 * (if FTI support is present).
298 *
299 * returns:
300 * 0, no FIT support or no configuration found
301 * 1, configuration found
302 */
303int genimg_has_config(bootm_headers_t *images)
304{
Simon Glass1df654a2021-09-25 19:43:35 -0600305 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass41506ff2021-09-25 07:03:15 -0600306 return 1;
Simon Glass1df654a2021-09-25 19:43:35 -0600307
Simon Glass41506ff2021-09-25 07:03:15 -0600308 return 0;
309}
310
311/**
Simon Glasse4c92872021-09-25 19:43:37 -0600312 * select_ramdisk() - Select and locate the ramdisk to use
313 *
Simon Glass41506ff2021-09-25 07:03:15 -0600314 * @images: pointer to the bootm images structure
Simon Glass4f2d9412022-08-28 12:32:47 -0600315 * @select: name of ramdisk to select, or hex address, NULL for any
Simon Glass41506ff2021-09-25 07:03:15 -0600316 * @arch: expected ramdisk architecture
Simon Glasse4c92872021-09-25 19:43:37 -0600317 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
318 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100319 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glasse4c92872021-09-25 19:43:37 -0600320 * other -ve value on other error
Simon Glass41506ff2021-09-25 07:03:15 -0600321 */
Simon Glasse4c92872021-09-25 19:43:37 -0600322static int select_ramdisk(bootm_headers_t *images, const char *select, u8 arch,
323 ulong *rd_datap, ulong *rd_lenp)
Simon Glass41506ff2021-09-25 07:03:15 -0600324{
Simon Glassf7659f62022-08-28 12:32:49 -0600325 const char *fit_uname_config;
326 const char *fit_uname_ramdisk;
Simon Glassca788832022-08-28 12:32:51 -0600327 bool done_select = !select;
Simon Glass4f2d9412022-08-28 12:32:47 -0600328 bool done = false;
Simon Glassf7659f62022-08-28 12:32:49 -0600329 int rd_noffset;
Tom Rini621158d2021-12-20 09:36:32 -0500330 ulong rd_addr;
Simon Glasse4c92872021-09-25 19:43:37 -0600331 char *buf;
Simon Glass78f88792021-09-25 19:43:36 -0600332
Simon Glassca788832022-08-28 12:32:51 -0600333 if (CONFIG_IS_ENABLED(FIT)) {
Simon Glassf7659f62022-08-28 12:32:49 -0600334 fit_uname_config = images->fit_uname_cfg;
335 fit_uname_ramdisk = NULL;
Simon Glass78f88792021-09-25 19:43:36 -0600336
Tom Rini621158d2021-12-20 09:36:32 -0500337 if (select) {
338 ulong default_addr;
Simon Glass41506ff2021-09-25 07:03:15 -0600339 /*
340 * If the init ramdisk comes from the FIT image and
341 * the FIT image address is omitted in the command
342 * line argument, try to use os FIT image address or
343 * default load address.
344 */
345 if (images->fit_uname_os)
346 default_addr = (ulong)images->fit_hdr_os;
347 else
348 default_addr = image_load_addr;
349
Tom Rini621158d2021-12-20 09:36:32 -0500350 if (fit_parse_conf(select, default_addr,
351 &rd_addr, &fit_uname_config)) {
Simon Glass3d2a47f2021-09-25 07:03:16 -0600352 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
353 fit_uname_config, rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600354 done_select = true;
Simon Glass41506ff2021-09-25 07:03:15 -0600355 } else if (fit_parse_subimage(select, default_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600356 &rd_addr,
357 &fit_uname_ramdisk)) {
358 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
359 fit_uname_ramdisk, rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600360 done_select = true;
361 }
362 }
363 }
364 if (!done_select) {
Tom Rini621158d2021-12-20 09:36:32 -0500365 rd_addr = hextoul(select, NULL);
366 debug("* ramdisk: cmdline image address = 0x%08lx\n",
367 rd_addr);
Simon Glassca788832022-08-28 12:32:51 -0600368 }
Simon Glass1a68c032022-08-28 12:32:50 -0600369 if (CONFIG_IS_ENABLED(FIT) && !select) {
Tom Rini621158d2021-12-20 09:36:32 -0500370 /* use FIT configuration provided in first bootm
371 * command argument. If the property is not defined,
372 * quit silently (with -ENOPKG)
373 */
374 rd_addr = map_to_sysmem(images->fit_hdr_os);
375 rd_noffset = fit_get_node_from_config(images,
376 FIT_RAMDISK_PROP,
377 rd_addr);
378 if (rd_noffset == -ENOENT)
379 return -ENOPKG;
380 else if (rd_noffset < 0)
381 return rd_noffset;
Simon Glass41506ff2021-09-25 07:03:15 -0600382 }
Simon Glassf33a2c12021-09-25 19:43:38 -0600383
Tom Rini621158d2021-12-20 09:36:32 -0500384 /*
385 * Check if there is an initrd image at the
386 * address provided in the second bootm argument
387 * check image type, for FIT images get FIT node.
388 */
389 buf = map_sysmem(rd_addr, 0);
390 switch (genimg_get_format(buf)) {
Simon Glass0f81af42022-08-28 12:32:48 -0600391 case IMAGE_FORMAT_LEGACY:
392 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
393 const image_header_t *rd_hdr;
Simon Glass78f88792021-09-25 19:43:36 -0600394
Simon Glass0f81af42022-08-28 12:32:48 -0600395 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
396 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600397
Simon Glass0f81af42022-08-28 12:32:48 -0600398 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
399 rd_hdr = image_get_ramdisk(rd_addr, arch,
400 images->verify);
Tom Rini621158d2021-12-20 09:36:32 -0500401
Simon Glass0f81af42022-08-28 12:32:48 -0600402 if (!rd_hdr)
403 return -ENOENT;
Simon Glass41506ff2021-09-25 07:03:15 -0600404
Simon Glass0f81af42022-08-28 12:32:48 -0600405 *rd_datap = image_get_data(rd_hdr);
406 *rd_lenp = image_get_data_size(rd_hdr);
407 done = true;
408 }
Tom Rini621158d2021-12-20 09:36:32 -0500409 break;
Tom Rini621158d2021-12-20 09:36:32 -0500410 case IMAGE_FORMAT_FIT:
Simon Glassf7659f62022-08-28 12:32:49 -0600411 if (CONFIG_IS_ENABLED(FIT)) {
412 rd_noffset = fit_image_load(images, rd_addr,
413 &fit_uname_ramdisk, &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;
Simon Glass41506ff2021-09-25 07:03:15 -0600420
Simon Glassf7659f62022-08-28 12:32:49 -0600421 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 }
Tom Rini621158d2021-12-20 09:36:32 -0500426 break;
Tom Rini621158d2021-12-20 09:36:32 -0500427 case IMAGE_FORMAT_ANDROID:
Simon Glass1ce8e102022-08-28 12:32:46 -0600428 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
429 void *ptr = map_sysmem(images->os.start, 0);
430 int ret;
431
432 ret = android_image_get_ramdisk(ptr, rd_datap,
433 rd_lenp);
434 unmap_sysmem(ptr);
435 if (ret)
436 return ret;
Simon Glass4f2d9412022-08-28 12:32:47 -0600437 done = true;
Simon Glass1ce8e102022-08-28 12:32:46 -0600438 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600439 break;
440 }
Simon Glass1df654a2021-09-25 19:43:35 -0600441
Simon Glass4f2d9412022-08-28 12:32:47 -0600442 if (!done) {
443 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
444 char *end = NULL;
445
446 if (select)
447 end = strchr(select, ':');
448 if (end) {
449 *rd_lenp = hextoul(++end, NULL);
450 *rd_datap = rd_addr;
451 done = true;
Simon Glass41506ff2021-09-25 07:03:15 -0600452 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600453 }
454
455 if (!done) {
Simon Glass1df654a2021-09-25 19:43:35 -0600456 puts("Wrong Ramdisk Image Format\n");
Simon Glasse4c92872021-09-25 19:43:37 -0600457 return -EINVAL;
Simon Glass41506ff2021-09-25 07:03:15 -0600458 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600459 }
Simon Glasse4c92872021-09-25 19:43:37 -0600460
461 return 0;
462}
463
464/**
465 * boot_get_ramdisk - main ramdisk handling routine
466 * @argc: command argument count
467 * @argv: command argument list
468 * @images: pointer to the bootm images structure
469 * @arch: expected ramdisk architecture
470 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
471 * @rd_end: pointer to a ulong variable, will hold ramdisk end
472 *
473 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
474 * Currently supported are the following ramdisk sources:
475 * - multicomponent kernel/ramdisk image,
476 * - commandline provided address of decicated ramdisk image.
477 *
478 * returns:
479 * 0, if ramdisk image was found and valid, or skiped
480 * rd_start and rd_end are set to ramdisk start/end addresses if
481 * ramdisk image is found and valid
482 *
483 * 1, if ramdisk image is found but corrupted, or invalid
484 * rd_start and rd_end are set to 0 if no ramdisk exists
485 */
486int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
487 u8 arch, ulong *rd_start, ulong *rd_end)
488{
489 ulong rd_data, rd_len;
490 const char *select = NULL;
491
492 *rd_start = 0;
493 *rd_end = 0;
494
495 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
496 char *buf;
497
498 /* Look for an Android boot image */
499 buf = map_sysmem(images->os.start, 0);
500 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
501 select = (argc == 0) ? env_get("loadaddr") : argv[0];
502 }
503
504 if (argc >= 2)
505 select = argv[1];
506
507 /*
508 * Look for a '-' which indicates to ignore the
509 * ramdisk argument
510 */
511 if (select && strcmp(select, "-") == 0) {
512 debug("## Skipping init Ramdisk\n");
513 rd_len = 0;
514 rd_data = 0;
515 } else if (select || genimg_has_config(images)) {
516 int ret;
517
518 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
519 if (ret == -ENOPKG)
520 return 0;
521 else if (ret)
522 return ret;
Simon Glass41506ff2021-09-25 07:03:15 -0600523 } else if (images->legacy_hdr_valid &&
524 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600525 IH_TYPE_MULTI)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600526 /*
527 * Now check if we have a legacy mult-component image,
528 * get second entry data start address and len.
529 */
530 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600531 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
532 (ulong)images->legacy_hdr_os);
Simon Glass41506ff2021-09-25 07:03:15 -0600533
534 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
535 } else {
536 /*
537 * no initrd image
538 */
539 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600540 rd_len = 0;
541 rd_data = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600542 }
543
544 if (!rd_data) {
545 debug("## No init Ramdisk\n");
546 } else {
547 *rd_start = rd_data;
548 *rd_end = rd_data + rd_len;
549 }
550 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600551 *rd_start, *rd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600552
553 return 0;
554}
555
Ashok Reddy Soma65168912022-07-07 10:45:37 +0200556#if defined(CONFIG_LMB)
Simon Glass41506ff2021-09-25 07:03:15 -0600557/**
558 * boot_ramdisk_high - relocate init ramdisk
559 * @lmb: pointer to lmb handle, will be used for memory mgmt
560 * @rd_data: ramdisk data start address
561 * @rd_len: ramdisk data length
562 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
563 * start address (after possible relocation)
564 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
565 * end address (after possible relocation)
566 *
567 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
568 * variable and if requested ramdisk data is moved to a specified location.
569 *
570 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
571 * start/end addresses if ramdisk image start and len were provided,
572 * otherwise set initrd_start and initrd_end set to zeros.
573 *
574 * returns:
575 * 0 - success
576 * -1 - failure
577 */
578int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600579 ulong *initrd_start, ulong *initrd_end)
Simon Glass41506ff2021-09-25 07:03:15 -0600580{
581 char *s;
582 ulong initrd_high;
583 int initrd_copy_to_ram = 1;
584
585 s = env_get("initrd_high");
586 if (s) {
587 /* a value of "no" or a similar string will act like 0,
588 * turning the "load high" feature off. This is intentional.
589 */
590 initrd_high = hextoul(s, NULL);
591 if (initrd_high == ~0)
592 initrd_copy_to_ram = 0;
593 } else {
594 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
595 }
596
Simon Glass41506ff2021-09-25 07:03:15 -0600597 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600598 initrd_high, initrd_copy_to_ram);
Simon Glass41506ff2021-09-25 07:03:15 -0600599
600 if (rd_data) {
601 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
602 debug(" in-place initrd\n");
603 *initrd_start = rd_data;
604 *initrd_end = rd_data + rd_len;
605 lmb_reserve(lmb, rd_data, rd_len);
606 } else {
607 if (initrd_high)
608 *initrd_start = (ulong)lmb_alloc_base(lmb,
609 rd_len, 0x1000, initrd_high);
610 else
611 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
612 0x1000);
613
614 if (*initrd_start == 0) {
615 puts("ramdisk - allocation error\n");
616 goto error;
617 }
618 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
619
620 *initrd_end = *initrd_start + rd_len;
621 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600622 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600623
624 memmove_wd((void *)*initrd_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600625 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass41506ff2021-09-25 07:03:15 -0600626
Simon Glass41506ff2021-09-25 07:03:15 -0600627 /*
628 * Ensure the image is flushed to memory to handle
629 * AMP boot scenarios in which we might not be
630 * HW cache coherent
631 */
Simon Glass1df654a2021-09-25 19:43:35 -0600632 if (IS_ENABLED(CONFIG_MP)) {
633 flush_cache((unsigned long)*initrd_start,
634 ALIGN(rd_len, ARCH_DMA_MINALIGN));
635 }
Simon Glass41506ff2021-09-25 07:03:15 -0600636 puts("OK\n");
637 }
638 } else {
639 *initrd_start = 0;
640 *initrd_end = 0;
641 }
642 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600643 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600644
645 return 0;
646
647error:
648 return -1;
649}
Ashok Reddy Soma65168912022-07-07 10:45:37 +0200650#endif
Simon Glass41506ff2021-09-25 07:03:15 -0600651
Simon Glass3d2a47f2021-09-25 07:03:16 -0600652int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass41506ff2021-09-25 07:03:15 -0600653 ulong *setup_start, ulong *setup_len)
654{
Simon Glass1df654a2021-09-25 19:43:35 -0600655 if (!CONFIG_IS_ENABLED(FIT))
656 return -ENOENT;
657
Simon Glass41506ff2021-09-25 07:03:15 -0600658 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600659}
660
Simon Glass41506ff2021-09-25 07:03:15 -0600661int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600662 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600663{
664 ulong tmp_img_addr, img_data, img_len;
665 void *buf;
666 int conf_noffset;
667 int fit_img_result;
668 const char *uname, *name;
669 int err;
670 int devnum = 0; /* TODO support multi fpga platforms */
671
Simon Glass1df654a2021-09-25 19:43:35 -0600672 if (!IS_ENABLED(CONFIG_FPGA))
673 return -ENOSYS;
674
Simon Glass41506ff2021-09-25 07:03:15 -0600675 /* Check to see if the images struct has a FIT configuration */
676 if (!genimg_has_config(images)) {
677 debug("## FIT configuration was not specified\n");
678 return 0;
679 }
680
681 /*
682 * Obtain the os FIT header from the images struct
683 */
684 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
685 buf = map_sysmem(tmp_img_addr, 0);
686 /*
687 * Check image type. For FIT images get FIT node
688 * and attempt to locate a generic binary.
689 */
690 switch (genimg_get_format(buf)) {
691 case IMAGE_FORMAT_FIT:
692 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
693
694 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
695 NULL);
696 if (!uname) {
697 debug("## FPGA image is not specified\n");
698 return 0;
699 }
700 fit_img_result = fit_image_load(images,
701 tmp_img_addr,
702 (const char **)&uname,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600703 &images->fit_uname_cfg,
Simon Glass41506ff2021-09-25 07:03:15 -0600704 arch,
705 IH_TYPE_FPGA,
706 BOOTSTAGE_ID_FPGA_INIT,
707 FIT_LOAD_OPTIONAL_NON_ZERO,
708 &img_data, &img_len);
709
710 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
711 uname, img_data, img_len);
712
713 if (fit_img_result < 0) {
714 /* Something went wrong! */
715 return fit_img_result;
716 }
717
718 if (!fpga_is_partial_data(devnum, img_len)) {
719 name = "full";
720 err = fpga_loadbitstream(devnum, (char *)img_data,
721 img_len, BIT_FULL);
722 if (err)
723 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300724 img_len, BIT_FULL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600725 } else {
726 name = "partial";
727 err = fpga_loadbitstream(devnum, (char *)img_data,
728 img_len, BIT_PARTIAL);
729 if (err)
730 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300731 img_len, BIT_PARTIAL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600732 }
733
734 if (err)
735 return err;
736
737 printf(" Programming %s bitstream... OK\n", name);
738 break;
739 default:
740 printf("The given image format is not supported (corrupt?)\n");
741 return 1;
742 }
743
744 return 0;
745}
Simon Glass41506ff2021-09-25 07:03:15 -0600746
Simon Glass3d2a47f2021-09-25 07:03:16 -0600747static void fit_loadable_process(u8 img_type,
Simon Glass41506ff2021-09-25 07:03:15 -0600748 ulong img_data,
749 ulong img_len)
750{
751 int i;
752 const unsigned int count =
753 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
754 struct fit_loadable_tbl *fit_loadable_handler =
755 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
756 /* For each loadable handler */
757 for (i = 0; i < count; i++, fit_loadable_handler++)
758 /* matching this type */
759 if (fit_loadable_handler->type == img_type)
760 /* call that handler with this image data */
761 fit_loadable_handler->handler(img_data, img_len);
762}
763
764int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600765 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600766{
767 /*
768 * These variables are used to hold the current image location
769 * in system memory.
770 */
771 ulong tmp_img_addr;
772 /*
773 * These two variables are requirements for fit_image_load, but
774 * their values are not used
775 */
776 ulong img_data, img_len;
777 void *buf;
778 int loadables_index;
779 int conf_noffset;
780 int fit_img_result;
781 const char *uname;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600782 u8 img_type;
Simon Glass41506ff2021-09-25 07:03:15 -0600783
784 /* Check to see if the images struct has a FIT configuration */
785 if (!genimg_has_config(images)) {
786 debug("## FIT configuration was not specified\n");
787 return 0;
788 }
789
790 /*
791 * Obtain the os FIT header from the images struct
792 */
793 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
794 buf = map_sysmem(tmp_img_addr, 0);
795 /*
796 * Check image type. For FIT images get FIT node
797 * and attempt to locate a generic binary.
798 */
799 switch (genimg_get_format(buf)) {
800 case IMAGE_FORMAT_FIT:
801 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
802
803 for (loadables_index = 0;
804 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600805 FIT_LOADABLE_PROP,
806 loadables_index, NULL), uname;
807 loadables_index++) {
808 fit_img_result = fit_image_load(images, tmp_img_addr,
809 &uname,
810 &images->fit_uname_cfg,
811 arch, IH_TYPE_LOADABLE,
812 BOOTSTAGE_ID_FIT_LOADABLE_START,
813 FIT_LOAD_OPTIONAL_NON_ZERO,
814 &img_data, &img_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600815 if (fit_img_result < 0) {
816 /* Something went wrong! */
817 return fit_img_result;
818 }
819
820 fit_img_result = fit_image_get_node(buf, uname);
821 if (fit_img_result < 0) {
822 /* Something went wrong! */
823 return fit_img_result;
824 }
825 fit_img_result = fit_image_get_type(buf,
826 fit_img_result,
827 &img_type);
828 if (fit_img_result < 0) {
829 /* Something went wrong! */
830 return fit_img_result;
831 }
832
833 fit_loadable_process(img_type, img_data, img_len);
834 }
835 break;
836 default:
837 printf("The given image format is not supported (corrupt?)\n");
838 return 1;
839 }
840
841 return 0;
842}
Simon Glass41506ff2021-09-25 07:03:15 -0600843
Ashok Reddy Soma65168912022-07-07 10:45:37 +0200844#if defined(CONFIG_LMB)
Tom Rini68894122022-05-12 10:02:06 -0400845#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Simon Glass41506ff2021-09-25 07:03:15 -0600846/**
847 * boot_get_cmdline - allocate and initialize kernel cmdline
848 * @lmb: pointer to lmb handle, will be used for memory mgmt
849 * @cmd_start: pointer to a ulong variable, will hold cmdline start
850 * @cmd_end: pointer to a ulong variable, will hold cmdline end
851 *
852 * boot_get_cmdline() allocates space for kernel command line below
853 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
854 * variable is present its contents is copied to allocated kernel
855 * command line.
856 *
857 * returns:
858 * 0 - success
859 * -1 - failure
860 */
861int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
862{
863 char *cmdline;
864 char *s;
865
866 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
867 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass3d2a47f2021-09-25 07:03:16 -0600868 if (!cmdline)
Simon Glass41506ff2021-09-25 07:03:15 -0600869 return -1;
870
871 s = env_get("bootargs");
872 if (!s)
873 s = "";
874
875 strcpy(cmdline, s);
876
Simon Glass3d2a47f2021-09-25 07:03:16 -0600877 *cmd_start = (ulong)cmdline;
Simon Glass41506ff2021-09-25 07:03:15 -0600878 *cmd_end = *cmd_start + strlen(cmdline);
879
880 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
881
882 return 0;
883}
Simon Glass41506ff2021-09-25 07:03:15 -0600884
Simon Glass41506ff2021-09-25 07:03:15 -0600885/**
886 * boot_get_kbd - allocate and initialize kernel copy of board info
887 * @lmb: pointer to lmb handle, will be used for memory mgmt
888 * @kbd: double pointer to board info data
889 *
890 * boot_get_kbd() allocates space for kernel copy of board info data below
891 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
892 * with the current u-boot board info data.
893 *
894 * returns:
895 * 0 - success
896 * -1 - failure
897 */
898int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
899{
900 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
901 sizeof(struct bd_info),
902 0xf,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600903 env_get_bootm_mapsize() +
904 env_get_bootm_low());
905 if (!*kbd)
Simon Glass41506ff2021-09-25 07:03:15 -0600906 return -1;
907
Simon Glass3d2a47f2021-09-25 07:03:16 -0600908 **kbd = *gd->bd;
Simon Glass41506ff2021-09-25 07:03:15 -0600909
910 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
911
Simon Glass4ed37ab2021-09-25 07:03:20 -0600912#if defined(DEBUG)
Leo Yu-Chi Liang990e1e42021-10-27 17:00:41 +0800913 if (IS_ENABLED(CONFIG_CMD_BDI))
Simon Glass4ed37ab2021-09-25 07:03:20 -0600914 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass41506ff2021-09-25 07:03:15 -0600915#endif
916
917 return 0;
918}
Tom Rini68894122022-05-12 10:02:06 -0400919#endif
Simon Glass41506ff2021-09-25 07:03:15 -0600920
Simon Glass41506ff2021-09-25 07:03:15 -0600921int image_setup_linux(bootm_headers_t *images)
922{
923 ulong of_size = images->ft_len;
924 char **of_flat_tree = &images->ft_addr;
925 struct lmb *lmb = &images->lmb;
926 int ret;
927
Simon Glass0c303f92021-09-25 19:43:21 -0600928 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass41506ff2021-09-25 07:03:15 -0600929 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
930
Simon Glass806d1ff2021-09-25 19:43:25 -0600931 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600932 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600933 &images->cmdline_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600934 if (ret) {
935 puts("ERROR with allocation of cmdline\n");
936 return ret;
937 }
938 }
939
Simon Glass0c303f92021-09-25 19:43:21 -0600940 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600941 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
942 if (ret)
943 return ret;
944 }
945
Simon Glass0c303f92021-09-25 19:43:21 -0600946 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass41506ff2021-09-25 07:03:15 -0600947 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
948 if (ret)
949 return ret;
950 }
951
952 return 0;
953}
Ashok Reddy Soma65168912022-07-07 10:45:37 +0200954#endif
Simon Glass5d3248a2021-09-25 07:03:17 -0600955
956void genimg_print_size(uint32_t size)
957{
958 printf("%d Bytes = ", size);
959 print_size(size, "\n");
960}
961
962void genimg_print_time(time_t timestamp)
963{
964 struct rtc_time tm;
965
966 rtc_to_tm(timestamp, &tm);
967 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
968 tm.tm_year, tm.tm_mon, tm.tm_mday,
969 tm.tm_hour, tm.tm_min, tm.tm_sec);
970}