blob: 62bf239799f69f627e846b485c9d62cfdb03bb28 [file] [log] [blame]
Alice Wang167ab3f2023-01-23 13:39:25 +00001// Copyright 2023, 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
David Pursellb59bcc42023-11-10 16:59:19 -080015//! Structs and functions relating to AVB callback operations.
Alice Wang167ab3f2023-01-23 13:39:25 +000016
Alice Wang167ab3f2023-01-23 13:39:25 +000017use crate::partition::PartitionName;
David Pursellb59bcc42023-11-10 16:59:19 -080018use avb::{
19 slot_verify, HashtreeErrorMode, IoError, IoResult, PublicKeyForPartitionInfo, SlotVerifyData,
20 SlotVerifyFlags, SlotVerifyResult,
Alice Wang167ab3f2023-01-23 13:39:25 +000021};
David Pursellb59bcc42023-11-10 16:59:19 -080022use core::ffi::CStr;
Alice Wang167ab3f2023-01-23 13:39:25 +000023
24pub(crate) struct Payload<'a> {
25 kernel: &'a [u8],
26 initrd: Option<&'a [u8]>,
27 trusted_public_key: &'a [u8],
28}
29
Alice Wang167ab3f2023-01-23 13:39:25 +000030impl<'a> Payload<'a> {
31 pub(crate) fn new(
32 kernel: &'a [u8],
33 initrd: Option<&'a [u8]>,
34 trusted_public_key: &'a [u8],
35 ) -> Self {
36 Self { kernel, initrd, trusted_public_key }
37 }
38
David Pursellb59bcc42023-11-10 16:59:19 -080039 fn get_partition(&self, partition_name: &CStr) -> IoResult<&[u8]> {
Alice Wang167ab3f2023-01-23 13:39:25 +000040 match partition_name.try_into()? {
41 PartitionName::Kernel => Ok(self.kernel),
42 PartitionName::InitrdNormal | PartitionName::InitrdDebug => {
David Pursellb59bcc42023-11-10 16:59:19 -080043 self.initrd.ok_or(IoError::NoSuchPartition)
Alice Wang167ab3f2023-01-23 13:39:25 +000044 }
45 }
46 }
47}
48
David Pursellb59bcc42023-11-10 16:59:19 -080049/// Pvmfw customized operations used in the verification.
50pub(crate) struct Ops<'a> {
51 payload: &'a Payload<'a>,
Alice Wang167ab3f2023-01-23 13:39:25 +000052}
53
David Pursellb59bcc42023-11-10 16:59:19 -080054impl<'a> Ops<'a> {
55 pub(crate) fn new(payload: &'a Payload<'a>) -> Self {
56 Self { payload }
57 }
58
Alice Wang167ab3f2023-01-23 13:39:25 +000059 pub(crate) fn verify_partition(
60 &mut self,
61 partition_name: &CStr,
David Pursell09bfc852024-02-02 11:24:24 -080062 ) -> SlotVerifyResult<SlotVerifyData<'a>> {
David Pursellb59bcc42023-11-10 16:59:19 -080063 slot_verify(
64 self,
65 &[partition_name],
66 None, // No partition slot suffix.
67 SlotVerifyFlags::AVB_SLOT_VERIFY_FLAGS_NONE,
68 HashtreeErrorMode::AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
69 )
Alice Wang167ab3f2023-01-23 13:39:25 +000070 }
71}
72
David Pursell09bfc852024-02-02 11:24:24 -080073impl<'a> avb::Ops<'a> for Ops<'a> {
David Pursellb59bcc42023-11-10 16:59:19 -080074 fn read_from_partition(
75 &mut self,
76 partition: &CStr,
77 offset: i64,
78 buffer: &mut [u8],
79 ) -> IoResult<usize> {
80 let partition = self.payload.get_partition(partition)?;
81 copy_data_to_dst(partition, offset, buffer)?;
82 Ok(buffer.len())
83 }
84
David Pursell09bfc852024-02-02 11:24:24 -080085 fn get_preloaded_partition(&mut self, partition: &CStr) -> IoResult<&'a [u8]> {
David Pursellb59bcc42023-11-10 16:59:19 -080086 self.payload.get_partition(partition)
87 }
88
89 fn validate_vbmeta_public_key(
90 &mut self,
91 public_key: &[u8],
92 _public_key_metadata: Option<&[u8]>,
93 ) -> IoResult<bool> {
94 // The public key metadata is not used when we build the VBMeta.
95 Ok(self.payload.trusted_public_key == public_key)
96 }
97
98 fn read_rollback_index(&mut self, _rollback_index_location: usize) -> IoResult<u64> {
99 // TODO(291213394) : Refine this comment once capability for rollback protection is defined.
100 // pvmfw does not compare stored_rollback_index with rollback_index for Antirollback
101 // protection. Hence, we set `out_rollback_index` to 0 to ensure that the rollback_index
102 // (including default: 0) is never smaller than it, thus the rollback index check will pass.
103 Ok(0)
104 }
105
106 fn write_rollback_index(
107 &mut self,
108 _rollback_index_location: usize,
109 _index: u64,
110 ) -> IoResult<()> {
111 Err(IoError::NotImplemented)
112 }
113
114 fn read_is_device_unlocked(&mut self) -> IoResult<bool> {
115 Ok(false)
116 }
117
118 fn get_size_of_partition(&mut self, partition: &CStr) -> IoResult<u64> {
119 let partition = self.payload.get_partition(partition)?;
120 u64::try_from(partition.len()).map_err(|_| IoError::InvalidValueSize)
121 }
122
123 fn read_persistent_value(&mut self, _name: &CStr, _value: &mut [u8]) -> IoResult<usize> {
124 Err(IoError::NotImplemented)
125 }
126
127 fn write_persistent_value(&mut self, _name: &CStr, _value: &[u8]) -> IoResult<()> {
128 Err(IoError::NotImplemented)
129 }
130
131 fn erase_persistent_value(&mut self, _name: &CStr) -> IoResult<()> {
132 Err(IoError::NotImplemented)
133 }
134
135 fn validate_public_key_for_partition(
136 &mut self,
137 _partition: &CStr,
138 _public_key: &[u8],
139 _public_key_metadata: Option<&[u8]>,
140 ) -> IoResult<PublicKeyForPartitionInfo> {
141 Err(IoError::NotImplemented)
142 }
Alice Wang167ab3f2023-01-23 13:39:25 +0000143}
144
David Pursellb59bcc42023-11-10 16:59:19 -0800145fn copy_data_to_dst(src: &[u8], offset: i64, dst: &mut [u8]) -> IoResult<()> {
146 let start = to_copy_start(offset, src.len()).ok_or(IoError::InvalidValueSize)?;
147 let end = start.checked_add(dst.len()).ok_or(IoError::InvalidValueSize)?;
148 dst.copy_from_slice(src.get(start..end).ok_or(IoError::RangeOutsidePartition)?);
Alice Wang167ab3f2023-01-23 13:39:25 +0000149 Ok(())
150}
151
152fn to_copy_start(offset: i64, len: usize) -> Option<usize> {
153 usize::try_from(offset)
154 .ok()
155 .or_else(|| isize::try_from(offset).ok().and_then(|v| len.checked_add_signed(v)))
156}