BootEntryInfo.c 23.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** @file
  Copyright (C) 2019, vit9696. All rights reserved.

  All rights reserved.

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/

#include "BootManagementInternal.h"

#include <Guid/AppleBless.h>
V
vit9696 已提交
18
#include <IndustryStandard/AppleDiskLabel.h>
19 20

#include <Library/BaseLib.h>
21
#include <Library/BaseMemoryLib.h>
V
vit9696 已提交
22
#include <Library/OcDebugLogLib.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/OcDevicePathLib.h>
#include <Library/OcFileLib.h>
#include <Library/OcStringLib.h>
#include <Library/OcXmlLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiBootServicesTableLib.h>

CHAR16 *
InternalGetAppleDiskLabel (
  IN  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  *FileSystem,
  IN  CONST CHAR16                     *BootDirectoryName,
  IN  CONST CHAR16                     *LabelFilename
  )
{
  CHAR16   *DiskLabelPath;
  UINTN    DiskLabelPathSize;
  CHAR8    *AsciiDiskLabel;
  CHAR16   *UnicodeDiskLabel;
  UINT32   DiskLabelLength;

  DiskLabelPathSize = StrSize (BootDirectoryName) + StrSize (LabelFilename) - sizeof (CHAR16);
  DiskLabelPath     = AllocatePool (DiskLabelPathSize);

  if (DiskLabelPath == NULL) {
    return NULL;
  }

  UnicodeSPrint (DiskLabelPath, DiskLabelPathSize, L"%s%s", BootDirectoryName, LabelFilename);
53
  DEBUG ((DEBUG_INFO, "OCB: Trying to get label from %s\n", DiskLabelPath));
54

55
  AsciiDiskLabel = (CHAR8 *) OcReadFile (FileSystem, DiskLabelPath, &DiskLabelLength, OC_MAX_VOLUME_LABEL_SIZE);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  FreePool (DiskLabelPath);

  if (AsciiDiskLabel != NULL) {
    UnicodeDiskLabel = AsciiStrCopyToUnicode (AsciiDiskLabel, DiskLabelLength);
    if (UnicodeDiskLabel != NULL) {
      UnicodeFilterString (UnicodeDiskLabel, TRUE);
    }
    FreePool (AsciiDiskLabel);
  } else {
    UnicodeDiskLabel = NULL;
  }

  return UnicodeDiskLabel;
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
CHAR8 *
InternalGetContentFlavour (
  IN  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  *FileSystem,
  IN  CONST CHAR16                     *BootDirectoryName,
  IN  CONST CHAR16                     *FlavourFilename
  )
{
  CHAR16   *ContentFlavourPath;
  UINTN    ContentFlavourPathSize;
  CHAR8    *AsciiContentFlavour;
  UINT32   ContentFlavourLength;

  ContentFlavourPathSize = StrSize (BootDirectoryName) + StrSize (FlavourFilename) - sizeof (CHAR16);
  ContentFlavourPath     = AllocatePool (ContentFlavourPathSize);

  if (ContentFlavourPath == NULL) {
    return NULL;
  }

  UnicodeSPrint (ContentFlavourPath, ContentFlavourPathSize, L"%s%s", BootDirectoryName, FlavourFilename);
  DEBUG ((DEBUG_INFO, "OCB: Trying to get flavour from %s\n", ContentFlavourPath));

93
  AsciiContentFlavour = (CHAR8 *) OcReadFile (FileSystem, ContentFlavourPath, &ContentFlavourLength, OC_MAX_CONTENT_FLAVOUR_SIZE);
94 95 96 97 98 99 100 101 102
  FreePool (ContentFlavourPath);

  if (AsciiContentFlavour != NULL) {
    AsciiFilterString (AsciiContentFlavour, TRUE);
  }

  return AsciiContentFlavour;
}

V
vit9696 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
EFI_STATUS
InternalGetAppleImage (
  IN  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  *FileSystem,
  IN  CONST CHAR16                     *DirectoryName,
  IN  CONST CHAR16                     *LabelFilename,
  OUT VOID                             **ImageData,
  OUT UINT32                           *DataSize
  )
{
  CHAR16   *ImagePath;
  UINTN    ImagePathSize;

  ImagePathSize = StrSize (DirectoryName) + StrSize (LabelFilename) - sizeof (CHAR16);
  ImagePath     = AllocatePool (ImagePathSize);

  if (ImagePath == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  UnicodeSPrint (ImagePath, ImagePathSize, L"%s%s", DirectoryName, LabelFilename);
123
  DEBUG ((DEBUG_INFO, "OCB: Trying to get image from %s\n", ImagePath));
V
vit9696 已提交
124

125
  *ImageData = OcReadFile (FileSystem, ImagePath, DataSize, BASE_16MB);
V
vit9696 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

  FreePool (ImagePath);

  if (*ImageData == NULL) {
    return EFI_NOT_FOUND;
  }

  //
  // Whether it is disk label or .icns, disk label is always smaller.
  // Early abort on obviously small images.
  //
  if (*DataSize <= sizeof (APPLE_DISK_LABEL)) {
    FreePool (*ImageData);
    return EFI_UNSUPPORTED;
  }

  return EFI_SUCCESS;
}

145
STATIC
146 147 148 149 150
EFI_STATUS
GetAppleVersionFromPlist (
  IN  CHAR8     *SystemVersionData,
  IN  UINT32    SystemVersionDataSize,
  OUT CHAR8     *AppleVersion
151 152
  )
{
153
  EFI_STATUS          Status;
154 155 156 157 158 159 160 161 162 163 164
  XML_DOCUMENT        *Document;
  XML_NODE            *RootDict;
  UINT32              DictSize;
  UINT32              Index;
  CONST CHAR8         *CurrentKey;
  XML_NODE            *CurrentValue;
  CONST CHAR8         *Version;

  Document = XmlDocumentParse (SystemVersionData, SystemVersionDataSize, FALSE);

  if (Document == NULL) {
165
    return EFI_NOT_FOUND;
166 167 168 169 170 171
  }

  RootDict = PlistNodeCast (PlistDocumentRoot (Document), PLIST_NODE_TYPE_DICT);

  if (RootDict == NULL) {
    XmlDocumentFree (Document);
172
    return EFI_NOT_FOUND;
173 174
  }

175
  Status = EFI_NOT_FOUND;
176 177 178 179 180 181 182 183 184 185 186 187

  DictSize = PlistDictChildren (RootDict);
  for (Index = 0; Index < DictSize; Index++) {
    CurrentKey = PlistKeyValue (PlistDictChild (RootDict, Index, &CurrentValue));

    if (CurrentKey == NULL || AsciiStrCmp (CurrentKey, "ProductUserVisibleVersion") != 0) {
      continue;
    }

    if (PlistNodeCast (CurrentValue, PLIST_NODE_TYPE_STRING) != NULL) {
      Version = XmlNodeContent (CurrentValue);
      if (Version != NULL) {
188 189 190 191
        if (AsciiStrCpyS (AppleVersion, OC_APPLE_VERSION_MAX_SIZE, Version) != RETURN_SUCCESS) {
          Status = EFI_UNSUPPORTED;
        } else {
          Status = EFI_SUCCESS;
192 193 194 195 196 197 198 199
        }
      }
    }

    break;
  }

  XmlDocumentFree (Document);
200
  return Status;
201 202
}

203
STATIC
204 205
CHAR16 *
InternalGetAppleRecoveryName (
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  IN  CHAR8     *Version
  )
{
  CHAR16              *RecoveryName;
  UINTN               RecoveryNameSize;

  RecoveryName = NULL;

  if (Version[0] != '\0') {
    RecoveryNameSize = L_STR_SIZE(L"Recovery ") + AsciiStrLen (Version) * sizeof (CHAR16);
    RecoveryName = AllocatePool (RecoveryNameSize);
    if (RecoveryName != NULL) {
      UnicodeSPrint (RecoveryName, RecoveryNameSize, L"Recovery %a", Version);
      UnicodeFilterString (RecoveryName, TRUE);
    }
  }

  return RecoveryName;
}

STATIC
EFI_STATUS
InternalGetAppleVersion (
229
  IN  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  *FileSystem,
230 231
  IN  CONST CHAR16                     *DirectoryName,
  OUT CHAR8                            *AppleVersion
232 233
  )
{
234 235 236 237 238
  EFI_STATUS      Status;
  CHAR16          *SystemVersionPath;
  UINTN           SystemVersionPathSize;
  CHAR8           *SystemVersionData;
  UINT32          SystemVersionDataSize;
239

240
  SystemVersionPathSize = StrSize (DirectoryName) + L_STR_SIZE_NT (L"SystemVersion.plist");
241 242 243
  SystemVersionPath     = AllocatePool (SystemVersionPathSize);

  if (SystemVersionPath == NULL) {
244
    return EFI_OUT_OF_RESOURCES;
245 246
  }

247 248
  UnicodeSPrint (SystemVersionPath, SystemVersionPathSize, L"%s%s", DirectoryName, L"SystemVersion.plist");
  DEBUG ((DEBUG_INFO, "OCB: Trying to get Apple version from %s\n", SystemVersionPath));
249
  SystemVersionData = (CHAR8 *) OcReadFile (FileSystem, SystemVersionPath, &SystemVersionDataSize, BASE_1MB);
250 251 252
  FreePool (SystemVersionPath);

  if (SystemVersionData != NULL) {
253
    Status = GetAppleVersionFromPlist (SystemVersionData, SystemVersionDataSize, AppleVersion);
254 255
    FreePool (SystemVersionData);
  } else {
256
    Status = EFI_NOT_FOUND;
257 258
  }

259
  return Status;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
}

EFI_STATUS
InternalGetRecoveryOsBooter (
  IN  EFI_HANDLE                Device,
  OUT EFI_DEVICE_PATH_PROTOCOL  **FilePath,
  IN  BOOLEAN                   Basic
  )
{
  EFI_STATUS                       Status;
  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  *FileSystem;
  EFI_FILE_PROTOCOL                *Root;
  EFI_FILE_PROTOCOL                *Recovery;
  UINTN                            FilePathSize;
  EFI_DEVICE_PATH_PROTOCOL         *TmpPath;
  UINTN                            TmpPathSize;
276
  CHAR16                           *DevicePathText;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

  Status = gBS->HandleProtocol (
    Device,
    &gEfiSimpleFileSystemProtocolGuid,
    (VOID **) &FileSystem
    );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = FileSystem->OpenVolume (FileSystem, &Root);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  FilePathSize = 0;

  if (!Basic) {
295
    *FilePath = (EFI_DEVICE_PATH_PROTOCOL *) OcGetFileInfo (
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
                  Root,
                  &gAppleBlessedOsxFolderInfoGuid,
                  sizeof (EFI_DEVICE_PATH_PROTOCOL),
                  &FilePathSize
                  );
  } else {
    //
    // Requested basic recovery support, i.e. only com.apple.recovery.boot folder check.
    // This is useful for locating empty USB sticks with just a dmg in them.
    //
    *FilePath = NULL;
  }

  if (*FilePath != NULL) {
    if (IsDevicePathValid (*FilePath, FilePathSize)) {
      //
      // We skip alternate entry when current one is the same.
      // This is to prevent recovery and volume duplicates on HFS+ systems.
      //

316
      TmpPath = (EFI_DEVICE_PATH_PROTOCOL *) OcGetFileInfo (
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
        Root,
        &gAppleBlessedSystemFolderInfoGuid,
        sizeof (EFI_DEVICE_PATH_PROTOCOL),
        &TmpPathSize
        );

      if (TmpPath != NULL) {
        if (IsDevicePathValid (TmpPath, TmpPathSize)
          && IsDevicePathEqual (TmpPath, *FilePath)) {
          DEBUG ((DEBUG_INFO, "Skipping equal alternate device path %p\n", Device));
          Status = EFI_ALREADY_STARTED;
          FreePool (*FilePath);
          *FilePath = NULL;
        }
        FreePool (TmpPath);
      }

      if (!EFI_ERROR (Status)) {
        //
        // This entry should point to a folder with recovery.
        // Apple never adds trailing slashes to blessed folder paths.
        // However, we do rely on trailing slashes in folder paths and add them here.
        //
        TmpPath = TrailedBooterDevicePath (*FilePath);
        if (TmpPath != NULL) {
          FreePool (*FilePath);
          *FilePath = TmpPath;
        }
      }
    } else {
      FreePool (*FilePath);
      *FilePath = NULL;
      Status = EFI_NOT_FOUND;
    }
  } else {
    //
    // Ok, this one can still be FileVault 2 HFS+ recovery or just a hardcoded basic recovery.
    // Apple does add its path to so called "Alternate OS blessed file/folder", but this
    // path is not accessible from HFSPlus.efi driver. Just why???
    // Their SlingShot.efi app just bruteforces com.apple.recovery.boot directory existence,
    // and we have to copy.
    //
359
    Status = OcSafeFileOpen (Root, &Recovery, L"\\com.apple.recovery.boot", EFI_FILE_MODE_READ, 0);
360 361 362 363 364 365 366 367 368 369 370
    if (!EFI_ERROR (Status)) {
      //
      // Do not do any extra checks for simplicity, as they will be done later either way.
      //
      Root->Close (Recovery);
      Status    = EFI_NOT_FOUND;
      TmpPath   = DevicePathFromHandle (Device);

      if (TmpPath != NULL) {
        *FilePath = AppendFileNameDevicePath (TmpPath, L"\\com.apple.recovery.boot\\");
        if (*FilePath != NULL) {
V
vit9696 已提交
371
          DEBUG_CODE_BEGIN ();
372
          DevicePathText = ConvertDevicePathToText (*FilePath, FALSE, FALSE);
V
vit9696 已提交
373
          if (DevicePathText != NULL) {
374
            DEBUG ((DEBUG_INFO, "OCB: Got recovery dp %s\n", DevicePathText));
V
vit9696 已提交
375 376 377
            FreePool (DevicePathText);
          }
          DEBUG_CODE_END ();
378 379 380 381 382 383 384 385 386 387 388 389 390
          Status = EFI_SUCCESS;
        }
      }
    } else {
      Status = EFI_NOT_FOUND;
    }
  }

  Root->Close (Root);

  return Status;
}

391
EFI_STATUS
392
EFIAPI
393 394 395 396 397 398
OcGetBootEntryLabelImage (
  IN  OC_PICKER_CONTEXT          *Context,
  IN  OC_BOOT_ENTRY              *BootEntry,
  IN  UINT8                      Scale,
  OUT VOID                       **ImageData,
  OUT UINT32                     *DataLength
399 400 401
  )
{
  EFI_STATUS                       Status;
402 403 404
  CHAR16                           *BootDirectoryName;
  EFI_HANDLE                       Device;
  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  *FileSystem;
405

406 407
  *ImageData = NULL;
  *DataLength = 0;
408

409 410
  if (BootEntry->Type == OC_BOOT_EXTERNAL_TOOL || (BootEntry->Type & OC_BOOT_SYSTEM) != 0) {
    return EFI_NOT_FOUND;
411 412 413 414
  }

  ASSERT (BootEntry->DevicePath != NULL);

415
  Status = OcBootPolicyDevicePathToDirPath (
416 417
    BootEntry->DevicePath,
    &BootDirectoryName,
418
    &Device
419 420 421 422 423 424
    );

  if (EFI_ERROR (Status)) {
    return Status;
  }

425 426 427 428
  Status = gBS->HandleProtocol (
    Device,
    &gEfiSimpleFileSystemProtocolGuid,
    (VOID **) &FileSystem
429
    );
430

431
  if (EFI_ERROR (Status)) {
432
    FreePool (BootDirectoryName);
433 434 435
    return Status;
  }

436 437 438 439 440 441 442
  Status = InternalGetAppleImage (
    FileSystem,
    BootDirectoryName,
    Scale == 2 ? L".disk_label_2x" : L".disk_label",
    ImageData,
    DataLength
    );
443

444 445
  DEBUG ((DEBUG_INFO, "OCB: Get normal label %s - %r\n", BootEntry->Name, Status));
  FreePool (BootDirectoryName);
446

447 448
  return Status;
}
449

450
EFI_STATUS
451
EFIAPI
452 453 454 455 456 457 458 459 460
OcGetBootEntryIcon (
  IN  OC_PICKER_CONTEXT          *Context,
  IN  OC_BOOT_ENTRY              *BootEntry,
  OUT VOID                       **ImageData,
  OUT UINT32                     *DataLength
  )
{
  EFI_STATUS                       Status;
  CHAR16                           *BootDirectoryName;
461
  CHAR16                           *GuidPrefix;
462 463
  EFI_HANDLE                       Device;
  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  *FileSystem;
464

465 466
  *ImageData = NULL;
  *DataLength = 0;
467

468 469
  if (BootEntry->Type == OC_BOOT_EXTERNAL_TOOL || (BootEntry->Type & OC_BOOT_SYSTEM) != 0) {
    return EFI_NOT_FOUND;
470 471 472
  }

  ASSERT (BootEntry->DevicePath != NULL);
473

474
  Status = OcBootPolicyDevicePathToDirPath (
475 476
    BootEntry->DevicePath,
    &BootDirectoryName,
477
    &Device
478
    );
479

480 481
  if (EFI_ERROR (Status)) {
    return Status;
482 483
  }

484 485 486 487 488 489 490
  GuidPrefix = BootDirectoryName[0] == '\\' ? &BootDirectoryName[1] : &BootDirectoryName[0];
  if (HasValidGuidStringPrefix (GuidPrefix) && GuidPrefix[GUID_STRING_LENGTH] == '\\') {
    GuidPrefix[GUID_STRING_LENGTH+1] = '\0';
  } else {
    GuidPrefix = NULL;
  }

491 492 493 494 495 496 497 498 499
  Status = gBS->HandleProtocol (
    Device,
    &gEfiSimpleFileSystemProtocolGuid,
    (VOID **) &FileSystem
    );

  if (EFI_ERROR (Status)) {
    FreePool (BootDirectoryName);
    return Status;
500
  }
501

502 503 504 505
  //
  // OC-specific location, per-GUID and hence per-OS, below Preboot volume root.
  // Not recognised by Apple bootpicker.
  //
506 507 508 509 510 511 512 513 514 515 516 517
  if (GuidPrefix != NULL) {
    Status = InternalGetAppleImage (
      FileSystem,
      GuidPrefix,
      L".VolumeIcon.icns",
      ImageData,
      DataLength
      );
  } else {
    Status = EFI_UNSUPPORTED;
  }

518 519 520 521
  //
  // Apple default location at Preboot volume root (typically mounted within OS
  // at /System/Volumes/Preboot/), shared by all OSes on a volume.
  //
522 523 524 525 526 527 528 529 530
  if (EFI_ERROR (Status)) {
    Status = InternalGetAppleImage (
      FileSystem,
      L"",
      L".VolumeIcon.icns",
      ImageData,
      DataLength
      );
  }
531

532 533 534 535 536 537 538
  DEBUG ((
    DEBUG_INFO,
    "OCB: OcGetBootEntryIcon - %s in %s (volume icon) - %r\n",
    BootEntry->Name,
    GuidPrefix,
    Status
    ));
539 540

  FreePool (BootDirectoryName);
541 542 543 544

  return Status;
}

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
//
// Duplicate each flavour in list, w/ apple version added to first of each duplicate
//
STATIC
CHAR8 *
InternalAddAppleVersion (
  IN CHAR8 *Flavour,
  IN CHAR8 *Version)
{
  UINTN     VersionLength;
  UINTN     FlavourLength;
  UINTN     SepCount;
  UINTN     Size;
  UINTN     Index;
  CHAR8     *NewFlavour;
  CHAR8     *Start;
  CHAR8     *End;
  CHAR8     *Pos;

  ASSERT (Flavour  != NULL);
  ASSERT (Version  != NULL);

  VersionLength = AsciiStrLen (Version);
  FlavourLength = AsciiStrLen (Flavour);
  SepCount = 0;
  for (Index = 0; Index < FlavourLength; Index++) {
    if (Flavour[Index] == ':') {
      ++SepCount;
    }
  }

  Size = 2 * (FlavourLength + 1) + VersionLength * (SepCount + 1);

  if (Size > OC_MAX_CONTENT_FLAVOUR_SIZE) {
    return Flavour;
  }

  NewFlavour = AllocatePool (Size);
  if (NewFlavour == NULL) {
    return Flavour;
  }

  Pos = NewFlavour;
  End = Flavour - 1;
  do {
    for (Start = ++End; *End != '\0' && *End != ':'; ++End);
    AsciiStrnCpyS (Pos, Size - (Pos - NewFlavour), Start, End - Start);
    Pos += End - Start;
    AsciiStrnCpyS (Pos, Size - (Pos - NewFlavour), Version, VersionLength);
    Pos += VersionLength;
    *Pos++ = ':';
    AsciiStrnCpyS (Pos, Size - (Pos - NewFlavour), Start, End - Start);
    Pos += End - Start;
    *Pos++ = ':';
  } while (*End != '\0');
  *(Pos - 1) = '\0';

  ASSERT ((UINTN)(Pos - NewFlavour) == Size);

  FreePool (Flavour);

  return NewFlavour;
}

609 610
EFI_STATUS
InternalDescribeBootEntry (
611 612
  IN     OC_BOOT_CONTEXT  *BootContext,
  IN OUT OC_BOOT_ENTRY    *BootEntry
613 614
  )
{
615 616
  EFI_STATUS                       Status;
  CHAR16                           *BootDirectoryName;
617
  CHAR16                           *TmpBootName;
618 619 620
  EFI_HANDLE                       Device;
  UINT32                           BcdSize;
  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL  *FileSystem;
621 622 623 624 625
  CHAR8                            *ContentFlavour;
  CHAR8                            AppleVersion[OC_APPLE_VERSION_MAX_SIZE];
  CHAR8                            *Dot;

  AppleVersion[0] = '\0';
626

627 628 629 630 631 632
  //
  // Custom entries need no special description.
  //
  if (BootEntry->Type == OC_BOOT_EXTERNAL_OS || BootEntry->Type == OC_BOOT_EXTERNAL_TOOL) {
    return EFI_SUCCESS;
  }
633

634
  Status = OcBootPolicyDevicePathToDirPath (
635 636
    BootEntry->DevicePath,
    &BootDirectoryName,
637
    &Device
638
    );
639

640 641 642
  if (EFI_ERROR (Status)) {
    return Status;
  }
643

644 645 646 647 648
  Status = gBS->HandleProtocol (
    Device,
    &gEfiSimpleFileSystemProtocolGuid,
    (VOID **) &FileSystem
    );
649

650 651 652 653
  if (EFI_ERROR (Status)) {
    FreePool (BootDirectoryName);
    return Status;
  }
654

655 656 657 658 659 660 661
  //
  // Try to use APFS-style label or legacy HFS one.
  //
  BootEntry->Name = InternalGetAppleDiskLabel (FileSystem, BootDirectoryName, L".contentDetails");
  if (BootEntry->Name == NULL) {
    BootEntry->Name = InternalGetAppleDiskLabel (FileSystem, BootDirectoryName, L".disk_label.contentDetails");
  }
662

663 664 665 666 667 668 669 670 671 672 673 674 675 676
  //
  // With FV2 encryption on HFS+ the actual boot happens from "Recovery HD/S/L/CoreServices".
  // For some reason "Recovery HD/S/L/CoreServices/.disk_label" may not get updated immediately,
  // and will contain "Recovery HD" despite actually pointing to "Macintosh HD".
  // This also spontaneously happens with renamed APFS volumes. The workaround is to manually
  // edit the file or sometimes choose the boot volume once more in preferences.
  //
  // TODO: Bugreport this to Apple, as this is clearly their bug, which should be reproducible
  // on original hardware.
  //
  // There exists .root_uuid, which contains real partition UUID in ASCII, however, Apple
  // BootPicker only uses it for entry deduplication, and we cannot figure out the name
  // on an encrypted volume anyway.
  //
677

678 679 680
  //
  // Windows boot entry may have a custom name, so ensure OC_BOOT_WINDOWS is set correctly.
  //
681
  if (BootEntry->Type == OC_BOOT_UNKNOWN && BootEntry->IsGeneric) {
682
    DEBUG ((DEBUG_INFO, "OCB: Trying to detect Microsoft BCD\n"));
683
    Status = OcReadFileSize (FileSystem, L"\\EFI\\Microsoft\\Boot\\BCD", &BcdSize);
684
    if (!EFI_ERROR (Status)) {
685
      BootEntry->Type = OC_BOOT_WINDOWS;
686
    }
687
  }
688

689 690 691 692
  if (BootEntry->Type == OC_BOOT_WINDOWS && BootEntry->Name == NULL) {
    BootEntry->Name = AllocateCopyPool (sizeof (L"Windows"), L"Windows");
  }

693
  if (BootEntry->Name == NULL) {
694 695
    //
    // Special case - installer should be clearly identified to end users but does not normally
M
MikeBeaton 已提交
696
    // contain text label, only pre-rendered graphical label which is not usable in builtin
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    // picker, or in Canopy with disk labels disabled.
    //
    if (StrStr (BootDirectoryName, L"com.apple.installer") != NULL) {
      BootEntry->Name = AllocateCopyPool (L_STR_SIZE (L"macOS Installer"), L"macOS Installer");
    } else {
      BootEntry->Name = OcGetVolumeLabel (FileSystem);
      if (BootEntry->Name != NULL) {
        if (StrCmp (BootEntry->Name, L"Recovery HD") == 0
          || StrCmp (BootEntry->Name, L"Recovery") == 0) {
          if (BootEntry->Type == OC_BOOT_UNKNOWN || BootEntry->Type == OC_BOOT_APPLE_OS) {
            BootEntry->Type = OC_BOOT_APPLE_RECOVERY;
          }
          Status = InternalGetAppleVersion (FileSystem, BootDirectoryName, AppleVersion);
          if (EFI_ERROR (Status)) {
            TmpBootName = NULL;
          } else {
            TmpBootName = InternalGetAppleRecoveryName (AppleVersion);
          }
        } else if (StrCmp (BootEntry->Name, L"Preboot") == 0) {
          //
          // Common Big Sur beta bug failing to create .contentDetails files.
M
MikeBeaton 已提交
718 719 720
          // Workaround it by using the standard installed macOS system volume name.
          // Applies to anything on the system volume without text labels (and not already
          // handled above, such as installer).
721 722
          //
          TmpBootName = AllocateCopyPool (sizeof (L"Macintosh HD"), L"Macintosh HD");
723
        } else {
724
          TmpBootName = NULL;
725
        }
726

727 728 729 730
        if (TmpBootName != NULL) {
          FreePool (BootEntry->Name);
          BootEntry->Name = TmpBootName;
        }
731 732
      }
    }
733 734
  }

735 736 737 738 739 740 741
  if (BootEntry->Name == NULL) {
    FreePool (BootDirectoryName);
    return EFI_NOT_FOUND;
  }

  BootEntry->PathName = BootDirectoryName;

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
  //
  // Get user-specified or builtin content flavour.
  //
  if ((BootContext->PickerContext->PickerAttributes & OC_ATTR_USE_FLAVOUR_ICON) != 0) {
    BootEntry->Flavour = InternalGetContentFlavour (FileSystem, BootDirectoryName, L".contentFlavour");
  }

  if (BootEntry->Flavour == NULL || AsciiStrCmp (BootEntry->Flavour, OC_FLAVOUR_AUTO) == 0) {
    switch (BootEntry->Type) {
      case OC_BOOT_APPLE_OS:
        ContentFlavour = AllocateCopyPool(sizeof (OC_FLAVOUR_APPLE_OS), OC_FLAVOUR_APPLE_OS);
        if ((BootContext->PickerContext->PickerAttributes & OC_ATTR_USE_FLAVOUR_ICON) != 0) {
          InternalGetAppleVersion (FileSystem, BootDirectoryName, AppleVersion);
        }
        break;
      case OC_BOOT_APPLE_FW_UPDATE:
        ContentFlavour = AllocateCopyPool(sizeof (OC_FLAVOUR_APPLE_FW), OC_FLAVOUR_APPLE_FW);
        break;
      case OC_BOOT_APPLE_RECOVERY:
        ContentFlavour = AllocateCopyPool(sizeof (OC_FLAVOUR_APPLE_RECOVERY), OC_FLAVOUR_APPLE_RECOVERY);
        break;
      case OC_BOOT_APPLE_TIME_MACHINE:
        ContentFlavour = AllocateCopyPool(sizeof (OC_FLAVOUR_APPLE_TIME_MACHINE), OC_FLAVOUR_APPLE_TIME_MACHINE);
        break;
      case OC_BOOT_WINDOWS:
        ContentFlavour = AllocateCopyPool(sizeof (OC_FLAVOUR_WINDOWS), OC_FLAVOUR_WINDOWS);
        break;
      case OC_BOOT_UNKNOWN:
        ContentFlavour = NULL;
        break;
      default:
        DEBUG ((DEBUG_ERROR, "OCB: Entry kind %d unsupported for flavour\n", BootEntry->Type));
        ContentFlavour = NULL;
        break;
    }
    if ((BootEntry->Type & OC_BOOT_APPLE_ANY) != 0) {
      if (ContentFlavour == NULL) {
        ASSERT (FALSE);
      } else if (AppleVersion[0] != '\0'
        && (BootContext->PickerContext->PickerAttributes & OC_ATTR_USE_FLAVOUR_ICON) != 0
        ) {
        //
        // If 10.x(.y) remove .y and replace first . with _, otherwise remove from first .
        //
        Dot = AsciiStrStr (AppleVersion, ".");
        if (Dot != NULL) {
          if (Dot - AppleVersion == sizeof ("10") - 1
            && OcAsciiStartsWith (AppleVersion, "10", FALSE)
            ) {
            *Dot++ = '_';
            Dot = AsciiStrStr (Dot, ".");
            if (Dot != NULL) {
              *Dot = '\0';
            }
          } else {
            *Dot = '\0';
          }
        }
        ContentFlavour = InternalAddAppleVersion (ContentFlavour, AppleVersion);
      }
    }
    if (ContentFlavour == NULL && BootEntry->Flavour == NULL) {
      ContentFlavour = AllocateCopyPool(sizeof(OC_FLAVOUR_AUTO), OC_FLAVOUR_AUTO);
    }
    if (ContentFlavour != NULL) {
      if (BootEntry->Flavour != NULL) {
        FreePool (BootEntry->Flavour);
      }
      BootEntry->Flavour = ContentFlavour;
    }
  }

814
  return EFI_SUCCESS;
815
}