Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 1 | /* 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 |
| 49 | int mdtp_activated(bool * activated) |
| 50 | { |
| 51 | return 0; |
| 52 | } |
| 53 | void mdtp_fwlock_verify_lock(mdtp_ext_partition_verification_t *ext_partition) |
| 54 | { |
| 55 | return; |
| 56 | } |
| 57 | #endif |
| 58 | |
| 59 | static const CHAR8 *VerifiedState = " androidboot.verifiedbootstate="; |
| 60 | static const CHAR8 *KeymasterLoadState = " androidboot.keymaster=1"; |
| 61 | static const CHAR8 *Space = " "; |
| 62 | #if !VERIFIED_BOOT_2 |
| 63 | static const CHAR8 *VerityMode = " androidboot.veritymode="; |
| 64 | static struct verified_boot_verity_mode VbVm[] = |
| 65 | { |
| 66 | {FALSE, "logging"}, |
| 67 | {TRUE, "enforcing"}, |
| 68 | }; |
| 69 | #endif |
| 70 | |
| 71 | static struct verified_boot_state_name VbSn[] = |
| 72 | { |
| 73 | {GREEN, "green"}, |
| 74 | {ORANGE, "orange"}, |
| 75 | {YELLOW, "yellow"}, |
| 76 | {RED, "red"}, |
| 77 | }; |
| 78 | |
| 79 | struct boolean_string |
| 80 | { |
| 81 | BOOLEAN value; |
| 82 | CHAR8 *name; |
| 83 | }; |
| 84 | |
| 85 | static struct boolean_string BooleanString[] = |
| 86 | { |
| 87 | {FALSE, "false"}, |
| 88 | {TRUE, "true"} |
| 89 | }; |
| 90 | |
| 91 | |
| 92 | typedef struct { |
| 93 | AvbOps *Ops; |
| 94 | AvbSlotVerifyData *SlotData; |
| 95 | } VB2Data; |
| 96 | |
| 97 | UINT32 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 | |
| 108 | BOOLEAN VerifiedBootEnabled() |
| 109 | { |
| 110 | return (GetAVBVersion() > NO_AVB); |
| 111 | } |
| 112 | |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 113 | static 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 Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 122 | static 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 | */ |
| 136 | uint32_t GetSystemPath(char **SysPath) |
| 137 | { |
| 138 | INT32 Index; |
| 139 | UINT32 Lun; |
| 140 | CHAR8 PartitionName[MAX_GPT_NAME_SIZE]; |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 141 | CHAR8 LunCharMapping[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; |
Monika Singh | 0f7bfc8 | 2018-04-16 23:14:29 +0530 | [diff] [blame] | 142 | const char *current_slot_suffix; |
| 143 | int current_active_slot; |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 144 | |
| 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 Singh | 96b9721 | 2018-04-13 18:27:55 +0530 | [diff] [blame] | 151 | strlcpy(PartitionName, "system", strlen("system") + 1); |
Monika Singh | 0f7bfc8 | 2018-04-16 23:14:29 +0530 | [diff] [blame] | 152 | 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); |
Monika Singh | 83dacd4 | 2018-05-16 12:13:37 +0530 | [diff] [blame^] | 157 | strlcat(PartitionName, current_slot_suffix, MAX_GPT_NAME_SIZE - 1); |
Monika Singh | 0f7bfc8 | 2018-04-16 23:14:29 +0530 | [diff] [blame] | 158 | } |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 159 | |
| 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 | |
| 182 | static 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 | |
| 193 | static 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 | |
| 206 | static 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 */ |
| 229 | static EFI_STATUS LoadImageNoAuth(bootinfo *Info) |
| 230 | { |
| 231 | return ERROR; |
| 232 | } |
| 233 | static EFI_STATUS load_image_and_authVB1(bootinfo *Info) |
| 234 | { |
| 235 | return ERROR; |
| 236 | } |
| 237 | #else |
| 238 | static 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 Singh | 96b9721 | 2018-04-13 18:27:55 +0530 | [diff] [blame] | 256 | strlcpy(Info->images[0].name, Info->pname, strlen(Info->pname)); //FIXME |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 257 | return Status; |
| 258 | } |
| 259 | |
| 260 | static 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 Singh | 96b9721 | 2018-04-13 18:27:55 +0530 | [diff] [blame] | 277 | strlcpy(StrPname, "/", strlen("/")); |
| 278 | strlcpy(Pname, Info->pname, strlen(Info->pname)); |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 279 | if (Info->multi_slot_boot) { |
Monika Singh | 96b9721 | 2018-04-13 18:27:55 +0530 | [diff] [blame] | 280 | strlcat(StrPname, Pname, |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 281 | strlen(Pname) - (MAX_SLOT_SUFFIX_SZ - 1)); |
| 282 | } else { |
Monika Singh | 96b9721 | 2018-04-13 18:27:55 +0530 | [diff] [blame] | 283 | strlcat(StrPname, Pname, strlen(Pname)); |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 284 | } |
| 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 | |
| 314 | static 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 | |
| 334 | char *pname[] = { |
| 335 | "sbl1", |
| 336 | "rpm", |
| 337 | "tz", |
| 338 | "aboot", |
| 339 | }; |
| 340 | |
| 341 | static 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 | } |
Monika Singh | 2c9d2b8 | 2018-05-01 12:24:59 +0530 | [diff] [blame] | 384 | UserData->IsMultiSlot = Info->multi_slot_boot; |
| 385 | |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 386 | if(Info->multi_slot_boot) { |
Monika Singh | 96b9721 | 2018-04-13 18:27:55 +0530 | [diff] [blame] | 387 | strlcpy(Pname, Info->pname, strlen(Info->pname)); |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 388 | if ((MAX_SLOT_SUFFIX_SZ + 1) > strlen(Pname)) { |
| 389 | dprintf(CRITICAL, "ERROR: Can not determine slot suffix\n"); |
| 390 | Status = EFI_INVALID_PARAMETER; |
| 391 | goto out; |
| 392 | } |
| 393 | SlotSuffix = &Pname[strlen(Pname) - MAX_SLOT_SUFFIX_SZ + 1]; |
| 394 | } else { |
| 395 | SlotSuffix = "\0"; |
| 396 | } |
| 397 | |
| 398 | if(!Info->multi_slot_boot && Info->bootinto_recovery) { |
| 399 | RequestedPartition = RequestedPartitionRecovery; |
| 400 | NumRequestedPartition = ARRAY_SIZE (RequestedPartitionRecovery) - 1; |
| 401 | if (Info->num_loaded_images) { |
| 402 | /* fastboot boot option, skip Index 0, as boot image already |
| 403 | * loaded */ |
| 404 | RequestedPartition = &RequestedPartitionRecovery[1]; |
| 405 | } |
| 406 | } else { |
| 407 | RequestedPartition = RequestedPartitionMission; |
| 408 | NumRequestedPartition = ARRAY_SIZE(RequestedPartitionMission) - 1; |
| 409 | if (Info->num_loaded_images) { |
| 410 | /* fastboot boot option, skip Index 0, as boot image already |
| 411 | * loaded */ |
| 412 | RequestedPartition = &RequestedPartitionMission[1]; |
| 413 | } |
| 414 | } |
| 415 | if (Info->num_loaded_images) { |
| 416 | NumRequestedPartition--; |
| 417 | } |
| 418 | |
| 419 | // FIXME: is this correct? |
| 420 | VerityFlags = VerityEnforcing ? |
| 421 | AVB_HASHTREE_ERROR_MODE_RESTART : |
| 422 | AVB_HASHTREE_ERROR_MODE_EIO; |
| 423 | |
| 424 | Result = avb_slot_verify(Ops, RequestedPartition, SlotSuffix, |
| 425 | VerifyFlags, VerityFlags, |
| 426 | &SlotData); |
| 427 | |
| 428 | if (AllowVerificationError && ResultShouldContinue(Result)) { |
| 429 | dprintf(CRITICAL, "State: Unlocked, AvbSlotVerify returned " |
| 430 | "%s, continue boot\n", |
| 431 | avb_slot_verify_result_to_string(Result)); |
| 432 | } else if (Result != AVB_SLOT_VERIFY_RESULT_OK) { |
| 433 | dprintf(CRITICAL, |
| 434 | "ERROR: Device State %s, AvbSlotVerify returned %s\n", |
| 435 | AllowVerificationError ? "Unlocked" : "Locked", |
| 436 | avb_slot_verify_result_to_string(Result)); |
| 437 | Status = EFI_LOAD_ERROR; |
| 438 | Info->boot_state = RED; |
| 439 | goto out; |
| 440 | } |
Monika Singh | 0f7bfc8 | 2018-04-16 23:14:29 +0530 | [diff] [blame] | 441 | if (SlotData == NULL) { |
| 442 | Status = EFI_LOAD_ERROR; |
| 443 | Info->boot_state = RED; |
| 444 | goto out; |
| 445 | } |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 446 | |
| 447 | for (UINTN ReqIndex = 0; ReqIndex < NumRequestedPartition; ReqIndex++) { |
| 448 | dprintf(DEBUG, "Requested Partition: %s\n", |
| 449 | RequestedPartition[ReqIndex]); |
| 450 | for (UINTN loadedindex = 0; |
| 451 | loadedindex < SlotData->num_loaded_partitions; loadedindex++) { |
| 452 | dprintf(DEBUG, "Loaded Partition: %s\n", |
| 453 | SlotData->loaded_partitions[loadedindex].partition_name); |
| 454 | if (!strncmp((const char *) |
| 455 | RequestedPartition[ReqIndex], |
| 456 | SlotData->loaded_partitions[loadedindex].partition_name, |
| 457 | strlen(SlotData->loaded_partitions[loadedindex] |
| 458 | .partition_name))) { |
| 459 | if (Info->num_loaded_images >= ARRAY_SIZE(Info->images)) { |
| 460 | dprintf(CRITICAL, "NumLoadedPartition" |
| 461 | "(%d) too large " |
| 462 | "max images(%d)\n", |
| 463 | Info->num_loaded_images, |
| 464 | ARRAY_SIZE(Info->images)); |
| 465 | Status = EFI_LOAD_ERROR; |
| 466 | Info->boot_state = RED; |
| 467 | goto out; |
| 468 | } |
| 469 | Info->images[Info->num_loaded_images].name = |
| 470 | SlotData->loaded_partitions[loadedindex].partition_name; |
| 471 | Info->images[Info->num_loaded_images].image_buffer = |
| 472 | SlotData->loaded_partitions[loadedindex].data; |
| 473 | Info->images[Info->num_loaded_images].imgsize = |
| 474 | SlotData->loaded_partitions[loadedindex].data_size; |
| 475 | Info->num_loaded_images++; |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (Info->num_loaded_images < NumRequestedPartition) { |
| 482 | dprintf(CRITICAL, "ERROR: AvbSlotVerify slot data: num of loaded partitions %d, requested %lu\n",Info->num_loaded_images, NumRequestedPartition); |
| 483 | Status = EFI_LOAD_ERROR; |
| 484 | goto out; |
| 485 | } |
| 486 | |
| 487 | dprintf(DEBUG, "Total loaded partition %d\n", Info->num_loaded_images); |
| 488 | |
| 489 | VBData = (VB2Data *)avb_calloc(sizeof(VB2Data)); |
| 490 | if (VBData == NULL) { |
| 491 | dprintf(CRITICAL, "ERROR: Failed to allocate VB2Data\n"); |
| 492 | Status = EFI_OUT_OF_RESOURCES; |
| 493 | goto out; |
| 494 | } |
| 495 | VBData->Ops = Ops; |
| 496 | VBData->SlotData = SlotData; |
| 497 | Info->vb_data = (VOID *)VBData; |
| 498 | |
| 499 | ImageHdrSize = get_page_size(); |
| 500 | GUARD_OUT(getimage(Info, &image_buffer, &imgsize,(!Info->multi_slot_boot && Info->bootinto_recovery) ? "recovery" : "boot") ); |
| 501 | |
| 502 | Status = check_img_header(image_buffer, ImageHdrSize, &imgsizeActual); |
| 503 | if (Status != EFI_SUCCESS) { |
| 504 | dprintf(CRITICAL, "Invalid boot image header:%d\n", Status); |
| 505 | goto out; |
| 506 | } |
| 507 | |
| 508 | if (imgsizeActual > imgsize) { |
| 509 | Status = EFI_BUFFER_TOO_SMALL; |
| 510 | dprintf(CRITICAL, |
| 511 | "Boot size in vbmeta less than actual boot image size " |
| 512 | "flash corresponding vbmeta.img\n"); |
| 513 | goto out; |
| 514 | } |
| 515 | if (AllowVerificationError) { |
| 516 | Info->boot_state = ORANGE; |
| 517 | } else { |
| 518 | if (UserData->IsUserKey) { |
| 519 | Info->boot_state = YELLOW; |
| 520 | } else { |
| 521 | Info->boot_state = GREEN; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /* command line */ |
| 526 | GUARD_OUT(AppendVBCommonCmdLine(Info)); |
| 527 | GUARD_OUT(Appendvbcmdline(Info, SlotData->cmdline)); |
| 528 | DevInfo_vb.is_unlocked = !is_device_locked(); |
| 529 | set_os_version((unsigned char *)Info->images[0].image_buffer); |
| 530 | if(!send_rot_command((uint32_t)DevInfo_vb.is_unlocked)) |
| 531 | return EFI_LOAD_ERROR; |
| 532 | dprintf(INFO, "VB2: Authenticate complete! boot state is: %s\n", |
| 533 | VbSn[Info->boot_state].name); |
| 534 | |
| 535 | out: |
| 536 | if (Status != EFI_SUCCESS) { |
| 537 | if (SlotData != NULL) { |
| 538 | avb_slot_verify_data_free(SlotData); |
| 539 | } |
| 540 | if (Ops != NULL) { |
| 541 | AvbOpsFree(Ops); |
| 542 | } |
| 543 | if (UserData != NULL) { |
| 544 | avb_free(UserData); |
| 545 | } |
| 546 | if (VBData != NULL) { |
| 547 | avb_free(VBData); |
| 548 | } |
| 549 | Info->boot_state = RED; |
| 550 | if(Info->multi_slot_boot) { |
| 551 | HandleActiveSlotUnbootable(); |
| 552 | /* HandleActiveSlotUnbootable should have swapped slots and |
| 553 | * reboot the device. If no bootable slot found, enter fastboot */ |
| 554 | dprintf(CRITICAL, "No bootable slots found enter fastboot mode\n"); |
| 555 | } else { |
| 556 | dprintf(CRITICAL, "Non Multi-slot: Unbootable entering fastboot mode\n"); |
| 557 | } |
| 558 | |
| 559 | } |
| 560 | |
| 561 | dprintf(CRITICAL, "VB2: boot state: %s(%d)\n", |
| 562 | VbSn[Info->boot_state].name, Info->boot_state); |
| 563 | return Status; |
| 564 | } |
| 565 | |
| 566 | static EFI_STATUS DisplayVerifiedBootScreen(bootinfo *Info) |
| 567 | { |
| 568 | EFI_STATUS Status = EFI_SUCCESS; |
| 569 | CHAR8 ffbm_mode_string[FFBM_MODE_BUF_SIZE] = {'\0'}; |
| 570 | |
| 571 | if (GetAVBVersion() < AVB_1) { |
| 572 | return EFI_SUCCESS; |
| 573 | } |
| 574 | |
| 575 | if (!strncmp(Info->pname, "boot", strlen("boot"))) { |
| 576 | Status = get_ffbm(ffbm_mode_string, FFBM_MODE_BUF_SIZE); |
| 577 | if (Status != EFI_SUCCESS) { |
| 578 | dprintf(DEBUG, |
| 579 | "No Ffbm cookie found, ignore: %d\n", Status); |
| 580 | ffbm_mode_string[0] = '\0'; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | dprintf(DEBUG, "Boot State is : %d\n", Info->boot_state); |
| 585 | switch (Info->boot_state) |
| 586 | { |
| 587 | case RED: |
| 588 | display_bootverify_menu(DISPLAY_MENU_RED); |
| 589 | //if (Status != EFI_SUCCESS) { |
| 590 | dprintf(INFO, "Your device is corrupt. It can't be trusted and will not boot." \ |
| 591 | "\nYour device will shutdown in 30s\n"); |
| 592 | //} |
| 593 | udelay(30000000); |
| 594 | shutdown_device(); |
| 595 | break; |
| 596 | case YELLOW: |
| 597 | display_bootverify_menu(DISPLAY_MENU_YELLOW); |
| 598 | //if (Status == EFI_SUCCESS) { |
| 599 | wait_for_users_action(); |
| 600 | //} else { |
| 601 | dprintf(INFO, "Your device has loaded a different operating system." \ |
| 602 | "\nWait for 5 seconds before proceeding\n"); |
| 603 | udelay(5000000); |
| 604 | //} |
| 605 | break; |
| 606 | case ORANGE: |
| 607 | if (ffbm_mode_string[0] != '\0' && !target_build_variant_user()) { |
| 608 | dprintf(DEBUG, "Device will boot into FFBM mode\n"); |
| 609 | } else { |
Monika Singh | 90cf4b1 | 2018-04-09 16:47:15 +0530 | [diff] [blame] | 610 | display_bootverify_menu(DISPLAY_MENU_ORANGE); |
| 611 | if (Status == EFI_SUCCESS) { |
| 612 | wait_for_users_action(); |
| 613 | } else { |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 614 | dprintf(INFO, "Device is unlocked, Skipping boot verification\n"); |
| 615 | udelay(5000000); |
Monika Singh | 90cf4b1 | 2018-04-09 16:47:15 +0530 | [diff] [blame] | 616 | } |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 617 | } |
| 618 | break; |
| 619 | default: |
| 620 | break; |
| 621 | } |
| 622 | return EFI_SUCCESS; |
| 623 | } |
| 624 | |
| 625 | EFI_STATUS load_image_and_auth(bootinfo *Info) |
| 626 | { |
| 627 | EFI_STATUS Status = EFI_SUCCESS; |
| 628 | BOOLEAN MdtpActive = FALSE; |
| 629 | UINT32 AVBVersion = NO_AVB; |
| 630 | mdtp_ext_partition_verification_t ext_partition; |
Monika Singh | 0f7bfc8 | 2018-04-16 23:14:29 +0530 | [diff] [blame] | 631 | const char *current_slot_suffix; |
| 632 | int current_active_slot; |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 633 | |
| 634 | if (Info == NULL) { |
| 635 | dprintf(CRITICAL, "Invalid parameter Info\n"); |
| 636 | return EFI_INVALID_PARAMETER; |
| 637 | } |
| 638 | |
| 639 | if (!Info->multi_slot_boot) { |
| 640 | if (Info->bootinto_recovery) { |
| 641 | dprintf(INFO, "Booting Into Recovery Mode\n"); |
Monika Singh | 96b9721 | 2018-04-13 18:27:55 +0530 | [diff] [blame] | 642 | strlcpy(Info->pname, "recovery", strlen("recovery")); |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 643 | } else { |
| 644 | dprintf(INFO, "Booting Into Mission Mode\n"); |
Monika Singh | 96b9721 | 2018-04-13 18:27:55 +0530 | [diff] [blame] | 645 | strlcpy(Info->pname, "boot", strlen("boot")); |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 646 | } |
| 647 | } else { |
Monika Singh | 96b9721 | 2018-04-13 18:27:55 +0530 | [diff] [blame] | 648 | strlcpy(Info->pname, "boot", strlen("boot")); |
Monika Singh | 0f7bfc8 | 2018-04-16 23:14:29 +0530 | [diff] [blame] | 649 | current_active_slot = partition_find_active_slot(); |
| 650 | if (current_active_slot != INVALID ) { |
| 651 | current_slot_suffix = SUFFIX_SLOT(current_active_slot); |
| 652 | if (strlen(current_slot_suffix) == 0) { |
| 653 | dprintf(CRITICAL, "No bootable slot\n"); |
| 654 | return EFI_LOAD_ERROR; |
| 655 | } |
| 656 | strlcat(Info->pname, current_slot_suffix, strlen(current_slot_suffix)); |
| 657 | } |
Monika Singh | 5e17036 | 2018-03-14 00:48:36 +0530 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | dprintf(DEBUG, "MultiSlot %s, partition name %s\n", |
| 661 | BooleanString[Info->multi_slot_boot].name, Info->pname); |
| 662 | |
| 663 | Status = mdtp_activated(&MdtpActive); |
| 664 | if (Status) { |
| 665 | dprintf(CRITICAL, |
| 666 | "Failed to get activation state for MDTP, " |
| 667 | "Status=%d." |
| 668 | " Considering MDTP as active and continuing \n", |
| 669 | Status); |
| 670 | if (Status != -1) |
| 671 | MdtpActive = TRUE; |
| 672 | } |
| 673 | |
| 674 | AVBVersion = GetAVBVersion(); |
| 675 | dprintf(DEBUG, "AVB version %d\n", AVBVersion); |
| 676 | |
| 677 | /* Load and Authenticate */ |
| 678 | switch (AVBVersion) { |
| 679 | case NO_AVB: |
| 680 | return LoadImageNoAuth(Info); |
| 681 | break; |
| 682 | case AVB_1: |
| 683 | Status = load_image_and_authVB1(Info); |
| 684 | break; |
| 685 | case AVB_2: |
| 686 | Status = load_image_and_authVB2(Info); |
| 687 | break; |
| 688 | default: |
| 689 | dprintf(CRITICAL, "Unsupported AVB version %d\n", AVBVersion); |
| 690 | Status = EFI_UNSUPPORTED; |
| 691 | } |
| 692 | |
| 693 | // if MDTP is active Display Recovery UI |
| 694 | if (Status != EFI_SUCCESS && MdtpActive && !target_use_signed_kernel()) { |
| 695 | //FIXME: Hard coded to BOOT |
| 696 | ext_partition.partition = Info->bootinto_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT; |
| 697 | ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET; |
| 698 | ext_partition.page_size = get_page_size(); |
| 699 | ext_partition.image_addr = (uint32)Info->images[0].image_buffer; |
| 700 | ext_partition.image_size = Info->images[0].imgsize; |
| 701 | ext_partition.sig_avail = FALSE; |
| 702 | mdtp_fwlock_verify_lock(&ext_partition); |
| 703 | } |
| 704 | |
| 705 | if (!is_device_locked() && Status != EFI_SUCCESS) { |
| 706 | dprintf(CRITICAL, "load_image_and_auth failed %d\n", Status); |
| 707 | return Status; |
| 708 | } |
| 709 | |
| 710 | DisplayVerifiedBootScreen(Info); |
| 711 | |
| 712 | return Status; |
| 713 | } |
| 714 | |
| 715 | #if VERIFIED_BOOT_2 |
| 716 | VOID free_verified_boot_resource(bootinfo *Info) |
| 717 | { |
| 718 | dprintf(DEBUG, "free_verified_boot_resource\n"); |
| 719 | |
| 720 | if (Info == NULL) { |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | VB2Data *VBData = Info->vb_data; |
| 725 | if (VBData != NULL) { |
| 726 | AvbOps *Ops = VBData->Ops; |
| 727 | if (Ops != NULL) { |
| 728 | if (Ops->user_data != NULL) { |
| 729 | avb_free(Ops->user_data); |
| 730 | } |
| 731 | AvbOpsFree(Ops); |
| 732 | } |
| 733 | |
| 734 | AvbSlotVerifyData *SlotData = VBData->SlotData; |
| 735 | if (SlotData != NULL) { |
| 736 | avb_slot_verify_data_free(SlotData); |
| 737 | } |
| 738 | avb_free(VBData); |
| 739 | } |
| 740 | |
| 741 | if (Info->vbcmdline != NULL) { |
| 742 | free(Info->vbcmdline); |
| 743 | } |
| 744 | return; |
| 745 | } |
| 746 | #else |
| 747 | VOID free_verified_boot_resource(bootinfo *Info) |
| 748 | { |
| 749 | return; |
| 750 | } |
| 751 | #endif |