From e0c0cd55c6db38e5163971bb386c59dcf77760a4 Mon Sep 17 00:00:00 2001 From: Martin Whitaker Date: Sat, 23 Apr 2022 13:25:33 +0100 Subject: [PATCH] Tidy up code for performing reset via the EFI runtime sevices. Make this entirely local to hwctrl.c. --- app/main.c | 9 ++------- system/hwctrl.c | 47 +++++++++++++++++++++++++---------------------- system/hwctrl.h | 5 +++++ 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/app/main.c b/app/main.c index de8f57a..08c07c7 100644 --- a/app/main.c +++ b/app/main.c @@ -91,8 +91,6 @@ static int test_stage = 0; // Public Variables //------------------------------------------------------------------------------ -efi_info_t saved_efi_info; - // These are exposed in test.h. uint8_t chunk_index[MAX_CPUS]; @@ -199,6 +197,8 @@ static void global_init(void) floppy_off(); + hwctrl_init(); + cpuid_init(); screen_init(); @@ -277,11 +277,6 @@ static void global_init(void) reboot(); } - boot_params_t *boot_params = (boot_params_t *)boot_params_addr; - if(boot_params->efi_info.loader_signature){ - saved_efi_info = boot_params->efi_info; - } - start_barrier = smp_alloc_barrier(1); run_barrier = smp_alloc_barrier(1); diff --git a/system/hwctrl.c b/system/hwctrl.c index 0d165a5..945df87 100644 --- a/system/hwctrl.c +++ b/system/hwctrl.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 -// Copyright (C) 2020 Martin Whitaker. +// Copyright (C) 2020-2022 Martin Whitaker. // // Derived from an extract of memtest86+ lib.c: // @@ -14,39 +14,42 @@ #include "bootparams.h" #include "efi.h" -#include "hwctrl.h" #include "io.h" #include "unistd.h" +#include "hwctrl.h" + +//------------------------------------------------------------------------------ +// Private Variables +//------------------------------------------------------------------------------ + +static efi_runtime_services_t *efi_rs_table = NULL; + //------------------------------------------------------------------------------ // Public Functions //------------------------------------------------------------------------------ -extern efi_info_t saved_efi_info; - -static void efi_reset(uint8_t reset_type) +void hwctrl_init(void) { - static efi_runtime_services_t *rs_table = NULL; - + boot_params_t *boot_params = (boot_params_t *)boot_params_addr; #ifdef __x86_64__ - if (saved_efi_info.loader_signature == EFI64_LOADER_SIGNATURE) { - uintptr_t system_table_addr = ((uintptr_t)saved_efi_info.sys_tab_hi << 32) | saved_efi_info.sys_tab; + if (boot_params->efi_info.loader_signature == EFI64_LOADER_SIGNATURE) { + uintptr_t system_table_addr = (uintptr_t)boot_params->efi_info.sys_tab_hi << 32 | boot_params->efi_info.sys_tab; if (system_table_addr != 0) { efi64_system_table_t *sys_table = (efi64_system_table_t *)system_table_addr; - rs_table = (efi_runtime_services_t *)sys_table->runtime_services; - rs_table->reset_system(reset_type, 0, 0); + efi_rs_table = (efi_runtime_services_t *)sys_table->runtime_services; + } + } +#else + if (boot_params->efi_info.loader_signature == EFI32_LOADER_SIGNATURE) { + uintptr_t system_table_addr = boot_params->efi_info.sys_tab; + if (system_table_addr != 0) { + efi32_system_table_t *sys_table = (efi32_system_table_t *)system_table_addr; + efi_rs_table = (efi_runtime_services_t *)(uintptr_t)sys_table->runtime_services; } } #endif - if (saved_efi_info.loader_signature == EFI32_LOADER_SIGNATURE) { - uintptr_t system_table_addr = saved_efi_info.sys_tab; - if (system_table_addr != 0) { - efi32_system_table_t *sys_table = (efi32_system_table_t *)system_table_addr; - rs_table = (efi_runtime_services_t *)(uintptr_t)sys_table->runtime_services; - rs_table->reset_system(reset_type, 0, 0); - } - } } void reboot(void) @@ -59,8 +62,8 @@ void reboot(void) usleep(50); // If we have UEFI, try EFI reset service - if(saved_efi_info.loader_signature) { - efi_reset(EFI_RESET_COLD); + if (efi_rs_table != NULL) { + efi_rs_table->reset_system(EFI_RESET_COLD, 0, 0); usleep(1000000); } @@ -68,7 +71,7 @@ void reboot(void) outb(0xfe, 0x64); usleep(150000); - if(!saved_efi_info.loader_signature) { + if (efi_rs_table == NULL) { // In last resort, (very) obsolete reboot method using BIOS *((uint16_t *)0x472) = 0x1234; } diff --git a/system/hwctrl.h b/system/hwctrl.h index c70c2a6..7fc2f97 100644 --- a/system/hwctrl.h +++ b/system/hwctrl.h @@ -10,6 +10,11 @@ * Copyright (C) 2020-2022 Martin Whitaker. */ +/** + * Initialises the hardware control interface. + */ +void hwctrl_init(void); + /** * Reboots the machine. */