blob: cb2479f2f30dbfbc6c9f0abe2c47113d0ee036df [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>
17#include <mapmem.h>
Simon Glass4a8a8a12021-09-25 07:03:17 -060018#include <rtc.h>
Simon Glass4d7237b2021-09-25 07:03:15 -060019#include <watchdog.h>
20#include <asm/cache.h>
21#include <asm/global_data.h>
22
23#ifndef CONFIG_SYS_BARGSIZE
24#define CONFIG_SYS_BARGSIZE 512
25#endif
26
27DECLARE_GLOBAL_DATA_PTR;
28
29#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
30/**
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}
88#endif
89
90/*****************************************************************************/
91/* Shared dual-format routines */
92/*****************************************************************************/
93ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
94ulong image_save_addr; /* Default Save Address */
95ulong image_save_size; /* Default Save Size (in bytes) */
96
97static int on_loadaddr(const char *name, const char *value, enum env_op op,
Simon Glass60d71542021-09-25 07:03:16 -060098 int flags)
Simon Glass4d7237b2021-09-25 07:03:15 -060099{
100 switch (op) {
101 case env_op_create:
102 case env_op_overwrite:
103 image_load_addr = hextoul(value, NULL);
104 break;
105 default:
106 break;
107 }
108
109 return 0;
110}
111U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
112
113ulong env_get_bootm_low(void)
114{
115 char *s = env_get("bootm_low");
Simon Glass60d71542021-09-25 07:03:16 -0600116
Simon Glass4d7237b2021-09-25 07:03:15 -0600117 if (s) {
118 ulong tmp = hextoul(s, NULL);
119 return tmp;
120 }
121
122#if defined(CONFIG_SYS_SDRAM_BASE)
123 return CONFIG_SYS_SDRAM_BASE;
124#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
125 return gd->bd->bi_dram[0].start;
126#else
127 return 0;
128#endif
129}
130
131phys_size_t env_get_bootm_size(void)
132{
133 phys_size_t tmp, size;
134 phys_addr_t start;
135 char *s = env_get("bootm_size");
Simon Glass60d71542021-09-25 07:03:16 -0600136
Simon Glass4d7237b2021-09-25 07:03:15 -0600137 if (s) {
138 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
139 return tmp;
140 }
141
142 start = gd->ram_base;
143 size = gd->ram_size;
144
145 if (start + size > gd->ram_top)
146 size = gd->ram_top - start;
147
148 s = env_get("bootm_low");
149 if (s)
150 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
151 else
152 tmp = start;
153
154 return size - (tmp - start);
155}
156
157phys_size_t env_get_bootm_mapsize(void)
158{
159 phys_size_t tmp;
160 char *s = env_get("bootm_mapsize");
Simon Glass60d71542021-09-25 07:03:16 -0600161
Simon Glass4d7237b2021-09-25 07:03:15 -0600162 if (s) {
163 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
164 return tmp;
165 }
166
167#if defined(CONFIG_SYS_BOOTMAPSZ)
168 return CONFIG_SYS_BOOTMAPSZ;
169#else
170 return env_get_bootm_size();
171#endif
172}
173
174void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
175{
176 if (to == from)
177 return;
178
179#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
180 if (to > from) {
181 from += len;
182 to += len;
183 }
184 while (len > 0) {
185 size_t tail = (len > chunksz) ? chunksz : len;
Simon Glass60d71542021-09-25 07:03:16 -0600186
Simon Glass4d7237b2021-09-25 07:03:15 -0600187 WATCHDOG_RESET();
188 if (to > from) {
189 to -= tail;
190 from -= tail;
191 }
192 memmove(to, from, tail);
193 if (to < from) {
194 to += tail;
195 from += tail;
196 }
197 len -= tail;
198 }
199#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
200 memmove(to, from, len);
201#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
202}
203
204/**
205 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
206 * FIT strings
207 * @img_addr: a string might contain real image address
208 * @fit_uname_config: double pointer to a char, will hold pointer to a
209 * configuration unit name
210 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
211 * name
212 *
213 * genimg_get_kernel_addr_fit get the real kernel start address from a string
214 * which is normally the first argv of bootm/bootz
215 *
216 * returns:
217 * kernel start address
218 */
219ulong genimg_get_kernel_addr_fit(char * const img_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600220 const char **fit_uname_config,
221 const char **fit_uname_kernel)
Simon Glass4d7237b2021-09-25 07:03:15 -0600222{
223 ulong kernel_addr;
224
225 /* find out kernel image address */
226 if (!img_addr) {
227 kernel_addr = image_load_addr;
228 debug("* kernel: default image load address = 0x%08lx\n",
229 image_load_addr);
230#if CONFIG_IS_ENABLED(FIT)
231 } else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
232 fit_uname_config)) {
233 debug("* kernel: config '%s' from image at 0x%08lx\n",
234 *fit_uname_config, kernel_addr);
235 } else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
236 fit_uname_kernel)) {
237 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
238 *fit_uname_kernel, kernel_addr);
239#endif
240 } else {
241 kernel_addr = hextoul(img_addr, NULL);
242 debug("* kernel: cmdline image address = 0x%08lx\n",
243 kernel_addr);
244 }
245
246 return kernel_addr;
247}
248
249/**
250 * genimg_get_kernel_addr() is the simple version of
251 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
252 */
253ulong genimg_get_kernel_addr(char * const img_addr)
254{
255 const char *fit_uname_config = NULL;
256 const char *fit_uname_kernel = NULL;
257
258 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
259 &fit_uname_kernel);
260}
261
262/**
263 * genimg_get_format - get image format type
264 * @img_addr: image start address
265 *
266 * genimg_get_format() checks whether provided address points to a valid
267 * legacy or FIT image.
268 *
269 * New uImage format and FDT blob are based on a libfdt. FDT blob
270 * may be passed directly or embedded in a FIT image. In both situations
271 * genimg_get_format() must be able to dectect libfdt header.
272 *
273 * returns:
274 * image format type or IMAGE_FORMAT_INVALID if no image is present
275 */
276int genimg_get_format(const void *img_addr)
277{
278#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
279 const image_header_t *hdr;
280
281 hdr = (const image_header_t *)img_addr;
282 if (image_check_magic(hdr))
283 return IMAGE_FORMAT_LEGACY;
284#endif
Simon Glass85c057e2021-09-25 19:43:21 -0600285#if CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600286 if (fdt_check_header(img_addr) == 0)
287 return IMAGE_FORMAT_FIT;
288#endif
289#ifdef CONFIG_ANDROID_BOOT_IMAGE
290 if (android_image_check_header(img_addr) == 0)
291 return IMAGE_FORMAT_ANDROID;
292#endif
293
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 Glasse719d3b2021-09-25 19:43:20 -0600310#if CONFIG_IS_ENABLED(FIT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600311 if (images->fit_uname_cfg)
312 return 1;
313#endif
314 return 0;
315}
316
317/**
318 * boot_get_ramdisk - main ramdisk handling routine
319 * @argc: command argument count
320 * @argv: command argument list
321 * @images: pointer to the bootm images structure
322 * @arch: expected ramdisk architecture
323 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
324 * @rd_end: pointer to a ulong variable, will hold ramdisk end
325 *
326 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Simon Glass60d71542021-09-25 07:03:16 -0600327 * Currently supported are the following ramdisk sources:
Simon Glass4d7237b2021-09-25 07:03:15 -0600328 * - multicomponent kernel/ramdisk image,
329 * - commandline provided address of decicated ramdisk image.
330 *
331 * returns:
332 * 0, if ramdisk image was found and valid, or skiped
333 * rd_start and rd_end are set to ramdisk start/end addresses if
334 * ramdisk image is found and valid
335 *
336 * 1, if ramdisk image is found but corrupted, or invalid
337 * rd_start and rd_end are set to 0 if no ramdisk exists
338 */
339int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600340 u8 arch, ulong *rd_start, ulong *rd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600341{
342 ulong rd_addr, rd_load;
343 ulong rd_data, rd_len;
344#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
345 const image_header_t *rd_hdr;
346#endif
347 void *buf;
348#ifdef CONFIG_SUPPORT_RAW_INITRD
349 char *end;
350#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600351#if CONFIG_IS_ENABLED(FIT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600352 const char *fit_uname_config = images->fit_uname_cfg;
353 const char *fit_uname_ramdisk = NULL;
354 ulong default_addr;
355 int rd_noffset;
356#endif
357 const char *select = NULL;
358
359 *rd_start = 0;
360 *rd_end = 0;
361
362#ifdef CONFIG_ANDROID_BOOT_IMAGE
363 /*
364 * Look for an Android boot image.
365 */
366 buf = map_sysmem(images->os.start, 0);
367 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
368 select = (argc == 0) ? env_get("loadaddr") : argv[0];
369#endif
370
371 if (argc >= 2)
372 select = argv[1];
373
374 /*
375 * Look for a '-' which indicates to ignore the
376 * ramdisk argument
377 */
378 if (select && strcmp(select, "-") == 0) {
379 debug("## Skipping init Ramdisk\n");
Simon Glass60d71542021-09-25 07:03:16 -0600380 rd_len = 0;
381 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600382 } else if (select || genimg_has_config(images)) {
Simon Glasse719d3b2021-09-25 19:43:20 -0600383#if CONFIG_IS_ENABLED(FIT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600384 if (select) {
385 /*
386 * If the init ramdisk comes from the FIT image and
387 * the FIT image address is omitted in the command
388 * line argument, try to use os FIT image address or
389 * default load address.
390 */
391 if (images->fit_uname_os)
392 default_addr = (ulong)images->fit_hdr_os;
393 else
394 default_addr = image_load_addr;
395
396 if (fit_parse_conf(select, default_addr,
397 &rd_addr, &fit_uname_config)) {
Simon Glass60d71542021-09-25 07:03:16 -0600398 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
399 fit_uname_config, rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600400 } else if (fit_parse_subimage(select, default_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600401 &rd_addr,
402 &fit_uname_ramdisk)) {
403 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
404 fit_uname_ramdisk, rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600405 } else
406#endif
407 {
408 rd_addr = hextoul(select, NULL);
Simon Glass60d71542021-09-25 07:03:16 -0600409 debug("* ramdisk: cmdline image address = 0x%08lx\n",
410 rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600411 }
Simon Glasse719d3b2021-09-25 19:43:20 -0600412#if CONFIG_IS_ENABLED(FIT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600413 } else {
414 /* use FIT configuration provided in first bootm
415 * command argument. If the property is not defined,
416 * quit silently.
417 */
418 rd_addr = map_to_sysmem(images->fit_hdr_os);
419 rd_noffset = fit_get_node_from_config(images,
Simon Glass60d71542021-09-25 07:03:16 -0600420 FIT_RAMDISK_PROP,
421 rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600422 if (rd_noffset == -ENOENT)
423 return 0;
424 else if (rd_noffset < 0)
425 return 1;
426 }
427#endif
428
429 /*
430 * Check if there is an initrd image at the
431 * address provided in the second bootm argument
432 * check image type, for FIT images get FIT node.
433 */
434 buf = map_sysmem(rd_addr, 0);
435 switch (genimg_get_format(buf)) {
436#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
437 case IMAGE_FORMAT_LEGACY:
Simon Glass60d71542021-09-25 07:03:16 -0600438 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
439 rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600440
441 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
442 rd_hdr = image_get_ramdisk(rd_addr, arch,
Simon Glass60d71542021-09-25 07:03:16 -0600443 images->verify);
Simon Glass4d7237b2021-09-25 07:03:15 -0600444
Simon Glass60d71542021-09-25 07:03:16 -0600445 if (!rd_hdr)
Simon Glass4d7237b2021-09-25 07:03:15 -0600446 return 1;
447
448 rd_data = image_get_data(rd_hdr);
449 rd_len = image_get_data_size(rd_hdr);
450 rd_load = image_get_load(rd_hdr);
451 break;
452#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600453#if CONFIG_IS_ENABLED(FIT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600454 case IMAGE_FORMAT_FIT:
455 rd_noffset = fit_image_load(images,
Simon Glass60d71542021-09-25 07:03:16 -0600456 rd_addr, &fit_uname_ramdisk,
457 &fit_uname_config, arch,
458 IH_TYPE_RAMDISK,
459 BOOTSTAGE_ID_FIT_RD_START,
460 FIT_LOAD_OPTIONAL_NON_ZERO,
461 &rd_data, &rd_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600462 if (rd_noffset < 0)
463 return 1;
464
465 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
466 images->fit_uname_rd = fit_uname_ramdisk;
467 images->fit_noffset_rd = rd_noffset;
468 break;
469#endif
470#ifdef CONFIG_ANDROID_BOOT_IMAGE
471 case IMAGE_FORMAT_ANDROID:
472 android_image_get_ramdisk((void *)images->os.start,
Simon Glass60d71542021-09-25 07:03:16 -0600473 &rd_data, &rd_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600474 break;
475#endif
476 default:
477#ifdef CONFIG_SUPPORT_RAW_INITRD
478 end = NULL;
479 if (select)
480 end = strchr(select, ':');
481 if (end) {
482 rd_len = hextoul(++end, NULL);
483 rd_data = rd_addr;
484 } else
485#endif
486 {
487 puts("Wrong Ramdisk Image Format\n");
Simon Glass60d71542021-09-25 07:03:16 -0600488 rd_data = 0;
489 rd_len = 0;
490 rd_load = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600491 return 1;
492 }
493 }
494 } else if (images->legacy_hdr_valid &&
495 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass60d71542021-09-25 07:03:16 -0600496 IH_TYPE_MULTI)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600497 /*
498 * Now check if we have a legacy mult-component image,
499 * get second entry data start address and len.
500 */
501 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600502 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
503 (ulong)images->legacy_hdr_os);
Simon Glass4d7237b2021-09-25 07:03:15 -0600504
505 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
506 } else {
507 /*
508 * no initrd image
509 */
510 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600511 rd_len = 0;
512 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600513 }
514
515 if (!rd_data) {
516 debug("## No init Ramdisk\n");
517 } else {
518 *rd_start = rd_data;
519 *rd_end = rd_data + rd_len;
520 }
521 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600522 *rd_start, *rd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600523
524 return 0;
525}
526
527#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
528/**
529 * boot_ramdisk_high - relocate init ramdisk
530 * @lmb: pointer to lmb handle, will be used for memory mgmt
531 * @rd_data: ramdisk data start address
532 * @rd_len: ramdisk data length
533 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
534 * start address (after possible relocation)
535 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
536 * end address (after possible relocation)
537 *
538 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
539 * variable and if requested ramdisk data is moved to a specified location.
540 *
541 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
542 * start/end addresses if ramdisk image start and len were provided,
543 * otherwise set initrd_start and initrd_end set to zeros.
544 *
545 * returns:
546 * 0 - success
547 * -1 - failure
548 */
549int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass60d71542021-09-25 07:03:16 -0600550 ulong *initrd_start, ulong *initrd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600551{
552 char *s;
553 ulong initrd_high;
554 int initrd_copy_to_ram = 1;
555
556 s = env_get("initrd_high");
557 if (s) {
558 /* a value of "no" or a similar string will act like 0,
559 * turning the "load high" feature off. This is intentional.
560 */
561 initrd_high = hextoul(s, NULL);
562 if (initrd_high == ~0)
563 initrd_copy_to_ram = 0;
564 } else {
565 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
566 }
567
Simon Glass4d7237b2021-09-25 07:03:15 -0600568 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass60d71542021-09-25 07:03:16 -0600569 initrd_high, initrd_copy_to_ram);
Simon Glass4d7237b2021-09-25 07:03:15 -0600570
571 if (rd_data) {
572 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
573 debug(" in-place initrd\n");
574 *initrd_start = rd_data;
575 *initrd_end = rd_data + rd_len;
576 lmb_reserve(lmb, rd_data, rd_len);
577 } else {
578 if (initrd_high)
579 *initrd_start = (ulong)lmb_alloc_base(lmb,
580 rd_len, 0x1000, initrd_high);
581 else
582 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
583 0x1000);
584
585 if (*initrd_start == 0) {
586 puts("ramdisk - allocation error\n");
587 goto error;
588 }
589 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
590
591 *initrd_end = *initrd_start + rd_len;
592 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass60d71542021-09-25 07:03:16 -0600593 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600594
595 memmove_wd((void *)*initrd_start,
Simon Glass60d71542021-09-25 07:03:16 -0600596 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass4d7237b2021-09-25 07:03:15 -0600597
598#ifdef CONFIG_MP
599 /*
600 * Ensure the image is flushed to memory to handle
601 * AMP boot scenarios in which we might not be
602 * HW cache coherent
603 */
604 flush_cache((unsigned long)*initrd_start,
605 ALIGN(rd_len, ARCH_DMA_MINALIGN));
606#endif
607 puts("OK\n");
608 }
609 } else {
610 *initrd_start = 0;
611 *initrd_end = 0;
612 }
613 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600614 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600615
616 return 0;
617
618error:
619 return -1;
620}
621#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
622
Simon Glass60d71542021-09-25 07:03:16 -0600623int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass4d7237b2021-09-25 07:03:15 -0600624 ulong *setup_start, ulong *setup_len)
625{
Simon Glasse719d3b2021-09-25 19:43:20 -0600626#if CONFIG_IS_ENABLED(FIT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600627 return boot_get_setup_fit(images, arch, setup_start, setup_len);
628#else
629 return -ENOENT;
630#endif
631}
632
Simon Glasse719d3b2021-09-25 19:43:20 -0600633#if CONFIG_IS_ENABLED(FIT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600634#if defined(CONFIG_FPGA)
635int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600636 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600637{
638 ulong tmp_img_addr, img_data, img_len;
639 void *buf;
640 int conf_noffset;
641 int fit_img_result;
642 const char *uname, *name;
643 int err;
644 int devnum = 0; /* TODO support multi fpga platforms */
645
646 /* Check to see if the images struct has a FIT configuration */
647 if (!genimg_has_config(images)) {
648 debug("## FIT configuration was not specified\n");
649 return 0;
650 }
651
652 /*
653 * Obtain the os FIT header from the images struct
654 */
655 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
656 buf = map_sysmem(tmp_img_addr, 0);
657 /*
658 * Check image type. For FIT images get FIT node
659 * and attempt to locate a generic binary.
660 */
661 switch (genimg_get_format(buf)) {
662 case IMAGE_FORMAT_FIT:
663 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
664
665 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
666 NULL);
667 if (!uname) {
668 debug("## FPGA image is not specified\n");
669 return 0;
670 }
671 fit_img_result = fit_image_load(images,
672 tmp_img_addr,
673 (const char **)&uname,
Simon Glass60d71542021-09-25 07:03:16 -0600674 &images->fit_uname_cfg,
Simon Glass4d7237b2021-09-25 07:03:15 -0600675 arch,
676 IH_TYPE_FPGA,
677 BOOTSTAGE_ID_FPGA_INIT,
678 FIT_LOAD_OPTIONAL_NON_ZERO,
679 &img_data, &img_len);
680
681 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
682 uname, img_data, img_len);
683
684 if (fit_img_result < 0) {
685 /* Something went wrong! */
686 return fit_img_result;
687 }
688
689 if (!fpga_is_partial_data(devnum, img_len)) {
690 name = "full";
691 err = fpga_loadbitstream(devnum, (char *)img_data,
692 img_len, BIT_FULL);
693 if (err)
694 err = fpga_load(devnum, (const void *)img_data,
695 img_len, BIT_FULL);
696 } else {
697 name = "partial";
698 err = fpga_loadbitstream(devnum, (char *)img_data,
699 img_len, BIT_PARTIAL);
700 if (err)
701 err = fpga_load(devnum, (const void *)img_data,
702 img_len, BIT_PARTIAL);
703 }
704
705 if (err)
706 return err;
707
708 printf(" Programming %s bitstream... OK\n", name);
709 break;
710 default:
711 printf("The given image format is not supported (corrupt?)\n");
712 return 1;
713 }
714
715 return 0;
716}
717#endif
718
Simon Glass60d71542021-09-25 07:03:16 -0600719static void fit_loadable_process(u8 img_type,
Simon Glass4d7237b2021-09-25 07:03:15 -0600720 ulong img_data,
721 ulong img_len)
722{
723 int i;
724 const unsigned int count =
725 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
726 struct fit_loadable_tbl *fit_loadable_handler =
727 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
728 /* For each loadable handler */
729 for (i = 0; i < count; i++, fit_loadable_handler++)
730 /* matching this type */
731 if (fit_loadable_handler->type == img_type)
732 /* call that handler with this image data */
733 fit_loadable_handler->handler(img_data, img_len);
734}
735
736int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600737 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600738{
739 /*
740 * These variables are used to hold the current image location
741 * in system memory.
742 */
743 ulong tmp_img_addr;
744 /*
745 * These two variables are requirements for fit_image_load, but
746 * their values are not used
747 */
748 ulong img_data, img_len;
749 void *buf;
750 int loadables_index;
751 int conf_noffset;
752 int fit_img_result;
753 const char *uname;
Simon Glass60d71542021-09-25 07:03:16 -0600754 u8 img_type;
Simon Glass4d7237b2021-09-25 07:03:15 -0600755
756 /* Check to see if the images struct has a FIT configuration */
757 if (!genimg_has_config(images)) {
758 debug("## FIT configuration was not specified\n");
759 return 0;
760 }
761
762 /*
763 * Obtain the os FIT header from the images struct
764 */
765 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
766 buf = map_sysmem(tmp_img_addr, 0);
767 /*
768 * Check image type. For FIT images get FIT node
769 * and attempt to locate a generic binary.
770 */
771 switch (genimg_get_format(buf)) {
772 case IMAGE_FORMAT_FIT:
773 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
774
775 for (loadables_index = 0;
776 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass60d71542021-09-25 07:03:16 -0600777 FIT_LOADABLE_PROP,
778 loadables_index, NULL), uname;
779 loadables_index++) {
780 fit_img_result = fit_image_load(images, tmp_img_addr,
781 &uname,
782 &images->fit_uname_cfg,
783 arch, IH_TYPE_LOADABLE,
784 BOOTSTAGE_ID_FIT_LOADABLE_START,
785 FIT_LOAD_OPTIONAL_NON_ZERO,
786 &img_data, &img_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600787 if (fit_img_result < 0) {
788 /* Something went wrong! */
789 return fit_img_result;
790 }
791
792 fit_img_result = fit_image_get_node(buf, uname);
793 if (fit_img_result < 0) {
794 /* Something went wrong! */
795 return fit_img_result;
796 }
797 fit_img_result = fit_image_get_type(buf,
798 fit_img_result,
799 &img_type);
800 if (fit_img_result < 0) {
801 /* Something went wrong! */
802 return fit_img_result;
803 }
804
805 fit_loadable_process(img_type, img_data, img_len);
806 }
807 break;
808 default:
809 printf("The given image format is not supported (corrupt?)\n");
810 return 1;
811 }
812
813 return 0;
814}
815#endif
816
Simon Glass4d7237b2021-09-25 07:03:15 -0600817/**
818 * boot_get_cmdline - allocate and initialize kernel cmdline
819 * @lmb: pointer to lmb handle, will be used for memory mgmt
820 * @cmd_start: pointer to a ulong variable, will hold cmdline start
821 * @cmd_end: pointer to a ulong variable, will hold cmdline end
822 *
823 * boot_get_cmdline() allocates space for kernel command line below
824 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
825 * variable is present its contents is copied to allocated kernel
826 * command line.
827 *
828 * returns:
829 * 0 - success
830 * -1 - failure
831 */
832int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
833{
834 char *cmdline;
835 char *s;
836
837 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
838 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass60d71542021-09-25 07:03:16 -0600839 if (!cmdline)
Simon Glass4d7237b2021-09-25 07:03:15 -0600840 return -1;
841
842 s = env_get("bootargs");
843 if (!s)
844 s = "";
845
846 strcpy(cmdline, s);
847
Simon Glass60d71542021-09-25 07:03:16 -0600848 *cmd_start = (ulong)cmdline;
Simon Glass4d7237b2021-09-25 07:03:15 -0600849 *cmd_end = *cmd_start + strlen(cmdline);
850
851 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
852
853 return 0;
854}
Simon Glass4d7237b2021-09-25 07:03:15 -0600855
Simon Glass4d7237b2021-09-25 07:03:15 -0600856/**
857 * boot_get_kbd - allocate and initialize kernel copy of board info
858 * @lmb: pointer to lmb handle, will be used for memory mgmt
859 * @kbd: double pointer to board info data
860 *
861 * boot_get_kbd() allocates space for kernel copy of board info data below
862 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
863 * with the current u-boot board info data.
864 *
865 * returns:
866 * 0 - success
867 * -1 - failure
868 */
869int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
870{
871 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
872 sizeof(struct bd_info),
873 0xf,
Simon Glass60d71542021-09-25 07:03:16 -0600874 env_get_bootm_mapsize() +
875 env_get_bootm_low());
876 if (!*kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600877 return -1;
878
Simon Glass60d71542021-09-25 07:03:16 -0600879 **kbd = *gd->bd;
Simon Glass4d7237b2021-09-25 07:03:15 -0600880
881 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
882
Simon Glassbc0c4db2021-09-25 07:03:20 -0600883#if defined(DEBUG)
884 if (IS_ENABLED(CONFIG_CMD_BDI)
885 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass4d7237b2021-09-25 07:03:15 -0600886#endif
887
888 return 0;
889}
Simon Glass4d7237b2021-09-25 07:03:15 -0600890
Simon Glass4d7237b2021-09-25 07:03:15 -0600891int image_setup_linux(bootm_headers_t *images)
892{
893 ulong of_size = images->ft_len;
894 char **of_flat_tree = &images->ft_addr;
895 struct lmb *lmb = &images->lmb;
896 int ret;
897
Simon Glass85c057e2021-09-25 19:43:21 -0600898 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass4d7237b2021-09-25 07:03:15 -0600899 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
900
901 if (IMAGE_BOOT_GET_CMDLINE) {
902 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass60d71542021-09-25 07:03:16 -0600903 &images->cmdline_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600904 if (ret) {
905 puts("ERROR with allocation of cmdline\n");
906 return ret;
907 }
908 }
909
Simon Glass85c057e2021-09-25 19:43:21 -0600910 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600911 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
912 if (ret)
913 return ret;
914 }
915
Simon Glass85c057e2021-09-25 19:43:21 -0600916 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600917 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
918 if (ret)
919 return ret;
920 }
921
922 return 0;
923}
Simon Glass4a8a8a12021-09-25 07:03:17 -0600924
925void genimg_print_size(uint32_t size)
926{
927 printf("%d Bytes = ", size);
928 print_size(size, "\n");
929}
930
931void genimg_print_time(time_t timestamp)
932{
933 struct rtc_time tm;
934
935 rtc_to_tm(timestamp, &tm);
936 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
937 tm.tm_year, tm.tm_mon, tm.tm_mday,
938 tm.tm_hour, tm.tm_min, tm.tm_sec);
939}