render / rpms / edk2

Forked from rpms/edk2 2 months ago
Clone

Blame ovmf-whitepaper-c770f8c.txt

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