libcros 0.5.1

A Rust library that provides easy-to-use functions for interacting with a Chrome device
Documentation
#[cfg(all(not(feature = "tpm1_2"), not(feature = "tpm2_0"), feature = "tlcl"))]
use crate::tlcl::commands::stubs::TlclReadWithOffset;
#[cfg(all(feature = "tlcl", feature = "tpm1_2", not(feature = "tpm2_0")))]
use crate::tlcl::commands::tpm12::TlclReadWithOffset;
#[cfg(all(feature = "tlcl", feature = "tpm2_0"))]
use crate::tlcl::commands::tpm20::TlclReadWithOffset;
use crate::LOG_DBG;

/// Read active kernel version from TPM NV.
/// Returns u32::MAX on error.
#[cfg(feature = "tlcl")]
pub fn kernver() -> u32 {
  let mut outbuf: [u8; 4] = unsafe { core::mem::zeroed() };

  let rc = TlclReadWithOffset(
    0x1008,
    0x4,
    0x5,
    outbuf.as_mut_ptr() as *mut core::ffi::c_void,
  );

  if rc != 0 {
    LOG_DBG!("TlclReadWithOffset failed with code: {}", rc);
    return u32::MAX;
  }

  if outbuf.len() < 4 {
    LOG_DBG!("TlclReadWithOffset returned too few bytes");
    return u32::MAX;
  }

  let bytes: [u8; 4] = outbuf[0..4].try_into().unwrap();
  let val = u32::from_le_bytes(bytes);

  LOG_DBG!("read bytes: {}", val);

  val
}

#[cfg(not(feature = "tlcl"))]
/// Fallback when tlcl is disabled.
pub fn kernver() -> u32 {
  LOG_DBG!("tlcl feature not enabled");
  u32::MAX
}