/** @file
  System Firmware descriptor.

  Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <PiPei.h>
#include <Protocol/FirmwareManagement.h>
#include <Guid/EdkiiSystemFmpCapsule.h>

#define PACKAGE_VERSION                     0xFFFFFFFF
#define PACKAGE_VERSION_STRING              L"Unknown"

#define CURRENT_FIRMWARE_VERSION            0x00000002
#define CURRENT_FIRMWARE_VERSION_STRING     L"0x00000002"
#define LOWEST_SUPPORTED_FIRMWARE_VERSION   0x00000001

#define IMAGE_ID                            SIGNATURE_64('Q', 'U', 'A', 'R', 'K', '_', 'F', 'd')
#define IMAGE_ID_STRING                     L"QuarkPlatformFd"

// PcdSystemFmpCapsuleImageTypeIdGuid
#define IMAGE_TYPE_ID_GUID                  { 0x62af20c0, 0x7016, 0x424a, { 0x9b, 0xf8, 0x9c, 0xcc, 0x86, 0x58, 0x40, 0x90 } }

typedef struct {
  EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR  Descriptor;
  // real string data
  CHAR16                                  ImageIdNameStr[sizeof(IMAGE_ID_STRING)/sizeof(CHAR16)];
  CHAR16                                  VersionNameStr[sizeof(CURRENT_FIRMWARE_VERSION_STRING)/sizeof(CHAR16)];
  CHAR16                                  PackageVersionNameStr[sizeof(PACKAGE_VERSION_STRING)/sizeof(CHAR16)];
} IMAGE_DESCRIPTOR;

IMAGE_DESCRIPTOR mImageDescriptor =
{
  {
    EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR_SIGNATURE,
    sizeof(EDKII_SYSTEM_FIRMWARE_IMAGE_DESCRIPTOR),
    sizeof(IMAGE_DESCRIPTOR),
    PACKAGE_VERSION,                                       // PackageVersion
    OFFSET_OF (IMAGE_DESCRIPTOR, PackageVersionNameStr),   // PackageVersionName
    1,                                                     // ImageIndex;
    {0x0},                                                 // Reserved
    IMAGE_TYPE_ID_GUID,                                    // ImageTypeId;
    IMAGE_ID,                                              // ImageId;
    OFFSET_OF (IMAGE_DESCRIPTOR, ImageIdNameStr),          // ImageIdName;
    CURRENT_FIRMWARE_VERSION,                              // Version;
    OFFSET_OF (IMAGE_DESCRIPTOR, VersionNameStr),          // VersionName;
    {0x0},                                                 // Reserved2
    FixedPcdGet32(PcdFlashAreaSize),                       // Size;
    IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |
      IMAGE_ATTRIBUTE_RESET_REQUIRED |
      IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED |
      IMAGE_ATTRIBUTE_IN_USE,                              // AttributesSupported;
    IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |
      IMAGE_ATTRIBUTE_RESET_REQUIRED |
      IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED |
      IMAGE_ATTRIBUTE_IN_USE,                              // AttributesSetting;
    0x0,                                                   // Compatibilities;
    LOWEST_SUPPORTED_FIRMWARE_VERSION,                     // LowestSupportedImageVersion;
    0x00000000,                                            // LastAttemptVersion;
    0,                                                     // LastAttemptStatus;
    {0x0},                                                 // Reserved3
    0,                                                     // HardwareInstance;
  },
  // real string data
  {IMAGE_ID_STRING},
  {CURRENT_FIRMWARE_VERSION_STRING},
  {PACKAGE_VERSION_STRING},
};


VOID*
ReferenceAcpiTable (
  VOID
  )
{
  //
  // Reference the table being generated to prevent the optimizer from
  // removing the data structure from the executable
  //
  return (VOID*)&mImageDescriptor;
}
