blob: 131584a858a14dd14db24e454aa4214f4a8ed6e1 [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);
Simon Glass0f781382021-09-25 19:43:35 -0600230 } else if (CONFIG_IS_ENABLED(FIT) &&
231 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
Simon Glass4d7237b2021-09-25 07:03:15 -0600232 fit_uname_config)) {
233 debug("* kernel: config '%s' from image at 0x%08lx\n",
234 *fit_uname_config, kernel_addr);
Simon Glass0f781382021-09-25 19:43:35 -0600235 } else if (CONFIG_IS_ENABLED(FIT) &&
236 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
237 fit_uname_kernel)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600238 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
239 *fit_uname_kernel, kernel_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600240 } 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{
Simon Glass0f781382021-09-25 19:43:35 -0600278 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
279 const image_header_t *hdr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600280
Simon Glass0f781382021-09-25 19:43:35 -0600281 hdr = (const image_header_t *)img_addr;
282 if (image_check_magic(hdr))
283 return IMAGE_FORMAT_LEGACY;
284 }
285 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
286 if (!fdt_check_header(img_addr))
287 return IMAGE_FORMAT_FIT;
288 }
289 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
290 !android_image_check_header(img_addr))
Simon Glass4d7237b2021-09-25 07:03:15 -0600291 return IMAGE_FORMAT_ANDROID;
Simon Glass4d7237b2021-09-25 07:03:15 -0600292
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{
Simon Glass0f781382021-09-25 19:43:35 -0600309 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
Simon Glass4d7237b2021-09-25 07:03:15 -0600310 return 1;
Simon Glass0f781382021-09-25 19:43:35 -0600311
Simon Glass4d7237b2021-09-25 07:03:15 -0600312 return 0;
313}
314
315/**
316 * boot_get_ramdisk - main ramdisk handling routine
317 * @argc: command argument count
318 * @argv: command argument list
319 * @images: pointer to the bootm images structure
320 * @arch: expected ramdisk architecture
321 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
322 * @rd_end: pointer to a ulong variable, will hold ramdisk end
323 *
324 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
Simon Glass60d71542021-09-25 07:03:16 -0600325 * Currently supported are the following ramdisk sources:
Simon Glass4d7237b2021-09-25 07:03:15 -0600326 * - multicomponent kernel/ramdisk image,
327 * - commandline provided address of decicated ramdisk image.
328 *
329 * returns:
330 * 0, if ramdisk image was found and valid, or skiped
331 * rd_start and rd_end are set to ramdisk start/end addresses if
332 * ramdisk image is found and valid
333 *
334 * 1, if ramdisk image is found but corrupted, or invalid
335 * rd_start and rd_end are set to 0 if no ramdisk exists
336 */
337int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600338 u8 arch, ulong *rd_start, ulong *rd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600339{
Simon Glass4d7237b2021-09-25 07:03:15 -0600340 ulong rd_data, rd_len;
Simon Glass4d7237b2021-09-25 07:03:15 -0600341 void *buf;
Simon Glass4d7237b2021-09-25 07:03:15 -0600342 const char *select = NULL;
343
344 *rd_start = 0;
345 *rd_end = 0;
346
Simon Glass0f781382021-09-25 19:43:35 -0600347 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
348 /* Look for an Android boot image */
349 buf = map_sysmem(images->os.start, 0);
350 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
351 select = (argc == 0) ? env_get("loadaddr") : argv[0];
352 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600353
354 if (argc >= 2)
355 select = argv[1];
356
357 /*
358 * Look for a '-' which indicates to ignore the
359 * ramdisk argument
360 */
361 if (select && strcmp(select, "-") == 0) {
362 debug("## Skipping init Ramdisk\n");
Simon Glass60d71542021-09-25 07:03:16 -0600363 rd_len = 0;
364 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600365 } else if (select || genimg_has_config(images)) {
Simon Glass2de5b012021-09-25 19:43:36 -0600366 ulong rd_addr, rd_load;
367
Simon Glasse719d3b2021-09-25 19:43:20 -0600368#if CONFIG_IS_ENABLED(FIT)
Simon Glass2de5b012021-09-25 19:43:36 -0600369 const char *fit_uname_config = images->fit_uname_cfg;
370 const char *fit_uname_ramdisk = NULL;
371 int rd_noffset;
372
Simon Glass4d7237b2021-09-25 07:03:15 -0600373 if (select) {
Simon Glass2de5b012021-09-25 19:43:36 -0600374 ulong default_addr;
Simon Glass4d7237b2021-09-25 07:03:15 -0600375 /*
376 * If the init ramdisk comes from the FIT image and
377 * the FIT image address is omitted in the command
378 * line argument, try to use os FIT image address or
379 * default load address.
380 */
381 if (images->fit_uname_os)
382 default_addr = (ulong)images->fit_hdr_os;
383 else
384 default_addr = image_load_addr;
385
386 if (fit_parse_conf(select, default_addr,
387 &rd_addr, &fit_uname_config)) {
Simon Glass60d71542021-09-25 07:03:16 -0600388 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
389 fit_uname_config, rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600390 } else if (fit_parse_subimage(select, default_addr,
Simon Glass60d71542021-09-25 07:03:16 -0600391 &rd_addr,
392 &fit_uname_ramdisk)) {
393 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
394 fit_uname_ramdisk, rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600395 } else
396#endif
397 {
398 rd_addr = hextoul(select, NULL);
Simon Glass60d71542021-09-25 07:03:16 -0600399 debug("* ramdisk: cmdline image address = 0x%08lx\n",
400 rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600401 }
Simon Glasse719d3b2021-09-25 19:43:20 -0600402#if CONFIG_IS_ENABLED(FIT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600403 } else {
404 /* use FIT configuration provided in first bootm
405 * command argument. If the property is not defined,
406 * quit silently.
407 */
408 rd_addr = map_to_sysmem(images->fit_hdr_os);
409 rd_noffset = fit_get_node_from_config(images,
Simon Glass60d71542021-09-25 07:03:16 -0600410 FIT_RAMDISK_PROP,
411 rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600412 if (rd_noffset == -ENOENT)
413 return 0;
414 else if (rd_noffset < 0)
415 return 1;
416 }
417#endif
418
419 /*
420 * Check if there is an initrd image at the
421 * address provided in the second bootm argument
422 * check image type, for FIT images get FIT node.
423 */
424 buf = map_sysmem(rd_addr, 0);
425 switch (genimg_get_format(buf)) {
426#if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
Simon Glass2de5b012021-09-25 19:43:36 -0600427 case IMAGE_FORMAT_LEGACY: {
428 const image_header_t *rd_hdr;
429
Simon Glass60d71542021-09-25 07:03:16 -0600430 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
431 rd_addr);
Simon Glass4d7237b2021-09-25 07:03:15 -0600432
433 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
434 rd_hdr = image_get_ramdisk(rd_addr, arch,
Simon Glass60d71542021-09-25 07:03:16 -0600435 images->verify);
Simon Glass4d7237b2021-09-25 07:03:15 -0600436
Simon Glass60d71542021-09-25 07:03:16 -0600437 if (!rd_hdr)
Simon Glass4d7237b2021-09-25 07:03:15 -0600438 return 1;
439
440 rd_data = image_get_data(rd_hdr);
441 rd_len = image_get_data_size(rd_hdr);
442 rd_load = image_get_load(rd_hdr);
443 break;
Simon Glass2de5b012021-09-25 19:43:36 -0600444 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600445#endif
Simon Glasse719d3b2021-09-25 19:43:20 -0600446#if CONFIG_IS_ENABLED(FIT)
Simon Glass4d7237b2021-09-25 07:03:15 -0600447 case IMAGE_FORMAT_FIT:
448 rd_noffset = fit_image_load(images,
Simon Glass60d71542021-09-25 07:03:16 -0600449 rd_addr, &fit_uname_ramdisk,
450 &fit_uname_config, arch,
451 IH_TYPE_RAMDISK,
452 BOOTSTAGE_ID_FIT_RD_START,
453 FIT_LOAD_OPTIONAL_NON_ZERO,
454 &rd_data, &rd_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600455 if (rd_noffset < 0)
456 return 1;
457
458 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
459 images->fit_uname_rd = fit_uname_ramdisk;
460 images->fit_noffset_rd = rd_noffset;
461 break;
462#endif
463#ifdef CONFIG_ANDROID_BOOT_IMAGE
464 case IMAGE_FORMAT_ANDROID:
465 android_image_get_ramdisk((void *)images->os.start,
Simon Glass60d71542021-09-25 07:03:16 -0600466 &rd_data, &rd_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600467 break;
468#endif
469 default:
Simon Glass0f781382021-09-25 19:43:35 -0600470 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
471 char *end = NULL;
472
473 if (select)
474 end = strchr(select, ':');
475 if (end) {
476 rd_len = hextoul(++end, NULL);
477 rd_data = rd_addr;
478 break;
479 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600480 }
Simon Glass0f781382021-09-25 19:43:35 -0600481 puts("Wrong Ramdisk Image Format\n");
482 rd_data = 0;
483 rd_len = 0;
484 rd_load = 0;
485 return 1;
Simon Glass4d7237b2021-09-25 07:03:15 -0600486 }
487 } else if (images->legacy_hdr_valid &&
488 image_check_type(&images->legacy_hdr_os_copy,
Simon Glass60d71542021-09-25 07:03:16 -0600489 IH_TYPE_MULTI)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600490 /*
491 * Now check if we have a legacy mult-component image,
492 * get second entry data start address and len.
493 */
494 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600495 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
496 (ulong)images->legacy_hdr_os);
Simon Glass4d7237b2021-09-25 07:03:15 -0600497
498 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
499 } else {
500 /*
501 * no initrd image
502 */
503 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
Simon Glass60d71542021-09-25 07:03:16 -0600504 rd_len = 0;
505 rd_data = 0;
Simon Glass4d7237b2021-09-25 07:03:15 -0600506 }
507
508 if (!rd_data) {
509 debug("## No init Ramdisk\n");
510 } else {
511 *rd_start = rd_data;
512 *rd_end = rd_data + rd_len;
513 }
514 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600515 *rd_start, *rd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600516
517 return 0;
518}
519
Simon Glass4d7237b2021-09-25 07:03:15 -0600520/**
521 * boot_ramdisk_high - relocate init ramdisk
522 * @lmb: pointer to lmb handle, will be used for memory mgmt
523 * @rd_data: ramdisk data start address
524 * @rd_len: ramdisk data length
525 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
526 * start address (after possible relocation)
527 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
528 * end address (after possible relocation)
529 *
530 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
531 * variable and if requested ramdisk data is moved to a specified location.
532 *
533 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
534 * start/end addresses if ramdisk image start and len were provided,
535 * otherwise set initrd_start and initrd_end set to zeros.
536 *
537 * returns:
538 * 0 - success
539 * -1 - failure
540 */
541int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
Simon Glass60d71542021-09-25 07:03:16 -0600542 ulong *initrd_start, ulong *initrd_end)
Simon Glass4d7237b2021-09-25 07:03:15 -0600543{
544 char *s;
545 ulong initrd_high;
546 int initrd_copy_to_ram = 1;
547
548 s = env_get("initrd_high");
549 if (s) {
550 /* a value of "no" or a similar string will act like 0,
551 * turning the "load high" feature off. This is intentional.
552 */
553 initrd_high = hextoul(s, NULL);
554 if (initrd_high == ~0)
555 initrd_copy_to_ram = 0;
556 } else {
557 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
558 }
559
Simon Glass4d7237b2021-09-25 07:03:15 -0600560 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
Simon Glass60d71542021-09-25 07:03:16 -0600561 initrd_high, initrd_copy_to_ram);
Simon Glass4d7237b2021-09-25 07:03:15 -0600562
563 if (rd_data) {
564 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
565 debug(" in-place initrd\n");
566 *initrd_start = rd_data;
567 *initrd_end = rd_data + rd_len;
568 lmb_reserve(lmb, rd_data, rd_len);
569 } else {
570 if (initrd_high)
571 *initrd_start = (ulong)lmb_alloc_base(lmb,
572 rd_len, 0x1000, initrd_high);
573 else
574 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
575 0x1000);
576
577 if (*initrd_start == 0) {
578 puts("ramdisk - allocation error\n");
579 goto error;
580 }
581 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
582
583 *initrd_end = *initrd_start + rd_len;
584 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
Simon Glass60d71542021-09-25 07:03:16 -0600585 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600586
587 memmove_wd((void *)*initrd_start,
Simon Glass60d71542021-09-25 07:03:16 -0600588 (void *)rd_data, rd_len, CHUNKSZ);
Simon Glass4d7237b2021-09-25 07:03:15 -0600589
Simon Glass4d7237b2021-09-25 07:03:15 -0600590 /*
591 * Ensure the image is flushed to memory to handle
592 * AMP boot scenarios in which we might not be
593 * HW cache coherent
594 */
Simon Glass0f781382021-09-25 19:43:35 -0600595 if (IS_ENABLED(CONFIG_MP)) {
596 flush_cache((unsigned long)*initrd_start,
597 ALIGN(rd_len, ARCH_DMA_MINALIGN));
598 }
Simon Glass4d7237b2021-09-25 07:03:15 -0600599 puts("OK\n");
600 }
601 } else {
602 *initrd_start = 0;
603 *initrd_end = 0;
604 }
605 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
Simon Glass60d71542021-09-25 07:03:16 -0600606 *initrd_start, *initrd_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600607
608 return 0;
609
610error:
611 return -1;
612}
Simon Glass4d7237b2021-09-25 07:03:15 -0600613
Simon Glass60d71542021-09-25 07:03:16 -0600614int boot_get_setup(bootm_headers_t *images, u8 arch,
Simon Glass4d7237b2021-09-25 07:03:15 -0600615 ulong *setup_start, ulong *setup_len)
616{
Simon Glass0f781382021-09-25 19:43:35 -0600617 if (!CONFIG_IS_ENABLED(FIT))
618 return -ENOENT;
619
Simon Glass4d7237b2021-09-25 07:03:15 -0600620 return boot_get_setup_fit(images, arch, setup_start, setup_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600621}
622
Simon Glass4d7237b2021-09-25 07:03:15 -0600623int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600624 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600625{
626 ulong tmp_img_addr, img_data, img_len;
627 void *buf;
628 int conf_noffset;
629 int fit_img_result;
630 const char *uname, *name;
631 int err;
632 int devnum = 0; /* TODO support multi fpga platforms */
633
Simon Glass0f781382021-09-25 19:43:35 -0600634 if (!IS_ENABLED(CONFIG_FPGA))
635 return -ENOSYS;
636
Simon Glass4d7237b2021-09-25 07:03:15 -0600637 /* Check to see if the images struct has a FIT configuration */
638 if (!genimg_has_config(images)) {
639 debug("## FIT configuration was not specified\n");
640 return 0;
641 }
642
643 /*
644 * Obtain the os FIT header from the images struct
645 */
646 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
647 buf = map_sysmem(tmp_img_addr, 0);
648 /*
649 * Check image type. For FIT images get FIT node
650 * and attempt to locate a generic binary.
651 */
652 switch (genimg_get_format(buf)) {
653 case IMAGE_FORMAT_FIT:
654 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
655
656 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
657 NULL);
658 if (!uname) {
659 debug("## FPGA image is not specified\n");
660 return 0;
661 }
662 fit_img_result = fit_image_load(images,
663 tmp_img_addr,
664 (const char **)&uname,
Simon Glass60d71542021-09-25 07:03:16 -0600665 &images->fit_uname_cfg,
Simon Glass4d7237b2021-09-25 07:03:15 -0600666 arch,
667 IH_TYPE_FPGA,
668 BOOTSTAGE_ID_FPGA_INIT,
669 FIT_LOAD_OPTIONAL_NON_ZERO,
670 &img_data, &img_len);
671
672 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
673 uname, img_data, img_len);
674
675 if (fit_img_result < 0) {
676 /* Something went wrong! */
677 return fit_img_result;
678 }
679
680 if (!fpga_is_partial_data(devnum, img_len)) {
681 name = "full";
682 err = fpga_loadbitstream(devnum, (char *)img_data,
683 img_len, BIT_FULL);
684 if (err)
685 err = fpga_load(devnum, (const void *)img_data,
686 img_len, BIT_FULL);
687 } else {
688 name = "partial";
689 err = fpga_loadbitstream(devnum, (char *)img_data,
690 img_len, BIT_PARTIAL);
691 if (err)
692 err = fpga_load(devnum, (const void *)img_data,
693 img_len, BIT_PARTIAL);
694 }
695
696 if (err)
697 return err;
698
699 printf(" Programming %s bitstream... OK\n", name);
700 break;
701 default:
702 printf("The given image format is not supported (corrupt?)\n");
703 return 1;
704 }
705
706 return 0;
707}
Simon Glass4d7237b2021-09-25 07:03:15 -0600708
Simon Glass60d71542021-09-25 07:03:16 -0600709static void fit_loadable_process(u8 img_type,
Simon Glass4d7237b2021-09-25 07:03:15 -0600710 ulong img_data,
711 ulong img_len)
712{
713 int i;
714 const unsigned int count =
715 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
716 struct fit_loadable_tbl *fit_loadable_handler =
717 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
718 /* For each loadable handler */
719 for (i = 0; i < count; i++, fit_loadable_handler++)
720 /* matching this type */
721 if (fit_loadable_handler->type == img_type)
722 /* call that handler with this image data */
723 fit_loadable_handler->handler(img_data, img_len);
724}
725
726int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
Simon Glass60d71542021-09-25 07:03:16 -0600727 u8 arch, const ulong *ld_start, ulong * const ld_len)
Simon Glass4d7237b2021-09-25 07:03:15 -0600728{
729 /*
730 * These variables are used to hold the current image location
731 * in system memory.
732 */
733 ulong tmp_img_addr;
734 /*
735 * These two variables are requirements for fit_image_load, but
736 * their values are not used
737 */
738 ulong img_data, img_len;
739 void *buf;
740 int loadables_index;
741 int conf_noffset;
742 int fit_img_result;
743 const char *uname;
Simon Glass60d71542021-09-25 07:03:16 -0600744 u8 img_type;
Simon Glass4d7237b2021-09-25 07:03:15 -0600745
746 /* Check to see if the images struct has a FIT configuration */
747 if (!genimg_has_config(images)) {
748 debug("## FIT configuration was not specified\n");
749 return 0;
750 }
751
752 /*
753 * Obtain the os FIT header from the images struct
754 */
755 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
756 buf = map_sysmem(tmp_img_addr, 0);
757 /*
758 * Check image type. For FIT images get FIT node
759 * and attempt to locate a generic binary.
760 */
761 switch (genimg_get_format(buf)) {
762 case IMAGE_FORMAT_FIT:
763 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
764
765 for (loadables_index = 0;
766 uname = fdt_stringlist_get(buf, conf_noffset,
Simon Glass60d71542021-09-25 07:03:16 -0600767 FIT_LOADABLE_PROP,
768 loadables_index, NULL), uname;
769 loadables_index++) {
770 fit_img_result = fit_image_load(images, tmp_img_addr,
771 &uname,
772 &images->fit_uname_cfg,
773 arch, IH_TYPE_LOADABLE,
774 BOOTSTAGE_ID_FIT_LOADABLE_START,
775 FIT_LOAD_OPTIONAL_NON_ZERO,
776 &img_data, &img_len);
Simon Glass4d7237b2021-09-25 07:03:15 -0600777 if (fit_img_result < 0) {
778 /* Something went wrong! */
779 return fit_img_result;
780 }
781
782 fit_img_result = fit_image_get_node(buf, uname);
783 if (fit_img_result < 0) {
784 /* Something went wrong! */
785 return fit_img_result;
786 }
787 fit_img_result = fit_image_get_type(buf,
788 fit_img_result,
789 &img_type);
790 if (fit_img_result < 0) {
791 /* Something went wrong! */
792 return fit_img_result;
793 }
794
795 fit_loadable_process(img_type, img_data, img_len);
796 }
797 break;
798 default:
799 printf("The given image format is not supported (corrupt?)\n");
800 return 1;
801 }
802
803 return 0;
804}
Simon Glass4d7237b2021-09-25 07:03:15 -0600805
Simon Glass4d7237b2021-09-25 07:03:15 -0600806/**
807 * boot_get_cmdline - allocate and initialize kernel cmdline
808 * @lmb: pointer to lmb handle, will be used for memory mgmt
809 * @cmd_start: pointer to a ulong variable, will hold cmdline start
810 * @cmd_end: pointer to a ulong variable, will hold cmdline end
811 *
812 * boot_get_cmdline() allocates space for kernel command line below
813 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
814 * variable is present its contents is copied to allocated kernel
815 * command line.
816 *
817 * returns:
818 * 0 - success
819 * -1 - failure
820 */
821int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
822{
823 char *cmdline;
824 char *s;
825
826 cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
827 env_get_bootm_mapsize() + env_get_bootm_low());
Simon Glass60d71542021-09-25 07:03:16 -0600828 if (!cmdline)
Simon Glass4d7237b2021-09-25 07:03:15 -0600829 return -1;
830
831 s = env_get("bootargs");
832 if (!s)
833 s = "";
834
835 strcpy(cmdline, s);
836
Simon Glass60d71542021-09-25 07:03:16 -0600837 *cmd_start = (ulong)cmdline;
Simon Glass4d7237b2021-09-25 07:03:15 -0600838 *cmd_end = *cmd_start + strlen(cmdline);
839
840 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
841
842 return 0;
843}
Simon Glass4d7237b2021-09-25 07:03:15 -0600844
Simon Glass4d7237b2021-09-25 07:03:15 -0600845/**
846 * boot_get_kbd - allocate and initialize kernel copy of board info
847 * @lmb: pointer to lmb handle, will be used for memory mgmt
848 * @kbd: double pointer to board info data
849 *
850 * boot_get_kbd() allocates space for kernel copy of board info data below
851 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
852 * with the current u-boot board info data.
853 *
854 * returns:
855 * 0 - success
856 * -1 - failure
857 */
858int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
859{
860 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
861 sizeof(struct bd_info),
862 0xf,
Simon Glass60d71542021-09-25 07:03:16 -0600863 env_get_bootm_mapsize() +
864 env_get_bootm_low());
865 if (!*kbd)
Simon Glass4d7237b2021-09-25 07:03:15 -0600866 return -1;
867
Simon Glass60d71542021-09-25 07:03:16 -0600868 **kbd = *gd->bd;
Simon Glass4d7237b2021-09-25 07:03:15 -0600869
870 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
871
Simon Glassbc0c4db2021-09-25 07:03:20 -0600872#if defined(DEBUG)
873 if (IS_ENABLED(CONFIG_CMD_BDI)
874 do_bdinfo(NULL, 0, 0, NULL);
Simon Glass4d7237b2021-09-25 07:03:15 -0600875#endif
876
877 return 0;
878}
Simon Glass4d7237b2021-09-25 07:03:15 -0600879
Simon Glass4d7237b2021-09-25 07:03:15 -0600880int image_setup_linux(bootm_headers_t *images)
881{
882 ulong of_size = images->ft_len;
883 char **of_flat_tree = &images->ft_addr;
884 struct lmb *lmb = &images->lmb;
885 int ret;
886
Simon Glass85c057e2021-09-25 19:43:21 -0600887 if (CONFIG_IS_ENABLED(OF_LIBFDT))
Simon Glass4d7237b2021-09-25 07:03:15 -0600888 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
889
Simon Glassb8eb1fe2021-09-25 19:43:25 -0600890 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600891 ret = boot_get_cmdline(lmb, &images->cmdline_start,
Simon Glass60d71542021-09-25 07:03:16 -0600892 &images->cmdline_end);
Simon Glass4d7237b2021-09-25 07:03:15 -0600893 if (ret) {
894 puts("ERROR with allocation of cmdline\n");
895 return ret;
896 }
897 }
898
Simon Glass85c057e2021-09-25 19:43:21 -0600899 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600900 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
901 if (ret)
902 return ret;
903 }
904
Simon Glass85c057e2021-09-25 19:43:21 -0600905 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
Simon Glass4d7237b2021-09-25 07:03:15 -0600906 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
907 if (ret)
908 return ret;
909 }
910
911 return 0;
912}
Simon Glass4a8a8a12021-09-25 07:03:17 -0600913
914void genimg_print_size(uint32_t size)
915{
916 printf("%d Bytes = ", size);
917 print_size(size, "\n");
918}
919
920void genimg_print_time(time_t timestamp)
921{
922 struct rtc_time tm;
923
924 rtc_to_tm(timestamp, &tm);
925 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
926 tm.tm_year, tm.tm_mon, tm.tm_mday,
927 tm.tm_hour, tm.tm_min, tm.tm_sec);
928}