blob: 81351f45dc0cce3fe8e0483e66d3cc87ac9e4a7f [file] [log] [blame]
Amit Blaydf42d2f2015-02-03 16:37:09 +02001/* Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
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 <target.h>
31#include <mmc.h>
32#include <partition_parser.h>
33#include <string.h>
34#include <stdlib.h>
35
36#include "mdtp.h"
37#include "scm.h"
38
Amit Blaydf42d2f2015-02-03 16:37:09 +020039#define MAX_METADATA_SIZE (0x1000)
40#define QFPROM_ADDR_SPACE_RAW (0)
41
42/********************************************************************************/
43
44typedef union
45{
46 struct {
47 uint8_t enable1 : 1;
48 uint8_t disable1 : 1;
49 uint8_t enable2 : 1;
50 uint8_t disable2 : 1;
51 uint8_t enable3 : 1;
52 uint8_t disable3 : 1;
53 uint8_t reserved1 : 1;
54 uint8_t reserved2 : 1;
55 } bitwise;
56 uint8_t mask;
57} mdtp_eFuses_t;
58
59typedef struct metadata {
60 mdtp_eFuses_t eFuses;
61} metadata_t;
62
63/********************************************************************************/
64
65/**
66 * Checks if we are in test mode according to relevant eFuses
67 *
68 * @return - negative value for an error, 0 for success.
69 */
70static int is_test_mode(void)
71{
72 static int test_mode_set = 0;
73 static int test_mode = 0;
74 int ret = 0;
75 uint32_t status_low = 0;
76 uint32_t status_high = 0;
77
78#define SECBOOT_FUSE 0x01
79#define SHK_FUSE 0x02
80#define DEBUG_FUSE 0x04
81
82 /* Make sure we only read the test mode once */
83 if (test_mode_set)
84 return test_mode;
85
86 ret = scm_svc_get_secure_state(&status_low, &status_high);
87
88 if(ret == 0)
89 {
90 /* (SECBOOT_FUSE | SHK_FUSE | DEBUG_FUSE) implies that none of the fuses are blown */
91 if((status_low & (SECBOOT_FUSE | SHK_FUSE | DEBUG_FUSE)) == (SECBOOT_FUSE | SHK_FUSE | DEBUG_FUSE))
92 test_mode = 1;
93 }
94 else
95 {
96 dprintf(CRITICAL, "mdtp: is_test_mode: qsee_get_secure_state returned error: %d, status.value[0]: %d", ret, status_low);
97 test_mode = 0;
98 }
99
100 test_mode_set = 1;
101 dprintf(INFO, "mdtp: is_test_mode: test mode is set to %d", test_mode);
102
103 return test_mode;
104}
105
106/**
107 * Read the Firmware Lock Metadata from EMMC
108 *
109 * @param metadata - Read a metadata block holding eFuse emulation from MDTP partition.
110 *
111 * @return - negative value for an error, 0 for success.
112 */
113static int read_metadata(metadata_t *metadata)
114{
115 unsigned long long ptn = 0;
116 uint32_t actual_size;
Amit Blaydf42d2f2015-02-03 16:37:09 +0200117 int index = INVALID_PTN;
118 uint32_t block_size = mmc_get_device_blocksize();
Amit Blay4418fb42015-05-05 08:45:13 +0300119 unsigned char *buf = (unsigned char *)target_get_scratch_address() + MDTP_SCRATCH_OFFSET;
Amit Blaydf42d2f2015-02-03 16:37:09 +0200120
121 index = partition_get_index("mdtp");
122 ptn = partition_get_offset(index);
123
124 if(ptn == 0)
125 {
126 return -1;
127 }
128
Shay Nachmani6e27e3c2015-03-12 13:56:13 +0200129 actual_size = ROUNDUP(sizeof(metadata_t), block_size);
130
131 if (actual_size > MAX_METADATA_SIZE)
Amit Blaydf42d2f2015-02-03 16:37:09 +0200132 {
Shay Nachmani6e27e3c2015-03-12 13:56:13 +0200133 dprintf(CRITICAL, "mdtp: read_metadata: ERROR, meta data size %d too big\n", actual_size);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200134 return -1;
135 }
136
Amit Blay4418fb42015-05-05 08:45:13 +0300137 if(mmc_read(ptn, (void *)buf, actual_size))
Amit Blaydf42d2f2015-02-03 16:37:09 +0200138 {
139 dprintf(CRITICAL, "mdtp: read_metadata: ERROR, cannot read mdtp info\n");
140 return -1;
141 }
142
Amit Blay4418fb42015-05-05 08:45:13 +0300143 memscpy((uint8_t*)metadata, sizeof(metadata_t), (uint8_t*)(buf), MAX_METADATA_SIZE);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200144
145 dprintf(INFO, "mdtp: read_metadata: SUCCESS, read %d bytes\n", actual_size);
146
147 return 0;
148}
149
150/**
151 * read_QFPROM_fuse
152 *
153 * @param mask[out] - MDTP efuse value represented by a bitfield.
154 *
155 * @return - negative value for an error, 0 for success.
156 */
157static int read_QFPROM_fuse(uint8_t *mask)
158{
159 static const uint32_t row_address = MDTP_EFUSE_ADDRESS;
160 uint32_t addr_type = QFPROM_ADDR_SPACE_RAW;
161 uint32_t row_data[2] = {0};
162 uint32_t qfprom_api_status = 0;
163
164 /* Read the current row where the eFuse is located */
165 (void) qfprom_read_row_cmd(row_address, addr_type, row_data, &qfprom_api_status);
166 if (qfprom_api_status)
167 {
168 dprintf(CRITICAL, "mdtp: write_QFPROM_fuse: qsee_fuse_read failed. qfprom_api_status=%d", qfprom_api_status);
169 return -1;
170 }
171
172 /* Shift the read data to be reflected in mask */
173 *mask = (uint8_t)(row_data[0] >> MDTP_EFUSE_START);
174
175 return 0;
176}
177
178/*-------------------------------------------------------------------------*/
179
180/**
181 * read_test_fuse
182 *
183 * @param mask[out] - MDTP efuse value represented by a bitfield.
184 *
185 * @return - negative value for an error, 0 for success.
186 */
187static int read_test_fuse(uint8_t *mask)
188{
189 int status = 0;
190 metadata_t metadata;
191
192 status = read_metadata(&metadata);
193 if (status) {
194 dprintf(CRITICAL, "mdtp: read_test_fuse: Failure getting metadata");
195 return -1;
196 }
197
198 *mask = metadata.eFuses.mask;
199
200 return 0;
201}
202
203/*-------------------------------------------------------------------------*/
204
205/**
206 * read_fuse
207 *
208 * @param mask[out] - MDTP efuse value represented by a bitfield.
209 *
210 * @return - negative value for an error, 0 for success.
211 */
212static int read_fuse(uint8_t *mask)
213{
214 if (is_test_mode())
215 return read_test_fuse(mask);
216 else
217 return read_QFPROM_fuse(mask);
218}
219
220/*-------------------------------------------------------------------------*/
221
222/**
223 * mdtp_fuse_get_enabled
224 *
225 * Read the Firmware Lock eFuses and return whether the Firmware
226 * Lock is currently enabled or disabled in HW.
227 *
Amit Blay8e2731c2015-04-28 21:54:55 +0300228 * @param[out] enabled: 0 - disabled, 1 - enabled.
Amit Blaydf42d2f2015-02-03 16:37:09 +0200229 *
230 * @return - negative value for an error, 0 for success.
231 */
232int mdtp_fuse_get_enabled(bool *enabled)
233{
234 int status;
235 mdtp_eFuses_t eFuses;
236
Amit Blay8e2731c2015-04-28 21:54:55 +0300237 *enabled = 1;
238
Amit Blaydf42d2f2015-02-03 16:37:09 +0200239 status = read_fuse(&eFuses.mask);
240 if (status)
241 {
242 dprintf(CRITICAL, "mdtp: mdtp_fuse_get_enabled: Failure in reading fuse");
243 return -1;
244 }
245
Amit Blay8e2731c2015-04-28 21:54:55 +0300246 if (!(eFuses.bitwise.enable1 && !eFuses.bitwise.disable1) &&
247 !(eFuses.bitwise.enable2 && !eFuses.bitwise.disable2) &&
248 !(eFuses.bitwise.enable3 && !eFuses.bitwise.disable3))
Amit Blaydf42d2f2015-02-03 16:37:09 +0200249 {
Amit Blaydf42d2f2015-02-03 16:37:09 +0200250 *enabled = 0;
Amit Blay8e2731c2015-04-28 21:54:55 +0300251 }
Amit Blaydf42d2f2015-02-03 16:37:09 +0200252
253 return 0;
254}
255