From 8fb4dbb9591ff13acdfab761ca6cfc8f42bdd961 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 | 31 ++++++++++++++++++++++++++ lib/uefi/configuration_table.h | 37 ++++++++++++++++++++++++++++++++ lib/uefi/defer.h | 17 +++++++++++++++ lib/uefi/include/system_table.h | 12 +++++------ lib/uefi/rules.mk | 1 + lib/uefi/uefi.cpp | 31 ++++++++++++++++++++++---- 6 files changed, 119 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..ef9115766 --- /dev/null +++ b/lib/uefi/configuration_table.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#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..348608fd3 --- /dev/null +++ b/lib/uefi/configuration_table.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#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/defer.h b/lib/uefi/defer.h index 87400e1b8..60692a59f 100644 --- a/lib/uefi/defer.h +++ b/lib/uefi/defer.h @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + #ifndef __DEFER_HEADER_ #define __DEFER_HEADER_ 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..d943ba195 100644 --- a/lib/uefi/uefi.cpp +++ b/lib/uefi/uefi.cpp @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + #include "arch/mmu.h" #include "boot_service.h" #include "boot_service_provider.h" @@ -11,11 +28,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 +72,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 +281,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 +293,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); }