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