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