optee_utee/parameter/value.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Type-safe wrappers for TEE value-type parameters.
19//!
20//! A *value* parameter carries two `u32` fields (`a` and `b`) rather than a
21//! shared memory buffer.
22//!
23//! This module provides:
24//!
25//! * [`ParameterValueRead`] / [`ParameterValueWrite`] traits for reading and
26//! writing the two `u32` fields.
27//! * Three concrete wrappers that encode the data direction in the type:
28//! [`ParameterValueInput`], [`ParameterValueOutput`],
29//! [`ParameterValueInout`].
30//!
31//! # Direction guarantees
32//!
33//! | Type | Host → TA | TA → Host |
34//! |---|---|---|
35//! | `ParameterValueInput` | ✓ | ✗ |
36//! | `ParameterValueOutput` | ✗ | ✓ |
37//! | `ParameterValueInout` | ✓ | ✓ |
38
39use super::{FromRawParameter, ParamType, RawParamType, check_type_is};
40use crate::{Result, raw::TEE_Param};
41
42/// Read-only access to the two `u32` fields of a value parameter.
43///
44/// Implemented by [`ParameterValueInput`] and [`ParameterValueInout`].
45pub trait ParameterValueRead {
46 /// Returns the `a` field.
47 fn get_a(&self) -> u32;
48 /// Returns the `b` field.
49 fn get_b(&self) -> u32;
50}
51
52/// Write access to the two `u32` fields of a value parameter.
53///
54/// Implemented by [`ParameterValueOutput`] and [`ParameterValueInout`].
55/// Values written here will be read back by the host after the TA invocation
56/// completes.
57pub trait ParameterValueWrite {
58 /// Set the `a` field.
59 fn set_a(&mut self, a: u32);
60 /// Set the `b` field.
61 fn set_b(&mut self, b: u32);
62}
63
64/// A value-type input parameter.
65///
66/// The two `u32` values are copied out of the raw union at construction time,
67pub struct ParameterValueInput {
68 a: u32,
69 b: u32,
70}
71
72/// A value-type output parameter.
73///
74/// Holds a mutable reference into the raw `TEE_Param` union. Values written
75/// via [`ParameterValueWrite`] will be propagated back to the host.
76pub struct ParameterValueOutput<'a>(&'a mut TEE_Param);
77
78/// A value-type in/out parameter.
79///
80/// Combines the semantics of both [`ParameterValueInput`] and
81/// [`ParameterValueOutput`]: the host initializes the values, and the TA may
82/// both read and write them.
83pub struct ParameterValueInout<'a>(&'a mut TEE_Param);
84
85impl<'a> FromRawParameter<'a> for ParameterValueInput {
86 unsafe fn from_raw(raw_type: RawParamType, raw_param: &'a mut TEE_Param) -> Result<Self> {
87 check_type_is(raw_type, ParamType::ValueInput)?;
88 Ok(Self {
89 a: unsafe { raw_param.value.a },
90 b: unsafe { raw_param.value.b },
91 })
92 }
93}
94
95impl<'a> FromRawParameter<'a> for ParameterValueOutput<'a> {
96 unsafe fn from_raw(raw_type: RawParamType, raw_param: &'a mut TEE_Param) -> Result<Self> {
97 check_type_is(raw_type, ParamType::ValueOutput)?;
98 Ok(Self(raw_param))
99 }
100}
101
102impl<'a> FromRawParameter<'a> for ParameterValueInout<'a> {
103 unsafe fn from_raw(raw_type: RawParamType, raw_param: &'a mut TEE_Param) -> Result<Self> {
104 check_type_is(raw_type, ParamType::ValueInout)?;
105 Ok(Self(raw_param))
106 }
107}
108
109impl ParameterValueRead for ParameterValueInput {
110 fn get_a(&self) -> u32 {
111 self.a
112 }
113 fn get_b(&self) -> u32 {
114 self.b
115 }
116}
117
118impl<'a> ParameterValueRead for ParameterValueInout<'a> {
119 fn get_a(&self) -> u32 {
120 unsafe { self.0.value.a }
121 }
122 fn get_b(&self) -> u32 {
123 unsafe { self.0.value.b }
124 }
125}
126
127impl<'a> ParameterValueWrite for ParameterValueInout<'a> {
128 fn set_a(&mut self, a: u32) {
129 self.0.value.a = a;
130 }
131 fn set_b(&mut self, b: u32) {
132 self.0.value.b = b;
133 }
134}
135
136impl<'a> ParameterValueWrite for ParameterValueOutput<'a> {
137 fn set_a(&mut self, a: u32) {
138 self.0.value.a = a;
139 }
140 fn set_b(&mut self, b: u32) {
141 self.0.value.b = b;
142 }
143}