Optimize the code to not call StrToBuf when DataLength is 0.

Signed-off-by: Ruiyu Ni <ruyu.ni@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15143 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg: Carve 128KB out of MAINFV in MEMFD

In an effort to consolidate fixed memory used by OVMF,
we'll move 2 SEC/PEI phase RAM users to 8MB.
* X64 page tables (24KB)   0x80000 => 0x800000
* PEI temporary RAM (64KB) 0x70000 => 0x810000

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15144 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg/Sec/SecMain.c: Convert to CRLF (dos) text

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15145 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg X64 ResetVector: Move page tables from 512KB to 8MB

To help consolidate OVMF fixed memory uses, we declare this
range in MEMFD and thereby move it to 8MB.

We also now declare the table range in the FDF to set
PCDs. This allows us to ASSERT that CR3 is set as expected
in OVMF SEC.

OvmfPkgIa32.fdf and OvmfPkgIa32X64.fdf are updated simply
for consistency.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15146 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg: Move SEC/PEI Temporary RAM from 0x70000 to 0x810000

Note: The Temporary RAM memory size is being reduced from
      64KB to 32KB. This still appears to be more than
      adequate for OVMF's early PEI phase. We will be adding
      another 32KB range of RAM just above this range for
      use on S3 resume.

The range is declared as part of MEMFD, so it is easier
to identify the memory range.

We also now assign PCDs to the memory range.

The PCDs are used to set the initial SEC/PEI stack in
SEC's assembly code.

The PCDs are also used in the SEC C code to setup
the Temporary RAM PPI.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15147 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg/Sec: Cleanup debug messages

Remove some not-so-useful messages (during FV scanning).

Convert ERROR to INFO and vise versa where appropriate.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15148 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg/Sec: Remove EFIAPI from functions that don't require it

These are all internal functions that don't interface with
assembly code or other drivers. Therefore EFIAPI is not
required.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15149 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg/Sec: Add FindFfsSectionInstance

This allow you to search for an 'instance' of a section
within a series of FFS sections.

For example, we will split the MAINFV into a PEI and DXE
FV, and then compress those two FV's together within a
FFS FV file. The DXE FV will appear as the second section
of the file, and therefore we will search for it using
an Instance=1 value.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15150 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg: Split MAINFV into a separate PEI and DXE FVs

By splitting the PEI and DXE phases into separate FVs,
we can only reserve the PEI FV for ACPI S3 support.
This should save about 7MB.

Unfortunately, this all has to happen in a single commit.

DEC:
* Remove PcdOvmfMemFv(Base|Size)
* Add PcdOvmfPeiMemFv(Base|Size)
* Add PcdOvmfDxeMemFv(Base|Size)

FDF:
* Add new PEIFV. Move PEI modules here.
* Remove MAINFV
* Add PEIFV and DXEFV into FVMAIN_COMPACT
   - They are added as 2 sections of a file, and compressed
     together so they should retain good compression
* PcdOvmf(Pei|Dxe)MemFv(Base|Size) are set

SEC:
* Find both the PEI and DXE FVs after decompression.
   - Copy them separately to their memory locations.

Platform PEI driver:
* Fv.c: Publish both FVs as appropriate
* MemDetect.c: PcdOvmfMemFv(Base|Size) =>
                PcdOvmfDxeMemFv(Base|Size)

OVMF.fd before:

  Non-volatile data storage
  FVMAIN_COMPACT                    uncompressed
    FV FFS file                     LZMA compressed
      MAINFV                        uncompressed
        individual PEI modules      uncompressed
        FV FFS file                 compressed with PI_NONE
          DXEFV                     uncompressed
            individual DXE modules  uncompressed
  SECFV                             uncompressed

OVMF.fd after:

  Non-volatile data storage
  FVMAIN_COMPACT                    uncompressed
    FV FFS file                     LZMA compressed
      PEIFV                         uncompressed
        individual PEI modules      uncompressed
      DXEFV                         uncompressed
        individual DXE modules      uncompressed
  SECFV                             uncompressed

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15151 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg/PlatformPei: Move BootMode detection before mem-init

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg: PlatformPei: detect S3 Resume in CMOS and set boot mode accordingly

Data is transferred between S3 Suspend and S3 Resume as follows:

S3 Suspend (DXE):

(1) BdsLibBootViaBootOption()
      EFI_ACPI_S3_SAVE_PROTOCOL [AcpiS3SaveDxe]
      - saves ACPI S3 Context to LockBox  ---------------------+
        (including FACS address -- FACS ACPI table             |
        contains OS waking vector)                             |
                                                               |
      - prepares boot script:                                  |
        EFI_S3_SAVE_STATE_PROTOCOL.Write() [S3SaveStateDxe]    |
          S3BootScriptLib [PiDxeS3BootScriptLib]               |
          - opcodes & arguments are saved in NVS.  --+         |
                                                     |         |
      - issues a notification by installing          |         |
        EFI_DXE_SMM_READY_TO_LOCK_PROTOCOL           |         |
                                                     |         |
(2) EFI_S3_SAVE_STATE_PROTOCOL [S3SaveStateDxe]      |         |
      S3BootScriptLib [PiDxeS3BootScriptLib]         |         |
      - closes script with special opcode  <---------+         |
      - script is available in non-volatile memory             |
        via PcdS3BootScriptTablePrivateDataPtr  --+            |
                                                  |            |
    BootScriptExecutorDxe                         |            |
      S3BootScriptLib [PiDxeS3BootScriptLib]      |            |
      - Knows about boot script location by  <----+            |
        synchronizing with the other library                   |
        instance via                                           |
        PcdS3BootScriptTablePrivateDataPtr.                    |
      - Copies relocated image of itself to                    |
        reserved memory. --------------------------------+     |
      - Saved image contains pointer to boot script.  ---|--+  |
                                                         |  |  |
Runtime:                                                 |  |  |
                                                         |  |  |
(3) OS is booted, writes OS waking vector to FACS,       |  |  |
    suspends machine                                     |  |  |
                                                         |  |  |
S3 Resume (PEI):                                         |  |  |
                                                         |  |  |
(4) PlatformPei sets S3 Boot Mode based on CMOS          |  |  |
                                                         |  |  |
(5) DXE core is skipped and EFI_PEI_S3_RESUME2 is        |  |  |
    called as last step of PEI                           |  |  |
                                                         |  |  |
(6) S3Resume2Pei retrieves from LockBox:                 |  |  |
    - ACPI S3 Context (path to FACS)  <------------------|--|--+
                                      |                  |  |
                                      +------------------|--|--+
    - Boot Script Executor Image  <----------------------+  |  |
                                                            |  |
(7) BootScriptExecutorDxe                                   |  |
      S3BootScriptLib [PiDxeS3BootScriptLib]                |  |
      - executes boot script  <-----------------------------+  |
                                                               |
(8) OS waking vector available from ACPI S3 Context / FACS  <--+
    is called

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
[jordan.l.justen@intel.com: move code into BootModeInitialization]
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg/PlatformPei: Add mBootMode driver variable

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg/PlatformPei: Call PublishPeiMemory in one place

The Xen and QEMU/KVM paths were calling this at nearly
the same time in the boot flow anyhow, so just make
the call in one spot.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg: Add section of memory to use for PEI on S3 resume

This 32k section of RAM will be declared to the PEI Core on
S3 resume to allow memory allocations during S3 resume PEI.

If the boot mode is BOOT_ON_S3_RESUME, then we publish
the pre-reserved PcdS3AcpiReservedMemory range to PEI.

If the boot mode is not BOOT_ON_S3_RESUME, then we reserve
this range as ACPI NVS so the OS will not use it.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg/PlatformPei: Skip various items for S3 resume

We will not be running DXE on S3 resume, so we don't
need to do these initialization items:
 * Reserve EMU Variable memory range
 * Declare Firmware volumes
 * Add memory HOBs
 * MiscInitialization:
   - Disable A20 Mask
   - Build CPU hob
   - Set Power Management Base Address register

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg/Sec: Don't decompress the FV on S3 resume

Since we marked the FV at PcdOvmfPeiMemFvBase as ACPI NVS memory,
we can use it on S3 resume.

The FV at PcdOvmfDxeMemFvBase may have been overwritten by the OS,
but we do not use it's contents on S3 resume.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg: PlatformPei: reserve SEC/PEI temp RAM for S3 resume

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
[jordan.l.justen@intel.com: move to MemDetect.c; use PCDs]
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg: PlatformPei: reserve early page tables on X64

On X64, the reset vector code in
"OvmfPkg/ResetVector/Ia32/PageTables64.asm" identity maps the first 4GB of
RAM for PEI, consuming six frames starting at 8MB.

This range is declared by the PcdOvmfSecPageTablesBase/Size PCDs.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
[jordan.l.justen@intel.com: Move to MemDetect.c; use PCDs]
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg: S3 Resume: pull in PEIM orchestrating S3 Resume

"UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf" produces the
EFI_PEI_S3_RESUME2 PEIM-to-PEIM Interface.

When the platform-specific initialization code (in PEI) sets the Boot Mode
to BOOT_ON_S3_RESUME, the DXE IPL (which is the last step in PEI) skips
the DXE phase entirely, and executes the S3 Resume PEIM through the
EFI_PEI_S3_RESUME2 interface instead. (See DxeLoadCore() in
"MdeModulePkg/Core/DxeIplPeim/DxeLoad.c".)

S3Resume2Pei depends on LockBoxLib.

  EFI_PEI_S3_RESUME2 [S3Resume2Pei]
    LockBoxLib [OvmfPkg/Library/LockBoxLib]

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg: implement LockBoxLib

The S3 suspend/resume infrastructure depends on the LockBox library class.
The edk2 tree currently contains Null and SMM instances. The Null instance
is useless, and the SMM instance would require SMM emulation by including
the SMM core and adding several new drivers, which is deemed too complex.

Hence add a simple LockBoxLib instance for OVMF.

jordan.l.justen@intel.com:
 * use PCDs instead of EmuNvramLib
   - clear memory in PlatformPei on non S3 boots
 * allocate NVS memory and store a pointer to that memory
   - reduces memory use at fixed locations

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>