From 97d331175ae47aec3d0b990ada9754a9e4298e32 Mon Sep 17 00:00:00 2001 From: andromeda Date: Mon, 19 Oct 2026 13:37:35 -0400 Subject: minimum that compiles --- lib/uefi.c3l/src/def.c3i | 339 +++++++++++++--------------- lib/uefi.c3l/src/exports.c3 | 11 +- lib/uefi.c3l/src/lib/console.c3 | 2 +- lib/uefi.c3l/src/lib/init.c3 | 2 +- lib/uefi.c3l/src/lib/private/print.c3 | 2 +- lib/uefi.c3l/src/lib/string.c3 | 2 +- lib/uefi.c3l/src/protocol/evt_timer_task.c3 | 2 +- lib/uefi.c3l/src/protocol/mem.c3 | 2 +- main.c3 | 25 +- project.json | 12 +- 10 files changed, 208 insertions(+), 191 deletions(-) diff --git a/lib/uefi.c3l/src/def.c3i b/lib/uefi.c3l/src/def.c3i index f595caa..769f15e 100644 --- a/lib/uefi.c3l/src/def.c3i +++ b/lib/uefi.c3l/src/def.c3i @@ -1,240 +1,221 @@ module uefi; -/* - * EFI_HANDLE alias (sec x.x.x of the UEFI 2.11 specification). - */ +/** EFI_HANDLE alias (sec x.x.x of the UEFI 2.11 specification). */ typedef EfiHandle = void*; -/* - * All UEFI strings are wchar_t strings (Wide-Char types). - */ +/** All UEFI strings are wchar_t strings (Wide-Char types). */ alias EfiString = ushort[]; alias EfiStringRaw = ushort*; -/* - * ... But ASCII strings can also be used and dynamically converted to wide ones. - */ +/** ... But ASCII strings can also be used and dynamically converted to wide ones. */ alias AsciiString = char[]; alias AsciiStringRaw = char*; -/* - * EFI_EVENT alias (sec 7.1.1 of the UEFI 2.11 specification). - */ +/** EFI_EVENT alias (sec 7.1.1 of the UEFI 2.11 specification). */ typedef EfiEvent = void*; -/* - * EFI_TABLE_HEADER structure (sec 4.2.1 of the UEFI 2.11 specification). +/** EFI_TABLE_HEADER structure (sec 4.2.1 of the UEFI 2.11 specification). * The same section contains the following constant expressions - * as well as the EFI_SYSTEM_TABLE structure. - */ + * as well as the EFI_SYSTEM_TABLE structure. */ struct EfiTableHeader @packed { - ulong signature; - uint revision; - uint header_size; - uint crc32; - uint _reserved; + ulong signature; + uint revision; + uint header_size; + uint crc32; + uint _reserved; } -const ulong EFI_SYSTEM_TABLE_SIGNATURE = 0x5453595320494249; -const uint EFI_2_100_SYSTEM_TABLE_REVISION = ((2<<16) | (100)); -const uint EFI_2_90_SYSTEM_TABLE_REVISION = ((2<<16) | (90)); -const uint EFI_2_80_SYSTEM_TABLE_REVISION = ((2<<16) | (80)); -const uint EFI_2_70_SYSTEM_TABLE_REVISION = ((2<<16) | (70)); -const uint EFI_2_60_SYSTEM_TABLE_REVISION = ((2<<16) | (60)); -const uint EFI_2_50_SYSTEM_TABLE_REVISION = ((2<<16) | (50)); -const uint EFI_2_40_SYSTEM_TABLE_REVISION = ((2<<16) | (40)); -const uint EFI_2_31_SYSTEM_TABLE_REVISION = ((2<<16) | (31)); -const uint EFI_2_30_SYSTEM_TABLE_REVISION = ((2<<16) | (30)); -const uint EFI_2_20_SYSTEM_TABLE_REVISION = ((2<<16) | (20)); -const uint EFI_2_10_SYSTEM_TABLE_REVISION = ((2<<16) | (10)); -const uint EFI_2_00_SYSTEM_TABLE_REVISION = ((2<<16) | (00)); -const uint EFI_1_10_SYSTEM_TABLE_REVISION = ((1<<16) | (10)); -const uint EFI_1_02_SYSTEM_TABLE_REVISION = ((1<<16) | (02)); -const uint EFI_SYSTEM_TABLE_REVISION = EFI_2_100_SYSTEM_TABLE_REVISION; -const uint EFI_SPECIFICATION_VERSION = EFI_SYSTEM_TABLE_REVISION; +const ulong EFI_SYSTEM_TABLE_SIGNATURE = 0x5453595320494249; +const uint EFI_2_100_SYSTEM_TABLE_REVISION = ((2 << 16) | (100)); +const uint EFI_2_90_SYSTEM_TABLE_REVISION = ((2 << 16) | (90)); +const uint EFI_2_80_SYSTEM_TABLE_REVISION = ((2 << 16) | (80)); +const uint EFI_2_70_SYSTEM_TABLE_REVISION = ((2 << 16) | (70)); +const uint EFI_2_60_SYSTEM_TABLE_REVISION = ((2 << 16) | (60)); +const uint EFI_2_50_SYSTEM_TABLE_REVISION = ((2 << 16) | (50)); +const uint EFI_2_40_SYSTEM_TABLE_REVISION = ((2 << 16) | (40)); +const uint EFI_2_31_SYSTEM_TABLE_REVISION = ((2 << 16) | (31)); +const uint EFI_2_30_SYSTEM_TABLE_REVISION = ((2 << 16) | (30)); +const uint EFI_2_20_SYSTEM_TABLE_REVISION = ((2 << 16) | (20)); +const uint EFI_2_10_SYSTEM_TABLE_REVISION = ((2 << 16) | (10)); +const uint EFI_2_00_SYSTEM_TABLE_REVISION = ((2 << 16) | (00)); +const uint EFI_1_10_SYSTEM_TABLE_REVISION = ((1 << 16) | (10)); +const uint EFI_1_02_SYSTEM_TABLE_REVISION = ((1 << 16) | (02)); +const uint EFI_SYSTEM_TABLE_REVISION = EFI_2_100_SYSTEM_TABLE_REVISION; +const uint EFI_SPECIFICATION_VERSION = EFI_SYSTEM_TABLE_REVISION; /* NOTE: Do NOT pack this struct: firmware_revision being 32-bit causes misalignment */ struct SystemTable { - EfiTableHeader header; - EfiStringRaw firmware_vendor; - uint firmware_revision; - EfiHandle conin_handle; - SimpleTextInputProtocol *conin; - EfiHandle conout_handle; - SimpleTextOutputProtocol *conout; - EfiHandle conerr_handle; - SimpleTextOutputProtocol *conerr; - RuntimeServices *runtime_services; - BootServices *boot_services; - usz num_entries; - ConfigurationTable *configuration_table; + EfiTableHeader header; + EfiStringRaw firmware_vendor; + uint firmware_revision; + EfiHandle conin_handle; + SimpleTextInputProtocol* conin; + EfiHandle conout_handle; + SimpleTextOutputProtocol* conout; + EfiHandle conerr_handle; + SimpleTextOutputProtocol* conerr; + RuntimeServices* runtime_services; + BootServices* boot_services; + usz num_entries; + ConfigurationTable* configuration_table; } -/* - * EFI_CONFIGURATION_TABLE structure (sec 4.6.1 of the UEFI 2.11 specification). - */ +/** EFI_CONFIGURATION_TABLE structure (sec 4.6.1 of the UEFI 2.11 specification). */ struct ConfigurationTable @packed { - EfiGuid vendor_guid; - void *vendor_table; + EfiGuid vendor_guid; + void* vendor_table; } /* EFI_BOOT_SERVICES structure (4.4.1) */ -const ulong EFI_BOOT_SERVICES_SIGNATURE = 0x56524553544f4f42; //'BOOTSERV' <- EightCC +const ulong EFI_BOOT_SERVICES_SIGNATURE = 0x56524553544f4f42; // 'BOOTSERV' <- EightCC const uint EFI_BOOT_SERVICES_REVISION = EFI_SPECIFICATION_VERSION; struct BootServices @packed { - EfiTableHeader header; - - /* Task priority services (uefi::proto::evt) */ - FnRaiseTpl raise_tpl; - FnRestoreTpl restore_tpl; - - /* Memory services (uefi::proto::mem) */ - FnAllocatePages allocate_pages; - FnFreePages free_pages; - FnGetMemoryMap get_memory_map; - FnAllocatePool allocate_pool; - FnFreePool free_pool; - - /* Event and Timer services (uefi::proto::evt) */ - FnCreateEvent create_event; - FnSetTimer set_timer; - FnWaitForEvent wait_for_event; - FnSignalEvent signal_event; - FnCloseEvent close_event; - FnCheckEvent check_event; - - /* Protocol Handler services (uefi::proto::handler) */ - FnInstallProtocolInterface install_protocol_interface; - FnReinstallProtocolInterface reinstall_protocol_interface; - FnUninstallProtocolInterface uninstall_protocol_interface; - FnHandleProtocol handle_protocol; - void *_reserved; /* alright then */ - FnRegisterProtocolNotify register_protocol_notify; - FnLocateHandle locate_handle; - FnLocateDevicePath locate_device_path; - FnInstallConfigurationTable install_configuration_table; - - /* Image services (uefi::proto::image) */ - FnLoadImage load_image; - FnStartImage start_image; - FnExitImage exit_image; - FnUnloadImage unload_image; - FnExitBootServices exit_boot_services; - - /* Misc services (uefi::proto::misc_bs) */ - FnGetNextMonotonicCount get_next_monotonic_count; - FnStall stall; - FnSetWatchdogTimer set_watchdog_timer; - - /* Driver Support services (uefi::proto::handler) */ - FnConnectController connect_controller; - FnDisconnectController disconnect_controller; - - /* Open/Close Protocol services (uefi::proto::handler) */ - FnOpenProtocol open_protocol; - FnCloseProtocol close_protocol; - FnOpenProtocolInformation open_protocol_information; - - /* Library services (uefi::proto::handler) */ - FnProtocolsPerHandle protocols_per_handle; - FnLocateHandleBuffer locate_handle_buffer; - FnLocateProtocol locate_protocol; - FnInstallMultipleProtocolInterfaces install_multiple_protocol_interfaces; - FnUninstallMultipleProtocolInterfaces uninstall_multiple_protocol_interfaces; - - /* 32-bit CRC services (uefi::proto::misc_bs) */ - FnCalculateCrc32 calculate_crc32; - - /* More misc/trailing services */ - FnCopyMem copy_mem; - FnSetMem set_mem; - FnCreateEventEx create_event_ex; + EfiTableHeader header; + + /* Task priority services (uefi::proto::evt) */ + FnRaiseTpl raise_tpl; + FnRestoreTpl restore_tpl; + + /* Memory services (uefi::proto::mem) */ + FnAllocatePages allocate_pages; + FnFreePages free_pages; + FnGetMemoryMap get_memory_map; + FnAllocatePool allocate_pool; + FnFreePool free_pool; + + /* Event and Timer services (uefi::proto::evt) */ + FnCreateEvent create_event; + FnSetTimer set_timer; + FnWaitForEvent wait_for_event; + FnSignalEvent signal_event; + FnCloseEvent close_event; + FnCheckEvent check_event; + + /* Protocol Handler services (uefi::proto::handler) */ + FnInstallProtocolInterface install_protocol_interface; + FnReinstallProtocolInterface reinstall_protocol_interface; + FnUninstallProtocolInterface uninstall_protocol_interface; + FnHandleProtocol handle_protocol; + void* _reserved; /* alright then */ + FnRegisterProtocolNotify register_protocol_notify; + FnLocateHandle locate_handle; + FnLocateDevicePath locate_device_path; + FnInstallConfigurationTable install_configuration_table; + + /* Image services (uefi::proto::image) */ + FnLoadImage load_image; + FnStartImage start_image; + FnExitImage exit_image; + FnUnloadImage unload_image; + FnExitBootServices exit_boot_services; + + /* Misc services (uefi::proto::misc_bs) */ + FnGetNextMonotonicCount get_next_monotonic_count; + FnStall stall; + FnSetWatchdogTimer set_watchdog_timer; + + /* Driver Support services (uefi::proto::handler) */ + FnConnectController connect_controller; + FnDisconnectController disconnect_controller; + + /* Open/Close Protocol services (uefi::proto::handler) */ + FnOpenProtocol open_protocol; + FnCloseProtocol close_protocol; + FnOpenProtocolInformation open_protocol_information; + + /* Library services (uefi::proto::handler) */ + FnProtocolsPerHandle protocols_per_handle; + FnLocateHandleBuffer locate_handle_buffer; + FnLocateProtocol locate_protocol; + FnInstallMultipleProtocolInterfaces install_multiple_protocol_interfaces; + FnUninstallMultipleProtocolInterfaces uninstall_multiple_protocol_interfaces; + + /* 32-bit CRC services (uefi::proto::misc_bs) */ + FnCalculateCrc32 calculate_crc32; + + /* More misc/trailing services */ + FnCopyMem copy_mem; + FnSetMem set_mem; + FnCreateEventEx create_event_ex; } struct RuntimeServices @packed { - EfiTableHeader header; - - /* Time services (uefi::proto::time) */ - FnGetTime get_time; - FnSetTime set_time; - FnGetWakeupTime get_wakeup_time; - FnSetWakeupTime set_wakeup_time; - - /* Virtual Memory services (uefi::proto::mem) */ - FnSetVirtualAddressMap set_virtual_address_map; - FnConvertPointer convert_pointer; - - /* Variable services (uefi::proto::variable) */ - FnGetVariable get_variable; - FnGetNextVariableName get_next_variable_name; - FnSetVariable set_variable; - - /* Miscellaneous and Capsule services (uefi::proto::misc_rt) */ - FnGetNextHighMonotonicCount get_next_high_monotonic_count; - FnResetSystem reset_system; - FnUpdateCapsule update_capsule; - FnQueryCapsuleCapabilities query_capsule_capabilities; - FnQueryVariableInfo query_variable_info; + EfiTableHeader header; + + /* Time services (uefi::proto::time) */ + FnGetTime get_time; + FnSetTime set_time; + FnGetWakeupTime get_wakeup_time; + FnSetWakeupTime set_wakeup_time; + + /* Virtual Memory services (uefi::proto::mem) */ + FnSetVirtualAddressMap set_virtual_address_map; + FnConvertPointer convert_pointer; + + /* Variable services (uefi::proto::variable) */ + FnGetVariable get_variable; + FnGetNextVariableName get_next_variable_name; + FnSetVariable set_variable; + + /* Miscellaneous and Capsule services (uefi::proto::misc_rt) */ + FnGetNextHighMonotonicCount get_next_high_monotonic_count; + FnResetSystem reset_system; + FnUpdateCapsule update_capsule; + FnQueryCapsuleCapabilities query_capsule_capabilities; + FnQueryVariableInfo query_variable_info; } - -/* - * EFI_GUID structure (sec x.x.x of the UEFI 2.11 specification). - */ +/** EFI_GUID structure (sec x.x.x of the UEFI 2.11 specification). */ struct EfiGuid @packed { - uint data1; - ushort data2; - ushort data3; - char[8] data4; + uint data1; + ushort data2; + ushort data3; + char[8] data4; } -/* - * EFI_TIME structure (sec 8.3.1 of the UEFI 2.11 specification). - */ +/** EFI_TIME structure (sec 8.3.1 of the UEFI 2.11 specification). */ struct EfiTime @packed { - ushort year; - char month; - char day; - char hour; - char minute; - char second; - char _pad1; - uint nanosecond; - short timezone; - EfiDaylightFlags daylight; - char _pad2; + ushort year; + char month; + char day; + char hour; + char minute; + char second; + char _pad1; + uint nanosecond; + short timezone; + EfiDaylightFlags daylight; + char _pad2; } bitstruct EfiDaylightFlags : char { - bool adjust : 0; - bool in_daylight : 1; - char reserved : 2..7; + bool adjust : 0; + bool in_daylight : 1; + char reserved : 2..7; } const short EFI_UNSPECIFIED_TIMEZONE = 0x07FF; -/* - * EFI_TIME_CAPABILITIES (sec 8.3.1 of the UEFI 2.11 specification). - */ +/** EFI_TIME_CAPABILITIES (sec 8.3.1 of the UEFI 2.11 specification). */ struct EfiTimeCapabilities @packed { - uint resolution; - uint accuracy; - bool sets_to_zero; -} + uint resolution; + uint accuracy; + bool sets_to_zero; +} \ No newline at end of file diff --git a/lib/uefi.c3l/src/exports.c3 b/lib/uefi.c3l/src/exports.c3 index 3b337e3..332311c 100644 --- a/lib/uefi.c3l/src/exports.c3 +++ b/lib/uefi.c3l/src/exports.c3 @@ -188,6 +188,15 @@ fn void* memcpy(void* into, void* from, usz length) @export("memcpy") @nostrip return into; } +fn void* memmove(void* into, void* from, usz length) @export("memmove") @nostrip +{ + void* tmp = malloc(length); + memcpy(tmp, from, length); + memcpy(into, tmp, length); + free(tmp); + return into; +} + fn void memset( void* at, char value, @@ -204,4 +213,4 @@ fn void* realloc( ) @export("realloc") @nostrip => efi_system::efi_sys.realloc(pool, prev_len, new_len)!!; fn void free(void* pool) @export("free") @nostrip => efi_system::efi_sys.free(pool); -fn void? free_safe(void* pool) @export("free_safe") @nostrip => efi_system::efi_sys.free_safe(pool); \ No newline at end of file +fn void? free_safe(void* pool) @export("free_safe") @nostrip => efi_system::efi_sys.free_safe(pool); diff --git a/lib/uefi.c3l/src/lib/console.c3 b/lib/uefi.c3l/src/lib/console.c3 index 43c4116..862b95a 100644 --- a/lib/uefi.c3l/src/lib/console.c3 +++ b/lib/uefi.c3l/src/lib/console.c3 @@ -129,4 +129,4 @@ fn void? Console.clear(&self, ConsoleColor color, bool preserve_attr = false, bo /* Reset the cursor position if specified. */ if (reset_cursor) efi_error(self.stdout.set_cursor_position(self.stdout, 0, 0))!; -} +} \ No newline at end of file diff --git a/lib/uefi.c3l/src/lib/init.c3 b/lib/uefi.c3l/src/lib/init.c3 index 23dc781..e81c5e3 100644 --- a/lib/uefi.c3l/src/lib/init.c3 +++ b/lib/uefi.c3l/src/lib/init.c3 @@ -89,4 +89,4 @@ fn bool? init(EfiHandle image, SystemTable* st) @public @allow_deprecated initialized = initialized && (null != (void*)efi_system::efi_sys); return initialized; -} +} \ No newline at end of file diff --git a/lib/uefi.c3l/src/lib/private/print.c3 b/lib/uefi.c3l/src/lib/private/print.c3 index cb5185c..4dc2d84 100644 --- a/lib/uefi.c3l/src/lib/private/print.c3 +++ b/lib/uefi.c3l/src/lib/private/print.c3 @@ -156,4 +156,4 @@ fn EfiString? _bool_to_str(bool b) => b ? a2w("TRUE") : a2w("FALSE"); fn EfiString? _guid_to_str(EfiGuid* guid) { return a2w("SOMEGUID"); -} +} \ No newline at end of file diff --git a/lib/uefi.c3l/src/lib/string.c3 b/lib/uefi.c3l/src/lib/string.c3 index a03981e..26d4f21 100644 --- a/lib/uefi.c3l/src/lib/string.c3 +++ b/lib/uefi.c3l/src/lib/string.c3 @@ -142,4 +142,4 @@ macro void? strcpy(void* into, AsciiString from) @builtin => ascii_strcpy(into, macro EfiString? a2w(AsciiString c) @builtin => ascii_to_wide(c); macro EfiString wslice_from_raw(void* rawptr, usz length) @builtin => ((EfiStringRaw)rawptr)[:length]; -macro AsciiString slice_from_raw(void* rawptr, usz length) @builtin => ((AsciiStringRaw)rawptr)[:length]; +macro AsciiString slice_from_raw(void* rawptr, usz length) @builtin => ((AsciiStringRaw)rawptr)[:length]; \ No newline at end of file diff --git a/lib/uefi.c3l/src/protocol/evt_timer_task.c3 b/lib/uefi.c3l/src/protocol/evt_timer_task.c3 index 3d2929b..95c6cf3 100644 --- a/lib/uefi.c3l/src/protocol/evt_timer_task.c3 +++ b/lib/uefi.c3l/src/protocol/evt_timer_task.c3 @@ -100,4 +100,4 @@ const EventTpl TPL_NOTIFY = (EventTpl)16; const EventTpl TPL_HIGH_LEVEL = (EventTpl)31; /* EFI_RESTORE_TPL (7.1.9) */ -alias FnRestoreTpl = fn void(EventTpl); +alias FnRestoreTpl = fn void(EventTpl); \ No newline at end of file diff --git a/lib/uefi.c3l/src/protocol/mem.c3 b/lib/uefi.c3l/src/protocol/mem.c3 index 8bb2cbb..d829005 100644 --- a/lib/uefi.c3l/src/protocol/mem.c3 +++ b/lib/uefi.c3l/src/protocol/mem.c3 @@ -102,4 +102,4 @@ struct MemoryRange { PhysicalAddress address; ulong length; -} +} \ No newline at end of file diff --git a/main.c3 b/main.c3 index c857210..e3a8c15 100644 --- a/main.c3 +++ b/main.c3 @@ -1,8 +1,25 @@ +/** Copyright (C) 2026 Andromeda + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +**/ + module c3os; -import std::io; -fn int main(String[] args) +import uefi; + +fn EfiStatus efi_main(EfiHandle image_handle, SystemTable* system_table) @export("efi_main") { - io::printn("Hello, World!"); - return 0; + pre_load::init(image_handle, system_table)!!; + return SUCCESS; } diff --git a/project.json b/project.json index 50523aa..a053d9a 100644 --- a/project.json +++ b/project.json @@ -5,9 +5,19 @@ "dependencies": [ "uefi" ], "authors": [ "Andromeda " ], "version": "0.1.0", - "sources": [ "**" ], + "sources": [ "main.c3" ], "build-dir": "build", "output": "build", + "stack-probe": "none", + "stack-protector": "none", + "reloc": "PIC", + "win-subsystem": "efi-app", + "link-args": [ + "-nostdlib", + "-eefi_main", + ], + "link-libc": false, + "no-entry": true, "targets": { "c3os": { "type": "executable", -- cgit v1.3.1