blob: 8b061505471c7801ca6ef87172133c7e0ce69b31 [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
15//! This module contains the error thrown by the payload verification API.
16
17use avb_bindgen::AvbSlotVerifyResult;
18
19use core::fmt;
20
21/// This error is the error part of `AvbSlotVerifyResult`.
22/// It is the error thrown by the payload verification API `verify_payload()`.
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub enum AvbSlotVerifyError {
25 /// AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT
26 InvalidArgument,
27 /// AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA
28 InvalidMetadata,
29 /// AVB_SLOT_VERIFY_RESULT_ERROR_IO
30 Io,
31 /// AVB_SLOT_VERIFY_RESULT_ERROR_OOM
32 Oom,
33 /// AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED
34 PublicKeyRejected,
35 /// AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX
36 RollbackIndex,
37 /// AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION
38 UnsupportedVersion,
39 /// AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION
40 Verification,
41}
42
43impl fmt::Display for AvbSlotVerifyError {
44 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45 match self {
46 Self::InvalidArgument => write!(f, "Invalid parameters."),
47 Self::InvalidMetadata => write!(f, "Invalid metadata."),
48 Self::Io => write!(f, "I/O error while trying to load data or get a rollback index."),
49 Self::Oom => write!(f, "Unable to allocate memory."),
50 Self::PublicKeyRejected => write!(f, "Public key rejected or data not signed."),
51 Self::RollbackIndex => write!(f, "Rollback index is less than its stored value."),
52 Self::UnsupportedVersion => write!(
53 f,
54 "Some of the metadata requires a newer version of libavb than what is in use."
55 ),
56 Self::Verification => write!(f, "Data does not verify."),
57 }
58 }
59}
60
61pub(crate) fn slot_verify_result_to_verify_payload_result(
62 result: AvbSlotVerifyResult,
63) -> Result<(), AvbSlotVerifyError> {
64 match result {
65 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_OK => Ok(()),
66 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_ARGUMENT => {
67 Err(AvbSlotVerifyError::InvalidArgument)
68 }
69 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_INVALID_METADATA => {
70 Err(AvbSlotVerifyError::InvalidMetadata)
71 }
72 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_IO => Err(AvbSlotVerifyError::Io),
73 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_OOM => Err(AvbSlotVerifyError::Oom),
74 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_PUBLIC_KEY_REJECTED => {
75 Err(AvbSlotVerifyError::PublicKeyRejected)
76 }
77 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_ROLLBACK_INDEX => {
78 Err(AvbSlotVerifyError::RollbackIndex)
79 }
80 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_UNSUPPORTED_VERSION => {
81 Err(AvbSlotVerifyError::UnsupportedVersion)
82 }
83 AvbSlotVerifyResult::AVB_SLOT_VERIFY_RESULT_ERROR_VERIFICATION => {
84 Err(AvbSlotVerifyError::Verification)
85 }
86 }
87}