]> Git Repo - J-u-boot.git/blame - boot/image-board.c
bootstd: Support creating a boot menu
[J-u-boot.git] / boot / image-board.c
CommitLineData
41506ff5
SG
1// 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, [email protected].
9 */
10
11#include <common.h>
12#include <bootstage.h>
13#include <cpu_func.h>
4e4bf944 14#include <display_options.h>
41506ff5
SG
15#include <env.h>
16#include <fpga.h>
17#include <image.h>
4b32531b 18#include <init.h>
9c2e9128 19#include <log.h>
41506ff5 20#include <mapmem.h>
5d3248a6 21#include <rtc.h>
41506ff5
SG
22#include <watchdog.h>
23#include <asm/cache.h>
24#include <asm/global_data.h>
25
41506ff5
SG
26DECLARE_GLOBAL_DATA_PTR;
27
41506ff5
SG
28/**
29 * image_get_ramdisk - get and verify ramdisk image
30 * @rd_addr: ramdisk image start address
31 * @arch: expected ramdisk architecture
32 * @verify: checksum verification flag
33 *
34 * image_get_ramdisk() returns a pointer to the verified ramdisk image
35 * header. Routine receives image start address and expected architecture
36 * flag. Verification done covers data and header integrity and os/type/arch
37 * fields checking.
38 *
39 * returns:
40 * pointer to a ramdisk image header, if image was found and valid
41 * otherwise, return NULL
42 */
f3543e69
SG
43static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
44 int verify)
41506ff5 45{
f3543e69 46 const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr;
41506ff5
SG
47
48 if (!image_check_magic(rd_hdr)) {
49 puts("Bad Magic Number\n");
50 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
51 return NULL;
52 }
53
54 if (!image_check_hcrc(rd_hdr)) {
55 puts("Bad Header Checksum\n");
56 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
57 return NULL;
58 }
59
60 bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
61 image_print_contents(rd_hdr);
62
63 if (verify) {
64 puts(" Verifying Checksum ... ");
65 if (!image_check_dcrc(rd_hdr)) {
66 puts("Bad Data CRC\n");
67 bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
68 return NULL;
69 }
70 puts("OK\n");
71 }
72
73 bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
74
75 if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
76 !image_check_arch(rd_hdr, arch) ||
77 !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
78 printf("No Linux %s Ramdisk Image\n",
3d2a47f1 79 genimg_get_arch_name(arch));
41506ff5
SG
80 bootstage_error(BOOTSTAGE_ID_RAMDISK);
81 return NULL;
82 }
83
84 return rd_hdr;
85}
41506ff5
SG
86
87/*****************************************************************************/
88/* Shared dual-format routines */
89/*****************************************************************************/
90ulong image_load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
91ulong image_save_addr; /* Default Save Address */
92ulong image_save_size; /* Default Save Size (in bytes) */
93
94static int on_loadaddr(const char *name, const char *value, enum env_op op,
3d2a47f1 95 int flags)
41506ff5
SG
96{
97 switch (op) {
98 case env_op_create:
99 case env_op_overwrite:
100 image_load_addr = hextoul(value, NULL);
101 break;
102 default:
103 break;
104 }
105
106 return 0;
107}
108U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
110ulong env_get_bootm_low(void)
111{
112 char *s = env_get("bootm_low");
3d2a47f1 113
41506ff5
SG
114 if (s) {
115 ulong tmp = hextoul(s, NULL);
116 return tmp;
117 }
118
aa6e94de
TR
119#if defined(CFG_SYS_SDRAM_BASE)
120 return CFG_SYS_SDRAM_BASE;
41506ff5
SG
121#elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
122 return gd->bd->bi_dram[0].start;
123#else
124 return 0;
125#endif
126}
127
128phys_size_t env_get_bootm_size(void)
129{
130 phys_size_t tmp, size;
131 phys_addr_t start;
132 char *s = env_get("bootm_size");
3d2a47f1 133
41506ff5
SG
134 if (s) {
135 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
136 return tmp;
137 }
138
139 start = gd->ram_base;
140 size = gd->ram_size;
141
142 if (start + size > gd->ram_top)
143 size = gd->ram_top - start;
144
145 s = env_get("bootm_low");
146 if (s)
147 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
148 else
149 tmp = start;
150
151 return size - (tmp - start);
152}
153
154phys_size_t env_get_bootm_mapsize(void)
155{
156 phys_size_t tmp;
157 char *s = env_get("bootm_mapsize");
3d2a47f1 158
41506ff5
SG
159 if (s) {
160 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
161 return tmp;
162 }
163
65cc0e2a
TR
164#if defined(CFG_SYS_BOOTMAPSZ)
165 return CFG_SYS_BOOTMAPSZ;
41506ff5
SG
166#else
167 return env_get_bootm_size();
168#endif
169}
170
171void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
172{
173 if (to == from)
174 return;
175
9c2e9128 176 if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
41506ff5 177 if (to > from) {
9c2e9128
SG
178 from += len;
179 to += len;
41506ff5 180 }
9c2e9128
SG
181 while (len > 0) {
182 size_t tail = (len > chunksz) ? chunksz : len;
183
29caf930 184 schedule();
9c2e9128
SG
185 if (to > from) {
186 to -= tail;
187 from -= tail;
188 }
189 memmove(to, from, tail);
190 if (to < from) {
191 to += tail;
192 from += tail;
193 }
194 len -= tail;
41506ff5 195 }
9c2e9128
SG
196 } else {
197 memmove(to, from, len);
41506ff5 198 }
41506ff5
SG
199}
200
201/**
202 * genimg_get_kernel_addr_fit - get the real kernel address and return 2
203 * FIT strings
204 * @img_addr: a string might contain real image address
205 * @fit_uname_config: double pointer to a char, will hold pointer to a
206 * configuration unit name
207 * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
208 * name
209 *
210 * genimg_get_kernel_addr_fit get the real kernel start address from a string
211 * which is normally the first argv of bootm/bootz
212 *
213 * returns:
214 * kernel start address
215 */
216ulong genimg_get_kernel_addr_fit(char * const img_addr,
3d2a47f1
SG
217 const char **fit_uname_config,
218 const char **fit_uname_kernel)
41506ff5
SG
219{
220 ulong kernel_addr;
221
222 /* find out kernel image address */
223 if (!img_addr) {
224 kernel_addr = image_load_addr;
225 debug("* kernel: default image load address = 0x%08lx\n",
226 image_load_addr);
1df654a6
SG
227 } else if (CONFIG_IS_ENABLED(FIT) &&
228 fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
41506ff5
SG
229 fit_uname_config)) {
230 debug("* kernel: config '%s' from image at 0x%08lx\n",
231 *fit_uname_config, kernel_addr);
1df654a6
SG
232 } else if (CONFIG_IS_ENABLED(FIT) &&
233 fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
234 fit_uname_kernel)) {
41506ff5
SG
235 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
236 *fit_uname_kernel, kernel_addr);
41506ff5
SG
237 } else {
238 kernel_addr = hextoul(img_addr, NULL);
239 debug("* kernel: cmdline image address = 0x%08lx\n",
240 kernel_addr);
241 }
242
243 return kernel_addr;
244}
245
246/**
247 * genimg_get_kernel_addr() is the simple version of
248 * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
249 */
250ulong genimg_get_kernel_addr(char * const img_addr)
251{
252 const char *fit_uname_config = NULL;
253 const char *fit_uname_kernel = NULL;
254
255 return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
256 &fit_uname_kernel);
257}
258
259/**
260 * genimg_get_format - get image format type
261 * @img_addr: image start address
262 *
263 * genimg_get_format() checks whether provided address points to a valid
264 * legacy or FIT image.
265 *
266 * New uImage format and FDT blob are based on a libfdt. FDT blob
267 * may be passed directly or embedded in a FIT image. In both situations
268 * genimg_get_format() must be able to dectect libfdt header.
269 *
270 * returns:
271 * image format type or IMAGE_FORMAT_INVALID if no image is present
272 */
273int genimg_get_format(const void *img_addr)
274{
1df654a6 275 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
f3543e69 276 const struct legacy_img_hdr *hdr;
41506ff5 277
f3543e69 278 hdr = (const struct legacy_img_hdr *)img_addr;
1df654a6
SG
279 if (image_check_magic(hdr))
280 return IMAGE_FORMAT_LEGACY;
281 }
282 if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
283 if (!fdt_check_header(img_addr))
284 return IMAGE_FORMAT_FIT;
285 }
286 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
287 !android_image_check_header(img_addr))
41506ff5 288 return IMAGE_FORMAT_ANDROID;
41506ff5
SG
289
290 return IMAGE_FORMAT_INVALID;
291}
292
293/**
294 * fit_has_config - check if there is a valid FIT configuration
295 * @images: pointer to the bootm command headers structure
296 *
297 * fit_has_config() checks if there is a FIT configuration in use
298 * (if FTI support is present).
299 *
300 * returns:
301 * 0, no FIT support or no configuration found
302 * 1, configuration found
303 */
d9d7c20b 304int genimg_has_config(struct bootm_headers *images)
41506ff5 305{
1df654a6 306 if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
41506ff5 307 return 1;
1df654a6 308
41506ff5
SG
309 return 0;
310}
311
312/**
e4c92879
SG
313 * select_ramdisk() - Select and locate the ramdisk to use
314 *
41506ff5 315 * @images: pointer to the bootm images structure
4f2d9412 316 * @select: name of ramdisk to select, or hex address, NULL for any
41506ff5 317 * @arch: expected ramdisk architecture
e4c92879
SG
318 * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
319 * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
185f812c 320 * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
e4c92879 321 * other -ve value on other error
41506ff5 322 */
d9d7c20b 323static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
e4c92879 324 ulong *rd_datap, ulong *rd_lenp)
41506ff5 325{
f7659f69
SG
326 const char *fit_uname_config;
327 const char *fit_uname_ramdisk;
ca78883f 328 bool done_select = !select;
4f2d9412 329 bool done = false;
f7659f69 330 int rd_noffset;
621158d1 331 ulong rd_addr;
e4c92879 332 char *buf;
78f88790 333
ca78883f 334 if (CONFIG_IS_ENABLED(FIT)) {
f7659f69
SG
335 fit_uname_config = images->fit_uname_cfg;
336 fit_uname_ramdisk = NULL;
78f88790 337
621158d1
TR
338 if (select) {
339 ulong default_addr;
41506ff5
SG
340 /*
341 * If the init ramdisk comes from the FIT image and
342 * the FIT image address is omitted in the command
343 * line argument, try to use os FIT image address or
344 * default load address.
345 */
346 if (images->fit_uname_os)
347 default_addr = (ulong)images->fit_hdr_os;
348 else
349 default_addr = image_load_addr;
350
20f5d83f
SG
351 if (fit_parse_conf(select, default_addr, &rd_addr,
352 &fit_uname_config)) {
3d2a47f1
SG
353 debug("* ramdisk: config '%s' from image at 0x%08lx\n",
354 fit_uname_config, rd_addr);
ca78883f 355 done_select = true;
41506ff5 356 } else if (fit_parse_subimage(select, default_addr,
3d2a47f1
SG
357 &rd_addr,
358 &fit_uname_ramdisk)) {
359 debug("* ramdisk: subimage '%s' from image at 0x%08lx\n",
360 fit_uname_ramdisk, rd_addr);
ca78883f
SG
361 done_select = true;
362 }
363 }
364 }
365 if (!done_select) {
20f5d83f
SG
366 rd_addr = hextoul(select, NULL);
367 debug("* ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
ca78883f 368 }
20f5d83f
SG
369 if (CONFIG_IS_ENABLED(FIT) && !select) {
370 /* use FIT configuration provided in first bootm
371 * command argument. If the property is not defined,
372 * quit silently (with -ENOPKG)
621158d1 373 */
20f5d83f
SG
374 rd_addr = map_to_sysmem(images->fit_hdr_os);
375 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
376 rd_addr);
377 if (rd_noffset == -ENOENT)
378 return -ENOPKG;
379 else if (rd_noffset < 0)
380 return rd_noffset;
381 }
621158d1 382
20f5d83f
SG
383 /*
384 * Check if there is an initrd image at the
385 * address provided in the second bootm argument
386 * check image type, for FIT images get FIT node.
387 */
388 buf = map_sysmem(rd_addr, 0);
389 switch (genimg_get_format(buf)) {
390 case IMAGE_FORMAT_LEGACY:
391 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
f3543e69 392 const struct legacy_img_hdr *rd_hdr;
20f5d83f
SG
393
394 printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
395 rd_addr);
396
397 bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
398 rd_hdr = image_get_ramdisk(rd_addr, arch,
399 images->verify);
400
401 if (!rd_hdr)
402 return -ENOENT;
403
404 *rd_datap = image_get_data(rd_hdr);
405 *rd_lenp = image_get_data_size(rd_hdr);
406 done = true;
407 }
408 break;
409 case IMAGE_FORMAT_FIT:
410 if (CONFIG_IS_ENABLED(FIT)) {
411 rd_noffset = fit_image_load(images, rd_addr,
412 &fit_uname_ramdisk,
413 &fit_uname_config,
414 arch, IH_TYPE_RAMDISK,
415 BOOTSTAGE_ID_FIT_RD_START,
416 FIT_LOAD_OPTIONAL_NON_ZERO,
417 rd_datap, rd_lenp);
418 if (rd_noffset < 0)
419 return rd_noffset;
41506ff5 420
20f5d83f
SG
421 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
422 images->fit_uname_rd = fit_uname_ramdisk;
423 images->fit_noffset_rd = rd_noffset;
424 done = true;
425 }
426 break;
427 case IMAGE_FORMAT_ANDROID:
428 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
429 void *ptr = map_sysmem(images->os.start, 0);
430 int ret;
431
432 ret = android_image_get_ramdisk(ptr, rd_datap, rd_lenp);
433 unmap_sysmem(ptr);
434 if (ret)
435 return ret;
436 done = true;
4f2d9412 437 }
20f5d83f
SG
438 break;
439 }
4f2d9412
SG
440
441 if (!done) {
442 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
443 char *end = NULL;
444
445 if (select)
446 end = strchr(select, ':');
447 if (end) {
448 *rd_lenp = hextoul(++end, NULL);
449 *rd_datap = rd_addr;
450 done = true;
41506ff5 451 }
4f2d9412
SG
452 }
453
454 if (!done) {
1df654a6 455 puts("Wrong Ramdisk Image Format\n");
e4c92879 456 return -EINVAL;
41506ff5 457 }
4f2d9412 458 }
e4c92879
SG
459
460 return 0;
461}
462
463/**
464 * boot_get_ramdisk - main ramdisk handling routine
465 * @argc: command argument count
466 * @argv: command argument list
467 * @images: pointer to the bootm images structure
468 * @arch: expected ramdisk architecture
469 * @rd_start: pointer to a ulong variable, will hold ramdisk start address
470 * @rd_end: pointer to a ulong variable, will hold ramdisk end
471 *
472 * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
473 * Currently supported are the following ramdisk sources:
474 * - multicomponent kernel/ramdisk image,
475 * - commandline provided address of decicated ramdisk image.
476 *
477 * returns:
478 * 0, if ramdisk image was found and valid, or skiped
479 * rd_start and rd_end are set to ramdisk start/end addresses if
480 * ramdisk image is found and valid
481 *
482 * 1, if ramdisk image is found but corrupted, or invalid
483 * rd_start and rd_end are set to 0 if no ramdisk exists
484 */
d9d7c20b 485int boot_get_ramdisk(int argc, char *const argv[], struct bootm_headers *images,
e4c92879
SG
486 u8 arch, ulong *rd_start, ulong *rd_end)
487{
488 ulong rd_data, rd_len;
489 const char *select = NULL;
490
491 *rd_start = 0;
492 *rd_end = 0;
493
494 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
495 char *buf;
496
497 /* Look for an Android boot image */
498 buf = map_sysmem(images->os.start, 0);
499 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
500 select = (argc == 0) ? env_get("loadaddr") : argv[0];
501 }
502
503 if (argc >= 2)
504 select = argv[1];
505
506 /*
507 * Look for a '-' which indicates to ignore the
508 * ramdisk argument
509 */
510 if (select && strcmp(select, "-") == 0) {
511 debug("## Skipping init Ramdisk\n");
512 rd_len = 0;
513 rd_data = 0;
514 } else if (select || genimg_has_config(images)) {
515 int ret;
516
517 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
518 if (ret == -ENOPKG)
519 return 0;
520 else if (ret)
521 return ret;
41506ff5
SG
522 } else if (images->legacy_hdr_valid &&
523 image_check_type(&images->legacy_hdr_os_copy,
3d2a47f1 524 IH_TYPE_MULTI)) {
41506ff5
SG
525 /*
526 * Now check if we have a legacy mult-component image,
527 * get second entry data start address and len.
528 */
529 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
3d2a47f1
SG
530 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
531 (ulong)images->legacy_hdr_os);
41506ff5
SG
532
533 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
534 } else {
535 /*
536 * no initrd image
537 */
538 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
3d2a47f1
SG
539 rd_len = 0;
540 rd_data = 0;
41506ff5
SG
541 }
542
543 if (!rd_data) {
544 debug("## No init Ramdisk\n");
545 } else {
546 *rd_start = rd_data;
547 *rd_end = rd_data + rd_len;
548 }
549 debug(" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
3d2a47f1 550 *rd_start, *rd_end);
41506ff5
SG
551
552 return 0;
553}
554
41506ff5
SG
555/**
556 * boot_ramdisk_high - relocate init ramdisk
557 * @lmb: pointer to lmb handle, will be used for memory mgmt
558 * @rd_data: ramdisk data start address
559 * @rd_len: ramdisk data length
560 * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
561 * start address (after possible relocation)
562 * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
563 * end address (after possible relocation)
564 *
565 * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
566 * variable and if requested ramdisk data is moved to a specified location.
567 *
568 * Initrd_start and initrd_end are set to final (after relocation) ramdisk
569 * start/end addresses if ramdisk image start and len were provided,
570 * otherwise set initrd_start and initrd_end set to zeros.
571 *
572 * returns:
573 * 0 - success
574 * -1 - failure
575 */
576int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
3d2a47f1 577 ulong *initrd_start, ulong *initrd_end)
41506ff5
SG
578{
579 char *s;
580 ulong initrd_high;
581 int initrd_copy_to_ram = 1;
582
583 s = env_get("initrd_high");
584 if (s) {
585 /* a value of "no" or a similar string will act like 0,
586 * turning the "load high" feature off. This is intentional.
587 */
588 initrd_high = hextoul(s, NULL);
589 if (initrd_high == ~0)
590 initrd_copy_to_ram = 0;
591 } else {
592 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
593 }
594
41506ff5 595 debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
3d2a47f1 596 initrd_high, initrd_copy_to_ram);
41506ff5
SG
597
598 if (rd_data) {
599 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
600 debug(" in-place initrd\n");
601 *initrd_start = rd_data;
602 *initrd_end = rd_data + rd_len;
603 lmb_reserve(lmb, rd_data, rd_len);
604 } else {
605 if (initrd_high)
606 *initrd_start = (ulong)lmb_alloc_base(lmb,
607 rd_len, 0x1000, initrd_high);
608 else
609 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
610 0x1000);
611
612 if (*initrd_start == 0) {
613 puts("ramdisk - allocation error\n");
614 goto error;
615 }
616 bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
617
618 *initrd_end = *initrd_start + rd_len;
619 printf(" Loading Ramdisk to %08lx, end %08lx ... ",
3d2a47f1 620 *initrd_start, *initrd_end);
41506ff5
SG
621
622 memmove_wd((void *)*initrd_start,
3d2a47f1 623 (void *)rd_data, rd_len, CHUNKSZ);
41506ff5 624
41506ff5
SG
625 /*
626 * Ensure the image is flushed to memory to handle
627 * AMP boot scenarios in which we might not be
628 * HW cache coherent
629 */
1df654a6
SG
630 if (IS_ENABLED(CONFIG_MP)) {
631 flush_cache((unsigned long)*initrd_start,
632 ALIGN(rd_len, ARCH_DMA_MINALIGN));
633 }
41506ff5
SG
634 puts("OK\n");
635 }
636 } else {
637 *initrd_start = 0;
638 *initrd_end = 0;
639 }
640 debug(" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
3d2a47f1 641 *initrd_start, *initrd_end);
41506ff5
SG
642
643 return 0;
644
645error:
646 return -1;
647}
41506ff5 648
d9d7c20b 649int boot_get_setup(struct bootm_headers *images, u8 arch,
41506ff5
SG
650 ulong *setup_start, ulong *setup_len)
651{
1df654a6
SG
652 if (!CONFIG_IS_ENABLED(FIT))
653 return -ENOENT;
654
41506ff5 655 return boot_get_setup_fit(images, arch, setup_start, setup_len);
41506ff5
SG
656}
657
d9d7c20b 658int boot_get_fpga(int argc, char *const argv[], struct bootm_headers *images,
3d2a47f1 659 u8 arch, const ulong *ld_start, ulong * const ld_len)
41506ff5
SG
660{
661 ulong tmp_img_addr, img_data, img_len;
662 void *buf;
663 int conf_noffset;
664 int fit_img_result;
665 const char *uname, *name;
666 int err;
667 int devnum = 0; /* TODO support multi fpga platforms */
668
1df654a6
SG
669 if (!IS_ENABLED(CONFIG_FPGA))
670 return -ENOSYS;
671
41506ff5
SG
672 /* Check to see if the images struct has a FIT configuration */
673 if (!genimg_has_config(images)) {
674 debug("## FIT configuration was not specified\n");
675 return 0;
676 }
677
678 /*
679 * Obtain the os FIT header from the images struct
680 */
681 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
682 buf = map_sysmem(tmp_img_addr, 0);
683 /*
684 * Check image type. For FIT images get FIT node
685 * and attempt to locate a generic binary.
686 */
687 switch (genimg_get_format(buf)) {
688 case IMAGE_FORMAT_FIT:
689 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
690
691 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
692 NULL);
693 if (!uname) {
694 debug("## FPGA image is not specified\n");
695 return 0;
696 }
697 fit_img_result = fit_image_load(images,
698 tmp_img_addr,
699 (const char **)&uname,
3d2a47f1 700 &images->fit_uname_cfg,
41506ff5
SG
701 arch,
702 IH_TYPE_FPGA,
703 BOOTSTAGE_ID_FPGA_INIT,
704 FIT_LOAD_OPTIONAL_NON_ZERO,
705 &img_data, &img_len);
706
707 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
708 uname, img_data, img_len);
709
710 if (fit_img_result < 0) {
711 /* Something went wrong! */
712 return fit_img_result;
713 }
714
715 if (!fpga_is_partial_data(devnum, img_len)) {
716 name = "full";
717 err = fpga_loadbitstream(devnum, (char *)img_data,
718 img_len, BIT_FULL);
719 if (err)
720 err = fpga_load(devnum, (const void *)img_data,
282eed50 721 img_len, BIT_FULL, 0);
41506ff5
SG
722 } else {
723 name = "partial";
724 err = fpga_loadbitstream(devnum, (char *)img_data,
725 img_len, BIT_PARTIAL);
726 if (err)
727 err = fpga_load(devnum, (const void *)img_data,
282eed50 728 img_len, BIT_PARTIAL, 0);
41506ff5
SG
729 }
730
731 if (err)
732 return err;
733
734 printf(" Programming %s bitstream... OK\n", name);
735 break;
736 default:
737 printf("The given image format is not supported (corrupt?)\n");
738 return 1;
739 }
740
741 return 0;
742}
41506ff5 743
3d2a47f1 744static void fit_loadable_process(u8 img_type,
41506ff5
SG
745 ulong img_data,
746 ulong img_len)
747{
748 int i;
749 const unsigned int count =
750 ll_entry_count(struct fit_loadable_tbl, fit_loadable);
751 struct fit_loadable_tbl *fit_loadable_handler =
752 ll_entry_start(struct fit_loadable_tbl, fit_loadable);
753 /* For each loadable handler */
754 for (i = 0; i < count; i++, fit_loadable_handler++)
755 /* matching this type */
756 if (fit_loadable_handler->type == img_type)
757 /* call that handler with this image data */
758 fit_loadable_handler->handler(img_data, img_len);
759}
760
d9d7c20b 761int boot_get_loadable(int argc, char *const argv[], struct bootm_headers *images,
3d2a47f1 762 u8 arch, const ulong *ld_start, ulong * const ld_len)
41506ff5
SG
763{
764 /*
765 * These variables are used to hold the current image location
766 * in system memory.
767 */
768 ulong tmp_img_addr;
769 /*
770 * These two variables are requirements for fit_image_load, but
771 * their values are not used
772 */
773 ulong img_data, img_len;
774 void *buf;
775 int loadables_index;
776 int conf_noffset;
777 int fit_img_result;
778 const char *uname;
3d2a47f1 779 u8 img_type;
41506ff5
SG
780
781 /* Check to see if the images struct has a FIT configuration */
782 if (!genimg_has_config(images)) {
783 debug("## FIT configuration was not specified\n");
784 return 0;
785 }
786
787 /*
788 * Obtain the os FIT header from the images struct
789 */
790 tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
791 buf = map_sysmem(tmp_img_addr, 0);
792 /*
793 * Check image type. For FIT images get FIT node
794 * and attempt to locate a generic binary.
795 */
796 switch (genimg_get_format(buf)) {
797 case IMAGE_FORMAT_FIT:
798 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
799
800 for (loadables_index = 0;
801 uname = fdt_stringlist_get(buf, conf_noffset,
3d2a47f1
SG
802 FIT_LOADABLE_PROP,
803 loadables_index, NULL), uname;
804 loadables_index++) {
805 fit_img_result = fit_image_load(images, tmp_img_addr,
806 &uname,
807 &images->fit_uname_cfg,
808 arch, IH_TYPE_LOADABLE,
809 BOOTSTAGE_ID_FIT_LOADABLE_START,
810 FIT_LOAD_OPTIONAL_NON_ZERO,
811 &img_data, &img_len);
41506ff5
SG
812 if (fit_img_result < 0) {
813 /* Something went wrong! */
814 return fit_img_result;
815 }
816
817 fit_img_result = fit_image_get_node(buf, uname);
818 if (fit_img_result < 0) {
819 /* Something went wrong! */
820 return fit_img_result;
821 }
822 fit_img_result = fit_image_get_type(buf,
823 fit_img_result,
824 &img_type);
825 if (fit_img_result < 0) {
826 /* Something went wrong! */
827 return fit_img_result;
828 }
829
830 fit_loadable_process(img_type, img_data, img_len);
831 }
832 break;
833 default:
834 printf("The given image format is not supported (corrupt?)\n");
835 return 1;
836 }
837
838 return 0;
839}
41506ff5 840
41506ff5
SG
841/**
842 * boot_get_cmdline - allocate and initialize kernel cmdline
843 * @lmb: pointer to lmb handle, will be used for memory mgmt
844 * @cmd_start: pointer to a ulong variable, will hold cmdline start
845 * @cmd_end: pointer to a ulong variable, will hold cmdline end
846 *
9c2e9128 847 * This allocates space for kernel command line below
41506ff5
SG
848 * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
849 * variable is present its contents is copied to allocated kernel
850 * command line.
851 *
852 * returns:
853 * 0 - success
854 * -1 - failure
855 */
856int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
857{
9c2e9128 858 int barg;
41506ff5
SG
859 char *cmdline;
860 char *s;
861
9c2e9128
SG
862 /*
863 * Help the compiler detect that this function is only called when
864 * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
865 */
866 if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
867 return 0;
868
869 barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
870 cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
41506ff5 871 env_get_bootm_mapsize() + env_get_bootm_low());
3d2a47f1 872 if (!cmdline)
41506ff5
SG
873 return -1;
874
875 s = env_get("bootargs");
876 if (!s)
877 s = "";
878
879 strcpy(cmdline, s);
880
3d2a47f1 881 *cmd_start = (ulong)cmdline;
41506ff5
SG
882 *cmd_end = *cmd_start + strlen(cmdline);
883
884 debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
885
886 return 0;
887}
41506ff5 888
41506ff5
SG
889/**
890 * boot_get_kbd - allocate and initialize kernel copy of board info
891 * @lmb: pointer to lmb handle, will be used for memory mgmt
892 * @kbd: double pointer to board info data
893 *
894 * boot_get_kbd() allocates space for kernel copy of board info data below
895 * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
896 * with the current u-boot board info data.
897 *
898 * returns:
899 * 0 - success
900 * -1 - failure
901 */
902int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
903{
904 *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
905 sizeof(struct bd_info),
906 0xf,
3d2a47f1
SG
907 env_get_bootm_mapsize() +
908 env_get_bootm_low());
909 if (!*kbd)
41506ff5
SG
910 return -1;
911
3d2a47f1 912 **kbd = *gd->bd;
41506ff5
SG
913
914 debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
915
9c2e9128 916 if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
4ed37abc 917 do_bdinfo(NULL, 0, 0, NULL);
41506ff5
SG
918
919 return 0;
920}
41506ff5 921
d9d7c20b 922int image_setup_linux(struct bootm_headers *images)
41506ff5
SG
923{
924 ulong of_size = images->ft_len;
925 char **of_flat_tree = &images->ft_addr;
9c2e9128 926 struct lmb *lmb = images_lmb(images);
41506ff5
SG
927 int ret;
928
9c2e9128
SG
929 /* This function cannot be called without lmb support */
930 if (!CONFIG_IS_ENABLED(LMB))
931 return -EFAULT;
0c303f9a 932 if (CONFIG_IS_ENABLED(OF_LIBFDT))
41506ff5
SG
933 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
934
806d1ff3 935 if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
41506ff5 936 ret = boot_get_cmdline(lmb, &images->cmdline_start,
3d2a47f1 937 &images->cmdline_end);
41506ff5
SG
938 if (ret) {
939 puts("ERROR with allocation of cmdline\n");
940 return ret;
941 }
942 }
943
0c303f9a 944 if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
41506ff5
SG
945 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
946 if (ret)
947 return ret;
948 }
949
0c303f9a 950 if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
41506ff5
SG
951 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
952 if (ret)
953 return ret;
954 }
955
956 return 0;
957}
5d3248a6
SG
958
959void genimg_print_size(uint32_t size)
960{
961 printf("%d Bytes = ", size);
962 print_size(size, "\n");
963}
964
965void genimg_print_time(time_t timestamp)
966{
967 struct rtc_time tm;
968
969 rtc_to_tm(timestamp, &tm);
970 printf("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
971 tm.tm_year, tm.tm_mon, tm.tm_mday,
972 tm.tm_hour, tm.tm_min, tm.tm_sec);
973}
30f3333d
SG
974
975/**
976 * get_default_image() - Return default property from /images
977 *
978 * Return: Pointer to value of default property (or NULL)
979 */
980static const char *get_default_image(const void *fit)
981{
982 int images_noffset;
983
984 images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
985 if (images_noffset < 0)
986 return NULL;
987
988 return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
989}
990
991int image_locate_script(void *buf, int size, const char *fit_uname,
992 const char *confname, char **datap, uint *lenp)
993{
994 const struct legacy_img_hdr *hdr;
995 const void *fit_data;
996 const void *fit_hdr;
997 size_t fit_len;
998 int noffset;
999 int verify;
1000 ulong len;
1001 u32 *data;
1002
1003 verify = env_get_yesno("verify");
1004
1005 switch (genimg_get_format(buf)) {
1006 case IMAGE_FORMAT_LEGACY:
1007 if (IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
1008 hdr = buf;
1009
1010 if (!image_check_magic(hdr)) {
1011 puts("Bad magic number\n");
1012 return 1;
1013 }
1014
1015 if (!image_check_hcrc(hdr)) {
1016 puts("Bad header crc\n");
1017 return 1;
1018 }
1019
1020 if (verify) {
1021 if (!image_check_dcrc(hdr)) {
1022 puts("Bad data crc\n");
1023 return 1;
1024 }
1025 }
1026
1027 if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
1028 puts("Bad image type\n");
1029 return 1;
1030 }
1031
1032 /* get length of script */
1033 data = (u32 *)image_get_data(hdr);
1034
1035 len = uimage_to_cpu(*data);
1036 if (!len) {
1037 puts("Empty Script\n");
1038 return 1;
1039 }
1040
1041 /*
1042 * scripts are just multi-image files with one
1043 * component, so seek past the zero-terminated sequence
1044 * of image lengths to get to the actual image data
1045 */
1046 while (*data++);
1047 }
1048 break;
1049 case IMAGE_FORMAT_FIT:
1050 if (IS_ENABLED(CONFIG_FIT)) {
1051 fit_hdr = buf;
1052 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1053 puts("Bad FIT image format\n");
1054 return 1;
1055 }
1056
1057 if (!fit_uname) {
1058 /* If confname is empty, use the default */
1059 if (confname && *confname)
1060 noffset = fit_conf_get_node(fit_hdr, confname);
1061 else
1062 noffset = fit_conf_get_node(fit_hdr, NULL);
1063 if (noffset < 0) {
1064 if (!confname)
1065 goto fallback;
1066 printf("Could not find config %s\n", confname);
1067 return 1;
1068 }
1069
1070 if (verify && fit_config_verify(fit_hdr, noffset))
1071 return 1;
1072
1073 noffset = fit_conf_get_prop_node(fit_hdr,
1074 noffset,
1075 FIT_SCRIPT_PROP,
1076 IH_PHASE_NONE);
1077 if (noffset < 0) {
1078 if (!confname)
1079 goto fallback;
1080 printf("Could not find script in %s\n", confname);
1081 return 1;
1082 }
1083 } else {
1084fallback:
1085 if (!fit_uname || !*fit_uname)
1086 fit_uname = get_default_image(fit_hdr);
1087 if (!fit_uname) {
1088 puts("No FIT subimage unit name\n");
1089 return 1;
1090 }
1091
1092 /* get script component image node offset */
1093 noffset = fit_image_get_node(fit_hdr, fit_uname);
1094 if (noffset < 0) {
1095 printf("Can't find '%s' FIT subimage\n",
1096 fit_uname);
1097 return 1;
1098 }
1099 }
1100
1101 if (!fit_image_check_type(fit_hdr, noffset,
1102 IH_TYPE_SCRIPT)) {
1103 puts("Not a image image\n");
1104 return 1;
1105 }
1106
1107 /* verify integrity */
1108 if (verify && !fit_image_verify(fit_hdr, noffset)) {
1109 puts("Bad Data Hash\n");
1110 return 1;
1111 }
1112
1113 /* get script subimage data address and length */
1114 if (fit_image_get_data(fit_hdr, noffset, &fit_data, &fit_len)) {
1115 puts("Could not find script subimage data\n");
1116 return 1;
1117 }
1118
1119 data = (u32 *)fit_data;
1120 len = (ulong)fit_len;
1121 }
1122 break;
1123 default:
1124 puts("Wrong image format for \"source\" command\n");
1125 return -EPERM;
1126 }
1127
1128 *datap = (char *)data;
1129 *lenp = len;
1130
1131 return 0;
1132}
This page took 0.227664 seconds and 4 git commands to generate.