blob: 44d2eb5e9b49506ec21bf79f16cc9792b84fcdc7 [file] [log] [blame]
Reut Zysmanff6bab92016-02-09 14:06:31 +02001/* Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
Amit Blaydf42d2f2015-02-03 16:37:09 +02002 *
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>
Amit Blaydf42d2f2015-02-03 16:37:09 +020031#include <partition_parser.h>
32#include <string.h>
33#include <stdlib.h>
Amit Blay9fd4ddb2015-08-07 12:57:57 +030034#include <reg.h>
Amit Blaydf42d2f2015-02-03 16:37:09 +020035#include "mdtp.h"
Reut Zysman561d81a2015-08-26 17:37:28 +030036#include "mdtp_defs.h"
Amit Blaydf42d2f2015-02-03 16:37:09 +020037#include "scm.h"
Reut Zysmanff6bab92016-02-09 14:06:31 +020038#include "mdtp_fs.h"
Amit Blaydf42d2f2015-02-03 16:37:09 +020039
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{
Reut Zysman3f3eccd2016-04-20 22:05:36 +030046 struct {
Amit Blaydf42d2f2015-02-03 16:37:09 +020047 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
Reut Zysman3f3eccd2016-04-20 22:05:36 +030082 /* Make sure we only read the test mode once */
Amit Blaydf42d2f2015-02-03 16:37:09 +020083 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;
Reut Zysman3f3eccd2016-04-20 22:05:36 +030098 }
Amit Blaydf42d2f2015-02-03 16:37:09 +020099
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
Amit Blaydf42d2f2015-02-03 16:37:09 +0200108 * @param metadata - Read a metadata block holding eFuse emulation from MDTP partition.
Amit Blaydf42d2f2015-02-03 16:37:09 +0200109 * @return - negative value for an error, 0 for success.
110 */
111static int read_metadata(metadata_t *metadata)
112{
Reut Zysmanff6bab92016-02-09 14:06:31 +0200113 int eFuse = mdtp_fs_get_param(VIRTUAL_FUSE);
Reut Zysman3f3eccd2016-04-20 22:05:36 +0300114 if(eFuse == MDTP_PARAM_UNSET_VALUE){ //Error initializing eFuse
Reut Zysmanff6bab92016-02-09 14:06:31 +0200115 dprintf(CRITICAL, "mdtp: eFuse reading error\n");
Amit Blaydf42d2f2015-02-03 16:37:09 +0200116 return -1;
117 }
Reut Zysmanff6bab92016-02-09 14:06:31 +0200118 metadata->eFuses.mask = (uint8_t)eFuse;
119 dprintf(INFO, "mdtp: read_metadata: SUCCESS \n");
Amit Blaydf42d2f2015-02-03 16:37:09 +0200120 return 0;
121}
122
123/**
124 * read_QFPROM_fuse
125 *
126 * @param mask[out] - MDTP efuse value represented by a bitfield.
127 *
128 * @return - negative value for an error, 0 for success.
129 */
130static int read_QFPROM_fuse(uint8_t *mask)
131{
Reut Zysman561d81a2015-08-26 17:37:28 +0300132 struct mdtp_target_efuse target_efuse;
Amit Blay9fd4ddb2015-08-07 12:57:57 +0300133 uint32_t val = 0;
Amit Blaydf42d2f2015-02-03 16:37:09 +0200134
Reut Zysman561d81a2015-08-26 17:37:28 +0300135 if (mdtp_get_target_efuse(&target_efuse))
136 {
137 dprintf(CRITICAL, "mdtp: read_QFPROM_fuse: failed to get target eFuse\n");
138 return -1;
139 }
140
141 val = readl(target_efuse.address);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200142
143 /* Shift the read data to be reflected in mask */
Reut Zysman561d81a2015-08-26 17:37:28 +0300144 *mask = (uint8_t)(val >> target_efuse.start);
Amit Blaydf42d2f2015-02-03 16:37:09 +0200145
146 return 0;
147}
148
149/*-------------------------------------------------------------------------*/
150
151/**
152 * read_test_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_test_fuse(uint8_t *mask)
159{
160 int status = 0;
161 metadata_t metadata;
162
163 status = read_metadata(&metadata);
164 if (status) {
Reut Zysman561d81a2015-08-26 17:37:28 +0300165 dprintf(CRITICAL, "mdtp: read_test_fuse: Failure getting metadata\n");
Amit Blaydf42d2f2015-02-03 16:37:09 +0200166 return -1;
167 }
168
169 *mask = metadata.eFuses.mask;
170
171 return 0;
172}
173
174/*-------------------------------------------------------------------------*/
175
176/**
177 * read_fuse
178 *
179 * @param mask[out] - MDTP efuse value represented by a bitfield.
180 *
181 * @return - negative value for an error, 0 for success.
182 */
183static int read_fuse(uint8_t *mask)
184{
185 if (is_test_mode())
186 return read_test_fuse(mask);
187 else
188 return read_QFPROM_fuse(mask);
189}
190
191/*-------------------------------------------------------------------------*/
192
193/**
194 * mdtp_fuse_get_enabled
195 *
196 * Read the Firmware Lock eFuses and return whether the Firmware
197 * Lock is currently enabled or disabled in HW.
198 *
Amit Blay8e2731c2015-04-28 21:54:55 +0300199 * @param[out] enabled: 0 - disabled, 1 - enabled.
Amit Blaydf42d2f2015-02-03 16:37:09 +0200200 *
201 * @return - negative value for an error, 0 for success.
202 */
203int mdtp_fuse_get_enabled(bool *enabled)
204{
205 int status;
206 mdtp_eFuses_t eFuses;
207
Amit Blay8e2731c2015-04-28 21:54:55 +0300208 *enabled = 1;
209
Amit Blaydf42d2f2015-02-03 16:37:09 +0200210 status = read_fuse(&eFuses.mask);
211 if (status)
212 {
Reut Zysman561d81a2015-08-26 17:37:28 +0300213 dprintf(CRITICAL, "mdtp: mdtp_fuse_get_enabled: Failure in reading fuse\n");
Amit Blaydf42d2f2015-02-03 16:37:09 +0200214 return -1;
215 }
216
Amit Blay8e2731c2015-04-28 21:54:55 +0300217 if (!(eFuses.bitwise.enable1 && !eFuses.bitwise.disable1) &&
Reut Zysman3f3eccd2016-04-20 22:05:36 +0300218 !(eFuses.bitwise.enable2 && !eFuses.bitwise.disable2) &&
219 !(eFuses.bitwise.enable3 && !eFuses.bitwise.disable3))
Amit Blaydf42d2f2015-02-03 16:37:09 +0200220 {
Amit Blaydf42d2f2015-02-03 16:37:09 +0200221 *enabled = 0;
Amit Blay8e2731c2015-04-28 21:54:55 +0300222 }
Amit Blaydf42d2f2015-02-03 16:37:09 +0200223
224 return 0;
225}
226