dmidecode 1.0.1

Decode SMBIOS/DMI information into accessible data structures
Documentation
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Cache Information (Type 7)
//!
//! Information in this structure defines the attributes of CPU cache device in the system. One
//! structure is specified for each such device, whether the device is internal to or external to
//! the CPU module. Cache modules can be associated with a processor structure in one or two ways
//! depending on the SMBIOS version.

use core::fmt;

use bitflags::bitflags;

use crate::{MalformedStructureError, RawStructure};

/// The `Cache Information` 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 Cache<'buffer> {
    pub handle: u16,
    /// String number for reference designation EXAMPLE: “CACHE1”, 0
    pub socket_designation: &'buffer str,
    /// Cache Configuration
    pub cache_configuration: CacheConfiguration,
    /// Maximum size that can be installed
    pub maximum_cache_size: CacheSize,
    /// Same format as Max Cache Size field; set to 0 if no cache is installed
    pub installed_size: CacheSize,
    /// Supported SRAM Type
    pub supported_sram_type: CacheSramType,
    /// Current SRAM Type
    pub current_sram_type: CacheSramType,
    /// Cache module speed, in nanoseconds. The value is 0 if the speed is unknown.
    pub cache_speed: Option<u8>,
    /// Error-correction scheme supported by this cache component
    pub error_correction_type: Option<CacheErrorCorrectionType>,
    /// Logical type of cache
    pub system_cache_type: Option<SystemCacheType>,
    /// Associativity of the cache
    pub associativity: Option<CacheAssociativity>,
    /// If this field is present, for cache sizes of 2047 MB or smaller the value in the Max size
    /// in given granularity portion of the field equals the size given in the corresponding
    /// portion of the Maximum Cache Size field, and the Granularity bit matches the value of the
    /// Granularity bit in the Maximum Cache Size field.  For Cache sizes greater than 2047 MB, the
    /// Maximum Cache Size field is set to 0xFFFF and the Maximum Cache Size 2 field is present,
    /// the Granularity bit is set to 1b, and the size set as required;
    pub maximum_cache_size_2: Option<CacheSize2>,
    /// Same format as Maximum Cache Size 2 field; Absent or set to 0 if no cache is installed.
    pub installed_size_2: Option<CacheSize2>,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct CacheConfiguration {
    /// Cache Level – 1 through 8
    level: CacheLevel,
    /// Cache Socketed (e.g. Cache on a Stick)
    socketed: bool,
    /// Location, relative to the CPU module
    location: CacheLocation,
    /// Enabled/Disabled (at boot time)
    enabled_at_boot_time: bool,
    /// Operational Mode
    operational_mode: CacheOperationalMode,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum CacheLevel {
    L1,
    L2,
    L3,
    L4,
    L5,
    L6,
    L7,
    L8,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum CacheLocation {
    Internal,
    External,
    Reserved,
    Unknown,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum CacheOperationalMode {
    WriteThrough,
    WriteBack,
    ValuesWithMemoryAddress,
    Unknown,
}

/// Cache size is same for Maximum Cache Size and Installed Size
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum CacheSize {
    Granularity1K(u16),
    Granularity64K(u16),
}

bitflags! {
    /// Cache SRAM Type is same for Supported SRAM Type and Current SRAM Type
    #[derive(Debug, Clone, PartialEq, Eq, Hash)]
    pub struct CacheSramType: u16 {
        const OTHER             = 0b0000_0001;
        const UNKNOWN           = 0b0000_0010;
        const NONBURST          = 0b0000_0100;
        const BURST             = 0b0000_1000;
        const PIPELINE_BURST    = 0b0001_0000;
        const SYNCHRONOUS       = 0b0010_0000;
        const ASYNCHRONOUS      = 0b0100_0000;
    }
}

/// Error Correction Type field
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum CacheErrorCorrectionType {
    Other,
    Unknown,
    None,
    Parity,
    SingleBitEcc,
    MultiBitEcc,
    Undefined(u8),
}

/// The cache type for a cache level (L1, L2, L3, ...) is type 03h (Instruction) when all the
/// caches at that level are Instruction caches. The cache type for a specific cache level (L1, L2,
/// L3, ...) is type 04h (Data) when all the caches at that level are Data caches. The cache type
/// for a cache level (L1, L2, L3, ...) is type 05h (Unified) when the caches at that level are a
/// mix of Instruction and Data caches.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum SystemCacheType {
    Other,
    Unknown,
    Instruction,
    Data,
    Unified,
    Undefined(u8),
}

/// Associativity field
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum CacheAssociativity {
    Other,
    Unknown,
    DirectMapped,
    TwowaySetAssociative,
    FourWaySetAssociative,
    FullyAssociative,
    EightWaySetAssociative,
    SixteenWaySetAssociative,
    TwelveWaySetAssociative,
    TwentyFourWaySetAssociative,
    ThirtyTwoWaySetAssociative,
    FourtyEightWaySetAssociative,
    SixtyFourWaySetAssociative,
    TwentyWaySetAssociative,
    Undefined(u8),
}

/// Cache size is same for Maximum Cache Size and Installed Size
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum CacheSize2 {
    Granularity1K(u32),
    Granularity64K(u32),
}

impl<'buffer> Cache<'buffer> {
    pub(crate) fn try_from(structure: RawStructure<'buffer>) -> Result<Cache<'buffer>, MalformedStructureError> {
        #[repr(C)]
        #[repr(packed)]
        struct CachePacked3_1 {
            socket_designation: u8,
            cache_configuration: u16,
            maximum_cache_size: u16,
            installed_size: u16,
            supported_sram_type: u16,
            current_sram_type: u16,
            cache_speed: u8,
            error_correction_type: u8,
            system_cache_type: u8,
            associativity: u8,
            maximum_cache_size_2: u32,
            installed_size_2: u32,
        }

        #[repr(C)]
        #[repr(packed)]
        struct CachePacked2_1 {
            socket_designation: u8,
            cache_configuration: u16,
            maximum_cache_size: u16,
            installed_size: u16,
            supported_sram_type: u16,
            current_sram_type: u16,
            cache_speed: u8,
            error_correction_type: u8,
            system_cache_type: u8,
            associativity: u8,
        }

        #[repr(C)]
        #[repr(packed)]
        struct CachePacked2_0 {
            socket_designation: u8,
            cache_configuration: u16,
            maximum_cache_size: u16,
            installed_size: u16,
            supported_sram_type: u16,
            current_sram_type: u16,
        }

        match structure.version {
            v if v > (3, 1).into() => {
                let_as_struct!(packed, CachePacked3_1, structure.data);
                Ok(Cache {
                    handle: structure.handle,
                    socket_designation: structure.find_string(packed.socket_designation)?,
                    cache_configuration: u16::from_le(packed.cache_configuration).into(),
                    maximum_cache_size: u16::from_le(packed.maximum_cache_size).into(),
                    installed_size: u16::from_le(packed.installed_size).into(),
                    supported_sram_type: CacheSramType::from_bits_truncate(u16::from_le(packed.supported_sram_type)),
                    current_sram_type: CacheSramType::from_bits_truncate(u16::from_le(packed.current_sram_type)),
                    cache_speed: Some(packed.cache_speed),
                    error_correction_type: Some(packed.error_correction_type.into()),
                    system_cache_type: Some(packed.system_cache_type.into()),
                    associativity: Some(packed.associativity.into()),
                    maximum_cache_size_2: Some(u32::from_le(packed.maximum_cache_size_2).into()),
                    installed_size_2: Some(u32::from_le(packed.installed_size_2).into()),
                })
            }
            v if v > (2, 1).into() => {
                let_as_struct!(packed, CachePacked2_1, structure.data);
                Ok(Cache {
                    handle: structure.handle,
                    socket_designation: structure.find_string(packed.socket_designation)?,
                    cache_configuration: u16::from_le(packed.cache_configuration).into(),
                    maximum_cache_size: u16::from_le(packed.maximum_cache_size).into(),
                    installed_size: u16::from_le(packed.installed_size).into(),
                    supported_sram_type: CacheSramType::from_bits_truncate(u16::from_le(packed.supported_sram_type)),
                    current_sram_type: CacheSramType::from_bits_truncate(u16::from_le(packed.current_sram_type)),
                    cache_speed: Some(packed.cache_speed),
                    error_correction_type: Some(packed.error_correction_type.into()),
                    system_cache_type: Some(packed.system_cache_type.into()),
                    associativity: Some(packed.associativity.into()),
                    maximum_cache_size_2: None,
                    installed_size_2: None,
                })
            }
            v if v > (2, 0).into() => {
                let_as_struct!(packed, CachePacked2_0, structure.data);
                Ok(Cache {
                    handle: structure.handle,
                    socket_designation: structure.find_string(packed.socket_designation)?,
                    cache_configuration: u16::from_le(packed.cache_configuration).into(),
                    maximum_cache_size: u16::from_le(packed.maximum_cache_size).into(),
                    installed_size: u16::from_le(packed.installed_size).into(),
                    supported_sram_type: CacheSramType::from_bits_truncate(u16::from_le(packed.supported_sram_type)),
                    current_sram_type: CacheSramType::from_bits_truncate(u16::from_le(packed.current_sram_type)),
                    cache_speed: None,
                    error_correction_type: None,
                    system_cache_type: None,
                    associativity: None,
                    maximum_cache_size_2: None,
                    installed_size_2: None,
                })
            }
            _ => unreachable!(),
        }
    }
}

impl From<u16> for CacheConfiguration {
    fn from(word: u16) -> CacheConfiguration {
        CacheConfiguration {
            level: CacheLevel::from(word & 0b0000_0111),
            socketed: (word & 0b0000_1000) >> 3 == 1,
            location: CacheLocation::from((word & 0b0110_0000) >> 5),
            enabled_at_boot_time: (word & 0b1000_0000) >> 7 == 1,
            operational_mode: CacheOperationalMode::from((word & 0b0000_0011_0000_0000) >> 8),
        }
    }
}

impl From<u16> for CacheSize {
    fn from(word: u16) -> CacheSize {
        let val = word & (!(1 << 15));
        if word & (1 << 15) == 0 {
            CacheSize::Granularity1K(val)
        } else {
            CacheSize::Granularity64K(val)
        }
    }
}
impl CacheSize {
    pub fn bytes(&self) -> u64 {
        match &self {
            Self::Granularity1K(val) => (*val as u64) * (1 << 10),
            Self::Granularity64K(val) => (*val as u64) * (1 << 16),
        }
    }
}

impl From<u16> for CacheLevel {
    fn from(word: u16) -> CacheLevel {
        match word {
            0 => CacheLevel::L1,
            1 => CacheLevel::L2,
            2 => CacheLevel::L3,
            3 => CacheLevel::L4,
            4 => CacheLevel::L5,
            5 => CacheLevel::L6,
            6 => CacheLevel::L7,
            7 => CacheLevel::L8,
            _ => unreachable!(),
        }
    }
}
impl fmt::Display for CacheLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::L1 => write!(f, "L1"),
            Self::L2 => write!(f, "L2"),
            Self::L3 => write!(f, "L3"),
            Self::L4 => write!(f, "L4"),
            Self::L5 => write!(f, "L5"),
            Self::L6 => write!(f, "L6"),
            Self::L7 => write!(f, "L7"),
            Self::L8 => write!(f, "L8"),
        }
    }
}

impl From<u16> for CacheLocation {
    fn from(word: u16) -> CacheLocation {
        match word {
            0 => CacheLocation::Internal,
            1 => CacheLocation::External,
            2 => CacheLocation::Reserved,
            3 => CacheLocation::Unknown,
            _ => unreachable!(),
        }
    }
}
impl fmt::Display for CacheLocation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Internal => write!(f, "Internal"),
            Self::External => write!(f, "External"),
            Self::Reserved => write!(f, "Reserved"),
            Self::Unknown => write!(f, "Unknown"),
        }
    }
}

impl From<u16> for CacheOperationalMode {
    fn from(word: u16) -> CacheOperationalMode {
        match word {
            0 => CacheOperationalMode::WriteThrough,
            1 => CacheOperationalMode::WriteBack,
            2 => CacheOperationalMode::ValuesWithMemoryAddress,
            3 => CacheOperationalMode::Unknown,
            _ => unreachable!(),
        }
    }
}
impl fmt::Display for CacheOperationalMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::WriteThrough => write!(f, "Write Through"),
            Self::WriteBack => write!(f, "Write Back"),
            Self::ValuesWithMemoryAddress => write!(f, "Values with Memory Address"),
            Self::Unknown => write!(f, "Unknown"),
        }
    }
}

impl From<u8> for CacheErrorCorrectionType {
    fn from(byte: u8) -> CacheErrorCorrectionType {
        match byte {
            0x01 => CacheErrorCorrectionType::Other,
            0x02 => CacheErrorCorrectionType::Unknown,
            0x03 => CacheErrorCorrectionType::None,
            0x04 => CacheErrorCorrectionType::Parity,
            0x05 => CacheErrorCorrectionType::SingleBitEcc,
            0x06 => CacheErrorCorrectionType::MultiBitEcc,
            t => CacheErrorCorrectionType::Undefined(t),
        }
    }
}
impl fmt::Display for CacheErrorCorrectionType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other => write!(f, "Other"),
            Self::Unknown => write!(f, "Unknown"),
            Self::None => write!(f, "None"),
            Self::Parity => write!(f, "Parity"),
            Self::SingleBitEcc => write!(f, "Single-bit ECC"),
            Self::MultiBitEcc => write!(f, "Multi-bit ECC"),
            Self::Undefined(t) => write!(f, "Undefined: {t}"),
        }
    }
}

impl From<u8> for SystemCacheType {
    fn from(byte: u8) -> SystemCacheType {
        match byte {
            0x01 => SystemCacheType::Other,
            0x02 => SystemCacheType::Unknown,
            0x03 => SystemCacheType::Instruction,
            0x04 => SystemCacheType::Data,
            0x05 => SystemCacheType::Unified,
            t => SystemCacheType::Undefined(t),
        }
    }
}
impl fmt::Display for SystemCacheType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other => write!(f, "Other"),
            Self::Unknown => write!(f, "Unknown"),
            Self::Instruction => write!(f, "Instruction"),
            Self::Data => write!(f, "Data"),
            Self::Unified => write!(f, "Unified"),
            Self::Undefined(t) => write!(f, "Undefined: {t}"),
        }
    }
}

impl From<u8> for CacheAssociativity {
    fn from(byte: u8) -> CacheAssociativity {
        match byte {
            0x01 => CacheAssociativity::Other,
            0x02 => CacheAssociativity::Unknown,
            0x03 => CacheAssociativity::DirectMapped,
            0x04 => CacheAssociativity::TwowaySetAssociative,
            0x05 => CacheAssociativity::FourWaySetAssociative,
            0x06 => CacheAssociativity::FullyAssociative,
            0x07 => CacheAssociativity::EightWaySetAssociative,
            0x08 => CacheAssociativity::SixteenWaySetAssociative,
            0x09 => CacheAssociativity::TwelveWaySetAssociative,
            0x0A => CacheAssociativity::TwentyFourWaySetAssociative,
            0x0B => CacheAssociativity::ThirtyTwoWaySetAssociative,
            0x0C => CacheAssociativity::FourtyEightWaySetAssociative,
            0x0D => CacheAssociativity::SixtyFourWaySetAssociative,
            0x0E => CacheAssociativity::TwentyWaySetAssociative,
            t => CacheAssociativity::Undefined(t),
        }
    }
}
impl fmt::Display for CacheAssociativity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other => write!(f, "Other"),
            Self::Unknown => write!(f, "Unknown"),
            Self::DirectMapped => write!(f, "Direct Mapped"),
            Self::TwowaySetAssociative => write!(f, "2-way Set-Associative"),
            Self::FourWaySetAssociative => write!(f, "4-way Set-Associative"),
            Self::FullyAssociative => write!(f, "Fully Associative"),
            Self::EightWaySetAssociative => write!(f, "8-way Set-Associative"),
            Self::SixteenWaySetAssociative => write!(f, "16-way Set-Associative"),
            Self::TwelveWaySetAssociative => write!(f, "12-way Set-Associative"),
            Self::TwentyFourWaySetAssociative => write!(f, "24-way Set-Associative"),
            Self::ThirtyTwoWaySetAssociative => write!(f, "32-way Set-Associative"),
            Self::FourtyEightWaySetAssociative => write!(f, "48-way Set-Associative"),
            Self::SixtyFourWaySetAssociative => write!(f, "64-way Set-Associative"),
            Self::TwentyWaySetAssociative => write!(f, "20-way Set-Associative"),
            Self::Undefined(t) => write!(f, "Undefined: {t}"),
        }
    }
}

impl From<u32> for CacheSize2 {
    fn from(dword: u32) -> CacheSize2 {
        let val = dword & (!(1 << 31));
        if dword & (1 << 31) == 0 {
            CacheSize2::Granularity1K(val)
        } else {
            CacheSize2::Granularity64K(val)
        }
    }
}
impl CacheSize2 {
    pub fn bytes(&self) -> u64 {
        match &self {
            Self::Granularity1K(val) => (*val as u64) * (1 << 10),
            Self::Granularity64K(val) => (*val as u64) * (1 << 16),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn cache_configuration() {
        let data = 0b0000_0010_1010_1010;
        let sample = CacheConfiguration {
            level: CacheLevel::L3,
            socketed: true,
            location: CacheLocation::External,
            enabled_at_boot_time: true,
            operational_mode: CacheOperationalMode::ValuesWithMemoryAddress,
        };
        let result: CacheConfiguration = data.into();
        assert_eq!(sample, result);
    }
    #[test]
    fn cache_size() {
        let data = [0b0000_0010_1010_1010, 0b1000_0010_1010_1010];
        let cs_1k = CacheSize::from(data[0]);
        let cs_64k = CacheSize::from(data[1]);
        let cs2_1k = CacheSize2::from((data[0] as u32) << 16);
        let cs2_64k = CacheSize2::from((data[1] as u32) << 16);
        assert_eq!(CacheSize::Granularity1K(682), cs_1k);
        assert_eq!(682 * 1024, cs_1k.bytes());
        assert_eq!(CacheSize::Granularity64K(682), cs_64k);
        assert_eq!(682 * 65536, cs_64k.bytes());
        assert_eq!(CacheSize2::Granularity1K(44695552), cs2_1k);
        assert_eq!(44695552 * 1024, cs2_1k.bytes());
        assert_eq!(CacheSize2::Granularity64K(44695552), cs2_64k);
        assert_eq!(44695552 * 65536, cs2_64k.bytes());
    }
    #[test]
    fn cache_enums() {
        let data = 0b0101_0101;
        let sram = CacheSramType::from_bits_truncate(data);
        assert!(sram.contains(CacheSramType::OTHER));
        assert!(sram.contains(CacheSramType::NONBURST));
        assert!(sram.contains(CacheSramType::PIPELINE_BURST));
        assert!(sram.contains(CacheSramType::ASYNCHRONOUS));
        assert_eq!(CacheErrorCorrectionType::Undefined(85), (data as u8).into());
        assert_eq!(CacheErrorCorrectionType::SingleBitEcc, ((data & 0b111) as u8).into());
        assert_eq!(SystemCacheType::Undefined(85), (data as u8).into());
        assert_eq!(SystemCacheType::Unified, ((data & 0b111) as u8).into());
        assert_eq!(CacheAssociativity::Undefined(85), (data as u8).into());
        assert_eq!(
            CacheAssociativity::FourWaySetAssociative,
            ((data & 0b1111) as u8).into()
        );
    }
}