Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved. |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +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 <dev/fbcon.h> |
| 31 | #include <target.h> |
| 32 | #include <mmc.h> |
| 33 | #include <partition_parser.h> |
| 34 | #include <platform.h> |
| 35 | #include <crypto_hash.h> |
| 36 | #include <malloc.h> |
| 37 | #include <sha.h> |
| 38 | #include <string.h> |
| 39 | #include <rand.h> |
| 40 | #include <stdlib.h> |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 41 | #include <boot_verifier.h> |
| 42 | #include <image_verify.h> |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 43 | #include "scm.h" |
| 44 | #include "mdtp.h" |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 45 | #include "mdtp_fs.h" |
| 46 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 47 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 48 | #define DIP_ENCRYPT (0) |
| 49 | #define DIP_DECRYPT (1) |
| 50 | #define MAX_CIPHER_DIP_SCM_CALLS (3) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 51 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 52 | #define MDTP_MAJOR_VERSION (0) |
| 53 | #define MDTP_MINOR_VERSION (2) |
| 54 | |
Reut Zysman | d713862 | 2016-01-18 14:43:59 +0200 | [diff] [blame] | 55 | #define MDTP_CORRECT_PIN_DELAY_MSEC (1000) |
| 56 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 57 | /** Extract major version number from complete version. */ |
| 58 | #define MDTP_GET_MAJOR_VERSION(version) ((version) >> 16) |
| 59 | |
Amir Kotzer | bb8be14 | 2016-02-23 09:38:25 +0200 | [diff] [blame] | 60 | |
| 61 | /** UT defines **/ |
| 62 | #define BAD_PARAM_SIZE 0 |
| 63 | #define BAD_PARAM_VERIF_RATIO 101 |
| 64 | #define BAD_HASH_MODE 10 |
| 65 | |
| 66 | /********************************************************************************/ |
| 67 | |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 68 | static int mdtp_tzbsp_dec_verify_DIP(DIP_t *enc_dip, DIP_t *dec_dip, uint32_t *verified); |
| 69 | static int mdtp_tzbsp_enc_hash_DIP(DIP_t *dec_dip, DIP_t *enc_dip); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 70 | static void mdtp_tzbsp_disallow_cipher_DIP(void); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 71 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 72 | uint32_t g_mdtp_version = (((MDTP_MAJOR_VERSION << 16) & 0xFFFF0000) | (MDTP_MINOR_VERSION & 0x0000FFFF)); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 73 | static int is_mdtp_activated = -1; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 74 | |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 75 | extern int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 76 | int scm_random(uint32_t * rbuf, uint32_t r_len); |
Reut Zysman | d713862 | 2016-01-18 14:43:59 +0200 | [diff] [blame] | 77 | extern void mdelay(unsigned msecs); |
Rami Butstein | faecf7f | 2015-06-04 16:39:30 +0300 | [diff] [blame] | 78 | void free_mdtp_image(void); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 79 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 80 | /********************************************************************************/ |
| 81 | |
| 82 | /* Read the DIP from EMMC */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 83 | static int read_DIP(DIP_t *dip) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 84 | { |
| 85 | unsigned long long ptn = 0; |
| 86 | uint32_t actual_partition_size; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 87 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 88 | |
| 89 | int index = INVALID_PTN; |
| 90 | |
| 91 | ASSERT(dip != NULL); |
| 92 | |
| 93 | index = partition_get_index("dip"); |
| 94 | ptn = partition_get_offset(index); |
| 95 | |
| 96 | if(ptn == 0) |
| 97 | { |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | actual_partition_size = ROUNDUP(sizeof(DIP_t), block_size); |
| 102 | |
| 103 | if(mmc_read(ptn, (void *)dip, actual_partition_size)) |
| 104 | { |
| 105 | dprintf(CRITICAL, "mdtp: read_DIP: ERROR, cannot read DIP info\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 109 | dprintf(SPEW, "mdtp: read_DIP: SUCCESS, read %d bytes\n", actual_partition_size); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* Store the DIP into the EMMC */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 115 | static int write_DIP(DIP_t *dip) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 116 | { |
| 117 | unsigned long long ptn = 0; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 118 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 119 | |
| 120 | int index = INVALID_PTN; |
| 121 | |
| 122 | ASSERT(dip != NULL); |
| 123 | |
| 124 | index = partition_get_index("dip"); |
| 125 | ptn = partition_get_offset(index); |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 126 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 127 | if(ptn == 0) |
| 128 | { |
| 129 | return -1; |
| 130 | } |
| 131 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 132 | if(mmc_write(ptn, ROUNDUP(sizeof(DIP_t), block_size), (void *)dip)) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 133 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 134 | dprintf(CRITICAL, "mdtp: write_DIP: ERROR, cannot write DIP info\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 135 | return -1; |
| 136 | } |
| 137 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 138 | dprintf(SPEW, "mdtp: write_DIP: SUCCESS, write %d bytes\n", ROUNDUP(sizeof(DIP_t), block_size)); |
| 139 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 143 | /* Deactivate MDTP by storing the default DIP into the EMMC */ |
| 144 | static void write_deactivated_DIP() |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 145 | { |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 146 | DIP_t *enc_dip; |
| 147 | DIP_t *dec_dip; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 148 | int ret; |
| 149 | |
| 150 | enc_dip = malloc(sizeof(DIP_t)); |
| 151 | if (enc_dip == NULL) |
| 152 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 153 | dprintf(CRITICAL, "mdtp: write_deactivated_DIP: ERROR, cannot allocate DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
| 157 | dec_dip = malloc(sizeof(DIP_t)); |
| 158 | if (dec_dip == NULL) |
| 159 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 160 | dprintf(CRITICAL, "mdtp: write_deactivated_DIP: ERROR, cannot allocate DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 161 | free(enc_dip); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | memset(dec_dip, 0, sizeof(DIP_t)); |
| 166 | |
| 167 | dec_dip->status = DIP_STATUS_DEACTIVATED; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 168 | dec_dip->version = g_mdtp_version; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 169 | |
| 170 | ret = mdtp_tzbsp_enc_hash_DIP(dec_dip, enc_dip); |
| 171 | if(ret < 0) |
| 172 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 173 | dprintf(CRITICAL, "mdtp: write_deactivated_DIP: ERROR, cannot cipher DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 174 | goto out; |
| 175 | } |
| 176 | |
| 177 | ret = write_DIP(enc_dip); |
| 178 | if(ret < 0) |
| 179 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 180 | dprintf(CRITICAL, "mdtp: write_deactivated_DIP: ERROR, cannot write DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 181 | goto out; |
| 182 | } |
| 183 | |
| 184 | out: |
| 185 | free(enc_dip); |
| 186 | free(dec_dip); |
| 187 | } |
| 188 | |
| 189 | /* Validate a hash calculated on entire given partition */ |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 190 | static int verify_partition_single_hash(char *name, uint64_t size, DIP_hash_table_entry_t *hash_table) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 191 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 192 | unsigned char digest[HASH_LEN]={0}; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 193 | unsigned long long ptn = 0; |
| 194 | int index = INVALID_PTN; |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 195 | unsigned char *buf = (unsigned char *)target_get_scratch_address() + MDTP_SCRATCH_OFFSET; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 196 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 197 | uint64_t actual_partition_size = ROUNDUP(size, block_size); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 198 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 199 | dprintf(SPEW, "mdtp: verify_partition_single_hash: %s, %llu\n", name, size); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 200 | |
| 201 | ASSERT(name != NULL); |
| 202 | ASSERT(hash_table != NULL); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 203 | ASSERT(size > 0); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 204 | |
| 205 | index = partition_get_index(name); |
| 206 | ptn = partition_get_offset(index); |
| 207 | |
| 208 | if(ptn == 0) { |
| 209 | dprintf(CRITICAL, "mdtp: verify_partition_single_hash: %s: partition was not found\n", name); |
| 210 | return -1; |
| 211 | } |
| 212 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 213 | if (mmc_read(ptn, (void *)buf, actual_partition_size)) |
| 214 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 215 | dprintf(CRITICAL, "mdtp: verify_partition_single_hash: %s: mmc_read() fail.\n", name); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 216 | return -1; |
| 217 | } |
| 218 | |
Amit Blay | fe64921 | 2015-01-25 11:21:10 +0200 | [diff] [blame] | 219 | /* calculating the hash value using HW crypto */ |
| 220 | target_crypto_init_params(); |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 221 | |
| 222 | if(strcmp(name, "mdtp") == 0){ |
| 223 | buf[0] = 0; // removes first byte |
| 224 | dprintf(INFO, "mdtp: verify_partition_single_hash: removes 1st byte\n"); |
| 225 | } |
| 226 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 227 | hash_find(buf, size, digest, CRYPTO_AUTH_ALG_SHA256); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 228 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 229 | if (memcmp(digest, hash_table->hash, HASH_LEN)) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 230 | { |
| 231 | dprintf(CRITICAL, "mdtp: verify_partition_single_hash: %s: Failed partition hash verification\n", name); |
| 232 | |
| 233 | return -1; |
| 234 | } |
| 235 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 236 | dprintf(SPEW, "verify_partition_single_hash: %s: VERIFIED!\n", name); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /* Validate a hash table calculated per block of a given partition */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 242 | static int verify_partition_block_hash(char *name, |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 243 | uint64_t size, |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 244 | uint32_t verify_num_blocks, |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 245 | DIP_hash_table_entry_t *hash_table, |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 246 | uint8_t *force_verify_block) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 247 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 248 | unsigned char digest[HASH_LEN]={0}; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 249 | unsigned long long ptn = 0; |
| 250 | int index = INVALID_PTN; |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 251 | unsigned char *buf = (unsigned char *)target_get_scratch_address() + MDTP_SCRATCH_OFFSET; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 252 | uint32_t bytes_to_read; |
| 253 | uint32_t block_num = 0; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 254 | uint32_t total_num_blocks = ((size - 1) / MDTP_FWLOCK_BLOCK_SIZE) + 1; |
| 255 | uint32_t rand_int; |
| 256 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 257 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 258 | dprintf(SPEW, "mdtp: verify_partition_block_hash: %s, %llu\n", name, size); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 259 | |
| 260 | ASSERT(name != NULL); |
| 261 | ASSERT(hash_table != NULL); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 262 | ASSERT(size > 0); |
| 263 | ASSERT(force_verify_block != NULL); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 264 | |
| 265 | index = partition_get_index(name); |
| 266 | ptn = partition_get_offset(index); |
| 267 | |
| 268 | if(ptn == 0) { |
| 269 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: partition was not found\n", name); |
| 270 | return -1; |
| 271 | } |
| 272 | |
Amit Blay | fe64921 | 2015-01-25 11:21:10 +0200 | [diff] [blame] | 273 | /* initiating parameters for hash calculation using HW crypto */ |
| 274 | target_crypto_init_params(); |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 275 | if (check_aboot_addr_range_overlap((uintptr_t)buf, ROUNDUP(MDTP_FWLOCK_BLOCK_SIZE, block_size))) |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 276 | { |
| 277 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: image buffer address overlaps with aboot addresses.\n", name); |
| 278 | return -1; |
| 279 | } |
Amit Blay | fe64921 | 2015-01-25 11:21:10 +0200 | [diff] [blame] | 280 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 281 | while (MDTP_FWLOCK_BLOCK_SIZE * block_num < size) |
| 282 | { |
| 283 | if (*force_verify_block == 0) |
| 284 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 285 | if(scm_random((uint32_t *)&rand_int, sizeof(rand_int))) |
| 286 | { |
| 287 | dprintf(CRITICAL,"mdtp: scm_call for random failed\n"); |
| 288 | return -1; |
| 289 | } |
| 290 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 291 | /* Skip validation of this block with probability of verify_num_blocks / total_num_blocks */ |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 292 | if ((rand_int % total_num_blocks) >= verify_num_blocks) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 293 | { |
| 294 | block_num++; |
| 295 | hash_table += 1; |
| 296 | force_verify_block += 1; |
| 297 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: skipped verification of block %d\n", name, block_num); |
| 298 | continue; |
| 299 | } |
| 300 | } |
| 301 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 302 | if ((size - (MDTP_FWLOCK_BLOCK_SIZE * block_num) < MDTP_FWLOCK_BLOCK_SIZE)) |
| 303 | { |
| 304 | bytes_to_read = size - (MDTP_FWLOCK_BLOCK_SIZE * block_num); |
| 305 | } else |
| 306 | { |
| 307 | bytes_to_read = MDTP_FWLOCK_BLOCK_SIZE; |
| 308 | } |
| 309 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 310 | if (mmc_read(ptn + (MDTP_FWLOCK_BLOCK_SIZE * block_num), (void *)buf, ROUNDUP(bytes_to_read, block_size))) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 311 | { |
| 312 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: mmc_read() fail.\n", name); |
| 313 | return -1; |
| 314 | } |
| 315 | |
Amit Blay | fe64921 | 2015-01-25 11:21:10 +0200 | [diff] [blame] | 316 | /* calculating the hash value using HW */ |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 317 | hash_find(buf, bytes_to_read, digest, CRYPTO_AUTH_ALG_SHA256); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 318 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 319 | if (memcmp(digest, hash_table->hash, HASH_LEN)) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 320 | { |
| 321 | dprintf(CRITICAL, "mdtp: verify_partition_block_hash: %s: Failed partition hash[%d] verification\n", name, block_num); |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | block_num++; |
| 326 | hash_table += 1; |
| 327 | force_verify_block += 1; |
| 328 | } |
| 329 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 330 | dprintf(SPEW, "verify_partition_block_hash: %s: VERIFIED!\n", name); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 335 | /* Validate the partition parameters read from DIP */ |
| 336 | static int validate_partition_params(uint64_t size, |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 337 | mdtp_fwlock_mode_t hash_mode, |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 338 | uint32_t verify_ratio) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 339 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 340 | if (size == 0 || size > (uint64_t)MDTP_FWLOCK_BLOCK_SIZE * (uint64_t)MAX_BLOCKS || |
Amir Kotzer | 7c768c0 | 2016-04-13 09:08:05 +0300 | [diff] [blame] | 341 | hash_mode > MDTP_FWLOCK_MODE_FILES || verify_ratio > 100) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 342 | { |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 343 | dprintf(CRITICAL, "mdtp: validate_partition_params: error, size=%llu, hash_mode=%d, verify_ratio=%d\n", |
| 344 | size, hash_mode, verify_ratio); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 351 | /* Verify a given partitinon */ |
| 352 | static int verify_partition(char *name, |
| 353 | uint64_t size, |
| 354 | mdtp_fwlock_mode_t hash_mode, |
| 355 | uint32_t verify_num_blocks, |
| 356 | DIP_hash_table_entry_t *hash_table, |
| 357 | uint8_t *force_verify_block) |
| 358 | { |
| 359 | if (hash_mode == MDTP_FWLOCK_MODE_SINGLE) |
| 360 | { |
| 361 | return verify_partition_single_hash(name, size, hash_table); |
| 362 | } else if (hash_mode == MDTP_FWLOCK_MODE_BLOCK || hash_mode == MDTP_FWLOCK_MODE_FILES) |
| 363 | { |
| 364 | return verify_partition_block_hash(name, size, verify_num_blocks, hash_table, force_verify_block); |
| 365 | } |
| 366 | |
| 367 | /* Illegal value of hash_mode */ |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | static int validate_dip(DIP_t *dip) |
| 372 | { |
| 373 | uint8_t *dip_p; |
| 374 | |
| 375 | ASSERT(dip != NULL); |
| 376 | |
| 377 | /* Make sure DIP version is supported by current SW */ |
| 378 | if (MDTP_GET_MAJOR_VERSION(dip->version) != MDTP_MAJOR_VERSION) |
| 379 | { |
| 380 | dprintf(CRITICAL, "mdtp: validate_dip: Wrong DIP version 0x%x\n", dip->version); |
| 381 | return -1; |
| 382 | } |
| 383 | |
| 384 | /* Make sure that deactivated DIP content is as expected */ |
| 385 | if (dip->status == DIP_STATUS_DEACTIVATED) |
| 386 | { |
| 387 | dip_p = (uint8_t*)&dip->mdtp_cfg; |
| 388 | while (dip_p < dip->hash) |
| 389 | { |
| 390 | if (*dip_p != 0) |
| 391 | { |
| 392 | dprintf(CRITICAL, "mdtp: validate_dip: error in deactivated DIP\n"); |
| 393 | return -1; |
| 394 | } |
| 395 | dip_p++; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 402 | /* Display the recovery UI in case mdtp image is corrupted */ |
| 403 | static void display_mdtp_fail_recovery_ui(){ |
| 404 | display_error_msg_mdtp(); |
| 405 | } |
| 406 | |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 407 | /* Display the recovery UI to allow the user to enter the PIN and continue boot */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 408 | static void display_recovery_ui(mdtp_cfg_t *mdtp_cfg) |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 409 | { |
| 410 | uint32_t pin_length = 0; |
Amir Kotzer | 20716f1 | 2016-02-24 10:31:18 +0200 | [diff] [blame] | 411 | char entered_pin[MDTP_PIN_LEN+1] = {0}; |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 412 | uint32_t i; |
Amir Kotzer | 3b3217c | 2016-02-29 17:20:28 +0200 | [diff] [blame] | 413 | int pin_mismatch = -1; |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 414 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 415 | if (mdtp_cfg->enable_local_pin_authentication) |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 416 | { |
| 417 | dprintf(SPEW, "mdtp: display_recovery_ui: Local deactivation enabled\n"); |
| 418 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 419 | pin_length = strlen(mdtp_cfg->mdtp_pin.mdtp_pin); |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 420 | |
Amir Kotzer | 20716f1 | 2016-02-24 10:31:18 +0200 | [diff] [blame] | 421 | if (pin_length != MDTP_PIN_LEN) |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 422 | { |
| 423 | dprintf(CRITICAL, "mdtp: display_recovery_ui: Error, invalid PIN length\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 424 | display_error_msg(); /* This will never return */ |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // Set entered_pin to initial '0' string + null terminator |
| 428 | for (i=0; i<pin_length; i++) |
| 429 | { |
| 430 | entered_pin[i] = '0'; |
| 431 | } |
| 432 | |
| 433 | // Allow the user to enter the PIN as many times as he wishes |
| 434 | // (with INVALID_PIN_DELAY_MSECONDS after each failed attempt) |
| 435 | while (1) |
| 436 | { |
Amir Kotzer | 3b3217c | 2016-02-29 17:20:28 +0200 | [diff] [blame] | 437 | pin_mismatch = pin_length; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 438 | get_pin_from_user(entered_pin, pin_length); |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 439 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 440 | // Go over the entire PIN in any case, to prevent side-channel attacks |
| 441 | for (i=0; i<pin_length; i++) |
| 442 | { |
Amir Kotzer | 3b3217c | 2016-02-29 17:20:28 +0200 | [diff] [blame] | 443 | // If current digit match, reduce 1 from pin_mismatch |
| 444 | pin_mismatch -= (((mdtp_cfg->mdtp_pin.mdtp_pin[i] ^ entered_pin[i]) == 0) ? 1 : 0); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 445 | } |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 446 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 447 | if (0 == pin_mismatch) |
| 448 | { |
| 449 | // Valid PIN - deactivate and continue boot |
| 450 | dprintf(SPEW, "mdtp: display_recovery_ui: valid PIN, continue boot\n"); |
| 451 | write_deactivated_DIP(); |
Rami Butstein | faecf7f | 2015-06-04 16:39:30 +0300 | [diff] [blame] | 452 | goto out; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 453 | } |
| 454 | else |
| 455 | { |
| 456 | // Invalid PIN - display an appropriate message (which also includes a wait |
| 457 | // for INVALID_PIN_DELAY_MSECONDS), and allow the user to try again |
| 458 | dprintf(CRITICAL, "mdtp: display_recovery_ui: ERROR, invalid PIN\n"); |
| 459 | display_invalid_pin_msg(); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 460 | } |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | else |
| 464 | { |
| 465 | dprintf(CRITICAL, "mdtp: display_recovery_ui: Local deactivation disabled, unable to display recovery UI\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 466 | display_error_msg(); /* This will never return */ |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 467 | } |
Rami Butstein | faecf7f | 2015-06-04 16:39:30 +0300 | [diff] [blame] | 468 | |
| 469 | out: |
Amit Blay | 5484915 | 2015-11-09 15:51:40 +0200 | [diff] [blame] | 470 | display_image_on_screen(); |
Rami Butstein | faecf7f | 2015-06-04 16:39:30 +0300 | [diff] [blame] | 471 | free_mdtp_image(); |
Reut Zysman | d713862 | 2016-01-18 14:43:59 +0200 | [diff] [blame] | 472 | mdelay(MDTP_CORRECT_PIN_DELAY_MSEC); |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 473 | } |
| 474 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 475 | /* Verify the boot or recovery partitions using boot_verifier. */ |
| 476 | static int verify_ext_partition(mdtp_ext_partition_verification_t *ext_partition) |
| 477 | { |
| 478 | int ret = 0; |
| 479 | bool restore_to_orange = false; |
| 480 | unsigned long long ptn = 0; |
| 481 | int index = INVALID_PTN; |
| 482 | |
| 483 | /* If image was already verified in aboot, return its status */ |
| 484 | if (ext_partition->integrity_state == MDTP_PARTITION_STATE_INVALID) |
| 485 | { |
| 486 | dprintf(CRITICAL, "mdtp: verify_ext_partition: image %s verified externally and failed.\n", |
| 487 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 488 | return -1; |
| 489 | } |
| 490 | else if (ext_partition->integrity_state == MDTP_PARTITION_STATE_VALID) |
| 491 | { |
| 492 | dprintf(CRITICAL, "mdtp: verify_ext_partition: image %s verified externally succesfully.\n", |
| 493 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | /* If image was not verified in aboot, verify it ourselves using boot_verifier. */ |
| 498 | |
| 499 | /* 1) Initialize keystore. We don't care about return value which is Verified Boot's state machine state. */ |
| 500 | boot_verify_keystore_init(); |
| 501 | |
| 502 | /* 2) If boot_verifier is ORANGE, it will prevent verifying an image. So |
| 503 | * temporarly change boot_verifier state to BOOT_INIT. |
| 504 | */ |
| 505 | if (boot_verify_get_state() == ORANGE) |
| 506 | restore_to_orange = true; |
| 507 | boot_verify_send_event(BOOT_INIT); |
| 508 | |
| 509 | switch (ext_partition->partition) |
| 510 | { |
| 511 | case MDTP_PARTITION_BOOT: |
| 512 | case MDTP_PARTITION_RECOVERY: |
| 513 | |
| 514 | /* 3) Signature may or may not be at the end of the image. Read the signature if needed. */ |
| 515 | if (!ext_partition->sig_avail) |
| 516 | { |
Vijay Kumar Pendoti | 9c002ad | 2016-03-09 13:52:45 +0530 | [diff] [blame] | 517 | if (check_aboot_addr_range_overlap((uintptr_t)(ext_partition->image_addr + ext_partition->image_size), ext_partition->page_size)) |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 518 | { |
| 519 | dprintf(CRITICAL, "ERROR: Signature read buffer address overlaps with aboot addresses.\n"); |
| 520 | return -1; |
| 521 | } |
| 522 | |
| 523 | index = partition_get_index(ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 524 | ptn = partition_get_offset(index); |
| 525 | if(ptn == 0) { |
| 526 | dprintf(CRITICAL, "ERROR: partition %s not found\n", |
| 527 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | if(mmc_read(ptn + ext_partition->image_size, (void *)(ext_partition->image_addr + ext_partition->image_size), ext_partition->page_size)) |
| 532 | { |
| 533 | dprintf(CRITICAL, "ERROR: Cannot read %s image signature\n", |
| 534 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 535 | return -1; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /* 4) Verify the image using its signature. */ |
| 540 | ret = boot_verify_image((unsigned char *)ext_partition->image_addr, |
| 541 | ext_partition->image_size, |
Amit Blay | 59fad25 | 2015-05-17 17:27:17 +0300 | [diff] [blame] | 542 | ext_partition->partition == MDTP_PARTITION_BOOT ? "/boot" : "/recovery"); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 543 | break; |
| 544 | |
| 545 | default: |
| 546 | /* Only boot and recovery are legal here */ |
| 547 | dprintf(CRITICAL, "ERROR: wrong partition %d\n", ext_partition->partition); |
| 548 | return -1; |
| 549 | } |
| 550 | |
| 551 | if (ret) |
| 552 | { |
| 553 | dprintf(INFO, "mdtp: verify_ext_partition: image %s verified succesfully in MDTP.\n", |
| 554 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | dprintf(CRITICAL, "mdtp: verify_ext_partition: image %s verification failed in MDTP.\n", |
| 559 | ext_partition->partition == MDTP_PARTITION_BOOT ? "boot" : "recovery"); |
| 560 | } |
| 561 | |
| 562 | /* 5) Restore the right boot_verifier state upon exit. */ |
| 563 | if (restore_to_orange) |
| 564 | { |
| 565 | boot_verify_send_event(DEV_UNLOCK); |
| 566 | } |
| 567 | |
| 568 | return ret ? 0 : -1; |
| 569 | } |
| 570 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 571 | /* Verify all protected partitinons according to the DIP */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 572 | static void verify_all_partitions(DIP_t *dip, |
| 573 | mdtp_ext_partition_verification_t *ext_partition, |
| 574 | verify_result_t *verify_result) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 575 | { |
| 576 | int i; |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 577 | int verify_failure = 0; |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 578 | int verify_temp_result = 0; |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 579 | int ext_partition_verify_failure = 0; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 580 | uint32_t total_num_blocks; |
| 581 | |
| 582 | ASSERT(dip != NULL); |
| 583 | ASSERT(verify_result != NULL); |
| 584 | |
| 585 | *verify_result = VERIFY_FAILED; |
| 586 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 587 | if (validate_dip(dip)) |
| 588 | { |
| 589 | dprintf(CRITICAL, "mdtp: verify_all_partitions: failed DIP validation\n"); |
| 590 | return; |
| 591 | } |
| 592 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 593 | if (dip->status == DIP_STATUS_DEACTIVATED) |
| 594 | { |
| 595 | *verify_result = VERIFY_SKIPPED; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 596 | return; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 597 | } |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 598 | else |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 599 | { |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 600 | if (ext_partition->partition != MDTP_PARTITION_NONE) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 601 | { |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 602 | for(i=0; i<MAX_PARTITIONS; i++) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 603 | { |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 604 | verify_temp_result = 0; |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 605 | if(dip->partition_cfg[i].lock_enabled && dip->partition_cfg[i].size) |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 606 | { |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 607 | total_num_blocks = ((dip->partition_cfg[i].size - 1) / MDTP_FWLOCK_BLOCK_SIZE); |
| 608 | if (validate_partition_params(dip->partition_cfg[i].size, |
| 609 | dip->partition_cfg[i].hash_mode, |
| 610 | dip->partition_cfg[i].verify_ratio)) |
| 611 | { |
| 612 | dprintf(CRITICAL, "mdtp: verify_all_partitions: Wrong partition parameters\n"); |
| 613 | verify_failure = TRUE; |
| 614 | break; |
| 615 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 616 | |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 617 | verify_temp_result |= (verify_partition(dip->partition_cfg[i].name, |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 618 | dip->partition_cfg[i].size, |
| 619 | dip->partition_cfg[i].hash_mode, |
| 620 | (dip->partition_cfg[i].verify_ratio * total_num_blocks) / 100, |
| 621 | dip->partition_cfg[i].hash_table, |
| 622 | dip->partition_cfg[i].force_verify_block) != 0); |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 623 | |
| 624 | if((verify_temp_result) && (strcmp("mdtp",dip->partition_cfg[i].name) == 0)){ |
| 625 | *verify_result = VERIFY_MDTP_FAILED; |
| 626 | } |
| 627 | |
| 628 | verify_failure |= verify_temp_result; |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
| 632 | ext_partition_verify_failure = verify_ext_partition(ext_partition); |
| 633 | |
| 634 | if (verify_failure || ext_partition_verify_failure) |
| 635 | { |
| 636 | dprintf(CRITICAL, "mdtp: verify_all_partitions: Failed partition verification\n"); |
| 637 | return; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 638 | } |
| 639 | } |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 640 | is_mdtp_activated = 1; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | *verify_result = VERIFY_OK; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 644 | return; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /* Verify the DIP and all protected partitions */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 648 | static void validate_DIP_and_firmware(mdtp_ext_partition_verification_t *ext_partition) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 649 | { |
| 650 | int ret; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 651 | DIP_t *enc_dip; |
| 652 | DIP_t *dec_dip; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 653 | uint32_t verified = 0; |
| 654 | verify_result_t verify_result; |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 655 | uint32_t block_size = mmc_get_device_blocksize(); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 656 | mdtp_cfg_t mdtp_cfg; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 657 | |
| 658 | enc_dip = malloc(ROUNDUP(sizeof(DIP_t), block_size)); |
| 659 | if (enc_dip == NULL) |
| 660 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 661 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, cannot allocate DIP\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 662 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | dec_dip = malloc(ROUNDUP(sizeof(DIP_t), block_size)); |
| 666 | if (dec_dip == NULL) |
| 667 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 668 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, cannot allocate DIP\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 669 | free(enc_dip); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 670 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* Read the DIP holding the MDTP Firmware Lock state from the DIP partition */ |
| 674 | ret = read_DIP(enc_dip); |
| 675 | if(ret < 0) |
| 676 | { |
| 677 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, cannot read DIP\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 678 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /* Decrypt and verify the integrity of the DIP */ |
| 682 | ret = mdtp_tzbsp_dec_verify_DIP(enc_dip, dec_dip, &verified); |
| 683 | if(ret < 0) |
| 684 | { |
| 685 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, cannot verify DIP\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 686 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /* In case DIP integrity verification fails, notify the user and halt */ |
| 690 | if(!verified) |
| 691 | { |
| 692 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, corrupted DIP\n"); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 693 | display_error_msg(); /* This will never return */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 696 | /* Verify the integrity of the partitions which are protected, according to the content of the DIP */ |
| 697 | verify_all_partitions(dec_dip, ext_partition, &verify_result); |
| 698 | |
| 699 | mdtp_cfg = dec_dip->mdtp_cfg; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 700 | |
| 701 | /* Clear decrypted DIP since we don't need it anymore */ |
| 702 | memset(dec_dip, 0, sizeof(DIP_t)); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 703 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 704 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 705 | if (verify_result == VERIFY_OK) |
| 706 | { |
Reut Zysman | 1841127 | 2015-02-09 13:47:27 +0200 | [diff] [blame] | 707 | dprintf(SPEW, "mdtp: validate_DIP_and_firmware: Verify OK\n"); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 708 | } |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 709 | else if (verify_result == VERIFY_SKIPPED) |
| 710 | { |
| 711 | dprintf(SPEW, "mdtp: validate_DIP_and_firmware: Verify skipped\n"); |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 712 | } |
| 713 | else if(verify_result == VERIFY_MDTP_FAILED) |
| 714 | { |
| 715 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, corrupted mdtp image\n"); |
| 716 | display_mdtp_fail_recovery_ui(); |
| 717 | } |
| 718 | else /* VERIFY_FAILED */ |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 719 | { |
| 720 | dprintf(CRITICAL, "mdtp: validate_DIP_and_firmware: ERROR, corrupted firmware\n"); |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 721 | display_recovery_ui(&mdtp_cfg); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 724 | memset(&mdtp_cfg, 0, sizeof(mdtp_cfg)); |
| 725 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 726 | free(enc_dip); |
| 727 | free(dec_dip); |
| 728 | |
| 729 | return; |
| 730 | } |
| 731 | |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 732 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 733 | /********************************************************************************/ |
| 734 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 735 | /** Entry point of the MDTP Firmware Lock. |
| 736 | * If needed, verify the DIP and all protected partitions. |
| 737 | * Allow passing information about partition verified using an external method |
| 738 | * (either boot or recovery). For boot and recovery, either use aboot's |
| 739 | * verification result, or use boot_verifier APIs to verify internally. |
| 740 | **/ |
| 741 | void mdtp_fwlock_verify_lock(mdtp_ext_partition_verification_t *ext_partition) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 742 | { |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 743 | int ret; |
| 744 | bool enabled; |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 745 | |
Reut Zysman | ff6bab9 | 2016-02-09 14:06:31 +0200 | [diff] [blame] | 746 | if(mdtp_fs_init() != 0){ |
| 747 | dprintf(CRITICAL, "mdtp: mdtp_img: ERROR, image file could not be loaded\n"); |
| 748 | display_error_msg_mdtp(); /* This will never return */ |
| 749 | } |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 750 | /* sets the default value of this global to be MDTP not activated */ |
| 751 | is_mdtp_activated = 0; |
| 752 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 753 | do { |
| 754 | if (ext_partition == NULL) |
| 755 | { |
| 756 | dprintf(CRITICAL, "mdtp: mdtp_fwlock_verify_lock: ERROR, external partition is NULL\n"); |
| 757 | display_error_msg(); /* This will never return */ |
| 758 | break; |
| 759 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 760 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 761 | ret = mdtp_fuse_get_enabled(&enabled); |
| 762 | if(ret) |
| 763 | { |
| 764 | dprintf(CRITICAL, "mdtp: mdtp_fwlock_verify_lock: ERROR, cannot get enabled fuse\n"); |
| 765 | display_error_msg(); /* This will never return */ |
| 766 | } |
| 767 | |
| 768 | /* Continue with Firmware Lock verification only if enabled by eFuse */ |
| 769 | if (enabled) |
| 770 | { |
| 771 | /* This function will handle firmware verification failure via UI */ |
| 772 | validate_DIP_and_firmware(ext_partition); |
| 773 | } |
| 774 | } while (0); |
| 775 | |
| 776 | /* Disallow CIPHER_DIP SCM call from this point, unless we are in recovery */ |
| 777 | /* The recovery image will disallow CIPHER_DIP SCM call by itself. */ |
Amit Blay | 8a51030 | 2015-08-17 09:20:01 +0300 | [diff] [blame] | 778 | if (ext_partition->partition == MDTP_PARTITION_BOOT) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 779 | { |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 780 | mdtp_tzbsp_disallow_cipher_DIP(); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 781 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 782 | } |
Shay Nachmani | bc10dfe | 2015-02-10 14:45:55 +0200 | [diff] [blame] | 783 | /********************************************************************************/ |
| 784 | |
| 785 | /** Indicates whether the MDTP is currently in ACTIVATED state **/ |
| 786 | int mdtp_activated(bool * activated){ |
| 787 | if(is_mdtp_activated < 0){ |
| 788 | /* mdtp_fwlock_verify_lock was not called before, the value is not valid */ |
| 789 | return is_mdtp_activated; |
| 790 | } |
| 791 | |
| 792 | *activated = is_mdtp_activated; |
| 793 | return 0; |
| 794 | } |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 795 | |
| 796 | /********************************************************************************/ |
| 797 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 798 | /* Decrypt a given DIP and verify its integrity */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 799 | static int mdtp_tzbsp_dec_verify_DIP(DIP_t *enc_dip, DIP_t *dec_dip, uint32_t *verified) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 800 | { |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 801 | unsigned char hash[HASH_LEN]; |
| 802 | SHA256_CTX sha256_ctx; |
| 803 | int ret; |
| 804 | |
| 805 | ASSERT(enc_dip != NULL); |
| 806 | ASSERT(dec_dip != NULL); |
| 807 | ASSERT(verified != NULL); |
| 808 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 809 | arch_clean_invalidate_cache_range((addr_t)enc_dip, sizeof(DIP_t)); |
| 810 | arch_invalidate_cache_range((addr_t)dec_dip, sizeof(DIP_t)); |
| 811 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 812 | ret = mdtp_cipher_dip_cmd((uint8_t*)enc_dip, sizeof(DIP_t), |
| 813 | (uint8_t*)dec_dip, sizeof(DIP_t), |
| 814 | DIP_DECRYPT); |
| 815 | if (ret) |
| 816 | { |
| 817 | dprintf(CRITICAL, "mdtp: mdtp_tzbsp_dec_verify_DIP: ERROR, cannot cipher DIP\n"); |
| 818 | *verified = 0; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 819 | memset(dec_dip, 0, sizeof(DIP_t)); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 820 | return -1; |
| 821 | } |
| 822 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 823 | arch_invalidate_cache_range((addr_t)dec_dip, sizeof(DIP_t)); |
| 824 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 825 | SHA256_Init(&sha256_ctx); |
| 826 | SHA256_Update(&sha256_ctx, dec_dip, sizeof(DIP_t) - HASH_LEN); |
| 827 | SHA256_Final(hash, &sha256_ctx); |
| 828 | |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 829 | if (memcmp(hash, dec_dip->hash, HASH_LEN)) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 830 | { |
| 831 | *verified = 0; |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 832 | memset(dec_dip, 0, sizeof(DIP_t)); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 833 | } |
| 834 | else |
| 835 | { |
| 836 | *verified = 1; |
| 837 | } |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 842 | /* Encrypt a given DIP and calculate its integrity information */ |
Amit Blay | df42d2f | 2015-02-03 16:37:09 +0200 | [diff] [blame] | 843 | static int mdtp_tzbsp_enc_hash_DIP(DIP_t *dec_dip, DIP_t *enc_dip) |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 844 | { |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 845 | SHA256_CTX sha256_ctx; |
| 846 | int ret; |
| 847 | |
| 848 | ASSERT(dec_dip != NULL); |
| 849 | ASSERT(enc_dip != NULL); |
| 850 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 851 | SHA256_Init(&sha256_ctx); |
| 852 | SHA256_Update(&sha256_ctx, dec_dip, sizeof(DIP_t) - HASH_LEN); |
Amit Blay | 8e2731c | 2015-04-28 21:54:55 +0300 | [diff] [blame] | 853 | SHA256_Final(dec_dip->hash, &sha256_ctx); |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 854 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 855 | arch_clean_invalidate_cache_range((addr_t)dec_dip, sizeof(DIP_t)); |
| 856 | arch_invalidate_cache_range((addr_t)enc_dip, sizeof(DIP_t)); |
| 857 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 858 | ret = mdtp_cipher_dip_cmd((uint8_t*)dec_dip, sizeof(DIP_t), |
| 859 | (uint8_t*)enc_dip, sizeof(DIP_t), |
| 860 | DIP_ENCRYPT); |
| 861 | if (ret) |
| 862 | { |
| 863 | dprintf(CRITICAL, "mdtp: mdtp_tzbsp_enc_hash_DIP: ERROR, cannot cipher DIP\n"); |
| 864 | return -1; |
| 865 | } |
| 866 | |
Amit Blay | 4418fb4 | 2015-05-05 08:45:13 +0300 | [diff] [blame] | 867 | arch_invalidate_cache_range((addr_t)enc_dip, sizeof(DIP_t)); |
| 868 | |
Amit Blay | 6281ebc | 2015-01-11 14:44:08 +0200 | [diff] [blame] | 869 | return 0; |
| 870 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 871 | |
| 872 | /* Disallow the CIPHER_DIP SCM call */ |
| 873 | static void mdtp_tzbsp_disallow_cipher_DIP(void) |
| 874 | { |
| 875 | DIP_t *dip; |
| 876 | int i; |
| 877 | |
| 878 | dip = malloc(sizeof(DIP_t)); |
| 879 | if (dip == NULL) |
| 880 | { |
| 881 | dprintf(CRITICAL, "mdtp: mdtp_tzbsp_disallow_cipher_DIP: ERROR, cannot allocate DIP\n"); |
Amir Kotzer | 7c768c0 | 2016-04-13 09:08:05 +0300 | [diff] [blame] | 882 | /* Could not allocate DIP - stop device from booting */ |
| 883 | display_error_msg(); /* This will never return */ |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* Disallow the CIPHER_DIP SCM by calling it MAX_CIPHER_DIP_SCM_CALLS times */ |
| 887 | for (i=0; i<MAX_CIPHER_DIP_SCM_CALLS; i++) |
| 888 | { |
Amir Kotzer | 7c768c0 | 2016-04-13 09:08:05 +0300 | [diff] [blame] | 889 | if(mdtp_tzbsp_enc_hash_DIP(dip, dip) != 0) |
| 890 | { |
| 891 | /* Couldn't disallowd CIPHER_DIP SCM - stop device from booting */ |
| 892 | display_error_msg(); /* This will never return */ |
| 893 | } |
Amit Blay | 4aa292f | 2015-04-28 21:55:59 +0300 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | free(dip); |
| 897 | } |
Amir Kotzer | bb8be14 | 2016-02-23 09:38:25 +0200 | [diff] [blame] | 898 | |
| 899 | /********************************************************************************/ |
| 900 | |
| 901 | /** UT functions **/ |
| 902 | |
| 903 | /** Hashing fuctions UT **/ |
| 904 | int mdtp_verify_hash_ut(){ |
| 905 | unsigned char digest[HASH_LEN]={0}; |
| 906 | unsigned int hash_expected_result = 0xD42B0A29; |
| 907 | char *buf = "MTDP LK UT hashing functions sanity check"; |
| 908 | int size = 0; |
| 909 | DIP_hash_table_entry_t partition_hash_table; |
| 910 | uint8_t partition_force_verify_block = 0; |
| 911 | |
| 912 | char ptr = buf[0]; |
| 913 | while(ptr){ |
| 914 | ptr = buf[++size]; |
| 915 | } |
| 916 | //Bad partition name - single mode |
| 917 | if(verify_partition_single_hash("BAD_PARTITION", 1, &partition_hash_table) != -1){ |
| 918 | dprintf(INFO, "verify_hash_ut: [FAIL (1)].\n"); |
| 919 | return -1; |
| 920 | } |
| 921 | |
| 922 | //Bad partition name - block mode |
| 923 | if(verify_partition_block_hash("BAD_PARTITION", 1, 1, &partition_hash_table, &partition_force_verify_block) != -1){ |
| 924 | dprintf(INFO, "verify_hash_ut: [FAIL (2)].\n"); |
| 925 | return -1; |
| 926 | } |
| 927 | |
| 928 | //Hashing sanity check |
| 929 | hash_find((unsigned char*)buf, size, digest, CRYPTO_AUTH_ALG_SHA256); |
| 930 | unsigned int *hash_res = (unsigned int *)digest; |
| 931 | if (*hash_res != hash_expected_result){ |
| 932 | dprintf(INFO, "verify_hash_ut: [FAIL (3)].\n"); |
| 933 | return -1; |
| 934 | } |
| 935 | dprintf(INFO, "verify_hash_ut: [PASS].\n"); |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | /** Validate partitions params UT **/ |
| 940 | int mdtp_validate_partition_params_ut(){ |
| 941 | int partition_size = 10; |
| 942 | //Bad size |
| 943 | if(validate_partition_params(BAD_PARAM_SIZE, MDTP_FWLOCK_MODE_SINGLE, 1) != -1){ |
| 944 | dprintf(INFO, "validate_partition_params_ut: [FAIL (1)].\n"); |
| 945 | return -1; |
| 946 | } |
| 947 | |
| 948 | //Bad size |
| 949 | if(validate_partition_params((uint64_t)MDTP_FWLOCK_BLOCK_SIZE * (uint64_t)MAX_BLOCKS + 1, |
| 950 | MDTP_FWLOCK_MODE_SINGLE, 1) != -1){ |
| 951 | dprintf(INFO, "validate_partition_params_ut: [FAIL (2)].\n"); |
| 952 | return -1; |
| 953 | } |
| 954 | |
| 955 | //Bad verification ratio |
| 956 | if(validate_partition_params(partition_size, MDTP_FWLOCK_MODE_SIZE, BAD_PARAM_VERIF_RATIO) != -1){ |
| 957 | dprintf(INFO, "validate_partition_params_ut: [FAIL (3)].\n"); |
| 958 | return -1; |
| 959 | } |
| 960 | dprintf(INFO, "MDTP LK UT: validate_partition_params_ut [ PASS ]\n"); |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | /** Verify partition UT **/ |
| 965 | int mdtp_verify_partition_ut(){ |
| 966 | uint8_t partition_force_verify_block = 0; |
| 967 | DIP_hash_table_entry_t partition_hash_table; |
| 968 | int verify_num_blocks = 10,partition_size = 1; |
| 969 | |
| 970 | //Unkown hashing mode |
| 971 | if(verify_partition("system", partition_size, BAD_HASH_MODE, verify_num_blocks, |
| 972 | &partition_hash_table, &partition_force_verify_block) != -1){ |
| 973 | dprintf(INFO, "verify_partition_ut: Failed Test 1.\n"); |
| 974 | dprintf(INFO, "MDTP LK UT: verify_partition_ut [ FAIL ]\n"); |
| 975 | return -1; |
| 976 | } |
| 977 | dprintf(INFO, "MDTP LK UT: verify_partition_ut [ PASS ]\n"); |
| 978 | return 0; |
| 979 | } |
| 980 | |