smbioslib/structs/types/
hardware_security.rs

1use crate::{SMBiosStruct, UndefinedStruct};
2use serde::{ser::SerializeStruct, Serialize, Serializer};
3use std::fmt;
4
5/// # Hardware Security (Type 24)
6///
7/// This structure describes the system-wide hardware security settings.
8///
9/// Compliant with:
10/// DMTF SMBIOS Reference Specification 3.4.0 (DSP0134)
11/// Document Date: 2020-07-17
12pub struct SMBiosHardwareSecurity<'a> {
13    parts: &'a UndefinedStruct,
14}
15
16impl<'a> SMBiosStruct<'a> for SMBiosHardwareSecurity<'a> {
17    const STRUCT_TYPE: u8 = 24u8;
18
19    fn new(parts: &'a UndefinedStruct) -> Self {
20        Self { parts }
21    }
22
23    fn parts(&self) -> &'a UndefinedStruct {
24        self.parts
25    }
26}
27
28impl<'a> SMBiosHardwareSecurity<'a> {
29    /// Bit field that identifies the password and reset status for the system
30    pub fn hardware_security_settings(&self) -> Option<HardwareSecuritySettings> {
31        self.parts
32            .get_field_byte(0x4)
33            .map(|raw| HardwareSecuritySettings::from(raw))
34    }
35}
36
37impl fmt::Debug for SMBiosHardwareSecurity<'_> {
38    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
39        fmt.debug_struct(std::any::type_name::<SMBiosHardwareSecurity<'_>>())
40            .field("header", &self.parts.header)
41            .field(
42                "hardware_security_settings",
43                &self.hardware_security_settings(),
44            )
45            .finish()
46    }
47}
48
49impl Serialize for SMBiosHardwareSecurity<'_> {
50    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
51    where
52        S: Serializer,
53    {
54        let mut state = serializer.serialize_struct("SMBiosHardwareSecurity", 2)?;
55        state.serialize_field("header", &self.parts.header)?;
56        state.serialize_field(
57            "hardware_security_settings",
58            &self.hardware_security_settings(),
59        )?;
60        state.end()
61    }
62}
63
64/// # Hardware Security Settings
65#[derive(PartialEq, Eq)]
66pub struct HardwareSecuritySettings {
67    /// Raw value
68    pub raw: u8,
69    /// Power-on Password Status
70    pub power_on_password_status: HardwareSecurityStatus,
71    /// Keyboard Password Status
72    pub keyboard_password_status: HardwareSecurityStatus,
73    /// Administrator Password Status
74    pub administrator_password_status: HardwareSecurityStatus,
75    /// Front Panel Reset Status
76    pub front_panel_reset_status: HardwareSecurityStatus,
77}
78
79impl fmt::Debug for HardwareSecuritySettings {
80    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
81        fmt.debug_struct(std::any::type_name::<HardwareSecuritySettings>())
82            .field("raw", &self.raw)
83            .field("power_on_password_status", &self.power_on_password_status)
84            .field("keyboard_password_status", &self.keyboard_password_status)
85            .field(
86                "administrator_password_status",
87                &self.administrator_password_status,
88            )
89            .field("front_panel_reset_status", &self.front_panel_reset_status)
90            .finish()
91    }
92}
93
94impl Serialize for HardwareSecuritySettings {
95    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
96    where
97        S: Serializer,
98    {
99        let mut state = serializer.serialize_struct("HardwareSecuritySettings", 5)?;
100        state.serialize_field("raw", &self.raw)?;
101        state.serialize_field("power_on_password_status", &self.power_on_password_status)?;
102        state.serialize_field("keyboard_password_status", &self.keyboard_password_status)?;
103        state.serialize_field(
104            "administrator_password_status",
105            &self.administrator_password_status,
106        )?;
107        state.serialize_field("front_panel_reset_status", &self.front_panel_reset_status)?;
108        state.end()
109    }
110}
111
112/// # Hardware Security Status
113#[derive(Serialize, Debug, PartialEq, Eq)]
114pub enum HardwareSecurityStatus {
115    /// Disabled
116    Disabled,
117    /// Enabled
118    Enabled,
119    /// Not implemented
120    NotImplemented,
121    /// Unknown status
122    Unknown,
123}
124
125impl From<u8> for HardwareSecuritySettings {
126    fn from(raw: u8) -> Self {
127        HardwareSecuritySettings {
128            power_on_password_status: match raw & 0b11_000000 {
129                0b00_000000 => HardwareSecurityStatus::Disabled,
130                0b01_000000 => HardwareSecurityStatus::Enabled,
131                0b10_000000 => HardwareSecurityStatus::NotImplemented,
132                0b11_000000 => HardwareSecurityStatus::Unknown,
133                _ => panic!("Impossible value"),
134            },
135            keyboard_password_status: match raw & 0b00_11_0000 {
136                0b00_00_0000 => HardwareSecurityStatus::Disabled,
137                0b00_01_0000 => HardwareSecurityStatus::Enabled,
138                0b00_10_0000 => HardwareSecurityStatus::NotImplemented,
139                0b00_11_0000 => HardwareSecurityStatus::Unknown,
140                _ => panic!("Impossible value"),
141            },
142            administrator_password_status: match raw & 0b0000_11_00 {
143                0b0000_00_00 => HardwareSecurityStatus::Disabled,
144                0b0000_01_00 => HardwareSecurityStatus::Enabled,
145                0b0000_10_00 => HardwareSecurityStatus::NotImplemented,
146                0b0000_11_00 => HardwareSecurityStatus::Unknown,
147                _ => panic!("Impossible value"),
148            },
149            front_panel_reset_status: match raw & 0b000000_11 {
150                0b000000_00 => HardwareSecurityStatus::Disabled,
151                0b000000_01 => HardwareSecurityStatus::Enabled,
152                0b000000_10 => HardwareSecurityStatus::NotImplemented,
153                0b000000_11 => HardwareSecurityStatus::Unknown,
154                _ => panic!("Impossible value"),
155            },
156            raw,
157        }
158    }
159}
160#[cfg(test)]
161mod tests {
162    use super::*;
163
164    #[test]
165    fn unit_test() {
166        let struct_type24 = vec![0x18, 0x05, 0x24, 0x00, 0x16, 0x00, 0x00];
167
168        let parts = UndefinedStruct::new(&struct_type24);
169        let test_struct = SMBiosHardwareSecurity::new(&parts);
170
171        assert_eq!(
172            test_struct.hardware_security_settings(),
173            Some(HardwareSecuritySettings::from(22))
174        );
175    }
176}