• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 mod apex;
18 
19 use crate::instance::ApexData;
20 use crate::ioutil::wait_for_file;
21 use anyhow::Result;
22 use apex::verify;
23 use log::info;
24 use microdroid_metadata::{read_metadata, ApexPayload, Metadata};
25 use std::time::Duration;
26 
27 const PAYLOAD_METADATA_PATH: &str = "/dev/block/by-name/payload-metadata";
28 const WAIT_TIMEOUT: Duration = Duration::from_secs(10);
29 
30 /// Loads payload metadata from /dev/block/by-name/payload-metadata
load_metadata() -> Result<Metadata>31 pub fn load_metadata() -> Result<Metadata> {
32     info!("loading payload metadata...");
33     let file = wait_for_file(PAYLOAD_METADATA_PATH, WAIT_TIMEOUT)?;
34     read_metadata(file)
35 }
36 
37 /// Loads (name, public_key, root_digest) from payload APEXes
get_apex_data_from_payload(metadata: &Metadata) -> Result<Vec<ApexData>>38 pub fn get_apex_data_from_payload(metadata: &Metadata) -> Result<Vec<ApexData>> {
39     metadata
40         .apexes
41         .iter()
42         .map(|apex| {
43             let name = apex.name.clone();
44             let apex_path = format!("/dev/block/by-name/{}", apex.partition_name);
45             let result = verify(&apex_path)?;
46             Ok(ApexData {
47                 name,
48                 public_key: result.public_key,
49                 root_digest: result.root_digest,
50                 last_update_seconds: apex.last_update_seconds,
51                 is_factory: apex.is_factory,
52             })
53         })
54         .collect()
55 }
56 
57 /// Convert vector of ApexData into Metadata
to_metadata(apex_data: &[ApexData]) -> Metadata58 pub fn to_metadata(apex_data: &[ApexData]) -> Metadata {
59     Metadata {
60         apexes: apex_data
61             .iter()
62             .map(|data| ApexPayload {
63                 name: data.name.clone(),
64                 public_key: data.public_key.clone(),
65                 root_digest: data.root_digest.clone(),
66                 last_update_seconds: data.last_update_seconds,
67                 is_factory: data.is_factory,
68                 ..Default::default()
69             })
70             .collect(),
71         ..Default::default()
72     }
73 }
74