Skip to contentSkip to conversation

efi_loader: Implement FileLoad2 for initramfs loading

PATCH New Ilias Apalodimas 2 participants 6 messages Feb 21, 2020 – Mar 12, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 efi_loader: Implement FileLoad2 for initramfs loading

Following kernel's proposal for an arch-agnostic initrd loading mechanism [1] let's implement the U-boot counterpart.
This new approach has a number of advantages compared to what we did up to now. The file is loaded into memory only when requested limiting the area of TOCTOU attacks. Users will be allowed to place the initramfs file on any u-boot accessible partition instead of just the ESP one. Finally this is an attempt of a generic interface across architectures in the linux kernel so it makes sense to support that.

The file location is intentionally only supported as a config option argument(CONFIG_EFI_INITRD_FILESPEC), in an effort to enhance security. Although U-boot is not responsible for verifying the integrity of the initramfs, we can enhance the offered security by only accepting a built-in option, which will be naturally verified by UEFI Secure Boot. This can easily change in the future if needed and configure that via ENV or UEFI variable.

[1] https://lore.kernel.org/linux-efi/20200207202637.GA3464906 at rani.riverdale.lan/T/#m4a25eb33112fab7a22faa0fd65d4d663209af32f

---
 cmd/efidebug.c                   |   4 +
 include/efi_api.h                |  17 +++
 include/efi_load_initrd.h        |  25 ++++
 include/efi_loader.h             |   1 +
 lib/efi_loader/Kconfig           |  15 +++
 lib/efi_loader/Makefile          |   1 +
 lib/efi_loader/efi_load_initrd.c | 198 +++++++++++++++++++++++++++++++
 lib/efi_loader/efi_setup.c       |   5 +
 8 files changed, 266 insertions(+)
 create mode 100644 include/efi_load_initrd.h
 create mode 100644 lib/efi_loader/efi_load_initrd.c
cmd/efidebug.c +4 -0
index 576e95b395dc..f450518c9e83 100644
@@ -264,6 +264,10 @@ static const struct {
 		"SMBIOS table",
 		SMBIOS_TABLE_GUID,
 	},
+	{
+		"Load File2",
+		EFI_LOAD_FILE2_PROTOCOL_GUID,
+	},
 };
 
 /**
include/efi_api.h +17 -0
index 22396172e15f..8ffed1334b84 100644
@@ -319,6 +319,14 @@ struct efi_runtime_services {
 	EFI_GUID(0xeb9d2d31, 0x2d88, 0x11d3,  \
 		 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
 
+#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)
+
 struct efi_configuration_table {
 	efi_guid_t guid;
 	void *table;
@@ -474,6 +482,7 @@ struct efi_device_path_nvme {
 #define DEVICE_PATH_TYPE_MEDIA_DEVICE		0x04
 #  define DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH	0x01
 #  define DEVICE_PATH_SUB_TYPE_CDROM_PATH	0x02
+#  define DEVICE_PATH_SUB_TYPE_VENDOR_PATH	0x03
 #  define DEVICE_PATH_SUB_TYPE_FILE_PATH	0x04
 
 struct efi_device_path_hard_drive_path {
@@ -1607,6 +1616,14 @@ struct efi_unicode_collation_protocol {
 	char *supported_languages;
 };
 
+struct efi_load_file_protocol {
+	efi_status_t (EFIAPI *load_file)(struct efi_load_file_protocol *this,
+					 struct efi_device_path *file_path,
+					 bool boot_policy,
+					 efi_uintn_t *buffer_size,
+					 void *buffer);
+};
+
 /* Boot manager load options */
 #define LOAD_OPTION_ACTIVE		0x00000001
 #define LOAD_OPTION_FORCE_RECONNECT	0x00000002
include/efi_load_initrd.h new file +25 -0
new file mode 100644
index 000000000000..478ae807c685
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2020, Linaro Limited
+ */
+
+#if !defined _EFI_LOAD_INITRD_H_
+#define _EFI_LOAD_INITRD_H_
+
+#include <efi.h>
+#include <efi_api.h>
+
+/*
+ * Vendor GUID used by Linux to identify the handle with the
+ * EFI_LOAD_FILE2_PROTOCOL and load an initial ramdisk.
+ */
+#define EFI_INITRD_MEDIA_GUID \
+	EFI_GUID(0x5568e427, 0x68fc, 0x4f3d, \
+		 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68)
+
+struct efi_initrd_dp {
+	struct efi_device_path_vendor vendor;
+	struct efi_device_path end;
+} __packed;
+
+#endif
include/efi_loader.h +1 -0
index d4c59b54c48b..8e3437983391 100644
@@ -378,6 +378,7 @@ efi_status_t efi_gop_register(void);
 efi_status_t efi_net_register(void);
 /* Called by bootefi to make the watchdog available */
 efi_status_t efi_watchdog_register(void);
+efi_status_t efi_initrd_register(void);
 /* Called by bootefi to make SMBIOS tables available */
 /**
  * efi_acpi_register() - write out ACPI tables
lib/efi_loader/Kconfig +15 -0
index a7afa3f29e88..50d4f81e6207 100644
@@ -130,4 +130,19 @@ config EFI_RNG_PROTOCOL
 	  "Support for EFI_RNG_PROTOCOL implementation. Uses the rng
 	   device on the platform"
 
+config EFI_LOAD_FILE2_INITRD
+	bool "EFI_FILE_LOAD2_PROTOCOL for Linux initial ramdisk"
+	default n
+	help
+	  Expose a EFI_FILE_LOAD2_PROTOCOL that the Linux UEFI stub can
+	  use to load the initial ramdisk. Once this is enabled using
+	  initrd=<ramdisk> will stop working.
+
+config EFI_INITRD_FILESPEC
+	string "initramfs path"
+	default "host 0:1 initrd"
+	depends on EFI_LOAD_FILE2_INITRD
+	help
+	  Full path of the initramfs file, e.g. mmc 0:2 initramfs.cpio.gz.
+
 endif
lib/efi_loader/Makefile +1 -0
index 04dc8648512b..9b3b70447336 100644
@@ -43,3 +43,4 @@ obj-$(CONFIG_NET) += efi_net.o
 obj-$(CONFIG_GENERATE_ACPI_TABLE) += efi_acpi.o
 obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
 obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o
+obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
lib/efi_loader/efi_load_initrd.c new file +198 -0

198 lines not on this page · raw message

lib/efi_loader/efi_setup.c +5 -0
index de7b616c6daa..f03685b11559 100644
@@ -155,6 +155,11 @@ efi_status_t efi_init_obj_list(void)
 	if (ret != EFI_SUCCESS)
 		goto out;
 #endif
+#ifdef CONFIG_EFI_LOAD_FILE2_INITRD
+	ret = efi_initrd_register();
+	if (ret != EFI_SUCCESS)
+		goto out;
+#endif
 #ifdef CONFIG_NET
 	ret = efi_net_register();
 	if (ret != EFI_SUCCESS)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 efi_selftest: add selftests for loadfile2 used to load initramfs

Provide a unit test loading an initial ramdisk using the EFI_LOAD_FILE2_PROTOCOL. The test is only executed on request.

An example usage - given a file image with a file system in partition 1 holding file initrd - is:

* Configure the sandbox with

CONFIG_EFI_SELFTEST=y
CONFIG_EFI_LOAD_FILE2_INITRD=y
CONFIG_EFI_INITRD_FILESPEC="host 0:1 initrd"

* Run ./u-boot and execute

host bind 0 image
setenv efi_selftest load initrd
bootefi selftest

This would provide a test output like:

Testing EFI API implementation

Selected test: 'load initrd'

Setting up 'load initrd'
Setting up 'load initrd' succeeded

Executing 'load initrd'
Loaded 12378613 bytes
CRC32 2997478465
Executing 'load initrd' succeeded

Now the size and CRC32 can be compared to the provided file.

---
 lib/efi_selftest/Makefile                   |   1 +
 lib/efi_selftest/efi_selftest_load_initrd.c | 220 ++++++++++++++++++++
 2 files changed, 221 insertions(+)
 create mode 100644 lib/efi_selftest/efi_selftest_load_initrd.c
lib/efi_selftest/Makefile +1 -0
index 3ad96e1cbf08..cf132c372e17 100644
@@ -49,6 +49,7 @@ obj-$(CONFIG_CPU_V7) += efi_selftest_unaligned.o
 obj-$(CONFIG_EFI_LOADER_HII) += efi_selftest_hii.o
 obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_selftest_rng.o
 obj-$(CONFIG_EFI_GET_TIME) += efi_selftest_rtc.o
+obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_selftest_load_initrd.o
 
 ifeq ($(CONFIG_GENERATE_ACPI_TABLE),)
 obj-y += efi_selftest_fdt.o
lib/efi_selftest/efi_selftest_load_initrd.c new file +220 -0

220 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

PATCH 1/2 efi_loader: Implement FileLoad2 for initramfs loading

In reply to Ilias Apalodimas

Ilias,

I haven't followed this thread.

Quoted 9 lines · Ilias Apalodimas

Following kernel's proposal for an arch-agnostic initrd loading mechanism [1] let's implement the U-boot counterpart.
This new approach has a number of advantages compared to what we did up to now. The file is loaded into memory only when requested limiting the area of TOCTOU attacks. Users will be allowed to place the initramfs file on any u-boot accessible partition instead of just the ESP one. Finally this is an attempt of a generic interface across architectures in the linux kernel so it makes sense to support that.

Quoted 7 lines

The file location is intentionally only supported as a config option argument(CONFIG_EFI_INITRD_FILESPEC), in an effort to enhance security. Although U-boot is not responsible for verifying the integrity of the initramfs, we can enhance the offered security by only accepting a built-in option, which will be naturally verified by UEFI Secure Boot. This can easily change in the future if needed and configure that via ENV or UEFI variable.

What if we have several bootable images with different initrd required at the same time? For example, we may install ubuntu 16.04, 18.04 etc to the same device (and then boot one of them via grub).

Since the guid is unique, U-Boot doesn't have any measure to identify which initrd be exported via FILE2_PROTOCOL.
In this sense, I don't think using a config option is a good idea in general.

Quoted 37 lines

[1] https://lore.kernel.org/linux-efi/20200207202637.GA3464906 at rani.riverdale.lan/T/#m4a25eb33112fab7a22faa0fd65d4d663209af32f

---
 cmd/efidebug.c                   |   4 +
 include/efi_api.h                |  17 +++
 include/efi_load_initrd.h        |  25 ++++
 include/efi_loader.h             |   1 +
 lib/efi_loader/Kconfig           |  15 +++
 lib/efi_loader/Makefile          |   1 +
 lib/efi_loader/efi_load_initrd.c | 198 +++++++++++++++++++++++++++++++
 lib/efi_loader/efi_setup.c       |   5 +
 8 files changed, 266 insertions(+)
 create mode 100644 include/efi_load_initrd.h
 create mode 100644 lib/efi_loader/efi_load_initrd.c
cmd/efidebug.c +4 -0
index 576e95b395dc..f450518c9e83 100644
@@ -264,6 +264,10 @@ static const struct {
 		"SMBIOS table",
 		SMBIOS_TABLE_GUID,
 	},
+	{
+		"Load File2",
+		EFI_LOAD_FILE2_PROTOCOL_GUID,
+	},
 };
 
 /**
include/efi_api.h +0 -0
index 22396172e15f..8ffed1334b84 100644

include/efi_api.h struct efi_runtime_services { · line 324

Quoted 6 lines
include/efi_api.h +3 -0
@@ -319,6 +319,14 @@ struct efi_runtime_services {
 	EFI_GUID(0xeb9d2d31, 0x2d88, 0x11d3,  \
 		 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d)
 
+#define EFI_LOAD_FILE_PROTOCOL_GUID \
+	EFI_GUID(0x56ec3091, 0x954c, 0x11d2, \
+		 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)

Do you need this definition?

include/efi_load_initrd.h

Quoted 35 lines
include/efi_api.h +13 -0
+#define EFI_LOAD_FILE2_PROTOCOL_GUID \
+	EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e, \
+		 0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)
+
 struct efi_configuration_table {
 	efi_guid_t guid;
 	void *table;
@@ -474,6 +482,7 @@ struct efi_device_path_nvme {
 #define DEVICE_PATH_TYPE_MEDIA_DEVICE		0x04
 #  define DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH	0x01
 #  define DEVICE_PATH_SUB_TYPE_CDROM_PATH	0x02
+#  define DEVICE_PATH_SUB_TYPE_VENDOR_PATH	0x03
 #  define DEVICE_PATH_SUB_TYPE_FILE_PATH	0x04
 
 struct efi_device_path_hard_drive_path {
@@ -1607,6 +1616,14 @@ struct efi_unicode_collation_protocol {
 	char *supported_languages;
 };
 
+struct efi_load_file_protocol {
+	efi_status_t (EFIAPI *load_file)(struct efi_load_file_protocol *this,
+					 struct efi_device_path *file_path,
+					 bool boot_policy,
+					 efi_uintn_t *buffer_size,
+					 void *buffer);
+};
+
 /* Boot manager load options */
 #define LOAD_OPTION_ACTIVE		0x00000001
 #define LOAD_OPTION_FORCE_RECONNECT	0x00000002
include/efi_load_initrd.h new file +0 -0
new file mode 100644
index 000000000000..478ae807c685

It might be unlikely, but the file name should be more generic for potential enhancement.

Quoted 47 lines
include/efi_load_initrd.h new file +25 -0
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2020, Linaro Limited
+ */
+
+#if !defined _EFI_LOAD_INITRD_H_
+#define _EFI_LOAD_INITRD_H_
+
+#include <efi.h>
+#include <efi_api.h>
+
+/*
+ * Vendor GUID used by Linux to identify the handle with the
+ * EFI_LOAD_FILE2_PROTOCOL and load an initial ramdisk.
+ */
+#define EFI_INITRD_MEDIA_GUID \
+	EFI_GUID(0x5568e427, 0x68fc, 0x4f3d, \
+		 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68)
+
+struct efi_initrd_dp {
+	struct efi_device_path_vendor vendor;
+	struct efi_device_path end;
+} __packed;
+
+#endif
include/efi_loader.h +1 -0
index d4c59b54c48b..8e3437983391 100644
@@ -378,6 +378,7 @@ efi_status_t efi_gop_register(void);
 efi_status_t efi_net_register(void);
 /* Called by bootefi to make the watchdog available */
 efi_status_t efi_watchdog_register(void);
+efi_status_t efi_initrd_register(void);
 /* Called by bootefi to make SMBIOS tables available */
 /**
  * efi_acpi_register() - write out ACPI tables
lib/efi_loader/Kconfig +1 -0
index a7afa3f29e88..50d4f81e6207 100644
@@ -130,4 +130,19 @@ config EFI_RNG_PROTOCOL
 	  "Support for EFI_RNG_PROTOCOL implementation. Uses the rng
 	   device on the platform"
 
+config EFI_LOAD_FILE2_INITRD

lib/efi_loader/Kconfig config EFI_RNG_PROTOCOL · line 143

Quoted 10 lines
lib/efi_loader/Kconfig +10 -0
+	bool "EFI_FILE_LOAD2_PROTOCOL for Linux initial ramdisk"
+	default n
+	help
+	  Expose a EFI_FILE_LOAD2_PROTOCOL that the Linux UEFI stub can
+	  use to load the initial ramdisk. Once this is enabled using
+	  initrd=<ramdisk> will stop working.
+
+config EFI_INITRD_FILESPEC
+	string "initramfs path"
+	default "host 0:1 initrd"

"host" interface makes sense only on sandbox, which will not be expected to be able to boot any linux kernel.
So this default value is inappropriate anyway.

lib/efi_loader/efi_load_initrd.c

Quoted 19 lines
lib/efi_loader/Kconfig +4 -0
+	depends on EFI_LOAD_FILE2_INITRD
+	help
+	  Full path of the initramfs file, e.g. mmc 0:2 initramfs.cpio.gz.
+
 endif
lib/efi_loader/Makefile +1 -0
index 04dc8648512b..9b3b70447336 100644
@@ -43,3 +43,4 @@ obj-$(CONFIG_NET) += efi_net.o
 obj-$(CONFIG_GENERATE_ACPI_TABLE) += efi_acpi.o
 obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
 obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o
+obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
lib/efi_loader/efi_load_initrd.c new file +0 -0
new file mode 100644
index 000000000000..574a83d7e308

I'd prefer efi_file_initrd.c for similarity to efi_file.c, or more specifically efi_linux.c.

lib/efi_loader/efi_load_initrd.c line 10

Quoted 11 lines
lib/efi_loader/efi_load_initrd.c new file +10 -0
@@ -0,0 +1,198 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2020, Linaro Limited
+ */
+
+#include <common.h>
+#include <env.h>
+#include <malloc.h>
+#include <mapmem.h>
+#include <dm.h>

Do you need env.h and dm.h?

Quoted 61 lines
lib/efi_loader/efi_load_initrd.c new file +61 -0
+#include <fs.h>
+#include <efi_loader.h>
+#include <efi_load_initrd.h>
+
+static const efi_guid_t efi_guid_load_file2_protocol =
+		EFI_LOAD_FILE2_PROTOCOL_GUID;
+
+static efi_status_t EFIAPI
+efi_load_file2_initrd(struct efi_load_file_protocol *this,
+		      struct efi_device_path *file_path, bool boot_policy,
+		      efi_uintn_t *buffer_size, void *buffer);
+
+static const struct efi_load_file_protocol efi_lf2_protocol = {
+	.load_file = efi_load_file2_initrd,
+};
+
+/*
+ * Device path defined by Linux to identify the handle providing the
+ * EFI_LOAD_FILE2_PROTOCOL used for loading the initial ramdisk.
+ */
+static const struct efi_initrd_dp dp = {
+	.vendor = {
+		{
+		   DEVICE_PATH_TYPE_MEDIA_DEVICE,
+		   DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
+		   sizeof(dp.vendor),
+		},
+		EFI_INITRD_MEDIA_GUID,
+	},
+	.end = {
+		DEVICE_PATH_TYPE_END,
+		DEVICE_PATH_SUB_TYPE_END,
+		sizeof(dp.end),
+	}
+};
+
+/**
+ * get_file_size() - retrieve the size of initramfs, set efi status on error
+ *
+ * @dev:			device to read from. i.e "mmc"
+ * @part:			device partition. i.e "0:1"
+ * @file:			name fo file
+ * @status:			EFI exit code in case of failure
+ *
+ * Return:			size of file
+ */
+static loff_t get_file_size(const char *dev, const char *part, const char *file,
+			    efi_status_t *status)
+{
+	loff_t sz = 0;
+	int ret;
+
+	ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
+	if (ret) {
+		*status = EFI_NO_MEDIA;
+		goto out;
+	}
+
+	ret = fs_size(file, &sz);
+	if (ret) {
+		sz = 0;

lib/efi_loader/efi_load_initrd.c line 81

Quoted 10 lines
lib/efi_loader/efi_load_initrd.c new file +10 -0
+		*status = EFI_NOT_FOUND;
+		goto out;
+	}
+
+out:
+	return sz;
+}
+
+/**
+ * load_file2() - get information about random number generation

Please correct the message.

Quoted 21 lines
lib/efi_loader/efi_load_initrd.c new file +21 -0
+ *
+ * This function implement the LoadFile2() service in order to load an initram
+ * disk requested by the Linux kernel stub.
+ * See the UEFI spec for details.
+ *
+ * @this:			loadfile2 protocol instance
+ * @file_path:			relative path of the file. "" in this case
+ * @boot_policy:		must be false for Loadfile2
+ * @buffer_size:		size of allocated buffer
+ * @buffer:			buffer to load the file
+ *
+ * Return:			status code
+ */
+static efi_status_t EFIAPI
+efi_load_file2_initrd(struct efi_load_file_protocol *this,
+		      struct efi_device_path *file_path, bool boot_policy,
+		      efi_uintn_t *buffer_size, void *buffer)
+{
+	const char *filespec = CONFIG_EFI_INITRD_FILESPEC;
+	efi_status_t status = EFI_NOT_FOUND;
+	loff_t file_sz = 0, read_sz = 0;

lib/efi_loader/efi_load_initrd.c line 112

Quoted 10 lines
lib/efi_loader/efi_load_initrd.c new file +10 -0
+	char *dev, *part, *file;
+	char *s;
+	int ret;
+
+	EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy,
+		  buffer_size, buffer);
+
+	s = strdup(filespec);
+	if (!s)
+		goto out;

I think that we should return a better error code rather than NOT_FOUND.

lib/efi_loader/efi_load_initrd.c line 114

Quoted 2 lines
lib/efi_loader/efi_load_initrd.c new file +2 -0
+
+	if (!this || this != &efi_lf2_protocol ||

Please use guidcmp() here.

Quoted 20 lines
lib/efi_loader/efi_load_initrd.c new file +20 -0
+	    !buffer_size) {
+		status = EFI_INVALID_PARAMETER;
+		goto out;
+	}
+
+	if (file_path->type != dp.end.type ||
+	    file_path->sub_type != dp.end.sub_type) {
+		status = EFI_INVALID_PARAMETER;
+		goto out;
+	}
+
+	if (boot_policy) {
+		status = EFI_UNSUPPORTED;
+		goto out;
+	}
+
+	/* expect something like 'mmc 0:1 initrd.cpio.gz' */
+	dev = strsep(&s, " ");
+	if (!dev)
+		goto out;

lib/efi_loader/efi_load_initrd.c line 144

Quoted 10 lines
lib/efi_loader/efi_load_initrd.c new file +10 -0
+	part = strsep(&s, " ");
+	if (!part)
+		goto out;
+	file = strsep(&s, " ");
+	if (!file)
+		goto out;
+
+	file_sz = get_file_size(dev, part, file, &status);
+	if (!file_sz)
+		goto out;

ditto.

Quoted 73 lines
lib/efi_loader/efi_load_initrd.c new file +54 -0
+
+	if (!buffer || *buffer_size < file_sz) {
+		status = EFI_BUFFER_TOO_SMALL;
+		*buffer_size = file_sz;
+	} else {
+		ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
+		if (ret) {
+			status = EFI_NO_MEDIA;
+			goto out;
+		}
+
+		ret = fs_read(file, map_to_sysmem(buffer), 0, *buffer_size,
+			      &read_sz);
+		if (ret || read_sz != file_sz)
+			goto out;
+		*buffer_size = read_sz;
+
+		status = EFI_SUCCESS;
+	}
+
+out:
+	free(s);
+	return EFI_EXIT(status);
+}
+
+/**
+ * efi_initrd_register() - Register a handle and loadfile2 protocol
+ *
+ * This function creates a new handle and installs a linux specific GUID
+ * to handle initram disk loading during boot.
+ * See the UEFI spec for details.
+ *
+ * Return:			status code
+ */
+efi_status_t efi_initrd_register(void)
+{
+	efi_handle_t efi_initrd_handle = NULL;
+	efi_status_t ret;
+
+	/*
+	 * Set up the handle with the EFI_LOAD_FILE2_PROTOCOL which Linux may
+	 * use to load the initial ramdisk.
+	 */
+	ret = EFI_CALL(efi_install_multiple_protocol_interfaces
+		       (&efi_initrd_handle,
+			/* initramfs */
+			&efi_guid_device_path, &dp,
+			/* LOAD_FILE2 */
+			&efi_guid_load_file2_protocol,
+			(void *)&efi_lf2_protocol,
+			NULL));
+
+	return ret;
+}
lib/efi_loader/efi_setup.c +5 -0
index de7b616c6daa..f03685b11559 100644
@@ -155,6 +155,11 @@ efi_status_t efi_init_obj_list(void)
 	if (ret != EFI_SUCCESS)
 		goto out;
 #endif
+#ifdef CONFIG_EFI_LOAD_FILE2_INITRD
+	ret = efi_initrd_register();
+	if (ret != EFI_SUCCESS)
+		goto out;
+#endif
 #ifdef CONFIG_NET
 	ret = efi_net_register();
 	if (ret != EFI_SUCCESS)
Signature · 1 line
2.25.1
#3 lore raw

PATCH 1/2 efi_loader: Implement FileLoad2 for initramfs loading

Akashi-san,

Quoted 22 lines · AKASHI Takahiro

Ilias,

I haven't followed this thread.

Quoted 9 lines · Ilias Apalodimas

Following kernel's proposal for an arch-agnostic initrd loading mechanism [1] let's implement the U-boot counterpart.
This new approach has a number of advantages compared to what we did up to now. The file is loaded into memory only when requested limiting the area of TOCTOU attacks. Users will be allowed to place the initramfs file on any u-boot accessible partition instead of just the ESP one. Finally this is an attempt of a generic interface across architectures in the linux kernel so it makes sense to support that.

Quoted 7 lines

The file location is intentionally only supported as a config option argument(CONFIG_EFI_INITRD_FILESPEC), in an effort to enhance security. Although U-boot is not responsible for verifying the integrity of the initramfs, we can enhance the offered security by only accepting a built-in option, which will be naturally verified by UEFI Secure Boot. This can easily change in the future if needed and configure that via ENV or UEFI variable.

Quoted 7 lines

What if we have several bootable images with different initrd required at the same time? For example, we may install ubuntu 16.04, 18.04 etc to the same device (and then boot one of them via grub).

Since the guid is unique, U-Boot doesn't have any measure to identify which initrd be exported via FILE2_PROTOCOL.
In this sense, I don't think using a config option is a good idea in general.

Can't you concatenate multiple cpio archives for this? I haven't checked this personally but i remember distros doing it to add firmware files they need for example. It might not be ideal because you may need different user-space program versions or different versions of the same script(??) but the linux module loading part, of different kernel revisions, should be there.

I don't have any strong preference on this anyway hence my commit message, it's just another naive protection, since the user can always replace the initramfs using the same name and break out of this. I never needed multiple initrd files so I preferred the config option. If people need a config option it's easily extensible.

Quoted 1 line
Quoted 1 line

+#define EFI_LOAD_FILE_PROTOCOL_GUID \

[...]

Quoted 5 lines

include/efi_api.h struct efi_runtime_services { · line 324

Quoted 2 lines
include/efi_api.h +2 -0
+	EFI_GUID(0x56ec3091, 0x954c, 0x11d2, \
+		 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)

Do you need this definition?

Not really it just felt weird adding LOAD_FILE2 without the LOAD_FILE definition.

Quoted 5 lines
Quoted 5 lines
include/efi_api.h +4 -1
+#define EFI_LOAD_FILE2_PROTOCOL_GUID \
+	EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e, \
+		 0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)
+
--- /dev/null

[...]

Quoted 5 lines
Quoted 1 line

+++ b/include/efi_load_initrd.h

It might be unlikely, but the file name should be more generic for potential enhancement.

This is defining a very specific GUID for initramfs that Linux consumes to load the file, I'd rather keep it as is.

Quoted 1 line
Quoted 1 line
(quoted diff) +0 -0
@@ -0,0 +1,25 @@

[...]

Quoted 9 lines

lib/efi_loader/Kconfig config EFI_RNG_PROTOCOL · line 143

Quoted 4 lines
lib/efi_loader/Kconfig +4 -0
+
+config EFI_INITRD_FILESPEC
+	string "initramfs path"
+	default "host 0:1 initrd"

"host" interface makes sense only on sandbox, which will not be expected to be able to boot any linux kernel.
So this default value is inappropriate anyway.

Not really. The user *must* have an idea on what he is doing and this is the default config we need to run on the sandbox. So it serves both as an example and a defult config for the sandbox testing.

Quoted 14 lines
Quoted 14 lines
lib/efi_loader/Kconfig +4 -0
+	depends on EFI_LOAD_FILE2_INITRD
+	help
+	  Full path of the initramfs file, e.g. mmc 0:2 initramfs.cpio.gz.
+
 endif
lib/efi_loader/Makefile +1 -0
index 04dc8648512b..9b3b70447336 100644
@@ -43,3 +43,4 @@ obj-$(CONFIG_NET) += efi_net.o
 obj-$(CONFIG_GENERATE_ACPI_TABLE) += efi_acpi.o
 obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
 obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o
+obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
Quoted 9 lines

lib/efi_loader/efi_load_initrd.c

Quoted 5 lines
lib/efi_loader/efi_load_initrd.c new file +0 -0
new file mode 100644
index 000000000000..574a83d7e308

I'd prefer efi_file_initrd.c for similarity to efi_file.c, or more specifically efi_linux.c.

I can send a patch renaming those but i think it's pointless. The filename describes exactly what's the intent imho.

Quoted 5 lines
Quoted 5 lines
lib/efi_loader/efi_load_initrd.c new file +4 -0
@@ -0,0 +1,198 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2020, Linaro Limited
+ */
Quoted 8 lines

lib/efi_loader/efi_load_initrd.c line 10

Quoted 6 lines
lib/efi_loader/efi_load_initrd.c new file +6 -0
+
+#include <common.h>
+#include <env.h>
+#include <malloc.h>
+#include <mapmem.h>
+#include <dm.h>

Do you need env.h and dm.h?

No, I'll remove those.

Quoted 66 lines
Quoted 66 lines
lib/efi_loader/efi_load_initrd.c new file +65 -0
+#include <fs.h>
+#include <efi_loader.h>
+#include <efi_load_initrd.h>
+
+static const efi_guid_t efi_guid_load_file2_protocol =
+		EFI_LOAD_FILE2_PROTOCOL_GUID;
+
+static efi_status_t EFIAPI
+efi_load_file2_initrd(struct efi_load_file_protocol *this,
+		      struct efi_device_path *file_path, bool boot_policy,
+		      efi_uintn_t *buffer_size, void *buffer);
+
+static const struct efi_load_file_protocol efi_lf2_protocol = {
+	.load_file = efi_load_file2_initrd,
+};
+
+/*
+ * Device path defined by Linux to identify the handle providing the
+ * EFI_LOAD_FILE2_PROTOCOL used for loading the initial ramdisk.
+ */
+static const struct efi_initrd_dp dp = {
+	.vendor = {
+		{
+		   DEVICE_PATH_TYPE_MEDIA_DEVICE,
+		   DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
+		   sizeof(dp.vendor),
+		},
+		EFI_INITRD_MEDIA_GUID,
+	},
+	.end = {
+		DEVICE_PATH_TYPE_END,
+		DEVICE_PATH_SUB_TYPE_END,
+		sizeof(dp.end),
+	}
+};
+
+/**
+ * get_file_size() - retrieve the size of initramfs, set efi status on error
+ *
+ * @dev:			device to read from. i.e "mmc"
+ * @part:			device partition. i.e "0:1"
+ * @file:			name fo file
+ * @status:			EFI exit code in case of failure
+ *
+ * Return:			size of file
+ */
+static loff_t get_file_size(const char *dev, const char *part, const char *file,
+			    efi_status_t *status)
+{
+	loff_t sz = 0;
+	int ret;
+
+	ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
+	if (ret) {
+		*status = EFI_NO_MEDIA;
+		goto out;
+	}
+
+	ret = fs_size(file, &sz);
+	if (ret) {
+		sz = 0;
+		*status = EFI_NOT_FOUND;
+		goto out;
+	}
+
Quoted 9 lines

lib/efi_loader/efi_load_initrd.c line 81

Quoted 6 lines
lib/efi_loader/efi_load_initrd.c new file +6 -0
+out:
+	return sz;
+}
+
+/**
+ * load_file2() - get information about random number generation

Please correct the message.

Ooops

Quoted 25 lines
Quoted 25 lines
lib/efi_loader/efi_load_initrd.c new file +25 -0
+ *
+ * This function implement the LoadFile2() service in order to load an initram
+ * disk requested by the Linux kernel stub.
+ * See the UEFI spec for details.
+ *
+ * @this:			loadfile2 protocol instance
+ * @file_path:			relative path of the file. "" in this case
+ * @boot_policy:		must be false for Loadfile2
+ * @buffer_size:		size of allocated buffer
+ * @buffer:			buffer to load the file
+ *
+ * Return:			status code
+ */
+static efi_status_t EFIAPI
+efi_load_file2_initrd(struct efi_load_file_protocol *this,
+		      struct efi_device_path *file_path, bool boot_policy,
+		      efi_uintn_t *buffer_size, void *buffer)
+{
+	const char *filespec = CONFIG_EFI_INITRD_FILESPEC;
+	efi_status_t status = EFI_NOT_FOUND;
+	loff_t file_sz = 0, read_sz = 0;
+	char *dev, *part, *file;
+	char *s;
+	int ret;
+
Quoted 9 lines

lib/efi_loader/efi_load_initrd.c line 112

Quoted 6 lines
lib/efi_loader/efi_load_initrd.c new file +6 -0
+	EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy,
+		  buffer_size, buffer);
+
+	s = strdup(filespec);
+	if (!s)
+		goto out;

I think that we should return a better error code rather than NOT_FOUND.

Like? The spec has a specific set of returns codes you can use there, EFI_UNSUPPORTED
EFI_INVALID_PARAMETER
EFI_NO_MEDIA
EFI_DEVICE_ERROR
EFI_NO_RESPONSE
EFI_NOT_FOUND
EFI_ABORTED
EFI_BUFFER_TOO_SMALL

Is there anything you like better?

Quoted 5 lines

lib/efi_loader/efi_load_initrd.c line 114

Quoted 2 lines
lib/efi_loader/efi_load_initrd.c new file +2 -0
+
+	if (!this || this != &efi_lf2_protocol ||

Please use guidcmp() here.

Sure

Quoted 2 lines
Quoted 2 lines
lib/efi_loader/efi_load_initrd.c new file +2 -0
+	    !buffer_size) {
+	if (!file_sz)

[...]

Quoted 4 lines

lib/efi_loader/efi_load_initrd.c

Quoted 1 line
lib/efi_loader/efi_load_initrd.c new file +1 -0
+		goto out;

ditto.

Sure

Quoted 2 lines
Quoted 2 lines
lib/efi_loader/efi_load_initrd.c new file +2 -0
+
+	if (!buffer || *buffer_size < file_sz) {

[...]

Quoted 4 lines
Quoted 4 lines

if (ret != EFI_SUCCESS)

Signature · 1 line
2.25.1

Regards
/Ilias

#4 lore raw

PATCH 1/2 efi_loader: Implement FileLoad2 for initramfs loading

Quoted 29 lines · Ilias Apalodimas

Akashi-san,

Quoted 26 lines · AKASHI Takahiro

Ilias,

I haven't followed this thread.

Quoted 9 lines · Ilias Apalodimas

Following kernel's proposal for an arch-agnostic initrd loading mechanism [1] let's implement the U-boot counterpart.
This new approach has a number of advantages compared to what we did up to now. The file is loaded into memory only when requested limiting the area of TOCTOU attacks. Users will be allowed to place the initramfs file on any u-boot accessible partition instead of just the ESP one. Finally this is an attempt of a generic interface across architectures in the linux kernel so it makes sense to support that.

Quoted 7 lines

The file location is intentionally only supported as a config option argument(CONFIG_EFI_INITRD_FILESPEC), in an effort to enhance security. Although U-boot is not responsible for verifying the integrity of the initramfs, we can enhance the offered security by only accepting a built-in option, which will be naturally verified by UEFI Secure Boot. This can easily change in the future if needed and configure that via ENV or UEFI variable.

What if we have several bootable images with different initrd required at the same time? For example, we may install ubuntu 16.04, 18.04 etc to the same device (and then boot one of them via grub).

Quoted 8 lines
Quoted 3 lines

Since the guid is unique, U-Boot doesn't have any measure to identify which initrd be exported via FILE2_PROTOCOL.
In this sense, I don't think using a config option is a good idea in general.

Can't you concatenate multiple cpio archives for this? I haven't checked this personally but i remember distros doing it to add firmware files they need for example. It might not be ideal because you may need different user-space program versions or different versions of the same script(??)

I wonder think why it is not a problem.

Quoted 2 lines

but the linux
module loading part, of different kernel revisions, should be there.

Can we assume that this is only the use of initrd?

Quoted 5 lines

I don't have any strong preference on this anyway hence my commit message, it's just another naive protection, since the user can always replace the initramfs using the same name and break out of this. I never needed multiple initrd files so I preferred the config option. If people need a config option it's easily extensible.

How? I don't think it's trivial.

Quoted 17 lines
Quoted 1 line
Quoted 1 line

+#define EFI_LOAD_FILE_PROTOCOL_GUID \

[...]

Quoted 5 lines

include/efi_api.h struct efi_runtime_services { · line 324

Quoted 2 lines
include/efi_api.h +2 -0
+	EFI_GUID(0x56ec3091, 0x954c, 0x11d2, \
+		 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)

Do you need this definition?

Not really it just felt weird adding LOAD_FILE2 without the LOAD_FILE definition.

Quoted 5 lines
Quoted 5 lines
include/efi_api.h +4 -1
+#define EFI_LOAD_FILE2_PROTOCOL_GUID \
+	EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e, \
+		 0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)
+
--- /dev/null

[...]

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

+++ b/include/efi_load_initrd.h

It might be unlikely, but the file name should be more generic for potential enhancement.

This is defining a very specific GUID for initramfs that Linux consumes to load the file, I'd rather keep it as is.

I didn't intent the guid, but a file name.

Quoted 12 lines
Quoted 1 line
Quoted 1 line
(quoted diff) +0 -0
@@ -0,0 +1,25 @@

[...]

Quoted 9 lines

lib/efi_loader/Kconfig config EFI_RNG_PROTOCOL · line 143

Quoted 4 lines
lib/efi_loader/Kconfig +4 -0
+
+config EFI_INITRD_FILESPEC
+	string "initramfs path"
+	default "host 0:1 initrd"

"host" interface makes sense only on sandbox, which will not be expected to be able to boot any linux kernel.
So this default value is inappropriate anyway.

Quoted 3 lines

Not really. The user *must* have an idea on what he is doing and this is the default config we need to run on the sandbox. So it serves both as an example and a defult config for the sandbox testing.

I don't think we can boot a linux kernel from U-Boot on sandbox.

Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
lib/efi_loader/Kconfig +4 -0
+	depends on EFI_LOAD_FILE2_INITRD
+	help
+	  Full path of the initramfs file, e.g. mmc 0:2 initramfs.cpio.gz.
+
 endif
lib/efi_loader/Makefile +1 -0
index 04dc8648512b..9b3b70447336 100644
@@ -43,3 +43,4 @@ obj-$(CONFIG_NET) += efi_net.o
 obj-$(CONFIG_GENERATE_ACPI_TABLE) += efi_acpi.o
 obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
 obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o
+obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
Quoted 9 lines

lib/efi_loader/efi_load_initrd.c

Quoted 5 lines
lib/efi_loader/efi_load_initrd.c new file +0 -0
new file mode 100644
index 000000000000..574a83d7e308

I'd prefer efi_file_initrd.c for similarity to efi_file.c, or more specifically efi_linux.c.

Quoted 2 lines

I can send a patch renaming those but i think it's pointless. The filename describes exactly what's the intent imho.

What if we will want to add another FILE2_PROTOCOL or linux-specifc hack?
Now I believe that OS (or platform)-dependent implementation should go into a separate directory, like lib/efi_linux/... for clarification.

Quoted 139 lines
Quoted 5 lines
Quoted 5 lines
lib/efi_loader/efi_load_initrd.c new file +4 -0
@@ -0,0 +1,198 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2020, Linaro Limited
+ */
Quoted 8 lines

lib/efi_loader/efi_load_initrd.c line 10

Quoted 6 lines
lib/efi_loader/efi_load_initrd.c new file +6 -0
+
+#include <common.h>
+#include <env.h>
+#include <malloc.h>
+#include <mapmem.h>
+#include <dm.h>

Do you need env.h and dm.h?

No, I'll remove those.

Quoted 66 lines
Quoted 66 lines
lib/efi_loader/efi_load_initrd.c new file +65 -0
+#include <fs.h>
+#include <efi_loader.h>
+#include <efi_load_initrd.h>
+
+static const efi_guid_t efi_guid_load_file2_protocol =
+		EFI_LOAD_FILE2_PROTOCOL_GUID;
+
+static efi_status_t EFIAPI
+efi_load_file2_initrd(struct efi_load_file_protocol *this,
+		      struct efi_device_path *file_path, bool boot_policy,
+		      efi_uintn_t *buffer_size, void *buffer);
+
+static const struct efi_load_file_protocol efi_lf2_protocol = {
+	.load_file = efi_load_file2_initrd,
+};
+
+/*
+ * Device path defined by Linux to identify the handle providing the
+ * EFI_LOAD_FILE2_PROTOCOL used for loading the initial ramdisk.
+ */
+static const struct efi_initrd_dp dp = {
+	.vendor = {
+		{
+		   DEVICE_PATH_TYPE_MEDIA_DEVICE,
+		   DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
+		   sizeof(dp.vendor),
+		},
+		EFI_INITRD_MEDIA_GUID,
+	},
+	.end = {
+		DEVICE_PATH_TYPE_END,
+		DEVICE_PATH_SUB_TYPE_END,
+		sizeof(dp.end),
+	}
+};
+
+/**
+ * get_file_size() - retrieve the size of initramfs, set efi status on error
+ *
+ * @dev:			device to read from. i.e "mmc"
+ * @part:			device partition. i.e "0:1"
+ * @file:			name fo file
+ * @status:			EFI exit code in case of failure
+ *
+ * Return:			size of file
+ */
+static loff_t get_file_size(const char *dev, const char *part, const char *file,
+			    efi_status_t *status)
+{
+	loff_t sz = 0;
+	int ret;
+
+	ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);
+	if (ret) {
+		*status = EFI_NO_MEDIA;
+		goto out;
+	}
+
+	ret = fs_size(file, &sz);
+	if (ret) {
+		sz = 0;
+		*status = EFI_NOT_FOUND;
+		goto out;
+	}
+
Quoted 9 lines

lib/efi_loader/efi_load_initrd.c line 81

Quoted 6 lines
lib/efi_loader/efi_load_initrd.c new file +6 -0
+out:
+	return sz;
+}
+
+/**
+ * load_file2() - get information about random number generation

Please correct the message.

Ooops

Quoted 25 lines
Quoted 25 lines
lib/efi_loader/efi_load_initrd.c new file +25 -0
+ *
+ * This function implement the LoadFile2() service in order to load an initram
+ * disk requested by the Linux kernel stub.
+ * See the UEFI spec for details.
+ *
+ * @this:			loadfile2 protocol instance
+ * @file_path:			relative path of the file. "" in this case
+ * @boot_policy:		must be false for Loadfile2
+ * @buffer_size:		size of allocated buffer
+ * @buffer:			buffer to load the file
+ *
+ * Return:			status code
+ */
+static efi_status_t EFIAPI
+efi_load_file2_initrd(struct efi_load_file_protocol *this,
+		      struct efi_device_path *file_path, bool boot_policy,
+		      efi_uintn_t *buffer_size, void *buffer)
+{
+	const char *filespec = CONFIG_EFI_INITRD_FILESPEC;
+	efi_status_t status = EFI_NOT_FOUND;
+	loff_t file_sz = 0, read_sz = 0;
+	char *dev, *part, *file;
+	char *s;
+	int ret;
+
Quoted 9 lines

lib/efi_loader/efi_load_initrd.c line 112

Quoted 6 lines
lib/efi_loader/efi_load_initrd.c new file +6 -0
+	EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy,
+		  buffer_size, buffer);
+
+	s = strdup(filespec);
+	if (!s)
+		goto out;

I think that we should return a better error code rather than NOT_FOUND.

Like? The spec has a specific set of returns codes you can use there, EFI_UNSUPPORTED
EFI_INVALID_PARAMETER
EFI_NO_MEDIA
EFI_DEVICE_ERROR
EFI_NO_RESPONSE
EFI_NOT_FOUND
EFI_ABORTED
EFI_BUFFER_TOO_SMALL

Quoted 1 line

Is there anything you like better?

In most of all cases, OUT_OF_RESOURCES.

Thanks,
-Takahiro Akashi

Quoted 29 lines
Quoted 5 lines

lib/efi_loader/efi_load_initrd.c line 114

Quoted 2 lines
lib/efi_loader/efi_load_initrd.c new file +2 -0
+
+	if (!this || this != &efi_lf2_protocol ||

Please use guidcmp() here.

Sure

Quoted 2 lines
Quoted 2 lines
lib/efi_loader/efi_load_initrd.c new file +2 -0
+	    !buffer_size) {
+	if (!file_sz)

[...]

Quoted 4 lines

lib/efi_loader/efi_load_initrd.c

Quoted 1 line
lib/efi_loader/efi_load_initrd.c new file +1 -0
+		goto out;

ditto.

Sure

Quoted 2 lines
Quoted 2 lines
lib/efi_loader/efi_load_initrd.c new file +2 -0
+
+	if (!buffer || *buffer_size < file_sz) {

[...]

Quoted 4 lines
Quoted 4 lines

if (ret != EFI_SUCCESS)

Signature · 1 line
2.25.1

Regards
/Ilias

#5 lore raw

PATCH 1/2 efi_loader: Implement FileLoad2 for initramfs loading

Quoted 34 lines · AKASHI Takahiro
Quoted 33 lines · Ilias Apalodimas

Akashi-san,

Quoted 29 lines · AKASHI Takahiro

Ilias,

I haven't followed this thread.

Quoted 9 lines · Ilias Apalodimas

Following kernel's proposal for an arch-agnostic initrd loading mechanism [1] let's implement the U-boot counterpart.
This new approach has a number of advantages compared to what we did up to now. The file is loaded into memory only when requested limiting the area of TOCTOU attacks. Users will be allowed to place the initramfs file on any u-boot accessible partition instead of just the ESP one. Finally this is an attempt of a generic interface across architectures in the linux kernel so it makes sense to support that.

Quoted 7 lines

The file location is intentionally only supported as a config option argument(CONFIG_EFI_INITRD_FILESPEC), in an effort to enhance security. Although U-boot is not responsible for verifying the integrity of the initramfs, we can enhance the offered security by only accepting a built-in option, which will be naturally verified by UEFI Secure Boot. This can easily change in the future if needed and configure that via ENV or UEFI variable.

What if we have several bootable images with different initrd required at the same time? For example, we may install ubuntu 16.04, 18.04 etc to the same device (and then boot one of them via grub).

Since the guid is unique, U-Boot doesn't have any measure to identify which initrd be exported via FILE2_PROTOCOL.
In this sense, I don't think using a config option is a good idea in general.

Quoted 7 lines
Quoted 4 lines

Can't you concatenate multiple cpio archives for this? I haven't checked this personally but i remember distros doing it to add firmware files they need for example. It might not be ideal because you may need different user-space program versions or different versions of the same script(??)

I wonder think why it is not a problem.

I never said it wasn't, on the contrary, i pointed out cases were concatenating cpio's might not be enough.

Quoted 5 lines
Quoted 2 lines

but the linux
module loading part, of different kernel revisions, should be there.

Can we assume that this is only the use of initrd?

Of course not.

Quoted 7 lines
Quoted 5 lines

I don't have any strong preference on this anyway hence my commit message, it's just another naive protection, since the user can always replace the initramfs using the same name and break out of this. I never needed multiple initrd files so I preferred the config option. If people need a config option it's easily extensible.

How? I don't think it's trivial.

Parse an env variable and override the config option? Something like:
setenv efi_initrd 'mmc 0:1 foo.cpio.gz' -> env_get("efi_initrd") wouldn't work?

Quoted 24 lines
Quoted 24 lines
Quoted 2 lines
Quoted 2 lines

+#define EFI_LOAD_FILE_PROTOCOL_GUID \

[...]

Quoted 5 lines

include/efi_api.h struct efi_runtime_services { · line 324

Quoted 2 lines
include/efi_api.h +2 -0
+	EFI_GUID(0x56ec3091, 0x954c, 0x11d2, \
+		 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b)

Do you need this definition?

Not really it just felt weird adding LOAD_FILE2 without the LOAD_FILE definition.

Quoted 5 lines
Quoted 5 lines
include/efi_api.h +4 -1
+#define EFI_LOAD_FILE2_PROTOCOL_GUID \
+	EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e, \
+		 0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)
+
--- /dev/null

[...]

Quoted 5 lines
Quoted 1 line

+++ b/include/efi_load_initrd.h

It might be unlikely, but the file name should be more generic for potential enhancement.

Quoted 4 lines
Quoted 2 lines

This is defining a very specific GUID for initramfs that Linux consumes to load the file, I'd rather keep it as is.

I didn't intent the guid, but a file name.

Yes my response is for the file name. It serves a sole purpose, define the explicit GUID linux expects.

Quoted 12 lines
Quoted 12 lines
Quoted 1 line
Quoted 1 line
(quoted diff) +0 -0
@@ -0,0 +1,25 @@

[...]

Quoted 9 lines

lib/efi_loader/Kconfig config EFI_RNG_PROTOCOL · line 143

Quoted 4 lines
lib/efi_loader/Kconfig +4 -0
+
+config EFI_INITRD_FILESPEC
+	string "initramfs path"
+	default "host 0:1 initrd"

"host" interface makes sense only on sandbox, which will not be expected to be able to boot any linux kernel.
So this default value is inappropriate anyway.

Quoted 6 lines
Quoted 3 lines

Not really. The user *must* have an idea on what he is doing and this is the default config we need to run on the sandbox. So it serves both as an example and a defult config for the sandbox testing.

I don't think we can boot a linux kernel from U-Boot on sandbox.

We run the sefltest though, which is replicating the Linux efi stub functionality.

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines

lib/efi_loader/efi_load_initrd.c

Quoted 19 lines
lib/efi_loader/Kconfig +4 -0
+	depends on EFI_LOAD_FILE2_INITRD
+	help
+	  Full path of the initramfs file, e.g. mmc 0:2 initramfs.cpio.gz.
+
 endif
lib/efi_loader/Makefile +1 -0
index 04dc8648512b..9b3b70447336 100644
@@ -43,3 +43,4 @@ obj-$(CONFIG_NET) += efi_net.o
 obj-$(CONFIG_GENERATE_ACPI_TABLE) += efi_acpi.o
 obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
 obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o
+obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
lib/efi_loader/efi_load_initrd.c new file +0 -0
new file mode 100644
index 000000000000..574a83d7e308

I'd prefer efi_file_initrd.c for similarity to efi_file.c, or more specifically efi_linux.c.

Quoted 9 lines
Quoted 2 lines

I can send a patch renaming those but i think it's pointless. The filename describes exactly what's the intent imho.

What if we will want to add another FILE2_PROTOCOL or linux-specifc hack?
Now I believe that OS (or platform)-dependent implementation should go into a separate directory, like lib/efi_linux/... for clarification.

I am not against that, i just think the tons of comments are already enough. But i see the point in the long run, assuming we'll add enough OS-specific options to justify this.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
lib/efi_loader/efi_load_initrd.c new file +2 -0
@@ -0,0 +1,198 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 2 lines

I think that we should return a better error code rather than NOT_FOUND.

Like? The spec has a specific set of returns codes you can use there, EFI_UNSUPPORTED
EFI_INVALID_PARAMETER
EFI_NO_MEDIA
EFI_DEVICE_ERROR
EFI_NO_RESPONSE
EFI_NOT_FOUND
EFI_ABORTED
EFI_BUFFER_TOO_SMALL

Quoted 3 lines
Quoted 1 line

Is there anything you like better?

In most of all cases, OUT_OF_RESOURCES.

If i am not reading the spec wrong you *can't* return that here and even if you could, the majority of error cases fail if the file was not found. Maybe apart from this:
ret = fs_set_blk_dev(dev, part, FS_TYPE_ANY);

if (ret) {
	status = EFI_NO_MEDIA;

goto out;
}

Thanks
/Ilias