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
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
#[macro_use]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use plain::Plain;
use core::num::Wrapping;

pub trait TableKind: Plain {
    const KIND: u8;
}

#[repr(packed)]
#[derive(Clone, Copy, Default, Debug)]
pub struct Smbios {
    pub anchor: [u8; 4],
    pub checksum: u8,
    pub length: u8,
    pub major_version: u8,
    pub minor_version: u8,
    pub max_structure_size: u16,
    pub revision: u8,
    pub formatted: [u8; 5],
    pub inter_anchor: [u8; 5],
    pub inter_checksum: u8,
    pub table_length: u16,
    pub table_address: u32,
    pub structure_count: u16,
    pub bcd_revision: u8
}

unsafe impl Plain for Smbios {}

impl Smbios {
    pub fn is_valid(&self) -> bool {
        let mut sum: Wrapping<u8> = self.anchor.iter().map(|x| Wrapping(*x)).sum();
        sum += self.checksum;
        sum += self.length;
        sum += self.major_version;
        sum += self.minor_version;
        sum += self.max_structure_size as u8;
        sum += self.revision;
        sum += self.formatted.iter().sum::<u8>();
        sum.0 == 0
    }
}

#[repr(packed)]
#[derive(Clone, Copy, Default, Debug)]
pub struct Smbios3 {
    pub anchor: [u8; 5],
    pub checksum: u8,
    pub length: u8,
    pub major_version: u8,
    pub minor_version: u8,
    pub docrev: u8,
    pub revision: u8,
    _reserved: u8,
    pub table_length: u32,
    pub table_address: u64,
}

unsafe impl Plain for Smbios3 {}

impl Smbios3 {
    pub fn is_valid(&self) -> bool {
        //TODO: Checksum
        self.anchor == *b"_SM3_"
    }
}

#[repr(packed)]
#[derive(Clone, Copy, Default, Debug)]
pub struct Header {
    pub kind: u8,
    pub len: u8,
    pub handle: u16
}

unsafe impl Plain for Header {}

#[derive(Clone)]
pub struct Table {
    pub header: Header,
    pub data: Vec<u8>,
    pub strings: Vec<String>
}

impl Table {
    pub fn get<T: TableKind>(&self) -> Option<&T> {
        if self.header.kind == T::KIND {
            T::from_bytes(&self.data).ok()
        } else {
            None
        }
    }

    pub fn get_str(&self, index: u8) -> Option<&String> {
        if index > 0 {
            self.strings.get((index - 1) as usize)
        } else {
            None
        }
    }
}

#[repr(packed)]
#[derive(Clone, Copy, Default, Debug)]
pub struct BiosInfo {
    pub vendor: u8,
    pub version: u8,
    pub address: u16,
    pub date: u8,
    pub size: u8,
    pub characteristics: u64,
}

unsafe impl Plain for BiosInfo {}

impl TableKind for BiosInfo {
    const KIND: u8 = 0;
}

#[repr(packed)]
#[derive(Default, Debug)]
pub struct SystemInfo {
    pub manufacturer: u8,
    pub name: u8,
    pub version: u8,
    pub serial: u8,
}

unsafe impl Plain for SystemInfo {}

impl TableKind for SystemInfo {
    const KIND: u8 = 1;
}

#[repr(packed)]
#[derive(Default, Debug)]
pub struct BaseBoardInfo {
    pub manufacturer: u8,
    pub product: u8,
    pub version: u8,
    pub serial: u8,
    pub asset_tag: u8,
}

unsafe impl Plain for BaseBoardInfo {}

impl TableKind for BaseBoardInfo {
    const KIND: u8 = 2;
}

#[repr(packed)]
#[derive(Default, Debug)]
pub struct ChassisInfo {
    pub manufacturer: u8,
    pub kind: u8,
    pub version: u8,
    pub serial: u8,
    pub asset_tag: u8,
}

unsafe impl Plain for ChassisInfo {}

impl TableKind for ChassisInfo {
    const KIND: u8 = 3;
}

#[repr(packed)]
#[derive(Clone, Copy, Default, Debug)]
pub struct ProcessorInfo {
    pub socket_designation: u8,
    pub processor_kind: u8,
    pub processor_family: u8,
    pub processor_manufacturer: u8,
    pub processor_id: u64,
    pub processor_version: u8,
    pub voltage: u8,
    pub external_clock: u16,
    pub max_speed: u16,
    pub current_speed: u16,
    pub status: u8,
    pub processor_upgrade: u8,
    pub l1_cache_handle: u16,
    pub l2_cache_handle: u16,
    pub l3_cache_handle: u16,
    pub serial_number: u8,
    pub asset_tag: u8,
    pub part_number: u8,
    pub core_count: u8,
    pub core_enabled: u8,
    pub thread_count: u8,
    pub processor_characteristics: u16,
    pub processor_family_2: u16,
}

unsafe impl Plain for ProcessorInfo {}

impl TableKind for ProcessorInfo {
    const KIND: u8 = 4;
}

#[repr(packed)]
#[derive(Clone, Copy, Default, Debug)]
pub struct MemoryDevice {
    pub array_handle: u16,
    pub error_information_handle: u16,
    pub total_width: u16,
    pub data_width: u16,
    pub size: u16,
    pub form_factor: u8,
    pub device_set: u8,
    pub device_locator: u8,
    pub bank_locator: u8,
    pub memory_kind: u8,
    pub kind_detail: u16,
    pub speed: u16,
    pub manufacturer: u8,
    pub serial_number: u8,
    pub asset_tag: u8,
    pub part_number: u8,
    pub attributes: u8,
    pub extended_size: u32,
    pub configured_speed: u16,
    pub minimum_voltage: u16,
    pub maximum_voltage: u16,
    pub configured_voltage: u16,
}

unsafe impl Plain for MemoryDevice {}

impl TableKind for MemoryDevice {
    const KIND: u8 = 17;
}

pub fn tables(data: &[u8]) -> Vec<Table> {
    let mut tables = Vec::new();

    let mut i = 0;
    while i < data.len() {
        // Read header
        let mut header = Header::default();
        {
            let bytes = unsafe { plain::as_mut_bytes(&mut header) };

            let mut j = 0;
            while i < data.len() && j < bytes.len() {
                bytes[j] = data[i];
                i += 1;
                j += 1;
            }
        }

        if header.kind == 127 {
            //println!("End header");
            break;
        }

        //println!("{:?}", header);

        // Read data
        let mut table = vec![0; header.len as usize - unsafe { plain::as_bytes(&header) }.len()];

        {
            let mut j = 0;
            while i < data.len() && j < table.len() {
                table[j] = data[i];
                i += 1;
                j += 1;
            }
        }

        // Read strings
        let mut strings = Vec::new();
        while i < data.len() {
            let mut string = String::new();
            while i < data.len() {
                let b = data[i];
                i += 1;

                if b == 0 {
                    break;
                } else {
                    string.push(b as char);
                }
            }

            if string.is_empty() && ! strings.is_empty() {
                break;
            } else {
                //println!("{}: {}", strings.len(), string);
                strings.push(string);
            }
        }

        tables.push(Table {
            header: header,
            data: table,
            strings: strings
        });
    }

    tables
}