1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
8 #include <remoteproc.h>
10 /* Basic function to verify ELF32 image format */
11 int rproc_elf32_sanity_check(ulong addr, ulong size)
17 pr_debug("Invalid fw address?\n");
21 if (size < sizeof(Elf32_Ehdr)) {
22 pr_debug("Image is too small\n");
26 ehdr = (Elf32_Ehdr *)addr;
27 class = ehdr->e_ident[EI_CLASS];
29 if (!IS_ELF(*ehdr) || ehdr->e_type != ET_EXEC || class != ELFCLASS32) {
30 pr_debug("Not an executable ELF32 image\n");
31 return -EPROTONOSUPPORT;
34 /* We assume the firmware has the same endianness as the host */
35 # ifdef __LITTLE_ENDIAN
36 if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
37 # else /* BIG ENDIAN */
38 if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
40 pr_debug("Unsupported firmware endianness\n");
44 if (size < ehdr->e_shoff + sizeof(Elf32_Shdr)) {
45 pr_debug("Image is too small\n");
49 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
50 pr_debug("Image is corrupted (bad magic)\n");
54 if (ehdr->e_phnum == 0) {
55 pr_debug("No loadable segments\n");
59 if (ehdr->e_phoff > size) {
60 pr_debug("Firmware size is too small\n");
67 /* A very simple elf loader, assumes the image is valid */
68 int rproc_elf32_load_image(struct udevice *dev, unsigned long addr)
70 Elf32_Ehdr *ehdr; /* Elf header structure pointer */
71 Elf32_Phdr *phdr; /* Program header structure pointer */
72 const struct dm_rproc_ops *ops;
75 ehdr = (Elf32_Ehdr *)addr;
76 phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
78 ops = rproc_get_ops(dev);
80 /* Load each program header */
81 for (i = 0; i < ehdr->e_phnum; ++i) {
82 void *dst = (void *)(uintptr_t)phdr->p_paddr;
83 void *src = (void *)addr + phdr->p_offset;
85 if (phdr->p_type != PT_LOAD)
88 if (ops->device_to_virt)
89 dst = ops->device_to_virt(dev, (ulong)dst);
91 dev_dbg(dev, "Loading phdr %i to 0x%p (%i bytes)\n",
92 i, dst, phdr->p_filesz);
94 memcpy(dst, src, phdr->p_filesz);
95 if (phdr->p_filesz != phdr->p_memsz)
96 memset(dst + phdr->p_filesz, 0x00,
97 phdr->p_memsz - phdr->p_filesz);
98 flush_cache(rounddown((unsigned long)dst, ARCH_DMA_MINALIGN),
99 roundup((unsigned long)dst + phdr->p_filesz,
101 rounddown((unsigned long)dst, ARCH_DMA_MINALIGN));