blob: 49ccf1c17bdaf56b429bfbf90fc21de82bfbaf58 [file] [log] [blame]
Simon Glassa6131a22016-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
York Suna6945fe2017-08-15 11:14:43 -070014#ifndef CONFIG_SYS_BOOTM_LEN
15#define CONFIG_SYS_BOOTM_LEN (64 << 20)
16#endif
17
Andre Przywara525e9c92017-04-26 01:32:34 +010018/**
19 * spl_fit_get_image_node(): By using the matching configuration subnode,
20 * retrieve the name of an image, specified by a property name and an index
21 * into that.
22 * @fit: Pointer to the FDT blob.
23 * @images: Offset of the /images subnode.
24 * @type: Name of the property within the configuration subnode.
25 * @index: Index into the list of strings in this property.
26 *
27 * Return: the node offset of the respective image node or a negative
28 * error number.
29 */
30static int spl_fit_get_image_node(const void *fit, int images,
31 const char *type, int index)
Andre Przywara1e329b62017-04-26 01:32:33 +010032{
33 const char *name, *str;
34 int node, conf_node;
35 int len, i;
36
Cooper Jr., Frankline6792402017-06-16 17:25:05 -050037 conf_node = fit_find_config_node(fit);
Andre Przywara1e329b62017-04-26 01:32:33 +010038 if (conf_node < 0) {
39#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
40 printf("No matching DT out of these options:\n");
41 for (node = fdt_first_subnode(fit, conf_node);
42 node >= 0;
43 node = fdt_next_subnode(fit, node)) {
44 name = fdt_getprop(fit, node, "description", &len);
45 printf(" %s\n", name);
Simon Glassa6131a22016-02-22 22:55:56 -070046 }
Andre Przywara1e329b62017-04-26 01:32:33 +010047#endif
48 return conf_node;
49 }
Simon Glassa6131a22016-02-22 22:55:56 -070050
Andre Przywara1e329b62017-04-26 01:32:33 +010051 name = fdt_getprop(fit, conf_node, type, &len);
52 if (!name) {
53 debug("cannot find property '%s': %d\n", type, len);
54 return -EINVAL;
55 }
Simon Glassa6131a22016-02-22 22:55:56 -070056
Andre Przywara1e329b62017-04-26 01:32:33 +010057 str = name;
58 for (i = 0; i < index; i++) {
59 str = strchr(str, '\0') + 1;
60 if (!str || (str - name >= len)) {
61 debug("no string for index %d\n", index);
62 return -E2BIG;
63 }
Simon Glassa6131a22016-02-22 22:55:56 -070064 }
65
Andre Przywara1e329b62017-04-26 01:32:33 +010066 debug("%s: '%s'\n", type, str);
67 node = fdt_subnode_offset(fit, images, str);
68 if (node < 0) {
69 debug("cannot find image node '%s': %d\n", str, node);
70 return -EINVAL;
Simon Glassa6131a22016-02-22 22:55:56 -070071 }
Simon Glassa6131a22016-02-22 22:55:56 -070072
Andre Przywara525e9c92017-04-26 01:32:34 +010073 return node;
Simon Glassa6131a22016-02-22 22:55:56 -070074}
75
Lokesh Vutlaf62cef12016-05-24 10:34:38 +053076static int get_aligned_image_offset(struct spl_load_info *info, int offset)
77{
78 /*
79 * If it is a FS read, get the first address before offset which is
80 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
81 * block number to which offset belongs.
82 */
83 if (info->filename)
84 return offset & ~(ARCH_DMA_MINALIGN - 1);
85
86 return offset / info->bl_len;
87}
88
89static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
90{
91 /*
92 * If it is a FS read, get the difference between the offset and
93 * the first address before offset which is aligned to
94 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
95 * block.
96 */
97 if (info->filename)
98 return offset & (ARCH_DMA_MINALIGN - 1);
99
100 return offset % info->bl_len;
101}
102
103static int get_aligned_image_size(struct spl_load_info *info, int data_size,
104 int offset)
105{
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530106 data_size = data_size + get_aligned_image_overhead(info, offset);
107
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530108 if (info->filename)
Lokesh Vutlaf0531a62016-07-19 14:56:14 +0530109 return data_size;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530110
111 return (data_size + info->bl_len - 1) / info->bl_len;
112}
113
Andre Przywara29053e92017-04-26 01:32:36 +0100114/**
115 * spl_load_fit_image(): load the image described in a certain FIT node
116 * @info: points to information about the device to load data from
117 * @sector: the start sector of the FIT image on the device
118 * @fit: points to the flattened device tree blob describing the FIT
119 * image
120 * @base_offset: the beginning of the data area containing the actual
121 * image data, relative to the beginning of the FIT
122 * @node: offset of the DT node describing the image to load (relative
123 * to @fit)
124 * @image_info: will be filled with information about the loaded image
125 * If the FIT node does not contain a "load" (address) property,
126 * the image gets loaded to the address pointed to by the
127 * load_addr member in this struct.
128 *
129 * Return: 0 on success or a negative error number.
130 */
131static int spl_load_fit_image(struct spl_load_info *info, ulong sector,
132 void *fit, ulong base_offset, int node,
133 struct spl_image_info *image_info)
134{
York Sunadf99fa2017-08-15 11:14:44 -0700135 int offset;
Andre Przywara29053e92017-04-26 01:32:36 +0100136 size_t length;
York Sunadf99fa2017-08-15 11:14:44 -0700137 int len;
Andre Przywara29053e92017-04-26 01:32:36 +0100138 ulong load_addr, load_ptr;
139 void *src;
140 ulong overhead;
141 int nr_sectors;
142 int align_len = ARCH_DMA_MINALIGN - 1;
York Suna6945fe2017-08-15 11:14:43 -0700143 uint8_t image_comp = -1, type = -1;
York Sunadf99fa2017-08-15 11:14:44 -0700144 const void *data;
York Suna6945fe2017-08-15 11:14:43 -0700145
146 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) {
147 if (fit_image_get_comp(fit, node, &image_comp))
148 puts("Cannot get image compression format.\n");
149 else
150 debug("%s ", genimg_get_comp_name(image_comp));
151
152 if (fit_image_get_type(fit, node, &type))
153 puts("Cannot get image type.\n");
154 else
155 debug("%s ", genimg_get_type_name(type));
156 }
Andre Przywara29053e92017-04-26 01:32:36 +0100157
York Sunadf99fa2017-08-15 11:14:44 -0700158 if (fit_image_get_load(fit, node, &load_addr))
Andre Przywara29053e92017-04-26 01:32:36 +0100159 load_addr = image_info->load_addr;
Andre Przywara29053e92017-04-26 01:32:36 +0100160
York Sunadf99fa2017-08-15 11:14:44 -0700161 if (!fit_image_get_data_offset(fit, node, &offset)) {
162 /* External data */
163 offset += base_offset;
164 if (fit_image_get_data_size(fit, node, &len))
165 return -ENOENT;
Andre Przywara29053e92017-04-26 01:32:36 +0100166
York Sunadf99fa2017-08-15 11:14:44 -0700167 load_ptr = (load_addr + align_len) & ~align_len;
168 length = len;
169
170 overhead = get_aligned_image_overhead(info, offset);
171 nr_sectors = get_aligned_image_size(info, length, offset);
172
173 if (info->read(info,
174 sector + get_aligned_image_offset(info, offset),
175 nr_sectors, (void *)load_ptr) != nr_sectors)
176 return -EIO;
177
178 debug("External data: dst=%lx, offset=%x, size=%lx\n",
179 load_ptr, offset, (unsigned long)length);
180 src = (void *)load_ptr + overhead;
181 } else {
182 /* Embedded data */
183 if (fit_image_get_data(fit, node, &data, &length)) {
184 puts("Cannot get image data/size\n");
185 return -ENOENT;
186 }
187 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
188 (unsigned long)length);
189 src = (void *)data;
190 }
Andre Przywara29053e92017-04-26 01:32:36 +0100191
Andre Przywara29053e92017-04-26 01:32:36 +0100192#ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
193 board_fit_image_post_process(&src, &length);
194#endif
195
York Suna6945fe2017-08-15 11:14:43 -0700196 if (IS_ENABLED(CONFIG_SPL_OS_BOOT) &&
197 IS_ENABLED(CONFIG_SPL_GZIP) &&
198 image_comp == IH_COMP_GZIP &&
199 type == IH_TYPE_KERNEL) {
200 if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN,
201 src, &length)) {
202 puts("Uncompressing error\n");
203 return -EIO;
204 }
205 } else {
206 memcpy((void *)load_addr, src, length);
207 }
Andre Przywara29053e92017-04-26 01:32:36 +0100208
209 if (image_info) {
210 image_info->load_addr = load_addr;
211 image_info->size = length;
212 image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
213 }
214
215 return 0;
216}
217
Simon Glass43a734f2016-09-24 18:20:16 -0600218int spl_load_simple_fit(struct spl_image_info *spl_image,
219 struct spl_load_info *info, ulong sector, void *fit)
Simon Glassa6131a22016-02-22 22:55:56 -0700220{
221 int sectors;
Andre Przywara29053e92017-04-26 01:32:36 +0100222 ulong size;
Simon Glassa6131a22016-02-22 22:55:56 -0700223 unsigned long count;
Andre Przywara29053e92017-04-26 01:32:36 +0100224 struct spl_image_info image_info;
York Suna058b8a2017-08-15 11:14:45 -0700225 bool boot_os = false;
226 int node = -1;
227 int images, ret;
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530228 int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
Andre Przywarae7c58562017-04-26 01:32:37 +0100229 int index = 0;
Simon Glassa6131a22016-02-22 22:55:56 -0700230
231 /*
York Suna058b8a2017-08-15 11:14:45 -0700232 * For FIT with external data, figure out where the external images
233 * start. This is the base for the data-offset properties in each
234 * image.
Simon Glassa6131a22016-02-22 22:55:56 -0700235 */
236 size = fdt_totalsize(fit);
237 size = (size + 3) & ~3;
238 base_offset = (size + 3) & ~3;
239
240 /*
241 * So far we only have one block of data from the FIT. Read the entire
242 * thing, including that first block, placing it so it finishes before
243 * where we will load the image.
244 *
245 * Note that we will load the image such that its first byte will be
246 * at the load address. Since that byte may be part-way through a
247 * block, we may load the image up to one block before the load
248 * address. So take account of that here by subtracting an addition
249 * block length from the FIT start position.
250 *
251 * In fact the FIT has its own load address, but we assume it cannot
252 * be before CONFIG_SYS_TEXT_BASE.
York Suna058b8a2017-08-15 11:14:45 -0700253 *
254 * For FIT with data embedded, data is loaded as part of FIT image.
255 * For FIT with external data, data is not loaded in this step.
Simon Glassa6131a22016-02-22 22:55:56 -0700256 */
Lokesh Vutlac8405852016-06-01 10:28:31 +0530257 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
258 align_len) & ~align_len);
Lokesh Vutlaf62cef12016-05-24 10:34:38 +0530259 sectors = get_aligned_image_size(info, size, 0);
Simon Glassa6131a22016-02-22 22:55:56 -0700260 count = info->read(info, sector, sectors, fit);
261 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
262 sector, sectors, fit, count);
263 if (count == 0)
264 return -EIO;
265
Andre Przywara525e9c92017-04-26 01:32:34 +0100266 /* find the node holding the images information */
Simon Glassa6131a22016-02-22 22:55:56 -0700267 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
268 if (images < 0) {
269 debug("%s: Cannot find /images node: %d\n", __func__, images);
270 return -1;
271 }
Andre Przywara525e9c92017-04-26 01:32:34 +0100272
York Suna058b8a2017-08-15 11:14:45 -0700273#ifdef CONFIG_SPL_OS_BOOT
274 /* Find OS image first */
275 node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0);
276 if (node < 0)
277 debug("No kernel image.\n");
278 else
279 boot_os = true;
280#endif
Andre Przywara525e9c92017-04-26 01:32:34 +0100281 /* find the U-Boot image */
York Suna058b8a2017-08-15 11:14:45 -0700282 if (node < 0)
283 node = spl_fit_get_image_node(fit, images, "firmware", 0);
Andre Przywara525e9c92017-04-26 01:32:34 +0100284 if (node < 0) {
285 debug("could not find firmware image, trying loadables...\n");
286 node = spl_fit_get_image_node(fit, images, "loadables", 0);
Andre Przywarae7c58562017-04-26 01:32:37 +0100287 /*
288 * If we pick the U-Boot image from "loadables", start at
289 * the second image when later loading additional images.
290 */
291 index = 1;
Andre Przywara525e9c92017-04-26 01:32:34 +0100292 }
Simon Glassa6131a22016-02-22 22:55:56 -0700293 if (node < 0) {
Andre Przywara525e9c92017-04-26 01:32:34 +0100294 debug("%s: Cannot find u-boot image node: %d\n",
295 __func__, node);
Simon Glassa6131a22016-02-22 22:55:56 -0700296 return -1;
297 }
298
Andre Przywara29053e92017-04-26 01:32:36 +0100299 /* Load the image and set up the spl_image structure */
300 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
301 spl_image);
302 if (ret)
303 return ret;
Daniel Allredcf839bd2016-06-27 09:19:21 -0500304
York Suna058b8a2017-08-15 11:14:45 -0700305#ifdef CONFIG_SPL_OS_BOOT
306 if (!fit_image_get_os(fit, node, &spl_image->os))
307 debug("Image OS is %s\n", genimg_get_os_name(spl_image->os));
308#else
Andre Przywara29053e92017-04-26 01:32:36 +0100309 spl_image->os = IH_OS_U_BOOT;
York Suna058b8a2017-08-15 11:14:45 -0700310#endif
Simon Glassa6131a22016-02-22 22:55:56 -0700311
York Suna058b8a2017-08-15 11:14:45 -0700312 if (!boot_os) {
313 /* Figure out which device tree the board wants to use */
314 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
315 if (node < 0) {
316 debug("%s: cannot find FDT node\n", __func__);
317 return node;
318 }
Simon Glassa6131a22016-02-22 22:55:56 -0700319
York Suna058b8a2017-08-15 11:14:45 -0700320 /*
321 * Read the device tree and place it after the image.
322 * Align the destination address to ARCH_DMA_MINALIGN.
323 */
324 image_info.load_addr = spl_image->load_addr + spl_image->size;
325 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
326 &image_info);
327 if (ret < 0)
328 return ret;
329 }
Andre Przywarae7c58562017-04-26 01:32:37 +0100330
331 /* Now check if there are more images for us to load */
332 for (; ; index++) {
333 node = spl_fit_get_image_node(fit, images, "loadables", index);
334 if (node < 0)
335 break;
336
337 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
338 &image_info);
339 if (ret < 0)
340 continue;
341
342 /*
343 * If the "firmware" image did not provide an entry point,
344 * use the first valid entry point from the loadables.
345 */
346 if (spl_image->entry_point == FDT_ERROR &&
347 image_info.entry_point != FDT_ERROR)
348 spl_image->entry_point = image_info.entry_point;
349 }
350
351 /*
352 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
353 * Makefile will set it to 0 and it will end up as the entry point
354 * here. What it actually means is: use the load address.
355 */
356 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
357 spl_image->entry_point = spl_image->load_addr;
358
359 return 0;
Simon Glassa6131a22016-02-22 22:55:56 -0700360}