dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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