Skip to content

Commit 7fd371f

Browse files
committed
uefi: Implement CalculateCrc32() boot services function
UEFI Boot services includes a helper function (CalculateCrc32) for calculating a 32-bit CRC over a buffer. This is useful in Rust applications as well, so expose it in the boot services API. Signed-off-by: Bjorn Andersson <[email protected]>
1 parent ee625da commit 7fd371f

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

uefi-test-runner/src/boot/misc.rs

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub fn test() {
2626
test_reinstall_protocol_interface();
2727
test_uninstall_protocol_interface();
2828
test_install_configuration_table();
29+
info!("Testing crc32...");
30+
test_calculate_crc32();
2931
}
3032

3133
fn test_tpl() {
@@ -247,3 +249,11 @@ fn test_install_configuration_table() {
247249
boot::free_pool(config).unwrap();
248250
}
249251
}
252+
253+
fn test_calculate_crc32() {
254+
let data = "uefi-rs";
255+
256+
let crc = boot::calculate_crc32(data.as_bytes()).unwrap();
257+
258+
assert_eq!(crc, 0xcfc96a3e);
259+
}

uefi/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Added `proto::ata::pass_thru::AtaPassThru`.
1414
- Added `boot::ScopedProtocol::open_params()`.
1515
- Added `boot::TplGuard::old_tpl()`.
16+
- Added `boot::calculate_crc32()`.
1617

1718
## Changed
1819
- **Breaking:** Removed `BootPolicyError` as `BootPolicy` construction is no

uefi/src/boot.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,18 @@ pub fn get_image_file_system(image_handle: Handle) -> Result<ScopedProtocol<Simp
14611461
open_protocol_exclusive(device_handle)
14621462
}
14631463

1464+
/// Calculates the 32-bit CRC32 for the provided slice.
1465+
///
1466+
/// # Errors
1467+
/// * [`Status::INVALID_PARAMETER`]
1468+
pub fn calculate_crc32(data: &[u8]) -> Result<u32> {
1469+
let bt = boot_services_raw_panicking();
1470+
let bt = unsafe { bt.as_ref() };
1471+
let mut crc = 0u32;
1472+
1473+
unsafe { (bt.calculate_crc32)(data.as_ptr().cast(), data.len(), &mut crc) }.to_result_with_val(|| crc)
1474+
}
1475+
14641476
/// Protocol interface [`Guids`][Guid] that are installed on a [`Handle`] as
14651477
/// returned by [`protocols_per_handle`].
14661478
#[derive(Debug)]

0 commit comments

Comments
 (0)