1 // Copyright 2023, 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 //! Error types used in libgbl. 16 17 use avb::{DescriptorError, IoError, SlotVerifyError}; 18 use core::{ 19 fmt::{Debug, Display, Formatter}, 20 num::TryFromIntError, 21 }; 22 23 /// A helper macro for declaring a composite enum type that simply wraps other types as entries. 24 /// It auto-generate `From<...>` implementation for each entry type. The type for each entry must 25 /// be different from each other. i.e.: 26 /// 27 /// ```rust 28 /// composite_enum! { 29 /// pub enum MyEnum { 30 /// Usize(usize), 31 /// I64(i64), 32 /// } 33 /// } 34 /// ``` 35 /// 36 /// expands to 37 /// 38 /// ```rust 39 /// pub enum MyEnum { 40 /// Usize(usize), 41 /// I64(i64), 42 /// } 43 /// 44 /// impl From<usize> for MyEnum { 45 /// fn from(ent: usize) -> MyEnum { 46 /// MyEnum::Usize(ent) 47 /// } 48 /// } 49 /// 50 /// impl From<i64> for MyEnum { 51 /// fn from(ent: i64) -> MyEnum { 52 /// MyEnum::I64(ent) 53 /// } 54 /// } 55 /// ``` 56 #[macro_export] 57 macro_rules! composite_enum { 58 ( 59 $(#[$outer:meta])* 60 $vis:vis enum $name:ident { 61 $( 62 $(#[$inner:ident $($args:tt)*])* 63 $ent:ident($ent_t:ty) 64 ),* 65 $(,)* 66 } 67 ) => { 68 #[allow(missing_docs)] 69 // Copy over enum declaration as it is. 70 $(#[$outer])* 71 $vis enum $name { 72 $( 73 $(#[$inner $($args)*])* 74 $ent($ent_t) 75 ),* 76 } 77 78 // Generate `From<...>` implementation. 79 composite_enum!{$name, $($ent($ent_t)),*} 80 }; 81 // `From<>` implementation generation. Base case. 82 ($name:ident, $ent:ident($ent_t:ty)) => { 83 impl From<$ent_t> for $name { 84 fn from(ent: $ent_t) -> $name { 85 $name::$ent(ent) 86 } 87 } 88 }; 89 // `From<>` implementation generation. Recursive case. 90 ($name:ident, $ent:ident($ent_t:ty), $($next:ident($next_t:ty)),+) => { 91 composite_enum!{$name, $ent($ent_t)} 92 composite_enum!{$name, $($next($next_t)),*} 93 }; 94 } 95 96 composite_enum! { 97 /// Top level error type that integrates errors from various dependency libraries. 98 #[derive(Debug, PartialEq, Eq)] 99 pub enum IntegrationError { 100 /// Failed to get descriptor from AvbMeta 101 AvbDescriptorError(DescriptorError), 102 AvbIoError(IoError), 103 /// Avb slot verification failed. 104 /// SlotVerifyError is used without verify data. 105 AvbSlotVerifyError(SlotVerifyError<'static>), 106 UnificationError(liberror::Error), 107 ZbiError(zbi::ZbiError), 108 TryFromIntError(TryFromIntError), 109 } 110 } 111 112 impl Display for IntegrationError { fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result113 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { 114 write!(f, "{:?}", self) 115 } 116 } 117 118 /// Helper type GBL functions will return. 119 pub type Result<T> = core::result::Result<T, IntegrationError>; 120