]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/OvmfXen: Creating an ELF header
authorAnthony PERARD <anthony.perard@citrix.com>
Tue, 13 Aug 2019 11:30:49 +0000 (12:30 +0100)
committerLaszlo Ersek <lersek@redhat.com>
Wed, 21 Aug 2019 16:03:49 +0000 (18:03 +0200)
This patch changes the flash device image of OvmfXen to make it look
like it's an ELF. For this, we replace the empty embedded variable store
by a binary array, which is a ELF file header.

The ELF header explain to a loader to load the binary at the address
1MB, then jump to the PVH entry point which will be created in a later
patch. The header also includes a Xen ELF note that is part of the
PVH ABI.

That patch include OvmfXenElfHeaderGenerator.c which can be use to
regenerate the ELF header, but this will be a manual step.

Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1689
Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20190813113119.14804-6-anthony.perard@citrix.com>

Maintainers.txt
OvmfPkg/OvmfXen.fdf
OvmfPkg/OvmfXenElfHeaderGenerator.c [new file with mode: 0644]

index 42ec8d54afb0ce1e6a94e4987a70d05e2e8ea86c..016523b4010479db5347a4f9ab1ef25656380f1f 100644 (file)
@@ -373,6 +373,7 @@ F: OvmfPkg/Library/XenConsoleSerialPortLib/
 F: OvmfPkg/Library/XenHypercallLib/\r
 F: OvmfPkg/Library/XenIoMmioLib/\r
 F: OvmfPkg/OvmfXen.*\r
+F: OvmfPkg/OvmfXenElfHeaderGenerator.c\r
 F: OvmfPkg/PlatformPei/MemDetect.c\r
 F: OvmfPkg/PlatformPei/Platform.*\r
 F: OvmfPkg/PlatformPei/Xen.*\r
index 2ceff7baa2c78f383bb246087a13a1d655281a81..43c268f6cb4e2b3fd963426e3644ba494691a9ce 100644 (file)
@@ -15,8 +15,8 @@
 !include OvmfPkg.fdf.inc\r
 \r
 #\r
-# Build the variable store and the firmware code as one unified flash device\r
-# image.\r
+# This will allow the flash device image to be recognize as an ELF, with first\r
+# an ELF headers, then the firmware code.\r
 #\r
 [FD.OVMF]\r
 BaseAddress   = $(FW_BASE_ADDRESS)\r
@@ -25,7 +25,102 @@ ErasePolarity = 1
 BlockSize     = $(BLOCK_SIZE)\r
 NumBlocks     = $(FW_BLOCKS)\r
 \r
-!include VarStore.fdf.inc\r
+!if ($(FD_SIZE_IN_KB) == 1024) || ($(FD_SIZE_IN_KB) == 2048)\r
+0x00000000|0x0000e000\r
+!endif\r
+!if $(FD_SIZE_IN_KB) == 4096\r
+0x00000000|0x00040000\r
+!endif\r
+DATA = {\r
+  #\r
+  # This hex array have been generated by OvmfPkg/OvmfXenElfHeaderGenerator.c\r
+  # and copied manually.\r
+  #\r
+  # ELF file header\r
+  0x7f, 0x45, 0x4c, 0x46, # e_ident[0..3]: Magic number\r
+  0x01, # File class: 32-bit objects\r
+  0x01, # Data encoding: 2's complement, little endian\r
+  0x01, # File version\r
+  0x03, # OS ABI identification: Object uses GNU ELF extensions\r
+  0x00, # ABI version\r
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  # e_ident[EI_PAD...]\r
+  0x02, 0x00, # e_type = Executable file\r
+  0x03, 0x00, # e_machine = Intel 80386\r
+  0x01, 0x00, 0x00, 0x00, # e_version\r
+  0xd0, 0xff, 0x2f, 0x00, # e_entry: Entry point virtual address\r
+  0x34, 0x00, 0x00, 0x00, # e_phoff: Program header table file offset\r
+  0x00, 0x00, 0x00, 0x00, # e_shoff: Section header table file offset\r
+  0x00, 0x00, 0x00, 0x00, # e_flags: Processor-specific flags\r
+  0x34, 0x00, #    e_ehsize: ELF header size\r
+  0x20, 0x00, # e_phentsize: Program header table entry size\r
+  0x02, 0x00, #     e_phnum: Program header table entry count\r
+  0x00, 0x00, # e_shentsize: Section header table entry size\r
+  0x00, 0x00, #     e_shnum: Section header table entry count\r
+  0x00, 0x00, # e_shstrndx\r
+\r
+  # ELF Program segment headers\r
+  # - Load segment\r
+  0x01, 0x00, 0x00, 0x00, # p_type = Loadable program segment\r
+  0x00, 0x00, 0x00, 0x00, # p_offset\r
+  0x00, 0x00, 0x10, 0x00, # p_vaddr: Segment virtual address\r
+  0x00, 0x00, 0x10, 0x00, # p_paddr: Segment physical address\r
+  0x00, 0x00, 0x20, 0x00, # p_filesz: Segment size in file\r
+  0x00, 0x00, 0x20, 0x00, # p_memsz: Segment size in memory\r
+  0x07, 0x00, 0x00, 0x00, # p_flags = Segment is executable | writable | readable\r
+  0x00, 0x00, 0x00, 0x00, # p_align\r
+  # - ELFNOTE segment\r
+  0x04, 0x00, 0x00, 0x00, # p_type = PT_NOTE\r
+  0x74, 0x00, 0x00, 0x00, # p_offset = point to XEN_ELFNOTE_PHYS32_ENTRY below\r
+  0x74, 0x00, 0x10, 0x00,\r
+  0x74, 0x00, 0x10, 0x00,\r
+  0x14, 0x00, 0x00, 0x00,\r
+  0x14, 0x00, 0x00, 0x00,\r
+  0x04, 0x00, 0x00, 0x00, # p_flags = Segment is readable\r
+  0x00, 0x00, 0x00, 0x00,\r
+\r
+  # XEN_ELFNOTE_PHYS32_ENTRY\r
+  0x04, 0x00, 0x00, 0x00, # name size\r
+  0x04, 0x00, 0x00, 0x00, # desc size\r
+  0x12, 0x00, 0x00, 0x00, # type = XEN_ELFNOTE_PHYS32_ENTRY\r
+  0x58, 0x65, 0x6e, 0x00, # name = "Xen"\r
+  0xd0, 0xff, 0x2f, 0x00, # desc: PVH entry point\r
+  0x00\r
+}\r
+\r
+!if ($(FD_SIZE_IN_KB) == 1024) || ($(FD_SIZE_IN_KB) == 2048)\r
+0x0000e000|0x00001000\r
+!endif\r
+!if $(FD_SIZE_IN_KB) == 4096\r
+0x00040000|0x00001000\r
+!endif\r
+#NV_EVENT_LOG\r
+\r
+!if ($(FD_SIZE_IN_KB) == 1024) || ($(FD_SIZE_IN_KB) == 2048)\r
+0x0000f000|0x00001000\r
+!endif\r
+!if $(FD_SIZE_IN_KB) == 4096\r
+0x00041000|0x00001000\r
+!endif\r
+#NV_FTW_WORKING\r
+DATA = {\r
+  # EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER->Signature = gEdkiiWorkingBlockSignatureGuid         =\r
+  #  { 0x9e58292b, 0x7c68, 0x497d, { 0xa0, 0xce, 0x65,  0x0, 0xfd, 0x9f, 0x1b, 0x95 }}\r
+  0x2b, 0x29, 0x58, 0x9e, 0x68, 0x7c, 0x7d, 0x49,\r
+  0xa0, 0xce, 0x65,  0x0, 0xfd, 0x9f, 0x1b, 0x95,\r
+  # Crc:UINT32            #WorkingBlockValid:1, WorkingBlockInvalid:1, Reserved\r
+  0x2c, 0xaf, 0x2c, 0x64, 0xFE, 0xFF, 0xFF, 0xFF,\r
+  # WriteQueueSize: UINT64\r
+  0xE0, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00\r
+}\r
+\r
+!if ($(FD_SIZE_IN_KB) == 1024) || ($(FD_SIZE_IN_KB) == 2048)\r
+0x00010000|0x00010000\r
+!endif\r
+!if $(FD_SIZE_IN_KB) == 4096\r
+0x00042000|0x00042000\r
+!endif\r
+#NV_FTW_SPARE\r
+\r
 \r
 $(VARS_SIZE)|$(FVMAIN_SIZE)\r
 FV = FVMAIN_COMPACT\r
diff --git a/OvmfPkg/OvmfXenElfHeaderGenerator.c b/OvmfPkg/OvmfXenElfHeaderGenerator.c
new file mode 100644 (file)
index 0000000..90f0574
--- /dev/null
@@ -0,0 +1,140 @@
+/** @file\r
+  This program generates a hex array to be manually coppied into\r
+  OvmfXen.fdf.\r
+\r
+  The purpose is for the flash device image to be recognize as an ELF.\r
+\r
+  Copyright (c) 2019, Citrix Systems, Inc.\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include "elf.h"\r
+#include "stdio.h"\r
+#include "stddef.h"\r
+\r
+void print_hdr(void *s, size_t size)\r
+{\r
+  char *c = s;\r
+\r
+  while (size--) {\r
+    printf("0x%02hhx, ", *(c++));\r
+  }\r
+}\r
+\r
+/* Format for the XEN_ELFNOTE_PHYS32_ENTRY program segment */\r
+#define XEN_ELFNOTE_PHYS32_ENTRY 18\r
+typedef struct {\r
+  uint32_t name_size;\r
+  uint32_t desc_size;\r
+  uint32_t type;\r
+  char name[4];\r
+  uint32_t desc;\r
+} xen_elfnote_phys32_entry;\r
+\r
+int main(void)\r
+{\r
+  /* FW_SIZE */\r
+  size_t ovmf_blob_size = 0x00200000;\r
+  /* Load OVMF at 1MB when running as PVH guest */\r
+  uint32_t ovmf_base_address = 0x00100000;\r
+  /* Xen PVH entry point */\r
+  uint32_t ovmfxen_pvh_entry_point = ovmf_base_address + ovmf_blob_size - 0x30;\r
+  size_t offset_into_file = 0;\r
+\r
+  /* ELF file header */\r
+  Elf32_Ehdr hdr = {\r
+    .e_ident = ELFMAG,\r
+    .e_type = ET_EXEC,\r
+    .e_machine = EM_386,\r
+    .e_version = EV_CURRENT,\r
+    .e_entry = ovmfxen_pvh_entry_point,\r
+    .e_flags = R_386_NONE,\r
+    .e_ehsize = sizeof (hdr),\r
+    .e_phentsize = sizeof (Elf32_Phdr),\r
+  };\r
+  offset_into_file += sizeof (hdr);\r
+\r
+  hdr.e_ident[EI_CLASS] = ELFCLASS32;\r
+  hdr.e_ident[EI_DATA] = ELFDATA2LSB;\r
+  hdr.e_ident[EI_VERSION] = EV_CURRENT;\r
+  hdr.e_ident[EI_OSABI] = ELFOSABI_LINUX;\r
+  /* Placing program headers just after hdr */\r
+  hdr.e_phoff = sizeof (hdr);\r
+\r
+  /* program header */\r
+  Elf32_Phdr phdr_load = {\r
+    .p_type = PT_LOAD,\r
+    .p_offset = 0, /* load everything */\r
+    .p_paddr = ovmf_base_address,\r
+    .p_filesz = ovmf_blob_size,\r
+    .p_memsz = ovmf_blob_size,\r
+    .p_flags = PF_X | PF_W | PF_R,\r
+    .p_align = 0,\r
+  };\r
+  phdr_load.p_vaddr = phdr_load.p_paddr;\r
+  hdr.e_phnum += 1;\r
+  offset_into_file += sizeof (phdr_load);\r
+\r
+  /* Xen ELF Note. */\r
+\r
+  xen_elfnote_phys32_entry xen_elf_note = {\r
+    .type = XEN_ELFNOTE_PHYS32_ENTRY,\r
+    .name = "Xen",\r
+    .desc = ovmfxen_pvh_entry_point,\r
+    .name_size =\r
+      offsetof (xen_elfnote_phys32_entry, desc) -\r
+      offsetof (xen_elfnote_phys32_entry, name),\r
+    .desc_size =\r
+      sizeof (xen_elfnote_phys32_entry) -\r
+      offsetof (xen_elfnote_phys32_entry, desc),\r
+  };\r
+  Elf32_Phdr phdr_note = {\r
+    .p_type = PT_NOTE,\r
+    .p_filesz = sizeof (xen_elf_note),\r
+    .p_memsz = sizeof (xen_elf_note),\r
+    .p_flags = PF_R,\r
+    .p_align = 0,\r
+  };\r
+  hdr.e_phnum += 1;\r
+  offset_into_file += sizeof (phdr_note);\r
+  phdr_note.p_offset = offset_into_file;\r
+  phdr_note.p_paddr = ovmf_base_address + phdr_note.p_offset;\r
+  phdr_note.p_vaddr = phdr_note.p_paddr;\r
+\r
+\r
+  /*\r
+   * print elf header\r
+   */\r
+\r
+  size_t i;\r
+  size_t hdr_size = sizeof (hdr);\r
+  size_t entry_off = offsetof(typeof(hdr), e_entry);\r
+\r
+  printf("# ELF file header\n");\r
+  print_hdr(&hdr, entry_off);\r
+  printf("\n");\r
+  print_hdr(&hdr.e_entry, sizeof (hdr.e_entry));\r
+  printf(" # hdr.e_entry\n");\r
+  print_hdr(&hdr.e_entry + 1, hdr_size - entry_off - sizeof (hdr.e_entry));\r
+\r
+  printf("\n\n# ELF Program segment headers\n");\r
+  printf("# - Load segment\n");\r
+  for (i = 0; i < sizeof (phdr_load); i += 4) {\r
+    print_hdr(((char*)&phdr_load) + i, 4);\r
+    printf("\n");\r
+  }\r
+  printf("# - ELFNOTE segment\n");\r
+  for (i = 0; i < sizeof (phdr_note); i += 4) {\r
+    print_hdr(((char*)&phdr_note) + i, 4);\r
+    printf("\n");\r
+  }\r
+\r
+  printf("\n# XEN_ELFNOTE_PHYS32_ENTRY\n");\r
+  for (i = 0; i < sizeof (xen_elf_note); i += 4) {\r
+    print_hdr(((char*)&xen_elf_note) + i, 4);\r
+    printf("\n");\r
+  }\r
+\r
+  return 0;\r
+}\r