blob: 835751e07c720ebc9c80b7048c0e4937bf3e036d [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>
17#include <mapmem.h>
18#include <watchdog.h>
19#include <asm/cache.h>
20#include <asm/global_data.h>
21
22#ifndef CONFIG_SYS_BARGSIZE
23#define CONFIG_SYS_BARGSIZE 512
24#endif
25
26DECLARE_GLOBAL_DATA_PTR;
27
28#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
29/**
30 * image_get_ramdisk - get and verify ramdisk image
31 * @rd_addr: ramdisk image start address
32 * @arch: expected ramdisk architecture
33 * @verify: checksum verification flag
34 *
35 * image_get_ramdisk() returns a pointer to the verified ramdisk image
36 * header. Routine receives image start address and expected architecture
37 * flag. Verification done covers data and header integrity and os/type/arch
38 * fields checking.
39 *
40 * returns:
41 * pointer to a ramdisk image header, if image was found and valid
42 * otherwise, return NULL
43 */
Simon Glass3d2a47f2021-09-25 07:03:16 -060044static const image_header_t *image_get_ramdisk(ulong rd_addr, u8 arch,
45 int verify)
Simon Glass41506ff2021-09-25 07:03:15 -060046{
47 const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
48
49 if (!image_check_magic(rd_hdr)) {
50 puts("Bad Magic Number\n");
51 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
52 return NULL;
53 }
54
55 if (!image_check_hcrc(rd_hdr)) {
56 puts("Bad Header Checksum\n");
57 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
58 return NULL;
59 }
60
61 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
62 image_print_contents(rd_hdr);
63
64 if (verify) {
65 puts(" Verifying Checksum ... ");
66 if (!image_check_dcrc(rd_hdr)) {
67 puts("Bad Data CRC\n");
68 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
69 return NULL;
70 }
71 puts("OK\n");
72 }
73
74 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
75
76 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
77 !image_check_arch(rd_hdr, arch) ||
78 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
79 printf("No Linux %s Ramdisk Image\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -060080 genimg_get_arch_name(arch));
Simon Glass41506ff2021-09-25 07:03:15 -060081 bootstage_error(BOOTSTAGE_ID_RAMDISK);
82 return NULL;
83 }
84
85 return rd_hdr;
86}
87#endif
88
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 Glass3d2a47f2021-09-25 07:03:16 -060097 int flags)
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -0600115
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -0600135
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -0600160
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -0600185
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -0600219 const char **fit_uname_config,
220 const char **fit_uname_kernel)
Simon Glass41506ff2021-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);
229#if CONFIG_IS_ENABLED(FIT)
230 } else if (fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
231 fit_uname_config)) {
232 debug("* kernel: config '%s' from image at 0x%08lx\n",
233 *fit_uname_config, kernel_addr);
234 } else if (fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
235 fit_uname_kernel)) {
236 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
237 *fit_uname_kernel, kernel_addr);
238#endif
239 } 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{
277#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
278 const image_header_t *hdr;
279
280 hdr = (const image_header_t *)img_addr;
281 if (image_check_magic(hdr))
282 return IMAGE_FORMAT_LEGACY;
283#endif
284#if IMAGE_ENABLE_FIT || IMAGE_ENABLE_OF_LIBFDT
285 if (fdt_check_header(img_addr) == 0)
286 return IMAGE_FORMAT_FIT;
287#endif
288#ifdef CONFIG_ANDROID_BOOT_IMAGE
289 if (android_image_check_header(img_addr) == 0)
290 return IMAGE_FORMAT_ANDROID;
291#endif
292
293 return IMAGE_FORMAT_INVALID;
294}
295
296/**
297 * fit_has_config - check if there is a valid FIT configuration
298 * @images: pointer to the bootm command headers structure
299 *
300 * fit_has_config() checks if there is a FIT configuration in use
301 * (if FTI support is present).
302 *
303 * returns:
304 * 0, no FIT support or no configuration found
305 * 1, configuration found
306 */
307int genimg_has_config(bootm_headers_t *images)
308{
309#if IMAGE_ENABLE_FIT
310 if (images->fit_uname_cfg)
311 return 1;
312#endif
313 return 0;
314}
315
316/**
317 * boot_get_ramdisk - main ramdisk handling routine
318 * @argc: command argument count
319 * @argv: command argument list
320 * @images: pointer to the bootm images structure
321 * @arch: expected ramdisk architecture
322 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
323 * @rd_end: pointer to a ulong variable, will hold ramdisk end
324 *
325 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Simon Glass3d2a47f2021-09-25 07:03:16 -0600326 * Currently supported are the following ramdisk sources:
Simon Glass41506ff2021-09-25 07:03:15 -0600327 * - multicomponent kernel/ramdisk image,
328 * - commandline provided address of decicated ramdisk image.
329 *
330 * returns:
331 * 0, if ramdisk image was found and valid, or skiped
332 * rd_start and rd_end are set to ramdisk start/end addresses if
333 * ramdisk image is found and valid
334 *
335 * 1, if ramdisk image is found but corrupted, or invalid
336 * rd_start and rd_end are set to 0 if no ramdisk exists
337 */
338int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600339 u8 arch, ulong *rd_start, ulong *rd_end)
Simon Glass41506ff2021-09-25 07:03:15 -0600340{
341 ulong rd_addr, rd_load;
342 ulong rd_data, rd_len;
343#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
344 const image_header_t *rd_hdr;
345#endif
346 void *buf;
347#ifdef CONFIG_SUPPORT_RAW_INITRD
348 char *end;
349#endif
350#if IMAGE_ENABLE_FIT
351 const char *fit_uname_config = images->fit_uname_cfg;
352 const char *fit_uname_ramdisk = NULL;
353 ulong default_addr;
354 int rd_noffset;
355#endif
356 const char *select = NULL;
357
358 *rd_start = 0;
359 *rd_end = 0;
360
361#ifdef CONFIG_ANDROID_BOOT_IMAGE
362 /*
363 * Look for an Android boot image.
364 */
365 buf = map_sysmem(images->os.start, 0);
366 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
367 select = (argc == 0) ? env_get("loadaddr") : argv[0];
368#endif
369
370 if (argc >= 2)
371 select = argv[1];
372
373 /*
374 * Look for a '-' which indicates to ignore the
375 * ramdisk argument
376 */
377 if (select && strcmp(select, "-") == 0) {
378 debug("## Skipping init Ramdisk\n");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600379 rd_len = 0;
380 rd_data = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600381 } else if (select || genimg_has_config(images)) {
382#if IMAGE_ENABLE_FIT
383 if (select) {
384 /*
385 * If the init ramdisk comes from the FIT image and
386 * the FIT image address is omitted in the command
387 * line argument, try to use os FIT image address or
388 * default load address.
389 */
390 if (images->fit_uname_os)
391 default_addr = (ulong)images->fit_hdr_os;
392 else
393 default_addr = image_load_addr;
394
395 if (fit_parse_conf(select, default_addr,
396 &rd_addr, &fit_uname_config)) {
Simon Glass3d2a47f2021-09-25 07:03:16 -0600397 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
398 fit_uname_config, rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600399 } else if (fit_parse_subimage(select, default_addr,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600400 &rd_addr,
401 &fit_uname_ramdisk)) {
402 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
403 fit_uname_ramdisk, rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600404 } else
405#endif
406 {
407 rd_addr = hextoul(select, NULL);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600408 debug("* ramdisk: cmdline image address = 0x%08lx\n",
409 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600410 }
411#if IMAGE_ENABLE_FIT
412 } else {
413 /* use FIT configuration provided in first bootm
414 * command argument. If the property is not defined,
415 * quit silently.
416 */
417 rd_addr = map_to_sysmem(images->fit_hdr_os);
418 rd_noffset = fit_get_node_from_config(images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600419 FIT_RAMDISK_PROP,
420 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600421 if (rd_noffset == -ENOENT)
422 return 0;
423 else if (rd_noffset < 0)
424 return 1;
425 }
426#endif
427
428 /*
429 * Check if there is an initrd image at the
430 * address provided in the second bootm argument
431 * check image type, for FIT images get FIT node.
432 */
433 buf = map_sysmem(rd_addr, 0);
434 switch (genimg_get_format(buf)) {
435#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
436 case IMAGE_FORMAT_LEGACY:
Simon Glass3d2a47f2021-09-25 07:03:16 -0600437 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
438 rd_addr);
Simon Glass41506ff2021-09-25 07:03:15 -0600439
440 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
441 rd_hdr = image_get_ramdisk(rd_addr, arch,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600442 images->verify);
Simon Glass41506ff2021-09-25 07:03:15 -0600443
Simon Glass3d2a47f2021-09-25 07:03:16 -0600444 if (!rd_hdr)
Simon Glass41506ff2021-09-25 07:03:15 -0600445 return 1;
446
447 rd_data = image_get_data(rd_hdr);
448 rd_len = image_get_data_size(rd_hdr);
449 rd_load = image_get_load(rd_hdr);
450 break;
451#endif
452#if IMAGE_ENABLE_FIT
453 case IMAGE_FORMAT_FIT:
454 rd_noffset = fit_image_load(images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600455 rd_addr, &fit_uname_ramdisk,
456 &fit_uname_config, arch,
457 IH_TYPE_RAMDISK,
458 BOOTSTAGE_ID_FIT_RD_START,
459 FIT_LOAD_OPTIONAL_NON_ZERO,
460 &rd_data, &rd_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600461 if (rd_noffset < 0)
462 return 1;
463
464 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
465 images->fit_uname_rd = fit_uname_ramdisk;
466 images->fit_noffset_rd = rd_noffset;
467 break;
468#endif
469#ifdef CONFIG_ANDROID_BOOT_IMAGE
470 case IMAGE_FORMAT_ANDROID:
471 android_image_get_ramdisk((void *)images->os.start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600472 &rd_data, &rd_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600473 break;
474#endif
475 default:
476#ifdef CONFIG_SUPPORT_RAW_INITRD
477 end = NULL;
478 if (select)
479 end = strchr(select, ':');
480 if (end) {
481 rd_len = hextoul(++end, NULL);
482 rd_data = rd_addr;
483 } else
484#endif
485 {
486 puts("Wrong Ramdisk Image Format\n");
Simon Glass3d2a47f2021-09-25 07:03:16 -0600487 rd_data = 0;
488 rd_len = 0;
489 rd_load = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600490 return 1;
491 }
492 }
493 } else if (images->legacy_hdr_valid &&
494 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600495 IH_TYPE_MULTI)) {
Simon Glass41506ff2021-09-25 07:03:15 -0600496 /*
497 * Now check if we have a legacy mult-component image,
498 * get second entry data start address and len.
499 */
500 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600501 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
502 (ulong)images->legacy_hdr_os);
Simon Glass41506ff2021-09-25 07:03:15 -0600503
504 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
505 } else {
506 /*
507 * no initrd image
508 */
509 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass3d2a47f2021-09-25 07:03:16 -0600510 rd_len = 0;
511 rd_data = 0;
Simon Glass41506ff2021-09-25 07:03:15 -0600512 }
513
514 if (!rd_data) {
515 debug("## No init Ramdisk\n");
516 } else {
517 *rd_start = rd_data;
518 *rd_end = rd_data + rd_len;
519 }
520 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600521 *rd_start, *rd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600522
523 return 0;
524}
525
526#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
527/**
528 * boot_ramdisk_high - relocate init ramdisk
529 * @lmb: pointer to lmb handle, will be used for memory mgmt
530 * @rd_data: ramdisk data start address
531 * @rd_len: ramdisk data length
532 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
533 * start address (after possible relocation)
534 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
535 * end address (after possible relocation)
536 *
537 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
538 * variable and if requested ramdisk data is moved to a specified location.
539 *
540 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
541 * start/end addresses if ramdisk image start and len were provided,
542 * otherwise set initrd_start and initrd_end set to zeros.
543 *
544 * returns:
545 * 0 - success
546 * -1 - failure
547 */
548int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600549 ulong *initrd_start, ulong *initrd_end)
Simon Glass41506ff2021-09-25 07:03:15 -0600550{
551 char *s;
552 ulong initrd_high;
553 int initrd_copy_to_ram = 1;
554
555 s = env_get("initrd_high");
556 if (s) {
557 /* a value of "no" or a similar string will act like 0,
558 * turning the "load high" feature off. This is intentional.
559 */
560 initrd_high = hextoul(s, NULL);
561 if (initrd_high == ~0)
562 initrd_copy_to_ram = 0;
563 } else {
564 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
565 }
566
Simon Glass41506ff2021-09-25 07:03:15 -0600567 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600568 initrd_high, initrd_copy_to_ram);
Simon Glass41506ff2021-09-25 07:03:15 -0600569
570 if (rd_data) {
571 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
572 debug(" in-place initrd\n");
573 *initrd_start = rd_data;
574 *initrd_end = rd_data + rd_len;
575 lmb_reserve(lmb, rd_data, rd_len);
576 } else {
577 if (initrd_high)
578 *initrd_start = (ulong)lmb_alloc_base(lmb,
579 rd_len, 0x1000, initrd_high);
580 else
581 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
582 0x1000);
583
584 if (*initrd_start == 0) {
585 puts("ramdisk - allocation error\n");
586 goto error;
587 }
588 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
589
590 *initrd_end = *initrd_start + rd_len;
591 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600592 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600593
594 memmove_wd((void *)*initrd_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600595 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass41506ff2021-09-25 07:03:15 -0600596
597#ifdef CONFIG_MP
598 /*
599 * Ensure the image is flushed to memory to handle
600 * AMP boot scenarios in which we might not be
601 * HW cache coherent
602 */
603 flush_cache((unsigned long)*initrd_start,
604 ALIGN(rd_len, ARCH_DMA_MINALIGN));
605#endif
606 puts("OK\n");
607 }
608 } else {
609 *initrd_start = 0;
610 *initrd_end = 0;
611 }
612 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass3d2a47f2021-09-25 07:03:16 -0600613 *initrd_start, *initrd_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600614
615 return 0;
616
617error:
618 return -1;
619}
620#endif /* CONFIG_SYS_BOOT_RAMDISK_HIGH */
621
Simon Glass3d2a47f2021-09-25 07:03:16 -0600622int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass41506ff2021-09-25 07:03:15 -0600623 ulong *setup_start, ulong *setup_len)
624{
625#if IMAGE_ENABLE_FIT
626 return boot_get_setup_fit(images, arch, setup_start, setup_len);
627#else
628 return -ENOENT;
629#endif
630}
631
632#if IMAGE_ENABLE_FIT
633#if defined(CONFIG_FPGA)
634int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600635 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600636{
637 ulong tmp_img_addr, img_data, img_len;
638 void *buf;
639 int conf_noffset;
640 int fit_img_result;
641 const char *uname, *name;
642 int err;
643 int devnum = 0; /* TODO support multi fpga platforms */
644
645 /* Check to see if the images struct has a FIT configuration */
646 if (!genimg_has_config(images)) {
647 debug("## FIT configuration was not specified\n");
648 return 0;
649 }
650
651 /*
652 * Obtain the os FIT header from the images struct
653 */
654 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
655 buf = map_sysmem(tmp_img_addr, 0);
656 /*
657 * Check image type. For FIT images get FIT node
658 * and attempt to locate a generic binary.
659 */
660 switch (genimg_get_format(buf)) {
661 case IMAGE_FORMAT_FIT:
662 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
663
664 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
665 NULL);
666 if (!uname) {
667 debug("## FPGA image is not specified\n");
668 return 0;
669 }
670 fit_img_result = fit_image_load(images,
671 tmp_img_addr,
672 (const char **)&uname,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600673 &images->fit_uname_cfg,
Simon Glass41506ff2021-09-25 07:03:15 -0600674 arch,
675 IH_TYPE_FPGA,
676 BOOTSTAGE_ID_FPGA_INIT,
677 FIT_LOAD_OPTIONAL_NON_ZERO,
678 &img_data, &img_len);
679
680 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
681 uname, img_data, img_len);
682
683 if (fit_img_result < 0) {
684 /* Something went wrong! */
685 return fit_img_result;
686 }
687
688 if (!fpga_is_partial_data(devnum, img_len)) {
689 name = "full";
690 err = fpga_loadbitstream(devnum, (char *)img_data,
691 img_len, BIT_FULL);
692 if (err)
693 err = fpga_load(devnum, (const void *)img_data,
694 img_len, BIT_FULL);
695 } else {
696 name = "partial";
697 err = fpga_loadbitstream(devnum, (char *)img_data,
698 img_len, BIT_PARTIAL);
699 if (err)
700 err = fpga_load(devnum, (const void *)img_data,
701 img_len, BIT_PARTIAL);
702 }
703
704 if (err)
705 return err;
706
707 printf(" Programming %s bitstream... OK\n", name);
708 break;
709 default:
710 printf("The given image format is not supported (corrupt?)\n");
711 return 1;
712 }
713
714 return 0;
715}
716#endif
717
Simon Glass3d2a47f2021-09-25 07:03:16 -0600718static void fit_loadable_process(u8 img_type,
Simon Glass41506ff2021-09-25 07:03:15 -0600719 ulong img_data,
720 ulong img_len)
721{
722 int i;
723 const unsigned int count =
724 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
725 struct fit_loadable_tbl *fit_loadable_handler =
726 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
727 /* For each loadable handler */
728 for (i = 0; i < count; i++, fit_loadable_handler++)
729 /* matching this type */
730 if (fit_loadable_handler->type == img_type)
731 /* call that handler with this image data */
732 fit_loadable_handler->handler(img_data, img_len);
733}
734
735int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600736 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass41506ff2021-09-25 07:03:15 -0600737{
738 /*
739 * These variables are used to hold the current image location
740 * in system memory.
741 */
742 ulong tmp_img_addr;
743 /*
744 * These two variables are requirements for fit_image_load, but
745 * their values are not used
746 */
747 ulong img_data, img_len;
748 void *buf;
749 int loadables_index;
750 int conf_noffset;
751 int fit_img_result;
752 const char *uname;
Simon Glass3d2a47f2021-09-25 07:03:16 -0600753 u8 img_type;
Simon Glass41506ff2021-09-25 07:03:15 -0600754
755 /* Check to see if the images struct has a FIT configuration */
756 if (!genimg_has_config(images)) {
757 debug("## FIT configuration was not specified\n");
758 return 0;
759 }
760
761 /*
762 * Obtain the os FIT header from the images struct
763 */
764 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
765 buf = map_sysmem(tmp_img_addr, 0);
766 /*
767 * Check image type. For FIT images get FIT node
768 * and attempt to locate a generic binary.
769 */
770 switch (genimg_get_format(buf)) {
771 case IMAGE_FORMAT_FIT:
772 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
773
774 for (loadables_index = 0;
775 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600776 FIT_LOADABLE_PROP,
777 loadables_index, NULL), uname;
778 loadables_index++) {
779 fit_img_result = fit_image_load(images, tmp_img_addr,
780 &uname,
781 &images->fit_uname_cfg,
782 arch, IH_TYPE_LOADABLE,
783 BOOTSTAGE_ID_FIT_LOADABLE_START,
784 FIT_LOAD_OPTIONAL_NON_ZERO,
785 &img_data, &img_len);
Simon Glass41506ff2021-09-25 07:03:15 -0600786 if (fit_img_result < 0) {
787 /* Something went wrong! */
788 return fit_img_result;
789 }
790
791 fit_img_result = fit_image_get_node(buf, uname);
792 if (fit_img_result < 0) {
793 /* Something went wrong! */
794 return fit_img_result;
795 }
796 fit_img_result = fit_image_get_type(buf,
797 fit_img_result,
798 &img_type);
799 if (fit_img_result < 0) {
800 /* Something went wrong! */
801 return fit_img_result;
802 }
803
804 fit_loadable_process(img_type, img_data, img_len);
805 }
806 break;
807 default:
808 printf("The given image format is not supported (corrupt?)\n");
809 return 1;
810 }
811
812 return 0;
813}
814#endif
815
816#ifdef CONFIG_SYS_BOOT_GET_CMDLINE
817/**
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 Glass3d2a47f2021-09-25 07:03:16 -0600839 if (!cmdline)
Simon Glass41506ff2021-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 Glass3d2a47f2021-09-25 07:03:16 -0600848 *cmd_start = (ulong)cmdline;
Simon Glass41506ff2021-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}
855#endif /* CONFIG_SYS_BOOT_GET_CMDLINE */
856
857#ifdef CONFIG_SYS_BOOT_GET_KBD
858/**
859 * boot_get_kbd - allocate and initialize kernel copy of board info
860 * @lmb: pointer to lmb handle, will be used for memory mgmt
861 * @kbd: double pointer to board info data
862 *
863 * boot_get_kbd() allocates space for kernel copy of board info data below
864 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
865 * with the current u-boot board info data.
866 *
867 * returns:
868 * 0 - success
869 * -1 - failure
870 */
871int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
872{
873 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
874 sizeof(struct bd_info),
875 0xf,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600876 env_get_bootm_mapsize() +
877 env_get_bootm_low());
878 if (!*kbd)
Simon Glass41506ff2021-09-25 07:03:15 -0600879 return -1;
880
Simon Glass3d2a47f2021-09-25 07:03:16 -0600881 **kbd = *gd->bd;
Simon Glass41506ff2021-09-25 07:03:15 -0600882
883 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
884
885#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
886 do_bdinfo(NULL, 0, 0, NULL);
887#endif
888
889 return 0;
890}
891#endif /* CONFIG_SYS_BOOT_GET_KBD */
892
893#ifdef CONFIG_LMB
894int image_setup_linux(bootm_headers_t *images)
895{
896 ulong of_size = images->ft_len;
897 char **of_flat_tree = &images->ft_addr;
898 struct lmb *lmb = &images->lmb;
899 int ret;
900
901 if (IMAGE_ENABLE_OF_LIBFDT)
902 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
903
904 if (IMAGE_BOOT_GET_CMDLINE) {
905 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass3d2a47f2021-09-25 07:03:16 -0600906 &images->cmdline_end);
Simon Glass41506ff2021-09-25 07:03:15 -0600907 if (ret) {
908 puts("ERROR with allocation of cmdline\n");
909 return ret;
910 }
911 }
912
913 if (IMAGE_ENABLE_OF_LIBFDT) {
914 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
915 if (ret)
916 return ret;
917 }
918
919 if (IMAGE_ENABLE_OF_LIBFDT && of_size) {
920 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
921 if (ret)
922 return ret;
923 }
924
925 return 0;
926}
927#endif /* CONFIG_LMB */