1 //
2 // Copyright (C) 2022 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 //! KeyMint TA core for Cuttlefish.
17 
18 extern crate alloc;
19 
20 use kmr_wire::keymint::SecurityLevel;
21 use libc::c_int;
22 use log::error;
23 use std::os::fd::OwnedFd;
24 
25 /// FFI wrapper around [`kmr_cf::ta_main`].
26 ///
27 /// # Safety
28 ///
29 /// `fd_in`, `fd_out`, and `snapshot_socket_fd` must be valid and open file descriptors and the
30 /// caller must not use or close them after the call.
31 ///
32 /// TODO: What are the preconditions for `trm`?
33 #[no_mangle]
kmr_ta_main( fd_in: OwnedFd, fd_out: OwnedFd, security_level: c_int, trm: *mut libc::c_void, snapshot_socket_fd: OwnedFd, )34 pub unsafe extern "C" fn kmr_ta_main(
35     fd_in: OwnedFd,
36     fd_out: OwnedFd,
37     security_level: c_int,
38     trm: *mut libc::c_void,
39     snapshot_socket_fd: OwnedFd,
40 ) {
41     let security_level = match SecurityLevel::n(security_level) {
42         Some(
43             x @ (SecurityLevel::Software
44             | SecurityLevel::TrustedEnvironment
45             | SecurityLevel::Strongbox),
46         ) => x,
47         _ => {
48             error!("unexpected security level {}, running as SOFTWARE", security_level);
49             SecurityLevel::Software
50         }
51     };
52     // SAFETY: TODO: What are the preconditions for `trm`?
53     unsafe {
54         kmr_cf::ta_main(fd_in.into(), fd_out.into(), security_level, trm, snapshot_socket_fd.into())
55     }
56 }
57