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
use crate::{measurement::MeasurementError, rsi};

use safe_abstraction::raw_ptr;

// B3.4.1 RmiCommandReturnCode type
// Default index is 0
#[derive(Debug)]
pub enum Error {
    RmiErrorInput,
    RmiErrorRealm(usize),
    RmiErrorRec,
    RmiErrorRtt(usize),
    RmiErrorInUse,
    RmiErrorCount,
    //// The below are our-defined errors not in TF-RMM
    RmiErrorOthers(InternalError),
}

#[derive(Debug)]
pub enum InternalError {
    NotExistRealm,
    NotExistVCPU,
    MeasurementError,
    InvalidMeasurementIndex,
}

impl From<Error> for usize {
    fn from(err: Error) -> Self {
        match err {
            Error::RmiErrorInput => 1,
            Error::RmiErrorRealm(index) => 2 | (index << 8),
            Error::RmiErrorRec => 3,
            Error::RmiErrorRtt(level) => 4 | (level << 8),
            Error::RmiErrorInUse => 5,
            Error::RmiErrorCount => 6,
            Error::RmiErrorOthers(_) => 7,
        }
    }
}

impl From<vmsa::error::Error> for Error {
    fn from(_e: vmsa::error::Error) -> Self {
        //error!("MmError occured: {}", <Error as Into<usize>>::into(e));
        Error::RmiErrorInput
    }
}

impl From<MeasurementError> for Error {
    fn from(_value: MeasurementError) -> Self {
        Error::RmiErrorOthers(InternalError::MeasurementError)
    }
}

impl From<rsi::error::Error> for Error {
    fn from(value: rsi::error::Error) -> Self {
        match value {
            rsi::error::Error::RealmDoesNotExists => {
                Self::RmiErrorOthers(InternalError::NotExistRealm)
            }
            _ => Self::RmiErrorOthers(InternalError::InvalidMeasurementIndex),
        }
    }
}

impl From<raw_ptr::Error> for Error {
    fn from(error: raw_ptr::Error) -> Self {
        error!("Failed to convert a raw pointer to the struct. {:?}", error);
        Error::RmiErrorInput
    }
}