<commit_before>/** @file
  Copyright (C) 2012 - 2014 <NAME>.  All rights reserved.<BR>
  Portions Copyright (C) 2017, CupertinoNet.  All rights reserved.<BR>

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in
  the documentation and/or other materials provided with the
  distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.
**/

#include <AppleMacEfi.h>

#include <Guid/AppleBooterExitNamedEvent.h>
#include <Guid/AppleOSLoaded.h>

#include <Protocol/AppleBooterHandle.h>
#include <Protocol/LoadedImage.h>

#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/EfiBootServicesLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/MiscDevicePathLib.h>
#include <Library/MiscRuntimeLib.h>
#include <Library/PcdLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>

// mStartImage
STATIC EFI_IMAGE_START mStartImage = NULL;

// InternalStartImage
/** Transfers control to a loaded image's entry point.

  @param[in]  ImageHandle   Handle of image to be started.
  @param[out] ExitDataSize  The pointer to the size, in bytes, of ExitData.
  @param[out] ExitData      The pointer to a pointer to a data buffer that
                            includes a Null-terminated string, optionally
                            followed by additional binary data.

  @retval EFI_INVALID_PARAMETER   ImageHandle is either an invalid image handle
                                  or the image has already been initialized
                                  with StartImage.
  @retval EFI_SECURITY_VIOLATION  The current platform policy specifies that
                                  the image should not be started.
  @return  Exit code from image
**/
STATIC
EFI_STATUS
EFIAPI
InternalStartImage (
  IN  EFI_HANDLE  ImageHandle,
  OUT UINTN       *ExitDataSize,
  OUT CHAR16      **ExitData OPTIONAL
  )
{
  EFI_STATUS                Status;

  EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
  CHAR16                    *FilePath;
  UINTN                     FilePathSize;
  UINTN                     Index;
  UINTN                     Result;
  BOOLEAN                   AppleOs;

  ASSERT (ImageHandle != NULL);
  ASSERT ((((ExitDataSize != 0) ? 1 : 0) ^ ((ExitData == NULL) ? 1 : 0)) != 0);
  ASSERT (!EfiAtRuntime ());
  ASSERT (EfiGetCurrentTpl () <= TPL_CALLBACK);
  ASSERT (gBS->StartImage != NULL);

  Status = EfiHandleProtocol (
             ImageHandle,
             &gEfiLoadedImageProtocolGuid,
             (VOID **)&LoadedImage
             );

  if (!EFI_ERROR (Status)) {
    FilePath = MiscFileDevicePathToText (LoadedImage->FilePath, &FilePathSize);

    if (FilePath != NULL) {
      FilePathSize -= sizeof (APPLE_BOOTER_FILE_NAME);
      Index         = (FilePathSize / sizeof (FilePath[0]));
      Result        = CompareMem (
                        (VOID *)&FilePath[Index],
                        (VOID *)APPLE_BOOTER_FILE_NAME,
                        sizeof (APPLE_BOOTER_FILE_NAME)
                        );

      FreePool ((VOID *)FilePath);

      AppleOs = (BOOLEAN)(Result == 0);

      if (AppleOs) {
        EfiInstallMultipleProtocolInterfaces (
          &gImageHandle,
          &gAppleBooterHandleProtocolGuid,
          (VOID *)ImageHandle,
          NULL
          );

        EfiUninstallMultipleProtocolInterfaces (
          gImageHandle,
          &gAppleBooterHandleProtocolGuid,
          (VOID *)ImageHandle,
          NULL
          );

        if (PcdGetBool (PcdSignalAppleOSLoadedEvent)) {
          EfiNamedEventSignal (&gAppleOSLoadedNamedEventGuid);
        }
      }
      
      Status = mStartImage (
                 ImageHandle,
                 ExitDataSize,
                 ExitData
                 );

      if (AppleOs) {
        EfiNamedEventSignal (&gAppleBooterExitNamedEventGuid);
      }
    }
  }

  return Status;
}

// AppleBooterNotifyMain
EFI_STATUS
EFIAPI
AppleBooterNotifyMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_TPL OldTpl;

  DEBUG_CODE (
    ASSERT (mStartImage == NULL);
    );

  OldTpl = EfiRaiseTPL (TPL_HIGH_LEVEL);

  mStartImage     = gBS->StartImage;
  gBS->StartImage = InternalStartImage;

  UPDATE_EFI_TABLE_HEADER_CRC32 (gBS->Hdr);

  EfiRestoreTPL (OldTpl);

  return EFI_SUCCESS;
}

// AppleBooterNotifyUnload
EFI_STATUS
EFIAPI
AppleBooterNotifyUnload (
  IN EFI_HANDLE  ImageHandle
  )
{
  EFI_TPL OldTpl;

  DEBUG_CODE (<commit_msg>AppleBooterNotifyDxe: Update code style.<commit_after>/** @file
  Copyright (C) 2012 - 2014 <NAME>.  All rights reserved.<BR>
  Portions Copyright (C) 2017, CupertinoNet.  All rights reserved.<BR>

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  1. Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in
  the documentation and/or other materials provided with the
  distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.

**/

#include <AppleMacEfi.h>

#include <Guid/AppleBooterExitNamedEvent.h>
#include <Guid/AppleOSLoaded.h>

#include <Protocol/AppleBooterHandle.h>
#include <Protocol/LoadedImage.h>

#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/EfiBootServicesLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/MiscDevicePathLib.h>
#include <Library/PcdLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>

STATIC EFI_IMAGE_START mStartImage = NULL;

/**
  Transfers control to a loaded image's entry point.

  @param[in]  ImageHandle   Handle of image to be started.
  @param[out] ExitDataSize  The pointer to the size, in bytes, of ExitData.
  @param[out] ExitData      The pointer to a pointer to a data buffer that
                            includes a Null-terminated string, optionally
                            followed by additional binary data.

  @retval EFI_INVALID_PARAMETER   ImageHandle is either an invalid image handle
                                  or the image has already been initialized
                                  with StartImage.
  @retval EFI_SECURITY_VIOLATION  The current platform policy specifies that
                                  the image should not be started.
  @return  Exit code from image

**/
STATIC
EFI_STATUS
EFIAPI
InternalStartImage (
  IN  EFI_HANDLE  ImageHandle,
  OUT UINTN       *ExitDataSize,
  OUT CHAR16      **ExitData OPTIONAL
  )
{
  EFI_STATUS                Status;

  EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
  CHAR16                    *FilePath;
  UINTN                     FilePathSize;
  UINTN                     Index;
  UINTN                     Result;
  BOOLEAN                   AppleOs;

  ASSERT (mStartImage != NULL);

  Status = EfiHandleProtocol (
             ImageHandle,
             &gEfiLoadedImageProtocolGuid,
             (VOID **)&LoadedImage
             );

  if (!EFI_ERROR (Status)) {
    FilePath = MiscFileDevicePathToText (LoadedImage->FilePath, &FilePathSize);

    if (FilePath != NULL) {
      FilePathSize -= sizeof (APPLE_BOOTER_FILE_NAME);
      Index         = (FilePathSize / sizeof (FilePath[0]));
      Result        = CompareMem (
                        (VOID *)&FilePath[Index],
                        (VOID *)APPLE_BOOTER_FILE_NAME,
                        sizeof (APPLE_BOOTER_FILE_NAME)
                        );

      FreePool ((VOID *)FilePath);

      AppleOs = (BOOLEAN)(Result == 0);

      if (AppleOs) {
        EfiInstallMultipleProtocolInterfaces (
          &gImageHandle,
          &gAppleBooterHandleProtocolGuid,
          (VOID *)ImageHandle,
          NULL
          );

        EfiUninstallMultipleProtocolInterfaces (
          gImageHandle,
          &gAppleBooterHandleProtocolGuid,
          (VOID *)ImageHandle,
          NULL
          );

        if (PcdGetBool (PcdSignalAppleOSLoadedEvent)) {
          EfiNamedEventSignal (&gAppleOSLoadedNamedEventGuid);
        }
      }
      
      Status = mStartImage (
                 ImageHandle,
                 ExitDataSize,
                 ExitData
                 );

      if (AppleOs) {
        EfiNamedEventSignal (&gAppleBooterExitNamedEventGuid);
      }
    }
  }

  return Status;
}

EFI_STATUS
EFIAPI
AppleBooterNotifyMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_TPL OldTpl;

  DEBUG_CODE (
    ASSERT (mStartImage == NULL);
    );

  OldTpl = EfiRaiseTPL (TPL_HIGH_LEVEL);

  mStartImage     = gBS->StartImage;
  gBS->StartImage = InternalStartImage;

  UPDATE_EFI_TABLE_HEADER_CRC32 (gBS->Hdr);

  EfiRestoreTPL (OldTpl);

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
AppleBooterNotifyUnload (
  IN EFI_HANDLE  ImageHandle
  )
{
  EFI_TPL OldTpl;

  DEBUG_CODE (
{'fraction_non_alphanumeric': 0.0701380524413972, 'fraction_numerical': 0.00463263226165107, 'mean_word_length': 2.5343811394891946, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3864722', 'n_tokens_mistral': 3261, 'n_tokens_neox': 3018, 'n_words': 935}