grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF P


From: Tomohiro B Berry
Subject: Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser
Date: Wed, 15 Jan 2014 13:49:19 -0600

PowerPC is the only architecture that I know of currently asking for this biendianness, so is it sufficient to include config.h in elf.c and an #ifdef __powerpc__ to the grub_elfXX_byteswap_header function (as shown below)?   That way, it will only perform the byteswap on the powerpc architecture, and continue to fail to load an opposite endian kernel on other architectures when it checks the e_version.  Or is there a better way to do it?  

Tomo

diff --git a/grub-core/kern/elf.c b/grub-core/kern/elf.c

index 5f99c43..2328b8e 100644
--- a/grub-core/kern/elf.c
+++ b/grub-core/kern/elf.c
@@ -17,6 +17,7 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <grub/err.h>
 #include <grub/elf.h>
 #include <grub/elfload.h>
@@ -28,20 +29,45 @@
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
+void grub_elf32_byteswap_header (grub_elf_t elf);
+void grub_elf64_byteswap_header (grub_elf_t elf);
+grub_err_t grub_elf32_check_version (grub_elf_t elf);
+grub_err_t grub_elf64_check_version (grub_elf_t elf);
+
 /* Check if EHDR is a valid ELF header.  */
 static grub_err_t
 grub_elf_check_header (grub_elf_t elf)
 {
-  Elf32_Ehdr *e = &elf->ehdr.ehdr32;
+  /* e_ident is the same for both 64-bit and 32-bit */
+  Elf32_Ehdr *e = &elf->ehdr.ehdr32;        
 
+  /* check if it is an ELF image at all */
   if (e->e_ident[EI_MAG0] != ELFMAG0
       || e->e_ident[EI_MAG1] != ELFMAG1
       || e->e_ident[EI_MAG2] != ELFMAG2
       || e->e_ident[EI_MAG3] != ELFMAG3
-      || e->e_ident[EI_VERSION] != EV_CURRENT
-      || e->e_version != EV_CURRENT)
-    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
-
+      || e->e_ident[EI_VERSION] != EV_CURRENT)
+    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));                        
+  
+  switch (e->e_ident[EI_CLASS])
+    {
+      case ELFCLASS32:
+        if (e->e_ident[EI_DATA] != ELFDATA_NATIVE)
+          grub_elf32_byteswap_header (elf);
+        if (grub_elf32_check_version (elf) != GRUB_ERR_NONE)
+          return grub_errno;
+        break;
+      case ELFCLASS64:
+        if (e->e_ident[EI_DATA] != ELFDATA_NATIVE)
+          grub_elf64_byteswap_header (elf);
+        if (grub_elf64_check_version (elf) != GRUB_ERR_NONE)
+          return grub_errno;
+        break;
+      default:
+        return grub_error (GRUB_ERR_BAD_OS, N_("unrecognized ELF class"));
+        break;
+    }
+  
   return GRUB_ERR_NONE;
 }
 
@@ -127,7 +153,16 @@ grub_elf_open (const char *name)
 #define grub_elf_is_elfXX grub_elf_is_elf32
 #define grub_elfXX_load_phdrs grub_elf32_load_phdrs
 #define ElfXX_Phdr Elf32_Phdr
+#define ElfXX_Ehdr Elf32_Ehdr
 #define grub_uintXX_t grub_uint32_t
+/* for phdr/ehdr byte swaps */
+#define byte_swap_halfXX grub_swap_bytes16
+#define byte_swap_wordXX grub_swap_bytes32
+#define byte_swap_addrXX grub_swap_bytes32
+#define byte_swap_offXX grub_swap_bytes32
+#define byte_swap_XwordXX byte_swap_wordXX /* the 64-bit phdr uses Xwords and the 32-bit uses words */
+#define grub_elfXX_byteswap_header grub_elf32_byteswap_header
+#define grub_elfXX_check_version grub_elf32_check_version
 
 #include "elfXX.c"
 
@@ -140,7 +175,15 @@ grub_elf_open (const char *name)
 #undef grub_elf_is_elfXX
 #undef grub_elfXX_load_phdrs
 #undef ElfXX_Phdr
+#undef ElfXX_Ehdr
 #undef grub_uintXX_t
+#undef byte_swap_halfXX
+#undef byte_swap_wordXX
+#undef byte_swap_addrXX
+#undef byte_swap_offXX
+#undef byte_swap_XwordXX
+#undef grub_elfXX_byteswap_header
+#undef grub_elfXX_check_version
 
 

 /* 64-bit */
@@ -153,6 +196,16 @@ grub_elf_open (const char *name)
 #define grub_elf_is_elfXX grub_elf_is_elf64
 #define grub_elfXX_load_phdrs grub_elf64_load_phdrs
 #define ElfXX_Phdr Elf64_Phdr
+#define ElfXX_Ehdr Elf64_Ehdr
 #define grub_uintXX_t grub_uint64_t
+/* for phdr/ehdr byte swaps */
+#define byte_swap_halfXX grub_swap_bytes16
+#define byte_swap_wordXX grub_swap_bytes32
+#define byte_swap_addrXX grub_swap_bytes64
+#define byte_swap_offXX grub_swap_bytes64
+#define byte_swap_XwordXX grub_swap_bytes64
+#define grub_elfXX_byteswap_header grub_elf64_byteswap_header
+#define grub_elfXX_check_version grub_elf64_check_version
 
 #include "elfXX.c"
+
diff --git a/grub-core/kern/elfXX.c b/grub-core/kern/elfXX.c
index 2e45449..aabfcdc 100644
--- a/grub-core/kern/elfXX.c
+++ b/grub-core/kern/elfXX.c
@@ -154,3 +154,54 @@ grub_elfXX_load (grub_elf_t elf, const char *filename,
 
   return grub_errno;
 }
+
+void
+grub_elfXX_byteswap_header (grub_elf_t elf)
+{
+  ElfXX_Ehdr *e = &(elf->ehdr.ehdrXX);
+  ElfXX_Phdr *phdr;  
+
+ /*
+  *  Swap bytes if the architecture supports bi-endian kernels.
+  *  So far the only architecture asking for it is powerpc.
+  */
+
+#ifdef __powerpc__
+  e->e_type = byte_swap_halfXX (e->e_type);
+  e->e_machine = byte_swap_halfXX (e->e_machine);
+  e->e_version = byte_swap_wordXX (e->e_version);
+  e->e_entry = byte_swap_addrXX (e->e_entry);
+  e->e_phoff = byte_swap_offXX (e->e_phoff);
+  e->e_shoff = byte_swap_offXX (e->e_shoff);
+  e->e_flags = byte_swap_wordXX (e->e_flags);
+  e->e_ehsize = byte_swap_halfXX (e->e_ehsize);  
+  e->e_phentsize = byte_swap_halfXX (e->e_phentsize);  
+  e->e_phnum = byte_swap_halfXX (e->e_phnum);  
+  e->e_shentsize = byte_swap_halfXX (e->e_shentsize);  
+  e->e_shnum = byte_swap_halfXX (e->e_shnum);
+  e->e_shstrndx = byte_swap_halfXX (e->e_shstrndx);
+
+  FOR_ELFXX_PHDRS (elf,phdr)
+    {
+      phdr->p_type = byte_swap_wordXX (phdr->p_type);
+      phdr->p_flags = byte_swap_wordXX (phdr->p_flags);
+      phdr->p_offset = byte_swap_offXX (phdr->p_offset);
+      phdr->p_vaddr = byte_swap_addrXX (phdr->p_vaddr);
+      phdr->p_paddr = byte_swap_addrXX (phdr->p_paddr);
+      phdr->p_filesz = byte_swap_XwordXX (phdr->p_filesz);
+      phdr->p_memsz = byte_swap_XwordXX (phdr->p_memsz);
+      phdr->p_align = byte_swap_XwordXX (phdr->p_align);
+    }
+#endif
+
+}
+
+grub_err_t
+grub_elfXX_check_version (grub_elf_t elf)
+{
+  if (elf->ehdr.ehdrXX.e_version != EV_CURRENT)
+    return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-independent ELF magic"));
+
+  return GRUB_ERR_NONE;
+}
+
diff --git a/include/grub/elf.h b/include/grub/elf.h
index 140d24d..d5adf3f 100644
--- a/include/grub/elf.h
+++ b/include/grub/elf.h
@@ -56,6 +56,13 @@ typedef grub_uint16_t Elf64_Section;
 typedef Elf32_Half Elf32_Versym;
 typedef Elf64_Half Elf64_Versym;
 
+/* Define the native endianness */
+
+#ifdef GRUB_CPU_WORDS_BIGENDIAN
+#define ELFDATA_NATIVE         ELFDATA2MSB
+#else
+#define ELFDATA_NATIVE         ELFDATA2LSB
+#endif
 
 /* The ELF file header.  This appears at the start of every ELF file.  */



From:        Andrey Borzenkov <address@hidden>
To:        The development of GNU GRUB <address@hidden>,
Cc:        Tomohiro B Berry/Austin/address@hidden
Date:        01/13/2014 11:36 PM
Subject:        Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser




On Tue, Jan 14, 2014 at 1:29 AM, Tomohiro B Berry <address@hidden> wrote:
> Hi Colin,
>
> After some testing, it seems that Grub is able to boot the kernel just fine
> with an initrd with only these changes.  The initrd is loaded into memory
> while still in BE mode and it looks like the jump is handled in the
> firmware, so the address and size still have to be in big endian mode (and
> in fact byte swapping them on the grub side breaks the boot process).  So it
> looks like the initrd will not be a problem in this case.
>

it would still be good to explicitly restrict it to platforms known to
support it.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]