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