Jooyung Han | 7a343f9 | 2021-09-08 22:53:11 +0900 | [diff] [blame] | 1 | // Copyright 2021, 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 | //! Routines for handling payload |
| 16 | |
| 17 | use crate::instance::ApexData; |
| 18 | use crate::ioutil::wait_for_file; |
| 19 | use anyhow::Result; |
| 20 | use log::info; |
| 21 | use microdroid_metadata::{read_metadata, Metadata}; |
| 22 | use std::fs::File; |
| 23 | use std::io::Read; |
| 24 | use std::time::Duration; |
| 25 | use zip::ZipArchive; |
| 26 | |
| 27 | const APEX_PUBKEY_ENTRY: &str = "apex_pubkey"; |
| 28 | const PAYLOAD_METADATA_PATH: &str = "/dev/block/by-name/payload-metadata"; |
| 29 | const WAIT_TIMEOUT: Duration = Duration::from_secs(10); |
| 30 | |
| 31 | /// Loads payload metadata from /dev/block/by-name/payload-metadata |
| 32 | pub fn load_metadata() -> Result<Metadata> { |
| 33 | info!("loading payload metadata..."); |
| 34 | let file = wait_for_file(PAYLOAD_METADATA_PATH, WAIT_TIMEOUT)?; |
| 35 | read_metadata(file) |
| 36 | } |
| 37 | |
| 38 | /// Loads (name, pubkey) from payload apexes and returns them as sorted by name. |
| 39 | pub fn get_apex_data_from_payload(metadata: &Metadata) -> Result<Vec<ApexData>> { |
| 40 | let mut apex_data: Vec<ApexData> = metadata |
| 41 | .apexes |
| 42 | .iter() |
| 43 | .map(|apex| { |
| 44 | let name = apex.name.clone(); |
| 45 | let partition = format!("/dev/block/by-name/{}", apex.partition_name); |
| 46 | let pubkey = get_pubkey_from_apex(&partition)?; |
| 47 | Ok(ApexData { name, pubkey }) |
| 48 | }) |
| 49 | .collect::<Result<Vec<_>>>()?; |
| 50 | apex_data.sort_by(|a, b| a.name.cmp(&b.name)); |
| 51 | Ok(apex_data) |
| 52 | } |
| 53 | |
| 54 | fn get_pubkey_from_apex(path: &str) -> Result<Vec<u8>> { |
| 55 | let f = File::open(path)?; |
| 56 | let mut z = ZipArchive::new(f)?; |
| 57 | let mut pubkey_file = z.by_name(APEX_PUBKEY_ENTRY)?; |
| 58 | let mut pubkey = Vec::new(); |
| 59 | pubkey_file.read_to_end(&mut pubkey)?; |
| 60 | Ok(pubkey) |
| 61 | } |