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