Skip to main content

rust_utee/api/
tee_api_objects.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
7use crate::api::tee_api_mm::TEE_CheckMemoryAccessRights;
8use crate::api::tee_api_panic::TEE_Panic;
9use crate::syscalls::syscall_table::{
10    _utee_cryp_obj_alloc, _utee_cryp_obj_close, _utee_cryp_obj_copy, _utee_cryp_obj_generate_key,
11    _utee_cryp_obj_get_attr, _utee_cryp_obj_get_info, _utee_cryp_obj_populate,
12    _utee_cryp_obj_reset, _utee_cryp_obj_restrict_usage, _utee_storage_alloc_enum,
13    _utee_storage_free_enum, _utee_storage_next_enum, _utee_storage_obj_create,
14    _utee_storage_obj_del, _utee_storage_obj_open, _utee_storage_obj_read, _utee_storage_obj_rename,
15    _utee_storage_obj_seek, _utee_storage_obj_trunc, _utee_storage_obj_write,
16    _utee_storage_reset_enum,
17    _utee_storage_start_enum,
18};
19use crate::tee_api_defines::*;
20use crate::tee_api_types::{
21    TEE_Attribute, TEE_ObjectEnumHandle, TEE_ObjectHandle, TEE_ObjectInfo, TEE_Result, TEE_Whence,
22};
23use crate::utee_types::utee_attribute;
24
25/// 默认使用标志
26pub const TEE_USAGE_DEFAULT: u32 = 0xffffffff;
27
28/// 将TEE属性转换为utee属性
29pub unsafe fn __utee_from_attr(
30    ua: *mut utee_attribute,
31    attrs: *const TEE_Attribute,
32    attr_count: u32,
33) {
34    unsafe {
35        for n in 0..attr_count as usize {
36            let ua_ptr = ua.add(n);
37            let attr_ptr = attrs.add(n);
38
39            (*ua_ptr).attribute_id = (*attr_ptr).attributeID;
40
41            if (*attr_ptr).attributeID & TEE_ATTR_FLAG_VALUE != 0 {
42                // 处理值类型属性
43                (*ua_ptr).a = (*attr_ptr).content.value.a as u64;
44                (*ua_ptr).b = (*attr_ptr).content.value.b as u64;
45            } else {
46                // 处理引用类型属性
47                (*ua_ptr).a = (*attr_ptr).content.memref.buffer as u64;
48                (*ua_ptr).b = (*attr_ptr).content.memref.size as u64;
49            }
50        }
51    }
52}
53
54/// 获取对象信息(已弃用)
55///
56/// 注意:此函数已弃用,新代码应使用 [TEE_GetObjectInfo1] 函数
57#[unsafe(no_mangle)]
58pub extern "C" fn TEE_GetObjectInfo(object: TEE_ObjectHandle, object_info: &mut TEE_ObjectInfo) {
59    let mut info = unsafe { std::mem::zeroed() }; // 使用 zeroed 替代 default
60    let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) };
61
62    if res != TEE_SUCCESS as usize {
63        TEE_Panic(res as u32);
64    }
65
66    if info.obj_type == TEE_TYPE_CORRUPTED_OBJECT {
67        // 对于损坏的对象,设置默认值
68        object_info.objectSize = 0;
69        object_info.maxObjectSize = 0;
70        object_info.objectUsage = 0;
71        object_info.dataSize = 0;
72        object_info.dataPosition = 0;
73        object_info.handleFlags = 0;
74    } else {
75        // 复制对象信息
76        object_info.objectType = info.obj_type;
77        object_info.objectSize = info.obj_size;
78        object_info.maxObjectSize = info.max_obj_size;
79        object_info.objectUsage = info.obj_usage;
80        object_info.dataSize = info.data_size as usize;
81        object_info.dataPosition = info.data_pos as usize;
82        object_info.handleFlags = info.handle_flags;
83    }
84}
85
86/// 获取对象信息
87///
88/// 此函数返回操作结果而不是 panic
89#[unsafe(no_mangle)]
90pub extern "C" fn TEE_GetObjectInfo1(
91    object: TEE_ObjectHandle,
92    object_info: &mut TEE_ObjectInfo,
93) -> TEE_Result {
94    let mut info = unsafe { std::mem::zeroed() };
95    let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
96
97    // 检查是否为非预期错误
98    if res != TEE_SUCCESS
99        && res != TEE_ERROR_CORRUPT_OBJECT
100        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
101    {
102        TEE_Panic(res as u32);
103    }
104
105    // 总是复制对象信息,即使对象已损坏或存储不可用
106    object_info.objectType = info.obj_type;
107    object_info.objectSize = info.obj_size;
108    object_info.maxObjectSize = info.max_obj_size;
109    object_info.objectUsage = info.obj_usage;
110    object_info.dataSize = info.data_size as usize;
111    object_info.dataPosition = info.data_pos as usize;
112    object_info.handleFlags = info.handle_flags;
113
114    res
115}
116
117/// 限制对象使用(已弃用)
118///
119/// 注意:此函数已弃用,新代码应使用 [TEE_RestrictObjectUsage1] 函数
120#[unsafe(no_mangle)]
121pub extern "C" fn TEE_RestrictObjectUsage(object: TEE_ObjectHandle, object_usage: u32) {
122    let mut info = unsafe { std::mem::zeroed() };
123    unsafe {
124        _utee_cryp_obj_get_info(object as u64, &mut info);
125    }
126
127    // 如果对象已损坏,则直接返回
128    if info.obj_type == TEE_TYPE_CORRUPTED_OBJECT {
129        return;
130    }
131
132    let res = TEE_RestrictObjectUsage1(object, object_usage);
133
134    if res != TEE_SUCCESS {
135        TEE_Panic(res as u32);
136    }
137}
138
139/// 限制对象使用
140///
141/// 设置对象的使用限制,返回操作结果
142#[unsafe(no_mangle)]
143pub extern "C" fn TEE_RestrictObjectUsage1(
144    object: TEE_ObjectHandle,
145    object_usage: u32,
146) -> TEE_Result {
147    let res =
148        unsafe { _utee_cryp_obj_restrict_usage(object as u64, object_usage as u64) } as TEE_Result;
149
150    // 检查是否为非预期错误
151    if res != TEE_SUCCESS
152        && res != TEE_ERROR_CORRUPT_OBJECT
153        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
154    {
155        TEE_Panic(res as u32);
156    }
157
158    res
159}
160
161/// 获取对象的缓冲区属性(已弃用)
162///
163/// 返回对象的缓冲区属性,将数据写入提供的缓冲区
164#[unsafe(no_mangle)]
165pub extern "C" fn TEE_GetObjectBufferAttribute(
166    object: TEE_ObjectHandle,
167    attribute_id: u32,
168    buffer: *mut core::ffi::c_void,
169    size: *mut usize,
170) -> TEE_Result {
171    // 检查参数有效性
172    if cfg!(feature = "strict_annotation_checks") {
173        let res = TEE_CheckMemoryAccessRights(
174            TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
175            size as *mut core::ffi::c_void,
176            std::mem::size_of::<usize>(),
177        );
178        if res != 0 {
179            eprintln!("[inout] size: error {:#010x}", res);
180            TEE_Panic(0);
181        }
182    }
183
184    let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
185    let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
186
187    if res != TEE_SUCCESS {
188        return check_result_and_panic(res);
189    }
190
191    // 此函数仅支持引用类型属性
192    if attribute_id & TEE_ATTR_FLAG_VALUE != 0 {
193        return check_result_and_panic(TEE_ERROR_BAD_PARAMETERS);
194    }
195
196    let mut buffer_size: u64 = 0;
197    unsafe {
198        if !size.is_null() {
199            buffer_size = *size as u64;
200        }
201    }
202
203    let res = unsafe {
204        _utee_cryp_obj_get_attr(object as u64, attribute_id as u64, buffer, &mut buffer_size)
205    } as TEE_Result;
206
207    unsafe {
208        if !size.is_null() {
209            *size = buffer_size as usize;
210        }
211    }
212
213    check_result_and_panic(res)
214}
215
216/// 获取对象的缓冲区属性
217///
218/// 返回对象的缓冲区属性,将数据写入提供的缓冲区
219#[unsafe(no_mangle)]
220pub extern "C" fn TEE_GetObjectBufferAttribute1(
221    object: TEE_ObjectHandle,
222    attribute_id: u32,
223    buffer: *mut core::ffi::c_void,
224    size: *mut usize,
225) -> TEE_Result {
226    // 检查参数有效性
227    if size.is_null() {
228        return TEE_ERROR_BAD_PARAMETERS;
229    }
230
231    let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
232    let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
233
234    if res != TEE_SUCCESS {
235        return check_result_and_panic(res);
236    }
237
238    // 此函数仅支持引用类型属性
239    if attribute_id & TEE_ATTR_FLAG_VALUE != 0 {
240        return TEE_ERROR_BAD_PARAMETERS;
241    }
242
243    // 首先获取所需缓冲区大小
244    let mut required_size: u64 = 0;
245    let res = unsafe {
246        _utee_cryp_obj_get_attr(
247            object as u64,
248            attribute_id as u64,
249            core::ptr::null_mut(),
250            &mut required_size,
251        ) as TEE_Result
252    };
253
254    if res == TEE_SUCCESS || res == TEE_ERROR_SHORT_BUFFER {
255        unsafe {
256            *size = required_size as usize;
257        }
258    } else {
259        return check_result_and_panic(res);
260    }
261
262    // 如果提供了缓冲区且大小足够,获取实际数据
263    if !buffer.is_null() && unsafe { *size } >= required_size as usize {
264        let res = unsafe {
265            _utee_cryp_obj_get_attr(
266                object as u64,
267                attribute_id as u64,
268                buffer,
269                &mut required_size,
270            ) as TEE_Result
271        };
272
273        if res != TEE_SUCCESS {
274            return check_result_and_panic(res);
275        }
276
277        unsafe {
278            *size = required_size as usize;
279        }
280    }
281
282    TEE_SUCCESS
283}
284
285/// 检查结果并对非预期错误进行 panic
286fn check_result_and_panic(res: TEE_Result) -> TEE_Result {
287    if res != TEE_SUCCESS
288        && res != TEE_ERROR_ITEM_NOT_FOUND
289        && res != TEE_ERROR_SHORT_BUFFER
290        && res != TEE_ERROR_CORRUPT_OBJECT
291        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
292    {
293        TEE_Panic(res as u32);
294    }
295    res
296}
297
298/// 处理结果并返回
299fn handle_result_and_return(
300    res: TEE_Result,
301    a: *mut u32,
302    b: *mut u32,
303    value_a: u32,
304    value_b: u32,
305) -> TEE_Result {
306    if res != TEE_SUCCESS
307        && res != TEE_ERROR_ITEM_NOT_FOUND
308        && res != TEE_ERROR_CORRUPT_OBJECT
309        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
310    {
311        TEE_Panic(res);
312    }
313
314    if res == TEE_SUCCESS {
315        if !a.is_null() {
316            unsafe { *a = value_a };
317        }
318        if !b.is_null() {
319            unsafe { *b = value_b };
320        }
321    }
322
323    res
324}
325
326/// 获取对象值属性
327///
328/// 此函数仅支持值类型属性(由TEE_ATTR_FLAG_VALUE标识)
329#[unsafe(no_mangle)]
330pub extern "C" fn TEE_GetObjectValueAttribute(
331    object: TEE_ObjectHandle,
332    attribute_id: u32,
333    a: *mut u32,
334    b: *mut u32,
335) -> TEE_Result {
336    // 检查输出参数的有效性(如果非空)
337    if cfg!(feature = "strict_annotation_checks") {
338        if !a.is_null() {
339            let res = TEE_CheckMemoryAccessRights(
340                TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
341                a as *mut core::ffi::c_void,
342                std::mem::size_of::<u32>(),
343            );
344            if res != 0 {
345                eprintln!("[inout] a: error {:#010x}", res);
346                TEE_Panic(0);
347            }
348        }
349        if !b.is_null() {
350            let res = TEE_CheckMemoryAccessRights(
351                TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
352                b as *mut core::ffi::c_void,
353                std::mem::size_of::<u32>(),
354            );
355            if res != 0 {
356                eprintln!("[inout] b: error {:#010x}", res);
357                TEE_Panic(0);
358            }
359        }
360    }
361
362    let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
363    let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
364
365    if res != TEE_SUCCESS {
366        return handle_result_and_return(res, a, b, 0, 0);
367    }
368
369    // 此函数仅支持值类型属性
370    if attribute_id & TEE_ATTR_FLAG_VALUE == 0 {
371        let res = TEE_ERROR_BAD_PARAMETERS;
372        return handle_result_and_return(res, a, b, 0, 0);
373    }
374
375    // 创建缓冲区来存储属性值
376    let mut buf = [0u32; 2];
377    let mut size = std::mem::size_of_val(&buf) as u64;
378
379    let res = unsafe {
380        _utee_cryp_obj_get_attr(
381            object as u64,
382            attribute_id as u64,
383            buf.as_mut_ptr() as *mut core::ffi::c_void,
384            &mut size,
385        ) as TEE_Result
386    };
387
388    // 检查返回结果是否为非预期错误
389    if res != TEE_SUCCESS
390        && res != TEE_ERROR_ITEM_NOT_FOUND
391        && res != TEE_ERROR_CORRUPT_OBJECT
392        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
393    {
394        TEE_Panic(res);
395    }
396
397    // 验证返回的大小是否正确
398    if size != std::mem::size_of_val(&buf) as u64 {
399        TEE_Panic(0);
400    }
401
402    // 如果成功,将值写入输出参数
403    if res == TEE_SUCCESS {
404        if !a.is_null() {
405            unsafe { *a = buf[0] };
406        }
407        if !b.is_null() {
408            unsafe { *b = buf[1] };
409        }
410    }
411
412    res
413}
414
415/// 关闭对象
416///
417/// 关闭并释放指定的TEE对象句柄
418#[unsafe(no_mangle)]
419pub extern "C" fn TEE_CloseObject(object: TEE_ObjectHandle) {
420    // 检查是否为NULL句柄
421    if object.is_null() {
422        return;
423    }
424
425    let res = unsafe { _utee_cryp_obj_close(object as u64) } as TEE_Result;
426
427    // 检查返回结果,如果不是成功则panic
428    if res != TEE_SUCCESS {
429        TEE_Panic(res as u32);
430    }
431}
432
433/// 分配临时对象
434///
435/// 分配一个新的临时对象句柄
436#[unsafe(no_mangle)]
437pub extern "C" fn TEE_AllocateTransientObject(
438    object_type: u32, // TEE_ObjectType
439    max_object_size: u32,
440    object: *mut TEE_ObjectHandle,
441) -> TEE_Result {
442    // 数据类型对象不支持
443    if object_type == TEE_TYPE_DATA {
444        return TEE_ERROR_NOT_SUPPORTED;
445    }
446
447    // 检查输出参数的有效性
448    if cfg!(feature = "strict_annotation_checks") {
449        let res = TEE_CheckMemoryAccessRights(
450            TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
451            object as *mut core::ffi::c_void,
452            std::mem::size_of::<TEE_ObjectHandle>(),
453        );
454        if res != 0 {
455            eprintln!("[inout] object: error {:#010x}", res);
456            TEE_Panic(0);
457        }
458    }
459
460    let mut obj: u32 = 0;
461
462    let res = unsafe { _utee_cryp_obj_alloc(object_type as u64, max_object_size as u64, &mut obj) }
463        as TEE_Result;
464
465    // 检查返回结果是否为非预期错误
466    if res != TEE_SUCCESS && res != TEE_ERROR_OUT_OF_MEMORY && res != TEE_ERROR_NOT_SUPPORTED {
467        TEE_Panic(res as u32);
468    }
469
470    // 如果成功,将对象句柄写入输出参数
471    if res == TEE_SUCCESS {
472        unsafe {
473            *object = obj as TEE_ObjectHandle;
474        }
475    }
476
477    res
478}
479
480/// 释放临时对象
481///
482/// 释放指定的临时对象句柄,仅当对象不是持久化对象时
483#[unsafe(no_mangle)]
484pub extern "C" fn TEE_FreeTransientObject(object: TEE_ObjectHandle) {
485    // 检查是否为NULL句柄
486    if object.is_null() {
487        return;
488    }
489
490    let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
491    let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
492
493    if res != TEE_SUCCESS {
494        TEE_Panic(res as u32);
495    }
496
497    // 检查对象是否为持久化对象,如果是则panic
498    if (info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
499        TEE_Panic(0);
500    }
501
502    let res = unsafe { _utee_cryp_obj_close(object as u64) } as TEE_Result;
503
504    if res != TEE_SUCCESS {
505        TEE_Panic(res as u32);
506    }
507}
508
509/// 重置临时对象
510///
511/// 重置指定的临时对象,仅当对象不是持久化对象时
512#[unsafe(no_mangle)]
513pub extern "C" fn TEE_ResetTransientObject(object: TEE_ObjectHandle) {
514    // 检查是否为NULL句柄
515    if object.is_null() {
516        return;
517    }
518
519    let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
520    let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
521
522    if res != TEE_SUCCESS {
523        TEE_Panic(res as u32);
524    }
525
526    // 检查对象是否为持久化对象,如果是则panic
527    if (info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
528        TEE_Panic(0);
529    }
530
531    let res = unsafe { _utee_cryp_obj_reset(object as u64) } as TEE_Result;
532
533    if res != TEE_SUCCESS {
534        TEE_Panic(res as u32);
535    }
536}
537
538/// 填充临时对象
539///
540/// 使用指定的属性填充临时对象
541#[unsafe(no_mangle)]
542pub extern "C" fn TEE_PopulateTransientObject(
543    object: TEE_ObjectHandle,
544    attrs: *const TEE_Attribute,
545    attr_count: u32,
546) -> TEE_Result {
547    // 检查属性数组的有效性
548    // 注意:这里简化处理,不进行详细的属性检查
549
550    let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
551    let res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) } as TEE_Result;
552
553    if res != TEE_SUCCESS {
554        TEE_Panic(res as u32);
555    }
556
557    // 必须是临时对象(不能是持久化对象)
558    if (info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
559        TEE_Panic(0);
560    }
561
562    // 对象不能已经被初始化
563    if (info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0 {
564        TEE_Panic(0);
565    }
566
567    // 创建临时属性数组并转换属性
568    let mut ua = vec![utee_attribute::default(); attr_count as usize];
569
570    unsafe {
571        __utee_from_attr(ua.as_mut_ptr(), attrs, attr_count);
572    }
573
574    let res = unsafe {
575        _utee_cryp_obj_populate(object as u64, ua.as_mut_ptr(), attr_count as u64) as TEE_Result
576    };
577
578    if res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS {
579        TEE_Panic(res as u32);
580    }
581
582    res
583}
584
585/// 初始化引用属性
586///
587/// 初始化一个引用类型的TEE属性
588#[unsafe(no_mangle)]
589pub extern "C" fn TEE_InitRefAttribute(
590    attr: *mut TEE_Attribute,
591    attribute_id: u32,
592    buffer: *const core::ffi::c_void,
593    length: usize,
594) {
595    // 检查输出参数的有效性
596    if cfg!(feature = "strict_annotation_checks") {
597        let res = TEE_CheckMemoryAccessRights(
598            TEE_MEMORY_ACCESS_WRITE,
599            attr as *mut core::ffi::c_void,
600            std::mem::size_of::<TEE_Attribute>(),
601        );
602        if res != 0 {
603            eprintln!("[out] attr: error {:#010x}", res);
604            TEE_Panic(0);
605        }
606    }
607
608    // 检查属性ID是否为引用类型(不能是值类型)
609    if (attribute_id & TEE_ATTR_FLAG_VALUE) != 0 {
610        TEE_Panic(0);
611    }
612
613    // 初始化属性
614    unsafe {
615        if !attr.is_null() {
616            (*attr).attributeID = attribute_id;
617            (*attr).content.memref.buffer = buffer as *mut core::ffi::c_void;
618            (*attr).content.memref.size = length;
619        }
620    }
621}
622
623/// 初始化值属性
624///
625/// 初始化一个值类型的TEE属性
626#[unsafe(no_mangle)]
627pub extern "C" fn TEE_InitValueAttribute(
628    attr: *mut TEE_Attribute,
629    attribute_id: u32,
630    a: u32,
631    b: u32,
632) {
633    // 检查输出参数的有效性
634    if cfg!(feature = "strict_annotation_checks") {
635        let res = TEE_CheckMemoryAccessRights(
636            TEE_MEMORY_ACCESS_WRITE,
637            attr as *mut core::ffi::c_void,
638            std::mem::size_of::<TEE_Attribute>(),
639        );
640        if res != 0 {
641            eprintln!("[out] attr: error {:#010x}", res);
642            TEE_Panic(0);
643        }
644    }
645
646    // 检查属性ID是否为值类型(必须是值类型)
647    if (attribute_id & TEE_ATTR_FLAG_VALUE) == 0 {
648        TEE_Panic(0);
649    }
650
651    // 初始化属性
652    unsafe {
653        if !attr.is_null() {
654            (*attr).attributeID = attribute_id;
655            (*attr).content.value.a = a;
656            (*attr).content.value.b = b;
657        }
658    }
659}
660
661/// 复制对象属性(已弃用)
662///
663/// 注意:此函数已弃用,新代码应使用 [TEE_CopyObjectAttributes1] 函数
664#[unsafe(no_mangle)]
665pub extern "C" fn TEE_CopyObjectAttributes(
666    dest_object: TEE_ObjectHandle,
667    src_object: TEE_ObjectHandle,
668) {
669    let mut src_info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
670    let _res = unsafe { _utee_cryp_obj_get_info(src_object as u64, &mut src_info) } as TEE_Result;
671
672    // 如果源对象已损坏,则直接返回
673    if src_info.obj_type == TEE_TYPE_CORRUPTED_OBJECT {
674        return;
675    }
676
677    let res = TEE_CopyObjectAttributes1(dest_object, src_object);
678
679    if res != TEE_SUCCESS {
680        TEE_Panic(res as u32);
681    }
682}
683
684/// 复制对象属性
685///
686/// 将源对象的属性复制到目标对象
687#[unsafe(no_mangle)]
688pub extern "C" fn TEE_CopyObjectAttributes1(
689    dest_object: TEE_ObjectHandle,
690    src_object: TEE_ObjectHandle,
691) -> TEE_Result {
692    let mut dst_info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
693    let mut src_info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
694
695    // 获取目标对象信息
696    let mut res =
697        unsafe { _utee_cryp_obj_get_info(dest_object as u64, &mut dst_info) } as TEE_Result;
698
699    if res != TEE_SUCCESS {
700        return check_copy_object_attributes_result(res);
701    }
702
703    // 获取源对象信息
704    res = unsafe { _utee_cryp_obj_get_info(src_object as u64, &mut src_info) } as TEE_Result;
705
706    if res != TEE_SUCCESS {
707        return check_copy_object_attributes_result(res);
708    }
709
710    // 源对象必须已初始化
711    if (src_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) == 0 {
712        TEE_Panic(0);
713    }
714
715    // 目标对象不能是持久化对象
716    if (dst_info.handle_flags & TEE_HANDLE_FLAG_PERSISTENT) != 0 {
717        TEE_Panic(0);
718    }
719
720    // 目标对象不能已经被初始化
721    if (dst_info.handle_flags & TEE_HANDLE_FLAG_INITIALIZED) != 0 {
722        TEE_Panic(0);
723    }
724
725    // 执行对象属性复制
726    res = unsafe { _utee_cryp_obj_copy(dest_object as u64, src_object as u64) } as TEE_Result;
727
728    check_copy_object_attributes_result(res)
729}
730
731/// 检查复制对象属性的结果
732fn check_copy_object_attributes_result(res: TEE_Result) -> TEE_Result {
733    if res != TEE_SUCCESS
734        && res != TEE_ERROR_CORRUPT_OBJECT
735        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
736    {
737        TEE_Panic(res as u32);
738    }
739
740    res
741}
742
743/// 生成密钥
744///
745/// 为指定对象生成密钥
746#[unsafe(no_mangle)]
747pub extern "C" fn TEE_GenerateKey(
748    object: TEE_ObjectHandle,
749    key_size: u32,
750    params: *const TEE_Attribute,
751    param_count: u32,
752) -> TEE_Result {
753    // 检查属性参数的有效性
754    if cfg!(feature = "strict_annotation_checks") && param_count > 0 {
755        let res = TEE_CheckMemoryAccessRights(
756            TEE_MEMORY_ACCESS_READ,
757            params as *mut core::ffi::c_void,
758            std::mem::size_of::<TEE_Attribute>() * param_count as usize,
759        );
760        if res != 0 {
761            eprintln!("[in] attrs: error {:#010x}", res);
762            TEE_Panic(0);
763        }
764    }
765
766    // 创建临时属性数组并转换属性
767    let mut ua = vec![utee_attribute::default(); param_count as usize];
768
769    unsafe {
770        __utee_from_attr(ua.as_mut_ptr(), params, param_count);
771    }
772
773    let res = unsafe {
774        _utee_cryp_obj_generate_key(
775            object as u64,
776            key_size as u64,
777            ua.as_ptr(),
778            param_count as u64,
779        )
780    } as TEE_Result;
781
782    if res != TEE_SUCCESS && res != TEE_ERROR_BAD_PARAMETERS {
783        TEE_Panic(res as u32);
784    }
785
786    res
787}
788
789/// 打开持久化对象
790///
791/// 打开一个持久化存储对象
792#[unsafe(no_mangle)]
793pub extern "C" fn TEE_OpenPersistentObject(
794    storage_id: u32,
795    object_id: *const core::ffi::c_void,
796    object_id_len: usize,
797    flags: u32,
798    object: *mut TEE_ObjectHandle,
799) -> TEE_Result {
800    // 检查输出参数的有效性
801    if cfg!(feature = "strict_annotation_checks") {
802        let res = TEE_CheckMemoryAccessRights(
803            TEE_MEMORY_ACCESS_WRITE,
804            object as *mut core::ffi::c_void,
805            std::mem::size_of::<TEE_ObjectHandle>(),
806        );
807        if res != 0 {
808            eprintln!("[out] object: error {:#010x}", res);
809            TEE_Panic(0);
810        }
811    }
812
813    let mut obj: u32 = 0;
814
815    let res = unsafe {
816        _utee_storage_obj_open(
817            storage_id as u64,
818            object_id,
819            object_id_len,
820            flags as u64,
821            &mut obj,
822        )
823    } as TEE_Result;
824
825    // 如果成功,将对象句柄写入输出参数
826    if res == TEE_SUCCESS {
827        unsafe {
828            *object = obj as TEE_ObjectHandle;
829        }
830    } else {
831        // 如果失败,将对象句柄设置为NULL
832        unsafe {
833            *object = core::ptr::null_mut();
834        }
835    }
836
837    // 检查返回结果是否为非预期错误
838    if res != TEE_SUCCESS
839        && res != TEE_ERROR_ITEM_NOT_FOUND
840        && res != TEE_ERROR_ACCESS_CONFLICT
841        && res != TEE_ERROR_OUT_OF_MEMORY
842        && res != TEE_ERROR_CORRUPT_OBJECT
843        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
844    {
845        TEE_Panic(res as u32);
846    }
847
848    res
849}
850
851/// 创建持久化对象
852///
853/// 创建一个持久化存储对象
854#[unsafe(no_mangle)]
855pub extern "C" fn TEE_CreatePersistentObject(
856    storage_id: u32,
857    object_id: *const core::ffi::c_void,
858    object_id_len: usize,
859    flags: u32,
860    attributes: TEE_ObjectHandle,
861    initial_data: *const core::ffi::c_void,
862    initial_data_len: usize,
863    object: *mut TEE_ObjectHandle,
864) -> TEE_Result {
865    let mut obj: u32 = 0;
866    let obj_ptr: *mut u32;
867
868    // 检查输出参数的有效性
869    if !object.is_null() {
870        if cfg!(feature = "strict_annotation_checks") {
871            let res = TEE_CheckMemoryAccessRights(
872                TEE_MEMORY_ACCESS_WRITE,
873                object as *mut core::ffi::c_void,
874                std::mem::size_of::<TEE_ObjectHandle>(),
875            );
876            if res != 0 {
877                eprintln!("[out] object: error {:#010x}", res);
878                TEE_Panic(0);
879            }
880        }
881        obj_ptr = &mut obj;
882    } else {
883        obj_ptr = core::ptr::null_mut();
884    }
885
886    let res = unsafe {
887        _utee_storage_obj_create(
888            storage_id as u64,
889            object_id,
890            object_id_len,
891            flags as u64,
892            attributes as u64,
893            initial_data,
894            initial_data_len,
895            obj_ptr,
896        )
897    } as TEE_Result;
898
899    // 如果成功且输出参数不为空,将对象句柄写入输出参数
900    if res == TEE_SUCCESS && !object.is_null() {
901        unsafe {
902            *object = obj as TEE_ObjectHandle;
903        }
904    } else if res != TEE_SUCCESS && !object.is_null() {
905        // 如果失败且输出参数不为空,将对象句柄设置为NULL
906        unsafe {
907            *object = core::ptr::null_mut();
908        }
909    }
910
911    // 检查返回结果是否为非预期错误
912    if res != TEE_SUCCESS
913        && res != TEE_ERROR_ITEM_NOT_FOUND
914        && res != TEE_ERROR_ACCESS_CONFLICT
915        && res != TEE_ERROR_OUT_OF_MEMORY
916        && res != TEE_ERROR_STORAGE_NO_SPACE
917        && res != TEE_ERROR_CORRUPT_OBJECT
918        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
919    {
920        TEE_Panic(res as u32);
921    }
922
923    res
924}
925
926/// 关闭并删除持久化对象(已弃用)
927///
928/// 注意:此函数已弃用,新代码应使用 [TEE_CloseAndDeletePersistentObject1] 函数
929#[unsafe(no_mangle)]
930pub extern "C" fn TEE_CloseAndDeletePersistentObject(object: TEE_ObjectHandle) {
931    // 检查是否为NULL句柄
932    if object.is_null() {
933        return;
934    }
935
936    let res = TEE_CloseAndDeletePersistentObject1(object);
937
938    if res != TEE_SUCCESS {
939        TEE_Panic(0);
940    }
941}
942
943/// 关闭并删除持久化对象
944///
945/// 关闭并删除指定的持久化对象
946#[unsafe(no_mangle)]
947pub extern "C" fn TEE_CloseAndDeletePersistentObject1(object: TEE_ObjectHandle) -> TEE_Result {
948    // 检查是否为NULL句柄
949    if object.is_null() {
950        return TEE_SUCCESS;
951    }
952
953    let res = unsafe { _utee_storage_obj_del(object as u64) } as TEE_Result;
954
955    // 检查返回结果是否为非预期错误
956    if res != TEE_SUCCESS && res != TEE_ERROR_STORAGE_NOT_AVAILABLE {
957        TEE_Panic(res as u32);
958    }
959
960    res
961}
962
963/// 重命名持久化对象
964///
965/// 将指定的持久化对象重命名
966#[unsafe(no_mangle)]
967pub extern "C" fn TEE_RenamePersistentObject(
968    object: TEE_ObjectHandle,
969    new_object_id: *const core::ffi::c_void,
970    new_object_id_len: usize,
971) -> TEE_Result {
972    let res = if object.is_null() {
973        TEE_ERROR_BAD_PARAMETERS
974    } else {
975        unsafe {
976            _utee_storage_obj_rename(object as u64, new_object_id, new_object_id_len) as TEE_Result
977        }
978    };
979
980    if res != TEE_SUCCESS
981        && res != TEE_ERROR_ACCESS_CONFLICT
982        && res != TEE_ERROR_CORRUPT_OBJECT
983        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
984        && res != TEE_ERROR_BAD_PARAMETERS
985    {
986        TEE_Panic(res as u32);
987    }
988
989    res
990}
991
992/// 分配持久化对象枚举器
993///
994/// 分配一个新的持久化对象枚举器句柄
995#[unsafe(no_mangle)]
996pub extern "C" fn TEE_AllocatePersistentObjectEnumerator(
997    object_enumerator: *mut TEE_ObjectEnumHandle,
998) -> TEE_Result {
999    // 检查输出参数的有效性
1000    if cfg!(feature = "strict_annotation_checks") {
1001        let res = TEE_CheckMemoryAccessRights(
1002            TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1003            object_enumerator as *mut core::ffi::c_void,
1004            std::mem::size_of::<TEE_ObjectEnumHandle>(),
1005        );
1006        if res != 0 {
1007            eprintln!("[out] objectEnumerator: error {:#010x}", res);
1008            TEE_Panic(0);
1009        }
1010    }
1011
1012    let mut oe: u32 = 0;
1013
1014    let res = unsafe { _utee_storage_alloc_enum(&mut oe) } as TEE_Result;
1015
1016    // 如果失败,将枚举器句柄设置为NULL
1017    if res != TEE_SUCCESS {
1018        oe = TEE_HANDLE_NULL as u32;
1019    }
1020
1021    // 将枚举器句柄写入输出参数
1022    unsafe {
1023        *object_enumerator = oe as TEE_ObjectEnumHandle;
1024    }
1025
1026    // 检查返回结果,如果不是预期的错误类型则panic
1027    if res != TEE_SUCCESS && res != TEE_ERROR_ACCESS_CONFLICT {
1028        TEE_Panic(res as u32);
1029    }
1030
1031    res
1032}
1033
1034/// 释放持久化对象枚举器
1035///
1036/// 释放指定的持久化对象枚举器句柄
1037#[unsafe(no_mangle)]
1038pub extern "C" fn TEE_FreePersistentObjectEnumerator(object_enumerator: TEE_ObjectEnumHandle) {
1039    // 检查枚举器句柄是否为空
1040    if object_enumerator.is_null() {
1041        return;
1042    }
1043
1044    let res = unsafe { _utee_storage_free_enum(object_enumerator as u64) } as TEE_Result;
1045
1046    if res != TEE_SUCCESS {
1047        TEE_Panic(res as u32);
1048    }
1049}
1050
1051/// 重置持久化对象枚举器
1052///
1053/// 重置指定的持久化对象枚举器句柄
1054#[unsafe(no_mangle)]
1055pub extern "C" fn TEE_ResetPersistentObjectEnumerator(object_enumerator: TEE_ObjectEnumHandle) {
1056    // 检查枚举器句柄是否为空
1057    if object_enumerator.is_null() {
1058        return;
1059    }
1060
1061    let res = unsafe { _utee_storage_reset_enum(object_enumerator as u64) } as TEE_Result;
1062
1063    if res != TEE_SUCCESS {
1064        TEE_Panic(res as u32);
1065    }
1066}
1067
1068/// 开始持久化对象枚举
1069///
1070/// 开始枚举指定存储空间中的持久化对象
1071#[unsafe(no_mangle)]
1072pub extern "C" fn TEE_StartPersistentObjectEnumerator(
1073    object_enumerator: TEE_ObjectEnumHandle,
1074    storage_id: u32,
1075) -> TEE_Result {
1076    let res = unsafe {
1077        _utee_storage_start_enum(object_enumerator as u64, storage_id as u64) as TEE_Result
1078    };
1079
1080    if res != TEE_SUCCESS
1081        && res != TEE_ERROR_ITEM_NOT_FOUND
1082        && res != TEE_ERROR_CORRUPT_OBJECT
1083        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1084    {
1085        TEE_Panic(res);
1086    }
1087
1088    res
1089}
1090
1091/// 获取下一个持久化对象
1092///
1093/// 从枚举器中获取下一个持久化对象的信息和ID
1094#[unsafe(no_mangle)]
1095pub extern "C" fn TEE_GetNextPersistentObject(
1096    object_enumerator: TEE_ObjectEnumHandle,
1097    object_info: *mut TEE_ObjectInfo,
1098    object_id: *mut core::ffi::c_void,
1099    object_id_len: *mut usize,
1100) -> TEE_Result {
1101    // 检查参数有效性
1102    if cfg!(feature = "strict_annotation_checks") {
1103        // 检查 object_info 参数(如果非空)
1104        if !object_info.is_null() {
1105            let res = TEE_CheckMemoryAccessRights(
1106                TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1107                object_info as *mut core::ffi::c_void,
1108                std::mem::size_of::<TEE_ObjectInfo>(),
1109            );
1110            if res != 0 {
1111                eprintln!("[out] objectInfo: error {:#010x}", res);
1112                TEE_Panic(0);
1113            }
1114        }
1115
1116        // 检查 object_id_len 参数
1117        let res = TEE_CheckMemoryAccessRights(
1118            TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1119            object_id_len as *mut core::ffi::c_void,
1120            std::mem::size_of::<usize>(),
1121        );
1122        if res != 0 {
1123            eprintln!("[out] objectIDLen: error {:#010x}", res);
1124            TEE_Panic(0);
1125        }
1126    }
1127
1128    // 检查必要参数是否为空
1129    if object_id.is_null() {
1130        return TEE_ERROR_BAD_PARAMETERS;
1131    }
1132
1133    let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
1134    let mut len: u64 = 0;
1135
1136    unsafe {
1137        if !object_id_len.is_null() {
1138            len = *object_id_len as u64;
1139        }
1140    }
1141
1142    let res = unsafe {
1143        _utee_storage_next_enum(object_enumerator as u64, &mut info, object_id, &mut len)
1144    } as TEE_Result;
1145
1146    // 如果提供了object_info参数,复制对象信息
1147    if !object_info.is_null() {
1148        unsafe {
1149            (*object_info).objectType = info.obj_type;
1150            (*object_info).objectSize = info.obj_size;
1151            (*object_info).maxObjectSize = info.max_obj_size;
1152            (*object_info).objectUsage = info.obj_usage;
1153            (*object_info).dataSize = info.data_size as usize;
1154            (*object_info).dataPosition = info.data_pos as usize;
1155            (*object_info).handleFlags = info.handle_flags;
1156        }
1157    }
1158
1159    // 更新objectIDLen
1160    unsafe {
1161        if !object_id_len.is_null() {
1162            *object_id_len = len as usize;
1163        }
1164    }
1165
1166    // 检查返回结果,如果不是预期的错误类型则panic
1167    if res != TEE_SUCCESS
1168        && res != TEE_ERROR_ITEM_NOT_FOUND
1169        && res != TEE_ERROR_CORRUPT_OBJECT
1170        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1171    {
1172        TEE_Panic(res as u32);
1173    }
1174
1175    res
1176}
1177
1178/// 读取对象数据
1179///
1180/// 从持久化对象中读取数据
1181#[unsafe(no_mangle)]
1182pub extern "C" fn TEE_ReadObjectData(
1183    object: TEE_ObjectHandle,
1184    buffer: *mut core::ffi::c_void,
1185    size: usize,
1186    count: *mut usize,
1187) -> TEE_Result {
1188    // 检查对象句柄是否为空
1189    if object.is_null() {
1190        return TEE_ERROR_BAD_PARAMETERS;
1191    }
1192
1193    // 检查参数有效性
1194    if cfg!(feature = "strict_annotation_checks") {
1195        let res = TEE_CheckMemoryAccessRights(
1196            TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE,
1197            count as *mut core::ffi::c_void,
1198            std::mem::size_of::<usize>(),
1199        );
1200        if res != 0 {
1201            eprintln!("[out] count: error {:#010x}", res);
1202            TEE_Panic(0);
1203        }
1204    }
1205
1206    let mut cnt64: u64 = 0;
1207    unsafe {
1208        if !count.is_null() {
1209            cnt64 = *count as u64;
1210        }
1211    }
1212
1213    let res =
1214        unsafe { _utee_storage_obj_read(object as u64, buffer, size, &mut cnt64) } as TEE_Result;
1215
1216    // 更新count值
1217    unsafe {
1218        if !count.is_null() {
1219            *count = cnt64 as usize;
1220        }
1221    }
1222
1223    // 检查返回结果,如果不是预期的错误类型则panic
1224    if res != TEE_SUCCESS
1225        && res != TEE_ERROR_CORRUPT_OBJECT
1226        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1227    {
1228        TEE_Panic(res as u32);
1229    }
1230
1231    res
1232}
1233
1234/// 写入对象数据
1235///
1236/// 向持久化对象写入数据
1237#[unsafe(no_mangle)]
1238pub extern "C" fn TEE_WriteObjectData(
1239    object: TEE_ObjectHandle,
1240    buffer: *const core::ffi::c_void,
1241    size: usize,
1242) -> TEE_Result {
1243    // 检查对象句柄是否为空
1244    if object.is_null() {
1245        return TEE_ERROR_BAD_PARAMETERS;
1246    }
1247
1248    // 检查数据大小是否超过最大限制
1249    if size > TEE_DATA_MAX_POSITION as usize {
1250        return TEE_ERROR_OVERFLOW;
1251    }
1252
1253    // 检查参数有效性
1254    if cfg!(feature = "strict_annotation_checks") && size > 0 && !buffer.is_null() {
1255        let res = TEE_CheckMemoryAccessRights(
1256            TEE_MEMORY_ACCESS_READ,
1257            buffer as *mut core::ffi::c_void,
1258            size,
1259        );
1260        if res != 0 {
1261            eprintln!("[in] buffer: error {:#010x}", res);
1262            TEE_Panic(0);
1263        }
1264    }
1265
1266    let res = unsafe { _utee_storage_obj_write(object as u64, buffer, size) } as TEE_Result;
1267
1268    // 检查返回结果,如果不是预期的错误类型则panic
1269    if res != TEE_SUCCESS
1270        && res != TEE_ERROR_STORAGE_NO_SPACE
1271        && res != TEE_ERROR_OVERFLOW
1272        && res != TEE_ERROR_CORRUPT_OBJECT
1273        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1274    {
1275        TEE_Panic(res as u32);
1276    }
1277
1278    res
1279}
1280
1281/// 截断对象数据
1282///
1283/// 截断持久化对象的数据到指定大小
1284#[unsafe(no_mangle)]
1285pub extern "C" fn TEE_TruncateObjectData(object: TEE_ObjectHandle, size: usize) -> TEE_Result {
1286    // 检查对象句柄是否为空
1287    if object.is_null() {
1288        return TEE_ERROR_BAD_PARAMETERS;
1289    }
1290
1291    let res = unsafe {
1292        _utee_storage_obj_trunc(
1293            object as u64,
1294            size, // 直接使用 usize,不需要转换为 u64
1295        )
1296    } as TEE_Result;
1297
1298    // 检查返回结果,如果不是预期的错误类型则panic
1299    if res != TEE_SUCCESS
1300        && res != TEE_ERROR_STORAGE_NO_SPACE
1301        && res != TEE_ERROR_CORRUPT_OBJECT
1302        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1303    {
1304        TEE_Panic(res as u32);
1305    }
1306
1307    res
1308}
1309
1310/// 寻找对象数据
1311///
1312/// 设置持久化对象的数据访问位置
1313/// 寻找对象数据
1314///
1315/// 设置持久化对象的数据访问位置
1316#[unsafe(no_mangle)]
1317pub extern "C" fn TEE_SeekObjectData(
1318    object: TEE_ObjectHandle,
1319    offset: i64, // intmax_t 对应 i64
1320    whence: TEE_Whence,
1321) -> TEE_Result {
1322    // 检查对象句柄是否为空
1323    if object.is_null() {
1324        return TEE_ERROR_BAD_PARAMETERS;
1325    }
1326
1327    // 获取对象信息
1328    let mut info = unsafe { std::mem::zeroed::<crate::utee_types::utee_object_info>() };
1329    let mut res = unsafe { _utee_cryp_obj_get_info(object as u64, &mut info) as TEE_Result };
1330
1331    // 非法/无效句柄:get_info 失败应对标 OP-TEE 走 TEE_Panic(CA 见 TARGET_DEAD),
1332    // 勿将 ITEM_NOT_FOUND 等直接返回给 TA。
1333    if res != TEE_SUCCESS {
1334        if res != TEE_ERROR_CORRUPT_OBJECT && res != TEE_ERROR_STORAGE_NOT_AVAILABLE {
1335            TEE_Panic(res as u32);
1336        }
1337        return res;
1338    }
1339
1340    // 保存whence的转换值,避免移动错误
1341    let whence_u32 = whence as u32;
1342    let whence_u64 = whence as u64;
1343
1344    // 检查偏移量和寻址方式
1345    match whence_u32 {
1346        TEE_DATA_SEEK_SET => {
1347            if offset > 0 && offset as u32 > TEE_DATA_MAX_POSITION {
1348                return TEE_ERROR_OVERFLOW;
1349            }
1350        }
1351        TEE_DATA_SEEK_CUR => {
1352            if offset > 0
1353                && (offset as u32 + info.data_pos > TEE_DATA_MAX_POSITION as u32
1354                    || offset as u32 + info.data_pos < info.data_pos)
1355            {
1356                return TEE_ERROR_OVERFLOW;
1357            }
1358        }
1359        TEE_DATA_SEEK_END => {
1360            if offset > 0
1361                && (offset as u32 + info.data_size > TEE_DATA_MAX_POSITION as u32
1362                    || offset as u32 + info.data_size < info.data_size)
1363            {
1364                return TEE_ERROR_OVERFLOW;
1365            }
1366        }
1367        _ => {
1368            return TEE_ERROR_ITEM_NOT_FOUND;
1369        }
1370    }
1371
1372    // 执行寻址操作
1373    res = unsafe { _utee_storage_obj_seek(object as u64, offset as i32, whence_u64) as TEE_Result };
1374
1375    // 检查返回结果,如果不是预期的错误类型则panic
1376    if res != TEE_SUCCESS
1377        && res != TEE_ERROR_OVERFLOW
1378        && res != TEE_ERROR_CORRUPT_OBJECT
1379        && res != TEE_ERROR_STORAGE_NOT_AVAILABLE
1380    {
1381        TEE_Panic(res as u32);
1382    }
1383
1384    res
1385}