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