]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OvmfPkg/OvmfXenElfHeaderGenerator.c
MdeModulePkg/Logo:Add RISCV64 architecture
[mirror_edk2.git] / OvmfPkg / OvmfXenElfHeaderGenerator.c
... / ...
CommitLineData
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
16void print_hdr(void *s, size_t size)\r
17{\r
18 char *c = s;\r
19\r
20 while (size--) {\r
21 printf("0x%02hhx, ", *(c++));\r
22 }\r
23}\r
24\r
25/* Format for the XEN_ELFNOTE_PHYS32_ENTRY program segment */\r
26#define XEN_ELFNOTE_PHYS32_ENTRY 18\r
27typedef struct {\r
28 uint32_t name_size;\r
29 uint32_t desc_size;\r
30 uint32_t type;\r
31 char name[4];\r
32 uint32_t desc;\r
33} xen_elfnote_phys32_entry;\r
34\r
35int main(void)\r
36{\r
37 /* FW_SIZE */\r
38 size_t ovmf_blob_size = 0x00200000;\r
39 /* Load OVMF at 1MB when running as PVH guest */\r
40 uint32_t ovmf_base_address = 0x00100000;\r
41 /* Xen PVH entry point */\r
42 uint32_t ovmfxen_pvh_entry_point = ovmf_base_address + ovmf_blob_size - 0x30;\r
43 size_t offset_into_file = 0;\r
44\r
45 /* ELF file header */\r
46 Elf32_Ehdr hdr = {\r
47 .e_ident = ELFMAG,\r
48 .e_type = ET_EXEC,\r
49 .e_machine = EM_386,\r
50 .e_version = EV_CURRENT,\r
51 .e_entry = ovmfxen_pvh_entry_point,\r
52 .e_flags = R_386_NONE,\r
53 .e_ehsize = sizeof (hdr),\r
54 .e_phentsize = sizeof (Elf32_Phdr),\r
55 };\r
56 offset_into_file += sizeof (hdr);\r
57\r
58 hdr.e_ident[EI_CLASS] = ELFCLASS32;\r
59 hdr.e_ident[EI_DATA] = ELFDATA2LSB;\r
60 hdr.e_ident[EI_VERSION] = EV_CURRENT;\r
61 hdr.e_ident[EI_OSABI] = ELFOSABI_LINUX;\r
62 /* Placing program headers just after hdr */\r
63 hdr.e_phoff = sizeof (hdr);\r
64\r
65 /* program header */\r
66 Elf32_Phdr phdr_load = {\r
67 .p_type = PT_LOAD,\r
68 .p_offset = 0, /* load everything */\r
69 .p_paddr = ovmf_base_address,\r
70 .p_filesz = ovmf_blob_size,\r
71 .p_memsz = ovmf_blob_size,\r
72 .p_flags = PF_X | PF_W | PF_R,\r
73 .p_align = 0,\r
74 };\r
75 phdr_load.p_vaddr = phdr_load.p_paddr;\r
76 hdr.e_phnum += 1;\r
77 offset_into_file += sizeof (phdr_load);\r
78\r
79 /* Xen ELF Note. */\r
80\r
81 xen_elfnote_phys32_entry xen_elf_note = {\r
82 .type = XEN_ELFNOTE_PHYS32_ENTRY,\r
83 .name = "Xen",\r
84 .desc = ovmfxen_pvh_entry_point,\r
85 .name_size =\r
86 offsetof (xen_elfnote_phys32_entry, desc) -\r
87 offsetof (xen_elfnote_phys32_entry, name),\r
88 .desc_size =\r
89 sizeof (xen_elfnote_phys32_entry) -\r
90 offsetof (xen_elfnote_phys32_entry, desc),\r
91 };\r
92 Elf32_Phdr phdr_note = {\r
93 .p_type = PT_NOTE,\r
94 .p_filesz = sizeof (xen_elf_note),\r
95 .p_memsz = sizeof (xen_elf_note),\r
96 .p_flags = PF_R,\r
97 .p_align = 0,\r
98 };\r
99 hdr.e_phnum += 1;\r
100 offset_into_file += sizeof (phdr_note);\r
101 phdr_note.p_offset = offset_into_file;\r
102 phdr_note.p_paddr = ovmf_base_address + phdr_note.p_offset;\r
103 phdr_note.p_vaddr = phdr_note.p_paddr;\r
104\r
105\r
106 /*\r
107 * print elf header\r
108 */\r
109\r
110 size_t i;\r
111 size_t hdr_size = sizeof (hdr);\r
112 size_t entry_off = offsetof(typeof(hdr), e_entry);\r
113\r
114 printf("# ELF file header\n");\r
115 print_hdr(&hdr, entry_off);\r
116 printf("\n");\r
117 print_hdr(&hdr.e_entry, sizeof (hdr.e_entry));\r
118 printf(" # hdr.e_entry\n");\r
119 print_hdr(&hdr.e_entry + 1, hdr_size - entry_off - sizeof (hdr.e_entry));\r
120\r
121 printf("\n\n# ELF Program segment headers\n");\r
122 printf("# - Load segment\n");\r
123 for (i = 0; i < sizeof (phdr_load); i += 4) {\r
124 print_hdr(((char*)&phdr_load) + i, 4);\r
125 printf("\n");\r
126 }\r
127 printf("# - ELFNOTE segment\n");\r
128 for (i = 0; i < sizeof (phdr_note); i += 4) {\r
129 print_hdr(((char*)&phdr_note) + i, 4);\r
130 printf("\n");\r
131 }\r
132\r
133 printf("\n# XEN_ELFNOTE_PHYS32_ENTRY\n");\r
134 for (i = 0; i < sizeof (xen_elf_note); i += 4) {\r
135 print_hdr(((char*)&xen_elf_note) + i, 4);\r
136 printf("\n");\r
137 }\r
138\r
139 return 0;\r
140}\r