Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Tool for handling AVMD blobs. |
| 16 | |
| 17 | use anyhow::{anyhow, bail, Result}; |
| 18 | use apexutil::get_payload_vbmeta_image_hash; |
Alice Wang | 0cafa14 | 2022-09-23 15:17:02 +0000 | [diff] [blame] | 19 | use apkverify::get_apk_digest; |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 20 | use avmd::{ApkDescriptor, Avmd, Descriptor, ResourceIdentifier, VbMetaDescriptor}; |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 21 | use clap::{ |
| 22 | builder::ValueParser, |
| 23 | parser::{Indices, ValuesRef}, |
| 24 | Arg, ArgAction, ArgMatches, Command, |
| 25 | }; |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 26 | use serde::ser::Serialize; |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 27 | use std::{fs::File, path::PathBuf}; |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 28 | use vbmeta::VbMetaImage; |
| 29 | |
| 30 | fn get_vbmeta_image_hash(file: &str) -> Result<Vec<u8>> { |
| 31 | let img = VbMetaImage::verify_path(file)?; |
| 32 | Ok(img.hash().ok_or_else(|| anyhow!("No hash as VBMeta image isn't signed"))?.to_vec()) |
| 33 | } |
| 34 | |
| 35 | /// Iterate over a set of argument values, that could be empty or come in |
| 36 | /// (<index>, <namespace>, <name>, <file>) tuple. |
| 37 | struct NamespaceNameFileIterator<'a> { |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 38 | indices: Option<Indices<'a>>, |
| 39 | values: Option<ValuesRef<'a, String>>, |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | impl<'a> NamespaceNameFileIterator<'a> { |
| 43 | fn new(args: &'a ArgMatches, name: &'a str) -> Self { |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 44 | NamespaceNameFileIterator { indices: args.indices_of(name), values: args.get_many(name) } |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | impl<'a> Iterator for NamespaceNameFileIterator<'a> { |
| 49 | type Item = (usize, &'a str, &'a str, &'a str); |
| 50 | |
| 51 | fn next(&mut self) -> Option<Self::Item> { |
| 52 | match (self.indices.as_mut(), self.values.as_mut()) { |
| 53 | (Some(indices), Some(values)) => { |
| 54 | match (indices.nth(2), values.next(), values.next(), values.next()) { |
| 55 | (Some(index), Some(namespace), Some(name), Some(file)) => { |
| 56 | Some((index, namespace, name, file)) |
| 57 | } |
| 58 | _ => None, |
| 59 | } |
| 60 | } |
| 61 | _ => None, |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn create(args: &ArgMatches) -> Result<()> { |
| 67 | // Store descriptors in the order they were given in the arguments |
| 68 | // TODO: instead, group them by namespace? |
| 69 | let mut descriptors = std::collections::BTreeMap::new(); |
| 70 | for (i, namespace, name, file) in NamespaceNameFileIterator::new(args, "vbmeta") { |
| 71 | descriptors.insert( |
| 72 | i, |
| 73 | Descriptor::VbMeta(VbMetaDescriptor { |
| 74 | resource: ResourceIdentifier::new(namespace, name), |
| 75 | vbmeta_digest: get_vbmeta_image_hash(file)?, |
| 76 | }), |
| 77 | ); |
| 78 | } |
| 79 | for (i, namespace, name, file) in NamespaceNameFileIterator::new(args, "apk") { |
| 80 | let file = File::open(file)?; |
Alice Wang | 9df25eb | 2022-09-26 08:35:16 +0000 | [diff] [blame] | 81 | let (signature_algorithm_id, apk_digest) = get_apk_digest(file, /*verify=*/ true)?; |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 82 | descriptors.insert( |
| 83 | i, |
| 84 | Descriptor::Apk(ApkDescriptor { |
| 85 | resource: ResourceIdentifier::new(namespace, name), |
| 86 | signature_algorithm_id, |
| 87 | apk_digest: apk_digest.to_vec(), |
| 88 | }), |
| 89 | ); |
| 90 | } |
| 91 | for (i, namespace, name, file) in NamespaceNameFileIterator::new(args, "apex-payload") { |
| 92 | descriptors.insert( |
| 93 | i, |
| 94 | Descriptor::VbMeta(VbMetaDescriptor { |
| 95 | resource: ResourceIdentifier::new(namespace, name), |
| 96 | vbmeta_digest: get_payload_vbmeta_image_hash(file)?, |
| 97 | }), |
| 98 | ); |
| 99 | } |
| 100 | let avmd = Avmd::new(descriptors.into_values().collect()); |
| 101 | let mut bytes = Vec::new(); |
| 102 | avmd.serialize( |
| 103 | &mut serde_cbor::Serializer::new(&mut serde_cbor::ser::IoWrite::new(&mut bytes)) |
| 104 | .packed_format() |
| 105 | .legacy_enums(), |
| 106 | )?; |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 107 | std::fs::write(args.get_one::<PathBuf>("file").unwrap(), &bytes)?; |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 108 | Ok(()) |
| 109 | } |
| 110 | |
| 111 | fn dump(args: &ArgMatches) -> Result<()> { |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 112 | let file = std::fs::read(args.get_one::<PathBuf>("file").unwrap())?; |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 113 | let avmd: Avmd = serde_cbor::from_slice(&file)?; |
| 114 | println!("{}", avmd); |
| 115 | Ok(()) |
| 116 | } |
| 117 | |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 118 | fn clap_command() -> Command { |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 119 | let namespace_name_file = ["namespace", "name", "file"]; |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 120 | |
| 121 | Command::new("avmdtool") |
| 122 | .subcommand_required(true) |
| 123 | .arg_required_else_help(true) |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 124 | .subcommand( |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 125 | Command::new("create") |
| 126 | .arg_required_else_help(true) |
| 127 | .arg(Arg::new("file").value_parser(ValueParser::path_buf()).required(true)) |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 128 | .arg( |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 129 | Arg::new("vbmeta") |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 130 | .long("vbmeta") |
Chris Wailes | 7526962 | 2022-12-05 23:01:44 -0800 | [diff] [blame] | 131 | .value_names(namespace_name_file) |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 132 | .num_args(3) |
| 133 | .action(ArgAction::Append), |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 134 | ) |
| 135 | .arg( |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 136 | Arg::new("apk") |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 137 | .long("apk") |
Chris Wailes | 7526962 | 2022-12-05 23:01:44 -0800 | [diff] [blame] | 138 | .value_names(namespace_name_file) |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 139 | .num_args(3) |
| 140 | .action(ArgAction::Append), |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 141 | ) |
| 142 | .arg( |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 143 | Arg::new("apex-payload") |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 144 | .long("apex-payload") |
Chris Wailes | 7526962 | 2022-12-05 23:01:44 -0800 | [diff] [blame] | 145 | .value_names(namespace_name_file) |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 146 | .num_args(3) |
| 147 | .action(ArgAction::Append), |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 148 | ), |
| 149 | ) |
| 150 | .subcommand( |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 151 | Command::new("dump") |
| 152 | .arg_required_else_help(true) |
| 153 | .arg(Arg::new("file").value_parser(ValueParser::path_buf()).required(true)), |
| 154 | ) |
| 155 | } |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 156 | |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 157 | fn main() -> Result<()> { |
| 158 | let args = clap_command().get_matches(); |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 159 | match args.subcommand() { |
Jeff Vander Stoep | a8dc271 | 2022-07-29 02:33:45 +0200 | [diff] [blame] | 160 | Some(("create", sub_args)) => create(sub_args)?, |
| 161 | Some(("dump", sub_args)) => dump(sub_args)?, |
Alice Wang | e089a21 | 2022-07-18 11:24:03 +0000 | [diff] [blame] | 162 | _ => bail!("Invalid arguments"), |
| 163 | } |
| 164 | Ok(()) |
| 165 | } |
Andrew Walbran | aa1efc4 | 2022-08-10 13:33:57 +0000 | [diff] [blame] | 166 | |
| 167 | #[cfg(test)] |
| 168 | mod tests { |
| 169 | use super::*; |
| 170 | |
| 171 | #[test] |
| 172 | fn verify_command() { |
| 173 | // Check that the command parsing has been configured in a valid way. |
| 174 | clap_command().debug_assert(); |
| 175 | } |
| 176 | } |