Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 1 | /* 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 Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 29 | #include "VerifiedBoot.h" |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 30 | #include "libavb/libavb.h" |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 31 | #include <Library/BaseMemoryLib.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 32 | #include <Library/DebugLib.h> |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 33 | #include <Library/UefiBootServicesTableLib.h> |
| 34 | #include <Library/UefiRuntimeServicesTableLib.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 35 | #include <Protocol/EFIQseecom.h> |
| 36 | #include <Protocol/LoadedImage.h> |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 37 | #include <Protocol/scm_sip_interface.h> |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 38 | |
| 39 | typedef struct { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 40 | QCOM_QSEECOM_PROTOCOL *QseeComProtocol; |
| 41 | UINT32 AppId; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 42 | } 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 Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 51 | #define TZ_FVER_QSEE 10 /**< QSEE application layer. */ |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 52 | #ifdef FEATURE_SOTER |
| 53 | #define KEYMASTER_CMD_ID_SOTER 0x10000UL |
| 54 | #endif /* FEATURE_SOTER */ |
| 55 | |
| 56 | typedef enum { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 57 | /* |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 58 | * List the commands supportedin by the hardware. |
| 59 | */ |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 60 | 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 Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 81 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 82 | 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 Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 92 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 93 | 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 Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 97 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 98 | 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 Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 102 | |
| 103 | #ifdef FEATURE_SOTER |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 104 | 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 Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 109 | #endif /* FEATURE_SOTER */ |
| 110 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 111 | KEYMASTER_LAST_CMD_ENTRY = (int)0xFFFFFFFFULL |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 112 | } KeyMasterCmd; |
| 113 | |
| 114 | typedef struct { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 115 | UINT32 CmdId; |
| 116 | UINT32 RotOffset; |
| 117 | UINT32 RotSize; |
| 118 | CHAR8 RotDigest[AVB_SHA256_DIGEST_SIZE]; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 119 | } __attribute__ ((packed)) KMSetRotReq; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 120 | |
| 121 | typedef struct { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 122 | INT32 Status; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 123 | } __attribute__ ((packed)) KMSetRotRsp; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 124 | |
| 125 | typedef struct { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 126 | UINT32 IsUnlocked; |
| 127 | CHAR8 PublicKey[AVB_SHA256_DIGEST_SIZE]; |
| 128 | UINT32 Color; |
| 129 | UINT32 SystemVersion; |
| 130 | UINT32 SystemSecurityLevel; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 131 | } __attribute__ ((packed)) KMBootState; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 132 | |
| 133 | typedef struct { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 134 | UINT32 CmdId; |
| 135 | UINT32 Version; |
| 136 | UINT32 Offset; |
| 137 | UINT32 Size; |
| 138 | KMBootState BootState; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 139 | } __attribute__ ((packed)) KMSetBootStateReq; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 140 | |
| 141 | typedef struct { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 142 | INT32 Status; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 143 | } __attribute__ ((packed)) KMSetBootStateRsp; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 144 | |
| 145 | typedef struct { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 146 | UINT32 CmdId; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 147 | } __attribute__ ((packed)) KMGetVersionReq; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 148 | |
| 149 | typedef struct { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 150 | INT32 Status; |
| 151 | UINT32 Major; |
| 152 | UINT32 Minor; |
| 153 | UINT32 AppMajor; |
| 154 | UINT32 AppMinor; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 155 | } __attribute__ ((packed)) KMGetVersionRsp; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 156 | |
| 157 | EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 158 | KeyMasterStartApp (KMHandle *Handle) |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 159 | { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 160 | EFI_STATUS Status = EFI_SUCCESS; |
| 161 | KMGetVersionReq Req = {0}; |
| 162 | KMGetVersionRsp Rsp = {0}; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 163 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 164 | if (Handle == NULL) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 165 | DEBUG ((EFI_D_ERROR, "KeyMasterStartApp: Invalid Handle\n")); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 166 | return EFI_INVALID_PARAMETER; |
| 167 | } |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 168 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 169 | Status = gBS->LocateProtocol (&gQcomQseecomProtocolGuid, NULL, |
| 170 | (VOID **)&(Handle->QseeComProtocol)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 171 | if (Status != EFI_SUCCESS) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 172 | DEBUG ((EFI_D_ERROR, "Unable to locate QSEECom protocol: %r\n", Status)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 173 | return Status; |
| 174 | } |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 175 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 176 | Status = Handle->QseeComProtocol->QseecomStartApp ( |
| 177 | Handle->QseeComProtocol, "keymaster", &(Handle->AppId)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 178 | if (Status != EFI_SUCCESS) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 179 | DEBUG ((EFI_D_ERROR, |
| 180 | "KeyMasterStartApp: QseecomStartApp failed status: %r\n", Status)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 181 | return Status; |
| 182 | } |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 183 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 184 | DEBUG ((EFI_D_VERBOSE, "keymaster app id %d\n", Handle->AppId)); |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 185 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 186 | Req.CmdId = KEYMASTER_GET_VERSION; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 187 | Status = Handle->QseeComProtocol->QseecomSendCmd ( |
| 188 | Handle->QseeComProtocol, Handle->AppId, (UINT8 *)&Req, sizeof (Req), |
| 189 | (UINT8 *)&Rsp, sizeof (Rsp)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 190 | if (Status != EFI_SUCCESS || Rsp.Status != 0 || Rsp.Major < 2) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 191 | DEBUG ((EFI_D_ERROR, "KeyMasterStartApp: Get Version err, status: " |
| 192 | "%d, response status: %d, Major: %d\n", |
| 193 | Status, Rsp.Status, Rsp.Major)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 194 | return EFI_LOAD_ERROR; |
| 195 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 196 | DEBUG ((EFI_D_VERBOSE, "KeyMasterStartApp success AppId: 0x%x, Major: %d\n", |
| 197 | Handle->AppId, Rsp.Major)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 198 | return Status; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 202 | KeyMasterSetRotAndBootState (KMRotAndBootState *BootState) |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 203 | { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 204 | 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 Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 217 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 218 | if (BootState == NULL) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 219 | DEBUG ((EFI_D_ERROR, "Invalid parameter BootState\n")); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 220 | return EFI_INVALID_PARAMETER; |
| 221 | } |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 222 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 223 | /* Compute ROT digest */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 224 | avb_sha256_init (&RotCtx); |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 225 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 226 | switch (BootState->Color) { |
| 227 | case GREEN: |
| 228 | case YELLOW: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 229 | 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 Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 233 | break; |
| 234 | case ORANGE: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 235 | avb_sha256_update (&RotCtx, (const uint8_t *)&BootState->IsUnlocked, |
| 236 | sizeof (BootState->IsUnlocked)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 237 | break; |
| 238 | case RED: |
| 239 | default: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 240 | DEBUG ((EFI_D_ERROR, "Invalid state to boot!\n")); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 241 | return EFI_LOAD_ERROR; |
| 242 | } |
Shivaprasad Hongal | 4cd06ac | 2017-05-10 11:14:51 -0700 | [diff] [blame] | 243 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 244 | RotDigest = (CHAR8 *)avb_sha256_final (&RotCtx); |
Shivaprasad Hongal | 4cd06ac | 2017-05-10 11:14:51 -0700 | [diff] [blame] | 245 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 246 | /* Compute BootState digest */ |
| 247 | switch (BootState->Color) { |
| 248 | case GREEN: |
| 249 | case YELLOW: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 250 | 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 Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 254 | break; |
| 255 | case ORANGE: |
| 256 | BootStateDigest = BootStateOrgangeDigest; |
| 257 | break; |
| 258 | case RED: |
| 259 | default: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 260 | DEBUG ((EFI_D_ERROR, "Invalid state to boot!\n")); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 261 | return EFI_LOAD_ERROR; |
| 262 | } |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 263 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 264 | /* Load KeyMaster App */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 265 | GUARD (KeyMasterStartApp (&Handle)); |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 266 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 267 | /* Set ROT */ |
| 268 | RotReq.CmdId = KEYMASTER_SET_ROT; |
| 269 | RotReq.RotOffset = (UINT8 *)&RotReq.RotDigest - (UINT8 *)&RotReq; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 270 | RotReq.RotSize = sizeof (RotReq.RotDigest); |
| 271 | CopyMem (RotReq.RotDigest, RotDigest, AVB_SHA256_DIGEST_SIZE); |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 272 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 273 | Status = Handle.QseeComProtocol->QseecomSendCmd ( |
| 274 | Handle.QseeComProtocol, Handle.AppId, (UINT8 *)&RotReq, sizeof (RotReq), |
| 275 | (UINT8 *)&RotRsp, sizeof (RotRsp)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 276 | if (Status != EFI_SUCCESS || RotRsp.Status != 0) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 277 | DEBUG ((EFI_D_ERROR, "KeyMasterSendRotAndBootState: Set ROT err, " |
| 278 | "Status: %r, response status: %d\n", |
| 279 | Status, RotRsp.Status)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 280 | return EFI_LOAD_ERROR; |
| 281 | } |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 282 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 283 | /* Set Boot State */ |
| 284 | BootStateReq.CmdId = KEYMASTER_SET_BOOT_STATE; |
| 285 | BootStateReq.Version = 0; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 286 | BootStateReq.Size = sizeof (BootStateReq.BootState); |
| 287 | BootStateReq.Offset = |
| 288 | (UINT8 *)&BootStateReq.BootState - (UINT8 *)&BootStateReq; |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 289 | BootStateReq.BootState.Color = BootState->Color; |
| 290 | BootStateReq.BootState.IsUnlocked = BootState->IsUnlocked; |
| 291 | BootStateReq.BootState.SystemSecurityLevel = BootState->SystemSecurityLevel; |
| 292 | BootStateReq.BootState.SystemVersion = BootState->SystemVersion; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 293 | CopyMem (BootStateReq.BootState.PublicKey, BootStateDigest, |
| 294 | AVB_SHA256_DIGEST_SIZE); |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 295 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 296 | Status = Handle.QseeComProtocol->QseecomSendCmd ( |
| 297 | Handle.QseeComProtocol, Handle.AppId, (UINT8 *)&BootStateReq, |
| 298 | sizeof (BootStateReq), (UINT8 *)&BootStateRsp, sizeof (BootStateRsp)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 299 | if (Status != EFI_SUCCESS || BootStateRsp.Status != 0) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 300 | DEBUG ((EFI_D_ERROR, "KeyMasterSendRotAndBootState: Set BootState err, " |
| 301 | "Status: %r, response status: %d\n", |
| 302 | Status, BootStateRsp.Status)); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 303 | return EFI_LOAD_ERROR; |
| 304 | } |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 305 | |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 306 | /* Provide boot tamper state to TZ */ |
Zhen Kong | 8898d3d | 2017-10-17 17:34:04 -0700 | [diff] [blame] | 307 | if (((Status = IsSecureDevice (&secure_device)) == EFI_SUCCESS) && |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 308 | secure_device && (BootState->Color != GREEN)) { |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 309 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 310 | Status = ScmGetFeatureVersion (TZ_FVER_QSEE, &version); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 311 | if (Status != EFI_SUCCESS) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 312 | DEBUG ((EFI_D_ERROR, |
| 313 | "KeyMasterSetRotAndBootState: ScmGetFeatureVersion fails!\n")); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 314 | return Status; |
| 315 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 316 | if (AllowSetFuse (version)) { |
| 317 | Status = SetFuse (TZ_HLOS_IMG_TAMPER_FUSE); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 318 | if (Status != EFI_SUCCESS) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 319 | DEBUG ((EFI_D_ERROR, "KeyMasterSetRotAndBootState: " |
| 320 | "SetFuse (TZ_HLOS_IMG_TAMPER_FUSE) fails!\n")); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 321 | return Status; |
| 322 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 323 | Status = SetFuse (TZ_HLOS_TAMPER_NOTIFY_FUSE); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 324 | if (Status != EFI_SUCCESS) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 325 | DEBUG ((EFI_D_ERROR, "KeyMasterSetRotAndBootState: " |
| 326 | "SetFuse (TZ_HLOS_TAMPER_NOTIFY_FUSE) fails!\n")); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 327 | return Status; |
| 328 | } |
| 329 | } else { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 330 | 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 Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 334 | return Status; |
| 335 | } |
| 336 | } |
| 337 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 338 | DEBUG ((EFI_D_VERBOSE, "KeyMasterSetRotAndBootState success\n")); |
Zhen Kong | 8b93cb3 | 2017-10-02 17:09:08 -0700 | [diff] [blame] | 339 | return Status; |
Shivaprasad Hongal | 5f3c5c4 | 2017-04-27 17:20:44 -0700 | [diff] [blame] | 340 | } |