blob: 265a0d68fc2f185441a1b574cdfcf4c184e9be67 [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
Channagoud Kadabi723f6792015-01-29 13:26:06 -080044enum device_type
45{
46 EMMC_RPMB = 3,
47 UFS_RPMB = 9,
48};
49
Sridhar Parasuram2b7e8ca2014-10-27 14:41:12 -070050/* RPMB request and response types */
51enum rpmb_rr_type {
52 KEY_PROVISION = 0x1,
53 READ_WRITE_COUNTER,
54 AUTH_WRITE,
55 AUTH_READ,
56 READ_RESULT_FLAG,
57};
58
59/* List of all return codes for rpmb frame verification from response */
60enum rpmb_verify_return_codes
61{
62 NONCE_MISMATCH = 0x100,
63};
64
65/* These are error codes returned for RPMB operations */
66enum rpmb_verify_error_codes
67{
68 OPERATION_OK = 0,
69 GENERAL_FAILURE,
70 AUTH_FAILURE,
71 COUNTER_FAILURE,
72 ADDRESS_FAILURE,
73 WRITE_FAILURE,
74 READ_FAILURE,
75 KEY_NOT_PROG,
76 MAXED_WR_COUNTER = 0x80,
77};
78
79/* RPMB Frame */
80struct rpmb_frame
81{
82 uint8_t stuff_bytes[196];
83 uint8_t keyMAC[32];
84 uint8_t data[256];
85 uint8_t nonce[16];
Channagoud Kadabi723f6792015-01-29 13:26:06 -080086 uint8_t writecounter[4];
87 uint8_t address[2];
88 uint8_t blockcount[2];
Sridhar Parasuram2b7e8ca2014-10-27 14:41:12 -070089 uint8_t result[2];
Channagoud Kadabi723f6792015-01-29 13:26:06 -080090 uint8_t requestresponse[2];
Sridhar Parasuram2b7e8ca2014-10-27 14:41:12 -070091};
Channagoud Kadabi723f6792015-01-29 13:26:06 -080092
93struct rpmb_init_info
94{
95 uint32_t size;
96 uint32_t rel_wr_count;
97 uint32_t dev_type;
98};
99
Sridhar Parasuramce1e5722015-09-29 12:13:05 -0700100/* Secure Write Protect Info Entry structure */
101typedef struct
102{
103 uint8_t wp_enable; /* UFS: WPF (Write Protect Flag), eMMC: SECURE_WP_MODE_ENABLE */
104 uint8_t wp_type_mask; /* UFS: WPT (Write Protect Type), eMMC: SECURE_WP_MODE_CONFIG */
105 uint64_t addr; /* UFS: LBA, eMMC: 0x1/0x2 (address of the device config register) */
106 uint32_t num_blocks; /* UFS: Num LBA, eMMC: Set to 0 */
107} __attribute__ ((packed)) qsee_stor_secure_wp_info_entry_t;
108
109/* Secure Write Protect Info structure */
110typedef struct
111{
112 uint8_t lun_number; /* UFS: LUN #, eMMC: Set to 0 */
113 uint8_t num_entries; /* Number of Secure wp entries */
114 qsee_stor_secure_wp_info_entry_t wp_entries[4]; /* Max 4 entries total */
115} __attribute__ ((packed)) qsee_stor_secure_wp_info_t;
116
Channagoud Kadabi723f6792015-01-29 13:26:06 -0800117/* dump a given RPMB frame */
118static inline void dump_rpmb_frame(uint8_t *frame, const char *frame_type)
119{
120 int i;
121 char buf[512] = {0};
122 char *p = buf;
123
124 printf("Printing %s frame (in hex)\n", frame_type);
125 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");
126 for (i = 0; i < 512; i++) {
127 if (!(i % 32)) {
128 snprintf(p, 8, "| %2d | ", (i + 1) / 32);
129 p += 7;
130 }
131 snprintf(p, 4, "%.2x ", frame[i]);
132 p += 3;
133 if (((i + 1) % 32) == 0) {
134 printf("%s\n", buf);
135 p = buf;
136 }
137 }
138 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");
139}
140
141int read_device_info_rpmb(void *info, uint32_t sz);
142int write_device_info_rpmb(void *info, uint32_t sz);
143
144/* Function Prototypes */
Sridhar Parasuramc97e0542015-06-26 16:14:58 -0700145int 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 -0800146int 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 -0700147int 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 -0800148int rpmb_read_ufs(struct ufs_dev *dev, uint32_t *req_buf, uint32_t blk_cnt, uint32_t *resp_buf, uint32_t *resp_len);
149
150/* APIs exposed to applications */
151int rpmb_init();
152int rpmb_uninit();
Sridhar Parasuramc97e0542015-06-26 16:14:58 -0700153int 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 -0800154int rpmb_read(uint32_t *req_buf, uint32_t blk_cnt, uint32_t *resp_buf, uint32_t *resp_len);
155struct rpmb_init_info *rpmb_get_init_info();
Sridhar Parasuramce1e5722015-09-29 12:13:05 -0700156/* RPMB API to set secure write protect configuration block */
157int swp_write(qsee_stor_secure_wp_info_t swp_cb);
Channagoud Kadabi723f6792015-01-29 13:26:06 -0800158#endif