blob: b846bff2491e3bd038b7793cb02cd1b7b6017bfe [file] [log] [blame]
Simon Glass4d7237b2021-09-25 07:03:15 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Image code used by boards (and not host tools)
4 *
5 * (C) Copyright 2008 Semihalf
6 *
7 * (C) Copyright 2000-2006
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 */
10
11#include <common.h>
12#include <bootstage.h>
13#include <cpu_func.h>
14#include <env.h>
15#include <fpga.h>
16#include <image.h>
Andy Shevchenkoe3b3ad92021-11-08 21:03:38 +030017#include <init.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060018#include <mapmem.h>
Simon Glass4a8a8a12021-09-25 07:03:17 -060019#include <rtc.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060020#include <watchdog.h>
21#include <asm/cache.h>
22#include <asm/global_data.h>
23
Simon Glass4d7237b2021-09-25 07:03:15 -060024DECLARE_GLOBAL_DATA_PTR;
25
Tom Rinibc7d9d22021-12-20 09:36:32 -050026#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -060042static const image_header_t *image_get_ramdisk(ulong rd_addr, u8 arch,
43 int verify)
Simon Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -060078 genimg_get_arch_name(arch));
Simon Glass4d7237b2021-09-25 07:03:15 -060079 bootstage_error(BOOTSTAGE_ID_RAMDISK);
80 return NULL;
81 }
82
83 return rd_hdr;
84}
Tom Rinibc7d9d22021-12-20 09:36:32 -050085#endif
Simon Glass4d7237b2021-09-25 07:03:15 -060086
87/*****************************************************************************/
88/* Shared dual-format routines */
89/*****************************************************************************/
90ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
91ulong image_save_addr; /* Default Save Address */
92ulong image_save_size; /* Default Save Size (in bytes) */
93
94static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass60d71542021-09-25 07:03:16 -060095 int flags)
Simon Glass4d7237b2021-09-25 07:03:15 -060096{
97 switch (op) {
98 case env_op_create:
99 case env_op_overwrite:
100 image_load_addr = hextoul(value, NULL);
101 break;
102 default:
103 break;
104 }
105
106 return 0;
107}
108U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
110ulong env_get_bootm_low(void)
111{
112 char *s = env_get("bootm_low");
Simon Glass60d71542021-09-25 07:03:16 -0600113
Simon Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -0600133
Simon Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -0600158
Simon Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -0600183
Simon Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -0600217 const char **fit_uname_config,
218 const char **fit_uname_kernel)
Simon Glass4d7237b2021-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 Glass0f781382021-09-25 19:43:35 -0600227 } else if (CONFIG_IS_ENABLED(FIT) &&
228 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass4d7237b2021-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 Glass0f781382021-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 Glass4d7237b2021-09-25 07:03:15 -0600235 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
236 *fit_uname_kernel, kernel_addr);
Simon Glass4d7237b2021-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 Glass0f781382021-09-25 19:43:35 -0600275 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
276 const image_header_t *hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600277
Simon Glass0f781382021-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 Glass4d7237b2021-09-25 07:03:15 -0600288 return IMAGE_FORMAT_ANDROID;
Simon Glass4d7237b2021-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 Glass0f781382021-09-25 19:43:35 -0600306 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass4d7237b2021-09-25 07:03:15 -0600307 return 1;
Simon Glass0f781382021-09-25 19:43:35 -0600308
Simon Glass4d7237b2021-09-25 07:03:15 -0600309 return 0;
310}
311
312/**
Simon Glassccda2fa2021-09-25 19:43:37 -0600313 * select_ramdisk() - Select and locate the ramdisk to use
314 *
Simon Glass4d7237b2021-09-25 07:03:15 -0600315 * @images: pointer to the bootm images structure
Simon Glassccda2fa2021-09-25 19:43:37 -0600316 * @select: name of ramdisk to select, or NULL for any
Simon Glass4d7237b2021-09-25 07:03:15 -0600317 * @arch: expected ramdisk architecture
Simon Glassccda2fa2021-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 Schuchardt47b4c022022-01-19 18:05:50 +0100320 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glassccda2fa2021-09-25 19:43:37 -0600321 * other -ve value on other error
Simon Glass4d7237b2021-09-25 07:03:15 -0600322 */
Simon Glassccda2fa2021-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 Glass4d7237b2021-09-25 07:03:15 -0600325{
Tom Rinibc7d9d22021-12-20 09:36:32 -0500326 ulong rd_addr;
Simon Glassccda2fa2021-09-25 19:43:37 -0600327 char *buf;
Simon Glass2de5b012021-09-25 19:43:36 -0600328
Tom Rinibc7d9d22021-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 Glass2de5b012021-09-25 19:43:36 -0600333
Tom Rinibc7d9d22021-12-20 09:36:32 -0500334 if (select) {
335 ulong default_addr;
Simon Glass4d7237b2021-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 Rinibc7d9d22021-12-20 09:36:32 -0500347 if (fit_parse_conf(select, default_addr,
348 &rd_addr, &fit_uname_config)) {
Simon Glass60d71542021-09-25 07:03:16 -0600349 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
350 fit_uname_config, rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600351 } else if (fit_parse_subimage(select, default_addr,
Simon Glass60d71542021-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 Rinibc7d9d22021-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 Glass4d7237b2021-09-25 07:03:15 -0600362 }
Tom Rinibc7d9d22021-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 Glasse5661c92021-09-25 19:43:38 -0600377 }
Tom Rinibc7d9d22021-12-20 09:36:32 -0500378#endif
Simon Glasse5661c92021-09-25 19:43:38 -0600379
Tom Rinibc7d9d22021-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 Glass2de5b012021-09-25 19:43:36 -0600389 const image_header_t *rd_hdr;
390
Simon Glass60d71542021-09-25 07:03:16 -0600391 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
392 rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600393
394 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Tom Rinibc7d9d22021-12-20 09:36:32 -0500395 rd_hdr = image_get_ramdisk(rd_addr, arch,
396 images->verify);
397
Simon Glass60d71542021-09-25 07:03:16 -0600398 if (!rd_hdr)
Simon Glassccda2fa2021-09-25 19:43:37 -0600399 return -ENOENT;
Simon Glass4d7237b2021-09-25 07:03:15 -0600400
Simon Glassccda2fa2021-09-25 19:43:37 -0600401 *rd_datap = image_get_data(rd_hdr);
402 *rd_lenp = image_get_data_size(rd_hdr);
Tom Rinibc7d9d22021-12-20 09:36:32 -0500403 break;
Simon Glass2de5b012021-09-25 19:43:36 -0600404 }
Tom Rinibc7d9d22021-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 Glass60d71542021-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 Glassccda2fa2021-09-25 19:43:37 -0600414 rd_datap, rd_lenp);
Simon Glass4d7237b2021-09-25 07:03:15 -0600415 if (rd_noffset < 0)
Simon Glassccda2fa2021-09-25 19:43:37 -0600416 return rd_noffset;
Simon Glass4d7237b2021-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 Rinibc7d9d22021-12-20 09:36:32 -0500421 break;
422#endif
423#ifdef CONFIG_ANDROID_BOOT_IMAGE
424 case IMAGE_FORMAT_ANDROID:
Simon Glass4d7237b2021-09-25 07:03:15 -0600425 android_image_get_ramdisk((void *)images->os.start,
Simon Glassccda2fa2021-09-25 19:43:37 -0600426 rd_datap, rd_lenp);
Tom Rinibc7d9d22021-12-20 09:36:32 -0500427 break;
428#endif
429 default:
430 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
431 char *end = NULL;
Simon Glass0f781382021-09-25 19:43:35 -0600432
Tom Rinibc7d9d22021-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 Glass4d7237b2021-09-25 07:03:15 -0600440 }
Simon Glass0f781382021-09-25 19:43:35 -0600441 puts("Wrong Ramdisk Image Format\n");
Simon Glassccda2fa2021-09-25 19:43:37 -0600442 return -EINVAL;
Simon Glass4d7237b2021-09-25 07:03:15 -0600443 }
Simon Glassccda2fa2021-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 Glass4d7237b2021-09-25 07:03:15 -0600507 } else if (images->legacy_hdr_valid &&
508 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass60d71542021-09-25 07:03:16 -0600509 IH_TYPE_MULTI)) {
Simon Glass4d7237b2021-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 Glass60d71542021-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 Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -0600524 rd_len = 0;
525 rd_data = 0;
Simon Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -0600535 *rd_start, *rd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600536
537 return 0;
538}
539
Ashok Reddy Soma8aaae3d2022-07-07 10:45:37 +0200540#if defined(CONFIG_LMB)
Simon Glass4d7237b2021-09-25 07:03:15 -0600541/**
542 * boot_ramdisk_high - relocate init ramdisk
543 * @lmb: pointer to lmb handle, will be used for memory mgmt
544 * @rd_data: ramdisk data start address
545 * @rd_len: ramdisk data length
546 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
547 * start address (after possible relocation)
548 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
549 * end address (after possible relocation)
550 *
551 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
552 * variable and if requested ramdisk data is moved to a specified location.
553 *
554 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
555 * start/end addresses if ramdisk image start and len were provided,
556 * otherwise set initrd_start and initrd_end set to zeros.
557 *
558 * returns:
559 * 0 - success
560 * -1 - failure
561 */
562int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass60d71542021-09-25 07:03:16 -0600563 ulong *initrd_start, ulong *initrd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600564{
565 char *s;
566 ulong initrd_high;
567 int initrd_copy_to_ram = 1;
568
569 s = env_get("initrd_high");
570 if (s) {
571 /* a value of "no" or a similar string will act like 0,
572 * turning the "load high" feature off. This is intentional.
573 */
574 initrd_high = hextoul(s, NULL);
575 if (initrd_high == ~0)
576 initrd_copy_to_ram = 0;
577 } else {
578 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
579 }
580
Simon Glass4d7237b2021-09-25 07:03:15 -0600581 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass60d71542021-09-25 07:03:16 -0600582 initrd_high, initrd_copy_to_ram);
Simon Glass4d7237b2021-09-25 07:03:15 -0600583
584 if (rd_data) {
585 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
586 debug(" in-place initrd\n");
587 *initrd_start = rd_data;
588 *initrd_end = rd_data + rd_len;
589 lmb_reserve(lmb, rd_data, rd_len);
590 } else {
591 if (initrd_high)
592 *initrd_start = (ulong)lmb_alloc_base(lmb,
593 rd_len, 0x1000, initrd_high);
594 else
595 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
596 0x1000);
597
598 if (*initrd_start == 0) {
599 puts("ramdisk - allocation error\n");
600 goto error;
601 }
602 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
603
604 *initrd_end = *initrd_start + rd_len;
605 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass60d71542021-09-25 07:03:16 -0600606 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600607
608 memmove_wd((void *)*initrd_start,
Simon Glass60d71542021-09-25 07:03:16 -0600609 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass4d7237b2021-09-25 07:03:15 -0600610
Simon Glass4d7237b2021-09-25 07:03:15 -0600611 /*
612 * Ensure the image is flushed to memory to handle
613 * AMP boot scenarios in which we might not be
614 * HW cache coherent
615 */
Simon Glass0f781382021-09-25 19:43:35 -0600616 if (IS_ENABLED(CONFIG_MP)) {
617 flush_cache((unsigned long)*initrd_start,
618 ALIGN(rd_len, ARCH_DMA_MINALIGN));
619 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600620 puts("OK\n");
621 }
622 } else {
623 *initrd_start = 0;
624 *initrd_end = 0;
625 }
626 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600627 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600628
629 return 0;
630
631error:
632 return -1;
633}
Ashok Reddy Soma8aaae3d2022-07-07 10:45:37 +0200634#endif
Simon Glass4d7237b2021-09-25 07:03:15 -0600635
Simon Glass60d71542021-09-25 07:03:16 -0600636int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass4d7237b2021-09-25 07:03:15 -0600637 ulong *setup_start, ulong *setup_len)
638{
Simon Glass0f781382021-09-25 19:43:35 -0600639 if (!CONFIG_IS_ENABLED(FIT))
640 return -ENOENT;
641
Simon Glass4d7237b2021-09-25 07:03:15 -0600642 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600643}
644
Simon Glass4d7237b2021-09-25 07:03:15 -0600645int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600646 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600647{
648 ulong tmp_img_addr, img_data, img_len;
649 void *buf;
650 int conf_noffset;
651 int fit_img_result;
652 const char *uname, *name;
653 int err;
654 int devnum = 0; /* TODO support multi fpga platforms */
655
Simon Glass0f781382021-09-25 19:43:35 -0600656 if (!IS_ENABLED(CONFIG_FPGA))
657 return -ENOSYS;
658
Simon Glass4d7237b2021-09-25 07:03:15 -0600659 /* Check to see if the images struct has a FIT configuration */
660 if (!genimg_has_config(images)) {
661 debug("## FIT configuration was not specified\n");
662 return 0;
663 }
664
665 /*
666 * Obtain the os FIT header from the images struct
667 */
668 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
669 buf = map_sysmem(tmp_img_addr, 0);
670 /*
671 * Check image type. For FIT images get FIT node
672 * and attempt to locate a generic binary.
673 */
674 switch (genimg_get_format(buf)) {
675 case IMAGE_FORMAT_FIT:
676 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
677
678 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
679 NULL);
680 if (!uname) {
681 debug("## FPGA image is not specified\n");
682 return 0;
683 }
684 fit_img_result = fit_image_load(images,
685 tmp_img_addr,
686 (const char **)&uname,
Simon Glass60d71542021-09-25 07:03:16 -0600687 &images->fit_uname_cfg,
Simon Glass4d7237b2021-09-25 07:03:15 -0600688 arch,
689 IH_TYPE_FPGA,
690 BOOTSTAGE_ID_FPGA_INIT,
691 FIT_LOAD_OPTIONAL_NON_ZERO,
692 &img_data, &img_len);
693
694 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
695 uname, img_data, img_len);
696
697 if (fit_img_result < 0) {
698 /* Something went wrong! */
699 return fit_img_result;
700 }
701
702 if (!fpga_is_partial_data(devnum, img_len)) {
703 name = "full";
704 err = fpga_loadbitstream(devnum, (char *)img_data,
705 img_len, BIT_FULL);
706 if (err)
707 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300708 img_len, BIT_FULL, 0);
Simon Glass4d7237b2021-09-25 07:03:15 -0600709 } else {
710 name = "partial";
711 err = fpga_loadbitstream(devnum, (char *)img_data,
712 img_len, BIT_PARTIAL);
713 if (err)
714 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300715 img_len, BIT_PARTIAL, 0);
Simon Glass4d7237b2021-09-25 07:03:15 -0600716 }
717
718 if (err)
719 return err;
720
721 printf(" Programming %s bitstream... OK\n", name);
722 break;
723 default:
724 printf("The given image format is not supported (corrupt?)\n");
725 return 1;
726 }
727
728 return 0;
729}
Simon Glass4d7237b2021-09-25 07:03:15 -0600730
Simon Glass60d71542021-09-25 07:03:16 -0600731static void fit_loadable_process(u8 img_type,
Simon Glass4d7237b2021-09-25 07:03:15 -0600732 ulong img_data,
733 ulong img_len)
734{
735 int i;
736 const unsigned int count =
737 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
738 struct fit_loadable_tbl *fit_loadable_handler =
739 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
740 /* For each loadable handler */
741 for (i = 0; i < count; i++, fit_loadable_handler++)
742 /* matching this type */
743 if (fit_loadable_handler->type == img_type)
744 /* call that handler with this image data */
745 fit_loadable_handler->handler(img_data, img_len);
746}
747
748int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600749 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600750{
751 /*
752 * These variables are used to hold the current image location
753 * in system memory.
754 */
755 ulong tmp_img_addr;
756 /*
757 * These two variables are requirements for fit_image_load, but
758 * their values are not used
759 */
760 ulong img_data, img_len;
761 void *buf;
762 int loadables_index;
763 int conf_noffset;
764 int fit_img_result;
765 const char *uname;
Simon Glass60d71542021-09-25 07:03:16 -0600766 u8 img_type;
Simon Glass4d7237b2021-09-25 07:03:15 -0600767
768 /* Check to see if the images struct has a FIT configuration */
769 if (!genimg_has_config(images)) {
770 debug("## FIT configuration was not specified\n");
771 return 0;
772 }
773
774 /*
775 * Obtain the os FIT header from the images struct
776 */
777 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
778 buf = map_sysmem(tmp_img_addr, 0);
779 /*
780 * Check image type. For FIT images get FIT node
781 * and attempt to locate a generic binary.
782 */
783 switch (genimg_get_format(buf)) {
784 case IMAGE_FORMAT_FIT:
785 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
786
787 for (loadables_index = 0;
788 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass60d71542021-09-25 07:03:16 -0600789 FIT_LOADABLE_PROP,
790 loadables_index, NULL), uname;
791 loadables_index++) {
792 fit_img_result = fit_image_load(images, tmp_img_addr,
793 &uname,
794 &images->fit_uname_cfg,
795 arch, IH_TYPE_LOADABLE,
796 BOOTSTAGE_ID_FIT_LOADABLE_START,
797 FIT_LOAD_OPTIONAL_NON_ZERO,
798 &img_data, &img_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600799 if (fit_img_result < 0) {
800 /* Something went wrong! */
801 return fit_img_result;
802 }
803
804 fit_img_result = fit_image_get_node(buf, uname);
805 if (fit_img_result < 0) {
806 /* Something went wrong! */
807 return fit_img_result;
808 }
809 fit_img_result = fit_image_get_type(buf,
810 fit_img_result,
811 &img_type);
812 if (fit_img_result < 0) {
813 /* Something went wrong! */
814 return fit_img_result;
815 }
816
817 fit_loadable_process(img_type, img_data, img_len);
818 }
819 break;
820 default:
821 printf("The given image format is not supported (corrupt?)\n");
822 return 1;
823 }
824
825 return 0;
826}
Simon Glass4d7237b2021-09-25 07:03:15 -0600827
Ashok Reddy Soma8aaae3d2022-07-07 10:45:37 +0200828#if defined(CONFIG_LMB)
Tom Rinic253f122022-05-12 10:02:06 -0400829#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
Simon Glass4d7237b2021-09-25 07:03:15 -0600830/**
831 * boot_get_cmdline - allocate and initialize kernel cmdline
832 * @lmb: pointer to lmb handle, will be used for memory mgmt
833 * @cmd_start: pointer to a ulong variable, will hold cmdline start
834 * @cmd_end: pointer to a ulong variable, will hold cmdline end
835 *
836 * boot_get_cmdline() allocates space for kernel command line below
837 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
838 * variable is present its contents is copied to allocated kernel
839 * command line.
840 *
841 * returns:
842 * 0 - success
843 * -1 - failure
844 */
845int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
846{
847 char *cmdline;
848 char *s;
849
850 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
851 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass60d71542021-09-25 07:03:16 -0600852 if (!cmdline)
Simon Glass4d7237b2021-09-25 07:03:15 -0600853 return -1;
854
855 s = env_get("bootargs");
856 if (!s)
857 s = "";
858
859 strcpy(cmdline, s);
860
Simon Glass60d71542021-09-25 07:03:16 -0600861 *cmd_start = (ulong)cmdline;
Simon Glass4d7237b2021-09-25 07:03:15 -0600862 *cmd_end = *cmd_start + strlen(cmdline);
863
864 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
865
866 return 0;
867}
Simon Glass4d7237b2021-09-25 07:03:15 -0600868
Simon Glass4d7237b2021-09-25 07:03:15 -0600869/**
870 * boot_get_kbd - allocate and initialize kernel copy of board info
871 * @lmb: pointer to lmb handle, will be used for memory mgmt
872 * @kbd: double pointer to board info data
873 *
874 * boot_get_kbd() allocates space for kernel copy of board info data below
875 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
876 * with the current u-boot board info data.
877 *
878 * returns:
879 * 0 - success
880 * -1 - failure
881 */
882int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
883{
884 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
885 sizeof(struct bd_info),
886 0xf,
Simon Glass60d71542021-09-25 07:03:16 -0600887 env_get_bootm_mapsize() +
888 env_get_bootm_low());
889 if (!*kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600890 return -1;
891
Simon Glass60d71542021-09-25 07:03:16 -0600892 **kbd = *gd->bd;
Simon Glass4d7237b2021-09-25 07:03:15 -0600893
894 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
895
Simon Glassbc0c4db2021-09-25 07:03:20 -0600896#if defined(DEBUG)
Leo Yu-Chi Liangde53e5e2021-10-27 17:00:41 +0800897 if (IS_ENABLED(CONFIG_CMD_BDI))
Simon Glassbc0c4db2021-09-25 07:03:20 -0600898 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass4d7237b2021-09-25 07:03:15 -0600899#endif
900
901 return 0;
902}
Tom Rinic253f122022-05-12 10:02:06 -0400903#endif
Simon Glass4d7237b2021-09-25 07:03:15 -0600904
Simon Glass4d7237b2021-09-25 07:03:15 -0600905int image_setup_linux(bootm_headers_t *images)
906{
907 ulong of_size = images->ft_len;
908 char **of_flat_tree = &images->ft_addr;
909 struct lmb *lmb = &images->lmb;
910 int ret;
911
Simon Glass85c057e2021-09-25 19:43:21 -0600912 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass4d7237b2021-09-25 07:03:15 -0600913 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
914
Simon Glassb8eb1fe2021-09-25 19:43:25 -0600915 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600916 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass60d71542021-09-25 07:03:16 -0600917 &images->cmdline_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600918 if (ret) {
919 puts("ERROR with allocation of cmdline\n");
920 return ret;
921 }
922 }
923
Simon Glass85c057e2021-09-25 19:43:21 -0600924 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600925 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
926 if (ret)
927 return ret;
928 }
929
Simon Glass85c057e2021-09-25 19:43:21 -0600930 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600931 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
932 if (ret)
933 return ret;
934 }
935
936 return 0;
937}
Ashok Reddy Soma8aaae3d2022-07-07 10:45:37 +0200938#endif
Simon Glass4a8a8a12021-09-25 07:03:17 -0600939
940void genimg_print_size(uint32_t size)
941{
942 printf("%d Bytes = ", size);
943 print_size(size, "\n");
944}
945
946void genimg_print_time(time_t timestamp)
947{
948 struct rtc_time tm;
949
950 rtc_to_tm(timestamp, &tm);
951 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
952 tm.tm_year, tm.tm_mon, tm.tm_mday,
953 tm.tm_hour, tm.tm_min, tm.tm_sec);
954}