From d74411b933a17c34b3170a1e158625018f7d8556 Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Thu, 26 Sep 2024 16:30:23 -0700 Subject: [PATCH] [app][uefi] Add configuration table --- lib/uefi/configuration_table.cpp | 14 ++++++++++++++ lib/uefi/configuration_table.h | 20 ++++++++++++++++++++ lib/uefi/include/system_table.h | 12 ++++++------ lib/uefi/rules.mk | 1 + lib/uefi/uefi.cpp | 14 ++++++++++---- 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 lib/uefi/configuration_table.cpp create mode 100644 lib/uefi/configuration_table.h diff --git a/lib/uefi/configuration_table.cpp b/lib/uefi/configuration_table.cpp new file mode 100644 index 000000000..b187f387d --- /dev/null +++ b/lib/uefi/configuration_table.cpp @@ -0,0 +1,14 @@ +#include "configuration_table.h" +#include "boot_service_provider.h" +#include "system_table.h" +#include + +void setup_configuration_table(EfiSystemTable *table) { + auto &rng = table->configuration_table[0]; + rng.vendor_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID; + rng.vendor_table = alloc_page(PAGE_SIZE); + auto rng_seed = reinterpret_cast(rng.vendor_table); + rng_seed->size = 512; + memset(&rng_seed->bits, 0, rng_seed->size); + table->number_of_table_entries = 1; +} \ No newline at end of file diff --git a/lib/uefi/configuration_table.h b/lib/uefi/configuration_table.h new file mode 100644 index 000000000..a00b1ba15 --- /dev/null +++ b/lib/uefi/configuration_table.h @@ -0,0 +1,20 @@ +#ifndef __CONFIGURATION_TABLE_ +#define __CONFIGURATION_TABLE_ + +#include "system_table.h" +#include "types.h" + +struct linux_efi_random_seed { + uint32_t size; + uint8_t bits[]; +}; + +static constexpr auto LINUX_EFI_RANDOM_SEED_TABLE_GUID = + EfiGuid{0x1ce1e5bc, + 0x7ceb, + 0x42f2, + {0x81, 0xe5, 0x8a, 0xad, 0xf1, 0x80, 0xf5, 0x7b}}; + +void setup_configuration_table(EfiSystemTable *table); + +#endif diff --git a/lib/uefi/include/system_table.h b/lib/uefi/include/system_table.h index 9d8fbdd2c..a446969be 100644 --- a/lib/uefi/include/system_table.h +++ b/lib/uefi/include/system_table.h @@ -25,12 +25,12 @@ #include "protocols/simple_text_output_protocol.h" #include "runtime_service.h" -typedef struct { +struct EfiConfigurationTable { EfiGuid vendor_guid; - const void* vendor_table; -} EfiConfigurationTable; + void *vendor_table; +}; -typedef struct EfiSystemTable { +struct EfiSystemTable { EfiTableHeader header; char16_t* firmware_vendor; uint32_t firmware_revision; @@ -43,7 +43,7 @@ typedef struct EfiSystemTable { EfiRuntimeService *runtime_service; EfiBootService* boot_services; size_t number_of_table_entries; - const EfiConfigurationTable* configuration_table; -} EfiSystemTable; + EfiConfigurationTable *configuration_table; +}; #endif // __SYSTEM_TABLE_H__ diff --git a/lib/uefi/rules.mk b/lib/uefi/rules.mk index 6dabf4ab7..97dfbdc14 100644 --- a/lib/uefi/rules.mk +++ b/lib/uefi/rules.mk @@ -12,5 +12,6 @@ MODULE_SRCS += \ $(LOCAL_DIR)/boot_service_provider.cpp \ $(LOCAL_DIR)/runtime_service_provider.cpp \ $(LOCAL_DIR)/switch_stack.S \ + $(LOCAL_DIR)/configuration_table.cpp \ include make/module.mk diff --git a/lib/uefi/uefi.cpp b/lib/uefi/uefi.cpp index a86a50b4c..e11a9c311 100644 --- a/lib/uefi/uefi.cpp +++ b/lib/uefi/uefi.cpp @@ -11,11 +11,13 @@ #include #include #include +#include #include #include #include #include +#include "configuration_table.h" #include "protocols/simple_text_output_protocol.h" #include "runtime_service.h" #include "runtime_service_provider.h" @@ -53,7 +55,7 @@ static constexpr size_t BIT10 = 1 << 10; @return Immediate address encoded in the instruction **/ -uint16_t ThumbMovtImmediateAddress(uint16_t *Instruction) { +uint16_t ThumbMovtImmediateAddress(const uint16_t *Instruction) { uint32_t Movt; uint16_t Address; @@ -262,7 +264,7 @@ int load_sections_and_execute(bdev_t *dev, image_base + optional_header->AddressOfEntryPoint); printf("Entry function located at %p\n", entry); - EfiSystemTable table{}; + EfiSystemTable &table = *static_cast(alloc_page(PAGE_SIZE)); EfiBootService boot_service{}; EfiRuntimeService runtime_service{}; fill(&runtime_service, 0); @@ -274,8 +276,12 @@ int load_sections_and_execute(bdev_t *dev, table.header.signature = EFI_SYSTEM_TABLE_SIGNATURE; EfiSimpleTextOutputProtocol console_out = get_text_output_protocol(); table.con_out = &console_out; - constexpr size_t kStackSize = 1024ul * 1024; - auto stack = reinterpret_cast(alloc_page(kStackSize, PAGE_SIZE)); + table.configuration_table = + reinterpret_cast(alloc_page(PAGE_SIZE)); + setup_configuration_table(&table); + + constexpr size_t kStackSize = 8 * 1024ul * 1024; + auto stack = reinterpret_cast(alloc_page(kStackSize, 23)); memset(stack, 0, kStackSize); return call_with_stack(stack + kStackSize, entry, image_base, &table); }