Sridhar Parasuram | c97e054 | 2015-06-26 16:14:58 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. |
Sridhar Parasuram | 2b7e8ca | 2014-10-27 14:41:12 -0700 | [diff] [blame] | 2 | * |
| 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 Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 29 | #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> |
Parth Dixit | c6cef02 | 2015-10-15 13:47:07 +0530 | [diff] [blame] | 36 | #include <qseecomi_lk.h> |
Channagoud Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 37 | |
| 38 | #define RPMB_SECTOR_SIZE 256 |
| 39 | #define RPMB_LSTNR_ID 0x2000 |
| 40 | #define RPMB_BLK_SIZE 512 |
| 41 | #define RPMB_FRAME_SIZE 512 |
| 42 | #define RPMB_MIN_BLK_CNT 1 |
| 43 | #define RPMB_MIN_BLK_SZ 512 |
| 44 | |
Channagoud Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 45 | enum device_type |
| 46 | { |
| 47 | EMMC_RPMB = 3, |
| 48 | UFS_RPMB = 9, |
| 49 | }; |
| 50 | |
Sridhar Parasuram | 2b7e8ca | 2014-10-27 14:41:12 -0700 | [diff] [blame] | 51 | /* RPMB request and response types */ |
| 52 | enum rpmb_rr_type { |
| 53 | KEY_PROVISION = 0x1, |
| 54 | READ_WRITE_COUNTER, |
| 55 | AUTH_WRITE, |
| 56 | AUTH_READ, |
| 57 | READ_RESULT_FLAG, |
| 58 | }; |
| 59 | |
| 60 | /* List of all return codes for rpmb frame verification from response */ |
| 61 | enum rpmb_verify_return_codes |
| 62 | { |
| 63 | NONCE_MISMATCH = 0x100, |
| 64 | }; |
| 65 | |
| 66 | /* These are error codes returned for RPMB operations */ |
| 67 | enum rpmb_verify_error_codes |
| 68 | { |
| 69 | OPERATION_OK = 0, |
| 70 | GENERAL_FAILURE, |
| 71 | AUTH_FAILURE, |
| 72 | COUNTER_FAILURE, |
| 73 | ADDRESS_FAILURE, |
| 74 | WRITE_FAILURE, |
| 75 | READ_FAILURE, |
| 76 | KEY_NOT_PROG, |
| 77 | MAXED_WR_COUNTER = 0x80, |
| 78 | }; |
| 79 | |
| 80 | /* RPMB Frame */ |
| 81 | struct rpmb_frame |
| 82 | { |
| 83 | uint8_t stuff_bytes[196]; |
| 84 | uint8_t keyMAC[32]; |
| 85 | uint8_t data[256]; |
| 86 | uint8_t nonce[16]; |
Channagoud Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 87 | uint8_t writecounter[4]; |
| 88 | uint8_t address[2]; |
| 89 | uint8_t blockcount[2]; |
Sridhar Parasuram | 2b7e8ca | 2014-10-27 14:41:12 -0700 | [diff] [blame] | 90 | uint8_t result[2]; |
Channagoud Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 91 | uint8_t requestresponse[2]; |
Sridhar Parasuram | 2b7e8ca | 2014-10-27 14:41:12 -0700 | [diff] [blame] | 92 | }; |
Channagoud Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 93 | |
| 94 | struct rpmb_init_info |
| 95 | { |
| 96 | uint32_t size; |
| 97 | uint32_t rel_wr_count; |
| 98 | uint32_t dev_type; |
| 99 | }; |
| 100 | |
Sridhar Parasuram | ce1e572 | 2015-09-29 12:13:05 -0700 | [diff] [blame] | 101 | /* Secure Write Protect Info Entry structure */ |
| 102 | typedef struct |
| 103 | { |
| 104 | uint8_t wp_enable; /* UFS: WPF (Write Protect Flag), eMMC: SECURE_WP_MODE_ENABLE */ |
| 105 | uint8_t wp_type_mask; /* UFS: WPT (Write Protect Type), eMMC: SECURE_WP_MODE_CONFIG */ |
| 106 | uint64_t addr; /* UFS: LBA, eMMC: 0x1/0x2 (address of the device config register) */ |
| 107 | uint32_t num_blocks; /* UFS: Num LBA, eMMC: Set to 0 */ |
| 108 | } __attribute__ ((packed)) qsee_stor_secure_wp_info_entry_t; |
| 109 | |
| 110 | /* Secure Write Protect Info structure */ |
| 111 | typedef struct |
| 112 | { |
| 113 | uint8_t lun_number; /* UFS: LUN #, eMMC: Set to 0 */ |
| 114 | uint8_t num_entries; /* Number of Secure wp entries */ |
| 115 | qsee_stor_secure_wp_info_entry_t wp_entries[4]; /* Max 4 entries total */ |
| 116 | } __attribute__ ((packed)) qsee_stor_secure_wp_info_t; |
| 117 | |
Channagoud Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 118 | /* dump a given RPMB frame */ |
| 119 | static inline void dump_rpmb_frame(uint8_t *frame, const char *frame_type) |
| 120 | { |
| 121 | int i; |
| 122 | char buf[512] = {0}; |
| 123 | char *p = buf; |
| 124 | |
| 125 | printf("Printing %s frame (in hex)\n", frame_type); |
| 126 | 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"); |
| 127 | for (i = 0; i < 512; i++) { |
| 128 | if (!(i % 32)) { |
| 129 | snprintf(p, 8, "| %2d | ", (i + 1) / 32); |
| 130 | p += 7; |
| 131 | } |
| 132 | snprintf(p, 4, "%.2x ", frame[i]); |
| 133 | p += 3; |
| 134 | if (((i + 1) % 32) == 0) { |
| 135 | printf("%s\n", buf); |
| 136 | p = buf; |
| 137 | } |
| 138 | } |
| 139 | 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"); |
| 140 | } |
| 141 | |
| 142 | int read_device_info_rpmb(void *info, uint32_t sz); |
| 143 | int write_device_info_rpmb(void *info, uint32_t sz); |
| 144 | |
| 145 | /* Function Prototypes */ |
Sridhar Parasuram | c97e054 | 2015-06-26 16:14:58 -0700 | [diff] [blame] | 146 | int 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 Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 147 | int rpmb_read_emmc(struct mmc_device *dev, uint32_t *req_buf, uint32_t blk_cnt, uint32_t *resp_buf, uint32_t *resp_len); |
Sridhar Parasuram | c97e054 | 2015-06-26 16:14:58 -0700 | [diff] [blame] | 148 | int 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 Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 149 | int rpmb_read_ufs(struct ufs_dev *dev, uint32_t *req_buf, uint32_t blk_cnt, uint32_t *resp_buf, uint32_t *resp_len); |
| 150 | |
| 151 | /* APIs exposed to applications */ |
| 152 | int rpmb_init(); |
| 153 | int rpmb_uninit(); |
Sridhar Parasuram | c97e054 | 2015-06-26 16:14:58 -0700 | [diff] [blame] | 154 | int rpmb_write(uint32_t *req_buf, uint32_t blk_cnt, uint32_t rel_wr_count, uint32_t *resp_buf, uint32_t *resp_len); |
Channagoud Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 155 | int rpmb_read(uint32_t *req_buf, uint32_t blk_cnt, uint32_t *resp_buf, uint32_t *resp_len); |
| 156 | struct rpmb_init_info *rpmb_get_init_info(); |
Sridhar Parasuram | ce1e572 | 2015-09-29 12:13:05 -0700 | [diff] [blame] | 157 | /* RPMB API to set secure write protect configuration block */ |
| 158 | int swp_write(qsee_stor_secure_wp_info_t swp_cb); |
Channagoud Kadabi | 723f679 | 2015-01-29 13:26:06 -0800 | [diff] [blame] | 159 | #endif |