Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -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 | |
| 29 | #include "VerifiedBoot.h" |
| 30 | #include "BootLinux.h" |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 31 | #include "KeymasterClient.h" |
| 32 | #include "libavb/libavb.h" |
Shivaprasad Hongal | f1fb047 | 2017-05-23 20:05:16 -0700 | [diff] [blame] | 33 | #include <Library/MenuKeysDetection.h> |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 34 | #include <Library/VerifiedBootMenu.h> |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 35 | |
| 36 | STATIC CONST CHAR8 *VerityMode = " androidboot.veritymode="; |
| 37 | STATIC CONST CHAR8 *VerifiedState = " androidboot.verifiedbootstate="; |
| 38 | STATIC CONST CHAR8 *KeymasterLoadState = " androidboot.keymaster=1"; |
| 39 | STATIC CONST CHAR8 *Space = " "; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 40 | |
| 41 | STATIC struct verified_boot_verity_mode VbVm[] = { |
| 42 | {FALSE, "logging"}, |
| 43 | {TRUE, "enforcing"}, |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 46 | STATIC struct verified_boot_state_name VbSn[] = { |
| 47 | {GREEN, "green"}, |
| 48 | {ORANGE, "orange"}, |
| 49 | {YELLOW, "yellow"}, |
| 50 | {RED, "red"}, |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 53 | struct boolean_string { |
| 54 | BOOLEAN value; |
| 55 | CHAR8 *name; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 58 | STATIC struct boolean_string BooleanString[] = { |
| 59 | {FALSE, "false"}, |
| 60 | {TRUE, "true"} |
| 61 | }; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 62 | |
| 63 | typedef struct { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 64 | AvbOps *Ops; |
| 65 | AvbSlotVerifyData *SlotData; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 66 | } VB2Data; |
| 67 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 68 | UINT32 |
| 69 | GetAVBVersion () |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 70 | { |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 71 | #if VERIFIED_BOOT_2 |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 72 | return 2; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 73 | #elif VERIFIED_BOOT |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 74 | return 1; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 75 | #else |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 76 | return 0; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 77 | #endif |
| 78 | } |
| 79 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 80 | BOOLEAN |
| 81 | VerifiedBootEnbled () |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 82 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 83 | return (GetAVBVersion () > NO_AVB); |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 86 | STATIC EFI_STATUS |
| 87 | AppendVBCmdLine (BootInfo *Info, CONST CHAR8 *Src) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 88 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 89 | EFI_STATUS Status = EFI_SUCCESS; |
| 90 | INT32 SrcLen = AsciiStrLen (Src); |
| 91 | CHAR8 *Dst = Info->VBCmdLine + Info->VBCmdLineFilledLen; |
| 92 | INT32 DstLen = Info->VBCmdLineLen - Info->VBCmdLineFilledLen; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 93 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 94 | GUARD (AsciiStrnCatS (Dst, DstLen, Src, SrcLen)); |
| 95 | Info->VBCmdLineFilledLen += SrcLen; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 96 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 97 | return EFI_SUCCESS; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 100 | STATIC EFI_STATUS |
| 101 | AppendVBCommonCmdLine (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 102 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 103 | EFI_STATUS Status = EFI_SUCCESS; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 104 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 105 | if (Info->VbIntf->Revision >= QCOM_VERIFIEDBOOT_PROTOCOL_REVISION) { |
| 106 | GUARD (AppendVBCmdLine (Info, VerifiedState)); |
| 107 | GUARD (AppendVBCmdLine (Info, VbSn[Info->BootState].name)); |
| 108 | } |
| 109 | GUARD (AppendVBCmdLine (Info, KeymasterLoadState)); |
| 110 | GUARD (AppendVBCmdLine (Info, Space)); |
| 111 | return EFI_SUCCESS; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 114 | STATIC EFI_STATUS |
| 115 | VBCommonInit (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 116 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 117 | EFI_STATUS Status = EFI_SUCCESS; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 118 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 119 | /* AVB 2.0 requires multi slot */ |
| 120 | if (GetAVBVersion () >= AVB_2 && !Info->MultiSlotBoot) { |
| 121 | DEBUG ((EFI_D_ERROR, "AVB requires multislot support!\n")); |
| 122 | return EFI_LOAD_ERROR; |
| 123 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 124 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 125 | Info->BootState = RED; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 126 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 127 | Status = gBS->LocateProtocol (&gEfiQcomVerifiedBootProtocolGuid, NULL, |
| 128 | (VOID **)&(Info->VbIntf)); |
| 129 | if (Status != EFI_SUCCESS) { |
| 130 | DEBUG ((EFI_D_ERROR, "Unable to locate VB protocol: %r\n", Status)); |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 131 | return Status; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 132 | } |
| 133 | |
| 134 | return Status; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 137 | STATIC EFI_STATUS |
| 138 | VBAllocateCmdLine (BootInfo *Info) |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 139 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 140 | EFI_STATUS Status = EFI_SUCCESS; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 141 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 142 | /* allocate VB command line*/ |
| 143 | Info->VBCmdLine = AllocatePool (DTB_PAD_SIZE); |
| 144 | if (Info->VBCmdLine == NULL) { |
| 145 | DEBUG ((EFI_D_ERROR, "VB CmdLine allocation failed!\n")); |
| 146 | Status = EFI_OUT_OF_RESOURCES; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 147 | return Status; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 148 | } |
| 149 | Info->VBCmdLineLen = DTB_PAD_SIZE; |
| 150 | Info->VBCmdLineFilledLen = 0; |
| 151 | Info->VBCmdLine[Info->VBCmdLineFilledLen] = '\0'; |
| 152 | |
| 153 | return Status; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 156 | STATIC EFI_STATUS |
| 157 | LoadImageNoAuth (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 158 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 159 | EFI_STATUS Status = EFI_SUCCESS; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 160 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 161 | if (Info->Images[0].ImageBuffer != NULL && Info->Images[0].ImageSize > 0) { |
| 162 | /* fastboot boot option image already loaded */ |
| 163 | return Status; |
| 164 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 165 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 166 | Status = LoadImage (Info->Pname, (VOID **)&(Info->Images[0].ImageBuffer), |
| 167 | (UINT32 *)&(Info->Images[0].ImageSize)); |
| 168 | if (Status != EFI_SUCCESS) { |
| 169 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to load image from partition: %r\n", |
| 170 | Status)); |
| 171 | return EFI_LOAD_ERROR; |
| 172 | } |
| 173 | Info->NumLoadedImages = 1; |
| 174 | Info->Images[0].Name = AllocatePool (StrLen (Info->Pname) + 1); |
| 175 | UnicodeStrToAsciiStr (Info->Pname, Info->Images[0].Name); |
| 176 | return Status; |
| 177 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 178 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 179 | STATIC EFI_STATUS |
| 180 | LoadImageNoAuthWrapper (BootInfo *Info) |
| 181 | { |
| 182 | EFI_STATUS Status = EFI_SUCCESS; |
| 183 | CHAR8 *SystemPath = NULL; |
| 184 | UINT32 SystemPathLen = 0; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 185 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 186 | GUARD (VBAllocateCmdLine (Info)); |
| 187 | GUARD (LoadImageNoAuth (Info)); |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 188 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 189 | if (!IsBootDevImage ()) { |
| 190 | SystemPathLen = GetSystemPath (&SystemPath); |
| 191 | if (SystemPathLen == 0 || SystemPath == NULL) { |
| 192 | DEBUG ((EFI_D_ERROR, "GetSystemPath failed!\n")); |
| 193 | return EFI_LOAD_ERROR; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 194 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 195 | GUARD (AppendVBCmdLine (Info, SystemPath)); |
| 196 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 197 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 198 | return Status; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 201 | STATIC EFI_STATUS |
| 202 | LoadImageAndAuthVB1 (BootInfo *Info) |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 203 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 204 | EFI_STATUS Status = EFI_SUCCESS; |
| 205 | CHAR8 StrPnameAscii[MAX_GPT_NAME_SIZE]; /* partition name starting with |
| 206 | / and no suffix */ |
| 207 | CHAR8 PnameAscii[MAX_GPT_NAME_SIZE]; |
| 208 | CHAR8 *SystemPath = NULL; |
| 209 | UINT32 SystemPathLen = 0; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 210 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 211 | GUARD (VBCommonInit (Info)); |
| 212 | GUARD (VBAllocateCmdLine (Info)); |
| 213 | GUARD (LoadImageNoAuth (Info)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 214 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 215 | device_info_vb_t DevInfo_vb; |
| 216 | DevInfo_vb.is_unlocked = IsUnlocked (); |
| 217 | DevInfo_vb.is_unlock_critical = IsUnlockCritical (); |
| 218 | Status = Info->VbIntf->VBDeviceInit (Info->VbIntf, |
| 219 | (device_info_vb_t *)&DevInfo_vb); |
| 220 | if (Status != EFI_SUCCESS) { |
| 221 | DEBUG ((EFI_D_ERROR, "Error during VBDeviceInit: %r\n", Status)); |
| 222 | return Status; |
| 223 | } |
| 224 | |
| 225 | AsciiStrnCpyS (StrPnameAscii, ARRAY_SIZE (StrPnameAscii), "/", |
| 226 | AsciiStrLen ("/")); |
| 227 | UnicodeStrToAsciiStr (Info->Pname, PnameAscii); |
| 228 | if (Info->MultiSlotBoot) { |
| 229 | AsciiStrnCatS (StrPnameAscii, ARRAY_SIZE (StrPnameAscii), PnameAscii, |
| 230 | AsciiStrLen (PnameAscii) - (MAX_SLOT_SUFFIX_SZ - 1)); |
| 231 | } else { |
| 232 | AsciiStrnCatS (StrPnameAscii, ARRAY_SIZE (StrPnameAscii), PnameAscii, |
| 233 | AsciiStrLen (PnameAscii)); |
| 234 | } |
| 235 | |
| 236 | Status = |
| 237 | Info->VbIntf->VBVerifyImage (Info->VbIntf, (UINT8 *)StrPnameAscii, |
| 238 | (UINT8 *)Info->Images[0].ImageBuffer, |
| 239 | Info->Images[0].ImageSize, &Info->BootState); |
| 240 | if (Status != EFI_SUCCESS || Info->BootState == BOOT_STATE_MAX) { |
| 241 | DEBUG ((EFI_D_ERROR, "VBVerifyImage failed with: %r\n", Status)); |
| 242 | return Status; |
| 243 | } |
| 244 | |
| 245 | Status = Info->VbIntf->VBSendRot (Info->VbIntf); |
| 246 | if (Status != EFI_SUCCESS) { |
| 247 | DEBUG ((EFI_D_ERROR, "Error sending Rot : %r\n", Status)); |
| 248 | return Status; |
| 249 | } |
| 250 | |
| 251 | if (!IsBootDevImage ()) { |
| 252 | SystemPathLen = GetSystemPath (&SystemPath); |
| 253 | if (SystemPathLen == 0 || SystemPath == NULL) { |
| 254 | DEBUG ((EFI_D_ERROR, "GetSystemPath failed!\n")); |
| 255 | return EFI_LOAD_ERROR; |
| 256 | } |
| 257 | GUARD (AppendVBCmdLine (Info, SystemPath)); |
| 258 | } |
| 259 | GUARD (AppendVBCommonCmdLine (Info)); |
| 260 | GUARD (AppendVBCmdLine (Info, VerityMode)); |
| 261 | GUARD (AppendVBCmdLine (Info, VbVm[IsEnforcing ()].name)); |
| 262 | |
| 263 | Info->VBData = NULL; |
| 264 | return Status; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 267 | STATIC BOOLEAN |
| 268 | ResultShouldContinue (AvbSlotVerifyResult Result) |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 269 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 270 | switch (Result) { |
| 271 | case AVB_SLOT_VERIFY_RESULT_ERROR_OOM: |
| 272 | case AVB_SLOT_VERIFY_RESULT_ERROR_IO: |
| 273 | case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA: |
| 274 | case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION: |
| 275 | case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT: |
| 276 | return FALSE; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 277 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 278 | case AVB_SLOT_VERIFY_RESULT_OK: |
| 279 | case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION: |
| 280 | case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX: |
| 281 | case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED: |
| 282 | return TRUE; |
| 283 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 284 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 285 | return FALSE; |
| 286 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 287 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 288 | STATIC EFI_STATUS |
| 289 | LoadImageAndAuthVB2 (BootInfo *Info) |
| 290 | { |
| 291 | EFI_STATUS Status = EFI_SUCCESS; |
| 292 | AvbSlotVerifyResult Result; |
| 293 | AvbSlotVerifyData *SlotData = NULL; |
| 294 | VB2Data *VBData = NULL; |
| 295 | AvbOpsUserData *UserData = NULL; |
| 296 | AvbOps *Ops = NULL; |
| 297 | CHAR8 PnameAscii[MAX_GPT_NAME_SIZE] = {0}; |
| 298 | CHAR8 *SlotSuffix = NULL; |
| 299 | BOOLEAN AllowVerificationError = IsUnlocked (); |
| 300 | CONST CHAR8 *RequestedPartitionAll[] = {"boot", "dtbo", NULL}; |
| 301 | CONST CHAR8 *CONST *RequestedPartition = NULL; |
| 302 | UINTN NumRequestedPartition = 0; |
| 303 | UINT32 ImageHdrSize = 0; |
| 304 | UINT32 PageSize = 0; |
| 305 | UINT32 ImageSizeActual = 0; |
| 306 | VOID *ImageBuffer = NULL; |
| 307 | UINTN ImageSize = 0; |
| 308 | KMRotAndBootState Data = {0}; |
| 309 | CONST boot_img_hdr *BootImgHdr = NULL; |
| 310 | AvbSlotVerifyFlags VerifyFlags = |
| 311 | AllowVerificationError ? AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR |
| 312 | : AVB_SLOT_VERIFY_FLAGS_NONE; |
| 313 | AvbHashtreeErrorMode VerityFlags = |
| 314 | AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 315 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 316 | Info->BootState = RED; |
| 317 | GUARD (VBCommonInit (Info)); |
| 318 | GUARD (VBAllocateCmdLine (Info)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 319 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 320 | UserData = avb_calloc (sizeof (AvbOpsUserData)); |
| 321 | if (UserData == NULL) { |
| 322 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to allocate AvbOpsUserData\n")); |
| 323 | Status = EFI_OUT_OF_RESOURCES; |
| 324 | goto out; |
| 325 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 326 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 327 | Ops = AvbOpsNew (UserData); |
| 328 | if (Ops == NULL) { |
| 329 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to allocate AvbOps\n")); |
| 330 | Status = EFI_OUT_OF_RESOURCES; |
| 331 | goto out; |
| 332 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 333 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 334 | UnicodeStrToAsciiStr (Info->Pname, PnameAscii); |
| 335 | if ((MAX_SLOT_SUFFIX_SZ + 1) > AsciiStrLen (PnameAscii)) { |
| 336 | DEBUG ((EFI_D_ERROR, "ERROR: Can not determine slot suffix\n")); |
| 337 | Status = EFI_INVALID_PARAMETER; |
| 338 | goto out; |
| 339 | } |
| 340 | SlotSuffix = &PnameAscii[AsciiStrLen (PnameAscii) - MAX_SLOT_SUFFIX_SZ + 1]; |
Shivaprasad Hongal | 39236ae | 2017-06-06 16:10:35 -0700 | [diff] [blame] | 341 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 342 | DEBUG ((EFI_D_VERBOSE, "Slot: %a, allow verification error: %a\n", SlotSuffix, |
| 343 | BooleanString[AllowVerificationError].name)); |
Shivaprasad Hongal | 39236ae | 2017-06-06 16:10:35 -0700 | [diff] [blame] | 344 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 345 | RequestedPartition = RequestedPartitionAll; |
| 346 | NumRequestedPartition = ARRAY_SIZE (RequestedPartitionAll) - 1; |
| 347 | if (Info->NumLoadedImages) { |
| 348 | /* fastboot boot option, skip Index 0, as boot image already |
| 349 | * loaded */ |
| 350 | RequestedPartition = &RequestedPartitionAll[1]; |
| 351 | NumRequestedPartition--; |
| 352 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 353 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 354 | if (FixedPcdGetBool (AllowEio)) { |
| 355 | VerityFlags = IsEnforcing () ? AVB_HASHTREE_ERROR_MODE_RESTART |
| 356 | : AVB_HASHTREE_ERROR_MODE_EIO; |
| 357 | } else { |
| 358 | VerityFlags = AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE; |
| 359 | } |
| 360 | |
| 361 | Result = avb_slot_verify (Ops, RequestedPartition, SlotSuffix, VerifyFlags, |
| 362 | VerityFlags, &SlotData); |
| 363 | |
| 364 | if (AllowVerificationError && ResultShouldContinue (Result)) { |
| 365 | DEBUG ((EFI_D_ERROR, "State: Unlocked, AvbSlotVerify returned " |
| 366 | "%a, continue boot\n", |
| 367 | avb_slot_verify_result_to_string (Result))); |
| 368 | } else if (Result != AVB_SLOT_VERIFY_RESULT_OK) { |
| 369 | DEBUG ((EFI_D_ERROR, "ERROR: Device State %a, AvbSlotVerify returned %a\n", |
| 370 | AllowVerificationError ? "Unlocked" : "Locked", |
| 371 | avb_slot_verify_result_to_string (Result))); |
| 372 | Status = EFI_LOAD_ERROR; |
| 373 | Info->BootState = RED; |
| 374 | goto out; |
| 375 | } |
| 376 | |
| 377 | for (UINTN ReqIndex = 0; ReqIndex < NumRequestedPartition; ReqIndex++) { |
| 378 | DEBUG ((EFI_D_VERBOSE, "Requested Partition: %a\n", |
| 379 | RequestedPartition[ReqIndex])); |
| 380 | for (UINTN LoadedIndex = 0; LoadedIndex < SlotData->num_loaded_partitions; |
| 381 | LoadedIndex++) { |
| 382 | DEBUG ((EFI_D_VERBOSE, "Loaded Partition: %a\n", |
| 383 | SlotData->loaded_partitions[LoadedIndex].partition_name)); |
| 384 | if (!AsciiStrnCmp ( |
| 385 | RequestedPartition[ReqIndex], |
| 386 | SlotData->loaded_partitions[LoadedIndex].partition_name, |
| 387 | AsciiStrLen ( |
| 388 | SlotData->loaded_partitions[LoadedIndex].partition_name))) { |
| 389 | if (Info->NumLoadedImages >= ARRAY_SIZE (Info->Images)) { |
| 390 | DEBUG ((EFI_D_ERROR, "NumLoadedPartition" |
| 391 | "(%d) too large " |
| 392 | "max images(%d)\n", |
| 393 | Info->NumLoadedImages, ARRAY_SIZE (Info->Images))); |
| 394 | Status = EFI_LOAD_ERROR; |
| 395 | Info->BootState = RED; |
| 396 | goto out; |
| 397 | } |
| 398 | Info->Images[Info->NumLoadedImages].Name = |
| 399 | SlotData->loaded_partitions[LoadedIndex].partition_name; |
| 400 | Info->Images[Info->NumLoadedImages].ImageBuffer = |
| 401 | SlotData->loaded_partitions[LoadedIndex].data; |
| 402 | Info->Images[Info->NumLoadedImages].ImageSize = |
| 403 | SlotData->loaded_partitions[LoadedIndex].data_size; |
| 404 | Info->NumLoadedImages++; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 409 | |
Zhen Kong | 3b3a0df | 2017-10-04 19:00:09 -0700 | [diff] [blame] | 410 | if (Info->NumLoadedImages < (ARRAY_SIZE (RequestedPartitionAll) - 1)) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 411 | DEBUG ((EFI_D_ERROR, "ERROR: AvbSlotVerify slot data error: num of " |
| 412 | "loaded partitions %d, requested %d\n", |
| 413 | Info->NumLoadedImages, ARRAY_SIZE (RequestedPartitionAll) - 1)); |
Zhen Kong | 3b3a0df | 2017-10-04 19:00:09 -0700 | [diff] [blame] | 414 | Status = EFI_LOAD_ERROR; |
| 415 | goto out; |
| 416 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 417 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 418 | DEBUG ((EFI_D_VERBOSE, "Total loaded partition %d\n", Info->NumLoadedImages)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 419 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 420 | VBData = (VB2Data *)avb_calloc (sizeof (VB2Data)); |
| 421 | if (VBData == NULL) { |
| 422 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to allocate VB2Data\n")); |
| 423 | Status = EFI_OUT_OF_RESOURCES; |
| 424 | goto out; |
| 425 | } |
| 426 | VBData->Ops = Ops; |
| 427 | VBData->SlotData = SlotData; |
| 428 | Info->VBData = (VOID *)VBData; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 429 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 430 | GetPageSize (&ImageHdrSize); |
| 431 | GUARD_OUT (GetImage (Info, &ImageBuffer, &ImageSize, "boot")); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 432 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 433 | Status = |
| 434 | CheckImageHeader (ImageBuffer, ImageHdrSize, &ImageSizeActual, &PageSize); |
| 435 | if (Status != EFI_SUCCESS) { |
| 436 | DEBUG ((EFI_D_ERROR, "Invalid boot image header:%r\n", Status)); |
| 437 | goto out; |
| 438 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 439 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 440 | if (ImageSizeActual > ImageSize) { |
| 441 | Status = EFI_BUFFER_TOO_SMALL; |
| 442 | DEBUG ((EFI_D_ERROR, "Boot size in vbmeta less than actual boot image size " |
| 443 | "flash corresponding vbmeta.img\n")); |
| 444 | goto out; |
| 445 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 446 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 447 | if (AllowVerificationError) { |
| 448 | Info->BootState = ORANGE; |
| 449 | } else { |
| 450 | if (UserData->IsUserKey) { |
| 451 | Info->BootState = YELLOW; |
| 452 | } else { |
| 453 | Info->BootState = GREEN; |
| 454 | } |
| 455 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 456 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 457 | /* command line */ |
| 458 | GUARD_OUT (AppendVBCommonCmdLine (Info)); |
| 459 | GUARD_OUT (AppendVBCmdLine (Info, SlotData->cmdline)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 460 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 461 | /* Set Rot & Boot State*/ |
| 462 | Data.Color = Info->BootState; |
| 463 | Data.IsUnlocked = AllowVerificationError; |
| 464 | Data.PublicKeyLength = UserData->PublicKeyLen; |
| 465 | Data.PublicKey = UserData->PublicKey; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 466 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 467 | BootImgHdr = (struct boot_img_hdr *)ImageBuffer; |
| 468 | Data.SystemSecurityLevel = (BootImgHdr->os_version & 0x7FF); |
| 469 | Data.SystemVersion = (BootImgHdr->os_version & 0xFFFFF800) >> 11; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 470 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 471 | GUARD_OUT (KeyMasterSetRotAndBootState (&Data)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 472 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 473 | DEBUG ((EFI_D_INFO, "VB2: Authenticate complete! boot state is: %a\n", |
| 474 | VbSn[Info->BootState].name)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 475 | |
| 476 | out: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 477 | if (Status != EFI_SUCCESS) { |
| 478 | if (SlotData != NULL) { |
| 479 | avb_slot_verify_data_free (SlotData); |
| 480 | } |
| 481 | if (Ops != NULL) { |
| 482 | AvbOpsFree (Ops); |
| 483 | } |
| 484 | if (UserData != NULL) { |
| 485 | avb_free (UserData); |
| 486 | } |
| 487 | if (VBData != NULL) { |
| 488 | avb_free (VBData); |
| 489 | } |
| 490 | Info->BootState = RED; |
| 491 | HandleActiveSlotUnbootable (); |
| 492 | /* HandleActiveSlotUnbootable should have swapped slots and |
| 493 | * reboot the device. If no bootable slot found, enter fastboot */ |
| 494 | DEBUG ((EFI_D_WARN, "No bootable slots found enter fastboot mode\n")); |
| 495 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 496 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 497 | DEBUG ((EFI_D_ERROR, "VB2: boot state: %a(%d)\n", VbSn[Info->BootState].name, |
| 498 | Info->BootState)); |
| 499 | return Status; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 502 | STATIC EFI_STATUS |
| 503 | DisplayVerifiedBootScreen (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 504 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 505 | EFI_STATUS Status = EFI_SUCCESS; |
| 506 | CHAR8 FfbmStr[FFBM_MODE_BUF_SIZE] = {'\0'}; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 507 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 508 | if (GetAVBVersion () < AVB_1) { |
| 509 | return EFI_SUCCESS; |
| 510 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 511 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 512 | if (!StrnCmp (Info->Pname, L"boot", StrLen (L"boot"))) { |
| 513 | Status = GetFfbmCommand (FfbmStr, FFBM_MODE_BUF_SIZE); |
| 514 | if (Status != EFI_SUCCESS) { |
| 515 | DEBUG ((EFI_D_VERBOSE, "No Ffbm cookie found, ignore: %r\n", Status)); |
| 516 | FfbmStr[0] = '\0'; |
| 517 | } |
| 518 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 519 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 520 | DEBUG ((EFI_D_VERBOSE, "Boot State is : %d\n", Info->BootState)); |
| 521 | switch (Info->BootState) { |
| 522 | case RED: |
| 523 | Status = DisplayVerifiedBootMenu (DISPLAY_MENU_RED); |
| 524 | if (Status != EFI_SUCCESS) { |
| 525 | DEBUG ((EFI_D_INFO, |
| 526 | "Your device is corrupt. It can't be trusted and will not boot." |
| 527 | "\nYour device will shutdown in 30s\n")); |
| 528 | } |
| 529 | MicroSecondDelay (30000000); |
| 530 | ShutdownDevice (); |
| 531 | break; |
| 532 | case YELLOW: |
| 533 | Status = DisplayVerifiedBootMenu (DISPLAY_MENU_YELLOW); |
| 534 | if (Status == EFI_SUCCESS) { |
| 535 | WaitForExitKeysDetection (); |
| 536 | } else { |
| 537 | DEBUG ((EFI_D_INFO, "Your device has loaded a different operating system." |
| 538 | "\nWait for 5 seconds before proceeding\n")); |
| 539 | MicroSecondDelay (5000000); |
| 540 | } |
| 541 | break; |
| 542 | case ORANGE: |
| 543 | if (FfbmStr[0] != '\0' && !TargetBuildVariantUser ()) { |
| 544 | DEBUG ((EFI_D_VERBOSE, "Device will boot into FFBM mode\n")); |
| 545 | } else { |
| 546 | Status = DisplayVerifiedBootMenu (DISPLAY_MENU_ORANGE); |
| 547 | if (Status == EFI_SUCCESS) { |
| 548 | WaitForExitKeysDetection (); |
| 549 | } else { |
| 550 | DEBUG ( |
| 551 | (EFI_D_INFO, "Device is unlocked, Skipping boot verification\n")); |
| 552 | MicroSecondDelay (5000000); |
| 553 | } |
| 554 | } |
| 555 | break; |
| 556 | default: |
| 557 | break; |
| 558 | } |
| 559 | return EFI_SUCCESS; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 562 | EFI_STATUS |
| 563 | LoadImageAndAuth (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 564 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 565 | EFI_STATUS Status = EFI_SUCCESS; |
| 566 | BOOLEAN MdtpActive = FALSE; |
| 567 | QCOM_MDTP_PROTOCOL *MdtpProtocol; |
| 568 | UINT32 AVBVersion = NO_AVB; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 569 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 570 | if (Info == NULL) { |
| 571 | DEBUG ((EFI_D_ERROR, "Invalid parameter Info\n")); |
| 572 | return EFI_INVALID_PARAMETER; |
| 573 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 574 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 575 | /* Get Partition Name*/ |
| 576 | if (!Info->MultiSlotBoot) { |
| 577 | if (Info->BootIntoRecovery) { |
| 578 | DEBUG ((EFI_D_INFO, "Booting Into Recovery Mode\n")); |
| 579 | StrnCpyS (Info->Pname, ARRAY_SIZE (Info->Pname), L"recovery", |
| 580 | StrLen (L"recovery")); |
| 581 | } else { |
| 582 | DEBUG ((EFI_D_INFO, "Booting Into Mission Mode\n")); |
| 583 | StrnCpyS (Info->Pname, ARRAY_SIZE (Info->Pname), L"boot", |
| 584 | StrLen (L"boot")); |
| 585 | } |
| 586 | } else { |
| 587 | Slot CurrentSlot = {{0}}; |
Shivaprasad Hongal | c017e81 | 2017-04-26 16:15:27 -0700 | [diff] [blame] | 588 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 589 | GUARD (FindBootableSlot (&CurrentSlot)); |
| 590 | if (IsSuffixEmpty (&CurrentSlot)) { |
| 591 | DEBUG ((EFI_D_ERROR, "No bootable slot\n")); |
| 592 | return EFI_LOAD_ERROR; |
| 593 | } |
Shivaprasad Hongal | c017e81 | 2017-04-26 16:15:27 -0700 | [diff] [blame] | 594 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 595 | GUARD (StrnCpyS (Info->Pname, ARRAY_SIZE (Info->Pname), L"boot", |
| 596 | StrLen (L"boot"))); |
| 597 | GUARD (StrnCatS (Info->Pname, ARRAY_SIZE (Info->Pname), CurrentSlot.Suffix, |
| 598 | StrLen (CurrentSlot.Suffix))); |
| 599 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 600 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 601 | DEBUG ((EFI_D_VERBOSE, "MultiSlot %a, partition name %s\n", |
| 602 | BooleanString[Info->MultiSlotBoot].name, Info->Pname)); |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 603 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 604 | if (FixedPcdGetBool (EnableMdtpSupport)) { |
| 605 | Status = IsMdtpActive (&MdtpActive); |
| 606 | if (EFI_ERROR (Status)) { |
| 607 | DEBUG ((EFI_D_ERROR, "Failed to get activation state for MDTP, " |
| 608 | "Status=%r." |
| 609 | " Considering MDTP as active and continuing \n", |
| 610 | Status)); |
| 611 | if (Status != EFI_NOT_FOUND) |
| 612 | MdtpActive = TRUE; |
| 613 | } |
| 614 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 615 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 616 | AVBVersion = GetAVBVersion (); |
| 617 | DEBUG ((EFI_D_VERBOSE, "AVB version %d\n", AVBVersion)); |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 618 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 619 | /* Load and Authenticate */ |
| 620 | switch (AVBVersion) { |
| 621 | case NO_AVB: |
| 622 | return LoadImageNoAuthWrapper (Info); |
| 623 | break; |
| 624 | case AVB_1: |
| 625 | Status = LoadImageAndAuthVB1 (Info); |
| 626 | break; |
| 627 | case AVB_2: |
| 628 | Status = LoadImageAndAuthVB2 (Info); |
| 629 | break; |
| 630 | default: |
| 631 | DEBUG ((EFI_D_ERROR, "Unsupported AVB version %d\n", AVBVersion)); |
| 632 | Status = EFI_UNSUPPORTED; |
| 633 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 634 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 635 | // if MDTP is active Display Recovery UI |
| 636 | if (Status != EFI_SUCCESS && MdtpActive) { |
| 637 | Status = gBS->LocateProtocol (&gQcomMdtpProtocolGuid, NULL, |
| 638 | (VOID **)&MdtpProtocol); |
| 639 | if (EFI_ERROR (Status)) { |
| 640 | DEBUG ( |
| 641 | (EFI_D_ERROR, "Failed to locate MDTP protocol, Status=%r\n", Status)); |
| 642 | return Status; |
| 643 | } |
| 644 | /* Perform Local Deactivation of MDTP */ |
| 645 | Status = MdtpProtocol->MdtpDeactivate (MdtpProtocol, FALSE); |
| 646 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 647 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 648 | if (IsUnlocked () && Status != EFI_SUCCESS) { |
| 649 | DEBUG ((EFI_D_ERROR, "LoadImageAndAuth failed %r\n", Status)); |
| 650 | return Status; |
| 651 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 652 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 653 | DisplayVerifiedBootScreen (Info); |
| 654 | DEBUG ((EFI_D_VERBOSE, "Sending Milestone Call\n")); |
| 655 | Status = Info->VbIntf->VBSendMilestone (Info->VbIntf); |
| 656 | if (Status != EFI_SUCCESS) { |
| 657 | DEBUG ((EFI_D_ERROR, "Error sending milestone call to TZ\n")); |
| 658 | return Status; |
| 659 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 660 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 661 | return Status; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 664 | VOID |
| 665 | FreeVerifiedBootResource (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 666 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 667 | DEBUG ((EFI_D_VERBOSE, "FreeVerifiedBootResource\n")); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 668 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 669 | if (Info == NULL) { |
| 670 | return; |
| 671 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 672 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 673 | VB2Data *VBData = Info->VBData; |
| 674 | if (VBData != NULL) { |
| 675 | AvbOps *Ops = VBData->Ops; |
| 676 | if (Ops != NULL) { |
| 677 | if (Ops->user_data != NULL) { |
| 678 | avb_free (Ops->user_data); |
| 679 | } |
| 680 | AvbOpsFree (Ops); |
| 681 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 682 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 683 | AvbSlotVerifyData *SlotData = VBData->SlotData; |
| 684 | if (SlotData != NULL) { |
| 685 | avb_slot_verify_data_free (SlotData); |
| 686 | } |
| 687 | avb_free (VBData); |
| 688 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 689 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 690 | if (Info->VBCmdLine != NULL) { |
| 691 | FreePool (Info->VBCmdLine); |
| 692 | } |
| 693 | return; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 694 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 695 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 696 | EFI_STATUS |
| 697 | GetCertFingerPrint (UINT8 *FingerPrint, |
| 698 | UINTN FingerPrintLen, |
| 699 | UINTN *FingerPrintLenOut) |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 700 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 701 | EFI_STATUS Status = EFI_SUCCESS; |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 702 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 703 | if (FingerPrint == NULL || FingerPrintLenOut == NULL || |
| 704 | FingerPrintLen < AVB_SHA256_DIGEST_SIZE) { |
| 705 | DEBUG ((EFI_D_ERROR, "GetCertFingerPrint: Invalid parameters\n")); |
| 706 | return EFI_INVALID_PARAMETER; |
| 707 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 708 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 709 | if (GetAVBVersion () == AVB_1) { |
| 710 | QCOM_VERIFIEDBOOT_PROTOCOL *VbIntf = NULL; |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 711 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 712 | Status = gBS->LocateProtocol (&gEfiQcomVerifiedBootProtocolGuid, NULL, |
| 713 | (VOID **)&VbIntf); |
| 714 | if (Status != EFI_SUCCESS) { |
| 715 | DEBUG ((EFI_D_ERROR, "Unable to locate VerifiedBoot Protocol\n")); |
| 716 | return Status; |
| 717 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 718 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 719 | if (VbIntf->Revision < QCOM_VERIFIEDBOOT_PROTOCOL_REVISION) { |
| 720 | DEBUG ((EFI_D_ERROR, "GetCertFingerPrint: VB1: not " |
| 721 | "supported for this revision\n")); |
| 722 | return EFI_UNSUPPORTED; |
| 723 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 724 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 725 | Status = VbIntf->VBGetCertFingerPrint (VbIntf, FingerPrint, FingerPrintLen, |
| 726 | FingerPrintLenOut); |
| 727 | if (Status != EFI_SUCCESS) { |
| 728 | DEBUG ((EFI_D_ERROR, "Failed Reading CERT FingerPrint\n")); |
| 729 | return Status; |
| 730 | } |
| 731 | } else if (GetAVBVersion () == AVB_2) { |
| 732 | CHAR8 *UserKeyBuffer = NULL; |
| 733 | UINT32 UserKeyLength = 0; |
| 734 | AvbSHA256Ctx UserKeyCtx = {{0}}; |
| 735 | CHAR8 *UserKeyDigest = NULL; |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 736 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 737 | GUARD (GetUserKey (&UserKeyBuffer, &UserKeyLength)); |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 738 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 739 | avb_sha256_init (&UserKeyCtx); |
| 740 | avb_sha256_update (&UserKeyCtx, (UINT8 *)UserKeyBuffer, UserKeyLength); |
| 741 | UserKeyDigest = (CHAR8 *)avb_sha256_final (&UserKeyCtx); |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 742 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 743 | CopyMem (FingerPrint, UserKeyDigest, AVB_SHA256_DIGEST_SIZE); |
| 744 | *FingerPrintLenOut = AVB_SHA256_DIGEST_SIZE; |
| 745 | } else { |
| 746 | DEBUG ((EFI_D_ERROR, "GetCertFingerPrint: not supported\n")); |
| 747 | return EFI_UNSUPPORTED; |
| 748 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 749 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame^] | 750 | return Status; |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 751 | } |