Skip to contentSkip to conversation

EFI stub early spring cleaning part 2

PATCH In review Ard Biesheuvel 2 participants 29 messages Feb 10, 2020 – Feb 27, 2020

Subsystems (3)
lore
Share & tools
lore

Ard Biesheuvel

author maintainer
#0 lore raw

cover EFI stub early spring cleaning part 2

This series is a second pass over the EFI stub code to clean it up and prepare it for some future changes:
- loading the initrd in a different way, which should remove the need for bootloaders to have knowledge about the initrd allocation policies and about how to pass the base and size to the kernel proper,
- support for RISC-V will be arriving soon, which makes this a good time to do some janitorial work to avoid RISC-V from inheriting legacy that only exists for the benefit of other architectures.

First of all, code has been moved into separate source files where appropriate, so that we can benefit from the fact that the EFI stub is a true static library, and so only the objects that are needed to satisfy symbol dependencies are incorporated into the final build. We also move x86's eboot.c into libstub/ so we can use the same cflags and make rules across the entire library. (#5, #7, #9, #12, #13, #14)

Patches #6, #15 and #17 clean up the memory allocation, file I/O and command line parsing routines that are shared by all architectures, to remove open coded logic that is already implemented elsewhere, either in the firmware or in code that we can incorporate from the kernel proper.

Patches #10, #11 and #16 deal with upper limits for memory allocations.

Patches #18 and #19 adds the plumbing to enable us to locate device paths and invoke the EFI LoadFile2 protocol.

The remaining patches are cleanups or minor bug fixes across the board.

Note that this series applies onto Arvind's x86/boot cleanups [0]. Full branch can be found here [1]

[0] https://lore.kernel.org/linux-efi/[email protected]/
[1] https://git.kernel.org/pub/scm/linux/kernel/git/ardb/linux.git/log/?h=efi-spring-cleaning-part2

Ard Biesheuvel (19):
efi/libstub/x86: Remove pointless zeroing of apm_bios_info efi/libstub/x86: Avoid overflowing code32_start on PE entry efi/libstub: Use hidden visiblity for all source files efi/libstub/arm: Relax FDT alignment requirement efi/libstub: Move memory map handling and allocation routines to mem.c efi/libstub: Simplify efi_high_alloc() and rename to
efi_allocate_pages()
efi/libstub/x86: Incorporate eboot.c into libstub efi/libstub: Use consistent type names for file I/O protocols efi/libstub: Move stub specific declarations into efistub.h efi/libstub/x86: Permit bootparams struct to be allocated above 4 GB efi/libstub/x86: Permit cmdline data to be allocated above 4 GB efi/libstub: Move efi_random_alloc() into separate source file efi/libstub: Move get_dram_base() into arm-stub.c efi/libstub: Move file I/O support code into separate file efi/libstub: Rewrite file I/O routine
efi/libstub: Take soft and hard memory limits into account for initrd
loading
efi/libstub: Clean up command line parsing routine efi/libstub: Expose LocateDevicePath boot service efi/libstub: Make the LoadFile EFI protocol accessible

 arch/arm64/include/asm/efi.h                  |  10 -
 arch/x86/boot/compressed/Makefile             |   5 +-
 arch/x86/boot/compressed/eboot.h              |  31 -
 arch/x86/include/asm/efi.h                    |   9 +-
 drivers/firmware/efi/libstub/Makefile         |   6 +-
 drivers/firmware/efi/libstub/arm-stub.c       |  47 +-
 drivers/firmware/efi/libstub/arm64-stub.c     |   8 +-
 .../firmware/efi/libstub/efi-stub-helper.c    | 725 +-----------------
 drivers/firmware/efi/libstub/efistub.h        | 586 +++++++++++++-
 drivers/firmware/efi/libstub/fdt.c            |   7 +-
 drivers/firmware/efi/libstub/file.c           | 258 +++++++
 drivers/firmware/efi/libstub/hidden.h         |   6 +
 drivers/firmware/efi/libstub/mem.c            | 253 ++++++
 drivers/firmware/efi/libstub/random.c         | 114 ---
 drivers/firmware/efi/libstub/randomalloc.c    | 124 +++
 drivers/firmware/efi/libstub/string.c         |  63 ++
 .../firmware/efi/libstub/x86-stub.c           |  79 +-
 include/linux/efi.h                           | 516 +------------
 18 files changed, 1389 insertions(+), 1458 deletions(-)
 delete mode 100644 arch/x86/boot/compressed/eboot.h
 create mode 100644 drivers/firmware/efi/libstub/file.c
 create mode 100644 drivers/firmware/efi/libstub/hidden.h
 create mode 100644 drivers/firmware/efi/libstub/mem.c
 create mode 100644 drivers/firmware/efi/libstub/randomalloc.c
 rename arch/x86/boot/compressed/eboot.c => drivers/firmware/efi/libstub/x86-stub.c (94%)
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH 1/19 efi/libstub/x86: Remove pointless zeroing of apm_bios_info

We have some code in the EFI stub entry point that takes the address of the apm_bios_info struct in the newly allocated and zeroed out boot_params structure, only to zero it out again. This is pointless so remove it.

---
 arch/x86/boot/compressed/eboot.c | 5 -----
 1 file changed, 5 deletions(-)
arch/x86/boot/compressed/eboot.c +0 -5
index 32423e83ba8f..4745a1ee7953 100644
@@ -358,7 +358,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 				   efi_system_table_t *sys_table_arg)
 {
 	struct boot_params *boot_params;
-	struct apm_bios_info *bi;
 	struct setup_header *hdr;
 	efi_loaded_image_t *image;
 	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
@@ -389,7 +388,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	memset(boot_params, 0x0, 0x4000);
 
 	hdr = &boot_params->hdr;
-	bi = &boot_params->apm_bios_info;
 
 	/* Copy the second sector to boot_params */
 	memcpy(&hdr->jump, image->image_base + 512, 512);
@@ -416,9 +414,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	hdr->ramdisk_image = 0;
 	hdr->ramdisk_size = 0;
 
-	/* Clear APM BIOS info */
-	memset(bi, 0, sizeof(*bi));
-
 	status = efi_parse_options(cmdline_ptr);
 	if (status != EFI_SUCCESS)
 		goto fail2;
Signature · 1 line
2.17.1
author maintainer
#2 lore raw

PATCH 2/19 efi/libstub/x86: Avoid overflowing code32_start on PE entry

In reply to Ard Biesheuvel

When using the native PE entry point (as opposed to the EFI handover protocol entry point that is used more widely), we set code32_start, which is a 32-bit wide field, to the effective symbol address of startup_32, which could overflow given that the EFI loader may have located the running image anywhere in memory, and we haven't reached the point yet where we relocate ourselves.

Since we relocate ourselves if code32_start != pref_address, this isn't likely to lead to problems in practice, given how unlikely it is that the truncated effective address of startup_32 happens to equal pref_address. But it is better to defer the assignment of code32_start to after the relocation, when it is guaranteed to fit.

While at it, move the call to efi_relocate_kernel() to an earlier stage so it is more likely that our preferred offset in memory has not been occupied by other memory allocations done in the mean time.

---
 arch/x86/boot/compressed/eboot.c | 40 +++++++++-----------
 1 file changed, 18 insertions(+), 22 deletions(-)
arch/x86/boot/compressed/eboot.c +18 -22
index 4745a1ee7953..55637135b50c 100644
@@ -439,8 +439,6 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
 	boot_params->ext_ramdisk_size  = (u64)ramdisk_size >> 32;
 
-	hdr->code32_start = (u32)(unsigned long)startup_32;
-
 	efi_stub_entry(handle, sys_table, boot_params);
 	/* not reached */
 
@@ -707,6 +705,7 @@ struct boot_params *efi_main(efi_handle_t handle,
 			     efi_system_table_t *sys_table_arg,
 			     struct boot_params *boot_params)
 {
+	unsigned long bzimage_addr = (unsigned long)startup_32;
 	struct setup_header *hdr = &boot_params->hdr;
 	efi_status_t status;
 	unsigned long cmdline_paddr;
@@ -717,6 +716,23 @@ struct boot_params *efi_main(efi_handle_t handle,
 	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
 		goto fail;
 
+	/*
+	 * If the kernel isn't already loaded at the preferred load
+	 * address, relocate it.
+	 */
+	if (bzimage_addr != hdr->pref_address) {
+		status = efi_relocate_kernel(&bzimage_addr,
+					     hdr->init_size, hdr->init_size,
+					     hdr->pref_address,
+					     hdr->kernel_alignment,
+					     LOAD_PHYSICAL_ADDR);
+		if (status != EFI_SUCCESS) {
+			efi_printk("efi_relocate_kernel() failed!\n");
+			goto fail;
+		}
+	}
+	hdr->code32_start = (u32)bzimage_addr;
+
 	/*
 	 * make_boot_params() may have been called before efi_main(), in which
 	 * case this is the second time we parse the cmdline. This is ok,
@@ -746,26 +762,6 @@ struct boot_params *efi_main(efi_handle_t handle,
 
 	setup_quirks(boot_params);
 
-	/*
-	 * If the kernel isn't already loaded at the preferred load
-	 * address, relocate it.
-	 */
-	if (hdr->pref_address != hdr->code32_start) {
-		unsigned long bzimage_addr = hdr->code32_start;
-		status = efi_relocate_kernel(&bzimage_addr,
-					     hdr->init_size, hdr->init_size,
-					     hdr->pref_address,
-					     hdr->kernel_alignment,
-					     LOAD_PHYSICAL_ADDR);
-		if (status != EFI_SUCCESS) {
-			efi_printk("efi_relocate_kernel() failed!\n");
-			goto fail;
-		}
-
-		hdr->pref_address = hdr->code32_start;
-		hdr->code32_start = bzimage_addr;
-	}
-
 	status = exit_boot(boot_params, handle);
 	if (status != EFI_SUCCESS) {
 		efi_printk("exit_boot() failed!\n");
Signature · 1 line
2.17.1
author maintainer
#3 lore raw

PATCH 3/19 efi/libstub: Use hidden visiblity for all source files

In reply to Ard Biesheuvel

Instead of setting the visibility pragma for a small set of symbol declarations that could result in absolute references that we cannot support in the stub, declare hidden visibility for all code in the EFI stub, which is more robust and future proof.

To ensure that the #pragma is taken into account before any other includes are processed, put it in a header file of its own and include it via the compiler command line using the -include option.

---
 arch/arm64/include/asm/efi.h              | 3 ---
 drivers/firmware/efi/libstub/Makefile     | 2 +-
 drivers/firmware/efi/libstub/arm64-stub.c | 8 +-------
 drivers/firmware/efi/libstub/hidden.h     | 6 ++++++
 4 files changed, 8 insertions(+), 11 deletions(-)
arch/arm64/include/asm/efi.h +0 -3
index 44531a69d32b..56ae87401a26 100644
@@ -107,9 +107,6 @@ static inline void free_screen_info(struct screen_info *si)
 {
 }
 
-/* redeclare as 'hidden' so the compiler will generate relative references */
-extern struct screen_info screen_info __attribute__((__visibility__("hidden")));
-
 static inline void efifb_setup_from_dmi(struct screen_info *si, const char *opt)
 {
 }
drivers/firmware/efi/libstub/Makefile +1 -1
index f14b7636323a..4efdbd711e8e 100644
@@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM)		:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 cflags-$(CONFIG_EFI_ARMSTUB)	+= -I$(srctree)/scripts/dtc/libfdt
 
 KBUILD_CFLAGS			:= $(cflags-y) -DDISABLE_BRANCH_PROFILING \
-				   -D__NO_FORTIFY \
+				   -include hidden.h -D__NO_FORTIFY \
 				   $(call cc-option,-ffreestanding) \
 				   $(call cc-option,-fno-stack-protector) \
 				   -D__DISABLE_EXPORTS
drivers/firmware/efi/libstub/arm64-stub.c +1 -7
index 2915b44132e6..719d03a64329 100644
@@ -6,17 +6,11 @@
  * Adapted from ARM version by Mark Salter <[email protected]>
  */
 
-/*
- * To prevent the compiler from emitting GOT-indirected (and thus absolute)
- * references to the section markers, override their visibility as 'hidden'
- */
-#pragma GCC visibility push(hidden)
-#include <asm/sections.h>
-#pragma GCC visibility pop
 
 #include <linux/efi.h>
 #include <asm/efi.h>
 #include <asm/memory.h>
+#include <asm/sections.h>
 #include <asm/sysreg.h>
 
 #include "efistub.h"
drivers/firmware/efi/libstub/hidden.h new file +6 -0
new file mode 100644
index 000000000000..3493b041f419
@@ -0,0 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * To prevent the compiler from emitting GOT-indirected (and thus absolute)
+ * references to any global symbols, override their visibility as 'hidden'
+ */
+#pragma GCC visibility push(hidden)
Signature · 1 line
2.17.1
#4 lore raw

Re: [PATCH 03/19] efi/libstub: Use hidden visiblity for all source files

Quoted 89 lines · Ard Biesheuvel

Instead of setting the visibility pragma for a small set of symbol declarations that could result in absolute references that we cannot support in the stub, declare hidden visibility for all code in the EFI stub, which is more robust and future proof.

To ensure that the #pragma is taken into account before any other includes are processed, put it in a header file of its own and include it via the compiler command line using the -include option.

---
 arch/arm64/include/asm/efi.h              | 3 ---
 drivers/firmware/efi/libstub/Makefile     | 2 +-
 drivers/firmware/efi/libstub/arm64-stub.c | 8 +-------
 drivers/firmware/efi/libstub/hidden.h     | 6 ++++++
 4 files changed, 8 insertions(+), 11 deletions(-)
arch/arm64/include/asm/efi.h +0 -1
index 44531a69d32b..56ae87401a26 100644
@@ -107,9 +107,6 @@ static inline void free_screen_info(struct
 screen_info *si)
 {
 }
 
-/* redeclare as 'hidden' so the compiler will generate relative

references */
-extern struct screen_info screen_info __attribute__((__visibility__("hidden")));
-
static inline void efifb_setup_from_dmi(struct screen_info *si,
const char *opt)
{
}

drivers/firmware/efi/libstub/Makefile +1 -2
index f14b7636323a..4efdbd711e8e 100644
@@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM)		:= $(subst
 $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 cflags-$(CONFIG_EFI_ARMSTUB)	+= -I$(srctree)/scripts/dtc/libfdt
 
 KBUILD_CFLAGS			:= $(cflags-y)
-DDISABLE_BRANCH_PROFILING \
-				   -D__NO_FORTIFY \
+				   -include hidden.h -D__NO_FORTIFY \
 				   $(call cc-option,-ffreestanding) \
 				   $(call cc-option,-fno-stack-

protector) \

drivers/firmware/efi/libstub/Makefile +0 -0
 				   -D__DISABLE_EXPORTS
drivers/firmware/efi/libstub/arm64-stub.c +0 -3
index 2915b44132e6..719d03a64329 100644
@@ -6,17 +6,11 @@
  * Adapted from ARM version by Mark Salter <[email protected]>
  */
 
-/*
- * To prevent the compiler from emitting GOT-indirected (and thus
 absolute)
- * references to the section markers, override their visibility as

'hidden'
- */

drivers/firmware/efi/libstub/arm64-stub.c +1 -3
-#pragma GCC visibility push(hidden)
-#include <asm/sections.h>
-#pragma GCC visibility pop
 
 #include <linux/efi.h>
 #include <asm/efi.h>
 #include <asm/memory.h>
+#include <asm/sections.h>
 #include <asm/sysreg.h>
 
 #include "efistub.h"
drivers/firmware/efi/libstub/hidden.h new file +3 -0
new file mode 100644
index 000000000000..3493b041f419
@@ -0,0 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * To prevent the compiler from emitting GOT-indirected (and thus
Quoted 5 lines

absolute)
+ * references to any global symbols, override their visibility as 'hidden'
+ */
+#pragma GCC visibility push(hidden)

I am getting following compiler errors because of this patch while cross compiling for ARM, ARM64 and RISC-V.

cc1: fatal error: hidden.h: No such file or directory

Adding the following line to cflags solves the issue which indicates that gcc is not looking at the source file directory for this particularly include file.

-I$(srctree)/drivers/firmware/efi/libstub

I might have some trick in my build setup. Just posting here to see if anybody else also seeing the same problem.

Signature · 2 lines
Regards,
Atish
author maintainer
#5 lore raw

Re: [PATCH 03/19] efi/libstub: Use hidden visiblity for all source files

Quoted 102 lines · Atish Patra
Quoted 89 lines · Ard Biesheuvel

Instead of setting the visibility pragma for a small set of symbol declarations that could result in absolute references that we cannot support in the stub, declare hidden visibility for all code in the EFI stub, which is more robust and future proof.

To ensure that the #pragma is taken into account before any other includes are processed, put it in a header file of its own and include it via the compiler command line using the -include option.

---
 arch/arm64/include/asm/efi.h              | 3 ---
 drivers/firmware/efi/libstub/Makefile     | 2 +-
 drivers/firmware/efi/libstub/arm64-stub.c | 8 +-------
 drivers/firmware/efi/libstub/hidden.h     | 6 ++++++
 4 files changed, 8 insertions(+), 11 deletions(-)
arch/arm64/include/asm/efi.h +0 -1
index 44531a69d32b..56ae87401a26 100644
@@ -107,9 +107,6 @@ static inline void free_screen_info(struct
 screen_info *si)
 {
 }
 
-/* redeclare as 'hidden' so the compiler will generate relative

references */
-extern struct screen_info screen_info __attribute__((__visibility__("hidden")));
-
static inline void efifb_setup_from_dmi(struct screen_info *si,
const char *opt)
{
}

drivers/firmware/efi/libstub/Makefile +1 -2
index f14b7636323a..4efdbd711e8e 100644
@@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM)                := $(subst
 $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 cflags-$(CONFIG_EFI_ARMSTUB) += -I$(srctree)/scripts/dtc/libfdt
 
 KBUILD_CFLAGS                        := $(cflags-y)
-DDISABLE_BRANCH_PROFILING \
-                                -D__NO_FORTIFY \
+                                -include hidden.h -D__NO_FORTIFY \
                                 $(call cc-option,-ffreestanding) \
                                 $(call cc-option,-fno-stack-

protector) \
-D__DISABLE_EXPORTS

drivers/firmware/efi/libstub/arm64-stub.c +0 -3
index 2915b44132e6..719d03a64329 100644
@@ -6,17 +6,11 @@
  * Adapted from ARM version by Mark Salter <[email protected]>
  */
 
-/*
- * To prevent the compiler from emitting GOT-indirected (and thus
 absolute)
- * references to the section markers, override their visibility as

'hidden'
- */

drivers/firmware/efi/libstub/arm64-stub.c +1 -3
-#pragma GCC visibility push(hidden)
-#include <asm/sections.h>
-#pragma GCC visibility pop
 
 #include <linux/efi.h>
 #include <asm/efi.h>
 #include <asm/memory.h>
+#include <asm/sections.h>
 #include <asm/sysreg.h>
 
 #include "efistub.h"
drivers/firmware/efi/libstub/hidden.h new file +3 -0
new file mode 100644
index 000000000000..3493b041f419
@@ -0,0 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * To prevent the compiler from emitting GOT-indirected (and thus
Quoted 5 lines

absolute)
+ * references to any global symbols, override their visibility as 'hidden'
+ */
+#pragma GCC visibility push(hidden)

I am getting following compiler errors because of this patch while cross compiling for ARM, ARM64 and RISC-V.

cc1: fatal error: hidden.h: No such file or directory

Quoted 9 lines

Adding the following line to cflags solves the issue which indicates that gcc is not looking at the source file directory for this particularly include file.

-I$(srctree)/drivers/firmware/efi/libstub

I might have some trick in my build setup. Just posting here to see if anybody else also seeing the same problem.

Thanks Atish. Heinrich reported the same issue. It didn't affect me or any of the bots because it only hits if you build in-tree (i.e., $srctree == $objtree)

My latest efi/next branch carries a fix for this (similar to your suggestion, but using -include)

#6 lore raw

Re: [PATCH 03/19] efi/libstub: Use hidden visiblity for all source files

Quoted 125 lines · Ard Biesheuvel
Quoted 116 lines · Atish Patra
Quoted 97 lines · Ard Biesheuvel

Instead of setting the visibility pragma for a small set of symbol
declarations that could result in absolute references that we cannot
support in the stub, declare hidden visibility for all code in the
EFI stub, which is more robust and future proof.

To ensure that the #pragma is taken into account before any other includes are processed, put it in a header file of its own and include it via the compiler command line using the -include option.

---
 arch/arm64/include/asm/efi.h              | 3 ---
 drivers/firmware/efi/libstub/Makefile     | 2 +-
 drivers/firmware/efi/libstub/arm64-stub.c | 8 +-------
 drivers/firmware/efi/libstub/hidden.h     | 6 ++++++
 4 files changed, 8 insertions(+), 11 deletions(-)
arch/arm64/include/asm/efi.h +0 -1
index 44531a69d32b..56ae87401a26 100644
@@ -107,9 +107,6 @@ static inline void free_screen_info(struct
 screen_info *si)
 {
 }
 
-/* redeclare as 'hidden' so the compiler will generate relative

references */
-extern struct screen_info screen_info __attribute__((__visibility__("hidden")));
-
static inline void efifb_setup_from_dmi(struct screen_info *si,
const char *opt)
{
}

drivers/firmware/efi/libstub/Makefile +1 -2
index f14b7636323a..4efdbd711e8e 100644
@@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM)                := $(subst
 $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \
 cflags-$(CONFIG_EFI_ARMSTUB) += -I$(srctree)/scripts/dtc/libfdt
 
 KBUILD_CFLAGS                        := $(cflags-y)
-DDISABLE_BRANCH_PROFILING \
-                                -D__NO_FORTIFY \
+                                -include hidden.h -D__NO_FORTIFY
 \
                                 $(call cc-option,-ffreestanding) 
\
                                 $(call cc-option,-fno-stack-

protector) \
-D__DISABLE_EXPORTS

drivers/firmware/efi/libstub/arm64-stub.c +0 -2
index 2915b44132e6..719d03a64329 100644
@@ -6,17 +6,11 @@
  * Adapted from ARM version by Mark Salter <[email protected]>
  */
 
-/*
- * To prevent the compiler from emitting GOT-indirected (and

thus
absolute)
- * references to the section markers, override their visibility as
'hidden'
- */

drivers/firmware/efi/libstub/arm64-stub.c +1 -3
-#pragma GCC visibility push(hidden)
-#include <asm/sections.h>
-#pragma GCC visibility pop
 
 #include <linux/efi.h>
 #include <asm/efi.h>
 #include <asm/memory.h>
+#include <asm/sections.h>
 #include <asm/sysreg.h>
 
 #include "efistub.h"
drivers/firmware/efi/libstub/hidden.h new file +3 -0
new file mode 100644
index 000000000000..3493b041f419
@@ -0,0 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * To prevent the compiler from emitting GOT-indirected (and
Quoted 7 lines

thus
absolute)
+ * references to any global symbols, override their visibility as
'hidden'
+ */
+#pragma GCC visibility push(hidden)

I am getting following compiler errors because of this patch while cross compiling for ARM, ARM64 and RISC-V.

cc1: fatal error: hidden.h: No such file or directory

Adding the following line to cflags solves the issue which indicates
that gcc is not looking at the source file directory for this particularly include file.

Quoted 6 lines

-I$(srctree)/drivers/firmware/efi/libstub

I might have some trick in my build setup. Just posting here to see if
anybody else also seeing the same problem.

Quoted 7 lines

Thanks Atish. Heinrich reported the same issue. It didn't affect me or
any of the bots because it only hits if you build in-tree (i.e., $srctree == $objtree)

My latest efi/next branch carries a fix for this (similar to your suggestion, but using -include)

Thanks. That solves the problem. I should have looked in the efi-next first :(.

Signature · 2 lines
Regards,
Atish
author maintainer
#7 lore raw

PATCH 4/19 efi/libstub/arm: Relax FDT alignment requirement

In reply to Ard Biesheuvel

The arm64 kernel no longer requires the FDT blob to fit inside a naturally aligned 2 MB memory block, so remove the code that aligns the allocation to 2 MB.

---
 arch/arm64/include/asm/efi.h       | 7 -------
 drivers/firmware/efi/libstub/fdt.c | 6 +-----
 2 files changed, 1 insertion(+), 12 deletions(-)
arch/arm64/include/asm/efi.h +0 -7
index 56ae87401a26..45e821222774 100644
@@ -57,13 +57,6 @@ efi_status_t __efi_rt_asm_wrapper(void *, const char *, ...);
 
 /* arch specific definitions used by the stub code */
 
-/*
- * AArch64 requires the DTB to be 8-byte aligned in the first 512MiB from
- * start of kernel and may not cross a 2MiB boundary. We set alignment to
- * 2MiB so we know it won't cross a 2MiB boundary.
- */
-#define EFI_FDT_ALIGN	SZ_2M   /* used by allocate_new_fdt_and_exit_boot() */
-
 /*
  * In some configurations (e.g. VMAP_STACK && 64K pages), stacks built into the
  * kernel need greater alignment than we require the segments to be padded to.
drivers/firmware/efi/libstub/fdt.c +1 -5
index 0a91e5232127..f71cd54823b7 100644
@@ -199,10 +199,6 @@ static efi_status_t update_fdt_memmap(void *fdt, struct efi_boot_memmap *map)
 	return EFI_SUCCESS;
 }
 
-#ifndef EFI_FDT_ALIGN
-# define EFI_FDT_ALIGN EFI_PAGE_SIZE
-#endif
-
 struct exit_boot_struct {
 	efi_memory_desc_t	*runtime_map;
 	int			*runtime_entry_count;
@@ -281,7 +277,7 @@ efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
 	pr_efi("Exiting boot services and installing virtual address map...\n");
 
 	map.map = &memory_map;
-	status = efi_high_alloc(MAX_FDT_SIZE, EFI_FDT_ALIGN,
+	status = efi_high_alloc(MAX_FDT_SIZE, EFI_PAGE_SIZE,
 				new_fdt_addr, max_addr);
 	if (status != EFI_SUCCESS) {
 		pr_efi_err("Unable to allocate memory for new device tree.\n");
Signature · 1 line
2.17.1
author maintainer
#8 lore raw

PATCH 5/19 efi/libstub: Move memory map handling and allocation routines to mem.c

In reply to Ard Biesheuvel

Create a new source file mem.c to keep the routines involved in memory allocation and deallocation and manipulation of the EFI memory map.

---
 drivers/firmware/efi/libstub/Makefile          |   2 +-
 drivers/firmware/efi/libstub/efi-stub-helper.c | 313 -------------------
 drivers/firmware/efi/libstub/mem.c             | 319 ++++++++++++++++++++
 3 files changed, 320 insertions(+), 314 deletions(-)
drivers/firmware/efi/libstub/Makefile +1 -1
index 4efdbd711e8e..45d6eb657437 100644
@@ -39,7 +39,7 @@ OBJECT_FILES_NON_STANDARD	:= y
 KCOV_INSTRUMENT			:= n
 
 lib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
-				   random.o pci.o
+				   mem.o random.o pci.o
 
 # include the stub's generic dependencies from lib/ when building for ARM/arm64
 arm-deps-y := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c
drivers/firmware/efi/libstub/efi-stub-helper.c +0 -313

337 lines not on this page · raw message

drivers/firmware/efi/libstub/mem.c new file +319 -0

319 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#9 lore raw

PATCH 6/19 efi/libstub: Simplify efi_high_alloc() and rename to efi_allocate_pages()

In reply to Ard Biesheuvel

The implementation of efi_high_alloc() uses a complicated way of traversing the memory map to find an available region that is located as close as possible to the provided upper limit, and calls AllocatePages subsequently to create the allocation at that exact address.

This is precisely what the EFI_ALLOCATE_MAX_ADDRESS allocation type argument to AllocatePages() does, and considering that EFI_ALLOC_ALIGN only exceeds EFI_PAGE_SIZE on arm64, let's use AllocatePages() directly and implement the alignment using code that the compiler can remove if it does not exceed EFI_PAGE_SIZE.

---
 drivers/firmware/efi/libstub/efi-stub-helper.c |   5 +-
 drivers/firmware/efi/libstub/fdt.c             |   3 +-
 drivers/firmware/efi/libstub/mem.c             | 102 ++++----------------
 include/linux/efi.h                            |   4 +-
 4 files changed, 23 insertions(+), 91 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +2 -3
index 60d13c7a2e92..7afe31357df3 100644
@@ -385,8 +385,7 @@ efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
 		 * so allocate enough memory for all the files.  This is used
 		 * for loading multiple files.
 		 */
-		status = efi_high_alloc(file_size_total, 0x1000, &file_addr,
-					max_addr);
+		status = efi_allocate_pages(file_size_total, &file_addr, max_addr);
 		if (status != EFI_SUCCESS) {
 			pr_efi_err("Failed to alloc highmem for files\n");
 			goto close_handles;
@@ -536,7 +535,7 @@ char *efi_convert_cmdline(efi_loaded_image_t *image,
 
 	options_bytes++;	/* NUL termination */
 
-	status = efi_high_alloc(options_bytes, 0, &cmdline_addr,
+	status = efi_allocate_pages(options_bytes, &cmdline_addr,
 				MAX_CMDLINE_ADDRESS);
 	if (status != EFI_SUCCESS)
 		return NULL;
drivers/firmware/efi/libstub/fdt.c +1 -2
index f71cd54823b7..46cffac7a5f1 100644
@@ -277,8 +277,7 @@ efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
 	pr_efi("Exiting boot services and installing virtual address map...\n");
 
 	map.map = &memory_map;
-	status = efi_high_alloc(MAX_FDT_SIZE, EFI_PAGE_SIZE,
-				new_fdt_addr, max_addr);
+	status = efi_allocate_pages(MAX_FDT_SIZE, new_fdt_addr, max_addr);
 	if (status != EFI_SUCCESS) {
 		pr_efi_err("Unable to allocate memory for new device tree.\n");
 		goto fail;
drivers/firmware/efi/libstub/mem.c +18 -84

118 lines not on this page · raw message

include/linux/efi.h +2 -2
index 7efd7072cca5..7e231c3cfb6f 100644
@@ -1508,8 +1508,8 @@ efi_status_t efi_low_alloc(unsigned long size, unsigned long align,
 	return efi_low_alloc_above(size, align, addr, 0x8);
 }
 
-efi_status_t efi_high_alloc(unsigned long size, unsigned long align,
-			    unsigned long *addr, unsigned long max);
+efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
+				unsigned long max);
 
 efi_status_t efi_relocate_kernel(unsigned long *image_addr,
 				 unsigned long image_size,
Signature · 1 line
2.17.1
author maintainer
#10 lore raw

PATCH 7/19 efi/libstub/x86: Incorporate eboot.c into libstub

In reply to Ard Biesheuvel

Most of the EFI stub source files of all architectures reside under drivers/firmware/efi/libstub, where they share a Makefile with special CFLAGS and an include file with declarations that are only relevant for stub code.

Currently, we carry a lot of stub specific stuff in linux/efi.h only because eboot.c in arch/x86 needs them as well. So let's move eboot.c into libstub/, and move the contents of eboot.h that we still care about into efistub.h

---
 arch/x86/boot/compressed/Makefile                                           |  5 +---
 arch/x86/boot/compressed/eboot.h                                            | 31 --------------------
 drivers/firmware/efi/libstub/Makefile                                       |  1 +
 drivers/firmware/efi/libstub/efistub.h                                      | 16 ++++++++++
 arch/x86/boot/compressed/eboot.c => drivers/firmware/efi/libstub/x86-stub.c |  5 +---
 5 files changed, 19 insertions(+), 39 deletions(-)
arch/x86/boot/compressed/Makefile +1 -4
index 26050ae0b27e..e51879bdc51c 100644
@@ -87,10 +87,7 @@ endif
 
 vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o
 
-$(obj)/eboot.o: KBUILD_CFLAGS += -fshort-wchar -mno-red-zone
-
-vmlinux-objs-$(CONFIG_EFI_STUB) += $(obj)/eboot.o \
-	$(objtree)/drivers/firmware/efi/libstub/lib.a
+vmlinux-objs-$(CONFIG_EFI_STUB) += $(objtree)/drivers/firmware/efi/libstub/lib.a
 vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o
 
 # The compressed kernel is built with -fPIC/-fPIE so that a boot loader
arch/x86/boot/compressed/eboot.h deleted +0 -31
deleted file mode 100644
index 99f35343d443..000000000000
@@ -1,31 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-#ifndef BOOT_COMPRESSED_EBOOT_H
-#define BOOT_COMPRESSED_EBOOT_H
-
-#define SEG_TYPE_DATA		(0 << 3)
-#define SEG_TYPE_READ_WRITE	(1 << 1)
-#define SEG_TYPE_CODE		(1 << 3)
-#define SEG_TYPE_EXEC_READ	(1 << 1)
-#define SEG_TYPE_TSS		((1 << 3) | (1 << 0))
-#define SEG_OP_SIZE_32BIT	(1 << 0)
-#define SEG_GRANULARITY_4KB	(1 << 0)
-
-#define DESC_TYPE_CODE_DATA	(1 << 0)
-
-typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t;
-
-union efi_uga_draw_protocol {
-	struct {
-		efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *,
-						  u32*, u32*, u32*, u32*);
-		void *set_mode;
-		void *blt;
-	};
-	struct {
-		u32 get_mode;
-		u32 set_mode;
-		u32 blt;
-	} mixed_mode;
-};
-
-#endif /* BOOT_COMPRESSED_EBOOT_H */
drivers/firmware/efi/libstub/Makefile +1 -0
index 45d6eb657437..8e15634fa929 100644
@@ -52,6 +52,7 @@ lib-$(CONFIG_EFI_ARMSTUB)	+= arm-stub.o fdt.o string.o \
 
 lib-$(CONFIG_ARM)		+= arm32-stub.o
 lib-$(CONFIG_ARM64)		+= arm64-stub.o
+lib-$(CONFIG_X86)		+= x86-stub.o
 CFLAGS_arm32-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
 CFLAGS_arm64-stub.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
 
drivers/firmware/efi/libstub/efistub.h +16 -0
index c244b165005e..55de118e8267 100644
@@ -90,4 +90,20 @@ void *get_efi_config_table(efi_guid_t guid);
 	efi_rt_call(set_variable, (efi_char16_t *)(name),	\
 		    (efi_guid_t *)(vendor), __VA_ARGS__)
 
+typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t;
+
+union efi_uga_draw_protocol {
+	struct {
+		efi_status_t (__efiapi *get_mode)(efi_uga_draw_protocol_t *,
+						  u32*, u32*, u32*, u32*);
+		void *set_mode;
+		void *blt;
+	};
+	struct {
+		u32 get_mode;
+		u32 set_mode;
+		u32 blt;
+	} mixed_mode;
+};
+
 #endif
drivers/firmware/efi/libstub/x86-stub.c renamed from arch/x86/boot/compressed/eboot.c +1 -4
similarity index 99%
rename from arch/x86/boot/compressed/eboot.c
rename to drivers/firmware/efi/libstub/x86-stub.c
index 55637135b50c..7e7c50883cce 100644
@@ -6,8 +6,6 @@
  *
  * ----------------------------------------------------------------------- */
 
-#pragma GCC visibility push(hidden)
-
 #include <linux/efi.h>
 #include <linux/pci.h>
 
@@ -17,8 +15,7 @@
 #include <asm/desc.h>
 #include <asm/boot.h>
 
-#include "../string.h"
-#include "eboot.h"
+#include "efistub.h"
 
 static efi_system_table_t *sys_table;
 extern const bool efi_is64;
Signature · 1 line
2.17.1
author maintainer
#11 lore raw

PATCH 8/19 efi/libstub: Use consistent type names for file I/O protocols

In reply to Ard Biesheuvel

Align the naming of efi_file_io_interface_t and efi_file_handle_t with the UEFI spec, and call them efi_simple_file_system_protocol_t and efi_file_protocol_t, respectively, using the same convention we use for all other type definitions that originate in the UEFI spec.

While at it, move the definitions to efistub.h, so they are only seen by code that needs them.

---
 drivers/firmware/efi/libstub/efi-stub-helper.c | 16 ++---
 drivers/firmware/efi/libstub/efistub.h         | 63 ++++++++++++++++++++
 include/linux/efi.h                            | 60 +------------------
 3 files changed, 72 insertions(+), 67 deletions(-)
drivers/firmware/efi/libstub/efi-stub-helper.c +8 -8
index 7afe31357df3..6db91655c743 100644
@@ -54,7 +54,7 @@ bool __pure __efi_soft_reserve_enabled(void)
 }
 
 struct file_info {
-	efi_file_handle_t *handle;
+	efi_file_protocol_t *handle;
 	u64 size;
 };
 
@@ -113,7 +113,7 @@ unsigned long get_dram_base(void)
 static efi_status_t efi_file_size(void *__fh, efi_char16_t *filename_16,
 				  void **handle, u64 *file_sz)
 {
-	efi_file_handle_t *h, *fh = __fh;
+	efi_file_protocol_t *h, *fh = __fh;
 	efi_file_info_t *info;
 	efi_status_t status;
 	efi_guid_t info_guid = EFI_FILE_INFO_ID;
@@ -159,22 +159,22 @@ static efi_status_t efi_file_size(void *__fh, efi_char16_t *filename_16,
 	return status;
 }
 
-static efi_status_t efi_file_read(efi_file_handle_t *handle,
+static efi_status_t efi_file_read(efi_file_protocol_t *handle,
 				  unsigned long *size, void *addr)
 {
 	return handle->read(handle, size, addr);
 }
 
-static efi_status_t efi_file_close(efi_file_handle_t *handle)
+static efi_status_t efi_file_close(efi_file_protocol_t *handle)
 {
 	return handle->close(handle);
 }
 
 static efi_status_t efi_open_volume(efi_loaded_image_t *image,
-				    efi_file_handle_t **__fh)
+				    efi_file_protocol_t **__fh)
 {
-	efi_file_io_interface_t *io;
-	efi_file_handle_t *fh;
+	efi_simple_file_system_protocol_t *io;
+	efi_file_protocol_t *fh;
 	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
 	efi_status_t status;
 	efi_handle_t handle = image->device_handle;
@@ -282,7 +282,7 @@ efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
 	struct file_info *files;
 	unsigned long file_addr;
 	u64 file_size_total;
-	efi_file_handle_t *fh = NULL;
+	efi_file_protocol_t *fh = NULL;
 	efi_status_t status;
 	int nr_files;
 	char *str;
drivers/firmware/efi/libstub/efistub.h +63 -0

67 lines not on this page · raw message

include/linux/efi.h +1 -59

66 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#12 lore raw

PATCH 9/19 efi/libstub: Move stub specific declarations into efistub.h

In reply to Ard Biesheuvel

Move all the declarations that are only used in stub code from linux/efi.h to efistub.h which is only included locally.

---
 drivers/firmware/efi/libstub/efistub.h | 507 ++++++++++++++++++--
 include/linux/efi.h                    | 456 +-----------------
 2 files changed, 479 insertions(+), 484 deletions(-)
drivers/firmware/efi/libstub/efistub.h +478 -29
index 79cdb219f439..8bb46c1fd2cd 100644
@@ -43,34 +43,6 @@ extern __pure efi_system_table_t  *efi_system_table(void);
 
 #define pr_efi_err(msg) efi_printk("EFI stub: ERROR: "msg)
 
-void efi_char16_printk(efi_char16_t *);
-void efi_char16_printk(efi_char16_t *);
-
-unsigned long get_dram_base(void);
-
-efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
-					    unsigned long *new_fdt_addr,
-					    unsigned long max_addr,
-					    u64 initrd_addr, u64 initrd_size,
-					    char *cmdline_ptr,
-					    unsigned long fdt_addr,
-					    unsigned long fdt_size);
-
-void *get_fdt(unsigned long *fdt_size);
-
-void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
-		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
-		     int *count);
-
-efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
-
-efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr, unsigned long random_seed);
-
-efi_status_t check_platform_features(void);
-
-void *get_efi_config_table(efi_guid_t guid);
-
 /* Helper macros for the usual case of using simple C variables: */
 #ifndef fdt_setprop_inplace_var
 #define fdt_setprop_inplace_var(fdt, node_offset, name, var) \
@@ -90,6 +62,156 @@ void *get_efi_config_table(efi_guid_t guid);
 	efi_rt_call(set_variable, (efi_char16_t *)(name),	\
 		    (efi_guid_t *)(vendor), __VA_ARGS__)
 
+#define efi_get_handle_at(array, idx)					\
+	(efi_is_native() ? (array)[idx] 				\
+		: (efi_handle_t)(unsigned long)((u32 *)(array))[idx])
+
+#define efi_get_handle_num(size)					\
+	((size) / (efi_is_native() ? sizeof(efi_handle_t) : sizeof(u32)))
+
+#define for_each_efi_handle(handle, array, size, i)			\
+	for (i = 0;							\
+	     i < efi_get_handle_num(size) &&				\
+		((handle = efi_get_handle_at((array), i)) || true);	\
+	     i++)
+
+/*
+ * Allocation types for calls to boottime->allocate_pages.
+ */
+#define EFI_ALLOCATE_ANY_PAGES		0
+#define EFI_ALLOCATE_MAX_ADDRESS	1
+#define EFI_ALLOCATE_ADDRESS		2
+#define EFI_MAX_ALLOCATE_TYPE		3
+
+/*
+ * The type of search to perform when calling boottime->locate_handle
+ */
+#define EFI_LOCATE_ALL_HANDLES			0
+#define EFI_LOCATE_BY_REGISTER_NOTIFY		1
+#define EFI_LOCATE_BY_PROTOCOL			2
+
+struct efi_boot_memmap {
+	efi_memory_desc_t	**map;
+	unsigned long		*map_size;
+	unsigned long		*desc_size;
+	u32			*desc_ver;
+	unsigned long		*key_ptr;
+	unsigned long		*buff_size;
+};
+
+/*
+ * EFI Boot Services table
+ */
+union efi_boot_services {
+	struct {
+		efi_table_hdr_t hdr;
+		void *raise_tpl;
+		void *restore_tpl;
+		efi_status_t (__efiapi *allocate_pages)(int, int, unsigned long,
+							efi_physical_addr_t *);
+		efi_status_t (__efiapi *free_pages)(efi_physical_addr_t,
+						    unsigned long);
+		efi_status_t (__efiapi *get_memory_map)(unsigned long *, void *,
+							unsigned long *,
+							unsigned long *, u32 *);
+		efi_status_t (__efiapi *allocate_pool)(int, unsigned long,
+						       void **);
+		efi_status_t (__efiapi *free_pool)(void *);
+		void *create_event;
+		void *set_timer;
+		void *wait_for_event;
+		void *signal_event;
+		void *close_event;
+		void *check_event;
+		void *install_protocol_interface;
+		void *reinstall_protocol_interface;
+		void *uninstall_protocol_interface;
+		efi_status_t (__efiapi *handle_protocol)(efi_handle_t,
+							 efi_guid_t *, void **);
+		void *__reserved;
+		void *register_protocol_notify;
+		efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *,
+						       void *, unsigned long *,
+						       efi_handle_t *);
+		void *locate_device_path;
+		efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *,
+								     void *);
+		void *load_image;
+		void *start_image;
+		void *exit;
+		void *unload_image;
+		efi_status_t (__efiapi *exit_boot_services)(efi_handle_t,
+							    unsigned long);
+		void *get_next_monotonic_count;
+		void *stall;
+		void *set_watchdog_timer;
+		void *connect_controller;
+		efi_status_t (__efiapi *disconnect_controller)(efi_handle_t,
+							       efi_handle_t,
+							       efi_handle_t);
+		void *open_protocol;
+		void *close_protocol;
+		void *open_protocol_information;
+		void *protocols_per_handle;
+		void *locate_handle_buffer;
+		efi_status_t (__efiapi *locate_protocol)(efi_guid_t *, void *,
+							 void **);
+		void *install_multiple_protocol_interfaces;
+		void *uninstall_multiple_protocol_interfaces;
+		void *calculate_crc32;
+		void *copy_mem;
+		void *set_mem;
+		void *create_event_ex;
+	};
+	struct {
+		efi_table_hdr_t hdr;
+		u32 raise_tpl;
+		u32 restore_tpl;
+		u32 allocate_pages;
+		u32 free_pages;
+		u32 get_memory_map;
+		u32 allocate_pool;
+		u32 free_pool;
+		u32 create_event;
+		u32 set_timer;
+		u32 wait_for_event;
+		u32 signal_event;
+		u32 close_event;
+		u32 check_event;
+		u32 install_protocol_interface;
+		u32 reinstall_protocol_interface;
+		u32 uninstall_protocol_interface;
+		u32 handle_protocol;
+		u32 __reserved;
+		u32 register_protocol_notify;
+		u32 locate_handle;
+		u32 locate_device_path;
+		u32 install_configuration_table;
+		u32 load_image;
+		u32 start_image;
+		u32 exit;
+		u32 unload_image;
+		u32 exit_boot_services;
+		u32 get_next_monotonic_count;
+		u32 stall;
+		u32 set_watchdog_timer;
+		u32 connect_controller;
+		u32 disconnect_controller;
+		u32 open_protocol;
+		u32 close_protocol;
+		u32 open_protocol_information;
+		u32 protocols_per_handle;
+		u32 locate_handle_buffer;
+		u32 locate_protocol;
+		u32 install_multiple_protocol_interfaces;
+		u32 uninstall_multiple_protocol_interfaces;
+		u32 calculate_crc32;
+		u32 copy_mem;
+		u32 set_mem;
+		u32 create_event_ex;
+	} mixed_mode;
+};
+
 typedef union efi_uga_draw_protocol efi_uga_draw_protocol_t;
 
 union efi_uga_draw_protocol {
@@ -106,7 +228,81 @@ union efi_uga_draw_protocol {
 	} mixed_mode;
 };
 
-typedef struct efi_loaded_image {
+union efi_simple_text_output_protocol {
+	struct {
+		void *reset;
+		efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *,
+						       efi_char16_t *);
+		void *test_string;
+	};
+	struct {
+		u32 reset;
+		u32 output_string;
+		u32 test_string;
+	} mixed_mode;
+};
+
+#define PIXEL_RGB_RESERVED_8BIT_PER_COLOR		0
+#define PIXEL_BGR_RESERVED_8BIT_PER_COLOR		1
+#define PIXEL_BIT_MASK					2
+#define PIXEL_BLT_ONLY					3
+#define PIXEL_FORMAT_MAX				4
+
+typedef struct {
+	u32 red_mask;
+	u32 green_mask;
+	u32 blue_mask;
+	u32 reserved_mask;
+} efi_pixel_bitmask_t;
+
+typedef struct {
+	u32 version;
+	u32 horizontal_resolution;
+	u32 vertical_resolution;
+	int pixel_format;
+	efi_pixel_bitmask_t pixel_information;
+	u32 pixels_per_scan_line;
+} efi_graphics_output_mode_info_t;
+
+typedef union efi_graphics_output_protocol_mode efi_graphics_output_protocol_mode_t;
+
+union efi_graphics_output_protocol_mode {
+	struct {
+		u32 max_mode;
+		u32 mode;
+		efi_graphics_output_mode_info_t *info;
+		unsigned long size_of_info;
+		efi_physical_addr_t frame_buffer_base;
+		unsigned long frame_buffer_size;
+	};
+	struct {
+		u32 max_mode;
+		u32 mode;
+		u32 info;
+		u32 size_of_info;
+		u64 frame_buffer_base;
+		u32 frame_buffer_size;
+	} mixed_mode;
+};
+
+typedef union efi_graphics_output_protocol efi_graphics_output_protocol_t;
+
+union efi_graphics_output_protocol {
+	struct {
+		void *query_mode;
+		void *set_mode;
+		void *blt;
+		efi_graphics_output_protocol_mode_t *mode;
+	};
+	struct {
+		u32 query_mode;
+		u32 set_mode;
+		u32 blt;
+		u32 mode;
+	} mixed_mode;
+};
+
+typedef struct {
 	u32			revision;
 	efi_handle_t		parent_handle;
 	efi_system_table_t	*system_table;
@@ -169,4 +365,257 @@ struct efi_simple_file_system_protocol {
 #define EFI_FILE_MODE_WRITE	0x0000000000000002
 #define EFI_FILE_MODE_CREATE	0x8000000000000000
 
+typedef enum {
+	EfiPciIoWidthUint8,
+	EfiPciIoWidthUint16,
+	EfiPciIoWidthUint32,
+	EfiPciIoWidthUint64,
+	EfiPciIoWidthFifoUint8,
+	EfiPciIoWidthFifoUint16,
+	EfiPciIoWidthFifoUint32,
+	EfiPciIoWidthFifoUint64,
+	EfiPciIoWidthFillUint8,
+	EfiPciIoWidthFillUint16,
+	EfiPciIoWidthFillUint32,
+	EfiPciIoWidthFillUint64,
+	EfiPciIoWidthMaximum
+} EFI_PCI_IO_PROTOCOL_WIDTH;
+
+typedef enum {
+	EfiPciIoAttributeOperationGet,
+	EfiPciIoAttributeOperationSet,
+	EfiPciIoAttributeOperationEnable,
+	EfiPciIoAttributeOperationDisable,
+	EfiPciIoAttributeOperationSupported,
+    EfiPciIoAttributeOperationMaximum
+} EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION;
+
+typedef struct {
+	u32 read;
+	u32 write;
+} efi_pci_io_protocol_access_32_t;
+
+typedef union efi_pci_io_protocol efi_pci_io_protocol_t;
+
+typedef
+efi_status_t (__efiapi *efi_pci_io_protocol_cfg_t)(efi_pci_io_protocol_t *,
+						   EFI_PCI_IO_PROTOCOL_WIDTH,
+						   u32 offset,
+						   unsigned long count,
+						   void *buffer);
+
+typedef struct {
+	void *read;
+	void *write;
+} efi_pci_io_protocol_access_t;
+
+typedef struct {
+	efi_pci_io_protocol_cfg_t read;
+	efi_pci_io_protocol_cfg_t write;
+} efi_pci_io_protocol_config_access_t;
+
+union efi_pci_io_protocol {
+	struct {
+		void *poll_mem;
+		void *poll_io;
+		efi_pci_io_protocol_access_t mem;
+		efi_pci_io_protocol_access_t io;
+		efi_pci_io_protocol_config_access_t pci;
+		void *copy_mem;
+		void *map;
+		void *unmap;
+		void *allocate_buffer;
+		void *free_buffer;
+		void *flush;
+		efi_status_t (__efiapi *get_location)(efi_pci_io_protocol_t *,
+						      unsigned long *segment_nr,
+						      unsigned long *bus_nr,
+						      unsigned long *device_nr,
+						      unsigned long *func_nr);
+		void *attributes;
+		void *get_bar_attributes;
+		void *set_bar_attributes;
+		uint64_t romsize;
+		void *romimage;
+	};
+	struct {
+		u32 poll_mem;
+		u32 poll_io;
+		efi_pci_io_protocol_access_32_t mem;
+		efi_pci_io_protocol_access_32_t io;
+		efi_pci_io_protocol_access_32_t pci;
+		u32 copy_mem;
+		u32 map;
+		u32 unmap;
+		u32 allocate_buffer;
+		u32 free_buffer;
+		u32 flush;
+		u32 get_location;
+		u32 attributes;
+		u32 get_bar_attributes;
+		u32 set_bar_attributes;
+		u64 romsize;
+		u32 romimage;
+	} mixed_mode;
+};
+
+#define EFI_PCI_IO_ATTRIBUTE_ISA_MOTHERBOARD_IO 0x0001
+#define EFI_PCI_IO_ATTRIBUTE_ISA_IO 0x0002
+#define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO 0x0004
+#define EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY 0x0008
+#define EFI_PCI_IO_ATTRIBUTE_VGA_IO 0x0010
+#define EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO 0x0020
+#define EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO 0x0040
+#define EFI_PCI_IO_ATTRIBUTE_MEMORY_WRITE_COMBINE 0x0080
+#define EFI_PCI_IO_ATTRIBUTE_IO 0x0100
+#define EFI_PCI_IO_ATTRIBUTE_MEMORY 0x0200
+#define EFI_PCI_IO_ATTRIBUTE_BUS_MASTER 0x0400
+#define EFI_PCI_IO_ATTRIBUTE_MEMORY_CACHED 0x0800
+#define EFI_PCI_IO_ATTRIBUTE_MEMORY_DISABLE 0x1000
+#define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_DEVICE 0x2000
+#define EFI_PCI_IO_ATTRIBUTE_EMBEDDED_ROM 0x4000
+#define EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE 0x8000
+#define EFI_PCI_IO_ATTRIBUTE_ISA_IO_16 0x10000
+#define EFI_PCI_IO_ATTRIBUTE_VGA_PALETTE_IO_16 0x20000
+#define EFI_PCI_IO_ATTRIBUTE_VGA_IO_16 0x40000
+
+struct efi_dev_path;
+
+typedef union apple_properties_protocol apple_properties_protocol_t;
+
+union apple_properties_protocol {
+	struct {
+		unsigned long version;
+		efi_status_t (__efiapi *get)(apple_properties_protocol_t *,
+					     struct efi_dev_path *,
+					     efi_char16_t *, void *, u32 *);
+		efi_status_t (__efiapi *set)(apple_properties_protocol_t *,
+					     struct efi_dev_path *,
+					     efi_char16_t *, void *, u32);
+		efi_status_t (__efiapi *del)(apple_properties_protocol_t *,
+					     struct efi_dev_path *,
+					     efi_char16_t *);
+		efi_status_t (__efiapi *get_all)(apple_properties_protocol_t *,
+						 void *buffer, u32 *);
+	};
+	struct {
+		u32 version;
+		u32 get;
+		u32 set;
+		u32 del;
+		u32 get_all;
+	} mixed_mode;
+};
+
+typedef u32 efi_tcg2_event_log_format;
+
+typedef union efi_tcg2_protocol efi_tcg2_protocol_t;
+
+union efi_tcg2_protocol {
+	struct {
+		void *get_capability;
+		efi_status_t (__efiapi *get_event_log)(efi_handle_t,
+						       efi_tcg2_event_log_format,
+						       efi_physical_addr_t *,
+						       efi_physical_addr_t *,
+						       efi_bool_t *);
+		void *hash_log_extend_event;
+		void *submit_command;
+		void *get_active_pcr_banks;
+		void *set_active_pcr_banks;
+		void *get_result_of_set_active_pcr_banks;
+	};
+	struct {
+		u32 get_capability;
+		u32 get_event_log;
+		u32 hash_log_extend_event;
+		u32 submit_command;
+		u32 get_active_pcr_banks;
+		u32 set_active_pcr_banks;
+		u32 get_result_of_set_active_pcr_banks;
+	} mixed_mode;
+};
+
+void efi_pci_disable_bridge_busmaster(void);
+
+typedef efi_status_t (*efi_exit_boot_map_processing)(
+	struct efi_boot_memmap *map,
+	void *priv);
+
+efi_status_t efi_exit_boot_services(void *handle,
+				    struct efi_boot_memmap *map,
+				    void *priv,
+				    efi_exit_boot_map_processing priv_func);
+
+void efi_char16_printk(efi_char16_t *);
+
+unsigned long get_dram_base(void);
+
+efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
+					    unsigned long *new_fdt_addr,
+					    unsigned long max_addr,
+					    u64 initrd_addr, u64 initrd_size,
+					    char *cmdline_ptr,
+					    unsigned long fdt_addr,
+					    unsigned long fdt_size);
+
+void *get_fdt(unsigned long *fdt_size);
+
+void efi_get_virtmap(efi_memory_desc_t *memory_map, unsigned long map_size,
+		     unsigned long desc_size, efi_memory_desc_t *runtime_map,
+		     int *count);
+
+efi_status_t efi_get_random_bytes(unsigned long size, u8 *out);
+
+efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
+			      unsigned long *addr, unsigned long random_seed);
+
+efi_status_t check_platform_features(void);
+
+void *get_efi_config_table(efi_guid_t guid);
+
+void efi_printk(char *str);
+
+void efi_free(unsigned long size, unsigned long addr);
+
+char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len);
+
+efi_status_t efi_get_memory_map(struct efi_boot_memmap *map);
+
+efi_status_t efi_low_alloc_above(unsigned long size, unsigned long align,
+				 unsigned long *addr, unsigned long min);
+
+static inline
+efi_status_t efi_low_alloc(unsigned long size, unsigned long align,
+			   unsigned long *addr)
+{
+	/*
+	 * Don't allocate at 0x0. It will confuse code that
+	 * checks pointers against NULL. Skip the first 8
+	 * bytes so we start at a nice even number.
+	 */
+	return efi_low_alloc_above(size, align, addr, 0x8);
+}
+
+efi_status_t efi_allocate_pages(unsigned long size, unsigned long *addr,
+				unsigned long max);
+
+efi_status_t efi_relocate_kernel(unsigned long *image_addr,
+				 unsigned long image_size,
+				 unsigned long alloc_size,
+				 unsigned long preferred_addr,
+				 unsigned long alignment,
+				 unsigned long min_addr);
+
+efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
+				  char *cmd_line, char *option_string,
+				  unsigned long max_addr,
+				  unsigned long *load_addr,
+				  unsigned long *load_size);
+
+efi_status_t efi_parse_options(char const *cmdline);
+
+efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
+			   unsigned long size);
+
 #endif
include/linux/efi.h +1 -455

514 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#13 lore raw

PATCH 10/19 efi/libstub/x86: Permit bootparams struct to be allocated above 4 GB

In reply to Ard Biesheuvel

We now support bootparams structures that are located in memory that is not 32-bit addressable, so relax the allocation limit on systems where this feature is enabled.

---
 drivers/firmware/efi/libstub/x86-stub.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/firmware/efi/libstub/x86-stub.c +6 -1
index 7e7c50883cce..9d60352baa0f 100644
@@ -363,6 +363,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	char *cmdline_ptr;
 	unsigned long ramdisk_addr;
 	unsigned long ramdisk_size;
+	bool above4g;
 
 	sys_table = sys_table_arg;
 
@@ -376,7 +377,11 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 		return status;
 	}
 
-	status = efi_low_alloc(0x4000, 1, (unsigned long *)&boot_params);
+	hdr = &((struct boot_params *)image->image_base)->hdr;
+	above4g = hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G;
+
+	status = efi_allocate_pages(0x4000, (unsigned long *)&boot_params,
+				    above4g ? ULONG_MAX : UINT_MAX);
 	if (status != EFI_SUCCESS) {
 		efi_printk("Failed to allocate lowmem for boot params\n");
 		return status;
Signature · 1 line
2.17.1
author maintainer
#14 lore raw

PATCH 11/19 efi/libstub/x86: Permit cmdline data to be allocated above 4 GB

In reply to Ard Biesheuvel

We now support cmdline data that is located in memory that is not 32-bit addressable, so relax the allocation limit on systems where this feature is enabled.

---
 arch/x86/include/asm/efi.h                     | 2 --
 drivers/firmware/efi/libstub/arm-stub.c        | 2 +-
 drivers/firmware/efi/libstub/efi-stub-helper.c | 9 ++-------
 drivers/firmware/efi/libstub/efistub.h         | 3 ++-
 drivers/firmware/efi/libstub/x86-stub.c        | 5 +++--
 5 files changed, 8 insertions(+), 13 deletions(-)
arch/x86/include/asm/efi.h +0 -2
index 86169a24b0d8..85f79accd3f8 100644
@@ -34,8 +34,6 @@ static inline bool efi_have_uv1_memmap(void)
 #define EFI32_LOADER_SIGNATURE	"EL32"
 #define EFI64_LOADER_SIGNATURE	"EL64"
 
-#define MAX_CMDLINE_ADDRESS	UINT_MAX
-
 #define ARCH_EFI_IRQ_FLAGS_MASK	X86_EFLAGS_IF
 
 /*
drivers/firmware/efi/libstub/arm-stub.c +1 -1
index d357393e680e..ebdf1964dd5c 100644
@@ -161,7 +161,7 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table_arg,
 	 * protocol. We are going to copy the command line into the
 	 * device tree, so this can be allocated anywhere.
 	 */
-	cmdline_ptr = efi_convert_cmdline(image, &cmdline_size);
+	cmdline_ptr = efi_convert_cmdline(image, &cmdline_size, ULONG_MAX);
 	if (!cmdline_ptr) {
 		pr_efi_err("getting command line via LOADED_IMAGE_PROTOCOL\n");
 		goto fail;
drivers/firmware/efi/libstub/efi-stub-helper.c +2 -7
index 6db91655c743..92ccd0a51ae6 100644
@@ -497,17 +497,13 @@ static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
 	return dst;
 }
 
-#ifndef MAX_CMDLINE_ADDRESS
-#define MAX_CMDLINE_ADDRESS	ULONG_MAX
-#endif
-
 /*
  * Convert the unicode UEFI command line to ASCII to pass to kernel.
  * Size of memory allocated return in *cmd_line_len.
  * Returns NULL on error.
  */
 char *efi_convert_cmdline(efi_loaded_image_t *image,
-			  int *cmd_line_len)
+			  int *cmd_line_len, unsigned long max_addr)
 {
 	const u16 *s2;
 	u8 *s1 = NULL;
@@ -535,8 +531,7 @@ char *efi_convert_cmdline(efi_loaded_image_t *image,
 
 	options_bytes++;	/* NUL termination */
 
-	status = efi_allocate_pages(options_bytes, &cmdline_addr,
-				MAX_CMDLINE_ADDRESS);
+	status = efi_allocate_pages(options_bytes, &cmdline_addr, max_addr);
 	if (status != EFI_SUCCESS)
 		return NULL;
 
drivers/firmware/efi/libstub/efistub.h +2 -1
index 8bb46c1fd2cd..b94c63d17a4f 100644
@@ -578,7 +578,8 @@ void efi_printk(char *str);
 
 void efi_free(unsigned long size, unsigned long addr);
 
-char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len);
+char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len,
+			  unsigned long max_addr);
 
 efi_status_t efi_get_memory_map(struct efi_boot_memmap *map);
 
drivers/firmware/efi/libstub/x86-stub.c +3 -2
index 9d60352baa0f..f2dbf119837c 100644
@@ -405,7 +405,8 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	hdr->type_of_loader = 0x21;
 
 	/* Convert unicode cmdline to ascii */
-	cmdline_ptr = efi_convert_cmdline(image, &options_size);
+	cmdline_ptr = efi_convert_cmdline(image, &options_size,
+					  above4g ? ULONG_MAX : UINT_MAX);
 	if (!cmdline_ptr)
 		goto fail;
 
@@ -445,7 +446,7 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	/* not reached */
 
 fail2:
-	efi_free(options_size, hdr->cmd_line_ptr);
+	efi_free(options_size, (unsigned long)cmdline_ptr);
 fail:
 	efi_free(0x4000, (unsigned long)boot_params);
 
Signature · 1 line
2.17.1
author maintainer
#15 lore raw

PATCH 12/19 efi/libstub: Move efi_random_alloc() into separate source file

In reply to Ard Biesheuvel

efi_random_alloc() is only used on arm64, but as it shares a source file with efi_random_get_seed(), the latter will pull in the former on other architectures as well.

Let's take advantage of the fact that libstub is a static library, and so the linker will only incorporate objects that are needed to satisfy dependencies in other objects. This means we can move the random alloc code to a separate source file that gets built unconditionally, but only used when needed.

---
 drivers/firmware/efi/libstub/Makefile      |   2 +-
 drivers/firmware/efi/libstub/random.c      | 114 ------------------
 drivers/firmware/efi/libstub/randomalloc.c | 124 ++++++++++++++++++++
 3 files changed, 125 insertions(+), 115 deletions(-)
drivers/firmware/efi/libstub/Makefile +1 -1
index 8e15634fa929..ac0d0e1ef32d 100644
@@ -39,7 +39,7 @@ OBJECT_FILES_NON_STANDARD	:= y
 KCOV_INSTRUMENT			:= n
 
 lib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
-				   mem.o random.o pci.o
+				   mem.o random.o randomalloc.o pci.o
 
 # include the stub's generic dependencies from lib/ when building for ARM/arm64
 arm-deps-y := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c
drivers/firmware/efi/libstub/random.c +0 -114

126 lines not on this page · raw message

drivers/firmware/efi/libstub/randomalloc.c new file +124 -0

124 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#16 lore raw

PATCH 13/19 efi/libstub: Move get_dram_base() into arm-stub.c

In reply to Ard Biesheuvel

get_dram_base() is only called from arm-stub.c so move it into the same source file as its caller.

---
 drivers/firmware/efi/libstub/arm-stub.c        | 33 ++++++++++++++++++
 drivers/firmware/efi/libstub/efi-stub-helper.c | 35 --------------------
 drivers/firmware/efi/libstub/efistub.h         |  2 --
 3 files changed, 33 insertions(+), 37 deletions(-)
drivers/firmware/efi/libstub/arm-stub.c +33 -0
index ebdf1964dd5c..fb5b2b35d3be 100644
@@ -87,6 +87,39 @@ void install_memreserve_table(void)
 		pr_efi_err("Failed to install memreserve config table!\n");
 }
 
+static unsigned long get_dram_base(void)
+{
+	efi_status_t status;
+	unsigned long map_size, buff_size;
+	unsigned long membase  = EFI_ERROR;
+	struct efi_memory_map map;
+	efi_memory_desc_t *md;
+	struct efi_boot_memmap boot_map;
+
+	boot_map.map		= (efi_memory_desc_t **)&map.map;
+	boot_map.map_size	= &map_size;
+	boot_map.desc_size	= &map.desc_size;
+	boot_map.desc_ver	= NULL;
+	boot_map.key_ptr	= NULL;
+	boot_map.buff_size	= &buff_size;
+
+	status = efi_get_memory_map(&boot_map);
+	if (status != EFI_SUCCESS)
+		return membase;
+
+	map.map_end = map.map + map_size;
+
+	for_each_efi_memory_desc_in_map(&map, md) {
+		if (md->attribute & EFI_MEMORY_WB) {
+			if (membase > md->phys_addr)
+				membase = md->phys_addr;
+		}
+	}
+
+	efi_bs_call(free_pool, map.map);
+
+	return membase;
+}
 
 /*
  * This function handles the architcture specific differences between arm and
drivers/firmware/efi/libstub/efi-stub-helper.c +0 -35
index 92ccd0a51ae6..1a8f2cf5a2bd 100644
@@ -75,41 +75,6 @@ void efi_printk(char *str)
 	}
 }
 
-
-unsigned long get_dram_base(void)
-{
-	efi_status_t status;
-	unsigned long map_size, buff_size;
-	unsigned long membase  = EFI_ERROR;
-	struct efi_memory_map map;
-	efi_memory_desc_t *md;
-	struct efi_boot_memmap boot_map;
-
-	boot_map.map =		(efi_memory_desc_t **)&map.map;
-	boot_map.map_size =	&map_size;
-	boot_map.desc_size =	&map.desc_size;
-	boot_map.desc_ver =	NULL;
-	boot_map.key_ptr =	NULL;
-	boot_map.buff_size =	&buff_size;
-
-	status = efi_get_memory_map(&boot_map);
-	if (status != EFI_SUCCESS)
-		return membase;
-
-	map.map_end = map.map + map_size;
-
-	for_each_efi_memory_desc_in_map(&map, md) {
-		if (md->attribute & EFI_MEMORY_WB) {
-			if (membase > md->phys_addr)
-				membase = md->phys_addr;
-		}
-	}
-
-	efi_bs_call(free_pool, map.map);
-
-	return membase;
-}
-
 static efi_status_t efi_file_size(void *__fh, efi_char16_t *filename_16,
 				  void **handle, u64 *file_sz)
 {
drivers/firmware/efi/libstub/efistub.h +0 -2
index b94c63d17a4f..5123def761e9 100644
@@ -549,8 +549,6 @@ efi_status_t efi_exit_boot_services(void *handle,
 
 void efi_char16_printk(efi_char16_t *);
 
-unsigned long get_dram_base(void);
-
 efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
 					    unsigned long *new_fdt_addr,
 					    unsigned long max_addr,
Signature · 1 line
2.17.1
#17 lore raw

Re: [PATCH 13/19] efi/libstub: Move get_dram_base() into arm-stub.c

Quoted 3 lines · Ard Biesheuvel

get_dram_base() is only called from arm-stub.c so move it into the same source file as its caller.

Just FYI: riscv efi stub port is also going to use get_dram_base(). However, I have renamed arm-stub.c to generic efi-stub.c so that arm, arm64 and riscv can reuse it. Thus, Moving get_dram_base() into arm- stub.c works for riscv as well. I will rebase my patches on top of this series.

Quoted 119 lines
---
 drivers/firmware/efi/libstub/arm-stub.c        | 33
++++++++++++++++++
 drivers/firmware/efi/libstub/efi-stub-helper.c | 35 ----------------
----
 drivers/firmware/efi/libstub/efistub.h         |  2 --
 3 files changed, 33 insertions(+), 37 deletions(-)
drivers/firmware/efi/libstub/arm-stub.c +33 -0
index ebdf1964dd5c..fb5b2b35d3be 100644
@@ -87,6 +87,39 @@ void install_memreserve_table(void)
 		pr_efi_err("Failed to install memreserve config
 table!\n");
 }
 
+static unsigned long get_dram_base(void)
+{
+	efi_status_t status;
+	unsigned long map_size, buff_size;
+	unsigned long membase  = EFI_ERROR;
+	struct efi_memory_map map;
+	efi_memory_desc_t *md;
+	struct efi_boot_memmap boot_map;
+
+	boot_map.map		= (efi_memory_desc_t **)&map.map;
+	boot_map.map_size	= &map_size;
+	boot_map.desc_size	= &map.desc_size;
+	boot_map.desc_ver	= NULL;
+	boot_map.key_ptr	= NULL;
+	boot_map.buff_size	= &buff_size;
+
+	status = efi_get_memory_map(&boot_map);
+	if (status != EFI_SUCCESS)
+		return membase;
+
+	map.map_end = map.map + map_size;
+
+	for_each_efi_memory_desc_in_map(&map, md) {
+		if (md->attribute & EFI_MEMORY_WB) {
+			if (membase > md->phys_addr)
+				membase = md->phys_addr;
+		}
+	}
+
+	efi_bs_call(free_pool, map.map);
+
+	return membase;
+}
 
 /*

* This function handles the architcture specific differences
between arm and

drivers/firmware/efi/libstub/efi-stub-helper.c +0 -35
index 92ccd0a51ae6..1a8f2cf5a2bd 100644
@@ -75,41 +75,6 @@ void efi_printk(char *str)
 	}
 }
 
-
-unsigned long get_dram_base(void)
-{
-	efi_status_t status;
-	unsigned long map_size, buff_size;
-	unsigned long membase  = EFI_ERROR;
-	struct efi_memory_map map;
-	efi_memory_desc_t *md;
-	struct efi_boot_memmap boot_map;
-
-	boot_map.map =		(efi_memory_desc_t **)&map.map;
-	boot_map.map_size =	&map_size;
-	boot_map.desc_size =	&map.desc_size;
-	boot_map.desc_ver =	NULL;
-	boot_map.key_ptr =	NULL;
-	boot_map.buff_size =	&buff_size;
-
-	status = efi_get_memory_map(&boot_map);
-	if (status != EFI_SUCCESS)
-		return membase;
-
-	map.map_end = map.map + map_size;
-
-	for_each_efi_memory_desc_in_map(&map, md) {
-		if (md->attribute & EFI_MEMORY_WB) {
-			if (membase > md->phys_addr)
-				membase = md->phys_addr;
-		}
-	}
-
-	efi_bs_call(free_pool, map.map);
-
-	return membase;
-}
-
 static efi_status_t efi_file_size(void *__fh, efi_char16_t
 *filename_16,
 				  void **handle, u64 *file_sz)

{

drivers/firmware/efi/libstub/efistub.h +0 -2
index b94c63d17a4f..5123def761e9 100644
@@ -549,8 +549,6 @@ efi_status_t efi_exit_boot_services(void *handle,
 
 void efi_char16_printk(efi_char16_t *);
 
-unsigned long get_dram_base(void);
-
 efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
 					    unsigned long
 *new_fdt_addr,

unsigned long max_addr,

Signature · 2 lines
Regards,
Atish
author maintainer
#18 lore raw

Re: [PATCH 13/19] efi/libstub: Move get_dram_base() into arm-stub.c

Quoted 6 lines · Atish Patra
Quoted 3 lines · Ard Biesheuvel

get_dram_base() is only called from arm-stub.c so move it into the same source file as its caller.

Quoted 6 lines

Just FYI: riscv efi stub port is also going to use get_dram_base(). However, I have renamed arm-stub.c to generic efi-stub.c so that arm, arm64 and riscv can reuse it. Thus, Moving get_dram_base() into arm- stub.c works for riscv as well. I will rebase my patches on top of this series.

Thanks Atish. I was hoping it would turn out like this, which is why I am pushing this series now.

I haven't looked at your code yet, but please avoid using the command line based initrd/dtb loading routines. I am proposing a cleaner way to provide the initrd from firmware [0], and dtb loading by the stub should not be done in the first place.

[0] https://lore.kernel.org/linux-efi/[email protected]/

#19 lore raw

Re: [PATCH 13/19] efi/libstub: Move get_dram_base() into arm-stub.c

Quoted 21 lines · Ard Biesheuvel
Quoted 5 lines · Atish Patra
Quoted 3 lines · Ard Biesheuvel

get_dram_base() is only called from arm-stub.c so move it into the same source file as its caller.

Quoted 9 lines

Just FYI: riscv efi stub port is also going to use get_dram_base(). However, I have renamed arm-stub.c to generic efi-stub.c so that arm,
arm64 and riscv can reuse it. Thus, Moving get_dram_base() into arm-
stub.c works for riscv as well. I will rebase my patches on top of this
series.

Thanks Atish. I was hoping it would turn out like this, which is why I
am pushing this series now.

Quoted 7 lines

I haven't looked at your code yet, but please avoid using the command line based initrd/dtb loading routines. I am proposing a cleaner way to provide the initrd from firmware [0], and dtb loading by the stub should not be done in the first place.

[0]
https://lore.kernel.org/linux-efi/[email protected]/

Sorry I missed this email earlier. Yes I am not using initrd or dtb loading from U-Boot command line.

Signature · 2 lines
Regards,
Atish
author maintainer
#20 lore raw

Re: [PATCH 13/19] efi/libstub: Move get_dram_base() into arm-stub.c

Quoted 28 lines · Atish Patra
Quoted 26 lines · Ard Biesheuvel
Quoted 5 lines · Atish Patra
Quoted 3 lines · Ard Biesheuvel

get_dram_base() is only called from arm-stub.c so move it into the same source file as its caller.

Quoted 9 lines

Just FYI: riscv efi stub port is also going to use get_dram_base(). However, I have renamed arm-stub.c to generic efi-stub.c so that arm,
arm64 and riscv can reuse it. Thus, Moving get_dram_base() into arm-
stub.c works for riscv as well. I will rebase my patches on top of this
series.

Thanks Atish. I was hoping it would turn out like this, which is why I
am pushing this series now.

I haven't looked at your code yet, but please avoid using the command line based initrd/dtb loading routines. I am proposing a cleaner way to provide the initrd from firmware [0], and dtb loading by the stub should not be done in the first place.

Quoted 6 lines
Quoted 2 lines

[0]
https://lore.kernel.org/linux-efi/[email protected]/

Sorry I missed this email earlier. Yes I am not using initrd or dtb loading from U-Boot command line.

If you use arm-stub.c mostly unmodified, you will be using initrd= command line loading, and it would be better to disable that, and only enable it if there is a request for it (which I doubt will happen).

#21 lore raw

Re: [PATCH 13/19] efi/libstub: Move get_dram_base() into arm-stub.c

Quoted 40 lines · Ard Biesheuvel
Quoted 38 lines · Atish Patra
Quoted 36 lines · Ard Biesheuvel
Quoted 6 lines · Atish Patra
Quoted 4 lines · Ard Biesheuvel

get_dram_base() is only called from arm-stub.c so move it into
the same source file as its caller.

Quoted 12 lines

Just FYI: riscv efi stub port is also going to use get_dram_base().
However, I have renamed arm-stub.c to generic efi-stub.c so that
arm,
arm64 and riscv can reuse it. Thus, Moving get_dram_base() into arm-
stub.c works for riscv as well. I will rebase my patches on top of
this
series.

Thanks Atish. I was hoping it would turn out like this, which is why
I
am pushing this series now.

I haven't looked at your code yet, but please avoid using the command
line based initrd/dtb loading routines. I am proposing a cleaner way
to provide the initrd from firmware [0], and dtb loading by the stub
should not be done in the first place.

[0]
https://lore.kernel.org/linux-efi/[email protected]/

Quoted 6 lines
Quoted 3 lines

Sorry I missed this email earlier. Yes I am not using initrd or dtb loading from U-Boot command line.

If you use arm-stub.c mostly unmodified, you will be using initrd= command line loading,

Do you mean the code path enters to "initrd=" loading part ? I think that code path has a minor issue where it prints even if the there was no "initrd=" supplied in the command line.

"Loaded initrd from command line option\n"

This happens because efi_load_initrd returns EFI_SUCCESS in absense of command string as well.

https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git/tree/drivers/firmware/efi/libstub/arm-stub.c?h=next#n281

Quoted 2 lines

and it would be better to disable that, and only
enable it if there is a request for it (which I doubt will happen).

Signature · 2 lines
Regards,
Atish
author maintainer
#22 lore raw

Re: [PATCH 13/19] efi/libstub: Move get_dram_base() into arm-stub.c

Quoted 53 lines · Atish Patra
Quoted 40 lines · Ard Biesheuvel
Quoted 38 lines · Atish Patra
Quoted 36 lines · Ard Biesheuvel
Quoted 6 lines · Atish Patra
Quoted 4 lines · Ard Biesheuvel

get_dram_base() is only called from arm-stub.c so move it into
the same source file as its caller.

Quoted 12 lines

Just FYI: riscv efi stub port is also going to use get_dram_base().
However, I have renamed arm-stub.c to generic efi-stub.c so that
arm,
arm64 and riscv can reuse it. Thus, Moving get_dram_base() into arm-
stub.c works for riscv as well. I will rebase my patches on top of
this
series.

Thanks Atish. I was hoping it would turn out like this, which is why
I
am pushing this series now.

I haven't looked at your code yet, but please avoid using the command
line based initrd/dtb loading routines. I am proposing a cleaner way
to provide the initrd from firmware [0], and dtb loading by the stub
should not be done in the first place.

[0]
https://lore.kernel.org/linux-efi/[email protected]/

Quoted 6 lines
Quoted 3 lines

Sorry I missed this email earlier. Yes I am not using initrd or dtb loading from U-Boot command line.

If you use arm-stub.c mostly unmodified, you will be using initrd= command line loading,

Do you mean the code path enters to "initrd=" loading part ? I think that code path has a minor issue where it prints even if the there was no "initrd=" supplied in the command line.

Quoted 7 lines

"Loaded initrd from command line option\n"

This happens because efi_load_initrd returns EFI_SUCCESS in absense of command string as well.

https://git.kernel.org/pub/scm/linux/kernel/git/efi/efi.git/tree/drivers/firmware/efi/libstub/arm-stub.c?h=next#n281

Ah, thanks for pointing that out.

author maintainer
#23 lore raw

PATCH 14/19 efi/libstub: Move file I/O support code into separate file

In reply to Ard Biesheuvel

Split off the file I/O support code into a separate source file so it ends up in a separate object file in the static library, allowing the linker to omit it if the routines are not used.

---
 drivers/firmware/efi/libstub/Makefile          |   2 +-
 drivers/firmware/efi/libstub/efi-stub-helper.c | 294 +------------------
 drivers/firmware/efi/libstub/efistub.h         |   1 +
 drivers/firmware/efi/libstub/file.c            | 303 ++++++++++++++++++++
 4 files changed, 311 insertions(+), 289 deletions(-)
drivers/firmware/efi/libstub/Makefile +1 -1
index ac0d0e1ef32d..74a097ef1780 100644
@@ -39,7 +39,7 @@ OBJECT_FILES_NON_STANDARD	:= y
 KCOV_INSTRUMENT			:= n
 
 lib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
-				   mem.o random.o randomalloc.o pci.o
+				   file.o mem.o random.o randomalloc.o pci.o
 
 # include the stub's generic dependencies from lib/ when building for ARM/arm64
 arm-deps-y := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c
drivers/firmware/efi/libstub/efi-stub-helper.c +6 -288

330 lines not on this page · raw message

drivers/firmware/efi/libstub/efistub.h +1 -0
index 5123def761e9..e057d509d5d8 100644
@@ -31,6 +31,7 @@
 #define __efistub_global
 #endif
 
+extern bool __pure nochunk(void);
 extern bool __pure nokaslr(void);
 extern bool __pure is_quiet(void);
 extern bool __pure novamap(void);
drivers/firmware/efi/libstub/file.c new file +303 -0

303 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#24 lore raw

PATCH 15/19 efi/libstub: Rewrite file I/O routine

In reply to Ard Biesheuvel

The file I/O routine that is used to load initrd or dtb files from the EFI system partition suffers from a few issues:
- it converts the u8[] command line back to a UTF-16 string, which is pointless since we only handle initrd or dtb arguments provided via the loaded image protocol anyway, which is where we got the command line from in the first place when booting via the PE entry point,
- in the far majority of cases, only a single initrd= option is present, but it optimizes for multiple options, by going over the command line twice, allocating heap buffers for dynamically sized arrays, etc.
- the coding style is hard to follow, with few comments, and all logic including string parsing etc all conbined in a single routine.

Let's fix this by rewriting most of it, based on the idea that in the case of multiple initrds, we can just allocate a new, bigger buffer and copy over the data before freeing the old one.

---
 drivers/firmware/efi/libstub/arm-stub.c |  12 +-
 drivers/firmware/efi/libstub/efistub.h  |  17 +-
 drivers/firmware/efi/libstub/file.c     | 356 +++++++++-----------
 drivers/firmware/efi/libstub/x86-stub.c |  12 +-
 4 files changed, 169 insertions(+), 228 deletions(-)
drivers/firmware/efi/libstub/arm-stub.c +4 -8
index fb5b2b35d3be..9b12f9b19533 100644
@@ -148,7 +148,7 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table_arg,
 	unsigned long dram_base;
 	/* addr/point and size pairs for memory management*/
 	unsigned long initrd_addr;
-	u64 initrd_size = 0;
+	unsigned long initrd_size = 0;
 	unsigned long fdt_addr = 0;  /* Original DTB */
 	unsigned long fdt_size = 0;
 	char *cmdline_ptr = NULL;
@@ -238,8 +238,7 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table_arg,
 		if (strstr(cmdline_ptr, "dtb="))
 			pr_efi("Ignoring DTB from command line.\n");
 	} else {
-		status = handle_cmdline_files(image, cmdline_ptr, "dtb=",
-					      ~0UL, &fdt_addr, &fdt_size);
+		status = efi_load_dtb(image, &fdt_addr, &fdt_size);
 
 		if (status != EFI_SUCCESS) {
 			pr_efi_err("Failed to load device tree!\n");
@@ -259,11 +258,8 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table_arg,
 	if (!fdt_addr)
 		pr_efi("Generating empty DTB\n");
 
-	status = handle_cmdline_files(image, cmdline_ptr, "initrd=",
-				      efi_get_max_initrd_addr(dram_base,
-							      *image_addr),
-				      (unsigned long *)&initrd_addr,
-				      (unsigned long *)&initrd_size);
+	status = efi_load_initrd(image, &initrd_addr, &initrd_size,
+				 efi_get_max_initrd_addr(dram_base, *image_addr));
 	if (status != EFI_SUCCESS)
 		pr_efi_err("Failed initrd from command line!\n");
 
drivers/firmware/efi/libstub/efistub.h +10 -7
index e057d509d5d8..60d929469b8b 100644
@@ -327,7 +327,7 @@ typedef struct {
 	efi_time_t		last_access_time;
 	efi_time_t		modification_time;
 	__aligned_u64		attribute;
-	efi_char16_t		filename[1];
+	efi_char16_t		filename[];
 } efi_file_info_t;
 
 typedef struct efi_file_protocol efi_file_protocol_t;
@@ -607,15 +607,18 @@ efi_status_t efi_relocate_kernel(unsigned long *image_addr,
 				 unsigned long alignment,
 				 unsigned long min_addr);
 
-efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
-				  char *cmd_line, char *option_string,
-				  unsigned long max_addr,
-				  unsigned long *load_addr,
-				  unsigned long *load_size);
-
 efi_status_t efi_parse_options(char const *cmdline);
 
 efi_status_t efi_setup_gop(struct screen_info *si, efi_guid_t *proto,
 			   unsigned long size);
 
+efi_status_t efi_load_dtb(efi_loaded_image_t *image,
+			  unsigned long *load_addr,
+			  unsigned long *load_size);
+
+efi_status_t efi_load_initrd(efi_loaded_image_t *image,
+			     unsigned long *load_addr,
+			     unsigned long *load_size,
+			     unsigned long max_addr);
+
 #endif
drivers/firmware/efi/libstub/file.c +151 -205

434 lines not on this page · raw message

drivers/firmware/efi/libstub/x86-stub.c +4 -8
index f2dbf119837c..39d04735f1c5 100644
@@ -421,18 +421,14 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 	if (status != EFI_SUCCESS)
 		goto fail2;
 
-	status = handle_cmdline_files(image,
-				      (char *)(unsigned long)hdr->cmd_line_ptr,
-				      "initrd=", hdr->initrd_addr_max,
-				      &ramdisk_addr, &ramdisk_size);
+	status = efi_load_initrd(image, &ramdisk_addr, &ramdisk_size,
+				 hdr->initrd_addr_max);
 
 	if (status != EFI_SUCCESS &&
 	    hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
 		efi_printk("Trying to load files to higher address\n");
-		status = handle_cmdline_files(image,
-				      (char *)(unsigned long)hdr->cmd_line_ptr,
-				      "initrd=", -1UL,
-				      &ramdisk_addr, &ramdisk_size);
+		status = efi_load_initrd(image, &ramdisk_addr, &ramdisk_size,
+					 ULONG_MAX);
 	}
 
 	if (status != EFI_SUCCESS)
Signature · 1 line
2.17.1
author maintainer
#25 lore raw

PATCH 16/19 efi/libstub: Take soft and hard memory limits into account for initrd loading

In reply to Ard Biesheuvel

On x86, the preferred load address of the initrd is still below 4 GB, even though in some cases, we can cope with an initrd that is loaded above that.

To simplify the code, and to make it more straightforward to introduce other ways to load the initrd, pass the soft and hard memory limits at the same time, and let the code handling the initrd= command line option deal with this.

---
 drivers/firmware/efi/libstub/arm-stub.c |  2 +-
 drivers/firmware/efi/libstub/efistub.h  |  3 ++-
 drivers/firmware/efi/libstub/file.c     | 21 ++++++++++++++------
 drivers/firmware/efi/libstub/x86-stub.c | 11 ++--------
 4 files changed, 20 insertions(+), 17 deletions(-)
drivers/firmware/efi/libstub/arm-stub.c +1 -1
index 9b12f9b19533..2edc673ea06c 100644
@@ -258,7 +258,7 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table_arg,
 	if (!fdt_addr)
 		pr_efi("Generating empty DTB\n");
 
-	status = efi_load_initrd(image, &initrd_addr, &initrd_size,
+	status = efi_load_initrd(image, &initrd_addr, &initrd_size, ULONG_MAX,
 				 efi_get_max_initrd_addr(dram_base, *image_addr));
 	if (status != EFI_SUCCESS)
 		pr_efi_err("Failed initrd from command line!\n");
drivers/firmware/efi/libstub/efistub.h +2 -1
index 60d929469b8b..d282d907cd33 100644
@@ -619,6 +619,7 @@ efi_status_t efi_load_dtb(efi_loaded_image_t *image,
 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
 			     unsigned long *load_addr,
 			     unsigned long *load_size,
-			     unsigned long max_addr);
+			     unsigned long soft_limit,
+			     unsigned long hard_limit);
 
 #endif
drivers/firmware/efi/libstub/file.c +15 -6
index 0d67432ed067..be78f64f8d80 100644
@@ -123,7 +123,8 @@ static int find_file_option(const efi_char16_t *cmdline, int cmdline_len,
 static efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
 					 const efi_char16_t *optstr,
 					 int optstr_size,
-					 unsigned long max_addr,
+					 unsigned long soft_limit,
+					 unsigned long hard_limit,
 					 unsigned long *load_addr,
 					 unsigned long *load_size)
 {
@@ -179,8 +180,15 @@ static efi_status_t handle_cmdline_files(efi_loaded_image_t *image,
 		    round_up(alloc_size, EFI_ALLOC_ALIGN)) {
 			unsigned long old_addr = alloc_addr;
 
-			status = efi_allocate_pages(alloc_size + size, &alloc_addr,
-						    max_addr);
+			status = EFI_OUT_OF_RESOURCES;
+			if (soft_limit < hard_limit)
+				status = efi_allocate_pages(alloc_size + size,
+							    &alloc_addr,
+							    soft_limit);
+			if (status == EFI_OUT_OF_RESOURCES)
+				status = efi_allocate_pages(alloc_size + size,
+							    &alloc_addr,
+							    hard_limit);
 			if (status != EFI_SUCCESS) {
 				pr_efi_err("Failed to reallocate memory for files\n");
 				goto err_close_file;
@@ -236,14 +244,15 @@ efi_status_t efi_load_dtb(efi_loaded_image_t *image,
 			  unsigned long *load_size)
 {
 	return handle_cmdline_files(image, L"dtb=", sizeof(L"dtb=") - 2,
-				    ULONG_MAX, load_addr, load_size);
+				    ULONG_MAX, ULONG_MAX, load_addr, load_size);
 }
 
 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
 			     unsigned long *load_addr,
 			     unsigned long *load_size,
-			     unsigned long max_addr)
+			     unsigned long soft_limit,
+			     unsigned long hard_limit)
 {
 	return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
-				    max_addr, load_addr, load_size);
+				    soft_limit, hard_limit, load_addr, load_size);
 }
drivers/firmware/efi/libstub/x86-stub.c +2 -9
index 39d04735f1c5..52a1a2df2071 100644
@@ -422,15 +422,8 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
 		goto fail2;
 
 	status = efi_load_initrd(image, &ramdisk_addr, &ramdisk_size,
-				 hdr->initrd_addr_max);
-
-	if (status != EFI_SUCCESS &&
-	    hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
-		efi_printk("Trying to load files to higher address\n");
-		status = efi_load_initrd(image, &ramdisk_addr, &ramdisk_size,
-					 ULONG_MAX);
-	}
-
+				 hdr->initrd_addr_max,
+				 above4g ? ULONG_MAX : hdr->initrd_addr_max);
 	if (status != EFI_SUCCESS)
 		goto fail2;
 	hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
Signature · 1 line
2.17.1
author maintainer
#26 lore raw

PATCH 17/19 efi/libstub: Clean up command line parsing routine

In reply to Ard Biesheuvel

We currently parse the command non-destructively, to avoid having to allocate memory for a copy before passing it to the standard parsing routines that are used by the core kernel, and which modify the input to delineate the parsed tokens with NUL characters.

Instead, we call strstr() and strncmp() to go over the input multiple times, and match prefixes rather than tokens, which implies that we would match, e.g., 'nokaslrfoo' in the stub and disable KASLR, while the kernel would disregard the option and run with KASLR enabled.

In order to avoid having to reason about whether and how this behavior may be abused, let's clean up the parsing routines, and rebuild them on top of the existing helpers.

---
 drivers/firmware/efi/libstub/Makefile          |  3 +-
 drivers/firmware/efi/libstub/efi-stub-helper.c | 79 +++++++-------------
 drivers/firmware/efi/libstub/string.c          | 63 ++++++++++++++++
 3 files changed, 91 insertions(+), 54 deletions(-)
drivers/firmware/efi/libstub/Makefile +2 -1
index 74a097ef1780..82cb9f9c08ce 100644
@@ -39,7 +39,8 @@ OBJECT_FILES_NON_STANDARD	:= y
 KCOV_INSTRUMENT			:= n
 
 lib-y				:= efi-stub-helper.o gop.o secureboot.o tpm.o \
-				   file.o mem.o random.o randomalloc.o pci.o
+				   file.o mem.o random.o randomalloc.o pci.o \
+				   lib-cmdline.o lib-ctype.o
 
 # include the stub's generic dependencies from lib/ when building for ARM/arm64
 arm-deps-y := fdt_rw.c fdt_ro.c fdt_wip.c fdt.c fdt_empty_tree.c fdt_sw.c
drivers/firmware/efi/libstub/efi-stub-helper.c +26 -53

92 lines not on this page · raw message

drivers/firmware/efi/libstub/string.c +63 -0

72 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#27 lore raw

PATCH 18/19 efi/libstub: Expose LocateDevicePath boot service

In reply to Ard Biesheuvel

We will be adding support for loading the initrd from a GUIDed device path in a subsequent patch, so update the prototype of the LocateDevicePath() boot service to make it callable from our code.

---
 arch/x86/include/asm/efi.h             | 3 +++
 drivers/firmware/efi/libstub/efistub.h | 6 +++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/include/asm/efi.h +3 -0
index 85f79accd3f8..9ced980b123b 100644
@@ -283,6 +283,9 @@ static inline void *efi64_zero_upper(void *p)
 #define __efi64_argmap_locate_protocol(protocol, reg, interface)	\
 	((protocol), (reg), efi64_zero_upper(interface))
 
+#define __efi64_argmap_locate_device_path(protocol, path, handle)	\
+	((protocol), (path), efi64_zero_upper(handle))
+
 /* PCI I/O */
 #define __efi64_argmap_get_location(protocol, seg, bus, dev, func)	\
 	((protocol), efi64_zero_upper(seg), efi64_zero_upper(bus),	\
drivers/firmware/efi/libstub/efistub.h +5 -1
index d282d907cd33..afa774a312f5 100644
@@ -100,6 +100,8 @@ struct efi_boot_memmap {
 	unsigned long		*buff_size;
 };
 
+typedef struct efi_generic_dev_path efi_device_path_protocol_t;
+
 /*
  * EFI Boot Services table
  */
@@ -134,7 +136,9 @@ union efi_boot_services {
 		efi_status_t (__efiapi *locate_handle)(int, efi_guid_t *,
 						       void *, unsigned long *,
 						       efi_handle_t *);
-		void *locate_device_path;
+		efi_status_t (__efiapi *locate_device_path)(efi_guid_t *,
+							    efi_device_path_protocol_t **,
+							    efi_handle_t *);
 		efi_status_t (__efiapi *install_configuration_table)(efi_guid_t *,
 								     void *);
 		void *load_image;
Signature · 1 line
2.17.1
author maintainer
#28 lore raw

PATCH 19/19 efi/libstub: Make the LoadFile EFI protocol accessible

In reply to Ard Biesheuvel

Add the protocol definitions, GUIDs and mixed mode glue so that the EFI loadfile protocol can be used from the stub. This will be used in a future patch to load the initrd.

---
 arch/x86/include/asm/efi.h             |  4 ++++
 drivers/firmware/efi/libstub/efistub.h | 14 ++++++++++++++
 include/linux/efi.h                    |  2 ++
 3 files changed, 20 insertions(+)
arch/x86/include/asm/efi.h +4 -0
index 9ced980b123b..fcb21e3d13c5 100644
@@ -291,6 +291,10 @@ static inline void *efi64_zero_upper(void *p)
 	((protocol), efi64_zero_upper(seg), efi64_zero_upper(bus),	\
 	 efi64_zero_upper(dev), efi64_zero_upper(func))
 
+/* LoadFile */
+#define __efi64_argmap_load_file(protocol, path, policy, bufsize, buf)	\
+	((protocol), (path), (policy), efi64_zero_upper(bufsize), (buf))
+
 /*
  * The macros below handle the plumbing for the argument mapping. To add a
  * mapping for a specific EFI method, simply define a macro
drivers/firmware/efi/libstub/efistub.h +14 -0
index afa774a312f5..34fe3fad042f 100644
@@ -541,6 +541,20 @@ union efi_tcg2_protocol {
 	} mixed_mode;
 };
 
+typedef union efi_load_file_protocol efi_load_file_protocol_t;
+typedef union efi_load_file_protocol efi_load_file2_protocol_t;
+
+union efi_load_file_protocol {
+	struct {
+		efi_status_t (__efiapi *load_file)(efi_load_file_protocol_t *,
+						   efi_device_path_protocol_t *,
+						   bool, unsigned long *, void *);
+	};
+	struct {
+		u32 load_file;
+	} mixed_mode;
+};
+
 void efi_pci_disable_bridge_busmaster(void);
 
 typedef efi_status_t (*efi_exit_boot_map_processing)(
include/linux/efi.h +2 -0
index 0e047d2738cd..9ccf313fe9de 100644
@@ -332,6 +332,8 @@ void efi_native_runtime_setup(void);
 #define EFI_CONSOLE_OUT_DEVICE_GUID		EFI_GUID(0xd3b36f2c, 0xd551, 0x11d4,  0x9a, 0x46, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
 #define APPLE_PROPERTIES_PROTOCOL_GUID		EFI_GUID(0x91bd12fe, 0xf6c3, 0x44fb,  0xa5, 0xb7, 0x51, 0x22, 0xab, 0x30, 0x3a, 0xe0)
 #define EFI_TCG2_PROTOCOL_GUID			EFI_GUID(0x607f766c, 0x7455, 0x42be,  0x93, 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
+#define EFI_LOAD_FILE_PROTOCOL_GUID		EFI_GUID(0x56ec3091, 0x954c, 0x11d2,  0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)
+#define EFI_LOAD_FILE2_PROTOCOL_GUID		EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e,  0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)
 
 #define EFI_IMAGE_SECURITY_DATABASE_GUID	EFI_GUID(0xd719b2cb, 0x3d3a, 0x4596,  0xa3, 0xbc, 0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f)
 #define EFI_SHIM_LOCK_GUID			EFI_GUID(0x605dab50, 0xe046, 0x4300,  0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23)
Signature · 1 line
2.17.1