blob: 2e1950abbb332a2aafaa001b6fc9c48073190654 [file] [log] [blame]
Alice Wang45d98fa2023-01-11 09:39:45 +00001// 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
Alice Wang18655632023-01-18 09:11:34 +000015//! This module contains the error thrown by the payload verification API
16//! and other errors used in the library.
Alice Wang45d98fa2023-01-11 09:39:45 +000017
David Pursellc420c8b2024-01-17 10:52:19 -080018use avb::{DescriptorError, SlotVerifyError};
Alice Wang45d98fa2023-01-11 09:39:45 +000019use core::fmt;
20
David Pursellb59bcc42023-11-10 16:59:19 -080021/// Wrapper around `SlotVerifyError` to add custom pvmfw errors.
Alice Wang45d98fa2023-01-11 09:39:45 +000022/// It is the error thrown by the payload verification API `verify_payload()`.
David Pursell009580b2023-11-20 17:38:09 -080023#[derive(Debug, PartialEq, Eq)]
David Pursella7c727b2023-08-14 16:24:40 -070024pub enum PvmfwVerifyError {
David Pursellb59bcc42023-11-10 16:59:19 -080025 /// Passthrough `SlotVerifyError` with no `SlotVerifyData`.
26 AvbError(SlotVerifyError<'static>),
Alice Wang14973b22023-05-22 14:54:16 +000027 /// VBMeta has invalid descriptors.
David Pursellc420c8b2024-01-17 10:52:19 -080028 InvalidDescriptors(DescriptorError),
David Pursella7c727b2023-08-14 16:24:40 -070029 /// Unknown vbmeta property.
Alice Wangab0d0202023-05-17 08:07:41 +000030 UnknownVbmetaProperty,
Alice Wang45d98fa2023-01-11 09:39:45 +000031}
32
David Pursellb59bcc42023-11-10 16:59:19 -080033impl From<SlotVerifyError<'_>> for PvmfwVerifyError {
34 fn from(error: SlotVerifyError) -> Self {
David Pursell009580b2023-11-20 17:38:09 -080035 // We don't use verification data on failure, drop it to get a `'static` lifetime.
36 Self::AvbError(error.without_verify_data())
David Pursella7c727b2023-08-14 16:24:40 -070037 }
38}
39
David Pursellc420c8b2024-01-17 10:52:19 -080040impl From<DescriptorError> for PvmfwVerifyError {
41 fn from(error: DescriptorError) -> Self {
42 Self::InvalidDescriptors(error)
43 }
44}
45
David Pursella7c727b2023-08-14 16:24:40 -070046impl fmt::Display for PvmfwVerifyError {
Alice Wang45d98fa2023-01-11 09:39:45 +000047 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 match self {
David Pursella7c727b2023-08-14 16:24:40 -070049 Self::AvbError(e) => write!(f, "{}", e),
Alice Wang14973b22023-05-22 14:54:16 +000050 Self::InvalidDescriptors(e) => {
51 write!(f, "VBMeta has invalid descriptors. Error: {:?}", e)
52 }
Alice Wangab0d0202023-05-17 08:07:41 +000053 Self::UnknownVbmetaProperty => write!(f, "Unknown vbmeta property"),
Alice Wang45d98fa2023-01-11 09:39:45 +000054 }
55 }
56}