From 68adca4b40ea8604ad5bbae6281ee22825656b74 Mon Sep 17 00:00:00 2001 From: Zhang Junyang Date: Mon, 14 Oct 2024 21:56:38 +0800 Subject: [PATCH] Migrate to latest UEFI dependencies --- Cargo.lock | 70 +++++++------- ostd/libs/linux-bzimage/setup/Cargo.toml | 8 +- .../setup/src/x86/amd64_efi/efi.rs | 91 ++++++++++--------- 3 files changed, 84 insertions(+), 85 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b560bd0a3..8efe63ea4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "acpi" @@ -875,9 +875,9 @@ dependencies = [ "log", "uart_16550", "uefi", - "uefi-services", - "x86_64", - "xmas-elf 0.8.0", + "uefi-raw", + "x86_64 0.15.1", + "xmas-elf 0.9.1", ] [[package]] @@ -953,7 +953,7 @@ dependencies = [ "log", "multiboot2-common", "ptr_meta", - "uefi-raw 0.8.0", + "uefi-raw", ] [[package]] @@ -1119,7 +1119,7 @@ dependencies = [ "unwinding", "volatile", "x86", - "x86_64", + "x86_64 0.14.11", "xarray", ] @@ -1238,6 +1238,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "qemu-exit" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bb0fd6580eeed0103c054e3fba2c2618ff476943762f28a645b63b8692b21c9" + [[package]] name = "quote" version = "1.0.37" @@ -1470,7 +1476,7 @@ dependencies = [ "iced-x86", "lazy_static", "raw-cpuid", - "x86_64", + "x86_64 0.14.11", ] [[package]] @@ -1572,50 +1578,41 @@ dependencies = [ [[package]] name = "ucs2" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bad643914094137d475641b6bab89462505316ec2ce70907ad20102d28a79ab8" +checksum = "df79298e11f316400c57ec268f3c2c29ac3c4d4777687955cd3d4f3a35ce7eba" dependencies = [ "bit_field", ] [[package]] name = "uefi" -version = "0.26.0" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07ead9f748a4646479b850add36b527113a80e80a7e0f44d7b0334291850dcc5" +checksum = "91f17ea8502a6bd414acb2bf5194f90ca4c48e33a2d18cb57eab3294d2050d99" dependencies = [ "bitflags 2.6.0", + "cfg-if", "log", "ptr_meta", + "qemu-exit", "ucs2", "uefi-macros", - "uefi-raw 0.5.0", + "uefi-raw", "uguid", ] [[package]] name = "uefi-macros" -version = "0.13.0" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26a7b1c2c808c3db854a54d5215e3f7e7aaf5dcfbce095598cba6af29895695d" +checksum = "c19ee3a01d435eda42cb9931269b349d28a1762f91ddf01c68d276f74b957cc3" dependencies = [ "proc-macro2", "quote", "syn 2.0.77", ] -[[package]] -name = "uefi-raw" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "864ac69eadd877bfb34e7814be1928122ed0057d9f975169a56ee496aa7bdfd7" -dependencies = [ - "bitflags 2.6.0", - "ptr_meta", - "uguid", -] - [[package]] name = "uefi-raw" version = "0.8.0" @@ -1627,17 +1624,6 @@ dependencies = [ "uguid", ] -[[package]] -name = "uefi-services" -version = "0.23.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a79fcb420624743c895bad0f9480fbc2f64e7c8d8611fb1ada6bdd799942feb4" -dependencies = [ - "cfg-if", - "log", - "uefi", -] - [[package]] name = "uguid" version = "2.1.0" @@ -1751,6 +1737,18 @@ dependencies = [ "volatile", ] +[[package]] +name = "x86_64" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bc79523af8abf92fb1a970c3e086c5a343f6bcc1a0eb890f575cbb3b45743df" +dependencies = [ + "bit_field", + "bitflags 2.6.0", + "rustversion", + "volatile", +] + [[package]] name = "xarray" version = "0.1.0" diff --git a/ostd/libs/linux-bzimage/setup/Cargo.toml b/ostd/libs/linux-bzimage/setup/Cargo.toml index b572eddf6..a93679b8f 100644 --- a/ostd/libs/linux-bzimage/setup/Cargo.toml +++ b/ostd/libs/linux-bzimage/setup/Cargo.toml @@ -18,14 +18,14 @@ core2 = { version = "0.4.0", default-features = false, features = ["nightly"] } libflate = { version = "2.1.0", default-features = false } linux-boot-params = { path = "../boot-params", version = "0.1.0" } uart_16550 = "0.3.0" -xmas-elf = "0.8.0" +xmas-elf = "0.9.1" [target.x86_64-unknown-none.dependencies] bitflags = "2.4.1" log = "0.4.20" -uefi = "0.26.0" -uefi-services = "0.23.0" -x86_64 = "0.14.11" +uefi = { version = "0.32.0", features = ["global_allocator", "panic_handler", "logger", "qemu"]} +uefi-raw = "0.8.0" +x86_64 = "0.15.1" [features] default = [] diff --git a/ostd/libs/linux-bzimage/setup/src/x86/amd64_efi/efi.rs b/ostd/libs/linux-bzimage/setup/src/x86/amd64_efi/efi.rs index 137b7f102..8e1bd026d 100644 --- a/ostd/libs/linux-bzimage/setup/src/x86/amd64_efi/efi.rs +++ b/ostd/libs/linux-bzimage/setup/src/x86/amd64_efi/efi.rs @@ -2,14 +2,12 @@ use linux_boot_params::BootParams; use uefi::{ - data_types::Handle, + prelude::*, proto::loaded_image::LoadedImage, - table::{ - boot::MemoryMap, - cfg::{ACPI2_GUID, ACPI_GUID}, - Boot, Runtime, SystemTable, - }, + boot::{exit_boot_services, open_protocol_exclusive}, + mem::memory_map::{MemoryMapOwned, MemoryMap}, }; +use uefi_raw::table::system::SystemTable; use super::{ decoder::decode_payload, @@ -22,77 +20,78 @@ use super::{ #[allow(unused_variables)] #[allow(clippy::diverging_sub_expression)] #[export_name = "efi_stub_entry"] -extern "sysv64" fn efi_stub_entry(handle: Handle, mut system_table: SystemTable) -> ! { +extern "sysv64" fn efi_stub_entry(handle: Handle, system_table: *const SystemTable) -> ! { unsafe { - system_table.boot_services().set_image_handle(handle); + boot::set_image_handle(handle); + uefi::table::set_system_table(system_table); } - uefi_services::init(&mut system_table).unwrap(); + uefi::helpers::init().unwrap(); let boot_params = todo!("Use EFI boot services to fill boot params"); - efi_phase_boot(handle, system_table, boot_params); + efi_phase_boot(boot_params); } #[export_name = "efi_handover_entry"] extern "sysv64" fn efi_handover_entry( handle: Handle, - mut system_table: SystemTable, + system_table: *const SystemTable, boot_params_ptr: *mut BootParams, ) -> ! { unsafe { - system_table.boot_services().set_image_handle(handle); + boot::set_image_handle(handle); + uefi::table::set_system_table(system_table); } - uefi_services::init(&mut system_table).unwrap(); + uefi::helpers::init().unwrap(); // SAFETY: boot_params is a valid pointer. let boot_params = unsafe { &mut *boot_params_ptr }; - efi_phase_boot(handle, system_table, boot_params) + efi_phase_boot(boot_params) } -fn efi_phase_boot( - handle: Handle, - system_table: SystemTable, - boot_params: &mut BootParams, -) -> ! { - // SAFETY: this init function is only called once. - unsafe { crate::console::init() }; +fn efi_phase_boot(boot_params: &mut BootParams) -> ! { + // SAFETY: This is the right time to initialize the console and it is only + // called once here before all console operations. + unsafe { + crate::console::init(); + } // SAFETY: this is the right time to apply relocations. unsafe { apply_rela_dyn_relocations() }; - uefi_services::println!("[EFI stub] Relocations applied."); + uefi::println!("[EFI stub] Relocations applied."); + uefi::println!("[EFI stub] Stub loaded at {:#x?}", crate::x86::get_image_loaded_offset()); // Fill the boot params with the RSDP address if it is not provided. if boot_params.acpi_rsdp_addr == 0 { - boot_params.acpi_rsdp_addr = get_rsdp_addr(&system_table); + boot_params.acpi_rsdp_addr = get_rsdp_addr(); } // Load the kernel payload to memory. let payload = crate::get_payload(boot_params); let kernel = decode_payload(payload); - uefi_services::println!("[EFI stub] Loading payload."); + uefi::println!("[EFI stub] Loading payload."); crate::loader::load_elf(&kernel); - uefi_services::println!("[EFI stub] Exiting EFI boot services."); + uefi::println!("[EFI stub] Exiting EFI boot services."); let memory_type = { - let boot_services = system_table.boot_services(); - let Ok(loaded_image) = boot_services.open_protocol_exclusive::(handle) else { + let Ok(loaded_image) = open_protocol_exclusive::(boot::image_handle()) else { panic!("Failed to open LoadedImage protocol"); }; loaded_image.data_type() }; - let (system_table, memory_map) = system_table.exit_boot_services(memory_type); + // SAFETY: All allocations in the boot services phase are not used after + // this point. + let memory_map = unsafe { + exit_boot_services(memory_type) + }; - efi_phase_runtime(system_table, memory_map, boot_params); + efi_phase_runtime(memory_map, boot_params); } -fn efi_phase_runtime( - _system_table: SystemTable, - memory_map: MemoryMap<'static>, - boot_params: &mut BootParams, -) -> ! { +fn efi_phase_runtime(memory_map: MemoryMapOwned, boot_params: &mut BootParams) -> ! { unsafe { crate::console::print_str("[EFI stub] Entered runtime services.\n"); } @@ -218,16 +217,18 @@ fn efi_phase_runtime( } } -fn get_rsdp_addr(boot_table: &SystemTable) -> u64 { - let config_table = boot_table.config_table(); - for entry in config_table { - // Prefer ACPI2 over ACPI. - if entry.guid == ACPI2_GUID { - return entry.address as usize as u64; +fn get_rsdp_addr() -> u64 { + use uefi::table::cfg::{ACPI2_GUID, ACPI_GUID}; + uefi::system::with_config_table(|table| { + for entry in table { + // Prefer ACPI2 over ACPI. + if entry.guid == ACPI2_GUID { + return entry.address as usize as u64; + } + if entry.guid == ACPI_GUID { + return entry.address as usize as u64; + } } - if entry.guid == ACPI_GUID { - return entry.address as usize as u64; - } - } - panic!("ACPI RSDP not found"); + panic!("ACPI RSDP not found"); + }) }