1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#define pr_fmt(fmt) "%s: " fmt, __func__
19
20#include <linux/module.h>
21#include <linux/firmware.h>
22#include <linux/remoteproc.h>
23#include <linux/elf.h>
24
25#include "remoteproc_internal.h"
26#include "remoteproc_elf_helpers.h"
27
28
29
30
31
32
33
34
35int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
36{
37 const char *name = rproc->firmware;
38 struct device *dev = &rproc->dev;
39
40
41
42
43
44 struct elf32_hdr *ehdr;
45 u32 elf_shdr_get_size;
46 u64 phoff, shoff;
47 char class;
48 u16 phnum;
49
50 if (!fw) {
51 dev_err(dev, "failed to load %s\n", name);
52 return -EINVAL;
53 }
54
55 if (fw->size < sizeof(struct elf32_hdr)) {
56 dev_err(dev, "Image is too small\n");
57 return -EINVAL;
58 }
59
60 ehdr = (struct elf32_hdr *)fw->data;
61
62 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
63 dev_err(dev, "Image is corrupted (bad magic)\n");
64 return -EINVAL;
65 }
66
67 class = ehdr->e_ident[EI_CLASS];
68 if (class != ELFCLASS32 && class != ELFCLASS64) {
69 dev_err(dev, "Unsupported class: %d\n", class);
70 return -EINVAL;
71 }
72
73 if (class == ELFCLASS64 && fw->size < sizeof(struct elf64_hdr)) {
74 dev_err(dev, "elf64 header is too small\n");
75 return -EINVAL;
76 }
77
78
79# ifdef __LITTLE_ENDIAN
80 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
81# else
82 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
83# endif
84 dev_err(dev, "Unsupported firmware endianness\n");
85 return -EINVAL;
86 }
87
88 phoff = elf_hdr_get_e_phoff(class, fw->data);
89 shoff = elf_hdr_get_e_shoff(class, fw->data);
90 phnum = elf_hdr_get_e_phnum(class, fw->data);
91 elf_shdr_get_size = elf_size_of_shdr(class);
92
93 if (fw->size < shoff + elf_shdr_get_size) {
94 dev_err(dev, "Image is too small\n");
95 return -EINVAL;
96 }
97
98 if (phnum == 0) {
99 dev_err(dev, "No loadable segments\n");
100 return -EINVAL;
101 }
102
103 if (phoff > fw->size) {
104 dev_err(dev, "Firmware size is too small\n");
105 return -EINVAL;
106 }
107
108 dev_dbg(dev, "Firmware is an elf%d file\n",
109 class == ELFCLASS32 ? 32 : 64);
110
111 return 0;
112}
113EXPORT_SYMBOL(rproc_elf_sanity_check);
114
115
116
117
118
119
120
121
122
123
124
125
126u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
127{
128 return elf_hdr_get_e_entry(fw_elf_get_class(fw), fw->data);
129}
130EXPORT_SYMBOL(rproc_elf_get_boot_addr);
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
157{
158 struct device *dev = &rproc->dev;
159 const void *ehdr, *phdr;
160 int i, ret = 0;
161 u16 phnum;
162 const u8 *elf_data = fw->data;
163 u8 class = fw_elf_get_class(fw);
164 u32 elf_phdr_get_size = elf_size_of_phdr(class);
165
166 ehdr = elf_data;
167 phnum = elf_hdr_get_e_phnum(class, ehdr);
168 phdr = elf_data + elf_hdr_get_e_phoff(class, ehdr);
169
170
171 for (i = 0; i < phnum; i++, phdr += elf_phdr_get_size) {
172 u64 da = elf_phdr_get_p_paddr(class, phdr);
173 u64 memsz = elf_phdr_get_p_memsz(class, phdr);
174 u64 filesz = elf_phdr_get_p_filesz(class, phdr);
175 u64 offset = elf_phdr_get_p_offset(class, phdr);
176 u32 type = elf_phdr_get_p_type(class, phdr);
177 void *ptr;
178
179 if (type != PT_LOAD)
180 continue;
181
182 dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
183 type, da, memsz, filesz);
184
185 if (filesz > memsz) {
186 dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
187 filesz, memsz);
188 ret = -EINVAL;
189 break;
190 }
191
192 if (offset + filesz > fw->size) {
193 dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
194 offset + filesz, fw->size);
195 ret = -EINVAL;
196 break;
197 }
198
199 if (!rproc_u64_fit_in_size_t(memsz)) {
200 dev_err(dev, "size (%llx) does not fit in size_t type\n",
201 memsz);
202 ret = -EOVERFLOW;
203 break;
204 }
205
206
207 ptr = rproc_da_to_va(rproc, da, memsz);
208 if (!ptr) {
209 dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
210 memsz);
211 ret = -EINVAL;
212 break;
213 }
214
215
216 if (filesz)
217 memcpy(ptr, elf_data + offset, filesz);
218
219
220
221
222
223
224
225
226 if (memsz > filesz)
227 memset(ptr + filesz, 0, memsz - filesz);
228 }
229
230 return ret;
231}
232EXPORT_SYMBOL(rproc_elf_load_segments);
233
234static const void *
235find_table(struct device *dev, const struct firmware *fw)
236{
237 const void *shdr, *name_table_shdr;
238 int i;
239 const char *name_table;
240 struct resource_table *table = NULL;
241 const u8 *elf_data = (void *)fw->data;
242 u8 class = fw_elf_get_class(fw);
243 size_t fw_size = fw->size;
244 const void *ehdr = elf_data;
245 u16 shnum = elf_hdr_get_e_shnum(class, ehdr);
246 u32 elf_shdr_get_size = elf_size_of_shdr(class);
247 u16 shstrndx = elf_hdr_get_e_shstrndx(class, ehdr);
248
249
250
251 shdr = elf_data + elf_hdr_get_e_shoff(class, ehdr);
252
253 name_table_shdr = shdr + (shstrndx * elf_shdr_get_size);
254
255 name_table = elf_data + elf_shdr_get_sh_offset(class, name_table_shdr);
256
257 for (i = 0; i < shnum; i++, shdr += elf_shdr_get_size) {
258 u64 size = elf_shdr_get_sh_size(class, shdr);
259 u64 offset = elf_shdr_get_sh_offset(class, shdr);
260 u32 name = elf_shdr_get_sh_name(class, shdr);
261
262 if (strcmp(name_table + name, ".resource_table"))
263 continue;
264
265 table = (struct resource_table *)(elf_data + offset);
266
267
268 if (offset + size > fw_size || offset + size < size) {
269 dev_err(dev, "resource table truncated\n");
270 return NULL;
271 }
272
273
274 if (sizeof(struct resource_table) > size) {
275 dev_err(dev, "header-less resource table\n");
276 return NULL;
277 }
278
279
280 if (table->ver != 1) {
281 dev_err(dev, "unsupported fw ver: %d\n", table->ver);
282 return NULL;
283 }
284
285
286 if (table->reserved[0] || table->reserved[1]) {
287 dev_err(dev, "non zero reserved bytes\n");
288 return NULL;
289 }
290
291
292 if (struct_size(table, offset, table->num) > size) {
293 dev_err(dev, "resource table incomplete\n");
294 return NULL;
295 }
296
297 return shdr;
298 }
299
300 return NULL;
301}
302
303
304
305
306
307
308
309
310
311
312
313int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
314{
315 const void *shdr;
316 struct device *dev = &rproc->dev;
317 struct resource_table *table = NULL;
318 const u8 *elf_data = fw->data;
319 size_t tablesz;
320 u8 class = fw_elf_get_class(fw);
321 u64 sh_offset;
322
323 shdr = find_table(dev, fw);
324 if (!shdr)
325 return -EINVAL;
326
327 sh_offset = elf_shdr_get_sh_offset(class, shdr);
328 table = (struct resource_table *)(elf_data + sh_offset);
329 tablesz = elf_shdr_get_sh_size(class, shdr);
330
331
332
333
334
335
336
337 rproc->cached_table = kmemdup(table, tablesz, GFP_KERNEL);
338 if (!rproc->cached_table)
339 return -ENOMEM;
340
341 rproc->table_ptr = rproc->cached_table;
342 rproc->table_sz = tablesz;
343
344 return 0;
345}
346EXPORT_SYMBOL(rproc_elf_load_rsc_table);
347
348
349
350
351
352
353
354
355
356
357
358
359struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
360 const struct firmware *fw)
361{
362 const void *shdr;
363 u64 sh_addr, sh_size;
364 u8 class = fw_elf_get_class(fw);
365 struct device *dev = &rproc->dev;
366
367 shdr = find_table(&rproc->dev, fw);
368 if (!shdr)
369 return NULL;
370
371 sh_addr = elf_shdr_get_sh_addr(class, shdr);
372 sh_size = elf_shdr_get_sh_size(class, shdr);
373
374 if (!rproc_u64_fit_in_size_t(sh_size)) {
375 dev_err(dev, "size (%llx) does not fit in size_t type\n",
376 sh_size);
377 return NULL;
378 }
379
380 return rproc_da_to_va(rproc, sh_addr, sh_size);
381}
382EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);
383