blob: a66f87bb891c521908cb6ab1b3149b0265e8704b [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"
37#include "scm.h"
38
Amit Blaydf42d2f2015-02-03 16:37:09 +020039#define MAX_METADATA_SIZE (0x1000)
Amit Blaydf42d2f2015-02-03 16:37:09 +020040
41/********************************************************************************/
42
43typedef union
44{
45 struct {
46 uint8_t enable1 : 1;
47 uint8_t disable1 : 1;
48 uint8_t enable2 : 1;
49 uint8_t disable2 : 1;
50 uint8_t enable3 : 1;
51 uint8_t disable3 : 1;
52 uint8_t reserved1 : 1;
53 uint8_t reserved2 : 1;
54 } bitwise;
55 uint8_t mask;
56} mdtp_eFuses_t;
57
58typedef struct metadata {
59 mdtp_eFuses_t eFuses;
60} metadata_t;
61
62/********************************************************************************/
63
64/**
65 * Checks if we are in test mode according to relevant eFuses
66 *
67 * @return - negative value for an error, 0 for success.
68 */
69static int is_test_mode(void)
70{
71 static int test_mode_set = 0;
72 static int test_mode = 0;
73 int ret = 0;
74 uint32_t status_low = 0;
75 uint32_t status_high = 0;
76
77#define SECBOOT_FUSE 0x01
78#define SHK_FUSE 0x02
79#define DEBUG_FUSE 0x04
80
81 /* Make sure we only read the test mode once */
82 if (test_mode_set)
83 return test_mode;
84
85 ret = scm_svc_get_secure_state(&status_low, &status_high);
86
87 if(ret == 0)
88 {
89 /* (SECBOOT_FUSE | SHK_FUSE | DEBUG_FUSE) implies that none of the fuses are blown */
90 if((status_low & (SECBOOT_FUSE | SHK_FUSE | DEBUG_FUSE)) == (SECBOOT_FUSE | SHK_FUSE | DEBUG_FUSE))
91 test_mode = 1;
92 }
93 else
94 {
95 dprintf(CRITICAL, "mdtp: is_test_mode: qsee_get_secure_state returned error: %d, status.value[0]: %d", ret, status_low);
96 test_mode = 0;
97 }
98
99 test_mode_set = 1;
100 dprintf(INFO, "mdtp: is_test_mode: test mode is set to %d", test_mode);
101
102 return test_mode;
103}
104
105/**
106 * Read the Firmware Lock Metadata from EMMC
107 *
108 * @param metadata - Read a metadata block holding eFuse emulation from MDTP partition.
109 *
110 * @return - negative value for an error, 0 for success.
111 */
112static int read_metadata(metadata_t *metadata)
113{
114 unsigned long long ptn = 0;
115 uint32_t actual_size;
Amit Blaydf42d2f2015-02-03 16:37:09 +0200116 int index = INVALID_PTN;
117 uint32_t block_size = mmc_get_device_blocksize();
Amit Blay4418fb42015-05-05 08:45:13 +0300118 unsigned char *buf = (unsigned char *)target_get_scratch_address() + MDTP_SCRATCH_OFFSET;
Amit Blaydf42d2f2015-02-03 16:37:09 +0200119
120 index = partition_get_index("mdtp");
121 ptn = partition_get_offset(index);
122
123 if(ptn == 0)
124 {
125 return -1;
126 }
127
Shay Nachmani6e27e3c2015-03-12 13:56:13 +0200128 actual_size = ROUNDUP(sizeof(metadata_t), block_size);
129
130 if (actual_size > MAX_METADATA_SIZE)
Amit Blaydf42d2f2015-02-03 16:37:09 +0200131 {
Shay Nachmani6e27e3c2015-03-12 13:56:13 +0200132 dprintf(CRITICAL, "mdtp: read_metadata: ERROR, meta data size %d too big\n", actual_size);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200133 return -1;
134 }
135
Amit Blay4418fb42015-05-05 08:45:13 +0300136 if(mmc_read(ptn, (void *)buf, actual_size))
Amit Blaydf42d2f2015-02-03 16:37:09 +0200137 {
138 dprintf(CRITICAL, "mdtp: read_metadata: ERROR, cannot read mdtp info\n");
139 return -1;
140 }
141
Amit Blay4418fb42015-05-05 08:45:13 +0300142 memscpy((uint8_t*)metadata, sizeof(metadata_t), (uint8_t*)(buf), MAX_METADATA_SIZE);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200143
144 dprintf(INFO, "mdtp: read_metadata: SUCCESS, read %d bytes\n", actual_size);
145
146 return 0;
147}
148
149/**
150 * read_QFPROM_fuse
151 *
152 * @param mask[out] - MDTP efuse value represented by a bitfield.
153 *
154 * @return - negative value for an error, 0 for success.
155 */
156static int read_QFPROM_fuse(uint8_t *mask)
157{
Amit Blay9fd4ddb2015-08-07 12:57:57 +0300158 uint32_t val = 0;
Amit Blaydf42d2f2015-02-03 16:37:09 +0200159
Amit Blay9fd4ddb2015-08-07 12:57:57 +0300160 val = readl(MDTP_EFUSE_ADDRESS);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200161
162 /* Shift the read data to be reflected in mask */
Amit Blay9fd4ddb2015-08-07 12:57:57 +0300163 *mask = (uint8_t)(val >> MDTP_EFUSE_START);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200164
165 return 0;
166}
167
168/*-------------------------------------------------------------------------*/
169
170/**
171 * read_test_fuse
172 *
173 * @param mask[out] - MDTP efuse value represented by a bitfield.
174 *
175 * @return - negative value for an error, 0 for success.
176 */
177static int read_test_fuse(uint8_t *mask)
178{
179 int status = 0;
180 metadata_t metadata;
181
182 status = read_metadata(&metadata);
183 if (status) {
184 dprintf(CRITICAL, "mdtp: read_test_fuse: Failure getting metadata");
185 return -1;
186 }
187
188 *mask = metadata.eFuses.mask;
189
190 return 0;
191}
192
193/*-------------------------------------------------------------------------*/
194
195/**
196 * read_fuse
197 *
198 * @param mask[out] - MDTP efuse value represented by a bitfield.
199 *
200 * @return - negative value for an error, 0 for success.
201 */
202static int read_fuse(uint8_t *mask)
203{
204 if (is_test_mode())
205 return read_test_fuse(mask);
206 else
207 return read_QFPROM_fuse(mask);
208}
209
210/*-------------------------------------------------------------------------*/
211
212/**
213 * mdtp_fuse_get_enabled
214 *
215 * Read the Firmware Lock eFuses and return whether the Firmware
216 * Lock is currently enabled or disabled in HW.
217 *
Amit Blay8e2731c2015-04-28 21:54:55 +0300218 * @param[out] enabled: 0 - disabled, 1 - enabled.
Amit Blaydf42d2f2015-02-03 16:37:09 +0200219 *
220 * @return - negative value for an error, 0 for success.
221 */
222int mdtp_fuse_get_enabled(bool *enabled)
223{
224 int status;
225 mdtp_eFuses_t eFuses;
226
Amit Blay8e2731c2015-04-28 21:54:55 +0300227 *enabled = 1;
228
Amit Blaydf42d2f2015-02-03 16:37:09 +0200229 status = read_fuse(&eFuses.mask);
230 if (status)
231 {
232 dprintf(CRITICAL, "mdtp: mdtp_fuse_get_enabled: Failure in reading fuse");
233 return -1;
234 }
235
Amit Blay8e2731c2015-04-28 21:54:55 +0300236 if (!(eFuses.bitwise.enable1 && !eFuses.bitwise.disable1) &&
237 !(eFuses.bitwise.enable2 && !eFuses.bitwise.disable2) &&
238 !(eFuses.bitwise.enable3 && !eFuses.bitwise.disable3))
Amit Blaydf42d2f2015-02-03 16:37:09 +0200239 {
Amit Blaydf42d2f2015-02-03 16:37:09 +0200240 *enabled = 0;
Amit Blay8e2731c2015-04-28 21:54:55 +0300241 }
Amit Blaydf42d2f2015-02-03 16:37:09 +0200242
243 return 0;
244}
245