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