blob: 4b8705e5844ab63a66745bc80bf27468e21e7b1c [file] [log] [blame]
Mukesh Ojha71be2c82018-02-02 19:13:10 +05301/* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07002 *
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 Hongale3b53392017-04-27 17:32:47 -070031#include "KeymasterClient.h"
32#include "libavb/libavb.h"
Shivaprasad Hongalf1fb0472017-05-23 20:05:16 -070033#include <Library/MenuKeysDetection.h>
Jeevan Shriram17f173d2017-10-24 22:11:07 -070034#include <Library/VerifiedBootMenu.h>
Zhen Kongbb3db752017-07-10 17:07:10 -070035#include <Library/LEOEMCertificate.h>
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070036
37STATIC CONST CHAR8 *VerityMode = " androidboot.veritymode=";
38STATIC CONST CHAR8 *VerifiedState = " androidboot.verifiedbootstate=";
39STATIC CONST CHAR8 *KeymasterLoadState = " androidboot.keymaster=1";
Mukesh Ojha71be2c82018-02-02 19:13:10 +053040STATIC CONST CHAR8 *DmVerityCmd = " root=/dev/dm-0 dm=\"system none ro,0 1 "
41 "android-verity";
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070042STATIC CONST CHAR8 *Space = " ";
Jeevan Shriram17f173d2017-10-24 22:11:07 -070043
Monika Singh43598872018-10-15 12:04:02 +053044#define MAX_NUM_REQ_PARTITION 8
45
46static CHAR8 *avb_verify_partition_name[] = {
47 "boot",
48 "dtbo",
49 "vbmeta",
50 "recovery"
51};
52
Jeevan Shriram17f173d2017-10-24 22:11:07 -070053STATIC struct verified_boot_verity_mode VbVm[] = {
54 {FALSE, "logging"},
55 {TRUE, "enforcing"},
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070056};
57
Jeevan Shriram17f173d2017-10-24 22:11:07 -070058STATIC struct verified_boot_state_name VbSn[] = {
59 {GREEN, "green"},
60 {ORANGE, "orange"},
61 {YELLOW, "yellow"},
62 {RED, "red"},
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070063};
64
Jeevan Shriram17f173d2017-10-24 22:11:07 -070065struct boolean_string {
66 BOOLEAN value;
67 CHAR8 *name;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070068};
69
Jeevan Shriram17f173d2017-10-24 22:11:07 -070070STATIC struct boolean_string BooleanString[] = {
71 {FALSE, "false"},
72 {TRUE, "true"}
73};
Shivaprasad Hongale3b53392017-04-27 17:32:47 -070074
75typedef struct {
Jeevan Shriram17f173d2017-10-24 22:11:07 -070076 AvbOps *Ops;
77 AvbSlotVerifyData *SlotData;
Shivaprasad Hongale3b53392017-04-27 17:32:47 -070078} VB2Data;
79
Jeevan Shriram17f173d2017-10-24 22:11:07 -070080UINT32
81GetAVBVersion ()
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070082{
Zhen Kongbb3db752017-07-10 17:07:10 -070083#if VERIFIED_BOOT_LE
84 return AVB_LE;
85#elif VERIFIED_BOOT_2
86 return AVB_2;
Shivaprasad Hongale3b53392017-04-27 17:32:47 -070087#elif VERIFIED_BOOT
Zhen Kongbb3db752017-07-10 17:07:10 -070088 return AVB_1;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070089#else
Zhen Kongbb3db752017-07-10 17:07:10 -070090 return NO_AVB;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070091#endif
92}
93
Jeevan Shriram17f173d2017-10-24 22:11:07 -070094BOOLEAN
95VerifiedBootEnbled ()
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070096{
Jeevan Shriram17f173d2017-10-24 22:11:07 -070097 return (GetAVBVersion () > NO_AVB);
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -070098}
99
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700100STATIC EFI_STATUS
101AppendVBCmdLine (BootInfo *Info, CONST CHAR8 *Src)
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700102{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700103 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 Hongala2c4dd72017-04-27 14:33:18 -0700107
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700108 GUARD (AsciiStrnCatS (Dst, DstLen, Src, SrcLen));
109 Info->VBCmdLineFilledLen += SrcLen;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700110
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700111 return EFI_SUCCESS;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700112}
113
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700114STATIC EFI_STATUS
115AppendVBCommonCmdLine (BootInfo *Info)
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700116{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700117 EFI_STATUS Status = EFI_SUCCESS;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700118
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700119 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 Hongala2c4dd72017-04-27 14:33:18 -0700126}
127
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700128STATIC EFI_STATUS
Runmin Wang261d2bf2017-11-03 11:40:40 -0700129NoAVBLoadDtboImage (BootInfo *Info, VOID **DtboImage,
130 UINT32 *DtboSize, CHAR16 *Pname)
131{
132 EFI_STATUS Status = EFI_SUCCESS;
133 Slot CurrentSlot;
Bhanuprakash Modem87f2c3e2018-07-09 19:08:13 +0530134 CHAR8 *AsciiPname = NULL;
Bhanuprakash Modemee92f012018-06-21 17:27:14 +0530135 UINT64 PartSize = 0;
136 AvbIOResult AvbStatus;
137 AvbOpsUserData *UserData = NULL;
138 AvbOps *Ops = NULL;
Runmin Wang261d2bf2017-11-03 11:40:40 -0700139
Runmin Wang261d2bf2017-11-03 11:40:40 -0700140 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 Modemee92f012018-06-21 17:27:14 +0530150 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 Modem87f2c3e2018-07-09 19:08:13 +0530171 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 Modemee92f012018-06-21 17:27:14 +0530179 AvbStatus = Ops->get_size_of_partition (Ops,
Bhanuprakash Modem87f2c3e2018-07-09 19:08:13 +0530180 AsciiPname,
Bhanuprakash Modemee92f012018-06-21 17:27:14 +0530181 &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 Wang261d2bf2017-11-03 11:40:40 -0700201 Status = LoadImageFromPartition (*DtboImage, DtboSize, Pname);
Bhanuprakash Modemee92f012018-06-21 17:27:14 +0530202
203out:
204 if (Ops != NULL) {
205 AvbOpsFree (Ops);
206 }
207 if (UserData != NULL) {
208 avb_free (UserData);
209 }
Bhanuprakash Modem87f2c3e2018-07-09 19:08:13 +0530210 if (AsciiPname != NULL) {
211 avb_free (AsciiPname);
212 }
Runmin Wang261d2bf2017-11-03 11:40:40 -0700213 return Status;
214}
215
216STATIC EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700217VBCommonInit (BootInfo *Info)
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700218{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700219 EFI_STATUS Status = EFI_SUCCESS;
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700220
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700221 Info->BootState = RED;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700222
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700223 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 Kongdb33a732017-07-10 12:23:00 -0700227 return Status;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700228 }
229
230 return Status;
Zhen Kongdb33a732017-07-10 12:23:00 -0700231}
232
Jeevan Shriram615b1e12017-10-12 14:10:06 -0700233/*
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 Shriram17f173d2017-10-24 22:11:07 -0700237STATIC EFI_STATUS
238VBAllocateCmdLine (BootInfo *Info)
Zhen Kongdb33a732017-07-10 12:23:00 -0700239{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700240 EFI_STATUS Status = EFI_SUCCESS;
Zhen Kongdb33a732017-07-10 12:23:00 -0700241
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700242 /* 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 Kongdb33a732017-07-10 12:23:00 -0700247 return Status;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700248 }
249 Info->VBCmdLineLen = DTB_PAD_SIZE;
250 Info->VBCmdLineFilledLen = 0;
251 Info->VBCmdLine[Info->VBCmdLineFilledLen] = '\0';
252
253 return Status;
Zhen Kongdb33a732017-07-10 12:23:00 -0700254}
255
Jeevan Shriram615b1e12017-10-12 14:10:06 -0700256STATIC
257BOOLEAN
258IsRootCmdLineUpdated (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 Shriram17f173d2017-10-24 22:11:07 -0700274STATIC EFI_STATUS
275LoadImageNoAuth (BootInfo *Info)
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700276{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700277 EFI_STATUS Status = EFI_SUCCESS;
Runmin Wang261d2bf2017-11-03 11:40:40 -0700278 CHAR16 Pname[MAX_GPT_NAME_SIZE];
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700279
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700280 if (Info->Images[0].ImageBuffer != NULL && Info->Images[0].ImageSize > 0) {
Jeevan Shrirame162e5e2018-04-12 15:59:52 -0700281 /* fastboot boot option, boot image is already loaded, check for dtbo */
282 goto load_dtbo;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700283 }
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700284
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700285 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 Wang261d2bf2017-11-03 11:40:40 -0700295
Jeevan Shrirame162e5e2018-04-12 15:59:52 -0700296
297load_dtbo:
Runmin Wang261d2bf2017-11-03 11:40:40 -0700298 /*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 Modemee92f012018-06-21 17:27:14 +0530303 if (Info->Images[1].ImageBuffer != NULL) {
304 FreePool (Info->Images[1].ImageBuffer);
305 }
Runmin Wang261d2bf2017-11-03 11:40:40 -0700306 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 Modemee92f012018-06-21 17:27:14 +0530311 if (Info->Images[1].ImageBuffer != NULL) {
312 FreePool (Info->Images[1].ImageBuffer);
313 }
Runmin Wang261d2bf2017-11-03 11:40:40 -0700314 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 Shriram17f173d2017-10-24 22:11:07 -0700320 return Status;
321}
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700322
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700323STATIC EFI_STATUS
324LoadImageNoAuthWrapper (BootInfo *Info)
325{
326 EFI_STATUS Status = EFI_SUCCESS;
327 CHAR8 *SystemPath = NULL;
328 UINT32 SystemPathLen = 0;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700329
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700330 GUARD (VBAllocateCmdLine (Info));
331 GUARD (LoadImageNoAuth (Info));
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700332
Jeevan Shriram615b1e12017-10-12 14:10:06 -0700333 if (!IsRootCmdLineUpdated (Info)) {
Jeevan Shriram9a500f12018-05-01 13:02:19 -0700334 SystemPathLen = GetSystemPath (&SystemPath, Info);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700335 if (SystemPathLen == 0 || SystemPath == NULL) {
336 DEBUG ((EFI_D_ERROR, "GetSystemPath failed!\n"));
337 return EFI_LOAD_ERROR;
Zhen Kongdb33a732017-07-10 12:23:00 -0700338 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700339 GUARD (AppendVBCmdLine (Info, SystemPath));
340 }
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700341
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700342 return Status;
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700343}
344
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700345STATIC EFI_STATUS
346LoadImageAndAuthVB1 (BootInfo *Info)
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700347{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700348 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 Ojha71be2c82018-02-02 19:13:10 +0530354 CHAR8 *Temp = NULL;
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700355
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700356 GUARD (VBCommonInit (Info));
357 GUARD (VBAllocateCmdLine (Info));
358 GUARD (LoadImageNoAuth (Info));
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700359
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700360 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 Shriram615b1e12017-10-12 14:10:06 -0700396 if (!IsRootCmdLineUpdated (Info)) {
Jeevan Shriram9a500f12018-05-01 13:02:19 -0700397 SystemPathLen = GetSystemPath (&SystemPath, Info);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700398 if (SystemPathLen == 0 || SystemPath == NULL) {
399 DEBUG ((EFI_D_ERROR, "GetSystemPath failed!\n"));
400 return EFI_LOAD_ERROR;
401 }
Mukesh Ojha71be2c82018-02-02 19:13:10 +0530402 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 Shriram17f173d2017-10-24 22:11:07 -0700411 GUARD (AppendVBCmdLine (Info, SystemPath));
Mukesh Ojha71be2c82018-02-02 19:13:10 +0530412 GUARD (AppendVBCmdLine (Info, "\""));
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700413 }
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 Hongale3b53392017-04-27 17:32:47 -0700420}
421
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700422STATIC BOOLEAN
423ResultShouldContinue (AvbSlotVerifyResult Result)
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700424{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700425 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 Hongale3b53392017-04-27 17:32:47 -0700432
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700433 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 Hongale3b53392017-04-27 17:32:47 -0700439
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700440 return FALSE;
441}
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700442
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700443STATIC EFI_STATUS
Zhen Kongbb3db752017-07-10 17:07:10 -0700444LEGetImageHash (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
520exit:
521 return Status;
522}
523
524STATIC 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;
559exit:
560 return Status;
561}
562STATIC 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;
657exit:
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
667STATIC 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;
709exit:
710 return Status;
711}
712
Monika Singh43598872018-10-15 12:04:02 +0530713static 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
727static 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 Kongbb3db752017-07-10 17:07:10 -0700738
739STATIC EFI_STATUS
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700740LoadImageAndAuthVB2 (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 Singh43598872018-10-15 12:04:02 +0530751 CHAR8 *RequestedPartitionAll[MAX_NUM_REQ_PARTITION] = {NULL};
752 CHAR8 **RequestedPartition = NULL;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700753 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 Hongale3b53392017-04-27 17:32:47 -0700766
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700767 Info->BootState = RED;
768 GUARD (VBCommonInit (Info));
769 GUARD (VBAllocateCmdLine (Info));
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700770
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700771 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 Hongale3b53392017-04-27 17:32:47 -0700777
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700778 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 Hongal82e18e92018-03-01 15:29:25 -0800784 UserData->IsMultiSlot = Info->MultiSlotBoot;
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700785
AnilKumar Chimata681e0f42018-01-19 17:47:56 -0800786 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 Shriram17f173d2017-10-24 22:11:07 -0700796 }
Shivaprasad Hongal39236ae2017-06-06 16:10:35 -0700797
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700798 DEBUG ((EFI_D_VERBOSE, "Slot: %a, allow verification error: %a\n", SlotSuffix,
799 BooleanString[AllowVerificationError].name));
Shivaprasad Hongal39236ae2017-06-06 16:10:35 -0700800
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700801 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 Singh43598872018-10-15 12:04:02 +0530807 RequestedPartition = RequestedPartitionAll;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700808
Monika Singh43598872018-10-15 12:04:02 +0530809 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 Shriram17f173d2017-10-24 22:11:07 -0700849
Mukesh Ojha88a4fb92018-03-09 13:54:54 +0530850 if (SlotData == NULL) {
851 Status = EFI_LOAD_ERROR;
852 Info->BootState = RED;
853 goto out;
854 }
855
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700856 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 Hongale3b53392017-04-27 17:32:47 -0700901
AnilKumar Chimata681e0f42018-01-19 17:47:56 -0800902 if (Info->NumLoadedImages < NumRequestedPartition) {
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700903 DEBUG ((EFI_D_ERROR, "ERROR: AvbSlotVerify slot data error: num of "
904 "loaded partitions %d, requested %d\n",
AnilKumar Chimata681e0f42018-01-19 17:47:56 -0800905 Info->NumLoadedImages, NumRequestedPartition));
Zhen Kong3b3a0df2017-10-04 19:00:09 -0700906 Status = EFI_LOAD_ERROR;
907 goto out;
908 }
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700909
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700910 DEBUG ((EFI_D_VERBOSE, "Total loaded partition %d\n", Info->NumLoadedImages));
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700911
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700912 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 Hongale3b53392017-04-27 17:32:47 -0700921
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700922 GetPageSize (&ImageHdrSize);
AnilKumar Chimata681e0f42018-01-19 17:47:56 -0800923 GUARD_OUT (GetImage (Info, &ImageBuffer, &ImageSize,
924 ((!Info->MultiSlotBoot) &&
925 Info->BootIntoRecovery) ?
926 "recovery" : "boot"));
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700927
AnilKumar Chimata681e0f42018-01-19 17:47:56 -0800928 Status = CheckImageHeader (ImageBuffer, ImageHdrSize,
929 &ImageSizeActual, &PageSize);
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700930 if (Status != EFI_SUCCESS) {
931 DEBUG ((EFI_D_ERROR, "Invalid boot image header:%r\n", Status));
932 goto out;
933 }
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700934
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700935 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 Hongale3b53392017-04-27 17:32:47 -0700944
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700945 /* command line */
946 GUARD_OUT (AppendVBCommonCmdLine (Info));
947 GUARD_OUT (AppendVBCmdLine (Info, SlotData->cmdline));
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700948
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700949 /* Set Rot & Boot State*/
950 Data.Color = Info->BootState;
Zhen Kongbb3db752017-07-10 17:07:10 -0700951 Data. IsUnlocked = AllowVerificationError;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700952 Data.PublicKeyLength = UserData->PublicKeyLen;
953 Data.PublicKey = UserData->PublicKey;
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700954
lijuangbcba7092018-04-10 19:57:54 +0800955 BootImgHdr = (boot_img_hdr *)ImageBuffer;
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700956 Data.SystemSecurityLevel = (BootImgHdr->os_version & 0x7FF);
957 Data.SystemVersion = (BootImgHdr->os_version & 0xFFFFF800) >> 11;
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700958
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700959 GUARD_OUT (KeyMasterSetRotAndBootState (&Data));
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700960
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700961 DEBUG ((EFI_D_INFO, "VB2: Authenticate complete! boot state is: %a\n",
962 VbSn[Info->BootState].name));
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700963
964out:
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700965 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 Chimata681e0f42018-01-19 17:47:56 -0800979 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 Shriram17f173d2017-10-24 22:11:07 -0700989 }
Shivaprasad Hongale3b53392017-04-27 17:32:47 -0700990
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700991 DEBUG ((EFI_D_ERROR, "VB2: boot state: %a(%d)\n", VbSn[Info->BootState].name,
992 Info->BootState));
993 return Status;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700994}
995
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700996STATIC EFI_STATUS
997DisplayVerifiedBootScreen (BootInfo *Info)
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -0700998{
Jeevan Shriram17f173d2017-10-24 22:11:07 -0700999 EFI_STATUS Status = EFI_SUCCESS;
1000 CHAR8 FfbmStr[FFBM_MODE_BUF_SIZE] = {'\0'};
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001001
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001002 if (GetAVBVersion () < AVB_1) {
1003 return EFI_SUCCESS;
1004 }
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001005
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001006 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 Hongala2c4dd72017-04-27 14:33:18 -07001013
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001014 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 }
lijuang3d51d3b2018-07-03 14:29:59 +08001053
1054 /* dm-verity warning */
Jeevan Shriram70d98572018-09-26 15:01:49 -07001055 if ((GetAVBVersion () != AVB_2) &&
1056 !IsEnforcing () &&
lijuang3d51d3b2018-07-03 14:29:59 +08001057 !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 Shriram17f173d2017-10-24 22:11:07 -07001068 return EFI_SUCCESS;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001069}
1070
Zhen Kongbb3db752017-07-10 17:07:10 -07001071STATIC 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 sunfeer4987c692018-04-18 19:29:28 +05301084 CHAR8 *SystemPath = NULL;
1085 UINT32 SystemPathLen = 0;
Zhen Kongbb3db752017-07-10 17:07:10 -07001086
1087 /*Load image*/
mohamed sunfeer4987c692018-04-18 19:29:28 +05301088 GUARD (VBAllocateCmdLine (Info));
Zhen Kongbb3db752017-07-10 17:07:10 -07001089 GUARD (VBCommonInit (Info));
1090 GUARD (LoadImageNoAuth (Info));
1091
Mohamed Sunfeer604b2ed2018-10-30 13:36:02 +05301092 if (!TargetBuildVariantUser ()) {
1093 DEBUG ((EFI_D_INFO, "VB: verification skipped for debug builds\n"));
1094 goto skip_verification;
1095 }
1096
Zhen Kongbb3db752017-07-10 17:07:10 -07001097 /* 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 sunfeer4987c692018-04-18 19:29:28 +05301155
Mohamed Sunfeer604b2ed2018-10-30 13:36:02 +05301156skip_verification:
mohamed sunfeer4987c692018-04-18 19:29:28 +05301157 if (!IsRootCmdLineUpdated (Info)) {
Jeevan Shriram9a500f12018-05-01 13:02:19 -07001158 SystemPathLen = GetSystemPath (&SystemPath, Info);
mohamed sunfeer4987c692018-04-18 19:29:28 +05301159 if (SystemPathLen == 0 ||
1160 SystemPath == NULL) {
1161 return EFI_LOAD_ERROR;
1162 }
1163 GUARD (AppendVBCmdLine (Info, SystemPath));
1164 }
Zhen Kongbb3db752017-07-10 17:07:10 -07001165 return Status;
1166}
1167
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001168EFI_STATUS
1169LoadImageAndAuth (BootInfo *Info)
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001170{
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001171 EFI_STATUS Status = EFI_SUCCESS;
1172 BOOLEAN MdtpActive = FALSE;
1173 QCOM_MDTP_PROTOCOL *MdtpProtocol;
1174 UINT32 AVBVersion = NO_AVB;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001175
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001176 if (Info == NULL) {
1177 DEBUG ((EFI_D_ERROR, "Invalid parameter Info\n"));
1178 return EFI_INVALID_PARAMETER;
1179 }
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001180
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001181 /* 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 Hongalc017e812017-04-26 16:15:27 -07001194
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001195 GUARD (FindBootableSlot (&CurrentSlot));
1196 if (IsSuffixEmpty (&CurrentSlot)) {
1197 DEBUG ((EFI_D_ERROR, "No bootable slot\n"));
1198 return EFI_LOAD_ERROR;
1199 }
Shivaprasad Hongalc017e812017-04-26 16:15:27 -07001200
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001201 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 Hongala2c4dd72017-04-27 14:33:18 -07001206
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001207 DEBUG ((EFI_D_VERBOSE, "MultiSlot %a, partition name %s\n",
1208 BooleanString[Info->MultiSlotBoot].name, Info->Pname));
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001209
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001210 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 Hongala2c4dd72017-04-27 14:33:18 -07001221
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001222 AVBVersion = GetAVBVersion ();
1223 DEBUG ((EFI_D_VERBOSE, "AVB version %d\n", AVBVersion));
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001224
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001225 /* 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 Kongbb3db752017-07-10 17:07:10 -07001236 case AVB_LE:
1237 Status = LoadImageAndAuthForLE (Info);
1238 break;
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001239 default:
1240 DEBUG ((EFI_D_ERROR, "Unsupported AVB version %d\n", AVBVersion));
1241 Status = EFI_UNSUPPORTED;
1242 }
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001243
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001244 // 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 Hongala2c4dd72017-04-27 14:33:18 -07001256
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001257 if (IsUnlocked () && Status != EFI_SUCCESS) {
1258 DEBUG ((EFI_D_ERROR, "LoadImageAndAuth failed %r\n", Status));
1259 return Status;
1260 }
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001261
Zhen Kongbb3db752017-07-10 17:07:10 -07001262 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 Shriram17f173d2017-10-24 22:11:07 -07001270 }
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001271 return Status;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001272}
1273
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001274VOID
1275FreeVerifiedBootResource (BootInfo *Info)
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001276{
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001277 DEBUG ((EFI_D_VERBOSE, "FreeVerifiedBootResource\n"));
Shivaprasad Hongale3b53392017-04-27 17:32:47 -07001278
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001279 if (Info == NULL) {
1280 return;
1281 }
Shivaprasad Hongale3b53392017-04-27 17:32:47 -07001282
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001283 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 Hongale3b53392017-04-27 17:32:47 -07001292
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001293 AvbSlotVerifyData *SlotData = VBData->SlotData;
1294 if (SlotData != NULL) {
1295 avb_slot_verify_data_free (SlotData);
1296 }
1297 avb_free (VBData);
1298 }
Shivaprasad Hongale3b53392017-04-27 17:32:47 -07001299
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001300 if (Info->VBCmdLine != NULL) {
1301 FreePool (Info->VBCmdLine);
1302 }
1303 return;
Shivaprasad Hongala2c4dd72017-04-27 14:33:18 -07001304}
Shivaprasad Hongal7fd6a522017-05-10 13:56:28 -07001305
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001306EFI_STATUS
1307GetCertFingerPrint (UINT8 *FingerPrint,
1308 UINTN FingerPrintLen,
1309 UINTN *FingerPrintLenOut)
Shivaprasad Hongal7fd6a522017-05-10 13:56:28 -07001310{
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001311 EFI_STATUS Status = EFI_SUCCESS;
Shivaprasad Hongal7fd6a522017-05-10 13:56:28 -07001312
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001313 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 Hongal7fd6a522017-05-10 13:56:28 -07001318
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001319 if (GetAVBVersion () == AVB_1) {
1320 QCOM_VERIFIEDBOOT_PROTOCOL *VbIntf = NULL;
Shivaprasad Hongal7fd6a522017-05-10 13:56:28 -07001321
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001322 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 Hongal7fd6a522017-05-10 13:56:28 -07001328
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001329 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 Hongal7fd6a522017-05-10 13:56:28 -07001334
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001335 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 Hongal7fd6a522017-05-10 13:56:28 -07001346
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001347 GUARD (GetUserKey (&UserKeyBuffer, &UserKeyLength));
Shivaprasad Hongal7fd6a522017-05-10 13:56:28 -07001348
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001349 avb_sha256_init (&UserKeyCtx);
1350 avb_sha256_update (&UserKeyCtx, (UINT8 *)UserKeyBuffer, UserKeyLength);
1351 UserKeyDigest = (CHAR8 *)avb_sha256_final (&UserKeyCtx);
Shivaprasad Hongal7fd6a522017-05-10 13:56:28 -07001352
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001353 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 Hongal7fd6a522017-05-10 13:56:28 -07001359
Jeevan Shriram17f173d2017-10-24 22:11:07 -07001360 return Status;
Shivaprasad Hongal7fd6a522017-05-10 13:56:28 -07001361}