Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Include/Acidanthera/Library/OcAppleFirmwarePasswordLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
/**
Install and initialise Apple Firmware Password protocol.

@param[in] Reinstall Overwrite installed protocol.

@retval installed or located protocol or NULL.
**/
APPLE_FIRMWARE_PASSWORD_PROTOCOL *
OcAppleFirmwarePasswordInstallProtocol (
VOID
IN BOOLEAN Reinstall
);

#endif // OC_APPLE_FIRMWARE_PASSWORD_LIB_H
16 changes: 10 additions & 6 deletions Include/Apple/Protocol/AppleFirmwarePassword.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ typedef struct {
///
UINT8 Mode;
///
/// Always 0 and changes only if unknown protocol is present in system.
/// The producer of this unknown protocol has not been found, its only
/// consumer is FirmwarePasswordDxe.
/// 0x8 if the unknown protocol (2C7B7BEE-1B43-4485-AB0D-963BF193CFE1) was
/// found and its request failed or reported bit 0, otherwise 0.
///
UINT8 Flags;
///
/// Zeroed by the original implementation.
///
UINT8 Reserved[6];
} APPLE_FIRMWARE_PASSWORD_MODE_INFO;

/**
Expand All @@ -67,8 +70,9 @@ EFI_STATUS
@param[in] Password Password to check.

@retval EFI_ACCESS_DENIED Entered password is incorrect.
@retval EFI_INVALID_PARAMETER This is NULL or has a wrong signature.
@retval EFI_NOT_FOUND There is no password set in the system.
@retval EFI_INVALID_PARAMETER This or Password is NULL or This has a wrong signature.
@retval EFI_NOT_FOUND There is no password set in the system or the unknown protocol
request failed or reported bit 0.
@retval EFI_SUCCESS Entered password is correct.
**/
typedef
Expand All @@ -84,7 +88,7 @@ EFI_STATUS
@param[in] This A pointer to the protocol instance.
@param[out] Info Pointer to APPLE_FIRMWARE_PASSWORD_MODE_INFO.

@retval EFI_INVALID_PARAMETER This is NULL or has a wrong signature.
@retval EFI_INVALID_PARAMETER This or Info is NULL or This has a wrong signature.
@retval EFI_SUCCESS Info was populated.
**/
typedef
Expand Down
63 changes: 51 additions & 12 deletions Library/OcAppleFirmwarePasswordLib/OcAppleFirmwarePasswordLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
**/

#include <Uefi.h>

#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>

#include <Library/OcAppleFirmwarePasswordLib.h>
#include <Library/OcMiscLib.h>

STATIC
EFI_STATUS
Expand All @@ -17,7 +22,11 @@ AppleFirmwarePasswordIsSet (
IN APPLE_FIRMWARE_PASSWORD_PROTOCOL *This
)
{
return EFI_UNSUPPORTED;
if ((This == NULL) || (This->Signature != APPLE_FIRMWARE_PASSWORD_PROTOCOL_SIGNATURE)) {
return EFI_INVALID_PARAMETER;
}

return EFI_NOT_FOUND;
}

STATIC
Expand All @@ -28,7 +37,11 @@ AppleFirmwarePasswordCheck (
IN CONST CHAR16 *Password
)
{
return EFI_UNSUPPORTED;
if ((This == NULL) || (Password == NULL) || (This->Signature != APPLE_FIRMWARE_PASSWORD_PROTOCOL_SIGNATURE)) {
return EFI_INVALID_PARAMETER;
}

return EFI_NOT_FOUND;
}

STATIC
Expand All @@ -39,7 +52,13 @@ AppleFirmwarePasswordGetMode (
OUT APPLE_FIRMWARE_PASSWORD_MODE_INFO *Info
)
{
return EFI_UNSUPPORTED;
if ((This == NULL) || (Info == NULL) || (This->Signature != APPLE_FIRMWARE_PASSWORD_PROTOCOL_SIGNATURE)) {
return EFI_INVALID_PARAMETER;
}

ZeroMem (Info, sizeof (*Info));

return EFI_SUCCESS;
}

STATIC APPLE_FIRMWARE_PASSWORD_PROTOCOL mAppleFirmwarePasswordProtocol = {
Expand All @@ -52,22 +71,41 @@ STATIC APPLE_FIRMWARE_PASSWORD_PROTOCOL mAppleFirmwarePasswordProtocol = {

APPLE_FIRMWARE_PASSWORD_PROTOCOL *
OcAppleFirmwarePasswordInstallProtocol (
VOID
IN BOOLEAN Reinstall
)
{
EFI_STATUS Status;
APPLE_FIRMWARE_PASSWORD_PROTOCOL *FwPass;
UINTN Index;
UINTN HandleCount;
EFI_HANDLE *HandleBuffer;
EFI_HANDLE NewHandle;

FwPass = NULL;
FwPass = NULL;
HandleBuffer = NULL;
HandleCount = 0;
Index = 0;

Status = gBS->LocateProtocol (
&gAppleFirmwarePasswordProtocolGuid,
NULL,
(VOID **)&FwPass
);
if (!EFI_ERROR (Status)) {
return FwPass;
if (Reinstall) {
Status = OcUninstallAllProtocolInstances (&gAppleFirmwarePasswordProtocolGuid);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "OCAFWP: Uninstall failed - %r\n", Status));
return NULL;
}
} else {
Status = gBS->LocateHandleBuffer (ByProtocol, &gAppleFirmwarePasswordProtocolGuid, NULL, &HandleCount, &HandleBuffer);
if (!EFI_ERROR (Status) && (HandleCount != 0) && (HandleBuffer != NULL)) {
for (Index = 0; Index < HandleCount; Index++) {
FwPass = NULL;
Status = OcHandleProtocolFallback (HandleBuffer[Index], &gAppleFirmwarePasswordProtocolGuid, (VOID **)&FwPass);
if (!EFI_ERROR (Status) && (FwPass != NULL) && (FwPass->Signature == APPLE_FIRMWARE_PASSWORD_PROTOCOL_SIGNATURE)) {
FreePool (HandleBuffer);
return FwPass;
}
}

FreePool (HandleBuffer);
}
Comment on lines +96 to +108

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apple does it like this:

Status = gBS->LocateHandleBuffer(
             SearchType: ByProtocol,
             Protocol: &APPLE_FIRMWARE_PASSWORD_PROTOCOL_GUID,
             SearchKey: nullptr,
             NoHandles,
             &Buffer);
  if ( (Status & 0x8000000000000000uLL) == 0LL )
  {
    if ( NoHandles[0] != 0 )
    {
      v4 = 0;
      while ( 1 )
      {
        Status = gBS->HandleProtocol(
                   Handle: Buffer[v4],
                   Protocol: &APPLE_FIRMWARE_PASSWORD_PROTOCOL_GUID,
                   Interface: (void **)&AppleFirmwarePasswordProtocol);
        if ( (Status & 0x8000000000000000uLL) == 0LL && AppleFirmwarePasswordProtocol->Signature == 0x4457535341505746LL )
          break;
        if ( ++v4 >= NoHandles[0] )
          goto LABEL_9;
      }

}

NewHandle = NULL;
Expand All @@ -77,6 +115,7 @@ OcAppleFirmwarePasswordInstallProtocol (
&mAppleFirmwarePasswordProtocol,
NULL
);

if (EFI_ERROR (Status)) {
return NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@
gAppleFirmwarePasswordProtocolGuid ## SOMETIMES_PRODUCES

[LibraryClasses]
DebugLib
BaseMemoryLib
MemoryAllocationLib
UefiBootServicesTableLib
OcMiscLib
Loading