File: 002_baseboard.rs

package info (click to toggle)
rust-dmidecode 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,004 kB
  • sloc: makefile: 2
file content (161 lines) | stat: -rw-r--r-- 5,531 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! Baseboard (or Module) Information (Type 2)
//!
//! The information in this structure defines attributes of a system baseboard (for example, a
//! motherboard, planar, server blade, or other standard system module).
use core::fmt;

use bitflags::bitflags;

use crate::{MalformedStructureError, RawStructure};

/// The baseboard type defined in the SMBIOS specification.
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum BoardType {
    Unknown,
    Other,
    ServerBlade,
    ConnectivitySwitch,
    SystemManagementModule,
    ProcessorModule,
    IoModule,
    MemoryModule,
    DaughterBoard,
    MotherBoard,
    ProcessorMemoryModule,
    ProcessorIoModule,
    InterconnectBoard,
    Undefined(u8),
}

impl From<u8> for BoardType {
    fn from(_type: u8) -> BoardType {
        match _type {
            1 => BoardType::Unknown,
            2 => BoardType::Other,
            3 => BoardType::ServerBlade,
            4 => BoardType::ConnectivitySwitch,
            5 => BoardType::SystemManagementModule,
            6 => BoardType::ProcessorModule,
            7 => BoardType::IoModule,
            8 => BoardType::MemoryModule,
            9 => BoardType::DaughterBoard,
            10 => BoardType::MotherBoard,
            11 => BoardType::ProcessorMemoryModule,
            12 => BoardType::ProcessorIoModule,
            13 => BoardType::InterconnectBoard,
            t => BoardType::Undefined(t),
        }
    }
}
impl fmt::Display for BoardType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BoardType::Unknown => write!(f, "Unknown"),
            BoardType::Other => write!(f, "Other"),
            BoardType::ServerBlade => write!(f, "Server Blade"),
            BoardType::ConnectivitySwitch => write!(f, "Connectivity Switch"),
            BoardType::SystemManagementModule => write!(f, "System Management Module"),
            BoardType::ProcessorModule => write!(f, "Processor Module"),
            BoardType::IoModule => write!(f, "I/O Module"),
            BoardType::MemoryModule => write!(f, "Memory Module"),
            BoardType::DaughterBoard => write!(f, "Daughter board"),
            BoardType::MotherBoard => {
                write!(f, "Motherboard (includes processor, memory, and I/O)")
            }
            BoardType::ProcessorMemoryModule => write!(f, "Processor/Memory Module"),
            BoardType::ProcessorIoModule => write!(f, "Processor/IO Module"),
            BoardType::InterconnectBoard => write!(f, "Interconnect board"),
            BoardType::Undefined(t) => write!(f, "Undefined: {}", t),
        }
    }
}

bitflags! {
    /// The baseboard characteristic flags defined in the SMBIOS specification.
    #[derive(Debug, Clone, PartialEq, Eq, Hash)]
    pub struct BaseBoardFlags: u8 {
        const HOSTING = 0b0000_0001;
        const REQUIRES_DAUGHTER = 0b0000_0010;
        const IS_REMOVABLE = 0b0000_0100;
        const IS_REPLACEABLE = 0b0000_1000;
        const IS_HOT_SWAPPABLE = 0b0001_0000;
    }
}

/// The `BaseBoard` table defined in the SMBIOS specification.
///
/// Optional fields will only be set if the version of the parsed SMBIOS table
/// is high enough to have defined the field.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct BaseBoard<'buffer> {
    pub handle: u16,
    pub manufacturer: &'buffer str,
    pub product: &'buffer str,
    pub version: &'buffer str,
    pub serial: &'buffer str,
    pub asset: Option<&'buffer str>,
    pub feature_flags: Option<BaseBoardFlags>,
    pub location_in_chassis: Option<&'buffer str>,
    pub chassis_handle: Option<u16>,
    pub board_type: Option<BoardType>,
}

impl<'buffer> BaseBoard<'buffer> {
    pub(crate) fn try_from(structure: RawStructure<'buffer>) -> Result<BaseBoard<'buffer>, MalformedStructureError> {
        #[repr(C)]
        #[repr(packed)]
        struct BaseBoardPacked {
            manufacturer: u8,
            product: u8,
            version: u8,
            serial: u8,
            asset: u8,
            feature_flags: u8,
            location_in_chassis: u8,
            chassis_handle: u16,
            board_type: u8,
        }

        let_as_struct!(packed, BaseBoardPacked, structure.data);

        let asset = if structure.data.len() > 4 {
            Some(structure.find_string(packed.asset)?)
        } else {
            None
        };
        let feature_flags = if structure.data.len() > 5 {
            Some(BaseBoardFlags::from_bits_truncate(packed.feature_flags))
        } else {
            None
        };
        let location_in_chassis = if structure.data.len() > 6 {
            Some(structure.find_string(packed.location_in_chassis)?)
        } else {
            None
        };
        let chassis_handle = if structure.data.len() > 7 {
            Some(u16::from_le(packed.chassis_handle))
        } else {
            None
        };
        let board_type = if structure.data.len() > 9 {
            Some(packed.board_type.into())
        } else {
            None
        };

        Ok(BaseBoard {
            handle: structure.handle,
            manufacturer: structure.find_string(packed.manufacturer)?,
            product: structure.find_string(packed.product)?,
            version: structure.find_string(packed.version)?,
            serial: structure.find_string(packed.serial)?,
            asset,
            feature_flags,
            location_in_chassis,
            chassis_handle,
            board_type,
        })
    }
}