blob: 538481241f8e071c0dda6d70974dfbff56ff4417 [file] [log] [blame]
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4 */
5#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07006#include <cpu_func.h>
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +02007#include <dm.h>
8#include <elf.h>
9#include <remoteproc.h>
10
Fabien Dessenneffcb8802019-10-30 14:38:28 +010011/**
12 * struct resource_table - firmware resource table header
13 * @ver: version number
14 * @num: number of resource entries
15 * @reserved: reserved (must be zero)
16 * @offset: array of offsets pointing at the various resource entries
17 *
18 * A resource table is essentially a list of system resources required
19 * by the remote processor. It may also include configuration entries.
20 * If needed, the remote processor firmware should contain this table
21 * as a dedicated ".resource_table" ELF section.
22 *
23 * Some resources entries are mere announcements, where the host is informed
24 * of specific remoteproc configuration. Other entries require the host to
25 * do something (e.g. allocate a system resource). Sometimes a negotiation
26 * is expected, where the firmware requests a resource, and once allocated,
27 * the host should provide back its details (e.g. address of an allocated
28 * memory region).
29 *
30 * The header of the resource table, as expressed by this structure,
31 * contains a version number (should we need to change this format in the
32 * future), the number of available resource entries, and their offsets
33 * in the table.
34 *
35 * Immediately following this header are the resource entries themselves.
36 */
37struct resource_table {
38 u32 ver;
39 u32 num;
40 u32 reserved[2];
41 u32 offset[0];
42} __packed;
43
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +020044/* Basic function to verify ELF32 image format */
45int rproc_elf32_sanity_check(ulong addr, ulong size)
46{
47 Elf32_Ehdr *ehdr;
48 char class;
49
50 if (!addr) {
51 pr_debug("Invalid fw address?\n");
52 return -EFAULT;
53 }
54
55 if (size < sizeof(Elf32_Ehdr)) {
56 pr_debug("Image is too small\n");
57 return -ENOSPC;
58 }
59
60 ehdr = (Elf32_Ehdr *)addr;
61 class = ehdr->e_ident[EI_CLASS];
62
63 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS32) {
64 pr_debug("Not an executable ELF32 image\n");
65 return -EPROTONOSUPPORT;
66 }
67
68 /* We assume the firmware has the same endianness as the host */
69# ifdef __LITTLE_ENDIAN
70 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
71# else /* BIG ENDIAN */
72 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
73# endif
74 pr_debug("Unsupported firmware endianness\n");
75 return -EILSEQ;
76 }
77
78 if (size < ehdr->e_shoff + sizeof(Elf32_Shdr)) {
79 pr_debug("Image is too small\n");
80 return -ENOSPC;
81 }
82
83 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
84 pr_debug("Image is corrupted (bad magic)\n");
85 return -EBADF;
86 }
87
88 if (ehdr->e_phnum == 0) {
89 pr_debug("No loadable segments\n");
90 return -ENOEXEC;
91 }
92
93 if (ehdr->e_phoff > size) {
94 pr_debug("Firmware size is too small\n");
95 return -ENOSPC;
96 }
97
98 return 0;
99}
100
Lokesh Vutlae3c4d6f2019-09-04 16:01:29 +0530101/* Basic function to verify ELF64 image format */
102int rproc_elf64_sanity_check(ulong addr, ulong size)
103{
104 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
105 char class;
106
107 if (!addr) {
108 pr_debug("Invalid fw address?\n");
109 return -EFAULT;
110 }
111
112 if (size < sizeof(Elf64_Ehdr)) {
113 pr_debug("Image is too small\n");
114 return -ENOSPC;
115 }
116
117 class = ehdr->e_ident[EI_CLASS];
118
119 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS64) {
120 pr_debug("Not an executable ELF64 image\n");
121 return -EPROTONOSUPPORT;
122 }
123
124 /* We assume the firmware has the same endianness as the host */
125# ifdef __LITTLE_ENDIAN
126 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
127# else /* BIG ENDIAN */
128 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
129# endif
130 pr_debug("Unsupported firmware endianness\n");
131 return -EILSEQ;
132 }
133
134 if (size < ehdr->e_shoff + sizeof(Elf64_Shdr)) {
135 pr_debug("Image is too small\n");
136 return -ENOSPC;
137 }
138
139 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
140 pr_debug("Image is corrupted (bad magic)\n");
141 return -EBADF;
142 }
143
144 if (ehdr->e_phnum == 0) {
145 pr_debug("No loadable segments\n");
146 return -ENOEXEC;
147 }
148
149 if (ehdr->e_phoff > size) {
150 pr_debug("Firmware size is too small\n");
151 return -ENOSPC;
152 }
153
154 return 0;
155}
156
Lokesh Vutla856c0ad2019-09-04 16:01:30 +0530157/* Basic function to verify ELF image format */
158int rproc_elf_sanity_check(ulong addr, ulong size)
159{
160 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
161
162 if (!addr) {
163 dev_err(dev, "Invalid firmware address\n");
164 return -EFAULT;
165 }
166
167 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
168 return rproc_elf64_sanity_check(addr, size);
169 else
170 return rproc_elf32_sanity_check(addr, size);
171}
172
Lokesh Vutla14d963d2019-09-04 16:01:28 +0530173int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size)
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200174{
175 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
176 Elf32_Phdr *phdr; /* Program header structure pointer */
177 const struct dm_rproc_ops *ops;
Lokesh Vutla14d963d2019-09-04 16:01:28 +0530178 unsigned int i, ret;
179
180 ret = rproc_elf32_sanity_check(addr, size);
181 if (ret) {
182 dev_err(dev, "Invalid ELF32 Image %d\n", ret);
183 return ret;
184 }
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200185
186 ehdr = (Elf32_Ehdr *)addr;
187 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
188
189 ops = rproc_get_ops(dev);
190
191 /* Load each program header */
192 for (i = 0; i < ehdr->e_phnum; ++i) {
193 void *dst = (void *)(uintptr_t)phdr->p_paddr;
194 void *src = (void *)addr + phdr->p_offset;
195
196 if (phdr->p_type != PT_LOAD)
197 continue;
198
199 if (ops->device_to_virt)
Lokesh Vutlac08eb932019-09-04 16:01:27 +0530200 dst = ops->device_to_virt(dev, (ulong)dst,
201 phdr->p_memsz);
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200202
203 dev_dbg(dev, "Loading phdr %i to 0x%p (%i bytes)\n",
204 i, dst, phdr->p_filesz);
205 if (phdr->p_filesz)
206 memcpy(dst, src, phdr->p_filesz);
207 if (phdr->p_filesz != phdr->p_memsz)
208 memset(dst + phdr->p_filesz, 0x00,
209 phdr->p_memsz - phdr->p_filesz);
210 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
211 roundup((unsigned long)dst + phdr->p_filesz,
212 ARCH_DMA_MINALIGN) -
213 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
214 ++phdr;
215 }
216
217 return 0;
218}
Lokesh Vutlae3c4d6f2019-09-04 16:01:29 +0530219
220int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size)
221{
222 const struct dm_rproc_ops *ops = rproc_get_ops(dev);
223 u64 da, memsz, filesz, offset;
224 Elf64_Ehdr *ehdr;
225 Elf64_Phdr *phdr;
226 int i, ret = 0;
227 void *ptr;
228
229 dev_dbg(dev, "%s: addr = 0x%lx size = 0x%lx\n", __func__, addr, size);
230
231 if (rproc_elf64_sanity_check(addr, size))
232 return -EINVAL;
233
234 ehdr = (Elf64_Ehdr *)addr;
235 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
236
237 /* go through the available ELF segments */
238 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
239 da = phdr->p_paddr;
240 memsz = phdr->p_memsz;
241 filesz = phdr->p_filesz;
242 offset = phdr->p_offset;
243
244 if (phdr->p_type != PT_LOAD)
245 continue;
246
247 dev_dbg(dev, "%s:phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
248 __func__, phdr->p_type, da, memsz, filesz);
249
250 ptr = (void *)(uintptr_t)da;
251 if (ops->device_to_virt) {
252 ptr = ops->device_to_virt(dev, da, phdr->p_memsz);
253 if (!ptr) {
254 dev_err(dev, "bad da 0x%llx mem 0x%llx\n", da,
255 memsz);
256 ret = -EINVAL;
257 break;
258 }
259 }
260
261 if (filesz)
262 memcpy(ptr, (void *)addr + offset, filesz);
263 if (filesz != memsz)
264 memset(ptr + filesz, 0x00, memsz - filesz);
265
266 flush_cache(rounddown((ulong)ptr, ARCH_DMA_MINALIGN),
267 roundup((ulong)ptr + filesz, ARCH_DMA_MINALIGN) -
268 rounddown((ulong)ptr, ARCH_DMA_MINALIGN));
269 }
270
271 return ret;
272}
Lokesh Vutla856c0ad2019-09-04 16:01:30 +0530273
274int rproc_elf_load_image(struct udevice *dev, ulong addr, ulong size)
275{
276 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
277
278 if (!addr) {
279 dev_err(dev, "Invalid firmware address\n");
280 return -EFAULT;
281 }
282
283 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
284 return rproc_elf64_load_image(dev, addr, size);
285 else
286 return rproc_elf32_load_image(dev, addr, size);
287}
Lokesh Vutla81e39fb2019-09-04 16:01:31 +0530288
289static ulong rproc_elf32_get_boot_addr(ulong addr)
290{
291 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
292
293 return ehdr->e_entry;
294}
295
296static ulong rproc_elf64_get_boot_addr(ulong addr)
297{
298 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
299
300 return ehdr->e_entry;
301}
302
303ulong rproc_elf_get_boot_addr(struct udevice *dev, ulong addr)
304{
305 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
306
307 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
308 return rproc_elf64_get_boot_addr(addr);
309 else
310 return rproc_elf32_get_boot_addr(addr);
311}
Fabien Dessenneffcb8802019-10-30 14:38:28 +0100312
313/*
314 * Search for the resource table in an ELF32 image.
315 * Returns the address of the resource table section if found, NULL if there is
316 * no resource table section, or error pointer.
317 */
318static Elf32_Shdr *rproc_elf32_find_rsc_table(struct udevice *dev,
319 ulong fw_addr, ulong fw_size)
320{
321 int ret;
322 unsigned int i;
323 const char *name_table;
324 struct resource_table *table;
325 const u8 *elf_data = (void *)fw_addr;
326 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
327 Elf32_Shdr *shdr;
328
329 ret = rproc_elf32_sanity_check(fw_addr, fw_size);
330 if (ret) {
331 pr_debug("Invalid ELF32 Image %d\n", ret);
332 return ERR_PTR(ret);
333 }
334
335 /* look for the resource table and handle it */
336 shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
337 name_table = (const char *)(elf_data +
338 shdr[ehdr->e_shstrndx].sh_offset);
339
340 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
341 u32 size = shdr->sh_size;
342 u32 offset = shdr->sh_offset;
343
344 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
345 continue;
346
347 table = (struct resource_table *)(elf_data + offset);
348
349 /* make sure we have the entire table */
350 if (offset + size > fw_size) {
351 pr_debug("resource table truncated\n");
352 return ERR_PTR(-ENOSPC);
353 }
354
355 /* make sure table has at least the header */
356 if (sizeof(*table) > size) {
357 pr_debug("header-less resource table\n");
358 return ERR_PTR(-ENOSPC);
359 }
360
361 /* we don't support any version beyond the first */
362 if (table->ver != 1) {
363 pr_debug("unsupported fw ver: %d\n", table->ver);
364 return ERR_PTR(-EPROTONOSUPPORT);
365 }
366
367 /* make sure reserved bytes are zeroes */
368 if (table->reserved[0] || table->reserved[1]) {
369 pr_debug("non zero reserved bytes\n");
370 return ERR_PTR(-EBADF);
371 }
372
373 /* make sure the offsets array isn't truncated */
374 if (table->num * sizeof(table->offset[0]) +
375 sizeof(*table) > size) {
376 pr_debug("resource table incomplete\n");
377 return ERR_PTR(-ENOSPC);
378 }
379
380 return shdr;
381 }
382
383 return NULL;
384}
385
386/* Load the resource table from an ELF32 image */
387int rproc_elf32_load_rsc_table(struct udevice *dev, ulong fw_addr,
388 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
389{
390 const struct dm_rproc_ops *ops;
391 Elf32_Shdr *shdr;
392 void *src, *dst;
393
394 shdr = rproc_elf32_find_rsc_table(dev, fw_addr, fw_size);
395 if (!shdr)
396 return -ENODATA;
397 if (IS_ERR(shdr))
398 return PTR_ERR(shdr);
399
400 ops = rproc_get_ops(dev);
401 *rsc_addr = (ulong)shdr->sh_addr;
402 *rsc_size = (ulong)shdr->sh_size;
403
404 src = (void *)fw_addr + shdr->sh_offset;
405 if (ops->device_to_virt)
406 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
407 else
408 dst = (void *)rsc_addr;
409
410 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
411 (ulong)dst, *rsc_size);
412
413 memcpy(dst, src, *rsc_size);
414 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
415 roundup((unsigned long)dst + *rsc_size,
416 ARCH_DMA_MINALIGN) -
417 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
418
419 return 0;
420}
421
422/*
423 * Search for the resource table in an ELF64 image.
424 * Returns the address of the resource table section if found, NULL if there is
425 * no resource table section, or error pointer.
426 */
427static Elf64_Shdr *rproc_elf64_find_rsc_table(struct udevice *dev,
428 ulong fw_addr, ulong fw_size)
429{
430 int ret;
431 unsigned int i;
432 const char *name_table;
433 struct resource_table *table;
434 const u8 *elf_data = (void *)fw_addr;
435 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)fw_addr;
436 Elf64_Shdr *shdr;
437
438 ret = rproc_elf64_sanity_check(fw_addr, fw_size);
439 if (ret) {
440 pr_debug("Invalid ELF64 Image %d\n", ret);
441 return ERR_PTR(ret);
442 }
443
444 /* look for the resource table and handle it */
445 shdr = (Elf64_Shdr *)(elf_data + ehdr->e_shoff);
446 name_table = (const char *)(elf_data +
447 shdr[ehdr->e_shstrndx].sh_offset);
448
449 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
450 u64 size = shdr->sh_size;
451 u64 offset = shdr->sh_offset;
452
453 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
454 continue;
455
456 table = (struct resource_table *)(elf_data + offset);
457
458 /* make sure we have the entire table */
459 if (offset + size > fw_size) {
460 pr_debug("resource table truncated\n");
461 return ERR_PTR(-ENOSPC);
462 }
463
464 /* make sure table has at least the header */
465 if (sizeof(*table) > size) {
466 pr_debug("header-less resource table\n");
467 return ERR_PTR(-ENOSPC);
468 }
469
470 /* we don't support any version beyond the first */
471 if (table->ver != 1) {
472 pr_debug("unsupported fw ver: %d\n", table->ver);
473 return ERR_PTR(-EPROTONOSUPPORT);
474 }
475
476 /* make sure reserved bytes are zeroes */
477 if (table->reserved[0] || table->reserved[1]) {
478 pr_debug("non zero reserved bytes\n");
479 return ERR_PTR(-EBADF);
480 }
481
482 /* make sure the offsets array isn't truncated */
483 if (table->num * sizeof(table->offset[0]) +
484 sizeof(*table) > size) {
485 pr_debug("resource table incomplete\n");
486 return ERR_PTR(-ENOSPC);
487 }
488
489 return shdr;
490 }
491
492 return NULL;
493}
494
495/* Load the resource table from an ELF64 image */
496int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr,
497 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
498{
499 const struct dm_rproc_ops *ops;
500 Elf64_Shdr *shdr;
501 void *src, *dst;
502
503 shdr = rproc_elf64_find_rsc_table(dev, fw_addr, fw_size);
504 if (!shdr)
505 return -ENODATA;
506 if (IS_ERR(shdr))
507 return PTR_ERR(shdr);
508
509 ops = rproc_get_ops(dev);
510 *rsc_addr = (ulong)shdr->sh_addr;
511 *rsc_size = (ulong)shdr->sh_size;
512
513 src = (void *)fw_addr + shdr->sh_offset;
514 if (ops->device_to_virt)
515 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
516 else
517 dst = (void *)rsc_addr;
518
519 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
520 (ulong)dst, *rsc_size);
521
522 memcpy(dst, src, *rsc_size);
523 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
524 roundup((unsigned long)dst + *rsc_size,
525 ARCH_DMA_MINALIGN) -
526 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
527
528 return 0;
529}
530
531/* Load the resource table from an ELF32 or ELF64 image */
532int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr,
533 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
534
535{
536 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
537
538 if (!fw_addr)
539 return -EFAULT;
540
541 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
542 return rproc_elf64_load_rsc_table(dev, fw_addr, fw_size,
543 rsc_addr, rsc_size);
544 else
545 return rproc_elf32_load_rsc_table(dev, fw_addr, fw_size,
546 rsc_addr, rsc_size);
547}