blob: f954dfb65e9fd773c9e8b2a5c177bffd68a0f42a [file] [log] [blame]
Sridhar Parasuramc97e0542015-06-26 16:14:58 -07001/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
Sridhar Parasuram2b7e8ca2014-10-27 14:41:12 -07002 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
Channagoud Kadabi723f6792015-01-29 13:26:06 -080029#ifndef _RPMB_H_
30#define _RPMB_H_
31
32#include <qseecom_lk_api.h>
33#include <mmc_sdhci.h>
34#include <ufs.h>
35#include <debug.h>
36
37#define RPMB_SECTOR_SIZE 256
38#define RPMB_LSTNR_ID 0x2000
39#define RPMB_BLK_SIZE 512
40#define RPMB_FRAME_SIZE 512
41#define RPMB_MIN_BLK_CNT 1
42#define RPMB_MIN_BLK_SZ 512
43
44enum app_commands
45{
46 CLIENT_CMD_READ_LK_DEVICE_STATE = 0,
47 CLIENT_CMD_LK_END_MILESTONE,
48 CLIENT_CMD_GET_VERSION,
49 CLIENT_CMD_WRITE_LK_DEVICE_STATE,
50};
51
52struct send_cmd_req
53{
54 uint32_t cmd_id;
55 uint32_t data;
56 uint32_t len;
57}__PACKED;
58
59struct send_cmd_rsp
60{
61 uint32_t cmd_id;
62 uint32_t data;
63 int32_t status;
64}__PACKED;
65
66enum device_type
67{
68 EMMC_RPMB = 3,
69 UFS_RPMB = 9,
70};
71
Sridhar Parasuram2b7e8ca2014-10-27 14:41:12 -070072/* RPMB request and response types */
73enum rpmb_rr_type {
74 KEY_PROVISION = 0x1,
75 READ_WRITE_COUNTER,
76 AUTH_WRITE,
77 AUTH_READ,
78 READ_RESULT_FLAG,
79};
80
81/* List of all return codes for rpmb frame verification from response */
82enum rpmb_verify_return_codes
83{
84 NONCE_MISMATCH = 0x100,
85};
86
87/* These are error codes returned for RPMB operations */
88enum rpmb_verify_error_codes
89{
90 OPERATION_OK = 0,
91 GENERAL_FAILURE,
92 AUTH_FAILURE,
93 COUNTER_FAILURE,
94 ADDRESS_FAILURE,
95 WRITE_FAILURE,
96 READ_FAILURE,
97 KEY_NOT_PROG,
98 MAXED_WR_COUNTER = 0x80,
99};
100
101/* RPMB Frame */
102struct rpmb_frame
103{
104 uint8_t stuff_bytes[196];
105 uint8_t keyMAC[32];
106 uint8_t data[256];
107 uint8_t nonce[16];
Channagoud Kadabi723f6792015-01-29 13:26:06 -0800108 uint8_t writecounter[4];
109 uint8_t address[2];
110 uint8_t blockcount[2];
Sridhar Parasuram2b7e8ca2014-10-27 14:41:12 -0700111 uint8_t result[2];
Channagoud Kadabi723f6792015-01-29 13:26:06 -0800112 uint8_t requestresponse[2];
Sridhar Parasuram2b7e8ca2014-10-27 14:41:12 -0700113};
Channagoud Kadabi723f6792015-01-29 13:26:06 -0800114
115struct rpmb_init_info
116{
117 uint32_t size;
118 uint32_t rel_wr_count;
119 uint32_t dev_type;
120};
121
122/* dump a given RPMB frame */
123static inline void dump_rpmb_frame(uint8_t *frame, const char *frame_type)
124{
125 int i;
126 char buf[512] = {0};
127 char *p = buf;
128
129 printf("Printing %s frame (in hex)\n", frame_type);
130 printf("________0__1__2__3__4__5__6__7__8__9__A__B__C__D__E__F__0__1__2__3__4__5__6__7__8__9__A__B__C__D__E__F\n");
131 for (i = 0; i < 512; i++) {
132 if (!(i % 32)) {
133 snprintf(p, 8, "| %2d | ", (i + 1) / 32);
134 p += 7;
135 }
136 snprintf(p, 4, "%.2x ", frame[i]);
137 p += 3;
138 if (((i + 1) % 32) == 0) {
139 printf("%s\n", buf);
140 p = buf;
141 }
142 }
143 printf("________F__E__D__C__B__A__9__8__7__6__5__4__3__2__1__0__F__E__D__C__B__A__9__8__7__6__5__4__3__2__1__0\n");
144}
145
146int read_device_info_rpmb(void *info, uint32_t sz);
147int write_device_info_rpmb(void *info, uint32_t sz);
148
149/* Function Prototypes */
Sridhar Parasuramc97e0542015-06-26 16:14:58 -0700150int rpmb_write_emmc(struct mmc_device *dev, uint32_t *req_buf, uint32_t blk_cnt, uint32_t rel_wr_count, uint32_t *resp_buf, uint32_t *resp_len);
Channagoud Kadabi723f6792015-01-29 13:26:06 -0800151int rpmb_read_emmc(struct mmc_device *dev, uint32_t *req_buf, uint32_t blk_cnt, uint32_t *resp_buf, uint32_t *resp_len);
Sridhar Parasuramc97e0542015-06-26 16:14:58 -0700152int rpmb_write_ufs(struct ufs_dev *dev, uint32_t *req_buf, uint32_t blk_cnt, uint32_t rel_wr_count, uint32_t *resp_buf, uint32_t *resp_len);
Channagoud Kadabi723f6792015-01-29 13:26:06 -0800153int rpmb_read_ufs(struct ufs_dev *dev, uint32_t *req_buf, uint32_t blk_cnt, uint32_t *resp_buf, uint32_t *resp_len);
154
155/* APIs exposed to applications */
156int rpmb_init();
157int rpmb_uninit();
Sridhar Parasuramc97e0542015-06-26 16:14:58 -0700158int rpmb_write(uint32_t *req_buf, uint32_t blk_cnt, uint32_t rel_wr_count, uint32_t *resp_buf, uint32_t *resp_len);
Channagoud Kadabi723f6792015-01-29 13:26:06 -0800159int rpmb_read(uint32_t *req_buf, uint32_t blk_cnt, uint32_t *resp_buf, uint32_t *resp_len);
160struct rpmb_init_info *rpmb_get_init_info();
161int rpmb_get_app_handle();
162bool is_sec_app_loaded();
163int load_sec_app();
164int unload_sec_app();
165#endif