1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 use alloc::collections::TryReserveError; 18 use core::num::TryFromIntError; 19 use diced_open_dice::DiceError; 20 use tipc::TipcError; 21 use trusty_std::alloc::AllocError; 22 use trusty_sys::Error; 23 24 #[derive(Debug, PartialEq)] 25 pub enum HwBccError { 26 NotAllowed, 27 BadLen, 28 OutOfBounds, 29 AllocError, 30 InvalidCmdResponse, 31 TryFromIntError, 32 Tipc(TipcError), 33 System(Error), 34 } 35 36 impl From<TipcError> for HwBccError { from(err: TipcError) -> Self37 fn from(err: TipcError) -> Self { 38 HwBccError::Tipc(err) 39 } 40 } 41 42 impl From<Error> for HwBccError { from(err: Error) -> Self43 fn from(err: Error) -> Self { 44 HwBccError::System(err) 45 } 46 } 47 48 impl From<TryFromIntError> for HwBccError { from(_err: TryFromIntError) -> Self49 fn from(_err: TryFromIntError) -> Self { 50 HwBccError::OutOfBounds 51 } 52 } 53 54 impl From<AllocError> for HwBccError { from(_err: AllocError) -> Self55 fn from(_err: AllocError) -> Self { 56 HwBccError::AllocError 57 } 58 } 59 60 impl From<TryReserveError> for HwBccError { from(_err: TryReserveError) -> Self61 fn from(_err: TryReserveError) -> Self { 62 HwBccError::AllocError 63 } 64 } 65 66 impl From<DiceError> for HwBccError { from(dice_error: DiceError) -> Self67 fn from(dice_error: DiceError) -> Self { 68 match dice_error { 69 DiceError::InvalidInput => HwBccError::System(trusty_sys::Error::InvalidArgs), 70 DiceError::BufferTooSmall(_) => HwBccError::System(trusty_sys::Error::NotEnoughBuffer), 71 DiceError::PlatformError => HwBccError::System(trusty_sys::Error::Generic), 72 DiceError::UnsupportedKeyAlgorithm(_) => { 73 HwBccError::System(trusty_sys::Error::NotSupported) 74 } 75 DiceError::MemoryAllocationError => HwBccError::System(trusty_sys::Error::NoMemory), 76 DiceError::DiceChainNotFound => HwBccError::System(trusty_sys::Error::InvalidArgs), 77 } 78 } 79 } 80