OvmfXenElfHeaderGenerator.c 3.6 KB

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