blob: d2345924452e4dbcfd915773381503c11bca6e61 [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 */
Fabien Dessenne83b539c2019-09-04 09:53:22 +0200192 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200193 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));
Fabien Dessenne7a7c4cb2019-05-31 15:11:33 +0200214 }
215
216 return 0;
217}
Lokesh Vutlae3c4d6f2019-09-04 16:01:29 +0530218
219int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size)
220{
221 const struct dm_rproc_ops *ops = rproc_get_ops(dev);
222 u64 da, memsz, filesz, offset;
223 Elf64_Ehdr *ehdr;
224 Elf64_Phdr *phdr;
225 int i, ret = 0;
226 void *ptr;
227
228 dev_dbg(dev, "%s: addr = 0x%lx size = 0x%lx\n", __func__, addr, size);
229
230 if (rproc_elf64_sanity_check(addr, size))
231 return -EINVAL;
232
233 ehdr = (Elf64_Ehdr *)addr;
234 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
235
236 /* go through the available ELF segments */
237 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
238 da = phdr->p_paddr;
239 memsz = phdr->p_memsz;
240 filesz = phdr->p_filesz;
241 offset = phdr->p_offset;
242
243 if (phdr->p_type != PT_LOAD)
244 continue;
245
246 dev_dbg(dev, "%s:phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
247 __func__, phdr->p_type, da, memsz, filesz);
248
249 ptr = (void *)(uintptr_t)da;
250 if (ops->device_to_virt) {
251 ptr = ops->device_to_virt(dev, da, phdr->p_memsz);
252 if (!ptr) {
253 dev_err(dev, "bad da 0x%llx mem 0x%llx\n", da,
254 memsz);
255 ret = -EINVAL;
256 break;
257 }
258 }
259
260 if (filesz)
261 memcpy(ptr, (void *)addr + offset, filesz);
262 if (filesz != memsz)
263 memset(ptr + filesz, 0x00, memsz - filesz);
264
265 flush_cache(rounddown((ulong)ptr, ARCH_DMA_MINALIGN),
266 roundup((ulong)ptr + filesz, ARCH_DMA_MINALIGN) -
267 rounddown((ulong)ptr, ARCH_DMA_MINALIGN));
268 }
269
270 return ret;
271}
Lokesh Vutla856c0ad2019-09-04 16:01:30 +0530272
273int rproc_elf_load_image(struct udevice *dev, ulong addr, ulong size)
274{
275 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
276
277 if (!addr) {
278 dev_err(dev, "Invalid firmware address\n");
279 return -EFAULT;
280 }
281
282 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
283 return rproc_elf64_load_image(dev, addr, size);
284 else
285 return rproc_elf32_load_image(dev, addr, size);
286}
Lokesh Vutla81e39fb2019-09-04 16:01:31 +0530287
288static ulong rproc_elf32_get_boot_addr(ulong addr)
289{
290 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
291
292 return ehdr->e_entry;
293}
294
295static ulong rproc_elf64_get_boot_addr(ulong addr)
296{
297 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
298
299 return ehdr->e_entry;
300}
301
302ulong rproc_elf_get_boot_addr(struct udevice *dev, ulong addr)
303{
304 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
305
306 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
307 return rproc_elf64_get_boot_addr(addr);
308 else
309 return rproc_elf32_get_boot_addr(addr);
310}
Fabien Dessenneffcb8802019-10-30 14:38:28 +0100311
312/*
313 * Search for the resource table in an ELF32 image.
314 * Returns the address of the resource table section if found, NULL if there is
315 * no resource table section, or error pointer.
316 */
317static Elf32_Shdr *rproc_elf32_find_rsc_table(struct udevice *dev,
318 ulong fw_addr, ulong fw_size)
319{
320 int ret;
321 unsigned int i;
322 const char *name_table;
323 struct resource_table *table;
324 const u8 *elf_data = (void *)fw_addr;
325 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
326 Elf32_Shdr *shdr;
327
328 ret = rproc_elf32_sanity_check(fw_addr, fw_size);
329 if (ret) {
330 pr_debug("Invalid ELF32 Image %d\n", ret);
331 return ERR_PTR(ret);
332 }
333
334 /* look for the resource table and handle it */
335 shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
336 name_table = (const char *)(elf_data +
337 shdr[ehdr->e_shstrndx].sh_offset);
338
339 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
340 u32 size = shdr->sh_size;
341 u32 offset = shdr->sh_offset;
342
343 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
344 continue;
345
346 table = (struct resource_table *)(elf_data + offset);
347
348 /* make sure we have the entire table */
349 if (offset + size > fw_size) {
350 pr_debug("resource table truncated\n");
351 return ERR_PTR(-ENOSPC);
352 }
353
354 /* make sure table has at least the header */
355 if (sizeof(*table) > size) {
356 pr_debug("header-less resource table\n");
357 return ERR_PTR(-ENOSPC);
358 }
359
360 /* we don't support any version beyond the first */
361 if (table->ver != 1) {
362 pr_debug("unsupported fw ver: %d\n", table->ver);
363 return ERR_PTR(-EPROTONOSUPPORT);
364 }
365
366 /* make sure reserved bytes are zeroes */
367 if (table->reserved[0] || table->reserved[1]) {
368 pr_debug("non zero reserved bytes\n");
369 return ERR_PTR(-EBADF);
370 }
371
372 /* make sure the offsets array isn't truncated */
373 if (table->num * sizeof(table->offset[0]) +
374 sizeof(*table) > size) {
375 pr_debug("resource table incomplete\n");
376 return ERR_PTR(-ENOSPC);
377 }
378
379 return shdr;
380 }
381
382 return NULL;
383}
384
385/* Load the resource table from an ELF32 image */
386int rproc_elf32_load_rsc_table(struct udevice *dev, ulong fw_addr,
387 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
388{
389 const struct dm_rproc_ops *ops;
390 Elf32_Shdr *shdr;
391 void *src, *dst;
392
393 shdr = rproc_elf32_find_rsc_table(dev, fw_addr, fw_size);
394 if (!shdr)
395 return -ENODATA;
396 if (IS_ERR(shdr))
397 return PTR_ERR(shdr);
398
399 ops = rproc_get_ops(dev);
400 *rsc_addr = (ulong)shdr->sh_addr;
401 *rsc_size = (ulong)shdr->sh_size;
402
403 src = (void *)fw_addr + shdr->sh_offset;
404 if (ops->device_to_virt)
405 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
406 else
407 dst = (void *)rsc_addr;
408
409 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
410 (ulong)dst, *rsc_size);
411
412 memcpy(dst, src, *rsc_size);
413 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
414 roundup((unsigned long)dst + *rsc_size,
415 ARCH_DMA_MINALIGN) -
416 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
417
418 return 0;
419}
420
421/*
422 * Search for the resource table in an ELF64 image.
423 * Returns the address of the resource table section if found, NULL if there is
424 * no resource table section, or error pointer.
425 */
426static Elf64_Shdr *rproc_elf64_find_rsc_table(struct udevice *dev,
427 ulong fw_addr, ulong fw_size)
428{
429 int ret;
430 unsigned int i;
431 const char *name_table;
432 struct resource_table *table;
433 const u8 *elf_data = (void *)fw_addr;
434 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)fw_addr;
435 Elf64_Shdr *shdr;
436
437 ret = rproc_elf64_sanity_check(fw_addr, fw_size);
438 if (ret) {
439 pr_debug("Invalid ELF64 Image %d\n", ret);
440 return ERR_PTR(ret);
441 }
442
443 /* look for the resource table and handle it */
444 shdr = (Elf64_Shdr *)(elf_data + ehdr->e_shoff);
445 name_table = (const char *)(elf_data +
446 shdr[ehdr->e_shstrndx].sh_offset);
447
448 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
449 u64 size = shdr->sh_size;
450 u64 offset = shdr->sh_offset;
451
452 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
453 continue;
454
455 table = (struct resource_table *)(elf_data + offset);
456
457 /* make sure we have the entire table */
458 if (offset + size > fw_size) {
459 pr_debug("resource table truncated\n");
460 return ERR_PTR(-ENOSPC);
461 }
462
463 /* make sure table has at least the header */
464 if (sizeof(*table) > size) {
465 pr_debug("header-less resource table\n");
466 return ERR_PTR(-ENOSPC);
467 }
468
469 /* we don't support any version beyond the first */
470 if (table->ver != 1) {
471 pr_debug("unsupported fw ver: %d\n", table->ver);
472 return ERR_PTR(-EPROTONOSUPPORT);
473 }
474
475 /* make sure reserved bytes are zeroes */
476 if (table->reserved[0] || table->reserved[1]) {
477 pr_debug("non zero reserved bytes\n");
478 return ERR_PTR(-EBADF);
479 }
480
481 /* make sure the offsets array isn't truncated */
482 if (table->num * sizeof(table->offset[0]) +
483 sizeof(*table) > size) {
484 pr_debug("resource table incomplete\n");
485 return ERR_PTR(-ENOSPC);
486 }
487
488 return shdr;
489 }
490
491 return NULL;
492}
493
494/* Load the resource table from an ELF64 image */
495int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr,
496 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
497{
498 const struct dm_rproc_ops *ops;
499 Elf64_Shdr *shdr;
500 void *src, *dst;
501
502 shdr = rproc_elf64_find_rsc_table(dev, fw_addr, fw_size);
503 if (!shdr)
504 return -ENODATA;
505 if (IS_ERR(shdr))
506 return PTR_ERR(shdr);
507
508 ops = rproc_get_ops(dev);
509 *rsc_addr = (ulong)shdr->sh_addr;
510 *rsc_size = (ulong)shdr->sh_size;
511
512 src = (void *)fw_addr + shdr->sh_offset;
513 if (ops->device_to_virt)
514 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
515 else
516 dst = (void *)rsc_addr;
517
518 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
519 (ulong)dst, *rsc_size);
520
521 memcpy(dst, src, *rsc_size);
522 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
523 roundup((unsigned long)dst + *rsc_size,
524 ARCH_DMA_MINALIGN) -
525 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
526
527 return 0;
528}
529
530/* Load the resource table from an ELF32 or ELF64 image */
531int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr,
532 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
533
534{
535 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
536
537 if (!fw_addr)
538 return -EFAULT;
539
540 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
541 return rproc_elf64_load_rsc_table(dev, fw_addr, fw_size,
542 rsc_addr, rsc_size);
543 else
544 return rproc_elf32_load_rsc_table(dev, fw_addr, fw_size,
545 rsc_addr, rsc_size);
546}