kaem-minimal.S: make sure to close files and free pools when exiting.

This commit is contained in:
Andrius Štikonas 2022-07-30 10:08:23 +01:00
parent 0895da7db6
commit 9af7fdf9bb
7 changed files with 17 additions and 15 deletions

View File

@ -33,11 +33,11 @@ efi_status_t efi_main(efi_handle_t image_handle, struct efi_system_table *system
*options = 0;
out = ++options;
/* Get root device */
/* Get root file system */
efi_handle_t root_device = image->device;
system->boot->open_protocol(root_device, &guid2, (void **) &rootfs, image_handle, 0,
EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
/* Get root fs */
/* Get root directory */
rootfs->open_volume(rootfs, &rootdir);
/* Open file for writing */

View File

@ -7,7 +7,7 @@
#include "efi/efi.h"
#define max_string 512
#define max_string 2048
#define HARDWARE_DEVICE_PATH 1
#define END_HARDWARE_DEVICE_PATH 0x7F
@ -44,11 +44,11 @@ efi_status_t efi_main(efi_handle_t image_handle, struct efi_system_table *system
script_file = ++options;
}
/* Get root device */
/* Get root file system */
efi_handle_t root_device = image->device;
system->boot->open_protocol(root_device, &guid2, (void **) &rootfs, image_handle, 0,
EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
/* Get root fs */
/* Get root directory */
rootfs->open_volume(rootfs, &rootdir);
/* Open file for reading */
@ -66,7 +66,7 @@ efi_status_t efi_main(efi_handle_t image_handle, struct efi_system_table *system
unsigned int i;
uint8_t c;
efi_uint_t size = 1;
efi_uint_t file_size = 1;
efi_uint_t file_size;
efi_uint_t return_code;
void *executable;
efi_handle_t child_ih;
@ -130,6 +130,7 @@ efi_status_t efi_main(efi_handle_t image_handle, struct efi_system_table *system
system->boot->allocate_pool(EFI_LOADER_CODE, file_size, (void **) &executable);
fcmd->read(fcmd, &file_size, executable);
fcmd->close(fcmd);
struct efi_device_path_protocol *device_path;
system->boot->allocate_pool(EFI_LOADER_DATA, 4 + sizeof(struct efi_device_path_protocol), (void **) &device_path);
@ -161,6 +162,7 @@ efi_status_t efi_main(efi_handle_t image_handle, struct efi_system_table *system
if(return_code != 0) {
system->boot->free_pool(command);
system->out->output_string(system->out, L"Subprocess error.\r\n");
rootdir->close(fin);
return return_code;
}
} while(true);

View File

@ -9,7 +9,7 @@ lflags = -subsystem:efi_application -nodefaultlib -dll
build = build
all: $(build)/hex0.efi
all: $(build)/hex0.efi $(build)/kaem-optional.efi
$(build)/%.o : %.S
mkdir -p $(build)

View File

@ -104,9 +104,9 @@ DEFINE TEST_ESI_ESI 85F6
SUBI8_RSP !32 # allocate shadow stack space for UEFI function
CALL_R14 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
LOAD64_into_RAX_from_Address_RSP_Immediate8 !64 # get_image
COPY_RCX_to_RAX # save image
# Command line args
COPY_RCX_to_RAX # save image
LOAD64_into_RBX_from_Address_RAX_Immediate8 !56 # options = image->load_options
:loop_options1 # Skip application name
@ -128,7 +128,7 @@ DEFINE TEST_ESI_ESI 85F6
ADDI8_RBX !2 # ++options
COPY_RBX_to_R13 # save output file
# Get root device
# Get root file system
PUSH_RAX # allocate stack for rootfs
COPY_RSP_to_R8 # arg3 = &rootfs
LOADI64_rel_RDX %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)

View File

@ -44,9 +44,9 @@ _start:
sub rsp, 32 # allocate shadow stack space for UEFI function
call r14 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
mov rax, [rsp+64] # get image
mov rcx, rax # save image
# Command line args
mov rcx, rax # save image
mov rbx, [rax+56] # options = image->load_options
loop_options1: # Skip application name
@ -68,7 +68,7 @@ loop_options2: # Skip argv[1]
add rbx, 2 # ++options
mov r13, rbx # save output file
# Get root device
# Get root file system
push rax # allocate stack for rootfs
mov r8, rsp # arg3 = &rootfs
mov rdx, [rip+SIMPLE_FS_PROTOCOL+8] # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)

View File

@ -42,9 +42,9 @@
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
41FFD6 ; CALL_R14 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B4424 40 ; LOAD64_into_RAX_from_Address_RSP_Immediate8 !64 # get_image
4889C1 ; COPY_RCX_to_RAX # save image
# Command line args
4889C1 ; COPY_RCX_to_RAX # save image
488B58 38 ; LOAD64_into_RBX_from_Address_RAX_Immediate8 !56 # options = image->load_options
:loop_options1 # Skip application name
@ -66,7 +66,7 @@
4883C3 02 ; ADDI8_RBX !2 # ++options
4989DD ; COPY_RBX_to_R13 # save output file
# Get root device
# Get root file system
50 ; PUSH_RAX # allocate stack for rootfs
4989E0 ; COPY_RSP_to_R8 # arg3 = &rootfs
488B15 %SIMPLE_FS_PROTOCOL_8 ; LOADI64_rel_RDX %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)

View File

@ -172,9 +172,9 @@ A3 01 00 00 ; SizeOfRawData
4883EC 20 ; SUBI8_RSP !32 # allocate shadow stack space for UEFI function
41FFD6 ; CALL_R14 # system->boot->open_protocol(image_handle, &guid, &image, image_handle, 0, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL)
488B4424 40 ; LOAD64_into_RAX_from_Address_RSP_Immediate8 !64 # get_image
4889C1 ; COPY_RCX_to_RAX # save image
# Command line args
4889C1 ; COPY_RCX_to_RAX # save image
488B58 38 ; LOAD64_into_RBX_from_Address_RAX_Immediate8 !56 # options = image->load_options
# :loop_options1 [_start+0x42] # Skip application name
@ -196,7 +196,7 @@ A3 01 00 00 ; SizeOfRawData
4883C3 02 ; ADDI8_RBX !2 # ++options
4989DD ; COPY_RBX_to_R13 # save output file
# Get root device [_start+0x67]
# Get root file system [_start+0x67]
50 ; PUSH_RAX # allocate stack for rootfs
4989E0 ; COPY_R8_to_RSP # arg3 = &rootfs
488B15 29010000 ; LOADI64_rel_RDX %SIMPLE_FS_PROTOCOL_8 # EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID (last 64 bits)