Skip to contentSkip to conversation

arm64: Support HP Envy X2

PATCH Superseded Alexander Graf 2 participants 7 messages Jan 23, 2019 – Jan 24, 2019

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover arm64: Support HP Envy X2

I got a new toy recently: 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 all PE alignments to 4K always on all efi targets. This way we improve overall compatibility - there surely will be more devices coming with similar constraints.

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

v3 -> v4:

- Reduce everything down to 1 patch which just adapts *all* alignment

    to GRUB_EFI_PAGE_SIZE (4k).

Alexander Graf (2):
mkimage: Use EFI32_HEADER_SIZE define in arm-efi case
mkimage: Align efi sections on 4k boundary

 include/grub/efi/pe32.h | 9 +++++++--
 util/mkimage.c          | 7 +------
 2 files changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
2.12.3
#1 lore raw

PATCH v4 1/2 mkimage: Use EFI32_HEADER_SIZE define in arm-efi case

The 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
index 353bb1098..16af12e0c 100644
@@ -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
Reviewed-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] mkimage: Use EFI32_HEADER_SIZE define in arm-efi case

Quoted 6 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_SIZE definition.

So let's clean it up to use the common define.

Daniel

#3 lore raw

PATCH v4 2/2 mkimage: Align efi sections on 4k boundary

In reply to 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.

Unfortunately our internals can't deal very well with a mismatch of alignment between the virtual and file offsets, so we have to also pad our target binary a bit.

---
 include/grub/efi/pe32.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
include/grub/efi/pe32.h +7 -2
index 7d44732d2..52ff208c0 100644
@@ -50,8 +50,13 @@
 /* According to the spec, the minimal alignment is 512 bytes...
    But some examples (such as EFI drivers in the Intel
    Sample Implementation) use 32 bytes (0x20) instead, and it seems
-   to be working. For now, GRUB uses 512 bytes for safety.  */
-#define GRUB_PE32_SECTION_ALIGNMENT	0x200
+   to be working.
+
+   However, there is firmware showing up in the field now with
+   page alignment constraints to guarantee that page protection
+   bits take effect. Because we can not easily distinguish between
+   in-memory and in-file layout, let's bump all alignment to 4k. */
+#define GRUB_PE32_SECTION_ALIGNMENT	0x1000
 #define GRUB_PE32_FILE_ALIGNMENT	GRUB_PE32_SECTION_ALIGNMENT
 
 struct grub_pe32_coff_header
Signature · 1 line
2.12.3
#4 lore raw

Re: [PATCH v4 2/2] mkimage: Align efi sections on 4k boundary

Quoted 25 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.

Unfortunately our internals can't deal very well with a mismatch of alignment between the virtual and file offsets, so we have to also pad our target binary a bit.

---
 include/grub/efi/pe32.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
include/grub/efi/pe32.h +0 -0
index 7d44732d2..52ff208c0 100644
@@ -50,8 +50,13 @@
 /* According to the spec, the minimal alignment is 512 bytes...

include/grub/efi/pe32.h line 58

Quoted 10 lines
include/grub/efi/pe32.h +6 -2
    But some examples (such as EFI drivers in the Intel
    Sample Implementation) use 32 bytes (0x20) instead, and it seems
-   to be working. For now, GRUB uses 512 bytes for safety.  */
-#define GRUB_PE32_SECTION_ALIGNMENT	0x200
+   to be working.
+
+   However, there is firmware showing up in the field now with
+   page alignment constraints to guarantee that page protection
+   bits take effect. Because we can not easily distinguish between
+   in-memory and in-file layout, let's bump all alignment to 4k. */

s/4k/GRUB_EFI_PAGE_SIZE/

include/grub/efi/pe32.h line 59

Quoted 1 line
include/grub/efi/pe32.h +1 -0
+#define GRUB_PE32_SECTION_ALIGNMENT	0x1000

I think that this should be:

#define GRUB_PE32_SECTION_ALIGNMENT GRUB_EFI_PAGE_SIZE

And I still miss two patches. One replacing GRUB_PE32_SECTION_ALIGNMENT with GRUB_PE32_FILE_ALIGNMENT in EFI32_HEADER_SIZE and EFI64_HEADER_SIZE macros.

And another for some code in the util/mkimage.c...

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);

...plus...

        o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);

Daniel

#5 lore raw

Re: [PATCH v4 2/2] mkimage: Align efi sections on 4k boundary

Quoted 30 lines · Daniel Kiper
Quoted 29 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.

Unfortunately our internals can't deal very well with a mismatch of alignment between the virtual and file offsets, so we have to also pad our target binary a bit.

---
 include/grub/efi/pe32.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
include/grub/efi/pe32.h +0 -2
index 7d44732d2..52ff208c0 100644
@@ -50,8 +50,13 @@
 /* According to the spec, the minimal alignment is 512 bytes...
    But some examples (such as EFI drivers in the Intel
    Sample Implementation) use 32 bytes (0x20) instead, and it seems
-   to be working. For now, GRUB uses 512 bytes for safety.  */
-#define GRUB_PE32_SECTION_ALIGNMENT	0x200
Quoted 8 lines

include/grub/efi/pe32.h line 58

Quoted 6 lines
include/grub/efi/pe32.h +6 -0
+   to be working.
+
+   However, there is firmware showing up in the field now with
+   page alignment constraints to guarantee that page protection
+   bits take effect. Because we can not easily distinguish between
+   in-memory and in-file layout, let's bump all alignment to 4k. */

s/4k/GRUB_EFI_PAGE_SIZE/

Are you sure you want an

  #include <efi/memory.h>

in this file? I omitted it on purpose ;).

Quoted 16 lines

include/grub/efi/pe32.h line 59

Quoted 2 lines
include/grub/efi/pe32.h +1 -0
+#define GRUB_PE32_SECTION_ALIGNMENT	0x1000

I think that this should be:

#define GRUB_PE32_SECTION_ALIGNMENT GRUB_EFI_PAGE_SIZE

And I still miss two patches. One replacing GRUB_PE32_SECTION_ALIGNMENT with GRUB_PE32_FILE_ALIGNMENT in EFI32_HEADER_SIZE and EFI64_HEADER_SIZE macros.

And another for some code in the util/mkimage.c...

reloc_addr = ALIGN_UP (header_size + core_size,
GRUB_PE32_FILE_ALIGNMENT);

Quoted 6 lines

pe_size = ALIGN_UP (reloc_addr + layout.reloc_size,
GRUB_PE32_FILE_ALIGNMENT);

...plus...

        o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);

As mentioned above, these *have* to be identical today, because otherwise all other assumptions in the code just fall apart. I see little point in playing that they may be unrelated.

Alex

#6 lore raw

Re: [PATCH v4 2/2] mkimage: Align efi sections on 4k boundary

Quoted 40 lines · Alexander Graf
Quoted 30 lines · Daniel Kiper
Quoted 29 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.

Unfortunately our internals can't deal very well with a mismatch of alignment between the virtual and file offsets, so we have to also pad our target binary a bit.

---
 include/grub/efi/pe32.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
include/grub/efi/pe32.h +0 -2
index 7d44732d2..52ff208c0 100644
@@ -50,8 +50,13 @@
 /* According to the spec, the minimal alignment is 512 bytes...
    But some examples (such as EFI drivers in the Intel
    Sample Implementation) use 32 bytes (0x20) instead, and it seems
-   to be working. For now, GRUB uses 512 bytes for safety.  */
-#define GRUB_PE32_SECTION_ALIGNMENT	0x200
Quoted 8 lines

include/grub/efi/pe32.h line 58

Quoted 6 lines
include/grub/efi/pe32.h +6 -0
+   to be working.
+
+   However, there is firmware showing up in the field now with
+   page alignment constraints to guarantee that page protection
+   bits take effect. Because we can not easily distinguish between
+   in-memory and in-file layout, let's bump all alignment to 4k. */

s/4k/GRUB_EFI_PAGE_SIZE/

Quoted 5 lines

Are you sure you want an

  #include <efi/memory.h>

in this file? I omitted it on purpose ;).

OK, probably this is not perfect but if nothing falls a part I would include it in this file. If something breaks I would explain in the comment why we hardcode this value here.

Quoted 18 lines
Quoted 18 lines

include/grub/efi/pe32.h line 59

Quoted 1 line
include/grub/efi/pe32.h +1 -0
+#define GRUB_PE32_SECTION_ALIGNMENT	0x1000

I think that this should be:

#define GRUB_PE32_SECTION_ALIGNMENT GRUB_EFI_PAGE_SIZE

And I still miss two patches. One replacing GRUB_PE32_SECTION_ALIGNMENT with GRUB_PE32_FILE_ALIGNMENT in EFI32_HEADER_SIZE and EFI64_HEADER_SIZE macros.

And another for some code in the util/mkimage.c...

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 7 lines
Quoted 3 lines

...plus...

        o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);

As mentioned above, these *have* to be identical today, because otherwise all other assumptions in the code just fall apart. I see little point in playing that they may be unrelated.

GRUB_PE32_FILE_ALIGNMENT is equal to GRUB_PE32_SECTION_ALIGNMENT (well, I think that it also makes sense to explain before GRUB_PE32_FILE_ALIGNMENT definition why we should keep it equal to GRUB_PE32_SECTION_ALIGNMENT) hence nothing breaks. However, IMO after these changes the code looks more logical and is less confusing.

Daniel