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
use std::{
    convert::TryFrom, convert::TryInto, fmt, fs::read, io::Error, io::ErrorKind, num::Wrapping,
};

/// # SMBIOS 2.1 (32 bit) Entry Point structure
///
/// On non-UEFI systems, the 32-bit SMBIOS Entry Point structure, can be located by application software
/// by searching for the anchor-string on paragraph (16-byte) boundaries within the physical memory address
/// range 000F0000h to 000FFFFFh. This entry point encapsulates an intermediate anchor string that is used
/// by some existing DMI browsers.
///
/// On UEFI-based systems, the SMBIOS Entry Point structure can be located by looking in the EFI
/// Configuration Table for the SMBIOS GUID (SMBIOS_TABLE_GUID, {EB9D2D31-2D88-11D3-9A16-
/// 0090273FC14D}) and using the associated pointer. See section 4.6 of the UEFI Specification for details.
/// See section 2.3 of the UEFI Specification for how to report the containing memory type.
pub struct SMBiosEntryPoint32 {
    raw: Vec<u8>,
}

impl<'a> SMBiosEntryPoint32 {
    /// Minimum acceptable size of this structure
    ///
    /// TODO: Review DMTF SMBIOS document history and see
    /// if structure sizes smaller than 0x1F existed.  If
    /// so then change this structure design to return Option<>
    /// values and adjust this size accordingly.
    pub const MINIMUM_SIZE: usize = 0x1F;

    /// Anchor String "_SM_" (offset 0)
    pub const SM_ANCHOR: [u8; 4] = [b'_', b'S', b'M', b'_'];

    /// Anchor String "_DMI_" (offset 0x10)
    pub const DMI_ANCHOR: [u8; 5] = [b'_', b'D', b'M', b'I', b'_'];

    /// Entry Point Structure Checksum Offset
    pub const ENTRY_POINT_STRUCTURE_CHECKSUM_OFFSET: usize = 0x04;

    /// Entry Point Length Offset
    pub const ENTRY_POINT_LENGTH_OFFSET: usize = 0x05;

    /// SMBIOS Major Version Offset
    pub const MAJOR_VERSION_OFFSET: usize = 0x06;

    /// SMBIOS Minor Version Offset
    pub const MINOR_VERSION_OFFSET: usize = 0x07;

    /// Maximum Structure Size Offset
    pub const MAXIMUM_STRUCTURE_SIZE_OFFSET: usize = 0x08;

    /// Entry Point Revision Offset
    pub const ENTRY_POINT_REVISION_OFFSET: usize = 0x0A;

    /// Formatted Area Offset
    pub const FORMATTED_AREA_OFFSET: usize = 0x0B;

    /// Intermediate Anchor String Offset
    ///
    /// NOTE: This field is paragraph-aligned, to allow legacy DMI browsers to
    /// find this entry point within the SMBIOS Entry Point Structure.
    pub const INTERMEDIATE_ANCHOR_OFFSET: usize = 0x10;

    /// Intermediate Checksum Offset
    pub const INTERMEDIATE_CHECKSUM_OFFSET: usize = 0x15;

    /// Structure Table Length Offset
    pub const STRUCTURE_TABLE_LENGTH_OFFSET: usize = 0x16;

    /// Structure Table Address Offset
    pub const STRUCTURE_TABLE_ADDRESS_OFFSET: usize = 0x18;

    /// Number of SMBIOS Structures Offset
    pub const NUMBER_OF_SMBIOS_STRUCTURES_OFFSET: usize = 0x1C;

    /// SMBIOS BCD Revision Offset
    pub const BCD_REVISION: usize = 0x1E;

    /// Entry Point Structure Checksum
    ///
    /// Checksum of the Entry Point Structure (EPS)
    ///
    /// This value, when added to all other bytes in the EPS, results in
    /// the value 00h (using 8-bit addition calculations). Values in the
    /// EPS are summed starting at offset 00h, for `entry_point_length`
    /// bytes.
    pub fn entry_point_structure_checksum(&self) -> u8 {
        self.raw[Self::ENTRY_POINT_STRUCTURE_CHECKSUM_OFFSET]
    }

    /// Entry Point Length
    ///
    /// Length of the Entry Point Structure, starting with the Anchor String
    /// field, in bytes, currently 1Fh
    ///
    /// NOTE: This value was incorrectly stated in version 2.1 of this specification
    /// as 1Eh. Because of this, there might be version 2.1
    /// implementations that use either the 1Eh or the 1Fh value, but
    /// version 2.2 or later implementations must use the 1Fh value.
    pub fn entry_point_length(&self) -> u8 {
        self.raw[Self::ENTRY_POINT_LENGTH_OFFSET]
    }

    /// SMBIOS Major Version
    ///
    /// Major version of this specification implemented in the table
    /// structures (for example, the value is 0Ah (10) for revision 10.22 and
    /// 02h for revision 2.1)
    pub fn major_version(&self) -> u8 {
        self.raw[Self::MAJOR_VERSION_OFFSET]
    }

    /// SMBIOS Minor Version
    ///
    /// Minor version of this specification implemented in the table
    /// structures (for example, the value is 16h (22) for revision 10.22 and
    /// 01h for revision 2.1)
    pub fn minor_version(&self) -> u8 {
        self.raw[Self::MINOR_VERSION_OFFSET]
    }

    /// Maximum Structure Size
    ///
    /// Size of the largest SMBIOS structure, in bytes, and encompasses
    /// the structure’s formatted area and text strings
    pub fn maximum_structure_size(&self) -> u16 {
        u16::from_le_bytes(
            self.raw[Self::MAXIMUM_STRUCTURE_SIZE_OFFSET..Self::MAXIMUM_STRUCTURE_SIZE_OFFSET + 2]
                .try_into()
                .expect("u16 is 2 bytes"),
        )
    }

    /// Entry Point Revision
    ///
    /// EPS revision implemented in this structure and identifies the
    /// formatting of offsets 0Bh to 0Fh as follows:
    /// - 00h Entry Point is based on SMBIOS 2.1 definition; formatted area is reserved and set to all 00h.
    /// - 01h-FFh Reserved for assignment by this specification
    pub fn entry_point_revision(&self) -> u8 {
        self.raw[Self::ENTRY_POINT_REVISION_OFFSET]
    }

    /// Formatted Area
    ///
    /// Value present in the `entry_point_revision` field defines the
    /// interpretation to be placed upon these 5 bytes
    pub fn formatted_area(&self) -> [u8; 5] {
        self.raw[Self::FORMATTED_AREA_OFFSET..Self::FORMATTED_AREA_OFFSET + 5]
            .try_into()
            .expect("5 bytes")
    }

    /// Intermediate Anchor String
    ///
    /// _DMI_, specified as five ASCII characters (5F 44 4D 49 5F).
    pub fn intermediate_anchor(&self) -> [u8; 5] {
        self.raw[Self::INTERMEDIATE_ANCHOR_OFFSET..Self::INTERMEDIATE_ANCHOR_OFFSET + 5]
            .try_into()
            .expect("5 bytes")
    }

    /// Intermediate Checksum
    ///
    /// Checksum of Intermediate Entry Point Structure (IEPS).
    ///
    /// This value, when added to all other bytes in the IEPS, results in
    /// the value 00h (using 8-bit addition calculations). Values in the
    /// IEPS are summed starting at offset 10h, for 0Fh bytes.
    pub fn intermediate_checksum(&self) -> u8 {
        self.raw[Self::INTERMEDIATE_CHECKSUM_OFFSET]
    }

    /// Structure Table Length
    ///
    /// Total length of SMBIOS Structure Table, pointed to by the
    /// `structure_table_address`, in bytes
    pub fn structure_table_length(&self) -> u16 {
        u16::from_le_bytes(
            self.raw[Self::STRUCTURE_TABLE_LENGTH_OFFSET..Self::STRUCTURE_TABLE_LENGTH_OFFSET + 2]
                .try_into()
                .expect("u16 is 2 bytes"),
        )
    }

    /// Structure Table Address
    ///
    /// 32-bit physical starting address of the read-only SMBIOS
    /// Structure Table, which can start at any 32-bit address
    /// This area contains all of the SMBIOS structures fully packed
    /// together. These structures can then be parsed to produce exactly
    /// the same format as that returned from a Get SMBIOS Structure
    /// function call.
    pub fn structure_table_address(&self) -> u32 {
        u32::from_le_bytes(
            self.raw
                [Self::STRUCTURE_TABLE_ADDRESS_OFFSET..Self::STRUCTURE_TABLE_ADDRESS_OFFSET + 4]
                .try_into()
                .expect("u32 is 4 bytes"),
        )
    }

    /// Number of SMBIOS Structures
    ///
    /// Total number of structures present in the SMBIOS Structure Table
    /// This is the value returned as NumStructures from the Get
    /// SMBIOS Information function.
    pub fn number_of_smbios_structures(&self) -> u16 {
        u16::from_le_bytes(
            self.raw[Self::NUMBER_OF_SMBIOS_STRUCTURES_OFFSET
                ..Self::NUMBER_OF_SMBIOS_STRUCTURES_OFFSET + 2]
                .try_into()
                .expect("u16 is 2 bytes"),
        )
    }

    /// SMBIOS BCD Revision
    ///
    /// Indicates compliance with a revision of this specification
    /// It is a BCD value where the upper nibble indicates the major
    /// version and the lower nibble the minor version. For revision 2.1,
    /// the returned value is 21h. If the value is 00h, only the Major and
    /// Minor Versions in offsets 6 and 7 of the Entry Point Structure
    /// provide the version information.
    pub fn bcd_revision(&self) -> u8 {
        self.raw[Self::BCD_REVISION]
    }

    /// Load this structure from a file
    pub fn try_load_from_file(filename: &str) -> Result<Self, Error> {
        read(filename)?.try_into()
    }
}

impl<'a> TryFrom<Vec<u8>> for SMBiosEntryPoint32 {
    type Error = Error;

    fn try_from(raw: Vec<u8>) -> Result<Self, Self::Error> {
        if raw.len() < Self::MINIMUM_SIZE {
            return Err(Error::new(
                ErrorKind::InvalidData,
                "Slice is smaller than SMBiosEntryPoint32::MINIMUM_SIZE",
            ));
        }

        if !raw
            .iter()
            .zip(Self::SM_ANCHOR.iter())
            .all(|pair| pair.0 == pair.1)
        {
            return Err(Error::new(ErrorKind::InvalidData, "_SM_ anchor not found"));
        }

        // Verify the EPS checksum
        // The checksum is calculated for a length of `entry_point_length`
        let entry_point_length = raw[Self::ENTRY_POINT_LENGTH_OFFSET] as usize;
        match raw.get(0..entry_point_length) {
            Some(checked_bytes) => {
                if !verify_checksum(checked_bytes) {
                    return Err(Error::new(
                ErrorKind::InvalidData,"Entry Point Structure checksum verification failed"));
                }
            }
            None => return Err(Error::new(
                ErrorKind::InvalidData,"The Entry Point Length field specified a value which exceeded the bounds of the Entry Point Structure")),
        }

        let intermediate_anchor: [u8; 5] = raw
            [Self::INTERMEDIATE_ANCHOR_OFFSET..Self::INTERMEDIATE_ANCHOR_OFFSET + 5]
            .try_into()
            .expect("5 bytes");

        if !intermediate_anchor
            .iter()
            .zip(Self::DMI_ANCHOR.iter())
            .all(|pair| pair.0 == pair.1)
        {
            return Err(Error::new(ErrorKind::InvalidData, "_DMI_ anchor not found"));
        }

        // Verify the IEPS checksum
        // The checksum is calculated for a length of 0x0F
        let intermediate_entry_point_structure: [u8; 0x0F] = raw
            [Self::INTERMEDIATE_ANCHOR_OFFSET..]
            .try_into()
            .expect("0x0F bytes");

        if !verify_checksum(&intermediate_entry_point_structure) {
            return Err(Error::new(
                ErrorKind::InvalidData,
                "Intermediate entry point structure checksum verification failed",
            ));
        }

        Ok(SMBiosEntryPoint32 { raw })
    }
}

impl fmt::Debug for SMBiosEntryPoint32 {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<SMBiosEntryPoint32>())
            .field(
                "entry_point_structure_checksum",
                &self.entry_point_structure_checksum(),
            )
            .field("entry_point_length", &self.entry_point_length())
            .field("major_version", &self.major_version())
            .field("minor_version", &self.minor_version())
            .field("maximum_structure_size", &self.maximum_structure_size())
            .field("entry_point_revision", &self.entry_point_revision())
            .field("formatted_area", &self.formatted_area())
            .field("intermediate_anchor", &self.intermediate_anchor())
            .field("intermediate_checksum", &self.intermediate_checksum())
            .field("structure_table_length", &self.structure_table_length())
            .field("structure_table_address", &self.structure_table_address())
            .field(
                "number_of_smbios_structures",
                &self.number_of_smbios_structures(),
            )
            .field("bcd_revision", &self.bcd_revision())
            .finish()
    }
}

/// # SMBIOS 3.0 (64 bit) Entry Point structure
///
/// On non-UEFI systems, the 64-bit SMBIOS Entry Point structure can be located by application software by
/// searching for the anchor-string on paragraph (16-byte) boundaries within the physical memory address
/// range 000F0000h to 000FFFFFh.
///
/// On UEFI-based systems, the SMBIOS Entry Point structure can be located by looking in the EFI
/// Configuration Table for the SMBIOS 3.x GUID (SMBIOS3_TABLE_GUID, {F2FD1544-9794-4A2C-992E836 E5BBCF20E394}) and using the associated pointer. See section 4.6 of the UEFI Specification for details.
/// See section 2.3 of the UEFI Specification for how to report the containing memory type.
pub struct SMBiosEntryPoint64 {
    raw: Vec<u8>,
}

impl<'a> SMBiosEntryPoint64 {
    /// Minimum acceptable size of this structure
    ///
    /// TODO: Review DMTF SMBIOS document history and see
    /// if structure sizes smaller than 0x18 existed.  If
    /// so then change this structure design to return Option<>
    /// values and adjust this size accordingly.
    pub const MINIMUM_SIZE: usize = 0x18;

    /// Anchor String "_SM3_" (offset 0)
    pub const SM3_ANCHOR: [u8; 5] = [b'_', b'S', b'M', b'3', b'_'];

    /// Entry Point Structure Checksum Offset
    pub const ENTRY_POINT_STRUCTURE_CHECKSUM_OFFSET: usize = 0x05;

    /// Entry Point Length Offset
    pub const ENTRY_POINT_LENGTH_OFFSET: usize = 0x06;

    /// SMBIOS Major Version Offset
    pub const MAJOR_VERSION_OFFSET: usize = 0x07;

    /// SMBIOS Minor Version Offset
    pub const MINOR_VERSION_OFFSET: usize = 0x08;

    /// SMBIOS Docrev
    pub const DOCREV_OFFSET: usize = 0x09;

    /// Entry Point Revision Offset
    pub const ENTRY_POINT_REVISION_OFFSET: usize = 0x0A;

    /// Structure Table Maximum Size Offset
    pub const STRUCTURE_TABLE_MAXIMUM_SIZE_OFFSET: usize = 0x0C;

    /// Structure Table Address Offset
    pub const STRUCTURE_TABLE_ADDRESS_OFFSET: usize = 0x10;

    /// Entry Point Structure Checksum
    ///
    /// Checksum of the Entry Point Structure (EPS)
    ///
    /// This value, when added to all other bytes in the EPS, results in
    /// the value 00h (using 8-bit addition calculations). Values in the
    /// EPS are summed starting at offset 00h, for `entry_point_length`
    /// bytes.
    pub fn entry_point_structure_checksum(&self) -> u8 {
        self.raw[Self::ENTRY_POINT_STRUCTURE_CHECKSUM_OFFSET]
    }

    /// Entry Point Length
    ///
    /// Length of the Entry Point Structure, starting with the Anchor String
    /// field, in bytes, currently 18h
    pub fn entry_point_length(&self) -> u8 {
        self.raw[Self::ENTRY_POINT_LENGTH_OFFSET]
    }

    /// SMBIOS Major Version
    ///
    /// Major version of this specification implemented in the table
    /// structures (for example, the value is 0Ah (10) for revision 10.22 and
    /// 02h for revision 2.1)
    pub fn major_version(&self) -> u8 {
        self.raw[Self::MAJOR_VERSION_OFFSET]
    }

    /// SMBIOS Minor Version
    ///
    /// Minor version of this specification implemented in the table
    /// structures (for example, the value is 16h (22) for revision 10.22 and
    /// 01h for revision 2.1)
    pub fn minor_version(&self) -> u8 {
        self.raw[Self::MINOR_VERSION_OFFSET]
    }

    /// SMBIOS Docrev
    ///
    /// Identifies the docrev of this specification implemented in the table
    /// structures (for example, the value is 00h for revision 10.22.0 and
    /// 01h for revision 2.7.1).
    pub fn docrev(&self) -> u8 {
        self.raw[Self::DOCREV_OFFSET]
    }

    /// Entry Point Revision
    ///
    /// EPS revision implemented in this structure and identifies the
    /// formatting of offsets 0Bh and beyond as follows:
    /// - 00h Reserved for assignment by this specification
    /// - 01h Entry Point is based on SMBIOS 3.0 definition;
    /// - 02h-FFh Reserved for assignment by this specification; offsets 0Ch-17h are defined per revision 01h
    pub fn entry_point_revision(&self) -> u8 {
        self.raw[Self::ENTRY_POINT_REVISION_OFFSET]
    }

    /// Structure Table Maximum Size
    ///
    /// Maximum size of SMBIOS Structure Table, pointed to by the
    /// Structure Table Address, in bytes. The actual size is guaranteed
    /// to be less or equal to the maximum size.
    pub fn structure_table_maximum_size(&self) -> u32 {
        u32::from_le_bytes(
            self.raw[Self::STRUCTURE_TABLE_MAXIMUM_SIZE_OFFSET
                ..Self::STRUCTURE_TABLE_MAXIMUM_SIZE_OFFSET + 4]
                .try_into()
                .expect("u32 is 4 bytes"),
        )
    }

    /// Structure Table Address
    ///
    /// The 64-bit physical starting address of the read-only SMBIOS
    /// Structure Table, which can start at any 64-bit address. This area
    /// contains all of the SMBIOS structures fully packed together
    pub fn structure_table_address(&self) -> u64 {
        u64::from_le_bytes(
            self.raw
                [Self::STRUCTURE_TABLE_ADDRESS_OFFSET..Self::STRUCTURE_TABLE_ADDRESS_OFFSET + 4]
                .try_into()
                .expect("u64 is 8 bytes"),
        )
    }

    /// Load this structure from a file
    pub fn try_load_from_file(filename: &str) -> Result<Self, Error> {
        read(filename)?.try_into()
    }
}

impl fmt::Debug for SMBiosEntryPoint64 {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<SMBiosEntryPoint64>())
            .field(
                "entry_point_structure_checksum",
                &self.entry_point_structure_checksum(),
            )
            .field("entry_point_length", &self.entry_point_length())
            .field("major_version", &self.major_version())
            .field("minor_version", &self.minor_version())
            .field("docrev", &self.docrev())
            .field(
                "structure_table_maximum_size",
                &self.structure_table_maximum_size(),
            )
            .field("structure_table_address", &self.structure_table_address())
            .finish()
    }
}

impl<'a> TryFrom<Vec<u8>> for SMBiosEntryPoint64 {
    type Error = Error;

    fn try_from(raw: Vec<u8>) -> Result<Self, Self::Error> {
        if raw.len() < Self::MINIMUM_SIZE {
            return Err(Error::new(
                ErrorKind::InvalidData,
                "Slice is smaller than SMBiosEntryPoint64::MINIMUM_SIZE",
            ));
        }

        if !raw
            .iter()
            .zip(Self::SM3_ANCHOR.iter())
            .all(|pair| pair.0 == pair.1)
        {
            return Err(Error::new(
                ErrorKind::InvalidData,
                "Expected _SM3_ identifier not found",
            ));
        }

        // Verify the checksum
        // The checksum is calculated for a length of `entry_point_length`
        let entry_point_length = raw[Self::ENTRY_POINT_LENGTH_OFFSET] as usize;
        match raw.get(0..entry_point_length) {
            Some(checked_bytes) => {
                if !verify_checksum(checked_bytes) {
                    return Err(Error::new(ErrorKind::InvalidData,"Entry Point Structure checksum verification failed"));
                }
            }
            None => return Err(Error::new(ErrorKind::InvalidData,"The Entry Point Length field specified a value which exceeded the bounds of the Entry Point Structure")),
        }

        Ok(SMBiosEntryPoint64 { raw })
    }
}

/// Verifies EPS and IEPS Checksums
///
/// The EPS and IEPS contain a checksum value.
///
/// The checksum value, when added to all other bytes in the EPS, results in
/// the value 00h (using 8-bit addition [Wrapping] calculations).
/// Values in the EPS are summed starting at offset 00h, for 'entry_point_length'
/// bytes.
fn verify_checksum(data: &[u8]) -> bool {
    let mut sum = Wrapping(0u8);

    data.iter().for_each(|b| sum += Wrapping(*b));

    sum == Wrapping(0)
}