arm64: Support HP Envy X2
v3 cover arm64: Support HP Envy X2
I got a new toy this week: An HP Envy X2 system. This is one of those shiny new Qualcomm Snapdragon based Windows tablet/notebook hybrid things.
While running Windows on those is actually not a terribly bad experience now that WSL is out, I would like to see Linux run on those as well in the future.
Unfortunately as far as I'm aware so far nobody was able to run self built binaries on the built-in UEFI version.
Turns out, it's a problem with aligning the start of the header to 4k. Once we do that, binaries can be loaded just fine and run.
The reason behind that is simple: Its firmware tries to ensure NX protection flags and can do so only when the code is 4K aligned.
So to maintain compatibility with that device, this patch set just bumps the header alignment to 4K always on arm64-efi and other efi targets. This way we improve overall compatibility - there surely will be more devices coming with similar constraints.
We also extend that alignemnt to any section that follows afterwards, to ensure that other protection mechanisms will have a chance to protect individual sections with page table bits.
This gets us into alignment with how the MS tools build UEFI applications, so we should not run into compatibility problems about alignment going forward.
v1 -> v2:
- Remove explicit device wording from patch
- Use GRUB_EFI_PAGE_SIZE
v2 -> v3:
- Apply alignment to all architectures
- new patch: mkimage: Align efi sections on 4k boundary
Alexander Graf (4):
mkimage: Simplify header size logic
mkimage: Use EFI32_HEADER_SIZE define in arm-efi case
mkimage: arm64-efi: Align header to page granularity
mkimage: Align efi sections on 4k boundary
util/mkimage.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-)
Signature · 1 line
2.12.3
PATCH v3 1/4 mkimage: Simplify header size logic
For EFI images, we always have the following layout:
[PE header]
[padding]
[first section (which also is the entry point)]
Currently there are 2 places where we define how big header+padding are:
in the .vaddr_offset member of our target image definition struct as well as in code in grub_install_generate_image().
Remove the latter, so that we only have a single place to modify if we need to change the padding.
--- util/mkimage.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
util/mkimage.c +1 -4
@@ -1226,10 +1226,7 @@ grub_install_generate_image (const char *dir, const char *prefix,int header_size;int reloc_addr; -if (image_target->voidp_sizeof ==4 ) - header_size =EFI32_HEADER_SIZE ; -else - header_size =EFI64_HEADER_SIZE ; + header_size = image_target->vaddr_offset; reloc_addr =ALIGN_UP (header_size + core_size, image_target->section_align);
Signature · 1 line
2.12.3
Re: [PATCH v3 1/4] mkimage: Simplify header size logic
Quoted 24 lines · Alexander Graf
For EFI images, we always have the following layout:
[PE header]
[padding]
[first section (which also is the entry point)]Currently there are 2 places where we define how big header+padding are:
in the .vaddr_offset member of our target image definition struct as well as in code ingrub_install_generate_image().Remove the latter, so that we only have a single place to modify if we need to change the padding.
Signed-off-by Alexander Graf <[email protected]>Reviewed-by Daniel Kiper <[email protected]>--- util/mkimage.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)util/mkimage.c +0
-0
util/mkimage.c grub_install_generate_image (const char *dir, const char *prefix, · line 1229
Quoted 8 lines
util/mkimage.c +1
-4@@ -1226,10 +1226,7 @@ grub_install_generate_image (const char *dir, const char *prefix,int header_size;int reloc_addr; -if (image_target->voidp_sizeof ==4 ) - header_size =EFI32_HEADER_SIZE ; -else - header_size =EFI64_HEADER_SIZE ; + header_size = image_target->vaddr_offset;
After some thinking it seems to me that this patch is wrong and should be dropped. It, at least, increases needlessly header size. More in the comments for the other patches.
Daniel
PATCH v3 2/4 mkimage: Use EFI32_HEADER_SIZE define in arm-efi case
In reply to Alexander GrafThe efi-arm case was defining its own header size calculation, even though it's 100% identical to the common EFI32_HEADER_SIZE definition.
So let's clean it up to use the common define.
--- util/mkimage.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
util/mkimage.c +1 -6
@@ -602,12 +602,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, .section_align = GRUB_PE32_SECTION_ALIGNMENT, - .vaddr_offset =ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE - +GRUB_PE32_SIGNATURE_SIZE - +sizeof (struct grub_pe32_coff_header ) - +sizeof (struct grub_pe32_optional_header ) - +4 *sizeof (struct grub_pe32_section_table ), - GRUB_PE32_SECTION_ALIGNMENT), + .vaddr_offset = EFI32_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_ARMTHUMB_MIXED, .elf_target = EM_ARM, },
Signature · 1 line
2.12.3
Re: [PATCH v3 2/4] mkimage: Use EFI32_HEADER_SIZE define in arm-efi case
Quoted 18 lines · Alexander Graf
The efi-arm case was defining its own header size calculation, even though it's 100% identical to the common
EFI32_HEADER_SIZEdefinition.So let's clean it up to use the common define.
Signed-off-by Alexander Graf <[email protected]>--- util/mkimage.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)util/mkimage.c +0
-0@@ -602,12 +602,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, .section_align = GRUB_PE32_SECTION_ALIGNMENT,
util/mkimage.c static const struct grub_install_image_target_desc image_targets[] = · line 608
Quoted 10 lines
util/mkimage.c +1
-6- .vaddr_offset = ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE - +GRUB_PE32_SIGNATURE_SIZE - +sizeof (struct grub_pe32_coff_header ) - +sizeof (struct grub_pe32_optional_header ) - +4 *sizeof (struct grub_pe32_section_table ), - GRUB_PE32_SECTION_ALIGNMENT), + .vaddr_offset = EFI32_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_ARMTHUMB_MIXED, .elf_target = EM_ARM, },
Daniel
PATCH v3 3/4 mkimage: arm64-efi: Align header to page granularity
In reply to Alexander GrafIn order to enforce NX semantics on non-code pages, UEFI firmware may require that all code is EFI_PAGE_SIZE (4k) aligned. A similar change has recently been applied to edk2 to accomodate for the same fact:
https://lists.01.org/pipermail/edk2-devel/2018-December/033708.html
This patch adapts grub to also implement the same alignment guarantees and thus ensures we can boot even when strict permission checks are in place.
---
v1 -> v2:
- Mention only NX requirement in patch description
- Use GRUB_EFI_PAGE_SIZE
v2 -> v3:
- Apply alignment to all architectures
--- util/mkimage.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
util/mkimage.c +3 -2
@@ -39,6 +39,7 @@#include <string.h> #include <stdlib.h> #include <assert.h> +#include <grub/efi/memory.h> #include <grub/efi/pe32.h> #include <grub/uboot/image.h> #include <grub/arm/reloc.h>
@@ -66,14 +67,14 @@ + sizeof (struct grub_pe32_coff_header) \ + sizeof (struct grub_pe32_optional_header) \ + 4 * sizeof (struct grub_pe32_section_table), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)#define EFI64_HEADER_SIZE ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE \ + GRUB_PE32_SIGNATURE_SIZE \ + sizeof (struct grub_pe32_coff_header) \ + sizeof (struct grub_pe64_optional_header) \ + 4 * sizeof (struct grub_pe32_section_table), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)static const struct grub_install_image_target_desc image_targets[] = {
Signature · 1 line
2.12.3
Re: [PATCH v3 3/4] mkimage: arm64-efi: Align header to page granularity
Quoted 43 lines · Alexander Graf
In order to enforce NX semantics on non-code pages, UEFI firmware may require that all code is
EFI_PAGE_SIZE(4k) aligned. A similar change has recently been applied to edk2 to accomodate for the same fact:https://lists.01.org/pipermail/edk2-devel/2018-December/033708.html
This patch adapts grub to also implement the same alignment guarantees and thus ensures we can boot even when strict permission checks are in place.
Signed-off-by Alexander Graf <[email protected]>---
v1 -> v2:
- Mention only NX requirement in patch description
- UseGRUB_EFI_PAGE_SIZEv2 -> v3:
- Apply alignment to all architectures
--- util/mkimage.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)util/mkimage.c +1
-0@@ -39,6 +39,7 @@#include <string.h> #include <stdlib.h> #include <assert.h> +#include <grub/efi/memory.h> #include <grub/efi/pe32.h> #include <grub/uboot/image.h> #include <grub/arm/reloc.h> @@ -66,14 +67,14 @@ +sizeof (struct grub_pe32_coff_header ) \ +sizeof (struct grub_pe32_optional_header ) \ + 4 * sizeof (struct grub_pe32_section_table), \
util/mkimage.c line 77
Quoted 10 lines
util/mkimage.c +2
-2- GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)#define EFI64_HEADER_SIZE ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE \ + GRUB_PE32_SIGNATURE_SIZE \ + sizeof (struct grub_pe32_coff_header) \ + sizeof (struct grub_pe64_optional_header) \ + 4 * sizeof (struct grub_pe32_section_table), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)
GRUB_PE32_SECTION_ALIGNMENT should be changed to GRUB_PE32_FILE_ALIGNMENT. However, earlier GRUB_PE32_FILE_ALIGNMENT should be defined as 0x20. Yeah, I know that this is contrary to the PE/COFF spec. Though everybody uses that value. Even MS...
Then below in the util/mkimage.c some code should look more or less like that:
reloc_addr = ALIGN_UP (header_size + core_size,
GRUB_PE32_FILE_ALIGNMENT);
pe_size = ALIGN_UP (reloc_addr + layout.reloc_size,
GRUB_PE32_FILE_ALIGNMENT);
...and...
o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
Last line should be changed at least in two places.
Daniel
Re: [PATCH v3 3/4] mkimage: arm64-efi: Align header to page granularity
Quoted 47 lines · Daniel Kiper
Quoted 46 lines · Alexander Graf
In order to enforce NX semantics on non-code pages, UEFI firmware may require that all code is
EFI_PAGE_SIZE(4k) aligned. A similar change has recently been applied to edk2 to accomodate for the same fact:https://lists.01.org/pipermail/edk2-devel/2018-December/033708.htmlThis patch adapts grub to also implement the same alignment guarantees and thus ensures we can boot even when strict permission checks are in place.
Signed-off-by Alexander Graf <[email protected]>---
v1 -> v2:
- Mention only NX requirement in patch description
- UseGRUB_EFI_PAGE_SIZEv2 -> v3:
- Apply alignment to all architectures
--- util/mkimage.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)util/mkimage.c +2
-1@@ -39,6 +39,7 @@#include <string.h> #include <stdlib.h> #include <assert.h> +#include <grub/efi/memory.h> #include <grub/efi/pe32.h> #include <grub/uboot/image.h> #include <grub/arm/reloc.h> @@ -66,14 +67,14 @@ +sizeof (struct grub_pe32_coff_header ) \ +sizeof (struct grub_pe32_optional_header ) \ +4 *sizeof (struct grub_pe32_section_table ), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)
Quoted 11 lines
util/mkimage.c line 77
Quoted 7 lines
util/mkimage.c +1
-1#define EFI64_HEADER_SIZE ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE \ + GRUB_PE32_SIGNATURE_SIZE \ + sizeof (struct grub_pe32_coff_header) \ + sizeof (struct grub_pe64_optional_header) \ + 4 * sizeof (struct grub_pe32_section_table), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)
GRUB_PE32_SECTION_ALIGNMENTshould be changed toGRUB_PE32_FILE_ALIGNMENT. However, earlierGRUB_PE32_FILE_ALIGNMENTshould be defined as0x20. Yeah, I know that this is contrary to the PE/COFF spec. Though everybody uses that value. Even MS...
I'm not sure I follow. When compiling code with MSVC, every section is definitely page aligned?
Quoted 5 lines
Then below in the
util/mkimage.csome code should look more or less like that:
reloc_addr=ALIGN_UP(header_size+core_size,
GRUB_PE32_FILE_ALIGNMENT);
Quoted 8 lines
pe_size=ALIGN_UP(reloc_addr+ layout.reloc_size,
GRUB_PE32_FILE_ALIGNMENT);...and...
o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);Last line should be changed at least in two places.
I again don't think I fully follow your thought process here yet. Can you please enlighten me?
Alex
Re: [PATCH v3 3/4] mkimage: arm64-efi: Align header to page granularity
Quoted 71 lines · Alexander Graf
Quoted 47 lines · Daniel Kiper
Quoted 46 lines · Alexander Graf
In order to enforce NX semantics on non-code pages, UEFI firmware may require that all code is
EFI_PAGE_SIZE(4k) aligned. A similar change has recently been applied to edk2 to accomodate for the same fact:https://lists.01.org/pipermail/edk2-devel/2018-December/033708.htmlThis patch adapts grub to also implement the same alignment guarantees and thus ensures we can boot even when strict permission checks are in place.
Signed-off-by Alexander Graf <[email protected]>---
v1 -> v2:
- Mention only NX requirement in patch description
- UseGRUB_EFI_PAGE_SIZEv2 -> v3:
- Apply alignment to all architectures
--- util/mkimage.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)util/mkimage.c +2
-1@@ -39,6 +39,7 @@#include <string.h> #include <stdlib.h> #include <assert.h> +#include <grub/efi/memory.h> #include <grub/efi/pe32.h> #include <grub/uboot/image.h> #include <grub/arm/reloc.h> @@ -66,14 +67,14 @@ +sizeof (struct grub_pe32_coff_header ) \ +sizeof (struct grub_pe32_optional_header ) \ +4 *sizeof (struct grub_pe32_section_table ), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)Quoted 11 lines
util/mkimage.c line 77
Quoted 7 lines
util/mkimage.c +1
-1#define EFI64_HEADER_SIZE ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE \ + GRUB_PE32_SIGNATURE_SIZE \ + sizeof (struct grub_pe32_coff_header) \ + sizeof (struct grub_pe64_optional_header) \ + 4 * sizeof (struct grub_pe32_section_table), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)
GRUB_PE32_SECTION_ALIGNMENTshould be changed toGRUB_PE32_FILE_ALIGNMENT. However, earlierGRUB_PE32_FILE_ALIGNMENTshould be defined as0x20. Yeah, I know that this is contrary to the PE/COFF spec. Though everybody uses that value. Even MS...I'm not sure I follow. When compiling code with MSVC, every section is definitely page aligned?
Quoted 8 lines
Then below in the
util/mkimage.csome code should look more or less like that:
reloc_addr=ALIGN_UP(header_size+core_size,
GRUB_PE32_FILE_ALIGNMENT);
pe_size=ALIGN_UP(reloc_addr+ layout.reloc_size,
GRUB_PE32_FILE_ALIGNMENT);
Quoted 8 lines
Quoted 5 lines
...and...
o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);Last line should be changed at least in two places.
I again don't think I fully follow your thought process here yet. Can you please enlighten me?
There are two alignments in PE/COFF file: FileAlignment and SectionAlignment. You can see both of them using "objdump -x" or "readpe". FileAlignment enforces PE/COFF data/sections/etc. alignment in a file. This should be quite small. SectionAlignment enforces PE/COFF data/sections/etc. in memory. This should be GRUB_EFI_PAGE_SIZE. Both are not related and can be different. And I think that we should try to use both FileAlignment and SectionAlignment properly. If it is not possible then we can make them equal but then in the worst case we will have extra ~14 KiB of zeros in PE GRUB image. Not much for today but why carry this garbage...
Daniel
Re: [PATCH v3 3/4] mkimage: arm64-efi: Align header to page granularity
Quoted 81 lines · Daniel Kiper
Quoted 71 lines · Alexander Graf
Quoted 47 lines · Daniel Kiper
Quoted 46 lines · Alexander Graf
In order to enforce NX semantics on non-code pages, UEFI firmware may require that all code is
EFI_PAGE_SIZE(4k) aligned. A similar change has recently been applied to edk2 to accomodate for the same fact:https://lists.01.org/pipermail/edk2-devel/2018-December/033708.htmlThis patch adapts grub to also implement the same alignment guarantees and thus ensures we can boot even when strict permission checks are in place.
Signed-off-by Alexander Graf <[email protected]>---
v1 -> v2:
- Mention only NX requirement in patch description
- UseGRUB_EFI_PAGE_SIZEv2 -> v3:
- Apply alignment to all architectures
--- util/mkimage.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)util/mkimage.c +2
-1@@ -39,6 +39,7 @@#include <string.h> #include <stdlib.h> #include <assert.h> +#include <grub/efi/memory.h> #include <grub/efi/pe32.h> #include <grub/uboot/image.h> #include <grub/arm/reloc.h> @@ -66,14 +67,14 @@ +sizeof (struct grub_pe32_coff_header ) \ +sizeof (struct grub_pe32_optional_header ) \ +4 *sizeof (struct grub_pe32_section_table ), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)Quoted 11 lines
util/mkimage.c line 77
Quoted 7 lines
util/mkimage.c +1
-1#define EFI64_HEADER_SIZE ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE \ + GRUB_PE32_SIGNATURE_SIZE \ + sizeof (struct grub_pe32_coff_header) \ + sizeof (struct grub_pe64_optional_header) \ + 4 * sizeof (struct grub_pe32_section_table), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)
GRUB_PE32_SECTION_ALIGNMENTshould be changed toGRUB_PE32_FILE_ALIGNMENT. However, earlierGRUB_PE32_FILE_ALIGNMENTshould be defined as0x20. Yeah, I know that this is contrary to the PE/COFF spec. Though everybody uses that value. Even MS...I'm not sure I follow. When compiling code with MSVC, every section is definitely page aligned?
Quoted 8 lines
Then below in the
util/mkimage.csome code should look more or less like that:
reloc_addr=ALIGN_UP(header_size+core_size,
GRUB_PE32_FILE_ALIGNMENT);
pe_size=ALIGN_UP(reloc_addr+ layout.reloc_size,
GRUB_PE32_FILE_ALIGNMENT);Quoted 8 lines
Quoted 5 lines
...and...
o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);Last line should be changed at least in two places.
I again don't think I fully follow your thought process here yet. Can you please enlighten me?
Quoted 11 lines
There are two alignments in PE/COFF file: FileAlignment and SectionAlignment. You can see both of them using "objdump -x" or "readpe". FileAlignment enforces PE/COFF data/sections/etc. alignment in a file. This should be quite small. SectionAlignment enforces PE/COFF data/sections/etc. in memory. This should be
GRUB_EFI_PAGE_SIZE. Both are not related and can be different. And I think that we should try to use both FileAlignment and SectionAlignment properly. If it is not possible then we can make them equal but then in the worst case we will have extra ~14 KiB of zeros in PE GRUB image. Not much for today but why carry this garbage...
I can't see a good way to make this work. All layers in mkimagexx are somewhat assuming that they own physical and virtual address space.
I think I managed to hack things up to a point where I can have them disjoint, but it's not pretty. I also still have problems where relocations just won't work, because they are constructed in mkimagexx which has no visibility on virtual placement eventually.
At this point, I would rather sacrifice 14kb rather than have an unmaintainable mess that is even worse than today's state of affairs.
Alex
Re: [PATCH v3 3/4] mkimage: arm64-efi: Align header to page granularity
Quoted 97 lines · Alexander Graf
Quoted 81 lines · Daniel Kiper
Quoted 71 lines · Alexander Graf
Quoted 47 lines · Daniel Kiper
Quoted 46 lines · Alexander Graf
In order to enforce NX semantics on non-code pages, UEFI firmware may require that all code is
EFI_PAGE_SIZE(4k) aligned. A similar change has recently been applied to edk2 to accomodate for the same fact:https://lists.01.org/pipermail/edk2-devel/2018-December/033708.htmlThis patch adapts grub to also implement the same alignment guarantees and thus ensures we can boot even when strict permission checks are in place.
Signed-off-by Alexander Graf <[email protected]>---
v1 -> v2:
- Mention only NX requirement in patch description
- UseGRUB_EFI_PAGE_SIZEv2 -> v3:
- Apply alignment to all architectures
--- util/mkimage.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)util/mkimage.c +2
-1@@ -39,6 +39,7 @@#include <string.h> #include <stdlib.h> #include <assert.h> +#include <grub/efi/memory.h> #include <grub/efi/pe32.h> #include <grub/uboot/image.h> #include <grub/arm/reloc.h> @@ -66,14 +67,14 @@ +sizeof (struct grub_pe32_coff_header ) \ +sizeof (struct grub_pe32_optional_header ) \ +4 *sizeof (struct grub_pe32_section_table ), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)Quoted 11 lines
util/mkimage.c line 77
Quoted 7 lines
util/mkimage.c +1
-1#define EFI64_HEADER_SIZE ALIGN_UP (GRUB_PE32_MSDOS_STUB_SIZE \ + GRUB_PE32_SIGNATURE_SIZE \ + sizeof (struct grub_pe32_coff_header) \ + sizeof (struct grub_pe64_optional_header) \ + 4 * sizeof (struct grub_pe32_section_table), \ - GRUB_PE32_SECTION_ALIGNMENT) + GRUB_EFI_PAGE_SIZE)
GRUB_PE32_SECTION_ALIGNMENTshould be changed toGRUB_PE32_FILE_ALIGNMENT. However, earlierGRUB_PE32_FILE_ALIGNMENTshould be defined as0x20. Yeah, I know that this is contrary to the PE/COFF spec. Though everybody uses that value. Even MS...I'm not sure I follow. When compiling code with MSVC, every section is definitely page aligned?
Quoted 8 lines
Then below in the
util/mkimage.csome code should look more or less like that:
reloc_addr=ALIGN_UP(header_size+core_size,
GRUB_PE32_FILE_ALIGNMENT);
pe_size=ALIGN_UP(reloc_addr+ layout.reloc_size,
GRUB_PE32_FILE_ALIGNMENT);Quoted 8 lines
Quoted 5 lines
...and...
o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);Last line should be changed at least in two places.
I again don't think I fully follow your thought process here yet. Can you please enlighten me?
Quoted 11 lines
There are two alignments in PE/COFF file: FileAlignment and SectionAlignment. You can see both of them using "objdump -x" or "readpe". FileAlignment enforces PE/COFF data/sections/etc. alignment in a file. This should be quite small. SectionAlignment enforces PE/COFF data/sections/etc. in memory. This should be
GRUB_EFI_PAGE_SIZE. Both are not related and can be different. And I think that we should try to use both FileAlignment and SectionAlignment properly. If it is not possible then we can make them equal but then in the worst case we will have extra ~14 KiB of zeros in PE GRUB image. Not much for today but why carry this garbage...I can't see a good way to make this work. All layers in mkimagexx are somewhat assuming that they own physical and virtual address space.
Quoted 7 lines
I think I managed to hack things up to a point where I can have them disjoint, but it's not pretty. I also still have problems where relocations just won't work, because they are constructed in mkimagexx which has no visibility on virtual placement eventually.
At this point, I would rather sacrifice 14kb rather than have an unmaintainable mess that is even worse than today's state of affairs.
Thank you for trying to do that. If it is difficult then make GRUB_PE32_SECTION_ALIGNMENT equal to GRUB_PE32_FILE_ALIGNMENT. So, just change GRUB_PE32_SECTION_ALIGNMENT value and the comment before it why GRUB_PE32_SECTION_ALIGNMENT have to be equal GRUB_PE32_FILE_ALIGNMENT. However, I think that the rest of my comments still stands.
Daniel
PATCH v3 4/4 mkimage: Align efi sections on 4k boundary
In reply to Alexander GrafThere is UEFI firmware popping up in the wild now that implements stricter permission checks using NX and write protect page table entry bits.
This means that firmware now may fail to load binaries if its individual sections are not page aligned, as otherwise it can not ensure permission boundaries.
So let's bump all efi section alignments up to 4k (EFI page size). That way we will stay compatible going forward. This brings us into sync with what the MS C compiler toolchain does.
--- util/mkimage.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
util/mkimage.c +5 -5
@@ -194,7 +194,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI32_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_I386, .elf_target = EM_386,
@@ -244,7 +244,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI64_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_X86_64, .elf_target = EM_X86_64,
@@ -421,7 +421,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI64_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_IA64, .elf_target = EM_IA_64,
@@ -602,7 +602,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI32_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_ARMTHUMB_MIXED, .elf_target = EM_ARM,
@@ -618,7 +618,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI64_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_ARM64, .elf_target = EM_AARCH64,
Signature · 1 line
2.12.3
Re: [PATCH v3 4/4] mkimage: Align efi sections on 4k boundary
Quoted 57 lines · Alexander Graf
There is UEFI firmware popping up in the wild now that implements stricter permission checks using NX and write protect page table entry bits.
This means that firmware now may fail to load binaries if its individual sections are not page aligned, as otherwise it can not ensure permission boundaries.
So let's bump all efi section alignments up to 4k (EFI page size). That way we will stay compatible going forward. This brings us into sync with what the MS C compiler toolchain does.
Signed-off-by Alexander Graf <[email protected]>--- util/mkimage.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)util/mkimage.c +4
-4@@ -194,7 +194,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI32_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_I386, .elf_target = EM_386,@@ -244,7 +244,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI64_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_X86_64, .elf_target = EM_X86_64,@@ -421,7 +421,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI64_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_IA64, .elf_target = EM_IA_64,@@ -602,7 +602,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI32_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_ARMTHUMB_MIXED, .elf_target = EM_ARM,
util/mkimage.c static const struct grub_install_image_target_desc image_targets[] = · line 624
Quoted 8 lines
util/mkimage.c +1
-1@@ -618,7 +618,7 @@ static const struct grub_install_image_target_desc image_targets[] = .decompressor_compressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_size = TARGET_NO_FIELD, .decompressor_uncompressed_addr = TARGET_NO_FIELD, - .section_align = GRUB_PE32_SECTION_ALIGNMENT, + .section_align = GRUB_EFI_PAGE_SIZE, .vaddr_offset = EFI64_HEADER_SIZE, .pe_target = GRUB_PE32_MACHINE_ARM64, .elf_target = EM_AARCH64,
GRUB_PE32_SECTION_ALIGNMENT should be defined as GRUB_EFI_PAGE_SIZE. Please explain in the comment before its definition why.
Daniel