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