blob: 85af9805af0f7a8c6babe2030fb2f17f429bd305 [file] [log] [blame]
Simon Glassf1dcee52016-02-22 22:55:56 -07001/*
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
14static 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
Andre Przywara4b9340a2017-04-26 01:32:33 +010025/*
26 * Iterate over all /configurations subnodes and call a platform specific
27 * function to find the matching configuration.
28 * Returns the node offset or a negative error number.
29 */
30static int spl_fit_find_config_node(const void *fdt)
Simon Glassf1dcee52016-02-22 22:55:56 -070031{
Andre Przywara4b9340a2017-04-26 01:32:33 +010032 const char *name;
33 int conf, node, len;
Simon Glassf1dcee52016-02-22 22:55:56 -070034
Simon Glassf1dcee52016-02-22 22:55:56 -070035 conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
36 if (conf < 0) {
37 debug("%s: Cannot find /configurations node: %d\n", __func__,
38 conf);
39 return -EINVAL;
40 }
41 for (node = fdt_first_subnode(fdt, conf);
42 node >= 0;
43 node = fdt_next_subnode(fdt, node)) {
44 name = fdt_getprop(fdt, node, "description", &len);
Michal Simek5adfa262016-05-04 15:08:00 +020045 if (!name) {
46#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
47 printf("%s: Missing FDT description in DTB\n",
48 __func__);
49#endif
Simon Glassf1dcee52016-02-22 22:55:56 -070050 return -EINVAL;
Michal Simek5adfa262016-05-04 15:08:00 +020051 }
Simon Glassf1dcee52016-02-22 22:55:56 -070052 if (board_fit_config_name_match(name))
53 continue;
54
55 debug("Selecting config '%s'", name);
Simon Glassf1dcee52016-02-22 22:55:56 -070056
Andre Przywara4b9340a2017-04-26 01:32:33 +010057 return node;
Simon Glassf1dcee52016-02-22 22:55:56 -070058 }
59
Simon Glassf1dcee52016-02-22 22:55:56 -070060 return -ENOENT;
61}
62
Andre Przywara736806f2017-04-26 01:32:34 +010063/**
64 * spl_fit_get_image_node(): By using the matching configuration subnode,
65 * retrieve the name of an image, specified by a property name and an index
66 * into that.
67 * @fit: Pointer to the FDT blob.
68 * @images: Offset of the /images subnode.
69 * @type: Name of the property within the configuration subnode.
70 * @index: Index into the list of strings in this property.
71 *
72 * Return: the node offset of the respective image node or a negative
73 * error number.
74 */
75static int spl_fit_get_image_node(const void *fit, int images,
76 const char *type, int index)
Andre Przywara4b9340a2017-04-26 01:32:33 +010077{
78 const char *name, *str;
79 int node, conf_node;
80 int len, i;
81
Andre Przywara4b9340a2017-04-26 01:32:33 +010082 conf_node = spl_fit_find_config_node(fit);
83 if (conf_node < 0) {
84#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
85 printf("No matching DT out of these options:\n");
86 for (node = fdt_first_subnode(fit, conf_node);
87 node >= 0;
88 node = fdt_next_subnode(fit, node)) {
89 name = fdt_getprop(fit, node, "description", &len);
90 printf(" %s\n", name);
91 }
92#endif
93 return conf_node;
94 }
95
96 name = fdt_getprop(fit, conf_node, type, &len);
97 if (!name) {
98 debug("cannot find property '%s': %d\n", type, len);
99 return -EINVAL;
100 }
101
102 str = name;
103 for (i = 0; i < index; i++) {
104 str = strchr(str, '\0') + 1;
105 if (!str || (str - name >= len)) {
106 debug("no string for index %d\n", index);
107 return -E2BIG;
108 }
109 }
110
111 debug("%s: '%s'\n", type, str);
112 node = fdt_subnode_offset(fit, images, str);
113 if (node < 0) {
114 debug("cannot find image node '%s': %d\n", str, node);
115 return -EINVAL;
116 }
117
Andre Przywara736806f2017-04-26 01:32:34 +0100118 return node;
Andre Przywara4b9340a2017-04-26 01:32:33 +0100119}
120
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530121static int get_aligned_image_offset(struct spl_load_info *info, int offset)
122{
123 /*
124 * If it is a FS read, get the first address before offset which is
125 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
126 * block number to which offset belongs.
127 */
128 if (info->filename)
129 return offset & ~(ARCH_DMA_MINALIGN - 1);
130
131 return offset / info->bl_len;
132}
133
134static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
135{
136 /*
137 * If it is a FS read, get the difference between the offset and
138 * the first address before offset which is aligned to
139 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
140 * block.
141 */
142 if (info->filename)
143 return offset & (ARCH_DMA_MINALIGN - 1);
144
145 return offset % info->bl_len;
146}
147
148static int get_aligned_image_size(struct spl_load_info *info, int data_size,
149 int offset)
150{
Lokesh Vutla3cc1f382016-07-19 14:56:14 +0530151 data_size = data_size + get_aligned_image_overhead(info, offset);
152
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530153 if (info->filename)
Lokesh Vutla3cc1f382016-07-19 14:56:14 +0530154 return data_size;
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530155
156 return (data_size + info->bl_len - 1) / info->bl_len;
157}
158
Simon Glassf4d7d852016-09-24 18:20:16 -0600159int spl_load_simple_fit(struct spl_image_info *spl_image,
160 struct spl_load_info *info, ulong sector, void *fit)
Simon Glassf1dcee52016-02-22 22:55:56 -0700161{
162 int sectors;
163 ulong size, load;
164 unsigned long count;
165 int node, images;
166 void *load_ptr;
167 int fdt_offset, fdt_len;
168 int data_offset, data_size;
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530169 int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
Simon Glassf1dcee52016-02-22 22:55:56 -0700170 int src_sector;
Daniel Allredda74d1f2016-06-27 09:19:21 -0500171 void *dst, *src;
Simon Glassf1dcee52016-02-22 22:55:56 -0700172
173 /*
174 * Figure out where the external images start. This is the base for the
175 * data-offset properties in each image.
176 */
177 size = fdt_totalsize(fit);
178 size = (size + 3) & ~3;
179 base_offset = (size + 3) & ~3;
180
181 /*
182 * So far we only have one block of data from the FIT. Read the entire
183 * thing, including that first block, placing it so it finishes before
184 * where we will load the image.
185 *
186 * Note that we will load the image such that its first byte will be
187 * at the load address. Since that byte may be part-way through a
188 * block, we may load the image up to one block before the load
189 * address. So take account of that here by subtracting an addition
190 * block length from the FIT start position.
191 *
192 * In fact the FIT has its own load address, but we assume it cannot
193 * be before CONFIG_SYS_TEXT_BASE.
194 */
Lokesh Vutla8b528702016-06-01 10:28:31 +0530195 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
196 align_len) & ~align_len);
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530197 sectors = get_aligned_image_size(info, size, 0);
Simon Glassf1dcee52016-02-22 22:55:56 -0700198 count = info->read(info, sector, sectors, fit);
199 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
200 sector, sectors, fit, count);
201 if (count == 0)
202 return -EIO;
203
Andre Przywara736806f2017-04-26 01:32:34 +0100204 /* find the node holding the images information */
Simon Glassf1dcee52016-02-22 22:55:56 -0700205 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
206 if (images < 0) {
207 debug("%s: Cannot find /images node: %d\n", __func__, images);
208 return -1;
209 }
Andre Przywara736806f2017-04-26 01:32:34 +0100210
211 /* find the U-Boot image */
212 node = spl_fit_get_image_node(fit, images, "firmware", 0);
Simon Glassf1dcee52016-02-22 22:55:56 -0700213 if (node < 0) {
Andre Przywara736806f2017-04-26 01:32:34 +0100214 debug("could not find firmware image, trying loadables...\n");
215 node = spl_fit_get_image_node(fit, images, "loadables", 0);
216 }
217 if (node < 0) {
218 debug("%s: Cannot find u-boot image node: %d\n",
219 __func__, node);
Simon Glassf1dcee52016-02-22 22:55:56 -0700220 return -1;
221 }
222
223 /* Get its information and set up the spl_image structure */
224 data_offset = fdt_getprop_u32(fit, node, "data-offset");
225 data_size = fdt_getprop_u32(fit, node, "data-size");
226 load = fdt_getprop_u32(fit, node, "load");
227 debug("data_offset=%x, data_size=%x\n", data_offset, data_size);
Simon Glassf4d7d852016-09-24 18:20:16 -0600228 spl_image->load_addr = load;
229 spl_image->entry_point = load;
230 spl_image->os = IH_OS_U_BOOT;
Simon Glassf1dcee52016-02-22 22:55:56 -0700231
232 /*
233 * Work out where to place the image. We read it so that the first
234 * byte will be at 'load'. This may mean we need to load it starting
235 * before then, since we can only read whole blocks.
236 */
Simon Glassf1dcee52016-02-22 22:55:56 -0700237 data_offset += base_offset;
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530238 sectors = get_aligned_image_size(info, data_size, data_offset);
Simon Glassf1dcee52016-02-22 22:55:56 -0700239 load_ptr = (void *)load;
240 debug("U-Boot size %x, data %p\n", data_size, load_ptr);
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530241 dst = load_ptr;
Simon Glassf1dcee52016-02-22 22:55:56 -0700242
243 /* Read the image */
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530244 src_sector = sector + get_aligned_image_offset(info, data_offset);
245 debug("Aligned image read: dst=%p, src_sector=%x, sectors=%x\n",
246 dst, src_sector, sectors);
Simon Glassf1dcee52016-02-22 22:55:56 -0700247 count = info->read(info, src_sector, sectors, dst);
248 if (count != sectors)
249 return -EIO;
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530250 debug("image: dst=%p, data_offset=%x, size=%x\n", dst, data_offset,
251 data_size);
Daniel Allredda74d1f2016-06-27 09:19:21 -0500252 src = dst + get_aligned_image_overhead(info, data_offset);
253
254#ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
255 board_fit_image_post_process((void **)&src, (size_t *)&data_size);
256#endif
257
258 memcpy(dst, src, data_size);
Simon Glassf1dcee52016-02-22 22:55:56 -0700259
260 /* Figure out which device tree the board wants to use */
Andre Przywara736806f2017-04-26 01:32:34 +0100261 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
262 if (node < 0) {
263 debug("%s: cannot find FDT node\n", __func__);
264 return node;
265 }
266 fdt_offset = fdt_getprop_u32(fit, node, "data-offset");
267 fdt_len = fdt_getprop_u32(fit, node, "data-size");
Simon Glassf1dcee52016-02-22 22:55:56 -0700268
269 /*
270 * Read the device tree and place it after the image. There may be
271 * some extra data before it since we can only read entire blocks.
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530272 * And also align the destination address to ARCH_DMA_MINALIGN.
Simon Glassf1dcee52016-02-22 22:55:56 -0700273 */
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530274 dst = (void *)((load + data_size + align_len) & ~align_len);
Simon Glassf1dcee52016-02-22 22:55:56 -0700275 fdt_offset += base_offset;
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530276 sectors = get_aligned_image_size(info, fdt_len, fdt_offset);
277 src_sector = sector + get_aligned_image_offset(info, fdt_offset);
278 count = info->read(info, src_sector, sectors, dst);
279 debug("Aligned fdt read: dst %p, src_sector = %x, sectors %x\n",
280 dst, src_sector, sectors);
Simon Glassf1dcee52016-02-22 22:55:56 -0700281 if (count != sectors)
282 return -EIO;
283
284 /*
285 * Copy the device tree so that it starts immediately after the image.
286 * After this we will have the U-Boot image and its device tree ready
287 * for us to start.
288 */
Lokesh Vutlaeafd5412016-05-24 10:34:38 +0530289 debug("fdt: dst=%p, data_offset=%x, size=%x\n", dst, fdt_offset,
290 fdt_len);
Daniel Allredda74d1f2016-06-27 09:19:21 -0500291 src = dst + get_aligned_image_overhead(info, fdt_offset);
292 dst = load_ptr + data_size;
293
294#ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
295 board_fit_image_post_process((void **)&src, (size_t *)&fdt_len);
296#endif
297
298 memcpy(dst, src, fdt_len);
Simon Glassf1dcee52016-02-22 22:55:56 -0700299
300 return 0;
301}