nand: Update SPL MXS NAND mini driver
[oweals/u-boot.git] / drivers / remoteproc / rproc-elf-loader.c
index e8026cdfbb4631d122906afb2caa5cae76fb420d..f2e033aa741e14f83d8e33d4bfffbc9c5eba8d59 100644 (file)
@@ -7,6 +7,41 @@
 #include <dm.h>
 #include <elf.h>
 #include <remoteproc.h>
+#include <dm/device_compat.h>
+#include <linux/compat.h>
+
+/**
+ * struct resource_table - firmware resource table header
+ * @ver: version number
+ * @num: number of resource entries
+ * @reserved: reserved (must be zero)
+ * @offset: array of offsets pointing at the various resource entries
+ *
+ * A resource table is essentially a list of system resources required
+ * by the remote processor. It may also include configuration entries.
+ * If needed, the remote processor firmware should contain this table
+ * as a dedicated ".resource_table" ELF section.
+ *
+ * Some resources entries are mere announcements, where the host is informed
+ * of specific remoteproc configuration. Other entries require the host to
+ * do something (e.g. allocate a system resource). Sometimes a negotiation
+ * is expected, where the firmware requests a resource, and once allocated,
+ * the host should provide back its details (e.g. address of an allocated
+ * memory region).
+ *
+ * The header of the resource table, as expressed by this structure,
+ * contains a version number (should we need to change this format in the
+ * future), the number of available resource entries, and their offsets
+ * in the table.
+ *
+ * Immediately following this header are the resource entries themselves.
+ */
+struct resource_table {
+       u32 ver;
+       u32 num;
+       u32 reserved[2];
+       u32 offset[0];
+} __packed;
 
 /* Basic function to verify ELF32 image format */
 int rproc_elf32_sanity_check(ulong addr, ulong size)
@@ -156,7 +191,7 @@ int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size)
        ops = rproc_get_ops(dev);
 
        /* Load each program header */
-       for (i = 0; i < ehdr->e_phnum; ++i) {
+       for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
                void *dst = (void *)(uintptr_t)phdr->p_paddr;
                void *src = (void *)addr + phdr->p_offset;
 
@@ -178,7 +213,6 @@ int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size)
                            roundup((unsigned long)dst + phdr->p_filesz,
                                    ARCH_DMA_MINALIGN) -
                            rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
-               ++phdr;
        }
 
        return 0;
@@ -276,3 +310,239 @@ ulong rproc_elf_get_boot_addr(struct udevice *dev, ulong addr)
        else
                return rproc_elf32_get_boot_addr(addr);
 }
+
+/*
+ * Search for the resource table in an ELF32 image.
+ * Returns the address of the resource table section if found, NULL if there is
+ * no resource table section, or error pointer.
+ */
+static Elf32_Shdr *rproc_elf32_find_rsc_table(struct udevice *dev,
+                                             ulong fw_addr, ulong fw_size)
+{
+       int ret;
+       unsigned int i;
+       const char *name_table;
+       struct resource_table *table;
+       const u8 *elf_data = (void *)fw_addr;
+       Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
+       Elf32_Shdr *shdr;
+
+       ret = rproc_elf32_sanity_check(fw_addr, fw_size);
+       if (ret) {
+               pr_debug("Invalid ELF32 Image %d\n", ret);
+               return ERR_PTR(ret);
+       }
+
+       /* look for the resource table and handle it */
+       shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff);
+       name_table = (const char *)(elf_data +
+                                   shdr[ehdr->e_shstrndx].sh_offset);
+
+       for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
+               u32 size = shdr->sh_size;
+               u32 offset = shdr->sh_offset;
+
+               if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+                       continue;
+
+               table = (struct resource_table *)(elf_data + offset);
+
+               /* make sure we have the entire table */
+               if (offset + size > fw_size) {
+                       pr_debug("resource table truncated\n");
+                       return ERR_PTR(-ENOSPC);
+               }
+
+               /* make sure table has at least the header */
+               if (sizeof(*table) > size) {
+                       pr_debug("header-less resource table\n");
+                       return ERR_PTR(-ENOSPC);
+               }
+
+               /* we don't support any version beyond the first */
+               if (table->ver != 1) {
+                       pr_debug("unsupported fw ver: %d\n", table->ver);
+                       return ERR_PTR(-EPROTONOSUPPORT);
+               }
+
+               /* make sure reserved bytes are zeroes */
+               if (table->reserved[0] || table->reserved[1]) {
+                       pr_debug("non zero reserved bytes\n");
+                       return ERR_PTR(-EBADF);
+               }
+
+               /* make sure the offsets array isn't truncated */
+               if (table->num * sizeof(table->offset[0]) +
+                                sizeof(*table) > size) {
+                       pr_debug("resource table incomplete\n");
+                       return ERR_PTR(-ENOSPC);
+               }
+
+               return shdr;
+       }
+
+       return NULL;
+}
+
+/* Load the resource table from an ELF32 image */
+int rproc_elf32_load_rsc_table(struct udevice *dev, ulong fw_addr,
+                              ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
+{
+       const struct dm_rproc_ops *ops;
+       Elf32_Shdr *shdr;
+       void *src, *dst;
+
+       shdr = rproc_elf32_find_rsc_table(dev, fw_addr, fw_size);
+       if (!shdr)
+               return -ENODATA;
+       if (IS_ERR(shdr))
+               return PTR_ERR(shdr);
+
+       ops = rproc_get_ops(dev);
+       *rsc_addr = (ulong)shdr->sh_addr;
+       *rsc_size = (ulong)shdr->sh_size;
+
+       src = (void *)fw_addr + shdr->sh_offset;
+       if (ops->device_to_virt)
+               dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
+       else
+               dst = (void *)rsc_addr;
+
+       dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
+               (ulong)dst, *rsc_size);
+
+       memcpy(dst, src, *rsc_size);
+       flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
+                   roundup((unsigned long)dst + *rsc_size,
+                           ARCH_DMA_MINALIGN) -
+                   rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
+
+       return 0;
+}
+
+/*
+ * Search for the resource table in an ELF64 image.
+ * Returns the address of the resource table section if found, NULL if there is
+ * no resource table section, or error pointer.
+ */
+static Elf64_Shdr *rproc_elf64_find_rsc_table(struct udevice *dev,
+                                             ulong fw_addr, ulong fw_size)
+{
+       int ret;
+       unsigned int i;
+       const char *name_table;
+       struct resource_table *table;
+       const u8 *elf_data = (void *)fw_addr;
+       Elf64_Ehdr *ehdr = (Elf64_Ehdr *)fw_addr;
+       Elf64_Shdr *shdr;
+
+       ret = rproc_elf64_sanity_check(fw_addr, fw_size);
+       if (ret) {
+               pr_debug("Invalid ELF64 Image %d\n", ret);
+               return ERR_PTR(ret);
+       }
+
+       /* look for the resource table and handle it */
+       shdr = (Elf64_Shdr *)(elf_data + ehdr->e_shoff);
+       name_table = (const char *)(elf_data +
+                                   shdr[ehdr->e_shstrndx].sh_offset);
+
+       for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
+               u64 size = shdr->sh_size;
+               u64 offset = shdr->sh_offset;
+
+               if (strcmp(name_table + shdr->sh_name, ".resource_table"))
+                       continue;
+
+               table = (struct resource_table *)(elf_data + offset);
+
+               /* make sure we have the entire table */
+               if (offset + size > fw_size) {
+                       pr_debug("resource table truncated\n");
+                       return ERR_PTR(-ENOSPC);
+               }
+
+               /* make sure table has at least the header */
+               if (sizeof(*table) > size) {
+                       pr_debug("header-less resource table\n");
+                       return ERR_PTR(-ENOSPC);
+               }
+
+               /* we don't support any version beyond the first */
+               if (table->ver != 1) {
+                       pr_debug("unsupported fw ver: %d\n", table->ver);
+                       return ERR_PTR(-EPROTONOSUPPORT);
+               }
+
+               /* make sure reserved bytes are zeroes */
+               if (table->reserved[0] || table->reserved[1]) {
+                       pr_debug("non zero reserved bytes\n");
+                       return ERR_PTR(-EBADF);
+               }
+
+               /* make sure the offsets array isn't truncated */
+               if (table->num * sizeof(table->offset[0]) +
+                                sizeof(*table) > size) {
+                       pr_debug("resource table incomplete\n");
+                       return ERR_PTR(-ENOSPC);
+               }
+
+               return shdr;
+       }
+
+       return NULL;
+}
+
+/* Load the resource table from an ELF64 image */
+int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr,
+                              ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
+{
+       const struct dm_rproc_ops *ops;
+       Elf64_Shdr *shdr;
+       void *src, *dst;
+
+       shdr = rproc_elf64_find_rsc_table(dev, fw_addr, fw_size);
+       if (!shdr)
+               return -ENODATA;
+       if (IS_ERR(shdr))
+               return PTR_ERR(shdr);
+
+       ops = rproc_get_ops(dev);
+       *rsc_addr = (ulong)shdr->sh_addr;
+       *rsc_size = (ulong)shdr->sh_size;
+
+       src = (void *)fw_addr + shdr->sh_offset;
+       if (ops->device_to_virt)
+               dst = (void *)ops->device_to_virt(dev, *rsc_addr, *rsc_size);
+       else
+               dst = (void *)rsc_addr;
+
+       dev_dbg(dev, "Loading resource table to 0x%8lx (%ld bytes)\n",
+               (ulong)dst, *rsc_size);
+
+       memcpy(dst, src, *rsc_size);
+       flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
+                   roundup((unsigned long)dst + *rsc_size,
+                           ARCH_DMA_MINALIGN) -
+                   rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));
+
+       return 0;
+}
+
+/* Load the resource table from an ELF32 or ELF64 image */
+int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr,
+                            ulong fw_size, ulong *rsc_addr, ulong *rsc_size)
+
+{
+       Elf32_Ehdr *ehdr = (Elf32_Ehdr *)fw_addr;
+
+       if (!fw_addr)
+               return -EFAULT;
+
+       if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
+               return rproc_elf64_load_rsc_table(dev, fw_addr, fw_size,
+                                                 rsc_addr, rsc_size);
+       else
+               return rproc_elf32_load_rsc_table(dev, fw_addr, fw_size,
+                                                 rsc_addr, rsc_size);
+}