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
42static int
43rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
44{
45 const char *name = rproc->firmware;
46 struct device *dev = &rproc->dev;
47 struct elf32_hdr *ehdr;
48 char class;
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
63 class = ehdr->e_ident[EI_CLASS];
64 if (class != ELFCLASS32) {
65 dev_err(dev, "Unsupported class: %d\n", class);
66 return -EINVAL;
67 }
68
69
70# ifdef __LITTLE_ENDIAN
71 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
72# else
73 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
74# endif
75 dev_err(dev, "Unsupported firmware endianness\n");
76 return -EINVAL;
77 }
78
79 if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
80 dev_err(dev, "Image is too small\n");
81 return -EINVAL;
82 }
83
84 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
85 dev_err(dev, "Image is corrupted (bad magic)\n");
86 return -EINVAL;
87 }
88
89 if (ehdr->e_phnum == 0) {
90 dev_err(dev, "No loadable segments\n");
91 return -EINVAL;
92 }
93
94 if (ehdr->e_phoff > fw->size) {
95 dev_err(dev, "Firmware size is too small\n");
96 return -EINVAL;
97 }
98
99 return 0;
100}
101
102
103
104
105
106
107
108
109
110
111
112
113static
114u32 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
115{
116 struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
117
118 return ehdr->e_entry;
119}
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
145static int
146rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
147{
148 struct device *dev = &rproc->dev;
149 struct elf32_hdr *ehdr;
150 struct elf32_phdr *phdr;
151 int i, ret = 0;
152 const u8 *elf_data = fw->data;
153
154 ehdr = (struct elf32_hdr *)elf_data;
155 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
156
157
158 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
159 u32 da = phdr->p_paddr;
160 u32 memsz = phdr->p_memsz;
161 u32 filesz = phdr->p_filesz;
162 u32 offset = phdr->p_offset;
163 void *ptr;
164
165 if (phdr->p_type != PT_LOAD)
166 continue;
167
168 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
169 phdr->p_type, da, memsz, filesz);
170
171 if (filesz > memsz) {
172 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
173 filesz, memsz);
174 ret = -EINVAL;
175 break;
176 }
177
178 if (offset + filesz > fw->size) {
179 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
180 offset + filesz, fw->size);
181 ret = -EINVAL;
182 break;
183 }
184
185
186 ptr = rproc_da_to_va(rproc, da, memsz);
187 if (!ptr) {
188 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
189 ret = -EINVAL;
190 break;
191 }
192
193
194 if (phdr->p_filesz)
195 memcpy(ptr, elf_data + phdr->p_offset, filesz);
196
197
198
199
200
201
202
203
204 if (memsz > filesz)
205 memset(ptr + filesz, 0, memsz - filesz);
206 }
207
208 return ret;
209}
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
280
281
282
283
284
285static struct resource_table *
286rproc_elf_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
287 int *tablesz)
288{
289 struct elf32_hdr *ehdr;
290 struct elf32_shdr *shdr;
291 struct device *dev = &rproc->dev;
292 struct resource_table *table = NULL;
293 const u8 *elf_data = fw->data;
294
295 ehdr = (struct elf32_hdr *)elf_data;
296
297 shdr = find_table(dev, ehdr, fw->size);
298 if (!shdr)
299 return NULL;
300
301 table = (struct resource_table *)(elf_data + shdr->sh_offset);
302 *tablesz = shdr->sh_size;
303
304 return table;
305}
306
307
308
309
310
311
312
313
314
315
316
317
318static struct resource_table *
319rproc_elf_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
320{
321 struct elf32_hdr *ehdr = (struct elf32_hdr *)fw->data;
322 struct elf32_shdr *shdr;
323
324 shdr = find_table(&rproc->dev, ehdr, fw->size);
325 if (!shdr)
326 return NULL;
327
328 return rproc_da_to_va(rproc, shdr->sh_addr, shdr->sh_size);
329}
330
331const struct rproc_fw_ops rproc_elf_fw_ops = {
332 .load = rproc_elf_load_segments,
333 .find_rsc_table = rproc_elf_find_rsc_table,
334 .find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table,
335 .sanity_check = rproc_elf_sanity_check,
336 .get_boot_addr = rproc_elf_get_boot_addr
337};
338