Skip to content

Commit f65f1dc

Browse files
kiram9JohnAZoidberg
authored andcommitted
create version check function
This will check if versions match and return 0 if match or 1 if not match. This can run in UEFI shell etc. Signed-off-by: Kieran Levin <[email protected]>
1 parent 8ff67b6 commit f65f1dc

File tree

7 files changed

+162
-8
lines changed

7 files changed

+162
-8
lines changed

framework_lib/src/chromium_ec/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,17 @@ pub enum CrosEcDriverType {
864864
Windows,
865865
}
866866

867+
#[cfg_attr(not(feature = "uefi"), derive(clap::ValueEnum))]
868+
#[derive(Clone, Debug, Copy, PartialEq)]
869+
pub enum HardwareDeviceType {
870+
BIOS,
871+
EC,
872+
PD0,
873+
PD1,
874+
RTM01,
875+
RTM23,
876+
}
877+
867878
impl CrosEcDriver for CrosEc {
868879
fn read_memory(&self, offset: u16, length: u16) -> Option<Vec<u8>> {
869880
if !smbios::is_framework() {

framework_lib/src/commandline/clap_std.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
use clap::Parser;
55

66
use crate::chromium_ec::CrosEcDriverType;
7-
use crate::commandline::{Cli, ConsoleArg, FpBrightnessArg, InputDeckModeArg, RebootEcArg};
7+
use crate::commandline::{
8+
Cli, ConsoleArg, FpBrightnessArg, HardwareDeviceType, InputDeckModeArg, RebootEcArg,
9+
};
810

911
/// Swiss army knife for Framework laptops
1012
#[derive(Parser)]
@@ -25,6 +27,15 @@ struct ClapCli {
2527
#[arg(long)]
2628
esrt: bool,
2729

30+
// Device type to compare_version string with version string on device
31+
#[clap(value_enum)]
32+
#[arg(long)]
33+
device: Option<HardwareDeviceType>,
34+
35+
// version to compare with
36+
#[arg(long)]
37+
compare_version: Option<String>,
38+
2839
/// Show current power status of battery and AC (Add -vv for more details)
2940
#[arg(long)]
3041
power: bool,
@@ -198,6 +209,8 @@ pub fn parse(args: &[String]) -> Cli {
198209
versions: args.versions,
199210
version: args.version,
200211
esrt: args.esrt,
212+
device: args.device,
213+
compare_version: args.compare_version,
201214
power: args.power,
202215
thermal: args.thermal,
203216
sensors: args.sensors,

framework_lib/src/commandline/mod.rs

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use sha2::{Digest, Sha256, Sha384, Sha512};
5454
//use smbioslib::*;
5555
use smbioslib::{DefinedStruct, SMBiosInformation};
5656

57-
use crate::chromium_ec::{CrosEc, CrosEcDriverType};
57+
use crate::chromium_ec::{CrosEc, CrosEcDriverType, HardwareDeviceType};
5858

5959
#[cfg(feature = "uefi")]
6060
use core::prelude::rust_2021::derive;
@@ -120,6 +120,8 @@ pub struct Cli {
120120
pub versions: bool,
121121
pub version: bool,
122122
pub esrt: bool,
123+
pub device: Option<HardwareDeviceType>,
124+
pub compare_version: Option<String>,
123125
pub power: bool,
124126
pub thermal: bool,
125127
pub sensors: bool,
@@ -506,6 +508,86 @@ fn dump_ec_flash(ec: &CrosEc, dump_path: &str) {
506508
}
507509
}
508510

511+
fn compare_version(device: Option<HardwareDeviceType>, version: String, ec: &CrosEc) -> i32 {
512+
println!("Target Version {:?}", version);
513+
514+
if let Some(smbios) = get_smbios() {
515+
let bios_entries = smbios.collect::<SMBiosInformation>();
516+
let bios = bios_entries.get(0).unwrap();
517+
518+
if device == Some(HardwareDeviceType::BIOS) {
519+
println!("Comparing BIOS version {:?}", bios.version().to_string());
520+
if version.to_uppercase() == bios.version().to_string().to_uppercase() {
521+
return 0;
522+
} else {
523+
return 1;
524+
}
525+
}
526+
}
527+
528+
if device == Some(HardwareDeviceType::EC) {
529+
let ver = print_err(ec.version_info()).unwrap_or_else(|| "UNKNOWN".to_string());
530+
println!("Comparing EC version {:?}", ver);
531+
532+
if ver.contains(&version) {
533+
return 0;
534+
} else {
535+
return 1;
536+
}
537+
}
538+
if device == Some(HardwareDeviceType::PD0) {
539+
if let Ok(pd_versions) = ccgx::get_pd_controller_versions(ec) {
540+
let ver = pd_versions.controller01.main_fw.app.to_string();
541+
println!("Comparing PD0 version {:?}", ver);
542+
543+
if ver.contains(&version) {
544+
return 0;
545+
} else {
546+
return 1;
547+
}
548+
}
549+
}
550+
if device == Some(HardwareDeviceType::PD1) {
551+
if let Ok(pd_versions) = ccgx::get_pd_controller_versions(ec) {
552+
let ver = pd_versions.controller23.main_fw.app.to_string();
553+
println!("Comparing PD1 version {:?}", ver);
554+
555+
if ver.contains(&version) {
556+
return 0;
557+
} else {
558+
return 1;
559+
}
560+
}
561+
}
562+
563+
if let Some(esrt) = esrt::get_esrt() {
564+
for entry in &esrt.entries {
565+
match entry.fw_class {
566+
esrt::TGL_RETIMER01_GUID | esrt::ADL_RETIMER01_GUID | esrt::RPL_RETIMER01_GUID => {
567+
if device == Some(HardwareDeviceType::RTM01) {
568+
println!("Comparing RTM01 version {:?}", entry.fw_version.to_string());
569+
570+
if entry.fw_version.to_string().contains(&version) {
571+
return 0;
572+
}
573+
}
574+
}
575+
esrt::TGL_RETIMER23_GUID | esrt::ADL_RETIMER23_GUID | esrt::RPL_RETIMER23_GUID => {
576+
if device == Some(HardwareDeviceType::RTM23) {
577+
println!("Comparing RTM23 version {:?}", entry.fw_version.to_string());
578+
if entry.fw_version.to_string().contains(&version) {
579+
return 0;
580+
}
581+
}
582+
}
583+
_ => {}
584+
}
585+
}
586+
}
587+
588+
1
589+
}
590+
509591
pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
510592
#[cfg(feature = "uefi")]
511593
{
@@ -563,6 +645,10 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
563645
print_tool_version();
564646
} else if args.esrt {
565647
print_esrt();
648+
} else if let Some(compare_version_ver) = &args.compare_version {
649+
let compare_ret = compare_version(args.device, compare_version_ver.to_string(), &ec);
650+
println!("Compared version: {}", compare_ret);
651+
return compare_ret;
566652
} else if args.intrusion {
567653
println!("Chassis status:");
568654
if let Some(status) = print_err(ec.get_intrusion_status()) {
@@ -653,7 +739,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
653739
return 1;
654740
}
655741
} else if args.power {
656-
power::get_and_print_power_info(&ec);
742+
return power::get_and_print_power_info(&ec);
657743
} else if args.thermal {
658744
power::print_thermal(&ec);
659745
} else if args.sensors {
@@ -835,6 +921,8 @@ Options:
835921
--versions List current firmware versions
836922
--version Show tool version information (Add -vv for more detailed information)
837923
--esrt Display the UEFI ESRT table
924+
--device <DEVICE> Device used to compare firmware version [possible values: bios, ec, pd0, pd1, rtm01, rtm23]
925+
--compare-version Version string used to match firmware version (use with --device)
838926
--power Show current power status (battery and AC)
839927
--thermal Print thermal information (Temperatures and Fan speed)
840928
--sensors Print sensor information (ALS, G-Sensor)

framework_lib/src/commandline/uefi.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use uefi::proto::shell_params::*;
99
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams, SearchType};
1010
use uefi::Identify;
1111

12-
use crate::chromium_ec::CrosEcDriverType;
12+
use crate::chromium_ec::{CrosEcDriverType, HardwareDeviceType};
1313
use crate::commandline::Cli;
1414

1515
use super::{ConsoleArg, FpBrightnessArg, InputDeckModeArg, RebootEcArg};
@@ -58,6 +58,8 @@ pub fn parse(args: &[String]) -> Cli {
5858
versions: false,
5959
version: false,
6060
esrt: false,
61+
device: None,
62+
compare_version: None,
6163
power: false,
6264
thermal: false,
6365
sensors: false,
@@ -403,6 +405,37 @@ pub fn parse(args: &[String]) -> Cli {
403405
found_an_option = true;
404406
} else if arg == "--raw-command" {
405407
cli.raw_command = args[1..].to_vec();
408+
} else if arg == "--compare-version" {
409+
cli.compare_version = if args.len() > i + 1 {
410+
Some(args[i + 1].clone())
411+
} else {
412+
println!("--compare-version requires extra argument to denote version");
413+
None
414+
};
415+
found_an_option = true;
416+
} else if arg == "--device" {
417+
cli.device = if args.len() > i + 1 {
418+
let console_arg = &args[i + 1];
419+
if console_arg == "bios" {
420+
Some(HardwareDeviceType::BIOS)
421+
} else if console_arg == "ec" {
422+
Some(HardwareDeviceType::EC)
423+
} else if console_arg == "pd0" {
424+
Some(HardwareDeviceType::PD0)
425+
} else if console_arg == "pd1" {
426+
Some(HardwareDeviceType::PD1)
427+
} else if console_arg == "rtm01" {
428+
Some(HardwareDeviceType::RTM01)
429+
} else if console_arg == "rtm23" {
430+
Some(HardwareDeviceType::RTM23)
431+
} else {
432+
println!("Invalid value for --device: {}", console_arg);
433+
None
434+
}
435+
} else {
436+
println!("Need to provide a value for --console. Either `follow` or `recent`");
437+
None
438+
};
406439
}
407440
}
408441

framework_lib/src/power.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,14 @@ pub fn is_standalone(ec: &CrosEc) -> bool {
306306
}
307307
}
308308

309-
pub fn get_and_print_power_info(ec: &CrosEc) {
309+
pub fn get_and_print_power_info(ec: &CrosEc) -> i32 {
310310
if let Some(power_info) = power_info(ec) {
311311
print_battery_information(&power_info);
312+
if let Some(_battery) = &power_info.battery {
313+
return 0;
314+
}
312315
}
316+
1
313317
}
314318

315319
fn print_battery_information(power_info: &PowerInfo) {

framework_tool/src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ fn get_args() -> Vec<String> {
55
std::env::args().collect()
66
}
77

8-
fn main() {
8+
fn main() -> Result<(), &'static str> {
99
let args = commandline::parse(&get_args());
10-
commandline::run_with_args(&args, false);
10+
if (commandline::run_with_args(&args, false)) != 0 {
11+
return Err("Fail");
12+
}
13+
Ok(())
1114
}

framework_uefi/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ fn main(_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
1818

1919
let args = commandline::uefi::get_args(bs);
2020
let args = commandline::parse(&args);
21-
commandline::run_with_args(&args, false);
21+
if commandline::run_with_args(&args, false) == 0 {
22+
return Status::SUCCESS;
23+
}
2224

2325
// Force it go into UEFI shell
2426
Status::LOAD_ERROR

0 commit comments

Comments
 (0)