VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/OvmfPkg/OvmfXenElfHeaderGenerator.c@ 80820

Last change on this file since 80820 was 80721, checked in by vboxsync, 5 years ago

Devices/EFI/FirmwareNew: Start upgrade process to edk2-stable201908 (compiles on Windows and works to some extent), bugref:4643

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1/** @file
2 This program generates a hex array to be manually coppied into
3 OvmfXen.fdf.
4
5 The purpose is for the flash device image to be recognize as an ELF.
6
7 Copyright (c) 2019, Citrix Systems, Inc.
8
9 SPDX-License-Identifier: BSD-2-Clause-Patent
10**/
11
12#include "elf.h"
13#include "stdio.h"
14#include "stddef.h"
15
16void print_hdr(void *s, size_t size)
17{
18 char *c = s;
19
20 while (size--) {
21 printf("0x%02hhx, ", *(c++));
22 }
23}
24
25/* Format for the XEN_ELFNOTE_PHYS32_ENTRY program segment */
26#define XEN_ELFNOTE_PHYS32_ENTRY 18
27typedef struct {
28 uint32_t name_size;
29 uint32_t desc_size;
30 uint32_t type;
31 char name[4];
32 uint32_t desc;
33} xen_elfnote_phys32_entry;
34
35int main(void)
36{
37 /* FW_SIZE */
38 size_t ovmf_blob_size = 0x00200000;
39 /* Load OVMF at 1MB when running as PVH guest */
40 uint32_t ovmf_base_address = 0x00100000;
41 /* Xen PVH entry point */
42 uint32_t ovmfxen_pvh_entry_point = ovmf_base_address + ovmf_blob_size - 0x30;
43 size_t offset_into_file = 0;
44
45 /* ELF file header */
46 Elf32_Ehdr hdr = {
47 .e_ident = ELFMAG,
48 .e_type = ET_EXEC,
49 .e_machine = EM_386,
50 .e_version = EV_CURRENT,
51 .e_entry = ovmfxen_pvh_entry_point,
52 .e_flags = R_386_NONE,
53 .e_ehsize = sizeof (hdr),
54 .e_phentsize = sizeof (Elf32_Phdr),
55 };
56 offset_into_file += sizeof (hdr);
57
58 hdr.e_ident[EI_CLASS] = ELFCLASS32;
59 hdr.e_ident[EI_DATA] = ELFDATA2LSB;
60 hdr.e_ident[EI_VERSION] = EV_CURRENT;
61 hdr.e_ident[EI_OSABI] = ELFOSABI_LINUX;
62 /* Placing program headers just after hdr */
63 hdr.e_phoff = sizeof (hdr);
64
65 /* program header */
66 Elf32_Phdr phdr_load = {
67 .p_type = PT_LOAD,
68 .p_offset = 0, /* load everything */
69 .p_paddr = ovmf_base_address,
70 .p_filesz = ovmf_blob_size,
71 .p_memsz = ovmf_blob_size,
72 .p_flags = PF_X | PF_W | PF_R,
73 .p_align = 0,
74 };
75 phdr_load.p_vaddr = phdr_load.p_paddr;
76 hdr.e_phnum += 1;
77 offset_into_file += sizeof (phdr_load);
78
79 /* Xen ELF Note. */
80
81 xen_elfnote_phys32_entry xen_elf_note = {
82 .type = XEN_ELFNOTE_PHYS32_ENTRY,
83 .name = "Xen",
84 .desc = ovmfxen_pvh_entry_point,
85 .name_size =
86 offsetof (xen_elfnote_phys32_entry, desc) -
87 offsetof (xen_elfnote_phys32_entry, name),
88 .desc_size =
89 sizeof (xen_elfnote_phys32_entry) -
90 offsetof (xen_elfnote_phys32_entry, desc),
91 };
92 Elf32_Phdr phdr_note = {
93 .p_type = PT_NOTE,
94 .p_filesz = sizeof (xen_elf_note),
95 .p_memsz = sizeof (xen_elf_note),
96 .p_flags = PF_R,
97 .p_align = 0,
98 };
99 hdr.e_phnum += 1;
100 offset_into_file += sizeof (phdr_note);
101 phdr_note.p_offset = offset_into_file;
102 phdr_note.p_paddr = ovmf_base_address + phdr_note.p_offset;
103 phdr_note.p_vaddr = phdr_note.p_paddr;
104
105
106 /*
107 * print elf header
108 */
109
110 size_t i;
111 size_t hdr_size = sizeof (hdr);
112 size_t entry_off = offsetof(typeof(hdr), e_entry);
113
114 printf("# ELF file header\n");
115 print_hdr(&hdr, entry_off);
116 printf("\n");
117 print_hdr(&hdr.e_entry, sizeof (hdr.e_entry));
118 printf(" # hdr.e_entry\n");
119 print_hdr(&hdr.e_entry + 1, hdr_size - entry_off - sizeof (hdr.e_entry));
120
121 printf("\n\n# ELF Program segment headers\n");
122 printf("# - Load segment\n");
123 for (i = 0; i < sizeof (phdr_load); i += 4) {
124 print_hdr(((char*)&phdr_load) + i, 4);
125 printf("\n");
126 }
127 printf("# - ELFNOTE segment\n");
128 for (i = 0; i < sizeof (phdr_note); i += 4) {
129 print_hdr(((char*)&phdr_note) + i, 4);
130 printf("\n");
131 }
132
133 printf("\n# XEN_ELFNOTE_PHYS32_ENTRY\n");
134 for (i = 0; i < sizeof (xen_elf_note); i += 4) {
135 print_hdr(((char*)&xen_elf_note) + i, 4);
136 printf("\n");
137 }
138
139 return 0;
140}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette