diff --git a/data/Tupfile.lua b/data/Tupfile.lua index a6404bb1a5..8d3a2143fe 100644 --- a/data/Tupfile.lua +++ b/data/Tupfile.lua @@ -825,7 +825,7 @@ for i,v in ipairs(distr_extra_files) do end end --- kolibri.raw +-- build kolibri.raw raw_mbr = "../programs/hd_load/usb_boot/mbr" raw_bootsector = "../kernel/trunk/bootloader/extended_primary_loader/fat32/bootsect.bin" raw_files = { @@ -833,10 +833,11 @@ raw_files = { {"KORDLDR.F32", "../kernel/trunk/bootloader/extended_primary_loader/fat32/kordldr.f32"}, {"KERNEL.MNT", "../kernel/trunk/kernel.mnt.ext_loader"}, {"CONFIG.INI", "../kernel/trunk/bootloader/extended_primary_loader/config.ini"}, - {"EFI/BOOT/BOOTX64.EFI", "../kernel/trunk/bootloader/uefi4kos/kolibri.efi"}, - {"EFI/BOOT/KOLIBRI.IMG", "kolibri.img"}, - {"EFI/BOOT/KOLIBRI.INI", "../kernel/trunk/bootloader/uefi4kos/kolibri.ini"}, - {"EFI/BOOT/KOLIBRI.KRN", "../kernel/trunk/kolibri.krn"} + {"EFI/BOOT/BOOTX64.EFI", "../kernel/trunk/bootloader/uefi4kos/bootx64.efi"}, + {"EFI/BOOT/BOOTIA32.EFI", "../kernel/trunk/bootloader/uefi4kos/bootia32.efi"}, + {"EFI/KOLIBRIOS/KOLIBRI.IMG", "kolibri.img"}, + {"EFI/KOLIBRIOS/KOLIBRI.INI", "../kernel/trunk/bootloader/uefi4kos/kolibri.ini"}, + {"EFI/KOLIBRIOS/KOLIBRI.KRN", "../kernel/trunk/kolibri.krn"} } for i,v in ipairs(img_files) do diff --git a/kernel/trunk/bootloader/uefi4kos/Tupfile.lua b/kernel/trunk/bootloader/uefi4kos/Tupfile.lua index d5bd7e082b..ef45904aec 100644 --- a/kernel/trunk/bootloader/uefi4kos/Tupfile.lua +++ b/kernel/trunk/bootloader/uefi4kos/Tupfile.lua @@ -1,2 +1,3 @@ if tup.getconfig("NO_FASM") ~= "" then return end -tup.rule("uefi4kos.asm", "fasm -dUEFI=1 -dextended_primary_loader=1 %f %o", "kolibri.efi") +tup.rule("uefi64kos.asm", "fasm -dUEFI=1 -dextended_primary_loader=1 %f %o", "bootx64.efi") +tup.rule("uefi32kos.asm", "fasm -dUEFI=1 -dextended_primary_loader=1 %f %o", "bootia32.efi") diff --git a/kernel/trunk/bootloader/uefi4kos/kolibri.ini b/kernel/trunk/bootloader/uefi4kos/kolibri.ini index b554da870e..521724721d 100644 --- a/kernel/trunk/bootloader/uefi4kos/kolibri.ini +++ b/kernel/trunk/bootloader/uefi4kos/kolibri.ini @@ -1,10 +1,8 @@ -; Place this file to ESP:/EFI/BOOT/kolibri.ini, near kolibri.efi and kolibri.krn - ; Screen resolution resolution=1024x768 ; Duplicate debug output to the screen -debug_print=0 +debug_print=1 ; Start LAUNCHER app after kernel is loaded launcher_start=1 @@ -18,6 +16,3 @@ imgfrom=3 ; Path to /sys directory syspath=/rd/1 - -; Interrupt booting to ask the user for boot params -ask_params=0 diff --git a/kernel/trunk/bootloader/uefi4kos/uefi.inc b/kernel/trunk/bootloader/uefi4kos/uefi.inc index 0798fe853e..edb37bdfb2 100644 --- a/kernel/trunk/bootloader/uefi4kos/uefi.inc +++ b/kernel/trunk/bootloader/uefi4kos/uefi.inc @@ -1,8 +1,19 @@ -;********************************************************************* -;* * -;* UEFI library for fasm by bzt, Public Domain * -;* * -;********************************************************************* +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; ;; +;; Copyright (C) KolibriOS team 2020. All rights reserved. ;; +;; Distributed under terms of the GNU General Public License ;; +;; Version 2, or (at your option) any later version. ;; +;; ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; ;; +;; Based on UEFI library for fasm by bzt, Public Domain. ;; +;; ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +EFI_LOCATE_SEARCH_TYPE: +.AllHandles = 0 +.ByRegisterNotify = 1 +.ByProtocol = 2 ; EFI_MEMORY_TYPE EFI_RESERVED_MEMORY_TYPE = 0 @@ -27,20 +38,18 @@ EFI_ALLOCATE_ANY_PAGES = 0 EFI_ALLOCATE_MAX_ADDRESS = 1 EFI_ALLOCATE_ADDRESS = 2 -EFI_MEMORY_UC = 0x0000000000000001 -EFI_MEMORY_WC = 0x0000000000000002 -EFI_MEMORY_WT = 0x0000000000000004 -EFI_MEMORY_WB = 0x0000000000000008 -EFI_MEMORY_UCE = 0x0000000000000010 -EFI_MEMORY_WP = 0x0000000000001000 -EFI_MEMORY_RP = 0x0000000000002000 -EFI_MEMORY_XP = 0x0000000000004000 -EFI_MEMORY_NV = 0x0000000000008000 -EFI_MEMORY_MORE_RELIABLE = 0x0000000000010000 -EFI_MEMORY_RO = 0x0000000000020000 -EFI_MEMORY_RUNTIME = 0x8000000000000000 +EFI_MEMORY_UC = 0x00000001 +EFI_MEMORY_WC = 0x00000002 +EFI_MEMORY_WT = 0x00000004 +EFI_MEMORY_WB = 0x00000008 +EFI_MEMORY_UCE = 0x00000010 +EFI_MEMORY_WP = 0x00001000 +EFI_MEMORY_RP = 0x00002000 +EFI_MEMORY_XP = 0x00004000 +EFI_MEMORY_NV = 0x00008000 +EFI_MEMORY_MORE_RELIABLE = 0x00010000 +EFI_MEMORY_RO = 0x00020000 -EFIERR = 0x8000000000000000 EFI_SUCCESS = 0 EFI_LOAD_ERROR = EFIERR or 1 EFI_INVALID_PARAMETER = EFIERR or 2 @@ -67,231 +76,190 @@ EFI_ICMP_ERROR = EFIERR or 22 EFI_TFTP_ERROR = EFIERR or 23 EFI_PROTOCOL_ERROR = EFIERR or 24 -struct EFI_FILE_SYSTEM_INFO - Size dq ? - ReadOnly db ? - rb 7 - VolumeSize dq ? - FreeSpace dq ? - BlockSize dd ? - VolumeLabel rw 32 -ends EFI_FILE_SYSTEM_INFO_ID equ 0x93,0x6e,0x57,0x09,0x3f,0x6d,0xd2,0x11, \ 0x39,0x8e,0x00,0xa0,0xc9,0x69,0x72,0x3b -EFI_SYSTEM_TABLE_SIGNATURE equ 0x49,0x42,0x49,0x20,0x53,0x59,0x53,0x54 +EFI_SYSTEM_TABLE_SIGNATURE equ 0x49,0x42,0x49,0x20,0x53,0x59,0x53,0x54 + +EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID equ 0x22,0x5b,0x4e,0x96, \ + 0x59,0x64,0xd2,0x11, \ + 0x8e,0x39,0x00,0xa0, \ + 0xc9,0x69,0x72,0x3b + +EFI_LOADED_IMAGE_PROTOCOL_GUID equ 0xA1,0x31,0x1b,0x5b,0x62,0x95,0xd2,0x11, \ + 0x8E,0x3F,0x00,0xA0,0xC9,0x69,0x72,0x3B + +EFI_BLOCK_IO_PROTOCOL_GUID equ 0x21,0x5b,0x4e,0x96,0x59,0x64,0xd2,0x11, \ + 0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b + +EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID equ 0xde,0xa9,0x42,0x90,0xdc,0x23,0x38,0x4a, \ + 0x96,0xfb,0x7a,0xde,0xd0,0x80,0x51,0x6a + +EFI_FILE_MODE_READ = 1 +EFI_FILE_MODE_WRITE = 2 +EFI_FILE_MODE_CREATE = 0x8000000000000000 + +struct EFI_MEMORY_DESCRIPTOR + Type dd ? + dd ? ; align + PhysicalStart DQ ? + VirtualStart DQ ? + NumberOfPages DQ ? + Attribute DQ ? +ends + +struct EFI_FILE_SYSTEM_INFO + Size DQ ? + ReadOnly db ? + rb 7 + VolumeSize DQ ? + FreeSpace DQ ? + BlockSize dd ? + VolumeLabel rw 32 +ends + struct EFI_TABLE_HEADER - Signature dq ? + Signature DQ ? Revision dd ? HeaderSize dd ? CRC32 dd ? Reserved dd ? ends -struct EFI_SYSTEM_TABLE - Hdr EFI_TABLE_HEADER - FirmwareVendor dq ? - FirmwareRevision dd ? - dd ? - ConsoleInHandle dq ? - ConIn dq ? - ConsoleOutHandle dq ? - ConOut dq ? - StandardErrorHandle dq ? - StdErr dq ? - RuntimeServices dq ? - BootServices dq ? - NumberOfTableEntries dq ? - ConfigurationTable dq ? -ends - -struct SIMPLE_TEXT_OUTPUT_INTERFACE - Reset dq ? - OutputString dq ? - TestString dq ? - QueryMode dq ? - SetMode dq ? - SetAttribute dq ? - ClearScreen dq ? - SetCursorPosition dq ? - EnableCursor dq ? - Mode dq ? +struct EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL + Reset DN ? + OutputString DN ? + TestString DN ? + QueryMode DN ? + SetMode DN ? + SetAttribute DN ? + ClearScreen DN ? + SetCursorPosition DN ? + EnableCursor DN ? + Mode DN ? ends struct SIMPLE_INPUT_INTERFACE - Reset dq ? - ReadKeyStroke dq ? - WaitForKey dq ? + Reset DN ? + ReadKeyStroke DN ? + WaitForKey DN ? ends -struct EFI_BOOT_SERVICES_TABLE +struct EFI_BOOT_SERVICES + Hdr EFI_TABLE_HEADER + RaisePriority DN ? + RestorePriority DN ? + AllocatePages DN ? + FreePages DN ? + GetMemoryMap DN ? + AllocatePool DN ? + FreePool DN ? + CreateEvent DN ? + SetTimer DN ? + WaitForEvent DN ? + SignalEvent DN ? + CloseEvent DN ? + CheckEvent DN ? + InstallProtocolInterface DN ? + ReInstallProtocolInterface DN ? + UnInstallProtocolInterface DN ? + HandleProtocol DN ? + Reserved DN ? + RegisterProtocolNotify DN ? + LocateHandle DN ? + LocateDevicePath DN ? + InstallConfigurationTable DN ? + ImageLoad DN ? + ImageStart DN ? + Exit DN ? + ImageUnLoad DN ? + ExitBootServices DN ? + GetNextMonotonicCount DN ? + Stall DN ? + SetWatchdogTimer DN ? + ConnectController DN ? + DisConnectController DN ? + OpenProtocol DN ? + CloseProtocol DN ? + OpenProtocolInformation DN ? + ProtocolsPerHandle DN ? + LocateHandleBuffer DN ? + LocateProtocol DN ? + InstallMultipleProtocolInterfaces DN ? + UnInstallMultipleProtocolInterfaces DN ? + CalculateCrc32 DN ? + CopyMem DN ? + SetMem DN ? +ends + +struct EFI_RUNTIME_SERVICES Hdr EFI_TABLE_HEADER - RaisePriority dq ? - RestorePriority dq ? - AllocatePages dq ? - FreePages dq ? - GetMemoryMap dq ? - AllocatePool dq ? - FreePool dq ? - CreateEvent dq ? - SetTimer dq ? - WaitForEvent dq ? - SignalEvent dq ? - CloseEvent dq ? - CheckEvent dq ? - InstallProtocolInterface dq ? - ReInstallProtocolInterface dq ? - UnInstallProtocolInterface dq ? - HandleProtocol dq ? - Void dq ? - RegisterProtocolNotify dq ? - LocateHandle dq ? - LocateDevicePath dq ? - InstallConfigurationTable dq ? - ImageLoad dq ? - ImageStart dq ? - Exit dq ? - ImageUnLoad dq ? - ExitBootServices dq ? - GetNextMonotonicCount dq ? - Stall dq ? - SetWatchdogTimer dq ? - ConnectController dq ? - DisConnectController dq ? - OpenProtocol dq ? - CloseProtocol dq ? - OpenProtocolInformation dq ? - ProtocolsPerHandle dq ? - LocateHandleBuffer dq ? - LocateProtocol dq ? - InstallMultipleProtocolInterfaces dq ? - UnInstallMultipleProtocolInterfaces dq ? - CalculateCrc32 dq ? - CopyMem dq ? - SetMem dq ? + GetTime DN ? + SetTime DN ? + GetWakeUpTime DN ? + SetWakeUpTime DN ? + SetVirtualAddressMap DN ? + ConvertPointer DN ? + GetVariable DN ? + GetNextVariableName DN ? + SetVariable DN ? + GetNextHighMonoCount DN ? + ResetSystem DN ? ends -struct EFI_RUNTIME_SERVICES_TABLE - Hdr EFI_TABLE_HEADER - GetTime dq ? - SetTime dq ? - GetWakeUpTime dq ? - SetWakeUpTime dq ? - SetVirtualAddressMap dq ? - ConvertPointer dq ? - GetVariable dq ? - GetNextVariableName dq ? - SetVariable dq ? - GetNextHighMonoCount dq ? - ResetSystem dq ? -ends - -struct EFI_TIME - Year dw ? - Month db ? - Day db ? - Hour db ? - Minute db ? - Second db ? - Pad1 db ? - Nanosecond dd ? - TimeZone dw ? - Daylight db ? - Pad2 db ? - sizeof db ? -ends - -EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID equ 0x22,0x5b,0x4e,0x96,0x59,0x64,0xd2,0x11, \ - 0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b - struct EFI_SIMPLE_FILE_SYSTEM_PROTOCOL - Revision dq ? - OpenVolume dq ? + Revision DQ ? + OpenVolume DN ? ends -EFI_FILE_MODE_READ = 1 -EFI_FILE_MODE_WRITE = 2 -EFI_FILE_MODE_CREATE = 0x8000000000000000 - struct EFI_FILE_PROTOCOL - Revision dq ? - Open dq ? - Close dq ? - Delete dq ? - Read dq ? - Write dq ? - GetPosition dq ? - SetPosition dq ? - GetInfo dq ? - SetInfo dq ? - Flush dq ? - OpenEx dq ? - ReadEx dq ? - WriteEx dq ? - FlushEx dq ? + Revision DQ ? + Open DN ? + Close DN ? + Delete DN ? + Read DN ? + Write DN ? + GetPosition DN ? + SetPosition DN ? + GetInfo DN ? + SetInfo DN ? + Flush DN ? + OpenEx DN ? + ReadEx DN ? + WriteEx DN ? + FlushEx DN ? ends -EFI_LOADED_IMAGE_PROTOCOL_GUID equ 0xA1,0x31,0x1b,0x5b,0x62,0x95,0xd2,0x11, \ - 0x8E,0x3F,0x00,0xA0,0xC9,0x69,0x72,0x3B -struct EFI_LOADED_IMAGE_PROTOCOL - Revision dd ? - dd ? - ParentHandle dq ? - SystemTable dq ? - DeviceHandle dq ? - FilePath dq ? - Reserved dq ? - LoadOptionsSize dd ? - dd ? - ImageBase dq ? - ImageSize dq ? - ImageCodeType dd ? - ImageDataType dd ? - UnLoad dq ? -ends - -EFI_BLOCK_IO_PROTOCOL_GUID equ 0x21,0x5b,0x4e,0x96,0x59,0x64,0xd2,0x11, \ - 0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b -struct EFI_BLOCK_IO_PROTOCOL - Revision dq ? - Media dq ? - Reset dq ? - ReadBlocks dq ? - WriteBlocks dq ? - FlushBlocks dq ? -ends - -struct EFI_BLOCK_IO_MEDIA - MediaId dd ? - RemovableMedia db ? - MediaPresent db ? - LogicalPartition db ? - ReadOnly db ? - WriteCaching db ? - rb 3 - BlockSize dd ? - IoAlign dd ? - LastBlock dq ? -ends - -EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID equ 0xde,0xa9,0x42,0x90,0xdc,0x23,0x38,0x4a, \ - 0x96,0xfb,0x7a,0xde,0xd0,0x80,0x51,0x6a struct EFI_GRAPHICS_OUTPUT_PROTOCOL - QueryMode dq ? - SetMode dq ? - Blt dq ? - Mode dq ? + QueryMode DN ? + SetMode DN ? + Blt DN ? + Mode DN ? ends struct EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE MaxMode dd ? Mode dd ? - nfo dq ? - SizeOfInfo dq ? - FrameBufferBase dq ? - FrameBufferSize dq ? + Info DN ? + SizeOfInfo DN ? + FrameBufferBase DQ ? + FrameBufferSize DN ? +ends + +EFI_GRAPHICS_PIXEL_FORMAT: +.PixelRedGreenBlueReserved8BitPerColor = 0 +.PixelBlueGreenRedReserved8BitPerColor = 1 +.PixelBitMask = 2 +.PixelBltOnly = 3 +.PixelFormatMax = 4 + +struct EFI_PIXEL_BITMASK + RedMask dd ? + GreenMask dd ? + BlueMask dd ? + ReservedMask dd ? ends struct EFI_GRAPHICS_OUTPUT_MODE_INFORMATION @@ -299,217 +267,6 @@ struct EFI_GRAPHICS_OUTPUT_MODE_INFORMATION HorizontalResolution dd ? VerticalResolution dd ? PixelFormat dd ? - RedMask dd ? - GreenMask dd ? - BlueMask dd ? - Reserved dd ? + PixelInformation EFI_PIXEL_BITMASK PixelsPerScanLine dd ? ends - -;---macros to make life easier--- -;call it early, after entry point is the best -macro InitializeLib -{ - clc - test rdx, rdx - jz .badout - cmp dword[rdx], 'IBI ' ; 20494249h - jz @f - .badout: - xor ecx, ecx - xor edx, edx - stc - @@: - mov [efi_handler], rcx ; ImageHandle - mov [efi_ptr], rdx ; pointer to SystemTable -} - -;invoke an UEFI function -macro eficall interface,function,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11 -{ -numarg = 0 - -if ~ arg11 eq - numarg = numarg + 1 - if ~ arg11 eq rdi - mov rdi, arg11 - end if -end if - -if ~ arg10 eq - numarg = numarg + 1 - if ~ arg10 eq rsi - mov rsi, arg10 - end if -end if - -if ~ arg9 eq - numarg = numarg + 1 - if ~ arg9 eq r14 - mov r14, arg9 - end if -end if - -if ~ arg8 eq - numarg = numarg + 1 - if ~ arg8 eq r13 - mov r13, arg8 - end if -end if - -if ~ arg7 eq - numarg = numarg + 1 - if ~ arg7 eq r12 - mov r12, arg7 - end if -end if - -if ~ arg6 eq - numarg = numarg + 1 - if ~ arg6 eq r11 - mov r11, arg6 - end if -end if - -if ~ arg5 eq - numarg = numarg + 1 - if ~ arg5 eq r10 - mov r10, arg5 - end if -end if - -if ~ arg4 eq - numarg = numarg + 1 - if ~ arg4 eq r9 - mov r9, arg4 - end if -end if - -if ~ arg3 eq - numarg = numarg + 1 - if ~ arg3 eq r8 - mov r8, arg3 - end if -end if - -if ~ arg2 eq - numarg = numarg + 1 - if ~ arg2 eq rdx - mov rdx, arg2 - end if -end if - -if ~ arg1 eq - numarg = numarg + 1 - if ~ arg1 eq rcx - if ~ arg1 in - mov rcx, arg1 - end if - end if -end if - - xor rax, rax - mov al, numarg - -if interface in - mov rbx, [efi_ptr] - mov rbx, [rbx + EFI_SYSTEM_TABLE.#interface] -else - if ~ interface eq rbx - mov rbx, interface - end if -end if - -if arg1 in - mov rcx, rbx -end if - -if defined SIMPLE_INPUT_INTERFACE.#function - mov rbx, [rbx + SIMPLE_INPUT_INTERFACE.#function] -else - if defined SIMPLE_TEXT_OUTPUT_INTERFACE.#function - mov rbx, [rbx + SIMPLE_TEXT_OUTPUT_INTERFACE.#function] - else - if defined EFI_BOOT_SERVICES_TABLE.#function - mov rbx, [rbx + EFI_BOOT_SERVICES_TABLE.#function] - else - if defined EFI_RUNTIME_SERVICES_TABLE.#function - mov rbx, [rbx + EFI_RUNTIME_SERVICES_TABLE.#function] - else - if defined EFI_GRAPHICS_OUTPUT_PROTOCOL.#function - mov rbx, [rbx + EFI_GRAPHICS_OUTPUT_PROTOCOL.#function] - else - if defined EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.#function - mov rbx, [rbx + EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.#function] - else - mov rbx, [rbx + function] - end if - end if - end if - end if - end if -end if - call uefifunc -} - -;********************************************************************* -;* Library functions * -;********************************************************************* - -section '.text' code executable readable - -uefifunc: - ;save stack pointer - mov qword [uefi_rsptmp], rsp - ;set up new aligned stack - and esp, 0xFFFFFFF0 - ;alignment check on arguments - bt eax, 0 - jnc @f - push rax - ;arguments -@@: - cmp al, 11 - jb @f - push rdi -@@: - cmp al, 10 - jb @f - push rsi -@@: - cmp al, 9 - jb @f - push r14 -@@: - cmp al, 8 - jb @f - push r13 -@@: - cmp al, 7 - jb @f - push r12 -@@: - cmp al, 6 - jb @f - push r11 -@@: - cmp al, 5 - jb @f - push r10 -@@: - ;space for - ;r9 - ;r8 - ;rdx - ;rcx - sub rsp, 4*8 - ;call function - call rbx - ;restore old stack - mov rsp, qword [uefi_rsptmp] - ret - -section '.data' data readable writeable -efi_handler dq 0 -efi_ptr dq 0 -uefi_rsptmp dq 0 diff --git a/kernel/trunk/bootloader/uefi4kos/uefi32.inc b/kernel/trunk/bootloader/uefi4kos/uefi32.inc new file mode 100644 index 0000000000..7b55a6b568 --- /dev/null +++ b/kernel/trunk/bootloader/uefi4kos/uefi32.inc @@ -0,0 +1,58 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; ;; +;; Copyright (C) KolibriOS team 2020. All rights reserved. ;; +;; Distributed under terms of the GNU General Public License ;; +;; Version 2, or (at your option) any later version. ;; +;; ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; ;; +;; Based on UEFI library for fasm by bzt, Public Domain. ;; +;; ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +DN fix dd ; native + +include "uefi.inc" + +EFIERR = 0x80000000 + +struct EFI_SYSTEM_TABLE + Hdr EFI_TABLE_HEADER + FirmwareVendor dd ? + FirmwareRevision dd ? + ConsoleInHandle dd ? + ConIn dd ? + ConsoleOutHandle dd ? + ConOut dd ? + StandardErrorHandle dd ? + StdErr dd ? + RuntimeServices dd ? + BootServices dd ? + NumberOfTableEntries dd ? + ConfigurationTable dd ? +ends + +struct EFI_CONFIGURATION_TABLE + VendorGUID rd 4 + VendorTable dd ? +ends + +struct EFI_LOADED_IMAGE_PROTOCOL + Revision dd ? + ParentHandle dd ? + SystemTable dd ? + DeviceHandle dd ? + FilePath dd ? + Reserved dd ? + LoadOptionsSize dd ? + ImageBase dd ? + ImageSize DQ ? + ImageCodeType dd ? + ImageDataType dd ? + UnLoad dd ? +ends + +section '.text' code executable readable + +uefifunc: + ret diff --git a/kernel/trunk/bootloader/uefi4kos/uefi32kos.asm b/kernel/trunk/bootloader/uefi4kos/uefi32kos.asm new file mode 100644 index 0000000000..20a1fb4d91 --- /dev/null +++ b/kernel/trunk/bootloader/uefi4kos/uefi32kos.asm @@ -0,0 +1,1005 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; ;; +;; Copyright (C) KolibriOS team 2020. All rights reserved. ;; +;; Distributed under terms of the GNU General Public License ;; +;; Version 2, or (at your option) any later version. ;; +;; ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +format pe efi +entry main + +section '.text' code executable readable + +include '../../struct.inc' +include '../../macros.inc' +include '../../proc32.inc' +include '../../const.inc' +include 'uefi32.inc' + +MEMORY_MAP_SIZE = 0x4000 +GOP_BUFFER_SIZE = 0x100 +LIP_BUFFER_SIZE = 0x100 +FILE_BUFFER_SIZE = 0x1000 + +KERNEL_BASE = 0x10000 +RAMDISK_BASE = 0x100000 + +CODE_32_SELECTOR = 8 +DATA_32_SELECTOR = 16 + +; linux/arch/x86/include/uapi/asm/e820.h +E820_RAM = 1 +E820_RESERVED = 2 +E820_ACPI = 3 +E820_NVS = 4 +E820_UNUSABLE = 5 +E820_PMEM = 7 + +proc load_file stdcall uses ebx esi edi, _root, _name, _buffer, _size, _fatal + mov eax, [_root] + ccall [eax+EFI_FILE_PROTOCOL.Open], eax, file_handle, [_name], \ + EFI_FILE_MODE_READ, 0 + test eax, eax + jz @f + xor eax, eax + cmp [_fatal], 1 + jnz .done + mov ebx, [efi_table] + mov ebx, [ebx+EFI_SYSTEM_TABLE.ConOut] + ccall [ebx+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], ebx, \ + msg_error_open_file + ccall [ebx+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], ebx, \ + [_name] + jmp $ +@@: + mov eax, [file_handle] + lea ecx, [_size] + ccall [eax+EFI_FILE_PROTOCOL.Read], eax, ecx, [_buffer] + mov eax, [file_handle] + ccall [eax+EFI_FILE_PROTOCOL.Close], eax + mov eax, [_size] +.done: + ret +endp + +proc skip_whitespace +.next_char: + cmp byte[esi], 0 + jz .done + cmp byte[esi], 0x20 ; ' ' + jz .whitespace + cmp byte[esi], 9 ; '\t' + jz .whitespace + jmp .done +.whitespace: + inc esi + jmp .next_char +.done: + ret +endp + +proc skip_until_newline +.next_char: + cmp byte[esi], 0 + jz .done + cmp byte[esi], 0xd ; '\r' + jz .done + cmp byte[esi], 0xa ; '\n' + jz .done + inc esi + jmp .next_char +.done: + ret +endp + +proc skip_newline +.next_char: + cmp byte[esi], 0xd ; '\r' + jz .newline + cmp byte[esi], 0xa ; '\n' + jz .newline + jmp .done +.newline: + inc esi + jmp .next_char +.done: + ret +endp + +proc skip_line + call skip_until_newline + call skip_newline + ret +endp + +proc dec2bin + mov edx, 0 +.next_char: + movzx eax, byte[esi] + test eax, eax + jz .done + sub eax, '0' + jb .done + cmp eax, 9 + ja .done + inc esi + imul edx, 10 + add edx, eax + jmp .next_char +.done: + mov eax, edx + ret +endp + +proc parse_option + mov ebx, config_options-3*4 +.try_next_option: + add ebx, 3*4 + mov edi, esi + mov edx, [ebx] ; option name + test edx, edx + jz .done +.next_char: + cmp byte[edx], 0 + jnz @f + cmp byte[edi], '=' + jz .opt_name_ok +@@: + cmp byte[edi], 0 + jz .done + movzx eax, byte[edi] + cmp [edx], al + jnz .try_next_option + inc edi + inc edx + jmp .next_char +.opt_name_ok: + inc edi + mov esi, edi + call dword[ebx+4] +.done: + ret +endp + +proc parse_line +.next_line: + cmp byte[esi], 0 + jz .done + cmp byte[esi], 0xd ; '\r' + jz .skip + cmp byte[esi], 0xa ; '\n' + jz .skip + cmp byte[esi], '#' + jz .skip + call parse_option + call skip_line + jmp .next_line +.skip: + call skip_line + jmp .next_line +.done: + ret +endp + +proc cfg_opt_func_resolution + call dec2bin + xor edx, edx + mov [edx+BOOT_LO.x_res], ax + cmp byte[esi], 'x' + jz @f + cmp byte[esi], '*' + jz @f + jmp .done +@@: + inc esi + call dec2bin + xor edx, edx + mov [edx+BOOT_LO.y_res], ax + mov [cfg_opt_used_resolution], 1 +.done: + ret +endp + +proc cfg_opt_func_acpi + call dec2bin + mov [cfg_opt_used_acpi], 1 + mov [cfg_opt_value_acpi], al + ret +endp + +proc cfg_opt_func_debug_print + call dec2bin + mov [cfg_opt_used_debug_print], 1 + mov [cfg_opt_value_debug_print], al + ret +endp + +proc cfg_opt_func_launcher_start + call dec2bin + mov [cfg_opt_used_launcher_start], 1 + mov [cfg_opt_value_launcher_start], al + ret +endp + +proc cfg_opt_func_mtrr + call dec2bin + mov [cfg_opt_used_mtrr], 1 + mov [cfg_opt_value_mtrr], al + ret +endp + +proc cfg_opt_func_ask_params + call dec2bin + mov [cfg_opt_used_ask_params], 1 + mov [cfg_opt_value_ask_params], al + ret +endp + +proc cfg_opt_func_imgfrom + call dec2bin + mov [cfg_opt_used_imgfrom], 1 + mov [cfg_opt_value_imgfrom], al + ret +endp + +proc cfg_opt_func_syspath + mov edi, cfg_opt_value_syspath +.next_char: + movzx eax, byte[esi] + cmp al, 0xd ; \r + jz .done + cmp al, 0xa ; \n + jz .done + inc esi + stosb + jmp .next_char +.done: + mov byte[edi], 0 + ret +endp + +proc parse_config stdcall uses ebx esi edi, _buffer +; mov esi, [_buffer] + mov esi, KERNEL_BASE +.next_line: + call parse_line + cmp byte[esi], 0 + jnz .next_line + ret +endp + +proc read_options_from_config stdcall uses ebx esi edi + mov ebx, [efi_table] + mov ebx, [ebx+EFI_SYSTEM_TABLE.BootServices] + ccall [ebx+EFI_BOOT_SERVICES.HandleProtocol], [efi_handle], \ + lipuuid, lip_interface + test eax, eax + jnz .error + mov eax, [lip_interface] + + mov ebx, [efi_table] + mov ebx, [ebx+EFI_SYSTEM_TABLE.BootServices] + ccall [ebx+EFI_BOOT_SERVICES.HandleProtocol], \ + [eax+EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle], sfspguid, \ + sfsp_interface + test eax, eax + jnz .error + + mov eax, [sfsp_interface] + ccall [eax+EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume], eax, esp_root + test eax, eax + jnz .error + + stdcall load_file, [esp_root], file_name, KERNEL_BASE, \ + FILE_BUFFER_SIZE, 0 + test eax, eax + jz @f + stdcall parse_config, KERNEL_BASE +@@: +.error: + ret +endp + +proc print_vmode uses eax ebx ecx esi edi, _gop_if + mov ebx, [_gop_if] + call clearbuf + mov eax, [ebx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution] + mov edi, msg + call num2dec + mov eax, [ebx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution] + mov edi, msg+8*2 + call num2dec + + mov eax, [ebx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelFormat] + mov edi, msg+16*2 + call num2dec + + mov eax, [ebx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelsPerScanLine] + mov edi, msg+24*2 + call num2dec + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg + ret +endp + +proc find_vmode_index_by_resolution uses ebx esi edi + mov [cfg_opt_value_vmode], 0 +.next_mode: +; mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] +; ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ +; msg_query_vmode + + movzx ecx, [cfg_opt_value_vmode] + mov eax, [gop_interface] + ccall [eax+EFI_GRAPHICS_OUTPUT_PROTOCOL.QueryMode], eax, ecx, \ + gop_info_size, gop_info + test eax, eax + jz @f + call clearbuf + mov edi, msg + call num2hex + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg + jmp .skip_mode +@@: + mov ecx, [gop_info] + stdcall print_vmode, ecx + ; PixelBlueGreenRedReserved8BitPerColor + cmp [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelFormat], 1 + jnz .skip_mode + xor edx, edx + mov eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution] + cmp ax, [edx+BOOT_LO.x_res] + jnz .skip_mode + mov eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution] + cmp ax, [edx+BOOT_LO.y_res] + jnz .skip_mode + jmp .done +.skip_mode: + inc [cfg_opt_value_vmode] + movzx eax, [cfg_opt_value_vmode] + mov ecx, [gop_interface] + mov edx, [ecx+EFI_GRAPHICS_OUTPUT_PROTOCOL.Mode] + cmp eax, [edx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.MaxMode] + jnz .next_mode + mov [cfg_opt_used_resolution], 0 + mov [cfg_opt_value_ask_params], 1 + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_error_no_such_vmode + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg_error + jmp $ +.error: +.done: + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_vmode_found + ret +endp + +proc ask_for_params + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_ask_for_params + jmp $ +.error: +.done: + ret +endp + +main: + mov esi, [esp+4] + mov [efi_handle], esi + mov esi, [esp+8] + mov [efi_table], esi + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset], eax, 1 + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_u4k_loaded + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_read_options + call read_options_from_config + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_load_kernel + stdcall load_file, [esp_root], kernel_name, KERNEL_BASE, -1, 1 + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_load_ramdisk + stdcall load_file, [esp_root], ramdisk_name, RAMDISK_BASE, -1, 1 + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_alloc_devicesdat + + mov eax, [esi+EFI_SYSTEM_TABLE.BootServices] + ccall [eax+EFI_BOOT_SERVICES.AllocatePages], \ + EFI_ALLOCATE_MAX_ADDRESS, EFI_RESERVED_MEMORY_TYPE, 1, \ + devicesdat_data + test eax, eax + jnz .error + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_load_devicesdat + + ccall load_file, [esp_root], devicesdat_name, [devicesdat_data], \ + [devicesdat_size], 0 + mov [devicesdat_size], eax + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_locate_gop_handlers + + mov eax, [esi+EFI_SYSTEM_TABLE.BootServices] + ccall [eax+EFI_BOOT_SERVICES.LocateHandle], \ + EFI_LOCATE_SEARCH_TYPE.ByProtocol, gopuuid, 0, \ + gop_buffer_size, gop_buffer + mov [status], eax + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_gop_buffer_size + call clearbuf + mov eax, [gop_buffer_size] + mov edi, msg + call num2hex + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg + + mov eax, [status] + test eax, eax + jz @f + call clearbuf + mov edi, msg + call num2hex + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg_error + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg + jmp $ +@@: + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_look_for_gop_handler + + mov ebx, gop_buffer +.next_gop_handle: + mov eax, ebx + sub eax, gop_buffer + cmp eax, [gop_buffer_size] + jb @f + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_error_out_of_handlers + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg_error + jmp $ +@@: + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_query_handler + + mov eax, [esi+EFI_SYSTEM_TABLE.BootServices] + ccall [eax+EFI_BOOT_SERVICES.HandleProtocol], \ + [ebx], gopuuid, gop_interface +;mov eax, 0x80000003 + test eax, eax + jz @f + call clearbuf + mov edi, msg + call num2hex + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg + + add ebx, 4 + jmp .next_gop_handle +@@: + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_look_for_rsdp + + mov ebx, [efi_table] + mov edi, [ebx+EFI_SYSTEM_TABLE.ConfigurationTable] + mov ecx, [ebx+EFI_SYSTEM_TABLE.NumberOfTableEntries] +.next_table: + dec ecx + js .all_tables_done + ; EFI_ACPI_TABLE_GUID + cmp dword[edi+EFI_CONFIGURATION_TABLE.VendorGUID+0x0], 0x8868e871 + jnz .not_acpi20 + cmp dword[edi+EFI_CONFIGURATION_TABLE.VendorGUID+0x4], 0x11d3e4f1 + jnz .not_acpi20 + cmp dword[edi+EFI_CONFIGURATION_TABLE.VendorGUID+0x8], 0x800022bc + jnz .not_acpi20 + cmp dword[edi+EFI_CONFIGURATION_TABLE.VendorGUID+0xc], 0x81883cc7 + jnz .not_acpi20 + mov eax, [edi+EFI_CONFIGURATION_TABLE.VendorTable] + mov edx, BOOT_LO.acpi_rsdp + mov [edx], eax + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_rsdp_found + jmp .all_tables_done +.not_acpi20: + add edi, sizeof.EFI_CONFIGURATION_TABLE + jmp .next_table +.all_tables_done: + + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_acpi_tables_done + + cmp [cfg_opt_used_resolution], 0 + jz .not_used_resolution + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_opt_resolution + call clearbuf + xor edx, edx + movzx eax, [edx+BOOT_LO.x_res] + mov edi, msg + call num2dec + xor edx, edx + movzx eax, [edx+BOOT_LO.y_res] + mov edi, msg+8*2 + call num2dec + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg + + call find_vmode_index_by_resolution +.not_used_resolution: + cmp [cfg_opt_used_debug_print], 0 + jz .not_used_debug_print + movzx eax, [cfg_opt_value_debug_print] + xor edx, edx + mov [edx+BOOT_LO.debug_print], al +.not_used_debug_print: + + cmp [cfg_opt_value_ask_params], 0 + jz @f + call ask_for_params +@@: + + movzx ecx, [cfg_opt_value_vmode] + mov eax, [gop_interface] + ccall [eax+EFI_GRAPHICS_OUTPUT_PROTOCOL.SetMode], eax, ecx + test eax, eax + jz @f + call clearbuf + mov edi, msg + call num2hex + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, msg_error + jmp $ +@@: + + mov ecx, [gop_interface] + mov edx, [ecx+EFI_GRAPHICS_OUTPUT_PROTOCOL.Mode] + mov edi, [edx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.FrameBufferBase.lo] + mov [fb_base], edi + + + mov ebx, [edx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.Mode] + mov eax, [gop_interface] + ccall [eax+EFI_GRAPHICS_OUTPUT_PROTOCOL.QueryMode], eax, ebx, \ + gop_info_size, gop_info + test eax, eax + jnz .error + mov ecx, [gop_info] + mov eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution] + xor edx, edx + mov [edx+BOOT_LO.x_res], ax + mov eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution] + mov [edx+BOOT_LO.y_res], ax + mov eax, [ecx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelsPerScanLine] + shl eax, 2 + mov [edx+BOOT_LO.pitch], ax + + mov byte[edx+BOOT_LO.pci_data+0], 1 ; PCI access mechanism + mov byte[edx+BOOT_LO.pci_data+1], 8 ; last bus, don't know how to count them + mov byte[edx+BOOT_LO.pci_data+2], 0x10 ; PCI version + mov byte[edx+BOOT_LO.pci_data+3], 0x02 + mov dword[edx+BOOT_LO.pci_data+4], 0xe3 + + + mov eax, [efi_table] + mov eax, [eax+EFI_SYSTEM_TABLE.BootServices] + ccall [eax+EFI_BOOT_SERVICES.AllocatePages], \ + EFI_ALLOCATE_ANY_PAGES, EFI_RESERVED_MEMORY_TYPE, \ + MEMORY_MAP_SIZE/0x1000, memory_map + test eax, eax + jnz .error + + mov eax, [efi_table] + mov eax, [eax+EFI_SYSTEM_TABLE.BootServices] + ccall [eax+EFI_BOOT_SERVICES.GetMemoryMap], memory_map_size, \ + [memory_map], memory_map_key, descriptor_size, descriptor_ver + test eax, eax + jnz .error + + mov edi, BOOT_LO.memmap_block_cnt + mov dword[edi], 0 + mov edi, BOOT_LO.memmap_blocks + mov eax, [memory_map_size] + xor edx, edx + mov ecx, [descriptor_size] + div ecx + mov ecx, eax + mov esi, [memory_map] +.next_descr: + call add_uefi_memmap + add esi, [descriptor_size] + add edi, sizeof.e820entry + dec ecx + test ecx, ecx + jnz .next_descr + + ; kernel +; eficall BootServices, AllocatePages, EFI_RESERVED_MEMORY_TYPE, \ +; 450000/0x1000, EFI_ALLOCATE_ADDRESS + + ; ramdisk +; eficall BootServices, AllocatePages, EFI_RESERVED_MEMORY_TYPE, \ +; 2880*512/0x1000, EFI_ALLOCATE_ADDRESS + + mov eax, [efi_table] + mov eax, [eax+EFI_SYSTEM_TABLE.BootServices] + ccall [eax+EFI_BOOT_SERVICES.ExitBootServices], [efi_handle], \ + [memory_map_key] + test eax, eax + jnz .error + + + cli + + xor edx, edx + xor esi, esi + mov [esi+BOOT_LO.bpp], 32 + mov [esi+BOOT_LO.vesa_mode], dx + mov [esi+BOOT_LO.bank_switch], edx + mov edi, [fb_base] + mov [esi+BOOT_LO.lfb], edi + + movzx eax, [cfg_opt_value_mtrr] + mov [esi+BOOT_LO.mtrr], al + + movzx eax, [cfg_opt_value_launcher_start] + mov [esi+BOOT_LO.launcher_start], al + + movzx eax, [cfg_opt_value_debug_print] + mov [esi+BOOT_LO.debug_print], al + + mov [esi+BOOT_LO.dma], dl +; mov qword[esi+BOOT_LO.pci_data], 0 + mov [esi+BOOT_LO.apm_entry], edx + mov [esi+BOOT_LO.apm_version], dx + mov [esi+BOOT_LO.apm_flags], dx + mov [esi+BOOT_LO.apm_code_32], dx + mov [esi+BOOT_LO.apm_code_16], dx + mov [esi+BOOT_LO.apm_data_16], dx + mov [esi+BOOT_LO.bios_hd_cnt], dl + + movzx eax, [cfg_opt_value_imgfrom] + mov [esi+BOOT_LO.rd_load_from], al + + mov eax, dword[devicesdat_size] + mov [edx+BOOT_LO.devicesdat_size], eax + mov eax, dword[devicesdat_data] + mov [edx+BOOT_LO.devicesdat_data], eax + + mov esi, cfg_opt_value_syspath + mov edi, BOOT_LO.syspath + mov ecx, 0x17 + rep movsb + + lgdt [cs:GDTR] + + mov ax, DATA_32_SELECTOR + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + + push CODE_32_SELECTOR + lea eax, [.next] + push eax + retf + +.next: + mov eax, cr0 + and eax, not CR0_PG + mov cr0, eax + + mov eax, cr4 + and eax, not CR4_PAE + mov cr4, eax + + push KERNEL_BASE + retn + +.error: + mov esi, [efi_table] + mov eax, [esi+EFI_SYSTEM_TABLE.ConOut] + ccall [eax+EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString], eax, \ + msg_error + jmp $ + +; linux/arch/x86/platform/efi/efi.c +; do_add_efi_memmap +proc add_uefi_memmap +locals + pew dd ? +endl + pushad + + mov edx, [esi+EFI_MEMORY_DESCRIPTOR.Type] + + mov eax, [esi+EFI_MEMORY_DESCRIPTOR.PhysicalStart.lo] ; FIXME hi + mov [edi+e820entry.addr], eax + + mov eax, [esi+EFI_MEMORY_DESCRIPTOR.NumberOfPages] + shl eax, 12 + mov [edi+e820entry.size], eax + + + cmp edx, EFI_LOADER_CODE + jz .case0 + cmp edx, EFI_LOADER_DATA + jz .case0 + cmp edx, EFI_BOOT_SERVICES_CODE + jz .case0 + cmp edx, EFI_BOOT_SERVICES_DATA + jz .case0 + cmp edx, EFI_CONVENTIONAL_MEMORY + jz .case0 + cmp edx, EFI_ACPI_RECLAIM_MEMORY + jz .case1 + cmp edx, EFI_ACPI_MEMORY_NVS + jz .case2 + cmp edx, EFI_UNUSABLE_MEMORY + jz .case3 + cmp edx, EFI_PERSISTENT_MEMORY + jz .case4 + jmp .default + +.case0: + test [esi+EFI_MEMORY_DESCRIPTOR.Attribute.lo], EFI_MEMORY_WB + jz @f + mov eax, E820_RAM + jmp .done + @@: + mov eax, E820_RESERVED + jmp .done +.case1: + mov eax, E820_ACPI + jmp .done +.case2: + mov eax, E820_NVS + jmp .done +.case3: + mov eax, E820_UNUSABLE + jmp .done +.case4: + mov eax, E820_PMEM + jmp .done +.default: + mov eax, E820_RESERVED + jmp .done + +.done: + mov [edi+e820entry.type], eax + + mov eax, BOOT_LO.memmap_block_cnt + inc word[eax] + + popad + ret +endp + + +proc num2dec + pushad + + xor ecx, ecx + mov ebx, 10 +.next_digit: + xor edx, edx + div ebx + push edx + inc ecx + test eax, eax + jnz .next_digit + +.next_char: + pop eax + add eax, '0' + stosw + loop .next_char + + popad + ret +endp + + +proc num2hex + pushad + + xchg edx, eax + mov ecx, 8 +.next_tetra: + rol edx, 4 + movzx eax, dl + and eax, 0x0f + movzx eax, byte[hex+eax] + stosw + loop .next_tetra + + popad + ret +endp + + +hex db '0123456789ABCDEF' + +proc clearbuf + pushad + mov eax, 0x0020 + mov ecx, 79 + mov edi, msg + rep stosw + popad + ret +endp + +section '.data' data readable writeable +efi_handle dd 0 +efi_table dd 0 + +GDTR: + dw 3*8-1 + dq GDT +GDT: + dw 0, 0, 0, 0 + dw 0FFFFh,0,9A00h,0CFh ; 32-bit code + dw 0FFFFh,0,9200h,0CFh ; flat data + + +fb_base dd 0 + +gopuuid db EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID +gop_buffer_size dd GOP_BUFFER_SIZE +gop_handle dd 0 +gop_interface dd 0 +gop_info_size dd 0 +gop_info dd 0 + +lipuuid db EFI_LOADED_IMAGE_PROTOCOL_GUID +lip_buffer_size dd LIP_BUFFER_SIZE +lip_handle dd 0 +lip_interface dd 0 + +sfspguid db EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID +sfsp_interface dd 0 + +esp_root dd ? +file_handle dd ? +file_name du "\EFI\KOLIBRIOS\KOLIBRI.INI",0 +kernel_name du "\EFI\KOLIBRIOS\KOLIBRI.KRN",0 +ramdisk_name du "\EFI\KOLIBRIOS\KOLIBRI.IMG",0 +devicesdat_name du "\EFI\KOLIBRIOS\DEVICES.DAT",0 +file_buffer_size dd FILE_BUFFER_SIZE-1 ; leave the last byte for \0 + +config_options dd cfg_opt_name_resolution, cfg_opt_func_resolution, \ + cfg_opt_cmnt_resolution, \ + cfg_opt_name_acpi, cfg_opt_func_acpi, cfg_opt_cmnt_acpi, \ + cfg_opt_name_debug_print, cfg_opt_func_debug_print, \ + cfg_opt_cmnt_debug_print, \ + cfg_opt_name_launcher_start, cfg_opt_func_launcher_start, \ + cfg_opt_cmnt_launcher_start, \ + cfg_opt_name_mtrr, cfg_opt_func_mtrr, cfg_opt_cmnt_mtrr, \ + cfg_opt_name_ask_params, cfg_opt_func_ask_params, \ + cfg_opt_cmnt_ask_params, \ + cfg_opt_name_imgfrom, cfg_opt_func_imgfrom, \ + cfg_opt_cmnt_imgfrom, \ + cfg_opt_name_syspath, cfg_opt_func_syspath, \ + cfg_opt_cmnt_syspath, \ + 0 + +cfg_opt_name_resolution db "resolution",0 +cfg_opt_name_acpi db "acpi",0 +cfg_opt_name_debug_print db "debug_print",0 +cfg_opt_name_launcher_start db "launcher_start",0 +cfg_opt_name_mtrr db "mtrr",0 +cfg_opt_name_ask_params db "ask_params",0 +cfg_opt_name_imgfrom db "imgfrom",0 +cfg_opt_name_syspath db "syspath",0 + +cfg_opt_cmnt_resolution db "# Graphic mode",0 +cfg_opt_cmnt_acpi db "# ACPI settings",0xa, \ + "# 0: don't use",0xa, \ + "# 1: parse ACPI tables",0xa, \ + "# 2: + call _PIC method",0xa, \ + "# 3: + get APIC interrupts",0xa,0 +cfg_opt_cmnt_debug_print db "# Duplicate debug output to the screen",0 +cfg_opt_cmnt_launcher_start db "# Start LAUNCHER app after kernel is loaded",0 +cfg_opt_cmnt_mtrr db "# Configure MTRR's",0 +cfg_opt_cmnt_ask_params db "# Interrupt booting to ask the user for boot", \ + " params",0 +cfg_opt_cmnt_imgfrom db "# Where to load ramdisk image from",0 +cfg_opt_cmnt_syspath db "# Path to /sys directory",0 + +cfg_opt_used_resolution db 0 +cfg_opt_used_acpi db 0 +cfg_opt_used_debug_print db 0 +cfg_opt_used_launcher_start db 0 +cfg_opt_used_mtrr db 0 +cfg_opt_used_ask_params db 0 +cfg_opt_used_imgfrom db 0 +cfg_opt_used_syspath db 0 + +cfg_opt_value_vmode db 0 +cfg_opt_value_acpi db 0 +cfg_opt_value_debug_print db 0 +cfg_opt_value_launcher_start db 1 +cfg_opt_value_mtrr db 0 +cfg_opt_value_ask_params db 0 +cfg_opt_value_imgfrom db RD_LOAD_FROM_MEMORY +cfg_opt_value_syspath db "/RD/1",0 + rb 20 + +memory_map_key dd 0 +descriptor_size dd 0 +descriptor_ver dd 0 +memory_map_size dd MEMORY_MAP_SIZE + +msg_u4k_loaded du "uefi32kos loaded",13,10,0 +msg_read_options du "Read options from config file",13,10,0 +msg_load_kernel du "Load kernel",13,10,0 +msg_load_ramdisk du "Load ramdisk",13,10,0 +msg_load_devicesdat du "Load DEVICES.DAT",13,10,0 +msg_alloc_devicesdat du "Allocate memory for DEVICES.DAT",13,10,0 +msg_locate_gop_handlers du "Locate GOP handlers",13,10,0 +msg_look_for_gop_handler du "Look for GOP handler",13,10,0 +msg_query_handler du "Query handler",13,10,0 +msg_query_vmode du "Query vmode",13,10,0 +msg_vmode_found du "Video mode found",13,10,0 +msg_look_for_rsdp du "Look for RSDP",13,10,0 +msg_rsdp_found du "RSDP found",13,10,0 +msg_acpi_tables_done du "ACPI tables done",13,10,0 +msg_ask_for_params du "Ask for params",13,10,0 +msg_set_graphic_mode du "Set graphic mode",13,10,0 +msg_success du "Success!",13,10,0 +msg_gop_buffer_size du "GOP buffer size",13,10,0 +msg_opt_resolution du "option resolution: ",0 +msg_error du "Error!",13,10,0 +msg_error_no_such_vmode du "No such vmode",13,10,0 +msg_error_out_of_handlers du "Out of handlers",13,10,0 +msg_error_open_file du "Error: can't open file ",0 +msg du 79 dup " ",13,10,0 + +efi_fs_info_id db EFI_FILE_SYSTEM_INFO_ID +efi_fs_info_size dq sizeof.EFI_FILE_SYSTEM_INFO +efi_fs_info EFI_FILE_SYSTEM_INFO + +memory_map dd ? +gop_buffer rd GOP_BUFFER_SIZE/4 +devicesdat_data dd 0xffffffff +devicesdat_size dd 0x1000 +status dd ? + +section '.reloc' fixups data discardable diff --git a/kernel/trunk/bootloader/uefi4kos/uefi64.inc b/kernel/trunk/bootloader/uefi4kos/uefi64.inc new file mode 100644 index 0000000000..b565bae0cc --- /dev/null +++ b/kernel/trunk/bootloader/uefi4kos/uefi64.inc @@ -0,0 +1,196 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; ;; +;; Copyright (C) KolibriOS team 2020. All rights reserved. ;; +;; Distributed under terms of the GNU General Public License ;; +;; Version 2, or (at your option) any later version. ;; +;; ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; ;; +;; Based on UEFI library for fasm by bzt, Public Domain. ;; +;; ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +DN fix dq ; native + +include "uefi.inc" + +EFIERR = 0x8000000000000000 + +struct EFI_SYSTEM_TABLE + Hdr EFI_TABLE_HEADER + FirmwareVendor dq ? + FirmwareRevision dd ? + dd ? + ConsoleInHandle dq ? + ConIn dq ? + ConsoleOutHandle dq ? + ConOut dq ? + StandardErrorHandle dq ? + StdErr dq ? + RuntimeServices dq ? + BootServices dq ? + NumberOfTableEntries dq ? + ConfigurationTable dq ? +ends + +struct EFI_LOADED_IMAGE_PROTOCOL + Revision dd ? + dd ? + ParentHandle dq ? + SystemTable dq ? + DeviceHandle dq ? + FilePath dq ? + Reserved dq ? + LoadOptionsSize dd ? + dd ? + ImageBase dq ? + ImageSize dq ? + ImageCodeType dd ? + ImageDataType dd ? + UnLoad dq ? +ends + +macro eficall interface,function,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10,arg11 +{ +numarg = 0 + +if ~ arg11 eq + numarg = numarg + 1 + if ~ arg11 eq rdi + mov rdi, arg11 + end if +end if + +if ~ arg10 eq + numarg = numarg + 1 + if ~ arg10 eq rsi + mov rsi, arg10 + end if +end if + +if ~ arg9 eq + numarg = numarg + 1 + if ~ arg9 eq r14 + mov r14, arg9 + end if +end if + +if ~ arg8 eq + numarg = numarg + 1 + if ~ arg8 eq r13 + mov r13, arg8 + end if +end if + +if ~ arg7 eq + numarg = numarg + 1 + if ~ arg7 eq r12 + mov r12, arg7 + end if +end if + +if ~ arg6 eq + numarg = numarg + 1 + if ~ arg6 eq r11 + mov r11, arg6 + end if +end if + +if ~ arg5 eq + numarg = numarg + 1 + if ~ arg5 eq r10 + mov r10, arg5 + end if +end if + +if ~ arg4 eq + numarg = numarg + 1 + if ~ arg4 eq r9 + mov r9, arg4 + end if +end if + +if ~ arg3 eq + numarg = numarg + 1 + if ~ arg3 eq r8 + mov r8, arg3 + end if +end if + +if ~ arg2 eq + numarg = numarg + 1 + if ~ arg2 eq rdx + mov rdx, arg2 + end if +end if + +if ~ arg1 eq + numarg = numarg + 1 + if ~ arg1 eq rcx + mov rcx, arg1 + end if +end if + + xor eax, eax + mov al, numarg + +if ~ interface eq rbx + mov rbx, interface +end if + + mov rbx, [rbx + function] + call uefifunc +} + +section '.text' code executable readable + +uefifunc: + ;save stack pointer + mov [uefi_rsptmp], rsp + ;set up new aligned stack + and esp, 0xFFFFFFF0 + ;alignment check on arguments + bt eax, 0 + jnc @f + push rax + ;arguments +@@: + cmp al, 11 + jb @f + push rdi +@@: + cmp al, 10 + jb @f + push rsi +@@: + cmp al, 9 + jb @f + push r14 +@@: + cmp al, 8 + jb @f + push r13 +@@: + cmp al, 7 + jb @f + push r12 +@@: + cmp al, 6 + jb @f + push r11 +@@: + cmp al, 5 + jb @f + push r10 +@@: + ;space for + ;r9 + ;r8 + ;rdx + ;rcx + sub rsp, 4*8 + ;call function + call rbx + ;restore old stack + mov rsp, [uefi_rsptmp] + ret diff --git a/kernel/trunk/bootloader/uefi4kos/uefi4kos.asm b/kernel/trunk/bootloader/uefi4kos/uefi64kos.asm similarity index 58% rename from kernel/trunk/bootloader/uefi4kos/uefi4kos.asm rename to kernel/trunk/bootloader/uefi4kos/uefi64kos.asm index fb79c5f3dc..486bf337b9 100644 --- a/kernel/trunk/bootloader/uefi4kos/uefi4kos.asm +++ b/kernel/trunk/bootloader/uefi4kos/uefi64kos.asm @@ -1,4 +1,12 @@ -format pe64 dll efi +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; ;; +;; Copyright (C) KolibriOS team 2020. All rights reserved. ;; +;; Distributed under terms of the GNU General Public License ;; +;; Version 2, or (at your option) any later version. ;; +;; ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +format pe64 efi entry main section '.text' code executable readable @@ -6,7 +14,9 @@ section '.text' code executable readable include '../../struct.inc' include '../../macros.inc' include '../../const.inc' -include 'uefi.inc' + +purge DQ +include 'uefi64.inc' MEMORY_MAP_SIZE = 0x4000 GOP_BUFFER_SIZE = 0x100 @@ -44,8 +54,14 @@ end virtual xor eax, eax cmp [.fatal], 1 jnz .done - eficall ConOut, OutputString, ConOut, msg_error_open_file - eficall ConOut, OutputString, ConOut, [.name] + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_error_open_file + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + [.name] jmp $ @@: @@ -101,7 +117,6 @@ skip_newline: skip_line: call skip_until_newline call skip_newline -.done: ret dec2bin: @@ -252,13 +267,17 @@ end virtual ret 1*8 read_options_from_config: - eficall BootServices, HandleProtocol, qword[efi_handler], lipuuid, \ + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.BootServices] + eficall rbx, EFI_BOOT_SERVICES.HandleProtocol, [efi_handle], lipuuid, \ lip_interface cmp eax, EFI_SUCCESS jnz .error mov rax, [lip_interface] - eficall BootServices, HandleProtocol, \ + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.BootServices] + eficall rbx, EFI_BOOT_SERVICES.HandleProtocol, \ [rax+EFI_LOADED_IMAGE_PROTOCOL.DeviceHandle], sfspguid, \ sfsp_interface cmp rax, EFI_SUCCESS @@ -285,16 +304,41 @@ read_options_from_config: .error: ret +print_vmode: + push rax rbx rcx rdx rsi rdi + mov rbx, rcx + call clearbuf + mov eax, [rbx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.HorizontalResolution] + mov rdi, msg + call num2dec + mov eax, [rbx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.VerticalResolution] + mov rdi, msg+8*2 + call num2dec + + mov eax, [rbx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelFormat] + mov rdi, msg+16*2 + call num2dec + + mov eax, [rbx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelsPerScanLine] + mov rdi, msg+24*2 + call num2dec + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg + pop rdi rsi rdx rcx rbx rax + ret + find_vmode_index_by_resolution: mov [cfg_opt_used_resolution], 1 mov [cfg_opt_value_vmode], 0 - .next_mode: +.next_mode: movzx eax, [cfg_opt_value_vmode] eficall [gop_interface], EFI_GRAPHICS_OUTPUT_PROTOCOL.QueryMode, \ [gop_interface], rax, gop_info_size, gop_info cmp rax, EFI_SUCCESS jnz .error mov rcx, [gop_info] + call print_vmode ; PixelBlueGreenRedReserved8BitPerColor cmp [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelFormat], 1 jnz .skip_mode @@ -306,22 +350,38 @@ find_vmode_index_by_resolution: cmp ax, [rdx+BOOT_LO.y_res] jnz .skip_mode jmp .done - .skip_mode: +.skip_mode: inc [cfg_opt_value_vmode] movzx eax, [cfg_opt_value_vmode] mov rcx, [gop_interface] mov rdx, [rcx+EFI_GRAPHICS_OUTPUT_PROTOCOL.Mode] - cmp eax, [rdx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.MaxMode] + cmp eax, 8;[rdx+EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.MaxMode] jnz .next_mode mov [cfg_opt_used_resolution], 0 mov [cfg_opt_value_ask_params], 1 + + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_error_no_such_vmode + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg_error + jmp $ .error: .done: ret ask_for_params: + ret + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.BootServices] + eficall rbx, EFI_BOOT_SERVICES.HandleProtocol, [rax], gopuuid, \ + msg_ask_for_params + jmp $ + xor ebx, ebx - .next_mode: +.next_mode: call clearbuf mov eax, ebx lea rdi, [msg] @@ -345,8 +405,10 @@ ask_for_params: ; mov eax, [rcx+EFI_GRAPHICS_OUTPUT_MODE_INFORMATION.PixelsPerScanLine] ; lea rdi, [msg+14*2] ; call num2dec - .skip: - eficall ConOut, OutputString, ConOut, msg +.skip: + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg cmp rax, EFI_SUCCESS jnz .error @@ -358,13 +420,17 @@ ask_for_params: jnz .next_mode - eficall ConIn, Reset, ConIn, 1 + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConIn] + eficall rbx, SIMPLE_INPUT_INTERFACE.Reset, rbx, 1 cmp rax, EFI_SUCCESS jnz .error xor ecx, ecx @@: push rcx - eficall ConIn, ReadKeyStroke, ConIn, msg + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConIn] + eficall rbx, SIMPLE_INPUT_INTERFACE.ReadKeyStroke, rbx, msg pop rcx mov rdx, EFI_DEVICE_ERROR cmp rax, rdx @@ -381,30 +447,39 @@ ask_for_params: sub eax, '0' add ecx, eax jmp @b - .key_done: +.key_done: mov [cfg_opt_value_vmode], cl .error: .done: ret main: - sub rsp, 0x38 + mov [efi_handle], rcx + mov [efi_table], rdx - ; initialize UEFI library - InitializeLib - jc .error - -; eficall ConOut, Reset, ConOut, 1 -; cmp rax, EFI_SUCCESS -; jnz .error - - eficall ConOut, ClearScreen, ConOut - cmp rax, EFI_SUCCESS - jnz .error + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset, rbx, 1 + test eax, eax + jz @f + jmp $ ; what can I do here? +@@: + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_u4k_loaded + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_read_options call read_options_from_config ; read kernel file + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_load_kernel push 1 push -1 push KERNEL_BASE @@ -413,6 +488,10 @@ main: call load_file ; read ramdisk image + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_load_ramdisk push 1 push -1 push RAMDISK_BASE @@ -421,12 +500,24 @@ main: call load_file ; alloc buffer for devices.dat - eficall BootServices, AllocatePages, EFI_ALLOCATE_MAX_ADDRESS, \ - EFI_RESERVED_MEMORY_TYPE, 1, devicesdat_data + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_alloc_devicesdat + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.BootServices] + eficall rbx, EFI_BOOT_SERVICES.AllocatePages, \ + EFI_ALLOCATE_MAX_ADDRESS, EFI_RESERVED_MEMORY_TYPE, 1, \ + devicesdat_data cmp eax, EFI_SUCCESS jnz .error ; read devices.dat + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_load_devicesdat + push 0 ; not fatal push [devicesdat_size] push [devicesdat_data] @@ -435,24 +526,112 @@ main: call load_file mov [devicesdat_size], rax - eficall BootServices, LocateHandle, 2, gopuuid, 0, gop_buffer_size, \ - gop_buffer - cmp eax, EFI_SUCCESS - jnz .error - mov rsi, gop_buffer - lodsq - mov [gop_handle], rax - eficall BootServices, HandleProtocol, qword[gop_handle], gopuuid, \ - gop_interface - cmp eax, EFI_SUCCESS - jnz .error + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_locate_gop_handlers - mov rbx, [efi_ptr] + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.BootServices] + eficall rbx, EFI_BOOT_SERVICES.LocateHandle, \ + EFI_LOCATE_SEARCH_TYPE.ByProtocol, gopuuid, 0, \ + gop_buffer_size, gop_buffer + mov [status], rax + + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_gop_buffer_size + call clearbuf + mov rax, [gop_buffer_size] + mov rdi, msg + call num2hex + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg + + mov rax, [status] + test eax, eax + jz @f + call clearbuf + mov rdi, msg + call num2hex + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg_error + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg + jmp $ +@@: + + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_look_for_gop_handler + + mov rbx, gop_buffer +.next_gop_handle: + mov rax, rbx + sub rax, gop_buffer + cmp rax, [gop_buffer_size] + jb @f + push rbx + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_error_out_of_handlers + pop rbx + push rbx + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg_error + pop rbx + jmp $ +@@: + push rbx + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_query_handler + pop rbx + + mov rax, rbx + push rbx + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.BootServices] + eficall rbx, EFI_BOOT_SERVICES.HandleProtocol, [rax], gopuuid, \ + gop_interface + pop rbx +;mov rax, 0x8000_0000_0000_0003 + test eax, eax + jz @f + call clearbuf + mov rdi, msg + call num2hex + push rbx + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg + pop rbx + + add rbx, 8 + jmp .next_gop_handle +@@: + + push rbx + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_look_for_rsdp + pop rbx + + mov rbx, [efi_table] mov rdi, [rbx+EFI_SYSTEM_TABLE.ConfigurationTable] mov rcx, [rbx+EFI_SYSTEM_TABLE.NumberOfTableEntries] mov rax, 0x11d3e4f18868e871 mov rdx, 0x81883cc7800022bc - .next_table: +.next_table: dec ecx js .all_tables_done cmp [rdi+0], rax @@ -463,13 +642,34 @@ main: mov rdx, BOOT_LO.acpi_rsdp mov [rdx], eax jmp .all_tables_done - .not_acpi20: +.not_acpi20: add rdi, 24 jmp .next_table - .all_tables_done: +.all_tables_done: + + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_acpi_tables_done cmp [cfg_opt_used_resolution], 0 jz .not_used_resolution + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_opt_resolution + call clearbuf + xor edx, edx + movzx eax, [rdx+BOOT_LO.x_res] + mov rdi, msg + call num2dec + xor edx, edx + movzx eax, [rdx+BOOT_LO.y_res] + mov rdi, msg+8*2 + call num2dec + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg call find_vmode_index_by_resolution .not_used_resolution: cmp [cfg_opt_used_debug_print], 0 @@ -487,8 +687,19 @@ main: movzx ecx, [cfg_opt_value_vmode] eficall [gop_interface], EFI_GRAPHICS_OUTPUT_PROTOCOL.SetMode, \ [gop_interface], rcx - cmp eax, EFI_SUCCESS - jnz .error + test eax, eax + jz @f + call clearbuf + mov rdi, msg + call num2hex + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, msg_error + jmp $ +@@: mov rcx, [gop_interface] mov rdx, [rcx+EFI_GRAPHICS_OUTPUT_PROTOCOL.Mode] @@ -518,13 +729,17 @@ main: mov dword[rdx+BOOT_LO.pci_data+4], 0xe3 - eficall BootServices, AllocatePages, EFI_ALLOCATE_ANY_PAGES, \ + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.BootServices] + eficall rbx, EFI_BOOT_SERVICES.AllocatePages, EFI_ALLOCATE_ANY_PAGES, \ EFI_RESERVED_MEMORY_TYPE, MEMORY_MAP_SIZE/0x1000, memory_map cmp eax, EFI_SUCCESS jnz .error - eficall BootServices, GetMemoryMap, memory_map_size, [memory_map], \ - memory_map_key, descriptor_size, descriptor_ver + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.BootServices] + eficall rbx, EFI_BOOT_SERVICES.GetMemoryMap, memory_map_size, \ + [memory_map], memory_map_key, descriptor_size, descriptor_ver cmp eax, EFI_SUCCESS jnz .error @@ -537,7 +752,7 @@ main: div ecx mov ecx, eax mov rsi, [memory_map] - .next_descr: +.next_descr: call add_uefi_memmap add rsi, [descriptor_size] add rdi, sizeof.e820entry @@ -553,7 +768,10 @@ main: ; eficall BootServices, AllocatePages, EFI_RESERVED_MEMORY_TYPE, \ ; 2880*512/0x1000, EFI_ALLOCATE_ADDRESS - eficall BootServices, ExitBootServices, [efi_handler], [memory_map_key] + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.BootServices] + eficall rbx, EFI_BOOT_SERVICES.ExitBootServices, [efi_handle], \ + [memory_map_key] cmp eax, EFI_SUCCESS jnz .error @@ -612,11 +830,10 @@ main: push CODE_32_SELECTOR lea rax, [.next] push rax -; push .next retf use32 align 16 - .next: +.next: mov eax, cr0 and eax, not CR0_PG mov cr0, eax @@ -634,12 +851,12 @@ align 16 retn use64 - .error: - eficall ConOut, OutputString, ConOut, msg_error - jmp .quit - .quit: - mov rcx, -1 - loop $ +.error: + mov rbx, [efi_table] + mov rbx, [rbx+EFI_SYSTEM_TABLE.ConOut] + eficall rbx, EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.OutputString, rbx, \ + msg_error + jmp $ ; linux/arch/x86/platform/efi/efi.c @@ -647,11 +864,10 @@ use64 add_uefi_memmap: push rax rbx rcx rdx rsi rdi - mov r10d, [rsi+0] - mov r11, [rsi+8] -; mov r12, [rsi+16] - mov r13, [rsi+24] - mov r14, [rsi+32] + mov r10d, [rsi+EFI_MEMORY_DESCRIPTOR.Type] + mov r11, [rsi+EFI_MEMORY_DESCRIPTOR.PhysicalStart] + mov r13, [rsi+EFI_MEMORY_DESCRIPTOR.NumberOfPages] + mov r14, [rsi+EFI_MEMORY_DESCRIPTOR.Attribute] mov [rdi+e820entry.addr], r11 mov rax, r13 @@ -679,7 +895,7 @@ add_uefi_memmap: jz .case4 jmp .default - .case0: +.case0: test r14, EFI_MEMORY_WB jz @f mov eax, E820_RAM @@ -687,23 +903,23 @@ add_uefi_memmap: @@: mov eax, E820_RESERVED jmp .done - .case1: +.case1: mov eax, E820_ACPI jmp .done - .case2: +.case2: mov eax, E820_NVS jmp .done - .case3: +.case3: mov eax, E820_UNUSABLE jmp .done - .case4: +.case4: mov eax, E820_PMEM jmp .done - .default: +.default: mov eax, E820_RESERVED jmp .done - .done: +.done: mov [rdi+e820entry.type], eax mov rax, BOOT_LO.memmap_block_cnt @@ -718,7 +934,7 @@ num2dec: xor ecx, ecx mov ebx, 10 - .next_digit: +.next_digit: xor edx, edx div ebx push rdx @@ -726,7 +942,7 @@ num2dec: test eax, eax jnz .next_digit - .next_char: +.next_char: pop rax add eax, '0' stosw @@ -741,7 +957,7 @@ num2hex: xchg rdx, rax mov ecx, 16 - .next_tetra: +.next_tetra: rol rdx, 4 movzx eax, dl and eax, 0x0f @@ -764,6 +980,9 @@ clearbuf: ret section '.data' data readable writeable +efi_handle dq 0 +efi_table dq 0 +uefi_rsptmp dq 0 GDTR: dw 4*8-1 @@ -794,10 +1013,10 @@ sfsp_interface dq 0 esp_root dq ? file_handle dq ? -file_name du '\EFI\BOOT\KOLIBRI.INI',0 -kernel_name du '\EFI\BOOT\KOLIBRI.KRN',0 -ramdisk_name du '\EFI\BOOT\KOLIBRI.IMG',0 -devicesdat_name du '\EFI\BOOT\DEVICES.DAT',0 +file_name du '\EFI\KOLIBRIOS\KOLIBRI.INI',0 +kernel_name du '\EFI\KOLIBRIOS\KOLIBRI.KRN',0 +ramdisk_name du '\EFI\KOLIBRIOS\KOLIBRI.IMG',0 +devicesdat_name du '\EFI\KOLIBRIOS\DEVICES.DAT',0 file_buffer_size dq FILE_BUFFER_SIZE-1 ; leave the last byte for \0 config_options dq cfg_opt_name_resolution, cfg_opt_func_resolution, \ @@ -816,67 +1035,86 @@ config_options dq cfg_opt_name_resolution, cfg_opt_func_resolution, \ cfg_opt_cmnt_syspath, \ 0 -cfg_opt_name_resolution db 'resolution',0 -cfg_opt_name_acpi db 'acpi',0 -cfg_opt_name_debug_print db 'debug_print',0 -cfg_opt_name_launcher_start db 'launcher_start',0 -cfg_opt_name_mtrr db 'mtrr',0 -cfg_opt_name_ask_params db 'ask_params',0 -cfg_opt_name_imgfrom db 'imgfrom',0 -cfg_opt_name_syspath db 'syspath',0 +cfg_opt_name_resolution db "resolution",0 +cfg_opt_name_acpi db "acpi",0 +cfg_opt_name_debug_print db "debug_print",0 +cfg_opt_name_launcher_start db "launcher_start",0 +cfg_opt_name_mtrr db "mtrr",0 +cfg_opt_name_ask_params db "ask_params",0 +cfg_opt_name_imgfrom db "imgfrom",0 +cfg_opt_name_syspath db "syspath",0 -cfg_opt_cmnt_resolution db "# Graphic mode",0 -cfg_opt_cmnt_acpi db "# ACPI settings",0xa, \ - "# 0: don't use",0xa, \ - "# 1: parse ACPI tables",0xa, \ - "# 2: + call _PIC method",0xa, \ - "# 3: + get APIC interrupts",0xa,0 -cfg_opt_cmnt_debug_print db "# Duplicate debug output to the screen",0 +cfg_opt_cmnt_resolution db "# Graphic mode",0 +cfg_opt_cmnt_acpi db "# ACPI settings",0xa, \ + "# 0: don't use",0xa, \ + "# 1: parse ACPI tables",0xa, \ + "# 2: + call _PIC method",0xa, \ + "# 3: + get APIC interrupts",0xa,0 +cfg_opt_cmnt_debug_print db "# Duplicate debug output to the screen",0 cfg_opt_cmnt_launcher_start db "# Start LAUNCHER app after kernel is loaded",0 -cfg_opt_cmnt_mtrr db "# Configure MTRR's",0 -cfg_opt_cmnt_ask_params db "# Interrupt booting to ask the user for boot params",0 -cfg_opt_cmnt_imgfrom db "# Where to load ramdisk image from",0 -cfg_opt_cmnt_syspath db "# Path to /sys directory",0 +cfg_opt_cmnt_mtrr db "# Configure MTRR's",0 +cfg_opt_cmnt_ask_params db "# Interrupt booting to ask the user for boot", \ + " params",0 +cfg_opt_cmnt_imgfrom db "# Where to load ramdisk image from",0 +cfg_opt_cmnt_syspath db "# Path to /sys directory",0 -cfg_opt_used_resolution db 0 -cfg_opt_used_acpi db 0 -cfg_opt_used_debug_print db 0 +cfg_opt_used_resolution db 0 +cfg_opt_used_acpi db 0 +cfg_opt_used_debug_print db 0 cfg_opt_used_launcher_start db 0 -cfg_opt_used_mtrr db 0 -cfg_opt_used_ask_params db 0 -cfg_opt_used_imgfrom db 0 -cfg_opt_used_syspath db 0 +cfg_opt_used_mtrr db 0 +cfg_opt_used_ask_params db 0 +cfg_opt_used_imgfrom db 0 +cfg_opt_used_syspath db 0 -cfg_opt_value_vmode db 0 -cfg_opt_value_acpi db 0 -cfg_opt_value_debug_print db 0 +cfg_opt_value_vmode db 0 +cfg_opt_value_acpi db 0 +cfg_opt_value_debug_print db 0 cfg_opt_value_launcher_start db 1 -cfg_opt_value_mtrr db 0 -cfg_opt_value_ask_params db 1 -cfg_opt_value_imgfrom db RD_LOAD_FROM_MEMORY -cfg_opt_value_syspath db '/RD/1',0 - rb 20 +cfg_opt_value_mtrr db 0 +cfg_opt_value_ask_params db 0 +cfg_opt_value_imgfrom db RD_LOAD_FROM_MEMORY +cfg_opt_value_syspath db "/RD/1",0 + rb 20 memory_map_key dq 0 descriptor_size dq 0 descriptor_ver dq 0 memory_map_size dq MEMORY_MAP_SIZE -msg_success du 'Success!',13,10,0 -msg_error du 'Error!',13,10,0 -msg du 79 dup ' ',13,10,0 -msg_error_open_file du "Error: can't open file ",0 +msg_u4k_loaded du "uefi64kos loaded",13,10,0 +msg_read_options du "Read options from config file",13,10,0 +msg_load_kernel du "Load kernel",13,10,0 +msg_load_ramdisk du "Load ramdisk",13,10,0 +msg_load_devicesdat du "Load DEVICES.DAT",13,10,0 +msg_alloc_devicesdat du "Allocate memory for DEVICES.DAT",13,10,0 +msg_locate_gop_handlers du "Locate GOP handlers",13,10,0 +msg_look_for_gop_handler du "Look for GOP handler",13,10,0 +msg_query_handler du "Query handler",13,10,0 +msg_query_vmode du "Query vmode",13,10,0 +msg_vmode_found du "Video mode found",13,10,0 +msg_look_for_rsdp du "Look for RSDP",13,10,0 +msg_rsdp_found du "RSDP found",13,10,0 +msg_acpi_tables_done du "ACPI tables done",13,10,0 +msg_ask_for_params du "Ask for params",13,10,0 +msg_set_graphic_mode du "Set graphic mode",13,10,0 +msg_success du "Success!",13,10,0 +msg_gop_buffer_size du "GOP buffer size",13,10,0 +msg_opt_resolution du "option resolution: ",0 +msg_error du "Error!",13,10,0 +msg_error_no_such_vmode du "No such vmode",13,10,0 +msg_error_out_of_handlers du "Out of handlers",13,10,0 +msg_error_open_file du "Error: can't open file ",0 +msg du 79 dup " ",13,10,0 efi_fs_info_id db EFI_FILE_SYSTEM_INFO_ID efi_fs_info_size dq sizeof.EFI_FILE_SYSTEM_INFO efi_fs_info EFI_FILE_SYSTEM_INFO -;section '.bss' data readable writeable - memory_map dq ? -gop_buffer rb GOP_BUFFER_SIZE -lip_buffer rb LIP_BUFFER_SIZE +gop_buffer rq GOP_BUFFER_SIZE/8 devicesdat_data dq 0xffffffff devicesdat_size dq 0x1000 +status dq ? section '.reloc' fixups data discardable