|
| 1 | +// Copyright (c) 2021 Intel Corporation |
| 2 | +// Copyright (c) 2022 Alibaba Cloud |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: BSD-2-Clause-Patent |
| 5 | + |
| 6 | +#[macro_use] |
| 7 | +extern crate clap; |
| 8 | +use log::{error, LevelFilter}; |
| 9 | +use std::path::PathBuf; |
| 10 | +use std::str::FromStr; |
| 11 | +use std::vec::Vec; |
| 12 | +use std::{env, io, path::Path}; |
| 13 | +use td_shim_tools::enroller::{create_key_file, enroll_files, FirmwareRawFile}; |
| 14 | +use td_shim_tools::InputData; |
| 15 | +use td_uefi_pi::pi::guid; |
| 16 | +const TDSHIM_SB_NAME: &str = "final.sb.bin"; |
| 17 | + |
| 18 | +struct Config { |
| 19 | + // Input file path to be read |
| 20 | + pub input: String, |
| 21 | + // Output file path to be written |
| 22 | + pub output: PathBuf, |
| 23 | + // Public key file path |
| 24 | + pub key: Option<String>, |
| 25 | + // Hash algorithm "SHA384" by default |
| 26 | + pub hash_alg: String, |
| 27 | + // Firmware file information to be enrolled into CFV, |
| 28 | + // consists of (Guid, FilePath) |
| 29 | + pub firmware_files: Vec<(guid::Guid, String)>, |
| 30 | + // Log level "SHA384" by default |
| 31 | + pub log_level: String, |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Debug)] |
| 35 | +pub enum ConfigParseError { |
| 36 | + InvlidGuid, |
| 37 | + InvalidLogLevel, |
| 38 | + InvalidInputFilePath, |
| 39 | +} |
| 40 | + |
| 41 | +impl Config { |
| 42 | + pub fn new() -> Result<Self, ConfigParseError> { |
| 43 | + let matches = command!() |
| 44 | + .arg( |
| 45 | + arg!([tdshim] "shim binary file") |
| 46 | + .required(true) |
| 47 | + .allow_invalid_utf8(false), |
| 48 | + ) |
| 49 | + .arg( |
| 50 | + arg!(-k --key "public key file for enrollment") |
| 51 | + .required(false) |
| 52 | + .takes_value(true) |
| 53 | + .allow_invalid_utf8(false), |
| 54 | + ) |
| 55 | + .arg( |
| 56 | + arg!(-H --hash "hash algorithm to compute digest") |
| 57 | + .required(false) |
| 58 | + .takes_value(true) |
| 59 | + .default_value("SHA384"), |
| 60 | + ) |
| 61 | + .arg( |
| 62 | + arg!(-f --file "<Guid> <FilePath> Firmware file to be enrolled into CFV") |
| 63 | + .required(false) |
| 64 | + .multiple_values(true) |
| 65 | + .multiple_occurrences(true) |
| 66 | + .takes_value(true) |
| 67 | + .allow_invalid_utf8(false), |
| 68 | + ) |
| 69 | + .arg( |
| 70 | + arg!(-l --"log-level" "logging level: [off, error, warn, info, debug, trace]") |
| 71 | + .required(false) |
| 72 | + .default_value("info"), |
| 73 | + ) |
| 74 | + .arg( |
| 75 | + arg!(-o --output "output of the enrolled shim binary file") |
| 76 | + .required(false) |
| 77 | + .takes_value(true) |
| 78 | + .allow_invalid_utf8(false), |
| 79 | + ) |
| 80 | + .get_matches(); |
| 81 | + |
| 82 | + // Safe to unwrap() because they are mandatory or have default values. |
| 83 | + // |
| 84 | + // rust-td binary file |
| 85 | + let input = matches.value_of("tdshim").unwrap().to_string(); |
| 86 | + let output = match matches.value_of("output") { |
| 87 | + Some(v) => Path::new(v).to_path_buf(), |
| 88 | + None => { |
| 89 | + let p = Path::new(input.as_str()) |
| 90 | + .canonicalize() |
| 91 | + .map_err(|_| ConfigParseError::InvalidInputFilePath)?; |
| 92 | + p.parent().unwrap_or(Path::new("/")).join(TDSHIM_SB_NAME) |
| 93 | + } |
| 94 | + }; |
| 95 | + let hash_alg = String::from_str(matches.value_of("hash").unwrap()).unwrap(); |
| 96 | + let key = match matches.value_of("key") { |
| 97 | + Some(v) => Some(v.to_string()), |
| 98 | + None => None, |
| 99 | + }; |
| 100 | + |
| 101 | + let firmware_files = match matches.values_of("file") { |
| 102 | + Some(inputs) => { |
| 103 | + let inputs = inputs.collect::<Vec<&str>>(); |
| 104 | + let mut firmware_files: Vec<(guid::Guid, String)> = Vec::new(); |
| 105 | + for i in 0..(inputs.len() / 2) { |
| 106 | + firmware_files.push(( |
| 107 | + // Guid |
| 108 | + guid::Guid::from_str(inputs[i * 2]) |
| 109 | + .map_err(|_| ConfigParseError::InvlidGuid)?, |
| 110 | + // File path |
| 111 | + inputs[i * 2 + 1].to_string(), |
| 112 | + )); |
| 113 | + } |
| 114 | + firmware_files |
| 115 | + } |
| 116 | + None => Vec::new(), |
| 117 | + }; |
| 118 | + |
| 119 | + // Safe to unwrap() because they are mandatory or have default values. |
| 120 | + let log_level = String::from_str(matches.value_of("log-level").unwrap()) |
| 121 | + .map_err(|_| ConfigParseError::InvalidLogLevel)?; |
| 122 | + |
| 123 | + Ok(Self { |
| 124 | + input, |
| 125 | + output, |
| 126 | + hash_alg, |
| 127 | + key, |
| 128 | + firmware_files, |
| 129 | + log_level, |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +fn main() -> io::Result<()> { |
| 135 | + use env_logger::Env; |
| 136 | + let env = Env::default() |
| 137 | + .filter_or("MY_LOG_LEVEL", "info") |
| 138 | + .write_style_or("MY_LOG_STYLE", "always"); |
| 139 | + env_logger::init_from_env(env); |
| 140 | + let config = Config::new().map_err(|e| { |
| 141 | + error!("Parse command line error: {:?}", e); |
| 142 | + io::Error::new(io::ErrorKind::Other, "Invalid command line parameter") |
| 143 | + })?; |
| 144 | + |
| 145 | + if let Ok(lvl) = LevelFilter::from_str(config.log_level.as_str()) { |
| 146 | + log::set_max_level(lvl); |
| 147 | + } |
| 148 | + |
| 149 | + // Convert input files as firmware file format |
| 150 | + let ffs = create_firmware_files(&config)?; |
| 151 | + // Enroll the files into CFV |
| 152 | + enroll_files(config.input.as_str(), config.output, ffs)?; |
| 153 | + |
| 154 | + Ok(()) |
| 155 | +} |
| 156 | + |
| 157 | +// Build firmware files according to command line input |
| 158 | +// 0 / 1 public key file to be enrolled |
| 159 | +// 0 ~ n raw file read from system path to be enrolled |
| 160 | +fn create_firmware_files(config: &Config) -> io::Result<Vec<FirmwareRawFile>> { |
| 161 | + let mut files: Vec<FirmwareRawFile> = Vec::new(); |
| 162 | + |
| 163 | + if let Some(key) = &config.key { |
| 164 | + let ff_sb = create_key_file(key.as_str(), config.hash_alg.as_str())?; |
| 165 | + files.push(ff_sb); |
| 166 | + } |
| 167 | + |
| 168 | + for (guid, path) in &config.firmware_files { |
| 169 | + // Create a firmware file |
| 170 | + let mut f = FirmwareRawFile::new(guid.as_bytes()); |
| 171 | + let data = InputData::new(path, 1..=1024 * 1024, "firmware file")?; |
| 172 | + f.append(data.as_bytes()); |
| 173 | + files.push(f) |
| 174 | + } |
| 175 | + |
| 176 | + Ok(files) |
| 177 | +} |
0 commit comments