Skip to contentSkip to conversation

RISCV_EFI_BOOT_PROTOCOL support in U-boot

RFC PATCH Superseded Sunil V L 3 participants 6 messages Jan 26, 2022 – Jan 28, 2022 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sunil V L

author
#0 lore raw

cover RISCV_EFI_BOOT_PROTOCOL support in U-boot

This patch adds the support in u-boot for new RISCV_EFI_BOOT_PROTOCOL for RISC-V UEFI platforms. This protocol is required to communicate the boot hart ID to the bootloader/kernel which need to follow the EFI calling conventions.

The latest draft spec of this new protocol is available at
https://github.com/riscv-non-isa/riscv-uefi/releases/download/1.0-rc2/RISCV_UEFI_PROTOCOL-spec.pdf

This u-boot patch can be found in:
riscv_boot_protocol_rfc_v1 branch at https://github.com/vlsunil/u-boot.git

This patch is tested in qemu. To fully test the feature, we need Linux changes which consume this protocol.
The linux patch can be found in:
riscv_boot_protocol_rfc_v1 branch at https://github.com/vlsunil/linux.git

Sunil V L (1):
efi_loader: Enable RISCV_EFI_BOOT_PROTOCOL support

 include/efi_api.h          |  4 +++
 include/efi_loader.h       |  2 ++
 include/efi_riscv.h        | 16 +++++++++
 lib/efi_loader/Kconfig     |  7 ++++
 lib/efi_loader/Makefile    |  1 +
 lib/efi_loader/efi_riscv.c | 69 ++++++++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_setup.c |  6 ++++
 7 files changed, 105 insertions(+)
 create mode 100644 include/efi_riscv.h
 create mode 100644 lib/efi_loader/efi_riscv.c
Signature · 1 line
2.25.1
author
#1 lore raw

RFC PATCH 1/1 efi_loader: Enable RISCV_EFI_BOOT_PROTOCOL support

This adds support for new RISCV_EFI_BOOT_PROTOCOL to communicate the boot hart ID to bootloader/kernel on RISC-V UEFI platforms.

---
 include/efi_api.h          |  4 +++
 include/efi_loader.h       |  2 ++
 include/efi_riscv.h        | 16 +++++++++
 lib/efi_loader/Kconfig     |  7 ++++
 lib/efi_loader/Makefile    |  1 +
 lib/efi_loader/efi_riscv.c | 69 ++++++++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_setup.c |  6 ++++
 7 files changed, 105 insertions(+)
 create mode 100644 include/efi_riscv.h
 create mode 100644 lib/efi_loader/efi_riscv.c
include/efi_api.h +4 -0
index 8d5d835bd0..f123d0557c 100644
@@ -438,6 +438,10 @@ struct efi_runtime_services {
 	EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, \
 		 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
 
+#define RISCV_EFI_BOOT_PROTOCOL_GUID \
+	EFI_GUID(0xccd15fec, 0x6f73, 0x4eec, 0x83, \
+		 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf)
+
 /**
  * struct efi_configuration_table - EFI Configuration Table
  *
include/efi_loader.h +2 -0
index 701efcd2b6..1fa75b40fe 100644
@@ -527,6 +527,8 @@ efi_status_t efi_disk_register(void);
 efi_status_t efi_rng_register(void);
 /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
 efi_status_t efi_tcg2_register(void);
+/* Called by efi_init_obj_list() to install RISCV_EFI_BOOT_PROTOCOL */
+efi_status_t efi_riscv_register(void);
 /* Called by efi_init_obj_list() to do initial measurement */
 efi_status_t efi_tcg2_do_initial_measurement(void);
 /* measure the pe-coff image, extend PCR and add Event Log */
include/efi_riscv.h new file +16 -0
new file mode 100644
index 0000000000..6beb2637f6
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * RISCV_EFI_BOOT_PROTOCOL
+ *
+ * Copyright (c) 2022 Ventana Micro Systems Inc
+ */
+
+#include <efi_api.h>
+
+#define RISCV_EFI_BOOT_PROTOCOL_REVISION 0x00010000
+
+struct riscv_efi_boot_protocol {
+	u64 revision;
+	efi_status_t (EFIAPI *get_boot_hartid) (struct riscv_efi_boot_protocol *this,
+						efi_uintn_t *boot_hartid);
+};
lib/efi_loader/Kconfig +7 -0
index 24f9a2bb75..77ba6a7ea1 100644
@@ -369,4 +369,11 @@ config EFI_ESRT
 	help
 	  Enabling this option creates the ESRT UEFI system table.
 
+config EFI_RISCV_BOOT_PROTOCOL
+	bool "RISCV_EFI_BOOT_PROTOCOL support"
+	default y
+	depends on RISCV
+	help
+	  Provide a RISCV_EFI_BOOT_PROTOCOL implementation on RISC-V platform.
+
 endif
lib/efi_loader/Makefile +1 -0
index fd344cea29..b2c664d108 100644
@@ -62,6 +62,7 @@ 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_TCG2_PROTOCOL) += efi_tcg2.o
+obj-$(CONFIG_EFI_RISCV_BOOT_PROTOCOL) += efi_riscv.o
 obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
 obj-$(CONFIG_EFI_SIGNATURE_SUPPORT) += efi_signature.o
 
lib/efi_loader/efi_riscv.c new file +69 -0

69 lines not on this page · raw message

lib/efi_loader/efi_setup.c +6 -0
index 49172e3579..380adc15c8 100644
@@ -247,6 +247,12 @@ efi_status_t efi_init_obj_list(void)
 			goto out;
 	}
 
+	if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
+		ret = efi_riscv_register();
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+
 	/* Secure boot */
 	ret = efi_init_secure_boot();
 	if (ret != EFI_SUCCESS)
Signature · 1 line
2.25.1
#2 lore raw

Re: [RFC PATCH 1/1] efi_loader: Enable RISCV_EFI_BOOT_PROTOCOL support

Quoted 73 lines · Sunil V L

This adds support for new RISCV_EFI_BOOT_PROTOCOL to communicate the boot hart ID to bootloader/kernel on RISC-V UEFI platforms.

---

include/efi_api.h          |  4 +++
include/efi_loader.h       |  2 ++
include/efi_riscv.h        | 16 +++++++++
lib/efi_loader/Kconfig     |  7 ++++
lib/efi_loader/Makefile    |  1 +
lib/efi_loader/efi_riscv.c | 69 ++++++++++++++++++++++++++++++++++++++
lib/efi_loader/efi_setup.c |  6 ++++
7 files changed, 105 insertions(+)

create mode 100644 include/efi_riscv.h
create mode 100644 lib/efi_loader/efi_riscv.c

include/efi_api.h +4 -0
index 8d5d835bd0..f123d0557c 100644
@@ -438,6 +438,10 @@ struct efi_runtime_services {
 	EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, \
 		 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
 
+#define RISCV_EFI_BOOT_PROTOCOL_GUID \
+	EFI_GUID(0xccd15fec, 0x6f73, 0x4eec, 0x83, \
+		 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf)
+
 /**
 * struct efi_configuration_table - EFI Configuration Table
 *
include/efi_loader.h +2 -0
index 701efcd2b6..1fa75b40fe 100644
@@ -527,6 +527,8 @@ efi_status_t efi_disk_register(void);
 efi_status_t efi_rng_register(void);
 /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
 efi_status_t efi_tcg2_register(void);
+/* Called by efi_init_obj_list() to install RISCV_EFI_BOOT_PROTOCOL */
+efi_status_t efi_riscv_register(void);
 /* Called by efi_init_obj_list() to do initial measurement */
 efi_status_t efi_tcg2_do_initial_measurement(void);
 /* measure the pe-coff image, extend PCR and add Event Log */
include/efi_riscv.h new file +16 -0
new file mode 100644
index 0000000000..6beb2637f6
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * RISCV_EFI_BOOT_PROTOCOL
+ *
+ * Copyright (c) 2022 Ventana Micro Systems Inc
+ */
+
+#include <efi_api.h>
+
+#define RISCV_EFI_BOOT_PROTOCOL_REVISION 0x00010000
+
+struct riscv_efi_boot_protocol {
+	u64 revision;
+	efi_status_t (EFIAPI *get_boot_hartid) (struct riscv_efi_boot_protocol *this,
+						efi_uintn_t *boot_hartid);
+};
lib/efi_loader/Kconfig +0 -0
index 24f9a2bb75..77ba6a7ea1 100644

lib/efi_loader/Kconfig config EFI_ESRT · line 374

Quoted 6 lines
lib/efi_loader/Kconfig +3 -0
@@ -369,4 +369,11 @@ config EFI_ESRT
 	help
 	  Enabling this option creates the ESRT UEFI system table.
 
+config EFI_RISCV_BOOT_PROTOCOL
+	bool "RISCV_EFI_BOOT_PROTOCOL support"
+	default y

Why would you ever turn it off for a RISC-V EFI system?

Quoted 97 lines
lib/efi_loader/Kconfig +4 -0
+	depends on RISCV
+	help
+	  Provide a RISCV_EFI_BOOT_PROTOCOL implementation on RISC-V platform.
+

endif

lib/efi_loader/Makefile +0 -0
index fd344cea29..b2c664d108 100644
@@ -62,6 +62,7 @@ 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_TCG2_PROTOCOL) += efi_tcg2.o +obj-$(CONFIG_EFI_RISCV_BOOT_PROTOCOL) += efi_riscv.o obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o obj-$(CONFIG_EFI_SIGNATURE_SUPPORT) += efi_signature.o

lib/efi_loader/efi_riscv.c new file +69 -0
new file mode 100644
index 0000000000..91b8d2b927
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Defines APIs that allow an OS to interact with UEFI firmware to query
+ * information about the boot hart ID.
+ *
+ * Copyright (c) 2022, Ventana Micro Systems Inc
+ */
+
+#define LOG_CATEGORY LOGC_EFI
+#include <common.h>
+#include <efi_loader.h>
+#include <efi_variable.h>
+#include <log.h>
+#include <asm/global_data.h>
+#include <efi_riscv.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static const efi_guid_t efi_guid_riscv_boot_protocol = RISCV_EFI_BOOT_PROTOCOL_GUID;
+static efi_uintn_t hartid;
+
+/**
+ * efi_riscv_get_boot_hartid() - return boot hart ID
+ *
+ * @this:		RISCV_EFI_BOOT_PROTOCOL instance
+ * @boot_hartid		caller allocated memory to return boot hart id
+
+ * Return:	status code
+ */
+static efi_status_t EFIAPI
+efi_riscv_get_boot_hartid(struct riscv_efi_boot_protocol *this,
+					efi_uintn_t *boot_hartid)
+{
+	log_err(" efi_riscv_get_boot_hartid ENTER\n");
+	if ((this == NULL) || (boot_hartid == NULL))
+		return EFI_INVALID_PARAMETER;
+
+	*boot_hartid = hartid;
+
+	return EFI_SUCCESS;
+}
+
+static const struct riscv_efi_boot_protocol riscv_efi_boot_prot = {
+	.revision = RISCV_EFI_BOOT_PROTOCOL_REVISION,
+	.get_boot_hartid = efi_riscv_get_boot_hartid
+};
+
+/**
+ * efi_riscv_register() - register RISCV_EFI_BOOT_PROTOCOL
+ *
+ *
+ * Return:	status code
+ */
+efi_status_t efi_riscv_register(void)
+{
+	efi_status_t ret = EFI_SUCCESS;
+
+	/* save boot hart id since gd is not accessible after launching
+	 * the kernel
+	 */
+	hartid = gd->arch.boot_hart;
+
+	ret = efi_add_protocol(efi_root, &efi_guid_riscv_boot_protocol,
+			       (void *)&riscv_efi_boot_prot);
+	if (ret != EFI_SUCCESS) {
+		log_err("Cannot install RISCV_EFI_BOOT_PROTOCOL\n");
+	}
+	return ret;
+}
lib/efi_loader/efi_setup.c +0 -0
index 49172e3579..380adc15c8 100644

lib/efi_loader/efi_setup.c efi_status_t efi_init_obj_list(void) · line 255

Quoted 9 lines
lib/efi_loader/efi_setup.c +6 -0
@@ -247,6 +247,12 @@ efi_status_t efi_init_obj_list(void)
 			goto out;
 	}
 
+	if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
+		ret = efi_riscv_register();
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+

I don’t think this will link if EFI_RISCV_BOOT_PROTOCOL is disabled. At least not at -O0, at -O1 and above it’ll probably optimise away the dead code and thus not reference the undefined symbol.

Jess

Quoted 6 lines
lib/efi_loader/efi_setup.c +0 -0
 	/* Secure boot */
 	ret = efi_init_secure_boot();
 	if (ret != EFI_SUCCESS)
Signature · 1 line
2.25.1
#3 lore raw

Re: [RFC PATCH 1/1] efi_loader: Enable RISCV_EFI_BOOT_PROTOCOL support

Am 26. Januar 2022 15:01:56 MEZ schrieb Jessica Clarke <[email protected]>:

Quoted 77 lines · Sunil V L
Quoted 76 lines

This adds support for new RISCV_EFI_BOOT_PROTOCOL to communicate the boot hart ID to bootloader/kernel on RISC-V UEFI platforms.

---

include/efi_api.h          |  4 +++
include/efi_loader.h       |  2 ++
include/efi_riscv.h        | 16 +++++++++
lib/efi_loader/Kconfig     |  7 ++++
lib/efi_loader/Makefile    |  1 +
lib/efi_loader/efi_riscv.c | 69 ++++++++++++++++++++++++++++++++++++++
lib/efi_loader/efi_setup.c |  6 ++++
7 files changed, 105 insertions(+)

create mode 100644 include/efi_riscv.h
create mode 100644 lib/efi_loader/efi_riscv.c

include/efi_api.h +4 -0
index 8d5d835bd0..f123d0557c 100644
@@ -438,6 +438,10 @@ struct efi_runtime_services {
 	EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, \
 		 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
 
+#define RISCV_EFI_BOOT_PROTOCOL_GUID \
+	EFI_GUID(0xccd15fec, 0x6f73, 0x4eec, 0x83, \
+		 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf)
+
 /**
 * struct efi_configuration_table - EFI Configuration Table
 *
include/efi_loader.h +2 -0
index 701efcd2b6..1fa75b40fe 100644
@@ -527,6 +527,8 @@ efi_status_t efi_disk_register(void);
 efi_status_t efi_rng_register(void);
 /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
 efi_status_t efi_tcg2_register(void);
+/* Called by efi_init_obj_list() to install RISCV_EFI_BOOT_PROTOCOL */
+efi_status_t efi_riscv_register(void);
 /* Called by efi_init_obj_list() to do initial measurement */
 efi_status_t efi_tcg2_do_initial_measurement(void);
 /* measure the pe-coff image, extend PCR and add Event Log */
include/efi_riscv.h new file +16 -0
new file mode 100644
index 0000000000..6beb2637f6
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * RISCV_EFI_BOOT_PROTOCOL
+ *
+ * Copyright (c) 2022 Ventana Micro Systems Inc
+ */
+
+#include <efi_api.h>
+
+#define RISCV_EFI_BOOT_PROTOCOL_REVISION 0x00010000
+
+struct riscv_efi_boot_protocol {
+	u64 revision;
+	efi_status_t (EFIAPI *get_boot_hartid) (struct riscv_efi_boot_protocol *this,
+						efi_uintn_t *boot_hartid);
+};
lib/efi_loader/Kconfig +0 -0
index 24f9a2bb75..77ba6a7ea1 100644
@@ -369,4 +369,11 @@ config EFI_ESRT
 	help
 	  Enabling this option creates the ESRT UEFI system table.
 
Quoted 5 lines

lib/efi_loader/Kconfig config EFI_ESRT · line 374

Quoted 3 lines
lib/efi_loader/Kconfig +3 -0
+config EFI_RISCV_BOOT_PROTOCOL
+	bool "RISCV_EFI_BOOT_PROTOCOL support"
+	default y

Why would you ever turn it off for a RISC-V EFI system?

We don't need the protocol on ARM. So we want a configuration symbol for it.

The feature is experimental as there is no ratified specification requiring it. So it is ok to be able to disable it. Anyway it defaults to yes on RISC-V.

We still put the boot-hartid into the device-tree as elder kernels need it and future kernels may support this due to the legacy entry point. To minimize the code size on constrained devices it may be necessary to disable the protocol.

Quoted 101 lines
Quoted 101 lines
lib/efi_loader/Kconfig +4 -0
+	depends on RISCV
+	help
+	  Provide a RISCV_EFI_BOOT_PROTOCOL implementation on RISC-V platform.
+

endif

lib/efi_loader/Makefile +0 -0
index fd344cea29..b2c664d108 100644
@@ -62,6 +62,7 @@ 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_TCG2_PROTOCOL) += efi_tcg2.o +obj-$(CONFIG_EFI_RISCV_BOOT_PROTOCOL) += efi_riscv.o obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o obj-$(CONFIG_EFI_SIGNATURE_SUPPORT) += efi_signature.o

lib/efi_loader/efi_riscv.c new file +69 -0
new file mode 100644
index 0000000000..91b8d2b927
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Defines APIs that allow an OS to interact with UEFI firmware to query
+ * information about the boot hart ID.
+ *
+ * Copyright (c) 2022, Ventana Micro Systems Inc
+ */
+
+#define LOG_CATEGORY LOGC_EFI
+#include <common.h>
+#include <efi_loader.h>
+#include <efi_variable.h>
+#include <log.h>
+#include <asm/global_data.h>
+#include <efi_riscv.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static const efi_guid_t efi_guid_riscv_boot_protocol = RISCV_EFI_BOOT_PROTOCOL_GUID;
+static efi_uintn_t hartid;
+
+/**
+ * efi_riscv_get_boot_hartid() - return boot hart ID
+ *
+ * @this:		RISCV_EFI_BOOT_PROTOCOL instance
+ * @boot_hartid		caller allocated memory to return boot hart id
+
+ * Return:	status code
+ */
+static efi_status_t EFIAPI
+efi_riscv_get_boot_hartid(struct riscv_efi_boot_protocol *this,
+					efi_uintn_t *boot_hartid)
+{
+	log_err(" efi_riscv_get_boot_hartid ENTER\n");
+	if ((this == NULL) || (boot_hartid == NULL))
+		return EFI_INVALID_PARAMETER;
+
+	*boot_hartid = hartid;
+
+	return EFI_SUCCESS;
+}
+
+static const struct riscv_efi_boot_protocol riscv_efi_boot_prot = {
+	.revision = RISCV_EFI_BOOT_PROTOCOL_REVISION,
+	.get_boot_hartid = efi_riscv_get_boot_hartid
+};
+
+/**
+ * efi_riscv_register() - register RISCV_EFI_BOOT_PROTOCOL
+ *
+ *
+ * Return:	status code
+ */
+efi_status_t efi_riscv_register(void)
+{
+	efi_status_t ret = EFI_SUCCESS;
+
+	/* save boot hart id since gd is not accessible after launching
+	 * the kernel
+	 */
+	hartid = gd->arch.boot_hart;
+
+	ret = efi_add_protocol(efi_root, &efi_guid_riscv_boot_protocol,
+			       (void *)&riscv_efi_boot_prot);
+	if (ret != EFI_SUCCESS) {
+		log_err("Cannot install RISCV_EFI_BOOT_PROTOCOL\n");
+	}
+	return ret;
+}
lib/efi_loader/efi_setup.c +0 -0
index 49172e3579..380adc15c8 100644
@@ -247,6 +247,12 @@ efi_status_t efi_init_obj_list(void)
 			goto out;
 	}
 
Quoted 10 lines

lib/efi_loader/efi_setup.c efi_status_t efi_init_obj_list(void) · line 255

Quoted 6 lines
lib/efi_loader/efi_setup.c +6 -0
+	if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
+		ret = efi_riscv_register();
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+

I don’t think this will link if EFI_RISCV_BOOT_PROTOCOL is disabled. At least not at -O0, at -O1 and above it’ll probably optimise away the dead code and thus not reference the undefined symbol.

U-Boot is built with -O2 (or -Os). The project wants to avoid #ifdef. See the warning in scripts/checkpatch.pl. With this coding style we catch more coding issues in our CI without increasing the binary size.

Best regards

Heinrich

Quoted 10 lines

Jess

Quoted 6 lines
lib/efi_loader/efi_setup.c +0 -0
 	/* Secure boot */
 	ret = efi_init_secure_boot();
 	if (ret != EFI_SUCCESS)
Signature · 1 line
2.25.1
#4 lore raw

Re: [RFC PATCH 1/1] efi_loader: Enable RISCV_EFI_BOOT_PROTOCOL support

In reply to Sunil V L
Quoted 52 lines · Sunil V L

This adds support for new RISCV_EFI_BOOT_PROTOCOL to communicate the boot hart ID to bootloader/kernel on RISC-V UEFI platforms.

---
  include/efi_api.h          |  4 +++
  include/efi_loader.h       |  2 ++
  include/efi_riscv.h        | 16 +++++++++
  lib/efi_loader/Kconfig     |  7 ++++
  lib/efi_loader/Makefile    |  1 +
  lib/efi_loader/efi_riscv.c | 69 ++++++++++++++++++++++++++++++++++++++
  lib/efi_loader/efi_setup.c |  6 ++++
  7 files changed, 105 insertions(+)

create mode 100644 include/efi_riscv.h
create mode 100644 lib/efi_loader/efi_riscv.c

include/efi_api.h +4 -0
index 8d5d835bd0..f123d0557c 100644
@@ -438,6 +438,10 @@ struct efi_runtime_services {
  	EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, \
  		 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
 
+#define RISCV_EFI_BOOT_PROTOCOL_GUID \
+	EFI_GUID(0xccd15fec, 0x6f73, 0x4eec, 0x83, \
+		 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf)
+
  /**
   * struct efi_configuration_table - EFI Configuration Table
   *
include/efi_loader.h +2 -0
index 701efcd2b6..1fa75b40fe 100644
@@ -527,6 +527,8 @@ efi_status_t efi_disk_register(void);
  efi_status_t efi_rng_register(void);
  /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
  efi_status_t efi_tcg2_register(void);
+/* Called by efi_init_obj_list() to install RISCV_EFI_BOOT_PROTOCOL */
+efi_status_t efi_riscv_register(void);
  /* Called by efi_init_obj_list() to do initial measurement */
  efi_status_t efi_tcg2_do_initial_measurement(void);
  /* measure the pe-coff image, extend PCR and add Event Log */
include/efi_riscv.h new file +1 -0
new file mode 100644
index 0000000000..6beb2637f6
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0+ */

include/efi_riscv.h line 11

Quoted 10 lines
include/efi_riscv.h new file +10 -0
+/*
+ * RISCV_EFI_BOOT_PROTOCOL
+ *
+ * Copyright (c) 2022 Ventana Micro Systems Inc
+ */
+
+#include <efi_api.h>
+
+#define RISCV_EFI_BOOT_PROTOCOL_REVISION 0x00010000
+

Please, add Sphinx style comments to describe the structure.

Quoted 49 lines
include/efi_riscv.h new file +5 -0
+struct riscv_efi_boot_protocol {
+	u64 revision;
+	efi_status_t (EFIAPI *get_boot_hartid) (struct riscv_efi_boot_protocol *this,
+						efi_uintn_t *boot_hartid);
+};
lib/efi_loader/Kconfig +7 -0
index 24f9a2bb75..77ba6a7ea1 100644
@@ -369,4 +369,11 @@ config EFI_ESRT
  	help
  	  Enabling this option creates the ESRT UEFI system table.
 
+config EFI_RISCV_BOOT_PROTOCOL
+	bool "RISCV_EFI_BOOT_PROTOCOL support"
+	default y
+	depends on RISCV
+	help
+	  Provide a RISCV_EFI_BOOT_PROTOCOL implementation on RISC-V platform.
+
  endif
lib/efi_loader/Makefile +1 -0
index fd344cea29..b2c664d108 100644
@@ -62,6 +62,7 @@ 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_TCG2_PROTOCOL) += efi_tcg2.o
+obj-$(CONFIG_EFI_RISCV_BOOT_PROTOCOL) += efi_riscv.o
  obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
  obj-$(CONFIG_EFI_SIGNATURE_SUPPORT) += efi_signature.o
 
lib/efi_loader/efi_riscv.c new file +10 -0
new file mode 100644
index 0000000000..91b8d2b927
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Defines APIs that allow an OS to interact with UEFI firmware to query
+ * information about the boot hart ID.
+ *
+ * Copyright (c) 2022, Ventana Micro Systems Inc
+ */
+
+#define LOG_CATEGORY LOGC_EFI
+#include <common.h>

lib/efi_loader/efi_riscv.c line 20

Quoted 10 lines
lib/efi_loader/efi_riscv.c new file +10 -0
+#include <efi_loader.h>
+#include <efi_variable.h>
+#include <log.h>
+#include <asm/global_data.h>
+#include <efi_riscv.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static const efi_guid_t efi_guid_riscv_boot_protocol = RISCV_EFI_BOOT_PROTOCOL_GUID;
+static efi_uintn_t hartid;

I think we can do without this static variable. gd->arch.boot_hart does not change.

Quoted 4 lines
lib/efi_loader/efi_riscv.c new file +4 -0
+
+/**
+ * efi_riscv_get_boot_hartid() - return boot hart ID
+ *

lib/efi_loader/efi_riscv.c line 34

Quoted 10 lines
lib/efi_loader/efi_riscv.c new file +10 -0
+ * @this:		RISCV_EFI_BOOT_PROTOCOL instance
+ * @boot_hartid		caller allocated memory to return boot hart id
+
+ * Return:	status code
+ */
+static efi_status_t EFIAPI
+efi_riscv_get_boot_hartid(struct riscv_efi_boot_protocol *this,
+					efi_uintn_t *boot_hartid)
+{
+	log_err(" efi_riscv_get_boot_hartid ENTER\n");

This seems to be left from debugging.

Please, use EFI_ENTRY in all UEFI API interface implementations.

EFI_ENTRY("");

This will show the function name when enabling debug messages.

lib/efi_loader/efi_riscv.c line 35

Quoted 1 line
lib/efi_loader/efi_riscv.c new file +1 -0
+	if ((this == NULL) || (boot_hartid == NULL))

We try to avoid == NULL. We should check against the actual protocol.

if (this != riscv_efi_boot_prot || !boot_hartid)

lib/efi_loader/efi_riscv.c line 40

Quoted 5 lines
lib/efi_loader/efi_riscv.c new file +5 -0
+		return EFI_INVALID_PARAMETER;
+
+	*boot_hartid = hartid;
+
+	return EFI_SUCCESS;

return EFI_EXIT(EFI_SUCCESS).

Quoted 9 lines
lib/efi_loader/efi_riscv.c new file +9 -0
+}
+
+static const struct riscv_efi_boot_protocol riscv_efi_boot_prot = {
+	.revision = RISCV_EFI_BOOT_PROTOCOL_REVISION,
+	.get_boot_hartid = efi_riscv_get_boot_hartid
+};
+
+/**
+ * efi_riscv_register() - register RISCV_EFI_BOOT_PROTOCOL

lib/efi_loader/efi_riscv.c line 59

Quoted 10 lines
lib/efi_loader/efi_riscv.c new file +10 -0
+ *
+ *
+ * Return:	status code
+ */
+efi_status_t efi_riscv_register(void)
+{
+	efi_status_t ret = EFI_SUCCESS;
+
+	/* save boot hart id since gd is not accessible after launching
+	 * the kernel

gd is accessible until EXIT_BOOT_SERVICES(). After EXIT_BOOT_SERVICES the protocol cannot be called.

set_gd(efi_gd) called by EFI_ENTRY() takes care of restoring the gp register if the kernel should mess it up, which an UEFI program should not do.

Best regards

Heinrich

Quoted 27 lines
lib/efi_loader/efi_riscv.c new file +10 -0
+	 */
+	hartid = gd->arch.boot_hart;
+
+	ret = efi_add_protocol(efi_root, &efi_guid_riscv_boot_protocol,
+			       (void *)&riscv_efi_boot_prot);
+	if (ret != EFI_SUCCESS) {
+		log_err("Cannot install RISCV_EFI_BOOT_PROTOCOL\n");
+	}
+	return ret;
+}
lib/efi_loader/efi_setup.c +6 -0
index 49172e3579..380adc15c8 100644
@@ -247,6 +247,12 @@ efi_status_t efi_init_obj_list(void)
  			goto out;
  	}
 
+	if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
+		ret = efi_riscv_register();
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+
  	/* Secure boot */
  	ret = efi_init_secure_boot();
  	if (ret != EFI_SUCCESS)
author
#5 lore raw

Re: [RFC PATCH 1/1] efi_loader: Enable RISCV_EFI_BOOT_PROTOCOL support

Hi Heinrich,

Quoted 57 lines · Heinrich Schuchardt
Quoted 56 lines · Sunil V L

This adds support for new RISCV_EFI_BOOT_PROTOCOL to communicate the boot hart ID to bootloader/kernel on RISC-V UEFI platforms.

---
  include/efi_api.h          |  4 +++
  include/efi_loader.h       |  2 ++
  include/efi_riscv.h        | 16 +++++++++
  lib/efi_loader/Kconfig     |  7 ++++
  lib/efi_loader/Makefile    |  1 +
  lib/efi_loader/efi_riscv.c | 69 ++++++++++++++++++++++++++++++++++++++
  lib/efi_loader/efi_setup.c |  6 ++++
  7 files changed, 105 insertions(+)

create mode 100644 include/efi_riscv.h
create mode 100644 lib/efi_loader/efi_riscv.c

include/efi_api.h +4 -0
index 8d5d835bd0..f123d0557c 100644
@@ -438,6 +438,10 @@ struct efi_runtime_services {
  	EFI_GUID(0x607f766c, 0x7455, 0x42be, 0x93, \
  		 0x0b, 0xe4, 0xd7, 0x6d, 0xb2, 0x72, 0x0f)
 
+#define RISCV_EFI_BOOT_PROTOCOL_GUID \
+	EFI_GUID(0xccd15fec, 0x6f73, 0x4eec, 0x83, \
+		 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf)
+
  /**
   * struct efi_configuration_table - EFI Configuration Table
   *
include/efi_loader.h +2 -0
index 701efcd2b6..1fa75b40fe 100644
@@ -527,6 +527,8 @@ efi_status_t efi_disk_register(void);
  efi_status_t efi_rng_register(void);
  /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
  efi_status_t efi_tcg2_register(void);
+/* Called by efi_init_obj_list() to install RISCV_EFI_BOOT_PROTOCOL */
+efi_status_t efi_riscv_register(void);
  /* Called by efi_init_obj_list() to do initial measurement */
  efi_status_t efi_tcg2_do_initial_measurement(void);
  /* measure the pe-coff image, extend PCR and add Event Log */
include/efi_riscv.h new file +5 -0
new file mode 100644
index 0000000000..6beb2637f6
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * RISCV_EFI_BOOT_PROTOCOL
+ *
+ * Copyright (c) 2022 Ventana Micro Systems Inc
Quoted 8 lines

include/efi_riscv.h line 11

Quoted 6 lines
include/efi_riscv.h new file +6 -0
+ */
+
+#include <efi_api.h>
+
+#define RISCV_EFI_BOOT_PROTOCOL_REVISION 0x00010000
+

Please, add Sphinx style comments to describe the structure.

Sure.

Quoted 55 lines
Quoted 55 lines
include/efi_riscv.h new file +5 -0
+struct riscv_efi_boot_protocol {
+	u64 revision;
+	efi_status_t (EFIAPI *get_boot_hartid) (struct riscv_efi_boot_protocol *this,
+						efi_uintn_t *boot_hartid);
+};
lib/efi_loader/Kconfig +7 -0
index 24f9a2bb75..77ba6a7ea1 100644
@@ -369,4 +369,11 @@ config EFI_ESRT
  	help
  	  Enabling this option creates the ESRT UEFI system table.
 
+config EFI_RISCV_BOOT_PROTOCOL
+	bool "RISCV_EFI_BOOT_PROTOCOL support"
+	default y
+	depends on RISCV
+	help
+	  Provide a RISCV_EFI_BOOT_PROTOCOL implementation on RISC-V platform.
+
  endif
lib/efi_loader/Makefile +1 -0
index fd344cea29..b2c664d108 100644
@@ -62,6 +62,7 @@ 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_TCG2_PROTOCOL) += efi_tcg2.o
+obj-$(CONFIG_EFI_RISCV_BOOT_PROTOCOL) += efi_riscv.o
  obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
  obj-$(CONFIG_EFI_SIGNATURE_SUPPORT) += efi_signature.o
 
lib/efi_loader/efi_riscv.c new file +15 -0
new file mode 100644
index 0000000000..91b8d2b927
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Defines APIs that allow an OS to interact with UEFI firmware to query
+ * information about the boot hart ID.
+ *
+ * Copyright (c) 2022, Ventana Micro Systems Inc
+ */
+
+#define LOG_CATEGORY LOGC_EFI
+#include <common.h>
+#include <efi_loader.h>
+#include <efi_variable.h>
+#include <log.h>
+#include <asm/global_data.h>
+#include <efi_riscv.h>
Quoted 8 lines

lib/efi_loader/efi_riscv.c line 20

Quoted 5 lines
lib/efi_loader/efi_riscv.c new file +5 -0
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static const efi_guid_t efi_guid_riscv_boot_protocol = RISCV_EFI_BOOT_PROTOCOL_GUID;
+static efi_uintn_t hartid;

I think we can do without this static variable. gd->arch.boot_hart does not change.

Yes. I missed EFI_ENTRY. Without EFI_ENTRY(), this didn't work. Will fix this in RFC V2 patch.

Quoted 22 lines
Quoted 5 lines
lib/efi_loader/efi_riscv.c new file +4 -0
+
+/**
+ * efi_riscv_get_boot_hartid() - return boot hart ID
+ *

lib/efi_loader/efi_riscv.c line 34

Quoted 10 lines
lib/efi_loader/efi_riscv.c new file +10 -0
+ * @this:		RISCV_EFI_BOOT_PROTOCOL instance
+ * @boot_hartid		caller allocated memory to return boot hart id
+
+ * Return:	status code
+ */
+static efi_status_t EFIAPI
+efi_riscv_get_boot_hartid(struct riscv_efi_boot_protocol *this,
+					efi_uintn_t *boot_hartid)
+{
+	log_err(" efi_riscv_get_boot_hartid ENTER\n");

This seems to be left from debugging.

Please, use EFI_ENTRY in all UEFI API interface implementations.

EFI_ENTRY("");

Quoted 8 lines

This will show the function name when enabling debug messages.

lib/efi_loader/efi_riscv.c line 35

Quoted 1 line
lib/efi_loader/efi_riscv.c new file +1 -0
+	if ((this == NULL) || (boot_hartid == NULL))

We try to avoid == NULL. We should check against the actual protocol.

if (this != riscv_efi_boot_prot || !boot_hartid)

Thanks!. Will fix these issues.

Quoted 31 lines

lib/efi_loader/efi_riscv.c line 40

Quoted 5 lines
lib/efi_loader/efi_riscv.c new file +5 -0
+		return EFI_INVALID_PARAMETER;
+
+	*boot_hartid = hartid;
+
+	return EFI_SUCCESS;

return EFI_EXIT(EFI_SUCCESS).

Quoted 9 lines
lib/efi_loader/efi_riscv.c new file +9 -0
+}
+
+static const struct riscv_efi_boot_protocol riscv_efi_boot_prot = {
+	.revision = RISCV_EFI_BOOT_PROTOCOL_REVISION,
+	.get_boot_hartid = efi_riscv_get_boot_hartid
+};
+
+/**
+ * efi_riscv_register() - register RISCV_EFI_BOOT_PROTOCOL

lib/efi_loader/efi_riscv.c line 59

Quoted 10 lines
lib/efi_loader/efi_riscv.c new file +10 -0
+ *
+ *
+ * Return:	status code
+ */
+efi_status_t efi_riscv_register(void)
+{
+	efi_status_t ret = EFI_SUCCESS;
+
+	/* save boot hart id since gd is not accessible after launching
+	 * the kernel

gd is accessible until EXIT_BOOT_SERVICES(). After EXIT_BOOT_SERVICES the protocol cannot be called.

Quoted 3 lines

set_gd(efi_gd) called by EFI_ENTRY() takes care of restoring the gp register if the kernel should mess it up, which an UEFI program should not do.

Thanks a lot for your comments. Will address these issues and send V2 version.

Thanks!
Sunil

Quoted 33 lines

Best regards

Heinrich

Quoted 27 lines
lib/efi_loader/efi_riscv.c new file +10 -0
+	 */
+	hartid = gd->arch.boot_hart;
+
+	ret = efi_add_protocol(efi_root, &efi_guid_riscv_boot_protocol,
+			       (void *)&riscv_efi_boot_prot);
+	if (ret != EFI_SUCCESS) {
+		log_err("Cannot install RISCV_EFI_BOOT_PROTOCOL\n");
+	}
+	return ret;
+}
lib/efi_loader/efi_setup.c +6 -0
index 49172e3579..380adc15c8 100644
@@ -247,6 +247,12 @@ efi_status_t efi_init_obj_list(void)
  			goto out;
  	}
 
+	if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
+		ret = efi_riscv_register();
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+
  	/* Secure boot */
  	ret = efi_init_secure_boot();
  	if (ret != EFI_SUCCESS)