Imre Kis | 01d0ce8 | 2023-10-17 18:31:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef RPMB_BACKEND_H_ |
| 8 | #define RPMB_BACKEND_H_ |
| 9 | |
| 10 | #include "psa/error.h" |
| 11 | #include "compiler.h" |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | |
| 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
| 19 | /** |
| 20 | * \brief RPMB request/response message types |
| 21 | * |
| 22 | * Defined in eMMC 4.5 (JESD84-B51) standard's Table 18. |
| 23 | */ |
| 24 | #define RPMB_REQ_TYPE_AUTHENTICATION_KEY_WRITE (0x0001) |
| 25 | #define RPMB_REQ_TYPE_READ_WRITE_COUNTER (0x0002) |
| 26 | #define RPMB_REQ_TYPE_AUTHENTICATED_DATA_WRITE (0x0003) |
| 27 | #define RPMB_REQ_TYPE_AUTHENTICATED_DATA_READ (0x0004) |
| 28 | #define RPMB_REQ_TYPE_RESULT_READ_REQUEST (0x0005) |
| 29 | #define RPMB_REQ_TYPE_DEVICE_CONFIG_WRITE (0x0006) |
| 30 | #define RPMB_REQ_TYPE_DEVICE_CONFIG_READ (0x0007) |
| 31 | |
| 32 | #define RPMB_RESP_TYPE_AUTHENTICATION_KEY_WRITE (0x0100) |
| 33 | #define RPMB_RESP_TYPE_READ_WRITE_COUNTER (0x0200) |
| 34 | #define RPMB_RESP_TYPE_AUTHENTICATED_DATA_WRITE (0x0300) |
| 35 | #define RPMB_RESP_TYPE_AUTHENTICATED_DATA_READ (0x0400) |
| 36 | #define RPMB_RESP_TYPE_DEVICE_CONFIG_WRITE (0x0600) |
| 37 | #define RPMB_RESP_TYPE_DEVICE_CONFIG_READ (0x0700) |
| 38 | |
| 39 | /** |
| 40 | * \brief RPMB operation results |
| 41 | * |
| 42 | * Defined in eMMC 4.5 (JESD84-B51) standard's Table 20. |
| 43 | */ |
| 44 | #define RPMB_RES_OK (0x0000) |
| 45 | #define RPMB_RES_GENERAL_FAILURE (0x0001) |
| 46 | #define RPMB_RES_AUTHENTICATION_FAILURE (0x0002) |
| 47 | #define RPMB_RES_COUNTER_FAILURE (0x0003) |
| 48 | #define RPMB_RES_ADDRESS_FAILURE (0x0004) |
| 49 | #define RPMB_RES_WRITE_FAILURE (0x0005) |
| 50 | #define RPMB_RES_READ_FAILURE (0x0006) |
| 51 | #define RPMB_RES_KEY_NOT_PROGRAMMED (0x0007) |
| 52 | |
| 53 | #define RPMB_RES_COUNTER_EXPIRED (0x0080) |
| 54 | |
| 55 | /** |
| 56 | * \brief Additional RPMB related definitions |
| 57 | */ |
| 58 | #define RPMB_EMMC_CID_SIZE (16) |
| 59 | #define RPMB_CID_PRODUCT_REVISION (9) |
| 60 | #define RPMB_CID_CRC7 (15) |
| 61 | #define RPMB_STUFF_DATA_SIZE (196) |
| 62 | #define RPMB_KEY_MAC_SIZE (32) |
| 63 | #define RPMB_DATA_SIZE (256) |
| 64 | #define RPMB_NONCE_SIZE (16) |
| 65 | #define RPMB_SIZE_MULT_UNIT (128 * 1024) |
| 66 | |
| 67 | /** |
| 68 | * \brief RPMB device info |
| 69 | * |
| 70 | * The RPMB device info structure contains the Device Identification (CID) and RPMB_SIZE_MULT |
| 71 | * registers' value. The CID value is unique to each RPMB device and it can be involved into the |
| 72 | * authentication key generation process. The RPMB_SIZE_MULT value indicates the size of the RPMB |
| 73 | * in 128kB units. |
| 74 | */ |
| 75 | struct rpmb_dev_info { |
| 76 | uint8_t cid[RPMB_EMMC_CID_SIZE]; |
| 77 | uint8_t rpmb_size_mult; |
| 78 | } __packed; |
| 79 | |
| 80 | /** |
| 81 | * \brief RPMB data frame |
| 82 | * |
| 83 | * Defined in eMMC 4.5 (JESD84-B51) standard's Table 17. |
| 84 | */ |
| 85 | struct rpmb_data_frame { |
| 86 | uint8_t stuff_bytes[RPMB_STUFF_DATA_SIZE]; |
| 87 | uint8_t key_mac[RPMB_KEY_MAC_SIZE]; |
| 88 | uint8_t data[RPMB_DATA_SIZE]; |
| 89 | uint8_t nonce[RPMB_NONCE_SIZE]; |
| 90 | uint8_t write_counter[4]; |
| 91 | uint8_t address[2]; |
| 92 | uint8_t block_count[2]; |
| 93 | uint8_t op_result[2]; |
| 94 | uint8_t msg_type[2]; |
| 95 | } __packed; |
| 96 | |
| 97 | /** |
| 98 | * \brief RPMB backend interface |
| 99 | * |
| 100 | * The structure defines the function interface that the backend has to implement in order to |
| 101 | * provide hardware access to the RPMB device. |
| 102 | */ |
| 103 | struct rpmb_backend_interface { |
| 104 | psa_status_t (*get_dev_info)(void *context, uint32_t dev_id, |
| 105 | struct rpmb_dev_info *dev_info); |
| 106 | |
| 107 | psa_status_t (*data_request)(void *context, uint32_t dev_id, |
| 108 | const struct rpmb_data_frame *request_frames, |
| 109 | size_t request_frame_count, |
| 110 | struct rpmb_data_frame *response_frames, |
| 111 | size_t *response_frame_count); |
| 112 | }; |
| 113 | |
| 114 | /** |
| 115 | * \brief RPMB backend |
| 116 | * |
| 117 | * Generic object for storing the RPMB backend interface and the implementation specific context. |
| 118 | */ |
| 119 | struct rpmb_backend { |
| 120 | void *context; |
| 121 | const struct rpmb_backend_interface *interface; |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * \brief Query RPMB device info from the device |
| 126 | * |
| 127 | * \return Pointer to the base block_store or NULL on failure |
| 128 | * \param[in] instance RPMB backend instance |
| 129 | * \param[in] dev_id RPMB device ID |
| 130 | * \param[out] dev_info Device info |
| 131 | * \return psa_status_t |
| 132 | */ |
| 133 | psa_status_t rpmb_backend_get_dev_info(struct rpmb_backend *instance, uint32_t dev_id, |
| 134 | struct rpmb_dev_info *dev_info); |
| 135 | |
| 136 | /** |
| 137 | * \brief Write and read data frames into and from the RPMB device. |
| 138 | * |
| 139 | * \param[in] instance RPMB backend instance |
| 140 | * \param[in] dev_id RPMB device ID |
| 141 | * \param[in] request_frames Request data frames |
| 142 | * \param[in] request_frame_count Request data frame count |
| 143 | * \param[out] response_frames Response data frames |
| 144 | * \param[inout] response_frame_count in: maximal response data frame count, out: actual response |
| 145 | * data frame count |
| 146 | * \return psa_status_t |
| 147 | */ |
| 148 | psa_status_t rpmb_backend_data_request(struct rpmb_backend *instance, uint32_t dev_id, |
| 149 | const struct rpmb_data_frame *request_frames, |
| 150 | size_t request_frame_count, |
| 151 | struct rpmb_data_frame *response_frames, |
| 152 | size_t *response_frame_count); |
| 153 | |
| 154 | #ifdef __cplusplus |
| 155 | } |
| 156 | #endif |
| 157 | |
| 158 | #endif /* RPMB_BACKEND_H_ */ |