1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
10 #include <remoteproc.h>
11 #include <asm/cache.h>
12 #include <dm/device_compat.h>
13 #include <linux/compat.h>
16 * struct resource_table - firmware resource table header
17 * @ver: version number
18 * @num: number of resource entries
19 * @reserved: reserved (must be zero)
20 * @offset: array of offsets pointing at the various resource entries
22 * A resource table is essentially a list of system resources required
23 * by the remote processor. It may also include configuration entries.
24 * If needed, the remote processor firmware should contain this table
25 * as a dedicated ".resource_table" ELF section.
27 * Some resources entries are mere announcements, where the host is informed
28 * of specific remoteproc configuration. Other entries require the host to
29 * do something (e.g. allocate a system resource). Sometimes a negotiation
30 * is expected, where the firmware requests a resource, and once allocated,
31 * the host should provide back its details (e.g. address of an allocated
34 * The header of the resource table, as expressed by this structure,
35 * contains a version number (should we need to change this format in the
36 * future), the number of available resource entries, and their offsets
39 * Immediately following this header are the resource entries themselves.
41 struct resource_table {
48 /* Basic function to verify ELF32 image format */
49 int rproc_elf32_sanity_check(ulong addr, ulong size)
55 pr_debug("Invalid fw address?\n");
59 if (size < sizeof(Elf32_Ehdr)) {
60 pr_debug("Image is too small\n");
64 ehdr = (Elf32_Ehdr *)addr;
65 class = ehdr->e_ident[EI_CLASS];
67 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS32) {
68 pr_debug("Not an executable ELF32 image\n");
69 return -EPROTONOSUPPORT;
72 /* We assume the firmware has the same endianness as the host */
73 # ifdef __LITTLE_ENDIAN
74 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
75 # else /* BIG ENDIAN */
76 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
78 pr_debug("Unsupported firmware endianness\n");
82 if (size < ehdr->e_shoff + sizeof(Elf32_Shdr)) {
83 pr_debug("Image is too small\n");
87 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
88 pr_debug("Image is corrupted (bad magic)\n");
92 if (ehdr->e_phnum == 0) {
93 pr_debug("No loadable segments\n");
97 if (ehdr->e_phoff > size) {
98 pr_debug("Firmware size is too small\n");
105 /* Basic function to verify ELF64 image format */
106 int rproc_elf64_sanity_check(ulong addr, ulong size)
108 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
112 pr_debug("Invalid fw address?\n");
116 if (size < sizeof(Elf64_Ehdr)) {
117 pr_debug("Image is too small\n");
121 class = ehdr->e_ident[EI_CLASS];
123 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS64) {
124 pr_debug("Not an executable ELF64 image\n");
125 return -EPROTONOSUPPORT;
128 /* We assume the firmware has the same endianness as the host */
129 # ifdef __LITTLE_ENDIAN
130 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
131 # else /* BIG ENDIAN */
132 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
134 pr_debug("Unsupported firmware endianness\n");
138 if (size < ehdr->e_shoff + sizeof(Elf64_Shdr)) {
139 pr_debug("Image is too small\n");
143 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
144 pr_debug("Image is corrupted (bad magic)\n");
148 if (ehdr->e_phnum == 0) {
149 pr_debug("No loadable segments\n");
153 if (ehdr->e_phoff > size) {
154 pr_debug("Firmware size is too small\n");
161 /* Basic function to verify ELF image format */
162 int rproc_elf_sanity_check(ulong addr, ulong size)
164 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
167 dev_err(dev, "Invalid firmware address\n");
171 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
172 return rproc_elf64_sanity_check(addr, size);
174 return rproc_elf32_sanity_check(addr, size);
177 int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size)
179 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
180 Elf32_Phdr *phdr; /* Program header structure pointer */
181 const struct dm_rproc_ops *ops;
184 ret = rproc_elf32_sanity_check(addr, size);
186 dev_err(dev, "Invalid ELF32 Image %d\n", ret);
190 ehdr = (Elf32_Ehdr *)addr;
191 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
193 ops = rproc_get_ops(dev);
195 /* Load each program header */
196 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
197 void *dst = (void *)(uintptr_t)phdr->p_paddr;
198 void *src = (void *)addr + phdr->p_offset;
200 if (phdr->p_type != PT_LOAD)
203 if (ops->device_to_virt)
204 dst = ops->device_to_virt(dev, (ulong)dst,
207 dev_dbg(dev, "Loading phdr %i to 0x%p (%i bytes)\n",
208 i, dst, phdr->p_filesz);
210 memcpy(dst, src, phdr->p_filesz);
211 if (phdr->p_filesz != phdr->p_memsz)
212 memset(dst + phdr->p_filesz, 0x00,
213 phdr->p_memsz - phdr->p_filesz);
214 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
215 roundup((unsigned long)dst + phdr->p_filesz,
217 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
223 int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size)
225 const struct dm_rproc_ops *ops = rproc_get_ops(dev);
226 u64 da, memsz, filesz, offset;
232 dev_dbg(dev, "%s: addr = 0x%lx size = 0x%lx\n", __func__, addr, size);
234 if (rproc_elf64_sanity_check(addr, size))
237 ehdr = (Elf64_Ehdr *)addr;
238 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
240 /* go through the available ELF segments */
241 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
243 memsz = phdr->p_memsz;
244 filesz = phdr->p_filesz;
245 offset = phdr->p_offset;
247 if (phdr->p_type != PT_LOAD)
250 dev_dbg(dev, "%s:phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
251 __func__, phdr->p_type, da, memsz, filesz);
253 ptr = (void *)(uintptr_t)da;
254 if (ops->device_to_virt) {
255 ptr = ops->device_to_virt(dev, da, phdr->p_memsz);
257 dev_err(dev, "bad da 0x%llx mem 0x%llx\n", da,
265 memcpy(ptr, (void *)addr + offset, filesz);
267 memset(ptr + filesz, 0x00, memsz - filesz);
269 flush_cache(rounddown((ulong)ptr, ARCH_DMA_MINALIGN),
270 roundup((ulong)ptr + filesz, ARCH_DMA_MINALIGN) -
271 rounddown((ulong)ptr, ARCH_DMA_MINALIGN));
277 int rproc_elf_load_image(struct udevice *dev, ulong addr, ulong size)
279 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
282 dev_err(dev, "Invalid firmware address\n");
286 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
287 return rproc_elf64_load_image(dev, addr, size);
289 return rproc_elf32_load_image(dev, addr, size);
292 static ulong rproc_elf32_get_boot_addr(ulong addr)
294 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
296 return ehdr->e_entry;
299 static ulong rproc_elf64_get_boot_addr(ulong addr)
301 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
303 return ehdr->e_entry;
306 ulong rproc_elf_get_boot_addr(struct udevice *dev, ulong addr)
308 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
310 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
311 return rproc_elf64_get_boot_addr(addr);
313 return rproc_elf32_get_boot_addr(addr);
317 * Search for the resource table in an ELF32 image.
318 * Returns the address of the resource table section if found, NULL if there is
319 * no resource table section, or error pointer.
321 static Elf32_Shdr *rproc_elf32_find_rsc_table(struct udevice *dev,
322 ulong fw_addr, ulong fw_size)
326 const char *name_table;
327 struct resource_table *table;
328 const u8 *elf_data = (void *)fw_addr;
329 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
332 ret = rproc_elf32_sanity_check(fw_addr, fw_size);
334 pr_debug("Invalid ELF32 Image %d\n", ret);
338 /* look for the resource table and handle it */
339 shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
340 name_table = (const char *)(elf_data +
341 shdr[ehdr->e_shstrndx].sh_offset);
343 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
344 u32 size = shdr->sh_size;
345 u32 offset = shdr->sh_offset;
347 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
350 table = (struct resource_table *)(elf_data + offset);
352 /* make sure we have the entire table */
353 if (offset + size > fw_size) {
354 pr_debug("resource table truncated\n");
355 return ERR_PTR(-ENOSPC);
358 /* make sure table has at least the header */
359 if (sizeof(*table) > size) {
360 pr_debug("header-less resource table\n");
361 return ERR_PTR(-ENOSPC);
364 /* we don't support any version beyond the first */
365 if (table->ver != 1) {
366 pr_debug("unsupported fw ver: %d\n", table->ver);
367 return ERR_PTR(-EPROTONOSUPPORT);
370 /* make sure reserved bytes are zeroes */
371 if (table->reserved[0] || table->reserved[1]) {
372 pr_debug("non zero reserved bytes\n");
373 return ERR_PTR(-EBADF);
376 /* make sure the offsets array isn't truncated */
377 if (table->num * sizeof(table->offset[0]) +
378 sizeof(*table) > size) {
379 pr_debug("resource table incomplete\n");
380 return ERR_PTR(-ENOSPC);
389 /* Load the resource table from an ELF32 image */
390 int rproc_elf32_load_rsc_table(struct udevice *dev, ulong fw_addr,
391 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
393 const struct dm_rproc_ops *ops;
397 shdr = rproc_elf32_find_rsc_table(dev, fw_addr, fw_size);
401 return PTR_ERR(shdr);
403 ops = rproc_get_ops(dev);
404 *rsc_addr = (ulong)shdr->sh_addr;
405 *rsc_size = (ulong)shdr->sh_size;
407 src = (void *)fw_addr + shdr->sh_offset;
408 if (ops->device_to_virt)
409 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
411 dst = (void *)rsc_addr;
413 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
414 (ulong)dst, *rsc_size);
416 memcpy(dst, src, *rsc_size);
417 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
418 roundup((unsigned long)dst + *rsc_size,
420 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
426 * Search for the resource table in an ELF64 image.
427 * Returns the address of the resource table section if found, NULL if there is
428 * no resource table section, or error pointer.
430 static Elf64_Shdr *rproc_elf64_find_rsc_table(struct udevice *dev,
431 ulong fw_addr, ulong fw_size)
435 const char *name_table;
436 struct resource_table *table;
437 const u8 *elf_data = (void *)fw_addr;
438 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)fw_addr;
441 ret = rproc_elf64_sanity_check(fw_addr, fw_size);
443 pr_debug("Invalid ELF64 Image %d\n", ret);
447 /* look for the resource table and handle it */
448 shdr = (Elf64_Shdr *)(elf_data + ehdr->e_shoff);
449 name_table = (const char *)(elf_data +
450 shdr[ehdr->e_shstrndx].sh_offset);
452 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
453 u64 size = shdr->sh_size;
454 u64 offset = shdr->sh_offset;
456 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
459 table = (struct resource_table *)(elf_data + offset);
461 /* make sure we have the entire table */
462 if (offset + size > fw_size) {
463 pr_debug("resource table truncated\n");
464 return ERR_PTR(-ENOSPC);
467 /* make sure table has at least the header */
468 if (sizeof(*table) > size) {
469 pr_debug("header-less resource table\n");
470 return ERR_PTR(-ENOSPC);
473 /* we don't support any version beyond the first */
474 if (table->ver != 1) {
475 pr_debug("unsupported fw ver: %d\n", table->ver);
476 return ERR_PTR(-EPROTONOSUPPORT);
479 /* make sure reserved bytes are zeroes */
480 if (table->reserved[0] || table->reserved[1]) {
481 pr_debug("non zero reserved bytes\n");
482 return ERR_PTR(-EBADF);
485 /* make sure the offsets array isn't truncated */
486 if (table->num * sizeof(table->offset[0]) +
487 sizeof(*table) > size) {
488 pr_debug("resource table incomplete\n");
489 return ERR_PTR(-ENOSPC);
498 /* Load the resource table from an ELF64 image */
499 int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr,
500 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
502 const struct dm_rproc_ops *ops;
506 shdr = rproc_elf64_find_rsc_table(dev, fw_addr, fw_size);
510 return PTR_ERR(shdr);
512 ops = rproc_get_ops(dev);
513 *rsc_addr = (ulong)shdr->sh_addr;
514 *rsc_size = (ulong)shdr->sh_size;
516 src = (void *)fw_addr + shdr->sh_offset;
517 if (ops->device_to_virt)
518 dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
520 dst = (void *)rsc_addr;
522 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
523 (ulong)dst, *rsc_size);
525 memcpy(dst, src, *rsc_size);
526 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
527 roundup((unsigned long)dst + *rsc_size,
529 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
534 /* Load the resource table from an ELF32 or ELF64 image */
535 int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr,
536 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
539 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
544 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
545 return rproc_elf64_load_rsc_table(dev, fw_addr, fw_size,
548 return rproc_elf32_load_rsc_table(dev, fw_addr, fw_size,