1 /* 2 * Copyright (C) 2018-2022 Intel Corporation. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer 9 * in this position and unchanged. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * Contact Information: weideng <wei.a.deng@intel.com> 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 #ifndef __RPMB_H__ 30 #define __RPMB_H__ 31 32 #include <linux/types.h> 33 34 35 #define RPMB_PHY_MODE 0 36 #define RPMB_SIM_MODE 1 37 #define RPMB_BLOCK_SIZE 256 38 #define RPMB_FRAME_SIZE 512 39 #define RPMB_PHY_PATH_NAME "/dev/rpmbmux" 40 #define RPMB_SIM_PATH_NAME "/data/rpmbfile" 41 #define SEQ_CMD_MAX 3 /*support up to 3 cmds*/ 42 43 #define RPMB_F_WRITE (1UL << 0) 44 #define RPMB_F_REL_WRITE (1UL << 1) 45 46 enum rpmb_request { 47 RPMB_REQ_PROGRAM_KEY = 0x0001, 48 RPMB_REQ_GET_COUNTER = 0x0002, 49 RPMB_REQ_DATA_WRITE = 0x0003, 50 RPMB_REQ_DATA_READ = 0x0004, 51 RPMB_REQ_RESULT_READ = 0x0005, 52 }; 53 54 enum rpmb_response { 55 RPMB_RESP_PROGRAM_KEY = 0x0100, 56 RPMB_RESP_GET_COUNTER = 0x0200, 57 RPMB_RESP_DATA_WRITE = 0x0300, 58 RPMB_RESP_DATA_READ = 0x0400, 59 }; 60 61 enum rpmb_result { 62 RPMB_RES_OK = 0x0000, 63 RPMB_RES_GENERAL_FAILURE = 0x0001, 64 RPMB_RES_AUTH_FAILURE = 0x0002, 65 RPMB_RES_COUNT_FAILURE = 0x0003, 66 RPMB_RES_ADDR_FAILURE = 0x0004, 67 RPMB_RES_WRITE_FAILURE = 0x0005, 68 RPMB_RES_READ_FAILURE = 0x0006, 69 RPMB_RES_NO_AUTH_KEY = 0x0007, 70 71 RPMB_RES_WRITE_COUNTER_EXPIRED = 0x0080, 72 }; 73 74 /** 75 * struct rpmb_cmd: rpmb access command 76 * 77 * @flags: command flags 78 * 0 - read command 79 * 1 - write command RPMB_F_WRITE 80 * 2 - reliable write RPMB_F_REL_WRITE 81 * @nframes: number of rpmb frames in the command 82 * @frames: list of rpmb frames 83 */ 84 struct rpmb_cmd { 85 __u32 flags; 86 __u32 nframes; 87 struct rpmb_frame *frames; 88 }; 89 90 /** 91 * struct rpmb_data - rpmb data be transmitted in RPMB request 92 * 93 * @req_type: request type (program key, read, write, write counter) 94 * @icmd: list of input frames 95 * @ocmd: list of result frames 96 */ 97 struct rpmb_data { 98 __u16 req_type; 99 struct rpmb_cmd icmd; 100 struct rpmb_cmd ocmd; 101 }; 102 103 /** 104 * struct rpmb_frame - rpmb frame as defined by specs 105 * 106 * @stuff : stuff bytes 107 * @key_mac : The authentication key or the message authentication 108 * code (MAC) depending on the request/response type. 109 * The MAC will be delivered in the last (or the only) 110 * block of data. 111 * @data : Data to be written or read by signed access. 112 * @nonce : Random number generated by the host for the requests 113 * and copied to the response by the RPMB engine. 114 * @write_counter: Counter value for the total amount of the successful 115 * authenticated data write requests made by the host. 116 * @addr : Address of the data to be programmed to or read 117 * from the RPMB. Address is the serial number of 118 * the accessed block (half sector 256B). 119 * @block_count : Number of blocks (half sectors, 256B) requested to be 120 * read/programmed. 121 * @result : Includes information about the status of the write counter 122 * (valid, expired) and result of the access made to the RPMB. 123 * @req_resp : Defines the type of request and response to/from the memory. 124 */ 125 struct rpmb_frame { 126 __u8 stuff[196]; 127 __u8 key_mac[32]; 128 __u8 data[256]; 129 __u8 nonce[16]; 130 __be32 write_counter; 131 __be16 addr; 132 __be16 block_count; 133 __be16 result; 134 __be16 req_resp; 135 } __attribute__((packed)); 136 137 /** 138 * struct rpmb_cmd - rpmb access command 139 * 140 * @flags: command flags 141 * 0 - read command 142 * 1 - write commnad RPMB_F_WRITE 143 * 2 - reliable write RPMB_F_REL_WRITE 144 * @nframes: number of rpmb frames in the command 145 * @frames_ptr: a pointer to the list of rpmb frames 146 */ 147 struct rpmb_ioc_cmd { 148 __u32 flags; 149 __u32 nframes; 150 __aligned_u64 frames_ptr; 151 }; 152 153 /** 154 * struct rpmb_ioc_req_cmd - rpmb operation request command 155 * 156 * @req_type: request type: must match the in frame req_resp 157 * program key 158 * get write counter 159 * write data 160 * read data 161 * @icmd: input command 162 * @ocmd: output/result command 163 */ 164 struct rpmb_ioc_req_cmd { 165 __u64 req_type; 166 struct rpmb_ioc_cmd icmd; 167 struct rpmb_ioc_cmd ocmd; 168 }; 169 170 /** 171 * struct rpmb_ioc_seq_cmd - rpmb command sequence 172 * 173 * @num_of_cmds: number of commands 174 * @cmds: list of rpmb commands 175 */ 176 struct rpmb_ioc_seq_cmd { 177 __u64 num_of_cmds; 178 struct rpmb_ioc_cmd cmds[0]; 179 }; 180 181 struct rpmb_ioc_seq_data { 182 struct rpmb_ioc_seq_cmd h; 183 struct rpmb_ioc_cmd cmd[SEQ_CMD_MAX + 1]; 184 }; 185 186 int 187 rpmb_get_counter(__u8 mode, __u8 *key, __u32 *counter, __u16 *result); 188 189 #define RPMB_IOC_REQ_CMD _IOWR(0xB5, 80, struct rpmb_ioc_req_cmd) 190 #define RPMB_IOC_SEQ_CMD _IOWR(0xB5, 82, struct rpmb_ioc_seq_cmd) 191 192 __u16 rpmb_get_blocks(void); 193 194 #endif /* __RPMB_H__ */ 195