blob: 0d2e0fc9692e8f09b0f2a720a65c0878a1926b2a [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
24#ifndef CONFIG_SYS_BARGSIZE
25#define CONFIG_SYS_BARGSIZE 512
26#endif
27
28DECLARE_GLOBAL_DATA_PTR;
29
Tom Rini621158d2021-12-20 09:36:32 -050030#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass41506ff2021-09-25 07:03:15 -060031/**
32 * image_get_ramdisk - get and verify ramdisk image
33 * @rd_addr: ramdisk image start address
34 * @arch: expected ramdisk architecture
35 * @verify: checksum verification flag
36 *
37 * image_get_ramdisk() returns a pointer to the verified ramdisk image
38 * header. Routine receives image start address and expected architecture
39 * flag. Verification done covers data and header integrity and os/type/arch
40 * fields checking.
41 *
42 * returns:
43 * pointer to a ramdisk image header, if image was found and valid
44 * otherwise, return NULL
45 */
Simon Glass3d2a47f2021-09-25 07:03:16 -060046static const image_header_t *image_get_ramdisk(ulong rd_addr, u8 arch,
47 int verify)
Simon Glass41506ff2021-09-25 07:03:15 -060048{
49 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
50
51 if (!image_check_magic(rd_hdr)) {
52 puts("Bad Magic Number\n");
53 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
54 return NULL;
55 }
56
57 if (!image_check_hcrc(rd_hdr)) {
58 puts("Bad Header Checksum\n");
59 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
60 return NULL;
61 }
62
63 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
64 image_print_contents(rd_hdr);
65
66 if (verify) {
67 puts(" Verifying Checksum ... ");
68 if (!image_check_dcrc(rd_hdr)) {
69 puts("Bad Data CRC\n");
70 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
71 return NULL;
72 }
73 puts("OK\n");
74 }
75
76 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
77
78 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
79 !image_check_arch(rd_hdr, arch) ||
80 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
81 printf("No Linux %s Ramdisk Image\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -060082 genimg_get_arch_name(arch));
Simon Glass41506ff2021-09-25 07:03:15 -060083 bootstage_error(BOOTSTAGE_ID_RAMDISK);
84 return NULL;
85 }
86
87 return rd_hdr;
88}
Tom Rini621158d2021-12-20 09:36:32 -050089#endif
Simon Glass41506ff2021-09-25 07:03:15 -060090
91/*****************************************************************************/
92/* Shared dual-format routines */
93/*****************************************************************************/
94ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
95ulong image_save_addr; /* Default Save Address */
96ulong image_save_size; /* Default Save Size (in bytes) */
97
98static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass3d2a47f2021-09-25 07:03:16 -060099 int flags)
Simon Glass41506ff2021-09-25 07:03:15 -0600100{
101 switch (op) {
102 case env_op_create:
103 case env_op_overwrite:
104 image_load_addr = hextoul(value, NULL);
105 break;
106 default:
107 break;
108 }
109
110 return 0;
111}
112U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
113
114ulong env_get_bootm_low(void)
115{
116 char *s = env_get("bootm_low");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600117
Simon Glass41506ff2021-09-25 07:03:15 -0600118 if (s) {
119 ulong tmp = hextoul(s, NULL);
120 return tmp;
121 }
122
123#if defined(CONFIG_SYS_SDRAM_BASE)
124 return CONFIG_SYS_SDRAM_BASE;
125#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
126 return gd->bd->bi_dram[0].start;
127#else
128 return 0;
129#endif
130}
131
132phys_size_t env_get_bootm_size(void)
133{
134 phys_size_t tmp, size;
135 phys_addr_t start;
136 char *s = env_get("bootm_size");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600137
Simon Glass41506ff2021-09-25 07:03:15 -0600138 if (s) {
139 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
140 return tmp;
141 }
142
143 start = gd->ram_base;
144 size = gd->ram_size;
145
146 if (start + size > gd->ram_top)
147 size = gd->ram_top - start;
148
149 s = env_get("bootm_low");
150 if (s)
151 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
152 else
153 tmp = start;
154
155 return size - (tmp - start);
156}
157
158phys_size_t env_get_bootm_mapsize(void)
159{
160 phys_size_t tmp;
161 char *s = env_get("bootm_mapsize");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600162
Simon Glass41506ff2021-09-25 07:03:15 -0600163 if (s) {
164 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
165 return tmp;
166 }
167
168#if defined(CONFIG_SYS_BOOTMAPSZ)
169 return CONFIG_SYS_BOOTMAPSZ;
170#else
171 return env_get_bootm_size();
172#endif
173}
174
175void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
176{
177 if (to == from)
178 return;
179
180#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
181 if (to > from) {
182 from += len;
183 to += len;
184 }
185 while (len > 0) {
186 size_t tail = (len > chunksz) ? chunksz : len;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600187
Simon Glass41506ff2021-09-25 07:03:15 -0600188 WATCHDOG_RESET();
189 if (to > from) {
190 to -= tail;
191 from -= tail;
192 }
193 memmove(to, from, tail);
194 if (to < from) {
195 to += tail;
196 from += tail;
197 }
198 len -= tail;
199 }
200#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
201 memmove(to, from, len);
202#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
203}
204
205/**
206 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
207 * FIT strings
208 * @img_addr: a string might contain real image address
209 * @fit_uname_config: double pointer to a char, will hold pointer to a
210 * configuration unit name
211 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
212 * name
213 *
214 * genimg_get_kernel_addr_fit get the real kernel start address from a string
215 * which is normally the first argv of bootm/bootz
216 *
217 * returns:
218 * kernel start address
219 */
220ulong genimg_get_kernel_addr_fit(char * const img_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600221 const char **fit_uname_config,
222 const char **fit_uname_kernel)
Simon Glass41506ff2021-09-25 07:03:15 -0600223{
224 ulong kernel_addr;
225
226 /* find out kernel image address */
227 if (!img_addr) {
228 kernel_addr = image_load_addr;
229 debug("* kernel: default image load address = 0x%08lx\n",
230 image_load_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600231 } else if (CONFIG_IS_ENABLED(FIT) &&
232 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass41506ff2021-09-25 07:03:15 -0600233 fit_uname_config)) {
234 debug("* kernel: config '%s' from image at 0x%08lx\n",
235 *fit_uname_config, kernel_addr);
Simon Glass1df654a2021-09-25 19:43:35 -0600236 } else if (CONFIG_IS_ENABLED(FIT) &&
237 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
238 fit_uname_kernel)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600239 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
240 *fit_uname_kernel, kernel_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600241 } else {
242 kernel_addr = hextoul(img_addr, NULL);
243 debug("* kernel: cmdline image address = 0x%08lx\n",
244 kernel_addr);
245 }
246
247 return kernel_addr;
248}
249
250/**
251 * genimg_get_kernel_addr() is the simple version of
252 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
253 */
254ulong genimg_get_kernel_addr(char * const img_addr)
255{
256 const char *fit_uname_config = NULL;
257 const char *fit_uname_kernel = NULL;
258
259 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
260 &fit_uname_kernel);
261}
262
263/**
264 * genimg_get_format - get image format type
265 * @img_addr: image start address
266 *
267 * genimg_get_format() checks whether provided address points to a valid
268 * legacy or FIT image.
269 *
270 * New uImage format and FDT blob are based on a libfdt. FDT blob
271 * may be passed directly or embedded in a FIT image. In both situations
272 * genimg_get_format() must be able to dectect libfdt header.
273 *
274 * returns:
275 * image format type or IMAGE_FORMAT_INVALID if no image is present
276 */
277int genimg_get_format(const void *img_addr)
278{
Simon Glass1df654a2021-09-25 19:43:35 -0600279 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
280 const image_header_t *hdr;
Simon Glass41506ff2021-09-25 07:03:15 -0600281
Simon Glass1df654a2021-09-25 19:43:35 -0600282 hdr = (const image_header_t *)img_addr;
283 if (image_check_magic(hdr))
284 return IMAGE_FORMAT_LEGACY;
285 }
286 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
287 if (!fdt_check_header(img_addr))
288 return IMAGE_FORMAT_FIT;
289 }
290 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
291 !android_image_check_header(img_addr))
Simon Glass41506ff2021-09-25 07:03:15 -0600292 return IMAGE_FORMAT_ANDROID;
Simon Glass41506ff2021-09-25 07:03:15 -0600293
294 return IMAGE_FORMAT_INVALID;
295}
296
297/**
298 * fit_has_config - check if there is a valid FIT configuration
299 * @images: pointer to the bootm command headers structure
300 *
301 * fit_has_config() checks if there is a FIT configuration in use
302 * (if FTI support is present).
303 *
304 * returns:
305 * 0, no FIT support or no configuration found
306 * 1, configuration found
307 */
308int genimg_has_config(bootm_headers_t *images)
309{
Simon Glass1df654a2021-09-25 19:43:35 -0600310 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass41506ff2021-09-25 07:03:15 -0600311 return 1;
Simon Glass1df654a2021-09-25 19:43:35 -0600312
Simon Glass41506ff2021-09-25 07:03:15 -0600313 return 0;
314}
315
316/**
Simon Glasse4c92872021-09-25 19:43:37 -0600317 * select_ramdisk() - Select and locate the ramdisk to use
318 *
Simon Glass41506ff2021-09-25 07:03:15 -0600319 * @images: pointer to the bootm images structure
Simon Glasse4c92872021-09-25 19:43:37 -0600320 * @select: name of ramdisk to select, or NULL for any
Simon Glass41506ff2021-09-25 07:03:15 -0600321 * @arch: expected ramdisk architecture
Simon Glasse4c92872021-09-25 19:43:37 -0600322 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
323 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100324 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
Simon Glasse4c92872021-09-25 19:43:37 -0600325 * other -ve value on other error
Simon Glass41506ff2021-09-25 07:03:15 -0600326 */
Simon Glasse4c92872021-09-25 19:43:37 -0600327static int select_ramdisk(bootm_headers_t *images, const char *select, u8 arch,
328 ulong *rd_datap, ulong *rd_lenp)
Simon Glass41506ff2021-09-25 07:03:15 -0600329{
Tom Rini621158d2021-12-20 09:36:32 -0500330 ulong rd_addr;
Simon Glasse4c92872021-09-25 19:43:37 -0600331 char *buf;
Simon Glass78f88792021-09-25 19:43:36 -0600332
Tom Rini621158d2021-12-20 09:36:32 -0500333#if CONFIG_IS_ENABLED(FIT)
334 const char *fit_uname_config = images->fit_uname_cfg;
335 const char *fit_uname_ramdisk = NULL;
336 int rd_noffset;
Simon Glass78f88792021-09-25 19:43:36 -0600337
Tom Rini621158d2021-12-20 09:36:32 -0500338 if (select) {
339 ulong default_addr;
Simon Glass41506ff2021-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
Tom Rini621158d2021-12-20 09:36:32 -0500351 if (fit_parse_conf(select, default_addr,
352 &rd_addr, &fit_uname_config)) {
Simon Glass3d2a47f2021-09-25 07:03:16 -0600353 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
354 fit_uname_config, rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600355 } else if (fit_parse_subimage(select, default_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600356 &rd_addr,
357 &fit_uname_ramdisk)) {
358 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
359 fit_uname_ramdisk, rd_addr);
Tom Rini621158d2021-12-20 09:36:32 -0500360 } else
361#endif
362 {
363 rd_addr = hextoul(select, NULL);
364 debug("* ramdisk: cmdline image address = 0x%08lx\n",
365 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600366 }
Tom Rini621158d2021-12-20 09:36:32 -0500367#if CONFIG_IS_ENABLED(FIT)
368 } else {
369 /* use FIT configuration provided in first bootm
370 * command argument. If the property is not defined,
371 * quit silently (with -ENOPKG)
372 */
373 rd_addr = map_to_sysmem(images->fit_hdr_os);
374 rd_noffset = fit_get_node_from_config(images,
375 FIT_RAMDISK_PROP,
376 rd_addr);
377 if (rd_noffset == -ENOENT)
378 return -ENOPKG;
379 else if (rd_noffset < 0)
380 return rd_noffset;
Simon Glass41506ff2021-09-25 07:03:15 -0600381 }
Tom Rini621158d2021-12-20 09:36:32 -0500382#endif
Simon Glassf33a2c12021-09-25 19:43:38 -0600383
Tom Rini621158d2021-12-20 09:36:32 -0500384 /*
385 * Check if there is an initrd image at the
386 * address provided in the second bootm argument
387 * check image type, for FIT images get FIT node.
388 */
389 buf = map_sysmem(rd_addr, 0);
390 switch (genimg_get_format(buf)) {
391#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
392 case IMAGE_FORMAT_LEGACY: {
Simon Glass78f88792021-09-25 19:43:36 -0600393 const image_header_t *rd_hdr;
394
Simon Glass3d2a47f2021-09-25 07:03:16 -0600395 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
396 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600397
398 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Tom Rini621158d2021-12-20 09:36:32 -0500399 rd_hdr = image_get_ramdisk(rd_addr, arch,
400 images->verify);
401
Simon Glass3d2a47f2021-09-25 07:03:16 -0600402 if (!rd_hdr)
Simon Glasse4c92872021-09-25 19:43:37 -0600403 return -ENOENT;
Simon Glass41506ff2021-09-25 07:03:15 -0600404
Simon Glasse4c92872021-09-25 19:43:37 -0600405 *rd_datap = image_get_data(rd_hdr);
406 *rd_lenp = image_get_data_size(rd_hdr);
Tom Rini621158d2021-12-20 09:36:32 -0500407 break;
Simon Glass78f88792021-09-25 19:43:36 -0600408 }
Tom Rini621158d2021-12-20 09:36:32 -0500409#endif
410#if CONFIG_IS_ENABLED(FIT)
411 case IMAGE_FORMAT_FIT:
412 rd_noffset = fit_image_load(images,
413 rd_addr, &fit_uname_ramdisk,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600414 &fit_uname_config, arch,
415 IH_TYPE_RAMDISK,
416 BOOTSTAGE_ID_FIT_RD_START,
417 FIT_LOAD_OPTIONAL_NON_ZERO,
Simon Glasse4c92872021-09-25 19:43:37 -0600418 rd_datap, rd_lenp);
Simon Glass41506ff2021-09-25 07:03:15 -0600419 if (rd_noffset < 0)
Simon Glasse4c92872021-09-25 19:43:37 -0600420 return rd_noffset;
Simon Glass41506ff2021-09-25 07:03:15 -0600421
422 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
423 images->fit_uname_rd = fit_uname_ramdisk;
424 images->fit_noffset_rd = rd_noffset;
Tom Rini621158d2021-12-20 09:36:32 -0500425 break;
426#endif
427#ifdef CONFIG_ANDROID_BOOT_IMAGE
428 case IMAGE_FORMAT_ANDROID:
Simon Glass41506ff2021-09-25 07:03:15 -0600429 android_image_get_ramdisk((void *)images->os.start,
Simon Glasse4c92872021-09-25 19:43:37 -0600430 rd_datap, rd_lenp);
Tom Rini621158d2021-12-20 09:36:32 -0500431 break;
432#endif
433 default:
434 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
435 char *end = NULL;
Simon Glass1df654a2021-09-25 19:43:35 -0600436
Tom Rini621158d2021-12-20 09:36:32 -0500437 if (select)
438 end = strchr(select, ':');
439 if (end) {
440 *rd_lenp = hextoul(++end, NULL);
441 *rd_datap = rd_addr;
442 break;
443 }
Simon Glass41506ff2021-09-25 07:03:15 -0600444 }
Simon Glass1df654a2021-09-25 19:43:35 -0600445 puts("Wrong Ramdisk Image Format\n");
Simon Glasse4c92872021-09-25 19:43:37 -0600446 return -EINVAL;
Simon Glass41506ff2021-09-25 07:03:15 -0600447 }
Simon Glasse4c92872021-09-25 19:43:37 -0600448
449 return 0;
450}
451
452/**
453 * boot_get_ramdisk - main ramdisk handling routine
454 * @argc: command argument count
455 * @argv: command argument list
456 * @images: pointer to the bootm images structure
457 * @arch: expected ramdisk architecture
458 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
459 * @rd_end: pointer to a ulong variable, will hold ramdisk end
460 *
461 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
462 * Currently supported are the following ramdisk sources:
463 * - multicomponent kernel/ramdisk image,
464 * - commandline provided address of decicated ramdisk image.
465 *
466 * returns:
467 * 0, if ramdisk image was found and valid, or skiped
468 * rd_start and rd_end are set to ramdisk start/end addresses if
469 * ramdisk image is found and valid
470 *
471 * 1, if ramdisk image is found but corrupted, or invalid
472 * rd_start and rd_end are set to 0 if no ramdisk exists
473 */
474int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
475 u8 arch, ulong *rd_start, ulong *rd_end)
476{
477 ulong rd_data, rd_len;
478 const char *select = NULL;
479
480 *rd_start = 0;
481 *rd_end = 0;
482
483 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
484 char *buf;
485
486 /* Look for an Android boot image */
487 buf = map_sysmem(images->os.start, 0);
488 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
489 select = (argc == 0) ? env_get("loadaddr") : argv[0];
490 }
491
492 if (argc >= 2)
493 select = argv[1];
494
495 /*
496 * Look for a '-' which indicates to ignore the
497 * ramdisk argument
498 */
499 if (select && strcmp(select, "-") == 0) {
500 debug("## Skipping init Ramdisk\n");
501 rd_len = 0;
502 rd_data = 0;
503 } else if (select || genimg_has_config(images)) {
504 int ret;
505
506 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
507 if (ret == -ENOPKG)
508 return 0;
509 else if (ret)
510 return ret;
Simon Glass41506ff2021-09-25 07:03:15 -0600511 } else if (images->legacy_hdr_valid &&
512 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600513 IH_TYPE_MULTI)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600514 /*
515 * Now check if we have a legacy mult-component image,
516 * get second entry data start address and len.
517 */
518 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600519 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
520 (ulong)images->legacy_hdr_os);
Simon Glass41506ff2021-09-25 07:03:15 -0600521
522 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
523 } else {
524 /*
525 * no initrd image
526 */
527 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600528 rd_len = 0;
529 rd_data = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600530 }
531
532 if (!rd_data) {
533 debug("## No init Ramdisk\n");
534 } else {
535 *rd_start = rd_data;
536 *rd_end = rd_data + rd_len;
537 }
538 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600539 *rd_start, *rd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600540
541 return 0;
542}
543
Simon Glass41506ff2021-09-25 07:03:15 -0600544/**
545 * boot_ramdisk_high - relocate init ramdisk
546 * @lmb: pointer to lmb handle, will be used for memory mgmt
547 * @rd_data: ramdisk data start address
548 * @rd_len: ramdisk data length
549 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
550 * start address (after possible relocation)
551 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
552 * end address (after possible relocation)
553 *
554 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
555 * variable and if requested ramdisk data is moved to a specified location.
556 *
557 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
558 * start/end addresses if ramdisk image start and len were provided,
559 * otherwise set initrd_start and initrd_end set to zeros.
560 *
561 * returns:
562 * 0 - success
563 * -1 - failure
564 */
565int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600566 ulong *initrd_start, ulong *initrd_end)
Simon Glass41506ff2021-09-25 07:03:15 -0600567{
568 char *s;
569 ulong initrd_high;
570 int initrd_copy_to_ram = 1;
571
572 s = env_get("initrd_high");
573 if (s) {
574 /* a value of "no" or a similar string will act like 0,
575 * turning the "load high" feature off. This is intentional.
576 */
577 initrd_high = hextoul(s, NULL);
578 if (initrd_high == ~0)
579 initrd_copy_to_ram = 0;
580 } else {
581 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
582 }
583
Simon Glass41506ff2021-09-25 07:03:15 -0600584 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600585 initrd_high, initrd_copy_to_ram);
Simon Glass41506ff2021-09-25 07:03:15 -0600586
587 if (rd_data) {
588 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
589 debug(" in-place initrd\n");
590 *initrd_start = rd_data;
591 *initrd_end = rd_data + rd_len;
592 lmb_reserve(lmb, rd_data, rd_len);
593 } else {
594 if (initrd_high)
595 *initrd_start = (ulong)lmb_alloc_base(lmb,
596 rd_len, 0x1000, initrd_high);
597 else
598 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
599 0x1000);
600
601 if (*initrd_start == 0) {
602 puts("ramdisk - allocation error\n");
603 goto error;
604 }
605 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
606
607 *initrd_end = *initrd_start + rd_len;
608 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600609 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600610
611 memmove_wd((void *)*initrd_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600612 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass41506ff2021-09-25 07:03:15 -0600613
Simon Glass41506ff2021-09-25 07:03:15 -0600614 /*
615 * Ensure the image is flushed to memory to handle
616 * AMP boot scenarios in which we might not be
617 * HW cache coherent
618 */
Simon Glass1df654a2021-09-25 19:43:35 -0600619 if (IS_ENABLED(CONFIG_MP)) {
620 flush_cache((unsigned long)*initrd_start,
621 ALIGN(rd_len, ARCH_DMA_MINALIGN));
622 }
Simon Glass41506ff2021-09-25 07:03:15 -0600623 puts("OK\n");
624 }
625 } else {
626 *initrd_start = 0;
627 *initrd_end = 0;
628 }
629 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600630 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600631
632 return 0;
633
634error:
635 return -1;
636}
Simon Glass41506ff2021-09-25 07:03:15 -0600637
Simon Glass3d2a47f2021-09-25 07:03:16 -0600638int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass41506ff2021-09-25 07:03:15 -0600639 ulong *setup_start, ulong *setup_len)
640{
Simon Glass1df654a2021-09-25 19:43:35 -0600641 if (!CONFIG_IS_ENABLED(FIT))
642 return -ENOENT;
643
Simon Glass41506ff2021-09-25 07:03:15 -0600644 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600645}
646
Simon Glass41506ff2021-09-25 07:03:15 -0600647int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600648 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600649{
650 ulong tmp_img_addr, img_data, img_len;
651 void *buf;
652 int conf_noffset;
653 int fit_img_result;
654 const char *uname, *name;
655 int err;
656 int devnum = 0; /* TODO support multi fpga platforms */
657
Simon Glass1df654a2021-09-25 19:43:35 -0600658 if (!IS_ENABLED(CONFIG_FPGA))
659 return -ENOSYS;
660
Simon Glass41506ff2021-09-25 07:03:15 -0600661 /* Check to see if the images struct has a FIT configuration */
662 if (!genimg_has_config(images)) {
663 debug("## FIT configuration was not specified\n");
664 return 0;
665 }
666
667 /*
668 * Obtain the os FIT header from the images struct
669 */
670 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
671 buf = map_sysmem(tmp_img_addr, 0);
672 /*
673 * Check image type. For FIT images get FIT node
674 * and attempt to locate a generic binary.
675 */
676 switch (genimg_get_format(buf)) {
677 case IMAGE_FORMAT_FIT:
678 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
679
680 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
681 NULL);
682 if (!uname) {
683 debug("## FPGA image is not specified\n");
684 return 0;
685 }
686 fit_img_result = fit_image_load(images,
687 tmp_img_addr,
688 (const char **)&uname,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600689 &images->fit_uname_cfg,
Simon Glass41506ff2021-09-25 07:03:15 -0600690 arch,
691 IH_TYPE_FPGA,
692 BOOTSTAGE_ID_FPGA_INIT,
693 FIT_LOAD_OPTIONAL_NON_ZERO,
694 &img_data, &img_len);
695
696 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
697 uname, img_data, img_len);
698
699 if (fit_img_result < 0) {
700 /* Something went wrong! */
701 return fit_img_result;
702 }
703
704 if (!fpga_is_partial_data(devnum, img_len)) {
705 name = "full";
706 err = fpga_loadbitstream(devnum, (char *)img_data,
707 img_len, BIT_FULL);
708 if (err)
709 err = fpga_load(devnum, (const void *)img_data,
710 img_len, BIT_FULL);
711 } else {
712 name = "partial";
713 err = fpga_loadbitstream(devnum, (char *)img_data,
714 img_len, BIT_PARTIAL);
715 if (err)
716 err = fpga_load(devnum, (const void *)img_data,
717 img_len, BIT_PARTIAL);
718 }
719
720 if (err)
721 return err;
722
723 printf(" Programming %s bitstream... OK\n", name);
724 break;
725 default:
726 printf("The given image format is not supported (corrupt?)\n");
727 return 1;
728 }
729
730 return 0;
731}
Simon Glass41506ff2021-09-25 07:03:15 -0600732
Simon Glass3d2a47f2021-09-25 07:03:16 -0600733static void fit_loadable_process(u8 img_type,
Simon Glass41506ff2021-09-25 07:03:15 -0600734 ulong img_data,
735 ulong img_len)
736{
737 int i;
738 const unsigned int count =
739 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
740 struct fit_loadable_tbl *fit_loadable_handler =
741 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
742 /* For each loadable handler */
743 for (i = 0; i < count; i++, fit_loadable_handler++)
744 /* matching this type */
745 if (fit_loadable_handler->type == img_type)
746 /* call that handler with this image data */
747 fit_loadable_handler->handler(img_data, img_len);
748}
749
750int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600751 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600752{
753 /*
754 * These variables are used to hold the current image location
755 * in system memory.
756 */
757 ulong tmp_img_addr;
758 /*
759 * These two variables are requirements for fit_image_load, but
760 * their values are not used
761 */
762 ulong img_data, img_len;
763 void *buf;
764 int loadables_index;
765 int conf_noffset;
766 int fit_img_result;
767 const char *uname;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600768 u8 img_type;
Simon Glass41506ff2021-09-25 07:03:15 -0600769
770 /* Check to see if the images struct has a FIT configuration */
771 if (!genimg_has_config(images)) {
772 debug("## FIT configuration was not specified\n");
773 return 0;
774 }
775
776 /*
777 * Obtain the os FIT header from the images struct
778 */
779 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
780 buf = map_sysmem(tmp_img_addr, 0);
781 /*
782 * Check image type. For FIT images get FIT node
783 * and attempt to locate a generic binary.
784 */
785 switch (genimg_get_format(buf)) {
786 case IMAGE_FORMAT_FIT:
787 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
788
789 for (loadables_index = 0;
790 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600791 FIT_LOADABLE_PROP,
792 loadables_index, NULL), uname;
793 loadables_index++) {
794 fit_img_result = fit_image_load(images, tmp_img_addr,
795 &uname,
796 &images->fit_uname_cfg,
797 arch, IH_TYPE_LOADABLE,
798 BOOTSTAGE_ID_FIT_LOADABLE_START,
799 FIT_LOAD_OPTIONAL_NON_ZERO,
800 &img_data, &img_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600801 if (fit_img_result < 0) {
802 /* Something went wrong! */
803 return fit_img_result;
804 }
805
806 fit_img_result = fit_image_get_node(buf, uname);
807 if (fit_img_result < 0) {
808 /* Something went wrong! */
809 return fit_img_result;
810 }
811 fit_img_result = fit_image_get_type(buf,
812 fit_img_result,
813 &img_type);
814 if (fit_img_result < 0) {
815 /* Something went wrong! */
816 return fit_img_result;
817 }
818
819 fit_loadable_process(img_type, img_data, img_len);
820 }
821 break;
822 default:
823 printf("The given image format is not supported (corrupt?)\n");
824 return 1;
825 }
826
827 return 0;
828}
Simon Glass41506ff2021-09-25 07:03:15 -0600829
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -0600852 if (!cmdline)
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -0600861 *cmd_start = (ulong)cmdline;
Simon Glass41506ff2021-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 Glass41506ff2021-09-25 07:03:15 -0600868
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -0600887 env_get_bootm_mapsize() +
888 env_get_bootm_low());
889 if (!*kbd)
Simon Glass41506ff2021-09-25 07:03:15 -0600890 return -1;
891
Simon Glass3d2a47f2021-09-25 07:03:16 -0600892 **kbd = *gd->bd;
Simon Glass41506ff2021-09-25 07:03:15 -0600893
894 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
895
Simon Glass4ed37ab2021-09-25 07:03:20 -0600896#if defined(DEBUG)
Leo Yu-Chi Liang990e1e42021-10-27 17:00:41 +0800897 if (IS_ENABLED(CONFIG_CMD_BDI))
Simon Glass4ed37ab2021-09-25 07:03:20 -0600898 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass41506ff2021-09-25 07:03:15 -0600899#endif
900
901 return 0;
902}
Simon Glass41506ff2021-09-25 07:03:15 -0600903
Simon Glass41506ff2021-09-25 07:03:15 -0600904int image_setup_linux(bootm_headers_t *images)
905{
906 ulong of_size = images->ft_len;
907 char **of_flat_tree = &images->ft_addr;
908 struct lmb *lmb = &images->lmb;
909 int ret;
910
Simon Glass0c303f92021-09-25 19:43:21 -0600911 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass41506ff2021-09-25 07:03:15 -0600912 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
913
Simon Glass806d1ff2021-09-25 19:43:25 -0600914 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600915 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600916 &images->cmdline_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600917 if (ret) {
918 puts("ERROR with allocation of cmdline\n");
919 return ret;
920 }
921 }
922
Simon Glass0c303f92021-09-25 19:43:21 -0600923 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600924 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
925 if (ret)
926 return ret;
927 }
928
Simon Glass0c303f92021-09-25 19:43:21 -0600929 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass41506ff2021-09-25 07:03:15 -0600930 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
931 if (ret)
932 return ret;
933 }
934
935 return 0;
936}
Simon Glass5d3248a2021-09-25 07:03:17 -0600937
938void genimg_print_size(uint32_t size)
939{
940 printf("%d Bytes = ", size);
941 print_size(size, "\n");
942}
943
944void genimg_print_time(time_t timestamp)
945{
946 struct rtc_time tm;
947
948 rtc_to_tm(timestamp, &tm);
949 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
950 tm.tm_year, tm.tm_mon, tm.tm_mday,
951 tm.tm_hour, tm.tm_min, tm.tm_sec);
952}