Skip to contentSkip to conversation

Add ESRT and test ESRT creation

PATCH Superseded Jose Marinho 1 participant 3 messages Feb 20, 2021

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add ESRT and test ESRT creation

The following 2 commits add the ESRT and provide a test of the functionality.

The first commit adds the ESRT as defined in the UEFI 2.8 specification. An empty ESRT is created during the execution of the efi_init_obj_list(). The ESRT is updated when:
1) a FMP protocol is installed in the system: this will add the
corresponding entries to the ESRT.
2) a capsule is installed via UpdateCapsule: this should update
entries already present in the ESRT.

This implementation of the ESRT creation takes input from FMP only. It is assumed that the FMP will maintain the following values across reboot:
- LastAttemptVersion.
- LastAttemptStatus.

The second commit enables testing the ESRT creation in the sandbox platform. That commit is composed of 2 tests.
- Test 1 executes from the u-boot shell with "ut lib".
- Test 2 executes in the pytest environment.

Patch v3:
- Address v2 comments.

Patch v2:
- The ESRT is now regenerated from scratch at every FMP EVT_NOTIFY_SIGNAL and whenever a capsule is updated.
- Extended TestEfiCapsuleFirmwareFit::test_efi_capsule_fw3 to verify that the ESRT is correctly populated after an UpdateCapsule.
- Addressed v1 comments.

Patch v1:
- reworked the ESRT creation code, allowing table to resize as FMPs are installed.
- registered a callback for the FMP protocol install.
- Created a unit test running on the sandbox platform.

rfc: initial patch submission

CC Heinrich Schuchardt <[email protected]>
CC Sughosh Ganu <[email protected]>
CC AKASHI Takahiro <[email protected]>
CC Ilias Apalodimas <[email protected]>
CC Andre Przywara <[email protected]>
CC Alexander Graf <[email protected]>
CC nd at arm.com

Jose Marinho (2):
efi: Add ESRT to the EFI system table
efi: ESRT creation tests

 cmd/efidebug.c                                |  68 +++
 include/efi_api.h                             |  21 +
 include/efi_loader.h                          |  20 +
 lib/efi_loader/Kconfig                        |   7 +
 lib/efi_loader/Makefile                       |   1 +
 lib/efi_loader/efi_capsule.c                  |   8 +
 lib/efi_loader/efi_esrt.c                     | 518 ++++++++++++++++++
 lib/efi_loader/efi_setup.c                    |   6 +
 test/lib/Makefile                             |   1 +
 test/lib/efi_esrt.c                           | 191 +++++++
 .../test_efi_capsule/test_capsule_firmware.py |   4 +
 11 files changed, 845 insertions(+)
 create mode 100644 lib/efi_loader/efi_esrt.c
 create mode 100644 test/lib/efi_esrt.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 1/2 efi: Add ESRT to the EFI system table

The ESRT is initialised during efi_init_objlist after efi_initialize_system_table().

The ESRT is initially created with size for 50 FW image entries. The ESRT is resized when it runs out of space. Every resize adds 50 additional entries.
The ESRT is populated from information provided by FMP instances only.

CC Heinrich Schuchardt <[email protected]>
CC Sughosh Ganu <[email protected]>
CC AKASHI Takahiro <[email protected]>
CC Ilias Apalodimas <[email protected]>
CC Andre Przywara <[email protected]>
CC Alexander Graf <[email protected]>
CC nd at arm.com
---
 cmd/efidebug.c               |   4 +
 include/efi_api.h            |  21 ++
 include/efi_loader.h         |  20 ++
 lib/efi_loader/Kconfig       |   7 +
 lib/efi_loader/Makefile      |   1 +
 lib/efi_loader/efi_capsule.c |   8 +
 lib/efi_loader/efi_esrt.c    | 518 +++++++++++++++++++++++++++++++++++
 lib/efi_loader/efi_setup.c   |   6 +
 8 files changed, 585 insertions(+)
 create mode 100644 lib/efi_loader/efi_esrt.c
cmd/efidebug.c +4 -0
index bbbcb0a546..a7dace2f80 100644
@@ -459,6 +459,10 @@ static const struct {
 		"Block IO",
 		EFI_BLOCK_IO_PROTOCOL_GUID,
 	},
+	{
+		"EFI System Resource Table",
+		EFI_SYSTEM_RESOURCE_TABLE_GUID,
+	},
 	{
 		"Simple File System",
 		EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
include/efi_api.h +21 -0
index 48e48a6263..fb53637419 100644
@@ -1722,6 +1722,23 @@ struct efi_load_file_protocol {
 					 void *buffer);
 };
 
+struct efi_system_resource_entry {
+	efi_guid_t fw_class;
+	u32 fw_type;
+	u32 fw_version;
+	u32 lowest_supported_fw_version;
+	u32 capsule_flags;
+	u32 last_attempt_version;
+	u32 last_attempt_status;
+} __packed;
+
+struct efi_system_resource_table {
+	u32 fw_resource_count;
+	u32 fw_resource_count_max;
+	u64 fw_resource_version;
+	struct efi_system_resource_entry entries[];
+} __packed;
+
 /* Boot manager load options */
 #define LOAD_OPTION_ACTIVE		0x00000001
 #define LOAD_OPTION_FORCE_RECONNECT	0x00000002
@@ -1740,6 +1757,10 @@ struct efi_load_file_protocol {
 #define ESRT_FW_TYPE_DEVICEFIRMWARE	0x00000002
 #define ESRT_FW_TYPE_UEFIDRIVER		0x00000003
 
+#define EFI_SYSTEM_RESOURCE_TABLE_GUID\
+	EFI_GUID(0xb122a263, 0x3661, 0x4f68,\
+		0x99, 0x29, 0x78, 0xf8, 0xb0, 0xd6, 0x21, 0x80)
+
 /* Last Attempt Status Values */
 #define LAST_ATTEMPT_STATUS_SUCCESS			0x00000000
 #define LAST_ATTEMPT_STATUS_ERROR_UNSUCCESSFUL		0x00000001
include/efi_loader.h +20 -0
index f470bbd636..c2720f2823 100644
@@ -214,6 +214,8 @@ extern const efi_guid_t efi_guid_rng_protocol;
 extern const efi_guid_t efi_guid_capsule_report;
 /* GUID of firmware management protocol */
 extern const efi_guid_t efi_guid_firmware_management_protocol;
+/* GUID for the ESRT */
+extern const efi_guid_t efi_esrt_guid;
 
 extern unsigned int __efi_runtime_start, __efi_runtime_stop;
 extern unsigned int __efi_runtime_rel_start, __efi_runtime_rel_stop;
@@ -884,4 +886,22 @@ static inline efi_status_t efi_launch_capsules(void)
 
 #endif /* CONFIG_IS_ENABLED(EFI_LOADER) */
 
+/**
+ * Install the ESRT system table.
+ *
+ * @return	status code
+ */
+efi_status_t efi_esrt_register(void);
+
+/**
+ * efi_esrt_populate() - Populates the ESRT entries from the FMP instances
+ * present in the system.
+ * If an ESRT already exists, the old ESRT is replaced in the system table.
+ * The memory of the old ESRT is deallocated.
+ *
+ * Return:
+ * - EFI_SUCCESS if the ESRT is correctly created
+ * - error code otherwise.
+ */
+efi_status_t efi_esrt_populate(void);
 #endif /* _EFI_LOADER_H */
lib/efi_loader/Kconfig +7 -0
index e729f727df..a96014ce18 100644
@@ -347,4 +347,11 @@ config EFI_SECURE_BOOT
 	  it is signed with a trusted key. To do that, you need to install,
 	  at least, PK, KEK and db.
 
+config EFI_ESRT
+	bool "Enable the UEFI ESRT generation"
+	depends on EFI_CAPSULE_FIRMWARE_MANAGEMENT
+	default y
+	help
+	  Enabling this option creates the ESRT UEFI system table.
+
 endif
lib/efi_loader/Makefile +1 -0
index 10b42e8847..9a8127846f 100644
@@ -52,6 +52,7 @@ obj-y += efi_variable.o
 obj-$(CONFIG_EFI_VARIABLES_PRESEED) += efi_var_seed.o
 endif
 obj-y += efi_watchdog.o
+obj-$(CONFIG_EFI_ESRT) += efi_esrt.o
 obj-$(CONFIG_LCD) += efi_gop.o
 obj-$(CONFIG_DM_VIDEO) += efi_gop.o
 obj-$(CONFIG_PARTITIONS) += efi_disk.o
lib/efi_loader/efi_capsule.c +8 -0
index b57f0302c5..a1a69e619d 100644
@@ -482,6 +482,14 @@ efi_status_t EFIAPI efi_update_capsule(
 			goto out;
 	}
 out:
+
+	if (IS_ENABLED(CONFIG_EFI_ESRT)) {
+		/* Rebuild the ESRT to reflect any updated FW images. */
+		ret = EFI_CALL(efi_esrt_populate());
+		if (ret != EFI_SUCCESS)
+			log_warning("EFI Capsule: failed to update ESRT\n");
+	}
+
 	return EFI_EXIT(ret);
 }
 
lib/efi_loader/efi_esrt.c new file +518 -0

518 lines not on this page · raw message

lib/efi_loader/efi_setup.c +6 -0
index b1c5125032..3c5cf9a435 100644
@@ -227,6 +227,12 @@ efi_status_t efi_init_obj_list(void)
 	if (ret != EFI_SUCCESS)
 		goto out;
 
+	if (IS_ENABLED(CONFIG_EFI_ESRT)) {
+		ret = efi_esrt_register();
+		if (ret != EFI_SUCCESS)
+			goto out;
+	}
+
 	if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
 		ret = efi_tcg2_register();
 		if (ret != EFI_SUCCESS)
Signature · 1 line
2.17.1
#2 lore raw

PATCH v3 2/2 efi: ESRT creation tests

In reply to Jose Marinho

This commmit exercises the ESRT creation -- introduced in the previous commit -- in two tests.

test 1:
A fake FMP, with TEST_ESRT_NUM_ENTRIES FW images, is installed in the system leading to the corresponding ESRT entries being populated. The ESRT entries are checked against the datastructure used to initialize the FMP.

test 1 invocation:
make sandbox_capsule_defconfig all
./u-boot -d arch/sandbox/dts/test.dtb
ut lib

test 2:
The test is part of test_efi_capsule_fw3.

In order to run the test the following must be added to sandbox_defconfig:
+CONFIG_CMD_SF=y
+CONFIG_CMD_MEMORY=y
+CONFIG_CMD_FAT=y
+CONFIG_DFU=y

The ESRT is printed in the u-boot shell by calling efidebug esrt. The test ensures that, after the capsule is installed, the ESRT contains entries with the GUIDs:
- EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID;
- EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID;

test 2 invocation:
sudo ./test/py/test.py --bd sandbox -k capsule_fw3 -l --build

CC Heinrich Schuchardt <[email protected]>
CC Sughosh Ganu <[email protected]>
CC AKASHI Takahiro <[email protected]>
CC Ilias Apalodimas <[email protected]>
CC Andre Przywara <[email protected]>
CC Alexander Graf <[email protected]>
CC nd at arm.com
---
 cmd/efidebug.c                                |  64 ++++++
 test/lib/Makefile                             |   1 +
 test/lib/efi_esrt.c                           | 191 ++++++++++++++++++
 .../test_efi_capsule/test_capsule_firmware.py |   4 +
 4 files changed, 260 insertions(+)
 create mode 100644 test/lib/efi_esrt.c
cmd/efidebug.c +64 -0
index a7dace2f80..5a9ff2bd9a 100644
@@ -129,6 +129,61 @@ static int do_efi_capsule_show(struct cmd_tbl *cmdtp, int flag,
 	return CMD_RET_SUCCESS;
 }
 
+#ifdef CONFIG_EFI_ESRT
+/**
+ * do_efi_capsule_esrt() - manage UEFI capsules
+ *
+ * @cmdtp:	Command table
+ * @flag:	Command flag
+ * @argc:	Number of arguments
+ * @argv:	Argument array
+ * Return:	CMD_RET_SUCCESS on success,
+ *		CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
+ *
+ * Implement efidebug "capsule esrt" sub-command.
+ * The prints the current ESRT table.
+ *
+ *     efidebug capsule esrt
+ */
+static int do_efi_capsule_esrt(struct cmd_tbl *cmdtp, int flag,
+			       int argc, char * const argv[])
+{
+	struct efi_system_resource_table *esrt = NULL;
+
+	if (argc != 1)
+		return CMD_RET_USAGE;
+
+	for (int idx = 0; idx < systab.nr_tables; idx++)
+		if (!guidcmp(&efi_esrt_guid, &systab.tables[idx].guid))
+			esrt = (struct efi_system_resource_table *)systab.tables[idx].table;
+
+	if (!esrt)
+		return CMD_RET_FAILURE;
+
+	printf("========================================\n");
+	printf("ESRT: fw_resource_count=%d\n", esrt->fw_resource_count);
+	printf("ESRT: fw_resource_count_max=%d\n", esrt->fw_resource_count_max);
+	printf("ESRT: fw_resource_version=%lld\n", esrt->fw_resource_version);
+
+	for (int idx = 0; idx < esrt->fw_resource_count; idx++) {
+		printf("[entry %d]==============================\n", idx);
+		printf("ESRT: fw_class=%pUL\n", &esrt->entries[idx].fw_class);
+		printf("ESRT: fw_type=%d\n", esrt->entries[idx].fw_type);
+		printf("ESRT: fw_version=%d\n", esrt->entries[idx].fw_version);
+		printf("ESRT: lowest_supported_fw_version=%d\n",
+		       esrt->entries[idx].lowest_supported_fw_version);
+		printf("ESRT: capsule_flags=%d\n",
+		       esrt->entries[idx].capsule_flags);
+		printf("ESRT: last_attempt_version=%d\n",
+		       esrt->entries[idx].last_attempt_version);
+		printf("ESRT: last_attempt_status=%d\n",
+		       esrt->entries[idx].last_attempt_status);
+	}
+	printf("========================================\n");
+
+	return CMD_RET_SUCCESS;
+}
+#endif /*  CONFIG_EFI_ESRT */
 /**
  * do_efi_capsule_res() - show a capsule update result
  *
@@ -221,6 +276,10 @@ static struct cmd_tbl cmd_efidebug_capsule_sub[] = {
 			 "", ""),
 	U_BOOT_CMD_MKENT(show, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_show,
 			 "", ""),
+#ifdef CONFIG_EFI_ESRT
+	U_BOOT_CMD_MKENT(esrt, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_esrt,
+			 "", ""),
+#endif
 	U_BOOT_CMD_MKENT(disk-update, 0, 0, do_efi_capsule_on_disk_update,
 			 "", ""),
 	U_BOOT_CMD_MKENT(result, CONFIG_SYS_MAXARGS, 1, do_efi_capsule_res,
@@ -256,6 +315,7 @@ static int do_efi_capsule(struct cmd_tbl *cmdtp, int flag,
 
 	return cp->cmd(cmdtp, flag, argc, argv);
 }
+
 #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT */
 
 /**
@@ -1580,6 +1640,10 @@ static char efidebug_help_text[] =
 	"  - show capsule information\n"
 	"efidebug capsule result [<capsule result var>]\n"
 	"  - show a capsule update result\n"
+#ifdef CONFIG_EFI_ESRT
+	"efidebug capsule esrt\n"
+	"  - print the ESRT\n"
+#endif
 	"\n"
 #endif
 	"efidebug devices\n"
test/lib/Makefile +1 -0
index 97c11e35a8..aeb8c13eb0 100644
@@ -15,3 +15,4 @@ obj-$(CONFIG_UT_LIB_ASN1) += asn1.o
 obj-$(CONFIG_UT_LIB_RSA) += rsa.o
 obj-$(CONFIG_AES) += test_aes.o
 obj-$(CONFIG_GETOPT) += getopt.o
+obj-$(CONFIG_EFI_ESRT) += efi_esrt.o
test/lib/efi_esrt.c new file +191 -0

191 lines not on this page · raw message

test/py/tests/test_efi_capsule/test_capsule_firmware.py +4 -0
index f006fa95d6..3a7c2e1ac8 100644
@@ -229,6 +229,10 @@ class TestEfiCapsuleFirmwareFit(object):
                 output = u_boot_console.run_command(
                     'env print -e -all Capsule0000')
 
+            output = u_boot_console.run_command_list(['efidebug capsule esrt'])
+            assert 'AE13FF2D-9AD4-4E25-9AC8-6D80B3B22147' in ''.join(output)
+            assert 'E2BB9C06-70E9-4B14-97A3-5A7913176E3F' in ''.join(output)
+
             output = u_boot_console.run_command_list([
                 'host bind 0 %s' % disk_img,
                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
Signature · 1 line
2.17.1