blob: d951933f89354975999c0055925ef37fa036a3b4 [file] [log] [blame]
Channagoud Kadabi88b6a482015-01-29 13:16:44 -08001/* Copyright (c) 2014-2015 The Linux Foundation. All rights reserved.
Sridhar Parasurame4266f42014-10-23 20:11:09 -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
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 Parasuramc02d1072014-11-06 12:55:42 -080038#include <rand.h>
Sridhar Parasurame4266f42014-10-23 20:11:09 -070039#include <string.h>
40#include <stdlib.h>
41#include <endian.h>
Sridhar Parasuramc02d1072014-11-06 12:55:42 -080042#include <target.h>
Sridhar Parasurame4266f42014-10-23 20:11:09 -070043#include "ufs_rpmb.h"
Sridhar Parasurame4266f42014-10-23 20:11:09 -070044
45void 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
56int 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
77void 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
89bool rpmb_test(struct ufs_dev *dev, uint16_t address, uint16_t rpmb_num_blocks)
90{
91 struct rpmb_frame data_frame, result_frame[rpmb_num_blocks];
Sridhar Parasurame4266f42014-10-23 20:11:09 -070092 int i = 0, ret;
93 uint32_t response_len = 0;
94 // check if address + sectors requested for read do not exceed total size of rpmb
95 if ((address + rpmb_num_blocks) > dev->rpmb_num_blocks)
96 {
97 dprintf(CRITICAL, "Invalid request to rpmb\n");
98 return false;
99 }
100 memset(&data_frame, 0, sizeof(data_frame));
101 memset(&result_frame, 0, sizeof(result_frame));
Channagoud Kadabi88b6a482015-01-29 13:16:44 -0800102 data_frame.address[0] = (address & 0xff00) >> 8;
103 data_frame.address[1] = address & 0x00ff;
104 data_frame.blockcount[0] = (rpmb_num_blocks & 0xff00) >> 8;
105 data_frame.blockcount[1] = rpmb_num_blocks & 0x00ff;
106 data_frame.requestresponse[1] = AUTH_READ;
Sridhar Parasurame4266f42014-10-23 20:11:09 -0700107 for(i = 0 ; i < 16; i++)
108 {
109 data_frame.nonce[i] = (rand() % 256) + 1;
110 }
111#ifdef DEBUG_UFS_RPMB
112 dprintf(INFO, "Dumping RPMB Request frame\n");
113 dprintf(INFO, "--------------------------\n");
114 dump((void *) &data_frame, sizeof(struct rpmb_frame));
115#endif
116 ret = ucs_do_scsi_rpmb_read(dev, (uint32_t *) &data_frame, rpmb_num_blocks,
117 (uint32_t *) &result_frame, &response_len);
118 if (ret)
119 {
120 dprintf(CRITICAL, "RPMB Read error\n");
Sridhar Parasuramc02d1072014-11-06 12:55:42 -0800121 return false;
Sridhar Parasurame4266f42014-10-23 20:11:09 -0700122 }
123 for (i = 0; i < rpmb_num_blocks; i++)
124 {
125 ret = verify_rpmb_frame(&data_frame, &result_frame[i], AUTH_READ);
126 if(ret)
127 {
128 dprintf(CRITICAL, "Error in verifying RPMB frame\n");
129 dump((void *) &result_frame[i], sizeof(struct rpmb_frame));
130 }
131 }
132#ifdef DEBUG_UFS_RPMB
133 dprintf(INFO, "Dumping RPMB Response frames\n");
134 dprintf(INFO, "----------------------------\n");
135 dump((void *) &result_frame, sizeof(struct rpmb_frame)*rpmb_num_blocks);
136#endif
137 dprintf(INFO, "Data dump for RPMB read request\n");
138 printf("-------------------------------\n");
139
140 for (i = 0; i < rpmb_num_blocks; i++)
141 dump_rpmb_data((void *) &result_frame[i]);
142
143 printf("-------------------------------\n");
144
145 return true;
146}