Skip to content

Commit

Permalink
MsCorePkg: Add HelloWorldRustDxe
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Michael Kubacki <[email protected]>
  • Loading branch information
makubacki and joschock committed Aug 29, 2023
1 parent b699049 commit 236c4af
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Add packages that generate binaries here
members = [
"MsCorePkg/HelloWorldRustDxe",
]

# Add packages that generate libraries here
Expand Down
3 changes: 3 additions & 0 deletions MsCorePkg/HelloWorldRustDxe/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock
14 changes: 14 additions & 0 deletions MsCorePkg/HelloWorldRustDxe/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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}
20 changes: 20 additions & 0 deletions MsCorePkg/HelloWorldRustDxe/HelloWorldRustDxe.inf
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions MsCorePkg/HelloWorldRustDxe/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}
1 change: 1 addition & 0 deletions MsCorePkg/MsCorePkg.ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"RustHostUnitTestPlugin": {
"Coverage": .75,
"CoverageOverrides": {
"HelloWorldRustDxe": 0.0,
"RustBootServicesAllocatorDxe": 0.03
}
}
Expand Down
1 change: 1 addition & 0 deletions MsCorePkg/MsCorePkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,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
Expand Down

0 comments on commit 236c4af

Please sign in to comment.