Add ESRT and test ESRT creation
v6 cover Add ESRT and test ESRT creation
The following 4 commits add the ESRT and provide tests for 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 introduces a helper cmd function to print the ESRT in the u-boot shell.
The third and forth commits contain two test for the ESRT creation in the sandbox platform.
- Test 1 executes from the u-boot shell with "bootefi selftest".
- Test 2 executes in the pytest environment.
Patch v6:
- split the v5 3/3 commit into v6 3/4 (efi selftest) and v6 4/4 (addition to test_efi_capsule_fw3).
- Address v5 comments.
Patch v5:
- Address v4 comments.
- split the v4 2/2 commit into the v5 2/3 (debug print ESRT) and 3/3 (ESRT tests).
Patch v4:
- update stale [Patch 1/2 v3] commit message.
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
Jose Marinho (4):
efi: Add ESRT to the EFI system table
cmd: efi: ESRT table debug print
efi: ESRT creation tests
Test the ESRT creation within an existing pytest.
cmd/efidebug.c | 70 +++ include/efi_api.h | 33 ++ include/efi_loader.h | 24 + lib/efi_loader/Kconfig | 7 + lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_boottime.c | 7 +- lib/efi_loader/efi_capsule.c | 8 + lib/efi_loader/efi_esrt.c | 518 ++++++++++++++++++ lib/efi_loader/efi_setup.c | 6 + lib/efi_selftest/Makefile | 2 + lib/efi_selftest/efi_selftest_esrt.c | 264 +++++++++ .../test_efi_capsule/test_capsule_firmware.py | 8 + 12 files changed, 944 insertions(+), 4 deletions(-) create mode 100644 lib/efi_loader/efi_esrt.c create mode 100644 lib/efi_selftest/efi_selftest_esrt.c
Signature · 1 line
2.17.1
PATCH v6 1/4 efi: Add ESRT to the EFI system table
The ESRT is initialised during efi_init_objlist after efi_initialize_system_table().
The ESRT is recreated from scratch at the following events:
- successful UpdateCapsule;
- FMP instance install.
The code ensures that every ESRT entry has a unique fw_class value.
Limitations:
- The ESRT is not updated if an FMP instance is uninstalled;
- the fields image_type and flags are in the current implementation left undefined. Setting these values will require a per-platform function that returns the image_type/flags as a function of the image fw_class.
Remove two EFI_CALL() indirections.
Move ESRT GUID in efidebug's list of GUIDs.
--- cmd/efidebug.c | 4 + include/efi_api.h | 21 ++ include/efi_loader.h | 24 ++ lib/efi_loader/Kconfig | 7 + lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_boottime.c | 7 +- lib/efi_loader/efi_capsule.c | 8 + lib/efi_loader/efi_esrt.c | 510 ++++++++++++++++++++++++++++++++++ lib/efi_loader/efi_setup.c | 6 + 9 files changed, 584 insertions(+), 4 deletions(-) create mode 100644 lib/efi_loader/efi_esrt.c
cmd/efidebug.c +4 -0
@@ -516,6 +516,10 @@ static const struct {"ACPI table" ,EFI_ACPI_TABLE_GUID , }, + { +"EFI System Resource Table" , +EFI_SYSTEM_RESOURCE_TABLE_GUID , + }, {"device tree" , EFI_FDT_GUID,
include/efi_api.h +21 -0
@@ -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 +24 -0
@@ -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;
@@ -558,6 +560,10 @@ struct efi_simple_file_system_protocol *efi_simple_file_system(/* open file from device-path: */ struct efi_file_handle *efi_file_from_path (struct efi_device_path *fp); +/* Registers a callback function for a notification event. */ +efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol, +struct efi_event *event, +void **registration);/** * efi_size_in_pages() - convert size in bytes to size in pages *
@@ -890,4 +896,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
@@ -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
@@ -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_boottime.c +3 -4
@@ -1406,10 +1406,9 @@ out:* * Return: status code */ -static efi_status_t EFIAPI efi_register_protocol_notify( -const efi_guid_t *protocol, -struct efi_event *event, -void **registration) +efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol, +struct efi_event *event, +void **registration) {struct efi_register_notify_event *item;efi_status_t ret =EFI_SUCCESS ;
lib/efi_loader/efi_capsule.c +8 -0
@@ -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_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 +510 -0
510 lines not on this page · raw message
lib/efi_loader/efi_setup.c +6 -0
@@ -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
PATCH v6 2/4 cmd: efi: ESRT table debug print
In reply to Jose MarinhoThis commit enables the ESRT printing from the u-boot shell by invoking:
- efidebug capsule esrt
--- cmd/efidebug.c | 66 +++++++++++++++++++++++++++++++++++++++ include/efi_api.h | 12 +++++++ lib/efi_loader/efi_esrt.c | 8 +++++ 3 files changed, 86 insertions(+)
cmd/efidebug.c +66 -0
@@ -129,6 +129,63 @@ 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) { +log_info ("ESRT: table not present\n" ); +return CMD_RET_SUCCESS ; + } + +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=%s\n" ,EFI_FW_TYPE_STR_GET (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=%s\n" , +EFI_FW_STATUS_STR_GET (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 +278,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, +"" ,"" ), +#endifU_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 +317,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 */ /**
@@ -1584,6 +1646,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"
include/efi_api.h +12 -0
@@ -1757,6 +1757,12 @@ struct efi_system_resource_table {#define ESRT_FW_TYPE_DEVICEFIRMWARE 0x00000002#define ESRT_FW_TYPE_UEFIDRIVER 0x00000003 +#define EFI_FW_TYPE_NUM 4 +extern char *efi_fw_type_str[EFI_FW_TYPE_NUM ]; +#define EFI_FW_TYPE_STR_GET (idx) (\ +EFI_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\ +) +#define EFI_SYSTEM_RESOURCE_TABLE_GUID \ EFI_GUID(0xb122a263, 0x3661, 0x4f68,\ 0x99, 0x29, 0x78, 0xf8, 0xb0, 0xd6, 0x21, 0x80)
@@ -1772,6 +1778,12 @@ struct efi_system_resource_table {#define LAST_ATTEMPT_STATUS_ERROR_PWR_EVT_BATT 0x00000007#define LAST_ATTEMPT_STATUS_ERROR_UNSATISFIED_DEPENDENCIES 0x00000008 +#define EFI_UPDATE_STATUS_NUM 9 +extern char *efi_update_status_str[EFI_UPDATE_STATUS_NUM ]; +#define EFI_FW_STATUS_STR_GET (idx) (\ +EFI_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\ +) +/* * The LastAttemptStatus values of 0x1000 - 0x4000 are reserved for vendor * usage.
lib/efi_loader/efi_esrt.c +8 -0
@@ -15,6 +15,14 @@ const efi_guid_t efi_esrt_guid = EFI_SYSTEM_RESOURCE_TABLE_GUID;static struct efi_system_resource_table *esrt; +char *efi_fw_type_str[EFI_FW_TYPE_NUM ] = {"unknown" ,"system FW" ,"device FW" , +"UEFI driver" }; + +char *efi_update_status_str[EFI_UPDATE_STATUS_NUM ] = {"success" ,"unsuccessful" , +"insufficient resources" ,"incorrect version" ,"invalid format" , +"auth error" ,"power event (AC)" ,"power event (batt)" , +"unsatisfied dependencies" }; +#define EFI_ESRT_VERSION 1/**
Signature · 1 line
2.17.1
PATCH v6 2/4 cmd: efi: ESRT table debug print
Quoted 151 lines · Jose Marinho
This commit enables the ESRT printing from the u-boot shell by invoking:
- efidebug capsule esrtCC 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.comSigned-off-by Jose Marinho <[email protected]>--- cmd/efidebug.c | 66 +++++++++++++++++++++++++++++++++++++++ include/efi_api.h | 12 +++++++ lib/efi_loader/efi_esrt.c | 8 +++++ 3 files changed, 86 insertions(+)cmd/efidebug.c +66
-0@@ -129,6 +129,63 @@ 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) { +log_info ("ESRT: table not present\n" ); +return CMD_RET_SUCCESS ; + } + +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=%s\n" ,EFI_FW_TYPE_STR_GET (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=%s\n" , +EFI_FW_STATUS_STR_GET (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 +278,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, +"" ,"" ), +#endifU_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 +317,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 */ /** @@ -1584,6 +1646,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" include/efi_api.h +12
-0@@ -1757,6 +1757,12 @@ struct efi_system_resource_table {#define ESRT_FW_TYPE_DEVICEFIRMWARE 0x00000002#define ESRT_FW_TYPE_UEFIDRIVER 0x00000003 +#define EFI_FW_TYPE_NUM 4 +extern char *efi_fw_type_str[EFI_FW_TYPE_NUM ]; +#define EFI_FW_TYPE_STR_GET (idx) (\ +EFI_FW_TYPE_NUM > (idx) ? efi_fw_type_str[(idx)] : "error"\ +) +#define EFI_SYSTEM_RESOURCE_TABLE_GUID \ EFI_GUID(0xb122a263, 0x3661, 0x4f68,\ 0x99, 0x29, 0x78, 0xf8, 0xb0, 0xd6, 0x21, 0x80)@@ -1772,6 +1778,12 @@ struct efi_system_resource_table {#define LAST_ATTEMPT_STATUS_ERROR_PWR_EVT_BATT 0x00000007#define LAST_ATTEMPT_STATUS_ERROR_UNSATISFIED_DEPENDENCIES 0x00000008 +#define EFI_UPDATE_STATUS_NUM 9 +extern char *efi_update_status_str[EFI_UPDATE_STATUS_NUM ]; +#define EFI_FW_STATUS_STR_GET (idx) (\ +EFI_UPDATE_STATUS_NUM > (idx) ? efi_update_status_str[(idx)] : "error"\ +) +/* * The LastAttemptStatus values of 0x1000 - 0x4000 are reserved for vendor * usage. lib/efi_loader/efi_esrt.c +0
-0@@ -15,6 +15,14 @@ const efi_guid_t efi_esrt_guid = EFI_SYSTEM_RESOURCE_TABLE_GUID;
lib/efi_loader/efi_esrt.c const efi_guid_t efi_esrt_guid = EFI_SYSTEM_RESOURCE_TABLE_GUID; · line 25
Quoted 10 lines
lib/efi_loader/efi_esrt.c +8
-0static struct efi_system_resource_table *esrt; +char *efi_fw_type_str[EFI_FW_TYPE_NUM ] = {"unknown" ,"system FW" ,"device FW" , +"UEFI driver" }; + +char *efi_update_status_str[EFI_UPDATE_STATUS_NUM ] = {"success" ,"unsuccessful" , +"insufficient resources" ,"incorrect version" ,"invalid format" , +"auth error" ,"power event (AC)" ,"power event (batt)" , +"unsatisfied dependencies" }; +
These tables are only used by the efidebug command and are not needed if CONFIG_CMD_EFIDEBUG=n. So shouldn't they better be moved to cmd/efidebug?
Best regards
Heinrich
Quoted 4 lines
lib/efi_loader/efi_esrt.c +0
-0#define EFI_ESRT_VERSION 1/**
PATCH v6 3/4 efi: ESRT creation tests
In reply to Jose MarinhoThis commmit exercises the ESRT creation in a EFI selftest.
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.
Invocation from the sandbox platform:
add to sandbox_defconfig:
+CONFIG_CMD_BOOTEFI_SELFTEST=y
make sandbox_capsule_defconfig all
./u-boot -d arch/sandbox/dts/test.dtb
bootefi selftest
--- lib/efi_selftest/Makefile | 2 + lib/efi_selftest/efi_selftest_esrt.c | 264 +++++++++++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 lib/efi_selftest/efi_selftest_esrt.c
lib/efi_selftest/Makefile +2 -0
@@ -72,6 +72,8 @@ ifeq ($(CONFIG_BLK)$(CONFIG_DOS_PARTITION),yy) obj-y += efi_selftest_block_device.o endif +obj-$(CONFIG_EFI_ESRT) += efi_selftest_esrt.o + targets += \ efi_miniapp_file_image_exception.h \ efi_miniapp_file_image_exit.h \
lib/efi_selftest/efi_selftest_esrt.c new file +264 -0
264 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v6 3/4 efi: ESRT creation tests
Quoted 131 lines · Jose Marinho
This commmit exercises the ESRT creation in a EFI selftest.
A fake FMP, with
TEST_ESRT_NUM_ENTRIESFW 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.Invocation from the sandbox platform:
add tosandbox_defconfig:
+CONFIG_CMD_BOOTEFI_SELFTEST=ymake
sandbox_capsule_defconfigall
./u-boot -darch/sandbox/dts/test.dtb
bootefi selftestCC 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.comSigned-off-by Jose Marinho <[email protected]>--- lib/efi_selftest/Makefile | 2 + lib/efi_selftest/efi_selftest_esrt.c | 264 +++++++++++++++++++++++++++ 2 files changed, 266 insertions(+)create mode 100644
lib/efi_selftest/efi_selftest_esrt.clib/efi_selftest/Makefile +2
-0@@ -72,6 +72,8 @@ ifeq ($(CONFIG_BLK)$(CONFIG_DOS_PARTITION),yy) obj-y += efi_selftest_block_device.o endif +obj-$(CONFIG_EFI_ESRT) += efi_selftest_esrt.o + targets += \ efi_miniapp_file_image_exception.h \ efi_miniapp_file_image_exit.h \lib/efi_selftest/efi_selftest_esrt.c new file +83
-0@@ -0,0 +1,264 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Test ESRT tables support +* +* Copyright (C) 2021 Arm Ltd. +*/ +#include <common.h> +#include <efi_loader.h> +#include <efi_selftest.h> + +// This value must not exceed 255. +// An FMP cannot contain more than 255 FW images. +#define TEST_ESRT_NUM_ENTRIES 255 + +static +struct efi_firmware_image_descriptor static_img_info[TEST_ESRT_NUM_ENTRIES ]; + +static const struct efi_system_table *local_systable; + +static void efi_test_esrt_init_info (void ) +{ +for (int idx =0 ; idx <TEST_ESRT_NUM_ENTRIES ; idx++) { + static_img_info[idx].image_index = idx; + +// Note: the 16 byte value present in +// static_img_info[idx].image_type_id is not strictly a GUID. +// The value is used for the sake of code testing. + static_img_info[idx].image_type_id.b[0 ] = idx; + + static_img_info[idx].image_id =0 ; + static_img_info[idx].image_id_name =NULL ; + static_img_info[idx].version =0 ; + static_img_info[idx].version_name =NULL ; + static_img_info[idx].size =0 ; + static_img_info[idx].lowest_supported_image_version =1 ; + static_img_info[idx].last_attempt_version =2 ; + static_img_info[idx].last_attempt_status =3 ; + static_img_info[idx].hardware_instance =1 ; + } +} + +static efi_status_t +EFIAPI efi_test_fmp_get_image_info(struct efi_firmware_management_protocol *this, +efi_uintn_t *image_info_size, +struct efi_firmware_image_descriptor *image_info, +u32 *descriptor_version, +u8 *descriptor_count, +efi_uintn_t *descriptor_size, +u32 *package_version, +u16 **package_version_name) +{ +efi_status_t ret =EFI_SUCCESS ; + +if (!image_info_size) +return EFI_INVALID_PARAMETER ; + +if (descriptor_version) + *descriptor_version =EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION ; +if (descriptor_count) + *descriptor_count =TEST_ESRT_NUM_ENTRIES ; +if (descriptor_size) + *descriptor_size =sizeof (*image_info); +if (package_version) + *package_version =0xffffffff ; +if (package_version_name) + *package_version_name =NULL ; + +if (*image_info_size <sizeof (*image_info)) { + *image_info_size = *descriptor_size * *descriptor_count; +return EFI_BUFFER_TOO_SMALL ; + } + +for (int idx =0 ; idx <TEST_ESRT_NUM_ENTRIES ; idx++) + image_info[idx] = static_img_info[idx]; + +return ret; +} + +static struct efi_firmware_management_protocol efi_test_fmp = { + .get_image_info = efi_test_fmp_get_image_info, + .get_image =NULL , + .set_image =NULL , + .check_image =NULL ,
lib/efi_selftest/efi_selftest_esrt.c line 93
Quoted 10 lines
lib/efi_selftest/efi_selftest_esrt.c new file +10
-0+ .get_package_info = NULL, + .set_package_info = NULL, +}; + +static void *lib_test_get_esrt (void ) +{ +if (!local_systable) { +efi_st_error ("systable not found or uninitialized\n" ); +return NULL ; + }
This cannot occur.
lib/efi_selftest/efi_selftest_esrt.c line 101
Quoted 8 lines
lib/efi_selftest/efi_selftest_esrt.c new file +8
-0+ +for (int idx =0 ; idx < local_systable->nr_tables; idx++) +if (!guidcmp (&efi_esrt_guid, &local_systable->tables[idx].guid)) +return local_systable->tables[idx].table; + +return NULL ; +} +
Please, provide a Sphinx style description of the function.
lib/efi_selftest/efi_selftest_esrt.c line 109
Quoted 8 lines
lib/efi_selftest/efi_selftest_esrt.c new file +8
-0+static bool lib_test_check_uuid_entry (struct efi_system_resource_table *esrt, +struct efi_firmware_image_descriptor + *img_info) +{ +const u32 filled_entries = esrt->fw_resource_count; +struct efi_system_resource_entry *entry = esrt->entries; + +for (u32 idx =0 ; idx < filled_entries; idx++) {
Why is the outside loop over the images and the inside loop over ESRT entries? Wouldn't the other way round make more sense?
Execution of the test will fail because you are starting at index 0. You should start@index base_entry_count.
lib/efi_selftest/efi_selftest_esrt.c line 111
Quoted 2 lines
lib/efi_selftest/efi_selftest_esrt.c new file +2
-0+if (!guidcmp (&entry[idx].fw_class, &img_info->image_type_id)) { +if (entry[idx].fw_version != img_info->version)
It would be helpful to indicate which field is wrong.
lib/efi_selftest/efi_selftest_esrt.c line 112
Quoted 1 line
lib/efi_selftest/efi_selftest_esrt.c new file +1
-0+return false;
Shouldn't you only check the entry with the matching image index?
Quoted 104 lines
lib/efi_selftest/efi_selftest_esrt.c new file +104
-0+ +if (entry[idx].lowest_supported_fw_version != + img_info->lowest_supported_image_version) +return false; + +if (entry[idx].last_attempt_version != + img_info->last_attempt_version) +return false; + +if (entry[idx].last_attempt_status != + img_info->last_attempt_status) +return false; + +/* +* The entry with fw_class = img_uuid matches with the +* remainder fmp input. +*/ +return true; + } + } + +/* There exists no entry with fw_class equal to img_uuid in the ESRT. */ +return false; +} + +/* +* Setup unit test. +* +* Initialize the test FMP datastructure. +* Install the FMP in the system. +* +* @handle: handle of the loaded image +* @systable: system table +* @return: EFI_ST_SUCCESS for success +*/ +static int setup (const efi_handle_t handle, +const struct efi_system_table *systable) +{ + local_systable = systable; + +if (!local_systable) { +efi_st_error ("systable not found\n" ); +return EFI_ST_FAILURE ; + } + +efi_test_esrt_init_info (); + +return EFI_ST_SUCCESS ; +} + +/* +* Tear down unit test. +* +* Uninstall the test FMP. +* +* @return: EFI_ST_SUCCESS for success +*/ +static int teardown (void ) +{ +efi_status_t ret =EFI_SUCCESS ; +struct efi_boot_services *bt; + +if (!local_systable) { +efi_st_error ("systable not found\n" ); +return EFI_ST_FAILURE ; + } + + bt = local_systable->boottime; + +if (!bt) { +efi_st_error ("Cannot find boottime services structure\n" ); +return EFI_ST_FAILURE ; + } + + ret = bt->uninstall_multiple_protocol_interfaces + (efi_root, &efi_guid_firmware_management_protocol, + &efi_test_fmp,NULL ); + +if (ret !=EFI_SUCCESS ) { +efi_st_error ("Failed to uninstall FMP\n" ); +return EFI_ST_FAILURE ; + } + +return EFI_ST_SUCCESS ; +} + +static int execute (void ) +{ +struct efi_system_resource_table *esrt; +efi_status_t ret =EFI_SUCCESS ; +u32 base_entry_count; +u32 entry_delta; +struct efi_boot_services *bt; + +if (!local_systable) { +efi_st_error ("systable not found\n" ); +return EFI_ST_FAILURE ; + } + + bt = local_systable->boottime; + +if (!bt) { +efi_st_error ("Cannot find boottime services structure\n" ); +return EFI_ST_FAILURE ;
lib/efi_selftest/efi_selftest_esrt.c line 226
Quoted 10 lines
lib/efi_selftest/efi_selftest_esrt.c new file +10
-0+ } + + esrt =lib_test_get_esrt (); +if (!esrt) { +efi_st_error ("ESRT table not present\n" ); +return EFI_ST_FAILURE ; + } + base_entry_count = esrt->fw_resource_count; + + ret = bt->install_multiple_protocol_interfaces(&efi_root,
You should not access variables from outside efi_selftest here.
There is already an FMP installed on the root node. So InstallMultipleProtocolInterfaces fails:
lib/efi_selftest/efi_selftest_esrt.c(232):
ERROR: Failed to install FMP
Instead pass the address of a pointer for a handle which is intialized to zero and let InstallMultipleProtocolInterfaces create a new handle.
Best regards
Heinrich
Quoted 39 lines
lib/efi_selftest/efi_selftest_esrt.c new file +38
-0+ &efi_guid_firmware_management_protocol, + &efi_test_fmp, + NULL); + +if (ret !=EFI_SUCCESS ) { +efi_st_error ("Failed to install FMP\n" ); +return EFI_ST_FAILURE ; + } + + esrt =lib_test_get_esrt (); +if (!esrt) { +efi_st_error ("ESRT table not present\n" ); +return EFI_ST_FAILURE ; + } + + entry_delta = esrt->fw_resource_count - base_entry_count; +if (entry_delta !=TEST_ESRT_NUM_ENTRIES ) { +efi_st_error ("ESRT mismatch in new entry count (%d), expected (%d).\n" , + entry_delta,TEST_ESRT_NUM_ENTRIES ); +return EFI_ST_FAILURE ; + } + +for (u32 idx =0 ; idx <TEST_ESRT_NUM_ENTRIES ; idx++) +if (!lib_test_check_uuid_entry (esrt, &static_img_info[idx])) { +efi_st_error ("ESRT entry mistmatch\n" ); +return EFI_ST_FAILURE ; + } + +return EFI_ST_SUCCESS ; +} + +EFI_UNIT_TEST (esrt) = { + .name ="esrt" , + .phase =EFI_EXECUTE_BEFORE_BOOTTIME_EXIT , + .setup = setup, + .execute = execute, + .teardown = teardown, +};
PATCH v6 4/4 Test the ESRT creation within an existing pytest.
In reply to Jose MarinhoThis commit slightly extends 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 invocation:
sudo ./test/py/test.py --bd sandbox -k capsule_fw3 -l --build
--- test/py/tests/test_efi_capsule/test_capsule_firmware.py | 8 ++++++++ 1 file changed, 8 insertions(+)
test/py/tests/test_efi_capsule/test_capsule_firmware.py +8 -0
@@ -229,6 +229,14 @@ class TestEfiCapsuleFirmwareFit(object): output = u_boot_console.run_command( 'env print -e -all Capsule0000') + output = u_boot_console.run_command_list(['efidebug capsule esrt']) + + # ensure that EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID is in the ESRT. + assert 'AE13FF2D-9AD4-4E25-9AC8-6D80B3B22147' in ''.join(output) + + # ensure that EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID is in the ESRT. + 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
PATCH v6 4/4 Test the ESRT creation within an existing pytest.
Quoted 18 lines · Jose Marinho
This commit slightly extends
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=yThe 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 invocation:
sudo./test/py/test.py--bd sandbox -kcapsule_fw3-l --build
Quoted 8 lines
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.comSigned-off-by Jose Marinho <[email protected]>
Quoted 24 lines
--- test/py/tests/test_efi_capsule/test_capsule_firmware.py | 8 ++++++++ 1 file changed, 8 insertions(+)test/py/tests/test_efi_capsule/test_capsule_firmware.py +8
-0@@ -229,6 +229,14 @@ class TestEfiCapsuleFirmwareFit(object): output = u_boot_console.run_command( 'env print -e -all Capsule0000') + output = u_boot_console.run_command_list(['efidebug capsule esrt']) + + # ensure that EFI_FIRMWARE_IMAGE_TYPE_UBOOT_FIT_GUID is in the ESRT. + assert 'AE13FF2D-9AD4-4E25-9AC8-6D80B3B22147' in ''.join(output) + + # ensure that EFI_FIRMWARE_IMAGE_TYPE_UBOOT_RAW_GUID is in the ESRT. + 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])