remoteproc: elf_loader: Introduce a common elf loader and checker functions

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 <lokeshvutla@ti.com>
Reviewed-by: Fabien Dessenne <fabien.dessenne@st.com>
diff --git a/drivers/remoteproc/rproc-elf-loader.c b/drivers/remoteproc/rproc-elf-loader.c
index 1aece2f..42a78f4 100644
--- a/drivers/remoteproc/rproc-elf-loader.c
+++ b/drivers/remoteproc/rproc-elf-loader.c
@@ -120,6 +120,22 @@
 	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 @@
 
 	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);
+}