blob: 7f676614798d68a4ab611f7adcf6fb72469c8bea [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>
Amit Blay9fd4ddb2015-08-07 12:57:57 +030035#include <reg.h>
Amit Blaydf42d2f2015-02-03 16:37:09 +020036#include "mdtp.h"
Reut Zysman561d81a2015-08-26 17:37:28 +030037#include "mdtp_defs.h"
Amit Blaydf42d2f2015-02-03 16:37:09 +020038#include "scm.h"
39
Amit Blaydf42d2f2015-02-03 16:37:09 +020040#define MAX_METADATA_SIZE (0x1000)
Amit Blaydf42d2f2015-02-03 16:37:09 +020041
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 {
Reut Zysman561d81a2015-08-26 17:37:28 +030096 dprintf(CRITICAL, "mdtp: is_test_mode: qsee_get_secure_state returned error: %d, status.value[0]: %d\n", ret, status_low);
Amit Blaydf42d2f2015-02-03 16:37:09 +020097 test_mode = 0;
98 }
99
100 test_mode_set = 1;
Reut Zysman561d81a2015-08-26 17:37:28 +0300101 dprintf(INFO, "mdtp: is_test_mode: test mode is set to %d\n", test_mode);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200102
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{
Reut Zysman561d81a2015-08-26 17:37:28 +0300159 struct mdtp_target_efuse target_efuse;
Amit Blay9fd4ddb2015-08-07 12:57:57 +0300160 uint32_t val = 0;
Amit Blaydf42d2f2015-02-03 16:37:09 +0200161
Reut Zysman561d81a2015-08-26 17:37:28 +0300162 if (mdtp_get_target_efuse(&target_efuse))
163 {
164 dprintf(CRITICAL, "mdtp: read_QFPROM_fuse: failed to get target eFuse\n");
165 return -1;
166 }
167
168 val = readl(target_efuse.address);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200169
170 /* Shift the read data to be reflected in mask */
Reut Zysman561d81a2015-08-26 17:37:28 +0300171 *mask = (uint8_t)(val >> target_efuse.start);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200172
173 return 0;
174}
175
176/*-------------------------------------------------------------------------*/
177
178/**
179 * read_test_fuse
180 *
181 * @param mask[out] - MDTP efuse value represented by a bitfield.
182 *
183 * @return - negative value for an error, 0 for success.
184 */
185static int read_test_fuse(uint8_t *mask)
186{
187 int status = 0;
188 metadata_t metadata;
189
190 status = read_metadata(&metadata);
191 if (status) {
Reut Zysman561d81a2015-08-26 17:37:28 +0300192 dprintf(CRITICAL, "mdtp: read_test_fuse: Failure getting metadata\n");
Amit Blaydf42d2f2015-02-03 16:37:09 +0200193 return -1;
194 }
195
196 *mask = metadata.eFuses.mask;
197
198 return 0;
199}
200
201/*-------------------------------------------------------------------------*/
202
203/**
204 * read_fuse
205 *
206 * @param mask[out] - MDTP efuse value represented by a bitfield.
207 *
208 * @return - negative value for an error, 0 for success.
209 */
210static int read_fuse(uint8_t *mask)
211{
212 if (is_test_mode())
213 return read_test_fuse(mask);
214 else
215 return read_QFPROM_fuse(mask);
216}
217
218/*-------------------------------------------------------------------------*/
219
220/**
221 * mdtp_fuse_get_enabled
222 *
223 * Read the Firmware Lock eFuses and return whether the Firmware
224 * Lock is currently enabled or disabled in HW.
225 *
Amit Blay8e2731c2015-04-28 21:54:55 +0300226 * @param[out] enabled: 0 - disabled, 1 - enabled.
Amit Blaydf42d2f2015-02-03 16:37:09 +0200227 *
228 * @return - negative value for an error, 0 for success.
229 */
230int mdtp_fuse_get_enabled(bool *enabled)
231{
232 int status;
233 mdtp_eFuses_t eFuses;
234
Amit Blay8e2731c2015-04-28 21:54:55 +0300235 *enabled = 1;
236
Amit Blaydf42d2f2015-02-03 16:37:09 +0200237 status = read_fuse(&eFuses.mask);
238 if (status)
239 {
Reut Zysman561d81a2015-08-26 17:37:28 +0300240 dprintf(CRITICAL, "mdtp: mdtp_fuse_get_enabled: Failure in reading fuse\n");
Amit Blaydf42d2f2015-02-03 16:37:09 +0200241 return -1;
242 }
243
Amit Blay8e2731c2015-04-28 21:54:55 +0300244 if (!(eFuses.bitwise.enable1 && !eFuses.bitwise.disable1) &&
245 !(eFuses.bitwise.enable2 && !eFuses.bitwise.disable2) &&
246 !(eFuses.bitwise.enable3 && !eFuses.bitwise.disable3))
Amit Blaydf42d2f2015-02-03 16:37:09 +0200247 {
Amit Blaydf42d2f2015-02-03 16:37:09 +0200248 *enabled = 0;
Amit Blay8e2731c2015-04-28 21:54:55 +0300249 }
Amit Blaydf42d2f2015-02-03 16:37:09 +0200250
251 return 0;
252}
253