Merge tag 'u-boot-atmel-fixes-2021.01-b' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / lib / efi_selftest / efi_selftest_tcg2.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * efi_selftest_devicepath
4  *
5  * Copyright (c) 2020 Heinrich Schuchardt <xypron.glpk@gmx.de>
6  *
7  * Test the EFI_TCG2_PROTOCOL
8  */
9
10 #include <efi_selftest.h>
11 #include <efi_tcg2.h>
12
13 static struct efi_boot_services *boottime;
14 static const efi_guid_t guid_tcg2 = EFI_TCG2_PROTOCOL_GUID;
15
16 /**
17  * efi_st_tcg2_setup() - setup test
18  *
19  * @handle:     handle of the loaded image
20  * @systable:   system table
21  * @return:     status code
22  */
23 static int efi_st_tcg2_setup(const efi_handle_t img_handle,
24                              const struct efi_system_table *systable)
25 {
26         boottime = systable->boottime;
27
28         return EFI_ST_SUCCESS;
29 }
30
31 /**
32  * efi_st_tcg2_execute() - execute test
33  *
34  * Call the GetCapability service of the EFI_TCG2_PROTOCOL.
35  *
36  * Return:      status code
37  */
38 static int efi_st_tcg2_execute(void)
39 {
40         struct efi_tcg2_protocol *tcg2;
41         struct efi_tcg2_boot_service_capability capability;
42         efi_status_t ret;
43
44         ret = boottime->locate_protocol(&guid_tcg2, NULL, (void **)&tcg2);
45         if (ret != EFI_SUCCESS) {
46                 efi_st_error("TCG2 protocol is not available.\n");
47                 return EFI_ST_FAILURE;
48         }
49         capability.size = sizeof(struct efi_tcg2_boot_service_capability) - 1;
50         ret = tcg2->get_capability(tcg2, &capability);
51         if (ret != EFI_BUFFER_TOO_SMALL) {
52                 efi_st_error("tcg2->get_capability on small buffer failed\n");
53                 return EFI_ST_FAILURE;
54         }
55         capability.size = sizeof(struct efi_tcg2_boot_service_capability);
56         ret = tcg2->get_capability(tcg2, &capability);
57         if (ret != EFI_SUCCESS) {
58                 efi_st_error("tcg2->get_capability failed\n");
59                 return EFI_ST_FAILURE;
60         }
61         if (!capability.tpm_present_flag) {
62                 efi_st_error("TPM not present\n");
63                 return EFI_ST_FAILURE;
64         }
65         efi_st_printf("TPM supports 0x%.8x event logs\n",
66                       capability.supported_event_logs);
67         return EFI_ST_SUCCESS;
68 }
69
70 EFI_UNIT_TEST(tcg2) = {
71         .name = "tcg2",
72         .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
73         .execute = efi_st_tcg2_execute,
74         .setup = efi_st_tcg2_setup,
75 };