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, warn};
21 use microdroid_metadata::{read_metadata, ApexPayload, Metadata};
22 use std::time::Duration;
23 
24 const PAYLOAD_METADATA_PATH: &str = "/dev/block/by-name/payload-metadata";
25 const WAIT_TIMEOUT: Duration = Duration::from_secs(10);
26 
27 /// Loads payload metadata from /dev/block/by-name/payload-metadata
load_metadata() -> Result<Metadata>28 pub fn load_metadata() -> Result<Metadata> {
29     info!("loading payload metadata...");
30     let file = wait_for_file(PAYLOAD_METADATA_PATH, WAIT_TIMEOUT)?;
31     read_metadata(file)
32 }
33 
34 /// Loads (name, public_key, root_digest) from payload APEXes
get_apex_data_from_payload(metadata: &Metadata) -> Result<Vec<ApexData>>35 pub fn get_apex_data_from_payload(metadata: &Metadata) -> Result<Vec<ApexData>> {
36     metadata
37         .apexes
38         .iter()
39         .map(|apex| {
40             let apex_path = format!("/dev/block/by-name/{}", apex.partition_name);
41             let extracted = apexutil::verify(&apex_path)?;
42             if let Some(manifest_name) = &extracted.name {
43                 if &apex.name != manifest_name {
44                     warn!("Apex named {} is named {} in its manifest", apex.name, manifest_name);
45                 }
46             };
47             Ok(ApexData {
48                 name: apex.name.clone(),
49                 manifest_name: extracted.name,
50                 manifest_version: extracted.version,
51                 public_key: extracted.public_key,
52                 root_digest: extracted.root_digest,
53                 last_update_seconds: apex.last_update_seconds,
54                 is_factory: apex.is_factory,
55             })
56         })
57         .collect()
58 }
59 
60 /// Convert vector of ApexData into Metadata
to_metadata(apex_data: &[ApexData]) -> Metadata61 pub fn to_metadata(apex_data: &[ApexData]) -> Metadata {
62     Metadata {
63         apexes: apex_data
64             .iter()
65             .map(|data| ApexPayload {
66                 name: data.name.clone(),
67                 public_key: data.public_key.clone(),
68                 root_digest: data.root_digest.clone(),
69                 manifest_name: data.manifest_name.clone().unwrap_or_default(),
70                 manifest_version: data.manifest_version.unwrap_or_default(),
71                 last_update_seconds: data.last_update_seconds,
72                 is_factory: data.is_factory,
73                 ..Default::default()
74             })
75             .collect(),
76         ..Default::default()
77     }
78 }
79