OvmfPkg/build.sh: Support GCC48 toolchain

If GCC 4.8 or 4.9 is detected, then use the GCC48 toolchain.

Previously we would use the GCC47 toolchain, but GCC48
was recently added to the main edk2 BaseTools/Conf.

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@15141 6f19259b-4bc3-4df7-8a09-765794883524
OvmfPkg: unify PcdMaxVariableSize at 0x2000 bytes

The Linux persistent store (pstore) feature serves, among other things,
for saving the trailing portion of the dmesg in case of a kernel oops. One
backend for the pstore facility is "efivars", ie. non-volatile UEFI
variables.

Linux splits the tail of the dmesg that is to be dumped in 1KB chunks, and
tries to save each chunk as a specially (and differently) named
non-volatile variable. The 1KB chunk size accounts for the variable data
only; Linux expects this size to be available per variable *without*
accounting for the variable name or any firmware-internal overhead.

For non-authenticated (ie. non-secure-boot) variables, OvmfPkg currently
sets the per-variable limit to 0x400 (1KB) through PcdMaxVariableSize.
However this PCD determines the size *before* subtracting the internal
overhead (which is sizeof(VARIABLE_HEADER) == 0x20 bytes for
non-authenticated variables, see
"MdeModulePkg/Include/Guid/VariableFormat.h"), and also before subtracting
the given variable's UCS-2 encoded name (including the trailing 0x0000).

Linux maximizes these special variable names in DUMP_NAME_LEN==52 code
points (including the trailing NUL). Hence we must provide at least

  0x020 == sizeof(VARIABLE_HEADER), for the internal overhead
  0x068 == 2 * 52, for the UCS-2 encoded name, including trailing 0x0000
  0x400 for the variable body
  -----
  0x488 == 1160

bytes in PcdMaxVariableSize, so that Linux's efivars-backed pstore can
work even on non-secure-boot builds of OVMF.

However, as PcdMaxVariableSize=0x2000 has proven reasonable when secure
boot is enabled, it should also be okay when secure boot is disabled; so
for simplicity's sake set PcdMaxVariableSize to 0x2000 unconditionally.

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

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15142 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>
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>
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>
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>
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>
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>
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>
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>
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>