blob: 07b7e467cc49df8dfde469bc80402b6ed423b12e [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 Glass4f2d9412022-08-28 12:32:47 -0600327 bool done = false;
Simon Glassf7659f62022-08-28 12:32:49 -0600328 int rd_noffset;
Tom Rini621158d2021-12-20 09:36:32 -0500329 ulong rd_addr;
Simon Glasse4c92872021-09-25 19:43:37 -0600330 char *buf;
Simon Glass78f88792021-09-25 19:43:36 -0600331
Tom Rini621158d2021-12-20 09:36:32 -0500332#if CONFIG_IS_ENABLED(FIT)
Simon Glassf7659f62022-08-28 12:32:49 -0600333 fit_uname_config = images->fit_uname_cfg;
334 fit_uname_ramdisk = NULL;
Simon Glass78f88792021-09-25 19:43:36 -0600335
Tom Rini621158d2021-12-20 09:36:32 -0500336 if (select) {
337 ulong default_addr;
Simon Glass41506ff2021-09-25 07:03:15 -0600338 /*
339 * If the init ramdisk comes from the FIT image and
340 * the FIT image address is omitted in the command
341 * line argument, try to use os FIT image address or
342 * default load address.
343 */
344 if (images->fit_uname_os)
345 default_addr = (ulong)images->fit_hdr_os;
346 else
347 default_addr = image_load_addr;
348
Tom Rini621158d2021-12-20 09:36:32 -0500349 if (fit_parse_conf(select, default_addr,
350 &rd_addr, &fit_uname_config)) {
Simon Glass3d2a47f2021-09-25 07:03:16 -0600351 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
352 fit_uname_config, rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600353 } else if (fit_parse_subimage(select, default_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600354 &rd_addr,
355 &fit_uname_ramdisk)) {
356 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
357 fit_uname_ramdisk, rd_addr);
Tom Rini621158d2021-12-20 09:36:32 -0500358 } else
359#endif
360 {
361 rd_addr = hextoul(select, NULL);
362 debug("* ramdisk: cmdline image address = 0x%08lx\n",
363 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600364 }
Tom Rini621158d2021-12-20 09:36:32 -0500365#if CONFIG_IS_ENABLED(FIT)
366 } else {
367 /* use FIT configuration provided in first bootm
368 * command argument. If the property is not defined,
369 * quit silently (with -ENOPKG)
370 */
371 rd_addr = map_to_sysmem(images->fit_hdr_os);
372 rd_noffset = fit_get_node_from_config(images,
373 FIT_RAMDISK_PROP,
374 rd_addr);
375 if (rd_noffset == -ENOENT)
376 return -ENOPKG;
377 else if (rd_noffset < 0)
378 return rd_noffset;
Simon Glass41506ff2021-09-25 07:03:15 -0600379 }
Tom Rini621158d2021-12-20 09:36:32 -0500380#endif
Simon Glassf33a2c12021-09-25 19:43:38 -0600381
Tom Rini621158d2021-12-20 09:36:32 -0500382 /*
383 * Check if there is an initrd image at the
384 * address provided in the second bootm argument
385 * check image type, for FIT images get FIT node.
386 */
387 buf = map_sysmem(rd_addr, 0);
388 switch (genimg_get_format(buf)) {
Simon Glass0f81af42022-08-28 12:32:48 -0600389 case IMAGE_FORMAT_LEGACY:
390 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
391 const image_header_t *rd_hdr;
Simon Glass78f88792021-09-25 19:43:36 -0600392
Simon Glass0f81af42022-08-28 12:32:48 -0600393 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
394 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600395
Simon Glass0f81af42022-08-28 12:32:48 -0600396 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
397 rd_hdr = image_get_ramdisk(rd_addr, arch,
398 images->verify);
Tom Rini621158d2021-12-20 09:36:32 -0500399
Simon Glass0f81af42022-08-28 12:32:48 -0600400 if (!rd_hdr)
401 return -ENOENT;
Simon Glass41506ff2021-09-25 07:03:15 -0600402
Simon Glass0f81af42022-08-28 12:32:48 -0600403 *rd_datap = image_get_data(rd_hdr);
404 *rd_lenp = image_get_data_size(rd_hdr);
405 done = true;
406 }
Tom Rini621158d2021-12-20 09:36:32 -0500407 break;
Tom Rini621158d2021-12-20 09:36:32 -0500408 case IMAGE_FORMAT_FIT:
Simon Glassf7659f62022-08-28 12:32:49 -0600409 if (CONFIG_IS_ENABLED(FIT)) {
410 rd_noffset = fit_image_load(images, rd_addr,
411 &fit_uname_ramdisk, &fit_uname_config,
412 arch, IH_TYPE_RAMDISK,
413 BOOTSTAGE_ID_FIT_RD_START,
414 FIT_LOAD_OPTIONAL_NON_ZERO,
415 rd_datap, rd_lenp);
416 if (rd_noffset < 0)
417 return rd_noffset;
Simon Glass41506ff2021-09-25 07:03:15 -0600418
Simon Glassf7659f62022-08-28 12:32:49 -0600419 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
420 images->fit_uname_rd = fit_uname_ramdisk;
421 images->fit_noffset_rd = rd_noffset;
422 done = true;
423 }
Tom Rini621158d2021-12-20 09:36:32 -0500424 break;
Tom Rini621158d2021-12-20 09:36:32 -0500425 case IMAGE_FORMAT_ANDROID:
Simon Glass1ce8e102022-08-28 12:32:46 -0600426 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
427 void *ptr = map_sysmem(images->os.start, 0);
428 int ret;
429
430 ret = android_image_get_ramdisk(ptr, rd_datap,
431 rd_lenp);
432 unmap_sysmem(ptr);
433 if (ret)
434 return ret;
Simon Glass4f2d9412022-08-28 12:32:47 -0600435 done = true;
Simon Glass1ce8e102022-08-28 12:32:46 -0600436 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600437 break;
438 }
Simon Glass1df654a2021-09-25 19:43:35 -0600439
Simon Glass4f2d9412022-08-28 12:32:47 -0600440 if (!done) {
441 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
442 char *end = NULL;
443
444 if (select)
445 end = strchr(select, ':');
446 if (end) {
447 *rd_lenp = hextoul(++end, NULL);
448 *rd_datap = rd_addr;
449 done = true;
Simon Glass41506ff2021-09-25 07:03:15 -0600450 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600451 }
452
453 if (!done) {
Simon Glass1df654a2021-09-25 19:43:35 -0600454 puts("Wrong Ramdisk Image Format\n");
Simon Glasse4c92872021-09-25 19:43:37 -0600455 return -EINVAL;
Simon Glass41506ff2021-09-25 07:03:15 -0600456 }
Simon Glass4f2d9412022-08-28 12:32:47 -0600457 }
Simon Glasse4c92872021-09-25 19:43:37 -0600458
459 return 0;
460}
461
462/**
463 * boot_get_ramdisk - main ramdisk handling routine
464 * @argc: command argument count
465 * @argv: command argument list
466 * @images: pointer to the bootm images structure
467 * @arch: expected ramdisk architecture
468 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
469 * @rd_end: pointer to a ulong variable, will hold ramdisk end
470 *
471 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
472 * Currently supported are the following ramdisk sources:
473 * - multicomponent kernel/ramdisk image,
474 * - commandline provided address of decicated ramdisk image.
475 *
476 * returns:
477 * 0, if ramdisk image was found and valid, or skiped
478 * rd_start and rd_end are set to ramdisk start/end addresses if
479 * ramdisk image is found and valid
480 *
481 * 1, if ramdisk image is found but corrupted, or invalid
482 * rd_start and rd_end are set to 0 if no ramdisk exists
483 */
484int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
485 u8 arch, ulong *rd_start, ulong *rd_end)
486{
487 ulong rd_data, rd_len;
488 const char *select = NULL;
489
490 *rd_start = 0;
491 *rd_end = 0;
492
493 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
494 char *buf;
495
496 /* Look for an Android boot image */
497 buf = map_sysmem(images->os.start, 0);
498 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
499 select = (argc == 0) ? env_get("loadaddr") : argv[0];
500 }
501
502 if (argc >= 2)
503 select = argv[1];
504
505 /*
506 * Look for a '-' which indicates to ignore the
507 * ramdisk argument
508 */
509 if (select && strcmp(select, "-") == 0) {
510 debug("## Skipping init Ramdisk\n");
511 rd_len = 0;
512 rd_data = 0;
513 } else if (select || genimg_has_config(images)) {
514 int ret;
515
516 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
517 if (ret == -ENOPKG)
518 return 0;
519 else if (ret)
520 return ret;
Simon Glass41506ff2021-09-25 07:03:15 -0600521 } else if (images->legacy_hdr_valid &&
522 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600523 IH_TYPE_MULTI)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600524 /*
525 * Now check if we have a legacy mult-component image,
526 * get second entry data start address and len.
527 */
528 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600529 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
530 (ulong)images->legacy_hdr_os);
Simon Glass41506ff2021-09-25 07:03:15 -0600531
532 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
533 } else {
534 /*
535 * no initrd image
536 */
537 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600538 rd_len = 0;
539 rd_data = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600540 }
541
542 if (!rd_data) {
543 debug("## No init Ramdisk\n");
544 } else {
545 *rd_start = rd_data;
546 *rd_end = rd_data + rd_len;
547 }
548 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600549 *rd_start, *rd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600550
551 return 0;
552}
553
Ashok Reddy Soma65168912022-07-07 10:45:37 +0200554#if defined(CONFIG_LMB)
Simon Glass41506ff2021-09-25 07:03:15 -0600555/**
556 * boot_ramdisk_high - relocate init ramdisk
557 * @lmb: pointer to lmb handle, will be used for memory mgmt
558 * @rd_data: ramdisk data start address
559 * @rd_len: ramdisk data length
560 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
561 * start address (after possible relocation)
562 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
563 * end address (after possible relocation)
564 *
565 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
566 * variable and if requested ramdisk data is moved to a specified location.
567 *
568 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
569 * start/end addresses if ramdisk image start and len were provided,
570 * otherwise set initrd_start and initrd_end set to zeros.
571 *
572 * returns:
573 * 0 - success
574 * -1 - failure
575 */
576int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600577 ulong *initrd_start, ulong *initrd_end)
Simon Glass41506ff2021-09-25 07:03:15 -0600578{
579 char *s;
580 ulong initrd_high;
581 int initrd_copy_to_ram = 1;
582
583 s = env_get("initrd_high");
584 if (s) {
585 /* a value of "no" or a similar string will act like 0,
586 * turning the "load high" feature off. This is intentional.
587 */
588 initrd_high = hextoul(s, NULL);
589 if (initrd_high == ~0)
590 initrd_copy_to_ram = 0;
591 } else {
592 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
593 }
594
Simon Glass41506ff2021-09-25 07:03:15 -0600595 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600596 initrd_high, initrd_copy_to_ram);
Simon Glass41506ff2021-09-25 07:03:15 -0600597
598 if (rd_data) {
599 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
600 debug(" in-place initrd\n");
601 *initrd_start = rd_data;
602 *initrd_end = rd_data + rd_len;
603 lmb_reserve(lmb, rd_data, rd_len);
604 } else {
605 if (initrd_high)
606 *initrd_start = (ulong)lmb_alloc_base(lmb,
607 rd_len, 0x1000, initrd_high);
608 else
609 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
610 0x1000);
611
612 if (*initrd_start == 0) {
613 puts("ramdisk - allocation error\n");
614 goto error;
615 }
616 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
617
618 *initrd_end = *initrd_start + rd_len;
619 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600620 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600621
622 memmove_wd((void *)*initrd_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600623 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass41506ff2021-09-25 07:03:15 -0600624
Simon Glass41506ff2021-09-25 07:03:15 -0600625 /*
626 * Ensure the image is flushed to memory to handle
627 * AMP boot scenarios in which we might not be
628 * HW cache coherent
629 */
Simon Glass1df654a2021-09-25 19:43:35 -0600630 if (IS_ENABLED(CONFIG_MP)) {
631 flush_cache((unsigned long)*initrd_start,
632 ALIGN(rd_len, ARCH_DMA_MINALIGN));
633 }
Simon Glass41506ff2021-09-25 07:03:15 -0600634 puts("OK\n");
635 }
636 } else {
637 *initrd_start = 0;
638 *initrd_end = 0;
639 }
640 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600641 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600642
643 return 0;
644
645error:
646 return -1;
647}
Ashok Reddy Soma65168912022-07-07 10:45:37 +0200648#endif
Simon Glass41506ff2021-09-25 07:03:15 -0600649
Simon Glass3d2a47f2021-09-25 07:03:16 -0600650int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass41506ff2021-09-25 07:03:15 -0600651 ulong *setup_start, ulong *setup_len)
652{
Simon Glass1df654a2021-09-25 19:43:35 -0600653 if (!CONFIG_IS_ENABLED(FIT))
654 return -ENOENT;
655
Simon Glass41506ff2021-09-25 07:03:15 -0600656 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600657}
658
Simon Glass41506ff2021-09-25 07:03:15 -0600659int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600660 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600661{
662 ulong tmp_img_addr, img_data, img_len;
663 void *buf;
664 int conf_noffset;
665 int fit_img_result;
666 const char *uname, *name;
667 int err;
668 int devnum = 0; /* TODO support multi fpga platforms */
669
Simon Glass1df654a2021-09-25 19:43:35 -0600670 if (!IS_ENABLED(CONFIG_FPGA))
671 return -ENOSYS;
672
Simon Glass41506ff2021-09-25 07:03:15 -0600673 /* Check to see if the images struct has a FIT configuration */
674 if (!genimg_has_config(images)) {
675 debug("## FIT configuration was not specified\n");
676 return 0;
677 }
678
679 /*
680 * Obtain the os FIT header from the images struct
681 */
682 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
683 buf = map_sysmem(tmp_img_addr, 0);
684 /*
685 * Check image type. For FIT images get FIT node
686 * and attempt to locate a generic binary.
687 */
688 switch (genimg_get_format(buf)) {
689 case IMAGE_FORMAT_FIT:
690 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
691
692 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
693 NULL);
694 if (!uname) {
695 debug("## FPGA image is not specified\n");
696 return 0;
697 }
698 fit_img_result = fit_image_load(images,
699 tmp_img_addr,
700 (const char **)&uname,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600701 &images->fit_uname_cfg,
Simon Glass41506ff2021-09-25 07:03:15 -0600702 arch,
703 IH_TYPE_FPGA,
704 BOOTSTAGE_ID_FPGA_INIT,
705 FIT_LOAD_OPTIONAL_NON_ZERO,
706 &img_data, &img_len);
707
708 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
709 uname, img_data, img_len);
710
711 if (fit_img_result < 0) {
712 /* Something went wrong! */
713 return fit_img_result;
714 }
715
716 if (!fpga_is_partial_data(devnum, img_len)) {
717 name = "full";
718 err = fpga_loadbitstream(devnum, (char *)img_data,
719 img_len, BIT_FULL);
720 if (err)
721 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300722 img_len, BIT_FULL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600723 } else {
724 name = "partial";
725 err = fpga_loadbitstream(devnum, (char *)img_data,
726 img_len, BIT_PARTIAL);
727 if (err)
728 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov282eed52022-07-22 17:16:07 +0300729 img_len, BIT_PARTIAL, 0);
Simon Glass41506ff2021-09-25 07:03:15 -0600730 }
731
732 if (err)
733 return err;
734
735 printf(" Programming %s bitstream... OK\n", name);
736 break;
737 default:
738 printf("The given image format is not supported (corrupt?)\n");
739 return 1;
740 }
741
742 return 0;
743}
Simon Glass41506ff2021-09-25 07:03:15 -0600744
Simon Glass3d2a47f2021-09-25 07:03:16 -0600745static void fit_loadable_process(u8 img_type,
Simon Glass41506ff2021-09-25 07:03:15 -0600746 ulong img_data,
747 ulong img_len)
748{
749 int i;
750 const unsigned int count =
751 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
752 struct fit_loadable_tbl *fit_loadable_handler =
753 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
754 /* For each loadable handler */
755 for (i = 0; i < count; i++, fit_loadable_handler++)
756 /* matching this type */
757 if (fit_loadable_handler->type == img_type)
758 /* call that handler with this image data */
759 fit_loadable_handler->handler(img_data, img_len);
760}
761
762int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600763 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600764{
765 /*
766 * These variables are used to hold the current image location
767 * in system memory.
768 */
769 ulong tmp_img_addr;
770 /*
771 * These two variables are requirements for fit_image_load, but
772 * their values are not used
773 */
774 ulong img_data, img_len;
775 void *buf;
776 int loadables_index;
777 int conf_noffset;
778 int fit_img_result;
779 const char *uname;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600780 u8 img_type;
Simon Glass41506ff2021-09-25 07:03:15 -0600781
782 /* Check to see if the images struct has a FIT configuration */
783 if (!genimg_has_config(images)) {
784 debug("## FIT configuration was not specified\n");
785 return 0;
786 }
787
788 /*
789 * Obtain the os FIT header from the images struct
790 */
791 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
792 buf = map_sysmem(tmp_img_addr, 0);
793 /*
794 * Check image type. For FIT images get FIT node
795 * and attempt to locate a generic binary.
796 */
797 switch (genimg_get_format(buf)) {
798 case IMAGE_FORMAT_FIT:
799 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
800
801 for (loadables_index = 0;
802 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600803 FIT_LOADABLE_PROP,
804 loadables_index, NULL), uname;
805 loadables_index++) {
806 fit_img_result = fit_image_load(images, tmp_img_addr,
807 &uname,
808 &images->fit_uname_cfg,
809 arch, IH_TYPE_LOADABLE,
810 BOOTSTAGE_ID_FIT_LOADABLE_START,
811 FIT_LOAD_OPTIONAL_NON_ZERO,
812 &img_data, &img_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600813 if (fit_img_result < 0) {
814 /* Something went wrong! */
815 return fit_img_result;
816 }
817
818 fit_img_result = fit_image_get_node(buf, uname);
819 if (fit_img_result < 0) {
820 /* Something went wrong! */
821 return fit_img_result;
822 }
823 fit_img_result = fit_image_get_type(buf,
824 fit_img_result,
825 &img_type);
826 if (fit_img_result < 0) {
827 /* Something went wrong! */
828 return fit_img_result;
829 }
830
831 fit_loadable_process(img_type, img_data, img_len);
832 }
833 break;
834 default:
835 printf("The given image format is not supported (corrupt?)\n");
836 return 1;
837 }
838
839 return 0;
840}
Simon Glass41506ff2021-09-25 07:03:15 -0600841
Ashok Reddy Soma65168912022-07-07 10:45:37 +0200842#if defined(CONFIG_LMB)
Tom Rini68894122022-05-12 10:02:06 -0400843#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Simon Glass41506ff2021-09-25 07:03:15 -0600844/**
845 * boot_get_cmdline - allocate and initialize kernel cmdline
846 * @lmb: pointer to lmb handle, will be used for memory mgmt
847 * @cmd_start: pointer to a ulong variable, will hold cmdline start
848 * @cmd_end: pointer to a ulong variable, will hold cmdline end
849 *
850 * boot_get_cmdline() allocates space for kernel command line below
851 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
852 * variable is present its contents is copied to allocated kernel
853 * command line.
854 *
855 * returns:
856 * 0 - success
857 * -1 - failure
858 */
859int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
860{
861 char *cmdline;
862 char *s;
863
864 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
865 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass3d2a47f2021-09-25 07:03:16 -0600866 if (!cmdline)
Simon Glass41506ff2021-09-25 07:03:15 -0600867 return -1;
868
869 s = env_get("bootargs");
870 if (!s)
871 s = "";
872
873 strcpy(cmdline, s);
874
Simon Glass3d2a47f2021-09-25 07:03:16 -0600875 *cmd_start = (ulong)cmdline;
Simon Glass41506ff2021-09-25 07:03:15 -0600876 *cmd_end = *cmd_start + strlen(cmdline);
877
878 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
879
880 return 0;
881}
Simon Glass41506ff2021-09-25 07:03:15 -0600882
Simon Glass41506ff2021-09-25 07:03:15 -0600883/**
884 * boot_get_kbd - allocate and initialize kernel copy of board info
885 * @lmb: pointer to lmb handle, will be used for memory mgmt
886 * @kbd: double pointer to board info data
887 *
888 * boot_get_kbd() allocates space for kernel copy of board info data below
889 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
890 * with the current u-boot board info data.
891 *
892 * returns:
893 * 0 - success
894 * -1 - failure
895 */
896int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
897{
898 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
899 sizeof(struct bd_info),
900 0xf,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600901 env_get_bootm_mapsize() +
902 env_get_bootm_low());
903 if (!*kbd)
Simon Glass41506ff2021-09-25 07:03:15 -0600904 return -1;
905
Simon Glass3d2a47f2021-09-25 07:03:16 -0600906 **kbd = *gd->bd;
Simon Glass41506ff2021-09-25 07:03:15 -0600907
908 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
909
Simon Glass4ed37ab2021-09-25 07:03:20 -0600910#if defined(DEBUG)
Leo Yu-Chi Liang990e1e42021-10-27 17:00:41 +0800911 if (IS_ENABLED(CONFIG_CMD_BDI))
Simon Glass4ed37ab2021-09-25 07:03:20 -0600912 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass41506ff2021-09-25 07:03:15 -0600913#endif
914
915 return 0;
916}
Tom Rini68894122022-05-12 10:02:06 -0400917#endif
Simon Glass41506ff2021-09-25 07:03:15 -0600918
Simon Glass41506ff2021-09-25 07:03:15 -0600919int image_setup_linux(bootm_headers_t *images)
920{
921 ulong of_size = images->ft_len;
922 char **of_flat_tree = &images->ft_addr;
923 struct lmb *lmb = &images->lmb;
924 int ret;
925
Simon Glass0c303f92021-09-25 19:43:21 -0600926 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass41506ff2021-09-25 07:03:15 -0600927 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
928
Simon Glass806d1ff2021-09-25 19:43:25 -0600929 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600930 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600931 &images->cmdline_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600932 if (ret) {
933 puts("ERROR with allocation of cmdline\n");
934 return ret;
935 }
936 }
937
Simon Glass0c303f92021-09-25 19:43:21 -0600938 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600939 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
940 if (ret)
941 return ret;
942 }
943
Simon Glass0c303f92021-09-25 19:43:21 -0600944 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass41506ff2021-09-25 07:03:15 -0600945 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
946 if (ret)
947 return ret;
948 }
949
950 return 0;
951}
Ashok Reddy Soma65168912022-07-07 10:45:37 +0200952#endif
Simon Glass5d3248a2021-09-25 07:03:17 -0600953
954void genimg_print_size(uint32_t size)
955{
956 printf("%d Bytes = ", size);
957 print_size(size, "\n");
958}
959
960void genimg_print_time(time_t timestamp)
961{
962 struct rtc_time tm;
963
964 rtc_to_tm(timestamp, &tm);
965 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
966 tm.tm_year, tm.tm_mon, tm.tm_mday,
967 tm.tm_hour, tm.tm_min, tm.tm_sec);
968}