Skip to content

Commit 588d216

Browse files
committed
Allow decoding of SMBIOS Type 11 serialnumbers
They're the serialnumbers of what the system was originally assembled with in the factory. TODO - [ ] Cleanup code - [ ] Make safer with fewer unwraps - [ ] Custom command, not in info - [ ] Make sure date is decoded correctly - [ ] Support Framework 12 - [x] Support Framework 13 - [ ] Support Framework 16 Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 2200639 commit 588d216

File tree

6 files changed

+236
-2
lines changed

6 files changed

+236
-2
lines changed

framework_lib/src/commandline/clap_std.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ struct ClapCli {
6161
#[arg(long)]
6262
pdports: bool,
6363

64-
/// Show info from SMBIOS (Only on UEFI)
64+
/// Show info from SMBIOS
6565
#[arg(long)]
6666
info: bool,
6767

68+
/// Show info about system serial numbers
69+
#[arg(long)]
70+
serialnums: bool,
71+
6872
/// Show details about the PD controllers
6973
#[arg(long)]
7074
pd_info: bool,
@@ -349,6 +353,7 @@ pub fn parse(args: &[String]) -> Cli {
349353
paginate: false,
350354
info: args.info,
351355
flash_gpu_descriptor,
356+
serialnums: args.serialnums,
352357
raw_command: vec![],
353358
}
354359
}

framework_lib/src/commandline/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ pub struct Cli {
185185
pub help: bool,
186186
pub info: bool,
187187
pub flash_gpu_descriptor: Option<(u8, String)>,
188+
pub serialnums: bool,
188189
// UEFI only
189190
pub allupdate: bool,
190191
pub paginate: bool,
@@ -854,6 +855,8 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
854855
power::get_and_print_pd_info(&ec);
855856
} else if args.info {
856857
smbios_info();
858+
} else if args.serialnums {
859+
serialnum_info();
857860
} else if args.pd_info {
858861
print_pd_details(&ec);
859862
} else if args.dp_hdmi_info {
@@ -1047,7 +1050,8 @@ Options:
10471050
--thermal Print thermal information (Temperatures and Fan speed)
10481051
--sensors Print sensor information (ALS, G-Sensor)
10491052
--pdports Show information about USB-C PD ports
1050-
--info Show info from SMBIOS (Only on UEFI)
1053+
--info Show info from SMBIOS
1054+
--serialnums Show info about system serial numbers
10511055
--pd-info Show details about the PD controllers
10521056
--privacy Show privacy switch statuses (camera and microphone)
10531057
--pd-bin <PD_BIN> Parse versions from PD firmware binary file
@@ -1282,6 +1286,19 @@ fn smbios_info() {
12821286
}
12831287
}
12841288

1289+
fn serialnum_info() {
1290+
let smbios = get_smbios();
1291+
if smbios.is_none() {
1292+
error!("Failed to find SMBIOS");
1293+
return;
1294+
}
1295+
for undefined_struct in smbios.unwrap().iter() {
1296+
if let DefinedStruct::OemStrings(data) = undefined_struct.defined_struct() {
1297+
smbios::dump_oem_strings(data.oem_strings());
1298+
}
1299+
}
1300+
}
1301+
12851302
fn analyze_ccgx_pd_fw(data: &[u8]) {
12861303
if let Some(versions) = ccgx::binary::read_versions(data, Ccg3) {
12871304
println!("Detected CCG3 firmware");

framework_lib/src/commandline/uefi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub fn parse(args: &[String]) -> Cli {
104104
flash_gpu_descriptor: None,
105105
allupdate: false,
106106
info: false,
107+
serialnums: false,
107108
raw_command: vec![],
108109
};
109110

@@ -157,6 +158,9 @@ pub fn parse(args: &[String]) -> Cli {
157158
} else if arg == "--info" {
158159
cli.info = true;
159160
found_an_option = true;
161+
} else if arg == "--serialnums" {
162+
cli.serialnums = true;
163+
found_an_option = true;
160164
} else if arg == "--intrusion" {
161165
cli.intrusion = true;
162166
found_an_option = true;

framework_lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub mod esrt;
3939
pub mod guid;
4040
mod os_specific;
4141
pub mod power;
42+
pub mod serialnum;
4243
pub mod smbios;
4344
#[cfg(feature = "uefi")]
4445
pub mod uefi;

framework_lib/src/serialnum.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use alloc::string::{String, ToString};
2+
use core::str::FromStr;
3+
4+
#[derive(Debug)]
5+
pub struct FrameworkSerial {
6+
// brand: Always FR for Framework
7+
// format: Always A
8+
/// Three letter string
9+
pub product: String,
10+
/// Two letter string
11+
pub oem: String,
12+
/// Development state
13+
pub cfg0: Cfg0,
14+
/// Defines config of that specific product
15+
pub cfg1: char,
16+
pub year: u16,
17+
pub week: u8,
18+
pub day: WeekDay,
19+
/// Four letter/digit string
20+
pub part: String,
21+
}
22+
23+
#[derive(Debug)]
24+
pub enum Cfg0 {
25+
SKU = 0x00,
26+
Poc1 = 0x01,
27+
Proto1 = 0x02,
28+
Proto2 = 0x03,
29+
Evt1 = 0x04,
30+
Evt2 = 0x05,
31+
Reserved = 0x06,
32+
Dvt1 = 0x07,
33+
Dvt2 = 0x08,
34+
Pvt = 0x09,
35+
Mp = 0x0A,
36+
}
37+
38+
#[derive(Debug)]
39+
pub enum WeekDay {
40+
Monday = 1,
41+
Tuesday,
42+
Wednesday,
43+
Thursday,
44+
Friday,
45+
Saturday,
46+
Sunday,
47+
}
48+
49+
impl FromStr for FrameworkSerial {
50+
type Err = String;
51+
52+
// TODO: !!! PROPER ERROR HANDLING !!!
53+
fn from_str(s: &str) -> Result<Self, Self::Err> {
54+
let pattern =
55+
r"FRA([A-Z]{3})([A-Z]{2})([0-9A-F])([0-9A-F])([0-9A-Z])([0-9]{2})([0-7])([0-9A-Z]{4})";
56+
let re = regex::Regex::new(pattern).unwrap();
57+
58+
let caps = re.captures(s).ok_or("Invalid Serial".to_string())?;
59+
60+
let cfg0 = match caps.get(3).unwrap().as_str().chars().next().unwrap() {
61+
'0' => Cfg0::SKU,
62+
'1' => Cfg0::Poc1,
63+
'2' => Cfg0::Proto1,
64+
'3' => Cfg0::Proto2,
65+
'4' => Cfg0::Evt1,
66+
'5' => Cfg0::Evt2,
67+
'6' => Cfg0::Reserved,
68+
'7' => Cfg0::Dvt1,
69+
'8' => Cfg0::Dvt2,
70+
'9' => Cfg0::Pvt,
71+
'A' => Cfg0::Mp,
72+
_ => return Err("Invalid CFG0".to_string()),
73+
};
74+
let cfg1 = caps.get(4).unwrap().as_str().chars().next().unwrap();
75+
let year = str::parse::<u16>(caps.get(5).unwrap().as_str()).unwrap();
76+
let year = 2020 + year;
77+
let week = str::parse::<u8>(caps.get(6).unwrap().as_str()).unwrap();
78+
// TODO: Decode into date
79+
let day = match str::parse::<u8>(caps.get(7).unwrap().as_str()).unwrap() {
80+
1 => WeekDay::Monday,
81+
2 => WeekDay::Tuesday,
82+
3 => WeekDay::Wednesday,
83+
4 => WeekDay::Thursday,
84+
5 => WeekDay::Friday,
85+
6 => WeekDay::Saturday,
86+
7 => WeekDay::Sunday,
87+
_ => return Err("Invalid Day".to_string()),
88+
};
89+
90+
Ok(FrameworkSerial {
91+
product: caps.get(1).unwrap().as_str().to_string(),
92+
oem: caps.get(2).unwrap().as_str().to_string(),
93+
cfg0,
94+
cfg1,
95+
year,
96+
week,
97+
day,
98+
part: caps.get(2).unwrap().as_str().to_string(),
99+
})
100+
}
101+
}

framework_lib/src/smbios.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//! Retrieve SMBIOS tables and extract information from them
22
3+
use core::str::FromStr;
34
use std::prelude::v1::*;
45

56
#[cfg(all(not(feature = "uefi"), not(target_os = "freebsd")))]
67
use std::io::ErrorKind;
78

9+
use crate::serialnum::FrameworkSerial;
810
use crate::util::Config;
911
pub use crate::util::Platform;
1012
use num_derive::FromPrimitive;
@@ -310,3 +312,107 @@ fn kenv_get(name: &str) -> nix::Result<String> {
310312
Ok(value)
311313
}
312314
}
315+
316+
#[derive(Debug)]
317+
enum SmbiosSerialNumber {
318+
Mainboard = 1,
319+
Laptop,
320+
Camera,
321+
Display,
322+
Battery,
323+
Touchpad,
324+
Keyboard,
325+
Fingerprint,
326+
AudioDaughtercard,
327+
ACover,
328+
BCover,
329+
CCover,
330+
AntennaMain,
331+
AntennaAux,
332+
TouchpadFpc,
333+
FingerprintFfc,
334+
EdpCable,
335+
LcdCable,
336+
ThermalAssembly,
337+
WifiModule,
338+
Speaker,
339+
RamSlot1,
340+
RamSlot2,
341+
Ssd,
342+
AudioFfc,
343+
}
344+
345+
pub fn dump_oem_strings(strings: &SMBiosStringSet) {
346+
for (i, s) in strings.into_iter().enumerate() {
347+
let idx = i + 1;
348+
let sn = match idx {
349+
1 => Some(SmbiosSerialNumber::Mainboard),
350+
2 => Some(SmbiosSerialNumber::Laptop),
351+
3 => Some(SmbiosSerialNumber::Camera),
352+
4 => Some(SmbiosSerialNumber::Display),
353+
5 => Some(SmbiosSerialNumber::Battery),
354+
6 => Some(SmbiosSerialNumber::Touchpad),
355+
7 => Some(SmbiosSerialNumber::Keyboard),
356+
8 => Some(SmbiosSerialNumber::Fingerprint),
357+
10 => Some(SmbiosSerialNumber::AudioDaughtercard),
358+
11 => Some(SmbiosSerialNumber::ACover),
359+
12 => Some(SmbiosSerialNumber::BCover),
360+
13 => Some(SmbiosSerialNumber::CCover),
361+
14 => Some(SmbiosSerialNumber::AntennaMain),
362+
15 => Some(SmbiosSerialNumber::AntennaAux),
363+
16 => Some(SmbiosSerialNumber::TouchpadFpc),
364+
17 => Some(SmbiosSerialNumber::FingerprintFfc),
365+
18 => Some(SmbiosSerialNumber::EdpCable),
366+
19 => Some(SmbiosSerialNumber::LcdCable),
367+
20 => Some(SmbiosSerialNumber::ThermalAssembly),
368+
21 => Some(SmbiosSerialNumber::WifiModule),
369+
22 => Some(SmbiosSerialNumber::Speaker),
370+
23 => Some(SmbiosSerialNumber::RamSlot1),
371+
24 => Some(SmbiosSerialNumber::RamSlot2),
372+
25 => Some(SmbiosSerialNumber::Ssd),
373+
26 => Some(SmbiosSerialNumber::AudioFfc),
374+
_ => None,
375+
};
376+
match sn {
377+
Some(SmbiosSerialNumber::RamSlot1)
378+
| Some(SmbiosSerialNumber::RamSlot2)
379+
| Some(SmbiosSerialNumber::Ssd)
380+
| Some(SmbiosSerialNumber::WifiModule) => {
381+
println!("{} {:<20} (Unused)", s, format!("{:?}", sn.unwrap()))
382+
}
383+
Some(SmbiosSerialNumber::Fingerprint) | Some(SmbiosSerialNumber::CCover) => {
384+
println!("{}", s);
385+
println!(" {:<20} (Only Pre-Built)", format!("{:?}", sn.unwrap()));
386+
if let Ok(serial) = FrameworkSerial::from_str(&format!("{:?}", s)) {
387+
println!(
388+
" {} (Config {}) by {}, {:>4} Phase ({:?}, Week {}, {})",
389+
serial.product,
390+
serial.cfg1,
391+
serial.oem,
392+
format!("{:?}", serial.cfg0),
393+
serial.day,
394+
serial.week,
395+
serial.year,
396+
);
397+
}
398+
}
399+
Some(sn) => {
400+
println!("{}", s);
401+
println!(" {:?}", sn);
402+
if let Ok(serial) = FrameworkSerial::from_str(&format!("{:?}", s)) {
403+
println!(
404+
" {} (Config {}) by {}, {:>4} Phase ({:?}, Week {}, {})",
405+
serial.product,
406+
serial.cfg1,
407+
serial.oem,
408+
format!("{:?}", serial.cfg0),
409+
serial.day,
410+
serial.week,
411+
serial.year,
412+
);
413+
}
414+
}
415+
_ => println!("{} Unknown/Reserved", s),
416+
}
417+
}
418+
}

0 commit comments

Comments
 (0)