Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 1 | /* 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 Blay | 9fd4ddb | 2015-08-07 12:57:57 +0300 | [diff] [blame^] | 35 | #include <reg.h> |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 36 | #include "mdtp.h" |
| 37 | #include "scm.h" |
| 38 | |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 39 | #define MAX_METADATA_SIZE (0x1000) |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 40 | |
| 41 | /********************************************************************************/ |
| 42 | |
| 43 | typedef 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 | |
| 58 | typedef 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 | */ |
| 69 | static 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 | */ |
| 112 | static int read_metadata(metadata_t *metadata) |
| 113 | { |
| 114 | unsigned long long ptn = 0; |
| 115 | uint32_t actual_size; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 116 | int index = INVALID_PTN; |
| 117 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 118 | unsigned char *buf = (unsigned char *)target_get_scratch_address() + MDTP_SCRATCH_OFFSET; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 119 | |
| 120 | index = partition_get_index("mdtp"); |
| 121 | ptn = partition_get_offset(index); |
| 122 | |
| 123 | if(ptn == 0) |
| 124 | { |
| 125 | return -1; |
| 126 | } |
| 127 | |
Shay Nachmani | 6e27e3c | 2015-03-12 13:56:13 +0200 | [diff] [blame] | 128 | actual_size = ROUNDUP(sizeof(metadata_t), block_size); |
| 129 | |
| 130 | if (actual_size > MAX_METADATA_SIZE) |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 131 | { |
Shay Nachmani | 6e27e3c | 2015-03-12 13:56:13 +0200 | [diff] [blame] | 132 | dprintf(CRITICAL, "mdtp: read_metadata: ERROR, meta data size %d too big\n", actual_size); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 133 | return -1; |
| 134 | } |
| 135 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 136 | if(mmc_read(ptn, (void *)buf, actual_size)) |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 137 | { |
| 138 | dprintf(CRITICAL, "mdtp: read_metadata: ERROR, cannot read mdtp info\n"); |
| 139 | return -1; |
| 140 | } |
| 141 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 142 | memscpy((uint8_t*)metadata, sizeof(metadata_t), (uint8_t*)(buf), MAX_METADATA_SIZE); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 143 | |
| 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 | */ |
| 156 | static int read_QFPROM_fuse(uint8_t *mask) |
| 157 | { |
Amit Blay | 9fd4ddb | 2015-08-07 12:57:57 +0300 | [diff] [blame^] | 158 | uint32_t val = 0; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 159 | |
Amit Blay | 9fd4ddb | 2015-08-07 12:57:57 +0300 | [diff] [blame^] | 160 | val = readl(MDTP_EFUSE_ADDRESS); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 161 | |
| 162 | /* Shift the read data to be reflected in mask */ |
Amit Blay | 9fd4ddb | 2015-08-07 12:57:57 +0300 | [diff] [blame^] | 163 | *mask = (uint8_t)(val >> MDTP_EFUSE_START); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 164 | |
| 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 | */ |
| 177 | static 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 | */ |
| 202 | static 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 Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 218 | * @param[out] enabled: 0 - disabled, 1 - enabled. |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 219 | * |
| 220 | * @return - negative value for an error, 0 for success. |
| 221 | */ |
| 222 | int mdtp_fuse_get_enabled(bool *enabled) |
| 223 | { |
| 224 | int status; |
| 225 | mdtp_eFuses_t eFuses; |
| 226 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 227 | *enabled = 1; |
| 228 | |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 229 | 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 Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 236 | if (!(eFuses.bitwise.enable1 && !eFuses.bitwise.disable1) && |
| 237 | !(eFuses.bitwise.enable2 && !eFuses.bitwise.disable2) && |
| 238 | !(eFuses.bitwise.enable3 && !eFuses.bitwise.disable3)) |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 239 | { |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 240 | *enabled = 0; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 241 | } |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |