]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/Tpm2CommandLib/Tpm2Startup.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / SecurityPkg / Library / Tpm2CommandLib / Tpm2Startup.c
1 /** @file
2 Implement TPM2 Startup related command.
3
4 Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved. <BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <IndustryStandard/UefiTcgPlatform.h>
11 #include <Library/Tpm2CommandLib.h>
12 #include <Library/Tpm2DeviceLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/BaseLib.h>
15 #include <Library/DebugLib.h>
16
17 #pragma pack(1)
18
19 typedef struct {
20 TPM2_COMMAND_HEADER Header;
21 TPM_SU StartupType;
22 } TPM2_STARTUP_COMMAND;
23
24 typedef struct {
25 TPM2_RESPONSE_HEADER Header;
26 } TPM2_STARTUP_RESPONSE;
27
28 typedef struct {
29 TPM2_COMMAND_HEADER Header;
30 TPM_SU ShutdownType;
31 } TPM2_SHUTDOWN_COMMAND;
32
33 typedef struct {
34 TPM2_RESPONSE_HEADER Header;
35 } TPM2_SHUTDOWN_RESPONSE;
36
37 #pragma pack()
38
39 /**
40 Send Startup command to TPM2.
41
42 @param[in] StartupType TPM_SU_CLEAR or TPM_SU_STATE
43
44 @retval EFI_SUCCESS Operation completed successfully.
45 @retval EFI_DEVICE_ERROR Unexpected device behavior.
46 **/
47 EFI_STATUS
48 EFIAPI
49 Tpm2Startup (
50 IN TPM_SU StartupType
51 )
52 {
53 EFI_STATUS Status;
54 TPM2_STARTUP_COMMAND Cmd;
55 TPM2_STARTUP_RESPONSE Res;
56 UINT32 ResultBufSize;
57 TPM_RC ResponseCode;
58
59 Cmd.Header.tag = SwapBytes16 (TPM_ST_NO_SESSIONS);
60 Cmd.Header.paramSize = SwapBytes32 (sizeof (Cmd));
61 Cmd.Header.commandCode = SwapBytes32 (TPM_CC_Startup);
62 Cmd.StartupType = SwapBytes16 (StartupType);
63
64 ResultBufSize = sizeof (Res);
65 Status = Tpm2SubmitCommand (sizeof (Cmd), (UINT8 *)&Cmd, &ResultBufSize, (UINT8 *)&Res);
66 if (EFI_ERROR (Status)) {
67 return Status;
68 }
69
70 ResponseCode = SwapBytes32 (Res.Header.responseCode);
71 switch (ResponseCode) {
72 case TPM_RC_SUCCESS:
73 DEBUG ((DEBUG_INFO, "TPM2Startup: TPM_RC_SUCCESS\n"));
74 return EFI_SUCCESS;
75 case TPM_RC_INITIALIZE:
76 // TPM_RC_INITIALIZE can be returned if Tpm2Startup is not required.
77 DEBUG ((DEBUG_INFO, "TPM2Startup: TPM_RC_INITIALIZE\n"));
78 return EFI_SUCCESS;
79 default:
80 DEBUG ((DEBUG_ERROR, "Tpm2Startup: Response Code error! 0x%08x\r\n", ResponseCode));
81 return EFI_DEVICE_ERROR;
82 }
83 }
84
85 /**
86 Send Shutdown command to TPM2.
87
88 @param[in] ShutdownType TPM_SU_CLEAR or TPM_SU_STATE.
89
90 @retval EFI_SUCCESS Operation completed successfully.
91 @retval EFI_DEVICE_ERROR Unexpected device behavior.
92 **/
93 EFI_STATUS
94 EFIAPI
95 Tpm2Shutdown (
96 IN TPM_SU ShutdownType
97 )
98 {
99 EFI_STATUS Status;
100 TPM2_SHUTDOWN_COMMAND Cmd;
101 TPM2_SHUTDOWN_RESPONSE Res;
102 UINT32 ResultBufSize;
103
104 Cmd.Header.tag = SwapBytes16 (TPM_ST_NO_SESSIONS);
105 Cmd.Header.paramSize = SwapBytes32 (sizeof (Cmd));
106 Cmd.Header.commandCode = SwapBytes32 (TPM_CC_Shutdown);
107 Cmd.ShutdownType = SwapBytes16 (ShutdownType);
108
109 ResultBufSize = sizeof (Res);
110 Status = Tpm2SubmitCommand (sizeof (Cmd), (UINT8 *)&Cmd, &ResultBufSize, (UINT8 *)&Res);
111 if (EFI_ERROR (Status)) {
112 return Status;
113 }
114
115 if (SwapBytes32 (Res.Header.responseCode) != TPM_RC_SUCCESS) {
116 DEBUG ((DEBUG_ERROR, "Tpm2Shutdown: Response Code error! 0x%08x\r\n", SwapBytes32 (Res.Header.responseCode)));
117 return EFI_DEVICE_ERROR;
118 }
119
120 return EFI_SUCCESS;
121 }