From 1963a0a6d7398a426d0701f90a5bc796e32899ae Mon Sep 17 00:00:00 2001 From: salmonik Date: Fri, 31 Jul 2026 02:55:10 +0200 Subject: feat(rsboot): initial commit — UEFI bootloader extracted from RNG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uefi.h | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 uefi.h (limited to 'uefi.h') diff --git a/uefi.h b/uefi.h new file mode 100644 index 0000000..6d46f16 --- /dev/null +++ b/uefi.h @@ -0,0 +1,230 @@ +/* ============================================================================= + * uefi.h — minimal UEFI types and protocols for the RNG bootloader + * + * Standalone, freestanding, no external headers. + * ============================================================================= */ + +#ifndef UEFI_H +#define UEFI_H + +/* ---- basic types ---- */ +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; +/* UEFI CHAR16 — wchar_t must be 16-bit (use -fshort-wchar). + * GCC by default makes wchar_t = int (32-bit). -fshort-wchar makes + * it unsigned short (16-bit) to match UEFI's CHAR16. + * We don't include (freestanding), so define our own. */ +typedef __CHAR16_TYPE__ char16; +typedef signed short int16; +typedef long long int64; +typedef void *handle; + +/* ---- EFI status codes ---- */ +#define EFI_SUCCESS 0 +#define EFI_LOAD_ERROR ((u64)1) +#define EFI_INVALID_PARAMETER ((u64)2) +#define EFI_UNSUPPORTED ((u64)3) +#define EFI_NOT_FOUND ((u64)14) +#define EFI_ALREADY_STARTED ((u64)25) +#define EFI_OUT_OF_RESOURCES ((u64)9) + +/* ---- GUIDs (128-bit) ---- */ +typedef struct { u32 a; u16 b; u16 c; u8 d[8]; } efi_guid; + +/* ---- EFI time (unused, part of file info struct) ---- */ +typedef struct { + u16 year; u8 month; u8 day; + u8 hour; u8 minute; u8 second; u8 pad1; + u32 ns; int16 tz; u8 daylight; +} efi_time; + +/* ---- table headers ---- */ +typedef struct { + u64 signature; + u32 revision; + u32 header_size; + u32 crc32; + u32 reserved; +} efi_table_header; + +/* ---- system table ---- */ +typedef struct efi_config_table efi_config_table; +typedef struct efi_simple_text_output efi_simple_text_output; +typedef struct efi_simple_text_input efi_simple_text_input; +typedef struct efi_boot_services efi_boot_services; +typedef struct efi_runtime_services efi_runtime_services; + +typedef struct { + efi_table_header hdr; + char16 *firmware_vendor; + u32 firmware_revision; + handle console_in_handle; + efi_simple_text_input *con_in; + handle console_out_handle; + efi_simple_text_output *con_out; + handle stderr_handle; + efi_simple_text_output *std_err; + efi_runtime_services *runtime_services; + efi_boot_services *boot_services; + u64 number_of_table_entries; + efi_config_table *configuration_table; +} efi_system_table; + +/* ---- EFI input key ---- */ +typedef struct { + u16 scan_code; + char16 unicode_char; +} efi_input_key; + +/* ---- simple text output (console) ---- */ +typedef u64 (*efi_text_reset)(efi_simple_text_output *, u8); +typedef u64 (*efi_text_output)(efi_simple_text_output *, const char16 *); + +struct efi_simple_text_output { + void *unused; // Index 0: Reset + efi_text_output output_string; // Index 1: OutputString + void *unused2[8]; // Index 2-9: TestString..Mode +}; + +/* ---- simple text input (keyboard) ---- */ +typedef u64 (*efi_input_reset)(efi_simple_text_input *, u8); +typedef u64 (*efi_input_read_key)(efi_simple_text_input *, efi_input_key *); + +struct efi_simple_text_input { + efi_input_reset reset; /* Index 0: Reset */ + efi_input_read_key read_key_stroke; /* Index 1: ReadKeyStroke */ + handle wait_for_key; /* Index 2: WaitForKey event */ +}; + +/* ---- boot services ---- */ +typedef u64 (*efi_exit_boot_services)(handle, u64); +typedef u64 (*efi_get_memory_map)(void*, void*, void*, void*, void*); +typedef u64 (*efi_locate_protocol)(const efi_guid *, void *, void **); +typedef u64 (*efi_load_image)(u8, handle, void *, void *, u64, handle *); +typedef u64 (*efi_start_image)(handle, void *, u64 *); +typedef u64 (*efi_handle_protocol)(handle, const efi_guid *, void **); +typedef u64 (*efi_open_volume)(void *, void **); +typedef u64 (*efi_open_file)(void *, void **, char16 *, u64, u64); +typedef u64 (*efi_read_file)(void *, u64 *, void *); +typedef u64 (*efi_allocate_pool)(u8, u64, void **); + +/* Boot Services table matching UEFI spec v2.10 function order. + Each entry is one function pointer (8 bytes on x64). */ +struct efi_boot_services { + efi_table_header hdr; /* 0 */ + void *raise_tpl; /* 24 index 0 */ + void *restore_tpl; /* 32 index 1 */ + void *allocate_pages; /* 40 index 2 */ + void *free_pages; /* 48 index 3 */ + efi_get_memory_map get_memory_map; /* 56 index 4 */ + efi_allocate_pool allocate_pool; /* 64 index 5 */ + void *free_pool; /* 72 index 6 */ + void *create_event; /* 80 index 7 */ + void *set_timer; /* 88 index 8 */ + void *wait_for_event; /* 96 index 9 */ + void *signal_event; /*104 index 10 */ + void *close_event; /*112 index 11 */ + void *check_event; /*120 index 12 */ + void *install_protocol_interface; /*128 index 13 */ + void *reinstall_protocol_interface;/*136 index 14 */ + void *uninstall_protocol_interface;/*144 index 15 */ + efi_handle_protocol handle_protocol; /*152 index 16 */ + void *reserved; /*160 index 17 */ + void *register_protocol_notify; /*168 index 18 */ + void *locate_handle; /*176 index 19 */ + void *locate_device_path; /*184 index 20 */ + void *install_config_table; /*192 index 21 */ + efi_load_image load_image; /*200 index 22 */ + efi_start_image start_image; /*208 index 23 */ + void *exit; /*216 index 24 */ + void *unload_image; /*224 index 25 */ + efi_exit_boot_services exit_boot_services; /*232 index 26 */ + void *get_next_monotonic_count; /*240 index 27 */ + void *stall; /*248 index 28 */ + void *set_watchdog_timer; /*256 index 29 */ + void *connect_controller; /*264 index 30 */ + void *disconnect_controller; /*272 index 31 */ + void *open_protocol; /*280 index 32 */ + void *close_protocol; /*288 index 33 */ + void *open_protocol_information; /*296 index 34 */ + void *protocols_per_handle; /*304 index 35 */ + void *locate_handle_buffer; /*312 index 36 */ + efi_locate_protocol locate_protocol; /*320 index 37 */ + void *install_multiple_protocols; /*328 index 38 */ + void *uninstall_multiple_protocols;/*336 index 39 */ + void *calculate_crc32; /*344 index 40 */ + void *copy_mem; /*352 index 41 */ + void *set_mem; /*360 index 42 */ + void *create_event_ex; /*368 index 43 */ +}; + +/* ---- GUID for Simple File System Protocol ---- */ +#define EFI_SIMPLE_FILE_SYSTEM_GUID \ + {0x0964e5b22, 0x6459, 0x11d2, {0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b}} + +/* ---- file protocol ---- */ +typedef struct { + u64 revision; + efi_open_volume open_volume; +} efi_simple_file_system; + +typedef struct efi_file_protocol efi_file_protocol; + +typedef u64 (*efi_file_open)(efi_file_protocol *, efi_file_protocol **, char16 *, u64, u64); +typedef u64 (*efi_file_read)(efi_file_protocol *, u64 *, void *); +typedef u64 (*efi_file_close)(efi_file_protocol *); + +#define EFI_FILE_MODE_READ 1 + +struct efi_file_protocol { + u64 revision; + efi_file_open open; + efi_file_close close; + void *unused; + efi_file_read read; + void *unused2[5]; +}; + +/* ---- file info (for getting file size) ---- */ +typedef struct { + u64 size; + u64 file_size; + u64 physical_size; + efi_time create_time; + efi_time last_access; + efi_time modification_time; + u64 attribute; + char16 file_name[1]; +} efi_file_info; + +/* ---- loaded image protocol ---- */ +typedef struct { + u32 revision; + handle parent_handle; + handle system_table; + handle device_handle; + void *file_path; + void *reserved; + u32 load_options_size; + void *load_options; + void *image_base; + u64 image_size; + u64 image_code_type; + u64 image_data_type; + void *unused; +} efi_loaded_image; + +/* ---- loaded image protocol GUID ---- */ +#define EFI_LOADED_IMAGE_GUID \ + {0x5b1b31a1, 0x9562, 0x11d2, {0x8e,0x3f,0x00,0xa0,0xc9,0x69,0x72,0x3b}} + +/* ---- device path protocol ---- */ +typedef struct { + u8 type; + u8 sub_type; + u16 length; +} efi_device_path; + +#endif /* UEFI_H */ -- cgit v1.2.3