Skip to main content

rust_utee/
user_ta_header.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2025 KylinSoft Co., Ltd. <https://www.kylinos.cn/>
3// See LICENSES for license details.
4//
5// This file has been modified by KylinSoft on 2025.
6//
7// Rust translation of OP-TEE `user_ta_header.h` (TA image layout, flags, and
8// linker-provided symbols). Property records `ta_props` / `ta_num_props` are
9// defined in `api::user_ta_headers`; this module does not re-declare them.
10
11#![allow(non_camel_case_types, non_snake_case)]
12
13use crate::tee_api_types::TEE_UUID;
14
15pub use crate::api::tee_api_property::{UserTaProperty as user_ta_property, UserTaPropType as user_ta_prop_type};
16
17// --- Bit helpers (C BIT32 / SHIFT_U32 / GENMASK_32) ---
18
19#[inline]
20pub const fn bit32(nr: u32) -> u32 {
21    1u32 << nr
22}
23
24#[inline]
25pub const fn bit64(nr: u32) -> u64 {
26    1u64 << nr
27}
28
29#[inline]
30pub const fn shift_u32(v: u32, shift: u32) -> u32 {
31    v << shift
32}
33
34#[inline]
35pub const fn shift_u64(v: u64, shift: u32) -> u64 {
36    v << shift
37}
38
39#[inline]
40pub const fn bit(nr: u32) -> u32 {
41    bit32(nr)
42}
43
44/// Inclusive bit mask for the low 32 bits: bits `[hi..lo]`.
45/// Caller must ensure `lo <= hi < 32` (matches Linux `GENMASK_32`).
46#[inline]
47pub const fn genmask_32(hi: u32, lo: u32) -> u32 {
48    (!0u32 >> (31 - hi)) & (!0u32 << lo)
49}
50
51// --- TA flags (OP-TEE) ---
52
53pub const TA_FLAG_USER_MODE: u32 = 0; // Deprecated, was BIT32(0)
54pub const TA_FLAG_EXEC_DDR: u32 = 0; // Deprecated, was BIT32(1)
55pub const TA_FLAG_SINGLE_INSTANCE: u32 = bit32(2);
56pub const TA_FLAG_MULTI_SESSION: u32 = bit32(3);
57pub const TA_FLAG_INSTANCE_KEEP_ALIVE: u32 = bit32(4); // remains after last close
58pub const TA_FLAG_SECURE_DATA_PATH: u32 = bit32(5); // accesses SDP memory
59pub const TA_FLAG_REMAP_SUPPORT: u32 = 0; // Deprecated, was BIT32(6)
60pub const TA_FLAG_CACHE_MAINTENANCE: u32 = bit32(7); // use cache flush syscall
61/// TA instance can execute multiple sessions concurrently (pseudo-TAs only).
62pub const TA_FLAG_CONCURRENT: u32 = bit32(8);
63pub const TA_FLAG_DEVICE_ENUM: u32 = bit32(9); // without tee-supplicant
64pub const TA_FLAG_DEVICE_ENUM_SUPP: u32 = bit32(10); // with tee-supplicant
65/// See also "gpd.ta.doesNotCloseHandleOnCorruptObject"
66pub const TA_FLAG_DONT_CLOSE_HANDLE_ON_CORRUPT_OBJECT: u32 = bit32(11);
67pub const TA_FLAG_DEVICE_ENUM_TEE_STORAGE_PRIVATE: u32 = bit32(12); // with TEE_STORAGE_PRIVATE
68/// Don't restart a TA with TA_FLAG_INSTANCE_KEEP_ALIVE set if it has crashed.
69pub const TA_FLAG_INSTANCE_KEEP_CRASHED: u32 = bit32(13);
70
71pub const TA_FLAGS_MASK: u32 = genmask_32(13, 0);
72
73/// Binary layout of the `.ta_head` section (GP / OP-TEE).
74#[repr(C)]
75#[derive(Copy, Clone)]
76pub struct ta_head {
77    pub uuid: TEE_UUID,
78    pub stack_size: u32,
79    pub flags: u32,
80    pub depr_entry: u64,
81}
82
83// --- GP TA property string keys (manifest) ---
84
85pub const TA_PROP_STR_SINGLE_INSTANCE: &[u8] = b"gpd.ta.singleInstance\0";
86pub const TA_PROP_STR_MULTI_SESSION: &[u8] = b"gpd.ta.multiSession\0";
87pub const TA_PROP_STR_KEEP_ALIVE: &[u8] = b"gpd.ta.instanceKeepAlive\0";
88pub const TA_PROP_STR_KEEP_CRASHED: &[u8] = b"optee.ta.instanceKeepCrashed\0";
89pub const TA_PROP_STR_DATA_SIZE: &[u8] = b"gpd.ta.dataSize\0";
90pub const TA_PROP_STR_STACK_SIZE: &[u8] = b"gpd.ta.stackSize\0";
91pub const TA_PROP_STR_VERSION: &[u8] = b"gpd.ta.version\0";
92pub const TA_PROP_STR_DESCRIPTION: &[u8] = b"gpd.ta.description\0";
93pub const TA_PROP_STR_ENDIAN: &[u8] = b"gpd.ta.endian\0";
94pub const TA_PROP_STR_DOES_NOT_CLOSE_HANDLE_ON_CORRUPT_OBJECT: &[u8] =
95    b"gpd.ta.doesNotCloseHandleOnCorruptObject\0";
96
97