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 <linux/err.h>
13 * struct resource_table - firmware resource table header
14 * @ver: version number
15 * @num: number of resource entries
16 * @reserved: reserved (must be zero)
17 * @offset: array of offsets pointing at the various resource entries
19 * A resource table is essentially a list of system resources required
20 * by the remote processor. It may also include configuration entries.
21 * If needed, the remote processor firmware should contain this table
22 * as a dedicated ".resource_table" ELF section.
24 * Some resources entries are mere announcements, where the host is informed
25 * of specific remoteproc configuration. Other entries require the host to
26 * do something (e.g. allocate a system resource). Sometimes a negotiation
27 * is expected, where the firmware requests a resource, and once allocated,
28 * the host should provide back its details (e.g. address of an allocated
31 * The header of the resource table, as expressed by this structure,
32 * contains a version number (should we need to change this format in the
33 * future), the number of available resource entries, and their offsets
36 * Immediately following this header are the resource entries themselves.
38 struct resource_table {
45 /* Basic function to verify ELF32 image format */
46 int rproc_elf32_sanity_check(ulong addr, ulong size)
52 pr_debug("Invalid fw address?\n");
56 if (size < sizeof(Elf32_Ehdr)) {
57 pr_debug("Image is too small\n");
61 ehdr = (Elf32_Ehdr *)addr;
62 class = ehdr->e_ident[EI_CLASS];
64 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS32) {
65 pr_debug("Not an executable ELF32 image\n");
66 return -EPROTONOSUPPORT;
69 /* We assume the firmware has the same endianness as the host */
70 # ifdef __LITTLE_ENDIAN
71 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
72 # else /* BIG ENDIAN */
73 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
75 pr_debug("Unsupported firmware endianness\n");
79 if (size < ehdr->e_shoff + sizeof(Elf32_Shdr)) {
80 pr_debug("Image is too small\n");
84 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
85 pr_debug("Image is corrupted (bad magic)\n");
89 if (ehdr->e_phnum == 0) {
90 pr_debug("No loadable segments\n");
94 if (ehdr->e_phoff > size) {
95 pr_debug("Firmware size is too small\n");
102 /* Basic function to verify ELF64 image format */
103 int rproc_elf64_sanity_check(ulong addr, ulong size)
105 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
109 pr_debug("Invalid fw address?\n");
113 if (size < sizeof(Elf64_Ehdr)) {
114 pr_debug("Image is too small\n");
118 class = ehdr->e_ident[EI_CLASS];
120 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS64) {
121 pr_debug("Not an executable ELF64 image\n");
122 return -EPROTONOSUPPORT;
125 /* We assume the firmware has the same endianness as the host */
126 # ifdef __LITTLE_ENDIAN
127 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
128 # else /* BIG ENDIAN */
129 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
131 pr_debug("Unsupported firmware endianness\n");
135 if (size < ehdr->e_shoff + sizeof(Elf64_Shdr)) {
136 pr_debug("Image is too small\n");
140 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
141 pr_debug("Image is corrupted (bad magic)\n");
145 if (ehdr->e_phnum == 0) {
146 pr_debug("No loadable segments\n");
150 if (ehdr->e_phoff > size) {
151 pr_debug("Firmware size is too small\n");
158 /* Basic function to verify ELF image format */
159 int rproc_elf_sanity_check(ulong addr, ulong size)
161 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
164 dev_err(dev, "Invalid firmware address\n");
168 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
169 return rproc_elf64_sanity_check(addr, size);
171 return rproc_elf32_sanity_check(addr, size);
174 int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size)
176 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
177 Elf32_Phdr *phdr; /* Program header structure pointer */
178 const struct dm_rproc_ops *ops;
181 ret = rproc_elf32_sanity_check(addr, size);
183 dev_err(dev, "Invalid ELF32 Image %d\n", ret);
187 ehdr = (Elf32_Ehdr *)addr;
188 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
190 ops = rproc_get_ops(dev);
192 /* Load each program header */
193 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
194 void *dst = (void *)(uintptr_t)phdr->p_paddr;
195 void *src = (void *)addr + phdr->p_offset;
197 if (phdr->p_type != PT_LOAD)
200 if (ops->device_to_virt)
201 dst = ops->device_to_virt(dev, (ulong)dst,
204 dev_dbg(dev, "Loading phdr %i to 0x%p (%i bytes)\n",
205 i, dst, phdr->p_filesz);
207 memcpy(dst, src, phdr->p_filesz);
208 if (phdr->p_filesz != phdr->p_memsz)
209 memset(dst + phdr->p_filesz, 0x00,
210 phdr->p_memsz - phdr->p_filesz);
211 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
212 roundup((unsigned long)dst + phdr->p_filesz,
214 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
220 int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size)
222 const struct dm_rproc_ops *ops = rproc_get_ops(dev);
223 u64 da, memsz, filesz, offset;
229 dev_dbg(dev, "%s: addr = 0x%lx size = 0x%lx\n", __func__, addr, size);
231 if (rproc_elf64_sanity_check(addr, size))
234 ehdr = (Elf64_Ehdr *)addr;
235 phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff);
237 /* go through the available ELF segments */
238 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
240 memsz = phdr->p_memsz;
241 filesz = phdr->p_filesz;
242 offset = phdr->p_offset;
244 if (phdr->p_type != PT_LOAD)
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);
250 ptr = (void *)(uintptr_t)da;
251 if (ops->device_to_virt) {
252 ptr = ops->device_to_virt(dev, da, phdr->p_memsz);
254 dev_err(dev, "bad da 0x%llx mem 0x%llx\n", da,
262 memcpy(ptr, (void *)addr + offset, filesz);
264 memset(ptr + filesz, 0x00, memsz - filesz);
266 flush_cache(rounddown((ulong)ptr, ARCH_DMA_MINALIGN),
267 roundup((ulong)ptr + filesz, ARCH_DMA_MINALIGN) -
268 rounddown((ulong)ptr, ARCH_DMA_MINALIGN));
274 int rproc_elf_load_image(struct udevice *dev, ulong addr, ulong size)
276 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
279 dev_err(dev, "Invalid firmware address\n");
283 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
284 return rproc_elf64_load_image(dev, addr, size);
286 return rproc_elf32_load_image(dev, addr, size);
289 static ulong rproc_elf32_get_boot_addr(ulong addr)
291 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
293 return ehdr->e_entry;
296 static ulong rproc_elf64_get_boot_addr(ulong addr)
298 Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr;
300 return ehdr->e_entry;
303 ulong rproc_elf_get_boot_addr(struct udevice *dev, ulong addr)
305 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr;
307 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
308 return rproc_elf64_get_boot_addr(addr);
310 return rproc_elf32_get_boot_addr(addr);
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.
318 static Elf32_Shdr *rproc_elf32_find_rsc_table(struct udevice *dev,
319 ulong fw_addr, ulong fw_size)
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;
329 ret = rproc_elf32_sanity_check(fw_addr, fw_size);
331 pr_debug("Invalid ELF32 Image %d\n", ret);
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);
340 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
341 u32 size = shdr->sh_size;
342 u32 offset = shdr->sh_offset;
344 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
347 table = (struct resource_table *)(elf_data + offset);
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);
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);
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);
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);
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);
386 /* Load the resource table from an ELF32 image */
387 int rproc_elf32_load_rsc_table(struct udevice *dev, ulong fw_addr,
388 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
390 const struct dm_rproc_ops *ops;
394 shdr = rproc_elf32_find_rsc_table(dev, fw_addr, fw_size);
398 return PTR_ERR(shdr);
400 ops = rproc_get_ops(dev);
401 *rsc_addr = (ulong)shdr->sh_addr;
402 *rsc_size = (ulong)shdr->sh_size;
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);
408 dst = (void *)rsc_addr;
410 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
411 (ulong)dst, *rsc_size);
413 memcpy(dst, src, *rsc_size);
414 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
415 roundup((unsigned long)dst + *rsc_size,
417 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
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.
427 static Elf64_Shdr *rproc_elf64_find_rsc_table(struct udevice *dev,
428 ulong fw_addr, ulong fw_size)
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;
438 ret = rproc_elf64_sanity_check(fw_addr, fw_size);
440 pr_debug("Invalid ELF64 Image %d\n", ret);
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);
449 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
450 u64 size = shdr->sh_size;
451 u64 offset = shdr->sh_offset;
453 if (strcmp(name_table + shdr->sh_name, ".resource_table"))
456 table = (struct resource_table *)(elf_data + offset);
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);
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);
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);
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);
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);
495 /* Load the resource table from an ELF64 image */
496 int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr,
497 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
499 const struct dm_rproc_ops *ops;
503 shdr = rproc_elf64_find_rsc_table(dev, fw_addr, fw_size);
507 return PTR_ERR(shdr);
509 ops = rproc_get_ops(dev);
510 *rsc_addr = (ulong)shdr->sh_addr;
511 *rsc_size = (ulong)shdr->sh_size;
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);
517 dst = (void *)rsc_addr;
519 dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
520 (ulong)dst, *rsc_size);
522 memcpy(dst, src, *rsc_size);
523 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
524 roundup((unsigned long)dst + *rsc_size,
526 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
531 /* Load the resource table from an ELF32 or ELF64 image */
532 int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr,
533 ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
536 Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
541 if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
542 return rproc_elf64_load_rsc_table(dev, fw_addr, fw_size,
545 return rproc_elf32_load_rsc_table(dev, fw_addr, fw_size,