blob: 98f903f93fb9ab49fbd352dae15db61a975bf559 [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>
Simon Glass1ab16922022-07-31 12:28:48 -060014#include <display_options.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060015#include <env.h>
16#include <fpga.h>
17#include <image.h>
Andy Shevchenkoe3b3ad92021-11-08 21:03:38 +030018#include <init.h>
Simon Glass7ae16bb2022-08-28 12:32:53 -060019#include <log.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060020#include <mapmem.h>
Simon Glass4a8a8a12021-09-25 07:03:17 -060021#include <rtc.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060022#include <watchdog.h>
23#include <asm/cache.h>
24#include <asm/global_data.h>
25
Simon Glass4d7237b2021-09-25 07:03:15 -060026DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass4d7237b2021-09-25 07:03:15 -060028/**
29 * image_get_ramdisk - get and verify ramdisk image
30 * @rd_addr: ramdisk image start address
31 * @arch: expected ramdisk architecture
32 * @verify: checksum verification flag
33 *
34 * image_get_ramdisk() returns a pointer to the verified ramdisk image
35 * header. Routine receives image start address and expected architecture
36 * flag. Verification done covers data and header integrity and os/type/arch
37 * fields checking.
38 *
39 * returns:
40 * pointer to a ramdisk image header, if image was found and valid
41 * otherwise, return NULL
42 */
Simon Glass60d71542021-09-25 07:03:16 -060043static const image_header_t *image_get_ramdisk(ulong rd_addr, u8 arch,
44 int verify)
Simon Glass4d7237b2021-09-25 07:03:15 -060045{
46 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
47
48 if (!image_check_magic(rd_hdr)) {
49 puts("Bad Magic Number\n");
50 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
51 return NULL;
52 }
53
54 if (!image_check_hcrc(rd_hdr)) {
55 puts("Bad Header Checksum\n");
56 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
57 return NULL;
58 }
59
60 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
61 image_print_contents(rd_hdr);
62
63 if (verify) {
64 puts(" Verifying Checksum ... ");
65 if (!image_check_dcrc(rd_hdr)) {
66 puts("Bad Data CRC\n");
67 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
68 return NULL;
69 }
70 puts("OK\n");
71 }
72
73 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
74
75 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
76 !image_check_arch(rd_hdr, arch) ||
77 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
78 printf("No Linux %s Ramdisk Image\n",
Simon Glass60d71542021-09-25 07:03:16 -060079 genimg_get_arch_name(arch));
Simon Glass4d7237b2021-09-25 07:03:15 -060080 bootstage_error(BOOTSTAGE_ID_RAMDISK);
81 return NULL;
82 }
83
84 return rd_hdr;
85}
Simon Glass4d7237b2021-09-25 07:03:15 -060086
87/*****************************************************************************/
88/* Shared dual-format routines */
89/*****************************************************************************/
90ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
91ulong image_save_addr; /* Default Save Address */
92ulong image_save_size; /* Default Save Size (in bytes) */
93
94static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass60d71542021-09-25 07:03:16 -060095 int flags)
Simon Glass4d7237b2021-09-25 07:03:15 -060096{
97 switch (op) {
98 case env_op_create:
99 case env_op_overwrite:
100 image_load_addr = hextoul(value, NULL);
101 break;
102 default:
103 break;
104 }
105
106 return 0;
107}
108U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
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
Simon Glass7ae16bb2022-08-28 12:32:53 -0600176 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600177 if (to > from) {
Simon Glass7ae16bb2022-08-28 12:32:53 -0600178 from += len;
179 to += len;
Simon Glass4d7237b2021-09-25 07:03:15 -0600180 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600181 while (len > 0) {
182 size_t tail = (len > chunksz) ? chunksz : len;
183
Stefan Roese80877fa2022-09-02 14:10:46 +0200184 schedule();
Simon Glass7ae16bb2022-08-28 12:32:53 -0600185 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;
Simon Glass4d7237b2021-09-25 07:03:15 -0600195 }
Simon Glass7ae16bb2022-08-28 12:32:53 -0600196 } else {
197 memmove(to, from, len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600198 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600199}
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 Glass664696f2022-08-28 12:32:47 -0600316 * @select: name of ramdisk to select, or hex address, 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{
Simon Glassf2d23de2022-08-28 12:32:49 -0600326 const char *fit_uname_config;
327 const char *fit_uname_ramdisk;
Simon Glass973b8932022-08-28 12:32:51 -0600328 bool done_select = !select;
Simon Glass664696f2022-08-28 12:32:47 -0600329 bool done = false;
Simon Glassf2d23de2022-08-28 12:32:49 -0600330 int rd_noffset;
Tom Rinibc7d9d22021-12-20 09:36:32 -0500331 ulong rd_addr;
Simon Glassccda2fa2021-09-25 19:43:37 -0600332 char *buf;
Simon Glass2de5b012021-09-25 19:43:36 -0600333
Simon Glass973b8932022-08-28 12:32:51 -0600334 if (CONFIG_IS_ENABLED(FIT)) {
Simon Glassf2d23de2022-08-28 12:32:49 -0600335 fit_uname_config = images->fit_uname_cfg;
336 fit_uname_ramdisk = NULL;
Simon Glass2de5b012021-09-25 19:43:36 -0600337
Tom Rinibc7d9d22021-12-20 09:36:32 -0500338 if (select) {
339 ulong default_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600340 /*
341 * If the init ramdisk comes from the FIT image and
342 * the FIT image address is omitted in the command
343 * line argument, try to use os FIT image address or
344 * default load address.
345 */
346 if (images->fit_uname_os)
347 default_addr = (ulong)images->fit_hdr_os;
348 else
349 default_addr = image_load_addr;
350
Simon Glass92c586f2022-08-28 12:32:52 -0600351 if (fit_parse_conf(select, default_addr, &rd_addr,
352 &fit_uname_config)) {
Simon Glass60d71542021-09-25 07:03:16 -0600353 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
354 fit_uname_config, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600355 done_select = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600356 } else if (fit_parse_subimage(select, default_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600357 &rd_addr,
358 &fit_uname_ramdisk)) {
359 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
360 fit_uname_ramdisk, rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600361 done_select = true;
362 }
363 }
364 }
365 if (!done_select) {
Simon Glass92c586f2022-08-28 12:32:52 -0600366 rd_addr = hextoul(select, NULL);
367 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
Simon Glass973b8932022-08-28 12:32:51 -0600368 }
Simon Glass92c586f2022-08-28 12:32:52 -0600369 if (CONFIG_IS_ENABLED(FIT) && !select) {
370 /* use FIT configuration provided in first bootm
371 * command argument. If the property is not defined,
372 * quit silently (with -ENOPKG)
Tom Rinibc7d9d22021-12-20 09:36:32 -0500373 */
Simon Glass92c586f2022-08-28 12:32:52 -0600374 rd_addr = map_to_sysmem(images->fit_hdr_os);
375 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
376 rd_addr);
377 if (rd_noffset == -ENOENT)
378 return -ENOPKG;
379 else if (rd_noffset < 0)
380 return rd_noffset;
381 }
Simon Glass2de5b012021-09-25 19:43:36 -0600382
Simon Glass92c586f2022-08-28 12:32:52 -0600383 /*
384 * Check if there is an initrd image at the
385 * address provided in the second bootm argument
386 * check image type, for FIT images get FIT node.
387 */
388 buf = map_sysmem(rd_addr, 0);
389 switch (genimg_get_format(buf)) {
390 case IMAGE_FORMAT_LEGACY:
391 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
392 const image_header_t *rd_hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600393
Simon Glass92c586f2022-08-28 12:32:52 -0600394 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
395 rd_addr);
Tom Rinibc7d9d22021-12-20 09:36:32 -0500396
Simon Glass92c586f2022-08-28 12:32:52 -0600397 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
398 rd_hdr = image_get_ramdisk(rd_addr, arch,
399 images->verify);
Simon Glass4d7237b2021-09-25 07:03:15 -0600400
Simon Glass92c586f2022-08-28 12:32:52 -0600401 if (!rd_hdr)
402 return -ENOENT;
Simon Glass4d7237b2021-09-25 07:03:15 -0600403
Simon Glass92c586f2022-08-28 12:32:52 -0600404 *rd_datap = image_get_data(rd_hdr);
405 *rd_lenp = image_get_data_size(rd_hdr);
406 done = true;
407 }
408 break;
409 case IMAGE_FORMAT_FIT:
410 if (CONFIG_IS_ENABLED(FIT)) {
411 rd_noffset = fit_image_load(images, rd_addr,
412 &fit_uname_ramdisk,
413 &fit_uname_config,
414 arch, IH_TYPE_RAMDISK,
415 BOOTSTAGE_ID_FIT_RD_START,
416 FIT_LOAD_OPTIONAL_NON_ZERO,
417 rd_datap, rd_lenp);
418 if (rd_noffset < 0)
419 return rd_noffset;
Simon Glassdc0aa042022-08-28 12:32:46 -0600420
Simon Glass92c586f2022-08-28 12:32:52 -0600421 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
422 images->fit_uname_rd = fit_uname_ramdisk;
423 images->fit_noffset_rd = rd_noffset;
424 done = true;
Simon Glass664696f2022-08-28 12:32:47 -0600425 }
Simon Glass92c586f2022-08-28 12:32:52 -0600426 break;
427 case IMAGE_FORMAT_ANDROID:
428 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
429 void *ptr = map_sysmem(images->os.start, 0);
430 int ret;
431
432 ret = android_image_get_ramdisk(ptr, rd_datap, rd_lenp);
433 unmap_sysmem(ptr);
434 if (ret)
435 return ret;
436 done = true;
437 }
438 break;
439 }
Simon Glass664696f2022-08-28 12:32:47 -0600440
441 if (!done) {
442 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
443 char *end = NULL;
Simon Glass0f781382021-09-25 19:43:35 -0600444
Simon Glass664696f2022-08-28 12:32:47 -0600445 if (select)
446 end = strchr(select, ':');
447 if (end) {
448 *rd_lenp = hextoul(++end, NULL);
449 *rd_datap = rd_addr;
450 done = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600451 }
Simon Glass664696f2022-08-28 12:32:47 -0600452 }
453
454 if (!done) {
Simon Glass0f781382021-09-25 19:43:35 -0600455 puts("Wrong Ramdisk Image Format\n");
Simon Glassccda2fa2021-09-25 19:43:37 -0600456 return -EINVAL;
Simon Glass4d7237b2021-09-25 07:03:15 -0600457 }
Simon Glass664696f2022-08-28 12:32:47 -0600458 }
Simon Glassccda2fa2021-09-25 19:43:37 -0600459
460 return 0;
461}
462
463/**
464 * boot_get_ramdisk - main ramdisk handling routine
465 * @argc: command argument count
466 * @argv: command argument list
467 * @images: pointer to the bootm images structure
468 * @arch: expected ramdisk architecture
469 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
470 * @rd_end: pointer to a ulong variable, will hold ramdisk end
471 *
472 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
473 * Currently supported are the following ramdisk sources:
474 * - multicomponent kernel/ramdisk image,
475 * - commandline provided address of decicated ramdisk image.
476 *
477 * returns:
478 * 0, if ramdisk image was found and valid, or skiped
479 * rd_start and rd_end are set to ramdisk start/end addresses if
480 * ramdisk image is found and valid
481 *
482 * 1, if ramdisk image is found but corrupted, or invalid
483 * rd_start and rd_end are set to 0 if no ramdisk exists
484 */
485int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
486 u8 arch, ulong *rd_start, ulong *rd_end)
487{
488 ulong rd_data, rd_len;
489 const char *select = NULL;
490
491 *rd_start = 0;
492 *rd_end = 0;
493
494 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
495 char *buf;
496
497 /* Look for an Android boot image */
498 buf = map_sysmem(images->os.start, 0);
499 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
500 select = (argc == 0) ? env_get("loadaddr") : argv[0];
501 }
502
503 if (argc >= 2)
504 select = argv[1];
505
506 /*
507 * Look for a '-' which indicates to ignore the
508 * ramdisk argument
509 */
510 if (select && strcmp(select, "-") == 0) {
511 debug("## Skipping init Ramdisk\n");
512 rd_len = 0;
513 rd_data = 0;
514 } else if (select || genimg_has_config(images)) {
515 int ret;
516
517 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
518 if (ret == -ENOPKG)
519 return 0;
520 else if (ret)
521 return ret;
Simon Glass4d7237b2021-09-25 07:03:15 -0600522 } else if (images->legacy_hdr_valid &&
523 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass60d71542021-09-25 07:03:16 -0600524 IH_TYPE_MULTI)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600525 /*
526 * Now check if we have a legacy mult-component image,
527 * get second entry data start address and len.
528 */
529 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600530 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
531 (ulong)images->legacy_hdr_os);
Simon Glass4d7237b2021-09-25 07:03:15 -0600532
533 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
534 } else {
535 /*
536 * no initrd image
537 */
538 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600539 rd_len = 0;
540 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600541 }
542
543 if (!rd_data) {
544 debug("## No init Ramdisk\n");
545 } else {
546 *rd_start = rd_data;
547 *rd_end = rd_data + rd_len;
548 }
549 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600550 *rd_start, *rd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600551
552 return 0;
553}
554
Simon Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -0600577 ulong *initrd_start, ulong *initrd_end)
Simon Glass4d7237b2021-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 Glass4d7237b2021-09-25 07:03:15 -0600595 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass60d71542021-09-25 07:03:16 -0600596 initrd_high, initrd_copy_to_ram);
Simon Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -0600620 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600621
622 memmove_wd((void *)*initrd_start,
Simon Glass60d71542021-09-25 07:03:16 -0600623 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass4d7237b2021-09-25 07:03:15 -0600624
Simon Glass4d7237b2021-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 Glass0f781382021-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 Glass4d7237b2021-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 Glass60d71542021-09-25 07:03:16 -0600641 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600642
643 return 0;
644
645error:
646 return -1;
647}
Simon Glass4d7237b2021-09-25 07:03:15 -0600648
Simon Glass60d71542021-09-25 07:03:16 -0600649int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass4d7237b2021-09-25 07:03:15 -0600650 ulong *setup_start, ulong *setup_len)
651{
Simon Glass0f781382021-09-25 19:43:35 -0600652 if (!CONFIG_IS_ENABLED(FIT))
653 return -ENOENT;
654
Simon Glass4d7237b2021-09-25 07:03:15 -0600655 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600656}
657
Simon Glass4d7237b2021-09-25 07:03:15 -0600658int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600659 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600660{
661 ulong tmp_img_addr, img_data, img_len;
662 void *buf;
663 int conf_noffset;
664 int fit_img_result;
665 const char *uname, *name;
666 int err;
667 int devnum = 0; /* TODO support multi fpga platforms */
668
Simon Glass0f781382021-09-25 19:43:35 -0600669 if (!IS_ENABLED(CONFIG_FPGA))
670 return -ENOSYS;
671
Simon Glass4d7237b2021-09-25 07:03:15 -0600672 /* Check to see if the images struct has a FIT configuration */
673 if (!genimg_has_config(images)) {
674 debug("## FIT configuration was not specified\n");
675 return 0;
676 }
677
678 /*
679 * Obtain the os FIT header from the images struct
680 */
681 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
682 buf = map_sysmem(tmp_img_addr, 0);
683 /*
684 * Check image type. For FIT images get FIT node
685 * and attempt to locate a generic binary.
686 */
687 switch (genimg_get_format(buf)) {
688 case IMAGE_FORMAT_FIT:
689 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
690
691 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
692 NULL);
693 if (!uname) {
694 debug("## FPGA image is not specified\n");
695 return 0;
696 }
697 fit_img_result = fit_image_load(images,
698 tmp_img_addr,
699 (const char **)&uname,
Simon Glass60d71542021-09-25 07:03:16 -0600700 &images->fit_uname_cfg,
Simon Glass4d7237b2021-09-25 07:03:15 -0600701 arch,
702 IH_TYPE_FPGA,
703 BOOTSTAGE_ID_FPGA_INIT,
704 FIT_LOAD_OPTIONAL_NON_ZERO,
705 &img_data, &img_len);
706
707 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
708 uname, img_data, img_len);
709
710 if (fit_img_result < 0) {
711 /* Something went wrong! */
712 return fit_img_result;
713 }
714
715 if (!fpga_is_partial_data(devnum, img_len)) {
716 name = "full";
717 err = fpga_loadbitstream(devnum, (char *)img_data,
718 img_len, BIT_FULL);
719 if (err)
720 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300721 img_len, BIT_FULL, 0);
Simon Glass4d7237b2021-09-25 07:03:15 -0600722 } else {
723 name = "partial";
724 err = fpga_loadbitstream(devnum, (char *)img_data,
725 img_len, BIT_PARTIAL);
726 if (err)
727 err = fpga_load(devnum, (const void *)img_data,
Oleksandr Suvorov4ff163d2022-07-22 17:16:07 +0300728 img_len, BIT_PARTIAL, 0);
Simon Glass4d7237b2021-09-25 07:03:15 -0600729 }
730
731 if (err)
732 return err;
733
734 printf(" Programming %s bitstream... OK\n", name);
735 break;
736 default:
737 printf("The given image format is not supported (corrupt?)\n");
738 return 1;
739 }
740
741 return 0;
742}
Simon Glass4d7237b2021-09-25 07:03:15 -0600743
Simon Glass60d71542021-09-25 07:03:16 -0600744static void fit_loadable_process(u8 img_type,
Simon Glass4d7237b2021-09-25 07:03:15 -0600745 ulong img_data,
746 ulong img_len)
747{
748 int i;
749 const unsigned int count =
750 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
751 struct fit_loadable_tbl *fit_loadable_handler =
752 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
753 /* For each loadable handler */
754 for (i = 0; i < count; i++, fit_loadable_handler++)
755 /* matching this type */
756 if (fit_loadable_handler->type == img_type)
757 /* call that handler with this image data */
758 fit_loadable_handler->handler(img_data, img_len);
759}
760
761int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600762 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600763{
764 /*
765 * These variables are used to hold the current image location
766 * in system memory.
767 */
768 ulong tmp_img_addr;
769 /*
770 * These two variables are requirements for fit_image_load, but
771 * their values are not used
772 */
773 ulong img_data, img_len;
774 void *buf;
775 int loadables_index;
776 int conf_noffset;
777 int fit_img_result;
778 const char *uname;
Simon Glass60d71542021-09-25 07:03:16 -0600779 u8 img_type;
Simon Glass4d7237b2021-09-25 07:03:15 -0600780
781 /* Check to see if the images struct has a FIT configuration */
782 if (!genimg_has_config(images)) {
783 debug("## FIT configuration was not specified\n");
784 return 0;
785 }
786
787 /*
788 * Obtain the os FIT header from the images struct
789 */
790 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
791 buf = map_sysmem(tmp_img_addr, 0);
792 /*
793 * Check image type. For FIT images get FIT node
794 * and attempt to locate a generic binary.
795 */
796 switch (genimg_get_format(buf)) {
797 case IMAGE_FORMAT_FIT:
798 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
799
800 for (loadables_index = 0;
801 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass60d71542021-09-25 07:03:16 -0600802 FIT_LOADABLE_PROP,
803 loadables_index, NULL), uname;
804 loadables_index++) {
805 fit_img_result = fit_image_load(images, tmp_img_addr,
806 &uname,
807 &images->fit_uname_cfg,
808 arch, IH_TYPE_LOADABLE,
809 BOOTSTAGE_ID_FIT_LOADABLE_START,
810 FIT_LOAD_OPTIONAL_NON_ZERO,
811 &img_data, &img_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600812 if (fit_img_result < 0) {
813 /* Something went wrong! */
814 return fit_img_result;
815 }
816
817 fit_img_result = fit_image_get_node(buf, uname);
818 if (fit_img_result < 0) {
819 /* Something went wrong! */
820 return fit_img_result;
821 }
822 fit_img_result = fit_image_get_type(buf,
823 fit_img_result,
824 &img_type);
825 if (fit_img_result < 0) {
826 /* Something went wrong! */
827 return fit_img_result;
828 }
829
830 fit_loadable_process(img_type, img_data, img_len);
831 }
832 break;
833 default:
834 printf("The given image format is not supported (corrupt?)\n");
835 return 1;
836 }
837
838 return 0;
839}
Simon Glass4d7237b2021-09-25 07:03:15 -0600840
Simon Glass4d7237b2021-09-25 07:03:15 -0600841/**
842 * boot_get_cmdline - allocate and initialize kernel cmdline
843 * @lmb: pointer to lmb handle, will be used for memory mgmt
844 * @cmd_start: pointer to a ulong variable, will hold cmdline start
845 * @cmd_end: pointer to a ulong variable, will hold cmdline end
846 *
Simon Glass7ae16bb2022-08-28 12:32:53 -0600847 * This allocates space for kernel command line below
Simon Glass4d7237b2021-09-25 07:03:15 -0600848 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
849 * variable is present its contents is copied to allocated kernel
850 * command line.
851 *
852 * returns:
853 * 0 - success
854 * -1 - failure
855 */
856int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
857{
Simon Glass7ae16bb2022-08-28 12:32:53 -0600858 int barg;
Simon Glass4d7237b2021-09-25 07:03:15 -0600859 char *cmdline;
860 char *s;
861
Simon Glass7ae16bb2022-08-28 12:32:53 -0600862 /*
863 * Help the compiler detect that this function is only called when
864 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
865 */
866 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
867 return 0;
868
869 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
870 cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
Simon Glass4d7237b2021-09-25 07:03:15 -0600871 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass60d71542021-09-25 07:03:16 -0600872 if (!cmdline)
Simon Glass4d7237b2021-09-25 07:03:15 -0600873 return -1;
874
875 s = env_get("bootargs");
876 if (!s)
877 s = "";
878
879 strcpy(cmdline, s);
880
Simon Glass60d71542021-09-25 07:03:16 -0600881 *cmd_start = (ulong)cmdline;
Simon Glass4d7237b2021-09-25 07:03:15 -0600882 *cmd_end = *cmd_start + strlen(cmdline);
883
884 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
885
886 return 0;
887}
Simon Glass4d7237b2021-09-25 07:03:15 -0600888
Simon Glass4d7237b2021-09-25 07:03:15 -0600889/**
890 * boot_get_kbd - allocate and initialize kernel copy of board info
891 * @lmb: pointer to lmb handle, will be used for memory mgmt
892 * @kbd: double pointer to board info data
893 *
894 * boot_get_kbd() allocates space for kernel copy of board info data below
895 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
896 * with the current u-boot board info data.
897 *
898 * returns:
899 * 0 - success
900 * -1 - failure
901 */
902int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
903{
904 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
905 sizeof(struct bd_info),
906 0xf,
Simon Glass60d71542021-09-25 07:03:16 -0600907 env_get_bootm_mapsize() +
908 env_get_bootm_low());
909 if (!*kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600910 return -1;
911
Simon Glass60d71542021-09-25 07:03:16 -0600912 **kbd = *gd->bd;
Simon Glass4d7237b2021-09-25 07:03:15 -0600913
914 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
915
Simon Glass7ae16bb2022-08-28 12:32:53 -0600916 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
Simon Glassbc0c4db2021-09-25 07:03:20 -0600917 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass4d7237b2021-09-25 07:03:15 -0600918
919 return 0;
920}
Simon Glass4d7237b2021-09-25 07:03:15 -0600921
Simon Glass4d7237b2021-09-25 07:03:15 -0600922int image_setup_linux(bootm_headers_t *images)
923{
924 ulong of_size = images->ft_len;
925 char **of_flat_tree = &images->ft_addr;
Simon Glass7ae16bb2022-08-28 12:32:53 -0600926 struct lmb *lmb = images_lmb(images);
Simon Glass4d7237b2021-09-25 07:03:15 -0600927 int ret;
928
Simon Glass7ae16bb2022-08-28 12:32:53 -0600929 /* This function cannot be called without lmb support */
930 if (!CONFIG_IS_ENABLED(LMB))
931 return -EFAULT;
Simon Glass85c057e2021-09-25 19:43:21 -0600932 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass4d7237b2021-09-25 07:03:15 -0600933 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
934
Simon Glassb8eb1fe2021-09-25 19:43:25 -0600935 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600936 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass60d71542021-09-25 07:03:16 -0600937 &images->cmdline_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600938 if (ret) {
939 puts("ERROR with allocation of cmdline\n");
940 return ret;
941 }
942 }
943
Simon Glass85c057e2021-09-25 19:43:21 -0600944 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600945 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
946 if (ret)
947 return ret;
948 }
949
Simon Glass85c057e2021-09-25 19:43:21 -0600950 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600951 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
952 if (ret)
953 return ret;
954 }
955
956 return 0;
957}
Simon Glass4a8a8a12021-09-25 07:03:17 -0600958
959void genimg_print_size(uint32_t size)
960{
961 printf("%d Bytes = ", size);
962 print_size(size, "\n");
963}
964
965void genimg_print_time(time_t timestamp)
966{
967 struct rtc_time tm;
968
969 rtc_to_tm(timestamp, &tm);
970 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
971 tm.tm_year, tm.tm_mon, tm.tm_mday,
972 tm.tm_hour, tm.tm_min, tm.tm_sec);
973}