//
//  FirmwareVolume.c
//  Clover
//
//  Created by Slice on 19.10.16.
//

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

#include <EfiImageFormat.h>
//#include <Framework/FirmwareVolumeImageFormat.h>
#include <Protocol/FirmwareVolume.h>

EFI_HANDLE              mHandle = NULL;

EFI_GUID gAppleCursorImageGuid  = {0x1A10742F, 0xFA80, 0x4B79, {0x9D, 0xA6, 0x35, 0x70, 0x58, 0xCC, 0x39, 0x7B}};
EFI_GUID gCloverImageGuid       = {0x00000000, 0x0000, 0x0000, {0xA6, 0xC9, 0x6C, 0xCC, 0x00, 0x00, 0x00, 0x00}};

UINT8 PointerImage[] = {
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
  0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x08, 0x03, 0x00, 0x00, 0x00, 0x44, 0xa4, 0x8a,
  0xc6, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61,
  0x05, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
  0x00, 0xae, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0xbf, 0xbf, 0xbf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xde, 0xde, 0xde, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xbc, 0xbc, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc4, 0xc4, 0xc4, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xe6, 0xe6, 0xe6, 0x00, 0x00, 0x00, 0xb6, 0xb6, 0xb6, 0xcb, 0xcb, 0xcb, 0x00, 0x00, 0x00,
  0xa5, 0xa5, 0xa5, 0x8a, 0x8a, 0x8a, 0x89, 0x89, 0x89, 0xce, 0xce, 0xce, 0x89, 0x89, 0x89, 0xb5,
  0xb5, 0xb5, 0xa3, 0xa3, 0xa3, 0x8e, 0x8e, 0x8e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x2a,
  0x2a, 0xff, 0xff, 0xff, 0x17, 0xad, 0xa5, 0xaa, 0x00, 0x00, 0x00, 0x37, 0x74, 0x52, 0x4e, 0x53,
  0x00, 0x05, 0x09, 0x04, 0x0a, 0x54, 0x02, 0xfe, 0x01, 0x03, 0x15, 0x48, 0x07, 0x1f, 0x08, 0x19,
  0x38, 0x28, 0x3c, 0x12, 0x5f, 0x14, 0x2b, 0x0e, 0x1d, 0x31, 0x27, 0x06, 0x0b, 0x11, 0x47, 0x96,
  0x1c, 0x13, 0x53, 0x17, 0x0f, 0x16, 0x32, 0x2f, 0x1e, 0xbf, 0x5a, 0x9b, 0x8b, 0x5b, 0xab, 0xde,
  0xe0, 0xd4, 0xdf, 0x9d, 0xad, 0xd9, 0x55, 0xbc, 0x05, 0xe2, 0x02, 0x00, 0x00, 0x00, 0xaa, 0x49,
  0x44, 0x41, 0x54, 0x38, 0xcb, 0xed, 0xd1, 0xc7, 0x16, 0xc2, 0x20, 0x10, 0x05, 0x50, 0x12, 0x94,
  0x21, 0xc4, 0x54, 0x7b, 0x8d, 0xbd, 0xf7, 0x06, 0xfc, 0xff, 0x8f, 0x49, 0xd4, 0x25, 0x99, 0x6c,
  0x5d, 0xf8, 0xb6, 0x73, 0xcf, 0x94, 0x33, 0xc4, 0xa1, 0x8c, 0x13, 0x2c, 0xba, 0xe1, 0x7a, 0xa8,
  0xd0, 0xba, 0x5a, 0x41, 0x85, 0x56, 0x80, 0x0b, 0x2d, 0x4b, 0x84, 0x96, 0x25, 0xc2, 0x00, 0x5c,
  0xe4, 0x00, 0x15, 0x6f, 0x80, 0x89, 0x0f, 0x40, 0xc4, 0x17, 0x18, 0x31, 0x0d, 0x99, 0x0d, 0x80,
  0xa9, 0x99, 0x6a, 0x9e, 0x21, 0xe5, 0x56, 0xa0, 0x2e, 0x4a, 0x9e, 0x8f, 0x07, 0x7f, 0x35, 0x71,
  0xac, 0x40, 0xc1, 0xfd, 0x6a, 0x5a, 0xa4, 0x49, 0x34, 0xb0, 0x2e, 0x01, 0xb0, 0x7d, 0x82, 0x92,
  0x8f, 0xbd, 0x28, 0x78, 0x1a, 0xf8, 0x71, 0x7c, 0x3b, 0x49, 0xb5, 0x59, 0x38, 0xf6, 0x33, 0x5b,
  0xbd, 0x68, 0x9d, 0x9a, 0x16, 0x30, 0xae, 0xd9, 0x41, 0xbd, 0x1d, 0xce, 0x93, 0x1d, 0x40, 0xd6,
  0x29, 0x00, 0xd4, 0x63, 0xfd, 0xd9, 0xd2, 0x1f, 0x75, 0x9b, 0x05, 0x23, 0x38, 0xe1, 0xcc, 0x15,
  0x41, 0x20, 0x5c, 0x46, 0xfe, 0xf9, 0xed, 0xbc, 0x00, 0x6a, 0x83, 0x16, 0x8f, 0xec, 0xab, 0x32,
  0x4c, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
};
/*
UINT8 CloverImage[] = {
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
  0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x01, 0x03, 0x00, 0x00, 0x00, 0x6d, 0xcc, 0x6b,
  0xc4, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc, 0x61,
  0x05, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
  0x00, 0x06, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0x6d, 0x6d, 0x6d, 0x06, 0x38, 0xe9, 0xb3,
  0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, 0xe6, 0xd8, 0x66, 0x00, 0x00, 0x00,
  0x5c, 0x49, 0x44, 0x41, 0x54, 0x18, 0xd3, 0x63, 0x60, 0x80, 0x00, 0xe6, 0xff, 0x40, 0xf0, 0x80,
  0x81, 0x1f, 0x44, 0xfd, 0x60, 0x90, 0x07, 0x51, 0x7f, 0x60, 0x94, 0x3d, 0x88, 0xfa, 0x47, 0x01,
  0xf5, 0xf9, 0x39, 0x98, 0x7a, 0x78, 0x18, 0x4c, 0x3d, 0x68, 0x06, 0x53, 0x07, 0x1a, 0xc1, 0x14,
  0x43, 0x43, 0x3d, 0x90, 0xfa, 0xc7, 0xd0, 0x60, 0x8f, 0xa0, 0xa0, 0x82, 0xff, 0x1b, 0x1a, 0x20,
  0xa6, 0xd4, 0x23, 0x53, 0x50, 0x41, 0x7c, 0xfa, 0xa0, 0x46, 0x43, 0x2d, 0x82, 0x5a, 0x0b, 0x75,
  0x04, 0x45, 0x5e, 0x41, 0x0d, 0x17, 0x68, 0x60, 0x41, 0x83, 0x0e, 0x02, 0x00, 0xcb, 0xf3, 0xd6,
  0xfb, 0xfa, 0x6e, 0x4d, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60,
  0x82,
};
*/

EFI_STATUS
EFIAPI
FvGetVolumeAttributes (
                       IN  EFI_FIRMWARE_VOLUME_PROTOCOL  *This,
                       OUT FRAMEWORK_EFI_FV_ATTRIBUTES   *Attributes
                       )
{
  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
FvSetVolumeAttributes (
                       IN EFI_FIRMWARE_VOLUME_PROTOCOL     *This,
                       IN OUT FRAMEWORK_EFI_FV_ATTRIBUTES  *Attributes
                       )
{
  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
FvReadFile (
            IN EFI_FIRMWARE_VOLUME_PROTOCOL   *This,
            IN EFI_GUID                       *NameGuid,
            IN OUT VOID                       **Buffer,
            IN OUT UINTN                      *BufferSize,
            OUT EFI_FV_FILETYPE               *FoundType,
            OUT EFI_FV_FILE_ATTRIBUTES        *FileAttributes,
            OUT UINT32                        *AuthenticationStatus
            )
{
  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
FvReadSection (
               IN EFI_FIRMWARE_VOLUME_PROTOCOL   *This,
               IN EFI_GUID                       *NameGuid,
               IN EFI_SECTION_TYPE               SectionType,
               IN UINTN                          SectionInstance,
               IN OUT VOID                       **Buffer,
               IN OUT UINTN                      *BufferSize,
               OUT UINT32                        *AuthenticationStatus
               )
{
  EFI_STATUS Status = EFI_SUCCESS;
  
  if (!Buffer || !BufferSize || !AuthenticationStatus) {
    return EFI_INVALID_PARAMETER;
  }
  if(CompareGuid (NameGuid, &gAppleCursorImageGuid)) {
    *BufferSize = sizeof(PointerImage);
    Status = gBS->AllocatePool(EfiBootServicesData, *BufferSize, (VOID **)Buffer);
    if (!EFI_ERROR(Status)) {
      gBS->CopyMem(*Buffer, &PointerImage, *BufferSize);
    }
    *AuthenticationStatus = 0;
    return Status;
  }
  /*
  if(CompareGuid (NameGuid, &gCloverImageGuid)) {
    *BufferSize = sizeof(CloverImage);
    Status = gBS->AllocatePool(EfiBootServicesData, *BufferSize, (VOID **)Buffer);
    if (!EFI_ERROR(Status)) {
      gBS->CopyMem(*Buffer, &CloverImage, *BufferSize);
    }
    *AuthenticationStatus = 0;
    return Status;
  } 
   */
  *AuthenticationStatus = EFI_LOCAL_AUTH_STATUS_TEST_FAILED;
  return EFI_NOT_FOUND;
}

EFI_STATUS
EFIAPI
FvWriteFile (
             IN EFI_FIRMWARE_VOLUME_PROTOCOL      *This,
             IN UINT32                            NumberOfFiles,
             IN FRAMEWORK_EFI_FV_WRITE_POLICY     WritePolicy,
             IN FRAMEWORK_EFI_FV_WRITE_FILE_DATA  *FileData
             )
{
  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
FvGetNextFile (
               IN EFI_FIRMWARE_VOLUME_PROTOCOL   *This,
               IN OUT VOID                       *Key,
               IN OUT EFI_FV_FILETYPE            *FileType,
               OUT EFI_GUID                      *NameGuid,
               OUT EFI_FV_FILE_ATTRIBUTES        *Attributes,
               OUT UINTN                         *Size
               )
{
  return EFI_SUCCESS;
}

EFI_FIRMWARE_VOLUME_PROTOCOL   FirmwareVolume = {
  FvGetVolumeAttributes,
  FvSetVolumeAttributes,
  FvReadFile,
  FvReadSection,
  FvWriteFile,
  FvGetNextFile,
  16,    //KeySize
  NULL  //ParentHandle
};

/****************************************************************
 * Entry point
 ***************************************************************/

/**
 * FirmwareVolume entry point. Installs AppleGraphConfigProtocol.
 */
EFI_STATUS
EFIAPI
FVEntrypoint (IN EFI_HANDLE           ImageHandle,
              IN EFI_SYSTEM_TABLE		*SystemTable
              )
{
  EFI_STATUS					            Status; 
  EFI_BOOT_SERVICES               *gBS;
  EFI_FIRMWARE_VOLUME_PROTOCOL    *ExistingFirmwareVolume;
  gBS				= SystemTable->BootServices;
  
  Status = gBS->LocateProtocol (&gEfiFirmwareVolumeProtocolGuid, NULL, (VOID **)&ExistingFirmwareVolume);

  if (EFI_ERROR (Status)) {
    Status = gBS->InstallMultipleProtocolInterfaces (
                                                     &mHandle,
                                                     &gEfiFirmwareVolumeProtocolGuid,
                                                     &FirmwareVolume,
                                                     NULL
                                                     );
  } else {
    ExistingFirmwareVolume->GetVolumeAttributes = FvGetVolumeAttributes;
    ExistingFirmwareVolume->SetVolumeAttributes = FvSetVolumeAttributes;
    ExistingFirmwareVolume->ReadFile            = FvReadFile;
    ExistingFirmwareVolume->ReadSection         = FvReadSection;
    ExistingFirmwareVolume->WriteFile           = FvWriteFile;
    ExistingFirmwareVolume->GetNextFile         = FvGetNextFile;
  }
  
  return Status;
}

