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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use crate::{SMBiosStruct, UndefinedStruct};
use std::fmt;
use std::ops::Deref;

/// # 32-Bit Memory Error Information (Type 18)
///
/// This structure identifies the specifics of an error that might be detected within a [SMBiosPhysicalMemoryArray].
///
/// Compliant with:
/// DMTF SMBIOS Reference Specification 3.4.0 (DSP0134)
/// Document Date: 2020-07-17
pub struct SMBiosMemoryErrorInformation32<'a> {
    parts: &'a UndefinedStruct,
}

impl<'a> SMBiosStruct<'a> for SMBiosMemoryErrorInformation32<'a> {
    const STRUCT_TYPE: u8 = 18u8;

    fn new(parts: &'a UndefinedStruct) -> Self {
        Self { parts }
    }

    fn parts(&self) -> &'a UndefinedStruct {
        self.parts
    }
}

impl<'a> SMBiosMemoryErrorInformation32<'a> {
    /// Type of error that is associated with the current
    /// status reported for the memory array or device
    pub fn error_type(&self) -> Option<MemoryErrorTypeData> {
        self.parts
            .get_field_byte(0x04)
            .map(|raw| MemoryErrorTypeData::from(raw))
    }

    /// Granularity (for example, device versus Partition)
    /// to which the error can be resolved
    pub fn error_granularity(&self) -> Option<MemoryErrorGranularityData> {
        self.parts
            .get_field_byte(0x05)
            .map(|raw| MemoryErrorGranularityData::from(raw))
    }

    /// Memory access operation that caused the error
    pub fn error_operation(&self) -> Option<MemoryErrorOperationData> {
        self.parts
            .get_field_byte(0x06)
            .map(|raw| MemoryErrorOperationData::from(raw))
    }

    /// Vendor-specific ECC syndrome or CRC data
    /// associated with the erroneous access
    /// If the value is unknown, this field contains 0000
    /// 0000h.
    pub fn vendor_syndrome(&self) -> Option<u32> {
        self.parts.get_field_dword(0x07)
    }

    /// 32-bit physical address of the error based on the
    /// addressing of the bus to which the memory array
    /// is connected
    /// If the address is unknown, this field contains
    /// 8000 0000h.
    pub fn memory_array_error_address(&self) -> Option<u32> {
        self.parts.get_field_dword(0x0B)
    }

    /// 32-bit physical address of the error relative to the
    /// start of the failing memory device, in bytes
    /// If the address is unknown, this field contains
    /// 8000 0000h.
    pub fn device_error_address(&self) -> Option<u32> {
        self.parts.get_field_dword(0x0F)
    }

    /// Range, in bytes, within which the error can be
    /// determined, when an error address is given
    /// If the range is unknown, this field contains 8000
    /// 0000h.
    pub fn error_resolution(&self) -> Option<u32> {
        self.parts.get_field_dword(0x13)
    }
}

impl fmt::Debug for SMBiosMemoryErrorInformation32<'_> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<SMBiosMemoryErrorInformation32<'_>>())
            .field("header", &self.parts.header)
            .field("error_type", &self.error_type())
            .field("error_granularity", &self.error_granularity())
            .field("error_operation", &self.error_operation())
            .field("vendor_syndrome", &self.vendor_syndrome())
            .field(
                "memory_array_error_address",
                &self.memory_array_error_address(),
            )
            .field("device_error_address", &self.device_error_address())
            .field("error_resolution", &self.error_resolution())
            .finish()
    }
}

/// # Memory Error - Error Type Data
pub struct MemoryErrorTypeData {
    /// Raw value
    ///
    /// _raw_ is most useful when _value_ is None.
    /// This is most likely to occur when the standard was updated but
    /// this library code has not been updated to match the current
    /// standard.
    pub raw: u8,
    /// The contained [MemoryErrorType] value
    pub value: MemoryErrorType,
}

impl fmt::Debug for MemoryErrorTypeData {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<MemoryErrorTypeData>())
            .field("raw", &self.raw)
            .field("value", &self.value)
            .finish()
    }
}

impl Deref for MemoryErrorTypeData {
    type Target = MemoryErrorType;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

/// # Memory Error - Error Type
#[derive(Debug, PartialEq, Eq)]
pub enum MemoryErrorType {
    /// Other
    Other,
    /// Unknown
    Unknown,
    /// OK
    OK,
    /// Bad read
    BadRead,
    /// Parity error
    ParityError,
    /// Single-bit error
    SingleBitError,
    /// Double-bit error
    DoubleBitError,
    /// Multi-bit error
    MultiBitError,
    /// Nibble error
    NibbleError,
    /// Checksum error
    ChecksumError,
    /// CRC error
    CrcError,
    /// Corrected single-bit error
    CorrectedSingleBitError,
    /// Corrected error
    CorrectedError,
    /// Uncorrectable error
    UncorrectableError,
    /// A value unknown to this standard, check the raw value
    None,
}

impl From<u8> for MemoryErrorTypeData {
    fn from(raw: u8) -> Self {
        MemoryErrorTypeData {
            value: match raw {
                0x01 => MemoryErrorType::Other,
                0x02 => MemoryErrorType::Unknown,
                0x03 => MemoryErrorType::OK,
                0x04 => MemoryErrorType::BadRead,
                0x05 => MemoryErrorType::ParityError,
                0x06 => MemoryErrorType::SingleBitError,
                0x07 => MemoryErrorType::DoubleBitError,
                0x08 => MemoryErrorType::MultiBitError,
                0x09 => MemoryErrorType::NibbleError,
                0x0A => MemoryErrorType::ChecksumError,
                0x0B => MemoryErrorType::CrcError,
                0x0C => MemoryErrorType::CorrectedSingleBitError,
                0x0D => MemoryErrorType::CorrectedError,
                0x0E => MemoryErrorType::UncorrectableError,
                _ => MemoryErrorType::None,
            },
            raw,
        }
    }
}

/// # Memory Error - Error Granularity Data
pub struct MemoryErrorGranularityData {
    /// Raw value
    ///
    /// _raw_ is most useful when _value_ is None.
    /// This is most likely to occur when the standard was updated but
    /// this library code has not been updated to match the current
    /// standard.
    pub raw: u8,
    /// The contained [MemoryErrorGranularity] value
    pub value: MemoryErrorGranularity,
}

impl fmt::Debug for MemoryErrorGranularityData {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<MemoryErrorGranularityData>())
            .field("raw", &self.raw)
            .field("value", &self.value)
            .finish()
    }
}

impl Deref for MemoryErrorGranularityData {
    type Target = MemoryErrorGranularity;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

/// # Memory Error - Error Granularity
#[derive(Debug, PartialEq, Eq)]
pub enum MemoryErrorGranularity {
    /// Other
    Other,
    /// Unknown
    Unknown,
    /// Device level
    DeviceLevel,
    /// Memory partition level
    MemoryPartitionLevel,
    /// A value unknown to this standard, check the raw value
    None,
}

impl From<u8> for MemoryErrorGranularityData {
    fn from(raw: u8) -> Self {
        MemoryErrorGranularityData {
            value: match raw {
                0x01 => MemoryErrorGranularity::Other,
                0x02 => MemoryErrorGranularity::Unknown,
                0x03 => MemoryErrorGranularity::DeviceLevel,
                0x04 => MemoryErrorGranularity::MemoryPartitionLevel,
                _ => MemoryErrorGranularity::None,
            },
            raw,
        }
    }
}

/// # Memory Error - Error Operation Data
pub struct MemoryErrorOperationData {
    /// Raw value
    ///
    /// _raw_ is most useful when _value_ is None.
    /// This is most likely to occur when the standard was updated but
    /// this library code has not been updated to match the current
    /// standard.
    pub raw: u8,
    /// The contained [MemoryErrorOperation] value
    pub value: MemoryErrorOperation,
}

impl fmt::Debug for MemoryErrorOperationData {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<MemoryErrorOperationData>())
            .field("raw", &self.raw)
            .field("value", &self.value)
            .finish()
    }
}

impl Deref for MemoryErrorOperationData {
    type Target = MemoryErrorOperation;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

/// # Memory Error - Error Operation
#[derive(Debug, PartialEq, Eq)]
pub enum MemoryErrorOperation {
    /// Other
    Other,
    /// Unknown
    Unknown,
    /// Read
    Read,
    /// Write
    Write,
    /// Partial write
    PartialWrite,
    /// A value unknown to this standard, check the raw value
    None,
}

impl From<u8> for MemoryErrorOperationData {
    fn from(raw: u8) -> Self {
        MemoryErrorOperationData {
            value: match raw {
                0x01 => MemoryErrorOperation::Other,
                0x02 => MemoryErrorOperation::Unknown,
                0x03 => MemoryErrorOperation::Read,
                0x04 => MemoryErrorOperation::Write,
                0x05 => MemoryErrorOperation::PartialWrite,
                _ => MemoryErrorOperation::None,
            },
            raw,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn unit_test() {
        let struct_type18 = vec![
            0x12, 0x17, 0x50, 0x00, 0x03, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
        ];

        let parts = UndefinedStruct::new(&struct_type18);
        let test_struct = SMBiosMemoryErrorInformation32::new(&parts);

        assert_eq!(*test_struct.error_type().unwrap(), MemoryErrorType::OK);
        assert_eq!(
            *test_struct.error_granularity().unwrap(),
            MemoryErrorGranularity::Unknown
        );
        assert_eq!(
            *test_struct.error_operation().unwrap(),
            MemoryErrorOperation::Unknown
        );
        assert_eq!(test_struct.vendor_syndrome(), Some(0));
        assert_eq!(test_struct.memory_array_error_address(), Some(0x8000_0000));
        assert_eq!(test_struct.device_error_address(), Some(0x8000_0000));
        assert_eq!(test_struct.error_resolution(), Some(0x8000_0000));
    }
}