Skip to contentSkip to conversation

arm64: Add EFI stub and runtime services support

PATCH In review Mark Salter 5 participants 15 messages Jan 10, 2014 – Jan 23, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover arm64: Add EFI stub and runtime services support

This patch series adds EFI support to the arm64 kernel. This support has two main parts: an EFI stub and runtime support. The EFI stub support has the kernel masquerade as a PE/COFF application which can be directly booted by EFI firmware (or by secondary loaders with EFI support). The runtime services support provides access to various EFI firmware services such as reboot, real-time clock, boot variables, and others.

Changes since v1:

* Added Acks (well, one anyway)

* The first 3 patches are new for v2 and provide more generic support for for following EFI patches.

* Lots of changes based on feedback from Catalin mostly. I think I addressed all of his comments.

These patches have dependencies on other patches which are not yet in the kernel but have been posted and are currently under review. In particular:

- Generic fixmap support being discussed here:
http://lkml.org/lkml/2013/11/25/474
This is now in the akpm tree

- early_ioremap support being discussed here:

      https://lkml.org/lkml/2014/1/9/708

- shared EFI update_fdt() function in this series:
http://news.gmane.org/gmane.linux.kernel.efi

A repo with this patch series and the prerequisite patches is at:

git://github.com/mosalter/linux.git (arm64-efi-patches-v2 branch)

Mark Salter (6):
efi: create memory map iteration helper
arm64: Add function to create identity mappings
efi: add helper function to get UEFI params from FDT
arm64: add EFI stub
doc: arm64: add description of EFI stub support
arm64: add EFI runtime services

 Documentation/arm64/booting.txt |   4 +
 Documentation/efi-stub.txt      |  12 +-
 arch/arm64/Kconfig              |  26 +++
 arch/arm64/include/asm/efi.h    |  12 ++
 arch/arm64/include/asm/mmu.h    |   1 +
 arch/arm64/kernel/Makefile      |   4 +
 arch/arm64/kernel/efi-entry.S   |  93 +++++++++++
 arch/arm64/kernel/efi-stub.c    | 181 ++++++++++++++++++++
 arch/arm64/kernel/efi.c         | 353 ++++++++++++++++++++++++++++++++++++++++
 arch/arm64/kernel/head.S        | 112 +++++++++++++
 arch/arm64/kernel/setup.c       |   6 +
 arch/arm64/mm/mmu.c             |  34 ++--
 drivers/firmware/efi/Kconfig    |   7 +
 drivers/firmware/efi/efi.c      |  79 +++++++++
 include/linux/efi.h             |  17 +-
 15 files changed, 927 insertions(+), 14 deletions(-)
 create mode 100644 arch/arm64/include/asm/efi.h
 create mode 100644 arch/arm64/kernel/efi-entry.S
 create mode 100644 arch/arm64/kernel/efi-stub.c
 create mode 100644 arch/arm64/kernel/efi.c
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/6 efi: create memory map iteration helper

There are a lot of places in the kernel which iterate through an EFI memory map. Most of these places use essentially the same for-loop code. This patch adds a for_each_efi_memory_desc() helper to clean up all of the existing duplicate code and avoid more in the future.

---
 include/linux/efi.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/efi.h +6 -0
index 11ce678..9dc5b05 100644
@@ -618,6 +618,12 @@ extern int efi_set_rtc_mmss(const struct timespec *now);
 extern void efi_reserve_boot_services(void);
 extern struct efi_memory_map memmap;
 
+/* Iterate through an efi_memory_map */
+#define for_each_efi_memory_desc(m, md)					   \
+	for ((md) = (m)->map;						   \
+	     (md) <= (efi_memory_desc_t *)((m)->map_end - (m)->desc_size); \
+	     (md) = (void *)(md) + (m)->desc_size)
+
 /**
  * efi_range_is_wc - check the WC bit on an address range
  * @start: starting kvirt address
Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 2/6 arm64: Add function to create identity mappings

In reply to Mark Salter

At boot time, UEFI runtime support needs to call into the UEFI firmware to switch to a virtual address map. This call must be made with UEFI memory regions identity mapped. The exisitng early boot code creates an identity map of kernel text/data but this is not sufficient for UEFI. This patch adds a create_id_mapping() function which reuses the core code of create_mapping() used to create the kernel RAM mappings.

CC Catalin Marinas <[email protected]>
CC Will Deacon <[email protected]>
CC linux-arm-kernel at lists.infradead.org
---
 arch/arm64/include/asm/mmu.h |  1 +
 arch/arm64/mm/mmu.c          | 34 ++++++++++++++++++++++++----------
 2 files changed, 25 insertions(+), 10 deletions(-)
arch/arm64/include/asm/mmu.h +1 -0
index f600d40..9ad4dd4 100644
@@ -28,5 +28,6 @@ extern void paging_init(void);
 extern void setup_mm_for_reboot(void);
 extern void __iomem *early_io_map(phys_addr_t phys, unsigned long virt);
 extern void init_mem_pgprot(void);
+extern void create_id_mapping(phys_addr_t addr, phys_addr_t size);
 
 #endif
arch/arm64/mm/mmu.c +24 -10
index 7b345e3..ccfca44 100644
@@ -228,22 +228,14 @@ static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
  * Create the page directory entries and any necessary page tables for the
  * mapping specified by 'md'.
  */
-static void __init create_mapping(phys_addr_t phys, unsigned long virt,
-				  phys_addr_t size)
+static void __init __create_mapping(pgd_t *pgd, phys_addr_t phys,
+				    unsigned long virt, phys_addr_t size)
 {
 	unsigned long addr, length, end, next;
-	pgd_t *pgd;
-
-	if (virt < VMALLOC_START) {
-		pr_warning("BUG: not creating mapping for 0x%016llx at 0x%016lx - outside kernel range\n",
-			   phys, virt);
-		return;
-	}
 
 	addr = virt & PAGE_MASK;
 	length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
 
-	pgd = pgd_offset_k(addr);
 	end = addr + length;
 	do {
 		next = pgd_addr_end(addr, end);
@@ -252,6 +244,28 @@ static void __init create_mapping(phys_addr_t phys, unsigned long virt,
 	} while (pgd++, addr = next, addr != end);
 }
 
+static void __init create_mapping(phys_addr_t phys, unsigned long virt,
+				  phys_addr_t size)
+{
+	if (virt < VMALLOC_START) {
+		pr_warn("BUG: not creating mapping for 0x%016llx@0x%016lx - outside kernel range\n",
+			phys, virt);
+		return;
+	}
+	__create_mapping(pgd_offset_k(virt & PAGE_MASK), phys, virt, size);
+}
+
+void __init create_id_mapping(phys_addr_t addr, phys_addr_t size)
+{
+	pgd_t *pgd = &idmap_pg_dir[pgd_index(addr)];
+
+	if (pgd >= &idmap_pg_dir[ARRAY_SIZE(idmap_pg_dir)]) {
+		pr_warn("BUG: not creating id mapping for 0x%016llx\n", addr);
+		return;
+	}
+	__create_mapping(pgd, addr, addr, size);
+}
+
 static void __init map_mem(void)
 {
 	struct memblock_region *reg;
Signature · 1 line
1.8.3.1
#6 lore raw

PATCH 3/6 efi: add helper function to get UEFI params from FDT

In reply to Mark Salter

ARM and ARM64 architectures use the device tree to pass UEFI parameters from stub to kernel. These parameters are things known to the stub but not discoverable by the kernel after the stub calls ExitBootSerives(). There is a helper function in:

drivers/firmware/efi/fdt.c

which the stub uses to add the UEFI parameters to the device tree. This patch adds a complimentary helper function which UEFI runtime support may use to retrieve the parameters from the device tree. If an architecture wants to use this helper, it should select CONFIG_UEFI_PARAMS_FROM_FDT.

---
 drivers/firmware/efi/Kconfig |  7 ++++
 drivers/firmware/efi/efi.c   | 79 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/efi.h          |  9 +++++
 3 files changed, 95 insertions(+)
drivers/firmware/efi/Kconfig +7 -0
index 6aecbc8..d5101e6 100644
@@ -36,6 +36,13 @@ config EFI_VARS_PSTORE_DEFAULT_DISABLE
 	  backend for pstore by default. This setting can be overridden
 	  using the efivars module's pstore_disable parameter.
 
+config UEFI_PARAMS_FROM_FDT
+	bool
+	help
+	  Select this config option from the architecture Kconfig if
+	  the EFI runtime support gets system table address, memory
+          map address, and other parameters from the device tree.
+
 endmenu
 
 config UEFI_CPER
drivers/firmware/efi/efi.c +79 -0

88 lines not on this page · raw message

include/linux/efi.h +9 -0
index 9dc5b05..ff8524e 100644
@@ -483,6 +483,14 @@ struct efi_memory_map {
 	unsigned long desc_size;
 };
 
+struct efi_fdt_params {
+	u64 system_table;
+	u64 mmap;
+	u32 mmap_size;
+	u32 desc_size;
+	u32 desc_ver;
+};
+
 typedef struct {
 	u32 revision;
 	void *parent_handle;
@@ -616,6 +624,7 @@ extern void efi_initialize_iomem_resources(struct resource *code_resource,
 extern void efi_get_time(struct timespec *now);
 extern int efi_set_rtc_mmss(const struct timespec *now);
 extern void efi_reserve_boot_services(void);
+extern int efi_get_fdt_params(struct efi_fdt_params *params, int verbose);
 extern struct efi_memory_map memmap;
 
 /* Iterate through an efi_memory_map */
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH 4/6 arm64: add EFI stub

In reply to Mark Salter

This patch adds PE/COFF header fields to the start of the Image so that it appears as an EFI application to EFI firmware. An EFI stub is included to allow direct booting of the kernel Image. Due to EFI firmware limitations, only little endian kernels with 4K page sizes are supported at this time. Support in the COFF header for signed images was provided by Ard Biesheuvel.

---
 arch/arm64/Kconfig            |  10 +++
 arch/arm64/kernel/Makefile    |   3 +
 arch/arm64/kernel/efi-entry.S |  93 ++++++++++++++++++++++
 arch/arm64/kernel/efi-stub.c  | 181 ++++++++++++++++++++++++++++++++++++++++++
 arch/arm64/kernel/head.S      | 112 ++++++++++++++++++++++++++
 5 files changed, 399 insertions(+)
 create mode 100644 arch/arm64/kernel/efi-entry.S
 create mode 100644 arch/arm64/kernel/efi-stub.c
arch/arm64/Kconfig +10 -0
index e66a317..3f1c2b2 100644
@@ -249,6 +249,16 @@ config CMDLINE_FORCE
 	  This is useful if you cannot or don't want to change the
 	  command-line options your boot loader passes to the kernel.
 
+config EFI_STUB
+	bool "EFI stub support"
+	depends on !ARM64_64K_PAGES && OF
+	select LIBFDT
+	default y
+	help
+	  This kernel feature allows an Image to be loaded directly
+	  by EFI firmware without the use of a bootloader.
+	  See Documentation/efi-stub.txt for more information.
+
 endmenu
 
 menu "Userspace binary formats"
arch/arm64/kernel/Makefile +3 -0
index 5ba2fd4..1c52b84 100644
@@ -4,6 +4,8 @@
 
 CPPFLAGS_vmlinux.lds	:= -DTEXT_OFFSET=$(TEXT_OFFSET)
 AFLAGS_head.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
+CFLAGS_efi-stub.o 	:= -DTEXT_OFFSET=$(TEXT_OFFSET) \
+			   -I$(src)/../../../scripts/dtc/libfdt
 
 # Object file lists.
 arm64-obj-y		:= cputable.o debug-monitors.o entry.o irq.o fpsimd.o	\
@@ -18,6 +20,7 @@ arm64-obj-$(CONFIG_SMP)			+= smp.o smp_spin_table.o
 arm64-obj-$(CONFIG_HW_PERF_EVENTS)	+= perf_event.o
 arm64-obj-$(CONFIG_HAVE_HW_BREAKPOINT)+= hw_breakpoint.o
 arm64-obj-$(CONFIG_EARLY_PRINTK)	+= early_printk.o
+arm64-obj-$(CONFIG_EFI_STUB)		+= efi-stub.o efi-entry.o
 
 obj-y					+= $(arm64-obj-y) vdso/
 obj-m					+= $(arm64-obj-m)
arch/arm64/kernel/efi-entry.S new file +93 -0

93 lines not on this page · raw message

arch/arm64/kernel/efi-stub.c new file +181 -0

181 lines not on this page · raw message

arch/arm64/kernel/head.S +112 -0

127 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#12 lore raw

PATCH 5/6 doc: arm64: add description of EFI stub support

In reply to Mark Salter

Add explanation of arm64 EFI stub and kernel image header changes needed to masquerade as a PE/COFF application.

CC linux-doc at vger.kernel.org
CC Rob Landley <[email protected]>
---
 Documentation/arm64/booting.txt |  4 ++++
 Documentation/efi-stub.txt      | 12 +++++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)
Documentation/arm64/booting.txt +4 -0
index a9691cc..aa95d38c 100644
@@ -85,6 +85,10 @@ The decompressed kernel image contains a 64-byte header as follows:
 Header notes:
 
 - code0/code1 are responsible for branching to stext.
+- when booting through EFI, code0/code1 are initially skipped.
+  res5 is an offset to the PE header and the PE header has the EFI
+  entry point (efi_stub_entry). When the stub has done its work, it
+  jumps to code0 to resume the normal boot process.
 
 The image must be placed at the specified offset (currently 0x80000)
 from the start of the system RAM and called there. The start of the
Documentation/efi-stub.txt +9 -3
index a55a0cd..d8ad0c1 100644
@@ -12,6 +12,11 @@ arch/arm/boot/compressed/efi-header.S and
 arch/arm/boot/compressed/efi-stub.c. EFI stub code that is shared
 between architectures is in drivers/firmware/efi/efi-stub-helper.c.
 
+For arm64, there is no compressed kernel support, so the Image itself
+masquerades as a PE/COFF image and the EFI stub is linked into the
+kernel. The arm64 EFI stub lives in arch/arm64/kernel/efi-entry.S
+and arch/arm64/kernel/efi-stub.c.
+
 By using the EFI boot stub it's possible to boot a Linux kernel
 without the use of a conventional EFI boot loader, such as grub or
 elilo. Since the EFI boot stub performs the jobs of a boot loader, in
@@ -28,7 +33,8 @@ the extension the EFI firmware loader will refuse to execute it. It's
 not possible to execute bzImage.efi from the usual Linux file systems
 because EFI firmware doesn't have support for them. For ARM the
 arch/arm/boot/zImage should be copied to the system partition, and it
-may not need to be renamed.
+may not need to be renamed. Similarly for arm64, arch/arm64/boot/Image
+should be copied but not necessarily renamed.
 
 
 **** Passing kernel parameters from the EFI shell
@@ -72,7 +78,7 @@ is passed to bzImage.efi.
 
 **** The "dtb=" option
 
-For the ARM architecture, we also need to be able to provide a device
-tree to the kernel. This is done with the "dtb=" command line option,
+For the ARM and arm64 architectures, we also need to be able to provide a
+device tree to the kernel. This is done with the "dtb=" command line option,
 and is processed in the same manner as the "initrd=" option that is
 described above.
Signature · 1 line
1.8.3.1
#13 lore raw

PATCH 6/6 arm64: add EFI runtime services

In reply to Mark Salter

This patch adds EFI runtime support for arm64. The runtime support allows the kernel to access various EFI runtime services provided by EFI firmware. Things like reboot, real time clock, EFI boot variables, and others.

---
 arch/arm64/Kconfig           |  16 ++
 arch/arm64/include/asm/efi.h |  12 ++
 arch/arm64/kernel/Makefile   |   1 +
 arch/arm64/kernel/efi.c      | 353 +++++++++++++++++++++++++++++++++++++++++++
 arch/arm64/kernel/setup.c    |   6 +
 include/linux/efi.h          |   2 +-
 6 files changed, 389 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/include/asm/efi.h
 create mode 100644 arch/arm64/kernel/efi.c
arch/arm64/Kconfig +16 -0
index 3f1c2b2..862026d 100644
@@ -249,6 +249,20 @@ config CMDLINE_FORCE
 	  This is useful if you cannot or don't want to change the
 	  command-line options your boot loader passes to the kernel.
 
+config EFI
+        bool "EFI runtime service support"
+	depends on !ARM64_64K_PAGES && OF
+	select UCS2_STRING
+	select LIBFDT
+	select UEFI_PARAMS_FROM_FDT
+	help
+	  This enables the kernel to use UEFI runtime services that are
+	  available (such as the UEFI variable services).
+
+	  This option is only useful on systems that have UEFI firmware.
+	  However, even with this option, the resultant kernel should
+	  continue to boot on existing non-UEFI platforms.
+
 config EFI_STUB
 	bool "EFI stub support"
 	depends on !ARM64_64K_PAGES && OF
@@ -290,6 +304,8 @@ source "net/Kconfig"
 
 source "drivers/Kconfig"
 
+source "drivers/firmware/Kconfig"
+
 source "fs/Kconfig"
 
 source "arch/arm64/kvm/Kconfig"
arch/arm64/include/asm/efi.h new file +12 -0
new file mode 100644
index 0000000..a835b2c
@@ -0,0 +1,12 @@
+#ifndef _ASM_ARM64_EFI_H
+#define _ASM_ARM64_EFI_H
+
+#include <asm/io.h>
+
+#ifdef CONFIG_EFI
+extern void efi_init(void);
+#else
+#define efi_init()
+#endif
+
+#endif /* _ASM_ARM64_EFI_H */
arch/arm64/kernel/Makefile +1 -0
index 1c52b84..8897cf5a5 100644
@@ -21,6 +21,7 @@ arm64-obj-$(CONFIG_HW_PERF_EVENTS)	+= perf_event.o
 arm64-obj-$(CONFIG_HAVE_HW_BREAKPOINT)+= hw_breakpoint.o
 arm64-obj-$(CONFIG_EARLY_PRINTK)	+= early_printk.o
 arm64-obj-$(CONFIG_EFI_STUB)		+= efi-stub.o efi-entry.o
+arm64-obj-$(CONFIG_EFI)			+= efi.o
 
 obj-y					+= $(arm64-obj-y) vdso/
 obj-m					+= $(arm64-obj-m)
arch/arm64/kernel/efi.c new file +353 -0

353 lines not on this page · raw message

arch/arm64/kernel/setup.c +6 -0
index 5516f54..7a45095 100644
@@ -41,6 +41,7 @@
 #include <linux/memblock.h>
 #include <linux/of_fdt.h>
 #include <linux/of_platform.h>
+#include <linux/efi.h>
 
 #include <asm/fixmap.h>
 #include <asm/cputype.h>
@@ -55,6 +56,7 @@
 #include <asm/traps.h>
 #include <asm/memblock.h>
 #include <asm/psci.h>
+#include <asm/efi.h>
 
 unsigned int processor_id;
 EXPORT_SYMBOL(processor_id);
@@ -229,9 +231,13 @@ void __init setup_arch(char **cmdline_p)
 
 	arm64_memblock_init();
 
+	efi_init();
 	paging_init();
 	request_standard_resources();
 
+	if (efi_enabled(EFI_BOOT))
+		efi_enter_virtual_mode();
+
 	unflatten_device_tree();
 
 	psci_init();
include/linux/efi.h +1 -1
index ff8524e..4d73efe 100644
@@ -670,7 +670,7 @@ extern int __init efi_setup_pcdp_console(char *);
 #define EFI_64BIT		5	/* Is the firmware 64-bit? */
 
 #ifdef CONFIG_EFI
-# ifdef CONFIG_X86
+# if defined(CONFIG_X86) || defined(CONFIG_ARM64)
 extern int efi_enabled(int facility);
 # else
 static inline int efi_enabled(int facility)
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH 1/6] efi: create memory map iteration helper

In reply to Mark Salter
Quoted 16 lines · Mark Salter

There are a lot of places in the kernel which iterate through an EFI memory map. Most of these places use essentially the same for-loop code. This patch adds a for_each_efi_memory_desc() helper to clean up all of the existing duplicate code and avoid more in the future.

---
 include/linux/efi.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/efi.h +0 -0
index 11ce678..9dc5b05 100644

include/linux/efi.h extern int efi_set_rtc_mmss(const struct timespec *now); · line 626

Quoted 9 lines
include/linux/efi.h +6 -0
@@ -618,6 +618,12 @@ extern int efi_set_rtc_mmss(const struct timespec *now);
 extern void efi_reserve_boot_services(void);
 extern struct efi_memory_map memmap;
 
+/* Iterate through an efi_memory_map */
+#define for_each_efi_memory_desc(m, md)					   \
+	for ((md) = (m)->map;						   \
+	     (md) <= (efi_memory_desc_t *)((m)->map_end - (m)->desc_size); \
+	     (md) = (void *)(md) + (m)->desc_size)
+

Err, what? I just picked up your previous patch ("x86/efi: Create memory map iteration helper") which adds this chunk, and I didn't see a follow up email telling me not to pick it up.

What's the plan?

Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
#3 lore raw

Re: [PATCH 1/6] efi: create memory map iteration helper

Quoted 31 lines · Matt Fleming
Quoted 16 lines · Mark Salter

There are a lot of places in the kernel which iterate through an EFI memory map. Most of these places use essentially the same for-loop code. This patch adds a for_each_efi_memory_desc() helper to clean up all of the existing duplicate code and avoid more in the future.

---
 include/linux/efi.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/efi.h +0 -0
index 11ce678..9dc5b05 100644

include/linux/efi.h extern int efi_set_rtc_mmss(const struct timespec *now); · line 626

Quoted 9 lines
include/linux/efi.h +6 -0
@@ -618,6 +618,12 @@ extern int efi_set_rtc_mmss(const struct timespec *now);
 extern void efi_reserve_boot_services(void);
 extern struct efi_memory_map memmap;
 
+/* Iterate through an efi_memory_map */
+#define for_each_efi_memory_desc(m, md)					   \
+	for ((md) = (m)->map;						   \
+	     (md) <= (efi_memory_desc_t *)((m)->map_end - (m)->desc_size); \
+	     (md) = (void *)(md) + (m)->desc_size)
+

Err, what? I just picked up your previous patch ("x86/efi: Create memory map iteration helper") which adds this chunk, and I didn't see a follow up email telling me not to pick it up.

Quoted 2 lines

What's the plan?

Sorry, I meant to drop this one from this series. The one you picked up is the one I wanted picked up.

--Mark

#8 lore raw

Re: [PATCH 4/6] arm64: add EFI stub

In reply to Mark Salter
Quoted 9 lines · Mark Salter

This patch adds PE/COFF header fields to the start of the Image so that it appears as an EFI application to EFI firmware. An EFI stub is included to allow direct booting of the kernel Image. Due to EFI firmware limitations, only little endian kernels with 4K page sizes are supported at this time. Support in the COFF header for signed images was provided by Ard Biesheuvel.

You got the ordering of the S-o-b lines wrong. Since you send the patch, your name should come last.

arch/arm64/Kconfig config CMDLINE_FORCE · line 262

Quoted 11 lines
arch/arm64/Kconfig +10 -0
+config EFI_STUB
+	bool "EFI stub support"
+	depends on !ARM64_64K_PAGES && OF
+	select LIBFDT
+	default y
+	help
+	  This kernel feature allows an Image to be loaded directly
+	  by EFI firmware without the use of a bootloader.
+	  See Documentation/efi-stub.txt for more information.
+
 endmenu

Why not ARM64_64K_PAGES? I thought that it was going to be the default for a lot of distros that would need to run on UEFI systems.

arch/arm64/kernel/Makefile line 8

Quoted 11 lines

menu "Userspace binary formats"

arch/arm64/kernel/Makefile +2 -0
index 5ba2fd4..1c52b84 100644
@@ -4,6 +4,8 @@
 
 CPPFLAGS_vmlinux.lds	:= -DTEXT_OFFSET=$(TEXT_OFFSET)
 AFLAGS_head.o		:= -DTEXT_OFFSET=$(TEXT_OFFSET)
+CFLAGS_efi-stub.o 	:= -DTEXT_OFFSET=$(TEXT_OFFSET) \
+			   -I$(src)/../../../scripts/dtc/libfdt

Hmm, this is pretty ugly. I notice the same has been done on MIPS as well, but I'd hope we can find a proper way to do it.

arch/arm64/kernel/efi-stub.c line 49

Quoted 9 lines
arch/arm64/kernel/efi-stub.c new file +4 -0
new file mode 100644
index 0000000..10d02bf
+
+/* Include shared EFI stub code */
+#include "../../../drivers/firmware/efi/efi-stub-helper.c"
+#include "../../../drivers/firmware/efi/fdt.c"

It gets worse here.

Arnd

#9 lore raw

Re: [PATCH 4/6] arm64: add EFI stub

Quoted 8 lines · Arnd Bergmann
Quoted 7 lines · Mark Salter

This patch adds PE/COFF header fields to the start of the Image so that it appears as an EFI application to EFI firmware. An EFI stub is included to allow direct booting of the kernel Image. Due to EFI firmware limitations, only little endian kernels with 4K page sizes are supported at this time. Support in the COFF header for signed images was provided by Ard Biesheuvel.

Quoted 5 lines
Quoted 2 lines

You got the ordering of the S-o-b lines wrong. Since you send the patch, your name should come last.

Yes.

Quoted 15 lines

arch/arm64/Kconfig config CMDLINE_FORCE · line 262

Quoted 12 lines
arch/arm64/Kconfig +10 -0
+config EFI_STUB
+     bool "EFI stub support"
+     depends on !ARM64_64K_PAGES && OF
+     select LIBFDT
+     default y
+     help
+       This kernel feature allows an Image to be loaded directly
+       by EFI firmware without the use of a bootloader.
+       See Documentation/efi-stub.txt for more information.
+
 endmenu

Why not ARM64_64K_PAGES? I thought that it was going to be the default for a lot of distros that would need to run on UEFI systems.

The UEFI spec currently mandates 4k page size. We may be able to work around that or get the spec changed, but for now it is just 4k pages.

Quoted 8 lines
Quoted 8 lines

menu "Userspace binary formats"

arch/arm64/kernel/Makefile +0 -0
index 5ba2fd4..1c52b84 100644
@@ -4,6 +4,8 @@
 
Quoted 7 lines

arch/arm64/kernel/Makefile line 8

Quoted 4 lines
arch/arm64/kernel/Makefile +2 -0
 CPPFLAGS_vmlinux.lds := -DTEXT_OFFSET=$(TEXT_OFFSET)
 AFLAGS_head.o                := -DTEXT_OFFSET=$(TEXT_OFFSET)
+CFLAGS_efi-stub.o    := -DTEXT_OFFSET=$(TEXT_OFFSET) \
+                        -I$(src)/../../../scripts/dtc/libfdt

Hmm, this is pretty ugly. I notice the same has been done on MIPS as well, but I'd hope we can find a proper way to do it.

Yes, I just copied that from mips. I can look into some more.

Quoted 12 lines

arch/arm64/kernel/efi-stub.c line 49

Quoted 10 lines
arch/arm64/kernel/efi-stub.c new file +4 -0
new file mode 100644
index 0000000..10d02bf
+
+/* Include shared EFI stub code */
+#include "../../../drivers/firmware/efi/efi-stub-helper.c"
+#include "../../../drivers/firmware/efi/fdt.c"

It gets worse here.

Well, I'm open to suggestions here. The shared code has to work with stubs which are built into kernel (arm64) and which are part of the compressed image wrapper.

#10 lore raw

Re: [PATCH 4/6] arm64: add EFI stub

Quoted 67 lines · Mark Salter
Quoted 8 lines · Arnd Bergmann
Quoted 7 lines · Mark Salter

This patch adds PE/COFF header fields to the start of the Image so that it appears as an EFI application to EFI firmware. An EFI stub is included to allow direct booting of the kernel Image. Due to EFI firmware limitations, only little endian kernels with 4K page sizes are supported at this time. Support in the COFF header for signed images was provided by Ard Biesheuvel.

Quoted 5 lines
Quoted 2 lines

You got the ordering of the S-o-b lines wrong. Since you send the patch, your name should come last.

Yes.

Quoted 15 lines

arch/arm64/Kconfig config CMDLINE_FORCE · line 262

Quoted 12 lines
arch/arm64/Kconfig +10 -0
+config EFI_STUB
+     bool "EFI stub support"
+     depends on !ARM64_64K_PAGES && OF
+     select LIBFDT
+     default y
+     help
+       This kernel feature allows an Image to be loaded directly
+       by EFI firmware without the use of a bootloader.
+       See Documentation/efi-stub.txt for more information.
+
 endmenu

Why not ARM64_64K_PAGES? I thought that it was going to be the default for a lot of distros that would need to run on UEFI systems.

The UEFI spec currently mandates 4k page size. We may be able to work around that or get the spec changed, but for now it is just 4k pages.

Quoted 8 lines
Quoted 8 lines

menu "Userspace binary formats"

arch/arm64/kernel/Makefile +0 -0
index 5ba2fd4..1c52b84 100644
@@ -4,6 +4,8 @@
 
Quoted 7 lines

arch/arm64/kernel/Makefile line 8

Quoted 4 lines
arch/arm64/kernel/Makefile +2 -0
 CPPFLAGS_vmlinux.lds := -DTEXT_OFFSET=$(TEXT_OFFSET)
 AFLAGS_head.o                := -DTEXT_OFFSET=$(TEXT_OFFSET)
+CFLAGS_efi-stub.o    := -DTEXT_OFFSET=$(TEXT_OFFSET) \
+                        -I$(src)/../../../scripts/dtc/libfdt

Hmm, this is pretty ugly. I notice the same has been done on MIPS as well, but I'd hope we can find a proper way to do it.

Yes, I just copied that from mips. I can look into some more.

Quoted 12 lines

arch/arm64/kernel/efi-stub.c line 49

Quoted 10 lines
arch/arm64/kernel/efi-stub.c new file +4 -0
new file mode 100644
index 0000000..10d02bf
+
+/* Include shared EFI stub code */
+#include "../../../drivers/firmware/efi/efi-stub-helper.c"
+#include "../../../drivers/firmware/efi/fdt.c"

It gets worse here.

Quoted 4 lines

Well, I'm open to suggestions here. The shared code has to work with stubs which are built into kernel (arm64) and which are part of the compressed image wrapper.

The decompression algorithms are shared among the compressed image wrappers using #include of C code, so this is
following that convention. The only other mechanism that I saw for sharing code with the compressed image wrappers was
copying of files, as is done with the FDT files for the ARM compressed wrapper. I agree that neither of these is elegant, but whatever we do needs to work for compiling into the decompression wrapper (arm) and the kernel proper (arm64).

Roy

maintainer
#5 lore raw

Re: [PATCH 2/6] arm64: Add function to create identity mappings

In reply to Mark Salter

arch/arm64/mm/mmu.c static void __init create_mapping(phys_addr_t phys, unsigned long virt, · line 265

Quoted 8 lines · Mark Salter
arch/arm64/mm/mmu.c +8 -0
+void __init create_id_mapping(phys_addr_t addr, phys_addr_t size)
+{
+	pgd_t *pgd = &idmap_pg_dir[pgd_index(addr)];
+
+	if (pgd >= &idmap_pg_dir[ARRAY_SIZE(idmap_pg_dir)]) {
+		pr_warn("BUG: not creating id mapping for 0x%016llx\n", addr);
+		return;
+	}

The condition above is always false since pgd_index() already ands the index with (PTRS_PER_PGD - 1). Better check addr against something like (PTRS_PER_PGD * PGDIR_SIZE) (for clarity, you could do other shifts, doesn't really matter).

Signature · 1 line
Catalin
maintainer
#11 lore raw

Re: [PATCH 4/6] arm64: add EFI stub

In reply to Mark Salter
Quoted 4 lines · Mark Salter
arch/arm64/kernel/efi-entry.S new file +4 -0
+ENTRY(efi_stub_entry)
+       stp     x29, x30, [sp, #-32]!
+
+       /*

arch/arm64/kernel/efi-entry.S line 44

Quoted 10 lines
arch/arm64/kernel/efi-entry.S new file +10 -0
+        * Call efi_entry to do the real work.
+        * x0 and x1 are already set up by firmware. Current runtime
+        * address of image is calculated and passed via *image_addr.
+        *
+        * unsigned long efi_entry(void *handle,
+        *                         efi_system_table_t *sys_table,
+        *                         unsigned long *image_addr) ;
+        */
+       adrp    x8, _text
+        add    x8, x8, #:lo12:_text

Possibly some minor whitespace corruption (or it's our email server).

Signature · 1 line
Catalin
maintainer
#14 lore raw

Re: [PATCH 6/6] arm64: add EFI runtime services

In reply to Mark Salter

arch/arm64/include/asm/efi.h line 2

Quoted 8 lines · Mark Salter
arch/arm64/include/asm/efi.h new file +2 -0
new file mode 100644
index 0000000..a835b2c
@@ -0,0 +1,12 @@
+#ifndef _ASM_ARM64_EFI_H
+#define _ASM_ARM64_EFI_H

__ASM_EFI_H please for consistency with the other files.

Quoted 23 lines
arch/arm64/kernel/efi.c new file +17 -0
new file mode 100644
index 0000000..a42dc38
@@ -0,0 +1,353 @@
+/*
+ * Extensible Firmware Interface
+ *
+ * Based on Extensible Firmware Interface Specification version 2.4
+ *
+ * Copyright (C) 2013 Linaro Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+
+#include <linux/efi.h>
+#include <linux/export.h>
+#include <linux/memblock.h>
+#include <linux/of.h>

arch/arm64/kernel/efi.c line 27

Quoted 10 lines
arch/arm64/kernel/efi.c new file +10 -0
+#include <linux/of_fdt.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+
+#include <asm/cacheflush.h>
+#include <asm/efi.h>
+#include <asm/tlbflush.h>
+#include <asm/mmu_context.h>
+
+struct efi_memory_map memmap;

Is this variable used outside this file or from common code? It has a too generic name, may clash with other things.

arch/arm64/kernel/efi.c line 33

Quoted 1 line
arch/arm64/kernel/efi.c new file +1 -0
+static unsigned long arm_efi_facility;

You could drop the "arm_" prefix here, that's just local to this file.

Quoted 21 lines
arch/arm64/kernel/setup.c +2 -0
index 5516f54..7a45095 100644
@@ -41,6 +41,7 @@
 #include <linux/memblock.h>
 #include <linux/of_fdt.h>
 #include <linux/of_platform.h>
+#include <linux/efi.h>
 
 #include <asm/fixmap.h>
 #include <asm/cputype.h>
@@ -55,6 +56,7 @@
 #include <asm/traps.h>
 #include <asm/memblock.h>
 #include <asm/psci.h>
+#include <asm/efi.h>
 
 unsigned int processor_id;
 EXPORT_SYMBOL(processor_id);

arch/arm64/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 239

Quoted 9 lines
arch/arm64/kernel/setup.c +3 -0
@@ -229,9 +231,13 @@ void __init setup_arch(char **cmdline_p)
 
        arm64_memblock_init();
 
+       efi_init();
        paging_init();
        request_standard_resources();
 
+       if (efi_enabled(EFI_BOOT))
+               efi_enter_virtual_mode();

The common start_kernel() function already has this call:

#ifdef CONFIG_X86
	if (efi_enabled(EFI_RUNTIME_SERVICES))
		efi_enter_virtual_mode();

#endif

Could we not use it for arm64 as well? Ideally with an empty efi_enter_virtual_mode() in include/linux/efi.h if !CONFIG_EFI but I noticed that ia64 does a separate call and they don't seem to have efi_enabled(), so probably not feasible for them.

--
Catalin