From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by mx.groups.io with SMTP id smtpd.web10.7218.1646227914344900897 for ; Wed, 02 Mar 2022 05:31:57 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=CmyjB6Ro; spf=pass (domain: intel.com, ip: 134.134.136.20, mailfrom: sebastien.boeuf@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646227917; x=1677763917; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UgRNkhu7zxkwOHIKJc+HmlIpde3up/16c9Copgs3whM=; b=CmyjB6RodFX66qKVmUSGaojTf1Niiys2Qf7G5Mkt6QEHGpvSy3x2Orb2 DE8qZft7YRmjGCo/oT12U9njEgQmnWXoruRcdTDojsE6/iOv1Zu0BSjNa zPCIXeny7YnWlXyKgOwAhsau3DjpjKjUBD4NaaLYiO5wU4za1Jc8bPnUG E3Ca/gFm2O2B40WQcqee03pAsUAdy7vw+mBwYm97VH/c6Fx9lux4nmmqn 9LLUdkQbzjLyUdRAf4yu7ox+tCEhdtYm/t8XdcINI60+lV5ephE1dlQyM bNaWKB2Pngum4By8vtTCgEl4o/t8eaWwEZ2a3/RUsWTZIWrwcz/nnrUJR w==; X-IronPort-AV: E=McAfee;i="6200,9189,10273"; a="240813671" X-IronPort-AV: E=Sophos;i="5.90,149,1643702400"; d="scan'208";a="240813671" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Mar 2022 05:31:53 -0800 X-IronPort-AV: E=Sophos;i="5.90,149,1643702400"; d="scan'208";a="535389198" Received: from spearce-mobl1.amr.corp.intel.com (HELO sboeuf-mobl.home) ([10.252.27.130]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Mar 2022 05:31:52 -0800 From: "Boeuf, Sebastien" To: devel@edk2.groups.io Cc: jiewen.yao@intel.com, jordan.l.justen@intel.com, kraxel@redhat.com, sebastien.boeuf@intel.com Subject: [PATCH v6 1/8] OvmfPkg: Make the Xen ELF header generator more flexible Date: Wed, 2 Mar 2022 14:31:30 +0100 Message-Id: <57faf25b046346821a99adbcf9a6718b8ca31a47.1646227817.git.sebastien.boeuf@intel.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable From: Sebastien Boeuf Adding some flexibility to the program through optional parameters and global define, so that other targets can use the generator. * A global define is added so that we can choose at build time if we want to use 32-bit or 64-bit base structures. * A first optional parameter is added so the user can provide the expected blob size of the generated binary. * A second optional parameter is added so the user can specify an output file to which the generated output will be printed. The default behavior isn't modified. Acked-by: Gerd Hoffmann Signed-off-by: Sebastien Boeuf --- OvmfPkg/OvmfXenElfHeaderGenerator.c | 141 +++++++++++++++++++++------- 1 file changed, 109 insertions(+), 32 deletions(-) diff --git a/OvmfPkg/OvmfXenElfHeaderGenerator.c b/OvmfPkg/OvmfXenElfHeader= Generator.c index 489060cdad..672129b85d 100644 --- a/OvmfPkg/OvmfXenElfHeaderGenerator.c +++ b/OvmfPkg/OvmfXenElfHeaderGenerator.c @@ -10,19 +10,31 @@ **/ = #include "elf.h" -#include "stdio.h" +#include "fcntl.h" +#include "stdbool.h" #include "stddef.h" +#include "stdio.h" +#include "stdlib.h" = void print_hdr ( + FILE *file, void *s, - size_t size + size_t size, + bool end_delimiter ) { char *c =3D s; = - while (size--) { - printf ("0x%02hhx, ", *(c++)); + fprintf (file, " "); + while (size-- > 1) { + fprintf (file, "0x%02hhx, ", *(c++)); + } + + if (end_delimiter) { + fprintf (file, "0x%02hhx,", *c); + } else { + fprintf (file, "0x%02hhx", *c); } } = @@ -36,34 +48,79 @@ typedef struct { uint32_t desc; } xen_elfnote_phys32_entry; = +#define LICENSE_HDR "\ +## @file\r\n\ +# FDF include file that defines a PVH ELF header.\r\n\ +#\r\n\ +# Copyright (c) 2022, Intel Corporation. All rights reserved.\r\n\ +#\r\n\ +# SPDX-License-Identifier: BSD-2-Clause-Patent\r\n\ +#\r\n\ +##\r\n\ +\r\n\ +" + int main ( - void + int argc, + char *argv[] ) { /* FW_SIZE */ size_t ovmf_blob_size =3D 0x00200000; /* Load OVMF at 1MB when running as PVH guest */ uint32_t ovmf_base_address =3D 0x00100000; + uint32_t ovmfxen_pvh_entry_point; + size_t offset_into_file =3D 0; + char *endptr, *str; + long param; + FILE *file =3D stdout; + + /* Parse the size parameter */ + if (argc > 1) { + str =3D argv[1]; + param =3D strtol (str, &endptr, 10); + if (endptr !=3D str) { + ovmf_blob_size =3D (size_t)param; + } + } + + /* Parse the filepath parameter */ + if (argc > 2) { + file =3D fopen (argv[2], "w"); + fprintf (file, LICENSE_HDR); + } + /* Xen PVH entry point */ - uint32_t ovmfxen_pvh_entry_point =3D ovmf_base_address + ovmf_blob_size= - 0x30; - size_t offset_into_file =3D 0; + ovmfxen_pvh_entry_point =3D ovmf_base_address + ovmf_blob_size - 0x30; = /* ELF file header */ + #ifdef PVH64 + Elf64_Ehdr hdr =3D { + #else Elf32_Ehdr hdr =3D { - .e_ident =3D ELFMAG, - .e_type =3D ET_EXEC, - .e_machine =3D EM_386, - .e_version =3D EV_CURRENT, - .e_entry =3D ovmfxen_pvh_entry_point, - .e_flags =3D R_386_NONE, - .e_ehsize =3D sizeof (hdr), + #endif + .e_ident =3D ELFMAG, + .e_type =3D ET_EXEC, + .e_machine =3D EM_386, + .e_version =3D EV_CURRENT, + .e_entry =3D ovmfxen_pvh_entry_point, + .e_flags =3D R_386_NONE, + .e_ehsize =3D sizeof (hdr), + #ifdef PVH64 + .e_phentsize =3D sizeof (Elf64_Phdr), + #else .e_phentsize =3D sizeof (Elf32_Phdr), + #endif }; = offset_into_file +=3D sizeof (hdr); = - hdr.e_ident[EI_CLASS] =3D ELFCLASS32; + #ifdef PVH64 + hdr.e_ident[EI_CLASS] =3D ELFCLASS64; + #else + hdr.e_ident[EI_CLASS] =3D ELFCLASS32; + #endif hdr.e_ident[EI_DATA] =3D ELFDATA2LSB; hdr.e_ident[EI_VERSION] =3D EV_CURRENT; hdr.e_ident[EI_OSABI] =3D ELFOSABI_LINUX; @@ -71,14 +128,22 @@ main ( hdr.e_phoff =3D sizeof (hdr); = /* program header */ + #ifdef PVH64 + Elf64_Phdr phdr_load =3D { + #else Elf32_Phdr phdr_load =3D { + #endif .p_type =3D PT_LOAD, .p_offset =3D 0, /* load everything */ .p_paddr =3D ovmf_base_address, .p_filesz =3D ovmf_blob_size, .p_memsz =3D ovmf_blob_size, .p_flags =3D PF_X | PF_W | PF_R, + #ifdef PVH64 + .p_align =3D 4, + #else .p_align =3D 0, + #endif }; = phdr_load.p_vaddr =3D phdr_load.p_paddr; @@ -98,12 +163,20 @@ main ( sizeof (xen_elfnote_phys32_entry) - offsetof (xen_elfnote_phys32_entry, desc), }; - Elf32_Phdr phdr_note =3D { + #ifdef PVH64 + Elf64_Phdr phdr_note =3D { + #else + Elf32_Phdr phdr_note =3D { + #endif .p_type =3D PT_NOTE, .p_filesz =3D sizeof (xen_elf_note), .p_memsz =3D sizeof (xen_elf_note), .p_flags =3D PF_R, + #ifdef PVH64 + .p_align =3D 4, + #else .p_align =3D 0, + #endif }; = hdr.e_phnum +=3D 1; @@ -120,31 +193,35 @@ main ( size_t hdr_size =3D sizeof (hdr); size_t entry_off =3D offsetof (typeof(hdr), e_entry); = - printf ("# ELF file header\n"); - print_hdr (&hdr, entry_off); - printf ("\n"); - print_hdr (&hdr.e_entry, sizeof (hdr.e_entry)); - printf (" # hdr.e_entry\n"); - print_hdr (&hdr.e_entry + 1, hdr_size - entry_off - sizeof (hdr.e_entry)= ); + fprintf (file, "DATA =3D {\r\n"); = - printf ("\n\n# ELF Program segment headers\n"); - printf ("# - Load segment\n"); + fprintf (file, " # ELF file header\r\n"); + print_hdr (file, &hdr, entry_off, true); + fprintf (file, "\r\n"); + print_hdr (file, &hdr.e_entry, sizeof (hdr.e_entry), true); + fprintf (file, " # hdr.e_entry\r\n"); + print_hdr (file, &hdr.e_entry + 1, hdr_size - entry_off - sizeof (hdr.e_= entry), true); + + fprintf (file, "\r\n\r\n # ELF Program segment headers\r\n"); + fprintf (file, " # - Load segment\r\n"); for (i =3D 0; i < sizeof (phdr_load); i +=3D 4) { - print_hdr (((char *)&phdr_load) + i, 4); - printf ("\n"); + print_hdr (file, ((char *)&phdr_load) + i, 4, true); + fprintf (file, "\r\n"); } = - printf ("# - ELFNOTE segment\n"); + fprintf (file, " # - ELFNOTE segment\r\n"); for (i =3D 0; i < sizeof (phdr_note); i +=3D 4) { - print_hdr (((char *)&phdr_note) + i, 4); - printf ("\n"); + print_hdr (file, ((char *)&phdr_note) + i, 4, true); + fprintf (file, "\r\n"); } = - printf ("\n# XEN_ELFNOTE_PHYS32_ENTRY\n"); + fprintf (file, "\r\n # XEN_ELFNOTE_PHYS32_ENTRY\r\n"); for (i =3D 0; i < sizeof (xen_elf_note); i +=3D 4) { - print_hdr (((char *)&xen_elf_note) + i, 4); - printf ("\n"); + print_hdr (file, ((char *)&xen_elf_note) + i, 4, (sizeof (xen_elf_note= ) - i) > 4); + fprintf (file, "\r\n"); } = + fprintf (file, "}\r\n"); + return 0; } -- = 2.32.0 --------------------------------------------------------------------- Intel Corporation SAS (French simplified joint stock company) Registered headquarters: "Les Montalets"- 2, rue de Paris, = 92196 Meudon Cedex, France Registration Number: 302 456 199 R.C.S. NANTERRE Capital: 4,572,000 Euros This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.