Skip to content

Add --feature readonly, to build a bin without risky commands #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ build = "build.rs"

[features]
default = ["hidapi", "rusb"]
readonly = [ ]
rusb = ["dep:rusb"]
hidapi = ["dep:hidapi"]
uefi = [ "lazy_static/spin_no_std" ]
Expand Down
4 changes: 2 additions & 2 deletions framework_lib/src/commandline/clap_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use clap_num::maybe_hex;
use crate::chromium_ec::commands::SetGpuSerialMagic;
use crate::chromium_ec::CrosEcDriverType;
use crate::commandline::{
Cli, ConsoleArg, FpBrightnessArg, HardwareDeviceType, InputDeckModeArg, RebootEcArg,
Cli, ConsoleArg, FpBrightnessArg, HardwareDeviceType, InputDeckModeArg, LogLevel, RebootEcArg,
TabletModeArg,
};

Expand Down Expand Up @@ -335,7 +335,7 @@ pub fn parse(args: &[String]) -> Cli {
};

Cli {
verbosity: args.verbosity.log_level_filter(),
verbosity: LogLevel(args.verbosity.log_level_filter()),
versions: args.versions,
version: args.version,
features: args.features,
Expand Down
85 changes: 79 additions & 6 deletions framework_lib/src/commandline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,22 @@ impl From<InputDeckModeArg> for DeckStateMode {
}
}

#[derive(Debug)]
pub struct LogLevel(log::LevelFilter);

impl Default for LogLevel {
fn default() -> Self {
LogLevel(log::LevelFilter::Error)
}
}

/// Shadows `clap_std::ClapCli` with extras for UEFI
///
/// The UEFI commandline currently doesn't use clap, so we need to shadow the struct.
/// Also it has extra options.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Cli {
pub verbosity: log::LevelFilter,
pub verbosity: LogLevel,
pub versions: bool,
pub version: bool,
pub features: bool,
Expand Down Expand Up @@ -203,9 +212,73 @@ pub struct Cli {

pub fn parse(args: &[String]) -> Cli {
#[cfg(feature = "uefi")]
return uefi::parse(args);
let cli = uefi::parse(args);
#[cfg(not(feature = "uefi"))]
return clap_std::parse(args);
let cli = clap_std::parse(args);

if cfg!(feature = "readonly") {
// Initialize a new Cli with no arguments
// Set all arguments that are readonly/safe
// We explicitly only cope the safe ones so that if we add new arguments in the future,
// which might be unsafe, we can't forget to exclude them from the safe set.
// TODO: Instead of silently ignoring blocked command, we should remind the user
Cli {
verbosity: cli.verbosity,
versions: cli.versions,
version: cli.version,
esrt: cli.esrt,
device: cli.device,
power: cli.power,
thermal: cli.thermal,
sensors: cli.sensors,
// fansetduty
// fansetrpm
// autofanctrl
privacy: cli.privacy,
pd_info: cli.version,
dp_hdmi_info: cli.dp_hdmi_info,
// dp_hdmi_update
audio_card_info: cli.audio_card_info,
pd_bin: cli.pd_bin,
ec_bin: cli.ec_bin,
capsule: cli.capsule,
dump: cli.dump,
h2o_capsule: cli.h2o_capsule,
// dump_ec_flash
// flash_ec
// flash_ro_ec
driver: cli.driver,
test: cli.test,
intrusion: cli.intrusion,
inputdeck: cli.inputdeck,
inputdeck_mode: cli.inputdeck_mode,
expansion_bay: cli.expansion_bay,
// charge_limit
// charge_current_limit
get_gpio: cli.get_gpio,
fp_led_level: cli.fp_led_level,
fp_brightness: cli.fp_brightness,
kblight: cli.kblight,
rgbkbd: cli.rgbkbd,
// tablet_mode
// touchscreen_enable
stylus_battery: cli.stylus_battery,
console: cli.console,
reboot_ec: cli.reboot_ec,
// ec_hib_delay
hash: cli.hash,
pd_addrs: cli.pd_addrs,
pd_ports: cli.pd_ports,
help: cli.help,
info: cli.info,
// allupdate
paginate: cli.paginate,
// raw_command
..Default::default()
}
} else {
cli
}
}

fn print_single_pd_details(pd: &PdController) {
Expand Down Expand Up @@ -696,7 +769,7 @@ fn compare_version(device: Option<HardwareDeviceType>, version: String, ec: &Cro
pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
#[cfg(feature = "uefi")]
{
log::set_max_level(args.verbosity);
log::set_max_level(args.verbosity.0);
}
#[cfg(not(feature = "uefi"))]
{
Expand All @@ -705,7 +778,7 @@ pub fn run_with_args(args: &Cli, _allupdate: bool) -> i32 {
// .filter("FRAMEWORK_COMPUTER_LOG")
// .write_style("FRAMEWORK_COMPUTER_LOG_STYLE");

let level = args.verbosity.as_str();
let level = args.verbosity.0.as_str();
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(level))
.format_target(false)
.format_timestamp(None)
Expand Down
14 changes: 7 additions & 7 deletions framework_lib/src/commandline/uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use uefi::Identify;

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

use super::{ConsoleArg, FpBrightnessArg, InputDeckModeArg, RebootEcArg, TabletModeArg};

Expand Down Expand Up @@ -54,7 +54,7 @@ pub fn get_args(boot_services: &BootServices) -> Vec<String> {

pub fn parse(args: &[String]) -> Cli {
let mut cli = Cli {
verbosity: log::LevelFilter::Error,
verbosity: LogLevel(log::LevelFilter::Error),
paginate: false,
versions: false,
version: false,
Expand Down Expand Up @@ -122,15 +122,15 @@ pub fn parse(args: &[String]) -> Cli {

for (i, arg) in args.iter().enumerate() {
if arg == "-q" {
cli.verbosity = log::LevelFilter::Off;
cli.verbosity = LogLevel(log::LevelFilter::Off);
} else if arg == "-v" {
cli.verbosity = log::LevelFilter::Warn;
cli.verbosity = LogLevel(log::LevelFilter::Warn);
} else if arg == "-vv" {
cli.verbosity = log::LevelFilter::Info;
cli.verbosity = LogLevel(log::LevelFilter::Info);
} else if arg == "-vvv" {
cli.verbosity = log::LevelFilter::Debug;
cli.verbosity = LogLevel(log::LevelFilter::Debug);
} else if arg == "-vvvv" {
cli.verbosity = log::LevelFilter::Trace;
cli.verbosity = LogLevel(log::LevelFilter::Trace);
} else if arg == "--versions" {
cli.versions = true;
found_an_option = true;
Expand Down
4 changes: 4 additions & 0 deletions framework_tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ name = "framework_tool"
version = "0.4.1"
edition = "2021"

[features]
default = [ ]
readonly = [ "framework_lib/readonly" ]

[dependencies.framework_lib]
path = "../framework_lib"

Expand Down
4 changes: 4 additions & 0 deletions framework_uefi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ rust-version = "1.74"
name = "uefitool"
path = "src/main.rs"

[features]
default = [ ]
readonly = [ "framework_lib/readonly" ]

[dependencies]
uefi = { version = "0.20", features = ["alloc"] }
uefi-services = "0.17"
Expand Down
Loading