optee_utee/time.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
18use crate::{Result, tee_check};
19use core::fmt;
20use optee_utee_sys as raw;
21
22/// A millisecond resolution structure for saving the time.
23pub struct Time {
24 /// The field for the seconds.
25 pub seconds: u32,
26 /// The field for the milliseconds within this second.
27 pub millis: u32,
28}
29
30impl Default for Time {
31 fn default() -> Self {
32 Self::new()
33 }
34}
35
36impl Time {
37 /// Create a new empty time structure.
38 pub fn new() -> Self {
39 Time {
40 seconds: 0,
41 millis: 0,
42 }
43 }
44
45 /// Retrieve the current system time.
46 /// The origin of this system time is arbitrary and implementation-dependent.
47 /// Different TA instances may even have different system times.
48 /// The only guarantee is that the system time is not reset or rolled back during the life of a
49 /// given TA instance, so it can be used to compute time differences and operation deadlines.
50 ///
51 /// # Example
52 ///
53 /// ``` rust,no_run
54 /// # use optee_utee::Time;
55 /// let mut time = Time::new();
56 /// time.system_time();
57 /// ```
58 ///
59 /// # Panics
60 ///
61 /// 1) If the Implementation detects any error.
62 pub fn system_time(&mut self) {
63 unsafe {
64 raw::TEE_GetSystemTime(self as *mut _ as _);
65 }
66 }
67
68 /// Wait for the specified number of milliseconds or wait forever if timeout equals
69 /// `raw::TEE_TIMEOUT_INFINITE` (0xFFFFFFFF). The waiting timer is `System Time`.
70 ///
71 /// # Parameters
72 ///
73 /// 1) `timeout`: The number of milliseconds to wait, or `raw::TEE_TIMEOUT_INFINITE`.
74 ///
75 /// # Example
76 ///
77 /// ``` rust,no_run
78 /// # use optee_utee::{Time, Result};
79 /// # fn main() -> Result<()> {
80 /// Time::wait(1000)?;
81 /// # Ok(())
82 /// # }
83 /// ```
84 ///
85 /// # Errors
86 ///
87 /// 1) `Cancel`: If the wait has been cancelled.
88 ///
89 /// # Panics
90 ///
91 /// 1) If the Implementation detects any error.
92 pub fn wait(timeout: u32) -> Result<()> {
93 tee_check(unsafe { raw::TEE_Wait(timeout) })
94 }
95
96 /// Retrieve the persisten time of the Trusted Application. Since the timer is not
97 /// automatically set, this function should be called after [set_ta_time](Time::set_ta_time).
98 /// The time is a real-time source of time and the origin of this time is set individually by each Trusted Application.
99 /// Also, the time SHALL persist across reboots.
100 ///
101 /// # Example
102 ///
103 /// ``` rust,no_run
104 /// # use optee_utee::{Time, Result};
105 /// # fn main() -> Result<()> {
106 /// let mut time = Time::new();
107 /// time.system_time();
108 /// time.set_ta_time()?;
109 /// time.ta_time()?;
110 /// # Ok(())
111 /// # }
112 /// ```
113 ///
114 /// # Errors
115 ///
116 /// 1) `TimeNotSet`: Time is not set.
117 /// 2) `TimeNeedsReset`: Time needs to be reset.
118 /// 3) `Overflow`: The number of seconds in the TA Persistent Time overflows the range of a
119 /// `u32`. The field `seconds` is still set to the TA Persistent Time truncated to 32 bits.
120 ///
121 /// # Panics
122 ///
123 /// 1) If the Implementation detects any error.
124 pub fn ta_time(&mut self) -> Result<()> {
125 tee_check(unsafe { raw::TEE_GetTAPersistentTime(self as *mut _ as _) })
126 }
127
128 /// Set the persistent time of the current Trusted Application.
129 ///
130 /// # Errors
131 ///
132 /// 1) `OutOfMemory`: If not enough memory is available to complete the operation.
133 /// 2) `SotrageNoSpace`: If insufficient storage space is available to complete the operation.
134 ///
135 /// # Panics
136 ///
137 /// 1) If the Implementation detects any error.
138 pub fn set_ta_time(&self) -> Result<()> {
139 tee_check(unsafe { raw::TEE_SetTAPersistentTime(self as *const _ as _) })
140 }
141
142 /// Retrieve the current REE system time.
143 /// The time is as trusted as the REE itself and may also be tampered by the user.
144 ///
145 /// Panics
146 ///
147 /// 1) If the Implementation detects any error.
148 pub fn ree_time(&mut self) {
149 unsafe {
150 raw::TEE_GetREETime(self as *mut _ as _);
151 }
152 }
153}
154
155impl fmt::Display for Time {
156 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
157 write!(
158 f,
159 "(second: {}, millisecond: {})",
160 self.seconds, self.millis
161 )
162 }
163}