]>
Commit | Line | Data |
---|---|---|
f1dcee59 SG |
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 | ||
7264f292 YS |
14 | #ifndef CONFIG_SYS_BOOTM_LEN |
15 | #define CONFIG_SYS_BOOTM_LEN (64 << 20) | |
16 | #endif | |
17 | ||
736806fb AP |
18 | /** |
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 | */ | |
30 | static int spl_fit_get_image_node(const void *fit, int images, | |
31 | const char *type, int index) | |
4b9340ab AP |
32 | { |
33 | const char *name, *str; | |
34 | int node, conf_node; | |
35 | int len, i; | |
36 | ||
3863f840 | 37 | conf_node = fit_find_config_node(fit); |
4b9340ab AP |
38 | 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); | |
f1dcee59 | 46 | } |
4b9340ab AP |
47 | #endif |
48 | return conf_node; | |
49 | } | |
f1dcee59 | 50 | |
4b9340ab AP |
51 | 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 | } | |
f1dcee59 | 56 | |
4b9340ab AP |
57 | 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 | } | |
f1dcee59 SG |
64 | } |
65 | ||
4b9340ab AP |
66 | 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; | |
f1dcee59 | 71 | } |
f1dcee59 | 72 | |
736806fb | 73 | return node; |
f1dcee59 SG |
74 | } |
75 | ||
eafd5410 LV |
76 | static 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 | ||
89 | static 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 | ||
103 | static int get_aligned_image_size(struct spl_load_info *info, int data_size, | |
104 | int offset) | |
105 | { | |
3cc1f380 LV |
106 | data_size = data_size + get_aligned_image_overhead(info, offset); |
107 | ||
eafd5410 | 108 | if (info->filename) |
3cc1f380 | 109 | return data_size; |
eafd5410 LV |
110 | |
111 | return (data_size + info->bl_len - 1) / info->bl_len; | |
112 | } | |
113 | ||
8baa3818 AP |
114 | /** |
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 | */ | |
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) | |
134 | { | |
135 | ulong offset; | |
136 | size_t length; | |
137 | ulong load_addr, load_ptr; | |
138 | void *src; | |
139 | ulong overhead; | |
140 | int nr_sectors; | |
141 | int align_len = ARCH_DMA_MINALIGN - 1; | |
7264f292 YS |
142 | uint8_t image_comp = -1, type = -1; |
143 | ||
144 | if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { | |
145 | if (fit_image_get_comp(fit, node, &image_comp)) | |
146 | puts("Cannot get image compression format.\n"); | |
147 | else | |
148 | debug("%s ", genimg_get_comp_name(image_comp)); | |
149 | ||
150 | if (fit_image_get_type(fit, node, &type)) | |
151 | puts("Cannot get image type.\n"); | |
152 | else | |
153 | debug("%s ", genimg_get_type_name(type)); | |
154 | } | |
8baa3818 AP |
155 | |
156 | offset = fdt_getprop_u32(fit, node, "data-offset"); | |
157 | if (offset == FDT_ERROR) | |
158 | return -ENOENT; | |
159 | offset += base_offset; | |
160 | length = fdt_getprop_u32(fit, node, "data-size"); | |
161 | if (length == FDT_ERROR) | |
162 | return -ENOENT; | |
163 | load_addr = fdt_getprop_u32(fit, node, "load"); | |
164 | if (load_addr == FDT_ERROR && image_info) | |
165 | load_addr = image_info->load_addr; | |
166 | load_ptr = (load_addr + align_len) & ~align_len; | |
167 | ||
168 | overhead = get_aligned_image_overhead(info, offset); | |
169 | nr_sectors = get_aligned_image_size(info, length, offset); | |
170 | ||
171 | if (info->read(info, sector + get_aligned_image_offset(info, offset), | |
172 | nr_sectors, (void*)load_ptr) != nr_sectors) | |
173 | return -EIO; | |
7264f292 | 174 | debug("image dst=%lx, offset=%lx, size=%lx\n", load_ptr, offset, |
8baa3818 AP |
175 | (unsigned long)length); |
176 | ||
177 | src = (void *)load_ptr + overhead; | |
178 | #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS | |
179 | board_fit_image_post_process(&src, &length); | |
180 | #endif | |
181 | ||
7264f292 YS |
182 | if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && |
183 | IS_ENABLED(CONFIG_SPL_GZIP) && | |
184 | image_comp == IH_COMP_GZIP && | |
185 | type == IH_TYPE_KERNEL) { | |
186 | if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, | |
187 | src, &length)) { | |
188 | puts("Uncompressing error\n"); | |
189 | return -EIO; | |
190 | } | |
191 | } else { | |
192 | memcpy((void *)load_addr, src, length); | |
193 | } | |
8baa3818 AP |
194 | |
195 | if (image_info) { | |
196 | image_info->load_addr = load_addr; | |
197 | image_info->size = length; | |
198 | image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); | |
199 | } | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
f4d7d859 SG |
204 | int spl_load_simple_fit(struct spl_image_info *spl_image, |
205 | struct spl_load_info *info, ulong sector, void *fit) | |
f1dcee59 SG |
206 | { |
207 | int sectors; | |
8baa3818 | 208 | ulong size; |
f1dcee59 | 209 | unsigned long count; |
8baa3818 AP |
210 | struct spl_image_info image_info; |
211 | int node, images, ret; | |
eafd5410 | 212 | int base_offset, align_len = ARCH_DMA_MINALIGN - 1; |
411cf32d | 213 | int index = 0; |
f1dcee59 SG |
214 | |
215 | /* | |
216 | * Figure out where the external images start. This is the base for the | |
217 | * data-offset properties in each image. | |
218 | */ | |
219 | size = fdt_totalsize(fit); | |
220 | size = (size + 3) & ~3; | |
221 | base_offset = (size + 3) & ~3; | |
222 | ||
223 | /* | |
224 | * So far we only have one block of data from the FIT. Read the entire | |
225 | * thing, including that first block, placing it so it finishes before | |
226 | * where we will load the image. | |
227 | * | |
228 | * Note that we will load the image such that its first byte will be | |
229 | * at the load address. Since that byte may be part-way through a | |
230 | * block, we may load the image up to one block before the load | |
231 | * address. So take account of that here by subtracting an addition | |
232 | * block length from the FIT start position. | |
233 | * | |
234 | * In fact the FIT has its own load address, but we assume it cannot | |
235 | * be before CONFIG_SYS_TEXT_BASE. | |
236 | */ | |
8b528709 LV |
237 | fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - |
238 | align_len) & ~align_len); | |
eafd5410 | 239 | sectors = get_aligned_image_size(info, size, 0); |
f1dcee59 SG |
240 | count = info->read(info, sector, sectors, fit); |
241 | debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", | |
242 | sector, sectors, fit, count); | |
243 | if (count == 0) | |
244 | return -EIO; | |
245 | ||
736806fb | 246 | /* find the node holding the images information */ |
f1dcee59 SG |
247 | images = fdt_path_offset(fit, FIT_IMAGES_PATH); |
248 | if (images < 0) { | |
249 | debug("%s: Cannot find /images node: %d\n", __func__, images); | |
250 | return -1; | |
251 | } | |
736806fb AP |
252 | |
253 | /* find the U-Boot image */ | |
254 | node = spl_fit_get_image_node(fit, images, "firmware", 0); | |
f1dcee59 | 255 | if (node < 0) { |
736806fb AP |
256 | debug("could not find firmware image, trying loadables...\n"); |
257 | node = spl_fit_get_image_node(fit, images, "loadables", 0); | |
411cf32d AP |
258 | /* |
259 | * If we pick the U-Boot image from "loadables", start at | |
260 | * the second image when later loading additional images. | |
261 | */ | |
262 | index = 1; | |
736806fb AP |
263 | } |
264 | if (node < 0) { | |
265 | debug("%s: Cannot find u-boot image node: %d\n", | |
266 | __func__, node); | |
f1dcee59 SG |
267 | return -1; |
268 | } | |
269 | ||
8baa3818 AP |
270 | /* Load the image and set up the spl_image structure */ |
271 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, | |
272 | spl_image); | |
273 | if (ret) | |
274 | return ret; | |
da74d1f3 | 275 | |
8baa3818 | 276 | spl_image->os = IH_OS_U_BOOT; |
f1dcee59 SG |
277 | |
278 | /* Figure out which device tree the board wants to use */ | |
736806fb AP |
279 | node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0); |
280 | if (node < 0) { | |
281 | debug("%s: cannot find FDT node\n", __func__); | |
282 | return node; | |
283 | } | |
f1dcee59 SG |
284 | |
285 | /* | |
8baa3818 AP |
286 | * Read the device tree and place it after the image. |
287 | * Align the destination address to ARCH_DMA_MINALIGN. | |
f1dcee59 | 288 | */ |
8baa3818 | 289 | image_info.load_addr = spl_image->load_addr + spl_image->size; |
411cf32d AP |
290 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, |
291 | &image_info); | |
292 | if (ret < 0) | |
293 | return ret; | |
294 | ||
295 | /* Now check if there are more images for us to load */ | |
296 | for (; ; index++) { | |
297 | node = spl_fit_get_image_node(fit, images, "loadables", index); | |
298 | if (node < 0) | |
299 | break; | |
300 | ||
301 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, | |
302 | &image_info); | |
303 | if (ret < 0) | |
304 | continue; | |
305 | ||
306 | /* | |
307 | * If the "firmware" image did not provide an entry point, | |
308 | * use the first valid entry point from the loadables. | |
309 | */ | |
310 | if (spl_image->entry_point == FDT_ERROR && | |
311 | image_info.entry_point != FDT_ERROR) | |
312 | spl_image->entry_point = image_info.entry_point; | |
313 | } | |
314 | ||
315 | /* | |
316 | * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's | |
317 | * Makefile will set it to 0 and it will end up as the entry point | |
318 | * here. What it actually means is: use the load address. | |
319 | */ | |
320 | if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) | |
321 | spl_image->entry_point = spl_image->load_addr; | |
322 | ||
323 | return 0; | |
f1dcee59 | 324 | } |