OcBootManagementLib.h 22.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>
23
#include <Protocol/AppleKeyMapAggregator.h>
24 25
#include <Protocol/LoadedImage.h>

26 27 28 29 30 31 32 33 34
/**
  Operating system boot type.
  WARNING: This is only for debug purposes.
**/
typedef enum OC_BOOT_ENTRY_TYPE_ {
  OcBootUnknown,
  OcBootApple,
  OcBootAppleRecovery,
  OcBootWindows,
35 36
  OcBootCustom,
  OcBootSystem
37 38
} OC_BOOT_ENTRY_TYPE;

39 40 41 42 43 44 45 46 47
/**
  Picker mode.
**/
typedef enum OC_PICKER_MODE_ {
  OcPickerModeBuiltin,
  OcPickerModeExternal,
  OcPickerModeApple,
} OC_PICKER_MODE;

48 49 50 51 52 53 54 55 56
/**
  Action to perform as part of executing a system boot entry.
**/
typedef
EFI_STATUS
(*OC_BOOT_SYSTEM_ACTION)(
  VOID
  );

57 58 59 60 61 62 63
/**
  Discovered boot entry.
  Note, inner resources must be freed with OcResetBootEntry.
**/
typedef struct OC_BOOT_ENTRY_ {
  //
  // Device path to booter or its directory.
64
  // Can be NULL, for example, for custom or system entries.
65 66 67
  //
  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
  //
68 69 70 71
  // Action to perform on execution. Only valid for system entries.
  //
  OC_BOOT_SYSTEM_ACTION     SystemAction;
  //
72 73 74 75 76
  // Obtained human visible name.
  //
  CHAR16                    *Name;
  //
  // Obtained boot path directory.
77
  // For custom entries this contains tool path.
78 79 80
  //
  CHAR16                    *PathName;
  //
81 82
  // Heuristical value signalising about booted os.
  // WARNING: This is only for debug purposes.
83
  //
84
  OC_BOOT_ENTRY_TYPE        Type;
85
  //
86 87 88 89
  // Set when this entry is an externally available entry (e.g. USB).
  //
  BOOLEAN                   IsExternal;
  //
90 91 92 93
  // Should try booting from first dmg found in DevicePath.
  //
  BOOLEAN                   IsFolder;
  //
94 95 96 97
  // Set when this entry is considered auxiliary.
  //
  BOOLEAN                   IsAuxiliary;
  //
98 99 100 101
  // Should make this option default boot option.
  //
  BOOLEAN                   SetDefault;
  //
102
  // Load option data (usually "boot args") size.
103 104
  //
  UINT32                    LoadOptionsSize;
105 106 107
  //
  // Load option data (usually "boot args").
  //
108
  VOID                      *LoadOptions;
109 110 111
} OC_BOOT_ENTRY;

/**
112 113 114
  Perform filtering based on file system basis.
  Ignores all filesystems by default.
  Remove this bit to allow any file system.
115
**/
116 117 118 119 120 121 122 123 124
#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

125 126 127 128 129 130 131
/**
  Perform filtering based on booter origin.
  Ignores all blessed options not on the same partition.
  Remove this bit to allow foreign booters.
**/
#define OC_SCAN_SELF_TRUST_LOCK          BIT2

132 133 134 135 136
/**
  Allow scanning APFS filesystems.
**/
#define OC_SCAN_ALLOW_FS_APFS            BIT8

137 138 139 140 141
/**
  Allow scanning HFS filesystems.
**/
#define OC_SCAN_ALLOW_FS_HFS             BIT9

142 143 144 145 146
/**
  Allow scanning ESP filesystems.
**/
#define OC_SCAN_ALLOW_FS_ESP             BIT10

147 148 149 150 151 152 153 154 155 156
/**
  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

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/**
  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

197 198 199 200 201 202 203 204 205 206 207 208 209
/**
  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 ( \
210 211
  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)
212

213 214 215
/**
  By default allow booting from APFS from internal drives.
**/
216
#define OC_SCAN_DEFAULT_POLICY ( \
217
  OC_SCAN_FILE_SYSTEM_LOCK   | OC_SCAN_DEVICE_LOCK | \
218 219 220
  OC_SCAN_SELF_TRUST_LOCK    | OC_SCAN_ALLOW_FS_APFS | \
  OC_SCAN_ALLOW_DEVICE_SATA  | OC_SCAN_ALLOW_DEVICE_SASEX | \
  OC_SCAN_ALLOW_DEVICE_SCSI  | OC_SCAN_ALLOW_DEVICE_NVME)
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/**
  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
  );

296 297 298 299 300 301 302 303 304 305
/**
  Exposed custom entry load interface.
  Must return allocated file buffer from pool.
**/
typedef
EFI_STATUS
(EFIAPI *OC_CUSTOM_READ) (
  IN  VOID                        *Context,
  IN  OC_BOOT_ENTRY               *ChosenEntry,
  OUT VOID                        **Data,
306
  OUT UINT32                      *DataSize,
307 308 309
  OUT EFI_DEVICE_PATH_PROTOCOL    **DevicePath         OPTIONAL,
  OUT EFI_HANDLE                  *ParentDeviceHandle  OPTIONAL,
  OUT EFI_DEVICE_PATH_PROTOCOL    **ParentFilePath     OPTIONAL
310 311
  );

312
/**
313
  Custom picker entry.
314 315
**/
typedef struct {
316 317 318 319 320 321 322 323
  //
  // Entry name.
  //
  CONST CHAR8  *Name;
  //
  // Entry path.
  //
  CONST CHAR8  *Path;
324 325 326 327
  //
  // Entry boot arguments.
  //
  CONST CHAR8  *Arguments;
328 329 330 331
  //
  // Whether this entry is auxiliary.
  //
  BOOLEAN      Auxiliary;
332 333
} OC_PICKER_ENTRY;

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
/**
  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)(
  IN VOID                *Context,
  IN OC_PRIVILEGE_LEVEL  Level
  );

352 353 354 355 356 357 358 359 360 361 362
/**
  Picker behaviour action.
**/
typedef enum {
  OcPickerDefault           = 0,
  OcPickerShowPicker        = 1,
  OcPickerResetNvram        = 2,
  OcPickerBootApple         = 3,
  OcPickerBootAppleRecovery = 4,
} OC_PICKER_CMD;

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
/**
  Boot picker context describing picker behaviour.
**/
typedef struct {
  //
  // Scan policy (e.g. OC_SCAN_DEFAULT_POLICY).
  //
  UINT32           ScanPolicy;
  //
  // Load policy (e.g. OC_LOAD_DEFAULT_POLICY).
  //
  UINT32           LoadPolicy;
  //
  // Default entry selection timeout (pass 0 to ignore).
  //
  UINT32           TimeoutSeconds;
  //
380 381 382 383
  // Default delay prior to handling hotkeys (pass 0 to ignore).
  //
  UINT32           TakeoffDelay;
  //
384 385
  // Define picker behaviour.
  // For example, show boot menu or just boot the default option.
386
  //
387
  OC_PICKER_CMD    PickerCommand;
388 389 390 391 392
  //
  // Use custom (gOcVendorVariableGuid) for Boot#### variables.
  //
  BOOLEAN          CustomBootGuid;
  //
393 394 395 396 397 398 399 400
  // Custom entry reading routine, optional for no custom entries.
  //
  OC_CUSTOM_READ   CustomRead;
  //
  // Context to pass to CustomRead, optional.
  //
  VOID             *CustomEntryContext;
  //
401 402 403 404 405 406 407 408
  // Image starting routine used, required.
  //
  OC_IMAGE_START   StartImage;
  //
  // Handle to exclude scanning from, optional.
  //
  EFI_HANDLE       ExcludeHandle;
  //
409 410 411 412 413 414 415 416
  // Privilege escalation requesting routine.
  //
  OC_REQ_PRIVILEGE RequestPrivilege;
  //
  // Context to pass to RequestPrivilege, optional.
  //
  VOID             *PrivilegeContext;
  //
417 418 419 420
  // Additional suffix to include by the interface.
  //
  CONST CHAR8      *TitleSuffix;
  //
421 422 423 424
  // Used picker mode.
  //
  OC_PICKER_MODE   PickerMode;
  //
425 426 427 428
  // Console attributes. 0 is reserved as disabled.
  //
  UINT32           ConsoleAttributes;
  //
429 430 431 432
  // Enable polling boot arguments.
  //
  BOOLEAN          PollAppleHotKeys;
  //
433 434 435 436
  // Append the "Reset NVRAM" option to the boot entry list.
  //
  BOOLEAN          ShowNvramReset;
  //
437 438 439 440
  // Allow setting default boot option from boot menu.
  //
  BOOLEAN          AllowSetDefault;
  //
441 442 443 444
  // Hide and do not scan auxiliary entries.
  //
  BOOLEAN          HideAuxiliary;
  //
445 446 447 448
  // Additional boot arguments for Apple loaders.
  //
  CHAR8            AppleBootArgs[BOOT_LINE_LENGTH];
  //
449 450 451 452 453 454
  // Number of custom boot paths (bless override).
  //
  UINTN            NumCustomBootPaths;
  //
  // Custom boot paths (bless override).  Must start with '\'.
  //
455
  CHAR16           **CustomBootPaths;
456
  //
457
  // Number of absolute custom entries.
458
  //
459
  UINT32           AbsoluteEntryCount;
460
  //
461 462 463 464 465
  // Number of total custom entries (absolute and tools).
  //
  UINT32           AllCustomEntryCount;
  //
  // Custom picker entries.  Absolute entries come first.
466 467 468 469
  //
  OC_PICKER_ENTRY  CustomEntries[];
} OC_PICKER_CONTEXT;

470 471 472 473 474 475 476
/**
  Hibernate detection bit mask for hibernate source usage.
**/
#define HIBERNATE_MODE_NONE   0U
#define HIBERNATE_MODE_RTC    1U
#define HIBERNATE_MODE_NVRAM  2U

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
/**
  Describe boot entry contents by setting fields other than DevicePath.

  @param[in]  BootPolicy     Apple Boot Policy Protocol.
  @param[in]  BootEntry      Located boot entry.

  @retval EFI_SUCCESS          The entry point is described successfully.
**/
EFI_STATUS
OcDescribeBootEntry (
  IN     APPLE_BOOT_POLICY_PROTOCOL *BootPolicy,
  IN OUT OC_BOOT_ENTRY              *BootEntry
  );

/**
  Release boot entry contents allocated from pool.

  @param[in,out]  BootEntry      Located boot entry.
**/
VOID
OcResetBootEntry (
  IN OUT OC_BOOT_ENTRY              *BootEntry
  );

/**
  Release boot entries.

  @param[in,out]  BootEntry      Located boot entry array from pool.
  @param[in]      Count          Boot entry count.
**/
VOID
OcFreeBootEntries (
  IN OUT OC_BOOT_ENTRY              *BootEntries,
  IN     UINTN                      Count
  );

513 514 515 516
/**
  Scan system for boot entries.

  @param[in]  BootPolicy     Apple Boot Policy Protocol.
517
  @param[in]  Context        Picker context.
518 519 520
  @param[out] BootEntries    List of boot entries (allocated from pool).
  @param[out] Count          Number of boot entries.
  @param[out] AllocCount     Number of allocated boot entries.
V
vit9696 已提交
521
  @param[in]  LoadHandle     Load handle to skip.
522 523 524 525 526 527 528
  @param[in]  Describe       Automatically fill description fields

  @retval EFI_SUCCESS        Executed successfully and found entries.
**/
EFI_STATUS
OcScanForBootEntries (
  IN  APPLE_BOOT_POLICY_PROTOCOL  *BootPolicy,
529
  IN  OC_PICKER_CONTEXT           *Context,
530 531 532 533 534 535
  OUT OC_BOOT_ENTRY               **BootEntries,
  OUT UINTN                       *Count,
  OUT UINTN                       *AllocCount OPTIONAL,
  IN  BOOLEAN                     Describe
  );

536
/**
537
  Obtain default entry from picker context.
538

539
  @param[in]      Context          Picker context.
540 541 542
  @param[in,out]  BootEntries      Described list of entries, may get updated.
  @param[in]      NumBootEntries   Positive number of boot entries.

543
  @retval  boot entry or 0.
544
**/
545
UINT32
546
OcGetDefaultBootEntry (
547 548 549
  IN     OC_PICKER_CONTEXT  *Context,
  IN OUT OC_BOOT_ENTRY      *BootEntries,
  IN     UINTN              NumBootEntries
550 551
  );

552 553 554 555 556 557 558 559 560 561 562 563 564 565
/**
  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
  );

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
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.

  @param[in] Context  Privilege context.
  @param[in] Level    The privilege level to request escalating to.

  @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 (
  IN VOID                *Context,
  IN OC_PRIVILEGE_LEVEL  Level
  );

591 592 593
/**
  Show simple boot entry selection menu and return chosen entry.

594
  @param[in]  Context          Picker context.
595 596 597 598 599 600 601 602 603 604
  @param[in]  BootEntries      Described list of entries.
  @param[in]  Count            Positive number of boot entries.
  @param[in]  DefaultEntry     Default boot entry (DefaultEntry < Count).
  @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
OcShowSimpleBootMenu (
605
  IN  OC_PICKER_CONTEXT           *Context,
606 607 608 609 610 611 612
  IN  OC_BOOT_ENTRY               *BootEntries,
  IN  UINTN                       Count,
  IN  UINTN                       DefaultEntry,
  OUT OC_BOOT_ENTRY               **ChosenBootEntry
  );

/**
613
  Load & start boot entry loader image with given options.
614

D
Download-Fritz 已提交
615
  @param[in]  BootPolicy     Apple Boot Policy Protocol.
616
  @param[in]  Context        Picker context.
617 618 619
  @param[in]  BootEntry      Located boot entry.
  @param[in]  ParentHandle   Parent image handle.

620
  @retval EFI_SUCCESS        The image was found, started, and ended succesfully.
621 622 623
**/
EFI_STATUS
OcLoadBootEntry (
D
Download-Fritz 已提交
624
  IN  APPLE_BOOT_POLICY_PROTOCOL  *BootPolicy,
625
  IN  OC_PICKER_CONTEXT           *Context,
626
  IN  OC_BOOT_ENTRY               *BootEntry,
627
  IN  EFI_HANDLE                  ParentHandle
628 629
  );

630 631 632 633 634 635 636 637
/**
  Handle hibernation detection for later loading.

  @param[in]  HibernateMask  Hibernate detection mask.

  @retval EFI_SUCCESS        Hibernation mode was found and activated.
**/
EFI_STATUS
638
OcActivateHibernateWake (
639 640 641
  IN UINT32                       HibernateMask
  );

V
vit9696 已提交
642 643 644 645 646 647 648 649 650 651
/**
  Check if active hibernation is happening.

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

652 653 654 655 656 657
/**
  Check pressed hotkeys and update booter context based on this.

  @param[in,out]  Context       Picker context.
**/
VOID
658
OcLoadPickerHotKeys (
659 660 661
  IN OUT OC_PICKER_CONTEXT  *Context
  );

662 663 664 665 666
/**
  Default index mapping macros.
**/
#define OC_INPUT_STR      "123456789ABCDEFGHIJKLMNOPQRSTUVXWZ"
#define OC_INPUT_MAX      L_STR_LEN (OC_INPUT_STR)
667
#define OC_INPUT_ABORTED  -1  ///< Esc or 0
668 669 670 671 672 673 674 675 676
#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
677
#define OC_INPUT_MORE     -11 ///< Show more entries (press space)
678

679 680 681
/**
  Obtains key index from user input.

682
  @param[in,out]  Context      Picker context.
683
  @param[in]      KeyMap       Apple Key Map Aggregator protocol.
684 685
  @param[in]      Time         Timeout to wait for.
  @param[in]      PollHotkeys  Poll key combinations.
686
  @param[out]     SetDefault   Set boot option as default, optional.
687

688
  @returns key index [0, OC_INPUT_MAX) or OC_INPUT_* value.
689 690 691
**/
INTN
OcWaitForAppleKeyIndex (
692 693 694 695 696
  IN OUT OC_PICKER_CONTEXT                  *Context,
  IN     APPLE_KEY_MAP_AGGREGATOR_PROTOCOL  *KeyMap,
  IN     UINTN                              Timeout,
  IN     BOOLEAN                            PollHotkeys,
     OUT BOOLEAN                            *SetDefault  OPTIONAL
697 698
  );

699 700 701
/**
  Install missing boot policy, scan, and show simple boot menu.

702
  @param[in]  Context       Picker context.
703 704 705 706

  @retval does not return unless a fatal error happened.
**/
EFI_STATUS
707
OcRunSimpleBootPicker (
708
  IN  OC_PICKER_CONTEXT  *Context
709 710
  );

711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
/**
  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
  );

737 738 739 740 741 742 743 744 745 746 747 748
/**
  Check if supplied device path contains Apple bootloader.

  @param[in]  DevicePath        Device path.

  @retval TRUE for potentially Apple images.
**/
BOOLEAN
OcIsAppleBootDevicePath (
  IN EFI_DEVICE_PATH_PROTOCOL  *DevicePath
  );

749 750 751 752 753 754 755 756 757 758 759 760
/**
  Get loaded image protocol for Apple bootloader.

  @param[in]  Handle        Image handle.

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

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
/**
  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 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
/**
  Check if boot argument is currently passed (via image options or NVRAM).

  @param[in]  LoadImage    UEFI loaded image protocol instance, optional.
  @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
  );

807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
/**
  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.

  @param[in, out] CommandLine  Argument command line, e.g. for boot.efi.
  @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.

838
  @param[in, out] Context         Picker context. NULL, if a privilege escalation is not required.
839 840 841 842 843 844 845 846
  @param[in, out] CommandLine     Argument command line of BOOT_LINE_LENGTH bytes.
  @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 (
847 848 849 850
  IN OUT OC_PICKER_CONTEXT  *Context OPTIONAL,
  IN OUT CHAR8              *CommandLine,
  IN     CONST CHAR8        *Argument,
  IN     CONST UINTN        ArgumentLength
851 852
  );

853 854 855 856 857 858 859 860
/**
  Perform NVRAM UEFI variable deletion.
**/
VOID
OcDeleteVariables (
  VOID
  );

861 862 863 864 865 866 867 868 869 870 871

/**
  Launch Apple BootPicker.

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

872
#endif // OC_BOOT_MANAGEMENT_LIB_H