Skip to content

Commit

Permalink
create version check function
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
kiram9 authored and JohnAZoidberg committed Aug 7, 2024
1 parent 31c145b commit 7ea4ab0
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 8 deletions.
11 changes: 11 additions & 0 deletions framework_lib/src/chromium_ec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,17 @@ pub enum CrosEcDriverType {
Windows,
}

#[cfg_attr(not(feature = "uefi"), derive(clap::ValueEnum))]
#[derive(Clone, Debug, Copy, PartialEq)]
pub enum HardwareDeviceType {
BIOS,
EC,
PD0,
PD1,
RTM01,
RTM23,
}

impl CrosEcDriver for CrosEc {
fn read_memory(&self, offset: u16, length: u16) -> Option<Vec<u8>> {
if !smbios::is_framework() {
Expand Down
15 changes: 14 additions & 1 deletion framework_lib/src/commandline/clap_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
use clap::Parser;

use crate::chromium_ec::CrosEcDriverType;
use crate::commandline::{Cli, ConsoleArg, FpBrightnessArg, InputDeckModeArg, RebootEcArg};
use crate::commandline::{
Cli, ConsoleArg, FpBrightnessArg, HardwareDeviceType, InputDeckModeArg, RebootEcArg,
};

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

// Device type to compare_version string with version string on device
#[clap(value_enum)]
#[arg(long)]
device: Option<HardwareDeviceType>,

// version to compare with
#[arg(long)]
compare_version: Option<String>,

/// Show current power status of battery and AC (Add -vv for more details)
#[arg(long)]
power: bool,
Expand Down Expand Up @@ -198,6 +209,8 @@ pub fn parse(args: &[String]) -> Cli {
versions: args.versions,
version: args.version,
esrt: args.esrt,
device: args.device,
compare_version: args.compare_version,
power: args.power,
thermal: args.thermal,
sensors: args.sensors,
Expand Down
92 changes: 90 additions & 2 deletions framework_lib/src/commandline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use sha2::{Digest, Sha256, Sha384, Sha512};
//use smbioslib::*;
use smbioslib::{DefinedStruct, SMBiosInformation};

use crate::chromium_ec::{CrosEc, CrosEcDriverType};
use crate::chromium_ec::{CrosEc, CrosEcDriverType, HardwareDeviceType};

#[cfg(feature = "uefi")]
use core::prelude::rust_2021::derive;
Expand Down Expand Up @@ -120,6 +120,8 @@ pub struct Cli {
pub versions: bool,
pub version: bool,
pub esrt: bool,
pub device: Option<HardwareDeviceType>,
pub compare_version: Option<String>,
pub power: bool,
pub thermal: bool,
pub sensors: bool,
Expand Down Expand Up @@ -506,6 +508,86 @@ fn dump_ec_flash(ec: &CrosEc, dump_path: &str) {
}
}

fn compare_version(device: Option<HardwareDeviceType>, version: String, ec: &CrosEc) -> i32 {
println!("Target Version {:?}", version);

if let Some(smbios) = get_smbios() {
let bios_entries = smbios.collect::<SMBiosInformation>();
let bios = bios_entries.get(0).unwrap();

if device == Some(HardwareDeviceType::BIOS) {
println!("Comparing BIOS version {:?}", bios.version().to_string());
if version.to_uppercase() == bios.version().to_string().to_uppercase() {
return 0;
} else {
return 1;
}
}
}

if device == Some(HardwareDeviceType::EC) {
let ver = print_err(ec.version_info()).unwrap_or_else(|| "UNKNOWN".to_string());
println!("Comparing EC version {:?}", ver);

if ver.contains(&version) {
return 0;
} else {
return 1;
}
}
if device == Some(HardwareDeviceType::PD0) {
if let Ok(pd_versions) = ccgx::get_pd_controller_versions(ec) {
let ver = pd_versions.controller01.main_fw.app.to_string();
println!("Comparing PD0 version {:?}", ver);

if ver.contains(&version) {
return 0;
} else {
return 1;
}
}
}
if device == Some(HardwareDeviceType::PD1) {
if let Ok(pd_versions) = ccgx::get_pd_controller_versions(ec) {
let ver = pd_versions.controller23.main_fw.app.to_string();
println!("Comparing PD1 version {:?}", ver);

if ver.contains(&version) {
return 0;
} else {
return 1;
}
}
}

if let Some(esrt) = esrt::get_esrt() {
for entry in &esrt.entries {
match entry.fw_class {
esrt::TGL_RETIMER01_GUID | esrt::ADL_RETIMER01_GUID | esrt::RPL_RETIMER01_GUID => {
if device == Some(HardwareDeviceType::RTM01) {
println!("Comparing RTM01 version {:?}", entry.fw_version.to_string());

if entry.fw_version.to_string().contains(&version) {
return 0;
}
}
}
esrt::TGL_RETIMER23_GUID | esrt::ADL_RETIMER23_GUID | esrt::RPL_RETIMER23_GUID => {
if device == Some(HardwareDeviceType::RTM23) {
println!("Comparing RTM23 version {:?}", entry.fw_version.to_string());
if entry.fw_version.to_string().contains(&version) {
return 0;
}
}
}
_ => {}
}
}
}

1
}

pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
#[cfg(feature = "uefi")]
{
Expand Down Expand Up @@ -563,6 +645,10 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
print_tool_version();
} else if args.esrt {
print_esrt();
} else if let Some(compare_version_ver) = &args.compare_version {
let compare_ret = compare_version(args.device, compare_version_ver.to_string(), &ec);
println!("Compared version: {}", compare_ret);
return compare_ret;
} else if args.intrusion {
println!("Chassis status:");
if let Some(status) = print_err(ec.get_intrusion_status()) {
Expand Down Expand Up @@ -653,7 +739,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
return 1;
}
} else if args.power {
power::get_and_print_power_info(&ec);
return power::get_and_print_power_info(&ec);
} else if args.thermal {
power::print_thermal(&ec);
} else if args.sensors {
Expand Down Expand Up @@ -835,6 +921,8 @@ Options:
--versions List current firmware versions
--version Show tool version information (Add -vv for more detailed information)
--esrt Display the UEFI ESRT table
--device <DEVICE> Device used to compare firmware version [possible values: bios, ec, pd0, pd1, rtm01, rtm23]
--compare-version Version string used to match firmware version (use with --device)
--power Show current power status (battery and AC)
--thermal Print thermal information (Temperatures and Fan speed)
--sensors Print sensor information (ALS, G-Sensor)
Expand Down
35 changes: 34 additions & 1 deletion framework_lib/src/commandline/uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use uefi::proto::shell_params::*;
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams, SearchType};
use uefi::Identify;

use crate::chromium_ec::CrosEcDriverType;
use crate::chromium_ec::{CrosEcDriverType, HardwareDeviceType};
use crate::commandline::Cli;

use super::{ConsoleArg, FpBrightnessArg, InputDeckModeArg, RebootEcArg};
Expand Down Expand Up @@ -58,6 +58,8 @@ pub fn parse(args: &[String]) -> Cli {
versions: false,
version: false,
esrt: false,
device: None,
compare_version: None,
power: false,
thermal: false,
sensors: false,
Expand Down Expand Up @@ -403,6 +405,37 @@ pub fn parse(args: &[String]) -> Cli {
found_an_option = true;
} else if arg == "--raw-command" {
cli.raw_command = args[1..].to_vec();
} else if arg == "--compare-version" {
cli.compare_version = if args.len() > i + 1 {
Some(args[i + 1].clone())
} else {
println!("--compare-version requires extra argument to denote version");
None
};
found_an_option = true;
} else if arg == "--device" {
cli.device = if args.len() > i + 1 {
let console_arg = &args[i + 1];
if console_arg == "bios" {
Some(HardwareDeviceType::BIOS)
} else if console_arg == "ec" {
Some(HardwareDeviceType::EC)
} else if console_arg == "pd0" {
Some(HardwareDeviceType::PD0)
} else if console_arg == "pd1" {
Some(HardwareDeviceType::PD1)
} else if console_arg == "rtm01" {
Some(HardwareDeviceType::RTM01)
} else if console_arg == "rtm23" {
Some(HardwareDeviceType::RTM23)
} else {
println!("Invalid value for --device: {}", console_arg);
None
}
} else {
println!("Need to provide a value for --console. Either `follow` or `recent`");
None
};
}
}

Expand Down
6 changes: 5 additions & 1 deletion framework_lib/src/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,14 @@ pub fn is_standalone(ec: &CrosEc) -> bool {
}
}

pub fn get_and_print_power_info(ec: &CrosEc) {
pub fn get_and_print_power_info(ec: &CrosEc) -> i32 {
if let Some(power_info) = power_info(ec) {
print_battery_information(&power_info);
if let Some(_battery) = &power_info.battery {
return 0;
}
}
1
}

fn print_battery_information(power_info: &PowerInfo) {
Expand Down
7 changes: 5 additions & 2 deletions framework_tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ fn get_args() -> Vec<String> {
std::env::args().collect()
}

fn main() {
fn main() -> Result<(), &'static str> {
let args = commandline::parse(&get_args());
commandline::run_with_args(&args, false);
if (commandline::run_with_args(&args, false)) != 0 {
return Err("Fail");
}
Ok(())
}
4 changes: 3 additions & 1 deletion framework_uefi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ fn main(_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {

let args = commandline::uefi::get_args(bs);
let args = commandline::parse(&args);
commandline::run_with_args(&args, false);
if commandline::run_with_args(&args, false) == 0 {
return Status::SUCCESS;
}

// Force it go into UEFI shell
Status::LOAD_ERROR
Expand Down

0 comments on commit 7ea4ab0

Please sign in to comment.