blob: 36c976cd9eb1baab9dff303baa607a4a2c718659 [file] [log] [blame]
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -07001/* Copyright (c) 2017, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28#include "KeymasterClient.h"
Jeevan Shriram17f173d2017-10-24 22:11:07 -070029#include "VerifiedBoot.h"
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070030#include "libavb/libavb.h"
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070031#include <Library/BaseMemoryLib.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070032#include <Library/DebugLib.h>
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070033#include <Library/UefiBootServicesTableLib.h>
34#include <Library/UefiRuntimeServicesTableLib.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070035#include <Protocol/EFIQseecom.h>
36#include <Protocol/LoadedImage.h>
Zhen Kong8b93cb32017-10-02 17:09:08 -070037#include <Protocol/scm_sip_interface.h>
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070038
39typedef struct {
Zhen Kong8b93cb32017-10-02 17:09:08 -070040 QCOM_QSEECOM_PROTOCOL *QseeComProtocol;
41 UINT32 AppId;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070042} KMHandle;
43
44/**
45 * KM Commands supported
46 */
47#define KEYMASTER_CMD_ID_OLD 0UL
48#define KEYMASTER_CMD_ID 0x100UL
49#define KEYMASTER_UTILS_CMD_ID 0x200UL
50#define GK_CMD_ID 0x1000UL
Zhen Kong8b93cb32017-10-02 17:09:08 -070051#define TZ_FVER_QSEE 10 /**< QSEE application layer. */
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070052#ifdef FEATURE_SOTER
53#define KEYMASTER_CMD_ID_SOTER 0x10000UL
54#endif /* FEATURE_SOTER */
55
56typedef enum {
Zhen Kong8b93cb32017-10-02 17:09:08 -070057 /*
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070058 * List the commands supportedin by the hardware.
59 */
Zhen Kong8b93cb32017-10-02 17:09:08 -070060 KEYMASTER_GET_SUPPORTED_ALGORITHMS = (KEYMASTER_CMD_ID + 1UL),
61 KEYMASTER_GET_SUPPORTED_BLOCK_MODES = (KEYMASTER_CMD_ID + 2UL),
62 KEYMASTER_GET_SUPPORTED_PADDING_MODES = (KEYMASTER_CMD_ID + 3UL),
63 KEYMASTER_GET_SUPPORTED_DIGESTS = (KEYMASTER_CMD_ID + 4UL),
64 KEYMASTER_GET_SUPPORTED_IMPORT_FORMATS = (KEYMASTER_CMD_ID + 5UL),
65 KEYMASTER_GET_SUPPORTED_EXPORT_FORMATS = (KEYMASTER_CMD_ID + 6UL),
66 KEYMASTER_ADD_RNG_ENTROPY = (KEYMASTER_CMD_ID + 7UL),
67 KEYMASTER_GENERATE_KEY = (KEYMASTER_CMD_ID + 8UL),
68 KEYMASTER_GET_KEY_CHARACTERISTICS = (KEYMASTER_CMD_ID + 9UL),
69 KEYMASTER_RESCOPE = (KEYMASTER_CMD_ID + 10UL),
70 KEYMASTER_IMPORT_KEY = (KEYMASTER_CMD_ID + 11UL),
71 KEYMASTER_EXPORT_KEY = (KEYMASTER_CMD_ID + 12UL),
72 KEYMASTER_DELETE_KEY = (KEYMASTER_CMD_ID + 13UL),
73 KEYMASTER_DELETE_ALL_KEYS = (KEYMASTER_CMD_ID + 14UL),
74 KEYMASTER_BEGIN = (KEYMASTER_CMD_ID + 15UL),
75 KEYMASTER_UPDATE = (KEYMASTER_CMD_ID + 17UL),
76 KEYMASTER_FINISH = (KEYMASTER_CMD_ID + 18UL),
77 KEYMASTER_ABORT = (KEYMASTER_CMD_ID + 19UL),
78 KEYMASTER_UPGRADE = (KEYMASTER_CMD_ID + 20UL),
79 KEYMASTER_ATTEST = (KEYMASTER_CMD_ID + 21UL),
80 KEYMASTER_CONFIGURE = (KEYMASTER_CMD_ID + 22UL),
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070081
Zhen Kong8b93cb32017-10-02 17:09:08 -070082 KEYMASTER_GET_VERSION = (KEYMASTER_UTILS_CMD_ID + 0UL),
83 KEYMASTER_SET_ROT = (KEYMASTER_UTILS_CMD_ID + 1UL),
84 KEYMASTER_READ_KM_DEVICE_STATE = (KEYMASTER_UTILS_CMD_ID + 2UL),
85 KEYMASTER_WRITE_KM_DEVICE_STATE = (KEYMASTER_UTILS_CMD_ID + 3UL),
86 KEYMASTER_MILESTONE_CALL = (KEYMASTER_UTILS_CMD_ID + 4UL),
87 KEYMASTER_GET_AUTH_TOKEN_KEY = (KEYMASTER_UTILS_CMD_ID + 5UL),
88 KEYMASTER_SECURE_WRITE_PROTECT = (KEYMASTER_UTILS_CMD_ID + 6UL),
89 KEYMASTER_SET_VERSION = (KEYMASTER_UTILS_CMD_ID + 7UL),
90 KEYMASTER_SET_BOOT_STATE = (KEYMASTER_UTILS_CMD_ID + 8UL),
91 KEYMASTER_PROVISION_ATTEST_KEY = (KEYMASTER_UTILS_CMD_ID + 9UL),
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070092
Zhen Kong8b93cb32017-10-02 17:09:08 -070093 GK_ENROLL = (GK_CMD_ID + 1UL),
94 GK_VERIFY = (GK_CMD_ID + 2UL),
95 GK_DELETE_USER = (GK_CMD_ID + 3UL),
96 GK_DELETE_ALL_USERS = (GK_CMD_ID + 4UL),
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -070097
Zhen Kong8b93cb32017-10-02 17:09:08 -070098 KEYMASTER_GENERATE_KEY_OLD = (KEYMASTER_CMD_ID_OLD + 1UL),
99 KEYMASTER_IMPORT_KEY_OLD = (KEYMASTER_CMD_ID_OLD + 2UL),
100 KEYMASTER_SIGN_DATA_OLD = (KEYMASTER_CMD_ID_OLD + 3UL),
101 KEYMASTER_VERIFY_DATA_OLD = (KEYMASTER_CMD_ID_OLD + 4UL),
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700102
103#ifdef FEATURE_SOTER
Zhen Kong8b93cb32017-10-02 17:09:08 -0700104 KEYMASTER_SOTER_GENERATE_ATTK = (KEYMASTER_CMD_ID_SOTER + 1UL),
105 KEYMASTER_SOTER_VERIFY_ATTK = (KEYMASTER_CMD_ID_SOTER + 2UL),
106 KEYMASTER_SOTER_EXPORT_ATTK_PUBLIC = (KEYMASTER_CMD_ID_SOTER + 3UL),
107 KEYMASTER_SOTER_GET_DEVICE_ID = (KEYMASTER_CMD_ID_SOTER + 4UL),
108 KEYMASTER_SOTER_EXPORT_SOTER = (KEYMASTER_CMD_ID_SOTER + 5UL),
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700109#endif /* FEATURE_SOTER */
110
Zhen Kong8b93cb32017-10-02 17:09:08 -0700111 KEYMASTER_LAST_CMD_ENTRY = (int)0xFFFFFFFFULL
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700112} KeyMasterCmd;
113
114typedef struct {
Zhen Kong8b93cb32017-10-02 17:09:08 -0700115 UINT32 CmdId;
116 UINT32 RotOffset;
117 UINT32 RotSize;
118 CHAR8 RotDigest[AVB_SHA256_DIGEST_SIZE];
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700119} __attribute__ ((packed)) KMSetRotReq;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700120
121typedef struct {
Zhen Kong8b93cb32017-10-02 17:09:08 -0700122 INT32 Status;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700123} __attribute__ ((packed)) KMSetRotRsp;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700124
125typedef struct {
Zhen Kong8b93cb32017-10-02 17:09:08 -0700126 UINT32 IsUnlocked;
127 CHAR8 PublicKey[AVB_SHA256_DIGEST_SIZE];
128 UINT32 Color;
129 UINT32 SystemVersion;
130 UINT32 SystemSecurityLevel;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700131} __attribute__ ((packed)) KMBootState;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700132
133typedef struct {
Zhen Kong8b93cb32017-10-02 17:09:08 -0700134 UINT32 CmdId;
135 UINT32 Version;
136 UINT32 Offset;
137 UINT32 Size;
138 KMBootState BootState;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700139} __attribute__ ((packed)) KMSetBootStateReq;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700140
141typedef struct {
Zhen Kong8b93cb32017-10-02 17:09:08 -0700142 INT32 Status;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700143} __attribute__ ((packed)) KMSetBootStateRsp;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700144
145typedef struct {
Zhen Kong8b93cb32017-10-02 17:09:08 -0700146 UINT32 CmdId;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700147} __attribute__ ((packed)) KMGetVersionReq;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700148
149typedef struct {
Zhen Kong8b93cb32017-10-02 17:09:08 -0700150 INT32 Status;
151 UINT32 Major;
152 UINT32 Minor;
153 UINT32 AppMajor;
154 UINT32 AppMinor;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700155} __attribute__ ((packed)) KMGetVersionRsp;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700156
157EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700158KeyMasterStartApp (KMHandle *Handle)
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700159{
Zhen Kong8b93cb32017-10-02 17:09:08 -0700160 EFI_STATUS Status = EFI_SUCCESS;
161 KMGetVersionReq Req = {0};
162 KMGetVersionRsp Rsp = {0};
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700163
Zhen Kong8b93cb32017-10-02 17:09:08 -0700164 if (Handle == NULL) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700165 DEBUG ((EFI_D_ERROR, "KeyMasterStartApp: Invalid Handle\n"));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700166 return EFI_INVALID_PARAMETER;
167 }
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700168
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700169 Status = gBS->LocateProtocol (&gQcomQseecomProtocolGuid, NULL,
170 (VOID **)&(Handle->QseeComProtocol));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700171 if (Status != EFI_SUCCESS) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700172 DEBUG ((EFI_D_ERROR, "Unable to locate QSEECom protocol: %r\n", Status));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700173 return Status;
174 }
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700175
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700176 Status = Handle->QseeComProtocol->QseecomStartApp (
177 Handle->QseeComProtocol, "keymaster", &(Handle->AppId));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700178 if (Status != EFI_SUCCESS) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700179 DEBUG ((EFI_D_ERROR,
180 "KeyMasterStartApp: QseecomStartApp failed status: %r\n", Status));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700181 return Status;
182 }
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700183
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700184 DEBUG ((EFI_D_VERBOSE, "keymaster app id %d\n", Handle->AppId));
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700185
Zhen Kong8b93cb32017-10-02 17:09:08 -0700186 Req.CmdId = KEYMASTER_GET_VERSION;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700187 Status = Handle->QseeComProtocol->QseecomSendCmd (
188 Handle->QseeComProtocol, Handle->AppId, (UINT8 *)&Req, sizeof (Req),
189 (UINT8 *)&Rsp, sizeof (Rsp));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700190 if (Status != EFI_SUCCESS || Rsp.Status != 0 || Rsp.Major < 2) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700191 DEBUG ((EFI_D_ERROR, "KeyMasterStartApp: Get Version err, status: "
192 "%d, response status: %d, Major: %d\n",
193 Status, Rsp.Status, Rsp.Major));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700194 return EFI_LOAD_ERROR;
195 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700196 DEBUG ((EFI_D_VERBOSE, "KeyMasterStartApp success AppId: 0x%x, Major: %d\n",
197 Handle->AppId, Rsp.Major));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700198 return Status;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700199}
200
201EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700202KeyMasterSetRotAndBootState (KMRotAndBootState *BootState)
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700203{
Zhen Kong8b93cb32017-10-02 17:09:08 -0700204 EFI_STATUS Status = EFI_SUCCESS;
205 CHAR8 *RotDigest = NULL;
206 CHAR8 *BootStateDigest = NULL;
207 CHAR8 BootStateOrgangeDigest[AVB_SHA256_DIGEST_SIZE] = {0};
208 AvbSHA256Ctx RotCtx;
209 AvbSHA256Ctx BootStateCtx;
210 KMHandle Handle = {NULL};
211 KMSetRotReq RotReq = {0};
212 KMSetRotRsp RotRsp = {0};
213 KMSetBootStateReq BootStateReq = {0};
214 KMSetBootStateRsp BootStateRsp = {0};
215 BOOLEAN secure_device = FALSE;
216 UINT32 version = 0;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700217
Zhen Kong8b93cb32017-10-02 17:09:08 -0700218 if (BootState == NULL) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700219 DEBUG ((EFI_D_ERROR, "Invalid parameter BootState\n"));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700220 return EFI_INVALID_PARAMETER;
221 }
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700222
Zhen Kong8b93cb32017-10-02 17:09:08 -0700223 /* Compute ROT digest */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700224 avb_sha256_init (&RotCtx);
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700225
Zhen Kong8b93cb32017-10-02 17:09:08 -0700226 switch (BootState->Color) {
227 case GREEN:
228 case YELLOW:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700229 avb_sha256_update (&RotCtx, (const uint8_t *)BootState->PublicKey,
230 BootState->PublicKeyLength);
231 avb_sha256_update (&RotCtx, (const uint8_t *)&BootState->IsUnlocked,
232 sizeof (BootState->IsUnlocked));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700233 break;
234 case ORANGE:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700235 avb_sha256_update (&RotCtx, (const uint8_t *)&BootState->IsUnlocked,
236 sizeof (BootState->IsUnlocked));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700237 break;
238 case RED:
239 default:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700240 DEBUG ((EFI_D_ERROR, "Invalid state to boot!\n"));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700241 return EFI_LOAD_ERROR;
242 }
Shivaprasad Hongal4cd06ac2017-05-10 11:14:51 -0700243
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700244 RotDigest = (CHAR8 *)avb_sha256_final (&RotCtx);
Shivaprasad Hongal4cd06ac2017-05-10 11:14:51 -0700245
Zhen Kong8b93cb32017-10-02 17:09:08 -0700246 /* Compute BootState digest */
247 switch (BootState->Color) {
248 case GREEN:
249 case YELLOW:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700250 avb_sha256_init (&BootStateCtx);
251 avb_sha256_update (&BootStateCtx, (const uint8_t *)BootState->PublicKey,
252 BootState->PublicKeyLength);
253 BootStateDigest = (CHAR8 *)avb_sha256_final (&BootStateCtx);
Zhen Kong8b93cb32017-10-02 17:09:08 -0700254 break;
255 case ORANGE:
256 BootStateDigest = BootStateOrgangeDigest;
257 break;
258 case RED:
259 default:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700260 DEBUG ((EFI_D_ERROR, "Invalid state to boot!\n"));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700261 return EFI_LOAD_ERROR;
262 }
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700263
Zhen Kong8b93cb32017-10-02 17:09:08 -0700264 /* Load KeyMaster App */
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700265 GUARD (KeyMasterStartApp (&Handle));
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700266
Zhen Kong8b93cb32017-10-02 17:09:08 -0700267 /* Set ROT */
268 RotReq.CmdId = KEYMASTER_SET_ROT;
269 RotReq.RotOffset = (UINT8 *)&RotReq.RotDigest - (UINT8 *)&RotReq;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700270 RotReq.RotSize = sizeof (RotReq.RotDigest);
271 CopyMem (RotReq.RotDigest, RotDigest, AVB_SHA256_DIGEST_SIZE);
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700272
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700273 Status = Handle.QseeComProtocol->QseecomSendCmd (
274 Handle.QseeComProtocol, Handle.AppId, (UINT8 *)&RotReq, sizeof (RotReq),
275 (UINT8 *)&RotRsp, sizeof (RotRsp));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700276 if (Status != EFI_SUCCESS || RotRsp.Status != 0) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700277 DEBUG ((EFI_D_ERROR, "KeyMasterSendRotAndBootState: Set ROT err, "
278 "Status: %r, response status: %d\n",
279 Status, RotRsp.Status));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700280 return EFI_LOAD_ERROR;
281 }
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700282
Zhen Kong8b93cb32017-10-02 17:09:08 -0700283 /* Set Boot State */
284 BootStateReq.CmdId = KEYMASTER_SET_BOOT_STATE;
285 BootStateReq.Version = 0;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700286 BootStateReq.Size = sizeof (BootStateReq.BootState);
287 BootStateReq.Offset =
288 (UINT8 *)&BootStateReq.BootState - (UINT8 *)&BootStateReq;
Zhen Kong8b93cb32017-10-02 17:09:08 -0700289 BootStateReq.BootState.Color = BootState->Color;
290 BootStateReq.BootState.IsUnlocked = BootState->IsUnlocked;
291 BootStateReq.BootState.SystemSecurityLevel = BootState->SystemSecurityLevel;
292 BootStateReq.BootState.SystemVersion = BootState->SystemVersion;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700293 CopyMem (BootStateReq.BootState.PublicKey, BootStateDigest,
294 AVB_SHA256_DIGEST_SIZE);
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700295
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700296 Status = Handle.QseeComProtocol->QseecomSendCmd (
297 Handle.QseeComProtocol, Handle.AppId, (UINT8 *)&BootStateReq,
298 sizeof (BootStateReq), (UINT8 *)&BootStateRsp, sizeof (BootStateRsp));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700299 if (Status != EFI_SUCCESS || BootStateRsp.Status != 0) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700300 DEBUG ((EFI_D_ERROR, "KeyMasterSendRotAndBootState: Set BootState err, "
301 "Status: %r, response status: %d\n",
302 Status, BootStateRsp.Status));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700303 return EFI_LOAD_ERROR;
304 }
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700305
Zhen Kong8b93cb32017-10-02 17:09:08 -0700306 /* Provide boot tamper state to TZ */
Zhen Kong8898d3d2017-10-17 17:34:04 -0700307 if (((Status = IsSecureDevice (&secure_device)) == EFI_SUCCESS) &&
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700308 secure_device && (BootState->Color != GREEN)) {
Zhen Kong8b93cb32017-10-02 17:09:08 -0700309
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700310 Status = ScmGetFeatureVersion (TZ_FVER_QSEE, &version);
Zhen Kong8b93cb32017-10-02 17:09:08 -0700311 if (Status != EFI_SUCCESS) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700312 DEBUG ((EFI_D_ERROR,
313 "KeyMasterSetRotAndBootState: ScmGetFeatureVersion fails!\n"));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700314 return Status;
315 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700316 if (AllowSetFuse (version)) {
317 Status = SetFuse (TZ_HLOS_IMG_TAMPER_FUSE);
Zhen Kong8b93cb32017-10-02 17:09:08 -0700318 if (Status != EFI_SUCCESS) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700319 DEBUG ((EFI_D_ERROR, "KeyMasterSetRotAndBootState: "
320 "SetFuse (TZ_HLOS_IMG_TAMPER_FUSE) fails!\n"));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700321 return Status;
322 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700323 Status = SetFuse (TZ_HLOS_TAMPER_NOTIFY_FUSE);
Zhen Kong8b93cb32017-10-02 17:09:08 -0700324 if (Status != EFI_SUCCESS) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700325 DEBUG ((EFI_D_ERROR, "KeyMasterSetRotAndBootState: "
326 "SetFuse (TZ_HLOS_TAMPER_NOTIFY_FUSE) fails!\n"));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700327 return Status;
328 }
329 } else {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700330 DEBUG ((EFI_D_ERROR, "TZ didn't support this feature! "
331 "Version: major = %d, minor = %d, patch = %d\n",
332 (version >> 22) & 0x3FF, (version >> 12) & 0x3FF,
333 version & 0x3FF));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700334 return Status;
335 }
336 }
337
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700338 DEBUG ((EFI_D_VERBOSE, "KeyMasterSetRotAndBootState success\n"));
Zhen Kong8b93cb32017-10-02 17:09:08 -0700339 return Status;
Shivaprasad Hongal5f3c5c42017-04-27 17:20:44 -0700340}