aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: da1fa80b15d3ecee1ce5dc91fdb1273ba745ee2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2022 the authors.
// This project is dual-licensed under Apache 2.0 and MIT terms.
// See LICENSE-APACHE and LICENSE-MIT for details.

//! PSCI error codes.

pub const SUCCESS: i32 = 0;
pub const NOT_SUPPORTED: i32 = -1;
pub const INVALID_PARAMETERS: i32 = -2;
pub const DENIED: i32 = -3;
pub const ALREADY_ON: i32 = -4;
pub const ON_PENDING: i32 = -5;
pub const INTERNAL_FAILURE: i32 = -6;
pub const NOT_PRESENT: i32 = -7;
pub const DISABLED: i32 = -8;
pub const INVALID_ADDRESS: i32 = -9;

/// Standard PSCI errors.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Error {
    NotSupported,
    InvalidParameters,
    Denied,
    AlreadyOn,
    OnPending,
    InternalFailure,
    NotPresent,
    Disabled,
    InvalidAddress,
    /// An unexpected return value from a PSCI function.
    Unknown(i32),
}

pub(crate) fn success_or_error_32(value: u32) -> Result<(), Error> {
    success_or_error(value as i32)
}

pub(crate) fn success_or_error_64(value: u64) -> Result<(), Error> {
    success_or_error(value as i32)
}

fn success_or_error(value: i32) -> Result<(), Error> {
    if value == SUCCESS {
        Ok(())
    } else {
        Err(value.into())
    }
}

impl From<Error> for i32 {
    fn from(error: Error) -> i32 {
        match error {
            Error::NotSupported => NOT_SUPPORTED,
            Error::InvalidParameters => INVALID_PARAMETERS,
            Error::Denied => DENIED,
            Error::AlreadyOn => ALREADY_ON,
            Error::OnPending => ON_PENDING,
            Error::InternalFailure => INTERNAL_FAILURE,
            Error::NotPresent => NOT_PRESENT,
            Error::Disabled => DISABLED,
            Error::InvalidAddress => INVALID_ADDRESS,
            Error::Unknown(value) => value,
        }
    }
}

impl From<i32> for Error {
    fn from(value: i32) -> Self {
        match value {
            NOT_SUPPORTED => Error::NotSupported,
            INVALID_PARAMETERS => Error::InvalidParameters,
            DENIED => Error::Denied,
            ALREADY_ON => Error::AlreadyOn,
            ON_PENDING => Error::OnPending,
            INTERNAL_FAILURE => Error::InternalFailure,
            NOT_PRESENT => Error::NotPresent,
            DISABLED => Error::Disabled,
            INVALID_ADDRESS => Error::InvalidAddress,
            _ => Error::Unknown(value),
        }
    }
}