Mukesh Ojha | 71be2c8 | 2018-02-02 19:13:10 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved. |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 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> |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 35 | #include <Library/LEOEMCertificate.h> |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 36 | |
| 37 | STATIC CONST CHAR8 *VerityMode = " androidboot.veritymode="; |
| 38 | STATIC CONST CHAR8 *VerifiedState = " androidboot.verifiedbootstate="; |
| 39 | STATIC CONST CHAR8 *KeymasterLoadState = " androidboot.keymaster=1"; |
Mukesh Ojha | 71be2c8 | 2018-02-02 19:13:10 +0530 | [diff] [blame] | 40 | STATIC CONST CHAR8 *DmVerityCmd = " root=/dev/dm-0 dm=\"system none ro,0 1 " |
| 41 | "android-verity"; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 42 | STATIC CONST CHAR8 *Space = " "; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 43 | |
Monika Singh | 4359887 | 2018-10-15 12:04:02 +0530 | [diff] [blame] | 44 | #define MAX_NUM_REQ_PARTITION 8 |
| 45 | |
| 46 | static CHAR8 *avb_verify_partition_name[] = { |
| 47 | "boot", |
| 48 | "dtbo", |
| 49 | "vbmeta", |
| 50 | "recovery" |
| 51 | }; |
| 52 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 53 | STATIC struct verified_boot_verity_mode VbVm[] = { |
| 54 | {FALSE, "logging"}, |
| 55 | {TRUE, "enforcing"}, |
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 verified_boot_state_name VbSn[] = { |
| 59 | {GREEN, "green"}, |
| 60 | {ORANGE, "orange"}, |
| 61 | {YELLOW, "yellow"}, |
| 62 | {RED, "red"}, |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 65 | struct boolean_string { |
| 66 | BOOLEAN value; |
| 67 | CHAR8 *name; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 70 | STATIC struct boolean_string BooleanString[] = { |
| 71 | {FALSE, "false"}, |
| 72 | {TRUE, "true"} |
| 73 | }; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 74 | |
| 75 | typedef struct { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 76 | AvbOps *Ops; |
| 77 | AvbSlotVerifyData *SlotData; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 78 | } VB2Data; |
| 79 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 80 | UINT32 |
| 81 | GetAVBVersion () |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 82 | { |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 83 | #if VERIFIED_BOOT_LE |
| 84 | return AVB_LE; |
| 85 | #elif VERIFIED_BOOT_2 |
| 86 | return AVB_2; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 87 | #elif VERIFIED_BOOT |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 88 | return AVB_1; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 89 | #else |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 90 | return NO_AVB; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 91 | #endif |
| 92 | } |
| 93 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 94 | BOOLEAN |
| 95 | VerifiedBootEnbled () |
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 (GetAVBVersion () > NO_AVB); |
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 | AppendVBCmdLine (BootInfo *Info, CONST CHAR8 *Src) |
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; |
| 104 | INT32 SrcLen = AsciiStrLen (Src); |
| 105 | CHAR8 *Dst = Info->VBCmdLine + Info->VBCmdLineFilledLen; |
| 106 | INT32 DstLen = Info->VBCmdLineLen - Info->VBCmdLineFilledLen; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 107 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 108 | GUARD (AsciiStrnCatS (Dst, DstLen, Src, SrcLen)); |
| 109 | Info->VBCmdLineFilledLen += SrcLen; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 110 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 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 | AppendVBCommonCmdLine (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 | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 118 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 119 | if (Info->VbIntf->Revision >= QCOM_VERIFIEDBOOT_PROTOCOL_REVISION) { |
| 120 | GUARD (AppendVBCmdLine (Info, VerifiedState)); |
| 121 | GUARD (AppendVBCmdLine (Info, VbSn[Info->BootState].name)); |
| 122 | } |
| 123 | GUARD (AppendVBCmdLine (Info, KeymasterLoadState)); |
| 124 | GUARD (AppendVBCmdLine (Info, Space)); |
| 125 | return EFI_SUCCESS; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 128 | STATIC EFI_STATUS |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 129 | NoAVBLoadDtboImage (BootInfo *Info, VOID **DtboImage, |
| 130 | UINT32 *DtboSize, CHAR16 *Pname) |
| 131 | { |
| 132 | EFI_STATUS Status = EFI_SUCCESS; |
| 133 | Slot CurrentSlot; |
Bhanuprakash Modem | 87f2c3e | 2018-07-09 19:08:13 +0530 | [diff] [blame] | 134 | CHAR8 *AsciiPname = NULL; |
Bhanuprakash Modem | ee92f01 | 2018-06-21 17:27:14 +0530 | [diff] [blame] | 135 | UINT64 PartSize = 0; |
| 136 | AvbIOResult AvbStatus; |
| 137 | AvbOpsUserData *UserData = NULL; |
| 138 | AvbOps *Ops = NULL; |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 139 | |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 140 | GUARD ( StrnCpyS (Pname, |
| 141 | (UINTN)MAX_GPT_NAME_SIZE, |
| 142 | (CONST CHAR16 *)L"dtbo", |
| 143 | StrLen (L"dtbo"))); |
| 144 | |
| 145 | if (Info->MultiSlotBoot) { |
| 146 | CurrentSlot = GetCurrentSlotSuffix (); |
| 147 | GUARD ( StrnCatS (Pname, MAX_GPT_NAME_SIZE, |
| 148 | CurrentSlot.Suffix, StrLen (CurrentSlot.Suffix))); |
| 149 | } |
Bhanuprakash Modem | ee92f01 | 2018-06-21 17:27:14 +0530 | [diff] [blame] | 150 | if (GetPartitionIndex (Pname) == INVALID_PTN) { |
| 151 | Status = EFI_NO_MEDIA; |
| 152 | goto out; |
| 153 | } |
| 154 | |
| 155 | /* Get Partition size & compare with MAX supported size |
| 156 | * If Partition size > DTBO_MAX_SIZE_ALLOWED return |
| 157 | * Allocate Partition size memory otherwise |
| 158 | */ |
| 159 | UserData = avb_calloc (sizeof (AvbOpsUserData)); |
| 160 | if (UserData == NULL) { |
| 161 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to allocate AvbOpsUserData\n")); |
| 162 | Status = EFI_OUT_OF_RESOURCES; |
| 163 | goto out; |
| 164 | } |
| 165 | Ops = AvbOpsNew (UserData); |
| 166 | if (Ops == NULL) { |
| 167 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to allocate AvbOps\n")); |
| 168 | Status = EFI_OUT_OF_RESOURCES; |
| 169 | goto out; |
| 170 | } |
Bhanuprakash Modem | 87f2c3e | 2018-07-09 19:08:13 +0530 | [diff] [blame] | 171 | AsciiPname = avb_calloc (MAX_GPT_NAME_SIZE); |
| 172 | if (AsciiPname == NULL) { |
| 173 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to allocate AsciiPname\n")); |
| 174 | Status = EFI_OUT_OF_RESOURCES; |
| 175 | goto out; |
| 176 | } |
| 177 | UnicodeStrToAsciiStr (Pname, AsciiPname); |
| 178 | |
Bhanuprakash Modem | ee92f01 | 2018-06-21 17:27:14 +0530 | [diff] [blame] | 179 | AvbStatus = Ops->get_size_of_partition (Ops, |
Bhanuprakash Modem | 87f2c3e | 2018-07-09 19:08:13 +0530 | [diff] [blame] | 180 | AsciiPname, |
Bhanuprakash Modem | ee92f01 | 2018-06-21 17:27:14 +0530 | [diff] [blame] | 181 | &PartSize); |
| 182 | if (AvbStatus != AVB_IO_RESULT_OK || |
| 183 | PartSize == 0 || |
| 184 | PartSize > DTBO_MAX_SIZE_ALLOWED) { |
| 185 | DEBUG ((EFI_D_ERROR, "VB: Failed to get partition size " |
| 186 | "(or) DTBO size is too big: 0x%x\n", PartSize)); |
| 187 | Status = EFI_OUT_OF_RESOURCES; |
| 188 | goto out; |
| 189 | } |
| 190 | |
| 191 | DEBUG ((EFI_D_VERBOSE, "VB: Trying to allocate memory " |
| 192 | "for DTBO: 0x%x\n", PartSize)); |
| 193 | *DtboSize = (UINT32) PartSize; |
| 194 | *DtboImage = AllocatePool (PartSize); |
| 195 | |
| 196 | if (*DtboImage == NULL) { |
| 197 | DEBUG ((EFI_D_ERROR, "VB: Unable to allocate memory for DTBO\n")); |
| 198 | Status = EFI_OUT_OF_RESOURCES; |
| 199 | goto out; |
| 200 | } |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 201 | Status = LoadImageFromPartition (*DtboImage, DtboSize, Pname); |
Bhanuprakash Modem | ee92f01 | 2018-06-21 17:27:14 +0530 | [diff] [blame] | 202 | |
| 203 | out: |
| 204 | if (Ops != NULL) { |
| 205 | AvbOpsFree (Ops); |
| 206 | } |
| 207 | if (UserData != NULL) { |
| 208 | avb_free (UserData); |
| 209 | } |
Bhanuprakash Modem | 87f2c3e | 2018-07-09 19:08:13 +0530 | [diff] [blame] | 210 | if (AsciiPname != NULL) { |
| 211 | avb_free (AsciiPname); |
| 212 | } |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 213 | return Status; |
| 214 | } |
| 215 | |
| 216 | STATIC EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 217 | VBCommonInit (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 218 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 219 | EFI_STATUS Status = EFI_SUCCESS; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 220 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 221 | Info->BootState = RED; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 222 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 223 | Status = gBS->LocateProtocol (&gEfiQcomVerifiedBootProtocolGuid, NULL, |
| 224 | (VOID **)&(Info->VbIntf)); |
| 225 | if (Status != EFI_SUCCESS) { |
| 226 | DEBUG ((EFI_D_ERROR, "Unable to locate VB protocol: %r\n", Status)); |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 227 | return Status; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return Status; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jeevan Shriram | 615b1e1 | 2017-10-12 14:10:06 -0700 | [diff] [blame] | 233 | /* |
| 234 | * Ensure Info->Pname is already updated before this function is called. |
| 235 | * If Command line already has "root=", return TRUE, else FALSE. |
| 236 | */ |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 237 | STATIC EFI_STATUS |
| 238 | VBAllocateCmdLine (BootInfo *Info) |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 239 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 240 | EFI_STATUS Status = EFI_SUCCESS; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 241 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 242 | /* allocate VB command line*/ |
| 243 | Info->VBCmdLine = AllocatePool (DTB_PAD_SIZE); |
| 244 | if (Info->VBCmdLine == NULL) { |
| 245 | DEBUG ((EFI_D_ERROR, "VB CmdLine allocation failed!\n")); |
| 246 | Status = EFI_OUT_OF_RESOURCES; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 247 | return Status; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 248 | } |
| 249 | Info->VBCmdLineLen = DTB_PAD_SIZE; |
| 250 | Info->VBCmdLineFilledLen = 0; |
| 251 | Info->VBCmdLine[Info->VBCmdLineFilledLen] = '\0'; |
| 252 | |
| 253 | return Status; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Jeevan Shriram | 615b1e1 | 2017-10-12 14:10:06 -0700 | [diff] [blame] | 256 | STATIC |
| 257 | BOOLEAN |
| 258 | IsRootCmdLineUpdated (BootInfo *Info) |
| 259 | { |
| 260 | CHAR8* ImageCmdLine = NULL; |
| 261 | |
| 262 | ImageCmdLine = |
| 263 | (CHAR8*) & (((boot_img_hdr*) (Info->Images[0].ImageBuffer))->cmdline[0]); |
| 264 | |
| 265 | ImageCmdLine[BOOT_ARGS_SIZE - 1] = '\0'; |
| 266 | if (AsciiStrStr (ImageCmdLine, "root=")) { |
| 267 | return TRUE; |
| 268 | } else { |
| 269 | return FALSE; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 274 | STATIC EFI_STATUS |
| 275 | LoadImageNoAuth (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 276 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 277 | EFI_STATUS Status = EFI_SUCCESS; |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 278 | CHAR16 Pname[MAX_GPT_NAME_SIZE]; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 279 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 280 | if (Info->Images[0].ImageBuffer != NULL && Info->Images[0].ImageSize > 0) { |
Jeevan Shriram | e162e5e | 2018-04-12 15:59:52 -0700 | [diff] [blame] | 281 | /* fastboot boot option, boot image is already loaded, check for dtbo */ |
| 282 | goto load_dtbo; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 283 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 284 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 285 | Status = LoadImage (Info->Pname, (VOID **)&(Info->Images[0].ImageBuffer), |
| 286 | (UINT32 *)&(Info->Images[0].ImageSize)); |
| 287 | if (Status != EFI_SUCCESS) { |
| 288 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to load image from partition: %r\n", |
| 289 | Status)); |
| 290 | return EFI_LOAD_ERROR; |
| 291 | } |
| 292 | Info->NumLoadedImages = 1; |
| 293 | Info->Images[0].Name = AllocatePool (StrLen (Info->Pname) + 1); |
| 294 | UnicodeStrToAsciiStr (Info->Pname, Info->Images[0].Name); |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 295 | |
Jeevan Shriram | e162e5e | 2018-04-12 15:59:52 -0700 | [diff] [blame] | 296 | |
| 297 | load_dtbo: |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 298 | /*load dt overlay when avb is disabled*/ |
| 299 | Status = NoAVBLoadDtboImage (Info, (VOID **)&(Info->Images[1].ImageBuffer), |
| 300 | (UINT32 *)&(Info->Images[1].ImageSize), Pname); |
| 301 | if (Status == EFI_NO_MEDIA) { |
| 302 | DEBUG ((EFI_D_ERROR, "No dtbo partition is found, Skip dtbo\n")); |
Bhanuprakash Modem | ee92f01 | 2018-06-21 17:27:14 +0530 | [diff] [blame] | 303 | if (Info->Images[1].ImageBuffer != NULL) { |
| 304 | FreePool (Info->Images[1].ImageBuffer); |
| 305 | } |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 306 | return EFI_SUCCESS; |
| 307 | } |
| 308 | else if (Status != EFI_SUCCESS) { |
| 309 | DEBUG ((EFI_D_ERROR, |
| 310 | "ERROR: Failed to load dtbo from partition: %r\n", Status)); |
Bhanuprakash Modem | ee92f01 | 2018-06-21 17:27:14 +0530 | [diff] [blame] | 311 | if (Info->Images[1].ImageBuffer != NULL) { |
| 312 | FreePool (Info->Images[1].ImageBuffer); |
| 313 | } |
Runmin Wang | 261d2bf | 2017-11-03 11:40:40 -0700 | [diff] [blame] | 314 | return EFI_LOAD_ERROR; |
| 315 | } |
| 316 | Info-> NumLoadedImages = 2; |
| 317 | Info-> Images[1].Name = AllocatePool (StrLen (Pname) + 1); |
| 318 | UnicodeStrToAsciiStr (Pname, Info->Images[1].Name); |
| 319 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 320 | return Status; |
| 321 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 322 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 323 | STATIC EFI_STATUS |
| 324 | LoadImageNoAuthWrapper (BootInfo *Info) |
| 325 | { |
| 326 | EFI_STATUS Status = EFI_SUCCESS; |
| 327 | CHAR8 *SystemPath = NULL; |
| 328 | UINT32 SystemPathLen = 0; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 329 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 330 | GUARD (VBAllocateCmdLine (Info)); |
| 331 | GUARD (LoadImageNoAuth (Info)); |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 332 | |
Jeevan Shriram | 615b1e1 | 2017-10-12 14:10:06 -0700 | [diff] [blame] | 333 | if (!IsRootCmdLineUpdated (Info)) { |
Jeevan Shriram | 9a500f1 | 2018-05-01 13:02:19 -0700 | [diff] [blame] | 334 | SystemPathLen = GetSystemPath (&SystemPath, Info); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 335 | if (SystemPathLen == 0 || SystemPath == NULL) { |
| 336 | DEBUG ((EFI_D_ERROR, "GetSystemPath failed!\n")); |
| 337 | return EFI_LOAD_ERROR; |
Zhen Kong | db33a73 | 2017-07-10 12:23:00 -0700 | [diff] [blame] | 338 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 339 | GUARD (AppendVBCmdLine (Info, SystemPath)); |
| 340 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 341 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 342 | return Status; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 345 | STATIC EFI_STATUS |
| 346 | LoadImageAndAuthVB1 (BootInfo *Info) |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 347 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 348 | EFI_STATUS Status = EFI_SUCCESS; |
| 349 | CHAR8 StrPnameAscii[MAX_GPT_NAME_SIZE]; /* partition name starting with |
| 350 | / and no suffix */ |
| 351 | CHAR8 PnameAscii[MAX_GPT_NAME_SIZE]; |
| 352 | CHAR8 *SystemPath = NULL; |
| 353 | UINT32 SystemPathLen = 0; |
Mukesh Ojha | 71be2c8 | 2018-02-02 19:13:10 +0530 | [diff] [blame] | 354 | CHAR8 *Temp = NULL; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 355 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 356 | GUARD (VBCommonInit (Info)); |
| 357 | GUARD (VBAllocateCmdLine (Info)); |
| 358 | GUARD (LoadImageNoAuth (Info)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 359 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 360 | device_info_vb_t DevInfo_vb; |
| 361 | DevInfo_vb.is_unlocked = IsUnlocked (); |
| 362 | DevInfo_vb.is_unlock_critical = IsUnlockCritical (); |
| 363 | Status = Info->VbIntf->VBDeviceInit (Info->VbIntf, |
| 364 | (device_info_vb_t *)&DevInfo_vb); |
| 365 | if (Status != EFI_SUCCESS) { |
| 366 | DEBUG ((EFI_D_ERROR, "Error during VBDeviceInit: %r\n", Status)); |
| 367 | return Status; |
| 368 | } |
| 369 | |
| 370 | AsciiStrnCpyS (StrPnameAscii, ARRAY_SIZE (StrPnameAscii), "/", |
| 371 | AsciiStrLen ("/")); |
| 372 | UnicodeStrToAsciiStr (Info->Pname, PnameAscii); |
| 373 | if (Info->MultiSlotBoot) { |
| 374 | AsciiStrnCatS (StrPnameAscii, ARRAY_SIZE (StrPnameAscii), PnameAscii, |
| 375 | AsciiStrLen (PnameAscii) - (MAX_SLOT_SUFFIX_SZ - 1)); |
| 376 | } else { |
| 377 | AsciiStrnCatS (StrPnameAscii, ARRAY_SIZE (StrPnameAscii), PnameAscii, |
| 378 | AsciiStrLen (PnameAscii)); |
| 379 | } |
| 380 | |
| 381 | Status = |
| 382 | Info->VbIntf->VBVerifyImage (Info->VbIntf, (UINT8 *)StrPnameAscii, |
| 383 | (UINT8 *)Info->Images[0].ImageBuffer, |
| 384 | Info->Images[0].ImageSize, &Info->BootState); |
| 385 | if (Status != EFI_SUCCESS || Info->BootState == BOOT_STATE_MAX) { |
| 386 | DEBUG ((EFI_D_ERROR, "VBVerifyImage failed with: %r\n", Status)); |
| 387 | return Status; |
| 388 | } |
| 389 | |
| 390 | Status = Info->VbIntf->VBSendRot (Info->VbIntf); |
| 391 | if (Status != EFI_SUCCESS) { |
| 392 | DEBUG ((EFI_D_ERROR, "Error sending Rot : %r\n", Status)); |
| 393 | return Status; |
| 394 | } |
| 395 | |
Jeevan Shriram | 615b1e1 | 2017-10-12 14:10:06 -0700 | [diff] [blame] | 396 | if (!IsRootCmdLineUpdated (Info)) { |
Jeevan Shriram | 9a500f1 | 2018-05-01 13:02:19 -0700 | [diff] [blame] | 397 | SystemPathLen = GetSystemPath (&SystemPath, Info); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 398 | if (SystemPathLen == 0 || SystemPath == NULL) { |
| 399 | DEBUG ((EFI_D_ERROR, "GetSystemPath failed!\n")); |
| 400 | return EFI_LOAD_ERROR; |
| 401 | } |
Mukesh Ojha | 71be2c8 | 2018-02-02 19:13:10 +0530 | [diff] [blame] | 402 | GUARD (AppendVBCmdLine (Info, DmVerityCmd)); |
| 403 | /* Copy only the portion after "root=" in the SystemPath */ |
| 404 | Temp = AsciiStrStr (SystemPath, "root="); |
| 405 | if (Temp) { |
| 406 | CopyMem (Temp, SystemPath + AsciiStrLen ("root=") + 1, |
| 407 | SystemPathLen - AsciiStrLen ("root=") - 1); |
| 408 | SystemPath[SystemPathLen - AsciiStrLen ("root=")] = '\0'; |
| 409 | } |
| 410 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 411 | GUARD (AppendVBCmdLine (Info, SystemPath)); |
Mukesh Ojha | 71be2c8 | 2018-02-02 19:13:10 +0530 | [diff] [blame] | 412 | GUARD (AppendVBCmdLine (Info, "\"")); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 413 | } |
| 414 | GUARD (AppendVBCommonCmdLine (Info)); |
| 415 | GUARD (AppendVBCmdLine (Info, VerityMode)); |
| 416 | GUARD (AppendVBCmdLine (Info, VbVm[IsEnforcing ()].name)); |
| 417 | |
| 418 | Info->VBData = NULL; |
| 419 | return Status; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 422 | STATIC BOOLEAN |
| 423 | ResultShouldContinue (AvbSlotVerifyResult Result) |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 424 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 425 | switch (Result) { |
| 426 | case AVB_SLOT_VERIFY_RESULT_ERROR_OOM: |
| 427 | case AVB_SLOT_VERIFY_RESULT_ERROR_IO: |
| 428 | case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA: |
| 429 | case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION: |
| 430 | case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT: |
| 431 | return FALSE; |
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 | case AVB_SLOT_VERIFY_RESULT_OK: |
| 434 | case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION: |
| 435 | case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX: |
| 436 | case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED: |
| 437 | return TRUE; |
| 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 | return FALSE; |
| 441 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 442 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 443 | STATIC EFI_STATUS |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 444 | LEGetImageHash (QcomAsn1x509Protocol *pEfiQcomASN1X509Protocol, |
| 445 | VB_HASH HashAlgorithm, |
| 446 | UINT8 *Img, UINTN ImgSize, |
| 447 | UINT8 *ImgHash, UINTN HashSize) |
| 448 | { |
| 449 | EFI_STATUS Status = EFI_FAILURE; |
| 450 | EFI_GUID *HashAlgorithmGuid; |
| 451 | UINTN DigestSize = 0; |
| 452 | EFI_HASH2_OUTPUT Hash2Output; |
| 453 | EFI_HASH2_PROTOCOL *pEfiHash2Protocol = NULL; |
| 454 | |
| 455 | if (pEfiQcomASN1X509Protocol == NULL || |
| 456 | Img == NULL || |
| 457 | ImgHash == NULL) { |
| 458 | DEBUG ((EFI_D_ERROR, |
| 459 | "LEGetRSAPublicKeyInfoFromCertificate: Invalid pointer\n")); |
| 460 | return EFI_INVALID_PARAMETER; |
| 461 | } |
| 462 | |
| 463 | switch (HashAlgorithm) { |
| 464 | case VB_SHA256: |
| 465 | HashAlgorithmGuid = &gEfiHashAlgorithmSha256Guid; |
| 466 | break; |
| 467 | default: |
| 468 | DEBUG ((EFI_D_ERROR, |
| 469 | "VB: LEGetImageHash: not supported algorithm:%d\n", HashAlgorithm)); |
| 470 | Status = EFI_UNSUPPORTED; |
| 471 | goto exit; |
| 472 | } |
| 473 | |
| 474 | Status = gBS->LocateProtocol (&gEfiHash2ProtocolGuid, |
| 475 | NULL, (VOID **)&pEfiHash2Protocol); |
| 476 | if (Status != EFI_SUCCESS) { |
| 477 | DEBUG ((EFI_D_ERROR, |
| 478 | "VB:LEGetImageHash: LocateProtocol unsuccessful!Status: %r\n", Status)); |
| 479 | goto exit; |
| 480 | } |
| 481 | |
| 482 | Status = pEfiHash2Protocol->GetHashSize (pEfiHash2Protocol, |
| 483 | HashAlgorithmGuid, &DigestSize); |
| 484 | if (Status != EFI_SUCCESS) { |
| 485 | DEBUG ((EFI_D_ERROR, |
| 486 | "VB: LEGetImageHash: GetHashSize unsuccessful! Status: %r\n", Status)); |
| 487 | goto exit; |
| 488 | } |
| 489 | if (HashSize != DigestSize) { |
| 490 | DEBUG ((EFI_D_ERROR, |
| 491 | "VB: LEGetImageHash: Invalid size! HashSize: %d, DigestSize: %d\n" |
| 492 | , HashSize, DigestSize)); |
| 493 | Status = EFI_FAILURE; |
| 494 | goto exit; |
| 495 | } |
| 496 | Status = pEfiHash2Protocol->HashInit (pEfiHash2Protocol, HashAlgorithmGuid); |
| 497 | if (Status != EFI_SUCCESS) { |
| 498 | DEBUG ((EFI_D_ERROR, |
| 499 | "VB: LEGetImageHash: HashInit unsuccessful! Status: %r\n", Status)); |
| 500 | goto exit; |
| 501 | } |
| 502 | Status = pEfiHash2Protocol->HashUpdate (pEfiHash2Protocol, Img, ImgSize); |
| 503 | if (EFI_SUCCESS != Status) { |
| 504 | |
| 505 | DEBUG ((EFI_D_ERROR, |
| 506 | "VB: LEGetImageHash: HashUpdate unsuccessful(Img)!Status: %r\n" |
| 507 | , Status)); |
| 508 | goto exit; |
| 509 | } |
| 510 | Status = pEfiHash2Protocol->HashFinal (pEfiHash2Protocol, &Hash2Output); |
| 511 | if (EFI_SUCCESS != Status) { |
| 512 | |
| 513 | DEBUG ((EFI_D_ERROR, |
| 514 | "VB: LEGetImageHash: HashFinal unsuccessful! Status: %r\n", Status)); |
| 515 | goto exit; |
| 516 | } |
| 517 | gBS->CopyMem ((VOID *)ImgHash, (VOID *)&Hash2Output, DigestSize); |
| 518 | Status = EFI_SUCCESS; |
| 519 | |
| 520 | exit: |
| 521 | return Status; |
| 522 | } |
| 523 | |
| 524 | STATIC EFI_STATUS LEGetRSAPublicKeyInfoFromCertificate ( |
| 525 | QcomAsn1x509Protocol *pEfiQcomASN1X509Protocol, |
| 526 | CERTIFICATE *Certificate, |
| 527 | secasn1_data_type *Modulus, |
| 528 | secasn1_data_type *PublicExp, |
| 529 | UINT32 *PaddingType) |
| 530 | { |
| 531 | EFI_STATUS Status = EFI_FAILURE; |
| 532 | RSA RsaKey = {NULL}; |
| 533 | |
| 534 | if (pEfiQcomASN1X509Protocol == NULL || |
| 535 | Certificate == NULL || |
| 536 | Modulus == NULL || |
| 537 | PublicExp == NULL || |
| 538 | PaddingType == NULL) { |
| 539 | DEBUG ((EFI_D_ERROR, |
| 540 | "LEGetRSAPublicKeyInfoFromCertificate: Invalid pointer\n")); |
| 541 | return EFI_INVALID_PARAMETER; |
| 542 | } |
| 543 | |
| 544 | Status = pEfiQcomASN1X509Protocol->ASN1X509GetRSAFromCert |
| 545 | (pEfiQcomASN1X509Protocol, Certificate, &RsaKey); |
| 546 | if (Status != EFI_SUCCESS) { |
| 547 | DEBUG ((EFI_D_ERROR, |
| 548 | "VB: ASN1X509GetRSAFromCert unsuccessful! Status : %r\n", Status)); |
| 549 | goto exit; |
| 550 | } |
| 551 | Status = pEfiQcomASN1X509Protocol->ASN1X509GetKeymaterial |
| 552 | (pEfiQcomASN1X509Protocol, &RsaKey, Modulus, PublicExp); |
| 553 | if (Status != EFI_SUCCESS) { |
| 554 | DEBUG ((EFI_D_ERROR, |
| 555 | "VB: ASN1X509GetKeymaterial unsuccessful! Status: %r\n", Status)); |
| 556 | goto exit; |
| 557 | } |
| 558 | *PaddingType = CE_RSA_PAD_PKCS1_V1_5_SIG; |
| 559 | exit: |
| 560 | return Status; |
| 561 | } |
| 562 | STATIC EFI_STATUS LEVerifyHashWithRSASignature ( |
| 563 | UINT8 *ImgHash, |
| 564 | VB_HASH HashAlgorithm, |
| 565 | secasn1_data_type *Modulus, |
| 566 | secasn1_data_type *PublicExp, |
| 567 | UINT32 PaddingType, |
| 568 | CONST UINT8 *SignaturePtr, |
| 569 | UINT32 SignatureLen) |
| 570 | { |
| 571 | EFI_STATUS Status = EFI_FAILURE; |
| 572 | CE_RSA_KEY Key = {0}; |
| 573 | BigInt ModulusBi; |
| 574 | BigInt PublicExpBi; |
| 575 | INT32 HashIdx; |
| 576 | INT32 HashLen; |
| 577 | VOID *PaddingInfo = NULL; |
| 578 | QcomSecRsaProtocol *pEfiQcomSecRSAProtocol = NULL; |
| 579 | SetMem (&Key, sizeof (CE_RSA_KEY), 0); |
| 580 | |
| 581 | if (ImgHash == NULL || |
| 582 | Modulus == NULL || |
| 583 | PublicExp == NULL || |
| 584 | SignaturePtr == NULL) { |
| 585 | DEBUG ((EFI_D_ERROR, "LEVerifyHashWithRSASignature:Invalid pointer\n")); |
| 586 | return EFI_INVALID_PARAMETER; |
| 587 | } |
| 588 | |
| 589 | switch (HashAlgorithm) { |
| 590 | case VB_SHA256: |
| 591 | HashIdx = CE_HASH_IDX_SHA256; |
| 592 | HashLen = VB_SHA256_SIZE; |
| 593 | break; |
| 594 | default: |
| 595 | DEBUG ((EFI_D_ERROR, |
| 596 | "VB: LEVerifySignature: Hash algorithm not supported\n")); |
| 597 | Status = EFI_UNSUPPORTED; |
| 598 | goto exit; |
| 599 | } |
| 600 | |
| 601 | Key.N = AllocatePool (sizeof (S_BIGINT)); |
| 602 | if (Key.N == NULL) { |
| 603 | DEBUG ((EFI_D_ERROR, |
| 604 | "VB: LEVerifySignature: mem allocation err for Key.N\n")); |
| 605 | goto exit; |
| 606 | } |
| 607 | Key.e = AllocatePool (sizeof (S_BIGINT)); |
| 608 | if (Key.e == NULL) { |
| 609 | DEBUG ((EFI_D_ERROR, |
| 610 | "VB: LEVerifySignature: mem allocation err for Key.e\n")); |
| 611 | goto exit; |
| 612 | } |
| 613 | Status = gBS->LocateProtocol (&gEfiQcomSecRSAProtocolGuid, |
| 614 | NULL, (VOID **) &pEfiQcomSecRSAProtocol); |
| 615 | if ( Status != EFI_SUCCESS) { |
| 616 | DEBUG ((EFI_D_ERROR, |
| 617 | "VB: LEVerifySignature: LocateProtocol failed, Status: %r\n", Status)); |
| 618 | goto exit; |
| 619 | } |
| 620 | |
| 621 | Status = pEfiQcomSecRSAProtocol->SecRSABigIntReadBin ( |
| 622 | pEfiQcomSecRSAProtocol, Modulus->data, Modulus->Len, &ModulusBi); |
| 623 | if ( Status != EFI_SUCCESS) { |
| 624 | DEBUG ((EFI_D_ERROR, |
| 625 | "VB: LEVerifySignature: SecRSABigIntReadBin for Modulus failed!" |
| 626 | "Status: %r\n", Status)); |
| 627 | goto exit; |
| 628 | } |
| 629 | Status = pEfiQcomSecRSAProtocol->SecRSABigIntReadBin ( |
| 630 | pEfiQcomSecRSAProtocol, PublicExp->data, PublicExp->Len, &PublicExpBi); |
| 631 | if ( Status != EFI_SUCCESS) { |
| 632 | DEBUG ((EFI_D_ERROR, "VB: LEVerifySignature: SecRSABigIntReadBin for" |
| 633 | " Modulus failed! Status: %r\n", Status)); |
| 634 | goto exit; |
| 635 | } |
| 636 | |
| 637 | Key.N->Bi = ModulusBi; |
| 638 | Key.e->Bi = PublicExpBi; |
| 639 | Key.e->Sign = S_BIGINT_POS; |
| 640 | Key.Type = CE_RSA_KEY_PUBLIC; |
| 641 | |
| 642 | Status = pEfiQcomSecRSAProtocol->SecRSAVerifySig (pEfiQcomSecRSAProtocol, |
| 643 | &Key, PaddingType, |
| 644 | PaddingInfo, HashIdx, |
| 645 | ImgHash, HashLen, (UINT8*)SignaturePtr, SignatureLen); |
| 646 | |
| 647 | if (Status != EFI_SUCCESS) { |
| 648 | DEBUG ((EFI_D_ERROR, |
| 649 | "VB: LEVerifySignature: SecRSAVerifySig failed! Status: %r\n", Status)); |
| 650 | goto exit; |
| 651 | } |
| 652 | |
| 653 | DEBUG ((EFI_D_VERBOSE, "VB: LEVerifySignature: SecRSAVerifySig success!" |
| 654 | " Status: %r\n", Status)); |
| 655 | |
| 656 | Status = EFI_SUCCESS; |
| 657 | exit: |
| 658 | if (Key.N != NULL) { |
| 659 | FreePool (Key.N); |
| 660 | } |
| 661 | if (Key.e != NULL) { |
| 662 | FreePool (Key.e); |
| 663 | } |
| 664 | return Status; |
| 665 | } |
| 666 | |
| 667 | STATIC EFI_STATUS LEVerifyHashWithSignature ( |
| 668 | QcomAsn1x509Protocol *pEfiQcomASN1X509Protocol, |
| 669 | UINT8 *ImgHash, VB_HASH HashAlgorithm, |
| 670 | CERTIFICATE *Certificate, |
| 671 | CONST UINT8 *SignaturePtr, |
| 672 | UINT32 SignatureLen) |
| 673 | { |
| 674 | EFI_STATUS Status = EFI_FAILURE; |
| 675 | secasn1_data_type Modulus = {NULL}; |
| 676 | secasn1_data_type PublicExp = {NULL}; |
| 677 | UINT32 PaddingType = 0; |
| 678 | |
| 679 | if (pEfiQcomASN1X509Protocol == NULL || |
| 680 | ImgHash == NULL || |
| 681 | Certificate == NULL || |
| 682 | SignaturePtr == NULL) { |
| 683 | DEBUG ((EFI_D_ERROR, "LEVerifyHashWithSignature: Invalid pointer\n")); |
| 684 | return EFI_INVALID_PARAMETER; |
| 685 | } |
| 686 | |
| 687 | /* TODO: get subject publick key info from certificate, implement new |
| 688 | algorithm in XBL*/ |
| 689 | /* XBL implemented by default sha256 and rsaEncryption with |
| 690 | PKCS1_V1_5 padding*/ |
| 691 | |
| 692 | Status = LEGetRSAPublicKeyInfoFromCertificate (pEfiQcomASN1X509Protocol, |
| 693 | Certificate, &Modulus, &PublicExp, &PaddingType); |
| 694 | if (Status != EFI_SUCCESS) { |
| 695 | DEBUG ((EFI_D_ERROR, "VB: LEGetRSAPublicKeyInfoFromCertificate " |
| 696 | "unsuccessful! Status: %r\n", Status)); |
| 697 | goto exit; |
| 698 | } |
| 699 | |
| 700 | Status = LEVerifyHashWithRSASignature (ImgHash, HashAlgorithm, |
| 701 | &Modulus, &PublicExp, PaddingType, |
| 702 | SignaturePtr, SignatureLen); |
| 703 | if (Status != EFI_SUCCESS) { |
| 704 | DEBUG ((EFI_D_ERROR, "VB: LEVerifyHashWithSignature unsuccessful! " |
| 705 | "Status: %r\n", Status)); |
| 706 | goto exit; |
| 707 | } |
| 708 | Status = EFI_SUCCESS; |
| 709 | exit: |
| 710 | return Status; |
| 711 | } |
| 712 | |
Monika Singh | 4359887 | 2018-10-15 12:04:02 +0530 | [diff] [blame] | 713 | static BOOLEAN GetHeaderVersion (AvbSlotVerifyData *SlotData) |
| 714 | { |
| 715 | BOOLEAN HeaderVersion = 0; |
| 716 | UINTN LoadedIndex = 0; |
| 717 | for (LoadedIndex = 0; LoadedIndex < SlotData->num_loaded_partitions; |
| 718 | LoadedIndex++) { |
| 719 | if (avb_strcmp (SlotData->loaded_partitions[LoadedIndex].partition_name, |
| 720 | "recovery") == 0 ) |
| 721 | return ( (boot_img_hdr *) |
| 722 | (SlotData->loaded_partitions[LoadedIndex].data))->header_version; |
| 723 | } |
| 724 | return HeaderVersion; |
| 725 | } |
| 726 | |
| 727 | static VOID AddRequestedPartition (CHAR8 **RequestedPartititon, UINT32 Index) |
| 728 | { |
| 729 | UINTN PartIndex = 0; |
| 730 | for (PartIndex = 0; PartIndex < MAX_NUM_REQ_PARTITION; PartIndex++) { |
| 731 | if (RequestedPartititon[PartIndex] == NULL) { |
| 732 | RequestedPartititon[PartIndex] = |
| 733 | avb_verify_partition_name[Index]; |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | } |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 738 | |
| 739 | STATIC EFI_STATUS |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 740 | LoadImageAndAuthVB2 (BootInfo *Info) |
| 741 | { |
| 742 | EFI_STATUS Status = EFI_SUCCESS; |
| 743 | AvbSlotVerifyResult Result; |
| 744 | AvbSlotVerifyData *SlotData = NULL; |
| 745 | VB2Data *VBData = NULL; |
| 746 | AvbOpsUserData *UserData = NULL; |
| 747 | AvbOps *Ops = NULL; |
| 748 | CHAR8 PnameAscii[MAX_GPT_NAME_SIZE] = {0}; |
| 749 | CHAR8 *SlotSuffix = NULL; |
| 750 | BOOLEAN AllowVerificationError = IsUnlocked (); |
Monika Singh | 4359887 | 2018-10-15 12:04:02 +0530 | [diff] [blame] | 751 | CHAR8 *RequestedPartitionAll[MAX_NUM_REQ_PARTITION] = {NULL}; |
| 752 | CHAR8 **RequestedPartition = NULL; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 753 | UINTN NumRequestedPartition = 0; |
| 754 | UINT32 ImageHdrSize = 0; |
| 755 | UINT32 PageSize = 0; |
| 756 | UINT32 ImageSizeActual = 0; |
| 757 | VOID *ImageBuffer = NULL; |
| 758 | UINTN ImageSize = 0; |
| 759 | KMRotAndBootState Data = {0}; |
| 760 | CONST boot_img_hdr *BootImgHdr = NULL; |
| 761 | AvbSlotVerifyFlags VerifyFlags = |
| 762 | AllowVerificationError ? AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR |
| 763 | : AVB_SLOT_VERIFY_FLAGS_NONE; |
| 764 | AvbHashtreeErrorMode VerityFlags = |
| 765 | AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 766 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 767 | Info->BootState = RED; |
| 768 | GUARD (VBCommonInit (Info)); |
| 769 | GUARD (VBAllocateCmdLine (Info)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 770 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 771 | UserData = avb_calloc (sizeof (AvbOpsUserData)); |
| 772 | if (UserData == NULL) { |
| 773 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to allocate AvbOpsUserData\n")); |
| 774 | Status = EFI_OUT_OF_RESOURCES; |
| 775 | goto out; |
| 776 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 777 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 778 | Ops = AvbOpsNew (UserData); |
| 779 | if (Ops == NULL) { |
| 780 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to allocate AvbOps\n")); |
| 781 | Status = EFI_OUT_OF_RESOURCES; |
| 782 | goto out; |
| 783 | } |
Shivaprasad Hongal | 82e18e9 | 2018-03-01 15:29:25 -0800 | [diff] [blame] | 784 | UserData->IsMultiSlot = Info->MultiSlotBoot; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 785 | |
AnilKumar Chimata | 681e0f4 | 2018-01-19 17:47:56 -0800 | [diff] [blame] | 786 | if (Info->MultiSlotBoot) { |
| 787 | UnicodeStrToAsciiStr (Info->Pname, PnameAscii); |
| 788 | if ((MAX_SLOT_SUFFIX_SZ + 1) > AsciiStrLen (PnameAscii)) { |
| 789 | DEBUG ((EFI_D_ERROR, "ERROR: Can not determine slot suffix\n")); |
| 790 | Status = EFI_INVALID_PARAMETER; |
| 791 | goto out; |
| 792 | } |
| 793 | SlotSuffix = &PnameAscii[AsciiStrLen (PnameAscii) - MAX_SLOT_SUFFIX_SZ + 1]; |
| 794 | } else { |
| 795 | SlotSuffix = "\0"; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 796 | } |
Shivaprasad Hongal | 39236ae | 2017-06-06 16:10:35 -0700 | [diff] [blame] | 797 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 798 | DEBUG ((EFI_D_VERBOSE, "Slot: %a, allow verification error: %a\n", SlotSuffix, |
| 799 | BooleanString[AllowVerificationError].name)); |
Shivaprasad Hongal | 39236ae | 2017-06-06 16:10:35 -0700 | [diff] [blame] | 800 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 801 | if (FixedPcdGetBool (AllowEio)) { |
| 802 | VerityFlags = IsEnforcing () ? AVB_HASHTREE_ERROR_MODE_RESTART |
| 803 | : AVB_HASHTREE_ERROR_MODE_EIO; |
| 804 | } else { |
| 805 | VerityFlags = AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE; |
| 806 | } |
Monika Singh | 4359887 | 2018-10-15 12:04:02 +0530 | [diff] [blame] | 807 | RequestedPartition = RequestedPartitionAll; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 808 | |
Monika Singh | 4359887 | 2018-10-15 12:04:02 +0530 | [diff] [blame] | 809 | if ((!Info->MultiSlotBoot) && |
| 810 | Info->BootIntoRecovery) { |
| 811 | AddRequestedPartition (RequestedPartitionAll, IMG_RECOVERY); |
| 812 | NumRequestedPartition += 1; |
| 813 | Result = avb_slot_verify (Ops, (CONST CHAR8 *CONST *)RequestedPartition, |
| 814 | SlotSuffix, VerifyFlags, VerityFlags, &SlotData); |
| 815 | if (Result != AVB_SLOT_VERIFY_RESULT_OK) { |
| 816 | DEBUG ((EFI_D_ERROR, "ERROR: Device State %a,AvbSlotVerify returned %a\n", |
| 817 | AllowVerificationError ? "Unlocked" : "Locked", |
| 818 | avb_slot_verify_result_to_string (Result))); |
| 819 | Status = EFI_LOAD_ERROR; |
| 820 | Info->BootState = RED; |
| 821 | goto out; |
| 822 | } |
| 823 | if (SlotData == NULL) { |
| 824 | Status = EFI_LOAD_ERROR; |
| 825 | Info->BootState = RED; |
| 826 | goto out; |
| 827 | } |
| 828 | BOOLEAN HeaderVersion = GetHeaderVersion (SlotData); |
| 829 | DEBUG ( (EFI_D_VERBOSE, "Recovery HeaderVersion %d \n", HeaderVersion)); |
| 830 | if (!HeaderVersion) { |
| 831 | AddRequestedPartition (RequestedPartitionAll, IMG_DTBO); |
| 832 | NumRequestedPartition += 1; |
| 833 | if (SlotData != NULL) { |
| 834 | avb_slot_verify_data_free (SlotData); |
| 835 | } |
| 836 | Result = avb_slot_verify (Ops, (CONST CHAR8 *CONST *)RequestedPartition, |
| 837 | SlotSuffix, VerifyFlags, VerityFlags, &SlotData); |
| 838 | } |
| 839 | } else { |
| 840 | if (!Info->NumLoadedImages) { |
| 841 | AddRequestedPartition (RequestedPartitionAll, IMG_BOOT); |
| 842 | NumRequestedPartition += 1; |
| 843 | } |
| 844 | AddRequestedPartition (RequestedPartitionAll, IMG_DTBO); |
| 845 | NumRequestedPartition += 1; |
| 846 | Result = avb_slot_verify (Ops, (CONST CHAR8 *CONST *)RequestedPartition, |
| 847 | SlotSuffix, VerifyFlags, VerityFlags, &SlotData); |
| 848 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 849 | |
Mukesh Ojha | 88a4fb9 | 2018-03-09 13:54:54 +0530 | [diff] [blame] | 850 | if (SlotData == NULL) { |
| 851 | Status = EFI_LOAD_ERROR; |
| 852 | Info->BootState = RED; |
| 853 | goto out; |
| 854 | } |
| 855 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 856 | if (AllowVerificationError && ResultShouldContinue (Result)) { |
| 857 | DEBUG ((EFI_D_ERROR, "State: Unlocked, AvbSlotVerify returned " |
| 858 | "%a, continue boot\n", |
| 859 | avb_slot_verify_result_to_string (Result))); |
| 860 | } else if (Result != AVB_SLOT_VERIFY_RESULT_OK) { |
| 861 | DEBUG ((EFI_D_ERROR, "ERROR: Device State %a, AvbSlotVerify returned %a\n", |
| 862 | AllowVerificationError ? "Unlocked" : "Locked", |
| 863 | avb_slot_verify_result_to_string (Result))); |
| 864 | Status = EFI_LOAD_ERROR; |
| 865 | Info->BootState = RED; |
| 866 | goto out; |
| 867 | } |
| 868 | |
| 869 | for (UINTN ReqIndex = 0; ReqIndex < NumRequestedPartition; ReqIndex++) { |
| 870 | DEBUG ((EFI_D_VERBOSE, "Requested Partition: %a\n", |
| 871 | RequestedPartition[ReqIndex])); |
| 872 | for (UINTN LoadedIndex = 0; LoadedIndex < SlotData->num_loaded_partitions; |
| 873 | LoadedIndex++) { |
| 874 | DEBUG ((EFI_D_VERBOSE, "Loaded Partition: %a\n", |
| 875 | SlotData->loaded_partitions[LoadedIndex].partition_name)); |
| 876 | if (!AsciiStrnCmp ( |
| 877 | RequestedPartition[ReqIndex], |
| 878 | SlotData->loaded_partitions[LoadedIndex].partition_name, |
| 879 | AsciiStrLen ( |
| 880 | SlotData->loaded_partitions[LoadedIndex].partition_name))) { |
| 881 | if (Info->NumLoadedImages >= ARRAY_SIZE (Info->Images)) { |
| 882 | DEBUG ((EFI_D_ERROR, "NumLoadedPartition" |
| 883 | "(%d) too large " |
| 884 | "max images(%d)\n", |
| 885 | Info->NumLoadedImages, ARRAY_SIZE (Info->Images))); |
| 886 | Status = EFI_LOAD_ERROR; |
| 887 | Info->BootState = RED; |
| 888 | goto out; |
| 889 | } |
| 890 | Info->Images[Info->NumLoadedImages].Name = |
| 891 | SlotData->loaded_partitions[LoadedIndex].partition_name; |
| 892 | Info->Images[Info->NumLoadedImages].ImageBuffer = |
| 893 | SlotData->loaded_partitions[LoadedIndex].data; |
| 894 | Info->Images[Info->NumLoadedImages].ImageSize = |
| 895 | SlotData->loaded_partitions[LoadedIndex].data_size; |
| 896 | Info->NumLoadedImages++; |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 901 | |
AnilKumar Chimata | 681e0f4 | 2018-01-19 17:47:56 -0800 | [diff] [blame] | 902 | if (Info->NumLoadedImages < NumRequestedPartition) { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 903 | DEBUG ((EFI_D_ERROR, "ERROR: AvbSlotVerify slot data error: num of " |
| 904 | "loaded partitions %d, requested %d\n", |
AnilKumar Chimata | 681e0f4 | 2018-01-19 17:47:56 -0800 | [diff] [blame] | 905 | Info->NumLoadedImages, NumRequestedPartition)); |
Zhen Kong | 3b3a0df | 2017-10-04 19:00:09 -0700 | [diff] [blame] | 906 | Status = EFI_LOAD_ERROR; |
| 907 | goto out; |
| 908 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 909 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 910 | DEBUG ((EFI_D_VERBOSE, "Total loaded partition %d\n", Info->NumLoadedImages)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 911 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 912 | VBData = (VB2Data *)avb_calloc (sizeof (VB2Data)); |
| 913 | if (VBData == NULL) { |
| 914 | DEBUG ((EFI_D_ERROR, "ERROR: Failed to allocate VB2Data\n")); |
| 915 | Status = EFI_OUT_OF_RESOURCES; |
| 916 | goto out; |
| 917 | } |
| 918 | VBData->Ops = Ops; |
| 919 | VBData->SlotData = SlotData; |
| 920 | Info->VBData = (VOID *)VBData; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 921 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 922 | GetPageSize (&ImageHdrSize); |
AnilKumar Chimata | 681e0f4 | 2018-01-19 17:47:56 -0800 | [diff] [blame] | 923 | GUARD_OUT (GetImage (Info, &ImageBuffer, &ImageSize, |
| 924 | ((!Info->MultiSlotBoot) && |
| 925 | Info->BootIntoRecovery) ? |
| 926 | "recovery" : "boot")); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 927 | |
AnilKumar Chimata | 681e0f4 | 2018-01-19 17:47:56 -0800 | [diff] [blame] | 928 | Status = CheckImageHeader (ImageBuffer, ImageHdrSize, |
| 929 | &ImageSizeActual, &PageSize); |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 930 | if (Status != EFI_SUCCESS) { |
| 931 | DEBUG ((EFI_D_ERROR, "Invalid boot image header:%r\n", Status)); |
| 932 | goto out; |
| 933 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 934 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 935 | if (AllowVerificationError) { |
| 936 | Info->BootState = ORANGE; |
| 937 | } else { |
| 938 | if (UserData->IsUserKey) { |
| 939 | Info->BootState = YELLOW; |
| 940 | } else { |
| 941 | Info->BootState = GREEN; |
| 942 | } |
| 943 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 944 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 945 | /* command line */ |
| 946 | GUARD_OUT (AppendVBCommonCmdLine (Info)); |
| 947 | GUARD_OUT (AppendVBCmdLine (Info, SlotData->cmdline)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 948 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 949 | /* Set Rot & Boot State*/ |
| 950 | Data.Color = Info->BootState; |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 951 | Data. IsUnlocked = AllowVerificationError; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 952 | Data.PublicKeyLength = UserData->PublicKeyLen; |
| 953 | Data.PublicKey = UserData->PublicKey; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 954 | |
lijuang | bcba709 | 2018-04-10 19:57:54 +0800 | [diff] [blame] | 955 | BootImgHdr = (boot_img_hdr *)ImageBuffer; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 956 | Data.SystemSecurityLevel = (BootImgHdr->os_version & 0x7FF); |
| 957 | Data.SystemVersion = (BootImgHdr->os_version & 0xFFFFF800) >> 11; |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 958 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 959 | GUARD_OUT (KeyMasterSetRotAndBootState (&Data)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 960 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 961 | DEBUG ((EFI_D_INFO, "VB2: Authenticate complete! boot state is: %a\n", |
| 962 | VbSn[Info->BootState].name)); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 963 | |
| 964 | out: |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 965 | if (Status != EFI_SUCCESS) { |
| 966 | if (SlotData != NULL) { |
| 967 | avb_slot_verify_data_free (SlotData); |
| 968 | } |
| 969 | if (Ops != NULL) { |
| 970 | AvbOpsFree (Ops); |
| 971 | } |
| 972 | if (UserData != NULL) { |
| 973 | avb_free (UserData); |
| 974 | } |
| 975 | if (VBData != NULL) { |
| 976 | avb_free (VBData); |
| 977 | } |
| 978 | Info->BootState = RED; |
AnilKumar Chimata | 681e0f4 | 2018-01-19 17:47:56 -0800 | [diff] [blame] | 979 | if (Info->MultiSlotBoot) { |
| 980 | HandleActiveSlotUnbootable (); |
| 981 | /* HandleActiveSlotUnbootable should have swapped slots and |
| 982 | * reboot the device. If no bootable slot found, enter fastboot |
| 983 | */ |
| 984 | DEBUG ((EFI_D_WARN, "No bootable slots found enter fastboot mode\n")); |
| 985 | } else { |
| 986 | DEBUG ((EFI_D_WARN, |
| 987 | "Non Multi-slot: Unbootable entering fastboot mode\n")); |
| 988 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 989 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 990 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 991 | DEBUG ((EFI_D_ERROR, "VB2: boot state: %a(%d)\n", VbSn[Info->BootState].name, |
| 992 | Info->BootState)); |
| 993 | return Status; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 996 | STATIC EFI_STATUS |
| 997 | DisplayVerifiedBootScreen (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 998 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 999 | EFI_STATUS Status = EFI_SUCCESS; |
| 1000 | CHAR8 FfbmStr[FFBM_MODE_BUF_SIZE] = {'\0'}; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1001 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1002 | if (GetAVBVersion () < AVB_1) { |
| 1003 | return EFI_SUCCESS; |
| 1004 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1005 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1006 | if (!StrnCmp (Info->Pname, L"boot", StrLen (L"boot"))) { |
| 1007 | Status = GetFfbmCommand (FfbmStr, FFBM_MODE_BUF_SIZE); |
| 1008 | if (Status != EFI_SUCCESS) { |
| 1009 | DEBUG ((EFI_D_VERBOSE, "No Ffbm cookie found, ignore: %r\n", Status)); |
| 1010 | FfbmStr[0] = '\0'; |
| 1011 | } |
| 1012 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1013 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1014 | DEBUG ((EFI_D_VERBOSE, "Boot State is : %d\n", Info->BootState)); |
| 1015 | switch (Info->BootState) { |
| 1016 | case RED: |
| 1017 | Status = DisplayVerifiedBootMenu (DISPLAY_MENU_RED); |
| 1018 | if (Status != EFI_SUCCESS) { |
| 1019 | DEBUG ((EFI_D_INFO, |
| 1020 | "Your device is corrupt. It can't be trusted and will not boot." |
| 1021 | "\nYour device will shutdown in 30s\n")); |
| 1022 | } |
| 1023 | MicroSecondDelay (30000000); |
| 1024 | ShutdownDevice (); |
| 1025 | break; |
| 1026 | case YELLOW: |
| 1027 | Status = DisplayVerifiedBootMenu (DISPLAY_MENU_YELLOW); |
| 1028 | if (Status == EFI_SUCCESS) { |
| 1029 | WaitForExitKeysDetection (); |
| 1030 | } else { |
| 1031 | DEBUG ((EFI_D_INFO, "Your device has loaded a different operating system." |
| 1032 | "\nWait for 5 seconds before proceeding\n")); |
| 1033 | MicroSecondDelay (5000000); |
| 1034 | } |
| 1035 | break; |
| 1036 | case ORANGE: |
| 1037 | if (FfbmStr[0] != '\0' && !TargetBuildVariantUser ()) { |
| 1038 | DEBUG ((EFI_D_VERBOSE, "Device will boot into FFBM mode\n")); |
| 1039 | } else { |
| 1040 | Status = DisplayVerifiedBootMenu (DISPLAY_MENU_ORANGE); |
| 1041 | if (Status == EFI_SUCCESS) { |
| 1042 | WaitForExitKeysDetection (); |
| 1043 | } else { |
| 1044 | DEBUG ( |
| 1045 | (EFI_D_INFO, "Device is unlocked, Skipping boot verification\n")); |
| 1046 | MicroSecondDelay (5000000); |
| 1047 | } |
| 1048 | } |
| 1049 | break; |
| 1050 | default: |
| 1051 | break; |
| 1052 | } |
lijuang | 3d51d3b | 2018-07-03 14:29:59 +0800 | [diff] [blame] | 1053 | |
| 1054 | /* dm-verity warning */ |
Jeevan Shriram | 70d9857 | 2018-09-26 15:01:49 -0700 | [diff] [blame] | 1055 | if ((GetAVBVersion () != AVB_2) && |
| 1056 | !IsEnforcing () && |
lijuang | 3d51d3b | 2018-07-03 14:29:59 +0800 | [diff] [blame] | 1057 | !Info->BootIntoRecovery) { |
| 1058 | Status = DisplayVerifiedBootMenu (DISPLAY_MENU_EIO); |
| 1059 | if (Status == EFI_SUCCESS) { |
| 1060 | WaitForExitKeysDetection (); |
| 1061 | } else { |
| 1062 | DEBUG ((EFI_D_INFO, "The dm-verity is not started in restart mode." \ |
| 1063 | "\nWait for 30 seconds before proceeding\n")); |
| 1064 | MicroSecondDelay (30000000); |
| 1065 | } |
| 1066 | } |
| 1067 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1068 | return EFI_SUCCESS; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 1071 | STATIC EFI_STATUS LoadImageAndAuthForLE (BootInfo *Info) |
| 1072 | { |
| 1073 | EFI_STATUS Status = EFI_SUCCESS; |
| 1074 | QcomAsn1x509Protocol *QcomAsn1X509Protocal = NULL; |
| 1075 | CONST UINT8 *OemCertFile = LeOemCertificate; |
| 1076 | UINTN OemCertFileLen = sizeof (LeOemCertificate); |
| 1077 | CERTIFICATE OemCert = {NULL}; |
| 1078 | UINTN HashSize; |
| 1079 | UINT8 *ImgHash = NULL; |
| 1080 | UINTN ImgSize; |
| 1081 | VB_HASH HashAlgorithm; |
| 1082 | UINT8 *SigAddr = NULL; |
| 1083 | UINT32 SigSize = 0; |
mohamed sunfeer | 4987c69 | 2018-04-18 19:29:28 +0530 | [diff] [blame] | 1084 | CHAR8 *SystemPath = NULL; |
| 1085 | UINT32 SystemPathLen = 0; |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 1086 | |
| 1087 | /*Load image*/ |
mohamed sunfeer | 4987c69 | 2018-04-18 19:29:28 +0530 | [diff] [blame] | 1088 | GUARD (VBAllocateCmdLine (Info)); |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 1089 | GUARD (VBCommonInit (Info)); |
| 1090 | GUARD (LoadImageNoAuth (Info)); |
| 1091 | |
Mohamed Sunfeer | 604b2ed | 2018-10-30 13:36:02 +0530 | [diff] [blame^] | 1092 | if (!TargetBuildVariantUser ()) { |
| 1093 | DEBUG ((EFI_D_INFO, "VB: verification skipped for debug builds\n")); |
| 1094 | goto skip_verification; |
| 1095 | } |
| 1096 | |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 1097 | /* Initialize Verified Boot*/ |
| 1098 | device_info_vb_t DevInfo_vb; |
| 1099 | DevInfo_vb.is_unlocked = IsUnlocked (); |
| 1100 | DevInfo_vb.is_unlock_critical = IsUnlockCritical (); |
| 1101 | Status = Info->VbIntf->VBDeviceInit (Info->VbIntf, |
| 1102 | (device_info_vb_t *)&DevInfo_vb); |
| 1103 | if (Status != EFI_SUCCESS) { |
| 1104 | DEBUG ((EFI_D_ERROR, "VB: Error during VBDeviceInit: %r\n", Status)); |
| 1105 | return Status; |
| 1106 | } |
| 1107 | |
| 1108 | /* Locate QcomAsn1x509Protocol*/ |
| 1109 | Status = gBS->LocateProtocol (&gEfiQcomASN1X509ProtocolGuid, NULL, |
| 1110 | (VOID **)&QcomAsn1X509Protocal); |
| 1111 | if (Status != EFI_SUCCESS) { |
| 1112 | DEBUG ((EFI_D_ERROR, "VB: Error LocateProtocol " |
| 1113 | "gEfiQcomASN1X509ProtocolGuid: %r\n", Status)); |
| 1114 | return Status; |
| 1115 | } |
| 1116 | |
| 1117 | /* Read OEM certificate from the embedded header file */ |
| 1118 | Status = QcomAsn1X509Protocal->ASN1X509VerifyOEMCertificate |
| 1119 | (QcomAsn1X509Protocal, OemCertFile, OemCertFileLen, &OemCert); |
| 1120 | if (Status != EFI_SUCCESS) { |
| 1121 | DEBUG ((EFI_D_ERROR, "VB: Error during " |
| 1122 | "ASN1X509VerifyOEMCertificate: %r\n", Status)); |
| 1123 | return Status; |
| 1124 | } |
| 1125 | |
| 1126 | /*Calculate kernel image hash, SHA256 is used by default*/ |
| 1127 | HashAlgorithm = VB_SHA256; |
| 1128 | HashSize = VB_SHA256_SIZE; |
| 1129 | ImgSize = Info->Images[0].ImageSize; |
| 1130 | ImgHash = AllocatePool (HashSize); |
| 1131 | if (ImgHash == NULL) { |
| 1132 | DEBUG ((EFI_D_ERROR, "kernel image hash buffer allocation failed!\n")); |
| 1133 | Status = EFI_OUT_OF_RESOURCES; |
| 1134 | return Status; |
| 1135 | } |
| 1136 | Status = LEGetImageHash (QcomAsn1X509Protocal, HashAlgorithm, |
| 1137 | (UINT8 *)Info->Images[0].ImageBuffer, |
| 1138 | ImgSize, ImgHash, HashSize); |
| 1139 | if (Status != EFI_SUCCESS) { |
| 1140 | DEBUG ((EFI_D_ERROR, "VB: Error during VBGetImageHash: %r\n", Status)); |
| 1141 | return Status; |
| 1142 | } |
| 1143 | |
| 1144 | SigAddr = (UINT8 *)Info->Images[0].ImageBuffer + ImgSize; |
| 1145 | SigSize = LE_BOOTIMG_SIG_SIZE; |
| 1146 | Status = LEVerifyHashWithSignature (QcomAsn1X509Protocal, ImgHash, |
| 1147 | HashAlgorithm, &OemCert, SigAddr, SigSize); |
| 1148 | |
| 1149 | if (Status != EFI_SUCCESS) { |
| 1150 | DEBUG ((EFI_D_ERROR, "VB: Error during " |
| 1151 | "LEVBVerifyHashWithSignature: %r\n", Status)); |
| 1152 | return Status; |
| 1153 | } |
| 1154 | DEBUG ((EFI_D_INFO, "VB: LoadImageAndAuthForLE complete!\n")); |
mohamed sunfeer | 4987c69 | 2018-04-18 19:29:28 +0530 | [diff] [blame] | 1155 | |
Mohamed Sunfeer | 604b2ed | 2018-10-30 13:36:02 +0530 | [diff] [blame^] | 1156 | skip_verification: |
mohamed sunfeer | 4987c69 | 2018-04-18 19:29:28 +0530 | [diff] [blame] | 1157 | if (!IsRootCmdLineUpdated (Info)) { |
Jeevan Shriram | 9a500f1 | 2018-05-01 13:02:19 -0700 | [diff] [blame] | 1158 | SystemPathLen = GetSystemPath (&SystemPath, Info); |
mohamed sunfeer | 4987c69 | 2018-04-18 19:29:28 +0530 | [diff] [blame] | 1159 | if (SystemPathLen == 0 || |
| 1160 | SystemPath == NULL) { |
| 1161 | return EFI_LOAD_ERROR; |
| 1162 | } |
| 1163 | GUARD (AppendVBCmdLine (Info, SystemPath)); |
| 1164 | } |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 1165 | return Status; |
| 1166 | } |
| 1167 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1168 | EFI_STATUS |
| 1169 | LoadImageAndAuth (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1170 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1171 | EFI_STATUS Status = EFI_SUCCESS; |
| 1172 | BOOLEAN MdtpActive = FALSE; |
| 1173 | QCOM_MDTP_PROTOCOL *MdtpProtocol; |
| 1174 | UINT32 AVBVersion = NO_AVB; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1175 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1176 | if (Info == NULL) { |
| 1177 | DEBUG ((EFI_D_ERROR, "Invalid parameter Info\n")); |
| 1178 | return EFI_INVALID_PARAMETER; |
| 1179 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1180 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1181 | /* Get Partition Name*/ |
| 1182 | if (!Info->MultiSlotBoot) { |
| 1183 | if (Info->BootIntoRecovery) { |
| 1184 | DEBUG ((EFI_D_INFO, "Booting Into Recovery Mode\n")); |
| 1185 | StrnCpyS (Info->Pname, ARRAY_SIZE (Info->Pname), L"recovery", |
| 1186 | StrLen (L"recovery")); |
| 1187 | } else { |
| 1188 | DEBUG ((EFI_D_INFO, "Booting Into Mission Mode\n")); |
| 1189 | StrnCpyS (Info->Pname, ARRAY_SIZE (Info->Pname), L"boot", |
| 1190 | StrLen (L"boot")); |
| 1191 | } |
| 1192 | } else { |
| 1193 | Slot CurrentSlot = {{0}}; |
Shivaprasad Hongal | c017e81 | 2017-04-26 16:15:27 -0700 | [diff] [blame] | 1194 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1195 | GUARD (FindBootableSlot (&CurrentSlot)); |
| 1196 | if (IsSuffixEmpty (&CurrentSlot)) { |
| 1197 | DEBUG ((EFI_D_ERROR, "No bootable slot\n")); |
| 1198 | return EFI_LOAD_ERROR; |
| 1199 | } |
Shivaprasad Hongal | c017e81 | 2017-04-26 16:15:27 -0700 | [diff] [blame] | 1200 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1201 | GUARD (StrnCpyS (Info->Pname, ARRAY_SIZE (Info->Pname), L"boot", |
| 1202 | StrLen (L"boot"))); |
| 1203 | GUARD (StrnCatS (Info->Pname, ARRAY_SIZE (Info->Pname), CurrentSlot.Suffix, |
| 1204 | StrLen (CurrentSlot.Suffix))); |
| 1205 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1206 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1207 | DEBUG ((EFI_D_VERBOSE, "MultiSlot %a, partition name %s\n", |
| 1208 | BooleanString[Info->MultiSlotBoot].name, Info->Pname)); |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1209 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1210 | if (FixedPcdGetBool (EnableMdtpSupport)) { |
| 1211 | Status = IsMdtpActive (&MdtpActive); |
| 1212 | if (EFI_ERROR (Status)) { |
| 1213 | DEBUG ((EFI_D_ERROR, "Failed to get activation state for MDTP, " |
| 1214 | "Status=%r." |
| 1215 | " Considering MDTP as active and continuing \n", |
| 1216 | Status)); |
| 1217 | if (Status != EFI_NOT_FOUND) |
| 1218 | MdtpActive = TRUE; |
| 1219 | } |
| 1220 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1221 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1222 | AVBVersion = GetAVBVersion (); |
| 1223 | DEBUG ((EFI_D_VERBOSE, "AVB version %d\n", AVBVersion)); |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1224 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1225 | /* Load and Authenticate */ |
| 1226 | switch (AVBVersion) { |
| 1227 | case NO_AVB: |
| 1228 | return LoadImageNoAuthWrapper (Info); |
| 1229 | break; |
| 1230 | case AVB_1: |
| 1231 | Status = LoadImageAndAuthVB1 (Info); |
| 1232 | break; |
| 1233 | case AVB_2: |
| 1234 | Status = LoadImageAndAuthVB2 (Info); |
| 1235 | break; |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 1236 | case AVB_LE: |
| 1237 | Status = LoadImageAndAuthForLE (Info); |
| 1238 | break; |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1239 | default: |
| 1240 | DEBUG ((EFI_D_ERROR, "Unsupported AVB version %d\n", AVBVersion)); |
| 1241 | Status = EFI_UNSUPPORTED; |
| 1242 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1243 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1244 | // if MDTP is active Display Recovery UI |
| 1245 | if (Status != EFI_SUCCESS && MdtpActive) { |
| 1246 | Status = gBS->LocateProtocol (&gQcomMdtpProtocolGuid, NULL, |
| 1247 | (VOID **)&MdtpProtocol); |
| 1248 | if (EFI_ERROR (Status)) { |
| 1249 | DEBUG ( |
| 1250 | (EFI_D_ERROR, "Failed to locate MDTP protocol, Status=%r\n", Status)); |
| 1251 | return Status; |
| 1252 | } |
| 1253 | /* Perform Local Deactivation of MDTP */ |
| 1254 | Status = MdtpProtocol->MdtpDeactivate (MdtpProtocol, FALSE); |
| 1255 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1256 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1257 | if (IsUnlocked () && Status != EFI_SUCCESS) { |
| 1258 | DEBUG ((EFI_D_ERROR, "LoadImageAndAuth failed %r\n", Status)); |
| 1259 | return Status; |
| 1260 | } |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1261 | |
Zhen Kong | bb3db75 | 2017-07-10 17:07:10 -0700 | [diff] [blame] | 1262 | if (AVBVersion != AVB_LE) { |
| 1263 | DisplayVerifiedBootScreen (Info); |
| 1264 | DEBUG ((EFI_D_VERBOSE, "Sending Milestone Call\n")); |
| 1265 | Status = Info->VbIntf->VBSendMilestone (Info->VbIntf); |
| 1266 | if (Status != EFI_SUCCESS) { |
| 1267 | DEBUG ((EFI_D_ERROR, "Error sending milestone call to TZ\n")); |
| 1268 | return Status; |
| 1269 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1270 | } |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1271 | return Status; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1274 | VOID |
| 1275 | FreeVerifiedBootResource (BootInfo *Info) |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1276 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1277 | DEBUG ((EFI_D_VERBOSE, "FreeVerifiedBootResource\n")); |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 1278 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1279 | if (Info == NULL) { |
| 1280 | return; |
| 1281 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 1282 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1283 | VB2Data *VBData = Info->VBData; |
| 1284 | if (VBData != NULL) { |
| 1285 | AvbOps *Ops = VBData->Ops; |
| 1286 | if (Ops != NULL) { |
| 1287 | if (Ops->user_data != NULL) { |
| 1288 | avb_free (Ops->user_data); |
| 1289 | } |
| 1290 | AvbOpsFree (Ops); |
| 1291 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 1292 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1293 | AvbSlotVerifyData *SlotData = VBData->SlotData; |
| 1294 | if (SlotData != NULL) { |
| 1295 | avb_slot_verify_data_free (SlotData); |
| 1296 | } |
| 1297 | avb_free (VBData); |
| 1298 | } |
Shivaprasad Hongal | e3b5339 | 2017-04-27 17:32:47 -0700 | [diff] [blame] | 1299 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1300 | if (Info->VBCmdLine != NULL) { |
| 1301 | FreePool (Info->VBCmdLine); |
| 1302 | } |
| 1303 | return; |
Shivaprasad Hongal | a2c4dd7 | 2017-04-27 14:33:18 -0700 | [diff] [blame] | 1304 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1305 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1306 | EFI_STATUS |
| 1307 | GetCertFingerPrint (UINT8 *FingerPrint, |
| 1308 | UINTN FingerPrintLen, |
| 1309 | UINTN *FingerPrintLenOut) |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1310 | { |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1311 | EFI_STATUS Status = EFI_SUCCESS; |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1312 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1313 | if (FingerPrint == NULL || FingerPrintLenOut == NULL || |
| 1314 | FingerPrintLen < AVB_SHA256_DIGEST_SIZE) { |
| 1315 | DEBUG ((EFI_D_ERROR, "GetCertFingerPrint: Invalid parameters\n")); |
| 1316 | return EFI_INVALID_PARAMETER; |
| 1317 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1318 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1319 | if (GetAVBVersion () == AVB_1) { |
| 1320 | QCOM_VERIFIEDBOOT_PROTOCOL *VbIntf = NULL; |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1321 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1322 | Status = gBS->LocateProtocol (&gEfiQcomVerifiedBootProtocolGuid, NULL, |
| 1323 | (VOID **)&VbIntf); |
| 1324 | if (Status != EFI_SUCCESS) { |
| 1325 | DEBUG ((EFI_D_ERROR, "Unable to locate VerifiedBoot Protocol\n")); |
| 1326 | return Status; |
| 1327 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1328 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1329 | if (VbIntf->Revision < QCOM_VERIFIEDBOOT_PROTOCOL_REVISION) { |
| 1330 | DEBUG ((EFI_D_ERROR, "GetCertFingerPrint: VB1: not " |
| 1331 | "supported for this revision\n")); |
| 1332 | return EFI_UNSUPPORTED; |
| 1333 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1334 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1335 | Status = VbIntf->VBGetCertFingerPrint (VbIntf, FingerPrint, FingerPrintLen, |
| 1336 | FingerPrintLenOut); |
| 1337 | if (Status != EFI_SUCCESS) { |
| 1338 | DEBUG ((EFI_D_ERROR, "Failed Reading CERT FingerPrint\n")); |
| 1339 | return Status; |
| 1340 | } |
| 1341 | } else if (GetAVBVersion () == AVB_2) { |
| 1342 | CHAR8 *UserKeyBuffer = NULL; |
| 1343 | UINT32 UserKeyLength = 0; |
| 1344 | AvbSHA256Ctx UserKeyCtx = {{0}}; |
| 1345 | CHAR8 *UserKeyDigest = NULL; |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1346 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1347 | GUARD (GetUserKey (&UserKeyBuffer, &UserKeyLength)); |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1348 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1349 | avb_sha256_init (&UserKeyCtx); |
| 1350 | avb_sha256_update (&UserKeyCtx, (UINT8 *)UserKeyBuffer, UserKeyLength); |
| 1351 | UserKeyDigest = (CHAR8 *)avb_sha256_final (&UserKeyCtx); |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1352 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1353 | CopyMem (FingerPrint, UserKeyDigest, AVB_SHA256_DIGEST_SIZE); |
| 1354 | *FingerPrintLenOut = AVB_SHA256_DIGEST_SIZE; |
| 1355 | } else { |
| 1356 | DEBUG ((EFI_D_ERROR, "GetCertFingerPrint: not supported\n")); |
| 1357 | return EFI_UNSUPPORTED; |
| 1358 | } |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1359 | |
Jeevan Shriram | 17f173d | 2017-10-24 22:11:07 -0700 | [diff] [blame] | 1360 | return Status; |
Shivaprasad Hongal | 7fd6a52 | 2017-05-10 13:56:28 -0700 | [diff] [blame] | 1361 | } |