2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
14 #ifndef CONFIG_SYS_BOOTM_LEN
15 #define CONFIG_SYS_BOOTM_LEN (64 << 20)
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
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.
27 * Return: the node offset of the respective image node or a negative
30 static int spl_fit_get_image_node(const void *fit, int images,
31 const char *type, int index)
33 const char *name, *str;
37 conf_node = fit_find_config_node(fit);
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);
43 node = fdt_next_subnode(fit, node)) {
44 name = fdt_getprop(fit, node, "description", &len);
45 printf(" %s\n", name);
51 name = fdt_getprop(fit, conf_node, type, &len);
53 debug("cannot find property '%s': %d\n", type, len);
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);
66 debug("%s: '%s'\n", type, str);
67 node = fdt_subnode_offset(fit, images, str);
69 debug("cannot find image node '%s': %d\n", str, node);
76 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
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.
84 return offset & ~(ARCH_DMA_MINALIGN - 1);
86 return offset / info->bl_len;
89 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
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
98 return offset & (ARCH_DMA_MINALIGN - 1);
100 return offset % info->bl_len;
103 static int get_aligned_image_size(struct spl_load_info *info, int data_size,
106 data_size = data_size + get_aligned_image_overhead(info, offset);
111 return (data_size + info->bl_len - 1) / info->bl_len;
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
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
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.
129 * Return: 0 on success or a negative error number.
131 static 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)
138 ulong load_addr, load_ptr;
142 int align_len = ARCH_DMA_MINALIGN - 1;
143 uint8_t image_comp = -1, type = -1;
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");
150 debug("%s ", genimg_get_comp_name(image_comp));
152 if (fit_image_get_type(fit, node, &type))
153 puts("Cannot get image type.\n");
155 debug("%s ", genimg_get_type_name(type));
158 if (fit_image_get_load(fit, node, &load_addr))
159 load_addr = image_info->load_addr;
161 if (!fit_image_get_data_offset(fit, node, &offset)) {
163 offset += base_offset;
164 if (fit_image_get_data_size(fit, node, &len))
167 load_ptr = (load_addr + align_len) & ~align_len;
170 overhead = get_aligned_image_overhead(info, offset);
171 nr_sectors = get_aligned_image_size(info, length, offset);
174 sector + get_aligned_image_offset(info, offset),
175 nr_sectors, (void *)load_ptr) != nr_sectors)
178 debug("External data: dst=%lx, offset=%x, size=%lx\n",
179 load_ptr, offset, (unsigned long)length);
180 src = (void *)load_ptr + overhead;
183 if (fit_image_get_data(fit, node, &data, &length)) {
184 puts("Cannot get image data/size\n");
187 debug("Embedded data: dst=%lx, size=%lx\n", load_addr,
188 (unsigned long)length);
192 #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS
193 board_fit_image_post_process(&src, &length);
196 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,
202 puts("Uncompressing error\n");
206 memcpy((void *)load_addr, src, length);
210 image_info->load_addr = load_addr;
211 image_info->size = length;
212 image_info->entry_point = fdt_getprop_u32(fit, node, "entry");
218 int spl_load_simple_fit(struct spl_image_info *spl_image,
219 struct spl_load_info *info, ulong sector, void *fit)
224 struct spl_image_info image_info;
225 int node, images, ret;
226 int base_offset, align_len = ARCH_DMA_MINALIGN - 1;
230 * Figure out where the external images start. This is the base for the
231 * data-offset properties in each image.
233 size = fdt_totalsize(fit);
234 size = (size + 3) & ~3;
235 base_offset = (size + 3) & ~3;
238 * So far we only have one block of data from the FIT. Read the entire
239 * thing, including that first block, placing it so it finishes before
240 * where we will load the image.
242 * Note that we will load the image such that its first byte will be
243 * at the load address. Since that byte may be part-way through a
244 * block, we may load the image up to one block before the load
245 * address. So take account of that here by subtracting an addition
246 * block length from the FIT start position.
248 * In fact the FIT has its own load address, but we assume it cannot
249 * be before CONFIG_SYS_TEXT_BASE.
251 fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len -
252 align_len) & ~align_len);
253 sectors = get_aligned_image_size(info, size, 0);
254 count = info->read(info, sector, sectors, fit);
255 debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n",
256 sector, sectors, fit, count);
260 /* find the node holding the images information */
261 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
263 debug("%s: Cannot find /images node: %d\n", __func__, images);
267 /* find the U-Boot image */
268 node = spl_fit_get_image_node(fit, images, "firmware", 0);
270 debug("could not find firmware image, trying loadables...\n");
271 node = spl_fit_get_image_node(fit, images, "loadables", 0);
273 * If we pick the U-Boot image from "loadables", start at
274 * the second image when later loading additional images.
279 debug("%s: Cannot find u-boot image node: %d\n",
284 /* Load the image and set up the spl_image structure */
285 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
290 spl_image->os = IH_OS_U_BOOT;
292 /* Figure out which device tree the board wants to use */
293 node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0);
295 debug("%s: cannot find FDT node\n", __func__);
300 * Read the device tree and place it after the image.
301 * Align the destination address to ARCH_DMA_MINALIGN.
303 image_info.load_addr = spl_image->load_addr + spl_image->size;
304 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
309 /* Now check if there are more images for us to load */
311 node = spl_fit_get_image_node(fit, images, "loadables", index);
315 ret = spl_load_fit_image(info, sector, fit, base_offset, node,
321 * If the "firmware" image did not provide an entry point,
322 * use the first valid entry point from the loadables.
324 if (spl_image->entry_point == FDT_ERROR &&
325 image_info.entry_point != FDT_ERROR)
326 spl_image->entry_point = image_info.entry_point;
330 * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's
331 * Makefile will set it to 0 and it will end up as the entry point
332 * here. What it actually means is: use the load address.
334 if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0)
335 spl_image->entry_point = spl_image->load_addr;