Blame SOURCES/0143-Suport-for-bi-endianess-in-elf-file.patch

f731ee
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f731ee
From: Paulo Flabiano Smorigo <pfsmorigo@linux.vnet.ibm.com>
f731ee
Date: Fri, 15 Aug 2014 14:39:53 -0300
f731ee
Subject: [PATCH] Suport for bi-endianess in elf file
f731ee
f731ee
* grub-core/kern/elf.c: check and switch endianess with grub_{be,le}_to
f731ee
  cpu functions.
f731ee
* grub-core/kern/elfXX.c: Likewise.
f731ee
f731ee
Also-by: Tomohiro B Berry <tbberry@us.ibm.com>
f731ee
---
f731ee
 grub-core/kern/elf.c   | 60 +++++++++++++++++++++++++++++++++++++++--
f731ee
 grub-core/kern/elfXX.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++
f731ee
 2 files changed, 131 insertions(+), 2 deletions(-)
f731ee
f731ee
diff --git a/grub-core/kern/elf.c b/grub-core/kern/elf.c
f731ee
index 5f99c43cc81..de9081180f0 100644
f731ee
--- a/grub-core/kern/elf.c
f731ee
+++ b/grub-core/kern/elf.c
f731ee
@@ -28,6 +28,11 @@
f731ee
 
f731ee
 GRUB_MOD_LICENSE ("GPLv3+");
f731ee
 
f731ee
+void grub_elf32_check_endianess (grub_elf_t elf);
f731ee
+void grub_elf64_check_endianess (grub_elf_t elf);
f731ee
+grub_err_t grub_elf32_check_version (grub_elf_t elf);
f731ee
+grub_err_t grub_elf64_check_version (grub_elf_t elf);
f731ee
+
f731ee
 /* Check if EHDR is a valid ELF header.  */
f731ee
 static grub_err_t
f731ee
 grub_elf_check_header (grub_elf_t elf)
f731ee
@@ -38,10 +43,22 @@ grub_elf_check_header (grub_elf_t elf)
f731ee
       || e->e_ident[EI_MAG1] != ELFMAG1
f731ee
       || e->e_ident[EI_MAG2] != ELFMAG2
f731ee
       || e->e_ident[EI_MAG3] != ELFMAG3
f731ee
-      || e->e_ident[EI_VERSION] != EV_CURRENT
f731ee
-      || e->e_version != EV_CURRENT)
f731ee
+      || e->e_ident[EI_VERSION] != EV_CURRENT)
f731ee
     return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
f731ee
 
f731ee
+  if (grub_elf_is_elf32 (elf))
f731ee
+    {
f731ee
+      grub_elf32_check_endianess (elf);
f731ee
+      grub_elf32_check_version (elf);
f731ee
+    }
f731ee
+  else if (grub_elf_is_elf64 (elf))
f731ee
+    {
f731ee
+      grub_elf64_check_endianess (elf);
f731ee
+      grub_elf64_check_version (elf);
f731ee
+    }
f731ee
+  else
f731ee
+    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
f731ee
+
f731ee
   return GRUB_ERR_NONE;
f731ee
 }
f731ee
 
f731ee
@@ -127,7 +144,20 @@ grub_elf_open (const char *name)
f731ee
 #define grub_elf_is_elfXX grub_elf_is_elf32
f731ee
 #define grub_elfXX_load_phdrs grub_elf32_load_phdrs
f731ee
 #define ElfXX_Phdr Elf32_Phdr
f731ee
+#define ElfXX_Ehdr Elf32_Ehdr
f731ee
 #define grub_uintXX_t grub_uint32_t
f731ee
+#define grub_be_to_halfXX grub_be_to_cpu16
f731ee
+#define grub_be_to_wordXX grub_be_to_cpu32
f731ee
+#define grub_be_to_addrXX grub_be_to_cpu32
f731ee
+#define grub_be_to_offXX grub_be_to_cpu32
f731ee
+#define grub_be_to_XwordXX grub_be_to_wordXX
f731ee
+#define grub_le_to_halfXX grub_le_to_cpu16
f731ee
+#define grub_le_to_wordXX grub_le_to_cpu32
f731ee
+#define grub_le_to_addrXX grub_le_to_cpu32
f731ee
+#define grub_le_to_offXX grub_le_to_cpu32
f731ee
+#define grub_le_to_XwordXX grub_le_to_wordXX
f731ee
+#define grub_elfXX_check_endianess grub_elf32_check_endianess
f731ee
+#define grub_elfXX_check_version grub_elf32_check_version
f731ee
 
f731ee
 #include "elfXX.c"
f731ee
 
f731ee
@@ -140,7 +170,20 @@ grub_elf_open (const char *name)
f731ee
 #undef grub_elf_is_elfXX
f731ee
 #undef grub_elfXX_load_phdrs
f731ee
 #undef ElfXX_Phdr
f731ee
+#undef ElfXX_Ehdr
f731ee
 #undef grub_uintXX_t
f731ee
+#undef grub_be_to_halfXX
f731ee
+#undef grub_be_to_wordXX
f731ee
+#undef grub_be_to_addrXX
f731ee
+#undef grub_be_to_offXX
f731ee
+#undef grub_be_to_XwordXX
f731ee
+#undef grub_le_to_halfXX
f731ee
+#undef grub_le_to_wordXX
f731ee
+#undef grub_le_to_addrXX
f731ee
+#undef grub_le_to_offXX
f731ee
+#undef grub_le_to_XwordXX
f731ee
+#undef grub_elfXX_check_endianess
f731ee
+#undef grub_elfXX_check_version
f731ee
 
f731ee
 
f731ee
 /* 64-bit */
f731ee
@@ -153,6 +196,19 @@ grub_elf_open (const char *name)
f731ee
 #define grub_elf_is_elfXX grub_elf_is_elf64
f731ee
 #define grub_elfXX_load_phdrs grub_elf64_load_phdrs
f731ee
 #define ElfXX_Phdr Elf64_Phdr
f731ee
+#define ElfXX_Ehdr Elf64_Ehdr
f731ee
 #define grub_uintXX_t grub_uint64_t
f731ee
+#define grub_be_to_halfXX grub_be_to_cpu16
f731ee
+#define grub_be_to_wordXX grub_be_to_cpu32
f731ee
+#define grub_be_to_addrXX grub_be_to_cpu64
f731ee
+#define grub_be_to_offXX grub_be_to_cpu64
f731ee
+#define grub_be_to_XwordXX grub_be_to_cpu64
f731ee
+#define grub_le_to_halfXX grub_le_to_cpu16
f731ee
+#define grub_le_to_wordXX grub_le_to_cpu32
f731ee
+#define grub_le_to_addrXX grub_le_to_cpu64
f731ee
+#define grub_le_to_offXX grub_le_to_cpu64
f731ee
+#define grub_le_to_XwordXX grub_le_to_cpu64
f731ee
+#define grub_elfXX_check_endianess grub_elf64_check_endianess
f731ee
+#define grub_elfXX_check_version grub_elf64_check_version
f731ee
 
f731ee
 #include "elfXX.c"
f731ee
diff --git a/grub-core/kern/elfXX.c b/grub-core/kern/elfXX.c
f731ee
index 1d0997186f7..ecf9df63a5e 100644
f731ee
--- a/grub-core/kern/elfXX.c
f731ee
+++ b/grub-core/kern/elfXX.c
f731ee
@@ -154,3 +154,76 @@ grub_elfXX_load (grub_elf_t elf, const char *filename,
f731ee
 
f731ee
   return grub_errno;
f731ee
 }
f731ee
+
f731ee
+void
f731ee
+grub_elfXX_check_endianess (grub_elf_t elf)
f731ee
+{
f731ee
+  ElfXX_Ehdr *e = &(elf->ehdr.ehdrXX);
f731ee
+  ElfXX_Phdr *phdr;
f731ee
+
f731ee
+  if (e->e_ident[EI_DATA] == ELFDATA2MSB)
f731ee
+    {
f731ee
+      e->e_type = grub_be_to_halfXX (e->e_type);
f731ee
+      e->e_machine = grub_be_to_halfXX (e->e_machine);
f731ee
+      e->e_version = grub_be_to_wordXX (e->e_version);
f731ee
+      e->e_entry = grub_be_to_addrXX (e->e_entry);
f731ee
+      e->e_phoff = grub_be_to_offXX (e->e_phoff);
f731ee
+      e->e_shoff = grub_be_to_offXX (e->e_shoff);
f731ee
+      e->e_flags = grub_be_to_wordXX (e->e_flags);
f731ee
+      e->e_ehsize = grub_be_to_halfXX (e->e_ehsize);
f731ee
+      e->e_phentsize = grub_be_to_halfXX (e->e_phentsize);
f731ee
+      e->e_phnum = grub_be_to_halfXX (e->e_phnum);
f731ee
+      e->e_shentsize = grub_be_to_halfXX (e->e_shentsize);
f731ee
+      e->e_shnum = grub_be_to_halfXX (e->e_shnum);
f731ee
+      e->e_shstrndx = grub_be_to_halfXX (e->e_shstrndx);
f731ee
+
f731ee
+      FOR_ELFXX_PHDRS (elf,phdr)
f731ee
+        {
f731ee
+          phdr->p_type = grub_be_to_wordXX (phdr->p_type);
f731ee
+          phdr->p_flags = grub_be_to_wordXX (phdr->p_flags);
f731ee
+          phdr->p_offset = grub_be_to_offXX (phdr->p_offset);
f731ee
+          phdr->p_vaddr = grub_be_to_addrXX (phdr->p_vaddr);
f731ee
+          phdr->p_paddr = grub_be_to_addrXX (phdr->p_paddr);
f731ee
+          phdr->p_filesz = grub_be_to_XwordXX (phdr->p_filesz);
f731ee
+          phdr->p_memsz = grub_be_to_XwordXX (phdr->p_memsz);
f731ee
+          phdr->p_align = grub_be_to_XwordXX (phdr->p_align);
f731ee
+        }
f731ee
+    }
f731ee
+  else if (e->e_ident[EI_DATA] == ELFDATA2LSB)
f731ee
+    {
f731ee
+      e->e_type = grub_le_to_halfXX (e->e_type);
f731ee
+      e->e_machine = grub_le_to_halfXX (e->e_machine);
f731ee
+      e->e_version = grub_le_to_wordXX (e->e_version);
f731ee
+      e->e_entry = grub_le_to_addrXX (e->e_entry);
f731ee
+      e->e_phoff = grub_le_to_offXX (e->e_phoff);
f731ee
+      e->e_shoff = grub_le_to_offXX (e->e_shoff);
f731ee
+      e->e_flags = grub_le_to_wordXX (e->e_flags);
f731ee
+      e->e_ehsize = grub_le_to_halfXX (e->e_ehsize);
f731ee
+      e->e_phentsize = grub_le_to_halfXX (e->e_phentsize);
f731ee
+      e->e_phnum = grub_le_to_halfXX (e->e_phnum);
f731ee
+      e->e_shentsize = grub_le_to_halfXX (e->e_shentsize);
f731ee
+      e->e_shnum = grub_le_to_halfXX (e->e_shnum);
f731ee
+      e->e_shstrndx = grub_le_to_halfXX (e->e_shstrndx);
f731ee
+
f731ee
+      FOR_ELFXX_PHDRS (elf,phdr)
f731ee
+        {
f731ee
+          phdr->p_type = grub_le_to_wordXX (phdr->p_type);
f731ee
+          phdr->p_flags = grub_le_to_wordXX (phdr->p_flags);
f731ee
+          phdr->p_offset = grub_le_to_offXX (phdr->p_offset);
f731ee
+          phdr->p_vaddr = grub_le_to_addrXX (phdr->p_vaddr);
f731ee
+          phdr->p_paddr = grub_le_to_addrXX (phdr->p_paddr);
f731ee
+          phdr->p_filesz = grub_le_to_XwordXX (phdr->p_filesz);
f731ee
+          phdr->p_memsz = grub_le_to_XwordXX (phdr->p_memsz);
f731ee
+          phdr->p_align = grub_le_to_XwordXX (phdr->p_align);
f731ee
+        }
f731ee
+    }
f731ee
+}
f731ee
+
f731ee
+grub_err_t
f731ee
+grub_elfXX_check_version (grub_elf_t elf)
f731ee
+{
f731ee
+  if (elf->ehdr.ehdrXX.e_version != EV_CURRENT)
f731ee
+    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
f731ee
+
f731ee
+  return GRUB_ERR_NONE;
f731ee
+}