blob: 1add3686c943fcb21b7018edf21a15614f06ccb2 [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
Alice Wang18655632023-01-18 09:11:34 +000018use avb_bindgen::{AvbIOResult, AvbSlotVerifyResult};
Alice Wang45d98fa2023-01-11 09:39:45 +000019
20use 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)]
25pub 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,
Alice Wang14973b22023-05-22 14:54:16 +000042 /// VBMeta has invalid descriptors.
43 InvalidDescriptors(AvbIOError),
Alice Wang45d98fa2023-01-11 09:39:45 +000044}
45
46impl fmt::Display for AvbSlotVerifyError {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 match self {
49 Self::InvalidArgument => write!(f, "Invalid parameters."),
50 Self::InvalidMetadata => write!(f, "Invalid metadata."),
51 Self::Io => write!(f, "I/O error while trying to load data or get a rollback index."),
52 Self::Oom => write!(f, "Unable to allocate memory."),
53 Self::PublicKeyRejected => write!(f, "Public key rejected or data not signed."),
54 Self::RollbackIndex => write!(f, "Rollback index is less than its stored value."),
55 Self::UnsupportedVersion => write!(
56 f,
57 "Some of the metadata requires a newer version of libavb than what is in use."
58 ),
59 Self::Verification => write!(f, "Data does not verify."),
Alice Wang14973b22023-05-22 14:54:16 +000060 Self::InvalidDescriptors(e) => {
61 write!(f, "VBMeta has invalid descriptors. Error: {:?}", e)
62 }
Alice Wang45d98fa2023-01-11 09:39:45 +000063 }
64 }
65}
66
67pub(crate) fn slot_verify_result_to_verify_payload_result(
68 result: AvbSlotVerifyResult,
69) -> Result<(), AvbSlotVerifyError> {
70 match result {
71 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_OK => Ok(()),
72 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT => {
73 Err(AvbSlotVerifyError::InvalidArgument)
74 }
75 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA => {
76 Err(AvbSlotVerifyError::InvalidMetadata)
77 }
78 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_IO => Err(AvbSlotVerifyError::Io),
79 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_OOM => Err(AvbSlotVerifyError::Oom),
80 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED => {
81 Err(AvbSlotVerifyError::PublicKeyRejected)
82 }
83 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX => {
84 Err(AvbSlotVerifyError::RollbackIndex)
85 }
86 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION => {
87 Err(AvbSlotVerifyError::UnsupportedVersion)
88 }
89 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION => {
90 Err(AvbSlotVerifyError::Verification)
91 }
92 }
93}
Alice Wang18655632023-01-18 09:11:34 +000094
Alice Wang14973b22023-05-22 14:54:16 +000095/// AVB IO Error.
96#[derive(Clone, Debug, PartialEq, Eq)]
97pub enum AvbIOError {
Alice Wang18655632023-01-18 09:11:34 +000098 /// AVB_IO_RESULT_ERROR_OOM,
99 #[allow(dead_code)]
100 Oom,
101 /// AVB_IO_RESULT_ERROR_IO,
Alice Wang18655632023-01-18 09:11:34 +0000102 Io,
103 /// AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION,
104 NoSuchPartition,
105 /// AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION,
106 RangeOutsidePartition,
107 /// AVB_IO_RESULT_ERROR_NO_SUCH_VALUE,
108 NoSuchValue,
109 /// AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE,
110 InvalidValueSize,
111 /// AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE,
112 #[allow(dead_code)]
113 InsufficientSpace,
114}
115
116impl From<AvbIOError> for AvbIOResult {
117 fn from(error: AvbIOError) -> Self {
118 match error {
119 AvbIOError::Oom => AvbIOResult::AVB_IO_RESULT_ERROR_OOM,
120 AvbIOError::Io => AvbIOResult::AVB_IO_RESULT_ERROR_IO,
121 AvbIOError::NoSuchPartition => AvbIOResult::AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION,
122 AvbIOError::RangeOutsidePartition => {
123 AvbIOResult::AVB_IO_RESULT_ERROR_RANGE_OUTSIDE_PARTITION
124 }
125 AvbIOError::NoSuchValue => AvbIOResult::AVB_IO_RESULT_ERROR_NO_SUCH_VALUE,
126 AvbIOError::InvalidValueSize => AvbIOResult::AVB_IO_RESULT_ERROR_INVALID_VALUE_SIZE,
127 AvbIOError::InsufficientSpace => AvbIOResult::AVB_IO_RESULT_ERROR_INSUFFICIENT_SPACE,
128 }
129 }
130}
131
132pub(crate) fn to_avb_io_result(result: Result<(), AvbIOError>) -> AvbIOResult {
133 result.map_or_else(|e| e.into(), |_| AvbIOResult::AVB_IO_RESULT_OK)
134}