Skip to contentSkip to conversation

arm64: Support HP Envy X2

PATCH In review Alexander Graf 3 participants 9 messages Jan 28, 2019 – Feb 1, 2019

lore
Share & tools
lore
#0 lore raw

v6 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).

v4 -> v5:

- Use GRUB_EFI_PAGE_SIZE
- Add include to have above const defined
- new patch: mkimage: Clarify file alignment in efi case

v5 -> v6:

- Change EFIxxHEADER_SIZE macros too
- Fix comment

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

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

PATCH v6 1/3 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
#2 lore raw

PATCH v6 2/3 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.

---

v4 -> v5:

- Use GRUB_EFI_PAGE_SIZE
- Add include to have above const defined

v5 -> v6:

- Fix comment

---
 include/grub/efi/pe32.h | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
include/grub/efi/pe32.h +9 -2
index 7d44732d2..207b0382e 100644
@@ -20,6 +20,7 @@
 #define GRUB_EFI_PE32_HEADER	1
 
 #include <grub/types.h>
+#include <grub/efi/memory.h>
 
 /* The MSDOS compatibility stub. This was copied from the output of
    objcopy, and it is not necessary to care about what this means.  */
@@ -50,8 +51,14 @@
 /* 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 currently existing GRUB code can not
+   properly distinguish between in-memory and in-file layout, let's
+   bump all alignment to GRUB_EFI_PAGE_SIZE. */
+#define GRUB_PE32_SECTION_ALIGNMENT	GRUB_EFI_PAGE_SIZE
 #define GRUB_PE32_FILE_ALIGNMENT	GRUB_PE32_SECTION_ALIGNMENT
 
 struct grub_pe32_coff_header
Signature · 1 line
2.12.3
#3 lore raw

PATCH v6 3/3 mkimage: Clarify file alignment in efi case

In reply to Alexander Graf

There are a few spots in the PE generation code for EFI binaries that uses the section alignment rather than file alignment, even though the alignment is really only file bound.

Replace those cases with the file alignment constant instead.

---

v5 -> v6:

- Change EFIxxHEADER_SIZE macros too

---
 util/mkimage.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
util/mkimage.c +6 -6
index 16af12e0c..cf1a47c94 100644
@@ -66,14 +66,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_PE32_FILE_ALIGNMENT)
 
 #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_PE32_FILE_ALIGNMENT)
 
 static const struct grub_install_image_target_desc image_targets[] =
   {
@@ -1227,10 +1227,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
 	  header_size = EFI64_HEADER_SIZE;
 
 	reloc_addr = ALIGN_UP (header_size + core_size,
-			       image_target->section_align);
+			       GRUB_PE32_FILE_ALIGNMENT);
 
 	pe_size = ALIGN_UP (reloc_addr + layout.reloc_size,
-			    image_target->section_align);
+			    GRUB_PE32_FILE_ALIGNMENT);
 	pe_img = xmalloc (reloc_addr + layout.reloc_size);
 	memset (pe_img, 0, header_size);
 	memcpy ((char *) pe_img + header_size, core_img, core_size);
@@ -1280,7 +1280,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
 
 	    o->image_base = 0;
 	    o->section_alignment = grub_host_to_target32 (image_target->section_align);
-	    o->file_alignment = grub_host_to_target32 (image_target->section_align);
+	    o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
 	    o->image_size = grub_host_to_target32 (pe_size);
 	    o->header_size = grub_host_to_target32 (header_size);
 	    o->subsystem = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
@@ -1315,7 +1315,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
 	    o->code_base = grub_cpu_to_le32 (header_size);
 	    o->image_base = 0;
 	    o->section_alignment = grub_host_to_target32 (image_target->section_align);
-	    o->file_alignment = grub_host_to_target32 (image_target->section_align);
+	    o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
 	    o->image_size = grub_host_to_target32 (pe_size);
 	    o->header_size = grub_host_to_target32 (header_size);
 	    o->subsystem = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
Signature · 1 line
2.12.3
Reviewed-by on the v6 series
#4 lore raw
In reply to Alexander Graf
Quoted 49 lines · Alexander Graf

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

v4 -> v5:

- Use GRUB_EFI_PAGE_SIZE
- Add include to have above const defined
- new patch: mkimage: Clarify file alignment in efi case

v5 -> v6:

- Change EFIxxHEADER_SIZE macros too
- Fix comment

Quoted 8 lines

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

 include/grub/efi/pe32.h | 11 +++++++++--
 util/mkimage.c          | 19 +++++++------------
 2 files changed, 16 insertions(+), 14 deletions(-)

Adding Julien.

Julien, may I ask you to test this patch set on your machine? This is what I am going to push if you confirm that it works.

Daniel

#5 lore raw

Hi,

Happy to give some help, so I tried with and without those patches on the current version of Grub available trough Git here : git clone git://git.savannah.gnu.org/grub.git

I can confirm that the patch set is working fine with the Asus NovaGo TP370QL (same SoC than HP Envy X2, Qualcomm Snapdragon 835) !

It's ok with grub-mkstandalone, also ok with grub-mkrescue on a Debian Installer (Buster) - on which kernel, initrd and device-tree are tuned for this kind of laptop
And even ok by completely replacing the grub files (efi file, and arm64-lib files) on the Ubuntu image available here https://github.com/aarch64-laptops/ (I used grub-mkimage, a grub-early.cfg file to tell him to find the filesystem by it's UUID).

I'm also able to confirm that without the patch it doesn't work (hangs up on the Asus logo) so the patch is clearly doing the difference.

Photos of it working :

* https://image.noelshack.com/fichiers/2019/05/3/1548813125-img-20190130-021120.jpg
* https://image.noelshack.com/fichiers/2019/05/3/1548813125-img-20190130-021830.jpg
* https://image.noelshack.com/fichiers/2019/05/3/1548813125-img-20190130-024934.jpg

Thanks again, and best regards

Julien ROBIN

Quoted 66 lines · Daniel Kiper
Quoted 49 lines · Alexander Graf

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

v4 -> v5:

- Use GRUB_EFI_PAGE_SIZE
- Add include to have above const defined
- new patch: mkimage: Clarify file alignment in efi case

v5 -> v6:

- Change EFIxxHEADER_SIZE macros too
- Fix comment

Quoted 8 lines

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

  include/grub/efi/pe32.h | 11 +++++++++--
  util/mkimage.c          | 19 +++++++------------
  2 files changed, 16 insertions(+), 14 deletions(-)

Adding Julien.

Julien, may I ask you to test this patch set on your machine? This is what I am going to push if you confirm that it works.

Daniel

#6 lore raw
Quoted 20 lines · Julien ROBIN

Hi,

Happy to give some help, so I tried with and without those patches on the current version of Grub available trough Git here : git clone
git://git.savannah.gnu.org/grub.git

I can confirm that the patch set is working fine with the Asus NovaGo TP370QL (same SoC than HP Envy X2, Qualcomm Snapdragon 835) !

It's ok with grub-mkstandalone, also ok with grub-mkrescue on a Debian Installer (Buster) - on which kernel, initrd and device-tree are tuned for this kind of laptop
And even ok by completely replacing the grub files (efi file, and arm64-lib files) on the Ubuntu image available here
https://github.com/aarch64-laptops/ (I used grub-mkimage, a grub-early.cfg file to tell him to find the filesystem by it's UUID).

I'm also able to confirm that without the patch it doesn't work (hangs up on the Asus logo) so the patch is clearly doing the difference.

Quoted 7 lines

Photos of it working :

* https://image.noelshack.com/fichiers/2019/05/3/1548813125-img-20190130-021120.jpg
* https://image.noelshack.com/fichiers/2019/05/3/1548813125-img-20190130-021830.jpg
* https://image.noelshack.com/fichiers/2019/05/3/1548813125-img-20190130-024934.jpg

Thanks again, and best regards

Great! May I add "Tested-by: Julien ROBIN <[email protected]>" to the patches?

Daniel

#7 lore raw

Hi, Yes that's fine, I assume that the name of the creator of the patch also deserves to appear ;) he did more than me!

Julien

Le 31/01/2019 à 15:59, Daniel Kiper a écrit :

Quoted 31 lines · Julien ROBIN
Quoted 20 lines

Hi,

Happy to give some help, so I tried with and without those patches on the current version of Grub available trough Git here : git clone
git://git.savannah.gnu.org/grub.git

I can confirm that the patch set is working fine with the Asus NovaGo TP370QL (same SoC than HP Envy X2, Qualcomm Snapdragon 835) !

It's ok with grub-mkstandalone, also ok with grub-mkrescue on a Debian Installer (Buster) - on which kernel, initrd and device-tree are tuned for this kind of laptop
And even ok by completely replacing the grub files (efi file, and arm64-lib files) on the Ubuntu image available here
https://github.com/aarch64-laptops/ (I used grub-mkimage, a grub-early.cfg file to tell him to find the filesystem by it's UUID).

I'm also able to confirm that without the patch it doesn't work (hangs up on the Asus logo) so the patch is clearly doing the difference.

Quoted 7 lines

Photos of it working :

* https://image.noelshack.com/fichiers/2019/05/3/1548813125-img-20190130-021120.jpg
* https://image.noelshack.com/fichiers/2019/05/3/1548813125-img-20190130-021830.jpg
* https://image.noelshack.com/fichiers/2019/05/3/1548813125-img-20190130-024934.jpg

Thanks again, and best regards

Great! May I add "Tested-by: Julien ROBIN <[email protected]>" to the patches?

Daniel

#8 lore raw
Quoted 1 line · Julien ROBIN

Hi, Yes that's fine, I assume that the name of the creator of the patch also

Thanks a lot!

Quoted 1 line

deserves to appear ;) he did more than me!

Yes, patch will have his SOB.

Daniel