commit 94b1ab14c4a3130b517edec157e2ce6550b6396c
parent 568231651d1ad8aecb0274157b4813ea7b445df1
Author: Matsuda Kenji <info@mtkn.jp>
Date: Tue, 2 Apr 2024 15:53:59 +0900
rename
Diffstat:
1 file changed, 71 insertions(+), 0 deletions(-)
diff --git a/sys/include/uefi_utils.h b/sys/include/uefi_utils.h
@@ -0,0 +1,70 @@
+// #include "uefi.h"
+
+extern EFI_SYSTEM_TABLE *SystemTable;
+
+// Printh converts the integer into a string of hex representation.
+// It returns the second argument.
+// The second argument should have length of 19 ("0x" + "0123456789abcdef" + "\0").
+CHAR16 *sprinth(UINT64, CHAR16[19]);
+
+// Wstr2str converts UTF16-encoded string s16 to string.
+// It ignores the high byte.
+// caller must ensure that s has enough space.
+// It returns s.
+char *wstr2str(CHAR16 *s16, char *s8);
+
+// Efi_printf prints formatted string to ConOut.
+// Can't format a string longer than 1024 CHAR16s.
+// It returns number of CHAR16s written excluding the final NULL.
+int efi_printf(char *fmt, ...);
+
+// Open_gop opens the graphics output protocol.
+EFI_STATUS open_gop(EFI_HANDLE, EFI_GRAPHICS_OUTPUT_PROTOCOL **);
+
+// Open_root opens the root directory of the efi image.
+EFI_STATUS open_root(EFI_HANDLE, EFI_FILE_PROTOCOL **);
+
+// ELF header.
+typedef struct {
+ unsigned char ident[16];
+ UINT16 type;
+ UINT16 machine;
+ UINT32 version;
+ UINTN entry;
+ UINTN phoff;
+ UINTN shoff;
+ UINT32 flags;
+ UINT16 ehsize; // ELF header's size.
+ UINT16 phentsize;
+ UINT16 phnum;
+ UINT16 shentsize;
+ UINT16 shnum;
+ UINT16 shstrndx;
+} ElfHdr;
+
+typedef struct {
+ UINT32 type;
+ UINT32 flags;
+ UINTN offset;
+ UINTN vaddr;
+ UINTN paddr;
+ UINT64 filesz;
+ UINT64 memsz;
+ UINT64 align;
+} Phdr;
+
+#define PT_LOAD 1
+
+// Read_ehdr reads elf header and populates ehdr.
+// It modifies elf's offset.
+EFI_STATUS read_ehdr(EFI_FILE_PROTOCOL *elf, ElfHdr *ehdr);
+
+// Read_phdr reads elf's program headers and populates phdr.
+// It uses ehdr to read phdr.
+// It modifies elf's offset.
+// Caller should allocate enough space in phdr.
+EFI_STATUS read_phdr(EFI_FILE_PROTOCOL *elf, ElfHdr *ehdr, Phdr phdr[]);
+
+// Load_elf copies loadable segments of elf to the memory specified by the
+// vaddr field of each Phdr.
+EFI_STATUS load_elf(EFI_FILE_PROTOCOL *elf, ElfHdr *ehdr, Phdr phdr[]);
+\ No newline at end of file