mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
rust/pv: Refactor seek_se_hdr_start and export the function
Refactor `seek_se_hdr_start` so it can be reused. While at it, improve the documentation of it and format the code. Reviewed-by: Steffen Eiden <seiden@linux.ibm.com> Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com> Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
committed by
Jan Höppner
parent
f095a892b9
commit
7d41430e68
@@ -7,11 +7,12 @@ use std::{
|
||||
mem::size_of,
|
||||
};
|
||||
|
||||
// (SE) boot request control block aka SE header
|
||||
use crate::{assert_size, request::MagicValue, static_assert, Error, Result, PAGESIZE};
|
||||
use log::debug;
|
||||
use zerocopy::{AsBytes, BigEndian, FromBytes, FromZeroes, U32, U64};
|
||||
|
||||
// (SE) boot request control block aka SE header
|
||||
use crate::{assert_size, request::MagicValue, static_assert, Error, Result, PAGESIZE};
|
||||
|
||||
/// Struct containing all SE-header tags.
|
||||
///
|
||||
/// Contains:
|
||||
@@ -52,6 +53,45 @@ impl MagicValue<8> for BootHdrMagic {
|
||||
const MAGIC: [u8; 8] = [0x49, 0x42, 0x4d, 0x53, 0x65, 0x63, 0x45, 0x78];
|
||||
}
|
||||
|
||||
/// Tries to seek to the start of the Secure Execution header.
|
||||
///
|
||||
/// Returns `false` if no Secure Execution header found, `true` otherwise.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// In the very unlikely case an IO error can appear when seeking to the
|
||||
/// beginning of the header.
|
||||
pub fn seek_se_hdr_start<R>(img: &mut R) -> Result<bool>
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
let max_iter: usize = 0x15;
|
||||
const BUF_SIZE: i64 = 8;
|
||||
static_assert!(BootHdrMagic::MAGIC.len() == BUF_SIZE as usize);
|
||||
|
||||
let mut buf = [0; BUF_SIZE as usize];
|
||||
for _ in 0..max_iter {
|
||||
match img.read_exact(&mut buf) {
|
||||
Ok(it) => it,
|
||||
Err(_) => return Ok(false),
|
||||
};
|
||||
|
||||
if BootHdrMagic::starts_with_magic(&buf) {
|
||||
// go back to the beginning of the header
|
||||
img.seek(Current(-BUF_SIZE))?;
|
||||
|
||||
return Ok(true);
|
||||
}
|
||||
// goto next page start
|
||||
// or report invalid file format if file ends "early"
|
||||
match img.seek(Current(PAGESIZE as i64 - BUF_SIZE)) {
|
||||
Ok(it) => it,
|
||||
Err(_) => return Ok(false),
|
||||
};
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
impl BootHdrTags {
|
||||
/// Returns a reference to the SE-header tag of this [`BootHdrTags`].
|
||||
pub fn tag(&self) -> &[u8; 16] {
|
||||
@@ -64,40 +104,6 @@ impl BootHdrTags {
|
||||
Self { ald, tld, pld, tag }
|
||||
}
|
||||
|
||||
/// Returns `false` if no SE-header found, `true` otherwise.
|
||||
/// In the very unlikely case an IO error can appear
|
||||
/// when seeking to the beginning of the header.
|
||||
fn seek_se_hdr_start<R>(img: &mut R) -> Result<bool>
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
const MAX_ITER: usize = 0x15;
|
||||
const BUF_SIZE: i64 = 8;
|
||||
static_assert!(BootHdrMagic::MAGIC.len() == BUF_SIZE as usize);
|
||||
|
||||
let mut buf = [0; BUF_SIZE as usize];
|
||||
for _ in [0; MAX_ITER] {
|
||||
match img.read_exact(&mut buf) {
|
||||
Ok(it) => it,
|
||||
Err(_) => return Ok(false),
|
||||
};
|
||||
|
||||
if BootHdrMagic::starts_with_magic(&buf) {
|
||||
// go back to the beginning of the header
|
||||
img.seek(Current(-BUF_SIZE))?;
|
||||
|
||||
return Ok(true);
|
||||
}
|
||||
// goto next page start
|
||||
// or report invalid file format if file ends "early"
|
||||
match img.seek(Current(PAGESIZE as i64 - BUF_SIZE)) {
|
||||
Ok(it) => it,
|
||||
Err(_) => return Ok(false),
|
||||
};
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Deserializes a (SE) boot header and extracts the tags.
|
||||
///
|
||||
/// Searches for the header; if found extracts the tags.
|
||||
@@ -110,7 +116,7 @@ impl BootHdrTags {
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
if !Self::seek_se_hdr_start(img)? {
|
||||
if !seek_se_hdr_start(img)? {
|
||||
debug!("No boot hdr found");
|
||||
return Err(Error::InvBootHdr);
|
||||
}
|
||||
@@ -186,8 +192,7 @@ mod tests {
|
||||
use std::io::Cursor;
|
||||
|
||||
use super::*;
|
||||
use crate::get_test_asset;
|
||||
use crate::Error;
|
||||
use crate::{get_test_asset, Error};
|
||||
|
||||
const EXP_HDR: BootHdrTags = BootHdrTags {
|
||||
pld: [
|
||||
|
||||
@@ -86,7 +86,7 @@ pub use crate::error::HkdVerifyErrorType;
|
||||
/// Functionalities to build UV requests
|
||||
pub mod request {
|
||||
pub use crate::{
|
||||
brcb::BootHdrTags,
|
||||
brcb::{seek_se_hdr_start, BootHdrTags},
|
||||
crypto::{
|
||||
decrypt_aead, derive_aes256_gcm_key, encrypt_aead, gen_ec_key, random_array,
|
||||
AeadDecryptionResult, AeadEncryptionResult, Aes256GcmKey, Aes256XtsKey, SymKey,
|
||||
|
||||
Reference in New Issue
Block a user