* [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser
@ 2014-01-10 17:58 Tomohiro B Berry
2014-01-10 19:52 ` Andrey Borzenkov
0 siblings, 1 reply; 7+ messages in thread
From: Tomohiro B Berry @ 2014-01-10 17:58 UTC (permalink / raw)
To: grub-devel
[-- Attachment #1: Type: text/plain, Size: 6164 bytes --]
This patch adds bi-endian support for both 32-bit and 64-bit elf files.
It compares the native endianness to the endianness of the elf file, and
swaps the header bytes if necessary. This will allow, for example, 32-bit
Big Endian grub to load a 64-bit Little Endian kernel.
Regards,
Tomo Berry
diff --git a/grub-core/kern/elf.c b/grub-core/kern/elf.c
index 5f99c43..3326cd7 100644
--- a/grub-core/kern/elf.c
+++ b/grub-core/kern/elf.c
@@ -28,20 +28,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 so just load into a
32-bit struct for now */
+ 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 +152,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 +174,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 +195,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..09062ce 100644
--- a/grub-core/kern/elfXX.c
+++ b/grub-core/kern/elfXX.c
@@ -154,3 +154,47 @@ 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;
+
+ 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);
+ }
+
+}
+
+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. */
[-- Attachment #2: Type: text/html, Size: 15013 bytes --]
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser
2014-01-10 17:58 [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser Tomohiro B Berry
@ 2014-01-10 19:52 ` Andrey Borzenkov
2014-01-13 12:52 ` Colin Watson
0 siblings, 1 reply; 7+ messages in thread
From: Andrey Borzenkov @ 2014-01-10 19:52 UTC (permalink / raw)
To: The development of GNU GRUB; +Cc: tbberry
В Fri, 10 Jan 2014 11:58:58 -0600
Tomohiro B Berry <tbberry@us.ibm.com> пишет:
> This patch adds bi-endian support for both 32-bit and 64-bit elf files.
>
> It compares the native endianness to the endianness of the elf file, and
> swaps the header bytes if necessary. This will allow, for example, 32-bit
> Big Endian grub to load a 64-bit Little Endian kernel.
>
I wonder - when big endian grub will pass boot parameters to little
endian kernel - won't there be endianness mismatch?
> Regards,
> Tomo Berry
>
> diff --git a/grub-core/kern/elf.c b/grub-core/kern/elf.c
> index 5f99c43..3326cd7 100644
> --- a/grub-core/kern/elf.c
> +++ b/grub-core/kern/elf.c
> @@ -28,20 +28,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 so just load into a
> 32-bit struct for now */
> + 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 +152,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 +174,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 +195,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..09062ce 100644
> --- a/grub-core/kern/elfXX.c
> +++ b/grub-core/kern/elfXX.c
> @@ -154,3 +154,47 @@ 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;
> +
> + 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);
> + }
> +
> +}
> +
> +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. */
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser
2014-01-10 19:52 ` Andrey Borzenkov
@ 2014-01-13 12:52 ` Colin Watson
2014-01-13 21:29 ` Tomohiro B Berry
2014-02-18 21:37 ` Tomohiro B Berry
0 siblings, 2 replies; 7+ messages in thread
From: Colin Watson @ 2014-01-13 12:52 UTC (permalink / raw)
To: The development of GNU GRUB; +Cc: tbberry
On Fri, Jan 10, 2014 at 11:52:52PM +0400, Andrey Borzenkov wrote:
> В Fri, 10 Jan 2014 11:58:58 -0600
> Tomohiro B Berry <tbberry@us.ibm.com> пишет:
> > This patch adds bi-endian support for both 32-bit and 64-bit elf files.
> >
> > It compares the native endianness to the endianness of the elf file, and
> > swaps the header bytes if necessary. This will allow, for example, 32-bit
> > Big Endian grub to load a 64-bit Little Endian kernel.
>
> I wonder - when big endian grub will pass boot parameters to little
> endian kernel - won't there be endianness mismatch?
I think this is probably not a problem for the case at hand (powerpc),
because command-line arguments are passed by way of an IEEE1275 property
rather than using a pointer in a structure as we do on x86. And the
kernel expects to enter the PROM in 32-bit BE mode, so passing its
address that way round should be fine.
However, Tomohiro, I wonder if you have tested this with an initrd? I
don't see code in the kernel to cope with a byteswapped initrd address
and size (though I certainly could be missing something).
If that side of things is confirmed to work, then this looks mostly fine
to me.
I wonder if perhaps we ought to only do this on architectures where we
know that the kernel can cope with being entered in the wrong
endianness? I don't know - maybe we don't need to worry as on other
architectures the chances of running across a wrong-endian kernel are
pretty infinitesimal anyway.
--
Colin Watson [cjwatson@ubuntu.com]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser
2014-01-13 12:52 ` Colin Watson
@ 2014-01-13 21:29 ` Tomohiro B Berry
2014-01-14 5:36 ` Andrey Borzenkov
2014-02-18 21:37 ` Tomohiro B Berry
1 sibling, 1 reply; 7+ messages in thread
From: Tomohiro B Berry @ 2014-01-13 21:29 UTC (permalink / raw)
To: Colin Watson; +Cc: The development of GNU GRUB
[-- Attachment #1: Type: text/plain, Size: 2366 bytes --]
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.
Regards,
Tomo Berry
From: Colin Watson <cjwatson@ubuntu.com>
To: The development of GNU GRUB <grub-devel@gnu.org>,
Cc: Tomohiro B Berry/Austin/IBM@IBMUS
Date: 01/13/2014 06:52 AM
Subject: Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to
the Grub ELF Parser
On Fri, Jan 10, 2014 at 11:52:52PM +0400, Andrey Borzenkov wrote:
> В Fri, 10 Jan 2014 11:58:58 -0600
> Tomohiro B Berry <tbberry@us.ibm.com> пишет:
> > This patch adds bi-endian support for both 32-bit and 64-bit elf
files.
> >
> > It compares the native endianness to the endianness of the elf file,
and
> > swaps the header bytes if necessary. This will allow, for example,
32-bit
> > Big Endian grub to load a 64-bit Little Endian kernel.
>
> I wonder - when big endian grub will pass boot parameters to little
> endian kernel - won't there be endianness mismatch?
I think this is probably not a problem for the case at hand (powerpc),
because command-line arguments are passed by way of an IEEE1275 property
rather than using a pointer in a structure as we do on x86. And the
kernel expects to enter the PROM in 32-bit BE mode, so passing its
address that way round should be fine.
However, Tomohiro, I wonder if you have tested this with an initrd? I
don't see code in the kernel to cope with a byteswapped initrd address
and size (though I certainly could be missing something).
If that side of things is confirmed to work, then this looks mostly fine
to me.
I wonder if perhaps we ought to only do this on architectures where we
know that the kernel can cope with being entered in the wrong
endianness? I don't know - maybe we don't need to worry as on other
architectures the chances of running across a wrong-endian kernel are
pretty infinitesimal anyway.
--
Colin Watson [cjwatson@ubuntu.com]
[-- Attachment #2: Type: text/html, Size: 3535 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser
2014-01-13 21:29 ` Tomohiro B Berry
@ 2014-01-14 5:36 ` Andrey Borzenkov
2014-01-15 19:49 ` Tomohiro B Berry
0 siblings, 1 reply; 7+ messages in thread
From: Andrey Borzenkov @ 2014-01-14 5:36 UTC (permalink / raw)
To: The development of GNU GRUB; +Cc: tbberry
On Tue, Jan 14, 2014 at 1:29 AM, Tomohiro B Berry <tbberry@us.ibm.com> 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.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser
2014-01-14 5:36 ` Andrey Borzenkov
@ 2014-01-15 19:49 ` Tomohiro B Berry
0 siblings, 0 replies; 7+ messages in thread
From: Tomohiro B Berry @ 2014-01-15 19:49 UTC (permalink / raw)
To: Andrey Borzenkov; +Cc: The development of GNU GRUB
[-- Attachment #1: Type: text/plain, Size: 7533 bytes --]
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 <arvidjaar@gmail.com>
To: The development of GNU GRUB <grub-devel@gnu.org>,
Cc: Tomohiro B Berry/Austin/IBM@IBMUS
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 <tbberry@us.ibm.com>
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.
[-- Attachment #2: Type: text/html, Size: 17898 bytes --]
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser
2014-01-13 12:52 ` Colin Watson
2014-01-13 21:29 ` Tomohiro B Berry
@ 2014-02-18 21:37 ` Tomohiro B Berry
1 sibling, 0 replies; 7+ messages in thread
From: Tomohiro B Berry @ 2014-02-18 21:37 UTC (permalink / raw)
To: Colin Watson; +Cc: The development of GNU GRUB
[-- Attachment #1: Type: text/plain, Size: 2075 bytes --]
Hi Colin,
Just wanting to confirm whether or not this patch has been included
upstream yet or not? And if not, what we need to add/change to get it
upstream?
Thanks,
Tomo
From: Colin Watson <cjwatson@ubuntu.com>
To: The development of GNU GRUB <grub-devel@gnu.org>,
Cc: Tomohiro B Berry/Austin/IBM@IBMUS
Date: 01/13/2014 06:52 AM
Subject: Re: [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to
the Grub ELF Parser
On Fri, Jan 10, 2014 at 11:52:52PM +0400, Andrey Borzenkov wrote:
> В Fri, 10 Jan 2014 11:58:58 -0600
> Tomohiro B Berry <tbberry@us.ibm.com> пишет:
> > This patch adds bi-endian support for both 32-bit and 64-bit elf
files.
> >
> > It compares the native endianness to the endianness of the elf file,
and
> > swaps the header bytes if necessary. This will allow, for example,
32-bit
> > Big Endian grub to load a 64-bit Little Endian kernel.
>
> I wonder - when big endian grub will pass boot parameters to little
> endian kernel - won't there be endianness mismatch?
I think this is probably not a problem for the case at hand (powerpc),
because command-line arguments are passed by way of an IEEE1275 property
rather than using a pointer in a structure as we do on x86. And the
kernel expects to enter the PROM in 32-bit BE mode, so passing its
address that way round should be fine.
However, Tomohiro, I wonder if you have tested this with an initrd? I
don't see code in the kernel to cope with a byteswapped initrd address
and size (though I certainly could be missing something).
If that side of things is confirmed to work, then this looks mostly fine
to me.
I wonder if perhaps we ought to only do this on architectures where we
know that the kernel can cope with being entered in the wrong
endianness? I don't know - maybe we don't need to worry as on other
architectures the chances of running across a wrong-endian kernel are
pretty infinitesimal anyway.
--
Colin Watson [cjwatson@ubuntu.com]
[-- Attachment #2: Type: text/html, Size: 3239 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2014-02-18 21:37 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-10 17:58 [PATCH] Adding Bi-Endian 32-bit and 64-bit Support to the Grub ELF Parser Tomohiro B Berry
2014-01-10 19:52 ` Andrey Borzenkov
2014-01-13 12:52 ` Colin Watson
2014-01-13 21:29 ` Tomohiro B Berry
2014-01-14 5:36 ` Andrey Borzenkov
2014-01-15 19:49 ` Tomohiro B Berry
2014-02-18 21:37 ` Tomohiro B Berry
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.