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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use crate::{SMBiosStruct, UndefinedStruct};
use serde::{ser::SerializeStruct, Serialize, Serializer};
use std::{fmt, ops::Deref};
/// # Physical Memory Array (Type 16)
///
/// This structure describes a collection of memory devices that operate together to form a memory address space.
///
/// Compliant with:
/// DMTF SMBIOS Reference Specification 3.4.0 (DSP0134)
/// Document Date: 2020-07-17
pub struct SMBiosPhysicalMemoryArray<'a> {
    parts: &'a UndefinedStruct,
}

impl<'a> SMBiosStruct<'a> for SMBiosPhysicalMemoryArray<'a> {
    const STRUCT_TYPE: u8 = 16u8;

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

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

impl<'a> SMBiosPhysicalMemoryArray<'a> {
    /// Physical location of the Memory Array, whether on
    /// the system board or an add-in board
    pub fn location(&self) -> Option<MemoryArrayLocationData> {
        self.parts
            .get_field_byte(0x04)
            .map(|raw| MemoryArrayLocationData::from(raw))
    }

    /// Function for which the array is used
    pub fn usage(&self) -> Option<MemoryArrayUseData> {
        self.parts
            .get_field_byte(0x05)
            .map(|raw| MemoryArrayUseData::from(raw))
    }

    /// Primary hardware error correction or detection
    /// method supported by this memory array
    pub fn memory_error_correction(&self) -> Option<MemoryArrayErrorCorrectionData> {
        self.parts
            .get_field_byte(0x06)
            .map(|raw| MemoryArrayErrorCorrectionData::from(raw))
    }

    /// Maximum memory capacity, in kilobytes, for this array
    ///
    /// If the capacity is not represented in this field, then
    /// the 'extended_maximum_capacity' field should be used.
    ///
    /// Values 2 TB (8000 0000h) or greater must be represented
    /// in the Extended Maximum Capacity field.
    pub fn maximum_capacity(&self) -> Option<MaximumMemoryCapacity> {
        self.parts
            .get_field_dword(0x07)
            .map(|raw| MaximumMemoryCapacity::from(raw))
    }

    /// Handle, or instance number, associated with any
    /// error that was previously detected for the array
    ///
    /// If the system does not provide the error
    /// information structure, the field contains FFFEh;
    /// otherwise, the field contains either FFFFh (if no
    /// error was detected) or the handle of the errorinformation structure.
    pub fn memory_error_information_handle(&self) -> Option<u16> {
        self.parts.get_field_word(0x0B)
    }

    /// Number of slots or sockets available for [SMBiosMemoryDevice]s in this array
    ///
    /// This value represents the number of [SMBiosMemoryDevice]
    /// structures that compose this Memory
    /// Array. Each [SMBiosMemoryDevice] has a reference to
    /// the "owning" Memory Array.
    pub fn number_of_memory_devices(&self) -> Option<u16> {
        self.parts.get_field_word(0x0D)
    }

    /// Maximum memory capacity, in bytes, for this array
    ///
    /// This field is only valid when the Maximum
    /// Capacity field contains 8000 0000h. When
    /// Maximum Capacity contains a value that is not
    /// 8000 0000h, Extended Maximum Capacity must
    /// contain zeros.
    pub fn extended_maximum_capacity(&self) -> Option<u64> {
        self.parts.get_field_qword(0x0F)
    }
}

impl fmt::Debug for SMBiosPhysicalMemoryArray<'_> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<SMBiosPhysicalMemoryArray<'_>>())
            .field("header", &self.parts.header)
            .field("location", &self.location())
            .field("usage", &self.usage())
            .field("memory_error_correction", &self.memory_error_correction())
            .field("maximum_capacity", &self.maximum_capacity())
            .field(
                "memory_error_information_handle",
                &self.memory_error_information_handle(),
            )
            .field("number_of_memory_devices", &self.number_of_memory_devices())
            .field(
                "extended_maximum_capacity",
                &self.extended_maximum_capacity(),
            )
            .finish()
    }
}

impl Serialize for SMBiosPhysicalMemoryArray<'_> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("SMBiosPhysicalMemoryArray", 8)?;
        state.serialize_field("header", &self.parts.header)?;
        state.serialize_field("location", &self.location())?;
        state.serialize_field("usage", &self.usage())?;
        state.serialize_field("memory_error_correction", &self.memory_error_correction())?;
        state.serialize_field("maximum_capacity", &self.maximum_capacity())?;
        state.serialize_field(
            "memory_error_information_handle",
            &self.memory_error_information_handle(),
        )?;
        state.serialize_field("number_of_memory_devices", &self.number_of_memory_devices())?;
        state.serialize_field(
            "extended_maximum_capacity",
            &self.extended_maximum_capacity(),
        )?;
        state.end()
    }
}

/// # Memory Array - Location Data
pub struct MemoryArrayLocationData {
    /// 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 [MemoryArrayLocation] value
    pub value: MemoryArrayLocation,
}

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

impl Serialize for MemoryArrayLocationData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("MemoryArrayLocationData", 2)?;
        state.serialize_field("raw", &self.raw)?;
        state.serialize_field("value", &self.value)?;
        state.end()
    }
}

impl Deref for MemoryArrayLocationData {
    type Target = MemoryArrayLocation;

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

/// # Memory Array - Location
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum MemoryArrayLocation {
    /// Other
    Other = 0x01,
    /// Unknown
    Unknown = 0x02,
    /// System board or motherboard
    SystemBoardOrMotherboard = 0x03,
    /// ISA add-on card
    IsaAddOnCard = 0x04,
    /// EISA add-on card
    EisaAddOnCard = 0x05,
    /// PCI add-on card
    PciAddOnCard = 0x06,
    /// MCA add-on card
    McaAddOnCard = 0x07,
    /// PCMCIA add-on card
    PcmciaAddOnCard = 0x08,
    /// Proprietary add-on card
    ProprietaryAddOnCard = 0x09,
    /// NuBus
    NuBus = 0x0A,
    /// PC-98/C20 add-on card
    PC98C20AddOnCard = 0xA0,
    /// PC-98/C24 add-on card
    PC98C24AddOnCard = 0xA1,
    /// PC-98/E add-on card
    PC98EAddOnCard = 0xA2,
    /// PC-98/Local bus add-on card
    PC98LocalBusAddOnCard = 0xA3,
    /// CXL add-on card
    CxlFlexbus10AddOnCard = 0xA4,
    /// A value unknown to this standard, check the raw value
    None,
}

impl From<u8> for MemoryArrayLocationData {
    fn from(raw: u8) -> Self {
        MemoryArrayLocationData {
            value: match raw {
                0x01 => MemoryArrayLocation::Other,
                0x02 => MemoryArrayLocation::Unknown,
                0x03 => MemoryArrayLocation::SystemBoardOrMotherboard,
                0x04 => MemoryArrayLocation::IsaAddOnCard,
                0x05 => MemoryArrayLocation::EisaAddOnCard,
                0x06 => MemoryArrayLocation::PciAddOnCard,
                0x07 => MemoryArrayLocation::McaAddOnCard,
                0x08 => MemoryArrayLocation::PcmciaAddOnCard,
                0x09 => MemoryArrayLocation::ProprietaryAddOnCard,
                0x0A => MemoryArrayLocation::NuBus,
                0xA0 => MemoryArrayLocation::PC98C20AddOnCard,
                0xA1 => MemoryArrayLocation::PC98C24AddOnCard,
                0xA2 => MemoryArrayLocation::PC98EAddOnCard,
                0xA3 => MemoryArrayLocation::PC98LocalBusAddOnCard,
                0xA4 => MemoryArrayLocation::CxlFlexbus10AddOnCard,
                _ => MemoryArrayLocation::None,
            },
            raw,
        }
    }
}

/// # Memory Array - Use Data
pub struct MemoryArrayUseData {
    /// 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 [MemoryArrayUse] value
    pub value: MemoryArrayUse,
}

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

impl Serialize for MemoryArrayUseData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("MemoryArrayUseData", 2)?;
        state.serialize_field("raw", &self.raw)?;
        state.serialize_field("value", &self.value)?;
        state.end()
    }
}

impl Deref for MemoryArrayUseData {
    type Target = MemoryArrayUse;

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

/// # Memory Array - Use
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum MemoryArrayUse {
    /// Other
    Other,
    /// Unknown
    Unknown,
    /// System memory
    SystemMemory,
    /// Video memory
    VideoMemory,
    /// Flash memory
    FlashMemory,
    /// Non-volatile RAM
    NonVolatileRam,
    /// Cache memory
    CacheMemory,
    /// A value unknown to this standard, check the raw value
    None,
}

impl From<u8> for MemoryArrayUseData {
    fn from(raw: u8) -> Self {
        MemoryArrayUseData {
            value: match raw {
                0x01 => MemoryArrayUse::Other,
                0x02 => MemoryArrayUse::Unknown,
                0x03 => MemoryArrayUse::SystemMemory,
                0x04 => MemoryArrayUse::VideoMemory,
                0x05 => MemoryArrayUse::FlashMemory,
                0x06 => MemoryArrayUse::NonVolatileRam,
                0x07 => MemoryArrayUse::CacheMemory,
                _ => MemoryArrayUse::None,
            },
            raw,
        }
    }
}

/// # Memory Array - Error Correction Types Data
pub struct MemoryArrayErrorCorrectionData {
    /// 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 [MemoryArrayErrorCorrection] value
    pub value: MemoryArrayErrorCorrection,
}

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

impl Serialize for MemoryArrayErrorCorrectionData {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("MemoryArrayErrorCorrectionData", 2)?;
        state.serialize_field("raw", &self.raw)?;
        state.serialize_field("value", &self.value)?;
        state.end()
    }
}

impl Deref for MemoryArrayErrorCorrectionData {
    type Target = MemoryArrayErrorCorrection;

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

/// # Memory Array - Error Correction Types
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum MemoryArrayErrorCorrection {
    /// Other
    Other,
    /// Unknown
    Unknown,
    /// No Error Correction
    NoCorrection,
    /// Parity
    Parity,
    /// Single-bit ECC
    SingleBitEcc,
    /// Multi-bit ECC
    MultiBitEcc,
    /// CRC
    Crc,
    /// A value unknown to this standard, check the raw value
    None,
}

impl From<u8> for MemoryArrayErrorCorrectionData {
    fn from(raw: u8) -> Self {
        MemoryArrayErrorCorrectionData {
            value: match raw {
                0x01 => MemoryArrayErrorCorrection::Other,
                0x02 => MemoryArrayErrorCorrection::Unknown,
                0x03 => MemoryArrayErrorCorrection::NoCorrection,
                0x04 => MemoryArrayErrorCorrection::Parity,
                0x05 => MemoryArrayErrorCorrection::SingleBitEcc,
                0x06 => MemoryArrayErrorCorrection::MultiBitEcc,
                0x07 => MemoryArrayErrorCorrection::Crc,
                _ => MemoryArrayErrorCorrection::None,
            },
            raw,
        }
    }
}

/// # Maximum memory capacity, in kilobytes, for this array
#[derive(Serialize, Debug, PartialEq, Eq)]
pub enum MaximumMemoryCapacity {
    /// Maximum memory capacity in Kilobytes
    Kilobytes(u32),
    /// Use `extended_maximum_capacity` to retrieve the maximum capacity
    SeeExtendedMaximumCapacity,
}

impl From<u32> for MaximumMemoryCapacity {
    fn from(raw: u32) -> Self {
        match raw {
            0x8000_0000 => MaximumMemoryCapacity::SeeExtendedMaximumCapacity,
            _ => MaximumMemoryCapacity::Kilobytes(raw),
        }
    }
}

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

    #[test]
    fn unit_test() {
        let struct_type16 = vec![
            0x10, 0x17, 0x3E, 0x00, 0x03, 0x03, 0x05, 0x00, 0x00, 0x00, 0x60, 0xFE, 0xFF, 0x04,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ];

        let parts = UndefinedStruct::new(&struct_type16);
        let test_struct = SMBiosPhysicalMemoryArray::new(&parts);

        assert_eq!(
            *test_struct.location().unwrap(),
            MemoryArrayLocation::SystemBoardOrMotherboard
        );
        assert_eq!(*test_struct.usage().unwrap(), MemoryArrayUse::SystemMemory);
        assert_eq!(
            *test_struct.memory_error_correction().unwrap(),
            MemoryArrayErrorCorrection::SingleBitEcc
        );
        match test_struct.maximum_capacity().unwrap() {
            MaximumMemoryCapacity::Kilobytes(kb) => assert_eq!(kb, 0x6000_0000),
            MaximumMemoryCapacity::SeeExtendedMaximumCapacity => panic!("expected kb"),
        }
        assert_eq!(test_struct.memory_error_information_handle(), Some(65534));
        assert_eq!(test_struct.number_of_memory_devices(), Some(4));
        assert_eq!(test_struct.extended_maximum_capacity(), Some(0));
    }
}