xref: /openbmc/u-boot/common/spl/spl_nor.c (revision 59e43c32)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2012 Stefan Roese <sr@denx.de>
4  */
5 
6 #include <common.h>
7 #include <spl.h>
8 
9 #ifdef CONFIG_SPL_LOAD_FIT
10 static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector,
11 			       ulong count, void *buf)
12 {
13 	debug("%s: sector %lx, count %lx, buf %p\n",
14 	      __func__, sector, count, buf);
15 	memcpy(buf, (void *)sector, count);
16 
17 	return count;
18 }
19 #endif
20 
21 static int spl_nor_load_image(struct spl_image_info *spl_image,
22 			      struct spl_boot_device *bootdev)
23 {
24 	int ret;
25 	__maybe_unused const struct image_header *header;
26 	__maybe_unused struct spl_load_info load;
27 
28 	/*
29 	 * Loading of the payload to SDRAM is done with skipping of
30 	 * the mkimage header in this SPL NOR driver
31 	 */
32 	spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
33 
34 #ifdef CONFIG_SPL_OS_BOOT
35 	if (!spl_start_uboot()) {
36 		/*
37 		 * Load Linux from its location in NOR flash to its defined
38 		 * location in SDRAM
39 		 */
40 		header = (const struct image_header *)CONFIG_SYS_OS_BASE;
41 #ifdef CONFIG_SPL_LOAD_FIT
42 		if (image_get_magic(header) == FDT_MAGIC) {
43 			debug("Found FIT\n");
44 			load.bl_len = 1;
45 			load.read = spl_nor_load_read;
46 
47 			ret = spl_load_simple_fit(spl_image, &load,
48 						  CONFIG_SYS_OS_BASE,
49 						  (void *)header);
50 
51 			return ret;
52 		}
53 #endif
54 		if (image_get_os(header) == IH_OS_LINUX) {
55 			/* happy - was a Linux */
56 
57 			ret = spl_parse_image_header(spl_image, header);
58 			if (ret)
59 				return ret;
60 
61 			memcpy((void *)spl_image->load_addr,
62 			       (void *)(CONFIG_SYS_OS_BASE +
63 					sizeof(struct image_header)),
64 			       spl_image->size);
65 #ifdef CONFIG_SYS_FDT_BASE
66 			spl_image->arg = (void *)CONFIG_SYS_FDT_BASE;
67 #endif
68 
69 			return 0;
70 		} else {
71 			puts("The Expected Linux image was not found.\n"
72 			     "Please check your NOR configuration.\n"
73 			     "Trying to start u-boot now...\n");
74 		}
75 	}
76 #endif
77 
78 	/*
79 	 * Load real U-Boot from its location in NOR flash to its
80 	 * defined location in SDRAM
81 	 */
82 #ifdef CONFIG_SPL_LOAD_FIT
83 	header = (const struct image_header *)CONFIG_SYS_UBOOT_BASE;
84 	if (image_get_magic(header) == FDT_MAGIC) {
85 		debug("Found FIT format U-Boot\n");
86 		load.bl_len = 1;
87 		load.read = spl_nor_load_read;
88 		ret = spl_load_simple_fit(spl_image, &load,
89 					  CONFIG_SYS_UBOOT_BASE,
90 					  (void *)header);
91 
92 		return ret;
93 	}
94 #endif
95 	ret = spl_parse_image_header(spl_image,
96 			(const struct image_header *)CONFIG_SYS_UBOOT_BASE);
97 	if (ret)
98 		return ret;
99 
100 	memcpy((void *)(unsigned long)spl_image->load_addr,
101 	       (void *)(CONFIG_SYS_UBOOT_BASE + sizeof(struct image_header)),
102 	       spl_image->size);
103 
104 	return 0;
105 }
106 SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image);
107