lanzaboote/rust/lanzatool/src/cli.rs

92 lines
2.0 KiB
Rust
Raw Normal View History

2022-11-21 19:29:16 -05:00
use std::path::{Path, PathBuf};
2022-11-25 17:48:20 -05:00
use anyhow::{Context, Result};
2022-11-21 19:29:16 -05:00
use clap::{Parser, Subcommand};
2022-11-23 09:30:24 -05:00
use crate::install;
2022-11-21 19:29:16 -05:00
#[derive(Parser)]
pub struct Cli {
#[clap(subcommand)]
pub commands: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
2022-11-23 09:26:26 -05:00
Install {
2022-11-24 08:12:00 -05:00
// Secure Boot Public Key
#[clap(long)]
2022-11-23 09:26:26 -05:00
public_key: PathBuf,
2022-11-24 08:12:00 -05:00
// Secure Boot Private Key
#[clap(long)]
private_key: PathBuf,
// Secure Boot PKI Bundle for auto enrolling key
#[clap(long)]
2022-11-25 07:08:37 -05:00
pki_bundle: Option<PathBuf>,
2022-11-24 08:12:00 -05:00
// Enable auto enrolling your keys in UEFI
// Be aware that this might irrevocably brick your device
#[clap(long, default_value = "false")]
auto_enroll: bool,
2022-11-23 09:26:26 -05:00
bootspec: PathBuf,
generations: Vec<PathBuf>,
2022-11-23 09:26:26 -05:00
},
2022-11-21 19:29:16 -05:00
}
impl Cli {
pub fn call(self) -> Result<()> {
self.commands.call()
}
}
impl Commands {
pub fn call(self) -> Result<()> {
match self {
2022-11-23 09:26:26 -05:00
Commands::Install {
public_key,
2022-11-24 08:12:00 -05:00
private_key,
pki_bundle,
auto_enroll,
2022-11-23 09:26:26 -05:00
bootspec,
generations,
2022-11-24 08:12:00 -05:00
} => install(
&public_key,
&private_key,
&pki_bundle,
2022-11-24 08:12:00 -05:00
auto_enroll,
&bootspec,
generations,
2022-11-24 08:12:00 -05:00
),
2022-11-21 19:29:16 -05:00
}
}
}
2022-11-24 08:12:00 -05:00
fn install(
public_key: &Path,
private_key: &Path,
pki_bundle: &Option<PathBuf>,
2022-11-24 08:12:00 -05:00
auto_enroll: bool,
bootspec: &Path,
generations: Vec<PathBuf>,
2022-11-24 08:12:00 -05:00
) -> Result<()> {
2022-11-25 17:48:20 -05:00
let lanzaboote_stub =
std::env::var("LANZABOOTE_STUB").context("Failed to read LANZABOOTE_STUB env variable")?;
let initrd_stub = std::env::var("LANZABOOTE_INITRD_STUB")
.context("Failed to read LANZABOOTE_INITRD_STUB env variable")?;
2022-11-24 08:12:00 -05:00
2022-11-23 14:40:01 -05:00
install::install(
public_key,
2022-11-24 08:12:00 -05:00
private_key,
pki_bundle,
auto_enroll,
2022-11-23 14:40:01 -05:00
bootspec,
generations,
2022-11-23 14:40:01 -05:00
Path::new(&lanzaboote_stub),
Path::new(&initrd_stub),
2022-11-23 14:40:01 -05:00
)
2022-11-23 09:26:26 -05:00
}