Skip to content
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Added `FilePathMediaDevicePath`.
- Added `DevicePath::as_acpi_device_path` and
`DevicePath::as_file_path_media_device_path`.
- Included `cstr8` and `cstr16` macros from uefi-macros in the prelude.

### Changed

Expand All @@ -28,6 +29,10 @@

## uefi-macros - [Unreleased]

### Added

- Added `cstr8` and `cstr16` macros.

## uefi-services - [Unreleased]

## uefi - 0.15.2
Expand Down
6 changes: 2 additions & 4 deletions src/data_types/owned_strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,14 @@ mod tests {
/// Test `CString16 == &CStr16` and `&CStr16 == CString16`.
#[test]
fn test_cstring16_cstr16_eq() {
let mut buf = [0; 4];

assert_eq!(
CStr16::from_str_with_buf("abc", &mut buf).unwrap(),
crate::prelude::cstr16!("abc"),
CString16::try_from("abc").unwrap()
);

assert_eq!(
CString16::try_from("abc").unwrap(),
CStr16::from_str_with_buf("abc", &mut buf).unwrap(),
crate::prelude::cstr16!("abc")
);
}
}
9 changes: 9 additions & 0 deletions src/data_types/strs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,15 @@ mod tests {
);
}

#[test]
fn test_cstr16_macro() {
// Just a sanity check to make sure it's spitting out the right characters
assert_eq!(
crate::prelude::cstr16!("ABC").to_u16_slice_with_nul(),
[65, 66, 67, 0]
)
}

#[test]
fn test_unaligned_cstr16() {
let mut buf = [0u16; 6];
Expand Down
4 changes: 2 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ pub use crate::table::boot::BootServices;
pub use crate::table::runtime::RuntimeServices;
pub use crate::table::{Boot, SystemTable};

// Import the macro for creating the custom entry point.
pub use uefi_macros::entry;
// Import the macro for creating the custom entry point, as well as the cstr macros.
pub use uefi_macros::{cstr16, cstr8, entry};
36 changes: 36 additions & 0 deletions uefi-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,39 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
};
result.into()
}

#[proc_macro]
pub fn cstr8(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input: LitStr = parse_macro_input!(input);
let input = input.value();
match input
.chars()
.map(u8::try_from)
.collect::<Result<Vec<u8>, _>>()
{
Ok(c) => {
quote!(unsafe { ::uefi::CStr8::from_bytes_with_nul_unchecked(&[ #(#c),* , 0 ]) }).into()
}
Err(_) => syn::Error::new_spanned(input, "Invalid character")
.into_compile_error()
.into(),
}
}

#[proc_macro]
pub fn cstr16(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input: LitStr = parse_macro_input!(input);
let input = input.value();
match input
.chars()
.map(|c| u8::try_from(c).map(|c| c as u16))
.collect::<Result<Vec<u16>, _>>()
{
Ok(c) => {
quote!(unsafe { ::uefi::CStr16::from_u16_with_nul_unchecked(&[ #(#c),* , 0 ]) }).into()
}
Err(_) => syn::Error::new_spanned(input, "Invalid character")
.into_compile_error()
.into(),
}
}
14 changes: 5 additions & 9 deletions uefi-test-runner/src/proto/media/known_disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use uefi::proto::media::file::{
use uefi::proto::media::fs::SimpleFileSystem;
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams};
use uefi::table::runtime::{Daylight, Time, TimeParams};
use uefi::CString16;

/// Test directory entry iteration.
fn test_existing_dir(directory: &mut Directory) {
info!("Testing existing directory");

let input_dir_path = CString16::try_from("test_dir").unwrap();
let input_dir_path = cstr16!("test_dir");
let mut dir = directory
.open(&input_dir_path, FileMode::Read, FileAttribute::empty())
.expect("failed to open directory")
Expand All @@ -37,7 +36,7 @@ fn test_existing_dir(directory: &mut Directory) {
/// warning. This is mostly just an excuse to verify that warnings are
/// properly converted to errors.
fn test_delete_warning(directory: &mut Directory) {
let input_file_path = CString16::try_from("test_dir\\test_input.txt").unwrap();
let input_file_path = cstr16!("test_dir\\test_input.txt");
let file = directory
.open(&input_file_path, FileMode::Read, FileAttribute::empty())
.expect("failed to open file")
Expand All @@ -55,7 +54,7 @@ fn test_existing_file(directory: &mut Directory) {
info!("Testing existing file");

// Open an existing file.
let input_file_path = CString16::try_from("test_dir\\test_input.txt").unwrap();
let input_file_path = cstr16!("test_dir\\test_input.txt");
let mut file = directory
.open(
&input_file_path,
Expand Down Expand Up @@ -111,10 +110,7 @@ fn test_existing_file(directory: &mut Directory) {
.unwrap()
);
assert_eq!(info.attribute(), FileAttribute::empty());
assert_eq!(
info.file_name(),
CString16::try_from("test_input.txt").unwrap()
);
assert_eq!(info.file_name(), cstr16!("test_input.txt"));

// Check that `get_boxed_info` returns the same info.
let boxed_info = file.get_boxed_info::<FileInfo>().unwrap();
Expand All @@ -136,7 +132,7 @@ fn test_create_file(directory: &mut Directory) {
// Create a new file.
let mut file = directory
.open(
&CString16::try_from("new_test_file.txt").unwrap(),
&cstr16!("new_test_file.txt"),
FileMode::CreateReadWrite,
FileAttribute::empty(),
)
Expand Down
5 changes: 2 additions & 3 deletions uefi-test-runner/src/runtime/vars.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use log::info;
use uefi::prelude::*;
use uefi::table::runtime::{VariableAttributes, VariableVendor};
use uefi::{CStr16, Guid};
use uefi::Guid;

fn test_variables(rt: &RuntimeServices) {
let mut buf = [0; 14];
let name = CStr16::from_str_with_buf("UefiRsTestVar", &mut buf).unwrap();
let name = cstr16!("UefiRsTestVar");
let test_value = b"TestValue";
let test_attrs = VariableAttributes::BOOTSERVICE_ACCESS | VariableAttributes::RUNTIME_ACCESS;

Expand Down