1 /*
2 * Flattened Image Tree loader.
3 *
4 * Copyright (c) 2016 Imagination Technologies
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "qemu/units.h"
23 #include "system/memory.h"
24 #include "hw/loader.h"
25 #include "hw/loader-fit.h"
26 #include "qemu/cutils.h"
27 #include "qemu/error-report.h"
28 #include "system/device_tree.h"
29
30 #include <libfdt.h>
31 #include <zlib.h>
32
33 #define FIT_LOADER_MAX_PATH (128)
34
fit_load_image_alloc(const void * itb,const char * name,int * poff,size_t * psz,Error ** errp)35 static void *fit_load_image_alloc(const void *itb, const char *name,
36 int *poff, size_t *psz, Error **errp)
37 {
38 const void *data;
39 const char *comp;
40 void *uncomp_data;
41 char path[FIT_LOADER_MAX_PATH];
42 int off, sz;
43 ssize_t uncomp_len;
44
45 snprintf(path, sizeof(path), "/images/%s", name);
46
47 off = fdt_path_offset(itb, path);
48 if (off < 0) {
49 error_setg(errp, "can't find node %s", path);
50 return NULL;
51 }
52 if (poff) {
53 *poff = off;
54 }
55
56 data = fdt_getprop(itb, off, "data", &sz);
57 if (!data) {
58 error_setg(errp, "can't get %s/data", path);
59 return NULL;
60 }
61
62 comp = fdt_getprop(itb, off, "compression", NULL);
63 if (!comp || !strcmp(comp, "none")) {
64 if (psz) {
65 *psz = sz;
66 }
67 uncomp_data = g_malloc(sz);
68 memmove(uncomp_data, data, sz);
69 return uncomp_data;
70 }
71
72 if (!strcmp(comp, "gzip")) {
73 uncomp_len = UBOOT_MAX_GUNZIP_BYTES;
74 uncomp_data = g_malloc(uncomp_len);
75
76 uncomp_len = gunzip(uncomp_data, uncomp_len, (void *) data, sz);
77 if (uncomp_len < 0) {
78 error_setg(errp, "unable to decompress %s image", name);
79 g_free(uncomp_data);
80 return NULL;
81 }
82
83 uncomp_data = g_realloc(uncomp_data, uncomp_len);
84 if (psz) {
85 *psz = uncomp_len;
86 }
87 return uncomp_data;
88 }
89
90 error_setg(errp, "unknown compression '%s'", comp);
91 return NULL;
92 }
93
fit_image_addr(const void * itb,int img,const char * name,hwaddr * addr,Error ** errp)94 static int fit_image_addr(const void *itb, int img, const char *name,
95 hwaddr *addr, Error **errp)
96 {
97 const void *prop;
98 int len;
99
100 prop = fdt_getprop(itb, img, name, &len);
101 if (!prop) {
102 error_setg(errp, "can't find %s address", name);
103 return -ENOENT;
104 }
105
106 switch (len) {
107 case 4:
108 *addr = fdt32_to_cpu(*(fdt32_t *)prop);
109 return 0;
110 case 8:
111 *addr = fdt64_to_cpu(*(fdt64_t *)prop);
112 return 0;
113 default:
114 error_setg(errp, "invalid %s address length %d", name, len);
115 return -EINVAL;
116 }
117 }
118
fit_load_kernel(const struct fit_loader * ldr,const void * itb,int cfg,void * opaque,hwaddr * pend,Error ** errp)119 static int fit_load_kernel(const struct fit_loader *ldr, const void *itb,
120 int cfg, void *opaque, hwaddr *pend,
121 Error **errp)
122 {
123 ERRP_GUARD();
124 const char *name;
125 const void *data;
126 const void *load_data;
127 hwaddr load_addr, entry_addr;
128 int img_off, err;
129 size_t sz;
130 int ret;
131
132 name = fdt_getprop(itb, cfg, "kernel", NULL);
133 if (!name) {
134 error_setg(errp, "no kernel specified by FIT configuration");
135 return -EINVAL;
136 }
137
138 load_data = data = fit_load_image_alloc(itb, name, &img_off, &sz, errp);
139 if (!data) {
140 error_prepend(errp, "unable to load kernel image from FIT: ");
141 return -EINVAL;
142 }
143
144 err = fit_image_addr(itb, img_off, "load", &load_addr, errp);
145 if (err) {
146 error_prepend(errp, "unable to read kernel load address from FIT: ");
147 ret = err;
148 goto out;
149 }
150
151 err = fit_image_addr(itb, img_off, "entry", &entry_addr, errp);
152 if (err) {
153 error_prepend(errp, "unable to read kernel entry address from FIT: ");
154 ret = err;
155 goto out;
156 }
157
158 if (ldr->kernel_filter) {
159 load_data = ldr->kernel_filter(opaque, data, &load_addr, &entry_addr);
160 }
161
162 if (pend) {
163 *pend = load_addr + sz;
164 }
165
166 load_addr = ldr->addr_to_phys(opaque, load_addr);
167 rom_add_blob_fixed(name, load_data, sz, load_addr);
168
169 ret = 0;
170 out:
171 g_free((void *) data);
172 if (data != load_data) {
173 g_free((void *) load_data);
174 }
175 return ret;
176 }
177
fit_load_fdt(const struct fit_loader * ldr,const void * itb,int cfg,void * opaque,const void * match_data,hwaddr kernel_end,void ** pfdt,Error ** errp)178 static int fit_load_fdt(const struct fit_loader *ldr, const void *itb,
179 int cfg, void *opaque, const void *match_data,
180 hwaddr kernel_end, void **pfdt, Error **errp)
181 {
182 ERRP_GUARD();
183 Error *err = NULL;
184 const char *name;
185 void *data;
186 hwaddr load_addr;
187 int img_off;
188 size_t sz;
189 int ret;
190
191 name = fdt_getprop(itb, cfg, "fdt", NULL);
192 if (!name) {
193 return 0;
194 }
195
196 data = fit_load_image_alloc(itb, name, &img_off, &sz, errp);
197 if (!data) {
198 error_prepend(errp, "unable to load FDT image from FIT: ");
199 return -EINVAL;
200 }
201
202 ret = fit_image_addr(itb, img_off, "load", &load_addr, &err);
203 if (ret == -ENOENT) {
204 load_addr = ROUND_UP(kernel_end, 64 * KiB) + (10 * MiB);
205 error_free(err);
206 } else if (ret) {
207 error_propagate_prepend(errp, err,
208 "unable to read FDT load address from FIT: ");
209 goto out;
210 }
211
212 if (ldr->fdt_filter) {
213 void *filtered_data;
214
215 filtered_data = ldr->fdt_filter(opaque, data, match_data, &load_addr);
216 if (filtered_data != data) {
217 g_free(data);
218 data = filtered_data;
219 }
220 }
221
222 load_addr = ldr->addr_to_phys(opaque, load_addr);
223 sz = fdt_totalsize(data);
224 rom_add_blob_fixed(name, data, sz, load_addr);
225
226 *pfdt = data;
227 return 0;
228 out:
229 g_free((void *) data);
230 return ret;
231 }
232
fit_cfg_compatible(const void * itb,int cfg,const char * compat)233 static bool fit_cfg_compatible(const void *itb, int cfg, const char *compat)
234 {
235 const void *fdt;
236 const char *fdt_name;
237 bool ret;
238
239 fdt_name = fdt_getprop(itb, cfg, "fdt", NULL);
240 if (!fdt_name) {
241 return false;
242 }
243
244 fdt = fit_load_image_alloc(itb, fdt_name, NULL, NULL, NULL);
245 if (!fdt) {
246 return false;
247 }
248
249 if (fdt_check_header(fdt)) {
250 ret = false;
251 goto out;
252 }
253
254 if (fdt_node_check_compatible(fdt, 0, compat)) {
255 ret = false;
256 goto out;
257 }
258
259 ret = true;
260 out:
261 g_free((void *) fdt);
262 return ret;
263 }
264
load_fit(const struct fit_loader * ldr,const char * filename,void ** pfdt,void * opaque)265 int load_fit(const struct fit_loader *ldr, const char *filename,
266 void **pfdt, void *opaque)
267 {
268 Error *err = NULL;
269 const struct fit_loader_match *match;
270 const void *itb, *match_data = NULL;
271 const char *def_cfg_name;
272 char path[FIT_LOADER_MAX_PATH];
273 int itb_size, configs, cfg_off, off;
274 hwaddr kernel_end = 0;
275 int ret;
276
277 itb = load_device_tree(filename, &itb_size);
278 if (!itb) {
279 return -EINVAL;
280 }
281
282 configs = fdt_path_offset(itb, "/configurations");
283 if (configs < 0) {
284 error_report("can't find node /configurations");
285 ret = configs;
286 goto out;
287 }
288
289 cfg_off = -FDT_ERR_NOTFOUND;
290
291 if (ldr->matches) {
292 for (match = ldr->matches; match->compatible; match++) {
293 off = fdt_first_subnode(itb, configs);
294 while (off >= 0) {
295 if (fit_cfg_compatible(itb, off, match->compatible)) {
296 cfg_off = off;
297 match_data = match->data;
298 break;
299 }
300
301 off = fdt_next_subnode(itb, off);
302 }
303
304 if (cfg_off >= 0) {
305 break;
306 }
307 }
308 }
309
310 if (cfg_off < 0) {
311 def_cfg_name = fdt_getprop(itb, configs, "default", NULL);
312 if (def_cfg_name) {
313 snprintf(path, sizeof(path), "/configurations/%s", def_cfg_name);
314 cfg_off = fdt_path_offset(itb, path);
315 }
316 }
317
318 if (cfg_off < 0) {
319 error_report("can't find configuration");
320 ret = cfg_off;
321 goto out;
322 }
323
324 ret = fit_load_kernel(ldr, itb, cfg_off, opaque, &kernel_end, &err);
325 if (ret) {
326 error_report_err(err);
327 goto out;
328 }
329
330 ret = fit_load_fdt(ldr, itb, cfg_off, opaque, match_data, kernel_end, pfdt,
331 &err);
332 if (ret) {
333 error_report_err(err);
334 goto out;
335 }
336
337 ret = 0;
338 out:
339 g_free((void *) itb);
340 return ret;
341 }
342