OcBootManagementLib.h 32.4 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.
**/

#ifndef OC_BOOT_MANAGEMENT_LIB_H
#define OC_BOOT_MANAGEMENT_LIB_H

18 19
#include <Uefi.h>
#include <IndustryStandard/AppleBootArgs.h>
20
#include <IndustryStandard/AppleHid.h>
21
#include <Library/OcAppleBootPolicyLib.h>
22
#include <Library/OcStringLib.h>
V
vit9696 已提交
23
#include <Library/OcStorageLib.h>
24
#include <Protocol/AppleKeyMapAggregator.h>
25
#include <Protocol/LoadedImage.h>
26 27 28 29 30 31 32
#include <Protocol/AppleBeepGen.h>
#include <Protocol/OcAudio.h>

/**
  Primary picker context.
**/
typedef struct OC_PICKER_CONTEXT_ OC_PICKER_CONTEXT;
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/**
  Default strings for use in the interfaces.
**/
#define OC_MENU_BOOT_MENU            L"OpenCore Boot Menu"
#define OC_MENU_RESET_NVRAM_ENTRY    L"Reset NVRAM"
#define OC_MENU_UEFI_SHELL_ENTRY     L"UEFI Shell"
#define OC_MENU_PASSWORD_REQUEST     L"Password: "
#define OC_MENU_PASSWORD_RETRY_LIMIT L"Password retry limit exceeded."
#define OC_MENU_CHOOSE_OS            L"Choose the Operating System: "
#define OC_MENU_SHOW_AUXILIARY       L"Show Auxiliary"
#define OC_MENU_RELOADING            L"Reloading"
#define OC_MENU_TIMEOUT              L"Timeout"
#define OC_MENU_OK                   L"OK"
#define OC_MENU_DISK_IMAGE           L" (dmg)"
#define OC_MENU_EXTERNAL             L" (external)"

V
vit9696 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
/**
  Paths allowed to be accessible by the interfaces.
**/
#define OPEN_CORE_IMAGE_PATH       L"Resources\\Image\\"
#define OPEN_CORE_LABEL_PATH       L"Resources\\Label\\"
#define OPEN_CORE_AUDIO_PATH       L"Resources\\Audio\\"
#define OPEN_CORE_FONT_PATH        L"Resources\\Font\\"

/**
  Attributes supported by the interfaces.
**/
#define OC_ATTR_USE_VOLUME_ICON          BIT0
#define OC_ATTR_USE_DISK_LABEL_FILE      BIT1
#define OC_ATTR_USE_GENERIC_LABEL_IMAGE  BIT2
64
#define OC_ATTR_USE_ALTERNATE_ICONS      BIT3
V
vit9696 已提交
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/**
  Default timeout for IDLE timeout during menu picker navigation
  before VoiceOver toggle.
**/
#define OC_VOICE_OVER_IDLE_TIMEOUT_MS     700  ///< Experimental, less is problematic.

/**
  Default VoiceOver BeepGen protocol values.
**/
#define OC_VOICE_OVER_SIGNAL_NORMAL_MS    200  ///< From boot.efi, constant.
#define OC_VOICE_OVER_SILENCE_NORMAL_MS   150  ///< From boot.efi, constant.
#define OC_VOICE_OVER_SIGNALS_NORMAL      1    ///< Username prompt or any input for boot.efi
#define OC_VOICE_OVER_SIGNALS_PASSWORD    2    ///< Password prompt for boot.efi
#define OC_VOICE_OVER_SIGNALS_PASSWORD_OK 3    ///< Password correct for boot.efi

#define OC_VOICE_OVER_SIGNAL_ERROR_MS     1000
#define OC_VOICE_OVER_SILENCE_ERROR_MS    150
#define OC_VOICE_OVER_SIGNALS_ERROR       1    ///< Password verification error or boot failure.
#define OC_VOICE_OVER_SIGNALS_HWERROR     3    ///< Hardware error

86 87 88 89
/**
  Operating system boot type.
  WARNING: This is only for debug purposes.
**/
90 91 92 93 94 95
typedef UINT32 OC_BOOT_ENTRY_TYPE;

#define OC_BOOT_UNKNOWN             BIT0
#define OC_BOOT_APPLE_OS            BIT1
#define OC_BOOT_APPLE_RECOVERY      BIT2
#define OC_BOOT_APPLE_TIME_MACHINE  BIT3
96 97 98 99 100 101
#define OC_BOOT_APPLE_FW_UPDATE     BIT4
#define OC_BOOT_APPLE_ANY           (OC_BOOT_APPLE_OS | OC_BOOT_APPLE_RECOVERY | OC_BOOT_APPLE_TIME_MACHINE | OC_BOOT_APPLE_FW_UPDATE)
#define OC_BOOT_WINDOWS             BIT5
#define OC_BOOT_EXTERNAL_OS         BIT6
#define OC_BOOT_EXTERNAL_TOOL       BIT7
#define OC_BOOT_RESET_NVRAM         BIT8
V
vit9696 已提交
102
#define OC_BOOT_SYSTEM              (OC_BOOT_RESET_NVRAM)
103

104 105 106 107 108 109
/**
  Default boot option numbers.
**/
#define OC_BOOT_OPTION                0x9696
#define OC_BOOT_OPTION_VARIABLE_NAME  L"Boot9696"

110 111 112 113 114 115 116 117 118
/**
  Picker mode.
**/
typedef enum OC_PICKER_MODE_ {
  OcPickerModeBuiltin,
  OcPickerModeExternal,
  OcPickerModeApple,
} OC_PICKER_MODE;

119 120 121 122 123 124 125 126 127
/**
  Action to perform as part of executing a system boot entry.
**/
typedef
EFI_STATUS
(*OC_BOOT_SYSTEM_ACTION)(
  VOID
  );

128 129 130 131 132
/**
  Discovered boot entry.
  Note, inner resources must be freed with OcResetBootEntry.
**/
typedef struct OC_BOOT_ENTRY_ {
133 134 135 136
  //
  // Link in entry list in OC_BOOT_FILESYSTEM.
  //
  LIST_ENTRY                Link;
137 138
  //
  // Device path to booter or its directory.
139
  // Can be NULL, for example, for custom or system entries.
140 141 142
  //
  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
  //
143 144 145 146
  // Action to perform on execution. Only valid for system entries.
  //
  OC_BOOT_SYSTEM_ACTION     SystemAction;
  //
147 148 149 150 151
  // Obtained human visible name.
  //
  CHAR16                    *Name;
  //
  // Obtained boot path directory.
152
  // For custom entries this contains tool path.
153 154 155
  //
  CHAR16                    *PathName;
  //
156 157
  // Heuristical value signalising about booted os.
  // WARNING: This is only for debug purposes.
158
  //
159
  OC_BOOT_ENTRY_TYPE        Type;
160
  //
161 162 163 164
  // Entry index number, assigned by picker.
  //
  UINT32                    EntryIndex;
  //
165 166 167 168
  // Set when this entry is an externally available entry (e.g. USB).
  //
  BOOLEAN                   IsExternal;
  //
169 170 171 172
  // Should try booting from first dmg found in DevicePath.
  //
  BOOLEAN                   IsFolder;
  //
173 174 175 176
  // Should make this option default boot option.
  //
  BOOLEAN                   SetDefault;
  //
177
  // Load option data (usually "boot args") size.
178 179
  //
  UINT32                    LoadOptionsSize;
180 181 182
  //
  // Load option data (usually "boot args").
  //
183
  VOID                      *LoadOptions;
184 185
} OC_BOOT_ENTRY;

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/**
  Boot filesystem containing boot entries.
**/
typedef struct OC_BOOT_FILESYSTEM_ OC_BOOT_FILESYSTEM;
struct OC_BOOT_FILESYSTEM_ {
  //
  // Link in filesystem list in OC_BOOT_CONTEXT.
  //
  LIST_ENTRY           Link;
  //
  // Filesystem handle.
  //
  EFI_HANDLE           Handle;
  //
  // List of boot entries (OC_BOOT_ENTRY).
  //
  LIST_ENTRY           BootEntries;
  //
  // Pointer to APFS Recovery partition (if any).
  //
  OC_BOOT_FILESYSTEM   *RecoveryFs;
  //
  // External filesystem.
  //
  BOOLEAN              External;
  //
  // Loader filesystem.
  //
  BOOLEAN              LoaderFs;
  //
  // Contains recovery on the filesystem.
  //
  BOOLEAN              HasSelfRecovery;
};

/**
  Boot context containing boot filesystems.
**/
typedef struct OC_BOOT_CONTEXT_ {
  //
  // Total boot entry count.
  //
  UINTN                       BootEntryCount;
  //
  // Total filesystem count.
  //
  UINTN                       FileSystemCount;
  //
  // List of filesystems containing boot entries (OC_BOOT_FILESYSTEM).
  //
  LIST_ENTRY                  FileSystems;
  //
  // GUID namespace for boot entries.
  //
  EFI_GUID                    *BootVariableGuid;
  //
  // Default entry to be booted.
  //
  OC_BOOT_ENTRY               *DefaultEntry;
  //
  // Picker context for externally configured parameters.
  //
  OC_PICKER_CONTEXT           *PickerContext;
  //
  // Boot policy protocol.
  //
  APPLE_BOOT_POLICY_PROTOCOL  *BootPolicy;
} OC_BOOT_CONTEXT;

255
/**
256 257 258
  Perform filtering based on file system basis.
  Ignores all filesystems by default.
  Remove this bit to allow any file system.
259
**/
260 261 262 263 264 265 266 267 268 269 270 271 272 273
#define OC_SCAN_FILE_SYSTEM_LOCK         BIT0

/**
  Perform filtering based on device basis.
  Ignores all devices by default.
  Remove this bit to allow any device type.
**/
#define OC_SCAN_DEVICE_LOCK              BIT1

/**
  Allow scanning APFS filesystems.
**/
#define OC_SCAN_ALLOW_FS_APFS            BIT8

274 275 276 277 278
/**
  Allow scanning HFS filesystems.
**/
#define OC_SCAN_ALLOW_FS_HFS             BIT9

279 280 281 282 283
/**
  Allow scanning ESP filesystems.
**/
#define OC_SCAN_ALLOW_FS_ESP             BIT10

284 285 286 287 288 289 290 291 292 293
/**
  Allow scanning NTFS filesystems.
**/
#define OC_SCAN_ALLOW_FS_NTFS            BIT11

/**
  Allow scanning EXT filesystems (e.g. EXT4).
**/
#define OC_SCAN_ALLOW_FS_EXT             BIT12

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
/**
  Allow scanning SATA devices.
**/
#define OC_SCAN_ALLOW_DEVICE_SATA        BIT16

/**
  Allow scanning SAS and Mac NVMe devices.
**/
#define OC_SCAN_ALLOW_DEVICE_SASEX       BIT17

/**
  Allow scanning SCSI devices.
**/
#define OC_SCAN_ALLOW_DEVICE_SCSI        BIT18

/**
  Allow scanning NVMe devices.
**/
#define OC_SCAN_ALLOW_DEVICE_NVME        BIT19

/**
  Allow scanning ATAPI devices.
**/
#define OC_SCAN_ALLOW_DEVICE_ATAPI       BIT20

/**
  Allow scanning USB devices.
**/
#define OC_SCAN_ALLOW_DEVICE_USB         BIT21

/**
  Allow scanning FireWire devices.
**/
#define OC_SCAN_ALLOW_DEVICE_FIREWIRE    BIT22

/**
  Allow scanning SD card devices.
**/
#define OC_SCAN_ALLOW_DEVICE_SDCARD      BIT23

334 335 336 337 338 339 340 341 342 343 344 345 346
/**
  All device bits used by OC_SCAN_DEVICE_LOCK.
**/
#define OC_SCAN_DEVICE_BITS ( \
  OC_SCAN_ALLOW_DEVICE_SATA     | OC_SCAN_ALLOW_DEVICE_SASEX | \
  OC_SCAN_ALLOW_DEVICE_SCSI     | OC_SCAN_ALLOW_DEVICE_NVME  | \
  OC_SCAN_ALLOW_DEVICE_ATAPI    | OC_SCAN_ALLOW_DEVICE_USB   | \
  OC_SCAN_ALLOW_DEVICE_FIREWIRE | OC_SCAN_ALLOW_DEVICE_SDCARD)

/**
  All device bits used by OC_SCAN_DEVICE_LOCK.
**/
#define OC_SCAN_FILE_SYSTEM_BITS ( \
347 348
  OC_SCAN_ALLOW_FS_APFS | OC_SCAN_ALLOW_FS_HFS | OC_SCAN_ALLOW_FS_ESP | \
  OC_SCAN_ALLOW_FS_NTFS | OC_SCAN_ALLOW_FS_EXT)
349

350 351 352
/**
  By default allow booting from APFS from internal drives.
**/
353
#define OC_SCAN_DEFAULT_POLICY ( \
354
  OC_SCAN_FILE_SYSTEM_LOCK   | OC_SCAN_DEVICE_LOCK | \
355
  OC_SCAN_ALLOW_FS_APFS | \
356 357
  OC_SCAN_ALLOW_DEVICE_SATA  | OC_SCAN_ALLOW_DEVICE_SASEX | \
  OC_SCAN_ALLOW_DEVICE_SCSI  | OC_SCAN_ALLOW_DEVICE_NVME)
358

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
/**
  OcLoadBootEntry Mode policy bits allow to configure OcLoadBootEntry behaviour.
**/

/**
  Thin EFI image loading (normal PE) is allowed.
**/
#define OC_LOAD_ALLOW_EFI_THIN_BOOT  BIT0
/**
  FAT EFI image loading (Apple FAT PE) is allowed.
  These can be found on macOS 10.8 and below.
**/
#define OC_LOAD_ALLOW_EFI_FAT_BOOT   BIT1
/**
  One level recursion into dmg file is allowed.
  It is assumed that dmg contains a single volume and a single blessed entry.
  Loading dmg from dmg is not allowed in any case.
**/
#define OC_LOAD_ALLOW_DMG_BOOT       BIT2
/**
  Abort loading on invalid Apple-like signature.
  If file is signed with Apple-like signature, and it is mismatched, then abort.
  @warn Unsigned files or UEFI-signed files will skip this check.
  @warn It is ignored what certificate was used for signing.
**/
#define OC_LOAD_VERIFY_APPLE_SIGN    BIT8
/**
  Abort loading on missing Apple-like signature.
  If file is not signed with Apple-like signature (valid or not) then abort.
  @warn Unsigned files or UEFI-signed files will not load with this check.
  @warn Without OC_LOAD_VERIFY_APPLE_SIGN corrupted binaries may still load.
**/
#define OC_LOAD_REQUIRE_APPLE_SIGN   BIT9
/**
  Abort loading on untrusted key (otherwise may warn).
  @warn Unsigned files or UEFI-signed files will skip this check.
**/
#define OC_LOAD_REQUIRE_TRUSTED_KEY  BIT10
/**
  Trust specified (as OcLoadBootEntry argument) custom keys.
**/
#define OC_LOAD_TRUST_CUSTOM_KEY     BIT16
/**
  Trust Apple CFFD3E6B public key.
  TODO: Move certificates from ApplePublicKeyDb.h to EfiPkg?
**/
#define OC_LOAD_TRUST_APPLE_V1_KEY   BIT17
/**
  Trust Apple E50AC288 public key.
  TODO: Move certificates from ApplePublicKeyDb.h to EfiPkg?
**/
#define OC_LOAD_TRUST_APPLE_V2_KEY   BIT18
/**
  Default moderate policy meant to augment secure boot facilities.
  Loads almost everything and bypasses secure boot for Apple and Custom signed binaries.
**/
#define OC_LOAD_DEFAULT_POLICY ( \
  OC_LOAD_ALLOW_EFI_THIN_BOOT | OC_LOAD_ALLOW_DMG_BOOT      | OC_LOAD_REQUIRE_APPLE_SIGN | \
  OC_LOAD_VERIFY_APPLE_SIGN   | OC_LOAD_REQUIRE_TRUSTED_KEY | \
  OC_LOAD_TRUST_CUSTOM_KEY    | OC_LOAD_TRUST_APPLE_V1_KEY  | OC_LOAD_TRUST_APPLE_V2_KEY)

/**
  Exposed start interface with chosen boot entry but otherwise equivalent
  to EFI_BOOT_SERVICES StartImage.
**/
typedef
EFI_STATUS
(EFIAPI *OC_IMAGE_START) (
  IN  OC_BOOT_ENTRY               *ChosenEntry,
  IN  EFI_HANDLE                  ImageHandle,
  OUT UINTN                       *ExitDataSize,
  OUT CHAR16                      **ExitData    OPTIONAL
  );

433 434
/**
  Exposed custom entry load interface.
435
  Returns allocated file buffer from pool on success.
436 437 438 439 440 441 442
**/
typedef
EFI_STATUS
(EFIAPI *OC_CUSTOM_READ) (
  IN  VOID                        *Context,
  IN  OC_BOOT_ENTRY               *ChosenEntry,
  OUT VOID                        **Data,
443
  OUT UINT32                      *DataSize,
444 445 446
  OUT EFI_DEVICE_PATH_PROTOCOL    **DevicePath         OPTIONAL,
  OUT EFI_HANDLE                  *ParentDeviceHandle  OPTIONAL,
  OUT EFI_DEVICE_PATH_PROTOCOL    **ParentFilePath     OPTIONAL
447 448
  );

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
/**
  Exposed custom entry describe interface.
  Return allocated file buffers from pool on success.
**/
typedef
EFI_STATUS
(EFIAPI *OC_CUSTOM_DESCRIBE) (
  IN  VOID                        *Context,
  IN  OC_BOOT_ENTRY               *ChosenEntry,
  IN  UINT8                       LabelScale           OPTIONAL,
  OUT VOID                        **IconData           OPTIONAL,
  OUT UINT32                      *IconDataSize        OPTIONAL,
  OUT VOID                        **LabelData          OPTIONAL,
  OUT UINT32                      *LabelDataSize       OPTIONAL
  );

465
/**
466
  Custom picker entry.
467 468
**/
typedef struct {
469 470 471 472 473 474 475 476
  //
  // Entry name.
  //
  CONST CHAR8  *Name;
  //
  // Entry path.
  //
  CONST CHAR8  *Path;
477 478 479 480
  //
  // Entry boot arguments.
  //
  CONST CHAR8  *Arguments;
481 482 483 484
  //
  // Whether this entry is auxiliary.
  //
  BOOLEAN      Auxiliary;
485 486 487 488
  //
  // Whether this entry is a tool.
  //
  BOOLEAN      Tool;
489 490
} OC_PICKER_ENTRY;

491 492 493 494 495 496 497 498 499 500 501 502 503 504
/**
  Privilege levels to escalate to
**/
typedef enum {
  OcPrivilegeUnauthorized = 0,
  OcPrivilegeAuthorized   = 1
} OC_PRIVILEGE_LEVEL;

/**
  Request a privilege escalation, for example by prompting for a password.
**/
typedef
EFI_STATUS
(EFIAPI *OC_REQ_PRIVILEGE)(
505
  IN OC_PICKER_CONTEXT   *Context,
506 507 508
  IN OC_PRIVILEGE_LEVEL  Level
  );

509 510 511 512 513 514
/**
  Display entries onscreen.
**/
typedef
EFI_STATUS
(EFIAPI *OC_SHOW_MENU) (
515 516
  IN  OC_BOOT_CONTEXT             *BootContext,
  IN  OC_BOOT_ENTRY               **BootEntries,
517 518 519
  OUT OC_BOOT_ENTRY               **ChosenBootEntry
  );

520 521 522 523 524 525 526 527 528 529 530
/**
  Picker behaviour action.
**/
typedef enum {
  OcPickerDefault           = 0,
  OcPickerShowPicker        = 1,
  OcPickerResetNvram        = 2,
  OcPickerBootApple         = 3,
  OcPickerBootAppleRecovery = 4,
} OC_PICKER_CMD;

531 532 533
/**
  Boot picker context describing picker behaviour.
**/
534
struct OC_PICKER_CONTEXT_ {
535 536 537
  //
  // Scan policy (e.g. OC_SCAN_DEFAULT_POLICY).
  //
538
  UINT32                     ScanPolicy;
539 540 541
  //
  // Load policy (e.g. OC_LOAD_DEFAULT_POLICY).
  //
542
  UINT32                     LoadPolicy;
543 544 545
  //
  // Default entry selection timeout (pass 0 to ignore).
  //
546
  UINT32                     TimeoutSeconds;
547
  //
548 549
  // Default delay prior to handling hotkeys (pass 0 to ignore).
  //
550
  UINT32                     TakeoffDelay;
551
  //
552 553
  // Define picker behaviour.
  // For example, show boot menu or just boot the default option.
554
  //
555
  OC_PICKER_CMD              PickerCommand;
556 557 558
  //
  // Use custom (gOcVendorVariableGuid) for Boot#### variables.
  //
559
  BOOLEAN                    CustomBootGuid;
560
  //
561 562 563 564
  // Ignore Apple peripheral firmware updates.
  //
  BOOLEAN                    BlacklistAppleUpdate;
  //
565 566
  // Custom entry reading routine, optional for no custom entries.
  //
567
  OC_CUSTOM_READ             CustomRead;
568 569 570 571
  //
  // Custom entry describing routine, optional for no custom entries.
  //
  OC_CUSTOM_DESCRIBE         CustomDescribe;
572
  //
573
  // Context to pass to CustomRead and CustomDescribe, optional.
574
  //
575
  VOID                       *CustomEntryContext;
576
  //
577 578
  // Image starting routine used, required.
  //
579
  OC_IMAGE_START             StartImage;
580
  //
581
  // Handle to perform loader detection, optional.
582
  //
583
  EFI_HANDLE                 LoaderHandle;
584
  //
585 586 587 588
  // Entry display routine.
  //
  OC_SHOW_MENU               ShowMenu;
  //
589 590
  // Privilege escalation requesting routine.
  //
591
  OC_REQ_PRIVILEGE           RequestPrivilege;
592 593 594
  //
  // Context to pass to RequestPrivilege, optional.
  //
595
  VOID                       *PrivilegeContext;
596
  //
597 598
  // Additional suffix to include by the interface.
  //
599
  CONST CHAR8                *TitleSuffix;
600
  //
601 602
  // Used picker mode.
  //
603
  OC_PICKER_MODE             PickerMode;
604
  //
605 606
  // Console attributes. 0 is reserved as disabled.
  //
607
  UINT32                     ConsoleAttributes;
608
  //
V
vit9696 已提交
609 610 611 612 613 614
  // Picker attribues:
  // - BIT0~BIT15  are OpenCore reserved.
  // - BIT16~BIT31 are OEM-specific.
  //
  UINT32                     PickerAttributes;
  //
615 616
  // Enable polling boot arguments.
  //
617
  BOOLEAN                    PollAppleHotKeys;
618
  //
619 620
  // Append the "Reset NVRAM" option to the boot entry list.
  //
621
  BOOLEAN                    ShowNvramReset;
622
  //
623 624
  // Allow setting default boot option from boot menu.
  //
625
  BOOLEAN                    AllowSetDefault;
626
  //
627 628
  // Hide and do not scan auxiliary entries.
  //
629 630 631 632 633 634
  BOOLEAN                    HideAuxiliary;
  //
  // Enable audio assistant during picker playback.
  //
  BOOLEAN                    PickerAudioAssist;
  //
635 636 637 638
  // Set when Apple picker cannot be used on this system.
  //
  BOOLEAN                    ApplePickerUnsupported;
  //
639 640 641 642 643 644 645
  // Recommended audio protocol, optional.
  //
  OC_AUDIO_PROTOCOL          *OcAudio;
  //
  // Recommended beeper protocol, optional.
  //
  APPLE_BEEP_GEN_PROTOCOL    *BeepGen;
646
  //
647 648 649 650 651 652 653 654 655
  // Custom boot order updated during scanning allocated from pool.
  // Preserved here to avoid situations with losing BootNext on rescan.
  //
  UINT16                     *BootOrder;
  //
  // Number of entries in boot order.
  //
  UINTN                      BootOrderCount;
  //
656 657
  // Additional boot arguments for Apple loaders.
  //
658
  CHAR8                      AppleBootArgs[BOOT_LINE_LENGTH];
659
  //
660 661
  // Number of custom boot paths (bless override).
  //
662
  UINTN                      NumCustomBootPaths;
663 664 665
  //
  // Custom boot paths (bless override).  Must start with '\'.
  //
666
  CHAR16                     **CustomBootPaths;
667
  //
668
  // Number of absolute custom entries.
669
  //
670
  UINT32                     AbsoluteEntryCount;
671
  //
672 673
  // Number of total custom entries (absolute and tools).
  //
674
  UINT32                     AllCustomEntryCount;
675 676
  //
  // Custom picker entries.  Absolute entries come first.
677
  //
678 679
  OC_PICKER_ENTRY            CustomEntries[];
};
680

681 682 683 684 685 686 687
/**
  Hibernate detection bit mask for hibernate source usage.
**/
#define HIBERNATE_MODE_NONE   0U
#define HIBERNATE_MODE_RTC    1U
#define HIBERNATE_MODE_NVRAM  2U

V
vit9696 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
/**
  Get '.disk_label' or '.disk_label_2x' file contents, if exists.

  @param[in]   BootPolicy     Apple Boot Policy Protocol.
  @param[in]   BootEntry      Located boot entry.
  @param[in]   Scale          User interface scale.
  @param[out]  ImageData      File contents.
  @param[out]  DataLength     File length.

  @retval EFI_SUCCESS   The file was read successfully.
**/
EFI_STATUS
OcGetBootEntryLabelImage (
  IN  OC_PICKER_CONTEXT          *Context,
  IN  APPLE_BOOT_POLICY_PROTOCOL *BootPolicy,
  IN  OC_BOOT_ENTRY              *BootEntry,
  IN  UINT8                      Scale,
  OUT VOID                       **ImageData,
  OUT UINT32                     *DataLength
  );

/**
  Get '.VolumeIcon.icns' file contents, if exists.

  @param[in]   BootPolicy     Apple Boot Policy Protocol.
  @param[in]   BootEntry      Located boot entry.
  @param[out]  ImageData      File contents.
  @param[out]  DataLength     File length.

  @retval EFI_SUCCESS   The file was read successfully.
**/
EFI_STATUS
OcGetBootEntryIcon (
  IN  OC_PICKER_CONTEXT          *Context,
  IN  APPLE_BOOT_POLICY_PROTOCOL *BootPolicy,
  IN  OC_BOOT_ENTRY              *BootEntry,
  OUT VOID                       **ImageData,
  OUT UINT32                     *DataLength
  );

728
/**
729 730 731 732
  Scan system for boot entries.

  @param[in]  BootPolicy     Apple Boot Policy Protocol.
  @param[in]  Context        Picker context.
733

734
  @retval boot context allocated from pool.
735
**/
736 737 738 739
OC_BOOT_CONTEXT *
OcScanForBootEntries (
  IN  APPLE_BOOT_POLICY_PROTOCOL  *BootPolicy,
  IN  OC_PICKER_CONTEXT           *Context
740 741 742
  );

/**
743 744 745 746 747 748
  Scan system for first entry to boot.
  This is likely to return an incomplete list and can even give NULL,
  when only tools and system entries are present.

  @param[in]  BootPolicy     Apple Boot Policy Protocol.
  @param[in]  Context        Picker context.
749

750
  @retval boot context allocated from pool.
751
**/
752 753 754 755
OC_BOOT_CONTEXT *
OcScanForDefaultBootEntry (
  IN  APPLE_BOOT_POLICY_PROTOCOL  *BootPolicy,
  IN  OC_PICKER_CONTEXT           *Context
756 757
  );

758
/**
759
  Perform boot entry enumeration.
760

761
  @param[in]  BootContext    Boot context.
762

763
  @retval enumerated boot entry list allocated from pool.
764
**/
765 766 767 768 769 770 771 772 773 774 775 776 777
OC_BOOT_ENTRY  **
OcEnumerateEntries (
  IN  OC_BOOT_CONTEXT  *BootContext
  );

/**
  Free boot context.

  @param[in,out]  Context    Boot context to free.
**/
VOID
OcFreeBootContext (
  IN OUT OC_BOOT_CONTEXT  *Context
778 779
  );

780
/**
781
  Obtain default entry from picker context.
782

783
  @param[in]      Context          Picker context.
784 785 786
  @param[in,out]  BootEntries      Described list of entries, may get updated.
  @param[in]      NumBootEntries   Positive number of boot entries.

787
  @retval  boot entry or 0.
788
**/
789
UINT32
790
OcGetDefaultBootEntry (
791 792 793
  IN     OC_PICKER_CONTEXT  *Context,
  IN OUT OC_BOOT_ENTRY      *BootEntries,
  IN     UINTN              NumBootEntries
794 795
  );

796 797 798 799 800 801 802 803 804 805 806 807 808 809
/**
  Set default entry to passed entry.

  @param[in]      Context          Picker context.
  @param[in,out]  Entry            Entry to make default.

  @retval EFI_SUCCESS on success.
**/
EFI_STATUS
OcSetDefaultBootEntry (
  IN OC_PICKER_CONTEXT  *Context,
  IN OC_BOOT_ENTRY      *Entry
  );

810 811 812 813 814 815 816 817 818 819
typedef struct {
  OC_PRIVILEGE_LEVEL CurrentLevel;
  CONST UINT8        *Salt;
  UINT32             SaltSize;
  CONST UINT8        *Hash;
} OC_PRIVILEGE_CONTEXT;

/**
  Show simple password prompt and return verification status.

820
  @param[in]  Context          Picker context.
821
  @param[in]  Level            The privilege level to request escalating to.
822 823 824 825 826 827 828 829 830

  @retval EFI_SUCCESS  The privilege level has been escalated successfully.
  @retval EFI_ABORTED  The privilege escalation has been aborted.
  @retval other        The system must be considered compromised.

**/
EFI_STATUS
EFIAPI
OcShowSimplePasswordRequest (
831 832
  IN OC_PICKER_CONTEXT      *Context,
  IN OC_PRIVILEGE_LEVEL     Level
833 834
  );

835 836 837
/**
  Show simple boot entry selection menu and return chosen entry.

838 839
  @param[in]  BootContext      Boot context.
  @param[in]  BootEntries      Enumerated entries.
840 841 842 843 844 845
  @param[in]  ChosenBootEntry  Chosen boot entry from BootEntries on success.

  @retval EFI_SUCCESS          Executed successfully and picked up an entry.
  @retval EFI_ABORTED          When the user chose to by pressing Esc or 0.
**/
EFI_STATUS
846
EFIAPI
847
OcShowSimpleBootMenu (
848 849
  IN  OC_BOOT_CONTEXT             *BootContext,
  IN  OC_BOOT_ENTRY               **BootEntries,
850 851 852 853
  OUT OC_BOOT_ENTRY               **ChosenBootEntry
  );

/**
854
  Load & start boot entry loader image with given options.
855

D
Download-Fritz 已提交
856
  @param[in]  BootPolicy     Apple Boot Policy Protocol.
857
  @param[in]  Context        Picker context.
858 859 860
  @param[in]  BootEntry      Located boot entry.
  @param[in]  ParentHandle   Parent image handle.

861
  @retval EFI_SUCCESS        The image was found, started, and ended succesfully.
862 863 864
**/
EFI_STATUS
OcLoadBootEntry (
D
Download-Fritz 已提交
865
  IN  APPLE_BOOT_POLICY_PROTOCOL  *BootPolicy,
866
  IN  OC_PICKER_CONTEXT           *Context,
867
  IN  OC_BOOT_ENTRY               *BootEntry,
868
  IN  EFI_HANDLE                  ParentHandle
869 870
  );

871 872 873 874 875 876 877 878
/**
  Handle hibernation detection for later loading.

  @param[in]  HibernateMask  Hibernate detection mask.

  @retval EFI_SUCCESS        Hibernation mode was found and activated.
**/
EFI_STATUS
879
OcActivateHibernateWake (
880 881 882
  IN UINT32                       HibernateMask
  );

V
vit9696 已提交
883 884 885 886 887 888 889 890 891 892
/**
  Check if active hibernation is happening.

  @retval TRUE on waking from hibernation.
**/
BOOLEAN
OcIsAppleHibernateWake (
  VOID
  );

893 894 895 896 897 898
/**
  Check pressed hotkeys and update booter context based on this.

  @param[in,out]  Context       Picker context.
**/
VOID
899
OcLoadPickerHotKeys (
900 901 902
  IN OUT OC_PICKER_CONTEXT  *Context
  );

903 904 905
/**
  Default index mapping macros.
**/
906 907 908 909 910 911 912 913 914 915 916 917 918
#define OC_INPUT_STR            "123456789ABCDEFGHIJKLMNOPQRSTUVXWZ"
#define OC_INPUT_MAX            L_STR_LEN (OC_INPUT_STR)
#define OC_INPUT_ABORTED        -1        ///< Esc or 0
#define OC_INPUT_INVALID        -2        ///< Some other key
#define OC_INPUT_TIMEOUT        -3        ///< Timeout
#define OC_INPUT_CONTINUE       -4        ///< Continue (press enter)
#define OC_INPUT_UP             -5        ///< Move up
#define OC_INPUT_DOWN           -6        ///< Move down
#define OC_INPUT_LEFT           -7        ///< Move left
#define OC_INPUT_RIGHT          -8        ///< Move right
#define OC_INPUT_TOP            -9        ///< Move to top
#define OC_INPUT_BOTTOM         -10       ///< Move to bottom
#define OC_INPUT_MORE           -11       ///< Show more entries (press space)
919
#define OC_INPUT_VOICE_OVER     -12       ///< Toggle VoiceOver (press CMD+F5)
920
#define OC_INPUT_FUNCTIONAL(x) (-20 - (x))  ///< Functional hotkeys
921

922 923 924
/**
  Obtains key index from user input.

925
  @param[in,out]  Context      Picker context.
926
  @param[in]      KeyMap       Apple Key Map Aggregator protocol.
927
  @param[in]      Timeout      Timeout to wait for in milliseconds.
928
  @param[in]      PollHotkeys  Poll key combinations.
929
  @param[out]     SetDefault   Set boot option as default, optional.
930

931
  @returns key index [0, OC_INPUT_MAX) or OC_INPUT_* value.
932 933 934
**/
INTN
OcWaitForAppleKeyIndex (
935 936 937 938 939
  IN OUT OC_PICKER_CONTEXT                  *Context,
  IN     APPLE_KEY_MAP_AGGREGATOR_PROTOCOL  *KeyMap,
  IN     UINTN                              Timeout,
  IN     BOOLEAN                            PollHotkeys,
     OUT BOOLEAN                            *SetDefault  OPTIONAL
940 941
  );

942 943 944
/**
  Install missing boot policy, scan, and show simple boot menu.

945
  @param[in]  Context       Picker context.
946 947 948 949

  @retval does not return unless a fatal error happened.
**/
EFI_STATUS
950
OcRunBootPicker (
951
  IN  OC_PICKER_CONTEXT  *Context
952 953
  );

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
/**
  Get device scan policy type.

  @param[in]  Handle        Device/partition handle.
  @param[out] External      Check whether device is external.

  @retval required policy or 0 on mismatch.
**/
UINT32
OcGetDevicePolicyType (
  IN  EFI_HANDLE   Handle,
  OUT BOOLEAN      *External  OPTIONAL
  );

/**
  Get file system scan policy type.

  @param[in]  Handle        Partition handle.

  @retval required policy or 0 on mismatch.
**/
UINT32
OcGetFileSystemPolicyType (
  IN  EFI_HANDLE   Handle
  );

980
/**
981
  Check if supplied device path contains known names (e.g. Apple bootloader).
982

983 984
  @param[in]   DevicePath        Device path.
  @param[out]  IsFolder          Device path represents directory, optional.
985

986 987
  @retval entry type for potentially known bootloaders.
  @retval OC_BOOT_UNKNOWN for unknown bootloaders.
988
**/
989 990 991
OC_BOOT_ENTRY_TYPE
OcGetBootDevicePathType (
  IN EFI_DEVICE_PATH_PROTOCOL  *DevicePath,
992
  OUT BOOLEAN                  *IsFolder  OPTIONAL
993 994
  );

995 996 997
/**
  Get loaded image protocol for Apple bootloader.

998
  @param[in]  ImageHandle        Image handle.
999 1000 1001 1002 1003 1004 1005 1006

  @retval loaded image protocol or NULL for non Apple images.
**/
EFI_LOADED_IMAGE_PROTOCOL *
OcGetAppleBootLoadedImage (
  IN EFI_HANDLE  ImageHandle
  );

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
/**
  Unified structure to hold macOS kernel boot arguments to make the code
  independent of their format version. Several values need changing
  by other libraries, so values are often pointers to original fields.
**/
typedef struct OC_BOOT_ARGUMENTS_ {
  UINT32  *MemoryMap;
  UINT32  *MemoryMapSize;
  UINT32  *MemoryMapDescriptorSize;
  UINT32  *MemoryMapDescriptorVersion;
  CHAR8   *CommandLine;
  UINT32  *DeviceTreeP;
  UINT32  *DeviceTreeLength;
  UINT32  *CsrActiveConfig;
} OC_BOOT_ARGUMENTS;

/**
  Parse macOS kernel into unified boot arguments structure.

  @param[out]  Arguments  Unified boot arguments structure.
  @param[in]   BootArgs   Kernel boot arguments strucutre.
**/
VOID
OcParseBootArgs (
  OUT OC_BOOT_ARGUMENTS *Arguments,
  IN  VOID              *BootArgs
  );

V
vit9696 已提交
1035 1036 1037
/**
  Check if boot argument is currently passed (via image options or NVRAM).

1038
  @param[in]  LoadedImage    UEFI loaded image protocol instance, optional.
V
vit9696 已提交
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
  @param[in]  GetVariable  Preferred UEFI NVRAM reader, optional.
  @param[in]  Argument        Argument, e.g. -v, slide=, debug=, etc.
  @param[in]  ArgumentLength  Argument length, e.g. L_STR_LEN ("-v").

  @retval TRUE if argument is present.
**/
BOOLEAN
OcCheckArgumentFromEnv (
  IN EFI_LOADED_IMAGE  *LoadedImage  OPTIONAL,
  IN EFI_GET_VARIABLE  GetVariable  OPTIONAL,
  IN CONST CHAR8       *Argument,
  IN CONST UINTN       ArgumentLength
  );

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
/**
  Get argument value from command line.

  @param[in]  CommandLine     Argument command line, e.g. for boot.efi.
  @param[in]  Argument        Argument, e.g. -v, slide=, debug=, etc.
  @param[in]  ArgumentLength  Argument length, e.g. L_STR_LEN ("-v").

  @retval pointer to argument value or NULL.
**/
CONST CHAR8 *
OcGetArgumentFromCmd (
  IN CONST CHAR8  *CommandLine,
  IN CONST CHAR8  *Argument,
  IN CONST UINTN  ArgumentLength
  );

/**
  Remove argument from command line if present.

1072
  @param[in,out]  CommandLine  Argument command line, e.g. for boot.efi.
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
  @param[in]      Argument     Argument, e.g. -v, slide=, debug=, etc.
**/
VOID
OcRemoveArgumentFromCmd (
  IN OUT CHAR8        *CommandLine,
  IN     CONST CHAR8  *Argument
  );

/**
  Append argument to command line without deduplication.

1084 1085
  @param[in,out]  Context         Picker context. NULL, if a privilege escalation is not required.
  @param[in,out]  CommandLine     Argument command line of BOOT_LINE_LENGTH bytes.
1086 1087 1088 1089 1090 1091 1092
  @param[in]      Argument        Argument, e.g. -v, slide=0, debug=0x100, etc.
  @param[in]      ArgumentLength  Argument length, e.g. L_STR_LEN ("-v").

  @retval TRUE on success.
**/
BOOLEAN
OcAppendArgumentToCmd (
1093 1094 1095 1096
  IN OUT OC_PICKER_CONTEXT  *Context OPTIONAL,
  IN OUT CHAR8              *CommandLine,
  IN     CONST CHAR8        *Argument,
  IN     CONST UINTN        ArgumentLength
1097 1098
  );

1099 1100 1101 1102 1103 1104 1105 1106
/**
  Perform NVRAM UEFI variable deletion.
**/
VOID
OcDeleteVariables (
  VOID
  );

1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
/**
  Launch Apple BootPicker.

  @retval error code, should not return. 
**/
EFI_STATUS
OcRunAppleBootPicker (
  VOID
  );

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
/**
  Play audio file for context.

  @param[in]  Context   Picker context.
  @param[in]  File      File to play.
  @param[in]  Fallback  Try to fallback to beeps on failure.

  @retval EFI_SUCCESS on success or when unnecessary.
**/
EFI_STATUS
OcPlayAudioFile (
1128 1129 1130
  IN  OC_PICKER_CONTEXT  *Context,
  IN  UINT32             File,
  IN  BOOLEAN            Fallback
1131 1132 1133 1134 1135
  );

/**
  Generate cycles of beep signals for context with silence afterwards, blocking.

1136
  @param[in] Context        Picker context.
1137 1138 1139 1140 1141 1142 1143 1144
  @param[in] ToneCount      Number of signals to produce.
  @param[in] ToneLength     Signal length in milliseconds.
  @param[in] SilenceLength  Silence length in milliseconds.

  @retval EFI_SUCCESS on success or when unnecessary.
**/
EFI_STATUS
OcPlayAudioBeep (
1145 1146 1147 1148
  IN  OC_PICKER_CONTEXT        *Context,
  IN  UINT32                   ToneCount,
  IN  UINT32                   ToneLength,
  IN  UINT32                   SilenceLength
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  );

/**
  Play audio file for context.

  @param[in]  Context   Picker context.
  @param[in]  Entry     Entry to play.
  @param[in]  Number    Entry index number.

  @retval EFI_SUCCESS on success or when unnecessary.
**/
EFI_STATUS
OcPlayAudioEntry (
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  IN  OC_PICKER_CONTEXT  *Context,
  IN  OC_BOOT_ENTRY      *Entry,
  IN  UINT32             Number
  );

/**
  Toggle VoiceOver support.

  @param[in]  Context   Picker context.
  @param[in]  File      File to play after enabling VoiceOver.
**/
VOID
OcToggleVoiceOver (
  IN  OC_PICKER_CONTEXT  *Context,
  IN  UINT32             File  OPTIONAL
1177 1178
  );

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
/**
  Obtain BootOrder entry list.

  @param[in]   BootVariableGuid  GUID namespace for boot entries.
  @param[in]   WithBootNext      Add BootNext as the first option if available.
  @param[out]  BootOrderCount    Number of entries in boot order.
  @param[out]  Deduplicated      Whether the list was changed during deduplication, optional.
  @param[out]  HasBootNext       Whether the list starts with BootNext, optional

  @retval  boot order entry list allocated from pool or NULL.
**/
UINT16 *
OcGetBootOrder (
  IN  EFI_GUID  *BootVariableGuid,
  IN  BOOLEAN   WithBootNext,
  OUT UINTN     *BootOrderCount,
  OUT BOOLEAN   *Deduplicated  OPTIONAL,
  OUT BOOLEAN   *HasBootNext   OPTIONAL
  );

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
/**
  Register top-most priority boot option.

  @param[in]  OptionName    Option name to create.
  @param[in]  DeviceHandle  Device handle of the file system.
  @param[in]  FilePath      Bootloader path.

  @retval EFI_SUCCESS on success.
**/
EFI_STATUS
OcRegisterBootOption (
  IN CONST CHAR16    *OptionName,
  IN EFI_HANDLE      DeviceHandle,
  IN CONST CHAR16    *FilePath
  );

1215
#endif // OC_BOOT_MANAGEMENT_LIB_H