From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=softfail (domain: citrix.com, ip: , mailfrom: anthony.perard@citrix.com) Received: from esa2.hc3370-68.iphmx.com (esa2.hc3370-68.iphmx.com []) by groups.io with SMTP; Thu, 04 Jul 2019 07:42:37 -0700 Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=anthony.perard@citrix.com; spf=Pass smtp.mailfrom=anthony.perard@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of anthony.perard@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="anthony.perard@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa2.hc3370-68.iphmx.com: domain of anthony.perard@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="anthony.perard@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: oWZxkNLqqPmlqfn+TXI9xs2s6uB23HpmXn60oaqU9O1DjC2JSgeu/F6/Xfhg+7u8DnLBPyHqWB qTSas+1uAhvbOAqrT63LlNc7TBmwLvLAZAPGfTgug5FDyJpPpl0dT7NLiGQHNwFUBH/2SWnej3 KF1TPdHFhWVAKZdTq5DPkZMfIBy+B5QKOdIXgKGg2nRpPsMzRX8imCT+VDeX7Bb3eaNpeywLvi F1rjLhT+f7s1LLinckddJrArstNHiF0phtB1D3baVkLBLWLOA6DwnGFFFETiRxmr045IbC4E1t 5bg= X-SBRS: 2.7 X-MesageID: 2588783 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,451,1557201600"; d="scan'208";a="2588783" From: "Anthony PERARD" To: CC: , Ard Biesheuvel , Jordan Justen , Laszlo Ersek , Julien Grall , Anthony PERARD Subject: [PATCH v3 05/35] OvmfPkg/OvmfXen: Creating an ELF header Date: Thu, 4 Jul 2019 15:42:03 +0100 Message-ID: <20190704144233.27968-6-anthony.perard@citrix.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190704144233.27968-1-anthony.perard@citrix.com> References: <20190704144233.27968-1-anthony.perard@citrix.com> MIME-Version: 1.0 Return-Path: anthony.perard@citrix.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain 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=3D1689 Signed-off-by: Anthony PERARD --- Notes: v3: - added license to generate_elf_header.c which is renamed to OvmfPkg/OvmfXenElfHeaderGenerator.c - added an ELF NOTE into the header OvmfPkg/OvmfXen.fdf | 101 +++++++++++++++++++- OvmfPkg/OvmfXenElfHeaderGenerator.c | 140 ++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 OvmfPkg/OvmfXenElfHeaderGenerator.c diff --git a/OvmfPkg/OvmfXen.fdf b/OvmfPkg/OvmfXen.fdf index 2ceff7baa2..43c268f6cb 100644 --- a/OvmfPkg/OvmfXen.fdf +++ b/OvmfPkg/OvmfXen.fdf @@ -15,8 +15,8 @@ [Defines] !include OvmfPkg.fdf.inc=0D =0D #=0D -# Build the variable store and the firmware code as one unified flash devi= ce=0D -# image.=0D +# This will allow the flash device image to be recognize as an ELF, with f= irst=0D +# an ELF headers, then the firmware code.=0D #=0D [FD.OVMF]=0D BaseAddress =3D $(FW_BASE_ADDRESS)=0D @@ -25,7 +25,102 @@ [FD.OVMF] BlockSize =3D $(BLOCK_SIZE)=0D NumBlocks =3D $(FW_BLOCKS)=0D =0D -!include VarStore.fdf.inc=0D +!if ($(FD_SIZE_IN_KB) =3D=3D 1024) || ($(FD_SIZE_IN_KB) =3D=3D 2048)=0D +0x00000000|0x0000e000=0D +!endif=0D +!if $(FD_SIZE_IN_KB) =3D=3D 4096=0D +0x00000000|0x00040000=0D +!endif=0D +DATA =3D {=0D + #=0D + # This hex array have been generated by OvmfPkg/OvmfXenElfHeaderGenerato= r.c=0D + # and copied manually.=0D + #=0D + # ELF file header=0D + 0x7f, 0x45, 0x4c, 0x46, # e_ident[0..3]: Magic number=0D + 0x01, # File class: 32-bit objects=0D + 0x01, # Data encoding: 2's complement, little endian=0D + 0x01, # File version=0D + 0x03, # OS ABI identification: Object uses GNU ELF extensions=0D + 0x00, # ABI version=0D + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # e_ident[EI_PAD...]=0D + 0x02, 0x00, # e_type =3D Executable file=0D + 0x03, 0x00, # e_machine =3D Intel 80386=0D + 0x01, 0x00, 0x00, 0x00, # e_version=0D + 0xd0, 0xff, 0x2f, 0x00, # e_entry: Entry point virtual address=0D + 0x34, 0x00, 0x00, 0x00, # e_phoff: Program header table file offset=0D + 0x00, 0x00, 0x00, 0x00, # e_shoff: Section header table file offset=0D + 0x00, 0x00, 0x00, 0x00, # e_flags: Processor-specific flags=0D + 0x34, 0x00, # e_ehsize: ELF header size=0D + 0x20, 0x00, # e_phentsize: Program header table entry size=0D + 0x02, 0x00, # e_phnum: Program header table entry count=0D + 0x00, 0x00, # e_shentsize: Section header table entry size=0D + 0x00, 0x00, # e_shnum: Section header table entry count=0D + 0x00, 0x00, # e_shstrndx=0D +=0D + # ELF Program segment headers=0D + # - Load segment=0D + 0x01, 0x00, 0x00, 0x00, # p_type =3D Loadable program segment=0D + 0x00, 0x00, 0x00, 0x00, # p_offset=0D + 0x00, 0x00, 0x10, 0x00, # p_vaddr: Segment virtual address=0D + 0x00, 0x00, 0x10, 0x00, # p_paddr: Segment physical address=0D + 0x00, 0x00, 0x20, 0x00, # p_filesz: Segment size in file=0D + 0x00, 0x00, 0x20, 0x00, # p_memsz: Segment size in memory=0D + 0x07, 0x00, 0x00, 0x00, # p_flags =3D Segment is executable | writable |= readable=0D + 0x00, 0x00, 0x00, 0x00, # p_align=0D + # - ELFNOTE segment=0D + 0x04, 0x00, 0x00, 0x00, # p_type =3D PT_NOTE=0D + 0x74, 0x00, 0x00, 0x00, # p_offset =3D point to XEN_ELFNOTE_PHYS32_ENTRY= below=0D + 0x74, 0x00, 0x10, 0x00,=0D + 0x74, 0x00, 0x10, 0x00,=0D + 0x14, 0x00, 0x00, 0x00,=0D + 0x14, 0x00, 0x00, 0x00,=0D + 0x04, 0x00, 0x00, 0x00, # p_flags =3D Segment is readable=0D + 0x00, 0x00, 0x00, 0x00,=0D +=0D + # XEN_ELFNOTE_PHYS32_ENTRY=0D + 0x04, 0x00, 0x00, 0x00, # name size=0D + 0x04, 0x00, 0x00, 0x00, # desc size=0D + 0x12, 0x00, 0x00, 0x00, # type =3D XEN_ELFNOTE_PHYS32_ENTRY=0D + 0x58, 0x65, 0x6e, 0x00, # name =3D "Xen"=0D + 0xd0, 0xff, 0x2f, 0x00, # desc: PVH entry point=0D + 0x00=0D +}=0D +=0D +!if ($(FD_SIZE_IN_KB) =3D=3D 1024) || ($(FD_SIZE_IN_KB) =3D=3D 2048)=0D +0x0000e000|0x00001000=0D +!endif=0D +!if $(FD_SIZE_IN_KB) =3D=3D 4096=0D +0x00040000|0x00001000=0D +!endif=0D +#NV_EVENT_LOG=0D +=0D +!if ($(FD_SIZE_IN_KB) =3D=3D 1024) || ($(FD_SIZE_IN_KB) =3D=3D 2048)=0D +0x0000f000|0x00001000=0D +!endif=0D +!if $(FD_SIZE_IN_KB) =3D=3D 4096=0D +0x00041000|0x00001000=0D +!endif=0D +#NV_FTW_WORKING=0D +DATA =3D {=0D + # EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER->Signature =3D gEdkiiWorkingBl= ockSignatureGuid =3D=0D + # { 0x9e58292b, 0x7c68, 0x497d, { 0xa0, 0xce, 0x65, 0x0, 0xfd, 0x9f, 0= x1b, 0x95 }}=0D + 0x2b, 0x29, 0x58, 0x9e, 0x68, 0x7c, 0x7d, 0x49,=0D + 0xa0, 0xce, 0x65, 0x0, 0xfd, 0x9f, 0x1b, 0x95,=0D + # Crc:UINT32 #WorkingBlockValid:1, WorkingBlockInvalid:1, Res= erved=0D + 0x2c, 0xaf, 0x2c, 0x64, 0xFE, 0xFF, 0xFF, 0xFF,=0D + # WriteQueueSize: UINT64=0D + 0xE0, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00=0D +}=0D +=0D +!if ($(FD_SIZE_IN_KB) =3D=3D 1024) || ($(FD_SIZE_IN_KB) =3D=3D 2048)=0D +0x00010000|0x00010000=0D +!endif=0D +!if $(FD_SIZE_IN_KB) =3D=3D 4096=0D +0x00042000|0x00042000=0D +!endif=0D +#NV_FTW_SPARE=0D +=0D =0D $(VARS_SIZE)|$(FVMAIN_SIZE)=0D FV =3D FVMAIN_COMPACT=0D diff --git a/OvmfPkg/OvmfXenElfHeaderGenerator.c b/OvmfPkg/OvmfXenElfHeader= Generator.c new file mode 100644 index 0000000000..6cbad8fbf7 --- /dev/null +++ b/OvmfPkg/OvmfXenElfHeaderGenerator.c @@ -0,0 +1,140 @@ +/*=0D + * @file=0D + * This program generates a hex array to be manually coppied into=0D + * OvmfXen.fdf.=0D + * The purpose is for the flash device image to be recognize as an ELF.=0D + *=0D + * Copyright (c) 2019, Citrix Systems, Inc.=0D + *=0D + * SPDX-License-Identifier: BSD-2-Clause-Patent=0D + */=0D +=0D +#include "elf.h"=0D +#include "stdio.h"=0D +#include "stddef.h"=0D +=0D +void print_hdr(void *s, size_t size)=0D +{=0D + char *c =3D s;=0D +=0D + while (size--) {=0D + printf("0x%02hhx, ", *(c++));=0D + }=0D +}=0D +=0D +/* Format for the XEN_ELFNOTE_PHYS32_ENTRY program segment */=0D +#define XEN_ELFNOTE_PHYS32_ENTRY 18=0D +typedef struct {=0D + uint32_t name_size;=0D + uint32_t desc_size;=0D + uint32_t type;=0D + char name[4];=0D + uint32_t desc;=0D +} xen_elfnote_phys32_entry;=0D +=0D +int main(void)=0D +{=0D + /* FW_SIZE */=0D + size_t ovmf_blob_size =3D 0x00200000;=0D + /* Load OVMF at 1MB when running as PVH guest */=0D + uint32_t ovmf_base_address =3D 0x00100000;=0D + /* Xen PVH entry point */=0D + uint32_t ovmfxen_pvh_entry_point =3D ovmf_base_address + ovmf_blob_size = - 0x30;=0D + size_t offset_into_file =3D 0;=0D +=0D + /* ELF file header */=0D + Elf32_Ehdr hdr =3D {=0D + .e_ident =3D ELFMAG,=0D + .e_type =3D ET_EXEC,=0D + .e_machine =3D EM_386,=0D + .e_version =3D EV_CURRENT,=0D + .e_entry =3D ovmfxen_pvh_entry_point,=0D + .e_flags =3D R_386_NONE,=0D + .e_ehsize =3D sizeof (hdr),=0D + .e_phentsize =3D sizeof (Elf32_Phdr),=0D + };=0D + offset_into_file +=3D sizeof (hdr);=0D +=0D + hdr.e_ident[EI_CLASS] =3D ELFCLASS32;=0D + hdr.e_ident[EI_DATA] =3D ELFDATA2LSB;=0D + hdr.e_ident[EI_VERSION] =3D EV_CURRENT;=0D + hdr.e_ident[EI_OSABI] =3D ELFOSABI_LINUX;=0D + /* Placing program headers just after hdr */=0D + hdr.e_phoff =3D sizeof (hdr);=0D +=0D + /* program header */=0D + Elf32_Phdr phdr_load =3D {=0D + .p_type =3D PT_LOAD,=0D + .p_offset =3D 0, /* load everything */=0D + .p_paddr =3D ovmf_base_address,=0D + .p_filesz =3D ovmf_blob_size,=0D + .p_memsz =3D ovmf_blob_size,=0D + .p_flags =3D PF_X | PF_W | PF_R,=0D + .p_align =3D 0,=0D + };=0D + phdr_load.p_vaddr =3D phdr_load.p_paddr;=0D + hdr.e_phnum +=3D 1;=0D + offset_into_file +=3D sizeof (phdr_load);=0D +=0D + /* Xen ELF Note. */=0D +=0D + xen_elfnote_phys32_entry xen_elf_note =3D {=0D + .type =3D XEN_ELFNOTE_PHYS32_ENTRY,=0D + .name =3D "Xen",=0D + .desc =3D ovmfxen_pvh_entry_point,=0D + .name_size =3D=0D + offsetof (xen_elfnote_phys32_entry, desc) -=0D + offsetof (xen_elfnote_phys32_entry, name),=0D + .desc_size =3D=0D + sizeof (xen_elfnote_phys32_entry) -=0D + offsetof (xen_elfnote_phys32_entry, desc),=0D + };=0D + Elf32_Phdr phdr_note =3D {=0D + .p_type =3D PT_NOTE,=0D + .p_filesz =3D sizeof (xen_elf_note),=0D + .p_memsz =3D sizeof (xen_elf_note),=0D + .p_flags =3D PF_R,=0D + .p_align =3D 0,=0D + };=0D + hdr.e_phnum +=3D 1;=0D + offset_into_file +=3D sizeof (phdr_note);=0D + phdr_note.p_offset =3D offset_into_file;=0D + phdr_note.p_paddr =3D ovmf_base_address + phdr_note.p_offset;=0D + phdr_note.p_vaddr =3D phdr_note.p_paddr;=0D +=0D +=0D + /*=0D + * print elf header=0D + */=0D +=0D + size_t i;=0D + size_t hdr_size =3D sizeof (hdr);=0D + size_t entry_off =3D offsetof(typeof(hdr), e_entry);=0D +=0D + printf("# ELF file header\n");=0D + print_hdr(&hdr, entry_off);=0D + printf("\n");=0D + print_hdr(&hdr.e_entry, sizeof (hdr.e_entry));=0D + printf(" # hdr.e_entry\n");=0D + print_hdr(&hdr.e_entry + 1, hdr_size - entry_off - sizeof (hdr.e_entry))= ;=0D +=0D + printf("\n\n# ELF Program segment headers\n");=0D + printf("# - Load segment\n");=0D + for (i =3D 0; i < sizeof (phdr_load); i +=3D 4) {=0D + print_hdr(((char*)&phdr_load) + i, 4);=0D + printf("\n");=0D + }=0D + printf("# - ELFNOTE segment\n");=0D + for (i =3D 0; i < sizeof (phdr_note); i +=3D 4) {=0D + print_hdr(((char*)&phdr_note) + i, 4);=0D + printf("\n");=0D + }=0D +=0D + printf("\n# XEN_ELFNOTE_PHYS32_ENTRY\n");=0D + for (i =3D 0; i < sizeof (xen_elf_note); i +=3D 4) {=0D + print_hdr(((char*)&xen_elf_note) + i, 4);=0D + printf("\n");=0D + }=0D +=0D + return 0;=0D +}=0D --=20 Anthony PERARD