Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@oweisse
Copy link
Owner

@oweisse oweisse commented Jul 1, 2019

Currently only supports extremely simple apps like HelloWorld.efi

  • kexec.h - Added KEXEC_RUN_PE flag to be passed to kexec_load. This is the
  • kexec.c
    - Implemented kimage_load_pe, kimage_run_pe
    - Implemented EFI hooks to generate fake EFI system table and
    fake EFI boot services.
    - Implemented naive EFI AllocatePool, FreePool
    - Implemented EFI conout to enable OutputString

Currently only supports extremely simple apps like HelloWorld.efi

* kexec.h - Added KEXEC_RUN_PE flag to be passed to kexec_load. This is the
* kexec.c
        - Implemented kimage_load_pe, kimage_run_pe
        - Implemented EFI hooks to generate fake EFI system table and
        fake EFI boot services.
        - Implemented naive EFI AllocatePool, FreePool
        - Implemented EFI conout to enable OutputString
@oweisse oweisse requested review from hugelgupf and jonmccune July 1, 2019 20:33
Ofir Weisse added 22 commits July 3, 2019 08:57
EFI Boot Services:
    efi_hook_HandleProtocol:
        - prints out the protocol GUID and name
        - implemented gEfiLoadedImageProtocolGuid
        - implemented gEfiDevicePathProtocolGuid

EFI Protocols:
    gEfiLoadedImageProtocolGuid:
        - Respond with a fixed EFI_LOADED_IMAGE_PROTOCOL containing a fixd boot-device and fixed LoadOptions

    gEfiLoadedImageProtocolGuid:
        - Acceplts only BOOT_DEVICE_HANDLE, replies with a fixed windows_boot_device_path
windows_loaded_image needs to be patched during kimage_load_pe
* efi_setup_11_mapping: creates 1:1 mapping
* efi_hook_AllocatePool: now calls efi_setup_11_mapping and returns
                         "physical" address, thanks to the 1:1 mapping
* efi_hook_FreePool: Removed call to kfree, which is incompatible with
                     our new 1:1 mapping
* efi_register_mem_allocation: register new memory allocation
* efi_unregister_allocation: reclaim memory and make it EfiConventionalMemory
* efi_hook_GetMemoryMap: get current memory mappings

The current bookkepping is naive and incorrect.
TODO:
1. When allocating memory, we should seearch if blocks of
   EfiConventionalMemory are available for allocation.
2. When registerring memory, we might need to split a block of
   EfiConventionalMemory
3. When unregistering memory, we need to potentially coalsce blocks of
   EfiConventionalMemory.
This os a preparation for smarter management of mem maps.
Windows loader sets up interrupts 0x3, 0x2C, 0x2D to empty interrupt
handlers. A good fix in the future would be to make the page temporarily.
Also refactored CHAR16 to char conversion function
…them.

Implemented the following functions:

* efi_hook_LocateHandle: implemented only for gEfiBlockIoProtocolGuid. Returning 4
                         devices: raw-hard-drive and partitions 1,2,3.

* efi_handle_protocol_DevicePath: implemented for the 4 devices mentioned above

* efi_handle_protocol_BlockIO: Returns the proper EFI_BLOCK_IO_PROTOCOL, depending
                               on the handle provided. Implemented Read/write
                               callbacks whcih print log msg to screen and return
                               an erro code.

* efi_hook_CloseProtocol: empty implementation (log and return success)
see efi_block_io_read_blocks
This ensures Windows loader can access these addresses
Ofir Weisse added 2 commits July 23, 2019 10:08
This is crucial to support Windows loader replacing CR3 with its own
page table
This is required to allow  Winload.efi to access the system table.

* Allocate memory in a 1:1 mapped area
* Print the memory map after every memory allocation.
Ofir Weisse added 29 commits July 24, 2019 10:49
This is important so that the system table and other structures can be
accessible when Windows loader (either bootmg or Winload.efi) try to
access them via a Windows page table.
laoder:

* fake_systab
* fake_systab->con_out
* efi_config_table
* runtime_services
… memory.

Also making sure the memory map is sorted
Passing the original EFI runtime services via the system table.

* Areas marked "reserved" in e820 map are marked EfiRuntimeServicesCode, as they contain runtime code.
* Modified InternalSerialPuts to be a MACRO
* Refactored memory mapping functions
This is required when winload.efi iterates the result of
GetMemoryMap, since it only considers memory descriptors with the
attribute EFI_MEMORY_RUNTIME. Failing to do so will later yield a
failure of the runtime call to set_virtual_address_map. See fix in
efi_mem_allocation_build_chunk.

Also fixed MapKey value to be a monotonically advancing value.
…ile.

* include/linux/efi.h: declaration of the list and relevant functions
* kernel/efi_protocol_guids.c: contains the list and search/comparison
                               functions
* kernel/Makefile: added kernel/efi_protocol_guids.c
When efi_hook_AllocatePages is called with EFI_ALLOCATE_TYPE of
AllocateAddress we take a leap of faith that the requested physical address is
available. We should make sure the address is actually available and
return error if not.
Right now the available devices are hard coded, based on our
very-specific drive.
The hack is to remove the NX bit from P4D in the page table. This allows
executing the windows loader code in a memory space which is seemingly
user-space. Since IA32_EFER.NXE == 1, having P4D with the NX bit will
result in a page fault, when we start fetching instructions for Windows
loader.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants