blob: cfc1c658e3a612d3de26a1df1cb51a962c988d16 [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>
14#include <env.h>
15#include <fpga.h>
16#include <image.h>
Andy Shevchenko4b325312021-11-08 21:03:38 +030017#include <init.h>
Simon Glass41506ff2021-09-25 07:03:15 -060018#include <mapmem.h>
Simon Glass5d3248a2021-09-25 07:03:17 -060019#include <rtc.h>
Simon Glass41506ff2021-09-25 07:03:15 -060020#include <watchdog.h>
21#include <asm/cache.h>
22#include <asm/global_data.h>
23
Simon Glass41506ff2021-09-25 07:03:15 -060024DECLARE_GLOBAL_DATA_PTR;
25
Tom Rini621158d2021-12-20 09:36:32 -050026#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
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}
Tom Rini621158d2021-12-20 09:36:32 -050085#endif
Simon Glass41506ff2021-09-25 07:03:15 -060086
87/*****************************************************************************/
88/* Shared dual-format routines */
89/*****************************************************************************/
90ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
91ulong image_save_addr; /* Default Save Address */
92ulong image_save_size; /* Default Save Size (in bytes) */
93
94static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass3d2a47f2021-09-25 07:03:16 -060095 int flags)
Simon Glass41506ff2021-09-25 07:03:15 -060096{
97 switch (op) {
98 case env_op_create:
99 case env_op_overwrite:
100 image_load_addr = hextoul(value, NULL);
101 break;
102 default:
103 break;
104 }
105
106 return 0;
107}
108U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
110ulong env_get_bootm_low(void)
111{
112 char *s = env_get("bootm_low");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600113
Simon Glass41506ff2021-09-25 07:03:15 -0600114 if (s) {
115 ulong tmp = hextoul(s, NULL);
116 return tmp;
117 }
118
119#if defined(CONFIG_SYS_SDRAM_BASE)
120 return CONFIG_SYS_SDRAM_BASE;
121#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
122 return gd->bd->bi_dram[0].start;
123#else
124 return 0;
125#endif
126}
127
128phys_size_t env_get_bootm_size(void)
129{
130 phys_size_t tmp, size;
131 phys_addr_t start;
132 char *s = env_get("bootm_size");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600133
Simon Glass41506ff2021-09-25 07:03:15 -0600134 if (s) {
135 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
136 return tmp;
137 }
138
139 start = gd->ram_base;
140 size = gd->ram_size;
141
142 if (start + size > gd->ram_top)
143 size = gd->ram_top - start;
144
145 s = env_get("bootm_low");
146 if (s)
147 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
148 else
149 tmp = start;
150
151 return size - (tmp - start);
152}
153
154phys_size_t env_get_bootm_mapsize(void)
155{
156 phys_size_t tmp;
157 char *s = env_get("bootm_mapsize");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600158
Simon Glass41506ff2021-09-25 07:03:15 -0600159 if (s) {
160 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
161 return tmp;
162 }
163
164#if defined(CONFIG_SYS_BOOTMAPSZ)
165 return CONFIG_SYS_BOOTMAPSZ;
166#else
167 return env_get_bootm_size();
168#endif
169}
170
171void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
172{
173 if (to == from)
174 return;
175
176#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
177 if (to > from) {
178 from += len;
179 to += len;
180 }
181 while (len > 0) {
182 size_t tail = (len > chunksz) ? chunksz : len;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600183
Simon Glass41506ff2021-09-25 07:03:15 -0600184 WATCHDOG_RESET();
185 if (to > from) {
186 to -= tail;
187 from -= tail;
188 }
189 memmove(to, from, tail);
190 if (to < from) {
191 to += tail;
192 from += tail;
193 }
194 len -= tail;
195 }
196#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
197 memmove(to, from, len);
198#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
199}
200
201/**
202 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
203 * FIT strings
204 * @img_addr: a string might contain real image address
205 * @fit_uname_config: double pointer to a char, will hold pointer to a
206 * configuration unit name
207 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
208 * name
209 *
210 * genimg_get_kernel_addr_fit get the real kernel start address from a string
211 * which is normally the first argv of bootm/bootz
212 *
213 * returns:
214 * kernel start address
215 */
216ulong genimg_get_kernel_addr_fit(char * const img_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600217 const char **fit_uname_config,
218 const char **fit_uname_kernel)
Simon Glass41506ff2021-09-25 07:03:15 -0600219{
220 ulong kernel_addr;
221
222 /* find out kernel image address */
223 if (!img_addr) {
224 kernel_addr = image_load_addr;
225 debug("* kernel: default image load address = 0x%08lx\n",
226 image_load_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600227 } else if (CONFIG_IS_ENABLED(FIT) &&
228 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass41506ff2021-09-25 07:03:15 -0600229 fit_uname_config)) {
230 debug("* kernel: config '%s' from image at 0x%08lx\n",
231 *fit_uname_config, kernel_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600232 } else if (CONFIG_IS_ENABLED(FIT) &&
233 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
234 fit_uname_kernel)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600235 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
236 *fit_uname_kernel, kernel_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600237 } else {
238 kernel_addr = hextoul(img_addr, NULL);
239 debug("* kernel: cmdline image address = 0x%08lx\n",
240 kernel_addr);
241 }
242
243 return kernel_addr;
244}
245
246/**
247 * genimg_get_kernel_addr() is the simple version of
248 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
249 */
250ulong genimg_get_kernel_addr(char * const img_addr)
251{
252 const char *fit_uname_config = NULL;
253 const char *fit_uname_kernel = NULL;
254
255 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
256 &fit_uname_kernel);
257}
258
259/**
260 * genimg_get_format - get image format type
261 * @img_addr: image start address
262 *
263 * genimg_get_format() checks whether provided address points to a valid
264 * legacy or FIT image.
265 *
266 * New uImage format and FDT blob are based on a libfdt. FDT blob
267 * may be passed directly or embedded in a FIT image. In both situations
268 * genimg_get_format() must be able to dectect libfdt header.
269 *
270 * returns:
271 * image format type or IMAGE_FORMAT_INVALID if no image is present
272 */
273int genimg_get_format(const void *img_addr)
274{
Simon Glass1df654a2021-09-25 19:43:35 -0600275 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
276 const image_header_t *hdr;
Simon Glass41506ff2021-09-25 07:03:15 -0600277
Simon Glass1df654a2021-09-25 19:43:35 -0600278 hdr = (const image_header_t *)img_addr;
279 if (image_check_magic(hdr))
280 return IMAGE_FORMAT_LEGACY;
281 }
282 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
283 if (!fdt_check_header(img_addr))
284 return IMAGE_FORMAT_FIT;
285 }
286 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
287 !android_image_check_header(img_addr))
Simon Glass41506ff2021-09-25 07:03:15 -0600288 return IMAGE_FORMAT_ANDROID;
Simon Glass41506ff2021-09-25 07:03:15 -0600289
290 return IMAGE_FORMAT_INVALID;
291}
292
293/**
294 * fit_has_config - check if there is a valid FIT configuration
295 * @images: pointer to the bootm command headers structure
296 *
297 * fit_has_config() checks if there is a FIT configuration in use
298 * (if FTI support is present).
299 *
300 * returns:
301 * 0, no FIT support or no configuration found
302 * 1, configuration found
303 */
304int genimg_has_config(bootm_headers_t *images)
305{
Simon Glass1df654a2021-09-25 19:43:35 -0600306 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass41506ff2021-09-25 07:03:15 -0600307 return 1;
Simon Glass1df654a2021-09-25 19:43:35 -0600308
Simon Glass41506ff2021-09-25 07:03:15 -0600309 return 0;
310}
311
312/**
Simon Glasse4c92872021-09-25 19:43:37 -0600313 * select_ramdisk() - Select and locate the ramdisk to use
314 *
Simon Glass41506ff2021-09-25 07:03:15 -0600315 * @images: pointer to the bootm images structure
Simon Glasse4c92872021-09-25 19:43:37 -0600316 * @select: name of ramdisk to select, or NULL for any
Simon Glass41506ff2021-09-25 07:03:15 -0600317 * @arch: expected ramdisk architecture
Simon Glasse4c92872021-09-25 19:43:37 -0600318 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
319 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100320 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glasse4c92872021-09-25 19:43:37 -0600321 * other -ve value on other error
Simon Glass41506ff2021-09-25 07:03:15 -0600322 */
Simon Glasse4c92872021-09-25 19:43:37 -0600323static int select_ramdisk(bootm_headers_t *images, const char *select, u8 arch,
324 ulong *rd_datap, ulong *rd_lenp)
Simon Glass41506ff2021-09-25 07:03:15 -0600325{
Tom Rini621158d2021-12-20 09:36:32 -0500326 ulong rd_addr;
Simon Glasse4c92872021-09-25 19:43:37 -0600327 char *buf;
Simon Glass78f88792021-09-25 19:43:36 -0600328
Tom Rini621158d2021-12-20 09:36:32 -0500329#if CONFIG_IS_ENABLED(FIT)
330 const char *fit_uname_config = images->fit_uname_cfg;
331 const char *fit_uname_ramdisk = NULL;
332 int rd_noffset;
Simon Glass78f88792021-09-25 19:43:36 -0600333
Tom Rini621158d2021-12-20 09:36:32 -0500334 if (select) {
335 ulong default_addr;
Simon Glass41506ff2021-09-25 07:03:15 -0600336 /*
337 * If the init ramdisk comes from the FIT image and
338 * the FIT image address is omitted in the command
339 * line argument, try to use os FIT image address or
340 * default load address.
341 */
342 if (images->fit_uname_os)
343 default_addr = (ulong)images->fit_hdr_os;
344 else
345 default_addr = image_load_addr;
346
Tom Rini621158d2021-12-20 09:36:32 -0500347 if (fit_parse_conf(select, default_addr,
348 &rd_addr, &fit_uname_config)) {
Simon Glass3d2a47f2021-09-25 07:03:16 -0600349 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
350 fit_uname_config, rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600351 } else if (fit_parse_subimage(select, default_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600352 &rd_addr,
353 &fit_uname_ramdisk)) {
354 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
355 fit_uname_ramdisk, rd_addr);
Tom Rini621158d2021-12-20 09:36:32 -0500356 } else
357#endif
358 {
359 rd_addr = hextoul(select, NULL);
360 debug("* ramdisk: cmdline image address = 0x%08lx\n",
361 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600362 }
Tom Rini621158d2021-12-20 09:36:32 -0500363#if CONFIG_IS_ENABLED(FIT)
364 } else {
365 /* use FIT configuration provided in first bootm
366 * command argument. If the property is not defined,
367 * quit silently (with -ENOPKG)
368 */
369 rd_addr = map_to_sysmem(images->fit_hdr_os);
370 rd_noffset = fit_get_node_from_config(images,
371 FIT_RAMDISK_PROP,
372 rd_addr);
373 if (rd_noffset == -ENOENT)
374 return -ENOPKG;
375 else if (rd_noffset < 0)
376 return rd_noffset;
Simon Glass41506ff2021-09-25 07:03:15 -0600377 }
Tom Rini621158d2021-12-20 09:36:32 -0500378#endif
Simon Glassf33a2c12021-09-25 19:43:38 -0600379
Tom Rini621158d2021-12-20 09:36:32 -0500380 /*
381 * Check if there is an initrd image at the
382 * address provided in the second bootm argument
383 * check image type, for FIT images get FIT node.
384 */
385 buf = map_sysmem(rd_addr, 0);
386 switch (genimg_get_format(buf)) {
387#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
388 case IMAGE_FORMAT_LEGACY: {
Simon Glass78f88792021-09-25 19:43:36 -0600389 const image_header_t *rd_hdr;
390
Simon Glass3d2a47f2021-09-25 07:03:16 -0600391 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
392 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600393
394 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Tom Rini621158d2021-12-20 09:36:32 -0500395 rd_hdr = image_get_ramdisk(rd_addr, arch,
396 images->verify);
397
Simon Glass3d2a47f2021-09-25 07:03:16 -0600398 if (!rd_hdr)
Simon Glasse4c92872021-09-25 19:43:37 -0600399 return -ENOENT;
Simon Glass41506ff2021-09-25 07:03:15 -0600400
Simon Glasse4c92872021-09-25 19:43:37 -0600401 *rd_datap = image_get_data(rd_hdr);
402 *rd_lenp = image_get_data_size(rd_hdr);
Tom Rini621158d2021-12-20 09:36:32 -0500403 break;
Simon Glass78f88792021-09-25 19:43:36 -0600404 }
Tom Rini621158d2021-12-20 09:36:32 -0500405#endif
406#if CONFIG_IS_ENABLED(FIT)
407 case IMAGE_FORMAT_FIT:
408 rd_noffset = fit_image_load(images,
409 rd_addr, &fit_uname_ramdisk,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600410 &fit_uname_config, arch,
411 IH_TYPE_RAMDISK,
412 BOOTSTAGE_ID_FIT_RD_START,
413 FIT_LOAD_OPTIONAL_NON_ZERO,
Simon Glasse4c92872021-09-25 19:43:37 -0600414 rd_datap, rd_lenp);
Simon Glass41506ff2021-09-25 07:03:15 -0600415 if (rd_noffset < 0)
Simon Glasse4c92872021-09-25 19:43:37 -0600416 return rd_noffset;
Simon Glass41506ff2021-09-25 07:03:15 -0600417
418 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
419 images->fit_uname_rd = fit_uname_ramdisk;
420 images->fit_noffset_rd = rd_noffset;
Tom Rini621158d2021-12-20 09:36:32 -0500421 break;
422#endif
423#ifdef CONFIG_ANDROID_BOOT_IMAGE
424 case IMAGE_FORMAT_ANDROID:
Simon Glass41506ff2021-09-25 07:03:15 -0600425 android_image_get_ramdisk((void *)images->os.start,
Simon Glasse4c92872021-09-25 19:43:37 -0600426 rd_datap, rd_lenp);
Tom Rini621158d2021-12-20 09:36:32 -0500427 break;
428#endif
429 default:
430 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
431 char *end = NULL;
Simon Glass1df654a2021-09-25 19:43:35 -0600432
Tom Rini621158d2021-12-20 09:36:32 -0500433 if (select)
434 end = strchr(select, ':');
435 if (end) {
436 *rd_lenp = hextoul(++end, NULL);
437 *rd_datap = rd_addr;
438 break;
439 }
Simon Glass41506ff2021-09-25 07:03:15 -0600440 }
Simon Glass1df654a2021-09-25 19:43:35 -0600441 puts("Wrong Ramdisk Image Format\n");
Simon Glasse4c92872021-09-25 19:43:37 -0600442 return -EINVAL;
Simon Glass41506ff2021-09-25 07:03:15 -0600443 }
Simon Glasse4c92872021-09-25 19:43:37 -0600444
445 return 0;
446}
447
448/**
449 * boot_get_ramdisk - main ramdisk handling routine
450 * @argc: command argument count
451 * @argv: command argument list
452 * @images: pointer to the bootm images structure
453 * @arch: expected ramdisk architecture
454 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
455 * @rd_end: pointer to a ulong variable, will hold ramdisk end
456 *
457 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
458 * Currently supported are the following ramdisk sources:
459 * - multicomponent kernel/ramdisk image,
460 * - commandline provided address of decicated ramdisk image.
461 *
462 * returns:
463 * 0, if ramdisk image was found and valid, or skiped
464 * rd_start and rd_end are set to ramdisk start/end addresses if
465 * ramdisk image is found and valid
466 *
467 * 1, if ramdisk image is found but corrupted, or invalid
468 * rd_start and rd_end are set to 0 if no ramdisk exists
469 */
470int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
471 u8 arch, ulong *rd_start, ulong *rd_end)
472{
473 ulong rd_data, rd_len;
474 const char *select = NULL;
475
476 *rd_start = 0;
477 *rd_end = 0;
478
479 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
480 char *buf;
481
482 /* Look for an Android boot image */
483 buf = map_sysmem(images->os.start, 0);
484 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
485 select = (argc == 0) ? env_get("loadaddr") : argv[0];
486 }
487
488 if (argc >= 2)
489 select = argv[1];
490
491 /*
492 * Look for a '-' which indicates to ignore the
493 * ramdisk argument
494 */
495 if (select && strcmp(select, "-") == 0) {
496 debug("## Skipping init Ramdisk\n");
497 rd_len = 0;
498 rd_data = 0;
499 } else if (select || genimg_has_config(images)) {
500 int ret;
501
502 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
503 if (ret == -ENOPKG)
504 return 0;
505 else if (ret)
506 return ret;
Simon Glass41506ff2021-09-25 07:03:15 -0600507 } else if (images->legacy_hdr_valid &&
508 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600509 IH_TYPE_MULTI)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600510 /*
511 * Now check if we have a legacy mult-component image,
512 * get second entry data start address and len.
513 */
514 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600515 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
516 (ulong)images->legacy_hdr_os);
Simon Glass41506ff2021-09-25 07:03:15 -0600517
518 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
519 } else {
520 /*
521 * no initrd image
522 */
523 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600524 rd_len = 0;
525 rd_data = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600526 }
527
528 if (!rd_data) {
529 debug("## No init Ramdisk\n");
530 } else {
531 *rd_start = rd_data;
532 *rd_end = rd_data + rd_len;
533 }
534 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600535 *rd_start, *rd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600536
537 return 0;
538}
539
Simon Glass41506ff2021-09-25 07:03:15 -0600540/**
541 * boot_ramdisk_high - relocate init ramdisk
542 * @lmb: pointer to lmb handle, will be used for memory mgmt
543 * @rd_data: ramdisk data start address
544 * @rd_len: ramdisk data length
545 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
546 * start address (after possible relocation)
547 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
548 * end address (after possible relocation)
549 *
550 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
551 * variable and if requested ramdisk data is moved to a specified location.
552 *
553 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
554 * start/end addresses if ramdisk image start and len were provided,
555 * otherwise set initrd_start and initrd_end set to zeros.
556 *
557 * returns:
558 * 0 - success
559 * -1 - failure
560 */
561int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600562 ulong *initrd_start, ulong *initrd_end)
Simon Glass41506ff2021-09-25 07:03:15 -0600563{
564 char *s;
565 ulong initrd_high;
566 int initrd_copy_to_ram = 1;
567
568 s = env_get("initrd_high");
569 if (s) {
570 /* a value of "no" or a similar string will act like 0,
571 * turning the "load high" feature off. This is intentional.
572 */
573 initrd_high = hextoul(s, NULL);
574 if (initrd_high == ~0)
575 initrd_copy_to_ram = 0;
576 } else {
577 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
578 }
579
Simon Glass41506ff2021-09-25 07:03:15 -0600580 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600581 initrd_high, initrd_copy_to_ram);
Simon Glass41506ff2021-09-25 07:03:15 -0600582
583 if (rd_data) {
584 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
585 debug(" in-place initrd\n");
586 *initrd_start = rd_data;
587 *initrd_end = rd_data + rd_len;
588 lmb_reserve(lmb, rd_data, rd_len);
589 } else {
590 if (initrd_high)
591 *initrd_start = (ulong)lmb_alloc_base(lmb,
592 rd_len, 0x1000, initrd_high);
593 else
594 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
595 0x1000);
596
597 if (*initrd_start == 0) {
598 puts("ramdisk - allocation error\n");
599 goto error;
600 }
601 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
602
603 *initrd_end = *initrd_start + rd_len;
604 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600605 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600606
607 memmove_wd((void *)*initrd_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600608 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass41506ff2021-09-25 07:03:15 -0600609
Simon Glass41506ff2021-09-25 07:03:15 -0600610 /*
611 * Ensure the image is flushed to memory to handle
612 * AMP boot scenarios in which we might not be
613 * HW cache coherent
614 */
Simon Glass1df654a2021-09-25 19:43:35 -0600615 if (IS_ENABLED(CONFIG_MP)) {
616 flush_cache((unsigned long)*initrd_start,
617 ALIGN(rd_len, ARCH_DMA_MINALIGN));
618 }
Simon Glass41506ff2021-09-25 07:03:15 -0600619 puts("OK\n");
620 }
621 } else {
622 *initrd_start = 0;
623 *initrd_end = 0;
624 }
625 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600626 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600627
628 return 0;
629
630error:
631 return -1;
632}
Simon Glass41506ff2021-09-25 07:03:15 -0600633
Simon Glass3d2a47f2021-09-25 07:03:16 -0600634int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass41506ff2021-09-25 07:03:15 -0600635 ulong *setup_start, ulong *setup_len)
636{
Simon Glass1df654a2021-09-25 19:43:35 -0600637 if (!CONFIG_IS_ENABLED(FIT))
638 return -ENOENT;
639
Simon Glass41506ff2021-09-25 07:03:15 -0600640 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600641}
642
Simon Glass41506ff2021-09-25 07:03:15 -0600643int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600644 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600645{
646 ulong tmp_img_addr, img_data, img_len;
647 void *buf;
648 int conf_noffset;
649 int fit_img_result;
650 const char *uname, *name;
651 int err;
652 int devnum = 0; /* TODO support multi fpga platforms */
653
Simon Glass1df654a2021-09-25 19:43:35 -0600654 if (!IS_ENABLED(CONFIG_FPGA))
655 return -ENOSYS;
656
Simon Glass41506ff2021-09-25 07:03:15 -0600657 /* Check to see if the images struct has a FIT configuration */
658 if (!genimg_has_config(images)) {
659 debug("## FIT configuration was not specified\n");
660 return 0;
661 }
662
663 /*
664 * Obtain the os FIT header from the images struct
665 */
666 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
667 buf = map_sysmem(tmp_img_addr, 0);
668 /*
669 * Check image type. For FIT images get FIT node
670 * and attempt to locate a generic binary.
671 */
672 switch (genimg_get_format(buf)) {
673 case IMAGE_FORMAT_FIT:
674 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
675
676 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
677 NULL);
678 if (!uname) {
679 debug("## FPGA image is not specified\n");
680 return 0;
681 }
682 fit_img_result = fit_image_load(images,
683 tmp_img_addr,
684 (const char **)&uname,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600685 &images->fit_uname_cfg,
Simon Glass41506ff2021-09-25 07:03:15 -0600686 arch,
687 IH_TYPE_FPGA,
688 BOOTSTAGE_ID_FPGA_INIT,
689 FIT_LOAD_OPTIONAL_NON_ZERO,
690 &img_data, &img_len);
691
692 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
693 uname, img_data, img_len);
694
695 if (fit_img_result < 0) {
696 /* Something went wrong! */
697 return fit_img_result;
698 }
699
700 if (!fpga_is_partial_data(devnum, img_len)) {
701 name = "full";
702 err = fpga_loadbitstream(devnum, (char *)img_data,
703 img_len, BIT_FULL);
704 if (err)
705 err = fpga_load(devnum, (const void *)img_data,
706 img_len, BIT_FULL);
707 } else {
708 name = "partial";
709 err = fpga_loadbitstream(devnum, (char *)img_data,
710 img_len, BIT_PARTIAL);
711 if (err)
712 err = fpga_load(devnum, (const void *)img_data,
713 img_len, BIT_PARTIAL);
714 }
715
716 if (err)
717 return err;
718
719 printf(" Programming %s bitstream... OK\n", name);
720 break;
721 default:
722 printf("The given image format is not supported (corrupt?)\n");
723 return 1;
724 }
725
726 return 0;
727}
Simon Glass41506ff2021-09-25 07:03:15 -0600728
Simon Glass3d2a47f2021-09-25 07:03:16 -0600729static void fit_loadable_process(u8 img_type,
Simon Glass41506ff2021-09-25 07:03:15 -0600730 ulong img_data,
731 ulong img_len)
732{
733 int i;
734 const unsigned int count =
735 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
736 struct fit_loadable_tbl *fit_loadable_handler =
737 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
738 /* For each loadable handler */
739 for (i = 0; i < count; i++, fit_loadable_handler++)
740 /* matching this type */
741 if (fit_loadable_handler->type == img_type)
742 /* call that handler with this image data */
743 fit_loadable_handler->handler(img_data, img_len);
744}
745
746int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600747 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600748{
749 /*
750 * These variables are used to hold the current image location
751 * in system memory.
752 */
753 ulong tmp_img_addr;
754 /*
755 * These two variables are requirements for fit_image_load, but
756 * their values are not used
757 */
758 ulong img_data, img_len;
759 void *buf;
760 int loadables_index;
761 int conf_noffset;
762 int fit_img_result;
763 const char *uname;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600764 u8 img_type;
Simon Glass41506ff2021-09-25 07:03:15 -0600765
766 /* Check to see if the images struct has a FIT configuration */
767 if (!genimg_has_config(images)) {
768 debug("## FIT configuration was not specified\n");
769 return 0;
770 }
771
772 /*
773 * Obtain the os FIT header from the images struct
774 */
775 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
776 buf = map_sysmem(tmp_img_addr, 0);
777 /*
778 * Check image type. For FIT images get FIT node
779 * and attempt to locate a generic binary.
780 */
781 switch (genimg_get_format(buf)) {
782 case IMAGE_FORMAT_FIT:
783 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
784
785 for (loadables_index = 0;
786 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600787 FIT_LOADABLE_PROP,
788 loadables_index, NULL), uname;
789 loadables_index++) {
790 fit_img_result = fit_image_load(images, tmp_img_addr,
791 &uname,
792 &images->fit_uname_cfg,
793 arch, IH_TYPE_LOADABLE,
794 BOOTSTAGE_ID_FIT_LOADABLE_START,
795 FIT_LOAD_OPTIONAL_NON_ZERO,
796 &img_data, &img_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600797 if (fit_img_result < 0) {
798 /* Something went wrong! */
799 return fit_img_result;
800 }
801
802 fit_img_result = fit_image_get_node(buf, uname);
803 if (fit_img_result < 0) {
804 /* Something went wrong! */
805 return fit_img_result;
806 }
807 fit_img_result = fit_image_get_type(buf,
808 fit_img_result,
809 &img_type);
810 if (fit_img_result < 0) {
811 /* Something went wrong! */
812 return fit_img_result;
813 }
814
815 fit_loadable_process(img_type, img_data, img_len);
816 }
817 break;
818 default:
819 printf("The given image format is not supported (corrupt?)\n");
820 return 1;
821 }
822
823 return 0;
824}
Simon Glass41506ff2021-09-25 07:03:15 -0600825
Tom Rini68894122022-05-12 10:02:06 -0400826#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Simon Glass41506ff2021-09-25 07:03:15 -0600827/**
828 * boot_get_cmdline - allocate and initialize kernel cmdline
829 * @lmb: pointer to lmb handle, will be used for memory mgmt
830 * @cmd_start: pointer to a ulong variable, will hold cmdline start
831 * @cmd_end: pointer to a ulong variable, will hold cmdline end
832 *
833 * boot_get_cmdline() allocates space for kernel command line below
834 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
835 * variable is present its contents is copied to allocated kernel
836 * command line.
837 *
838 * returns:
839 * 0 - success
840 * -1 - failure
841 */
842int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
843{
844 char *cmdline;
845 char *s;
846
847 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
848 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass3d2a47f2021-09-25 07:03:16 -0600849 if (!cmdline)
Simon Glass41506ff2021-09-25 07:03:15 -0600850 return -1;
851
852 s = env_get("bootargs");
853 if (!s)
854 s = "";
855
856 strcpy(cmdline, s);
857
Simon Glass3d2a47f2021-09-25 07:03:16 -0600858 *cmd_start = (ulong)cmdline;
Simon Glass41506ff2021-09-25 07:03:15 -0600859 *cmd_end = *cmd_start + strlen(cmdline);
860
861 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
862
863 return 0;
864}
Simon Glass41506ff2021-09-25 07:03:15 -0600865
Simon Glass41506ff2021-09-25 07:03:15 -0600866/**
867 * boot_get_kbd - allocate and initialize kernel copy of board info
868 * @lmb: pointer to lmb handle, will be used for memory mgmt
869 * @kbd: double pointer to board info data
870 *
871 * boot_get_kbd() allocates space for kernel copy of board info data below
872 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
873 * with the current u-boot board info data.
874 *
875 * returns:
876 * 0 - success
877 * -1 - failure
878 */
879int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
880{
881 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
882 sizeof(struct bd_info),
883 0xf,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600884 env_get_bootm_mapsize() +
885 env_get_bootm_low());
886 if (!*kbd)
Simon Glass41506ff2021-09-25 07:03:15 -0600887 return -1;
888
Simon Glass3d2a47f2021-09-25 07:03:16 -0600889 **kbd = *gd->bd;
Simon Glass41506ff2021-09-25 07:03:15 -0600890
891 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
892
Simon Glass4ed37ab2021-09-25 07:03:20 -0600893#if defined(DEBUG)
Leo Yu-Chi Liang990e1e42021-10-27 17:00:41 +0800894 if (IS_ENABLED(CONFIG_CMD_BDI))
Simon Glass4ed37ab2021-09-25 07:03:20 -0600895 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass41506ff2021-09-25 07:03:15 -0600896#endif
897
898 return 0;
899}
Tom Rini68894122022-05-12 10:02:06 -0400900#endif
Simon Glass41506ff2021-09-25 07:03:15 -0600901
Simon Glass41506ff2021-09-25 07:03:15 -0600902int image_setup_linux(bootm_headers_t *images)
903{
904 ulong of_size = images->ft_len;
905 char **of_flat_tree = &images->ft_addr;
906 struct lmb *lmb = &images->lmb;
907 int ret;
908
Simon Glass0c303f92021-09-25 19:43:21 -0600909 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass41506ff2021-09-25 07:03:15 -0600910 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
911
Simon Glass806d1ff2021-09-25 19:43:25 -0600912 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600913 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600914 &images->cmdline_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600915 if (ret) {
916 puts("ERROR with allocation of cmdline\n");
917 return ret;
918 }
919 }
920
Simon Glass0c303f92021-09-25 19:43:21 -0600921 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600922 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
923 if (ret)
924 return ret;
925 }
926
Simon Glass0c303f92021-09-25 19:43:21 -0600927 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass41506ff2021-09-25 07:03:15 -0600928 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
929 if (ret)
930 return ret;
931 }
932
933 return 0;
934}
Simon Glass5d3248a2021-09-25 07:03:17 -0600935
936void genimg_print_size(uint32_t size)
937{
938 printf("%d Bytes = ", size);
939 print_size(size, "\n");
940}
941
942void genimg_print_time(time_t timestamp)
943{
944 struct rtc_time tm;
945
946 rtc_to_tm(timestamp, &tm);
947 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
948 tm.tm_year, tm.tm_mon, tm.tm_mday,
949 tm.tm_hour, tm.tm_min, tm.tm_sec);
950}