/** @file
  System Firmware descriptor.

  Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
  Copyright (c) 2017, Linaro, Ltd. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

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

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

#define __BUILD_STRING(x)                   L ## #x
#define BUILD_STRING(x)                     L"build #" __BUILD_STRING(x)
#define CURRENT_FIRMWARE_VERSION            FixedPcdGet32 (PcdFirmwareRevision)
#define CURRENT_FIRMWARE_VERSION_STRING     BUILD_STRING (FixedPcdGet32 (PcdFirmwareRevision))
#define LOWEST_SUPPORTED_FIRMWARE_VERSION   FixedPcdGet32 (PcdLowestSupportedFirmwareVersion)

#define IMAGE_ID                            SIGNATURE_64('S', 'N', 'D', 'E', 'V', 'B', 'O', 'X')
#define IMAGE_ID_STRING                     L"Socionext Developer Box"

// PcdSystemFmpCapsuleImageTypeIdGuid
#define IMAGE_TYPE_ID_GUID                  { 0x50b94ce5, 0x8b63, 0x4849, { 0x8a, 0xf4, 0xea, 0x47, 0x93, 0x56, 0xf0, 0xe3 } }

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;

STATIC 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(PcdFdSize),                              // 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* CONST ReferenceAcpiTable = &mImageDescriptor;
