// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause /* * Copyright (C) 2019, STMicroelectronics - All Rights Reserved */ #include #include #include #include #include #include #include #include #include /** * 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) { Elf32_Ehdr *ehdr; char class; if (!addr) { pr_debug("Invalid fw address?\n"); return -EFAULT; } if (size < sizeof(Elf32_Ehdr)) { pr_debug("Image is too small\n"); return -ENOSPC; } ehdr = (Elf32_Ehdr *)addr; class = ehdr->e_ident[EI_CLASS]; if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS32) { pr_debug("Not an executable ELF32 image\n"); return -EPROTONOSUPPORT; } /* We assume the firmware has the same endianness as the host */ # ifdef __LITTLE_ENDIAN if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) { # else /* BIG ENDIAN */ if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) { # endif pr_debug("Unsupported firmware endianness\n"); return -EILSEQ; } if (size < ehdr->e_shoff + sizeof(Elf32_Shdr)) { pr_debug("Image is too small\n"); return -ENOSPC; } if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) { pr_debug("Image is corrupted (bad magic)\n"); return -EBADF; } if (ehdr->e_phnum == 0) { pr_debug("No loadable segments\n"); return -ENOEXEC; } if (ehdr->e_phoff > size) { pr_debug("Firmware size is too small\n"); return -ENOSPC; } return 0; } /* Basic function to verify ELF64 image format */ int rproc_elf64_sanity_check(ulong addr, ulong size) { Elf64_Ehdr *ehdr = (Elf64_Ehdr *)addr; char class; if (!addr) { pr_debug("Invalid fw address?\n"); return -EFAULT; } if (size < sizeof(Elf64_Ehdr)) { pr_debug("Image is too small\n"); return -ENOSPC; } class = ehdr->e_ident[EI_CLASS]; if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS64) { pr_debug("Not an executable ELF64 image\n"); return -EPROTONOSUPPORT; } /* We assume the firmware has the same endianness as the host */ # ifdef __LITTLE_ENDIAN if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) { # else /* BIG ENDIAN */ if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) { # endif pr_debug("Unsupported firmware endianness\n"); return -EILSEQ; } if (size < ehdr->e_shoff + sizeof(Elf64_Shdr)) { pr_debug("Image is too small\n"); return -ENOSPC; } if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) { pr_debug("Image is corrupted (bad magic)\n"); return -EBADF; } if (ehdr->e_phnum == 0) { pr_debug("No loadable segments\n"); return -ENOEXEC; } if (ehdr->e_phoff > size) { pr_debug("Firmware size is too small\n"); return -ENOSPC; } return 0; } int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size) { Elf32_Ehdr *ehdr; /* Elf header structure pointer */ Elf32_Phdr *phdr; /* Program header structure pointer */ const struct dm_rproc_ops *ops; unsigned int i, ret; ret = rproc_elf32_sanity_check(addr, size); if (ret) { dev_err(dev, "Invalid ELF32 Image %d\n", ret); return ret; } ehdr = (Elf32_Ehdr *)addr; phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff); ops = rproc_get_ops(dev); /* Load each program header */ for (i = 0; i < ehdr->e_phnum; i++, phdr++) { void *dst = (void *)(uintptr_t)phdr->p_paddr; void *src = (void *)addr + phdr->p_offset; if (phdr->p_type != PT_LOAD) continue; if (ops->device_to_virt) dst = ops->device_to_virt(dev, (ulong)dst, phdr->p_memsz); dev_dbg(dev, "Loading phdr %i to 0x%p (%i bytes)\n", i, dst, phdr->p_filesz); if (phdr->p_filesz) memcpy(dst, src, phdr->p_filesz); if (phdr->p_filesz != phdr->p_memsz) memset(dst + phdr->p_filesz, 0x00, phdr->p_memsz - phdr->p_filesz); flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN), roundup((unsigned long)dst + phdr->p_filesz, ARCH_DMA_MINALIGN) - rounddown((unsigned long)dst, ARCH_DMA_MINALIGN)); } return 0; } int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size) { const struct dm_rproc_ops *ops = rproc_get_ops(dev); u64 da, memsz, filesz, offset; Elf64_Ehdr *ehdr; Elf64_Phdr *phdr; int i, ret = 0; void *ptr; dev_dbg(dev, "%s: addr = 0x%lx size = 0x%lx\n", __func__, addr, size); if (rproc_elf64_sanity_check(addr, size)) return -EINVAL; ehdr = (Elf64_Ehdr *)addr; phdr = (Elf64_Phdr *)(addr + (ulong)ehdr->e_phoff); /* go through the available ELF segments */ for (i = 0; i < ehdr->e_phnum; i++, phdr++) { da = phdr->p_paddr; memsz = phdr->p_memsz; filesz = phdr->p_filesz; offset = phdr->p_offset; if (phdr->p_typ