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 //! This module contains the error thrown by the payload verification API
16 //! and other errors used in the library.
17 
18 use avb::{DescriptorError, SlotVerifyError};
19 use core::fmt;
20 
21 /// Wrapper around `SlotVerifyError` to add custom pvmfw errors.
22 /// It is the error thrown by the payload verification API `verify_payload()`.
23 #[derive(Debug, PartialEq, Eq)]
24 pub enum PvmfwVerifyError {
25     /// Passthrough `SlotVerifyError` with no `SlotVerifyData`.
26     AvbError(SlotVerifyError<'static>),
27     /// VBMeta has invalid descriptors.
28     InvalidDescriptors(DescriptorError),
29     /// Unknown vbmeta property.
30     UnknownVbmetaProperty,
31 }
32 
33 impl From<SlotVerifyError<'_>> for PvmfwVerifyError {
from(error: SlotVerifyError) -> Self34     fn from(error: SlotVerifyError) -> Self {
35         // We don't use verification data on failure, drop it to get a `'static` lifetime.
36         Self::AvbError(error.without_verify_data())
37     }
38 }
39 
40 impl From<DescriptorError> for PvmfwVerifyError {
from(error: DescriptorError) -> Self41     fn from(error: DescriptorError) -> Self {
42         Self::InvalidDescriptors(error)
43     }
44 }
45 
46 impl fmt::Display for PvmfwVerifyError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result47     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48         match self {
49             Self::AvbError(e) => write!(f, "{}", e),
50             Self::InvalidDescriptors(e) => {
51                 write!(f, "VBMeta has invalid descriptors. Error: {:?}", e)
52             }
53             Self::UnknownVbmetaProperty => write!(f, "Unknown vbmeta property"),
54         }
55     }
56 }
57