1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
9 #include <remoteproc.h>
10 #include <asm/cache.h>
11 #include <dm/device_compat.h>
12 #include <linux/compat.h>
15 * struct resource_table - firmware resource table header
16 * @ver: version number
17 * @num: number of resource entries
18 * @reserved: reserved (must be zero)
19 * @offset: array of offsets pointing at the various resource entries
21 * A resource table is essentially a list of system resources required
22 * by the remote processor. It may also include configuration entries.
23 * If needed, the remote processor firmware should contain this table
24 * as a dedicated ".resource_table" ELF section.
26 * Some resources entries are mere announcements, where the host is informed
27 * of specific remoteproc configuration. Other entries require the host to
28 * do something (e.g. allocate a system resource). Sometimes a negotiation
29 * is expected, where the firmware requests a resource, and once allocated,
30 * the host should provide back its details (e.g. address of an allocated
33 * The header of the resource table, as expressed by this structure,
34 * contains a version number (should we need to change this format in the
35 * future), the number of available resource entries, and their offsets
38 * Immediately following this header are the resource entries themselves.
40 struct resource_table {
47 /* Basic function to verify ELF32 image format */
48 int rproc_elf32_sanity_check(ulong addr, ulong size)
54 pr_debug("Invalid fw address?\n");
58 if (size < sizeof(Elf32_Ehdr)) {
59 pr_debug("Image is too small\n");
63 ehdr = (Elf32_Ehdr *)addr;
64 class = ehdr->e_ident[EI_CLASS];
66 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS32) {
67 pr_debug("Not an executable ELF32 image\n");
68 return -EPROTONOSUPPORT;
71 /* We assume the firmware has the same endianness as the host */
72 # ifdef __LITTLE_ENDIAN
73 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
74 # else /* BIG ENDIAN */
75 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
77 pr_debug("Unsupported firmware endianness\n");
81 if (size < ehdr->e_shoff + sizeof(Elf32_Shdr)) {
82 pr_debug("Image is too small\n");
86 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
87 pr_debug("Image is corrupted (bad magic)\n");
91 if (ehdr->e_phnum == 0) {
92 pr_debug("No loadable segments\n");
96 if (ehdr->e_phoff > size) {
97 pr_debug("Firmware size is too small\n");
104 /* Basic function to verify ELF64 image format */
105 int rproc_elf64_sanity_check(ulong addr, ulong size)
107 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
111 pr_debug("Invalid fw address?\n");
115 if (size < sizeof(Elf64_Ehdr)) {
116 pr_debug("Image is too small\n");
120 class = ehdr->e_ident[EI_CLASS];
122 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS64) {
123 pr_debug("Not an executable ELF64 image\n");
124 return -EPROTONOSUPPORT;
127 /* We assume the firmware has the same endianness as the host */
128 # ifdef __LITTLE_ENDIAN
129 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
130 # else /* BIG ENDIAN */
131 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
133 pr_debug("Unsupported firmware endianness\n");
137 if (size < ehdr->e_shoff + sizeof(Elf64_Shdr)) {
138 pr_debug("Image is too small\n");
142 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
143 pr_debug("Image is corrupted (bad magic)\n");
147 if (ehdr->e_phnum == 0) {
148 pr_debug("No loadable segments\n");
152 if (ehdr->e_phoff > size) {
153 pr_debug("Firmware size is too small\n");
160 /* Basic function to verify ELF image format */
161 int rproc_elf_sanity_check(ulong addr, ulong size)
163 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
166 dev_err(dev, "Invalid firmware address\n");
170 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
171 return rproc_elf64_sanity_check(addr, size);
173 return rproc_elf32_sanity_check(addr, size);
176 int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size)
178 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
179 Elf32_Phdr *phdr; /* Program header structure pointer */
180 const struct dm_rproc_ops *ops;
183 ret = rproc_elf32_sanity_check(addr, size);
185 dev_err(dev, "Invalid ELF32 Image %d\n", ret);
189 ehdr = (Elf32_Ehdr *)addr;
190 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
192 ops = rproc_get_ops(dev);
194 /* Load each program header */
195 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
196 void *dst = (void *)(uintptr_t)phdr->p_paddr;
197 void *src = (void *)addr + phdr->p_offset;
199 if (phdr->p_type != PT_LOAD)
202 if (ops->device_to_virt)
203 dst = ops->device_to_virt(dev, (ulong)dst,
206 dev_dbg(dev, "Loading phdr %i to 0x%p (%i bytes)\n",
207 i, dst, phdr->p_filesz);
209 memcpy(dst, src, phdr->p_filesz);
210 if (phdr->p_filesz != phdr->p_memsz)
211 memset(dst + phdr->p_filesz, 0x00,
212 phdr->p_memsz - phdr->p_filesz);
213 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
214 roundup((unsigned long)dst + phdr->p_filesz,
216 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
222 int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size)
224 const struct dm_rproc_ops *ops = rproc_get_ops(dev);
225 u64 da, memsz, filesz, offset;
231 dev_dbg(dev, "%s: addr = 0x%lx size = 0x%lx\n", __func__, addr, size);
233 if (rproc_elf64_sanity_check(addr, size))
236 ehdr = (Elf64_Ehdr *)addr;
237 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
239 /* go through the available ELF segments */
240 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
242 memsz = phdr->p_memsz;
243 filesz = phdr->p_filesz;
244 offset = phdr->p_offset;
246 if (phdr->p_type != PT_LOAD)
249 dev_dbg(dev, "%s:phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
250 __func__, phdr->p_type, da, memsz, filesz);
252 ptr = (void *)(uintptr_t)da;
253 if (ops->device_to_virt) {
254 ptr = ops->device_to_virt(dev, da, phdr->p_memsz);
256 dev_err(dev, "bad da 0x%llx mem 0x%llx\n", da,
264 memcpy(ptr, (void *)addr + offset, filesz);
266 memset(ptr + filesz, 0x00, memsz - filesz);
268 flush_cache(rounddown((ulong)ptr, ARCH_DMA_MINALIGN),
269 roundup((ulong)ptr + filesz, ARCH_DMA_MINALIGN) -
270 rounddown((ulong)ptr, ARCH_DMA_MINALIGN));
276 int rproc_elf_load_image(struct udevice *dev, ulong addr, ulong size)
278 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
281 dev_err(dev, "Invalid firmware address\n");
285 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
286 return rproc_elf64_load_image(dev, addr, size);
288 return rproc_elf32_load_image(dev, addr, size);
291 static ulong rproc_elf32_get_boot_addr(ulong addr)
293 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
295 return ehdr->e_entry;
298 static ulong rproc_elf64_get_boot_addr(ulong addr)
300 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
302 return ehdr->e_entry;
305 ulong rproc_elf_get_boot_addr(struct udevice *dev, ulong addr)
307 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
309 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
310 return rproc_elf64_get_boot_addr(addr);
312 return rproc_elf32_get_boot_addr(addr);
316 * Search for the resource table in an ELF32 image.
317 * Returns the address of the resource table section if found, NULL if there is
318 * no resource table section, or error pointer.
320 static Elf32_Shdr *rproc_elf32_find_rsc_table(struct udevice *dev,
321 ulong fw_addr, ulong fw_size)
325 const char *name_table;
326 struct resource_table *table;
327 const u8 *elf_data = (void *)fw_addr;
328 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
331 ret = rproc_elf32_sanity_check(fw_addr, fw_size);
333 pr_debug("Invalid ELF32 Image %d\n", ret);
337 /* look for the resource table and handle it */
338 shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
339 name_table = (const char *)(elf_data +
340 shdr[ehdr->e_shstrndx].sh_offset);
342 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
343 u32 size = shdr->sh_size;
344 u32 offset = shdr->sh_offset;
346 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
349 table = (struct resource_table *)(elf_data + offset);
351 /* make sure we have the entire table */
352 if (offset + size > fw_size) {
353 pr_debug("resource table truncated\n");
354 return ERR_PTR(-ENOSPC);
357 /* make sure table has at least the header */
358 if (sizeof(*table) > size) {
359 pr_debug("header-less resource table\n");
360 return ERR_PTR(-ENOSPC);
363 /* we don't support any version beyond the first */
364 if (table->ver != 1) {
365 pr_debug("unsupported fw ver: %d\n", table->ver);
366 return ERR_PTR(-EPROTONOSUPPORT);
369 /* make sure reserved bytes are zeroes */
370 if (table->reserved[0] || table->reserved[1]) {
371 pr_debug("non zero reserved bytes\n");
372 return ERR_PTR(-EBADF);
375 /* make sure the offsets array isn't truncated */
376 if (table->num * sizeof(table->offset[0]) +
377 sizeof(*table) > size) {
378 pr_debug("resource table incomplete\n");
379 return ERR_PTR(-ENOSPC);
388 /* Load the resource table from an ELF32 image */
389 int rproc_elf32_load_rsc_table(struct udevice *dev, ulong fw_addr,
390 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
392 const struct dm_rproc_ops *ops;
396 shdr = rproc_elf32_find_rsc_table(dev, fw_addr, fw_size);
400 return PTR_ERR(shdr);
402 ops = rproc_get_ops(dev);
403 *rsc_addr = (ulong)shdr->sh_addr;
404 *rsc_size = (ulong)shdr->sh_size;
406 src = (void *)fw_addr + shdr->sh_offset;
407 if (ops->device_to_virt)
408 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
410 dst = (void *)rsc_addr;
412 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
413 (ulong)dst, *rsc_size);
415 memcpy(dst, src, *rsc_size);
416 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
417 roundup((unsigned long)dst + *rsc_size,
419 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
425 * Search for the resource table in an ELF64 image.
426 * Returns the address of the resource table section if found, NULL if there is
427 * no resource table section, or error pointer.
429 static Elf64_Shdr *rproc_elf64_find_rsc_table(struct udevice *dev,
430 ulong fw_addr, ulong fw_size)
434 const char *name_table;
435 struct resource_table *table;
436 const u8 *elf_data = (void *)fw_addr;
437 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)fw_addr;
440 ret = rproc_elf64_sanity_check(fw_addr, fw_size);
442 pr_debug("Invalid ELF64 Image %d\n", ret);
446 /* look for the resource table and handle it */
447 shdr = (Elf64_Shdr *)(elf_data + ehdr->e_shoff);
448 name_table = (const char *)(elf_data +
449 shdr[ehdr->e_shstrndx].sh_offset);
451 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
452 u64 size = shdr->sh_size;
453 u64 offset = shdr->sh_offset;
455 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
458 table = (struct resource_table *)(elf_data + offset);
460 /* make sure we have the entire table */
461 if (offset + size > fw_size) {
462 pr_debug("resource table truncated\n");
463 return ERR_PTR(-ENOSPC);
466 /* make sure table has at least the header */
467 if (sizeof(*table) > size) {
468 pr_debug("header-less resource table\n");
469 return ERR_PTR(-ENOSPC);
472 /* we don't support any version beyond the first */
473 if (table->ver != 1) {
474 pr_debug("unsupported fw ver: %d\n", table->ver);
475 return ERR_PTR(-EPROTONOSUPPORT);
478 /* make sure reserved bytes are zeroes */
479 if (table->reserved[0] || table->reserved[1]) {
480 pr_debug("non zero reserved bytes\n");
481 return ERR_PTR(-EBADF);
484 /* make sure the offsets array isn't truncated */
485 if (table->num * sizeof(table->offset[0]) +
486 sizeof(*table) > size) {
487 pr_debug("resource table incomplete\n");
488 return ERR_PTR(-ENOSPC);
497 /* Load the resource table from an ELF64 image */
498 int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr,
499 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
501 const struct dm_rproc_ops *ops;
505 shdr = rproc_elf64_find_rsc_table(dev, fw_addr, fw_size);
509 return PTR_ERR(shdr);
511 ops = rproc_get_ops(dev);
512 *rsc_addr = (ulong)shdr->sh_addr;
513 *rsc_size = (ulong)shdr->sh_size;
515 src = (void *)fw_addr + shdr->sh_offset;
516 if (ops->device_to_virt)
517 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
519 dst = (void *)rsc_addr;
521 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
522 (ulong)dst, *rsc_size);
524 memcpy(dst, src, *rsc_size);
525 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
526 roundup((unsigned long)dst + *rsc_size,
528 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
533 /* Load the resource table from an ELF32 or ELF64 image */
534 int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr,
535 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
538 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
543 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
544 return rproc_elf64_load_rsc_table(dev, fw_addr, fw_size,
547 return rproc_elf32_load_rsc_table(dev, fw_addr, fw_size,