From 4f615df062e477005e85c0b408a6ebcb1766859a Mon Sep 17 00:00:00 2001 From: Joshua Job Date: Tue, 27 Aug 2024 15:17:33 -0700 Subject: [PATCH] Additional config files specified on commandline They are added in-order. cpuid - later IDs replace earlier ones msr - MSRs are added to the end of the MSR list --- src/bitfield.rs | 10 +++++----- src/main.rs | 48 ++++++++++++++++++++++++++++++++++++++++++------ src/msr.rs | 2 +- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/bitfield.rs b/src/bitfield.rs index 953bf86..9414031 100644 --- a/src/bitfield.rs +++ b/src/bitfield.rs @@ -26,7 +26,7 @@ pub trait Facter + From> { } ///Wraps a bit flag, usually representing if a feature is present or not -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Flag { pub name: String, pub bit: u8, @@ -44,7 +44,7 @@ impl Bindable for Flag { } ///Wraps an integer value from a bit field -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct Int { pub name: String, pub bounds: ops::Range, @@ -69,7 +69,7 @@ impl Bindable for Int { /// Wraps an X86Model representation /// These can have a number of weird conditions and are always going to be a part of a bit field -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct X86Model { pub name: String, } @@ -98,7 +98,7 @@ impl Bindable for X86Model { } } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct X86Family { pub name: String, } @@ -198,7 +198,7 @@ impl<'a> fmt::Display for Bound<'a, X86Family> { } } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, Clone)] #[serde(tag = "type")] pub enum Field { Int(Int), diff --git a/src/main.rs b/src/main.rs index 11f42d1..19d5e27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use std::error::Error; use std::fmt; +use std::path::{Path, PathBuf}; type YAMLFact = GenericFact; type YAMLFactSet = FactSet; @@ -37,6 +38,8 @@ enum CommandOpts { struct Disp { #[arg(short, long)] raw: bool, + #[arg(long)] + skip_cpu: bool, #[cfg(all(target_os = "linux", feature = "kvm"))] #[arg(long)] skip_kvm: bool, @@ -50,11 +53,13 @@ impl Command for Disp { if self.raw { display_raw() } else { - println!("CPUID:"); - let cpuid_db = cpuinfo::RunningCpuidDB::new(); - for (leaf, desc) in &config.cpuids { - if let Some(bound) = desc.bind_leaf(*leaf, &cpuid_db) { - println!("{:#010x}: {}", leaf, bound); + if !self.skip_cpu { + println!("CPUID:"); + let cpuid_db = cpuinfo::RunningCpuidDB::new(); + for (leaf, desc) in &config.cpuids { + if let Some(bound) = desc.bind_leaf(*leaf, &cpuid_db) { + println!("{:#010x}: {}", leaf, bound); + } } } @@ -307,6 +312,17 @@ struct Definition { pub msrs: Vec, } +impl Definition { + pub fn union(&mut self, b: Definition) { + let Definition { + mut cpuids, + mut msrs, + } = b; + self.cpuids.append(&mut cpuids); + self.msrs.append(&mut msrs); + } +} + fn find_read_config() -> Result> { let file = include_str!("config.yaml"); Ok(serde_yaml::from_str(file)?) @@ -332,15 +348,35 @@ fn display_raw() -> Result<(), Box> { Ok(()) } +fn read_additional_configs( + def: &mut Definition, + paths: Paths, +) -> Result<(), Box> +where + Paths: Iterator, + P: AsRef + Sized, +{ + for path in paths { + let file = std::fs::read(path)?; + let definition = serde_yaml::from_slice(&file)?; + def.union(definition); + } + Ok(()) +} + #[derive(Clone, Parser)] struct CmdLine { + #[arg(short, long)] + add_config: Vec, #[command(subcommand)] command: CommandOpts, } fn main() -> Result<(), Box> { let args = CmdLine::parse(); - let config = find_read_config()?; + let mut config = find_read_config()?; + + read_additional_configs(&mut config, args.add_config.iter())?; args.command.run(&config) } diff --git a/src/msr.rs b/src/msr.rs index 278d923..c35693d 100644 --- a/src/msr.rs +++ b/src/msr.rs @@ -95,7 +95,7 @@ pub mod linux { } /// Wraps a general description of an MSR -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Clone)] pub struct MSRDesc { pub name: String, pub address: u32,