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

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