Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 2 | /* |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 3 | * Copyright (C) 2010-2015 Freescale Semiconductor, Inc. |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Adrian Alonso | fba6f9e | 2015-10-12 13:48:14 -0500 | [diff] [blame] | 7 | #include <config.h> |
| 8 | #include <fuse.h> |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 9 | #include <mapmem.h> |
| 10 | #include <image.h> |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 12 | #include <asm/system.h> |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 13 | #include <asm/arch/clock.h> |
Stefano Babic | f2f07e8 | 2014-06-10 10:26:22 +0200 | [diff] [blame] | 14 | #include <asm/arch/sys_proto.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 15 | #include <asm/mach-imx/hab.h> |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 16 | |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 17 | #define ALIGN_SIZE 0x1000 |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 18 | #define MX6DQ_PU_IROM_MMU_EN_VAR 0x009024a8 |
| 19 | #define MX6DLS_PU_IROM_MMU_EN_VAR 0x00901dd0 |
Breno Matheus Lima | 5760069 | 2019-05-07 20:19:17 +0000 | [diff] [blame] | 20 | #define MX6SL_PU_IROM_MMU_EN_VAR 0x00901c60 |
Adrian Alonso | ee3899a | 2015-10-12 13:48:15 -0500 | [diff] [blame] | 21 | #define IS_HAB_ENABLED_BIT \ |
Peng Fan | 27117b2 | 2017-02-22 16:21:53 +0800 | [diff] [blame] | 22 | (is_soc_type(MXC_SOC_MX7ULP) ? 0x80000000 : \ |
| 23 | (is_soc_type(MXC_SOC_MX7) ? 0x2000000 : 0x2)) |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 24 | |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 25 | static int ivt_header_error(const char *err_str, struct ivt_header *ivt_hdr) |
| 26 | { |
| 27 | printf("%s magic=0x%x length=0x%02x version=0x%x\n", err_str, |
| 28 | ivt_hdr->magic, ivt_hdr->length, ivt_hdr->version); |
| 29 | |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | static int verify_ivt_header(struct ivt_header *ivt_hdr) |
| 34 | { |
| 35 | int result = 0; |
| 36 | |
| 37 | if (ivt_hdr->magic != IVT_HEADER_MAGIC) |
| 38 | result = ivt_header_error("bad magic", ivt_hdr); |
| 39 | |
| 40 | if (be16_to_cpu(ivt_hdr->length) != IVT_TOTAL_LENGTH) |
| 41 | result = ivt_header_error("bad length", ivt_hdr); |
| 42 | |
| 43 | if (ivt_hdr->version != IVT_HEADER_V1 && |
| 44 | ivt_hdr->version != IVT_HEADER_V2) |
| 45 | result = ivt_header_error("bad version", ivt_hdr); |
| 46 | |
| 47 | return result; |
| 48 | } |
| 49 | |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 50 | #if !defined(CONFIG_SPL_BUILD) |
| 51 | |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 52 | #define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */ |
| 53 | |
| 54 | struct record { |
| 55 | uint8_t tag; /* Tag */ |
| 56 | uint8_t len[2]; /* Length */ |
| 57 | uint8_t par; /* Version */ |
| 58 | uint8_t contents[MAX_RECORD_BYTES];/* Record Data */ |
| 59 | bool any_rec_flag; |
| 60 | }; |
| 61 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 62 | static char *rsn_str[] = { |
| 63 | "RSN = HAB_RSN_ANY (0x00)\n", |
| 64 | "RSN = HAB_ENG_FAIL (0x30)\n", |
| 65 | "RSN = HAB_INV_ADDRESS (0x22)\n", |
| 66 | "RSN = HAB_INV_ASSERTION (0x0C)\n", |
| 67 | "RSN = HAB_INV_CALL (0x28)\n", |
| 68 | "RSN = HAB_INV_CERTIFICATE (0x21)\n", |
| 69 | "RSN = HAB_INV_COMMAND (0x06)\n", |
| 70 | "RSN = HAB_INV_CSF (0x11)\n", |
| 71 | "RSN = HAB_INV_DCD (0x27)\n", |
| 72 | "RSN = HAB_INV_INDEX (0x0F)\n", |
| 73 | "RSN = HAB_INV_IVT (0x05)\n", |
| 74 | "RSN = HAB_INV_KEY (0x1D)\n", |
| 75 | "RSN = HAB_INV_RETURN (0x1E)\n", |
| 76 | "RSN = HAB_INV_SIGNATURE (0x18)\n", |
| 77 | "RSN = HAB_INV_SIZE (0x17)\n", |
| 78 | "RSN = HAB_MEM_FAIL (0x2E)\n", |
| 79 | "RSN = HAB_OVR_COUNT (0x2B)\n", |
| 80 | "RSN = HAB_OVR_STORAGE (0x2D)\n", |
| 81 | "RSN = HAB_UNS_ALGORITHM (0x12)\n", |
| 82 | "RSN = HAB_UNS_COMMAND (0x03)\n", |
| 83 | "RSN = HAB_UNS_ENGINE (0x0A)\n", |
| 84 | "RSN = HAB_UNS_ITEM (0x24)\n", |
| 85 | "RSN = HAB_UNS_KEY (0x1B)\n", |
| 86 | "RSN = HAB_UNS_PROTOCOL (0x14)\n", |
| 87 | "RSN = HAB_UNS_STATE (0x09)\n", |
| 88 | "RSN = INVALID\n", |
| 89 | NULL |
| 90 | }; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 91 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 92 | static char *sts_str[] = { |
| 93 | "STS = HAB_SUCCESS (0xF0)\n", |
| 94 | "STS = HAB_FAILURE (0x33)\n", |
| 95 | "STS = HAB_WARNING (0x69)\n", |
| 96 | "STS = INVALID\n", |
| 97 | NULL |
| 98 | }; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 99 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 100 | static char *eng_str[] = { |
| 101 | "ENG = HAB_ENG_ANY (0x00)\n", |
| 102 | "ENG = HAB_ENG_SCC (0x03)\n", |
| 103 | "ENG = HAB_ENG_RTIC (0x05)\n", |
| 104 | "ENG = HAB_ENG_SAHARA (0x06)\n", |
| 105 | "ENG = HAB_ENG_CSU (0x0A)\n", |
| 106 | "ENG = HAB_ENG_SRTC (0x0C)\n", |
| 107 | "ENG = HAB_ENG_DCP (0x1B)\n", |
| 108 | "ENG = HAB_ENG_CAAM (0x1D)\n", |
| 109 | "ENG = HAB_ENG_SNVS (0x1E)\n", |
| 110 | "ENG = HAB_ENG_OCOTP (0x21)\n", |
| 111 | "ENG = HAB_ENG_DTCP (0x22)\n", |
| 112 | "ENG = HAB_ENG_ROM (0x36)\n", |
| 113 | "ENG = HAB_ENG_HDCP (0x24)\n", |
| 114 | "ENG = HAB_ENG_RTL (0x77)\n", |
| 115 | "ENG = HAB_ENG_SW (0xFF)\n", |
| 116 | "ENG = INVALID\n", |
| 117 | NULL |
| 118 | }; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 119 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 120 | static char *ctx_str[] = { |
| 121 | "CTX = HAB_CTX_ANY(0x00)\n", |
| 122 | "CTX = HAB_CTX_FAB (0xFF)\n", |
| 123 | "CTX = HAB_CTX_ENTRY (0xE1)\n", |
| 124 | "CTX = HAB_CTX_TARGET (0x33)\n", |
| 125 | "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n", |
| 126 | "CTX = HAB_CTX_DCD (0xDD)\n", |
| 127 | "CTX = HAB_CTX_CSF (0xCF)\n", |
| 128 | "CTX = HAB_CTX_COMMAND (0xC0)\n", |
| 129 | "CTX = HAB_CTX_AUT_DAT (0xDB)\n", |
| 130 | "CTX = HAB_CTX_ASSERT (0xA0)\n", |
| 131 | "CTX = HAB_CTX_EXIT (0xEE)\n", |
| 132 | "CTX = INVALID\n", |
| 133 | NULL |
| 134 | }; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 135 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 136 | static uint8_t hab_statuses[5] = { |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 137 | HAB_STS_ANY, |
| 138 | HAB_FAILURE, |
| 139 | HAB_WARNING, |
| 140 | HAB_SUCCESS, |
| 141 | -1 |
| 142 | }; |
| 143 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 144 | static uint8_t hab_reasons[26] = { |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 145 | HAB_RSN_ANY, |
| 146 | HAB_ENG_FAIL, |
| 147 | HAB_INV_ADDRESS, |
| 148 | HAB_INV_ASSERTION, |
| 149 | HAB_INV_CALL, |
| 150 | HAB_INV_CERTIFICATE, |
| 151 | HAB_INV_COMMAND, |
| 152 | HAB_INV_CSF, |
| 153 | HAB_INV_DCD, |
| 154 | HAB_INV_INDEX, |
| 155 | HAB_INV_IVT, |
| 156 | HAB_INV_KEY, |
| 157 | HAB_INV_RETURN, |
| 158 | HAB_INV_SIGNATURE, |
| 159 | HAB_INV_SIZE, |
| 160 | HAB_MEM_FAIL, |
| 161 | HAB_OVR_COUNT, |
| 162 | HAB_OVR_STORAGE, |
| 163 | HAB_UNS_ALGORITHM, |
| 164 | HAB_UNS_COMMAND, |
| 165 | HAB_UNS_ENGINE, |
| 166 | HAB_UNS_ITEM, |
| 167 | HAB_UNS_KEY, |
| 168 | HAB_UNS_PROTOCOL, |
| 169 | HAB_UNS_STATE, |
| 170 | -1 |
| 171 | }; |
| 172 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 173 | static uint8_t hab_contexts[12] = { |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 174 | HAB_CTX_ANY, |
| 175 | HAB_CTX_FAB, |
| 176 | HAB_CTX_ENTRY, |
| 177 | HAB_CTX_TARGET, |
| 178 | HAB_CTX_AUTHENTICATE, |
| 179 | HAB_CTX_DCD, |
| 180 | HAB_CTX_CSF, |
| 181 | HAB_CTX_COMMAND, |
| 182 | HAB_CTX_AUT_DAT, |
| 183 | HAB_CTX_ASSERT, |
| 184 | HAB_CTX_EXIT, |
| 185 | -1 |
| 186 | }; |
| 187 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 188 | static uint8_t hab_engines[16] = { |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 189 | HAB_ENG_ANY, |
| 190 | HAB_ENG_SCC, |
| 191 | HAB_ENG_RTIC, |
| 192 | HAB_ENG_SAHARA, |
| 193 | HAB_ENG_CSU, |
| 194 | HAB_ENG_SRTC, |
| 195 | HAB_ENG_DCP, |
| 196 | HAB_ENG_CAAM, |
| 197 | HAB_ENG_SNVS, |
| 198 | HAB_ENG_OCOTP, |
| 199 | HAB_ENG_DTCP, |
| 200 | HAB_ENG_ROM, |
| 201 | HAB_ENG_HDCP, |
| 202 | HAB_ENG_RTL, |
| 203 | HAB_ENG_SW, |
| 204 | -1 |
| 205 | }; |
| 206 | |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 207 | static inline uint8_t get_idx(uint8_t *list, uint8_t tgt) |
| 208 | { |
| 209 | uint8_t idx = 0; |
| 210 | uint8_t element = list[idx]; |
| 211 | while (element != -1) { |
| 212 | if (element == tgt) |
| 213 | return idx; |
| 214 | element = list[++idx]; |
| 215 | } |
| 216 | return -1; |
| 217 | } |
| 218 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 219 | static void process_event_record(uint8_t *event_data, size_t bytes) |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 220 | { |
| 221 | struct record *rec = (struct record *)event_data; |
| 222 | |
| 223 | printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0])]); |
| 224 | printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1])]); |
| 225 | printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2])]); |
| 226 | printf("%s", eng_str[get_idx(hab_engines, rec->contents[3])]); |
| 227 | } |
| 228 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 229 | static void display_event(uint8_t *event_data, size_t bytes) |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 230 | { |
| 231 | uint32_t i; |
| 232 | |
| 233 | if (!(event_data && bytes > 0)) |
| 234 | return; |
| 235 | |
| 236 | for (i = 0; i < bytes; i++) { |
| 237 | if (i == 0) |
| 238 | printf("\t0x%02x", event_data[i]); |
| 239 | else if ((i % 8) == 0) |
| 240 | printf("\n\t0x%02x", event_data[i]); |
| 241 | else |
| 242 | printf(" 0x%02x", event_data[i]); |
| 243 | } |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 244 | |
| 245 | process_event_record(event_data, bytes); |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 248 | static int get_hab_status(void) |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 249 | { |
| 250 | uint32_t index = 0; /* Loop index */ |
| 251 | uint8_t event_data[128]; /* Event data buffer */ |
| 252 | size_t bytes = sizeof(event_data); /* Event size in bytes */ |
| 253 | enum hab_config config = 0; |
| 254 | enum hab_state state = 0; |
Stefano Babic | f2f07e8 | 2014-06-10 10:26:22 +0200 | [diff] [blame] | 255 | hab_rvt_report_event_t *hab_rvt_report_event; |
| 256 | hab_rvt_report_status_t *hab_rvt_report_status; |
| 257 | |
Breno Lima | 7b889ba | 2018-02-20 01:19:26 +0000 | [diff] [blame] | 258 | hab_rvt_report_event = (hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT; |
| 259 | hab_rvt_report_status = |
| 260 | (hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS; |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 261 | |
Bryan O'Donoghue | e5b30e4 | 2018-01-12 12:40:14 +0000 | [diff] [blame] | 262 | if (imx_hab_is_enabled()) |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 263 | puts("\nSecure boot enabled\n"); |
| 264 | else |
| 265 | puts("\nSecure boot disabled\n"); |
| 266 | |
| 267 | /* Check HAB status */ |
| 268 | if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) { |
| 269 | printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n", |
| 270 | config, state); |
| 271 | |
| 272 | /* Display HAB Error events */ |
| 273 | while (hab_rvt_report_event(HAB_FAILURE, index, event_data, |
| 274 | &bytes) == HAB_SUCCESS) { |
| 275 | puts("\n"); |
| 276 | printf("--------- HAB Event %d -----------------\n", |
| 277 | index + 1); |
| 278 | puts("event data:\n"); |
| 279 | display_event(event_data, bytes); |
| 280 | puts("\n"); |
| 281 | bytes = sizeof(event_data); |
| 282 | index++; |
| 283 | } |
| 284 | } |
| 285 | /* Display message if no HAB events are found */ |
| 286 | else { |
| 287 | printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n", |
| 288 | config, state); |
| 289 | puts("No HAB Events Found!\n\n"); |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 294 | static int do_hab_status(cmd_tbl_t *cmdtp, int flag, int argc, |
| 295 | char * const argv[]) |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 296 | { |
| 297 | if ((argc != 1)) { |
| 298 | cmd_usage(cmdtp); |
| 299 | return 1; |
| 300 | } |
| 301 | |
| 302 | get_hab_status(); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 307 | static ulong get_image_ivt_offset(ulong img_addr) |
| 308 | { |
| 309 | const void *buf; |
| 310 | |
| 311 | buf = map_sysmem(img_addr, 0); |
| 312 | switch (genimg_get_format(buf)) { |
Tom Rini | c76c93a | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 313 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 314 | case IMAGE_FORMAT_LEGACY: |
| 315 | return (image_get_image_size((image_header_t *)img_addr) |
| 316 | + 0x1000 - 1) & ~(0x1000 - 1); |
| 317 | #endif |
| 318 | #if IMAGE_ENABLE_FIT |
| 319 | case IMAGE_FORMAT_FIT: |
| 320 | return (fit_get_size(buf) + 0x1000 - 1) & ~(0x1000 - 1); |
| 321 | #endif |
| 322 | default: |
| 323 | return 0; |
| 324 | } |
| 325 | } |
| 326 | |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 327 | static int do_authenticate_image(cmd_tbl_t *cmdtp, int flag, int argc, |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 328 | char * const argv[]) |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 329 | { |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 330 | ulong addr, length, ivt_offset; |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 331 | int rcode = 0; |
| 332 | |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 333 | if (argc < 3) |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 334 | return CMD_RET_USAGE; |
| 335 | |
| 336 | addr = simple_strtoul(argv[1], NULL, 16); |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 337 | length = simple_strtoul(argv[2], NULL, 16); |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 338 | if (argc == 3) |
| 339 | ivt_offset = get_image_ivt_offset(addr); |
| 340 | else |
| 341 | ivt_offset = simple_strtoul(argv[3], NULL, 16); |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 342 | |
Bryan O'Donoghue | 57f6548 | 2018-01-12 12:40:13 +0000 | [diff] [blame] | 343 | rcode = imx_hab_authenticate_image(addr, length, ivt_offset); |
Bryan O'Donoghue | 9535b39 | 2018-01-12 12:39:56 +0000 | [diff] [blame] | 344 | if (rcode == 0) |
| 345 | rcode = CMD_RET_SUCCESS; |
| 346 | else |
| 347 | rcode = CMD_RET_FAILURE; |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 348 | |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 349 | return rcode; |
| 350 | } |
| 351 | |
Bryan O'Donoghue | 9587b0d | 2018-01-12 12:40:19 +0000 | [diff] [blame] | 352 | static int do_hab_failsafe(cmd_tbl_t *cmdtp, int flag, int argc, |
| 353 | char * const argv[]) |
| 354 | { |
| 355 | hab_rvt_failsafe_t *hab_rvt_failsafe; |
| 356 | |
| 357 | if (argc != 1) { |
| 358 | cmd_usage(cmdtp); |
| 359 | return 1; |
| 360 | } |
| 361 | |
Breno Lima | 7b889ba | 2018-02-20 01:19:26 +0000 | [diff] [blame] | 362 | hab_rvt_failsafe = (hab_rvt_failsafe_t *)HAB_RVT_FAILSAFE; |
Bryan O'Donoghue | 9587b0d | 2018-01-12 12:40:19 +0000 | [diff] [blame] | 363 | hab_rvt_failsafe(); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Sjoerd Simons | b4eca2d | 2019-06-18 23:08:59 +0200 | [diff] [blame] | 368 | static int do_hab_version(cmd_tbl_t *cmdtp, int flag, int argc, |
| 369 | char * const argv[]) |
| 370 | { |
| 371 | struct hab_hdr *hdr = (struct hab_hdr *)HAB_RVT_BASE; |
| 372 | |
| 373 | if (hdr->tag != HAB_TAG_RVT) { |
| 374 | printf("Unexpected header tag: %x\n", hdr->tag); |
| 375 | return CMD_RET_FAILURE; |
| 376 | } |
| 377 | |
| 378 | printf("HAB version: %d.%d\n", hdr->par >> 4, hdr->par & 0xf); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
Bryan O'Donoghue | 49e6242 | 2018-03-26 15:36:46 +0100 | [diff] [blame] | 383 | static int do_authenticate_image_or_failover(cmd_tbl_t *cmdtp, int flag, |
| 384 | int argc, char * const argv[]) |
| 385 | { |
| 386 | int ret = CMD_RET_FAILURE; |
| 387 | |
| 388 | if (argc != 4) { |
| 389 | ret = CMD_RET_USAGE; |
| 390 | goto error; |
| 391 | } |
| 392 | |
| 393 | if (!imx_hab_is_enabled()) { |
| 394 | printf("error: secure boot disabled\n"); |
| 395 | goto error; |
| 396 | } |
| 397 | |
| 398 | if (do_authenticate_image(NULL, flag, argc, argv) != CMD_RET_SUCCESS) { |
| 399 | fprintf(stderr, "authentication fail -> %s %s %s %s\n", |
| 400 | argv[0], argv[1], argv[2], argv[3]); |
| 401 | do_hab_failsafe(0, 0, 1, NULL); |
| 402 | }; |
| 403 | ret = CMD_RET_SUCCESS; |
| 404 | error: |
| 405 | return ret; |
| 406 | } |
| 407 | |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 408 | U_BOOT_CMD( |
| 409 | hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status, |
| 410 | "display HAB status", |
| 411 | "" |
| 412 | ); |
| 413 | |
| 414 | U_BOOT_CMD( |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 415 | hab_auth_img, 4, 0, do_authenticate_image, |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 416 | "authenticate image via HAB", |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 417 | "addr length ivt_offset\n" |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 418 | "addr - image hex address\n" |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 419 | "length - image hex length\n" |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 420 | "ivt_offset - hex offset of IVT in the image" |
| 421 | ); |
| 422 | |
Bryan O'Donoghue | 9587b0d | 2018-01-12 12:40:19 +0000 | [diff] [blame] | 423 | U_BOOT_CMD( |
| 424 | hab_failsafe, CONFIG_SYS_MAXARGS, 1, do_hab_failsafe, |
| 425 | "run BootROM failsafe routine", |
| 426 | "" |
| 427 | ); |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 428 | |
Bryan O'Donoghue | 49e6242 | 2018-03-26 15:36:46 +0100 | [diff] [blame] | 429 | U_BOOT_CMD( |
| 430 | hab_auth_img_or_fail, 4, 0, |
| 431 | do_authenticate_image_or_failover, |
| 432 | "authenticate image via HAB on failure drop to USB BootROM mode", |
| 433 | "addr length ivt_offset\n" |
| 434 | "addr - image hex address\n" |
| 435 | "length - image hex length\n" |
| 436 | "ivt_offset - hex offset of IVT in the image" |
| 437 | ); |
| 438 | |
Sjoerd Simons | b4eca2d | 2019-06-18 23:08:59 +0200 | [diff] [blame] | 439 | U_BOOT_CMD( |
| 440 | hab_version, 1, 0, do_hab_version, |
| 441 | "print HAB major/minor version", |
| 442 | "" |
| 443 | ); |
| 444 | |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 445 | #endif /* !defined(CONFIG_SPL_BUILD) */ |
| 446 | |
Utkarsh Gupta | ed286bc | 2018-02-20 01:19:24 +0000 | [diff] [blame] | 447 | /* Get CSF Header length */ |
| 448 | static int get_hab_hdr_len(struct hab_hdr *hdr) |
| 449 | { |
| 450 | return (size_t)((hdr->len[0] << 8) + (hdr->len[1])); |
| 451 | } |
| 452 | |
| 453 | /* Check whether addr lies between start and |
| 454 | * end and is within the length of the image |
| 455 | */ |
| 456 | static int chk_bounds(u8 *addr, size_t bytes, u8 *start, u8 *end) |
| 457 | { |
| 458 | size_t csf_size = (size_t)((end + 1) - addr); |
| 459 | |
| 460 | return (addr && (addr >= start) && (addr <= end) && |
| 461 | (csf_size >= bytes)); |
| 462 | } |
| 463 | |
| 464 | /* Get Length of each command in CSF */ |
| 465 | static int get_csf_cmd_hdr_len(u8 *csf_hdr) |
| 466 | { |
| 467 | if (*csf_hdr == HAB_CMD_HDR) |
| 468 | return sizeof(struct hab_hdr); |
| 469 | |
| 470 | return get_hab_hdr_len((struct hab_hdr *)csf_hdr); |
| 471 | } |
| 472 | |
| 473 | /* Check if CSF is valid */ |
| 474 | static bool csf_is_valid(struct ivt *ivt, ulong start_addr, size_t bytes) |
| 475 | { |
| 476 | u8 *start = (u8 *)start_addr; |
| 477 | u8 *csf_hdr; |
| 478 | u8 *end; |
| 479 | |
| 480 | size_t csf_hdr_len; |
| 481 | size_t cmd_hdr_len; |
| 482 | size_t offset = 0; |
| 483 | |
| 484 | if (bytes != 0) |
| 485 | end = start + bytes - 1; |
| 486 | else |
| 487 | end = start; |
| 488 | |
| 489 | /* Verify if CSF pointer content is zero */ |
| 490 | if (!ivt->csf) { |
| 491 | puts("Error: CSF pointer is NULL\n"); |
| 492 | return false; |
| 493 | } |
| 494 | |
| 495 | csf_hdr = (u8 *)ivt->csf; |
| 496 | |
| 497 | /* Verify if CSF Header exist */ |
| 498 | if (*csf_hdr != HAB_CMD_HDR) { |
| 499 | puts("Error: CSF header command not found\n"); |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | csf_hdr_len = get_hab_hdr_len((struct hab_hdr *)csf_hdr); |
| 504 | |
| 505 | /* Check if the CSF lies within the image bounds */ |
| 506 | if (!chk_bounds(csf_hdr, csf_hdr_len, start, end)) { |
| 507 | puts("Error: CSF lies outside the image bounds\n"); |
| 508 | return false; |
| 509 | } |
| 510 | |
| 511 | do { |
Utkarsh Gupta | 20fa1dd | 2018-02-20 01:19:25 +0000 | [diff] [blame] | 512 | struct hab_hdr *cmd; |
| 513 | |
| 514 | cmd = (struct hab_hdr *)&csf_hdr[offset]; |
| 515 | |
| 516 | switch (cmd->tag) { |
| 517 | case (HAB_CMD_WRT_DAT): |
| 518 | puts("Error: Deprecated write command found\n"); |
| 519 | return false; |
| 520 | case (HAB_CMD_CHK_DAT): |
| 521 | puts("Error: Deprecated check command found\n"); |
| 522 | return false; |
| 523 | case (HAB_CMD_SET): |
| 524 | if (cmd->par == HAB_PAR_MID) { |
| 525 | puts("Error: Deprecated Set MID command found\n"); |
| 526 | return false; |
| 527 | } |
| 528 | default: |
| 529 | break; |
| 530 | } |
| 531 | |
Utkarsh Gupta | ed286bc | 2018-02-20 01:19:24 +0000 | [diff] [blame] | 532 | cmd_hdr_len = get_csf_cmd_hdr_len(&csf_hdr[offset]); |
| 533 | if (!cmd_hdr_len) { |
| 534 | puts("Error: Invalid command length\n"); |
| 535 | return false; |
| 536 | } |
| 537 | offset += cmd_hdr_len; |
| 538 | |
| 539 | } while (offset < csf_hdr_len); |
| 540 | |
| 541 | return true; |
| 542 | } |
| 543 | |
Bryan O'Donoghue | 07eefaf | 2018-01-12 12:40:16 +0000 | [diff] [blame] | 544 | bool imx_hab_is_enabled(void) |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 545 | { |
| 546 | struct imx_sec_config_fuse_t *fuse = |
| 547 | (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse; |
| 548 | uint32_t reg; |
| 549 | int ret; |
| 550 | |
| 551 | ret = fuse_read(fuse->bank, fuse->word, ®); |
| 552 | if (ret) { |
| 553 | puts("\nSecure boot fuse read error\n"); |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | return (reg & IS_HAB_ENABLED_BIT) == IS_HAB_ENABLED_BIT; |
| 558 | } |
| 559 | |
Bryan O'Donoghue | 57f6548 | 2018-01-12 12:40:13 +0000 | [diff] [blame] | 560 | int imx_hab_authenticate_image(uint32_t ddr_start, uint32_t image_size, |
| 561 | uint32_t ivt_offset) |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 562 | { |
| 563 | uint32_t load_addr = 0; |
| 564 | size_t bytes; |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 565 | uint32_t ivt_addr = 0; |
Bryan O'Donoghue | 9535b39 | 2018-01-12 12:39:56 +0000 | [diff] [blame] | 566 | int result = 1; |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 567 | ulong start; |
| 568 | hab_rvt_authenticate_image_t *hab_rvt_authenticate_image; |
| 569 | hab_rvt_entry_t *hab_rvt_entry; |
| 570 | hab_rvt_exit_t *hab_rvt_exit; |
Bryan O'Donoghue | b7c3cae | 2018-01-12 12:40:10 +0000 | [diff] [blame] | 571 | hab_rvt_check_target_t *hab_rvt_check_target; |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 572 | struct ivt *ivt; |
| 573 | struct ivt_header *ivt_hdr; |
Bryan O'Donoghue | b7c3cae | 2018-01-12 12:40:10 +0000 | [diff] [blame] | 574 | enum hab_status status; |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 575 | |
Breno Lima | 7b889ba | 2018-02-20 01:19:26 +0000 | [diff] [blame] | 576 | hab_rvt_authenticate_image = |
| 577 | (hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE; |
| 578 | hab_rvt_entry = (hab_rvt_entry_t *)HAB_RVT_ENTRY; |
| 579 | hab_rvt_exit = (hab_rvt_exit_t *)HAB_RVT_EXIT; |
| 580 | hab_rvt_check_target = (hab_rvt_check_target_t *)HAB_RVT_CHECK_TARGET; |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 581 | |
Bryan O'Donoghue | e5b30e4 | 2018-01-12 12:40:14 +0000 | [diff] [blame] | 582 | if (!imx_hab_is_enabled()) { |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 583 | puts("hab fuse not enabled\n"); |
Bryan O'Donoghue | 4467ae6 | 2018-01-12 12:40:15 +0000 | [diff] [blame] | 584 | return 0; |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 585 | } |
| 586 | |
Bryan O'Donoghue | d2c6180 | 2018-01-12 12:39:57 +0000 | [diff] [blame] | 587 | printf("\nAuthenticate image from DDR location 0x%x...\n", |
| 588 | ddr_start); |
| 589 | |
| 590 | hab_caam_clock_enable(1); |
| 591 | |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 592 | /* Calculate IVT address header */ |
| 593 | ivt_addr = ddr_start + ivt_offset; |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 594 | ivt = (struct ivt *)ivt_addr; |
| 595 | ivt_hdr = &ivt->hdr; |
| 596 | |
| 597 | /* Verify IVT header bugging out on error */ |
| 598 | if (verify_ivt_header(ivt_hdr)) |
Breno Lima | 669f2d1 | 2018-02-20 01:19:22 +0000 | [diff] [blame] | 599 | goto hab_authentication_exit; |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 600 | |
Bryan O'Donoghue | e59eb9e | 2018-01-12 12:40:04 +0000 | [diff] [blame] | 601 | /* Verify IVT body */ |
| 602 | if (ivt->self != ivt_addr) { |
| 603 | printf("ivt->self 0x%08x pointer is 0x%08x\n", |
| 604 | ivt->self, ivt_addr); |
Breno Lima | 669f2d1 | 2018-02-20 01:19:22 +0000 | [diff] [blame] | 605 | goto hab_authentication_exit; |
Bryan O'Donoghue | e59eb9e | 2018-01-12 12:40:04 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Utkarsh Gupta | 8c4037a | 2018-02-20 01:19:23 +0000 | [diff] [blame] | 608 | /* Verify if IVT DCD pointer is NULL */ |
Breno Matheus Lima | b2ca890 | 2018-12-07 22:31:49 +0000 | [diff] [blame] | 609 | if (ivt->dcd) { |
| 610 | puts("Error: DCD pointer must be NULL\n"); |
| 611 | goto hab_authentication_exit; |
| 612 | } |
Utkarsh Gupta | 8c4037a | 2018-02-20 01:19:23 +0000 | [diff] [blame] | 613 | |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 614 | start = ddr_start; |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 615 | bytes = image_size; |
Bryan O'Donoghue | 04099e9 | 2018-01-12 12:40:05 +0000 | [diff] [blame] | 616 | |
Utkarsh Gupta | ed286bc | 2018-02-20 01:19:24 +0000 | [diff] [blame] | 617 | /* Verify CSF */ |
| 618 | if (!csf_is_valid(ivt, start, bytes)) |
| 619 | goto hab_authentication_exit; |
| 620 | |
Bryan O'Donoghue | 04099e9 | 2018-01-12 12:40:05 +0000 | [diff] [blame] | 621 | if (hab_rvt_entry() != HAB_SUCCESS) { |
| 622 | puts("hab entry function fail\n"); |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 623 | goto hab_exit_failure_print_status; |
Bryan O'Donoghue | 04099e9 | 2018-01-12 12:40:05 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Bryan O'Donoghue | b7c3cae | 2018-01-12 12:40:10 +0000 | [diff] [blame] | 626 | status = hab_rvt_check_target(HAB_TGT_MEMORY, (void *)ddr_start, bytes); |
| 627 | if (status != HAB_SUCCESS) { |
| 628 | printf("HAB check target 0x%08x-0x%08x fail\n", |
| 629 | ddr_start, ddr_start + bytes); |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 630 | goto hab_exit_failure_print_status; |
Bryan O'Donoghue | b7c3cae | 2018-01-12 12:40:10 +0000 | [diff] [blame] | 631 | } |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 632 | #ifdef DEBUG |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 633 | printf("\nivt_offset = 0x%x, ivt addr = 0x%x\n", ivt_offset, ivt_addr); |
Bryan O'Donoghue | 824ef30 | 2018-01-12 12:40:07 +0000 | [diff] [blame] | 634 | printf("ivt entry = 0x%08x, dcd = 0x%08x, csf = 0x%08x\n", ivt->entry, |
| 635 | ivt->dcd, ivt->csf); |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 636 | puts("Dumping IVT\n"); |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 637 | print_buffer(ivt_addr, (void *)(ivt_addr), 4, 0x8, 0); |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 638 | |
| 639 | puts("Dumping CSF Header\n"); |
Bryan O'Donoghue | fd15fe5 | 2018-01-12 12:40:06 +0000 | [diff] [blame] | 640 | print_buffer(ivt->csf, (void *)(ivt->csf), 4, 0x10, 0); |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 641 | |
| 642 | #if !defined(CONFIG_SPL_BUILD) |
| 643 | get_hab_status(); |
| 644 | #endif |
| 645 | |
| 646 | puts("\nCalling authenticate_image in ROM\n"); |
| 647 | printf("\tivt_offset = 0x%x\n", ivt_offset); |
| 648 | printf("\tstart = 0x%08lx\n", start); |
| 649 | printf("\tbytes = 0x%x\n", bytes); |
| 650 | #endif |
| 651 | /* |
| 652 | * If the MMU is enabled, we have to notify the ROM |
| 653 | * code, or it won't flush the caches when needed. |
| 654 | * This is done, by setting the "pu_irom_mmu_enabled" |
| 655 | * word to 1. You can find its address by looking in |
| 656 | * the ROM map. This is critical for |
| 657 | * authenticate_image(). If MMU is enabled, without |
| 658 | * setting this bit, authentication will fail and may |
| 659 | * crash. |
| 660 | */ |
| 661 | /* Check MMU enabled */ |
| 662 | if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) { |
| 663 | if (is_mx6dq()) { |
| 664 | /* |
| 665 | * This won't work on Rev 1.0.0 of |
| 666 | * i.MX6Q/D, since their ROM doesn't |
| 667 | * do cache flushes. don't think any |
| 668 | * exist, so we ignore them. |
| 669 | */ |
| 670 | if (!is_mx6dqp()) |
| 671 | writel(1, MX6DQ_PU_IROM_MMU_EN_VAR); |
| 672 | } else if (is_mx6sdl()) { |
| 673 | writel(1, MX6DLS_PU_IROM_MMU_EN_VAR); |
| 674 | } else if (is_mx6sl()) { |
| 675 | writel(1, MX6SL_PU_IROM_MMU_EN_VAR); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | load_addr = (uint32_t)hab_rvt_authenticate_image( |
| 680 | HAB_CID_UBOOT, |
| 681 | ivt_offset, (void **)&start, |
| 682 | (size_t *)&bytes, NULL); |
| 683 | if (hab_rvt_exit() != HAB_SUCCESS) { |
| 684 | puts("hab exit function fail\n"); |
| 685 | load_addr = 0; |
| 686 | } |
| 687 | |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 688 | hab_exit_failure_print_status: |
Bryan O'Donoghue | d2c6180 | 2018-01-12 12:39:57 +0000 | [diff] [blame] | 689 | #if !defined(CONFIG_SPL_BUILD) |
| 690 | get_hab_status(); |
| 691 | #endif |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 692 | |
Breno Lima | 669f2d1 | 2018-02-20 01:19:22 +0000 | [diff] [blame] | 693 | hab_authentication_exit: |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 694 | |
Bryan O'Donoghue | d2c6180 | 2018-01-12 12:39:57 +0000 | [diff] [blame] | 695 | if (load_addr != 0) |
Bryan O'Donoghue | 9535b39 | 2018-01-12 12:39:56 +0000 | [diff] [blame] | 696 | result = 0; |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 697 | |
| 698 | return result; |
| 699 | } |