render / rpms / edk2

Forked from rpms/edk2 3 months ago
Clone

Blame SOURCES/ovmf-whitepaper-c770f8c.txt

c4e3b2
Open Virtual Machine Firmware (OVMF) Status Report
c4e3b2
July 2014 (with updates in August 2014 - January 2015)
c4e3b2
c4e3b2
Author: Laszlo Ersek <lersek@redhat.com>
c4e3b2
Copyright (C) 2014-2015, Red Hat, Inc.
c4e3b2
CC BY-SA 4.0 <http://creativecommons.org/licenses/by-sa/4.0/>
c4e3b2
c4e3b2
Abstract
c4e3b2
--------
c4e3b2
c4e3b2
The Unified Extensible Firmware Interface (UEFI) is a specification that
c4e3b2
defines a software interface between an operating system and platform firmware.
c4e3b2
UEFI is designed to replace the Basic Input/Output System (BIOS) firmware
c4e3b2
interface.
c4e3b2
c4e3b2
Hardware platform vendors have been increasingly adopting the UEFI
c4e3b2
Specification to govern their boot firmware developments. OVMF (Open Virtual
c4e3b2
Machine Firmware), a sub-project of Intel's EFI Development Kit II (edk2),
c4e3b2
enables UEFI support for Ia32 and X64 Virtual Machines.
c4e3b2
c4e3b2
This paper reports on the status of the OVMF project, treats features and
c4e3b2
limitations, gives end-user hints, and examines some areas in-depth.
c4e3b2
c4e3b2
Keywords: ACPI, boot options, CSM, edk2, firmware, flash, fw_cfg, KVM, memory
c4e3b2
map, non-volatile variables, OVMF, PCD, QEMU, reset vector, S3, Secure Boot,
c4e3b2
Smbios, SMM, TianoCore, UEFI, VBE shim, Virtio
c4e3b2
c4e3b2
Table of Contents
c4e3b2
-----------------
c4e3b2
c4e3b2
- Motivation
c4e3b2
- Scope
c4e3b2
- Example qemu invocation
c4e3b2
- Installation of OVMF guests with virt-manager and virt-install
c4e3b2
- Supported guest operating systems
c4e3b2
- Compatibility Support Module (CSM)
c4e3b2
- Phases of the boot process
c4e3b2
- Project structure
c4e3b2
- Platform Configuration Database (PCD)
c4e3b2
- Firmware image structure
c4e3b2
- S3 (suspend to RAM and resume)
c4e3b2
- A comprehensive memory map of OVMF
c4e3b2
- Known Secure Boot limitations
c4e3b2
- Variable store and LockBox in SMRAM
c4e3b2
- Select features
c4e3b2
  - X64-specific reset vector for OVMF
c4e3b2
  - Client library for QEMU's firmware configuration interface
c4e3b2
  - Guest ACPI tables
c4e3b2
  - Guest SMBIOS tables
c4e3b2
  - Platform-specific boot policy
c4e3b2
  - Virtio drivers
c4e3b2
  - Platform Driver
c4e3b2
  - Video driver
c4e3b2
- Afterword
c4e3b2
c4e3b2
Motivation
c4e3b2
----------
c4e3b2
c4e3b2
OVMF extends the usual benefits of virtualization to UEFI. Reasons to use OVMF
c4e3b2
include:
c4e3b2
c4e3b2
- Legacy-free guests. A UEFI-based environment eliminates dependencies on
c4e3b2
  legacy address spaces and devices. This is especially beneficial when used
c4e3b2
  with physically assigned devices where the legacy operating mode is
c4e3b2
  troublesome to support, ex. assigned graphics cards operating in legacy-free,
c4e3b2
  non-VGA mode in the guest.
c4e3b2
c4e3b2
- Future proof guests. The x86 market is steadily moving towards a legacy-free
c4e3b2
  platform and guest operating systems may eventually require a UEFI
c4e3b2
  environment. OVMF provides that next generation firmware support for such
c4e3b2
  applications.
c4e3b2
c4e3b2
- GUID partition tables (GPTs). MBR partition tables represent partition
c4e3b2
  offsets and sizes with 32-bit integers, in units of 512 byte sectors. This
c4e3b2
  limits the addressable portion of the disk to 2 TB. GPT represents logical
c4e3b2
  block addresses with 64 bits.
c4e3b2
c4e3b2
- Liberating boot loader binaries from residing in contested and poorly defined
c4e3b2
  space between the partition table and the partitions.
c4e3b2
c4e3b2
- Support for booting off disks (eg. pass-through physical SCSI devices) with a
c4e3b2
  4kB physical and logical sector size, i.e. which don't have 512-byte block
c4e3b2
  emulation.
c4e3b2
c4e3b2
- Development and testing of Secure Boot-related features in guest operating
c4e3b2
  systems. Although OVMF's Secure Boot implementation is currently not secure
c4e3b2
  against malicious UEFI drivers, UEFI applications, and guest kernels,
c4e3b2
  trusted guest code that only uses standard UEFI interfaces will find a valid
c4e3b2
  Secure Boot environment under OVMF, with working key enrollment and signature
c4e3b2
  validation. This enables development and testing of portable, Secure
c4e3b2
  Boot-related guest code.
c4e3b2
c4e3b2
- Presence of non-volatile UEFI variables. This furthers development and
c4e3b2
  testing of OS installers, UEFI boot loaders, and unique, dependent guest OS
c4e3b2
  features. For example, an efivars-backed pstore (persistent storage)
c4e3b2
  file system works under Linux.
c4e3b2
c4e3b2
- Altogether, a near production-level UEFI environment for virtual machines
c4e3b2
  when Secure Boot is not required.
c4e3b2
c4e3b2
Scope
c4e3b2
-----
c4e3b2
c4e3b2
UEFI and especially Secure Boot have been topics fraught with controversy and
c4e3b2
political activism. This paper sidesteps these aspects and strives to focus on
c4e3b2
use cases, hands-on information for end users, and technical details.
c4e3b2
c4e3b2
Unless stated otherwise, the expression "X supports Y" means "X is technically
c4e3b2
compatible with interfaces provided or required by Y". It does not imply
c4e3b2
support as an activity performed by natural persons or companies.
c4e3b2
c4e3b2
We discuss the status of OVMF at a state no earlier than edk2 SVN revision
c4e3b2
16158. The paper concentrates on upstream projects and communities, but
c4e3b2
occasionally it pans out about OVMF as it is planned to be shipped (as
c4e3b2
Technical Preview) in Red Hat Enterprise Linux 7.1. Such digressions are marked
c4e3b2
with the [RHEL] margin notation.
c4e3b2
c4e3b2
Although other VMMs and accelerators are known to support (or plan to support)
c4e3b2
OVMF to various degrees -- for example, VirtualBox, Xen, BHyVe --, we'll
c4e3b2
emphasize OVMF on qemu/KVM, because QEMU and KVM have always been Red Hat's
c4e3b2
focus wrt. OVMF.
c4e3b2
c4e3b2
The recommended upstream QEMU version is 2.1+. The recommended host Linux
c4e3b2
kernel (KVM) version is 3.10+. The recommended QEMU machine type is
c4e3b2
"qemu-system-x86_64 -M pc-i440fx-2.1" or later.
c4e3b2
c4e3b2
The term "TianoCore" is used interchangeably with "edk2" in this paper.
c4e3b2
c4e3b2
Example qemu invocation
c4e3b2
-----------------------
c4e3b2
c4e3b2
The following commands give a quick foretaste of installing a UEFI operating
c4e3b2
system on OVMF, relying only on upstream edk2 and qemu.
c4e3b2
c4e3b2
- Clone and build OVMF:
c4e3b2
c4e3b2
  git clone https://github.com/tianocore/edk2.git
c4e3b2
  cd edk2
c4e3b2
  nice OvmfPkg/build.sh -a X64 -n $(getconf _NPROCESSORS_ONLN)
c4e3b2
c4e3b2
  (Note that this ad-hoc build will not include the Secure Boot feature.)
c4e3b2
c4e3b2
- The build output file, "OVMF.fd", includes not only the executable firmware
c4e3b2
  code, but the non-volatile variable store as well. For this reason, make a
c4e3b2
  VM-specific copy of the build output (the variable store should be private to
c4e3b2
  the virtual machine):
c4e3b2
c4e3b2
  cp Build/OvmfX64/DEBUG_GCC4?/FV/OVMF.fd fedora.flash
c4e3b2
c4e3b2
  (The variable store and the firmware executable are also available in the
c4e3b2
  build output as separate files: "OVMF_VARS.fd" and "OVMF_CODE.fd". This
c4e3b2
  enables central management and updates of the firmware executable, while each
c4e3b2
  virtual machine can retain its own variable store.)
c4e3b2
c4e3b2
- Download a Fedora LiveCD:
c4e3b2
c4e3b2
  wget https://dl.fedoraproject.org/pub/fedora/linux/releases/20/Live/x86_64/Fedora-Live-Xfce-x86_64-20-1.iso
c4e3b2
c4e3b2
- Create a virtual disk (qcow2 format, 20 GB in size):
c4e3b2
c4e3b2
  qemu-img create -f qcow2 fedora.img 20G
c4e3b2
c4e3b2
- Create the following qemu wrapper script under the name "fedora.sh":
c4e3b2
c4e3b2
  # Basic virtual machine properties: a recent i440fx machine type, KVM
c4e3b2
  # acceleration, 2048 MB RAM, two VCPUs.
c4e3b2
  OPTS="-M pc-i440fx-2.1 -enable-kvm -m 2048 -smp 2"
c4e3b2
c4e3b2
  # The OVMF binary, including the non-volatile variable store, appears as a
c4e3b2
  # "normal" qemu drive on the host side, and it is exposed to the guest as a
c4e3b2
  # persistent flash device.
c4e3b2
  OPTS="$OPTS -drive if=pflash,format=raw,file=fedora.flash"
c4e3b2
c4e3b2
  # The hard disk is exposed to the guest as a virtio-block device. OVMF has a
c4e3b2
  # driver stack that supports such a disk. We specify this disk as first boot
c4e3b2
  # option. OVMF recognizes the boot order specification.
c4e3b2
  OPTS="$OPTS -drive id=disk0,if=none,format=qcow2,file=fedora.img"
c4e3b2
  OPTS="$OPTS -device virtio-blk-pci,drive=disk0,bootindex=0"
c4e3b2
c4e3b2
  # The Fedora installer disk appears as an IDE CD-ROM in the guest. This is
c4e3b2
  # the 2nd boot option.
c4e3b2
  OPTS="$OPTS -drive id=cd0,if=none,format=raw,readonly"
c4e3b2
  OPTS="$OPTS,file=Fedora-Live-Xfce-x86_64-20-1.iso"
c4e3b2
  OPTS="$OPTS -device ide-cd,bus=ide.1,drive=cd0,bootindex=1"
c4e3b2
c4e3b2
  # The following setting enables S3 (suspend to RAM). OVMF supports S3
c4e3b2
  # suspend/resume.
c4e3b2
  OPTS="$OPTS -global PIIX4_PM.disable_s3=0"
c4e3b2
c4e3b2
  # OVMF emits a number of info / debug messages to the QEMU debug console, at
c4e3b2
  # ioport 0x402. We configure qemu so that the debug console is indeed
c4e3b2
  # available at that ioport. We redirect the host side of the debug console to
c4e3b2
  # a file.
c4e3b2
  OPTS="$OPTS -global isa-debugcon.iobase=0x402 -debugcon file:fedora.ovmf.log"
c4e3b2
c4e3b2
  # QEMU accepts various commands and queries from the user on the monitor
c4e3b2
  # interface. Connect the monitor with the qemu process's standard input and
c4e3b2
  # output.
c4e3b2
  OPTS="$OPTS -monitor stdio"
c4e3b2
c4e3b2
  # A USB tablet device in the guest allows for accurate pointer tracking
c4e3b2
  # between the host and the guest.
c4e3b2
  OPTS="$OPTS -device piix3-usb-uhci -device usb-tablet"
c4e3b2
c4e3b2
  # Provide the guest with a virtual network card (virtio-net).
c4e3b2
  #
c4e3b2
  # Normally, qemu provides the guest with a UEFI-conformant network driver
c4e3b2
  # from the iPXE project, in the form of a PCI expansion ROM. For this test,
c4e3b2
  # we disable the expansion ROM and allow OVMF's built-in virtio-net driver to
c4e3b2
  # take effect.
c4e3b2
  #
c4e3b2
  # On the host side, we use the SLIRP ("user") network backend, which has
c4e3b2
  # relatively low performance, but it doesn't require extra privileges from
c4e3b2
  # the user executing qemu.
c4e3b2
  OPTS="$OPTS -netdev id=net0,type=user"
c4e3b2
  OPTS="$OPTS -device virtio-net-pci,netdev=net0,romfile="
c4e3b2
c4e3b2
  # A Spice QXL GPU is recommended as the primary VGA-compatible display
c4e3b2
  # device. It is a full-featured virtual video card, with great operating
c4e3b2
  # system driver support. OVMF supports it too.
c4e3b2
  OPTS="$OPTS -device qxl-vga"
c4e3b2
c4e3b2
  qemu-system-x86_64 $OPTS
c4e3b2
c4e3b2
- Start the Fedora guest:
c4e3b2
c4e3b2
  sh fedora.sh
c4e3b2
c4e3b2
- The above command can be used for both installation and later boots of the
c4e3b2
  Fedora guest.
c4e3b2
c4e3b2
- In order to verify basic OVMF network connectivity:
c4e3b2
c4e3b2
  - Assuming that the non-privileged user running qemu belongs to group G
c4e3b2
    (where G is a numeric identifier), ensure as root on the host that the
c4e3b2
    group range in file "/proc/sys/net/ipv4/ping_group_range" includes G.
c4e3b2
c4e3b2
  - As the non-privileged user, boot the guest as usual.
c4e3b2
c4e3b2
  - On the TianoCore splash screen, press ESC.
c4e3b2
c4e3b2
  - Navigate to Boot Manager | EFI Internal Shell
c4e3b2
c4e3b2
  - In the UEFI Shell, issue the following commands:
c4e3b2
c4e3b2
    ifconfig -s eth0 dhcp
c4e3b2
    ping A.B.C.D
c4e3b2
c4e3b2
    where A.B.C.D is a public IPv4 address in dotted decimal notation that your
c4e3b2
    host can reach.
c4e3b2
c4e3b2
  - Type "quit" at the (qemu) monitor prompt.
c4e3b2
c4e3b2
Installation of OVMF guests with virt-manager and virt-install
c4e3b2
--------------------------------------------------------------
c4e3b2
c4e3b2
(1) Assuming OVMF has been installed on the host with the following files:
c4e3b2
    - /usr/share/OVMF/OVMF_CODE.fd
c4e3b2
    - /usr/share/OVMF/OVMF_VARS.fd
c4e3b2
c4e3b2
    locate the "nvram" stanza in "/etc/libvirt/qemu.conf", and edit it as
c4e3b2
    follows:
c4e3b2
c4e3b2
    nvram = [ "/usr/share/OVMF/OVMF_CODE.fd:/usr/share/OVMF/OVMF_VARS.fd" ]
c4e3b2
c4e3b2
(2) Restart libvirtd with your Linux distribution's service management tool;
c4e3b2
    for example,
c4e3b2
c4e3b2
    systemctl restart libvirtd
c4e3b2
c4e3b2
(3) In virt-manager, proceed with the guest installation as usual:
c4e3b2
    - select File | New Virtual Machine,
c4e3b2
    - advance to Step 5 of 5,
c4e3b2
    - in Step 5, check "Customize configuration before install",
c4e3b2
    - click Finish;
c4e3b2
    - in the customization dialog, select Overview | Firmware, and choose UEFI,
c4e3b2
    - click Apply and Begin Installation.
c4e3b2
c4e3b2
(4) With virt-install:
c4e3b2
c4e3b2
    LDR="loader=/usr/share/OVMF/OVMF_CODE.fd,loader_ro=yes,loader_type=pflash"
c4e3b2
    virt-install \
c4e3b2
      --name fedora20 \
c4e3b2
      --memory 2048 \
c4e3b2
      --vcpus 2 \
c4e3b2
      --os-variant fedora20 \
c4e3b2
      --boot hd,cdrom,$LDR \
c4e3b2
      --disk size=20 \
c4e3b2
      --disk path=Fedora-Live-Xfce-x86_64-20-1.iso,device=cdrom,bus=scsi
c4e3b2
c4e3b2
(5) A popular, distribution-independent, bleeding-edge OVMF package is
c4e3b2
    available under <https://www.kraxel.org/repos/>, courtesy of Gerd Hoffmann.
c4e3b2
c4e3b2
    The "edk2.git-ovmf-x64" package provides the following files, among others:
c4e3b2
    - /usr/share/edk2.git/ovmf-x64/OVMF_CODE-pure-efi.fd
c4e3b2
    - /usr/share/edk2.git/ovmf-x64/OVMF_VARS-pure-efi.fd
c4e3b2
c4e3b2
    When using this package, adapt steps (1) and (4) accordingly.
c4e3b2
c4e3b2
(6) Additionally, the "edk2.git-ovmf-x64" package seeks to simplify the
c4e3b2
    enablement of Secure Boot in a virtual machine (strictly for development
c4e3b2
    and testing purposes).
c4e3b2
c4e3b2
    - Boot the virtual machine off the CD-ROM image called
c4e3b2
      "/usr/share/edk2.git/ovmf-x64/UefiShell.iso"; before or after installing
c4e3b2
      the main guest operating system.
c4e3b2
c4e3b2
    - When the UEFI shell appears, issue the following commands:
c4e3b2
c4e3b2
      EnrollDefaultKeys.efi
c4e3b2
      reset -s
c4e3b2
c4e3b2
    - The EnrollDefaultKeys.efi utility enrolls the following keys:
c4e3b2
c4e3b2
      - A static example X.509 certificate (CN=TestCommonName) as Platform Key
c4e3b2
        and first Key Exchange Key.
c4e3b2
c4e3b2
        The private key matching this certificate has been destroyed (but you
c4e3b2
        shouldn't trust this statement).
c4e3b2
c4e3b2
      - "Microsoft Corporation KEK CA 2011" as second Key Exchange Key
c4e3b2
        (SHA1: 31:59:0b:fd:89:c9:d7:4e:d0:87:df:ac:66:33:4b:39:31:25:4b:30).
c4e3b2
c4e3b2
      - "Microsoft Windows Production PCA 2011" as first DB entry
c4e3b2
        (SHA1: 58:0a:6f:4c:c4:e4:b6:69:b9:eb:dc:1b:2b:3e:08:7b:80:d0:67:8d).
c4e3b2
c4e3b2
      - "Microsoft Corporation UEFI CA 2011" as second DB entry
c4e3b2
        (SHA1: 46:de:f6:3b:5c:e6:1c:f8:ba:0d:e2:e6:63:9c:10:19:d0:ed:14:f3).
c4e3b2
c4e3b2
      These keys suffice to boot released versions of popular Linux
c4e3b2
      distributions (through the shim.efi utility), and Windows 8 and Windows
c4e3b2
      Server 2012 R2, in Secure Boot mode.
c4e3b2
c4e3b2
Supported guest operating systems
c4e3b2
---------------------------------
c4e3b2
c4e3b2
Upstream OVMF does not favor some guest operating systems over others for
c4e3b2
political or ideological reasons. However, some operating systems are harder to
c4e3b2
obtain and/or technically more difficult to support. The general expectation is
c4e3b2
that recent UEFI OSes should just work. Please consult the "OvmfPkg/README"
c4e3b2
file.
c4e3b2
c4e3b2
The following guest OSes were tested with OVMF:
c4e3b2
- Red Hat Enterprise Linux 6
c4e3b2
- Red Hat Enterprise Linux 7
c4e3b2
- Fedora 18
c4e3b2
- Fedora 19
c4e3b2
- Fedora 20
c4e3b2
- Windows Server 2008 R2 SP1
c4e3b2
- Windows Server 2012
c4e3b2
- Windows 8
c4e3b2
c4e3b2
Notes about Windows Server 2008 R2 (paraphrasing the "OvmfPkg/README" file):
c4e3b2
c4e3b2
- QEMU should be started with one of the "-device qxl-vga" and "-device VGA"
c4e3b2
  options.
c4e3b2
c4e3b2
- Only one video mode, 1024x768x32, is supported at OS runtime.
c4e3b2
c4e3b2
  Please refer to the section about QemuVideoDxe (OVMF's built-in video driver)
c4e3b2
  for more details on this limitation.
c4e3b2
c4e3b2
- The qxl-vga video card is recommended ("-device qxl-vga"). After booting the
c4e3b2
  installed guest OS, select the video card in Device Manager, and upgrade the
c4e3b2
  video driver to the QXL XDDM one.
c4e3b2
c4e3b2
  The QXL XDDM driver can be downloaded from
c4e3b2
  <http://www.spice-space.org/download.html>, under Guest | Windows binaries.
c4e3b2
c4e3b2
  This driver enables additional graphics resolutions at OS runtime, and
c4e3b2
  provides S3 (suspend/resume) capability.
c4e3b2
c4e3b2
Notes about Windows Server 2012 and Windows 8:
c4e3b2
c4e3b2
- QEMU should be started with the "-device qxl-vga,revision=4" option (or a
c4e3b2
  later revision, if available).
c4e3b2
c4e3b2
- The guest OS's builtin video driver inherits the video mode / frame buffer
c4e3b2
  from OVMF. There's no way to change the resolution at OS runtime.
c4e3b2
c4e3b2
  For this reason, a platform driver has been developed for OVMF, which allows
c4e3b2
  users to change the preferred video mode in the firmware. Please refer to the
c4e3b2
  section about PlatformDxe for details.
c4e3b2
c4e3b2
- It is recommended to upgrade the guest OS's video driver to the QXL WDDM one,
c4e3b2
  via Device Manager.
c4e3b2
c4e3b2
  Binaries for the QXL WDDM driver can be found at
c4e3b2
  <http://people.redhat.com/~vrozenfe/qxlwddm> (pick a version greater than or
c4e3b2
  equal to 0.6), while the source code resides at
c4e3b2
  <https://github.com/vrozenfe/qxl-dod>.
c4e3b2
c4e3b2
  This driver enables additional graphics resolutions at OS runtime, and
c4e3b2
  provides S3 (suspend/resume) capability.
c4e3b2
c4e3b2
Compatibility Support Module (CSM)
c4e3b2
----------------------------------
c4e3b2
c4e3b2
Collaboration between SeaBIOS and OVMF developers has enabled SeaBIOS to be
c4e3b2
built as a Compatibility Support Module, and OVMF to embed and use it.
c4e3b2
c4e3b2
Benefits of a SeaBIOS CSM include:
c4e3b2
c4e3b2
- The ability to boot legacy (non-UEFI) operating systems, such as legacy Linux
c4e3b2
  systems, Windows 7, OpenBSD 5.2, FreeBSD 8/9, NetBSD, DragonflyBSD, Solaris
c4e3b2
  10/11.
c4e3b2
c4e3b2
- Legacy (non-UEFI-compliant) PCI expansion ROMs, such as a VGA BIOS, mapped by
c4e3b2
  QEMU in emulated devices' ROM BARs, are loaded and executed by OVMF.
c4e3b2
c4e3b2
  For example, this grants the Windows Server 2008 R2 SP1 guest's native,
c4e3b2
  legacy video driver access to all modes of all QEMU video cards.
c4e3b2
c4e3b2
Building the CSM target of the SeaBIOS source tree is out of scope for this
c4e3b2
report. Additionally, upstream OVMF does not enable the CSM by default.
c4e3b2
c4e3b2
Interested users and developers should look for OVMF's "-D CSM_ENABLE"
c4e3b2
build-time option, and check out the <https://www.kraxel.org/repos/> continuous
c4e3b2
integration repository, which provides CSM-enabled OVMF builds.
c4e3b2
c4e3b2
[RHEL] The "OVMF_CODE.fd" firmware image made available on the Red Hat
c4e3b2
       Enterprise Linux 7.1 host does not include a Compatibility Support
c4e3b2
       Module, for the following reasons:
c4e3b2
c4e3b2
       - Virtual machines running officially supported, legacy guest operating
c4e3b2
         systems should just use the standalone SeaBIOS firmware. Firmware
c4e3b2
         selection is flexible in virtualization, see eg. "Installation of OVMF
c4e3b2
         guests with virt-manager and virt-install" above.
c4e3b2
c4e3b2
       - The 16-bit thunking interface between OVMF and SeaBIOS is very complex
c4e3b2
         and presents a large debugging and support burden, based on past
c4e3b2
         experience.
c4e3b2
c4e3b2
       - Secure Boot is incompatible with CSM.
c4e3b2
c4e3b2
       - Inter-project dependencies should be minimized whenever possible.
c4e3b2
c4e3b2
       - Using the default QXL video card, the Windows 2008 R2 SP1 guest can be
c4e3b2
         installed with its built-in, legacy video driver. Said driver will
c4e3b2
         select the only available video mode, 1024x768x32. After installation,
c4e3b2
         the video driver can be upgraded to the full-featured QXL XDDM driver.
c4e3b2
c4e3b2
Phases of the boot process
c4e3b2
--------------------------
c4e3b2
c4e3b2
The PI and UEFI specifications, and Intel's UEFI and EDK II Learning and
c4e3b2
Development materials provide ample information on PI and UEFI concepts. The
c4e3b2
following is an absolutely minimal, rough glossary that is included only to
c4e3b2
help readers new to PI and UEFI understand references in later, OVMF-specific
c4e3b2
sections. We defer heavily to the official specifications and the training
c4e3b2
materials, and frequently quote them below.
c4e3b2
c4e3b2
A central concept to mention early is the GUID -- globally unique identifier. A
c4e3b2
GUID is a 128-bit number, written as XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX,
c4e3b2
where each X stands for a hexadecimal nibble. GUIDs are used to name everything
c4e3b2
in PI and in UEFI. Programmers introduce new GUIDs with the "uuidgen" utility,
c4e3b2
and standards bodies standardize well-known services by positing their GUIDs.
c4e3b2
c4e3b2
The boot process is roughly divided in the following phases:
c4e3b2
c4e3b2
- Reset vector code.
c4e3b2
c4e3b2
- SEC: Security phase. This phase is the root of firmware integrity.
c4e3b2
c4e3b2
- PEI: Pre-EFI Initialization. This phase performs "minimal processor, chipset
c4e3b2
  and platform configuration for the purpose of discovering memory". Modules in
c4e3b2
  PEI collectively save their findings about the platform in a list of HOBs
c4e3b2
  (hand-off blocks).
c4e3b2
c4e3b2
  When developing PEI code, the Platform Initialization (PI) specification
c4e3b2
  should be consulted.
c4e3b2
c4e3b2
- DXE: Driver eXecution Environment, pronounced as "Dixie". This "is the phase
c4e3b2
  where the bulk of the booting occurs: devices are enumerated and initialized,
c4e3b2
  UEFI services are supported, and protocols and drivers are implemented. Also,
c4e3b2
  the tables that create the UEFI interface are produced".
c4e3b2
c4e3b2
  On the PEI/DXE boundary, the HOBs produced by PEI are consumed. For example,
c4e3b2
  this is how the memory space map is configured initially.
c4e3b2
c4e3b2
- BDS: Boot Device Selection. It is "responsible for determining how and where
c4e3b2
  you want to boot the operating system".
c4e3b2
c4e3b2
  When developing DXE and BDS code, it is mainly the UEFI specification that
c4e3b2
  should be consulted. When speaking about DXE, BDS is frequently considered to
c4e3b2
  be a part of it.
c4e3b2
c4e3b2
The following concepts are tied to specific boot process phases:
c4e3b2
c4e3b2
- PEIM: a PEI Module (pronounced "PIM"). A binary module running in the PEI
c4e3b2
  phase, consuming some PPIs and producing other PPIs, and producing HOBs.
c4e3b2
c4e3b2
- PPI: PEIM-to-PEIM interface. A structure of function pointers and related
c4e3b2
  data members that establishes a PEI service, or an instance of a PEI service.
c4e3b2
  PPIs are identified by GUID.
c4e3b2
c4e3b2
  An example is EFI_PEI_S3_RESUME2_PPI (6D582DBC-DB85-4514-8FCC-5ADF6227B147).
c4e3b2
c4e3b2
- DXE driver: a binary module running in the DXE and BDS phases, consuming some
c4e3b2
  protocols and producing other protocols.
c4e3b2
c4e3b2
- Protocol: A structure of function pointers and related data members that
c4e3b2
  establishes a DXE service, or an instance of a DXE service. Protocols are
c4e3b2
  identified by GUID.
c4e3b2
c4e3b2
  An example is EFI_BLOCK_IO_PROTOCOL (964E5B21-6459-11D2-8E39-00A0C969723B).
c4e3b2
c4e3b2
- Architectural protocols: a set of standard protocols that are foundational to
c4e3b2
  the working of a UEFI system. Each architectural protocol has at most one
c4e3b2
  instance. Architectural protocols are implemented by a subset of DXE drivers.
c4e3b2
  DXE drivers explicitly list the set of protocols (including architectural
c4e3b2
  protocols) that they need to work. UEFI drivers can only be loaded once all
c4e3b2
  architectural protocols have become available during the DXE phase.
c4e3b2
c4e3b2
  An example is EFI_VARIABLE_WRITE_ARCH_PROTOCOL
c4e3b2
  (6441F818-6362-4E44-B570-7DBA31DD2453).
c4e3b2
c4e3b2
Project structure
c4e3b2
-----------------
c4e3b2
c4e3b2
The term "OVMF" usually denotes the project (community and development effort)
c4e3b2
that provide and maintain the subject matter UEFI firmware for virtual
c4e3b2
machines. However the term is also frequently applied to the firmware binary
c4e3b2
proper that a virtual machine executes.
c4e3b2
c4e3b2
OVMF emerges as a compilation of several modules from the edk2 source
c4e3b2
repository. "edk2" stands for EFI Development Kit II; it is a "modern,
c4e3b2
feature-rich, cross-platform firmware development environment for the UEFI and
c4e3b2
PI specifications".
c4e3b2
c4e3b2
The composition of OVMF is dictated by the following build control files:
c4e3b2
c4e3b2
  OvmfPkg/OvmfPkgIa32.dsc
c4e3b2
  OvmfPkg/OvmfPkgIa32.fdf
c4e3b2
c4e3b2
  OvmfPkg/OvmfPkgIa32X64.dsc
c4e3b2
  OvmfPkg/OvmfPkgIa32X64.fdf
c4e3b2
c4e3b2
  OvmfPkg/OvmfPkgX64.dsc
c4e3b2
  OvmfPkg/OvmfPkgX64.fdf
c4e3b2
c4e3b2
The format of these files is described in the edk2 DSC and FDF specifications.
c4e3b2
Roughly, the DSC file determines:
c4e3b2
- library instance resolutions for library class requirements presented by the
c4e3b2
  modules to be compiled,
c4e3b2
- the set of modules to compile.
c4e3b2
c4e3b2
The FDF file roughly determines:
c4e3b2
- what binary modules (compilation output files, precompiled binaries, graphics
c4e3b2
  image files, verbatim binary sections) to include in the firmware image,
c4e3b2
- how to lay out the firmware image.
c4e3b2
c4e3b2
The Ia32 flavor of these files builds a firmware where both PEI and DXE phases
c4e3b2
are 32-bit. The Ia32X64 flavor builds a firmware where the PEI phase consists
c4e3b2
of 32-bit modules, and the DXE phase is 64-bit. The X64 flavor builds a purely
c4e3b2
64-bit firmware.
c4e3b2
c4e3b2
The word size of the DXE phase must match the word size of the runtime OS -- a
c4e3b2
32-bit DXE can't cooperate with a 64-bit OS, and a 64-bit DXE can't work a
c4e3b2
32-bit OS.
c4e3b2
c4e3b2
OVMF pulls together modules from across the edk2 tree. For example:
c4e3b2
c4e3b2
- common drivers and libraries that are platform independent are usually
c4e3b2
  located under MdeModulePkg and MdePkg,
c4e3b2
c4e3b2
- common but hardware-specific drivers and libraries that match QEMU's
c4e3b2
  pc-i440fx-* machine type are pulled in from IntelFrameworkModulePkg,
c4e3b2
  PcAtChipsetPkg and UefiCpuPkg,
c4e3b2
c4e3b2
- the platform independent UEFI Shell is built from ShellPkg,
c4e3b2
c4e3b2
- OvmfPkg includes drivers and libraries that are useful for virtual machines
c4e3b2
  and may or may not be specific to QEMU's pc-i440fx-* machine type.
c4e3b2
c4e3b2
Platform Configuration Database (PCD)
c4e3b2
-------------------------------------
c4e3b2
c4e3b2
Like the "Phases of the boot process" section, this one introduces a concept in
c4e3b2
very raw form. We defer to the PCD related edk2 specifications, and we won't
c4e3b2
discuss implementation details here. Our purpose is only to offer the reader a
c4e3b2
usable (albeit possibly inaccurate) definition, so that we can refer to PCDs
c4e3b2
later on.
c4e3b2
c4e3b2
Colloquially, when we say "PCD", we actually mean "PCD entry"; that is, an
c4e3b2
entry stored in the Platform Configuration Database.
c4e3b2
c4e3b2
The Platform Configuration Database is
c4e3b2
- a firmware-wide
c4e3b2
- name-value store
c4e3b2
- of scalars and buffers
c4e3b2
- where each entry may be
c4e3b2
  - build-time constant, or
c4e3b2
  - run-time dynamic, or
c4e3b2
  - theoretically, a middle option: patchable in the firmware file itself,
c4e3b2
    using a dedicated tool. (OVMF does not utilize externally patchable
c4e3b2
    entries.)
c4e3b2
c4e3b2
A PCD entry is declared in the DEC file of the edk2 top-level Package directory
c4e3b2
whose modules (drivers and libraries) are the primary consumers of the PCD
c4e3b2
entry. (See for example OvmfPkg/OvmfPkg.dec). Basically, a PCD in a DEC file
c4e3b2
exposes a simple customization point.
c4e3b2
c4e3b2
Interest in a PCD entry is communicated to the build system by naming the PCD
c4e3b2
entry in the INF file of the interested module (application, driver or
c4e3b2
library). The module may read and -- dependent on the PCD entry's category --
c4e3b2
write the PCD entry.
c4e3b2
c4e3b2
Let's investigate the characteristics of the Database and the PCD entries.
c4e3b2
c4e3b2
- Firmware-wide: technically, all modules may access all entries they are
c4e3b2
  interested in, assuming they advertise their interest in their INF files.
c4e3b2
  With careful design, PCDs enable inter-driver propagation of (simple) system
c4e3b2
  configuration. PCDs are available in both PEI and DXE.
c4e3b2
c4e3b2
  (UEFI drivers meant to be portable (ie. from third party vendors) are not
c4e3b2
  supposed to use PCDs, since PCDs qualify internal to the specific edk2
c4e3b2
  firmware in question.)
c4e3b2
c4e3b2
- Name-value store of scalars and buffers: each PCD has a symbolic name, and a
c4e3b2
  fixed scalar type (UINT16, UINT32 etc), or VOID* for buffers. Each PCD entry
c4e3b2
  belongs to a namespace, where a namespace is (obviously) a GUID, defined in
c4e3b2
  the DEC file.
c4e3b2
c4e3b2
- A DEC file can permit several categories for a PCD:
c4e3b2
  - build-time constant ("FixedAtBuild"),
c4e3b2
  - patchable in the firmware image ("PatchableInModule", unused in OVMF),
c4e3b2
  - runtime modifiable ("Dynamic").
c4e3b2
c4e3b2
The platform description file (DSC) of a top-level Package directory may choose
c4e3b2
the exact category for a given PCD entry that its modules wish to use, and
c4e3b2
assign a default (or constant) initial value to it.
c4e3b2
c4e3b2
In addition, the edk2 build system too can initialize PCD entries to values
c4e3b2
that it calculates while laying out the flash device image. Such PCD
c4e3b2
assignments are described in the FDF control file.
c4e3b2
c4e3b2
Firmware image structure
c4e3b2
------------------------
c4e3b2
c4e3b2
(We assume the common X64 choice for both PEI and DXE, and the default DEBUG
c4e3b2
build target.)
c4e3b2
c4e3b2
The OvmfPkg/OvmfPkgX64.fdf file defines the following layout for the flash
c4e3b2
device image "OVMF.fd":
c4e3b2
c4e3b2
  Description                     Compression type        Size
c4e3b2
  ------------------------------  ----------------------  -------
c4e3b2
  Non-volatile data storage       open-coded binary data   128 KB
c4e3b2
    Variable store                                          56 KB
c4e3b2
    Event log                                                4 KB
c4e3b2
    Working block                                            4 KB
c4e3b2
    Spare area                                              64 KB
c4e3b2
c4e3b2
  FVMAIN_COMPACT                  uncompressed            1712 KB
c4e3b2
    FV Firmware File System file  LZMA compressed
c4e3b2
      PEIFV                       uncompressed             896 KB
c4e3b2
        individual PEI modules    uncompressed
c4e3b2
      DXEFV                       uncompressed            8192 KB
c4e3b2
        individual DXE modules    uncompressed
c4e3b2
c4e3b2
  SECFV                           uncompressed             208 KB
c4e3b2
    SEC driver
c4e3b2
    reset vector code
c4e3b2
c4e3b2
The top-level image consists of three regions (three firmware volumes):
c4e3b2
- non-volatile data store (128 KB),
c4e3b2
- main firmware volume (FVMAIN_COMPACT, 1712 KB),
c4e3b2
- firmware volume containing the reset vector code and the SEC phase code (208
c4e3b2
  KB).
c4e3b2
c4e3b2
In total, the OVMF.fd file has size 128 KB + 1712 KB + 208 KB == 2 MB.
c4e3b2
c4e3b2
(1) The firmware volume with non-volatile data store (128 KB) has the following
c4e3b2
    internal structure, in blocks of 4 KB:
c4e3b2
c4e3b2
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+  L: event log
c4e3b2
       LIVE | varstore                  |L|W|  W: working block
c4e3b2
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
c4e3b2
c4e3b2
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
c4e3b2
      SPARE |                               |
c4e3b2
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
c4e3b2
c4e3b2
    The first half of this firmware volume is "live", while the second half is
c4e3b2
    "spare". The spare half is important when the variable driver reclaims
c4e3b2
    unused storage and reorganizes the variable store.
c4e3b2
c4e3b2
    The live half dedicates 14 blocks (56 KB) to the variable store itself. On
c4e3b2
    top of those, one block is set aside for an event log, and one block is
c4e3b2
    used as the working block of the fault tolerant write protocol. Fault
c4e3b2
    tolerant writes are used to recover from an occasional (virtual) power loss
c4e3b2
    during variable updates.
c4e3b2
c4e3b2
    The blocks in this firmware volume are accessed, in stacking order from
c4e3b2
    least abstract to most abstract, by:
c4e3b2
c4e3b2
    - EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL (provided by
c4e3b2
      OvmfPkg/QemuFlashFvbServicesRuntimeDxe),
c4e3b2
c4e3b2
    - EFI_FAULT_TOLERANT_WRITE_PROTOCOL (provided by
c4e3b2
      MdeModulePkg/Universal/FaultTolerantWriteDxe),
c4e3b2
c4e3b2
    - architectural protocols instrumental to the runtime UEFI variable
c4e3b2
      services:
c4e3b2
      - EFI_VARIABLE_ARCH_PROTOCOL,
c4e3b2
      - EFI_VARIABLE_WRITE_ARCH_PROTOCOL.
c4e3b2
c4e3b2
      In a non-secure boot build, the DXE driver providing these architectural
c4e3b2
      protocols is MdeModulePkg/Universal/Variable/RuntimeDxe. In a secure boot
c4e3b2
      build, where authenticated variables are available, the DXE driver
c4e3b2
      offering these protocols is SecurityPkg/VariableAuthenticated/RuntimeDxe.
c4e3b2
c4e3b2
(2) The main firmware volume (FVMAIN_COMPACT, 1712 KB) embeds further firmware
c4e3b2
    volumes. The outermost layer is a Firmware File System (FFS), carrying a
c4e3b2
    single file. This file holds an LZMA-compressed section, which embeds two
c4e3b2
    firmware volumes: PEIFV (896 KB) with PEIMs, and DXEFV (8192 KB) with DXE
c4e3b2
    and UEFI drivers.
c4e3b2
c4e3b2
    This scheme enables us to build 896 KB worth of PEI drivers and 8192 KB
c4e3b2
    worth of DXE and UEFI drivers, compress them all with LZMA in one go, and
c4e3b2
    store the compressed result in 1712 KB, saving room in the flash device.
c4e3b2
c4e3b2
(3) The SECFV firmware volume (208 KB) is not compressed. It carries the
c4e3b2
    "volume top file" with the reset vector code, to end at 4 GB in
c4e3b2
    guest-physical address space, and the SEC phase driver (OvmfPkg/Sec).
c4e3b2
c4e3b2
    The last 16 bytes of the volume top file (mapped directly under 4 GB)
c4e3b2
    contain a NOP slide and a jump instruction. This is where QEMU starts
c4e3b2
    executing the firmware, at address 0xFFFF_FFF0. The reset vector and the
c4e3b2
    SEC driver run from flash directly.
c4e3b2
c4e3b2
    The SEC driver locates FVMAIN_COMPACT in the flash, and decompresses the
c4e3b2
    main firmware image to RAM. The rest of OVMF (PEI, DXE, BDS phases) run
c4e3b2
    from RAM.
c4e3b2
c4e3b2
As already mentioned, the OVMF.fd file is mapped by qemu's
c4e3b2
"hw/block/pflash_cfi01.c" device just under 4 GB in guest-physical address
c4e3b2
space, according to the command line option
c4e3b2
c4e3b2
  -drive if=pflash,format=raw,file=fedora.flash
c4e3b2
c4e3b2
(refer to the Example qemu invocation). This is a "ROMD device", which can
c4e3b2
switch out of "ROMD mode" and back into it.
c4e3b2
c4e3b2
Namely, in the default ROMD mode, the guest-physical address range backed by
c4e3b2
the flash device reads and executes as ROM (it does not trap from KVM to QEMU).
c4e3b2
The first write access in this mode traps to QEMU, and flips the device out of
c4e3b2
ROMD mode.
c4e3b2
c4e3b2
In non-ROMD mode, the flash chip is programmed by storing CFI (Common Flash
c4e3b2
Interface) command values at the flash-covered addresses; both reads and writes
c4e3b2
trap to QEMU, and the flash contents are modified and synchronized to the
c4e3b2
host-side file. A special CFI command flips the flash device back to ROMD mode.
c4e3b2
c4e3b2
Qemu implements the above based on the KVM_CAP_READONLY_MEM / KVM_MEM_READONLY
c4e3b2
KVM features, and OVMF puts it to use in its EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL
c4e3b2
implementation, under "OvmfPkg/QemuFlashFvbServicesRuntimeDxe".
c4e3b2
c4e3b2
IMPORTANT: Never pass OVMF.fd to qemu with the -bios option. That option maps
c4e3b2
the firmware image as ROM into the guest's address space, and forces OVMF to
c4e3b2
emulate non-volatile variables with a fallback driver that is bound to have
c4e3b2
insufficient and confusing semantics.
c4e3b2
c4e3b2
The 128 KB firmware volume with the variable store, discussed under (1), is
c4e3b2
also built as a separate host-side file, named "OVMF_VARS.fd". The "rest" is
c4e3b2
built into a third file, "OVMF_CODE.fd", which is only 1920 KB in size. The
c4e3b2
variable store is mapped into its usual location, at 4 GB - 2 MB = 0xFFE0_0000,
c4e3b2
through the following qemu options:
c4e3b2
c4e3b2
  -drive if=pflash,format=raw,readonly,file=OVMF_CODE.fd   \
c4e3b2
  -drive if=pflash,format=raw,file=fedora.varstore.fd
c4e3b2
c4e3b2
This way qemu configures two flash chips consecutively, with start addresses
c4e3b2
growing downwards, which is transparent to OVMF.
c4e3b2
c4e3b2
[RHEL] Red Hat Enterprise Linux 7.1 ships a Secure Boot-enabled, X64, DEBUG
c4e3b2
       firmware only. Furthermore, only the split files ("OVMF_VARS.fd" and
c4e3b2
       "OVMF_CODE.fd") are available.
c4e3b2
c4e3b2
S3 (suspend to RAM and resume)
c4e3b2
------------------------------
c4e3b2
c4e3b2
As noted in Example qemu invocation, the
c4e3b2
c4e3b2
  -global PIIX4_PM.disable_s3=0
c4e3b2
c4e3b2
command line option tells qemu and OVMF if the user would like to enable S3
c4e3b2
support. (This is corresponds to the /domain/pm/suspend-to-mem/@enabled libvirt
c4e3b2
domain XML attribute.)
c4e3b2
c4e3b2
Implementing / orchestrating S3 was a considerable community effort in OVMF. A
c4e3b2
detailed description exceeds the scope of this report; we only make a few
c4e3b2
statements.
c4e3b2
c4e3b2
(1) S3-related PPIs and protocols are well documented in the PI specification.
c4e3b2
c4e3b2
(2) Edk2 contains most modules that are needed to implement S3 on a given
c4e3b2
    platform. One abstraction that is central to the porting / extending of the
c4e3b2
    S3-related modules to a new platform is the LockBox library interface,
c4e3b2
    which a specific platform can fill in by implementing its own LockBox
c4e3b2
    library instance.
c4e3b2
c4e3b2
    The LockBox library provides a privileged name-value store (to be addressed
c4e3b2
    by GUIDs). The privilege separation stretches between the firmware and the
c4e3b2
    operating system. That is, the S3-related machinery of the firmware saves
c4e3b2
    some items in the LockBox securely, under well-known GUIDs, before booting
c4e3b2
    the operating system. During resume (which is a form of warm reset), the
c4e3b2
    firmware is activated again, and retrieves items from the LockBox. Before
c4e3b2
    jumping to the OS's resume vector, the LockBox is secured again.
c4e3b2
c4e3b2
    We'll return to this later when we separately discuss SMRAM and SMM.
c4e3b2
c4e3b2
(3) During resume, the DXE and later phases are never reached; only the reset
c4e3b2
    vector, and the SEC and PEI phases of the firmware run. The platform is
c4e3b2
    supposed to detect a resume in progress during PEI, and to store that fact
c4e3b2
    in the BootMode field of the Phase Handoff Information Table (PHIT) HOB.
c4e3b2
    OVMF keys this off the CMOS, see OvmfPkg/PlatformPei.
c4e3b2
c4e3b2
    At the end of PEI, the DXE IPL PEIM (Initial Program Load PEI Module, see
c4e3b2
    MdeModulePkg/Core/DxeIplPeim) examines the Boot Mode, and if it says "S3
c4e3b2
    resume in progress", then the IPL branches to the PEIM that exports
c4e3b2
    EFI_PEI_S3_RESUME2_PPI (provided by UefiCpuPkg/Universal/Acpi/S3Resume2Pei)
c4e3b2
    rather than loading the DXE core.
c4e3b2
c4e3b2
    S3Resume2Pei executes the technical steps of the resumption, relying on the
c4e3b2
    contents of the LockBox.
c4e3b2
c4e3b2
(4) During first boot (or after a normal platform reset), when DXE does run,
c4e3b2
    hardware drivers in the DXE phase are encouraged to "stash" their hardware
c4e3b2
    configuration steps (eg. accesses to PCI config space, I/O ports, memory
c4e3b2
    mapped addresses, and so on) in a centrally maintained, so called "S3 boot
c4e3b2
    script". Hardware accesses are represented with opcodes of a special binary
c4e3b2
    script language.
c4e3b2
c4e3b2
    This boot script is to be replayed during resume, by S3Resume2Pei. The
c4e3b2
    general goal is to bring back hardware devices -- which have been powered
c4e3b2
    off during suspend -- to their original after-first-boot state, and in
c4e3b2
    particular, to do so quickly.
c4e3b2
c4e3b2
    At the moment, OVMF saves only one opcode in the S3 resume boot script: an
c4e3b2
    INFORMATION opcode, with contents 0xDEADBEEF (in network byte order). The
c4e3b2
    consensus between Linux developers seems to be that boot firmware is only
c4e3b2
    responsible for restoring basic chipset state, which OVMF does during PEI
c4e3b2
    anyway, independently of S3 vs. normal reset. (One example is the power
c4e3b2
    management registers of the i440fx chipset.) Device and peripheral state is
c4e3b2
    the responsibility of the runtime operating system.
c4e3b2
c4e3b2
    Although an experimental OVMF S3 boot script was at one point captured for
c4e3b2
    the virtual Cirrus VGA card, such a boot script cannot follow eg. video
c4e3b2
    mode changes effected by the OS. Hence the operating system can never avoid
c4e3b2
    restoring device state, and most Linux display drivers (eg. stdvga, QXL)
c4e3b2
    already cover S3 resume fully.
c4e3b2
c4e3b2
    The XDDM and WDDM driver models used under Windows OSes seem to recognize
c4e3b2
    this notion of runtime OS responsibility as well. (See the list of OSes
c4e3b2
    supported by OVMF in a separate section.)
c4e3b2
c4e3b2
(5) The S3 suspend/resume data flow in OVMF is included here tersely, for
c4e3b2
    interested developers.
c4e3b2
c4e3b2
    (a) BdsLibBootViaBootOption()
c4e3b2
          EFI_ACPI_S3_SAVE_PROTOCOL [AcpiS3SaveDxe]
c4e3b2
          - saves ACPI S3 Context to LockBox  ---------------------+
c4e3b2
            (including FACS address -- FACS ACPI table             |
c4e3b2
            contains OS waking vector)                             |
c4e3b2
                                                                   |
c4e3b2
          - prepares boot script:                                  |
c4e3b2
            EFI_S3_SAVE_STATE_PROTOCOL.Write() [S3SaveStateDxe]    |
c4e3b2
              S3BootScriptLib [PiDxeS3BootScriptLib]               |
c4e3b2
              - opcodes & arguments are saved in NVS.  --+         |
c4e3b2
                                                         |         |
c4e3b2
          - issues a notification by installing          |         |
c4e3b2
            EFI_DXE_SMM_READY_TO_LOCK_PROTOCOL           |         |
c4e3b2
                                                         |         |
c4e3b2
    (b) EFI_S3_SAVE_STATE_PROTOCOL [S3SaveStateDxe]      |         |
c4e3b2
          S3BootScriptLib [PiDxeS3BootScriptLib]         |         |
c4e3b2
          - closes script with special opcode  <---------+         |
c4e3b2
          - script is available in non-volatile memory             |
c4e3b2
            via PcdS3BootScriptTablePrivateDataPtr  --+            |
c4e3b2
                                                      |            |
c4e3b2
        BootScriptExecutorDxe                         |            |
c4e3b2
          S3BootScriptLib [PiDxeS3BootScriptLib]      |            |
c4e3b2
          - Knows about boot script location by  <----+            |
c4e3b2
            synchronizing with the other library                   |
c4e3b2
            instance via                                           |
c4e3b2
            PcdS3BootScriptTablePrivateDataPtr.                    |
c4e3b2
          - Copies relocated image of itself to                    |
c4e3b2
            reserved memory. --------------------------------+     |
c4e3b2
          - Saved image contains pointer to boot script.  ---|--+  |
c4e3b2
                                                             |  |  |
c4e3b2
    Runtime:                                                 |  |  |
c4e3b2
                                                             |  |  |
c4e3b2
    (c) OS is booted, writes OS waking vector to FACS,       |  |  |
c4e3b2
        suspends machine                                     |  |  |
c4e3b2
                                                             |  |  |
c4e3b2
    S3 Resume (PEI):                                         |  |  |
c4e3b2
                                                             |  |  |
c4e3b2
    (d) PlatformPei sets S3 Boot Mode based on CMOS          |  |  |
c4e3b2
                                                             |  |  |
c4e3b2
    (e) DXE core is skipped and EFI_PEI_S3_RESUME2 is        |  |  |
c4e3b2
        called as last step of PEI                           |  |  |
c4e3b2
                                                             |  |  |
c4e3b2
    (f) S3Resume2Pei retrieves from LockBox:                 |  |  |
c4e3b2
        - ACPI S3 Context (path to FACS)  <------------------|--|--+
c4e3b2
                                          |                  |  |
c4e3b2
                                          +------------------|--|--+
c4e3b2
        - Boot Script Executor Image  <----------------------+  |  |
c4e3b2
                                                                |  |
c4e3b2
    (g) BootScriptExecutorDxe                                   |  |
c4e3b2
          S3BootScriptLib [PiDxeS3BootScriptLib]                |  |
c4e3b2
          - executes boot script  <-----------------------------+  |
c4e3b2
                                                                   |
c4e3b2
    (h) OS waking vector available from ACPI S3 Context / FACS  <--+
c4e3b2
        is called
c4e3b2
c4e3b2
A comprehensive memory map of OVMF
c4e3b2
----------------------------------
c4e3b2
c4e3b2
The following section gives a detailed analysis of memory ranges below 4 GB
c4e3b2
that OVMF statically uses.
c4e3b2
c4e3b2
In the rightmost column, the PCD entry is identified by which the source refers
c4e3b2
to the address or size in question.
c4e3b2
c4e3b2
The flash-covered range has been discussed previously in "Firmware image
c4e3b2
structure", therefore we include it only for completeness. Due to the fact that
c4e3b2
this range is always backed by a memory mapped device (and never RAM), it is
c4e3b2
unaffected by S3 (suspend to RAM and resume).
c4e3b2
c4e3b2
+--------------------------+ 4194304 KB
c4e3b2
|                          |
c4e3b2
|          SECFV           | size: 208 KB
c4e3b2
|                          |
c4e3b2
+--------------------------+ 4194096 KB
c4e3b2
|                          |
c4e3b2
|      FVMAIN_COMPACT      | size: 1712 KB
c4e3b2
|                          |
c4e3b2
+--------------------------+ 4192384 KB
c4e3b2
|                          |
c4e3b2
|      variable store      | size: 64 KB   PcdFlashNvStorageFtwSpareSize
c4e3b2
|        spare area        |
c4e3b2
|                          |
c4e3b2
+--------------------------+ 4192320 KB    PcdOvmfFlashNvStorageFtwSpareBase
c4e3b2
|                          |
c4e3b2
|    FTW working block     | size: 4 KB    PcdFlashNvStorageFtwWorkingSize
c4e3b2
|                          |
c4e3b2
+--------------------------+ 4192316 KB    PcdOvmfFlashNvStorageFtwWorkingBase
c4e3b2
|                          |
c4e3b2
|       Event log of       | size: 4 KB    PcdOvmfFlashNvStorageEventLogSize
c4e3b2
|   non-volatile storage   |
c4e3b2
|                          |
c4e3b2
+--------------------------+ 4192312 KB    PcdOvmfFlashNvStorageEventLogBase
c4e3b2
|                          |
c4e3b2
|      variable store      | size: 56 KB   PcdFlashNvStorageVariableSize
c4e3b2
|                          |
c4e3b2
+--------------------------+ 4192256 KB    PcdOvmfFlashNvStorageVariableBase
c4e3b2
c4e3b2
The flash-mapped image of OVMF.fd covers the entire structure above (2048 KB).
c4e3b2
c4e3b2
When using the split files, the address 4192384 KB
c4e3b2
(PcdOvmfFlashNvStorageFtwSpareBase + PcdFlashNvStorageFtwSpareSize) is the
c4e3b2
boundary between the mapped images of OVMF_VARS.fd (56 KB + 4 KB + 4 KB + 64 KB
c4e3b2
= 128 KB) and OVMF_CODE.fd (1712 KB + 208 KB = 1920 KB).
c4e3b2
c4e3b2
With regard to RAM that is statically used by OVMF, S3 (suspend to RAM and
c4e3b2
resume) complicates matters. Many ranges have been introduced only to support
c4e3b2
S3, hence for all ranges below, the following questions will be audited:
c4e3b2
c4e3b2
(a) when and how a given range is initialized after first boot of the VM,
c4e3b2
(b) how it is protected from memory allocations during DXE,
c4e3b2
(c) how it is protected from the OS,
c4e3b2
(d) how it is accessed on the S3 resume path,
c4e3b2
(e) how it is accessed on the warm reset path.
c4e3b2
c4e3b2
Importantly, the term "protected" is meant as protection against inadvertent
c4e3b2
reallocations and overwrites by co-operating DXE and OS modules. It does not
c4e3b2
imply security against malicious code.
c4e3b2
c4e3b2
+--------------------------+ 17408 KB
c4e3b2
|                          |
c4e3b2
|DXEFV from FVMAIN_COMPACT | size: 8192 KB PcdOvmfDxeMemFvSize
c4e3b2
|  decompressed firmware   |
c4e3b2
| volume with DXE modules  |
c4e3b2
|                          |
c4e3b2
+--------------------------+ 9216 KB       PcdOvmfDxeMemFvBase
c4e3b2
|                          |
c4e3b2
|PEIFV from FVMAIN_COMPACT | size: 896 KB  PcdOvmfPeiMemFvSize
c4e3b2
|  decompressed firmware   |
c4e3b2
| volume with PEI modules  |
c4e3b2
|                          |
c4e3b2
+--------------------------+ 8320 KB       PcdOvmfPeiMemFvBase
c4e3b2
|                          |
c4e3b2
| permanent PEI memory for | size: 32 KB   PcdS3AcpiReservedMemorySize
c4e3b2
|   the S3 resume path     |
c4e3b2
|                          |
c4e3b2
+--------------------------+ 8288 KB       PcdS3AcpiReservedMemoryBase
c4e3b2
|                          |
c4e3b2
|  temporary SEC/PEI heap  | size: 32 KB   PcdOvmfSecPeiTempRamSize
c4e3b2
|         and stack        |
c4e3b2
|                          |
c4e3b2
+--------------------------+ 8256 KB       PcdOvmfSecPeiTempRamBase
c4e3b2
|                          |
c4e3b2
|          unused          | size: 32 KB
c4e3b2
|                          |
c4e3b2
+--------------------------+ 8224 KB
c4e3b2
|                          |
c4e3b2
|      SEC's table of      | size: 4 KB    PcdGuidedExtractHandlerTableSize
c4e3b2
| GUIDed section handlers  |
c4e3b2
|                          |
c4e3b2
+--------------------------+ 8220 KB       PcdGuidedExtractHandlerTableAddress
c4e3b2
|                          |
c4e3b2
|     LockBox storage      | size: 4 KB    PcdOvmfLockBoxStorageSize
c4e3b2
|                          |
c4e3b2
+--------------------------+ 8216 KB       PcdOvmfLockBoxStorageBase
c4e3b2
|                          |
c4e3b2
| early page tables on X64 | size: 24 KB   PcdOvmfSecPageTablesSize
c4e3b2
|                          |
c4e3b2
+--------------------------+ 8192 KB       PcdOvmfSecPageTablesBase
c4e3b2
c4e3b2
(1) Early page tables on X64:
c4e3b2
c4e3b2
  (a) when and how it is initialized after first boot of the VM
c4e3b2
c4e3b2
    The range is filled in during the SEC phase
c4e3b2
    [OvmfPkg/ResetVector/Ia32/PageTables64.asm]. The CR3 register is verified
c4e3b2
    against the base address in SecCoreStartupWithStack()
c4e3b2
    [OvmfPkg/Sec/SecMain.c].
c4e3b2
c4e3b2
  (b) how it is protected from memory allocations during DXE
c4e3b2
c4e3b2
    If S3 was enabled on the QEMU command line (see "-global
c4e3b2
    PIIX4_PM.disable_s3=0" earlier), then InitializeRamRegions()
c4e3b2
    [OvmfPkg/PlatformPei/MemDetect.c] protects the range with an AcpiNVS memory
c4e3b2
    allocation HOB, in PEI.
c4e3b2
c4e3b2
    If S3 was disabled, then this range is not protected. DXE's own page tables
c4e3b2
    are first built while still in PEI (see HandOffToDxeCore()
c4e3b2
    [MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c]). Those tables are located
c4e3b2
    in permanent PEI memory. After CR3 is switched over to them (which occurs
c4e3b2
    before jumping to the DXE core entry point), we don't have to preserve the
c4e3b2
    initial tables.
c4e3b2
c4e3b2
  (c) how it is protected from the OS
c4e3b2
c4e3b2
    If S3 is enabled, then (1b) reserves it from the OS too.
c4e3b2
c4e3b2
    If S3 is disabled, then the range needs no protection.
c4e3b2
c4e3b2
  (d) how it is accessed on the S3 resume path
c4e3b2
c4e3b2
    It is rewritten same as in (1a), which is fine because (1c) reserved it.
c4e3b2
c4e3b2
  (e) how it is accessed on the warm reset path
c4e3b2
c4e3b2
    It is rewritten same as in (1a).
c4e3b2
c4e3b2
(2) LockBox storage:
c4e3b2
c4e3b2
  (a) when and how it is initialized after first boot of the VM
c4e3b2
c4e3b2
    InitializeRamRegions() [OvmfPkg/PlatformPei/MemDetect.c] zeroes out the
c4e3b2
    area during PEI. This is correct but not strictly necessary, since on first
c4e3b2
    boot the area is zero-filled anyway.
c4e3b2
c4e3b2
    The LockBox signature of the area is filled in by the PEI module or DXE
c4e3b2
    driver that has been linked against OVMF's LockBoxLib and is run first. The
c4e3b2
    signature is written in LockBoxLibInitialize()
c4e3b2
    [OvmfPkg/Library/LockBoxLib/LockBoxLib.c].
c4e3b2
c4e3b2
    Any module calling SaveLockBox() [OvmfPkg/Library/LockBoxLib/LockBoxLib.c]
c4e3b2
    will co-populate this area.
c4e3b2
c4e3b2
  (b) how it is protected from memory allocations during DXE
c4e3b2
c4e3b2
    If S3 is enabled, then InitializeRamRegions()
c4e3b2
    [OvmfPkg/PlatformPei/MemDetect.c] protects the range as AcpiNVS.
c4e3b2
c4e3b2
    Otherwise, the range is covered with a BootServicesData memory allocation
c4e3b2
    HOB.
c4e3b2
c4e3b2
  (c) how it is protected from the OS
c4e3b2
c4e3b2
    If S3 is enabled, then (2b) protects it sufficiently.
c4e3b2
c4e3b2
    Otherwise the range requires no runtime protection, and the
c4e3b2
    BootServicesData allocation type from (2b) ensures that the range will be
c4e3b2
    released to the OS.
c4e3b2
c4e3b2
  (d) how it is accessed on the S3 resume path
c4e3b2
c4e3b2
    The S3 Resume PEIM restores data from the LockBox, which has been correctly
c4e3b2
    protected in (2c).
c4e3b2
c4e3b2
  (e) how it is accessed on the warm reset path
c4e3b2
c4e3b2
    InitializeRamRegions() [OvmfPkg/PlatformPei/MemDetect.c] zeroes out the
c4e3b2
    range during PEI, effectively emptying the LockBox. Modules will
c4e3b2
    re-populate the LockBox as described in (2a).
c4e3b2
c4e3b2
(3) SEC's table of GUIDed section handlers
c4e3b2
c4e3b2
  (a) when and how it is initialized after first boot of the VM
c4e3b2
c4e3b2
    The following two library instances are linked into SecMain:
c4e3b2
    - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib,
c4e3b2
    - MdePkg/Library/BaseExtractGuidedSectionLib.
c4e3b2
c4e3b2
    The first library registers its LZMA decompressor plugin (which is a called
c4e3b2
    a "section handler") by calling the second library:
c4e3b2
c4e3b2
    LzmaDecompressLibConstructor() [GuidedSectionExtraction.c]
c4e3b2
      ExtractGuidedSectionRegisterHandlers() [BaseExtractGuidedSectionLib.c]
c4e3b2
c4e3b2
    The second library maintains its table of registered "section handlers", to
c4e3b2
    be indexed by GUID, in this fixed memory area, independently of S3
c4e3b2
    enablement.
c4e3b2
c4e3b2
    (The decompression of FVMAIN_COMPACT's FFS file section that contains the
c4e3b2
    PEIFV and DXEFV firmware volumes occurs with the LZMA decompressor
c4e3b2
    registered above. See (6) and (7) below.)
c4e3b2
c4e3b2
  (b) how it is protected from memory allocations during DXE
c4e3b2
c4e3b2
    There is no need to protect this area from DXE: because nothing else in
c4e3b2
    OVMF links against BaseExtractGuidedSectionLib, the area loses its
c4e3b2
    significance as soon as OVMF progresses from SEC to PEI, therefore DXE is
c4e3b2
    allowed to overwrite the region.
c4e3b2
c4e3b2
  (c) how it is protected from the OS
c4e3b2
c4e3b2
    When S3 is enabled, we cover the range with an AcpiNVS memory allocation
c4e3b2
    HOB in InitializeRamRegions().
c4e3b2
c4e3b2
    When S3 is disabled, the range is not protected.
c4e3b2
c4e3b2
  (d) how it is accessed on the S3 resume path
c4e3b2
c4e3b2
    The table of registered section handlers is again managed by
c4e3b2
    BaseExtractGuidedSectionLib linked into SecMain exclusively. Section
c4e3b2
    handler registrations update the table in-place (based on GUID matches).
c4e3b2
c4e3b2
  (e) how it is accessed on the warm reset path
c4e3b2
c4e3b2
    If S3 is enabled, then the OS won't damage the table (due to (3c)), thus
c4e3b2
    see (3d).
c4e3b2
c4e3b2
    If S3 is disabled, then the OS has most probably overwritten the range with
c4e3b2
    its own data, hence (3a) -- complete reinitialization -- will come into
c4e3b2
    effect, based on the table signature check in BaseExtractGuidedSectionLib.
c4e3b2
c4e3b2
(4) temporary SEC/PEI heap and stack
c4e3b2
c4e3b2
  (a) when and how it is initialized after first boot of the VM
c4e3b2
c4e3b2
    The range is configured in [OvmfPkg/Sec/X64/SecEntry.S] and
c4e3b2
    SecCoreStartupWithStack() [OvmfPkg/Sec/SecMain.c]. The stack half is read &
c4e3b2
    written by the CPU transparently. The heap half is used for memory
c4e3b2
    allocations during PEI.
c4e3b2
c4e3b2
    Data is migrated out (to permanent PEI stack & memory) in (or soon after)
c4e3b2
    PublishPeiMemory() [OvmfPkg/PlatformPei/MemDetect.c].
c4e3b2
c4e3b2
  (b) how it is protected from memory allocations during DXE
c4e3b2
c4e3b2
    It is not necessary to protect this range during DXE because its use ends
c4e3b2
    still in PEI.
c4e3b2
c4e3b2
  (c) how it is protected from the OS
c4e3b2
c4e3b2
    If S3 is enabled, then InitializeRamRegions()
c4e3b2
    [OvmfPkg/PlatformPei/MemDetect.c] reserves it as AcpiNVS.
c4e3b2
c4e3b2
    If S3 is disabled, then the range doesn't require protection.
c4e3b2
c4e3b2
  (d) how it is accessed on the S3 resume path
c4e3b2
c4e3b2
    Same as in (4a), except the target area of the migration triggered by
c4e3b2
    PublishPeiMemory() [OvmfPkg/PlatformPei/MemDetect.c] is different -- see
c4e3b2
    (5).
c4e3b2
c4e3b2
  (e) how it is accessed on the warm reset path
c4e3b2
c4e3b2
    Same as in (4a). The stack and heap halves both may contain garbage, but it
c4e3b2
    doesn't matter.
c4e3b2
c4e3b2
(5) permanent PEI memory for the S3 resume path
c4e3b2
c4e3b2
  (a) when and how it is initialized after first boot of the VM
c4e3b2
c4e3b2
    No particular initialization or use.
c4e3b2
c4e3b2
  (b) how it is protected from memory allocations during DXE
c4e3b2
c4e3b2
    We don't need to protect this area during DXE.
c4e3b2
c4e3b2
  (c) how it is protected from the OS
c4e3b2
c4e3b2
    When S3 is enabled, InitializeRamRegions()
c4e3b2
    [OvmfPkg/PlatformPei/MemDetect.c] makes sure the OS stays away by covering
c4e3b2
    the range with an AcpiNVS memory allocation HOB.
c4e3b2
c4e3b2
    When S3 is disabled, the range needs no protection.
c4e3b2
c4e3b2
  (d) how it is accessed on the S3 resume path
c4e3b2
c4e3b2
    PublishPeiMemory() installs the range as permanent RAM for PEI. The range
c4e3b2
    will serve as stack and will satisfy allocation requests during the rest of
c4e3b2
    PEI. OS data won't overlap due to (5c).
c4e3b2
c4e3b2
  (e) how it is accessed on the warm reset path
c4e3b2
c4e3b2
    Same as (5a).
c4e3b2
c4e3b2
(6) PEIFV -- decompressed firmware volume with PEI modules
c4e3b2
c4e3b2
  (a) when and how it is initialized after first boot of the VM
c4e3b2
c4e3b2
    DecompressMemFvs() [OvmfPkg/Sec/SecMain.c] populates the area, by
c4e3b2
    decompressing the flash-mapped FVMAIN_COMPACT volume's contents. (Refer to
c4e3b2
    "Firmware image structure".)
c4e3b2
c4e3b2
  (b) how it is protected from memory allocations during DXE
c4e3b2
c4e3b2
    When S3 is disabled, PeiFvInitialization() [OvmfPkg/PlatformPei/Fv.c]
c4e3b2
    covers the range with a BootServicesData memory allocation HOB.
c4e3b2
c4e3b2
    When S3 is enabled, the same is coverage is ensured, just with the stronger
c4e3b2
    AcpiNVS memory allocation type.
c4e3b2
c4e3b2
  (c) how it is protected from the OS
c4e3b2
c4e3b2
    When S3 is disabled, it is not necessary to keep the range from the OS.
c4e3b2
c4e3b2
    Otherwise the AcpiNVS type allocation from (6b) provides coverage.
c4e3b2
c4e3b2
  (d) how it is accessed on the S3 resume path
c4e3b2
c4e3b2
    Rather than decompressing it again from FVMAIN_COMPACT, GetS3ResumePeiFv()
c4e3b2
    [OvmfPkg/Sec/SecMain.c] reuses the protected area for parsing / execution
c4e3b2
    from (6c).
c4e3b2
c4e3b2
  (e) how it is accessed on the warm reset path
c4e3b2
c4e3b2
    Same as (6a).
c4e3b2
c4e3b2
(7) DXEFV -- decompressed firmware volume with DXE modules
c4e3b2
c4e3b2
  (a) when and how it is initialized after first boot of the VM
c4e3b2
c4e3b2
    Same as (6a).
c4e3b2
c4e3b2
  (b) how it is protected from memory allocations during DXE
c4e3b2
c4e3b2
    PeiFvInitialization() [OvmfPkg/PlatformPei/Fv.c] covers the range with a
c4e3b2
    BootServicesData memory allocation HOB.
c4e3b2
c4e3b2
  (c) how it is protected from the OS
c4e3b2
c4e3b2
    The OS is allowed to release and reuse this range.
c4e3b2
c4e3b2
  (d) how it is accessed on the S3 resume path
c4e3b2
c4e3b2
    It's not; DXE never runs during S3 resume.
c4e3b2
c4e3b2
  (e) how it is accessed on the warm reset path
c4e3b2
c4e3b2
    Same as in (7a).
c4e3b2
c4e3b2
Known Secure Boot limitations
c4e3b2
-----------------------------
c4e3b2
c4e3b2
Under "Motivation" we've mentioned that OVMF's Secure Boot implementation is
c4e3b2
not suitable for production use yet -- it's only good for development and
c4e3b2
testing of standards-conformant, non-malicious guest code (UEFI and operating
c4e3b2
system alike).
c4e3b2
c4e3b2
Now that we've examined the persistent flash device, the workings of S3, and
c4e3b2
the memory map, we can discuss two currently known shortcomings of OVMF's
c4e3b2
Secure Boot that in fact make it insecure. (Clearly problems other than these
c4e3b2
two might exist; the set of issues considered here is not meant to be
c4e3b2
exhaustive.)
c4e3b2
c4e3b2
One trait of Secure Boot is tamper-evidence. Secure Boot may not prevent
c4e3b2
malicious modification of software components (for example, operating system
c4e3b2
drivers), but by being the root of integrity on a platform, it can catch (or
c4e3b2
indirectly contribute to catching) unauthorized changes, by way of signature
c4e3b2
and certificate checks at the earliest phases of boot.
c4e3b2
c4e3b2
If an attacker can tamper with key material stored in authenticated and/or
c4e3b2
boot-time only persistent variables (for example, PK, KEK, db, dbt, dbx), then
c4e3b2
the intended security of this scheme is compromised. The UEFI 2.4A
c4e3b2
specification says
c4e3b2
c4e3b2
- in section 28.3.4:
c4e3b2
c4e3b2
  Platform Keys:
c4e3b2
c4e3b2
    The public key must be stored in non-volatile storage which is tamper and
c4e3b2
    delete resistant.
c4e3b2
c4e3b2
  Key Exchange Keys:
c4e3b2
c4e3b2
    The public key must be stored in non-volatile storage which is tamper
c4e3b2
    resistant.
c4e3b2
c4e3b2
- in section 28.6.1:
c4e3b2
c4e3b2
  The signature database variables db, dbt, and dbx must be stored in
c4e3b2
  tamper-resistant non-volatile storage.
c4e3b2
c4e3b2
(1) The combination of QEMU, KVM, and OVMF does not provide this kind of
c4e3b2
    resistance. The variable store in the emulated flash chip is directly
c4e3b2
    accessible to, and reprogrammable by, UEFI drivers, applications, and
c4e3b2
    operating systems.
c4e3b2
c4e3b2
(2) Under "S3 (suspend to RAM and resume)" we pointed out that the LockBox
c4e3b2
    storage must be similarly secure and tamper-resistant.
c4e3b2
c4e3b2
    On the S3 resume path, the PEIM providing EFI_PEI_S3_RESUME2_PPI
c4e3b2
    (UefiCpuPkg/Universal/Acpi/S3Resume2Pei) restores and interprets data from
c4e3b2
    the LockBox that has been saved there during boot. This PEIM, being part of
c4e3b2
    the firmware, has full access to the platform. If an operating system can
c4e3b2
    tamper with the contents of the LockBox, then at the next resume the
c4e3b2
    platform's integrity might be subverted.
c4e3b2
c4e3b2
    OVMF stores the LockBox in normal guest RAM (refer to the memory map
c4e3b2
    section above). Operating systems and third party UEFI drivers and UEFI
c4e3b2
    applications that respect the UEFI memory map will not inadvertently
c4e3b2
    overwrite the LockBox storage, but there's nothing to prevent eg. a
c4e3b2
    malicious kernel from modifying the LockBox.
c4e3b2
c4e3b2
One means to address these issues is SMM and SMRAM (System Management Mode and
c4e3b2
System Management RAM).
c4e3b2
c4e3b2
During boot and resume, the firmware can enter and leave SMM and access SMRAM.
c4e3b2
Before the DXE phase is left, and control is transferred to the BDS phase (when
c4e3b2
third party UEFI drivers and applications can be loaded, and an operating
c4e3b2
system can be loaded), SMRAM is locked in hardware, and subsequent modules
c4e3b2
cannot access it directly. (See EFI_DXE_SMM_READY_TO_LOCK_PROTOCOL.)
c4e3b2
c4e3b2
Once SMRAM has been locked, UEFI drivers and the operating system can enter SMM
c4e3b2
by raising a System Management Interrupt (SMI), at which point trusted code
c4e3b2
(part of the platform firmware) takes control. SMRAM is also unlocked by
c4e3b2
platform reset, at which point the boot firmware takes control again.
c4e3b2
c4e3b2
Variable store and LockBox in SMRAM
c4e3b2
-----------------------------------
c4e3b2
c4e3b2
Edk2 provides almost all components to implement the variable store and the
c4e3b2
LockBox in SMRAM. In this section we summarize ideas for utilizing those
c4e3b2
facilities.
c4e3b2
c4e3b2
The SMRAM and SMM infrastructure in edk2 is built up as follows:
c4e3b2
c4e3b2
(1) The platform hardware provides SMM / SMI / SMRAM.
c4e3b2
c4e3b2
    Qemu/KVM doesn't support these features currently and should implement them
c4e3b2
    in the longer term.
c4e3b2
c4e3b2
(2) The platform vendor (in this case, OVMF developers) implement device
c4e3b2
    drivers for the platform's System Management Mode:
c4e3b2
c4e3b2
    - EFI_SMM_CONTROL2_PROTOCOL: for raising a synchronous (and/or) periodic
c4e3b2
      SMI(s); that is, for entering SMM.
c4e3b2
c4e3b2
    - EFI_SMM_ACCESS2_PROTOCOL: for describing and accessing SMRAM.
c4e3b2
c4e3b2
    These protocols are documented in the PI Specification, Volume 4.
c4e3b2
c4e3b2
(3) The platform DSC file is to include the following platform-independent
c4e3b2
    modules:
c4e3b2
c4e3b2
    - MdeModulePkg/Core/PiSmmCore/PiSmmIpl.inf: SMM Initial Program Load
c4e3b2
    - MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf: SMM Core
c4e3b2
c4e3b2
(4) At this point, modules of type DXE_SMM_DRIVER can be loaded.
c4e3b2
c4e3b2
    Such drivers are privileged. They run in SMM, have access to SMRAM, and are
c4e3b2
    separated and switched from other drivers through SMIs. Secure
c4e3b2
    communication between unprivileged (non-SMM) and privileged (SMM) drivers
c4e3b2
    happens through EFI_SMM_COMMUNICATION_PROTOCOL (implemented by the SMM
c4e3b2
    Core, see (3)).
c4e3b2
c4e3b2
    DXE_SMM_DRIVER modules must sanitize their input (coming from unprivileged
c4e3b2
    drivers) carefully.
c4e3b2
c4e3b2
(5) The authenticated runtime variable services driver (for Secure Boot builds)
c4e3b2
    is located under "SecurityPkg/VariableAuthenticated/RuntimeDxe". OVMF
c4e3b2
    currently builds the driver (a DXE_RUNTIME_DRIVER module) with the
c4e3b2
    "VariableRuntimeDxe.inf" control file (refer to "OvmfPkg/OvmfPkgX64.dsc"),
c4e3b2
    which does not use SMM.
c4e3b2
c4e3b2
    The directory includes two more INF files:
c4e3b2
c4e3b2
    - VariableSmm.inf -- module type: DXE_SMM_DRIVER. A privileged driver that
c4e3b2
      runs in SMM and has access to SMRAM.
c4e3b2
c4e3b2
    - VariableSmmRuntimeDxe.inf -- module type: DXE_RUNTIME_DRIVER. A
c4e3b2
      non-privileged driver that implements the variable runtime services
c4e3b2
      (replacing the current "VariableRuntimeDxe.inf" file) by communicating
c4e3b2
      with the above privileged SMM half via EFI_SMM_COMMUNICATION_PROTOCOL.
c4e3b2
c4e3b2
(6) An SMRAM-based LockBox implementation needs to be discussed in two parts,
c4e3b2
    because the LockBox is accessed in both PEI and DXE.
c4e3b2
c4e3b2
    (a) During DXE, drivers save data in the LockBox. A save operation is
c4e3b2
        layered as follows:
c4e3b2
c4e3b2
        - The unprivileged driver wishing to store data in the LockBox links
c4e3b2
          against the "MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf"
c4e3b2
          library instance.
c4e3b2
c4e3b2
          The library allows the unprivileged driver to format requests for the
c4e3b2
          privileged SMM LockBox driver (see below), and to parse responses.
c4e3b2
c4e3b2
        - The privileged SMM LockBox driver is built from
c4e3b2
          "MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf". This
c4e3b2
          driver has module type DXE_SMM_DRIVER and can access SMRAM.
c4e3b2
c4e3b2
          The driver delegates command parsing and response formatting to
c4e3b2
          "MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxSmmLib.inf".
c4e3b2
c4e3b2
        - The above two halves (unprivileged and privileged) mirror what we've
c4e3b2
          seen in case of the variable service drivers, under (5).
c4e3b2
c4e3b2
    (b) In PEI, the S3 Resume PEIM (UefiCpuPkg/Universal/Acpi/S3Resume2Pei)
c4e3b2
        retrieves data from the LockBox.
c4e3b2
c4e3b2
        Presumably, S3Resume2Pei should be considered an "unprivileged PEIM",
c4e3b2
        and the SMRAM access should be layered as seen in DXE. Unfortunately,
c4e3b2
        edk2 does not implement all of the layers in PEI -- the code either
c4e3b2
        doesn't exist, or it is not open source:
c4e3b2
c4e3b2
  role         | DXE: protocol/module           | PEI: PPI/module
c4e3b2
  -------------+--------------------------------+------------------------------
c4e3b2
  unprivileged | any                            | S3Resume2Pei.inf
c4e3b2
  driver       |                                |
c4e3b2
  -------------+--------------------------------+------------------------------
c4e3b2
  command      | LIBRARY_CLASS = LockBoxLib     | LIBRARY_CLASS = LockBoxLib
c4e3b2
  formatting   |                                |
c4e3b2
  and response | SmmLockBoxDxeLib.inf           | SmmLockBoxPeiLib.inf
c4e3b2
  parsing      |                                |
c4e3b2
  -------------+--------------------------------+------------------------------
c4e3b2
  privilege    | EFI_SMM_COMMUNICATION_PROTOCOL | EFI_PEI_SMM_COMMUNICATION_PPI
c4e3b2
  separation   |                                |
c4e3b2
               | PiSmmCore.inf                  | missing!
c4e3b2
  -------------+--------------------------------+------------------------------
c4e3b2
  platform SMM | EFI_SMM_CONTROL2_PROTOCOL      | PEI_SMM_CONTROL_PPI
c4e3b2
  and SMRAM    | EFI_SMM_ACCESS2_PROTOCOL       | PEI_SMM_ACCESS_PPI
c4e3b2
  access       |                                |
c4e3b2
               | to be done in OVMF             | to be done in OVMF
c4e3b2
  -------------+--------------------------------+------------------------------
c4e3b2
  command      | LIBRARY_CLASS = LockBoxLib     | LIBRARY_CLASS = LockBoxLib
c4e3b2
  parsing and  |                                |
c4e3b2
  response     | SmmLockBoxSmmLib.inf           | missing!
c4e3b2
  formatting   |                                |
c4e3b2
  -------------+--------------------------------+------------------------------
c4e3b2
  privileged   | SmmLockBox.inf                 | missing!
c4e3b2
  LockBox      |                                |
c4e3b2
  driver       |                                |
c4e3b2
c4e3b2
        Alternatively, in the future OVMF might be able to provide a LockBoxLib
c4e3b2
        instance (an SmmLockBoxPeiLib substitute) for S3Resume2Pei that
c4e3b2
        accesses SMRAM directly, eliminating the need for deeper layers in the
c4e3b2
        stack (that is, EFI_PEI_SMM_COMMUNICATION_PPI and deeper).
c4e3b2
c4e3b2
        In fact, a "thin" EFI_PEI_SMM_COMMUNICATION_PPI implementation whose
c4e3b2
        sole Communicate() member invariably returns EFI_NOT_STARTED would
c4e3b2
        cause the current SmmLockBoxPeiLib library instance to directly perform
c4e3b2
        full-depth SMRAM access and LockBox search, obviating the "missing"
c4e3b2
        cells. (With reference to A Tour Beyond BIOS: Implementing S3 Resume
c4e3b2
        with EDK2, by Jiewen Yao and Vincent Zimmer, October 2014.)
c4e3b2
c4e3b2
Select features
c4e3b2
---------------
c4e3b2
c4e3b2
In this section we'll browse the top-level "OvmfPkg" package directory, and
c4e3b2
discuss the more interesting drivers and libraries that have not been mentioned
c4e3b2
thus far.
c4e3b2
c4e3b2
X64-specific reset vector for OVMF
c4e3b2
..................................
c4e3b2
c4e3b2
The "OvmfPkg/ResetVector" directory customizes the reset vector (found in
c4e3b2
"UefiCpuPkg/ResetVector/Vtf0") for "OvmfPkgX64.fdf", that is, when the SEC/PEI
c4e3b2
phases run in 64-bit (ie. long) mode.
c4e3b2
c4e3b2
The reset vector's control flow looks roughly like:
c4e3b2
c4e3b2
  resetVector                               [Ia16/ResetVectorVtf0.asm]
c4e3b2
  EarlyBspInitReal16                        [Ia16/Init16.asm]
c4e3b2
  Main16                                    [Main.asm]
c4e3b2
    EarlyInit16                             [Ia16/Init16.asm]
c4e3b2
c4e3b2
    ; Transition the processor from
c4e3b2
    ; 16-bit real mode to 32-bit flat mode
c4e3b2
    TransitionFromReal16To32BitFlat         [Ia16/Real16ToFlat32.asm]
c4e3b2
c4e3b2
    ; Search for the
c4e3b2
    ; Boot Firmware Volume (BFV)
c4e3b2
    Flat32SearchForBfvBase                  [Ia32/SearchForBfvBase.asm]
c4e3b2
c4e3b2
    ; Search for the SEC entry point
c4e3b2
    Flat32SearchForSecEntryPoint            [Ia32/SearchForSecEntry.asm]
c4e3b2
c4e3b2
    %ifdef ARCH_IA32
c4e3b2
      ; Jump to the 32-bit SEC entry point
c4e3b2
    %else
c4e3b2
      ; Transition the processor
c4e3b2
      ; from 32-bit flat mode
c4e3b2
      ; to 64-bit flat mode
c4e3b2
      Transition32FlatTo64Flat              [Ia32/Flat32ToFlat64.asm]
c4e3b2
c4e3b2
        SetCr3ForPageTables64               [Ia32/PageTables64.asm]
c4e3b2
          ; set CR3 to page tables
c4e3b2
          ; built into the ROM image
c4e3b2
c4e3b2
        ; enable PAE
c4e3b2
        ; set LME
c4e3b2
        ; enable paging
c4e3b2
c4e3b2
      ; Jump to the 64-bit SEC entry point
c4e3b2
    %endif
c4e3b2
c4e3b2
On physical platforms, the initial page tables referenced by
c4e3b2
SetCr3ForPageTables64 are built statically into the flash device image, and are
c4e3b2
present in ROM at runtime. This is fine on physical platforms because the
c4e3b2
pre-built page table entries have the Accessed and Dirty bits set from the
c4e3b2
start.
c4e3b2
c4e3b2
Accordingly, for OVMF running in long mode on qemu/KVM, the initial page tables
c4e3b2
were mapped as a KVM_MEM_READONLY slot, as part of QEMU's pflash device (refer
c4e3b2
to "Firmware image structure" above).
c4e3b2
c4e3b2
In spite of the Accessed and Dirty bits being pre-set in the read-only,
c4e3b2
in-flash PTEs, in a virtual machine attempts are made to update said PTE bits,
c4e3b2
differently from physical hardware. The component attempting to update the
c4e3b2
read-only PTEs can be one of the following:
c4e3b2
c4e3b2
- The processor itself, if it supports nested paging, and the user enables that
c4e3b2
  processor feature,
c4e3b2
c4e3b2
- KVM code implementing shadow paging, otherwise.
c4e3b2
c4e3b2
The first case presents no user-visible symptoms, but the second case (KVM,
c4e3b2
shadow paging) used to cause a triple fault, prior to Linux commit ba6a354
c4e3b2
("KVM: mmu: allow page tables to be in read-only slots").
c4e3b2
c4e3b2
For compatibility with earlier KVM versions, the OvmfPkg/ResetVector directory
c4e3b2
adapts the generic reset vector code as follows:
c4e3b2
c4e3b2
      Transition32FlatTo64Flat         [UefiCpuPkg/.../Ia32/Flat32ToFlat64.asm]
c4e3b2
c4e3b2
        SetCr3ForPageTables64       [OvmfPkg/ResetVector/Ia32/PageTables64.asm]
c4e3b2
c4e3b2
          ; dynamically build the initial page tables in RAM, at address
c4e3b2
          ; PcdOvmfSecPageTablesBase (refer to the memory map above),
c4e3b2
          ; identity-mapping the first 4 GB of address space
c4e3b2
c4e3b2
          ; set CR3 to PcdOvmfSecPageTablesBase
c4e3b2
c4e3b2
        ; enable PAE
c4e3b2
        ; set LME
c4e3b2
        ; enable paging
c4e3b2
c4e3b2
This way the PTEs that earlier KVM versions try to update (during shadow
c4e3b2
paging) are located in a read-write memory slot, and the write attempts
c4e3b2
succeed.
c4e3b2
c4e3b2
Client library for QEMU's firmware configuration interface
c4e3b2
..........................................................
c4e3b2
c4e3b2
QEMU provides a write-only, 16-bit wide control port, and a read-write, 8-bit
c4e3b2
wide data port for exchanging configuration elements with the firmware.
c4e3b2
c4e3b2
The firmware writes a selector (a key) to the control port (0x510), and then
c4e3b2
reads the corresponding configuration data (produced by QEMU) from the data
c4e3b2
port (0x511).
c4e3b2
c4e3b2
If the selected entry is writable, the firmware may overwrite it. If QEMU has
c4e3b2
associated a callback with the entry, then when the entry is completely
c4e3b2
rewritten, QEMU runs the callback. (OVMF does not rewrite any entries at the
c4e3b2
moment.)
c4e3b2
c4e3b2
A number of selector values (keys) are predefined. In particular, key 0x19
c4e3b2
selects (returns) a directory of { name, selector, size } triplets, roughly
c4e3b2
speaking.
c4e3b2
c4e3b2
The firmware can request configuration elements by well-known name as well, by
c4e3b2
looking up the selector value first in the directory, by name, and then writing
c4e3b2
the selector to the control port. The number of bytes to read subsequently from
c4e3b2
the data port is known from the directory entry's "size" field.
c4e3b2
c4e3b2
By convention, directory entries (well-known symbolic names of configuration
c4e3b2
elements) are formatted as POSIX pathnames. For example, the array selected by
c4e3b2
the "etc/system-states" name indicates (among other things) whether the user
c4e3b2
enabled S3 support in QEMU.
c4e3b2
c4e3b2
The above interface is called "fw_cfg".
c4e3b2
c4e3b2
The binary data associated with a symbolic name is called an "fw_cfg file".
c4e3b2
c4e3b2
OVMF's fw_cfg client library is found in "OvmfPkg/Library/QemuFwCfgLib". OVMF
c4e3b2
discovers many aspects of the virtual system with it; we refer to a few
c4e3b2
examples below.
c4e3b2
c4e3b2
Guest ACPI tables
c4e3b2
.................
c4e3b2
c4e3b2
An operating system discovers a good amount of its hardware by parsing ACPI
c4e3b2
tables, and by interpreting ACPI objects and methods. On physical hardware, the
c4e3b2
platform vendor's firmware installs ACPI tables in memory that match both the
c4e3b2
hardware present in the system and the user's firmware configuration ("BIOS
c4e3b2
setup").
c4e3b2
c4e3b2
Under qemu/KVM, the owner of the (virtual) hardware configuration is QEMU.
c4e3b2
Hardware can easily be reconfigured on the command line. Furthermore, features
c4e3b2
like CPU hotplug, PCI hotplug, memory hotplug are continuously developed for
c4e3b2
QEMU, and operating systems need direct ACPI support to exploit these features.
c4e3b2
c4e3b2
For this reason, QEMU builds its own ACPI tables dynamically, in a
c4e3b2
self-descriptive manner, and exports them to the firmware through a complex,
c4e3b2
multi-file fw_cfg interface. It is rooted in the "etc/table-loader" fw_cfg
c4e3b2
file. (Further details of this interface are out of scope for this report.)
c4e3b2
c4e3b2
OVMF's AcpiPlatformDxe driver fetches the ACPI tables, and installs them for
c4e3b2
the guest OS with the EFI_ACPI_TABLE_PROTOCOL (which is in turn provided by the
c4e3b2
generic "MdeModulePkg/Universal/Acpi/AcpiTableDxe" driver).
c4e3b2
c4e3b2
For earlier QEMU versions and machine types (which we generally don't recommend
c4e3b2
for OVMF; see "Scope"), the "OvmfPkg/AcpiTables" directory contains a few
c4e3b2
static ACPI table templates. When the "etc/table-loader" fw_cfg file is
c4e3b2
unavailable, AcpiPlatformDxe installs these default tables (with a little bit
c4e3b2
of dynamic patching).
c4e3b2
c4e3b2
When OVMF runs in a Xen domU, AcpiTableDxe also installs ACPI tables that
c4e3b2
originate from the hypervisor's environment.
c4e3b2
c4e3b2
Guest SMBIOS tables
c4e3b2
...................
c4e3b2
c4e3b2
Quoting the SMBIOS Reference Specification,
c4e3b2
c4e3b2
  [...] the System Management BIOS Reference Specification addresses how
c4e3b2
  motherboard and system vendors present management information about their
c4e3b2
  products in a standard format [...]
c4e3b2
c4e3b2
In practice SMBIOS tables are just another set of tables that the platform
c4e3b2
vendor's firmware installs in RAM for the operating system, and, importantly,
c4e3b2
for management applications running on the OS. Without rehashing the "Guest
c4e3b2
ACPI tables" section in full, let's map the OVMF roles seen there from ACPI to
c4e3b2
SMBIOS:
c4e3b2
c4e3b2
  role                     | ACPI                    | SMBIOS
c4e3b2
  -------------------------+-------------------------+-------------------------
c4e3b2
  fw_cfg file              | etc/table-loader        | etc/smbios/smbios-tables
c4e3b2
  -------------------------+-------------------------+-------------------------
c4e3b2
  OVMF driver              | AcpiPlatformDxe         | SmbiosPlatformDxe
c4e3b2
  under "OvmfPkg"          |                         |
c4e3b2
  -------------------------+-------------------------+-------------------------
c4e3b2
  Underlying protocol,     | EFI_ACPI_TABLE_PROTOCOL | EFI_SMBIOS_PROTOCOL
c4e3b2
  implemented by generic   |                         |
c4e3b2
  driver under             | Acpi/AcpiTableDxe       | SmbiosDxe
c4e3b2
  "MdeModulePkg/Universal" |                         |
c4e3b2
  -------------------------+-------------------------+-------------------------
c4e3b2
  default tables available | yes                     | [RHEL] yes, Type0 and
c4e3b2
  for earlier QEMU machine |                         |        Type1 tables
c4e3b2
  types, with hot-patching |                         |
c4e3b2
  -------------------------+-------------------------+-------------------------
c4e3b2
  tables fetched in Xen    | yes                     | yes
c4e3b2
  domUs                    |                         |
c4e3b2
c4e3b2
Platform-specific boot policy
c4e3b2
.............................
c4e3b2
c4e3b2
OVMF's BDS (Boot Device Selection) phase is implemented by
c4e3b2
IntelFrameworkModulePkg/Universal/BdsDxe. Roughly speaking, this large driver:
c4e3b2
c4e3b2
- provides the EFI BDS architectural protocol (which DXE transfers control to
c4e3b2
  after dispatching all DXE drivers),
c4e3b2
c4e3b2
- connects drivers to devices,
c4e3b2
c4e3b2
- enumerates boot devices,
c4e3b2
c4e3b2
- auto-generates boot options,
c4e3b2
c4e3b2
- provides "BIOS setup" screens, such as:
c4e3b2
c4e3b2
  - Boot Manager, for booting an option,
c4e3b2
c4e3b2
  - Boot Maintenance Manager, for adding, deleting, and reordering boot
c4e3b2
    options, changing console properties etc,
c4e3b2
c4e3b2
  - Device Manager, where devices can register configuration forms, including
c4e3b2
c4e3b2
    - Secure Boot configuration forms,
c4e3b2
c4e3b2
    - OVMF's Platform Driver form (see under PlatformDxe).
c4e3b2
c4e3b2
Firmware that includes the "IntelFrameworkModulePkg/Universal/BdsDxe" driver
c4e3b2
can customize its behavior by providing an instance of the PlatformBdsLib
c4e3b2
library class. The driver links against this platform library, and the
c4e3b2
platform library can call Intel's BDS utility functions from
c4e3b2
"IntelFrameworkModulePkg/Library/GenericBdsLib".
c4e3b2
c4e3b2
OVMF's PlatformBdsLib instance can be found in
c4e3b2
"OvmfPkg/Library/PlatformBdsLib". The main function where the BdsDxe driver
c4e3b2
enters the library is PlatformBdsPolicyBehavior(). We mention two OVMF
c4e3b2
particulars here.
c4e3b2
c4e3b2
(1) OVMF is capable of loading kernel images directly from fw_cfg, matching
c4e3b2
    QEMU's -kernel, -initrd, and -append command line options. This feature is
c4e3b2
    useful for rapid, repeated Linux kernel testing, and is implemented in the
c4e3b2
    following call tree:
c4e3b2
c4e3b2
    PlatformBdsPolicyBehavior() [OvmfPkg/Library/PlatformBdsLib/BdsPlatform.c]
c4e3b2
      TryRunningQemuKernel() [OvmfPkg/Library/PlatformBdsLib/QemuKernel.c]
c4e3b2
        LoadLinux*() [OvmfPkg/Library/LoadLinuxLib/Linux.c]
c4e3b2
c4e3b2
    OvmfPkg/Library/LoadLinuxLib ports the efilinux bootloader project into
c4e3b2
    OvmfPkg.
c4e3b2
c4e3b2
(2) OVMF seeks to comply with the boot order specification passed down by QEMU
c4e3b2
    over fw_cfg.
c4e3b2
c4e3b2
    (a) About Boot Modes
c4e3b2
c4e3b2
      During the PEI phase, OVMF determines and stores the Boot Mode in the
c4e3b2
      PHIT HOB (already mentioned in "S3 (suspend to RAM and resume)"). The
c4e3b2
      boot mode is supposed to influence the rest of the system, for example it
c4e3b2
      distinguishes S3 resume (BOOT_ON_S3_RESUME) from a "normal" boot.
c4e3b2
c4e3b2
      In general, "normal" boots can be further differentiated from each other;
c4e3b2
      for example for speed reasons. When the firmware can tell during PEI that
c4e3b2
      the chassis has not been opened since last power-up, then it might want
c4e3b2
      to save time by not connecting all devices and not enumerating all boot
c4e3b2
      options from scratch; it could just rely on the stored results of the
c4e3b2
      last enumeration. The matching BootMode value, to be set during PEI,
c4e3b2
      would be BOOT_ASSUMING_NO_CONFIGURATION_CHANGES.
c4e3b2
c4e3b2
      OVMF only sets one of the following two boot modes, based on CMOS
c4e3b2
      contents:
c4e3b2
      - BOOT_ON_S3_RESUME,
c4e3b2
      - BOOT_WITH_FULL_CONFIGURATION.
c4e3b2
c4e3b2
      For BOOT_ON_S3_RESUME, please refer to "S3 (suspend to RAM and resume)".
c4e3b2
      The other boot mode supported by OVMF, BOOT_WITH_FULL_CONFIGURATION, is
c4e3b2
      an appropriate "catch-all" for a virtual machine, where hardware can
c4e3b2
      easily change from boot to boot.
c4e3b2
c4e3b2
    (b) Auto-generation of boot options
c4e3b2
c4e3b2
      Accordingly, when not resuming from S3 sleep (*), OVMF always connects
c4e3b2
      all devices, and enumerates all bootable devices as new boot options
c4e3b2
      (non-volatile variables called Boot####).
c4e3b2
c4e3b2
      (*) During S3 resume, DXE is not reached, hence BDS isn't either.
c4e3b2
c4e3b2
      The auto-enumerated boot options are stored in the BootOrder non-volatile
c4e3b2
      variable after any preexistent options. (Boot options may exist before
c4e3b2
      auto-enumeration eg. because the user added them manually with the Boot
c4e3b2
      Maintenance Manager or the efibootmgr utility. They could also originate
c4e3b2
      from an earlier auto-enumeration.)
c4e3b2
c4e3b2
      PlatformBdsPolicyBehavior()                   [OvmfPkg/.../BdsPlatform.c]
c4e3b2
        TryRunningQemuKernel()                       [OvmfPkg/.../QemuKernel.c]
c4e3b2
        BdsLibConnectAll()           [IntelFrameworkModulePkg/.../BdsConnect.c]
c4e3b2
        BdsLibEnumerateAllBootOption()  [IntelFrameworkModulePkg/.../BdsBoot.c]
c4e3b2
          BdsLibBuildOptionFromHandle() [IntelFrameworkModulePkg/.../BdsBoot.c]
c4e3b2
            BdsLibRegisterNewOption()   [IntelFrameworkModulePkg/.../BdsMisc.c]
c4e3b2
              //
c4e3b2
              // Append the new option number to the original option order
c4e3b2
              //
c4e3b2
c4e3b2
    (c) Relative UEFI device paths in boot options
c4e3b2
c4e3b2
      The handling of relative ("short-form") UEFI device paths is best
c4e3b2
      demonstrated through an example, and by quoting the UEFI 2.4A
c4e3b2
      specification.
c4e3b2
c4e3b2
      A short-form hard drive UEFI device path could be (displaying each device
c4e3b2
      path node on a separate line for readability):
c4e3b2
c4e3b2
        HD(1,GPT,14DD1CC5-D576-4BBF-8858-BAF877C8DF61,0x800,0x64000)/
c4e3b2
        \EFI\fedora\shim.efi
c4e3b2
c4e3b2
      This device path lacks prefix nodes (eg. hardware or messaging type
c4e3b2
      nodes) that would lead to the hard drive. During load option processing,
c4e3b2
      the above short-form or relative device path could be matched against the
c4e3b2
      following absolute device path:
c4e3b2
c4e3b2
        PciRoot(0x0)/
c4e3b2
        Pci(0x4,0x0)/
c4e3b2
        HD(1,GPT,14DD1CC5-D576-4BBF-8858-BAF877C8DF61,0x800,0x64000)/
c4e3b2
        \EFI\fedora\shim.efi
c4e3b2
c4e3b2
      The motivation for this type of device path matching / completion is to
c4e3b2
      allow the user to move around the hard drive (for example, to plug a
c4e3b2
      controller in a different PCI slot, or to expose the block device on a
c4e3b2
      different iSCSI path) and still enable the firmware to find the hard
c4e3b2
      drive.
c4e3b2
c4e3b2
      The UEFI specification says,
c4e3b2
c4e3b2
        9.3.6 Media Device Path
c4e3b2
        9.3.6.1 Hard Drive
c4e3b2
c4e3b2
          [...] Section 3.1.2 defines special rules for processing the Hard
c4e3b2
          Drive Media Device Path. These special rules enable a disk's location
c4e3b2
          to change and still have the system boot from the disk. [...]
c4e3b2
c4e3b2
        3.1.2 Load Option Processing
c4e3b2
c4e3b2
          [...] The boot manager must [...] support booting from a short-form
c4e3b2
          device path that starts with the first element being a hard drive
c4e3b2
          media device path [...]. The boot manager must use the GUID or
c4e3b2
          signature and partition number in the hard drive device path to match
c4e3b2
          it to a device in the system. If the drive supports the GPT
c4e3b2
          partitioning scheme the GUID in the hard drive media device path is
c4e3b2
          compared with the UniquePartitionGuid field of the GUID Partition
c4e3b2
          Entry [...]. If the drive supports the PC-AT MBR scheme the signature
c4e3b2
          in the hard drive media device path is compared with the
c4e3b2
          UniqueMBRSignature in the Legacy Master Boot Record [...]. If a
c4e3b2
          signature match is made, then the partition number must also be
c4e3b2
          matched. The hard drive device path can be appended to the matching
c4e3b2
          hardware device path and normal boot behavior can then be used. If
c4e3b2
          more than one device matches the hard drive device path, the boot
c4e3b2
          manager will pick one arbitrarily. Thus the operating system must
c4e3b2
          ensure the uniqueness of the signatures on hard drives to guarantee
c4e3b2
          deterministic boot behavior.
c4e3b2
c4e3b2
      Edk2 implements and exposes the device path completion logic in the
c4e3b2
      already referenced "IntelFrameworkModulePkg/Library/GenericBdsLib"
c4e3b2
      library, in the BdsExpandPartitionPartialDevicePathToFull() function.
c4e3b2
c4e3b2
    (d) Filtering and reordering the boot options based on fw_cfg
c4e3b2
c4e3b2
      Once we have an "all-inclusive", partly preexistent, partly freshly
c4e3b2
      auto-generated boot option list from bullet (b), OVMF loads QEMU's
c4e3b2
      requested boot order from fw_cfg, and filters and reorders the list from
c4e3b2
      (b) with it:
c4e3b2
c4e3b2
      PlatformBdsPolicyBehavior()                   [OvmfPkg/.../BdsPlatform.c]
c4e3b2
        TryRunningQemuKernel()                       [OvmfPkg/.../QemuKernel.c]
c4e3b2
        BdsLibConnectAll()           [IntelFrameworkModulePkg/.../BdsConnect.c]
c4e3b2
        BdsLibEnumerateAllBootOption()  [IntelFrameworkModulePkg/.../BdsBoot.c]
c4e3b2
        SetBootOrderFromQemu()                    [OvmfPkg/.../QemuBootOrder.c]
c4e3b2
c4e3b2
      According to the (preferred) "-device ...,bootindex=N" and the (legacy)
c4e3b2
      '-boot order=drives' command line options, QEMU requests a boot order
c4e3b2
      from the firmware through the "bootorder" fw_cfg file. (For a bootindex
c4e3b2
      example, refer to the "Example qemu invocation" section.)
c4e3b2
c4e3b2
      This fw_cfg file consists of OpenFirmware (OFW) device paths -- note: not
c4e3b2
      UEFI device paths! --, one per line. An example list is:
c4e3b2
c4e3b2
        /pci@i0cf8/scsi@4/disk@0,0
c4e3b2
        /pci@i0cf8/ide@1,1/drive@1/disk@0
c4e3b2
        /pci@i0cf8/ethernet@3/ethernet-phy@0
c4e3b2
c4e3b2
      OVMF filters and reorders the boot option list from bullet (b) with the
c4e3b2
      following nested loops algorithm:
c4e3b2
c4e3b2
        new_uefi_order := <empty>
c4e3b2
        for each qemu_ofw_path in QEMU's OpenFirmware device path list:
c4e3b2
          qemu_uefi_path_prefix := translate(qemu_ofw_path)
c4e3b2
c4e3b2
          for each boot_option in current_uefi_order:
c4e3b2
            full_boot_option := complete(boot_option)
c4e3b2
c4e3b2
            if match(qemu_uefi_path_prefix, full_boot_option):
c4e3b2
              append(new_uefi_order, boot_option)
c4e3b2
              break
c4e3b2
c4e3b2
        for each unmatched boot_option in current_uefi_order:
c4e3b2
          if survives(boot_option):
c4e3b2
            append(new_uefi_order, boot_option)
c4e3b2
c4e3b2
        current_uefi_order := new_uefi_order
c4e3b2
c4e3b2
      OVMF iterates over QEMU's OFW device paths in order, translates each to a
c4e3b2
      UEFI device path prefix, tries to match the translated prefix against the
c4e3b2
      UEFI boot options (which are completed from relative form to absolute
c4e3b2
      form for the purpose of prefix matching), and if there's a match, the
c4e3b2
      matching boot option is appended to the new boot order (which starts out
c4e3b2
      empty).
c4e3b2
c4e3b2
      (We elaborate on the translate() function under bullet (e). The
c4e3b2
      complete() function has been explained in bullet (c).)
c4e3b2
c4e3b2
      In addition, UEFI boot options that remain unmatched after filtering and
c4e3b2
      reordering are post-processed, and some of them "survive". Due to the
c4e3b2
      fact that OpenFirmware device paths have less expressive power than their
c4e3b2
      UEFI counterparts, some UEFI boot options are simply inexpressible (hence
c4e3b2
      unmatchable) by the nested loops algorithm.
c4e3b2
c4e3b2
      An important example is the memory-mapped UEFI shell, whose UEFI device
c4e3b2
      path is inexpressible by QEMU's OFW device paths:
c4e3b2
c4e3b2
        MemoryMapped(0xB,0x900000,0x10FFFFF)/
c4e3b2
        FvFile(7C04A583-9E3E-4F1C-AD65-E05268D0B4D1)
c4e3b2
c4e3b2
      (Side remark: notice that the address range visible in the MemoryMapped()
c4e3b2
      node corresponds to DXEFV under "comprehensive memory map of OVMF"! In
c4e3b2
      addition, the FvFile() node's GUID originates from the FILE_GUID entry of
c4e3b2
      "ShellPkg/Application/Shell/Shell.inf".)
c4e3b2
c4e3b2
      The UEFI shell can be booted by pressing ESC in OVMF on the TianoCore
c4e3b2
      splash screen, and navigating to Boot Manager | EFI Internal Shell. If
c4e3b2
      the "survival policy" was not implemented, the UEFI shell's boot option
c4e3b2
      would always be filtered out.
c4e3b2
c4e3b2
      The current "survival policy" preserves all boot options that start with
c4e3b2
      neither PciRoot() nor HD().
c4e3b2
c4e3b2
    (e) Translating QEMU's OpenFirmware device paths to UEFI device path
c4e3b2
        prefixes
c4e3b2
c4e3b2
      In this section we list the (strictly heuristical) mappings currently
c4e3b2
      performed by OVMF.
c4e3b2
c4e3b2
      The "prefix only" nature of the translation output is rooted minimally in
c4e3b2
      the fact that QEMU's OpenFirmware device paths cannot carry pathnames
c4e3b2
      within filesystems. There's no way to specify eg.
c4e3b2
c4e3b2
        \EFI\fedora\shim.efi
c4e3b2
c4e3b2
      in an OFW device path, therefore a UEFI device path translated from an
c4e3b2
      OFW device path can at best be a prefix (not a full match) of a UEFI
c4e3b2
      device path that ends with "\EFI\fedora\shim.efi".
c4e3b2
c4e3b2
      - IDE disk, IDE CD-ROM:
c4e3b2
c4e3b2
        OpenFirmware device path:
c4e3b2
c4e3b2
          /pci@i0cf8/ide@1,1/drive@0/disk@0
c4e3b2
               ^         ^ ^       ^      ^
c4e3b2
               |         | |       |      master or slave
c4e3b2
               |         | |       primary or secondary
c4e3b2
               |         PCI slot & function holding IDE controller
c4e3b2
               PCI root at system bus port, PIO
c4e3b2
c4e3b2
        UEFI device path prefix:
c4e3b2
c4e3b2
          PciRoot(0x0)/Pci(0x1,0x1)/Ata(Primary,Master,0x0)
c4e3b2
                                                       ^
c4e3b2
                                                       fixed LUN
c4e3b2
c4e3b2
      - Floppy disk:
c4e3b2
c4e3b2
        OpenFirmware device path:
c4e3b2
c4e3b2
          /pci@i0cf8/isa@1/fdc@03f0/floppy@0
c4e3b2
               ^         ^     ^           ^
c4e3b2
               |         |     |           A: or B:
c4e3b2
               |         |     ISA controller io-port (hex)
c4e3b2
               |         PCI slot holding ISA controller
c4e3b2
               PCI root at system bus port, PIO
c4e3b2
c4e3b2
        UEFI device path prefix:
c4e3b2
c4e3b2
          PciRoot(0x0)/Pci(0x1,0x0)/Floppy(0x0)
c4e3b2
                                           ^
c4e3b2
                                           ACPI UID (A: or B:)
c4e3b2
c4e3b2
      - Virtio-block disk:
c4e3b2
c4e3b2
        OpenFirmware device path:
c4e3b2
c4e3b2
          /pci@i0cf8/scsi@6[,3]/disk@0,0
c4e3b2
               ^          ^  ^       ^ ^
c4e3b2
               |          |  |       fixed
c4e3b2
               |          |  PCI function corresponding to disk (optional)
c4e3b2
               |          PCI slot holding disk
c4e3b2
               PCI root at system bus port, PIO
c4e3b2
c4e3b2
        UEFI device path prefixes (dependent on the presence of a nonzero PCI
c4e3b2
        function in the OFW device path):
c4e3b2
c4e3b2
          PciRoot(0x0)/Pci(0x6,0x0)/HD(
c4e3b2
          PciRoot(0x0)/Pci(0x6,0x3)/HD(
c4e3b2
c4e3b2
      - Virtio-scsi disk and virtio-scsi passthrough:
c4e3b2
c4e3b2
        OpenFirmware device path:
c4e3b2
c4e3b2
          /pci@i0cf8/scsi@7[,3]/channel@0/disk@2,3
c4e3b2
               ^          ^             ^      ^ ^
c4e3b2
               |          |             |      | LUN
c4e3b2
               |          |             |      target
c4e3b2
               |          |             channel (unused, fixed 0)
c4e3b2
               |          PCI slot[, function] holding SCSI controller
c4e3b2
               PCI root at system bus port, PIO
c4e3b2
c4e3b2
        UEFI device path prefixes (dependent on the presence of a nonzero PCI
c4e3b2
        function in the OFW device path):
c4e3b2
c4e3b2
          PciRoot(0x0)/Pci(0x7,0x0)/Scsi(0x2,0x3)
c4e3b2
          PciRoot(0x0)/Pci(0x7,0x3)/Scsi(0x2,0x3)
c4e3b2
c4e3b2
      - Emulated and passed-through (physical) network cards:
c4e3b2
c4e3b2
        OpenFirmware device path:
c4e3b2
c4e3b2
          /pci@i0cf8/ethernet@3[,2]
c4e3b2
               ^              ^
c4e3b2
               |              PCI slot[, function] holding Ethernet card
c4e3b2
               PCI root at system bus port, PIO
c4e3b2
c4e3b2
        UEFI device path prefixes (dependent on the presence of a nonzero PCI
c4e3b2
        function in the OFW device path):
c4e3b2
c4e3b2
          PciRoot(0x0)/Pci(0x3,0x0)
c4e3b2
          PciRoot(0x0)/Pci(0x3,0x2)
c4e3b2
c4e3b2
Virtio drivers
c4e3b2
..............
c4e3b2
c4e3b2
UEFI abstracts various types of hardware resources into protocols, and allows
c4e3b2
firmware developers to implement those protocols in device drivers. The Virtio
c4e3b2
Specification defines various types of virtual hardware for virtual machines.
c4e3b2
Connecting the two specifications, OVMF provides UEFI drivers for QEMU's
c4e3b2
virtio-block, virtio-scsi, and virtio-net devices.
c4e3b2
c4e3b2
The following diagram presents the protocol and driver stack related to Virtio
c4e3b2
devices in edk2 and OVMF. Each node in the graph identifies a protocol and/or
c4e3b2
the edk2 driver that produces it. Nodes on the top are more abstract.
c4e3b2
c4e3b2
  EFI_BLOCK_IO_PROTOCOL                             EFI_SIMPLE_NETWORK_PROTOCOL
c4e3b2
  [OvmfPkg/VirtioBlkDxe]                              [OvmfPkg/VirtioNetDxe]
c4e3b2
             |                                                   |
c4e3b2
             |         EFI_EXT_SCSI_PASS_THRU_PROTOCOL           |
c4e3b2
             |             [OvmfPkg/VirtioScsiDxe]               |
c4e3b2
             |                        |                          |
c4e3b2
             +------------------------+--------------------------+
c4e3b2
                                      |
c4e3b2
                           VIRTIO_DEVICE_PROTOCOL
c4e3b2
                                      |
c4e3b2
                +---------------------+---------------------+
c4e3b2
                |                                           |
c4e3b2
  [OvmfPkg/VirtioPciDeviceDxe]                  [custom platform drivers]
c4e3b2
                |                                           |
c4e3b2
                |                                           |
c4e3b2
       EFI_PCI_IO_PROTOCOL                [OvmfPkg/Library/VirtioMmioDeviceLib]
c4e3b2
 [MdeModulePkg/Bus/Pci/PciBusDxe]              direct MMIO register access
c4e3b2
c4e3b2
The top three drivers produce standard UEFI abstractions: the Block IO
c4e3b2
Protocol, the Extended SCSI Pass Thru Protocol, and the Simple Network
c4e3b2
Protocol, for virtio-block, virtio-scsi, and virtio-net devices, respectively.
c4e3b2
c4e3b2
Comparing these device-specific virtio drivers to each other, we can determine:
c4e3b2
c4e3b2
- They all conform to the UEFI Driver Model. This means that their entry point
c4e3b2
  functions don't immediately start to search for devices and to drive them,
c4e3b2
  they only register instances of the EFI_DRIVER_BINDING_PROTOCOL. The UEFI
c4e3b2
  Driver Model then enumerates devices and chains matching drivers
c4e3b2
  automatically.
c4e3b2
c4e3b2
- They are as minimal as possible, while remaining correct (refer to source
c4e3b2
  code comments for details). For example, VirtioBlkDxe and VirtioScsiDxe both
c4e3b2
  support only one request in flight.
c4e3b2
c4e3b2
  In theory, VirtioBlkDxe could implement EFI_BLOCK_IO2_PROTOCOL, which allows
c4e3b2
  queueing. Similarly, VirtioScsiDxe does not support the non-blocking mode of
c4e3b2
  EFI_EXT_SCSI_PASS_THRU_PROTOCOL.PassThru(). (Which is permitted by the UEFI
c4e3b2
  specification.) Both VirtioBlkDxe and VirtioScsiDxe delegate synchronous
c4e3b2
  request handling to "OvmfPkg/Library/VirtioLib". This limitation helps keep
c4e3b2
  the implementation simple, and testing thus far seems to imply satisfactory
c4e3b2
  performance, for a virtual boot firmware.
c4e3b2
c4e3b2
  VirtioNetDxe cannot avoid queueing, because EFI_SIMPLE_NETWORK_PROTOCOL
c4e3b2
  requires it on the interface level. Consequently, VirtioNetDxe is
c4e3b2
  significantly more complex than VirtioBlkDxe and VirtioScsiDxe. Technical
c4e3b2
  notes are provided in "OvmfPkg/VirtioNetDxe/TechNotes.txt".
c4e3b2
c4e3b2
- None of these drivers access hardware directly. Instead, the Virtio Device
c4e3b2
  Protocol (OvmfPkg/Include/Protocol/VirtioDevice.h) collects / extracts virtio
c4e3b2
  operations defined in the Virtio Specification, and these backend-independent
c4e3b2
  virtio device drivers go through the abstract VIRTIO_DEVICE_PROTOCOL.
c4e3b2
c4e3b2
  IMPORTANT: the VIRTIO_DEVICE_PROTOCOL is not a standard UEFI protocol. It is
c4e3b2
  internal to edk2 and not described in the UEFI specification. It should only
c4e3b2
  be used by drivers and applications that live inside the edk2 source tree.
c4e3b2
c4e3b2
Currently two providers exist for VIRTIO_DEVICE_PROTOCOL:
c4e3b2
c4e3b2
- The first one is the "more traditional" virtio-pci backend, implemented by
c4e3b2
  OvmfPkg/VirtioPciDeviceDxe. This driver also complies with the UEFI Driver
c4e3b2
  Model. It consumes an instance of the EFI_PCI_IO_PROTOCOL, and, if the PCI
c4e3b2
  device/function under probing appears to be a virtio device, it produces a
c4e3b2
  Virtio Device Protocol instance for it. The driver translates abstract virtio
c4e3b2
  operations to PCI accesses.
c4e3b2
c4e3b2
- The second provider, the virtio-mmio backend, is a library, not a driver,
c4e3b2
  living in OvmfPkg/Library/VirtioMmioDeviceLib. This library translates
c4e3b2
  abstract virtio operations to MMIO accesses.
c4e3b2
c4e3b2
  The virtio-mmio backend is only a library -- rather than a standalone, UEFI
c4e3b2
  Driver Model-compliant driver -- because the type of resource it consumes, an
c4e3b2
  MMIO register block base address, is not enumerable.
c4e3b2
c4e3b2
  In other words, while the PCI root bridge driver and the PCI bus driver
c4e3b2
  produce instances of EFI_PCI_IO_PROTOCOL automatically, thereby enabling the
c4e3b2
  UEFI Driver Model to probe devices and stack up drivers automatically, no
c4e3b2
  such enumeration exists for MMIO register blocks.
c4e3b2
c4e3b2
  For this reason, VirtioMmioDeviceLib needs to be linked into thin, custom
c4e3b2
  platform drivers that dispose over this kind of information. As soon as a
c4e3b2
  driver knows about the MMIO register block base addresses, it can pass each
c4e3b2
  to the library, and then the VIRTIO_DEVICE_PROTOCOL will be instantiated
c4e3b2
  (assuming a valid virtio-mmio register block of course). From that point on
c4e3b2
  the UEFI Driver Model again takes care of the chaining.
c4e3b2
c4e3b2
  Typically, such a custom driver does not conform to the UEFI Driver Model
c4e3b2
  (because that would presuppose auto-enumeration for MMIO register blocks).
c4e3b2
  Hence it has the following responsibilities:
c4e3b2
c4e3b2
  - it shall behave as a "wrapper" UEFI driver around the library,
c4e3b2
c4e3b2
  - it shall know virtio-mmio base addresses,
c4e3b2
c4e3b2
  - in its entry point function, it shall create a new UEFI handle with an
c4e3b2
    instance of the EFI_DEVICE_PATH_PROTOCOL for each virtio-mmio device it
c4e3b2
    knows the base address for,
c4e3b2
c4e3b2
  - it shall call VirtioMmioInstallDevice() on those handles, with the
c4e3b2
    corresponding base addresses.
c4e3b2
c4e3b2
  OVMF itself does not employ VirtioMmioDeviceLib. However, the library is used
c4e3b2
  (or has been tested as Proof-of-Concept) in the following 64-bit and 32-bit
c4e3b2
  ARM emulator setups:
c4e3b2
c4e3b2
  - in "RTSM_VE_FOUNDATIONV8_EFI.fd" and "FVP_AARCH64_EFI.fd", on ARM Holdings'
c4e3b2
    ARM(R) v8-A Foundation Model and ARM(R) AEMv8-A Base Platform FVP
c4e3b2
    emulators, respectively:
c4e3b2
c4e3b2
                           EFI_BLOCK_IO_PROTOCOL
c4e3b2
                           [OvmfPkg/VirtioBlkDxe]
c4e3b2
                                      |
c4e3b2
                           VIRTIO_DEVICE_PROTOCOL
c4e3b2
        [ArmPlatformPkg/ArmVExpressPkg/ArmVExpressDxe/ArmFvpDxe.inf]
c4e3b2
                                      |
c4e3b2
                    [OvmfPkg/Library/VirtioMmioDeviceLib]
c4e3b2
                         direct MMIO register access
c4e3b2
c4e3b2
  - in "RTSM_VE_CORTEX-A15_EFI.fd" and "RTSM_VE_CORTEX-A15_MPCORE_EFI.fd", on
c4e3b2
    "qemu-system-arm -M vexpress-a15":
c4e3b2
c4e3b2
        EFI_BLOCK_IO_PROTOCOL            EFI_SIMPLE_NETWORK_PROTOCOL
c4e3b2
        [OvmfPkg/VirtioBlkDxe]             [OvmfPkg/VirtioNetDxe]
c4e3b2
                   |                                  |
c4e3b2
                   +------------------+---------------+
c4e3b2
                                      |
c4e3b2
                           VIRTIO_DEVICE_PROTOCOL
c4e3b2
        [ArmPlatformPkg/ArmVExpressPkg/ArmVExpressDxe/ArmFvpDxe.inf]
c4e3b2
                                      |
c4e3b2
                    [OvmfPkg/Library/VirtioMmioDeviceLib]
c4e3b2
                         direct MMIO register access
c4e3b2
c4e3b2
  In the above ARM / VirtioMmioDeviceLib configurations, VirtioBlkDxe was
c4e3b2
  tested with booting Linux distributions, while VirtioNetDxe was tested with
c4e3b2
  pinging public IPv4 addresses from the UEFI shell.
c4e3b2
c4e3b2
Platform Driver
c4e3b2
...............
c4e3b2
c4e3b2
Sometimes, elements of persistent firmware configuration are best exposed to
c4e3b2
the user in a friendly way. OVMF's platform driver (OvmfPkg/PlatformDxe)
c4e3b2
presents such settings on the "OVMF Platform Configuration" dialog:
c4e3b2
c4e3b2
- Press ESC on the TianoCore splash screen,
c4e3b2
- Navigate to Device Manager | OVMF Platform Configuration.
c4e3b2
c4e3b2
At the moment, OVMF's platform driver handles only one setting: the preferred
c4e3b2
graphics resolution. This is useful for two purposes:
c4e3b2
c4e3b2
- Some UEFI shell commands, like DRIVERS and DEVICES, benefit from a wide
c4e3b2
  display. Using the MODE shell command, the user can switch to a larger text
c4e3b2
  resolution (limited by the graphics resolution), and see the command output
c4e3b2
  in a more easily consumable way.
c4e3b2
c4e3b2
  [RHEL] The list of text modes available to the MODE command is also limited
c4e3b2
         by ConSplitterDxe (found under MdeModulePkg/Universal/Console).
c4e3b2
         ConSplitterDxe builds an intersection of text modes that are
c4e3b2
         simultaneously supported by all consoles that ConSplitterDxe
c4e3b2
         multiplexes console output to.
c4e3b2
c4e3b2
         In practice, the strongest text mode restriction comes from
c4e3b2
         TerminalDxe, which provides console I/O on serial ports. TerminalDxe
c4e3b2
         has a very limited built-in list of text modes, heavily pruning the
c4e3b2
         intersection built by ConSplitterDxe, and made available to the MODE
c4e3b2
         command.
c4e3b2
c4e3b2
         On the Red Hat Enterprise Linux 7.1 host, TerminalDxe's list of modes
c4e3b2
         has been extended with text resolutions that match the Spice QXL GPU's
c4e3b2
         common graphics resolutions. This way a "full screen" text mode should
c4e3b2
         always be available in the MODE command.
c4e3b2
c4e3b2
- The other advantage of controlling the graphics resolution lies with UEFI
c4e3b2
  operating systems that don't (yet) have a native driver for QEMU's virtual
c4e3b2
  video cards  -- eg. the Spice QXL GPU. Such OSes may choose to inherit the
c4e3b2
  properties of OVMF's EFI_GRAPHICS_OUTPUT_PROTOCOL (provided by
c4e3b2
  OvmfPkg/QemuVideoDxe, see later).
c4e3b2
c4e3b2
  Although the display can be used at runtime in such cases, by direct
c4e3b2
  framebuffer access, its properties, for example, the resolution, cannot be
c4e3b2
  modified. The platform driver allows the user to select the preferred GOP
c4e3b2
  resolution, reboot, and let the guest OS inherit that preferred resolution.
c4e3b2
c4e3b2
The platform driver has three access points: the "normal" driver entry point, a
c4e3b2
set of HII callbacks, and a GOP installation callback.
c4e3b2
c4e3b2
(1) Driver entry point: the PlatformInit() function.
c4e3b2
c4e3b2
    (a) First, this function loads any available settings, and makes them take
c4e3b2
        effect. For the preferred graphics resolution in particular, this means
c4e3b2
        setting the following PCDs:
c4e3b2
c4e3b2
          gEfiMdeModulePkgTokenSpaceGuid.PcdVideoHorizontalResolution
c4e3b2
          gEfiMdeModulePkgTokenSpaceGuid.PcdVideoVerticalResolution
c4e3b2
c4e3b2
        These PCDs influence the GraphicsConsoleDxe driver (located under
c4e3b2
        MdeModulePkg/Universal/Console), which switches to the preferred
c4e3b2
        graphics mode, and produces EFI_SIMPLE_TEXT_OUTPUT_PROTOCOLs on GOPs:
c4e3b2
c4e3b2
                    EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
c4e3b2
          [MdeModulePkg/Universal/Console/GraphicsConsoleDxe]
c4e3b2
                                   |
c4e3b2
                      EFI_GRAPHICS_OUTPUT_PROTOCOL
c4e3b2
                         [OvmfPkg/QemuVideoDxe]
c4e3b2
                                   |
c4e3b2
                          EFI_PCI_IO_PROTOCOL
c4e3b2
                   [MdeModulePkg/Bus/Pci/PciBusDxe]
c4e3b2
c4e3b2
  (b) Second, the driver entry point registers the user interface, including
c4e3b2
      HII callbacks.
c4e3b2
c4e3b2
  (c) Third, the driver entry point registers a GOP installation callback.
c4e3b2
c4e3b2
(2) HII callbacks and the user interface.
c4e3b2
c4e3b2
    The Human Interface Infrastructure (HII) "is a set of protocols that allow
c4e3b2
    a UEFI driver to provide the ability to register user interface and
c4e3b2
    configuration content with the platform firmware".
c4e3b2
c4e3b2
    OVMF's platform driver:
c4e3b2
c4e3b2
    - provides a static, basic, visual form (PlatformForms.vfr), written in the
c4e3b2
      Visual Forms Representation language,
c4e3b2
c4e3b2
    - includes a UCS-16 encoded message catalog (Platform.uni),
c4e3b2
c4e3b2
    - includes source code that dynamically populates parts of the form, with
c4e3b2
      the help of MdeModulePkg/Library/UefiHiiLib -- this library simplifies
c4e3b2
      the handling of IFR (Internal Forms Representation) opcodes,
c4e3b2
c4e3b2
    - processes form actions that the user takes (Callback() function),
c4e3b2
c4e3b2
    - loads and saves platform configuration in a private, non-volatile
c4e3b2
      variable (ExtractConfig() and RouteConfig() functions).
c4e3b2
c4e3b2
    The ExtractConfig() HII callback implements the following stack of
c4e3b2
    conversions, for loading configuration and presenting it to the user:
c4e3b2
c4e3b2
          MultiConfigAltResp       -- form engine / HII communication
c4e3b2
                  ^
c4e3b2
                  |
c4e3b2
           [BlockToConfig]
c4e3b2
                  |
c4e3b2
           MAIN_FORM_STATE         -- binary representation of form/widget
c4e3b2
                  ^                   state
c4e3b2
                  |
c4e3b2
      [PlatformConfigToFormState]
c4e3b2
                  |
c4e3b2
           PLATFORM_CONFIG         -- accessible to DXE and UEFI drivers
c4e3b2
                  ^
c4e3b2
                  |
c4e3b2
         [PlatformConfigLoad]
c4e3b2
                  |
c4e3b2
        UEFI non-volatile variable -- accessible to external utilities
c4e3b2
c4e3b2
    The layers are very similar for the reverse direction, ie. when taking
c4e3b2
    input from the user, and saving the configuration (RouteConfig() HII
c4e3b2
    callback):
c4e3b2
c4e3b2
             ConfigResp            -- form engine / HII communication
c4e3b2
                  |
c4e3b2
           [ConfigToBlock]
c4e3b2
                  |
c4e3b2
                  v
c4e3b2
           MAIN_FORM_STATE         -- binary representation of form/widget
c4e3b2
                  |                   state
c4e3b2
      [FormStateToPlatformConfig]
c4e3b2
                  |
c4e3b2
                  v
c4e3b2
           PLATFORM_CONFIG         -- accessible to DXE and UEFI drivers
c4e3b2
                  |
c4e3b2
         [PlatformConfigSave]
c4e3b2
                  |
c4e3b2
                  v
c4e3b2
        UEFI non-volatile variable -- accessible to external utilities
c4e3b2
c4e3b2
(3) When the platform driver starts, a GOP may not be available yet. Thus the
c4e3b2
    driver entry point registers a callback (the GopInstalled() function) for
c4e3b2
    GOP installations.
c4e3b2
c4e3b2
    When the first GOP is produced (usually by QemuVideoDxe, or potentially by
c4e3b2
    a third party video driver), PlatformDxe retrieves the list of graphics
c4e3b2
    modes the GOP supports, and dynamically populates the drop-down list of
c4e3b2
    available resolutions on the form. The GOP installation callback is then
c4e3b2
    removed.
c4e3b2
c4e3b2
Video driver
c4e3b2
............
c4e3b2
c4e3b2
OvmfPkg/QemuVideoDxe is OVMF's built-in video driver. We can divide its
c4e3b2
services in two parts: graphics output protocol (primary), and Int10h (VBE)
c4e3b2
shim (secondary).
c4e3b2
c4e3b2
(1) QemuVideoDxe conforms to the UEFI Driver Model; it produces an instance of
c4e3b2
    the EFI_GRAPHICS_OUTPUT_PROTOCOL (GOP) on each PCI display that it supports
c4e3b2
    and is connected to:
c4e3b2
c4e3b2
                      EFI_GRAPHICS_OUTPUT_PROTOCOL
c4e3b2
                         [OvmfPkg/QemuVideoDxe]
c4e3b2
                                   |
c4e3b2
                          EFI_PCI_IO_PROTOCOL
c4e3b2
                   [MdeModulePkg/Bus/Pci/PciBusDxe]
c4e3b2
c4e3b2
    It supports the following QEMU video cards:
c4e3b2
c4e3b2
    - Cirrus 5430 ("-device cirrus-vga"),
c4e3b2
    - Standard VGA ("-device VGA"),
c4e3b2
    - QXL VGA ("-device qxl-vga", "-device qxl").
c4e3b2
c4e3b2
    For Cirrus the following resolutions and color depths are available:
c4e3b2
    640x480x32, 800x600x32, 1024x768x24. On stdvga and QXL a long list of
c4e3b2
    resolutions is available. The list is filtered against the frame buffer
c4e3b2
    size during initialization.
c4e3b2
c4e3b2
    The size of the QXL VGA compatibility framebuffer can be changed with the
c4e3b2
c4e3b2
      -device qxl-vga,vgamem_mb=$NUM_MB
c4e3b2
c4e3b2
    QEMU option. If $NUM_MB exceeds 32, then the following is necessary
c4e3b2
    instead:
c4e3b2
c4e3b2
      -device qxl-vga,vgamem_mb=$NUM_MB,ram_size_mb=$((NUM_MB*2))
c4e3b2
c4e3b2
    because the compatibility framebuffer can't cover more than half of PCI BAR
c4e3b2
    #0. The latter defaults to 64MB in size, and is controlled by the
c4e3b2
    "ram_size_mb" property.
c4e3b2
c4e3b2
(2) When QemuVideoDxe binds the first Standard VGA or QXL VGA device, and there
c4e3b2
    is no real VGA BIOS present in the C to F segments (which could originate
c4e3b2
    from a legacy PCI option ROM -- refer to "Compatibility Support Module
c4e3b2
    (CSM)"), then QemuVideoDxe installs a minimal, "fake" VGA BIOS -- an Int10h
c4e3b2
    (VBE) "shim".
c4e3b2
c4e3b2
    The shim is implemented in 16-bit assembly in
c4e3b2
    "OvmfPkg/QemuVideoDxe/VbeShim.asm". The "VbeShim.sh" shell script assembles
c4e3b2
    it and formats it as a C array ("VbeShim.h") with the help of the "nasm"
c4e3b2
    utility. The driver's InstallVbeShim() function copies the shim in place
c4e3b2
    (the C segment), and fills in the VBE Info and VBE Mode Info structures.
c4e3b2
    The real-mode 10h interrupt vector is pointed to the shim's handler.
c4e3b2
c4e3b2
    The shim is (correctly) irrelevant and invisible for all UEFI operating
c4e3b2
    systems we know about -- except Windows Server 2008 R2 and other Windows
c4e3b2
    operating systems in that family.
c4e3b2
c4e3b2
    Namely, the Windows 2008 R2 SP1 (and Windows 7) UEFI guest's default video
c4e3b2
    driver dereferences the real mode Int10h vector, loads the pointed-to
c4e3b2
    handler code, and executes what it thinks to be VGA BIOS services in an
c4e3b2
    internal real-mode emulator. Consequently, video mode switching used not to
c4e3b2
    work in Windows 2008 R2 SP1 when it ran on the "pure UEFI" build of OVMF,
c4e3b2
    making the guest uninstallable. Hence the (otherwise optional, non-default)
c4e3b2
    Compatibility Support Module (CSM) ended up a requirement for running such
c4e3b2
    guests.
c4e3b2
c4e3b2
    The hard dependency on the sophisticated SeaBIOS CSM and the complex
c4e3b2
    supporting edk2 infrastructure, for enabling this family of guests, was
c4e3b2
    considered suboptimal by some members of the upstream community,
c4e3b2
c4e3b2
    [RHEL] and was certainly considered a serious maintenance disadvantage for
c4e3b2
           Red Hat Enterprise Linux 7.1 hosts.
c4e3b2
c4e3b2
    Thus, the shim has been collaboratively developed for the Windows 7 /
c4e3b2
    Windows Server 2008 R2 family. The shim provides a real stdvga / QXL
c4e3b2
    implementation for the few services that are in fact necessary for the
c4e3b2
    Windows 2008 R2 SP1 (and Windows 7) UEFI guest, plus some "fakes" that the
c4e3b2
    guest invokes but whose effect is not important. The only supported mode is
c4e3b2
    1024x768x32, which is enough to install the guest and then upgrade its
c4e3b2
    video driver to the full-featured QXL XDDM one.
c4e3b2
c4e3b2
    The C segment is not present in the UEFI memory map prepared by OVMF.
c4e3b2
    Memory space that would cover it is never added (either in PEI, in the form
c4e3b2
    of memory resource descriptor HOBs, or in DXE, via gDS->AddMemorySpace()).
c4e3b2
    This way the handler body is invisible to all other UEFI guests, and the
c4e3b2
    rest of edk2.
c4e3b2
c4e3b2
    The Int10h real-mode IVT entry is covered with a Boot Services Code page,
c4e3b2
    making that too inaccessible to the rest of edk2. Due to the allocation
c4e3b2
    type, UEFI guest OSes different from the Windows Server 2008 family can
c4e3b2
    reclaim the page at zero. (The Windows 2008 family accesses that page
c4e3b2
    regardless of the allocation type.)
c4e3b2
c4e3b2
Afterword
c4e3b2
---------
c4e3b2
c4e3b2
After the bulk of this document was written in July 2014, OVMF development has
c4e3b2
not stopped. To name two significant code contributions from the community: in
c4e3b2
January 2015, OVMF runs on the "q35" machine type of QEMU, and it features a
c4e3b2
driver for Xen paravirtual block devices (and another for the underlying Xen
c4e3b2
bus).
c4e3b2
c4e3b2
Furthermore, a dedicated virtualization platform has been contributed to
c4e3b2
ArmPlatformPkg that plays a role parallel to OvmfPkg's. It targets the "virt"
c4e3b2
machine type of qemu-system-arm and qemu-system-aarch64. Parts of OvmfPkg are
c4e3b2
being refactored and modularized so they can be reused in
c4e3b2
"ArmPlatformPkg/ArmVirtualizationPkg/ArmVirtualizationQemu.dsc".