17777dab0Sopenharmony_ci/*
27777dab0Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
37777dab0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
47777dab0Sopenharmony_ci * you may not use this file except in compliance with the License.
57777dab0Sopenharmony_ci * You may obtain a copy of the License at
67777dab0Sopenharmony_ci *
77777dab0Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
87777dab0Sopenharmony_ci *
97777dab0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
107777dab0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
117777dab0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127777dab0Sopenharmony_ci * See the License for the specific language governing permissions and
137777dab0Sopenharmony_ci * limitations under the License.
147777dab0Sopenharmony_ci */
157777dab0Sopenharmony_ci
167777dab0Sopenharmony_ci#ifndef RPMB_RPMB_FCNTL_H
177777dab0Sopenharmony_ci#define RPMB_RPMB_FCNTL_H
187777dab0Sopenharmony_ci/**
197777dab0Sopenharmony_ci * @addtogroup TeeTrusted
207777dab0Sopenharmony_ci * @{
217777dab0Sopenharmony_ci *
227777dab0Sopenharmony_ci * @brief TEE(Trusted Excution Environment) API.
237777dab0Sopenharmony_ci * Provides security capability APIs such as trusted storage, encryption and decryption,
247777dab0Sopenharmony_ci * and trusted time for trusted application development.
257777dab0Sopenharmony_ci *
267777dab0Sopenharmony_ci * @since 12
277777dab0Sopenharmony_ci */
287777dab0Sopenharmony_ci
297777dab0Sopenharmony_ci/**
307777dab0Sopenharmony_ci * @file rpmb_fcntl.h
317777dab0Sopenharmony_ci *
327777dab0Sopenharmony_ci * @brief Provides the APIs related to RPMB service.
337777dab0Sopenharmony_ci *
347777dab0Sopenharmony_ci * @library NA
357777dab0Sopenharmony_ci * @kit TEEKit
367777dab0Sopenharmony_ci * @syscap SystemCapability.Tee.TeeClient
377777dab0Sopenharmony_ci * @since 12
387777dab0Sopenharmony_ci * @version 1.0
397777dab0Sopenharmony_ci */
407777dab0Sopenharmony_ci
417777dab0Sopenharmony_ci#ifdef __cplusplus
427777dab0Sopenharmony_ciextern "C" {
437777dab0Sopenharmony_ci#endif
447777dab0Sopenharmony_ci
457777dab0Sopenharmony_ci/**
467777dab0Sopenharmony_ci * @brief Partition initialization, perform RPMB Key writing and formatting operations.
477777dab0Sopenharmony_ci *
487777dab0Sopenharmony_ci * @attention This function only needs to be executed once.
497777dab0Sopenharmony_ci *
507777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
517777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_GENERIC} if the RPMB controller general error.
527777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_MAC_FAIL} if the RPMB controller MAC check error.
537777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_RESP_UNEXPECT_MAC} if the RPMB response data MAC check error.
547777dab0Sopenharmony_ci *
557777dab0Sopenharmony_ci * @since 12
567777dab0Sopenharmony_ci * @version 1.0
577777dab0Sopenharmony_ci */
587777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_Init(void);
597777dab0Sopenharmony_ci
607777dab0Sopenharmony_ci/**
617777dab0Sopenharmony_ci * @brief RPMB secure storage fully formatted operation.
627777dab0Sopenharmony_ci *
637777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
647777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_GENERIC} if the RPMB controller general error.
657777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_MAC_FAIL} if the RPMB controller MAC check error.
667777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_RESP_UNEXPECT_MAC} if the RPMB response data MAC check error.
677777dab0Sopenharmony_ci *
687777dab0Sopenharmony_ci * @since 12
697777dab0Sopenharmony_ci * @version 1.0
707777dab0Sopenharmony_ci */
717777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_Format(void);
727777dab0Sopenharmony_ci
737777dab0Sopenharmony_ci/**
747777dab0Sopenharmony_ci * @brief Write files to RPMB.
757777dab0Sopenharmony_ci *
767777dab0Sopenharmony_ci * @attention If you want to improve the performance of writing files, you need to define the heap size in TA's
777777dab0Sopenharmony_ci * manifest to be at leaset 3 times the file size plus 256KB.
787777dab0Sopenharmony_ci * For example: To write a file with a size of 100KB, the defined heap size is at least
797777dab0Sopenharmony_ci * 556KB (3 * 100 + 256). If the heap size cannot be satisfied, the file writing will still succeed,
807777dab0Sopenharmony_ci * but the performance will be poor.
817777dab0Sopenharmony_ci *
827777dab0Sopenharmony_ci * @param filename Indicates the file name of the data to be written, the maximum length is 64 bytes.
837777dab0Sopenharmony_ci * @param buf Indicates the buffer for writting data.
847777dab0Sopenharmony_ci * @param size Indicates the size of the written data, the maximum size is 160KB.
857777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
867777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if input parameter is incorrect, or the file name is longer than 64
877777dab0Sopenharmony_ci * bytes.
887777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_NOSPC} if the RPMB partition has insufficient disk space.
897777dab0Sopenharmony_ci *
907777dab0Sopenharmony_ci * @since 12
917777dab0Sopenharmony_ci * @version 1.0
927777dab0Sopenharmony_ci */
937777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_Write(const char *filename, const uint8_t *buf, size_t size);
947777dab0Sopenharmony_ci
957777dab0Sopenharmony_ci/**
967777dab0Sopenharmony_ci * @brief Read files from RPMB.
977777dab0Sopenharmony_ci *
987777dab0Sopenharmony_ci * @attention If you want to improve the performance of reading files, you need to define the heap size in TA's
997777dab0Sopenharmony_ci * manifest to be at leaset 3 times the file size plus 256KB.
1007777dab0Sopenharmony_ci * For example: To read a file with a size of 100KB, the defined heap size is at least
1017777dab0Sopenharmony_ci * 556KB (3 * 100 + 256). If the heap size cannot be satisfied, the file reading will still succeed,
1027777dab0Sopenharmony_ci * but the performance will be poor.
1037777dab0Sopenharmony_ci *
1047777dab0Sopenharmony_ci * @param filename Indicates the file name of the data to be read, the maximum length is 64 bytes.
1057777dab0Sopenharmony_ci * @param buf Indicates the buffer for reading data.
1067777dab0Sopenharmony_ci * @param size Indicates the read data size.
1077777dab0Sopenharmony_ci * @param count Indicates the size of the actual read.
1087777dab0Sopenharmony_ci *
1097777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
1107777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if input parameter is incorrect, or the file name is longer than 64
1117777dab0Sopenharmony_ci * bytes.
1127777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_FILE_NOT_FOUND} if the file dose not exist.
1137777dab0Sopenharmony_ci *
1147777dab0Sopenharmony_ci * @since 12
1157777dab0Sopenharmony_ci * @version 1.0
1167777dab0Sopenharmony_ci */
1177777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_Read(const char *filename, uint8_t *buf, size_t size, uint32_t *count);
1187777dab0Sopenharmony_ci
1197777dab0Sopenharmony_ci/**
1207777dab0Sopenharmony_ci * @brief Rename file name in RPMB.
1217777dab0Sopenharmony_ci *
1227777dab0Sopenharmony_ci * @param old_name Indicates the old file name.
1237777dab0Sopenharmony_ci * @param new_name Indicates the new file name.
1247777dab0Sopenharmony_ci *
1257777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
1267777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if input parameter is incorrect, or the file name is longer than 64
1277777dab0Sopenharmony_ci * bytes.
1287777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_FILE_NOT_FOUND} if the file dose not exist.
1297777dab0Sopenharmony_ci *
1307777dab0Sopenharmony_ci * @since 12
1317777dab0Sopenharmony_ci * @version 1.0
1327777dab0Sopenharmony_ci */
1337777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_Rename(const char *old_name, const char *new_name);
1347777dab0Sopenharmony_ci
1357777dab0Sopenharmony_ci/**
1367777dab0Sopenharmony_ci * @brief Delete files in RPMB.
1377777dab0Sopenharmony_ci *
1387777dab0Sopenharmony_ci * @param filename Indicates the file name to be deleted.
1397777dab0Sopenharmony_ci *
1407777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
1417777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if input parameter is incorrect, or the file name is longer than 64
1427777dab0Sopenharmony_ci * bytes.
1437777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_FILE_NOT_FOUND} if the file dose not exist.
1447777dab0Sopenharmony_ci *
1457777dab0Sopenharmony_ci * @since 12
1467777dab0Sopenharmony_ci * @version 1.0
1477777dab0Sopenharmony_ci */
1487777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_Rm(const char *filename);
1497777dab0Sopenharmony_ci
1507777dab0Sopenharmony_ci/**
1517777dab0Sopenharmony_ci * @brief File status stored in RPMB partition, used in {@code TEE_RPMB_FS_Stat}.
1527777dab0Sopenharmony_ci *
1537777dab0Sopenharmony_ci * @since 12
1547777dab0Sopenharmony_ci */
1557777dab0Sopenharmony_cistruct rpmb_fs_stat {
1567777dab0Sopenharmony_ci    /** Indicates the file size. */
1577777dab0Sopenharmony_ci    uint32_t size;
1587777dab0Sopenharmony_ci    uint32_t reserved;
1597777dab0Sopenharmony_ci};
1607777dab0Sopenharmony_ci
1617777dab0Sopenharmony_ci/**
1627777dab0Sopenharmony_ci * @brief Get file status in RPMB.
1637777dab0Sopenharmony_ci *
1647777dab0Sopenharmony_ci * @param filename Indicates the file name in RPMB.
1657777dab0Sopenharmony_ci * @param stat Indicates the file status information obtained.
1667777dab0Sopenharmony_ci *
1677777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
1687777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if input parameter is incorrect, or the file name is longer than 64
1697777dab0Sopenharmony_ci * bytes.
1707777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_FILE_NOT_FOUND} if the file dose not exist.
1717777dab0Sopenharmony_ci *
1727777dab0Sopenharmony_ci * @since 12
1737777dab0Sopenharmony_ci * @version 1.0
1747777dab0Sopenharmony_ci */
1757777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_Stat(const char *filename, struct rpmb_fs_stat *stat);
1767777dab0Sopenharmony_ci
1777777dab0Sopenharmony_ci/**
1787777dab0Sopenharmony_ci * @brief Disk status stored in RPMB partition, used in {@code TEE_RPMB_FS_StatDisk}.
1797777dab0Sopenharmony_ci *
1807777dab0Sopenharmony_ci * @since 12
1817777dab0Sopenharmony_ci */
1827777dab0Sopenharmony_cistruct rpmb_fs_statdisk {
1837777dab0Sopenharmony_ci    /** Indicates the total size of RPMB partition. */
1847777dab0Sopenharmony_ci    uint32_t disk_size;
1857777dab0Sopenharmony_ci    /** Indicates the TA used size. */
1867777dab0Sopenharmony_ci    uint32_t ta_used_size;
1877777dab0Sopenharmony_ci    /** Indicates the free size of the RPMB partition. */
1887777dab0Sopenharmony_ci    uint32_t free_size;
1897777dab0Sopenharmony_ci    uint32_t reserved;
1907777dab0Sopenharmony_ci};
1917777dab0Sopenharmony_ci
1927777dab0Sopenharmony_ci/**
1937777dab0Sopenharmony_ci * @brief Get the disk status.
1947777dab0Sopenharmony_ci *
1957777dab0Sopenharmony_ci * @param stat Indicates the disk status information obtained.
1967777dab0Sopenharmony_ci *
1977777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
1987777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if input parameter is incorrect.
1997777dab0Sopenharmony_ci *
2007777dab0Sopenharmony_ci * @since 12
2017777dab0Sopenharmony_ci * @version 1.0
2027777dab0Sopenharmony_ci */
2037777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_StatDisk(struct rpmb_fs_statdisk *stat);
2047777dab0Sopenharmony_ci
2057777dab0Sopenharmony_ci/**
2067777dab0Sopenharmony_ci * @brief File attribute definition, which means that the file cannot be erased during the factory reset.
2077777dab0Sopenharmony_ci *
2087777dab0Sopenharmony_ci * @since 12
2097777dab0Sopenharmony_ci*/
2107777dab0Sopenharmony_ci#define TEE_RPMB_FMODE_NON_ERASURE (1U << 0)
2117777dab0Sopenharmony_ci
2127777dab0Sopenharmony_ci/**
2137777dab0Sopenharmony_ci * @brief  File attribute definition, which means the attribute value of the cleard file.
2147777dab0Sopenharmony_ci *
2157777dab0Sopenharmony_ci * @since 12
2167777dab0Sopenharmony_ci*/
2177777dab0Sopenharmony_ci#define TEE_RPMB_FMODE_CLEAR 0
2187777dab0Sopenharmony_ci
2197777dab0Sopenharmony_ci
2207777dab0Sopenharmony_ci/**
2217777dab0Sopenharmony_ci * @brief Set the file attribute in RPMB.
2227777dab0Sopenharmony_ci *
2237777dab0Sopenharmony_ci * @param filename Indicates the file name in RPMB.
2247777dab0Sopenharmony_ci * @param fmode Indicates the file attribute, currently supports {@code TEE_RPMB_FMODE_NON_ERASURE} and
2257777dab0Sopenharmony_ci * {@code TEE_RPMB_FMODE_CLEAR} two attributes, other values will return {@code TEE_ERROR_BAD_PARAMETERS}.
2267777dab0Sopenharmony_ci *
2277777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
2287777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if input parameter is incorrect,
2297777dab0Sopenharmony_ci * or the file name is longer than 64 bytes.
2307777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_FILE_NOT_FOUND} if the file dose not exist.
2317777dab0Sopenharmony_ci *
2327777dab0Sopenharmony_ci * @since 12
2337777dab0Sopenharmony_ci * @version 1.0
2347777dab0Sopenharmony_ci */
2357777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_SetAttr(const char *filename, uint32_t fmode);
2367777dab0Sopenharmony_ci
2377777dab0Sopenharmony_ci/**
2387777dab0Sopenharmony_ci * @brief Format, delete file attribute is erasable file, keep the file attribute is an inerasable file.
2397777dab0Sopenharmony_ci *
2407777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
2417777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_GENERIC} if the RPMB controller general error.
2427777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_MAC_FAIL} if the RPMB controller MAC check error.
2437777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_RPMB_RESP_UNEXPECT_MAC} if the RPMB response data MAC check error.
2447777dab0Sopenharmony_ci *
2457777dab0Sopenharmony_ci * @since 12
2467777dab0Sopenharmony_ci * @version 1.0
2477777dab0Sopenharmony_ci */
2487777dab0Sopenharmony_ciTEE_Result TEE_RPMB_FS_Erase(void);
2497777dab0Sopenharmony_ci
2507777dab0Sopenharmony_ci/**
2517777dab0Sopenharmony_ci * @brief  Enumerates the types of RPMB key status, used in {@code TEE_RPMB_KEY_Status}.
2527777dab0Sopenharmony_ci *
2537777dab0Sopenharmony_ci * @since 12
2547777dab0Sopenharmony_ci*/
2557777dab0Sopenharmony_cienum TEE_RPMB_KEY_STAT {
2567777dab0Sopenharmony_ci    /** RPMB Key status is invalid. */
2577777dab0Sopenharmony_ci    TEE_RPMB_KEY_INVALID = 0x0,
2587777dab0Sopenharmony_ci    /** RPMB Key has been programmed and matched correctly. */
2597777dab0Sopenharmony_ci    TEE_RPMB_KEY_SUCCESS,
2607777dab0Sopenharmony_ci    /** RPMB Key is not programmed. */
2617777dab0Sopenharmony_ci    TEE_RPMB_KEY_NOT_PROGRAM,
2627777dab0Sopenharmony_ci    /** RPMB Key has been programmed but failed to match. */
2637777dab0Sopenharmony_ci    TEE_RPMB_KEY_NOT_MATCH,
2647777dab0Sopenharmony_ci};
2657777dab0Sopenharmony_ci
2667777dab0Sopenharmony_ci/**
2677777dab0Sopenharmony_ci * @brief Obtain RPMB Key status.
2687777dab0Sopenharmony_ci *
2697777dab0Sopenharmony_ci * @return Returns {@code TEE_RPMB_KEY_SUCCESS} if the RPMB Key has been programmed and matched correctly.
2707777dab0Sopenharmony_ci *         Returns {@code TEE_RPMB_KEY_NOT_PROGRAM} if the RPMB Key is not programmed.
2717777dab0Sopenharmony_ci *         Returns {@code TEE_RPMB_KEY_NOT_MATCH} if RPMB Key has been programmed but failed to match.
2727777dab0Sopenharmony_ci *         Returns {@code TEE_RPMB_KEY_INVALID} if the RPMB Key status is invalid.
2737777dab0Sopenharmony_ci *
2747777dab0Sopenharmony_ci * @since 12
2757777dab0Sopenharmony_ci * @version 1.0
2767777dab0Sopenharmony_ci */
2777777dab0Sopenharmony_ciuint32_t TEE_RPMB_KEY_Status(void);
2787777dab0Sopenharmony_ci
2797777dab0Sopenharmony_ci/**
2807777dab0Sopenharmony_ci * @brief Process the current TA version information.
2817777dab0Sopenharmony_ci *
2827777dab0Sopenharmony_ci * @param ta_version Indicates the TA version.
2837777dab0Sopenharmony_ci *
2847777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
2857777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if input parameter is incorrect.
2867777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_GENERIC} if the processing failed.
2877777dab0Sopenharmony_ci *
2887777dab0Sopenharmony_ci * @since 12
2897777dab0Sopenharmony_ci * @version 1.0
2907777dab0Sopenharmony_ci */
2917777dab0Sopenharmony_ciTEE_Result TEE_RPMB_TAVERSION_Process(uint32_t ta_version);
2927777dab0Sopenharmony_ci#ifdef __cplusplus
2937777dab0Sopenharmony_ci}
2947777dab0Sopenharmony_ci#endif
2957777dab0Sopenharmony_ci
2967777dab0Sopenharmony_ci/** @} */
2977777dab0Sopenharmony_ci#endif
298