xref: /openbmc/u-boot/common/spl/spl_fit.c (revision ad5b5801)
1 /*
2  * Copyright (C) 2016 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <errno.h>
10 #include <image.h>
11 #include <libfdt.h>
12 #include <spl.h>
13 
14 static ulong fdt_getprop_u32(const void *fdt, int node, const char *prop)
15 {
16 	const u32 *cell;
17 	int len;
18 
19 	cell = fdt_getprop(fdt, node, prop, &len);
20 	if (len != sizeof(*cell))
21 		return -1U;
22 	return fdt32_to_cpu(*cell);
23 }
24 
25 static int spl_fit_select_fdt(const void *fdt, int images, int *fdt_offsetp)
26 {
27 	const char *name, *fdt_name;
28 	int conf, node, fdt_node;
29 	int len;
30 
31 	*fdt_offsetp = 0;
32 	conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
33 	if (conf < 0) {
34 		debug("%s: Cannot find /configurations node: %d\n", __func__,
35 		      conf);
36 		return -EINVAL;
37 	}
38 	for (node = fdt_first_subnode(fdt, conf);
39 	     node >= 0;
40 	     node = fdt_next_subnode(fdt, node)) {
41 		name = fdt_getprop(fdt, node, "description", &len);
42 		if (!name) {
43 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
44 			printf("%s: Missing FDT description in DTB\n",
45 			       __func__);
46 #endif
47 			return -EINVAL;
48 		}
49 		if (board_fit_config_name_match(name))
50 			continue;
51 
52 		debug("Selecting config '%s'", name);
53 		fdt_name = fdt_getprop(fdt, node, FIT_FDT_PROP, &len);
54 		if (!fdt_name) {
55 			debug("%s: Cannot find fdt name property: %d\n",
56 			      __func__, len);
57 			return -EINVAL;
58 		}
59 
60 		debug(", fdt '%s'\n", fdt_name);
61 		fdt_node = fdt_subnode_offset(fdt, images, fdt_name);
62 		if (fdt_node < 0) {
63 			debug("%s: Cannot find fdt node '%s': %d\n",
64 			      __func__, fdt_name, fdt_node);
65 			return -EINVAL;
66 		}
67 
68 		*fdt_offsetp = fdt_getprop_u32(fdt, fdt_node, "data-offset");
69 		len = fdt_getprop_u32(fdt, fdt_node, "data-size");
70 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
71 		printf("FIT: Selected '%s'\n", name);
72 #endif
73 
74 		return len;
75 	}
76 
77 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
78 	printf("No matching DT out of these options:\n");
79 	for (node = fdt_first_subnode(fdt, conf);
80 	     node >= 0;
81 	     node = fdt_next_subnode(fdt, node)) {
82 		name = fdt_getprop(fdt, node, "name", &len);
83 		printf("   %s\n", name);
84 	}
85 #endif
86 
87 	return -ENOENT;
88 }
89 
90 int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit)
91 {
92 	int sectors;
93 	ulong size, load;
94 	unsigned long count;
95 	int node, images;
96 	void *load_ptr;
97 	int fdt_offset, fdt_len;
98 	int data_offset, data_size;
99 	int base_offset;
100 	int src_sector;
101 	void *dst;
102 
103 	/*
104 	 * Figure out where the external images start. This is the base for the
105 	 * data-offset properties in each image.
106 	 */
107 	size = fdt_totalsize(fit);
108 	size = (size + 3) & ~3;
109 	base_offset = (size + 3) & ~3;
110 
111 	/*
112 	 * So far we only have one block of data from the FIT. Read the entire
113 	 * thing, including that first block, placing it so it finishes before
114 	 * where we will load the image.
115 	 *
116 	 * Note that we will load the image such that its first byte will be
117 	 * at the load address. Since that byte may be part-way through a
118 	 * block, we may load the image up to one block before the load
119 	 * address. So take account of that here by subtracting an addition
120 	 * block length from the FIT start position.
121 	 *
122 	 * In fact the FIT has its own load address, but we assume it cannot
123 	 * be before CONFIG_SYS_TEXT_BASE.
124 	 */
125 	fit = (void *)(CONFIG_SYS_TEXT_BASE - size - info->bl_len);
126 	sectors = (size + info->bl_len - 1) / info->bl_len;
127 	count = info->read(info, sector, sectors, fit);
128 	debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
129 	      sector, sectors, fit, count);
130 	if (count == 0)
131 		return -EIO;
132 
133 	/* find the firmware image to load */
134 	images = fdt_path_offset(fit, FIT_IMAGES_PATH);
135 	if (images < 0) {
136 		debug("%s: Cannot find /images node: %d\n", __func__, images);
137 		return -1;
138 	}
139 	node = fdt_first_subnode(fit, images);
140 	if (node < 0) {
141 		debug("%s: Cannot find first image node: %d\n", __func__, node);
142 		return -1;
143 	}
144 
145 	/* Get its information and set up the spl_image structure */
146 	data_offset = fdt_getprop_u32(fit, node, "data-offset");
147 	data_size = fdt_getprop_u32(fit, node, "data-size");
148 	load = fdt_getprop_u32(fit, node, "load");
149 	debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
150 	spl_image.load_addr = load;
151 	spl_image.entry_point = load;
152 	spl_image.os = IH_OS_U_BOOT;
153 
154 	/*
155 	 * Work out where to place the image. We read it so that the first
156 	 * byte will be at 'load'. This may mean we need to load it starting
157 	 * before then, since we can only read whole blocks.
158 	 */
159 	sectors = (data_size + info->bl_len - 1) / info->bl_len;
160 	data_offset += base_offset;
161 	load_ptr = (void *)load;
162 	debug("U-Boot size %x, data %p\n", data_size, load_ptr);
163 	dst = load_ptr - (data_offset % info->bl_len);
164 
165 	/* Read the image */
166 	src_sector = sector + data_offset / info->bl_len;
167 	debug("image: data_offset=%x, dst=%p, src_sector=%x, sectors=%x\n",
168 	      data_offset, dst, src_sector, sectors);
169 	count = info->read(info, src_sector, sectors, dst);
170 	if (count != sectors)
171 		return -EIO;
172 
173 	/* Figure out which device tree the board wants to use */
174 	fdt_len = spl_fit_select_fdt(fit, images, &fdt_offset);
175 	if (fdt_len < 0)
176 		return fdt_len;
177 
178 	/*
179 	 * Read the device tree and place it after the image. There may be
180 	 * some extra data before it since we can only read entire blocks.
181 	 */
182 	dst = load_ptr + data_size;
183 	fdt_offset += base_offset;
184 	count = info->read(info, sector + fdt_offset / info->bl_len, sectors,
185 			   dst);
186 	debug("fit read %x sectors to %x, dst %p, data_offset %x\n",
187 	      sectors, spl_image.load_addr, dst, fdt_offset);
188 	if (count != sectors)
189 		return -EIO;
190 
191 	/*
192 	 * Copy the device tree so that it starts immediately after the image.
193 	 * After this we will have the U-Boot image and its device tree ready
194 	 * for us to start.
195 	 */
196 	memcpy(dst, dst + fdt_offset % info->bl_len, fdt_len);
197 
198 	return 0;
199 }
200