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_DRIVER_RW_API_H
177777dab0Sopenharmony_ci#define RPMB_DRIVER_RW_API_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_driver_rw_api.h
317777dab0Sopenharmony_ci *
327777dab0Sopenharmony_ci * @brief APIs related to RPMB driver read and write.
337777dab0Sopenharmony_ci * Provides the function of reading and writing RPMB driver.
347777dab0Sopenharmony_ci *
357777dab0Sopenharmony_ci * @library NA
367777dab0Sopenharmony_ci * @kit TEEKit
377777dab0Sopenharmony_ci * @syscap SystemCapability.Tee.TeeClient
387777dab0Sopenharmony_ci * @since 12
397777dab0Sopenharmony_ci * @version 1.0
407777dab0Sopenharmony_ci */
417777dab0Sopenharmony_ci
427777dab0Sopenharmony_ci#ifdef __cplusplus
437777dab0Sopenharmony_ciextern "C" {
447777dab0Sopenharmony_ci#endif
457777dab0Sopenharmony_ci
467777dab0Sopenharmony_ci/**
477777dab0Sopenharmony_ci * @brief Defines the total block number.
487777dab0Sopenharmony_ci *
497777dab0Sopenharmony_ci * @since 12
507777dab0Sopenharmony_ci * @version 1.0
517777dab0Sopenharmony_ci */
527777dab0Sopenharmony_ci#define TOTAL_BLK 4
537777dab0Sopenharmony_ci
547777dab0Sopenharmony_ci/**
557777dab0Sopenharmony_ci * @brief Defines the size of block.
567777dab0Sopenharmony_ci *
577777dab0Sopenharmony_ci * @since 12
587777dab0Sopenharmony_ci * @version 1.0
597777dab0Sopenharmony_ci */
607777dab0Sopenharmony_ci#define BLK_SIZE 256
617777dab0Sopenharmony_ci
627777dab0Sopenharmony_ci/**
637777dab0Sopenharmony_ci * @brief Defines the size of the total block.
647777dab0Sopenharmony_ci *
657777dab0Sopenharmony_ci * @since 12
667777dab0Sopenharmony_ci * @version 1.0
677777dab0Sopenharmony_ci */
687777dab0Sopenharmony_ci#define TOTAL_BLK_SIZE (TOTAL_BLK * BLK_SIZE)
697777dab0Sopenharmony_ci
707777dab0Sopenharmony_ci#define SEC_WRITE_PROTECT_ENTRY_NUM 4
717777dab0Sopenharmony_ci#define SEC_WRITE_PROTECT_ENTRY_RESERVED_NUM 3
727777dab0Sopenharmony_ci#define SEC_WRITE_PROTECT_ENTRY_RESERVED_SIZE 16
737777dab0Sopenharmony_ci#define SEC_WRITE_PROTECT_FRAME_RESERVED_NUM 14
747777dab0Sopenharmony_ci#define SEC_WRITE_PROTECT_FRAME_RESERVED_END_NUM 176
757777dab0Sopenharmony_ci#define SEC_WRITE_PROTECT_BLK_SIZE 256
767777dab0Sopenharmony_ci#define SEC_WRITE_PROTECT_LUN_MAX 5
777777dab0Sopenharmony_ci
787777dab0Sopenharmony_ci/**
797777dab0Sopenharmony_ci * @brief A WPF set to one specifies that the logical unit shall inhibit alteration of the medium for LBA within
807777dab0Sopenharmony_ci * the range indicated by LOGICAL BLOCK ADDRESS field and NUMBER OF LOGICAL BLOCKS field.
817777dab0Sopenharmony_ci * Commands requiring writes to the medium shall be terminated with CHECK CONDITION status,
827777dab0Sopenharmony_ci * with the sense key set to DATA PROTECT, and the additional sense code set to WRITE PROTECTED.
837777dab0Sopenharmony_ci *
847777dab0Sopenharmony_ci * @since 12
857777dab0Sopenharmony_ci * @version 1.0
867777dab0Sopenharmony_ci */
877777dab0Sopenharmony_citypedef enum {
887777dab0Sopenharmony_ci    SEC_WRITE_PROTECT_DISABLE = 0,
897777dab0Sopenharmony_ci    SEC_WRITE_PROTECT_ENABLE = 1,
907777dab0Sopenharmony_ci} write_protect_flag;
917777dab0Sopenharmony_ci
927777dab0Sopenharmony_ci/**
937777dab0Sopenharmony_ci * @brief Write Protect Type specifies how WPF bit may be modified.
947777dab0Sopenharmony_ci *
957777dab0Sopenharmony_ci * @since 12
967777dab0Sopenharmony_ci * @version 1.0
977777dab0Sopenharmony_ci */
987777dab0Sopenharmony_citypedef enum {
997777dab0Sopenharmony_ci    /** WPF bit is persistent through power cycle and hardware reset.
1007777dab0Sopenharmony_ci    * WPF value may only be changed writing to Secure Write Protect Configuration Block.
1017777dab0Sopenharmony_ci    */
1027777dab0Sopenharmony_ci    NV_TYPE = 0,
1037777dab0Sopenharmony_ci    /** WPF bit is automatically cleared to 0b after power cycle or hardware reset. */
1047777dab0Sopenharmony_ci    P_TYPE = 1,
1057777dab0Sopenharmony_ci    /** WPF bit is automatically set to 1b after power cycle or hardware reset. */
1067777dab0Sopenharmony_ci    NV_AWP_TYPE = 2,
1077777dab0Sopenharmony_ci} write_protect_type;
1087777dab0Sopenharmony_ci
1097777dab0Sopenharmony_ci/**
1107777dab0Sopenharmony_ci * @brief Secure Write Protect Entry.
1117777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1127777dab0Sopenharmony_ci * |     | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0  |
1137777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1147777dab0Sopenharmony_ci * | 0   |       Reserved    |  WFT  | WPF| -> wp_data
1157777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1167777dab0Sopenharmony_ci * | 1   |           Reserved             |
1177777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1187777dab0Sopenharmony_ci * | 2   |           Reserved             |
1197777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1207777dab0Sopenharmony_ci * | 3   |           Reserved             |
1217777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1227777dab0Sopenharmony_ci * | 4   |     LOGICAL BLOCK ADDRESS      | -> logical_blk_addr
1237777dab0Sopenharmony_ci * +-----+                                +
1247777dab0Sopenharmony_ci * | ... |                                |
1257777dab0Sopenharmony_ci * +-----+                                +
1267777dab0Sopenharmony_ci * | 11  |                                |
1277777dab0Sopenharmony_ci * +-----+                                +
1287777dab0Sopenharmony_ci * | 12  |                                |
1297777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1307777dab0Sopenharmony_ci * | ... |     NUMBER OF LOGICAL BLOCKS   | -> logical_blk_num
1317777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1327777dab0Sopenharmony_ci * | 15  |                                |
1337777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1347777dab0Sopenharmony_ci *
1357777dab0Sopenharmony_ci * @since 12
1367777dab0Sopenharmony_ci * @version 1.0
1377777dab0Sopenharmony_ci */
1387777dab0Sopenharmony_cistruct rpmb_protect_cfg_blk_entry {
1397777dab0Sopenharmony_ci    uint8_t wp_data;
1407777dab0Sopenharmony_ci    uint8_t reserved[SEC_WRITE_PROTECT_ENTRY_RESERVED_NUM];
1417777dab0Sopenharmony_ci    /** This field specifies the LBA of the first logical address of the Secure Write Protect ares. */
1427777dab0Sopenharmony_ci    uint64_t logical_blk_addr;
1437777dab0Sopenharmony_ci    /** This field specifies the number of contiguous logical size that belong to the Secure Write Protect. */
1447777dab0Sopenharmony_ci    uint32_t logical_blk_num;
1457777dab0Sopenharmony_ci}__attribute__((packed));
1467777dab0Sopenharmony_ci
1477777dab0Sopenharmony_ci
1487777dab0Sopenharmony_ci/**
1497777dab0Sopenharmony_ci * @brief Secure Write Protect Configuration Block is supported by RPMB region 0 only.
1507777dab0Sopenharmony_ci * This block is used for configuring secure write protect areas in logical units.
1517777dab0Sopenharmony_ci * Each Secure Write Protect Configuration Block for each logical unit.
1527777dab0Sopenharmony_ci * Each entry represents one secure write protect area.
1537777dab0Sopenharmony_ci * If an entry is not used, then the related fields shall contain a value of zero.
1547777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1557777dab0Sopenharmony_ci * |     | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0  |
1567777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1577777dab0Sopenharmony_ci * | 0   |              LUN               |
1587777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1597777dab0Sopenharmony_ci * | 1   |          DATA LENGTH           |
1607777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1617777dab0Sopenharmony_ci * | 2   |                                |
1627777dab0Sopenharmony_ci * +-----+                                +
1637777dab0Sopenharmony_ci * | ... |           Reserved             |
1647777dab0Sopenharmony_ci * +-----+                                +
1657777dab0Sopenharmony_ci * | 15  |                                |
1667777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1677777dab0Sopenharmony_ci * | 16  |                                |
1687777dab0Sopenharmony_ci * +-----+                                +
1697777dab0Sopenharmony_ci * | ... | Secure Write Protect Entry 0   |
1707777dab0Sopenharmony_ci * +-----+                                +
1717777dab0Sopenharmony_ci * | 31  |                                |
1727777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1737777dab0Sopenharmony_ci * | 32  |                                |
1747777dab0Sopenharmony_ci * +-----+                                +
1757777dab0Sopenharmony_ci * | ... | Secure Write Protect Entry 1   |
1767777dab0Sopenharmony_ci * +-----+                                +
1777777dab0Sopenharmony_ci * | 47  |                                |
1787777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1797777dab0Sopenharmony_ci * | 48  |                                |
1807777dab0Sopenharmony_ci * +-----+                                +
1817777dab0Sopenharmony_ci * | ... | Secure Write Protect Entry 1   |
1827777dab0Sopenharmony_ci * +-----+                                +
1837777dab0Sopenharmony_ci * | 63  |                                |
1847777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1857777dab0Sopenharmony_ci * | 64  |                                |
1867777dab0Sopenharmony_ci * +-----+                                +
1877777dab0Sopenharmony_ci * | ... | Secure Write Protect Entry 1   |
1887777dab0Sopenharmony_ci * +-----+                                +
1897777dab0Sopenharmony_ci * | 79  |                                |
1907777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1917777dab0Sopenharmony_ci * | 80  |                                |
1927777dab0Sopenharmony_ci * +-----+                                +
1937777dab0Sopenharmony_ci * | ... |           Reserved             |
1947777dab0Sopenharmony_ci * +-----+                                +
1957777dab0Sopenharmony_ci * | 255 |                                |
1967777dab0Sopenharmony_ci * +-----+---+---+---+---+---+---+---+----+
1977777dab0Sopenharmony_ci *
1987777dab0Sopenharmony_ci * @since 12
1997777dab0Sopenharmony_ci * @version 1.0
2007777dab0Sopenharmony_ci */
2017777dab0Sopenharmony_cistruct rpmb_protect_cfg_block {
2027777dab0Sopenharmony_ci    uint8_t lun;
2037777dab0Sopenharmony_ci    uint8_t data_length;
2047777dab0Sopenharmony_ci    uint8_t reserved[SEC_WRITE_PROTECT_FRAME_RESERVED_NUM];
2057777dab0Sopenharmony_ci    struct rpmb_protect_cfg_blk_entry entries[SEC_WRITE_PROTECT_ENTRY_NUM];
2067777dab0Sopenharmony_ci    uint8_t reserved_end[SEC_WRITE_PROTECT_FRAME_RESERVED_END_NUM];
2077777dab0Sopenharmony_ci}__attribute__((packed));
2087777dab0Sopenharmony_ci
2097777dab0Sopenharmony_ci/**
2107777dab0Sopenharmony_ci * @brief Write protect config block by RPMB driver.
2117777dab0Sopenharmony_ci *
2127777dab0Sopenharmony_ci * @param lun Indicates the logical unit to which secure write protection shall apply,
2137777dab0Sopenharmony_ci * and <b>0</b> <= lun <= {@code SEC_WRITE_PROTECT_LUN_MAX}
2147777dab0Sopenharmony_ci * @param entries Indicates the Secure Write Protect Entry array, The maximum length is 4.
2157777dab0Sopenharmony_ci * @param len Indicates the real length of the Secure Write Protect Entry array, which value is less than 4.
2167777dab0Sopenharmony_ci *
2177777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
2187777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if the input parameter is incorrect.
2197777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_OUT_OF_MEMORY} if the send message fail.
2207777dab0Sopenharmony_ci *
2217777dab0Sopenharmony_ci * @since 12
2227777dab0Sopenharmony_ci * @version 1.0
2237777dab0Sopenharmony_ci */
2247777dab0Sopenharmony_ciTEE_Result tee_ext_rpmb_protect_cfg_blk_write(uint8_t lun, struct rpmb_protect_cfg_blk_entry *entries, uint32_t len);
2257777dab0Sopenharmony_ci
2267777dab0Sopenharmony_ci/**
2277777dab0Sopenharmony_ci * @brief Read protect config block by RPMB driver.
2287777dab0Sopenharmony_ci *
2297777dab0Sopenharmony_ci * @param lun Indicates the logical unit to which secure read protection shall apply,
2307777dab0Sopenharmony_ci * and 0 <= lun <= <b>SEC_WRITE_PROTECT_LUN_MAX</b>.
2317777dab0Sopenharmony_ci * @param entries Indicates the Secure Read Protect Entry array, The maximum length is 4.
2327777dab0Sopenharmony_ci * @param len Indicates the real length of the Secure Read Protect Entry array, which value is less than 4.
2337777dab0Sopenharmony_ci *
2347777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
2357777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if the input parameter is incorrect.
2367777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_OUT_OF_MEMORY} if the send message fail.
2377777dab0Sopenharmony_ci  *
2387777dab0Sopenharmony_ci * @since 12
2397777dab0Sopenharmony_ci * @version 1.0
2407777dab0Sopenharmony_ci */
2417777dab0Sopenharmony_ciTEE_Result tee_ext_rpmb_protect_cfg_blk_read(uint8_t lun, struct rpmb_protect_cfg_blk_entry *entries, uint32_t *len);
2427777dab0Sopenharmony_ci
2437777dab0Sopenharmony_ci/**
2447777dab0Sopenharmony_ci * @brief Write plaintext buffer to RPMB driver.
2457777dab0Sopenharmony_ci *
2467777dab0Sopenharmony_ci * @param buf Indicates the buffer for writing data.
2477777dab0Sopenharmony_ci * @param size Indicates the length of buffer, the maximum value is 1024.
2487777dab0Sopenharmony_ci * @param block Indicates the block index of the position of start block, the value is [0, 3].
2497777dab0Sopenharmony_ci * @param offset Indicates the offset bytes of data position, and the value of offest bytes is less than 256.
2507777dab0Sopenharmony_ci *
2517777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
2527777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if the input parameter is incorrect.
2537777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_OUT_OF_MEMORY} if the send message fail.
2547777dab0Sopenharmony_ci  *
2557777dab0Sopenharmony_ci * @since 12
2567777dab0Sopenharmony_ci * @version 1.0
2577777dab0Sopenharmony_ci */
2587777dab0Sopenharmony_ciTEE_Result tee_ext_rpmb_driver_write(const uint8_t *buf, size_t size, uint32_t block, uint32_t offset);
2597777dab0Sopenharmony_ci
2607777dab0Sopenharmony_ci/**
2617777dab0Sopenharmony_ci * @brief Read plaintext buffer from RPMB driver.
2627777dab0Sopenharmony_ci *
2637777dab0Sopenharmony_ci * @param buf Indicates the buffer for read data.
2647777dab0Sopenharmony_ci * @param size Indicates the length of buffer, the maximum value is 1024.
2657777dab0Sopenharmony_ci * @param block Indicates the block index of the position of start block, the value is [0, 3].
2667777dab0Sopenharmony_ci * @param offset Indicates the offset bytes of data position, and the value of offest bytes is less than 256.
2677777dab0Sopenharmony_ci *
2687777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
2697777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if the input parameter is incorrect.
2707777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_OUT_OF_MEMORY} if the send message fail.
2717777dab0Sopenharmony_ci  *
2727777dab0Sopenharmony_ci * @since 12
2737777dab0Sopenharmony_ci * @version 1.0
2747777dab0Sopenharmony_ci */
2757777dab0Sopenharmony_ciTEE_Result tee_ext_rpmb_driver_read(uint8_t *buf, size_t size, uint32_t block, uint32_t offset);
2767777dab0Sopenharmony_ci
2777777dab0Sopenharmony_ci/**
2787777dab0Sopenharmony_ci * @brief Remove data from RPMB driver.
2797777dab0Sopenharmony_ci *
2807777dab0Sopenharmony_ci * @param size Indicates the length of remove data, the maximum value is 1024.
2817777dab0Sopenharmony_ci * @param block Indicates the block index of the position of start block, the value is [0, 3].
2827777dab0Sopenharmony_ci * @param offset Indicates the offset bytes of data position, and the value of offest bytes is less than 256.
2837777dab0Sopenharmony_ci *
2847777dab0Sopenharmony_ci * @return Returns {@code TEE_SUCCESS} if the operation is successful.
2857777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_BAD_PARAMETERS} if the input parameter is incorrect.
2867777dab0Sopenharmony_ci *         Returns {@code TEE_ERROR_OUT_OF_MEMORY} if the send message fail.
2877777dab0Sopenharmony_ci  *
2887777dab0Sopenharmony_ci * @since 12
2897777dab0Sopenharmony_ci * @version 1.0
2907777dab0Sopenharmony_ci */
2917777dab0Sopenharmony_ciTEE_Result tee_ext_rpmb_driver_remove(size_t size, uint32_t block, uint32_t offset);
2927777dab0Sopenharmony_ci
2937777dab0Sopenharmony_ci#ifdef __cplusplus
2947777dab0Sopenharmony_ci}
2957777dab0Sopenharmony_ci#endif
2967777dab0Sopenharmony_ci
2977777dab0Sopenharmony_ci/** @} */
2987777dab0Sopenharmony_ci#endif
299