1 /* 2 * Copyright (C) 2021 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 crate::sys::*; 18 use crate::types::{c_long, c_void}; 19 20 /// Trusty system error 21 /// 22 /// Equivalent to the error codes from the Trusty kernel in `uapi/err.h`. These 23 /// error codes should only be used for interfacing with the kernel and non-Rust 24 /// libraries. Rust APIs should provide their own idiomatic error types. 25 #[repr(i32)] 26 #[non_exhaustive] 27 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 28 pub enum Error { 29 NoError = NO_ERROR as i32, 30 Generic = ERR_GENERIC, 31 NotFound = ERR_NOT_FOUND, 32 NotReady = ERR_NOT_READY, 33 NoMsg = ERR_NO_MSG, 34 NoMemory = ERR_NO_MEMORY, 35 AlreadyStarted = ERR_ALREADY_STARTED, 36 NotValid = ERR_NOT_VALID, 37 InvalidArgs = ERR_INVALID_ARGS, 38 NotEnoughBuffer = ERR_NOT_ENOUGH_BUFFER, 39 NotSuspended = ERR_NOT_SUSPENDED, 40 ObjectDestroyed = ERR_OBJECT_DESTROYED, 41 NotBlocked = ERR_NOT_BLOCKED, 42 TimedOut = ERR_TIMED_OUT, 43 AlreadyExists = ERR_ALREADY_EXISTS, 44 ChannelClosed = ERR_CHANNEL_CLOSED, 45 Offline = ERR_OFFLINE, 46 NotAllowed = ERR_NOT_ALLOWED, 47 BadPath = ERR_BAD_PATH, 48 AlreadyMounted = ERR_ALREADY_MOUNTED, 49 IO = ERR_IO, 50 NotDir = ERR_NOT_DIR, 51 NotFile = ERR_NOT_FILE, 52 RecurseTooDeep = ERR_RECURSE_TOO_DEEP, 53 NotSupported = ERR_NOT_SUPPORTED, 54 TooBig = ERR_TOO_BIG, 55 Cancelled = ERR_CANCELLED, 56 NotImplemented = ERR_NOT_IMPLEMENTED, 57 ChecksumFail = ERR_CHECKSUM_FAIL, 58 CrcFail = ERR_CRC_FAIL, 59 CmdUnknown = ERR_CMD_UNKNOWN, 60 BadState = ERR_BAD_STATE, 61 BadLen = ERR_BAD_LEN, 62 Busy = ERR_BUSY, 63 ThreadDetached = ERR_THREAD_DETACHED, 64 I2CNack = ERR_I2C_NACK, 65 AlreadyExpired = ERR_ALREADY_EXPIRED, 66 OutOfRange = ERR_OUT_OF_RANGE, 67 NotConfigured = ERR_NOT_CONFIGURED, 68 NotMounted = ERR_NOT_MOUNTED, 69 Fault = ERR_FAULT, 70 NoResources = ERR_NO_RESOURCES, 71 BadHandle = ERR_BAD_HANDLE, 72 AccessDenied = ERR_ACCESS_DENIED, 73 PartialWrite = ERR_PARTIAL_WRITE, 74 UserBase = ERR_USER_BASE, 75 } 76 77 impl Error { is_err(rc: c_long) -> bool78 pub fn is_err(rc: c_long) -> bool { 79 rc != NO_ERROR as c_long 80 } 81 is_ptr_err(ptr: *const c_void) -> bool82 pub fn is_ptr_err(ptr: *const c_void) -> bool { 83 ptr >= ERR_USER_BASE as usize as *const c_void 84 } 85 } 86 87 impl From<c_long> for Error { from(rc: c_long) -> Self88 fn from(rc: c_long) -> Self { 89 use Error::*; 90 91 if rc > i32::MAX as c_long || rc < i32::MIN as c_long { 92 return Generic; 93 } 94 match rc as i32 { 95 rc if rc == NO_ERROR as i32 => NoError, 96 ERR_GENERIC => Generic, 97 ERR_NOT_FOUND => NotFound, 98 ERR_NOT_READY => NotReady, 99 ERR_NO_MSG => NoMsg, 100 ERR_NO_MEMORY => NoMemory, 101 ERR_ALREADY_STARTED => AlreadyStarted, 102 ERR_NOT_VALID => NotValid, 103 ERR_INVALID_ARGS => InvalidArgs, 104 ERR_NOT_ENOUGH_BUFFER => NotEnoughBuffer, 105 ERR_NOT_SUSPENDED => NotSuspended, 106 ERR_OBJECT_DESTROYED => ObjectDestroyed, 107 ERR_NOT_BLOCKED => NotBlocked, 108 ERR_TIMED_OUT => TimedOut, 109 ERR_ALREADY_EXISTS => AlreadyExists, 110 ERR_CHANNEL_CLOSED => ChannelClosed, 111 ERR_OFFLINE => Offline, 112 ERR_NOT_ALLOWED => NotAllowed, 113 ERR_BAD_PATH => BadPath, 114 ERR_ALREADY_MOUNTED => AlreadyMounted, 115 ERR_IO => IO, 116 ERR_NOT_DIR => NotDir, 117 ERR_NOT_FILE => NotFile, 118 ERR_RECURSE_TOO_DEEP => RecurseTooDeep, 119 ERR_NOT_SUPPORTED => NotSupported, 120 ERR_TOO_BIG => TooBig, 121 ERR_CANCELLED => Cancelled, 122 ERR_NOT_IMPLEMENTED => NotImplemented, 123 ERR_CHECKSUM_FAIL => ChecksumFail, 124 ERR_CRC_FAIL => CrcFail, 125 ERR_CMD_UNKNOWN => CmdUnknown, 126 ERR_BAD_STATE => BadState, 127 ERR_BAD_LEN => BadLen, 128 ERR_BUSY => Busy, 129 ERR_THREAD_DETACHED => ThreadDetached, 130 ERR_I2C_NACK => I2CNack, 131 ERR_ALREADY_EXPIRED => AlreadyExpired, 132 ERR_OUT_OF_RANGE => OutOfRange, 133 ERR_NOT_CONFIGURED => NotConfigured, 134 ERR_NOT_MOUNTED => NotMounted, 135 ERR_FAULT => Fault, 136 ERR_NO_RESOURCES => NoResources, 137 ERR_BAD_HANDLE => BadHandle, 138 ERR_ACCESS_DENIED => AccessDenied, 139 ERR_PARTIAL_WRITE => PartialWrite, 140 ERR_USER_BASE => UserBase, 141 _ => Generic, 142 } 143 } 144 } 145