Channagoud Kadabi | 88b6a48 | 2015-01-29 13:16:44 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2014-2015 The Linux Foundation. All rights reserved. |
Sridhar Parasuram | e4266f4 | 2014-10-23 20:11:09 -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 | |
| 29 | #include <debug.h> |
| 30 | #include <reg.h> |
| 31 | #include <ufs_hw.h> |
| 32 | #include <utp.h> |
| 33 | #include <upiu.h> |
| 34 | #include <uic.h> |
| 35 | #include <ucs.h> |
| 36 | #include <dme.h> |
| 37 | #include <rpmb.h> |
Sridhar Parasuram | c02d107 | 2014-11-06 12:55:42 -0800 | [diff] [blame] | 38 | #include <rand.h> |
Sridhar Parasuram | e4266f4 | 2014-10-23 20:11:09 -0700 | [diff] [blame] | 39 | #include <string.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <endian.h> |
Sridhar Parasuram | c02d107 | 2014-11-06 12:55:42 -0800 | [diff] [blame] | 42 | #include <target.h> |
Sridhar Parasuram | e4266f4 | 2014-10-23 20:11:09 -0700 | [diff] [blame] | 43 | #include "ufs_rpmb.h" |
Sridhar Parasuram | e4266f4 | 2014-10-23 20:11:09 -0700 | [diff] [blame] | 44 | |
| 45 | void rpmb_run_test() |
| 46 | { |
| 47 | bool result; |
| 48 | uint16_t sector_address = 1, rpmb_num_blocks = 4; |
| 49 | void *dev; |
| 50 | dev = target_mmc_device(); |
| 51 | result = rpmb_test((struct ufs_dev *)dev, sector_address, rpmb_num_blocks); |
| 52 | if (!result) |
| 53 | dprintf(INFO, "RPMB test failed"); |
| 54 | } |
| 55 | |
| 56 | int verify_rpmb_frame(struct rpmb_frame *request_frame, struct rpmb_frame *result_frame, int type) |
| 57 | { |
| 58 | int i = 0; |
| 59 | if(result_frame->result[0] == MAXED_WR_COUNTER) |
| 60 | { |
| 61 | dprintf(INFO, "Max write counter value reached\n"); |
| 62 | return MAXED_WR_COUNTER; |
| 63 | } |
| 64 | if(result_frame->result[1] != OPERATION_OK) |
| 65 | { |
| 66 | dprintf(INFO, "RPMB operation error: 0x%x\n", result_frame->result[1]); |
| 67 | return result_frame->result[1]; |
| 68 | } |
| 69 | for(i = 0; i < 16; i++) |
| 70 | { |
| 71 | if(request_frame->nonce[i] != result_frame->nonce[i]) |
| 72 | return NONCE_MISMATCH; |
| 73 | } |
| 74 | return OPERATION_OK; |
| 75 | } |
| 76 | |
| 77 | void dump_rpmb_data(struct rpmb_frame *result_frame) |
| 78 | { |
| 79 | int data_counter; |
| 80 | for (data_counter = 0; data_counter < 256; data_counter++) |
| 81 | { |
| 82 | printf("0x%x ", result_frame->data[data_counter]); |
| 83 | if ((data_counter + 1) % 16 == 0) |
| 84 | printf("\n"); |
| 85 | } |
| 86 | printf("\n"); |
| 87 | } |
| 88 | |
Sridhar Parasuram | 6d3a632 | 2015-09-30 22:00:14 -0700 | [diff] [blame] | 89 | bool swp_test() |
| 90 | { |
| 91 | int ret = 0; |
| 92 | qsee_stor_secure_wp_info_t cb = {0}; |
| 93 | // Write protect 4 partitions in Lun 0. |
| 94 | cb.lun_number = 0; |
| 95 | cb.num_entries = 4; |
| 96 | cb.wp_entries[0].wp_enable = 0x1; |
| 97 | cb.wp_entries[0].wp_type_mask = 0x1; |
| 98 | cb.wp_entries[0].addr = 8; |
| 99 | cb.wp_entries[0].num_blocks = 8192; |
| 100 | |
| 101 | cb.wp_entries[1].wp_enable = 0x1; |
| 102 | cb.wp_entries[1].wp_type_mask = 0x1; |
| 103 | cb.wp_entries[1].addr = 74120; |
| 104 | cb.wp_entries[1].num_blocks = 32; |
| 105 | |
| 106 | cb.wp_entries[2].wp_enable = 0x1; |
| 107 | cb.wp_entries[2].wp_type_mask = 0x1; |
| 108 | cb.wp_entries[2].addr = 6; |
| 109 | cb.wp_entries[2].num_blocks = 2; |
| 110 | |
| 111 | cb.wp_entries[3].wp_enable = 0x1; |
| 112 | cb.wp_entries[3].wp_type_mask = 0x1; |
| 113 | cb.wp_entries[3].addr = 73736; |
| 114 | cb.wp_entries[3].num_blocks = 256; |
| 115 | |
| 116 | ret = swp_write(cb); |
| 117 | if (ret) |
| 118 | { |
| 119 | dprintf(CRITICAL, "SWP Write Test Failed\n"); |
| 120 | return false; |
| 121 | } |
| 122 | else |
| 123 | dprintf(CRITICAL, "SWP Write Test Passed\n"); |
| 124 | return true; |
| 125 | } |
| 126 | |
Sridhar Parasuram | e4266f4 | 2014-10-23 20:11:09 -0700 | [diff] [blame] | 127 | bool rpmb_test(struct ufs_dev *dev, uint16_t address, uint16_t rpmb_num_blocks) |
| 128 | { |
| 129 | struct rpmb_frame data_frame, result_frame[rpmb_num_blocks]; |
Sridhar Parasuram | e4266f4 | 2014-10-23 20:11:09 -0700 | [diff] [blame] | 130 | int i = 0, ret; |
| 131 | uint32_t response_len = 0; |
| 132 | // check if address + sectors requested for read do not exceed total size of rpmb |
| 133 | if ((address + rpmb_num_blocks) > dev->rpmb_num_blocks) |
| 134 | { |
| 135 | dprintf(CRITICAL, "Invalid request to rpmb\n"); |
| 136 | return false; |
| 137 | } |
| 138 | memset(&data_frame, 0, sizeof(data_frame)); |
| 139 | memset(&result_frame, 0, sizeof(result_frame)); |
Channagoud Kadabi | 88b6a48 | 2015-01-29 13:16:44 -0800 | [diff] [blame] | 140 | data_frame.address[0] = (address & 0xff00) >> 8; |
| 141 | data_frame.address[1] = address & 0x00ff; |
| 142 | data_frame.blockcount[0] = (rpmb_num_blocks & 0xff00) >> 8; |
| 143 | data_frame.blockcount[1] = rpmb_num_blocks & 0x00ff; |
| 144 | data_frame.requestresponse[1] = AUTH_READ; |
Sridhar Parasuram | e4266f4 | 2014-10-23 20:11:09 -0700 | [diff] [blame] | 145 | for(i = 0 ; i < 16; i++) |
| 146 | { |
| 147 | data_frame.nonce[i] = (rand() % 256) + 1; |
| 148 | } |
| 149 | #ifdef DEBUG_UFS_RPMB |
| 150 | dprintf(INFO, "Dumping RPMB Request frame\n"); |
| 151 | dprintf(INFO, "--------------------------\n"); |
| 152 | dump((void *) &data_frame, sizeof(struct rpmb_frame)); |
| 153 | #endif |
| 154 | ret = ucs_do_scsi_rpmb_read(dev, (uint32_t *) &data_frame, rpmb_num_blocks, |
| 155 | (uint32_t *) &result_frame, &response_len); |
| 156 | if (ret) |
| 157 | { |
| 158 | dprintf(CRITICAL, "RPMB Read error\n"); |
Sridhar Parasuram | c02d107 | 2014-11-06 12:55:42 -0800 | [diff] [blame] | 159 | return false; |
Sridhar Parasuram | e4266f4 | 2014-10-23 20:11:09 -0700 | [diff] [blame] | 160 | } |
| 161 | for (i = 0; i < rpmb_num_blocks; i++) |
| 162 | { |
| 163 | ret = verify_rpmb_frame(&data_frame, &result_frame[i], AUTH_READ); |
| 164 | if(ret) |
| 165 | { |
| 166 | dprintf(CRITICAL, "Error in verifying RPMB frame\n"); |
| 167 | dump((void *) &result_frame[i], sizeof(struct rpmb_frame)); |
| 168 | } |
| 169 | } |
| 170 | #ifdef DEBUG_UFS_RPMB |
| 171 | dprintf(INFO, "Dumping RPMB Response frames\n"); |
| 172 | dprintf(INFO, "----------------------------\n"); |
| 173 | dump((void *) &result_frame, sizeof(struct rpmb_frame)*rpmb_num_blocks); |
| 174 | #endif |
| 175 | dprintf(INFO, "Data dump for RPMB read request\n"); |
| 176 | printf("-------------------------------\n"); |
| 177 | |
| 178 | for (i = 0; i < rpmb_num_blocks; i++) |
| 179 | dump_rpmb_data((void *) &result_frame[i]); |
| 180 | |
| 181 | printf("-------------------------------\n"); |
| 182 | |
| 183 | return true; |
| 184 | } |