]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/Tpm2CommandLib/Tpm2Startup.c
dc2c5628f9201caac3259a3a286b7beb8c912a1d
[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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <IndustryStandard/UefiTcgPlatform.h>
17 #include <Library/Tpm2CommandLib.h>
18 #include <Library/Tpm2DeviceLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/BaseLib.h>
21 #include <Library/DebugLib.h>
22
23 #pragma pack(1)
24
25 typedef struct {
26 TPM2_COMMAND_HEADER Header;
27 TPM_SU StartupType;
28 } TPM2_STARTUP_COMMAND;
29
30 typedef struct {
31 TPM2_RESPONSE_HEADER Header;
32 } TPM2_STARTUP_RESPONSE;
33
34 typedef struct {
35 TPM2_COMMAND_HEADER Header;
36 TPM_SU ShutdownType;
37 } TPM2_SHUTDOWN_COMMAND;
38
39 typedef struct {
40 TPM2_RESPONSE_HEADER Header;
41 } TPM2_SHUTDOWN_RESPONSE;
42
43 #pragma pack()
44
45 /**
46 Send Startup command to TPM2.
47
48 @param[in] StartupType TPM_SU_CLEAR or TPM_SU_STATE
49
50 @retval EFI_SUCCESS Operation completed successfully.
51 @retval EFI_DEVICE_ERROR Unexpected device behavior.
52 **/
53 EFI_STATUS
54 EFIAPI
55 Tpm2Startup (
56 IN TPM_SU StartupType
57 )
58 {
59 EFI_STATUS Status;
60 TPM2_STARTUP_COMMAND Cmd;
61 TPM2_STARTUP_RESPONSE Res;
62 UINT32 ResultBufSize;
63 TPM_RC ResponseCode;
64
65 Cmd.Header.tag = SwapBytes16(TPM_ST_NO_SESSIONS);
66 Cmd.Header.paramSize = SwapBytes32(sizeof(Cmd));
67 Cmd.Header.commandCode = SwapBytes32(TPM_CC_Startup);
68 Cmd.StartupType = SwapBytes16(StartupType);
69
70 ResultBufSize = sizeof(Res);
71 Status = Tpm2SubmitCommand (sizeof(Cmd), (UINT8 *)&Cmd, &ResultBufSize, (UINT8 *)&Res);
72 if (EFI_ERROR(Status)) {
73 return Status;
74 }
75
76 ResponseCode = SwapBytes32(Res.Header.responseCode);
77 switch (ResponseCode) {
78 case TPM_RC_SUCCESS:
79 DEBUG ((DEBUG_INFO, "TPM2Startup: TPM_RC_SUCCESS\n"));
80 return EFI_SUCCESS;
81 case TPM_RC_INITIALIZE:
82 // TPM_RC_INITIALIZE can be returned if Tpm2Startup is not required.
83 DEBUG ((DEBUG_INFO, "TPM2Startup: TPM_RC_INITIALIZE\n"));
84 return EFI_SUCCESS;
85 default:
86 DEBUG ((EFI_D_ERROR, "Tpm2Startup: Response Code error! 0x%08x\r\n", ResponseCode));
87 return EFI_DEVICE_ERROR;
88 }
89 }
90
91 /**
92 Send Shutdown command to TPM2.
93
94 @param[in] ShutdownType TPM_SU_CLEAR or TPM_SU_STATE.
95
96 @retval EFI_SUCCESS Operation completed successfully.
97 @retval EFI_DEVICE_ERROR Unexpected device behavior.
98 **/
99 EFI_STATUS
100 EFIAPI
101 Tpm2Shutdown (
102 IN TPM_SU ShutdownType
103 )
104 {
105 EFI_STATUS Status;
106 TPM2_SHUTDOWN_COMMAND Cmd;
107 TPM2_SHUTDOWN_RESPONSE Res;
108 UINT32 ResultBufSize;
109
110 Cmd.Header.tag = SwapBytes16(TPM_ST_NO_SESSIONS);
111 Cmd.Header.paramSize = SwapBytes32(sizeof(Cmd));
112 Cmd.Header.commandCode = SwapBytes32(TPM_CC_Shutdown);
113 Cmd.ShutdownType = SwapBytes16(ShutdownType);
114
115 ResultBufSize = sizeof(Res);
116 Status = Tpm2SubmitCommand (sizeof(Cmd), (UINT8 *)&Cmd, &ResultBufSize, (UINT8 *)&Res);
117 if (EFI_ERROR(Status)) {
118 return Status;
119 }
120
121 if (SwapBytes32(Res.Header.responseCode) != TPM_RC_SUCCESS) {
122 DEBUG ((EFI_D_ERROR, "Tpm2Shutdown: Response Code error! 0x%08x\r\n", SwapBytes32(Res.Header.responseCode)));
123 return EFI_DEVICE_ERROR;
124 }
125
126 return EFI_SUCCESS;
127 }