]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmVirtualizationPkg: Intel BDS: load EFI-stubbed Linux kernel from fw_cfg
authorLaszlo Ersek <lersek@redhat.com>
Fri, 2 Jan 2015 12:08:33 +0000 (12:08 +0000)
committerlersek <lersek@Edk2>
Fri, 2 Jan 2015 12:08:33 +0000 (12:08 +0000)
A number of tools depend on passing the kernel image, the initial ramdisk,
and the kernel command line to the guest on the QEMU command line (options
-kernel, -initrd, -append, respectively). At the moment, these QEMU
options  work, but the guest kernel loaded this way is launched by a
minimal binary firmware that is dynamically composed by QEMU. As a
consequence, such a kernel has no UEFI environment.

This patch enables -kernel, -initrd, -append to work on top of the
ArmVirtualizationQemu firmware build. The approach it takes is different
from how the same functionality is implemented in OvmfPkg.

OvmfPkg contains a full-fledged Linux boot loader (see
"OvmfPkg/Library/PlatformBdsLib/QemuKernel.c" and
"OvmfPkg/Library/LoadLinuxLib/"). OVMF's LoadLinuxLib sets up the required
kernel environment in a sophisticated way (including x86-specific
artifacts like the GDT), calls ExitBootServices() itself (for legacy
kernels without EFI handover protocol), and jumps to the kernel (using x86
assembly).

In ArmVirtualizationPkg's PlatformIntelBdsLib, we require the kernel being
loaded to have an EFI stub -- that is, to be a genuine UEFI application.

(The EFI stub is not an additional burden for guest kernels -- the EFI
stub is a hard requirement anyway because it needs to process the DTB
heavily:
- it removes memory nodes,
- it removes memreserve entries,
- it adds UEFI properties to the "chosen" node,
- it calculates and installs virt-to-phys mappings with
  SetVirtualAddressMap() in a way that enables kexec [planned].

Kudos to Ard Biesheuvel for summarizing the above.)

An EFI-stubbed Linux guest kernel can be loaded with plain
gBS->LoadImage(). The EFI stub will look up its own
EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL instance (ie. the device path where
it has been loaded from), and it will locate the initial ramdisk named by
the "initrd" command line parameter as a *sibling file* on the same
device.

The initrd file is then loaded using the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.

This approach enables the EFI stub to load the initial ramdisk from normal
EFI System Partitions, from remote PXE/TFTP directories -- and it enables
us to provide the initrd from memory as well.

In this patch:

- We download the kernel image, the initrd image, and the kernel command
  line, using QEMU's fw_cfg interface.

- We create a read-only EFI_SIMPLE_FILE_SYSTEM_PROTOCOL instance that has
  just a root directory, with the three downloaded files in it.

- The handle that carries the simple file system has a single-node
  VenHw(...) device path (not counting the terminator node).

- We load the EFI-stubbed kernel (which is a UEFI application) with
  gBS->LoadImage(), passing "VenHw(...)/kernel" as device path. This
  causes gBS->LoadImage() to call back into our filesystem.

- Appended to the downloaded command line, we pass "initrd=initrd" to the
  EFI stub.

- Once the EFI stub is running, it loads the initial ramdisk from the
  "sibling" device path "VenHw(...)/initrd", also calling back into our
  filesystem.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16578 6f19259b-4bc3-4df7-8a09-765794883524

ArmPlatformPkg/ArmVirtualizationPkg/Library/PlatformIntelBdsLib/IntelBdsPlatform.c
ArmPlatformPkg/ArmVirtualizationPkg/Library/PlatformIntelBdsLib/IntelBdsPlatform.h
ArmPlatformPkg/ArmVirtualizationPkg/Library/PlatformIntelBdsLib/PlatformIntelBdsLib.inf
ArmPlatformPkg/ArmVirtualizationPkg/Library/PlatformIntelBdsLib/QemuKernel.c [new file with mode: 0644]

index f5d82edfffe149cd557df560132dd97af5f44de7..6fea2b0aebfd4634356717dae35b9d4b3fe780c4 100644 (file)
@@ -305,6 +305,11 @@ PlatformBdsPolicyBehavior (
   Status = PlatformBdsConnectConsole ();\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  //\r
+  // Process QEMU's -kernel command line option\r
+  //\r
+  TryRunningQemuKernel ();\r
+\r
   BdsLibConnectAll ();\r
   BdsLibEnumerateAllBootOption (BootOptionList);\r
 \r
index c50bda2f09d68d8f0b4990cb14e46be0df694c85..cfc496d2920ea3c474b7add04a50c2989065a3e7 100644 (file)
@@ -38,4 +38,26 @@ PlatformBdsEnterFrontPage (
   IN BOOLEAN                ConnectAllHappened\r
   );\r
 \r
+/**\r
+  Download the kernel, the initial ramdisk, and the kernel command line from\r
+  QEMU's fw_cfg. Construct a minimal SimpleFileSystem that contains the two\r
+  image files, and load and start the kernel from it.\r
+\r
+  The kernel will be instructed via its command line to load the initrd from\r
+  the same Simple FileSystem.\r
+\r
+  @retval EFI_NOT_FOUND         Kernel image was not found.\r
+  @retval EFI_OUT_OF_RESOURCES  Memory allocation failed.\r
+  @retval EFI_PROTOCOL_ERROR    Unterminated kernel command line.\r
+\r
+  @return                       Error codes from any of the underlying\r
+                                functions. On success, the function doesn't\r
+                                return.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+TryRunningQemuKernel (\r
+  VOID\r
+  );\r
+\r
 #endif // _INTEL_BDS_PLATFORM_H\r
index efd50f38160669c4a84878055a95af5c7f8302a2..8db2ad57c0b30f176cee2af3bd2e2b46bfca675d 100644 (file)
@@ -33,6 +33,7 @@
 [Sources]\r
   IntelBdsPlatform.c\r
   IntelBdsPlatform.h\r
+  QemuKernel.c\r
 \r
 [Packages]\r
   ArmPkg/ArmPkg.dec\r
   PcdLib\r
   GenericBdsLib\r
   QemuBootOrderLib\r
+  QemuFwCfgLib\r
+  PrintLib\r
+  UefiRuntimeServicesTableLib\r
 \r
 [Guids]\r
   gArmGlobalVariableGuid\r
+  gEfiFileInfoGuid\r
+  gEfiFileSystemInfoGuid\r
+  gEfiFileSystemVolumeLabelInfoIdGuid\r
 \r
 [Pcd]\r
   gArmPlatformTokenSpaceGuid.PcdDefaultConInPaths\r
@@ -65,3 +72,6 @@
 [Protocols]\r
   gEfiDevicePathFromTextProtocolGuid\r
   gEfiDevicePathToTextProtocolGuid\r
+  gEfiDevicePathProtocolGuid\r
+  gEfiLoadedImageProtocolGuid\r
+  gEfiSimpleFileSystemProtocolGuid\r
diff --git a/ArmPlatformPkg/ArmVirtualizationPkg/Library/PlatformIntelBdsLib/QemuKernel.c b/ArmPlatformPkg/ArmVirtualizationPkg/Library/PlatformIntelBdsLib/QemuKernel.c
new file mode 100644 (file)
index 0000000..abcac77
--- /dev/null
@@ -0,0 +1,1103 @@
+/** @file\r
+  Try to load an EFI-stubbed ARM Linux kernel from QEMU's fw_cfg.\r
+\r
+  This implementation differs from OvmfPkg/Library/LoadLinuxLib. An EFI\r
+  stub in the subject kernel is a hard requirement here.\r
+\r
+  Copyright (C) 2014, Red Hat, Inc.\r
+\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
+  WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+**/\r
+\r
+#include <Guid/FileInfo.h>\r
+#include <Guid/FileSystemInfo.h>\r
+#include <Guid/FileSystemVolumeLabelInfo.h>\r
+#include <Library/PrintLib.h>\r
+#include <Library/QemuFwCfgLib.h>\r
+#include <Protocol/DevicePath.h>\r
+#include <Protocol/LoadedImage.h>\r
+#include <Protocol/SimpleFileSystem.h>\r
+\r
+#include "IntelBdsPlatform.h"\r
+\r
+//\r
+// Static data that hosts the fw_cfg blobs and serves file requests.\r
+//\r
+typedef enum {\r
+  KernelBlobTypeKernel,\r
+  KernelBlobTypeInitrd,\r
+  KernelBlobTypeCommandLine,\r
+  KernelBlobTypeMax\r
+} KERNEL_BLOB_TYPE;\r
+\r
+typedef struct {\r
+  FIRMWARE_CONFIG_ITEM CONST SizeKey;\r
+  FIRMWARE_CONFIG_ITEM CONST DataKey;\r
+  CONST CHAR16 *       CONST Name;\r
+  UINT32                     Size;\r
+  UINT8                      *Data;\r
+} KERNEL_BLOB;\r
+\r
+STATIC KERNEL_BLOB mKernelBlob[KernelBlobTypeMax] = {\r
+  { QemuFwCfgItemKernelSize,      QemuFwCfgItemKernelData,      L"kernel"  },\r
+  { QemuFwCfgItemInitrdSize,      QemuFwCfgItemInitrdData,      L"initrd"  },\r
+  { QemuFwCfgItemCommandLineSize, QemuFwCfgItemCommandLineData, L"cmdline" }\r
+};\r
+\r
+STATIC UINT64 mTotalBlobBytes;\r
+\r
+//\r
+// Device path for the handle that incorporates our "EFI stub filesystem". The\r
+// GUID is arbitrary and need not be standardized or advertized.\r
+//\r
+#pragma pack(1)\r
+typedef struct {\r
+  VENDOR_DEVICE_PATH       VenHwNode;\r
+  EFI_DEVICE_PATH_PROTOCOL EndNode;\r
+} SINGLE_VENHW_NODE_DEVPATH;\r
+#pragma pack()\r
+\r
+STATIC CONST SINGLE_VENHW_NODE_DEVPATH mFileSystemDevicePath = {\r
+  {\r
+    { HARDWARE_DEVICE_PATH, HW_VENDOR_DP, { sizeof (VENDOR_DEVICE_PATH) } },\r
+    {\r
+      0xb0fae7e7, 0x6b07, 0x49d0,\r
+      { 0x9e, 0x5b, 0x3b, 0xde, 0xc8, 0x3b, 0x03, 0x9d }\r
+    }\r
+  },\r
+\r
+  {\r
+    END_DEVICE_PATH_TYPE, END_ENTIRE_DEVICE_PATH_SUBTYPE,\r
+    { sizeof (EFI_DEVICE_PATH_PROTOCOL) }\r
+  }\r
+};\r
+\r
+//\r
+// The "file in the EFI stub filesystem" abstraction.\r
+//\r
+STATIC EFI_TIME mInitTime;\r
+\r
+#define STUB_FILE_SIG SIGNATURE_64 ('S', 'T', 'U', 'B', 'F', 'I', 'L', 'E')\r
+\r
+typedef struct {\r
+  UINT64            Signature; // Carries STUB_FILE_SIG.\r
+\r
+  KERNEL_BLOB_TYPE  BlobType;  // Index into mKernelBlob. KernelBlobTypeMax\r
+                               // denotes the root directory of the filesystem.\r
+\r
+  UINT64            Position;  // Byte position for regular files;\r
+                               // next directory entry to return for the root\r
+                               // directory.\r
+\r
+  EFI_FILE_PROTOCOL File;      // Standard protocol interface.\r
+} STUB_FILE;\r
+\r
+#define STUB_FILE_FROM_FILE(FilePointer) \\r
+        CR (FilePointer, STUB_FILE, File, STUB_FILE_SIG)\r
+\r
+//\r
+// Tentative definition of the file protocol template. The initializer\r
+// (external definition) will be provided later.\r
+//\r
+STATIC CONST EFI_FILE_PROTOCOL mEfiFileProtocolTemplate;\r
+\r
+\r
+//\r
+// Protocol member functions for File.\r
+//\r
+\r
+/**\r
+  Opens a new file relative to the source file's location.\r
+\r
+  @param[in]  This        A pointer to the EFI_FILE_PROTOCOL instance that is\r
+                          the file handle to the source location. This would\r
+                          typically be an open handle to a directory.\r
+\r
+  @param[out] NewHandle   A pointer to the location to return the opened handle\r
+                          for the new file.\r
+\r
+  @param[in]  FileName    The Null-terminated string of the name of the file to\r
+                          be opened. The file name may contain the following\r
+                          path modifiers: "\", ".", and "..".\r
+\r
+  @param[in]  OpenMode    The mode to open the file. The only valid\r
+                          combinations that the file may be opened with are:\r
+                          Read, Read/Write, or Create/Read/Write.\r
+\r
+  @param[in]  Attributes  Only valid for EFI_FILE_MODE_CREATE, in which case\r
+                          these are the attribute bits for the newly created\r
+                          file.\r
+\r
+  @retval EFI_SUCCESS           The file was opened.\r
+  @retval EFI_NOT_FOUND         The specified file could not be found on the\r
+                                device.\r
+  @retval EFI_NO_MEDIA          The device has no medium.\r
+  @retval EFI_MEDIA_CHANGED     The device has a different medium in it or the\r
+                                medium is no longer supported.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error.\r
+  @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.\r
+  @retval EFI_WRITE_PROTECTED   An attempt was made to create a file, or open a\r
+                                file for write when the media is\r
+                                write-protected.\r
+  @retval EFI_ACCESS_DENIED     The service denied access to the file.\r
+  @retval EFI_OUT_OF_RESOURCES  Not enough resources were available to open the\r
+                                file.\r
+  @retval EFI_VOLUME_FULL       The volume is full.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileOpen (\r
+  IN EFI_FILE_PROTOCOL  *This,\r
+  OUT EFI_FILE_PROTOCOL **NewHandle,\r
+  IN CHAR16             *FileName,\r
+  IN UINT64             OpenMode,\r
+  IN UINT64             Attributes\r
+  )\r
+{\r
+  CONST STUB_FILE *StubFile;\r
+  UINTN           BlobType;\r
+  STUB_FILE       *NewStubFile;\r
+\r
+  //\r
+  // We're read-only.\r
+  //\r
+  switch (OpenMode) {\r
+    case EFI_FILE_MODE_READ:\r
+      break;\r
+\r
+    case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE:\r
+    case EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE:\r
+      return EFI_WRITE_PROTECTED;\r
+\r
+    default:\r
+      return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Only the root directory supports opening files in it.\r
+  //\r
+  StubFile = STUB_FILE_FROM_FILE (This);\r
+  if (StubFile->BlobType != KernelBlobTypeMax) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // Locate the file.\r
+  //\r
+  for (BlobType = 0; BlobType < KernelBlobTypeMax; ++BlobType) {\r
+    if (StrCmp (FileName, mKernelBlob[BlobType].Name) == 0) {\r
+      break;\r
+    }\r
+  }\r
+  if (BlobType == KernelBlobTypeMax) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  //\r
+  // Found it.\r
+  //\r
+  NewStubFile = AllocatePool (sizeof *NewStubFile);\r
+  if (NewStubFile == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  NewStubFile->Signature = STUB_FILE_SIG;\r
+  NewStubFile->BlobType  = (KERNEL_BLOB_TYPE)BlobType;\r
+  NewStubFile->Position  = 0;\r
+  CopyMem (&NewStubFile->File, &mEfiFileProtocolTemplate,\r
+    sizeof mEfiFileProtocolTemplate);\r
+  *NewHandle = &NewStubFile->File;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Closes a specified file handle.\r
+\r
+  @param[in] This  A pointer to the EFI_FILE_PROTOCOL instance that is the file\r
+                   handle to close.\r
+\r
+  @retval EFI_SUCCESS  The file was closed.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileClose (\r
+  IN EFI_FILE_PROTOCOL *This\r
+  )\r
+{\r
+  FreePool (STUB_FILE_FROM_FILE (This));\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Close and delete the file handle.\r
+\r
+  @param[in] This  A pointer to the EFI_FILE_PROTOCOL instance that is the\r
+                   handle to the file to delete.\r
+\r
+  @retval EFI_SUCCESS              The file was closed and deleted, and the\r
+                                   handle was closed.\r
+  @retval EFI_WARN_DELETE_FAILURE  The handle was closed, but the file was not\r
+                                   deleted.\r
+\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileDelete (\r
+  IN EFI_FILE_PROTOCOL *This\r
+  )\r
+{\r
+  FreePool (STUB_FILE_FROM_FILE (This));\r
+  return EFI_WARN_DELETE_FAILURE;\r
+}\r
+\r
+\r
+/**\r
+  Helper function that formats an EFI_FILE_INFO structure into the\r
+  user-allocated buffer, for any valid KERNEL_BLOB_TYPE value (including\r
+  KernelBlobTypeMax, which stands for the root directory).\r
+\r
+  The interface follows the EFI_FILE_GET_INFO -- and for directories, the\r
+  EFI_FILE_READ -- interfaces.\r
+\r
+  @param[in]     BlobType     The KERNEL_BLOB_TYPE value identifying the fw_cfg\r
+                              blob backing the STUB_FILE that information is\r
+                              being requested about. If BlobType equals\r
+                              KernelBlobTypeMax, then information will be\r
+                              provided about the root directory of the\r
+                              filesystem.\r
+\r
+  @param[in,out] BufferSize  On input, the size of Buffer. On output, the\r
+                             amount of data returned in Buffer. In both cases,\r
+                             the size is measured in bytes.\r
+\r
+  @param[out]    Buffer      A pointer to the data buffer to return. The\r
+                             buffer's type is EFI_FILE_INFO.\r
+\r
+  @retval EFI_SUCCESS           The information was returned.\r
+  @retval EFI_BUFFER_TOO_SMALL  BufferSize is too small to store the\r
+                                EFI_FILE_INFO structure. BufferSize has been\r
+                                updated with the size needed to complete the\r
+                                request.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+ConvertKernelBlobTypeToFileInfo (\r
+  IN KERNEL_BLOB_TYPE BlobType,\r
+  IN OUT UINTN        *BufferSize,\r
+  OUT VOID            *Buffer\r
+  )\r
+{\r
+  CONST CHAR16  *Name;\r
+  UINT64        FileSize;\r
+  UINT64        Attribute;\r
+\r
+  UINTN         NameSize;\r
+  UINTN         FileInfoSize;\r
+  EFI_FILE_INFO *FileInfo;\r
+  UINTN         OriginalBufferSize;\r
+\r
+  if (BlobType == KernelBlobTypeMax) {\r
+    //\r
+    // getting file info about the root directory\r
+    //\r
+    Name      = L"\\";\r
+    FileSize  = KernelBlobTypeMax;\r
+    Attribute = EFI_FILE_READ_ONLY | EFI_FILE_DIRECTORY;\r
+  } else {\r
+    CONST KERNEL_BLOB *Blob;\r
+\r
+    Blob      = &mKernelBlob[BlobType];\r
+    Name      = Blob->Name;\r
+    FileSize  = Blob->Size;\r
+    Attribute = EFI_FILE_READ_ONLY;\r
+  }\r
+\r
+  NameSize     = (StrLen(Name) + 1) * 2;\r
+  FileInfoSize = OFFSET_OF (EFI_FILE_INFO, FileName) + NameSize;\r
+  ASSERT (FileInfoSize >= sizeof *FileInfo);\r
+\r
+  OriginalBufferSize = *BufferSize;\r
+  *BufferSize        = FileInfoSize;\r
+  if (OriginalBufferSize < *BufferSize) {\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  FileInfo               = (EFI_FILE_INFO *)Buffer;\r
+  FileInfo->Size         = FileInfoSize;\r
+  FileInfo->FileSize     = FileSize;\r
+  FileInfo->PhysicalSize = FileSize;\r
+  FileInfo->Attribute    = Attribute;\r
+\r
+  CopyMem (&FileInfo->CreateTime,       &mInitTime, sizeof mInitTime);\r
+  CopyMem (&FileInfo->LastAccessTime,   &mInitTime, sizeof mInitTime);\r
+  CopyMem (&FileInfo->ModificationTime, &mInitTime, sizeof mInitTime);\r
+  CopyMem (FileInfo->FileName,          Name,       NameSize);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Reads data from a file, or continues scanning a directory.\r
+\r
+  @param[in]     This        A pointer to the EFI_FILE_PROTOCOL instance that\r
+                             is the file handle to read data from.\r
+\r
+  @param[in,out] BufferSize  On input, the size of the Buffer. On output, the\r
+                             amount of data returned in Buffer. In both cases,\r
+                             the size is measured in bytes. If the read goes\r
+                             beyond the end of the file, the read length is\r
+                             truncated to the end of the file.\r
+\r
+                             If This is a directory, the function reads the\r
+                             directory entry at the current position and\r
+                             returns the entry (as EFI_FILE_INFO) in Buffer. If\r
+                             there are no more directory entries, the\r
+                             BufferSize is set to zero on output.\r
+\r
+  @param[out]    Buffer      The buffer into which the data is read.\r
+\r
+  @retval EFI_SUCCESS           Data was read.\r
+  @retval EFI_NO_MEDIA          The device has no medium.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error.\r
+  @retval EFI_DEVICE_ERROR      An attempt was made to read from a deleted\r
+                                file.\r
+  @retval EFI_DEVICE_ERROR      On entry, the current file position is beyond\r
+                                the end of the file.\r
+  @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.\r
+  @retval EFI_BUFFER_TOO_SMALL  The BufferSize is too small to store the\r
+                                current directory entry as a EFI_FILE_INFO\r
+                                structure. BufferSize has been updated with the\r
+                                size needed to complete the request, and the\r
+                                directory position has not been advanced.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileRead (\r
+  IN EFI_FILE_PROTOCOL *This,\r
+  IN OUT UINTN         *BufferSize,\r
+  OUT VOID             *Buffer\r
+  )\r
+{\r
+  STUB_FILE         *StubFile;\r
+  CONST KERNEL_BLOB *Blob;\r
+  UINT64            Left;\r
+\r
+  StubFile = STUB_FILE_FROM_FILE (This);\r
+\r
+  //\r
+  // Scanning the root directory?\r
+  //\r
+  if (StubFile->BlobType == KernelBlobTypeMax) {\r
+    EFI_STATUS Status;\r
+\r
+    if (StubFile->Position == KernelBlobTypeMax) {\r
+      //\r
+      // Scanning complete.\r
+      //\r
+      *BufferSize = 0;\r
+      return EFI_SUCCESS;\r
+    }\r
+\r
+    Status = ConvertKernelBlobTypeToFileInfo (StubFile->Position, BufferSize,\r
+               Buffer);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    ++StubFile->Position;\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Reading a file.\r
+  //\r
+  Blob = &mKernelBlob[StubFile->BlobType];\r
+  if (StubFile->Position > Blob->Size) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  Left = Blob->Size - StubFile->Position;\r
+  if (*BufferSize > Left) {\r
+    *BufferSize = (UINTN)Left;\r
+  }\r
+  if (Blob->Data != NULL) {\r
+    CopyMem (Buffer, Blob->Data + StubFile->Position, *BufferSize);\r
+  }\r
+  StubFile->Position += *BufferSize;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Writes data to a file.\r
+\r
+  @param[in]     This        A pointer to the EFI_FILE_PROTOCOL instance that\r
+                             is the file handle to write data to.\r
+\r
+  @param[in,out] BufferSize  On input, the size of the Buffer. On output, the\r
+                             amount of data actually written. In both cases,\r
+                             the size is measured in bytes.\r
+\r
+  @param[in]     Buffer      The buffer of data to write.\r
+\r
+  @retval EFI_SUCCESS           Data was written.\r
+  @retval EFI_UNSUPPORTED       Writes to open directory files are not\r
+                                supported.\r
+  @retval EFI_NO_MEDIA          The device has no medium.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error.\r
+  @retval EFI_DEVICE_ERROR      An attempt was made to write to a deleted file.\r
+  @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.\r
+  @retval EFI_WRITE_PROTECTED   The file or medium is write-protected.\r
+  @retval EFI_ACCESS_DENIED     The file was opened read only.\r
+  @retval EFI_VOLUME_FULL       The volume is full.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileWrite (\r
+  IN EFI_FILE_PROTOCOL *This,\r
+  IN OUT UINTN         *BufferSize,\r
+  IN VOID              *Buffer\r
+  )\r
+{\r
+  STUB_FILE *StubFile;\r
+\r
+  StubFile = STUB_FILE_FROM_FILE (This);\r
+  return (StubFile->BlobType == KernelBlobTypeMax) ?\r
+         EFI_UNSUPPORTED :\r
+         EFI_WRITE_PROTECTED;\r
+}\r
+\r
+\r
+/**\r
+  Returns a file's current position.\r
+\r
+  @param[in]  This      A pointer to the EFI_FILE_PROTOCOL instance that is the\r
+                        file handle to get the current position on.\r
+\r
+  @param[out] Position  The address to return the file's current position\r
+                        value.\r
+\r
+  @retval EFI_SUCCESS      The position was returned.\r
+  @retval EFI_UNSUPPORTED  The request is not valid on open directories.\r
+  @retval EFI_DEVICE_ERROR An attempt was made to get the position from a\r
+                           deleted file.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileGetPosition (\r
+  IN EFI_FILE_PROTOCOL *This,\r
+  OUT UINT64           *Position\r
+  )\r
+{\r
+  STUB_FILE *StubFile;\r
+\r
+  StubFile = STUB_FILE_FROM_FILE (This);\r
+  if (StubFile->BlobType == KernelBlobTypeMax) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  *Position = StubFile->Position;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Sets a file's current position.\r
+\r
+  @param[in] This      A pointer to the EFI_FILE_PROTOCOL instance that is the\r
+                       file handle to set the requested position on.\r
+\r
+  @param[in] Position  The byte position from the start of the file to set. For\r
+                       regular files, MAX_UINT64 means "seek to end". For\r
+                       directories, zero means "rewind directory scan".\r
+\r
+  @retval EFI_SUCCESS       The position was set.\r
+  @retval EFI_UNSUPPORTED   The seek request for nonzero is not valid on open\r
+                            directories.\r
+  @retval EFI_DEVICE_ERROR  An attempt was made to set the position of a\r
+                            deleted file.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileSetPosition (\r
+  IN EFI_FILE_PROTOCOL *This,\r
+  IN UINT64            Position\r
+  )\r
+{\r
+  STUB_FILE   *StubFile;\r
+  KERNEL_BLOB *Blob;\r
+\r
+  StubFile = STUB_FILE_FROM_FILE (This);\r
+\r
+  if (StubFile->BlobType == KernelBlobTypeMax) {\r
+    if (Position == 0) {\r
+      //\r
+      // rewinding a directory scan is allowed\r
+      //\r
+      StubFile->Position = 0;\r
+      return EFI_SUCCESS;\r
+    }\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  //\r
+  // regular file seek\r
+  //\r
+  Blob = &mKernelBlob[StubFile->BlobType];\r
+  if (Position == MAX_UINT64) {\r
+    //\r
+    // seek to end\r
+    //\r
+    StubFile->Position = Blob->Size;\r
+  } else {\r
+    //\r
+    // absolute seek from beginning -- seeking past the end is allowed\r
+    //\r
+    StubFile->Position = Position;\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Returns information about a file.\r
+\r
+  @param[in]     This             A pointer to the EFI_FILE_PROTOCOL instance\r
+                                  that is the file handle the requested\r
+                                  information is for.\r
+\r
+  @param[in]     InformationType  The type identifier GUID for the information\r
+                                  being requested. The following information\r
+                                  types are supported, storing the\r
+                                  corresponding structures in Buffer:\r
+\r
+                                  - gEfiFileInfoGuid: EFI_FILE_INFO\r
+\r
+                                  - gEfiFileSystemInfoGuid:\r
+                                    EFI_FILE_SYSTEM_INFO\r
+\r
+                                  - gEfiFileSystemVolumeLabelInfoIdGuid:\r
+                                    EFI_FILE_SYSTEM_VOLUME_LABEL\r
+\r
+  @param[in,out] BufferSize       On input, the size of Buffer. On output, the\r
+                                  amount of data returned in Buffer. In both\r
+                                  cases, the size is measured in bytes.\r
+\r
+  @param[out]    Buffer           A pointer to the data buffer to return. The\r
+                                  buffer's type is indicated by\r
+                                  InformationType.\r
+\r
+  @retval EFI_SUCCESS           The information was returned.\r
+  @retval EFI_UNSUPPORTED       The InformationType is not known.\r
+  @retval EFI_NO_MEDIA          The device has no medium.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error.\r
+  @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.\r
+  @retval EFI_BUFFER_TOO_SMALL  The BufferSize is too small to store the\r
+                                information structure requested by\r
+                                InformationType. BufferSize has been updated\r
+                                with the size needed to complete the request.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileGetInfo (\r
+  IN EFI_FILE_PROTOCOL *This,\r
+  IN EFI_GUID          *InformationType,\r
+  IN OUT UINTN         *BufferSize,\r
+  OUT VOID             *Buffer\r
+  )\r
+{\r
+  CONST STUB_FILE *StubFile;\r
+  UINTN           OriginalBufferSize;\r
+\r
+  StubFile = STUB_FILE_FROM_FILE (This);\r
+\r
+  if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {\r
+    return ConvertKernelBlobTypeToFileInfo (StubFile->BlobType, BufferSize,\r
+             Buffer);\r
+  }\r
+\r
+  OriginalBufferSize = *BufferSize;\r
+\r
+  if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {\r
+    EFI_FILE_SYSTEM_INFO *FileSystemInfo;\r
+\r
+    *BufferSize = sizeof *FileSystemInfo;\r
+    if (OriginalBufferSize < *BufferSize) {\r
+      return EFI_BUFFER_TOO_SMALL;\r
+    }\r
+\r
+    FileSystemInfo                 = (EFI_FILE_SYSTEM_INFO *)Buffer;\r
+    FileSystemInfo->Size           = sizeof *FileSystemInfo;\r
+    FileSystemInfo->ReadOnly       = TRUE;\r
+    FileSystemInfo->VolumeSize     = mTotalBlobBytes;\r
+    FileSystemInfo->FreeSpace      = 0;\r
+    FileSystemInfo->BlockSize      = 1;\r
+    FileSystemInfo->VolumeLabel[0] = L'\0';\r
+\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {\r
+    EFI_FILE_SYSTEM_VOLUME_LABEL *FileSystemVolumeLabel;\r
+\r
+    *BufferSize = sizeof *FileSystemVolumeLabel;\r
+    if (OriginalBufferSize < *BufferSize) {\r
+      return EFI_BUFFER_TOO_SMALL;\r
+    }\r
+\r
+    FileSystemVolumeLabel = (EFI_FILE_SYSTEM_VOLUME_LABEL *)Buffer;\r
+    FileSystemVolumeLabel->VolumeLabel[0] = L'\0';\r
+\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  return EFI_UNSUPPORTED;\r
+}\r
+\r
+\r
+/**\r
+  Sets information about a file.\r
+\r
+  @param[in] File             A pointer to the EFI_FILE_PROTOCOL instance that\r
+                              is the file handle the information is for.\r
+\r
+  @param[in] InformationType  The type identifier for the information being\r
+                              set.\r
+\r
+  @param[in] BufferSize       The size, in bytes, of Buffer.\r
+\r
+  @param[in] Buffer           A pointer to the data buffer to write. The\r
+                              buffer's type is indicated by InformationType.\r
+\r
+  @retval EFI_SUCCESS           The information was set.\r
+  @retval EFI_UNSUPPORTED       The InformationType is not known.\r
+  @retval EFI_NO_MEDIA          The device has no medium.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error.\r
+  @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.\r
+  @retval EFI_WRITE_PROTECTED   InformationType is EFI_FILE_INFO_ID and the\r
+                                media is read-only.\r
+  @retval EFI_WRITE_PROTECTED   InformationType is\r
+                                EFI_FILE_PROTOCOL_SYSTEM_INFO_ID and the media\r
+                                is read only.\r
+  @retval EFI_WRITE_PROTECTED   InformationType is\r
+                                EFI_FILE_SYSTEM_VOLUME_LABEL_ID and the media\r
+                                is read-only.\r
+  @retval EFI_ACCESS_DENIED     An attempt is made to change the name of a file\r
+                                to a file that is already present.\r
+  @retval EFI_ACCESS_DENIED     An attempt is being made to change the\r
+                                EFI_FILE_DIRECTORY Attribute.\r
+  @retval EFI_ACCESS_DENIED     An attempt is being made to change the size of\r
+                                a directory.\r
+  @retval EFI_ACCESS_DENIED     InformationType is EFI_FILE_INFO_ID and the\r
+                                file was opened read-only and an attempt is\r
+                                being made to modify a field other than\r
+                                Attribute.\r
+  @retval EFI_VOLUME_FULL       The volume is full.\r
+  @retval EFI_BAD_BUFFER_SIZE   BufferSize is smaller than the size of the type\r
+                                indicated by InformationType.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileSetInfo (\r
+  IN EFI_FILE_PROTOCOL *This,\r
+  IN EFI_GUID          *InformationType,\r
+  IN UINTN             BufferSize,\r
+  IN VOID              *Buffer\r
+  )\r
+{\r
+  return EFI_WRITE_PROTECTED;\r
+}\r
+\r
+\r
+/**\r
+  Flushes all modified data associated with a file to a device.\r
+\r
+  @param [in] This  A pointer to the EFI_FILE_PROTOCOL instance that is the\r
+                    file handle to flush.\r
+\r
+  @retval EFI_SUCCESS           The data was flushed.\r
+  @retval EFI_NO_MEDIA          The device has no medium.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error.\r
+  @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.\r
+  @retval EFI_WRITE_PROTECTED   The file or medium is write-protected.\r
+  @retval EFI_ACCESS_DENIED     The file was opened read-only.\r
+  @retval EFI_VOLUME_FULL       The volume is full.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileFlush (\r
+  IN EFI_FILE_PROTOCOL *This\r
+  )\r
+{\r
+  return EFI_WRITE_PROTECTED;\r
+}\r
+\r
+//\r
+// External definition of the file protocol template.\r
+//\r
+STATIC CONST EFI_FILE_PROTOCOL mEfiFileProtocolTemplate = {\r
+  EFI_FILE_PROTOCOL_REVISION, // revision 1\r
+  StubFileOpen,\r
+  StubFileClose,\r
+  StubFileDelete,\r
+  StubFileRead,\r
+  StubFileWrite,\r
+  StubFileGetPosition,\r
+  StubFileSetPosition,\r
+  StubFileGetInfo,\r
+  StubFileSetInfo,\r
+  StubFileFlush,\r
+  NULL,                       // OpenEx, revision 2\r
+  NULL,                       // ReadEx, revision 2\r
+  NULL,                       // WriteEx, revision 2\r
+  NULL                        // FlushEx, revision 2\r
+};\r
+\r
+\r
+//\r
+// Protocol member functions for SimpleFileSystem.\r
+//\r
+\r
+/**\r
+  Open the root directory on a volume.\r
+\r
+  @param[in]  This  A pointer to the volume to open the root directory on.\r
+\r
+  @param[out] Root  A pointer to the location to return the opened file handle\r
+                    for the root directory in.\r
+\r
+  @retval EFI_SUCCESS           The device was opened.\r
+  @retval EFI_UNSUPPORTED       This volume does not support the requested file\r
+                                system type.\r
+  @retval EFI_NO_MEDIA          The device has no medium.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error.\r
+  @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.\r
+  @retval EFI_ACCESS_DENIED     The service denied access to the file.\r
+  @retval EFI_OUT_OF_RESOURCES  The volume was not opened due to lack of\r
+                                resources.\r
+  @retval EFI_MEDIA_CHANGED     The device has a different medium in it or the\r
+                                medium is no longer supported. Any existing\r
+                                file handles for this volume are no longer\r
+                                valid. To access the files on the new medium,\r
+                                the volume must be reopened with OpenVolume().\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+StubFileSystemOpenVolume (\r
+  IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,\r
+  OUT EFI_FILE_PROTOCOL              **Root\r
+  )\r
+{\r
+  STUB_FILE *StubFile;\r
+\r
+  StubFile = AllocatePool (sizeof *StubFile);\r
+  if (StubFile == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  StubFile->Signature = STUB_FILE_SIG;\r
+  StubFile->BlobType  = KernelBlobTypeMax;\r
+  StubFile->Position  = 0;\r
+  CopyMem (&StubFile->File, &mEfiFileProtocolTemplate,\r
+    sizeof mEfiFileProtocolTemplate);\r
+  *Root = &StubFile->File;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+STATIC CONST EFI_SIMPLE_FILE_SYSTEM_PROTOCOL mFileSystem = {\r
+  EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION,\r
+  StubFileSystemOpenVolume\r
+};\r
+\r
+\r
+//\r
+// Utility functions.\r
+//\r
+\r
+/**\r
+  Populate a blob in mKernelBlob.\r
+\r
+  param[in,out] Blob  Pointer to the KERNEL_BLOB element in mKernelBlob that is\r
+                      to be filled from fw_cfg.\r
+\r
+  @retval EFI_SUCCESS           Blob has been populated. If fw_cfg reported a\r
+                                size of zero for the blob, then Blob->Data has\r
+                                been left unchanged.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES  Failed to allocate memory for Blob->Data.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+FetchBlob (\r
+  IN OUT KERNEL_BLOB *Blob\r
+  )\r
+{\r
+  UINT32 Left;\r
+\r
+  //\r
+  // Read blob size.\r
+  //\r
+  QemuFwCfgSelectItem (Blob->SizeKey);\r
+  Blob->Size = QemuFwCfgRead32 ();\r
+  if (Blob->Size == 0) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Read blob.\r
+  //\r
+  Blob->Data = AllocatePool (Blob->Size);\r
+  if (Blob->Data == NULL) {\r
+    DEBUG ((EFI_D_ERROR, "%a: failed to allocate %Ld bytes for \"%s\"\n",\r
+      __FUNCTION__, (INT64)Blob->Size, Blob->Name));\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  DEBUG ((EFI_D_INFO, "%a: loading %Ld bytes for \"%s\"\n", __FUNCTION__,\r
+    (INT64)Blob->Size, Blob->Name));\r
+  QemuFwCfgSelectItem (Blob->DataKey);\r
+\r
+  Left = Blob->Size;\r
+  do {\r
+    UINT32 Chunk;\r
+\r
+    Chunk = (Left < SIZE_1MB) ? Left : SIZE_1MB;\r
+    QemuFwCfgReadBytes (Chunk, Blob->Data + (Blob->Size - Left));\r
+    Left -= Chunk;\r
+    DEBUG ((EFI_D_VERBOSE, "%a: %Ld bytes remaining for \"%s\"\n",\r
+      __FUNCTION__, (INT64)Left, Blob->Name));\r
+  } while (Left > 0);\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+//\r
+// The entry point of the feature.\r
+//\r
+\r
+/**\r
+  Download the kernel, the initial ramdisk, and the kernel command line from\r
+  QEMU's fw_cfg. Construct a minimal SimpleFileSystem that contains the two\r
+  image files, and load and start the kernel from it.\r
+\r
+  The kernel will be instructed via its command line to load the initrd from\r
+  the same Simple FileSystem.\r
+\r
+  @retval EFI_NOT_FOUND         Kernel image was not found.\r
+  @retval EFI_OUT_OF_RESOURCES  Memory allocation failed.\r
+  @retval EFI_PROTOCOL_ERROR    Unterminated kernel command line.\r
+\r
+  @return                       Error codes from any of the underlying\r
+                                functions. On success, the function doesn't\r
+                                return.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+TryRunningQemuKernel (\r
+  VOID\r
+  )\r
+{\r
+  UINTN                     BlobType;\r
+  KERNEL_BLOB               *CurrentBlob;\r
+  KERNEL_BLOB               *KernelBlob, *InitrdBlob, *CommandLineBlob;\r
+  EFI_STATUS                Status;\r
+  EFI_HANDLE                FileSystemHandle;\r
+  EFI_DEVICE_PATH_PROTOCOL  *KernelDevicePath;\r
+  EFI_HANDLE                KernelImageHandle;\r
+  EFI_LOADED_IMAGE_PROTOCOL *KernelLoadedImage;\r
+\r
+  Status = gRT->GetTime (&mInitTime, NULL /* Capabilities */);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "%a: GetTime(): %r\n", __FUNCTION__, Status));\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Fetch all blobs.\r
+  //\r
+  for (BlobType = 0; BlobType < KernelBlobTypeMax; ++BlobType) {\r
+    CurrentBlob = &mKernelBlob[BlobType];\r
+    Status = FetchBlob (CurrentBlob);\r
+    if (EFI_ERROR (Status)) {\r
+      goto FreeBlobs;\r
+    }\r
+    mTotalBlobBytes += CurrentBlob->Size;\r
+  }\r
+  KernelBlob      = &mKernelBlob[KernelBlobTypeKernel];\r
+  InitrdBlob      = &mKernelBlob[KernelBlobTypeInitrd];\r
+  CommandLineBlob = &mKernelBlob[KernelBlobTypeCommandLine];\r
+\r
+  //\r
+  // Create a new handle with a single VenHw() node device path protocol on it,\r
+  // plus a custom SimpleFileSystem protocol on it.\r
+  //\r
+  FileSystemHandle = NULL;\r
+  Status = gBS->InstallMultipleProtocolInterfaces (&FileSystemHandle,\r
+                  &gEfiDevicePathProtocolGuid,       &mFileSystemDevicePath,\r
+                  &gEfiSimpleFileSystemProtocolGuid, &mFileSystem,\r
+                  NULL);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "%a: InstallMultipleProtocolInterfaces(): %r\n",\r
+      __FUNCTION__, Status));\r
+    goto FreeBlobs;\r
+  }\r
+\r
+  //\r
+  // Create a device path for the kernel image to be loaded from that will call\r
+  // back into our file system.\r
+  //\r
+  KernelDevicePath = FileDevicePath (FileSystemHandle, KernelBlob->Name);\r
+  if (KernelDevicePath == NULL) {\r
+    DEBUG ((EFI_D_ERROR, "%a: failed to allocate kernel device path\n",\r
+      __FUNCTION__));\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto UninstallProtocols;\r
+  }\r
+\r
+  //\r
+  // Load the image. This should call back into our file system.\r
+  //\r
+  Status = gBS->LoadImage (\r
+                  FALSE,             // BootPolicy: exact match required\r
+                  gImageHandle,      // ParentImageHandle\r
+                  KernelDevicePath,\r
+                  NULL,              // SourceBuffer\r
+                  0,                 // SourceSize\r
+                  &KernelImageHandle\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "%a: LoadImage(): %r\n", __FUNCTION__, Status));\r
+    goto FreeKernelDevicePath;\r
+  }\r
+\r
+  //\r
+  // Construct the kernel command line.\r
+  //\r
+  Status = gBS->OpenProtocol (\r
+                  KernelImageHandle,\r
+                  &gEfiLoadedImageProtocolGuid,\r
+                  (VOID **)&KernelLoadedImage,\r
+                  gImageHandle,                  // AgentHandle\r
+                  NULL,                          // ControllerHandle\r
+                  EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  if (CommandLineBlob->Data == NULL) {\r
+    KernelLoadedImage->LoadOptionsSize = 0;\r
+  } else {\r
+    //\r
+    // Verify NUL-termination of the command line.\r
+    //\r
+    if (CommandLineBlob->Data[CommandLineBlob->Size - 1] != '\0') {\r
+      DEBUG ((EFI_D_ERROR, "%a: kernel command line is not NUL-terminated\n",\r
+        __FUNCTION__));\r
+      Status = EFI_PROTOCOL_ERROR;\r
+      goto UnloadKernelImage;\r
+    }\r
+\r
+    //\r
+    // Drop the terminating NUL, convert to UTF-16.\r
+    //\r
+    KernelLoadedImage->LoadOptionsSize = (CommandLineBlob->Size - 1) * 2;\r
+  }\r
+\r
+  if (InitrdBlob->Data != NULL) {\r
+    //\r
+    // Append ' initrd=<name>' in UTF-16.\r
+    //\r
+    KernelLoadedImage->LoadOptionsSize +=\r
+                                        (8 + StrLen(InitrdBlob->Name)) * 2;\r
+  }\r
+\r
+  if (KernelLoadedImage->LoadOptionsSize == 0) {\r
+    KernelLoadedImage->LoadOptions = NULL;\r
+  } else {\r
+    //\r
+    // NUL-terminate in UTF-16.\r
+    //\r
+    KernelLoadedImage->LoadOptionsSize += 2;\r
+\r
+    KernelLoadedImage->LoadOptions = AllocatePool (\r
+                                       KernelLoadedImage->LoadOptionsSize);\r
+    if (KernelLoadedImage->LoadOptions == NULL) {\r
+      KernelLoadedImage->LoadOptionsSize = 0;\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto UnloadKernelImage;\r
+    }\r
+\r
+    UnicodeSPrintAsciiFormat (\r
+      KernelLoadedImage->LoadOptions,\r
+      KernelLoadedImage->LoadOptionsSize,\r
+      "%a%a%s",\r
+      (CommandLineBlob->Data == NULL) ?  "" : (CHAR8 *)CommandLineBlob->Data,\r
+      (InitrdBlob->Data      == NULL) ?  "" : " initrd=",\r
+      (InitrdBlob->Data      == NULL) ? L"" : InitrdBlob->Name\r
+      );\r
+    DEBUG ((EFI_D_INFO, "%a: command line: \"%s\"\n", __FUNCTION__,\r
+      (CHAR16 *)KernelLoadedImage->LoadOptions));\r
+  }\r
+\r
+  //\r
+  // Start the image.\r
+  //\r
+  Status = gBS->StartImage (\r
+                KernelImageHandle,\r
+                NULL,              // ExitDataSize\r
+                NULL               // ExitData\r
+                );\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "%a: StartImage(): %r\n", __FUNCTION__, Status));\r
+  }\r
+\r
+  if (KernelLoadedImage->LoadOptions != NULL) {\r
+    FreePool (KernelLoadedImage->LoadOptions);\r
+  }\r
+  KernelLoadedImage->LoadOptionsSize = 0;\r
+\r
+UnloadKernelImage:\r
+  gBS->UnloadImage (KernelImageHandle);\r
+\r
+FreeKernelDevicePath:\r
+  FreePool (KernelDevicePath);\r
+\r
+UninstallProtocols:\r
+  gBS->UninstallMultipleProtocolInterfaces (FileSystemHandle,\r
+         &gEfiSimpleFileSystemProtocolGuid, &mFileSystem,\r
+         &gEfiDevicePathProtocolGuid,       &mFileSystemDevicePath,\r
+         NULL);\r
+\r
+FreeBlobs:\r
+  while (BlobType > 0) {\r
+    CurrentBlob = &mKernelBlob[--BlobType];\r
+    if (CurrentBlob->Data != NULL) {\r
+      FreePool (CurrentBlob->Data);\r
+      CurrentBlob->Size = 0;\r
+      CurrentBlob->Data = NULL;\r
+    }\r
+  }\r
+\r
+  return Status;\r
+}\r