blob: 554f250ffab57a16364f2287c98bb5a21471f32d [file] [log] [blame]
Monika Singh5e170362018-03-14 00:48:36 +05301/* Copyright (c) 2018, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#include "libavb/libavb.h"
30#include <malloc.h>
31
32#include <boot_verifier.h>
33#include <ab_partition_parser.h>
34#include <partition_parser.h>
35#include <recovery.h>
36#include <display_menu.h>
37#include <../../../app/aboot/mdtp.h>
38#include <platform/timer.h>
39#include "verifiedboot.h"
40#include <err.h>
41
42#ifndef DTB_PAD_SIZE
43#define DTB_PAD_SIZE 2048
44#endif
45#define INTERMEDIATE_DIGEST_LENGTH 64
46#define MAX_PART_NAME_SIZE 10
47
48#ifndef MDTP_SUPPORT
49int mdtp_activated(bool * activated)
50{
51 return 0;
52}
53void mdtp_fwlock_verify_lock(mdtp_ext_partition_verification_t *ext_partition)
54{
55 return;
56}
57#endif
58
59static const CHAR8 *VerifiedState = " androidboot.verifiedbootstate=";
60static const CHAR8 *KeymasterLoadState = " androidboot.keymaster=1";
61static const CHAR8 *Space = " ";
62#if !VERIFIED_BOOT_2
63static const CHAR8 *VerityMode = " androidboot.veritymode=";
64static struct verified_boot_verity_mode VbVm[] =
65{
66 {FALSE, "logging"},
67 {TRUE, "enforcing"},
68};
69#endif
70
71static struct verified_boot_state_name VbSn[] =
72{
73 {GREEN, "green"},
74 {ORANGE, "orange"},
75 {YELLOW, "yellow"},
76 {RED, "red"},
77};
78
79struct boolean_string
80{
81 BOOLEAN value;
82 CHAR8 *name;
83};
84
85static struct boolean_string BooleanString[] =
86{
87 {FALSE, "false"},
88 {TRUE, "true"}
89};
90
91
92typedef struct {
93 AvbOps *Ops;
94 AvbSlotVerifyData *SlotData;
95} VB2Data;
96
97UINT32 GetAVBVersion()
98{
99#if VERIFIED_BOOT_2
100 return 2;
101#elif VERIFIED_BOOT
102 return 1;
103#else
104 return 0;
105#endif
106}
107
108BOOLEAN VerifiedBootEnabled()
109{
110 return (GetAVBVersion() > NO_AVB);
111}
112
Monika Singh5e170362018-03-14 00:48:36 +0530113static int check_img_header(void *ImageHdrBuffer, uint32_t ImageHdrSize, uint32_t *imgsizeActual)
114{
115 /* These checks are already done before calling auth remove from here */
116#if VERIFIED_BOOT || VERIFIED_BOOT_2
117 boot_verifier_init();
118#endif
119 return 0;
120}
121
Monika Singh5e170362018-03-14 00:48:36 +0530122static int HandleActiveSlotUnbootable()
123{
124 int curr_slot;
125 curr_slot = partition_find_active_slot();
126 partition_deactivate_slot(curr_slot);
127 partition_find_boot_slot();
128
129 // should not reach here
130 return ERROR;
131}
132
133/*
134 * Returns length = 0 when there is failure.
135 */
136uint32_t GetSystemPath(char **SysPath)
137{
138 INT32 Index;
139 UINT32 Lun;
140 CHAR8 PartitionName[MAX_GPT_NAME_SIZE];
Monika Singh5e170362018-03-14 00:48:36 +0530141 CHAR8 LunCharMapping[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
Monika Singh0f7bfc82018-04-16 23:14:29 +0530142 const char *current_slot_suffix;
143 int current_active_slot;
Monika Singh5e170362018-03-14 00:48:36 +0530144
145 *SysPath = malloc(sizeof(char) * MAX_PATH_SIZE);
146 if (!*SysPath) {
147 dprintf(CRITICAL, "Failed to allocated memory for System path query\n");
148 return 0;
149 }
150
Monika Singh96b97212018-04-13 18:27:55 +0530151 strlcpy(PartitionName, "system", strlen("system") + 1);
Monika Singh0f7bfc82018-04-16 23:14:29 +0530152 current_active_slot = partition_find_active_slot();
153 if (partition_multislot_is_supported()) {
154 if (current_active_slot == INVALID)
155 return 0;
156 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
157 strncat(PartitionName, current_slot_suffix, MAX_GPT_NAME_SIZE - 1);
158 }
Monika Singh5e170362018-03-14 00:48:36 +0530159
160 Index = partition_get_index(PartitionName);
161 if (Index == INVALID_PTN || Index >= NUM_PARTITIONS) {
162 dprintf(CRITICAL, "System partition does not exit\n");
163 free(*SysPath);
164 return 0;
165 }
166
167 Lun = partition_get_lun(Index);
168 if (platform_boot_dev_isemmc()) {
169 snprintf(*SysPath, MAX_PATH_SIZE, " root=/dev/mmcblk0p%d",
170 Index + 1);
171 } else {
172 snprintf(*SysPath, MAX_PATH_SIZE, " root=/dev/sd%c%d",
173 LunCharMapping[Lun],
174 partition_get_index_in_lun(PartitionName, Lun));
175 }
176
177 dprintf(DEBUG, "System Path - %s \n", *SysPath);
178
179 return strlen(*SysPath);
180}
181
182static EFI_STATUS Appendvbcmdline(bootinfo *Info, const CHAR8 *Src)
183{
184 INT32 SrcLen = strlen(Src);
185 CHAR8 *Dst = (CHAR8 *)Info->vbcmdline + Info->vbcmdline_filled_len;
186
187 strlcat(Dst, Src, SrcLen);
188 Info->vbcmdline_filled_len += SrcLen;
189
190 return EFI_SUCCESS;
191}
192
193static EFI_STATUS AppendVBCommonCmdLine(bootinfo *Info)
194{
195 EFI_STATUS Status = EFI_SUCCESS;
196
197 if (GetAVBVersion() >= AVB_1) {
198 GUARD(Appendvbcmdline(Info, VerifiedState));
199 GUARD(Appendvbcmdline(Info, VbSn[Info->boot_state].name));
200 }
201 GUARD(Appendvbcmdline(Info, KeymasterLoadState));
202 GUARD(Appendvbcmdline(Info, Space));
203 return EFI_SUCCESS;
204}
205
206static EFI_STATUS VBCommonInit(bootinfo *Info)
207{
208 EFI_STATUS Status = EFI_SUCCESS;
209
210 Info->boot_state = RED;
211
212 // FIXME: Add boot call
213 /* allocate VB command line*/
214 Info->vbcmdline = malloc(2*DTB_PAD_SIZE);
215 if (Info->vbcmdline == NULL) {
216 dprintf(CRITICAL, "VB CmdLine allocation failed!\n");
217 Status = EFI_OUT_OF_RESOURCES;
218 return Status;
219 }
220 Info->vbcmdline_len = 2*DTB_PAD_SIZE;
221 Info->vbcmdline_filled_len = 0;
222 Info->vbcmdline[Info->vbcmdline_filled_len] = '\0';
223
224 return Status;
225}
226
227#if VERIFIED_BOOT_2
228/* Disable for VB 2.0 as this path is never taken */
229static EFI_STATUS LoadImageNoAuth(bootinfo *Info)
230{
231 return ERROR;
232}
233static EFI_STATUS load_image_and_authVB1(bootinfo *Info)
234{
235 return ERROR;
236}
237#else
238static EFI_STATUS LoadImageNoAuth(bootinfo *Info)
239{
240 EFI_STATUS Status = EFI_SUCCESS;
241
242 if (Info->images[0].image_buffer != NULL && Info->images[0].imgsize > 0) {
243 /* fastboot boot option image already loaded */
244 return Status;
245 }
246
247 Status = LoadImage(Info->pname, (VOID **)&(Info->images[0].image_buffer),
248 (UINT32 *)&(Info->images[0].imgsize));
249 if (Status != EFI_SUCCESS) {
250 dprintf(CRITICAL,
251 "ERROR: Failed to load image from partition: %d\n", Status);
252 return EFI_LOAD_ERROR;
253 }
254 Info->num_loaded_images = 1;
255 Info->images[0].name = malloc(strlen(Info->pname) + 1);
Monika Singh96b97212018-04-13 18:27:55 +0530256 strlcpy(Info->images[0].name, Info->pname, strlen(Info->pname)); //FIXME
Monika Singh5e170362018-03-14 00:48:36 +0530257 return Status;
258}
259
260static EFI_STATUS load_image_and_authVB1(bootinfo *Info)
261{
262 EFI_STATUS Status = EFI_SUCCESS;
263 CHAR8 StrPname[MAX_GPT_NAME_SIZE];
264 CHAR8 Pname[MAX_GPT_NAME_SIZE];
265 CHAR8 *SystemPath = NULL;
266 UINT32 SystemPathLen = 0;
267 device_info DevInfo_vb;
268
269 GUARD(VBCommonInit(Info));
270 GUARD(LoadImageNoAuth(Info));
271 boot_verifier_init();
272
273 // FIXME: INIT devinfo()
274 DevInfo_vb.is_unlocked = !is_device_locked();
275 DevInfo_vb.is_unlock_critical = !is_device_locked_critical();
276
Monika Singh96b97212018-04-13 18:27:55 +0530277 strlcpy(StrPname, "/", strlen("/"));
278 strlcpy(Pname, Info->pname, strlen(Info->pname));
Monika Singh5e170362018-03-14 00:48:36 +0530279 if (Info->multi_slot_boot) {
Monika Singh96b97212018-04-13 18:27:55 +0530280 strlcat(StrPname, Pname,
Monika Singh5e170362018-03-14 00:48:36 +0530281 strlen(Pname) - (MAX_SLOT_SUFFIX_SZ - 1));
282 } else {
Monika Singh96b97212018-04-13 18:27:55 +0530283 strlcat(StrPname, Pname, strlen(Pname));
Monika Singh5e170362018-03-14 00:48:36 +0530284 }
285
286 Status = boot_verify_image((UINT8 *)Info->images[0].image_buffer,
287 Info->images[0].imgsize,
288 StrPname,
289 &Info->boot_state);
290 if (Status != EFI_SUCCESS || Info->boot_state == BOOT_STATE_MAX) {
291 dprintf(CRITICAL, "VBVerifyImage failed with: %d\n", Status);
292 return Status;
293 }
294
295 set_os_version((unsigned char *)Info->images[0].image_buffer);
296 if(!send_rot_command((uint32_t)DevInfo_vb.is_unlocked))
297 return EFI_LOAD_ERROR;
298
299 SystemPathLen = GetSystemPath(&SystemPath);
300 if (SystemPathLen == 0 || SystemPath == NULL) {
301 dprintf(CRITICAL, "GetSystemPath failed!\n");
302 return EFI_LOAD_ERROR;
303 }
304 GUARD(AppendVBCommonCmdLine(Info));
305 GUARD(Appendvbcmdline(Info, VerityMode));
306 GUARD(Appendvbcmdline(Info, VbVm[is_verity_enforcing()].name));
307 GUARD(Appendvbcmdline(Info, SystemPath));
308
309 Info->vb_data = NULL;
310 return Status;
311}
312#endif
313
314static BOOLEAN ResultShouldContinue(AvbSlotVerifyResult Result)
315{
316 switch (Result) {
317 case AVB_SLOT_VERIFY_RESULT_ERROR_OOM:
318 case AVB_SLOT_VERIFY_RESULT_ERROR_IO:
319 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA:
320 case AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION:
321 case AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT:
322 return FALSE;
323
324 case AVB_SLOT_VERIFY_RESULT_OK:
325 case AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION:
326 case AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX:
327 case AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED:
328 return TRUE;
329 }
330
331 return FALSE;
332}
333
334char *pname[] = {
335 "sbl1",
336 "rpm",
337 "tz",
338 "aboot",
339};
340
341static EFI_STATUS load_image_and_authVB2(bootinfo *Info)
342{
343 EFI_STATUS Status = EFI_SUCCESS;
344 AvbSlotVerifyResult Result;
345 AvbSlotVerifyData *SlotData = NULL;
346 VB2Data *VBData = NULL;
347 AvbOpsUserData *UserData = NULL;
348 AvbOps *Ops = NULL;
349 CHAR8 Pname[MAX_GPT_NAME_SIZE] = {0};
350 CHAR8 *SlotSuffix = NULL;
351 BOOLEAN AllowVerificationError = !is_device_locked();
352 BOOLEAN VerityEnforcing = is_verity_enforcing();
353 const CHAR8 *RequestedPartitionMission[] = {"boot", "dtbo", NULL};
354 const CHAR8 *RequestedPartitionRecovery[] = {"recovery", "dtbo", NULL};
355 const CHAR8 **RequestedPartition = NULL;
356 UINTN NumRequestedPartition = 0;
357 UINT32 ImageHdrSize = 0;
358 UINT32 imgsizeActual = 0;
359 VOID *image_buffer = NULL;
360 UINT32 imgsize = 0;
361 AvbSlotVerifyFlags VerifyFlags = AllowVerificationError ?
362 AVB_SLOT_VERIFY_FLAGS_ALLOW_VERIFICATION_ERROR :
363 AVB_SLOT_VERIFY_FLAGS_NONE;
364 AvbHashtreeErrorMode VerityFlags = AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE;
365 device_info DevInfo_vb;
366
367 Info->boot_state = RED;
368 GUARD(VBCommonInit(Info));
369
370 UserData = avb_calloc(sizeof(AvbOpsUserData));
371 if (UserData == NULL) {
372 dprintf(CRITICAL,
373 "ERROR: Failed to allocate AvbOpsUserData\n");
374 Status = EFI_OUT_OF_RESOURCES;
375 goto out;
376 }
377
378 Ops = AvbOpsNew(UserData);
379 if (Ops == NULL) {
380 dprintf(CRITICAL, "ERROR: Failed to allocate AvbOps\n");
381 Status = EFI_OUT_OF_RESOURCES;
382 goto out;
383 }
384 if(Info->multi_slot_boot) {
Monika Singh96b97212018-04-13 18:27:55 +0530385 strlcpy(Pname, Info->pname, strlen(Info->pname));
Monika Singh5e170362018-03-14 00:48:36 +0530386 if ((MAX_SLOT_SUFFIX_SZ + 1) > strlen(Pname)) {
387 dprintf(CRITICAL, "ERROR: Can not determine slot suffix\n");
388 Status = EFI_INVALID_PARAMETER;
389 goto out;
390 }
391 SlotSuffix = &Pname[strlen(Pname) - MAX_SLOT_SUFFIX_SZ + 1];
392 } else {
393 SlotSuffix = "\0";
394 }
395
396 if(!Info->multi_slot_boot && Info->bootinto_recovery) {
397 RequestedPartition = RequestedPartitionRecovery;
398 NumRequestedPartition = ARRAY_SIZE (RequestedPartitionRecovery) - 1;
399 if (Info->num_loaded_images) {
400 /* fastboot boot option, skip Index 0, as boot image already
401 * loaded */
402 RequestedPartition = &RequestedPartitionRecovery[1];
403 }
404 } else {
405 RequestedPartition = RequestedPartitionMission;
406 NumRequestedPartition = ARRAY_SIZE(RequestedPartitionMission) - 1;
407 if (Info->num_loaded_images) {
408 /* fastboot boot option, skip Index 0, as boot image already
409 * loaded */
410 RequestedPartition = &RequestedPartitionMission[1];
411 }
412 }
413 if (Info->num_loaded_images) {
414 NumRequestedPartition--;
415 }
416
417 // FIXME: is this correct?
418 VerityFlags = VerityEnforcing ?
419 AVB_HASHTREE_ERROR_MODE_RESTART :
420 AVB_HASHTREE_ERROR_MODE_EIO;
421
422 Result = avb_slot_verify(Ops, RequestedPartition, SlotSuffix,
423 VerifyFlags, VerityFlags,
424 &SlotData);
425
426 if (AllowVerificationError && ResultShouldContinue(Result)) {
427 dprintf(CRITICAL, "State: Unlocked, AvbSlotVerify returned "
428 "%s, continue boot\n",
429 avb_slot_verify_result_to_string(Result));
430 } else if (Result != AVB_SLOT_VERIFY_RESULT_OK) {
431 dprintf(CRITICAL,
432 "ERROR: Device State %s, AvbSlotVerify returned %s\n",
433 AllowVerificationError ? "Unlocked" : "Locked",
434 avb_slot_verify_result_to_string(Result));
435 Status = EFI_LOAD_ERROR;
436 Info->boot_state = RED;
437 goto out;
438 }
Monika Singh0f7bfc82018-04-16 23:14:29 +0530439 if (SlotData == NULL) {
440 Status = EFI_LOAD_ERROR;
441 Info->boot_state = RED;
442 goto out;
443 }
Monika Singh5e170362018-03-14 00:48:36 +0530444
445 for (UINTN ReqIndex = 0; ReqIndex < NumRequestedPartition; ReqIndex++) {
446 dprintf(DEBUG, "Requested Partition: %s\n",
447 RequestedPartition[ReqIndex]);
448 for (UINTN loadedindex = 0;
449 loadedindex < SlotData->num_loaded_partitions; loadedindex++) {
450 dprintf(DEBUG, "Loaded Partition: %s\n",
451 SlotData->loaded_partitions[loadedindex].partition_name);
452 if (!strncmp((const char *)
453 RequestedPartition[ReqIndex],
454 SlotData->loaded_partitions[loadedindex].partition_name,
455 strlen(SlotData->loaded_partitions[loadedindex]
456 .partition_name))) {
457 if (Info->num_loaded_images >= ARRAY_SIZE(Info->images)) {
458 dprintf(CRITICAL, "NumLoadedPartition"
459 "(%d) too large "
460 "max images(%d)\n",
461 Info->num_loaded_images,
462 ARRAY_SIZE(Info->images));
463 Status = EFI_LOAD_ERROR;
464 Info->boot_state = RED;
465 goto out;
466 }
467 Info->images[Info->num_loaded_images].name =
468 SlotData->loaded_partitions[loadedindex].partition_name;
469 Info->images[Info->num_loaded_images].image_buffer =
470 SlotData->loaded_partitions[loadedindex].data;
471 Info->images[Info->num_loaded_images].imgsize =
472 SlotData->loaded_partitions[loadedindex].data_size;
473 Info->num_loaded_images++;
474 break;
475 }
476 }
477 }
478
479 if (Info->num_loaded_images < NumRequestedPartition) {
480 dprintf(CRITICAL, "ERROR: AvbSlotVerify slot data: num of loaded partitions %d, requested %lu\n",Info->num_loaded_images, NumRequestedPartition);
481 Status = EFI_LOAD_ERROR;
482 goto out;
483 }
484
485 dprintf(DEBUG, "Total loaded partition %d\n", Info->num_loaded_images);
486
487 VBData = (VB2Data *)avb_calloc(sizeof(VB2Data));
488 if (VBData == NULL) {
489 dprintf(CRITICAL, "ERROR: Failed to allocate VB2Data\n");
490 Status = EFI_OUT_OF_RESOURCES;
491 goto out;
492 }
493 VBData->Ops = Ops;
494 VBData->SlotData = SlotData;
495 Info->vb_data = (VOID *)VBData;
496
497 ImageHdrSize = get_page_size();
498 GUARD_OUT(getimage(Info, &image_buffer, &imgsize,(!Info->multi_slot_boot && Info->bootinto_recovery) ? "recovery" : "boot") );
499
500 Status = check_img_header(image_buffer, ImageHdrSize, &imgsizeActual);
501 if (Status != EFI_SUCCESS) {
502 dprintf(CRITICAL, "Invalid boot image header:%d\n", Status);
503 goto out;
504 }
505
506 if (imgsizeActual > imgsize) {
507 Status = EFI_BUFFER_TOO_SMALL;
508 dprintf(CRITICAL,
509 "Boot size in vbmeta less than actual boot image size "
510 "flash corresponding vbmeta.img\n");
511 goto out;
512 }
513 if (AllowVerificationError) {
514 Info->boot_state = ORANGE;
515 } else {
516 if (UserData->IsUserKey) {
517 Info->boot_state = YELLOW;
518 } else {
519 Info->boot_state = GREEN;
520 }
521 }
522
523 /* command line */
524 GUARD_OUT(AppendVBCommonCmdLine(Info));
525 GUARD_OUT(Appendvbcmdline(Info, SlotData->cmdline));
526 DevInfo_vb.is_unlocked = !is_device_locked();
527 set_os_version((unsigned char *)Info->images[0].image_buffer);
528 if(!send_rot_command((uint32_t)DevInfo_vb.is_unlocked))
529 return EFI_LOAD_ERROR;
530 dprintf(INFO, "VB2: Authenticate complete! boot state is: %s\n",
531 VbSn[Info->boot_state].name);
532
533out:
534 if (Status != EFI_SUCCESS) {
535 if (SlotData != NULL) {
536 avb_slot_verify_data_free(SlotData);
537 }
538 if (Ops != NULL) {
539 AvbOpsFree(Ops);
540 }
541 if (UserData != NULL) {
542 avb_free(UserData);
543 }
544 if (VBData != NULL) {
545 avb_free(VBData);
546 }
547 Info->boot_state = RED;
548 if(Info->multi_slot_boot) {
549 HandleActiveSlotUnbootable();
550 /* HandleActiveSlotUnbootable should have swapped slots and
551 * reboot the device. If no bootable slot found, enter fastboot */
552 dprintf(CRITICAL, "No bootable slots found enter fastboot mode\n");
553 } else {
554 dprintf(CRITICAL, "Non Multi-slot: Unbootable entering fastboot mode\n");
555 }
556
557 }
558
559 dprintf(CRITICAL, "VB2: boot state: %s(%d)\n",
560 VbSn[Info->boot_state].name, Info->boot_state);
561 return Status;
562}
563
564static EFI_STATUS DisplayVerifiedBootScreen(bootinfo *Info)
565{
566 EFI_STATUS Status = EFI_SUCCESS;
567 CHAR8 ffbm_mode_string[FFBM_MODE_BUF_SIZE] = {'\0'};
568
569 if (GetAVBVersion() < AVB_1) {
570 return EFI_SUCCESS;
571 }
572
573 if (!strncmp(Info->pname, "boot", strlen("boot"))) {
574 Status = get_ffbm(ffbm_mode_string, FFBM_MODE_BUF_SIZE);
575 if (Status != EFI_SUCCESS) {
576 dprintf(DEBUG,
577 "No Ffbm cookie found, ignore: %d\n", Status);
578 ffbm_mode_string[0] = '\0';
579 }
580 }
581
582 dprintf(DEBUG, "Boot State is : %d\n", Info->boot_state);
583 switch (Info->boot_state)
584 {
585 case RED:
586 display_bootverify_menu(DISPLAY_MENU_RED);
587 //if (Status != EFI_SUCCESS) {
588 dprintf(INFO, "Your device is corrupt. It can't be trusted and will not boot." \
589 "\nYour device will shutdown in 30s\n");
590 //}
591 udelay(30000000);
592 shutdown_device();
593 break;
594 case YELLOW:
595 display_bootverify_menu(DISPLAY_MENU_YELLOW);
596 //if (Status == EFI_SUCCESS) {
597 wait_for_users_action();
598 //} else {
599 dprintf(INFO, "Your device has loaded a different operating system." \
600 "\nWait for 5 seconds before proceeding\n");
601 udelay(5000000);
602 //}
603 break;
604 case ORANGE:
605 if (ffbm_mode_string[0] != '\0' && !target_build_variant_user()) {
606 dprintf(DEBUG, "Device will boot into FFBM mode\n");
607 } else {
Monika Singh90cf4b12018-04-09 16:47:15 +0530608 display_bootverify_menu(DISPLAY_MENU_ORANGE);
609 if (Status == EFI_SUCCESS) {
610 wait_for_users_action();
611 } else {
Monika Singh5e170362018-03-14 00:48:36 +0530612 dprintf(INFO, "Device is unlocked, Skipping boot verification\n");
613 udelay(5000000);
Monika Singh90cf4b12018-04-09 16:47:15 +0530614 }
Monika Singh5e170362018-03-14 00:48:36 +0530615 }
616 break;
617 default:
618 break;
619 }
620 return EFI_SUCCESS;
621}
622
623EFI_STATUS load_image_and_auth(bootinfo *Info)
624{
625 EFI_STATUS Status = EFI_SUCCESS;
626 BOOLEAN MdtpActive = FALSE;
627 UINT32 AVBVersion = NO_AVB;
628 mdtp_ext_partition_verification_t ext_partition;
Monika Singh0f7bfc82018-04-16 23:14:29 +0530629 const char *current_slot_suffix;
630 int current_active_slot;
Monika Singh5e170362018-03-14 00:48:36 +0530631
632 if (Info == NULL) {
633 dprintf(CRITICAL, "Invalid parameter Info\n");
634 return EFI_INVALID_PARAMETER;
635 }
636
637 if (!Info->multi_slot_boot) {
638 if (Info->bootinto_recovery) {
639 dprintf(INFO, "Booting Into Recovery Mode\n");
Monika Singh96b97212018-04-13 18:27:55 +0530640 strlcpy(Info->pname, "recovery", strlen("recovery"));
Monika Singh5e170362018-03-14 00:48:36 +0530641 } else {
642 dprintf(INFO, "Booting Into Mission Mode\n");
Monika Singh96b97212018-04-13 18:27:55 +0530643 strlcpy(Info->pname, "boot", strlen("boot"));
Monika Singh5e170362018-03-14 00:48:36 +0530644 }
645 } else {
Monika Singh96b97212018-04-13 18:27:55 +0530646 strlcpy(Info->pname, "boot", strlen("boot"));
Monika Singh0f7bfc82018-04-16 23:14:29 +0530647 current_active_slot = partition_find_active_slot();
648 if (current_active_slot != INVALID ) {
649 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
650 if (strlen(current_slot_suffix) == 0) {
651 dprintf(CRITICAL, "No bootable slot\n");
652 return EFI_LOAD_ERROR;
653 }
654 strlcat(Info->pname, current_slot_suffix, strlen(current_slot_suffix));
655 }
Monika Singh5e170362018-03-14 00:48:36 +0530656 }
657
658 dprintf(DEBUG, "MultiSlot %s, partition name %s\n",
659 BooleanString[Info->multi_slot_boot].name, Info->pname);
660
661 Status = mdtp_activated(&MdtpActive);
662 if (Status) {
663 dprintf(CRITICAL,
664 "Failed to get activation state for MDTP, "
665 "Status=%d."
666 " Considering MDTP as active and continuing \n",
667 Status);
668 if (Status != -1)
669 MdtpActive = TRUE;
670 }
671
672 AVBVersion = GetAVBVersion();
673 dprintf(DEBUG, "AVB version %d\n", AVBVersion);
674
675 /* Load and Authenticate */
676 switch (AVBVersion) {
677 case NO_AVB:
678 return LoadImageNoAuth(Info);
679 break;
680 case AVB_1:
681 Status = load_image_and_authVB1(Info);
682 break;
683 case AVB_2:
684 Status = load_image_and_authVB2(Info);
685 break;
686 default:
687 dprintf(CRITICAL, "Unsupported AVB version %d\n", AVBVersion);
688 Status = EFI_UNSUPPORTED;
689 }
690
691 // if MDTP is active Display Recovery UI
692 if (Status != EFI_SUCCESS && MdtpActive && !target_use_signed_kernel()) {
693 //FIXME: Hard coded to BOOT
694 ext_partition.partition = Info->bootinto_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
695 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
696 ext_partition.page_size = get_page_size();
697 ext_partition.image_addr = (uint32)Info->images[0].image_buffer;
698 ext_partition.image_size = Info->images[0].imgsize;
699 ext_partition.sig_avail = FALSE;
700 mdtp_fwlock_verify_lock(&ext_partition);
701 }
702
703 if (!is_device_locked() && Status != EFI_SUCCESS) {
704 dprintf(CRITICAL, "load_image_and_auth failed %d\n", Status);
705 return Status;
706 }
707
708 DisplayVerifiedBootScreen(Info);
709
710 return Status;
711}
712
713#if VERIFIED_BOOT_2
714VOID free_verified_boot_resource(bootinfo *Info)
715{
716 dprintf(DEBUG, "free_verified_boot_resource\n");
717
718 if (Info == NULL) {
719 return;
720 }
721
722 VB2Data *VBData = Info->vb_data;
723 if (VBData != NULL) {
724 AvbOps *Ops = VBData->Ops;
725 if (Ops != NULL) {
726 if (Ops->user_data != NULL) {
727 avb_free(Ops->user_data);
728 }
729 AvbOpsFree(Ops);
730 }
731
732 AvbSlotVerifyData *SlotData = VBData->SlotData;
733 if (SlotData != NULL) {
734 avb_slot_verify_data_free(SlotData);
735 }
736 avb_free(VBData);
737 }
738
739 if (Info->vbcmdline != NULL) {
740 free(Info->vbcmdline);
741 }
742 return;
743}
744#else
745VOID free_verified_boot_resource(bootinfo *Info)
746{
747 return;
748}
749#endif