From 42aa905ea557e712328a3f2558d9f203a89461eb Mon Sep 17 00:00:00 2001 From: Michael Kubacki Date: Mon, 28 Aug 2023 12:25:39 -0400 Subject: [PATCH] MsCorePkg: Add HelloWorldRustDxe Adds a simple Rust based DXE driver to demonstrate how to structure a DXE driver that only has Rust crate dependencies. Within the firmware build framework, this is considered a "pure Rust" DXE driver in that it only has a `Cargo.toml` file in the `[Sources]` section of the INF with no EDK II library dependencies. The module uses the `RustAdvancedLoggerDxe` crate (which is a wrapper around the Advanced Logger protocol) to write debug messages and the `RustBootServicesAllocatorDxe`. Co-authored-by: John Schock Signed-off-by: Michael Kubacki --- Cargo.toml | 1 + MsCorePkg/HelloWorldRustDxe/.gitignore | 3 ++ MsCorePkg/HelloWorldRustDxe/Cargo.toml | 14 ++++++ .../HelloWorldRustDxe/HelloWorldRustDxe.inf | 20 ++++++++ MsCorePkg/HelloWorldRustDxe/src/main.rs | 46 +++++++++++++++++++ MsCorePkg/MsCorePkg.ci.yaml | 1 + MsCorePkg/MsCorePkg.dsc | 1 + 7 files changed, 86 insertions(+) create mode 100644 MsCorePkg/HelloWorldRustDxe/.gitignore create mode 100644 MsCorePkg/HelloWorldRustDxe/Cargo.toml create mode 100644 MsCorePkg/HelloWorldRustDxe/HelloWorldRustDxe.inf create mode 100644 MsCorePkg/HelloWorldRustDxe/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index b696ebd07c..a182a23755 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ # Add packages that generate binaries here members = [ + "MsCorePkg/HelloWorldRustDxe", ] # Add packages that generate libraries here diff --git a/MsCorePkg/HelloWorldRustDxe/.gitignore b/MsCorePkg/HelloWorldRustDxe/.gitignore new file mode 100644 index 0000000000..2411493d26 --- /dev/null +++ b/MsCorePkg/HelloWorldRustDxe/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +Cargo.lock diff --git a/MsCorePkg/HelloWorldRustDxe/Cargo.toml b/MsCorePkg/HelloWorldRustDxe/Cargo.toml new file mode 100644 index 0000000000..005c87f6ee --- /dev/null +++ b/MsCorePkg/HelloWorldRustDxe/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "HelloWorldRustDxe" +version = "0.1.0" +edition = "2021" + +[[bin]] +name = "HelloWorldRustDxe" +path = "src/main.rs" +test = false + +[dependencies] +r-efi = {workspace=true} +RustAdvancedLoggerDxe = {workspace=true} +RustBootServicesAllocatorDxe = {workspace=true} diff --git a/MsCorePkg/HelloWorldRustDxe/HelloWorldRustDxe.inf b/MsCorePkg/HelloWorldRustDxe/HelloWorldRustDxe.inf new file mode 100644 index 0000000000..edc2db5324 --- /dev/null +++ b/MsCorePkg/HelloWorldRustDxe/HelloWorldRustDxe.inf @@ -0,0 +1,20 @@ +### @file +# Hello World DXE Rust driver. +# +# Copyright (c) Microsoft Corporation. All rights reserved. +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +### + +[Defines] + INF_VERSION = 1.27 + BASE_NAME = HelloWorldRustDxe + FILE_GUID = A14E35E2-BB32-4446-8622-C1874B8284E4 + MODULE_TYPE = DXE_DRIVER + RUST_MODULE = TRUE + +[Sources] + Cargo.toml + +[Depex] + TRUE diff --git a/MsCorePkg/HelloWorldRustDxe/src/main.rs b/MsCorePkg/HelloWorldRustDxe/src/main.rs new file mode 100644 index 0000000000..4cff82374c --- /dev/null +++ b/MsCorePkg/HelloWorldRustDxe/src/main.rs @@ -0,0 +1,46 @@ +//! Hello World Rust DXE Driver +//! +//! Demonstrates how to build a DXE driver written in Rust. +//! +//! ## License +//! +//! Copyright (c) Microsoft Corporation. All rights reserved. +//! +//! SPDX-License-Identifier: BSD-2-Clause-Patent +//! +#![no_std] +#![no_main] +#![allow(non_snake_case)] + +extern crate alloc; + +use alloc::vec; +use core::panic::PanicInfo; +use r_efi::efi::Status; +use rust_advanced_logger_dxe::{debugln, init_debug, DEBUG_INFO}; + +#[no_mangle] +pub extern "efiapi" fn efi_main( + _image_handle: *const core::ffi::c_void, + _system_table: *const r_efi::system::SystemTable, +) -> u64 { + rust_boot_services_allocator_dxe::GLOBAL_ALLOCATOR.init(unsafe { (*_system_table).boot_services }); + init_debug(unsafe { (*_system_table).boot_services }); + + debugln!(DEBUG_INFO, "Hello, World. This is Rust in UEFI."); + + debugln!(DEBUG_INFO, "file: {:} line: {:} as hex: {:x}", file!(), line!(), line!()); + + let mut foo = vec!["asdf", "xyzpdq", "abcdefg", "thxyzb"]; + + debugln!(DEBUG_INFO, "Unsorted vec: {:?}", foo); + foo.sort(); + debugln!(DEBUG_INFO, "Sorted vec: {:?}", foo); + + Status::SUCCESS.as_usize() as u64 +} + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} diff --git a/MsCorePkg/MsCorePkg.ci.yaml b/MsCorePkg/MsCorePkg.ci.yaml index 1e4fea2a97..9b239804e7 100644 --- a/MsCorePkg/MsCorePkg.ci.yaml +++ b/MsCorePkg/MsCorePkg.ci.yaml @@ -117,6 +117,7 @@ "RustHostUnitTestPlugin": { "Coverage": .75, "CoverageOverrides": { + "HelloWorldRustDxe": 0.0, "RustBootServicesAllocatorDxe": 0.03 } } diff --git a/MsCorePkg/MsCorePkg.dsc b/MsCorePkg/MsCorePkg.dsc index 9ef9090f7a..b3782a7e53 100644 --- a/MsCorePkg/MsCorePkg.dsc +++ b/MsCorePkg/MsCorePkg.dsc @@ -178,6 +178,7 @@ MsCorePkg/Library/BaseIsCapsuleSupportedLibNull/BaseIsCapsuleSupportedLibNull.inf MsCorePkg/Library/SecureBootKeyStoreLibNull/SecureBootKeyStoreLibNull.inf MsCorePkg/Library/MuSecureBootKeySelectorLib/MuSecureBootKeySelectorLib.inf + MsCorePkg/HelloWorldRustDxe/HelloWorldRustDxe.inf [Components.IA32] MsCorePkg/Core/GuidedSectionExtractPeim/GuidedSectionExtract.inf