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_bindgen::{AvbIOResult, AvbSlotVerifyResult};
19 
20 use core::fmt;
21 
22 /// This error is the error part of `AvbSlotVerifyResult`.
23 /// It is the error thrown by the payload verification API `verify_payload()`.
24 #[derive(Clone, Debug, PartialEq, Eq)]
25 pub enum AvbSlotVerifyError {
26     /// AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT
27     InvalidArgument,
28     /// AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA
29     InvalidMetadata,
30     /// AVB_SLOT_VERIFY_RESULT_ERROR_IO
31     Io,
32     /// AVB_SLOT_VERIFY_RESULT_ERROR_OOM
33     Oom,
34     /// AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED
35     PublicKeyRejected,
36     /// AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX
37     RollbackIndex,
38     /// AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION
39     UnsupportedVersion,
40     /// AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION
41     Verification,
42 }
43 
44 impl fmt::Display for AvbSlotVerifyError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result45     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46         match self {
47             Self::InvalidArgument => write!(f, "Invalid parameters."),
48             Self::InvalidMetadata => write!(f, "Invalid metadata."),
49             Self::Io => write!(f, "I/O error while trying to load data or get a rollback index."),
50             Self::Oom => write!(f, "Unable to allocate memory."),
51             Self::PublicKeyRejected => write!(f, "Public key rejected or data not signed."),
52             Self::RollbackIndex => write!(f, "Rollback index is less than its stored value."),
53             Self::UnsupportedVersion => write!(
54                 f,
55                 "Some of the metadata requires a newer version of libavb than what is in use."
56             ),
57             Self::Verification => write!(f, "Data does not verify."),
58         }
59     }
60 }
61 
slot_verify_result_to_verify_payload_result( result: AvbSlotVerifyResult, ) -> Result<(), AvbSlotVerifyError>62 pub(crate) fn slot_verify_result_to_verify_payload_result(
63     result: AvbSlotVerifyResult,
64 ) -> Result<(), AvbSlotVerifyError> {
65     match result {
66         AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_OK => Ok(()),
67         AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT => {
68             Err(AvbSlotVerifyError::InvalidArgument)
69         }
70         AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA => {
71             Err(AvbSlotVerifyError::InvalidMetadata)
72         }
73         AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_IO => Err(AvbSlotVerifyError::Io),
74         AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_OOM => Err(AvbSlotVerifyError::Oom),
75         AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED => {
76             Err(AvbSlotVerifyError::PublicKeyRejected)
77         }
78         AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX => {
79             Err(AvbSlotVerifyError::RollbackIndex)
80         }
81         AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION => {
82             Err(AvbSlotVerifyError::UnsupportedVersion)
83         }
84         AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION => {
85             Err(AvbSlotVerifyError::Verification)
86         }
87     }
88 }
89 
90 #[derive(Debug)]
91 pub(crate) enum AvbIOError {
92     /// AVB_IO_RESULT_ERROR_OOM,
93     #[allow(dead_code)]
94     Oom,
95     /// AVB_IO_RESULT_ERROR_IO,
96     #[allow(dead_code)]
97     Io,
98     /// AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION,
99     NoSuchPartition,
100     /// AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION,
101     RangeOutsidePartition,
102     /// AVB_IO_RESULT_ERROR_NO_SUCH_VALUE,
103     NoSuchValue,
104     /// AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE,
105     InvalidValueSize,
106     /// AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE,
107     #[allow(dead_code)]
108     InsufficientSpace,
109 }
110 
111 impl From<AvbIOError> for AvbIOResult {
from(error: AvbIOError) -> Self112     fn from(error: AvbIOError) -> Self {
113         match error {
114             AvbIOError::Oom => AvbIOResult::AVB_IO_RESULT_ERROR_OOM,
115             AvbIOError::Io => AvbIOResult::AVB_IO_RESULT_ERROR_IO,
116             AvbIOError::NoSuchPartition => AvbIOResult::AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION,
117             AvbIOError::RangeOutsidePartition => {
118                 AvbIOResult::AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION
119             }
120             AvbIOError::NoSuchValue => AvbIOResult::AVB_IO_RESULT_ERROR_NO_SUCH_VALUE,
121             AvbIOError::InvalidValueSize => AvbIOResult::AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE,
122             AvbIOError::InsufficientSpace => AvbIOResult::AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE,
123         }
124     }
125 }
126 
to_avb_io_result(result: Result<(), AvbIOError>) -> AvbIOResult127 pub(crate) fn to_avb_io_result(result: Result<(), AvbIOError>) -> AvbIOResult {
128     result.map_or_else(|e| e.into(), |_| AvbIOResult::AVB_IO_RESULT_OK)
129 }
130