From: Fabien DESSENNE Date: Wed, 4 Sep 2019 15:05:59 +0000 Subject: [U-Boot] [PATCH v2 05/26] remoteproc: elf_loader: Introduce a common elf loader and checker functions In-Reply-To: <20190904103151.20121-6-lokeshvutla@ti.com> References: <20190904103151.20121-1-lokeshvutla@ti.com> <20190904103151.20121-6-lokeshvutla@ti.com> Message-ID: <765de5e2-b481-6fe6-1efc-be7768003029@st.com> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de Hi Lokesh On 04/09/2019 12:31 PM, Lokesh Vutla wrote: > Introduce a common remoteproc elf loader and checker functions that > automatically detects the 64 bit elf file or 32 bit elf file and > loads/checks the sections accordingly. > > Signed-off-by: Lokesh Vutla Reviewed-by: Fabien Dessenne > --- > drivers/remoteproc/rproc-elf-loader.c | 31 +++++++++++++++++++++++++++ > include/remoteproc.h | 29 +++++++++++++++++++++++++ > 2 files changed, 60 insertions(+) > > diff --git a/drivers/remoteproc/rproc-elf-loader.c b/drivers/remoteproc/rproc-elf-loader.c > index 1aece2fc31..42a78f4207 100644 > --- a/drivers/remoteproc/rproc-elf-loader.c > +++ b/drivers/remoteproc/rproc-elf-loader.c > @@ -120,6 +120,22 @@ int rproc_elf64_sanity_check(ulong addr, ulong size) > return 0; > } > > +/* Basic function to verify ELF image format */ > +int rproc_elf_sanity_check(ulong addr, ulong size) > +{ > + Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr; > + > + if (!addr) { > + dev_err(dev, "Invalid firmware address\n"); > + return -EFAULT; > + } > + > + if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) > + return rproc_elf64_sanity_check(addr, size); > + else > + return rproc_elf32_sanity_check(addr, size); > +} > + > int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size) > { > Elf32_Ehdr *ehdr; /* Elf header structure pointer */ > @@ -220,3 +236,18 @@ int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size) > > return ret; > } > + > +int rproc_elf_load_image(struct udevice *dev, ulong addr, ulong size) > +{ > + Elf32_Ehdr *ehdr = (Elf32_Ehdr *)addr; > + > + if (!addr) { > + dev_err(dev, "Invalid firmware address\n"); > + return -EFAULT; > + } > + > + if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) > + return rproc_elf64_load_image(dev, addr, size); > + else > + return rproc_elf32_load_image(dev, addr, size); > +} > diff --git a/include/remoteproc.h b/include/remoteproc.h > index 56a11db6e3..812e0f47c4 100644 > --- a/include/remoteproc.h > +++ b/include/remoteproc.h > @@ -226,6 +226,19 @@ int rproc_elf32_sanity_check(ulong addr, ulong size); > */ > int rproc_elf64_sanity_check(ulong addr, ulong size); > > +/** > + * rproc_elf_sanity_check() - Verify if an image is a valid ELF one > + * > + * Check if a valid ELF image exists at the given memory location. Auto > + * detects ELF32/ELF64 and verifies basic ELF64/ELF32 format requirements > + * like magic number and sections size. > + * > + * @addr: address of the image to verify > + * @size: size of the image > + * @return 0 if the image looks good, else appropriate error value. > + */ > +int rproc_elf_sanity_check(ulong addr, ulong size); > + > /** > * rproc_elf32_load_image() - load an ELF32 image > * @dev: device loading the ELF32 image > @@ -243,6 +256,17 @@ int rproc_elf32_load_image(struct udevice *dev, unsigned long addr, ulong size); > * @return 0 if the image is successfully loaded, else appropriate error value. > */ > int rproc_elf64_load_image(struct udevice *dev, ulong addr, ulong size); > + > +/** > + * rproc_elf_load_image() - load an ELF image > + * @dev: device loading the ELF image > + * @addr: valid ELF image address > + * @size: size of the image > + * > + * Auto detects if the image is ELF32 or ELF64 image and load accordingly. > + * @return 0 if the image is successfully loaded, else appropriate error value. > + */ > +int rproc_elf_load_image(struct udevice *dev, unsigned long addr, ulong size); > #else > static inline int rproc_init(void) { return -ENOSYS; } > static inline int rproc_dev_init(int id) { return -ENOSYS; } > @@ -257,12 +281,17 @@ static inline int rproc_elf32_sanity_check(ulong addr, > ulong size) { return -ENOSYS; } > static inline int rproc_elf64_sanity_check(ulong addr, > ulong size) { return -ENOSYS; } > +static inline int rproc_elf_sanity_check(ulong addr, > + ulong size) { return -ENOSYS; } > static inline int rproc_elf32_load_image(struct udevice *dev, > unsigned long addr, ulong size) > { return -ENOSYS; } > static inline int rproc_elf64_load_image(struct udevice *dev, ulong addr, > ulong size) > { return -ENOSYS; } > +static inline int rproc_elf_load_image(struct udevice *dev, ulong addr, > + ulong size) > +{ return -ENOSYS; } > #endif > > #endif /* _RPROC_H_ */