Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 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> |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 31 | #include <partition_parser.h> |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
Amit Blay | 9fd4ddb | 2015-08-07 12:57:57 +0300 | [diff] [blame] | 34 | #include <reg.h> |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 35 | #include "mdtp.h" |
Reut Zysman | 561d81a | 2015-08-26 17:37:28 +0300 | [diff] [blame] | 36 | #include "mdtp_defs.h" |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 37 | #include "scm.h" |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 38 | #include "mdtp_fs.h" |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 39 | |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 40 | #define MAX_METADATA_SIZE (0x1000) |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 41 | |
| 42 | /********************************************************************************/ |
| 43 | |
| 44 | typedef union |
| 45 | { |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 46 | struct { |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 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 | |
| 59 | typedef 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 | */ |
| 70 | static 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 Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 82 | /* Make sure we only read the test mode once */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 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 Zysman | 561d81a | 2015-08-26 17:37:28 +0300 | [diff] [blame] | 96 | dprintf(CRITICAL, "mdtp: is_test_mode: qsee_get_secure_state returned error: %d, status.value[0]: %d\n", ret, status_low); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 97 | test_mode = 0; |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 98 | } |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 99 | |
| 100 | test_mode_set = 1; |
Reut Zysman | 561d81a | 2015-08-26 17:37:28 +0300 | [diff] [blame] | 101 | dprintf(INFO, "mdtp: is_test_mode: test mode is set to %d\n", test_mode); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 102 | |
| 103 | return test_mode; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Read the Firmware Lock Metadata from EMMC |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 108 | * @param metadata - Read a metadata block holding eFuse emulation from MDTP partition. |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 109 | * @return - negative value for an error, 0 for success. |
| 110 | */ |
| 111 | static int read_metadata(metadata_t *metadata) |
| 112 | { |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 113 | int eFuse = mdtp_fs_get_param(VIRTUAL_FUSE); |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 114 | if(eFuse == MDTP_PARAM_UNSET_VALUE){ //Error initializing eFuse |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 115 | dprintf(CRITICAL, "mdtp: eFuse reading error\n"); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 116 | return -1; |
| 117 | } |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 118 | metadata->eFuses.mask = (uint8_t)eFuse; |
| 119 | dprintf(INFO, "mdtp: read_metadata: SUCCESS \n"); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 120 | 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 | */ |
| 130 | static int read_QFPROM_fuse(uint8_t *mask) |
| 131 | { |
Reut Zysman | 561d81a | 2015-08-26 17:37:28 +0300 | [diff] [blame] | 132 | struct mdtp_target_efuse target_efuse; |
Amit Blay | 9fd4ddb | 2015-08-07 12:57:57 +0300 | [diff] [blame] | 133 | uint32_t val = 0; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 134 | |
Reut Zysman | 561d81a | 2015-08-26 17:37:28 +0300 | [diff] [blame] | 135 | 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 Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 142 | |
| 143 | /* Shift the read data to be reflected in mask */ |
Reut Zysman | 561d81a | 2015-08-26 17:37:28 +0300 | [diff] [blame] | 144 | *mask = (uint8_t)(val >> target_efuse.start); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 145 | |
| 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 | */ |
| 158 | static 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 Zysman | 561d81a | 2015-08-26 17:37:28 +0300 | [diff] [blame] | 165 | dprintf(CRITICAL, "mdtp: read_test_fuse: Failure getting metadata\n"); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 166 | 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 | */ |
| 183 | static 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 Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 199 | * @param[out] enabled: 0 - disabled, 1 - enabled. |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 200 | * |
| 201 | * @return - negative value for an error, 0 for success. |
| 202 | */ |
| 203 | int mdtp_fuse_get_enabled(bool *enabled) |
| 204 | { |
| 205 | int status; |
| 206 | mdtp_eFuses_t eFuses; |
| 207 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 208 | *enabled = 1; |
| 209 | |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 210 | status = read_fuse(&eFuses.mask); |
| 211 | if (status) |
| 212 | { |
Reut Zysman | 561d81a | 2015-08-26 17:37:28 +0300 | [diff] [blame] | 213 | dprintf(CRITICAL, "mdtp: mdtp_fuse_get_enabled: Failure in reading fuse\n"); |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 214 | return -1; |
| 215 | } |
| 216 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 217 | if (!(eFuses.bitwise.enable1 && !eFuses.bitwise.disable1) && |
Reut Zysman | 3f3eccd | 2016-04-20 22:05:36 +0300 | [diff] [blame] | 218 | !(eFuses.bitwise.enable2 && !eFuses.bitwise.disable2) && |
| 219 | !(eFuses.bitwise.enable3 && !eFuses.bitwise.disable3)) |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 220 | { |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 221 | *enabled = 0; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 222 | } |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |