blob: bf8817165cab2651f49f8bacf8de84201e7b4d20 [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
24#ifndef CONFIG_SYS_BARGSIZE
25#define CONFIG_SYS_BARGSIZE 512
26#endif
27
28DECLARE_GLOBAL_DATA_PTR;
29
Simon Glass4d7237b2021-09-25 07:03:15 -060030/**
31 * image_get_ramdisk - get and verify ramdisk image
32 * @rd_addr: ramdisk image start address
33 * @arch: expected ramdisk architecture
34 * @verify: checksum verification flag
35 *
36 * image_get_ramdisk() returns a pointer to the verified ramdisk image
37 * header. Routine receives image start address and expected architecture
38 * flag. Verification done covers data and header integrity and os/type/arch
39 * fields checking.
40 *
41 * returns:
42 * pointer to a ramdisk image header, if image was found and valid
43 * otherwise, return NULL
44 */
Simon Glass60d71542021-09-25 07:03:16 -060045static const image_header_t *image_get_ramdisk(ulong rd_addr, u8 arch,
46 int verify)
Simon Glass4d7237b2021-09-25 07:03:15 -060047{
48 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
49
50 if (!image_check_magic(rd_hdr)) {
51 puts("Bad Magic Number\n");
52 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
53 return NULL;
54 }
55
56 if (!image_check_hcrc(rd_hdr)) {
57 puts("Bad Header Checksum\n");
58 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
59 return NULL;
60 }
61
62 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
63 image_print_contents(rd_hdr);
64
65 if (verify) {
66 puts(" Verifying Checksum ... ");
67 if (!image_check_dcrc(rd_hdr)) {
68 puts("Bad Data CRC\n");
69 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
70 return NULL;
71 }
72 puts("OK\n");
73 }
74
75 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
76
77 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
78 !image_check_arch(rd_hdr, arch) ||
79 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
80 printf("No Linux %s Ramdisk Image\n",
Simon Glass60d71542021-09-25 07:03:16 -060081 genimg_get_arch_name(arch));
Simon Glass4d7237b2021-09-25 07:03:15 -060082 bootstage_error(BOOTSTAGE_ID_RAMDISK);
83 return NULL;
84 }
85
86 return rd_hdr;
87}
Simon Glass4d7237b2021-09-25 07:03:15 -060088
89/*****************************************************************************/
90/* Shared dual-format routines */
91/*****************************************************************************/
92ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
93ulong image_save_addr; /* Default Save Address */
94ulong image_save_size; /* Default Save Size (in bytes) */
95
96static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass60d71542021-09-25 07:03:16 -060097 int flags)
Simon Glass4d7237b2021-09-25 07:03:15 -060098{
99 switch (op) {
100 case env_op_create:
101 case env_op_overwrite:
102 image_load_addr = hextoul(value, NULL);
103 break;
104 default:
105 break;
106 }
107
108 return 0;
109}
110U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
111
112ulong env_get_bootm_low(void)
113{
114 char *s = env_get("bootm_low");
Simon Glass60d71542021-09-25 07:03:16 -0600115
Simon Glass4d7237b2021-09-25 07:03:15 -0600116 if (s) {
117 ulong tmp = hextoul(s, NULL);
118 return tmp;
119 }
120
121#if defined(CONFIG_SYS_SDRAM_BASE)
122 return CONFIG_SYS_SDRAM_BASE;
123#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
124 return gd->bd->bi_dram[0].start;
125#else
126 return 0;
127#endif
128}
129
130phys_size_t env_get_bootm_size(void)
131{
132 phys_size_t tmp, size;
133 phys_addr_t start;
134 char *s = env_get("bootm_size");
Simon Glass60d71542021-09-25 07:03:16 -0600135
Simon Glass4d7237b2021-09-25 07:03:15 -0600136 if (s) {
137 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
138 return tmp;
139 }
140
141 start = gd->ram_base;
142 size = gd->ram_size;
143
144 if (start + size > gd->ram_top)
145 size = gd->ram_top - start;
146
147 s = env_get("bootm_low");
148 if (s)
149 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
150 else
151 tmp = start;
152
153 return size - (tmp - start);
154}
155
156phys_size_t env_get_bootm_mapsize(void)
157{
158 phys_size_t tmp;
159 char *s = env_get("bootm_mapsize");
Simon Glass60d71542021-09-25 07:03:16 -0600160
Simon Glass4d7237b2021-09-25 07:03:15 -0600161 if (s) {
162 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
163 return tmp;
164 }
165
166#if defined(CONFIG_SYS_BOOTMAPSZ)
167 return CONFIG_SYS_BOOTMAPSZ;
168#else
169 return env_get_bootm_size();
170#endif
171}
172
173void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
174{
175 if (to == from)
176 return;
177
178#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
179 if (to > from) {
180 from += len;
181 to += len;
182 }
183 while (len > 0) {
184 size_t tail = (len > chunksz) ? chunksz : len;
Simon Glass60d71542021-09-25 07:03:16 -0600185
Simon Glass4d7237b2021-09-25 07:03:15 -0600186 WATCHDOG_RESET();
187 if (to > from) {
188 to -= tail;
189 from -= tail;
190 }
191 memmove(to, from, tail);
192 if (to < from) {
193 to += tail;
194 from += tail;
195 }
196 len -= tail;
197 }
198#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
199 memmove(to, from, len);
200#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
201}
202
203/**
204 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
205 * FIT strings
206 * @img_addr: a string might contain real image address
207 * @fit_uname_config: double pointer to a char, will hold pointer to a
208 * configuration unit name
209 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
210 * name
211 *
212 * genimg_get_kernel_addr_fit get the real kernel start address from a string
213 * which is normally the first argv of bootm/bootz
214 *
215 * returns:
216 * kernel start address
217 */
218ulong genimg_get_kernel_addr_fit(char * const img_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600219 const char **fit_uname_config,
220 const char **fit_uname_kernel)
Simon Glass4d7237b2021-09-25 07:03:15 -0600221{
222 ulong kernel_addr;
223
224 /* find out kernel image address */
225 if (!img_addr) {
226 kernel_addr = image_load_addr;
227 debug("* kernel: default image load address = 0x%08lx\n",
228 image_load_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600229 } else if (CONFIG_IS_ENABLED(FIT) &&
230 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass4d7237b2021-09-25 07:03:15 -0600231 fit_uname_config)) {
232 debug("* kernel: config '%s' from image at 0x%08lx\n",
233 *fit_uname_config, kernel_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600234 } else if (CONFIG_IS_ENABLED(FIT) &&
235 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
236 fit_uname_kernel)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600237 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
238 *fit_uname_kernel, kernel_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600239 } else {
240 kernel_addr = hextoul(img_addr, NULL);
241 debug("* kernel: cmdline image address = 0x%08lx\n",
242 kernel_addr);
243 }
244
245 return kernel_addr;
246}
247
248/**
249 * genimg_get_kernel_addr() is the simple version of
250 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
251 */
252ulong genimg_get_kernel_addr(char * const img_addr)
253{
254 const char *fit_uname_config = NULL;
255 const char *fit_uname_kernel = NULL;
256
257 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
258 &fit_uname_kernel);
259}
260
261/**
262 * genimg_get_format - get image format type
263 * @img_addr: image start address
264 *
265 * genimg_get_format() checks whether provided address points to a valid
266 * legacy or FIT image.
267 *
268 * New uImage format and FDT blob are based on a libfdt. FDT blob
269 * may be passed directly or embedded in a FIT image. In both situations
270 * genimg_get_format() must be able to dectect libfdt header.
271 *
272 * returns:
273 * image format type or IMAGE_FORMAT_INVALID if no image is present
274 */
275int genimg_get_format(const void *img_addr)
276{
Simon Glass0f781382021-09-25 19:43:35 -0600277 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
278 const image_header_t *hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600279
Simon Glass0f781382021-09-25 19:43:35 -0600280 hdr = (const image_header_t *)img_addr;
281 if (image_check_magic(hdr))
282 return IMAGE_FORMAT_LEGACY;
283 }
284 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
285 if (!fdt_check_header(img_addr))
286 return IMAGE_FORMAT_FIT;
287 }
288 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
289 !android_image_check_header(img_addr))
Simon Glass4d7237b2021-09-25 07:03:15 -0600290 return IMAGE_FORMAT_ANDROID;
Simon Glass4d7237b2021-09-25 07:03:15 -0600291
292 return IMAGE_FORMAT_INVALID;
293}
294
295/**
296 * fit_has_config - check if there is a valid FIT configuration
297 * @images: pointer to the bootm command headers structure
298 *
299 * fit_has_config() checks if there is a FIT configuration in use
300 * (if FTI support is present).
301 *
302 * returns:
303 * 0, no FIT support or no configuration found
304 * 1, configuration found
305 */
306int genimg_has_config(bootm_headers_t *images)
307{
Simon Glass0f781382021-09-25 19:43:35 -0600308 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass4d7237b2021-09-25 07:03:15 -0600309 return 1;
Simon Glass0f781382021-09-25 19:43:35 -0600310
Simon Glass4d7237b2021-09-25 07:03:15 -0600311 return 0;
312}
313
314/**
Simon Glassccda2fa2021-09-25 19:43:37 -0600315 * select_ramdisk() - Select and locate the ramdisk to use
316 *
Simon Glass4d7237b2021-09-25 07:03:15 -0600317 * @images: pointer to the bootm images structure
Simon Glassccda2fa2021-09-25 19:43:37 -0600318 * @select: name of ramdisk to select, or NULL for any
Simon Glass4d7237b2021-09-25 07:03:15 -0600319 * @arch: expected ramdisk architecture
Simon Glassccda2fa2021-09-25 19:43:37 -0600320 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
321 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
322 * @return 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
323 * other -ve value on other error
Simon Glass4d7237b2021-09-25 07:03:15 -0600324 */
Simon Glassccda2fa2021-09-25 19:43:37 -0600325static int select_ramdisk(bootm_headers_t *images, const char *select, u8 arch,
326 ulong *rd_datap, ulong *rd_lenp)
Simon Glass4d7237b2021-09-25 07:03:15 -0600327{
Simon Glasse5661c92021-09-25 19:43:38 -0600328 ulong rd_addr = 0;
Simon Glassccda2fa2021-09-25 19:43:37 -0600329 char *buf;
Simon Glasse5661c92021-09-25 19:43:38 -0600330 const char *fit_uname_config = images->fit_uname_cfg;
331 const char *fit_uname_ramdisk = NULL;
332 bool processed;
333 int rd_noffset;
Simon Glass2de5b012021-09-25 19:43:36 -0600334
Simon Glasse5661c92021-09-25 19:43:38 -0600335 if (select) {
336 ulong default_addr;
337 bool done = true;
Simon Glass2de5b012021-09-25 19:43:36 -0600338
Simon Glasse5661c92021-09-25 19:43:38 -0600339 if (CONFIG_IS_ENABLED(FIT)) {
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 Glasse5661c92021-09-25 19:43:38 -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 Glass4d7237b2021-09-25 07:03:15 -0600355 } else if (fit_parse_subimage(select, default_addr,
Simon Glass60d71542021-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);
Simon Glasse5661c92021-09-25 19:43:38 -0600360 } else {
361 done = false;
Simon Glass4d7237b2021-09-25 07:03:15 -0600362 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600363 }
Simon Glasse5661c92021-09-25 19:43:38 -0600364 if (!done) {
365 rd_addr = hextoul(select, NULL);
366 debug("* ramdisk: cmdline image address = 0x%08lx\n",
367 rd_addr);
368 }
369 } else if (CONFIG_IS_ENABLED(FIT)) {
370 /* use FIT configuration provided in first bootm
371 * command argument. If the property is not defined,
372 * quit silently (with -ENOPKG )
Simon Glass4d7237b2021-09-25 07:03:15 -0600373 */
Simon Glasse5661c92021-09-25 19:43:38 -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 }
382
383 /*
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 processed = false;
390 switch (genimg_get_format(buf)) {
391 case IMAGE_FORMAT_LEGACY:
392 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
Simon Glass2de5b012021-09-25 19:43:36 -0600393 const image_header_t *rd_hdr;
394
Simon Glass60d71542021-09-25 07:03:16 -0600395 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
396 rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600397
398 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
Simon Glasse5661c92021-09-25 19:43:38 -0600399 rd_hdr = image_get_ramdisk(rd_addr, arch, images->verify);
Simon Glass60d71542021-09-25 07:03:16 -0600400 if (!rd_hdr)
Simon Glassccda2fa2021-09-25 19:43:37 -0600401 return -ENOENT;
Simon Glass4d7237b2021-09-25 07:03:15 -0600402
Simon Glassccda2fa2021-09-25 19:43:37 -0600403 *rd_datap = image_get_data(rd_hdr);
404 *rd_lenp = image_get_data_size(rd_hdr);
Simon Glasse5661c92021-09-25 19:43:38 -0600405 processed = true;
Simon Glass2de5b012021-09-25 19:43:36 -0600406 }
Simon Glasse5661c92021-09-25 19:43:38 -0600407 break;
408 case IMAGE_FORMAT_FIT:
409 if (CONFIG_IS_ENABLED(FIT)) {
410 rd_noffset = fit_image_load(images, rd_addr,
411 &fit_uname_ramdisk,
Simon Glass60d71542021-09-25 07:03:16 -0600412 &fit_uname_config, arch,
413 IH_TYPE_RAMDISK,
414 BOOTSTAGE_ID_FIT_RD_START,
415 FIT_LOAD_OPTIONAL_NON_ZERO,
Simon Glassccda2fa2021-09-25 19:43:37 -0600416 rd_datap, rd_lenp);
Simon Glass4d7237b2021-09-25 07:03:15 -0600417 if (rd_noffset < 0)
Simon Glassccda2fa2021-09-25 19:43:37 -0600418 return rd_noffset;
Simon Glass4d7237b2021-09-25 07:03:15 -0600419
420 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
421 images->fit_uname_rd = fit_uname_ramdisk;
422 images->fit_noffset_rd = rd_noffset;
Simon Glasse5661c92021-09-25 19:43:38 -0600423 processed = true;
424 }
425 break;
426 case IMAGE_FORMAT_ANDROID:
427 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600428 android_image_get_ramdisk((void *)images->os.start,
Simon Glassccda2fa2021-09-25 19:43:37 -0600429 rd_datap, rd_lenp);
Simon Glasse5661c92021-09-25 19:43:38 -0600430 processed = true;
431 }
432 break;
433 }
434
435 if (!processed) {
436 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
437 char *end = NULL;
Simon Glass0f781382021-09-25 19:43:35 -0600438
Simon Glasse5661c92021-09-25 19:43:38 -0600439 if (select)
440 end = strchr(select, ':');
441 if (end) {
442 *rd_lenp = hextoul(++end, NULL);
443 *rd_datap = rd_addr;
444 processed = true;
Simon Glass4d7237b2021-09-25 07:03:15 -0600445 }
Simon Glasse5661c92021-09-25 19:43:38 -0600446 }
447
448 if (!processed) {
Simon Glass0f781382021-09-25 19:43:35 -0600449 puts("Wrong Ramdisk Image Format\n");
Simon Glassccda2fa2021-09-25 19:43:37 -0600450 return -EINVAL;
Simon Glass4d7237b2021-09-25 07:03:15 -0600451 }
Simon Glasse5661c92021-09-25 19:43:38 -0600452 }
Simon Glassccda2fa2021-09-25 19:43:37 -0600453
454 return 0;
455}
456
457/**
458 * boot_get_ramdisk - main ramdisk handling routine
459 * @argc: command argument count
460 * @argv: command argument list
461 * @images: pointer to the bootm images structure
462 * @arch: expected ramdisk architecture
463 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
464 * @rd_end: pointer to a ulong variable, will hold ramdisk end
465 *
466 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
467 * Currently supported are the following ramdisk sources:
468 * - multicomponent kernel/ramdisk image,
469 * - commandline provided address of decicated ramdisk image.
470 *
471 * returns:
472 * 0, if ramdisk image was found and valid, or skiped
473 * rd_start and rd_end are set to ramdisk start/end addresses if
474 * ramdisk image is found and valid
475 *
476 * 1, if ramdisk image is found but corrupted, or invalid
477 * rd_start and rd_end are set to 0 if no ramdisk exists
478 */
479int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
480 u8 arch, ulong *rd_start, ulong *rd_end)
481{
482 ulong rd_data, rd_len;
483 const char *select = NULL;
484
485 *rd_start = 0;
486 *rd_end = 0;
487
488 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
489 char *buf;
490
491 /* Look for an Android boot image */
492 buf = map_sysmem(images->os.start, 0);
493 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
494 select = (argc == 0) ? env_get("loadaddr") : argv[0];
495 }
496
497 if (argc >= 2)
498 select = argv[1];
499
500 /*
501 * Look for a '-' which indicates to ignore the
502 * ramdisk argument
503 */
504 if (select && strcmp(select, "-") == 0) {
505 debug("## Skipping init Ramdisk\n");
506 rd_len = 0;
507 rd_data = 0;
508 } else if (select || genimg_has_config(images)) {
509 int ret;
510
511 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
512 if (ret == -ENOPKG)
513 return 0;
514 else if (ret)
515 return ret;
Simon Glass4d7237b2021-09-25 07:03:15 -0600516 } else if (images->legacy_hdr_valid &&
517 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass60d71542021-09-25 07:03:16 -0600518 IH_TYPE_MULTI)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600519 /*
520 * Now check if we have a legacy mult-component image,
521 * get second entry data start address and len.
522 */
523 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600524 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
525 (ulong)images->legacy_hdr_os);
Simon Glass4d7237b2021-09-25 07:03:15 -0600526
527 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
528 } else {
529 /*
530 * no initrd image
531 */
532 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600533 rd_len = 0;
534 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600535 }
536
537 if (!rd_data) {
538 debug("## No init Ramdisk\n");
539 } else {
540 *rd_start = rd_data;
541 *rd_end = rd_data + rd_len;
542 }
543 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600544 *rd_start, *rd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600545
546 return 0;
547}
548
Simon Glass4d7237b2021-09-25 07:03:15 -0600549/**
550 * boot_ramdisk_high - relocate init ramdisk
551 * @lmb: pointer to lmb handle, will be used for memory mgmt
552 * @rd_data: ramdisk data start address
553 * @rd_len: ramdisk data length
554 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
555 * start address (after possible relocation)
556 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
557 * end address (after possible relocation)
558 *
559 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
560 * variable and if requested ramdisk data is moved to a specified location.
561 *
562 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
563 * start/end addresses if ramdisk image start and len were provided,
564 * otherwise set initrd_start and initrd_end set to zeros.
565 *
566 * returns:
567 * 0 - success
568 * -1 - failure
569 */
570int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass60d71542021-09-25 07:03:16 -0600571 ulong *initrd_start, ulong *initrd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600572{
573 char *s;
574 ulong initrd_high;
575 int initrd_copy_to_ram = 1;
576
577 s = env_get("initrd_high");
578 if (s) {
579 /* a value of "no" or a similar string will act like 0,
580 * turning the "load high" feature off. This is intentional.
581 */
582 initrd_high = hextoul(s, NULL);
583 if (initrd_high == ~0)
584 initrd_copy_to_ram = 0;
585 } else {
586 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
587 }
588
Simon Glass4d7237b2021-09-25 07:03:15 -0600589 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass60d71542021-09-25 07:03:16 -0600590 initrd_high, initrd_copy_to_ram);
Simon Glass4d7237b2021-09-25 07:03:15 -0600591
592 if (rd_data) {
593 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
594 debug(" in-place initrd\n");
595 *initrd_start = rd_data;
596 *initrd_end = rd_data + rd_len;
597 lmb_reserve(lmb, rd_data, rd_len);
598 } else {
599 if (initrd_high)
600 *initrd_start = (ulong)lmb_alloc_base(lmb,
601 rd_len, 0x1000, initrd_high);
602 else
603 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
604 0x1000);
605
606 if (*initrd_start == 0) {
607 puts("ramdisk - allocation error\n");
608 goto error;
609 }
610 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
611
612 *initrd_end = *initrd_start + rd_len;
613 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass60d71542021-09-25 07:03:16 -0600614 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600615
616 memmove_wd((void *)*initrd_start,
Simon Glass60d71542021-09-25 07:03:16 -0600617 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass4d7237b2021-09-25 07:03:15 -0600618
Simon Glass4d7237b2021-09-25 07:03:15 -0600619 /*
620 * Ensure the image is flushed to memory to handle
621 * AMP boot scenarios in which we might not be
622 * HW cache coherent
623 */
Simon Glass0f781382021-09-25 19:43:35 -0600624 if (IS_ENABLED(CONFIG_MP)) {
625 flush_cache((unsigned long)*initrd_start,
626 ALIGN(rd_len, ARCH_DMA_MINALIGN));
627 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600628 puts("OK\n");
629 }
630 } else {
631 *initrd_start = 0;
632 *initrd_end = 0;
633 }
634 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600635 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600636
637 return 0;
638
639error:
640 return -1;
641}
Simon Glass4d7237b2021-09-25 07:03:15 -0600642
Simon Glass60d71542021-09-25 07:03:16 -0600643int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass4d7237b2021-09-25 07:03:15 -0600644 ulong *setup_start, ulong *setup_len)
645{
Simon Glass0f781382021-09-25 19:43:35 -0600646 if (!CONFIG_IS_ENABLED(FIT))
647 return -ENOENT;
648
Simon Glass4d7237b2021-09-25 07:03:15 -0600649 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600650}
651
Simon Glass4d7237b2021-09-25 07:03:15 -0600652int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600653 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600654{
655 ulong tmp_img_addr, img_data, img_len;
656 void *buf;
657 int conf_noffset;
658 int fit_img_result;
659 const char *uname, *name;
660 int err;
661 int devnum = 0; /* TODO support multi fpga platforms */
662
Simon Glass0f781382021-09-25 19:43:35 -0600663 if (!IS_ENABLED(CONFIG_FPGA))
664 return -ENOSYS;
665
Simon Glass4d7237b2021-09-25 07:03:15 -0600666 /* Check to see if the images struct has a FIT configuration */
667 if (!genimg_has_config(images)) {
668 debug("## FIT configuration was not specified\n");
669 return 0;
670 }
671
672 /*
673 * Obtain the os FIT header from the images struct
674 */
675 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
676 buf = map_sysmem(tmp_img_addr, 0);
677 /*
678 * Check image type. For FIT images get FIT node
679 * and attempt to locate a generic binary.
680 */
681 switch (genimg_get_format(buf)) {
682 case IMAGE_FORMAT_FIT:
683 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
684
685 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
686 NULL);
687 if (!uname) {
688 debug("## FPGA image is not specified\n");
689 return 0;
690 }
691 fit_img_result = fit_image_load(images,
692 tmp_img_addr,
693 (const char **)&uname,
Simon Glass60d71542021-09-25 07:03:16 -0600694 &images->fit_uname_cfg,
Simon Glass4d7237b2021-09-25 07:03:15 -0600695 arch,
696 IH_TYPE_FPGA,
697 BOOTSTAGE_ID_FPGA_INIT,
698 FIT_LOAD_OPTIONAL_NON_ZERO,
699 &img_data, &img_len);
700
701 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
702 uname, img_data, img_len);
703
704 if (fit_img_result < 0) {
705 /* Something went wrong! */
706 return fit_img_result;
707 }
708
709 if (!fpga_is_partial_data(devnum, img_len)) {
710 name = "full";
711 err = fpga_loadbitstream(devnum, (char *)img_data,
712 img_len, BIT_FULL);
713 if (err)
714 err = fpga_load(devnum, (const void *)img_data,
715 img_len, BIT_FULL);
716 } else {
717 name = "partial";
718 err = fpga_loadbitstream(devnum, (char *)img_data,
719 img_len, BIT_PARTIAL);
720 if (err)
721 err = fpga_load(devnum, (const void *)img_data,
722 img_len, BIT_PARTIAL);
723 }
724
725 if (err)
726 return err;
727
728 printf(" Programming %s bitstream... OK\n", name);
729 break;
730 default:
731 printf("The given image format is not supported (corrupt?)\n");
732 return 1;
733 }
734
735 return 0;
736}
Simon Glass4d7237b2021-09-25 07:03:15 -0600737
Simon Glass60d71542021-09-25 07:03:16 -0600738static void fit_loadable_process(u8 img_type,
Simon Glass4d7237b2021-09-25 07:03:15 -0600739 ulong img_data,
740 ulong img_len)
741{
742 int i;
743 const unsigned int count =
744 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
745 struct fit_loadable_tbl *fit_loadable_handler =
746 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
747 /* For each loadable handler */
748 for (i = 0; i < count; i++, fit_loadable_handler++)
749 /* matching this type */
750 if (fit_loadable_handler->type == img_type)
751 /* call that handler with this image data */
752 fit_loadable_handler->handler(img_data, img_len);
753}
754
755int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600756 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600757{
758 /*
759 * These variables are used to hold the current image location
760 * in system memory.
761 */
762 ulong tmp_img_addr;
763 /*
764 * These two variables are requirements for fit_image_load, but
765 * their values are not used
766 */
767 ulong img_data, img_len;
768 void *buf;
769 int loadables_index;
770 int conf_noffset;
771 int fit_img_result;
772 const char *uname;
Simon Glass60d71542021-09-25 07:03:16 -0600773 u8 img_type;
Simon Glass4d7237b2021-09-25 07:03:15 -0600774
775 /* Check to see if the images struct has a FIT configuration */
776 if (!genimg_has_config(images)) {
777 debug("## FIT configuration was not specified\n");
778 return 0;
779 }
780
781 /*
782 * Obtain the os FIT header from the images struct
783 */
784 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
785 buf = map_sysmem(tmp_img_addr, 0);
786 /*
787 * Check image type. For FIT images get FIT node
788 * and attempt to locate a generic binary.
789 */
790 switch (genimg_get_format(buf)) {
791 case IMAGE_FORMAT_FIT:
792 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
793
794 for (loadables_index = 0;
795 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass60d71542021-09-25 07:03:16 -0600796 FIT_LOADABLE_PROP,
797 loadables_index, NULL), uname;
798 loadables_index++) {
799 fit_img_result = fit_image_load(images, tmp_img_addr,
800 &uname,
801 &images->fit_uname_cfg,
802 arch, IH_TYPE_LOADABLE,
803 BOOTSTAGE_ID_FIT_LOADABLE_START,
804 FIT_LOAD_OPTIONAL_NON_ZERO,
805 &img_data, &img_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600806 if (fit_img_result < 0) {
807 /* Something went wrong! */
808 return fit_img_result;
809 }
810
811 fit_img_result = fit_image_get_node(buf, uname);
812 if (fit_img_result < 0) {
813 /* Something went wrong! */
814 return fit_img_result;
815 }
816 fit_img_result = fit_image_get_type(buf,
817 fit_img_result,
818 &img_type);
819 if (fit_img_result < 0) {
820 /* Something went wrong! */
821 return fit_img_result;
822 }
823
824 fit_loadable_process(img_type, img_data, img_len);
825 }
826 break;
827 default:
828 printf("The given image format is not supported (corrupt?)\n");
829 return 1;
830 }
831
832 return 0;
833}
Simon Glass4d7237b2021-09-25 07:03:15 -0600834
Simon Glass4d7237b2021-09-25 07:03:15 -0600835/**
836 * boot_get_cmdline - allocate and initialize kernel cmdline
837 * @lmb: pointer to lmb handle, will be used for memory mgmt
838 * @cmd_start: pointer to a ulong variable, will hold cmdline start
839 * @cmd_end: pointer to a ulong variable, will hold cmdline end
840 *
841 * boot_get_cmdline() allocates space for kernel command line below
842 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
843 * variable is present its contents is copied to allocated kernel
844 * command line.
845 *
846 * returns:
847 * 0 - success
848 * -1 - failure
849 */
850int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
851{
852 char *cmdline;
853 char *s;
854
855 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
856 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass60d71542021-09-25 07:03:16 -0600857 if (!cmdline)
Simon Glass4d7237b2021-09-25 07:03:15 -0600858 return -1;
859
860 s = env_get("bootargs");
861 if (!s)
862 s = "";
863
864 strcpy(cmdline, s);
865
Simon Glass60d71542021-09-25 07:03:16 -0600866 *cmd_start = (ulong)cmdline;
Simon Glass4d7237b2021-09-25 07:03:15 -0600867 *cmd_end = *cmd_start + strlen(cmdline);
868
869 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
870
871 return 0;
872}
Simon Glass4d7237b2021-09-25 07:03:15 -0600873
Simon Glass4d7237b2021-09-25 07:03:15 -0600874/**
875 * boot_get_kbd - allocate and initialize kernel copy of board info
876 * @lmb: pointer to lmb handle, will be used for memory mgmt
877 * @kbd: double pointer to board info data
878 *
879 * boot_get_kbd() allocates space for kernel copy of board info data below
880 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
881 * with the current u-boot board info data.
882 *
883 * returns:
884 * 0 - success
885 * -1 - failure
886 */
887int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
888{
889 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
890 sizeof(struct bd_info),
891 0xf,
Simon Glass60d71542021-09-25 07:03:16 -0600892 env_get_bootm_mapsize() +
893 env_get_bootm_low());
894 if (!*kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600895 return -1;
896
Simon Glass60d71542021-09-25 07:03:16 -0600897 **kbd = *gd->bd;
Simon Glass4d7237b2021-09-25 07:03:15 -0600898
899 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
900
Simon Glassbc0c4db2021-09-25 07:03:20 -0600901#if defined(DEBUG)
Leo Yu-Chi Liangde53e5e2021-10-27 17:00:41 +0800902 if (IS_ENABLED(CONFIG_CMD_BDI))
Simon Glassbc0c4db2021-09-25 07:03:20 -0600903 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass4d7237b2021-09-25 07:03:15 -0600904#endif
905
906 return 0;
907}
Simon Glass4d7237b2021-09-25 07:03:15 -0600908
Simon Glass4d7237b2021-09-25 07:03:15 -0600909int image_setup_linux(bootm_headers_t *images)
910{
911 ulong of_size = images->ft_len;
912 char **of_flat_tree = &images->ft_addr;
913 struct lmb *lmb = &images->lmb;
914 int ret;
915
Simon Glass85c057e2021-09-25 19:43:21 -0600916 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass4d7237b2021-09-25 07:03:15 -0600917 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
918
Simon Glassb8eb1fe2021-09-25 19:43:25 -0600919 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600920 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass60d71542021-09-25 07:03:16 -0600921 &images->cmdline_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600922 if (ret) {
923 puts("ERROR with allocation of cmdline\n");
924 return ret;
925 }
926 }
927
Simon Glass85c057e2021-09-25 19:43:21 -0600928 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600929 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
930 if (ret)
931 return ret;
932 }
933
Simon Glass85c057e2021-09-25 19:43:21 -0600934 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600935 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
936 if (ret)
937 return ret;
938 }
939
940 return 0;
941}
Simon Glass4a8a8a12021-09-25 07:03:17 -0600942
943void genimg_print_size(uint32_t size)
944{
945 printf("%d Bytes = ", size);
946 print_size(size, "\n");
947}
948
949void genimg_print_time(time_t timestamp)
950{
951 struct rtc_time tm;
952
953 rtc_to_tm(timestamp, &tm);
954 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
955 tm.tm_year, tm.tm_mon, tm.tm_mday,
956 tm.tm_hour, tm.tm_min, tm.tm_sec);
957}