Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Remote Processor Framework Elf loader
0004  *
0005  * Copyright (C) 2011 Texas Instruments, Inc.
0006  * Copyright (C) 2011 Google, Inc.
0007  *
0008  * Ohad Ben-Cohen <ohad@wizery.com>
0009  * Brian Swetland <swetland@google.com>
0010  * Mark Grosen <mgrosen@ti.com>
0011  * Fernando Guzman Lugo <fernando.lugo@ti.com>
0012  * Suman Anna <s-anna@ti.com>
0013  * Robert Tivy <rtivy@ti.com>
0014  * Armando Uribe De Leon <x0095078@ti.com>
0015  * Sjur Brændeland <sjur.brandeland@stericsson.com>
0016  */
0017 
0018 #define pr_fmt(fmt)    "%s: " fmt, __func__
0019 
0020 #include <linux/module.h>
0021 #include <linux/firmware.h>
0022 #include <linux/remoteproc.h>
0023 #include <linux/elf.h>
0024 
0025 #include "remoteproc_internal.h"
0026 #include "remoteproc_elf_helpers.h"
0027 
0028 /**
0029  * rproc_elf_sanity_check() - Sanity Check for ELF32/ELF64 firmware image
0030  * @rproc: the remote processor handle
0031  * @fw: the ELF firmware image
0032  *
0033  * Make sure this fw image is sane (ie a correct ELF32/ELF64 file).
0034  *
0035  * Return: 0 on success and -EINVAL upon any failure
0036  */
0037 int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
0038 {
0039     const char *name = rproc->firmware;
0040     struct device *dev = &rproc->dev;
0041     /*
0042      * Elf files are beginning with the same structure. Thus, to simplify
0043      * header parsing, we can use the elf32_hdr one for both elf64 and
0044      * elf32.
0045      */
0046     struct elf32_hdr *ehdr;
0047     u32 elf_shdr_get_size;
0048     u64 phoff, shoff;
0049     char class;
0050     u16 phnum;
0051 
0052     if (!fw) {
0053         dev_err(dev, "failed to load %s\n", name);
0054         return -EINVAL;
0055     }
0056 
0057     if (fw->size < sizeof(struct elf32_hdr)) {
0058         dev_err(dev, "Image is too small\n");
0059         return -EINVAL;
0060     }
0061 
0062     ehdr = (struct elf32_hdr *)fw->data;
0063 
0064     if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
0065         dev_err(dev, "Image is corrupted (bad magic)\n");
0066         return -EINVAL;
0067     }
0068 
0069     class = ehdr->e_ident[EI_CLASS];
0070     if (class != ELFCLASS32 && class != ELFCLASS64) {
0071         dev_err(dev, "Unsupported class: %d\n", class);
0072         return -EINVAL;
0073     }
0074 
0075     if (class == ELFCLASS64 && fw->size < sizeof(struct elf64_hdr)) {
0076         dev_err(dev, "elf64 header is too small\n");
0077         return -EINVAL;
0078     }
0079 
0080     /* We assume the firmware has the same endianness as the host */
0081 # ifdef __LITTLE_ENDIAN
0082     if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
0083 # else /* BIG ENDIAN */
0084     if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
0085 # endif
0086         dev_err(dev, "Unsupported firmware endianness\n");
0087         return -EINVAL;
0088     }
0089 
0090     phoff = elf_hdr_get_e_phoff(class, fw->data);
0091     shoff = elf_hdr_get_e_shoff(class, fw->data);
0092     phnum =  elf_hdr_get_e_phnum(class, fw->data);
0093     elf_shdr_get_size = elf_size_of_shdr(class);
0094 
0095     if (fw->size < shoff + elf_shdr_get_size) {
0096         dev_err(dev, "Image is too small\n");
0097         return -EINVAL;
0098     }
0099 
0100     if (phnum == 0) {
0101         dev_err(dev, "No loadable segments\n");
0102         return -EINVAL;
0103     }
0104 
0105     if (phoff > fw->size) {
0106         dev_err(dev, "Firmware size is too small\n");
0107         return -EINVAL;
0108     }
0109 
0110     dev_dbg(dev, "Firmware is an elf%d file\n",
0111         class == ELFCLASS32 ? 32 : 64);
0112 
0113     return 0;
0114 }
0115 EXPORT_SYMBOL(rproc_elf_sanity_check);
0116 
0117 /**
0118  * rproc_elf_get_boot_addr() - Get rproc's boot address.
0119  * @rproc: the remote processor handle
0120  * @fw: the ELF firmware image
0121  *
0122  * Note that the boot address is not a configurable property of all remote
0123  * processors. Some will always boot at a specific hard-coded address.
0124  *
0125  * Return: entry point address of the ELF image
0126  *
0127  */
0128 u64 rproc_elf_get_boot_addr(struct rproc *rproc, const struct firmware *fw)
0129 {
0130     return elf_hdr_get_e_entry(fw_elf_get_class(fw), fw->data);
0131 }
0132 EXPORT_SYMBOL(rproc_elf_get_boot_addr);
0133 
0134 /**
0135  * rproc_elf_load_segments() - load firmware segments to memory
0136  * @rproc: remote processor which will be booted using these fw segments
0137  * @fw: the ELF firmware image
0138  *
0139  * This function loads the firmware segments to memory, where the remote
0140  * processor expects them.
0141  *
0142  * Some remote processors will expect their code and data to be placed
0143  * in specific device addresses, and can't have them dynamically assigned.
0144  *
0145  * We currently support only those kind of remote processors, and expect
0146  * the program header's paddr member to contain those addresses. We then go
0147  * through the physically contiguous "carveout" memory regions which we
0148  * allocated (and mapped) earlier on behalf of the remote processor,
0149  * and "translate" device address to kernel addresses, so we can copy the
0150  * segments where they are expected.
0151  *
0152  * Currently we only support remote processors that required carveout
0153  * allocations and got them mapped onto their iommus. Some processors
0154  * might be different: they might not have iommus, and would prefer to
0155  * directly allocate memory for every segment/resource. This is not yet
0156  * supported, though.
0157  *
0158  * Return: 0 on success and an appropriate error code otherwise
0159  */
0160 int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
0161 {
0162     struct device *dev = &rproc->dev;
0163     const void *ehdr, *phdr;
0164     int i, ret = 0;
0165     u16 phnum;
0166     const u8 *elf_data = fw->data;
0167     u8 class = fw_elf_get_class(fw);
0168     u32 elf_phdr_get_size = elf_size_of_phdr(class);
0169 
0170     ehdr = elf_data;
0171     phnum = elf_hdr_get_e_phnum(class, ehdr);
0172     phdr = elf_data + elf_hdr_get_e_phoff(class, ehdr);
0173 
0174     /* go through the available ELF segments */
0175     for (i = 0; i < phnum; i++, phdr += elf_phdr_get_size) {
0176         u64 da = elf_phdr_get_p_paddr(class, phdr);
0177         u64 memsz = elf_phdr_get_p_memsz(class, phdr);
0178         u64 filesz = elf_phdr_get_p_filesz(class, phdr);
0179         u64 offset = elf_phdr_get_p_offset(class, phdr);
0180         u32 type = elf_phdr_get_p_type(class, phdr);
0181         bool is_iomem = false;
0182         void *ptr;
0183 
0184         if (type != PT_LOAD || !memsz)
0185             continue;
0186 
0187         dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n",
0188             type, da, memsz, filesz);
0189 
0190         if (filesz > memsz) {
0191             dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n",
0192                 filesz, memsz);
0193             ret = -EINVAL;
0194             break;
0195         }
0196 
0197         if (offset + filesz > fw->size) {
0198             dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
0199                 offset + filesz, fw->size);
0200             ret = -EINVAL;
0201             break;
0202         }
0203 
0204         if (!rproc_u64_fit_in_size_t(memsz)) {
0205             dev_err(dev, "size (%llx) does not fit in size_t type\n",
0206                 memsz);
0207             ret = -EOVERFLOW;
0208             break;
0209         }
0210 
0211         /* grab the kernel address for this device address */
0212         ptr = rproc_da_to_va(rproc, da, memsz, &is_iomem);
0213         if (!ptr) {
0214             dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da,
0215                 memsz);
0216             ret = -EINVAL;
0217             break;
0218         }
0219 
0220         /* put the segment where the remote processor expects it */
0221         if (filesz) {
0222             if (is_iomem)
0223                 memcpy_toio((void __iomem *)ptr, elf_data + offset, filesz);
0224             else
0225                 memcpy(ptr, elf_data + offset, filesz);
0226         }
0227 
0228         /*
0229          * Zero out remaining memory for this segment.
0230          *
0231          * This isn't strictly required since dma_alloc_coherent already
0232          * did this for us. albeit harmless, we may consider removing
0233          * this.
0234          */
0235         if (memsz > filesz) {
0236             if (is_iomem)
0237                 memset_io((void __iomem *)(ptr + filesz), 0, memsz - filesz);
0238             else
0239                 memset(ptr + filesz, 0, memsz - filesz);
0240         }
0241     }
0242 
0243     return ret;
0244 }
0245 EXPORT_SYMBOL(rproc_elf_load_segments);
0246 
0247 static const void *
0248 find_table(struct device *dev, const struct firmware *fw)
0249 {
0250     const void *shdr, *name_table_shdr;
0251     int i;
0252     const char *name_table;
0253     struct resource_table *table = NULL;
0254     const u8 *elf_data = (void *)fw->data;
0255     u8 class = fw_elf_get_class(fw);
0256     size_t fw_size = fw->size;
0257     const void *ehdr = elf_data;
0258     u16 shnum = elf_hdr_get_e_shnum(class, ehdr);
0259     u32 elf_shdr_get_size = elf_size_of_shdr(class);
0260     u16 shstrndx = elf_hdr_get_e_shstrndx(class, ehdr);
0261 
0262     /* look for the resource table and handle it */
0263     /* First, get the section header according to the elf class */
0264     shdr = elf_data + elf_hdr_get_e_shoff(class, ehdr);
0265     /* Compute name table section header entry in shdr array */
0266     name_table_shdr = shdr + (shstrndx * elf_shdr_get_size);
0267     /* Finally, compute the name table section address in elf */
0268     name_table = elf_data + elf_shdr_get_sh_offset(class, name_table_shdr);
0269 
0270     for (i = 0; i < shnum; i++, shdr += elf_shdr_get_size) {
0271         u64 size = elf_shdr_get_sh_size(class, shdr);
0272         u64 offset = elf_shdr_get_sh_offset(class, shdr);
0273         u32 name = elf_shdr_get_sh_name(class, shdr);
0274 
0275         if (strcmp(name_table + name, ".resource_table"))
0276             continue;
0277 
0278         table = (struct resource_table *)(elf_data + offset);
0279 
0280         /* make sure we have the entire table */
0281         if (offset + size > fw_size || offset + size < size) {
0282             dev_err(dev, "resource table truncated\n");
0283             return NULL;
0284         }
0285 
0286         /* make sure table has at least the header */
0287         if (sizeof(struct resource_table) > size) {
0288             dev_err(dev, "header-less resource table\n");
0289             return NULL;
0290         }
0291 
0292         /* we don't support any version beyond the first */
0293         if (table->ver != 1) {
0294             dev_err(dev, "unsupported fw ver: %d\n", table->ver);
0295             return NULL;
0296         }
0297 
0298         /* make sure reserved bytes are zeroes */
0299         if (table->reserved[0] || table->reserved[1]) {
0300             dev_err(dev, "non zero reserved bytes\n");
0301             return NULL;
0302         }
0303 
0304         /* make sure the offsets array isn't truncated */
0305         if (struct_size(table, offset, table->num) > size) {
0306             dev_err(dev, "resource table incomplete\n");
0307             return NULL;
0308         }
0309 
0310         return shdr;
0311     }
0312 
0313     return NULL;
0314 }
0315 
0316 /**
0317  * rproc_elf_load_rsc_table() - load the resource table
0318  * @rproc: the rproc handle
0319  * @fw: the ELF firmware image
0320  *
0321  * This function finds the resource table inside the remote processor's
0322  * firmware, load it into the @cached_table and update @table_ptr.
0323  *
0324  * Return: 0 on success, negative errno on failure.
0325  */
0326 int rproc_elf_load_rsc_table(struct rproc *rproc, const struct firmware *fw)
0327 {
0328     const void *shdr;
0329     struct device *dev = &rproc->dev;
0330     struct resource_table *table = NULL;
0331     const u8 *elf_data = fw->data;
0332     size_t tablesz;
0333     u8 class = fw_elf_get_class(fw);
0334     u64 sh_offset;
0335 
0336     shdr = find_table(dev, fw);
0337     if (!shdr)
0338         return -EINVAL;
0339 
0340     sh_offset = elf_shdr_get_sh_offset(class, shdr);
0341     table = (struct resource_table *)(elf_data + sh_offset);
0342     tablesz = elf_shdr_get_sh_size(class, shdr);
0343 
0344     /*
0345      * Create a copy of the resource table. When a virtio device starts
0346      * and calls vring_new_virtqueue() the address of the allocated vring
0347      * will be stored in the cached_table. Before the device is started,
0348      * cached_table will be copied into device memory.
0349      */
0350     rproc->cached_table = kmemdup(table, tablesz, GFP_KERNEL);
0351     if (!rproc->cached_table)
0352         return -ENOMEM;
0353 
0354     rproc->table_ptr = rproc->cached_table;
0355     rproc->table_sz = tablesz;
0356 
0357     return 0;
0358 }
0359 EXPORT_SYMBOL(rproc_elf_load_rsc_table);
0360 
0361 /**
0362  * rproc_elf_find_loaded_rsc_table() - find the loaded resource table
0363  * @rproc: the rproc handle
0364  * @fw: the ELF firmware image
0365  *
0366  * This function finds the location of the loaded resource table. Don't
0367  * call this function if the table wasn't loaded yet - it's a bug if you do.
0368  *
0369  * Return: pointer to the resource table if it is found or NULL otherwise.
0370  * If the table wasn't loaded yet the result is unspecified.
0371  */
0372 struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
0373                                const struct firmware *fw)
0374 {
0375     const void *shdr;
0376     u64 sh_addr, sh_size;
0377     u8 class = fw_elf_get_class(fw);
0378     struct device *dev = &rproc->dev;
0379 
0380     shdr = find_table(&rproc->dev, fw);
0381     if (!shdr)
0382         return NULL;
0383 
0384     sh_addr = elf_shdr_get_sh_addr(class, shdr);
0385     sh_size = elf_shdr_get_sh_size(class, shdr);
0386 
0387     if (!rproc_u64_fit_in_size_t(sh_size)) {
0388         dev_err(dev, "size (%llx) does not fit in size_t type\n",
0389             sh_size);
0390         return NULL;
0391     }
0392 
0393     return rproc_da_to_va(rproc, sh_addr, sh_size, NULL);
0394 }
0395 EXPORT_SYMBOL(rproc_elf_find_loaded_rsc_table);