utils.h (2018B)
1 // #include "uefi.h" 2 3 extern EFI_SYSTEM_TABLE *SystemTable; 4 5 // Printh converts the integer into a string of hex representation. 6 // It returns the second argument. 7 // The second argument should have length of 19 ("0x" + "0123456789abcdef" + "\0"). 8 CHAR16 *sprinth(UINT64, CHAR16[19]); 9 10 // Wstr2str converts UTF16-encoded string s16 to string. 11 // It ignores the high byte. 12 // caller must ensure that s has enough space. 13 // It returns s. 14 char *wstr2str(CHAR16 *s16, char *s8); 15 16 // Efi_printf prints formatted string to ConOut. 17 // Can't format a string longer than 1024 CHAR16s. 18 // It returns number of CHAR16s written excluding the final NULL. 19 int efi_printf(char *fmt, ...); 20 21 // Open_gop opens the graphics output protocol. 22 EFI_STATUS open_gop(EFI_HANDLE, EFI_GRAPHICS_OUTPUT_PROTOCOL **); 23 24 // Open_root opens the root directory of the efi image. 25 EFI_STATUS open_root(EFI_HANDLE, EFI_FILE_PROTOCOL **); 26 27 // ELF header. 28 typedef struct { 29 unsigned char ident[16]; 30 UINT16 type; 31 UINT16 machine; 32 UINT32 version; 33 UINTN entry; 34 UINTN phoff; 35 UINTN shoff; 36 UINT32 flags; 37 UINT16 ehsize; // ELF header's size. 38 UINT16 phentsize; 39 UINT16 phnum; 40 UINT16 shentsize; 41 UINT16 shnum; 42 UINT16 shstrndx; 43 } ElfHdr; 44 45 typedef struct { 46 UINT32 type; 47 UINT32 flags; 48 UINTN offset; 49 UINTN vaddr; 50 UINTN paddr; 51 UINT64 filesz; 52 UINT64 memsz; 53 UINT64 align; 54 } Phdr; 55 56 #define PT_LOAD 1 57 58 // Read_ehdr reads elf header and populates ehdr. 59 // It modifies elf's offset. 60 EFI_STATUS read_ehdr(EFI_FILE_PROTOCOL *elf, ElfHdr *ehdr); 61 62 // Read_phdr reads elf's program headers and populates phdr. 63 // It uses ehdr to read phdr. 64 // It modifies elf's offset. 65 // Caller should allocate enough space in phdr. 66 EFI_STATUS read_phdr(EFI_FILE_PROTOCOL *elf, ElfHdr *ehdr, Phdr phdr[]); 67 68 // Load_elf copies loadable segments of elf to the memory specified by the 69 // vaddr field of each Phdr. 70 EFI_STATUS load_elf(EFI_FILE_PROTOCOL *elf, ElfHdr *ehdr, Phdr phdr[]);