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> |
Simon Glass | 4e4bf94 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 9 | #include <display_options.h> |
Adrian Alonso | fba6f9e | 2015-10-12 13:48:14 -0500 | [diff] [blame] | 10 | #include <fuse.h> |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 11 | #include <mapmem.h> |
| 12 | #include <image.h> |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 15 | #include <asm/system.h> |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 16 | #include <asm/arch/clock.h> |
Stefano Babic | f2f07e8 | 2014-06-10 10:26:22 +0200 | [diff] [blame] | 17 | #include <asm/arch/sys_proto.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 18 | #include <asm/mach-imx/hab.h> |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 19 | #include <linux/arm-smccc.h> |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 22 | |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 23 | #define ALIGN_SIZE 0x1000 |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 24 | #define MX6DQ_PU_IROM_MMU_EN_VAR 0x009024a8 |
| 25 | #define MX6DLS_PU_IROM_MMU_EN_VAR 0x00901dd0 |
Breno Matheus Lima | 5760069 | 2019-05-07 20:19:17 +0000 | [diff] [blame] | 26 | #define MX6SL_PU_IROM_MMU_EN_VAR 0x00901c60 |
Adrian Alonso | ee3899a | 2015-10-12 13:48:15 -0500 | [diff] [blame] | 27 | #define IS_HAB_ENABLED_BIT \ |
Peng Fan | 27117b2 | 2017-02-22 16:21:53 +0800 | [diff] [blame] | 28 | (is_soc_type(MXC_SOC_MX7ULP) ? 0x80000000 : \ |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 29 | ((is_soc_type(MXC_SOC_MX7) || is_soc_type(MXC_SOC_IMX8M)) ? 0x2000000 : 0x2)) |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 30 | |
Breno Lima | 58f75ef | 2021-03-25 17:30:13 +0800 | [diff] [blame] | 31 | #ifdef CONFIG_MX7ULP |
| 32 | #define HAB_M4_PERSISTENT_START ((soc_rev() >= CHIP_REV_2_0) ? 0x20008040 : \ |
| 33 | 0x20008180) |
| 34 | #define HAB_M4_PERSISTENT_BYTES 0xB80 |
| 35 | #endif |
| 36 | |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 37 | static int ivt_header_error(const char *err_str, struct ivt_header *ivt_hdr) |
| 38 | { |
| 39 | printf("%s magic=0x%x length=0x%02x version=0x%x\n", err_str, |
| 40 | ivt_hdr->magic, ivt_hdr->length, ivt_hdr->version); |
| 41 | |
| 42 | return 1; |
| 43 | } |
| 44 | |
| 45 | static int verify_ivt_header(struct ivt_header *ivt_hdr) |
| 46 | { |
| 47 | int result = 0; |
| 48 | |
| 49 | if (ivt_hdr->magic != IVT_HEADER_MAGIC) |
| 50 | result = ivt_header_error("bad magic", ivt_hdr); |
| 51 | |
| 52 | if (be16_to_cpu(ivt_hdr->length) != IVT_TOTAL_LENGTH) |
| 53 | result = ivt_header_error("bad length", ivt_hdr); |
| 54 | |
Breno Lima | f217470 | 2021-03-25 17:30:12 +0800 | [diff] [blame] | 55 | if ((ivt_hdr->version & HAB_MAJ_MASK) != HAB_MAJ_VER) |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 56 | result = ivt_header_error("bad version", ivt_hdr); |
| 57 | |
| 58 | return result; |
| 59 | } |
| 60 | |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 61 | #ifdef CONFIG_ARM64 |
| 62 | #define FSL_SIP_HAB 0xC2000007 |
| 63 | #define FSL_SIP_HAB_AUTHENTICATE 0x00 |
| 64 | #define FSL_SIP_HAB_ENTRY 0x01 |
| 65 | #define FSL_SIP_HAB_EXIT 0x02 |
| 66 | #define FSL_SIP_HAB_REPORT_EVENT 0x03 |
| 67 | #define FSL_SIP_HAB_REPORT_STATUS 0x04 |
| 68 | #define FSL_SIP_HAB_FAILSAFE 0x05 |
| 69 | #define FSL_SIP_HAB_CHECK_TARGET 0x06 |
| 70 | static volatile gd_t *gd_save; |
| 71 | #endif |
| 72 | |
| 73 | static inline void save_gd(void) |
| 74 | { |
| 75 | #ifdef CONFIG_ARM64 |
| 76 | gd_save = gd; |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | static inline void restore_gd(void) |
| 81 | { |
| 82 | #ifdef CONFIG_ARM64 |
| 83 | /* |
| 84 | * Make will already error that reserving x18 is not supported at the |
| 85 | * time of writing, clang: error: unknown argument: '-ffixed-x18' |
| 86 | */ |
| 87 | __asm__ volatile("mov x18, %0\n" : : "r" (gd_save)); |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | enum hab_status hab_rvt_report_event(enum hab_status status, u32 index, |
| 92 | u8 *event, size_t *bytes) |
| 93 | { |
| 94 | enum hab_status ret; |
| 95 | hab_rvt_report_event_t *hab_rvt_report_event_func; |
| 96 | struct arm_smccc_res res __maybe_unused; |
| 97 | |
| 98 | hab_rvt_report_event_func = (hab_rvt_report_event_t *)HAB_RVT_REPORT_EVENT; |
| 99 | #if defined(CONFIG_ARM64) |
| 100 | if (current_el() != 3) { |
| 101 | /* call sip */ |
| 102 | arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_REPORT_EVENT, (unsigned long)index, |
| 103 | (unsigned long)event, (unsigned long)bytes, 0, 0, 0, &res); |
| 104 | return (enum hab_status)res.a0; |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | save_gd(); |
| 109 | ret = hab_rvt_report_event_func(status, index, event, bytes); |
| 110 | restore_gd(); |
| 111 | |
| 112 | return ret; |
| 113 | |
| 114 | } |
| 115 | |
| 116 | enum hab_status hab_rvt_report_status(enum hab_config *config, enum hab_state *state) |
| 117 | { |
| 118 | enum hab_status ret; |
| 119 | hab_rvt_report_status_t *hab_rvt_report_status_func; |
| 120 | struct arm_smccc_res res __maybe_unused; |
| 121 | |
| 122 | hab_rvt_report_status_func = (hab_rvt_report_status_t *)HAB_RVT_REPORT_STATUS; |
| 123 | #if defined(CONFIG_ARM64) |
| 124 | if (current_el() != 3) { |
| 125 | /* call sip */ |
| 126 | arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_REPORT_STATUS, (unsigned long)config, |
| 127 | (unsigned long)state, 0, 0, 0, 0, &res); |
| 128 | return (enum hab_status)res.a0; |
| 129 | } |
| 130 | #endif |
| 131 | |
| 132 | save_gd(); |
| 133 | ret = hab_rvt_report_status_func(config, state); |
| 134 | restore_gd(); |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | enum hab_status hab_rvt_entry(void) |
| 140 | { |
| 141 | enum hab_status ret; |
| 142 | hab_rvt_entry_t *hab_rvt_entry_func; |
| 143 | struct arm_smccc_res res __maybe_unused; |
| 144 | |
| 145 | hab_rvt_entry_func = (hab_rvt_entry_t *)HAB_RVT_ENTRY; |
| 146 | #if defined(CONFIG_ARM64) |
| 147 | if (current_el() != 3) { |
| 148 | /* call sip */ |
| 149 | arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_ENTRY, 0, 0, 0, 0, 0, 0, &res); |
| 150 | return (enum hab_status)res.a0; |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | save_gd(); |
| 155 | ret = hab_rvt_entry_func(); |
| 156 | restore_gd(); |
| 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | enum hab_status hab_rvt_exit(void) |
| 162 | { |
| 163 | enum hab_status ret; |
| 164 | hab_rvt_exit_t *hab_rvt_exit_func; |
| 165 | struct arm_smccc_res res __maybe_unused; |
| 166 | |
| 167 | hab_rvt_exit_func = (hab_rvt_exit_t *)HAB_RVT_EXIT; |
| 168 | #if defined(CONFIG_ARM64) |
| 169 | if (current_el() != 3) { |
| 170 | /* call sip */ |
| 171 | arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_EXIT, 0, 0, 0, 0, 0, 0, &res); |
| 172 | return (enum hab_status)res.a0; |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | save_gd(); |
| 177 | ret = hab_rvt_exit_func(); |
| 178 | restore_gd(); |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | void hab_rvt_failsafe(void) |
| 184 | { |
| 185 | hab_rvt_failsafe_t *hab_rvt_failsafe_func; |
| 186 | |
| 187 | hab_rvt_failsafe_func = (hab_rvt_failsafe_t *)HAB_RVT_FAILSAFE; |
| 188 | #if defined(CONFIG_ARM64) |
| 189 | if (current_el() != 3) { |
| 190 | /* call sip */ |
| 191 | arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_FAILSAFE, 0, 0, 0, 0, 0, 0, NULL); |
| 192 | return; |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | save_gd(); |
| 197 | hab_rvt_failsafe_func(); |
| 198 | restore_gd(); |
| 199 | } |
| 200 | |
| 201 | enum hab_status hab_rvt_check_target(enum hab_target type, const void *start, |
| 202 | size_t bytes) |
| 203 | { |
| 204 | enum hab_status ret; |
| 205 | hab_rvt_check_target_t *hab_rvt_check_target_func; |
| 206 | struct arm_smccc_res res __maybe_unused; |
| 207 | |
| 208 | hab_rvt_check_target_func = (hab_rvt_check_target_t *)HAB_RVT_CHECK_TARGET; |
| 209 | #if defined(CONFIG_ARM64) |
| 210 | if (current_el() != 3) { |
| 211 | /* call sip */ |
| 212 | arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_CHECK_TARGET, (unsigned long)type, |
| 213 | (unsigned long)start, (unsigned long)bytes, 0, 0, 0, &res); |
| 214 | return (enum hab_status)res.a0; |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | save_gd(); |
| 219 | ret = hab_rvt_check_target_func(type, start, bytes); |
| 220 | restore_gd(); |
| 221 | |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | void *hab_rvt_authenticate_image(uint8_t cid, ptrdiff_t ivt_offset, |
| 226 | void **start, size_t *bytes, hab_loader_callback_f_t loader) |
| 227 | { |
| 228 | void *ret; |
| 229 | hab_rvt_authenticate_image_t *hab_rvt_authenticate_image_func; |
| 230 | struct arm_smccc_res res __maybe_unused; |
| 231 | |
| 232 | hab_rvt_authenticate_image_func = (hab_rvt_authenticate_image_t *)HAB_RVT_AUTHENTICATE_IMAGE; |
| 233 | #if defined(CONFIG_ARM64) |
| 234 | if (current_el() != 3) { |
| 235 | /* call sip */ |
| 236 | arm_smccc_smc(FSL_SIP_HAB, FSL_SIP_HAB_AUTHENTICATE, (unsigned long)ivt_offset, |
| 237 | (unsigned long)start, (unsigned long)bytes, 0, 0, 0, &res); |
| 238 | return (void *)res.a0; |
| 239 | } |
| 240 | #endif |
| 241 | |
| 242 | save_gd(); |
| 243 | ret = hab_rvt_authenticate_image_func(cid, ivt_offset, start, bytes, loader); |
| 244 | restore_gd(); |
| 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 249 | #if !defined(CONFIG_SPL_BUILD) |
| 250 | |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 251 | #define MAX_RECORD_BYTES (8*1024) /* 4 kbytes */ |
| 252 | |
| 253 | struct record { |
| 254 | uint8_t tag; /* Tag */ |
| 255 | uint8_t len[2]; /* Length */ |
| 256 | uint8_t par; /* Version */ |
| 257 | uint8_t contents[MAX_RECORD_BYTES];/* Record Data */ |
| 258 | bool any_rec_flag; |
| 259 | }; |
| 260 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 261 | static char *rsn_str[] = { |
| 262 | "RSN = HAB_RSN_ANY (0x00)\n", |
| 263 | "RSN = HAB_ENG_FAIL (0x30)\n", |
| 264 | "RSN = HAB_INV_ADDRESS (0x22)\n", |
| 265 | "RSN = HAB_INV_ASSERTION (0x0C)\n", |
| 266 | "RSN = HAB_INV_CALL (0x28)\n", |
| 267 | "RSN = HAB_INV_CERTIFICATE (0x21)\n", |
| 268 | "RSN = HAB_INV_COMMAND (0x06)\n", |
| 269 | "RSN = HAB_INV_CSF (0x11)\n", |
| 270 | "RSN = HAB_INV_DCD (0x27)\n", |
| 271 | "RSN = HAB_INV_INDEX (0x0F)\n", |
| 272 | "RSN = HAB_INV_IVT (0x05)\n", |
| 273 | "RSN = HAB_INV_KEY (0x1D)\n", |
| 274 | "RSN = HAB_INV_RETURN (0x1E)\n", |
| 275 | "RSN = HAB_INV_SIGNATURE (0x18)\n", |
| 276 | "RSN = HAB_INV_SIZE (0x17)\n", |
| 277 | "RSN = HAB_MEM_FAIL (0x2E)\n", |
| 278 | "RSN = HAB_OVR_COUNT (0x2B)\n", |
| 279 | "RSN = HAB_OVR_STORAGE (0x2D)\n", |
| 280 | "RSN = HAB_UNS_ALGORITHM (0x12)\n", |
| 281 | "RSN = HAB_UNS_COMMAND (0x03)\n", |
| 282 | "RSN = HAB_UNS_ENGINE (0x0A)\n", |
| 283 | "RSN = HAB_UNS_ITEM (0x24)\n", |
| 284 | "RSN = HAB_UNS_KEY (0x1B)\n", |
| 285 | "RSN = HAB_UNS_PROTOCOL (0x14)\n", |
| 286 | "RSN = HAB_UNS_STATE (0x09)\n", |
| 287 | "RSN = INVALID\n", |
| 288 | NULL |
| 289 | }; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 290 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 291 | static char *sts_str[] = { |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 292 | "STS = HAB_STS_ANY (0x00)\n", |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 293 | "STS = HAB_FAILURE (0x33)\n", |
| 294 | "STS = HAB_WARNING (0x69)\n", |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 295 | "STS = HAB_SUCCESS (0xF0)\n", |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 296 | "STS = INVALID\n", |
| 297 | NULL |
| 298 | }; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 299 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 300 | static char *eng_str[] = { |
| 301 | "ENG = HAB_ENG_ANY (0x00)\n", |
| 302 | "ENG = HAB_ENG_SCC (0x03)\n", |
| 303 | "ENG = HAB_ENG_RTIC (0x05)\n", |
| 304 | "ENG = HAB_ENG_SAHARA (0x06)\n", |
| 305 | "ENG = HAB_ENG_CSU (0x0A)\n", |
| 306 | "ENG = HAB_ENG_SRTC (0x0C)\n", |
| 307 | "ENG = HAB_ENG_DCP (0x1B)\n", |
| 308 | "ENG = HAB_ENG_CAAM (0x1D)\n", |
| 309 | "ENG = HAB_ENG_SNVS (0x1E)\n", |
| 310 | "ENG = HAB_ENG_OCOTP (0x21)\n", |
| 311 | "ENG = HAB_ENG_DTCP (0x22)\n", |
| 312 | "ENG = HAB_ENG_ROM (0x36)\n", |
| 313 | "ENG = HAB_ENG_HDCP (0x24)\n", |
| 314 | "ENG = HAB_ENG_RTL (0x77)\n", |
| 315 | "ENG = HAB_ENG_SW (0xFF)\n", |
| 316 | "ENG = INVALID\n", |
| 317 | NULL |
| 318 | }; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 319 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 320 | static char *ctx_str[] = { |
| 321 | "CTX = HAB_CTX_ANY(0x00)\n", |
| 322 | "CTX = HAB_CTX_FAB (0xFF)\n", |
| 323 | "CTX = HAB_CTX_ENTRY (0xE1)\n", |
| 324 | "CTX = HAB_CTX_TARGET (0x33)\n", |
| 325 | "CTX = HAB_CTX_AUTHENTICATE (0x0A)\n", |
| 326 | "CTX = HAB_CTX_DCD (0xDD)\n", |
| 327 | "CTX = HAB_CTX_CSF (0xCF)\n", |
| 328 | "CTX = HAB_CTX_COMMAND (0xC0)\n", |
| 329 | "CTX = HAB_CTX_AUT_DAT (0xDB)\n", |
| 330 | "CTX = HAB_CTX_ASSERT (0xA0)\n", |
| 331 | "CTX = HAB_CTX_EXIT (0xEE)\n", |
| 332 | "CTX = INVALID\n", |
| 333 | NULL |
| 334 | }; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 335 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 336 | static uint8_t hab_statuses[5] = { |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 337 | HAB_STS_ANY, |
| 338 | HAB_FAILURE, |
| 339 | HAB_WARNING, |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 340 | HAB_SUCCESS |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 341 | }; |
| 342 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 343 | static uint8_t hab_reasons[26] = { |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 344 | HAB_RSN_ANY, |
| 345 | HAB_ENG_FAIL, |
| 346 | HAB_INV_ADDRESS, |
| 347 | HAB_INV_ASSERTION, |
| 348 | HAB_INV_CALL, |
| 349 | HAB_INV_CERTIFICATE, |
| 350 | HAB_INV_COMMAND, |
| 351 | HAB_INV_CSF, |
| 352 | HAB_INV_DCD, |
| 353 | HAB_INV_INDEX, |
| 354 | HAB_INV_IVT, |
| 355 | HAB_INV_KEY, |
| 356 | HAB_INV_RETURN, |
| 357 | HAB_INV_SIGNATURE, |
| 358 | HAB_INV_SIZE, |
| 359 | HAB_MEM_FAIL, |
| 360 | HAB_OVR_COUNT, |
| 361 | HAB_OVR_STORAGE, |
| 362 | HAB_UNS_ALGORITHM, |
| 363 | HAB_UNS_COMMAND, |
| 364 | HAB_UNS_ENGINE, |
| 365 | HAB_UNS_ITEM, |
| 366 | HAB_UNS_KEY, |
| 367 | HAB_UNS_PROTOCOL, |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 368 | HAB_UNS_STATE |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 369 | }; |
| 370 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 371 | static uint8_t hab_contexts[12] = { |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 372 | HAB_CTX_ANY, |
| 373 | HAB_CTX_FAB, |
| 374 | HAB_CTX_ENTRY, |
| 375 | HAB_CTX_TARGET, |
| 376 | HAB_CTX_AUTHENTICATE, |
| 377 | HAB_CTX_DCD, |
| 378 | HAB_CTX_CSF, |
| 379 | HAB_CTX_COMMAND, |
| 380 | HAB_CTX_AUT_DAT, |
| 381 | HAB_CTX_ASSERT, |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 382 | HAB_CTX_EXIT |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 383 | }; |
| 384 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 385 | static uint8_t hab_engines[16] = { |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 386 | HAB_ENG_ANY, |
| 387 | HAB_ENG_SCC, |
| 388 | HAB_ENG_RTIC, |
| 389 | HAB_ENG_SAHARA, |
| 390 | HAB_ENG_CSU, |
| 391 | HAB_ENG_SRTC, |
| 392 | HAB_ENG_DCP, |
| 393 | HAB_ENG_CAAM, |
| 394 | HAB_ENG_SNVS, |
| 395 | HAB_ENG_OCOTP, |
| 396 | HAB_ENG_DTCP, |
| 397 | HAB_ENG_ROM, |
| 398 | HAB_ENG_HDCP, |
| 399 | HAB_ENG_RTL, |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 400 | HAB_ENG_SW |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 401 | }; |
| 402 | |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 403 | static inline u32 get_idx(u8 *list, u8 tgt, u32 size) |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 404 | { |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 405 | u32 idx = 0; |
| 406 | u8 element; |
| 407 | |
| 408 | while (idx < size) { |
| 409 | element = list[idx]; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 410 | if (element == tgt) |
| 411 | return idx; |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 412 | ++idx; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 413 | } |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 414 | return idx; |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 415 | } |
| 416 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 417 | static void process_event_record(uint8_t *event_data, size_t bytes) |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 418 | { |
| 419 | struct record *rec = (struct record *)event_data; |
| 420 | |
Ye Li | b645f95 | 2023-06-15 18:09:15 +0800 | [diff] [blame] | 421 | printf("\n\n%s", sts_str[get_idx(hab_statuses, rec->contents[0], |
| 422 | ARRAY_SIZE(hab_statuses))]); |
| 423 | printf("%s", rsn_str[get_idx(hab_reasons, rec->contents[1], |
| 424 | ARRAY_SIZE(hab_reasons))]); |
| 425 | printf("%s", ctx_str[get_idx(hab_contexts, rec->contents[2], |
| 426 | ARRAY_SIZE(hab_contexts))]); |
| 427 | printf("%s", eng_str[get_idx(hab_engines, rec->contents[3], |
| 428 | ARRAY_SIZE(hab_engines))]); |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 429 | } |
| 430 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 431 | static void display_event(uint8_t *event_data, size_t bytes) |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 432 | { |
| 433 | uint32_t i; |
| 434 | |
| 435 | if (!(event_data && bytes > 0)) |
| 436 | return; |
| 437 | |
| 438 | for (i = 0; i < bytes; i++) { |
| 439 | if (i == 0) |
| 440 | printf("\t0x%02x", event_data[i]); |
| 441 | else if ((i % 8) == 0) |
| 442 | printf("\n\t0x%02x", event_data[i]); |
| 443 | else |
| 444 | printf(" 0x%02x", event_data[i]); |
| 445 | } |
Ulises Cardenas | 29067ab | 2015-07-02 21:26:30 -0500 | [diff] [blame] | 446 | |
| 447 | process_event_record(event_data, bytes); |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Bryan O'Donoghue | 58bebfb | 2018-01-12 12:40:12 +0000 | [diff] [blame] | 450 | static int get_hab_status(void) |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 451 | { |
| 452 | uint32_t index = 0; /* Loop index */ |
| 453 | uint8_t event_data[128]; /* Event data buffer */ |
| 454 | size_t bytes = sizeof(event_data); /* Event size in bytes */ |
| 455 | enum hab_config config = 0; |
| 456 | enum hab_state state = 0; |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 457 | |
Bryan O'Donoghue | e5b30e4 | 2018-01-12 12:40:14 +0000 | [diff] [blame] | 458 | if (imx_hab_is_enabled()) |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 459 | puts("\nSecure boot enabled\n"); |
| 460 | else |
| 461 | puts("\nSecure boot disabled\n"); |
| 462 | |
| 463 | /* Check HAB status */ |
| 464 | if (hab_rvt_report_status(&config, &state) != HAB_SUCCESS) { |
| 465 | printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n", |
| 466 | config, state); |
| 467 | |
Utkarsh Gupta | cd83556 | 2021-03-25 17:30:11 +0800 | [diff] [blame] | 468 | /* Display HAB events */ |
| 469 | while (hab_rvt_report_event(HAB_STS_ANY, index, event_data, |
Stefano Babic | b83c709 | 2013-06-28 00:20:21 +0200 | [diff] [blame] | 470 | &bytes) == HAB_SUCCESS) { |
| 471 | puts("\n"); |
| 472 | printf("--------- HAB Event %d -----------------\n", |
| 473 | index + 1); |
| 474 | puts("event data:\n"); |
| 475 | display_event(event_data, bytes); |
| 476 | puts("\n"); |
| 477 | bytes = sizeof(event_data); |
| 478 | index++; |
| 479 | } |
| 480 | } |
| 481 | /* Display message if no HAB events are found */ |
| 482 | else { |
| 483 | printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n", |
| 484 | config, state); |
| 485 | puts("No HAB Events Found!\n\n"); |
| 486 | } |
| 487 | return 0; |
| 488 | } |
| 489 | |
Breno Lima | 58f75ef | 2021-03-25 17:30:13 +0800 | [diff] [blame] | 490 | #ifdef CONFIG_MX7ULP |
| 491 | |
| 492 | static int get_record_len(struct record *rec) |
| 493 | { |
| 494 | return (size_t)((rec->len[0] << 8) + (rec->len[1])); |
| 495 | } |
| 496 | |
| 497 | static int get_hab_status_m4(void) |
| 498 | { |
| 499 | unsigned int index = 0; |
| 500 | uint8_t event_data[128]; |
| 501 | size_t record_len, offset = 0; |
| 502 | enum hab_config config = 0; |
| 503 | enum hab_state state = 0; |
| 504 | |
| 505 | if (imx_hab_is_enabled()) |
| 506 | puts("\nSecure boot enabled\n"); |
| 507 | else |
| 508 | puts("\nSecure boot disabled\n"); |
| 509 | |
| 510 | /* |
| 511 | * HAB in both A7 and M4 gather the security state |
| 512 | * and configuration of the chip from |
| 513 | * shared SNVS module |
| 514 | */ |
| 515 | hab_rvt_report_status(&config, &state); |
| 516 | printf("\nHAB Configuration: 0x%02x, HAB State: 0x%02x\n", |
| 517 | config, state); |
| 518 | |
| 519 | struct record *rec = (struct record *)(HAB_M4_PERSISTENT_START); |
| 520 | |
| 521 | record_len = get_record_len(rec); |
| 522 | |
| 523 | /* Check if HAB persistent memory is valid */ |
| 524 | if (rec->tag != HAB_TAG_EVT_DEF || |
| 525 | record_len != sizeof(struct evt_def) || |
| 526 | (rec->par & HAB_MAJ_MASK) != HAB_MAJ_VER) { |
| 527 | puts("\nERROR: Invalid HAB persistent memory\n"); |
| 528 | return 1; |
| 529 | } |
| 530 | |
| 531 | /* Parse events in HAB M4 persistent memory region */ |
| 532 | while (offset < HAB_M4_PERSISTENT_BYTES) { |
| 533 | rec = (struct record *)(HAB_M4_PERSISTENT_START + offset); |
| 534 | |
| 535 | record_len = get_record_len(rec); |
| 536 | |
| 537 | if (rec->tag == HAB_TAG_EVT) { |
| 538 | memcpy(&event_data, rec, record_len); |
| 539 | puts("\n"); |
| 540 | printf("--------- HAB Event %d -----------------\n", |
| 541 | index + 1); |
| 542 | puts("event data:\n"); |
| 543 | display_event(event_data, record_len); |
| 544 | puts("\n"); |
| 545 | index++; |
| 546 | } |
| 547 | |
| 548 | offset += record_len; |
| 549 | |
| 550 | /* Ensure all records start on a word boundary */ |
| 551 | if ((offset % 4) != 0) |
| 552 | offset = offset + (4 - (offset % 4)); |
| 553 | } |
| 554 | |
| 555 | if (!index) |
| 556 | puts("No HAB Events Found!\n\n"); |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | #endif |
| 561 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 562 | static int do_hab_status(struct cmd_tbl *cmdtp, int flag, int argc, |
| 563 | char *const argv[]) |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 564 | { |
Breno Lima | 58f75ef | 2021-03-25 17:30:13 +0800 | [diff] [blame] | 565 | #ifdef CONFIG_MX7ULP |
| 566 | if ((argc > 2)) { |
| 567 | cmd_usage(cmdtp); |
| 568 | return 1; |
| 569 | } |
| 570 | |
| 571 | if (strcmp("m4", argv[1]) == 0) |
| 572 | get_hab_status_m4(); |
| 573 | else |
| 574 | get_hab_status(); |
| 575 | #else |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 576 | if ((argc != 1)) { |
| 577 | cmd_usage(cmdtp); |
| 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | get_hab_status(); |
Breno Lima | 58f75ef | 2021-03-25 17:30:13 +0800 | [diff] [blame] | 582 | #endif |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 587 | static ulong get_image_ivt_offset(ulong img_addr) |
| 588 | { |
| 589 | const void *buf; |
| 590 | |
| 591 | buf = map_sysmem(img_addr, 0); |
| 592 | switch (genimg_get_format(buf)) { |
Tom Rini | c76c93a | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 593 | #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT) |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 594 | case IMAGE_FORMAT_LEGACY: |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 595 | return (image_get_image_size((struct legacy_img_hdr *)img_addr) |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 596 | + 0x1000 - 1) & ~(0x1000 - 1); |
| 597 | #endif |
Simon Glass | bf371b4 | 2021-09-25 19:43:20 -0600 | [diff] [blame] | 598 | #if CONFIG_IS_ENABLED(FIT) |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 599 | case IMAGE_FORMAT_FIT: |
| 600 | return (fit_get_size(buf) + 0x1000 - 1) & ~(0x1000 - 1); |
| 601 | #endif |
| 602 | default: |
| 603 | return 0; |
| 604 | } |
| 605 | } |
| 606 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 607 | static int do_authenticate_image(struct cmd_tbl *cmdtp, int flag, int argc, |
| 608 | char *const argv[]) |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 609 | { |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 610 | ulong addr, length, ivt_offset; |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 611 | int rcode = 0; |
| 612 | |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 613 | if (argc < 3) |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 614 | return CMD_RET_USAGE; |
| 615 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 616 | addr = hextoul(argv[1], NULL); |
| 617 | length = hextoul(argv[2], NULL); |
Parthiban Nallathambi | ea91031 | 2018-11-21 14:50:40 +0100 | [diff] [blame] | 618 | if (argc == 3) |
| 619 | ivt_offset = get_image_ivt_offset(addr); |
| 620 | else |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 621 | ivt_offset = hextoul(argv[3], NULL); |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 622 | |
Bryan O'Donoghue | 57f6548 | 2018-01-12 12:40:13 +0000 | [diff] [blame] | 623 | rcode = imx_hab_authenticate_image(addr, length, ivt_offset); |
Bryan O'Donoghue | 9535b39 | 2018-01-12 12:39:56 +0000 | [diff] [blame] | 624 | if (rcode == 0) |
| 625 | rcode = CMD_RET_SUCCESS; |
| 626 | else |
| 627 | rcode = CMD_RET_FAILURE; |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 628 | |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 629 | return rcode; |
| 630 | } |
| 631 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 632 | static int do_hab_failsafe(struct cmd_tbl *cmdtp, int flag, int argc, |
| 633 | char *const argv[]) |
Bryan O'Donoghue | 9587b0d | 2018-01-12 12:40:19 +0000 | [diff] [blame] | 634 | { |
Bryan O'Donoghue | 9587b0d | 2018-01-12 12:40:19 +0000 | [diff] [blame] | 635 | if (argc != 1) { |
| 636 | cmd_usage(cmdtp); |
| 637 | return 1; |
| 638 | } |
| 639 | |
Bryan O'Donoghue | 9587b0d | 2018-01-12 12:40:19 +0000 | [diff] [blame] | 640 | hab_rvt_failsafe(); |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 645 | static int do_hab_version(struct cmd_tbl *cmdtp, int flag, int argc, |
| 646 | char *const argv[]) |
Sjoerd Simons | b4eca2d | 2019-06-18 23:08:59 +0200 | [diff] [blame] | 647 | { |
| 648 | struct hab_hdr *hdr = (struct hab_hdr *)HAB_RVT_BASE; |
| 649 | |
| 650 | if (hdr->tag != HAB_TAG_RVT) { |
| 651 | printf("Unexpected header tag: %x\n", hdr->tag); |
| 652 | return CMD_RET_FAILURE; |
| 653 | } |
| 654 | |
| 655 | printf("HAB version: %d.%d\n", hdr->par >> 4, hdr->par & 0xf); |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 660 | static int do_authenticate_image_or_failover(struct cmd_tbl *cmdtp, int flag, |
| 661 | int argc, char *const argv[]) |
Bryan O'Donoghue | 49e6242 | 2018-03-26 15:36:46 +0100 | [diff] [blame] | 662 | { |
| 663 | int ret = CMD_RET_FAILURE; |
| 664 | |
| 665 | if (argc != 4) { |
| 666 | ret = CMD_RET_USAGE; |
| 667 | goto error; |
| 668 | } |
| 669 | |
| 670 | if (!imx_hab_is_enabled()) { |
| 671 | printf("error: secure boot disabled\n"); |
| 672 | goto error; |
| 673 | } |
| 674 | |
| 675 | if (do_authenticate_image(NULL, flag, argc, argv) != CMD_RET_SUCCESS) { |
| 676 | fprintf(stderr, "authentication fail -> %s %s %s %s\n", |
| 677 | argv[0], argv[1], argv[2], argv[3]); |
| 678 | do_hab_failsafe(0, 0, 1, NULL); |
| 679 | }; |
| 680 | ret = CMD_RET_SUCCESS; |
| 681 | error: |
| 682 | return ret; |
| 683 | } |
| 684 | |
Breno Lima | 58f75ef | 2021-03-25 17:30:13 +0800 | [diff] [blame] | 685 | #ifdef CONFIG_MX7ULP |
| 686 | U_BOOT_CMD( |
| 687 | hab_status, CONFIG_SYS_MAXARGS, 2, do_hab_status, |
| 688 | "display HAB status and events", |
| 689 | "hab_status - A7 HAB event and status\n" |
| 690 | "hab_status m4 - M4 HAB event and status" |
| 691 | ); |
| 692 | #else |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 693 | U_BOOT_CMD( |
| 694 | hab_status, CONFIG_SYS_MAXARGS, 1, do_hab_status, |
| 695 | "display HAB status", |
| 696 | "" |
| 697 | ); |
Breno Lima | 58f75ef | 2021-03-25 17:30:13 +0800 | [diff] [blame] | 698 | #endif |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 699 | |
| 700 | U_BOOT_CMD( |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 701 | hab_auth_img, 4, 0, do_authenticate_image, |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 702 | "authenticate image via HAB", |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 703 | "addr length ivt_offset\n" |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 704 | "addr - image hex address\n" |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 705 | "length - image hex length\n" |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 706 | "ivt_offset - hex offset of IVT in the image" |
| 707 | ); |
| 708 | |
Bryan O'Donoghue | 9587b0d | 2018-01-12 12:40:19 +0000 | [diff] [blame] | 709 | U_BOOT_CMD( |
| 710 | hab_failsafe, CONFIG_SYS_MAXARGS, 1, do_hab_failsafe, |
| 711 | "run BootROM failsafe routine", |
| 712 | "" |
| 713 | ); |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 714 | |
Bryan O'Donoghue | 49e6242 | 2018-03-26 15:36:46 +0100 | [diff] [blame] | 715 | U_BOOT_CMD( |
| 716 | hab_auth_img_or_fail, 4, 0, |
| 717 | do_authenticate_image_or_failover, |
| 718 | "authenticate image via HAB on failure drop to USB BootROM mode", |
| 719 | "addr length ivt_offset\n" |
| 720 | "addr - image hex address\n" |
| 721 | "length - image hex length\n" |
| 722 | "ivt_offset - hex offset of IVT in the image" |
| 723 | ); |
| 724 | |
Sjoerd Simons | b4eca2d | 2019-06-18 23:08:59 +0200 | [diff] [blame] | 725 | U_BOOT_CMD( |
| 726 | hab_version, 1, 0, do_hab_version, |
| 727 | "print HAB major/minor version", |
| 728 | "" |
| 729 | ); |
| 730 | |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 731 | #endif /* !defined(CONFIG_SPL_BUILD) */ |
| 732 | |
Utkarsh Gupta | ed286bc | 2018-02-20 01:19:24 +0000 | [diff] [blame] | 733 | /* Get CSF Header length */ |
| 734 | static int get_hab_hdr_len(struct hab_hdr *hdr) |
| 735 | { |
| 736 | return (size_t)((hdr->len[0] << 8) + (hdr->len[1])); |
| 737 | } |
| 738 | |
| 739 | /* Check whether addr lies between start and |
| 740 | * end and is within the length of the image |
| 741 | */ |
| 742 | static int chk_bounds(u8 *addr, size_t bytes, u8 *start, u8 *end) |
| 743 | { |
| 744 | size_t csf_size = (size_t)((end + 1) - addr); |
| 745 | |
| 746 | return (addr && (addr >= start) && (addr <= end) && |
| 747 | (csf_size >= bytes)); |
| 748 | } |
| 749 | |
| 750 | /* Get Length of each command in CSF */ |
| 751 | static int get_csf_cmd_hdr_len(u8 *csf_hdr) |
| 752 | { |
| 753 | if (*csf_hdr == HAB_CMD_HDR) |
| 754 | return sizeof(struct hab_hdr); |
| 755 | |
| 756 | return get_hab_hdr_len((struct hab_hdr *)csf_hdr); |
| 757 | } |
| 758 | |
| 759 | /* Check if CSF is valid */ |
| 760 | static bool csf_is_valid(struct ivt *ivt, ulong start_addr, size_t bytes) |
| 761 | { |
| 762 | u8 *start = (u8 *)start_addr; |
| 763 | u8 *csf_hdr; |
| 764 | u8 *end; |
| 765 | |
| 766 | size_t csf_hdr_len; |
| 767 | size_t cmd_hdr_len; |
| 768 | size_t offset = 0; |
| 769 | |
| 770 | if (bytes != 0) |
| 771 | end = start + bytes - 1; |
| 772 | else |
| 773 | end = start; |
| 774 | |
| 775 | /* Verify if CSF pointer content is zero */ |
| 776 | if (!ivt->csf) { |
| 777 | puts("Error: CSF pointer is NULL\n"); |
| 778 | return false; |
| 779 | } |
| 780 | |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 781 | csf_hdr = (u8 *)(ulong)ivt->csf; |
Utkarsh Gupta | ed286bc | 2018-02-20 01:19:24 +0000 | [diff] [blame] | 782 | |
| 783 | /* Verify if CSF Header exist */ |
| 784 | if (*csf_hdr != HAB_CMD_HDR) { |
| 785 | puts("Error: CSF header command not found\n"); |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | csf_hdr_len = get_hab_hdr_len((struct hab_hdr *)csf_hdr); |
| 790 | |
| 791 | /* Check if the CSF lies within the image bounds */ |
| 792 | if (!chk_bounds(csf_hdr, csf_hdr_len, start, end)) { |
| 793 | puts("Error: CSF lies outside the image bounds\n"); |
| 794 | return false; |
| 795 | } |
| 796 | |
| 797 | do { |
Utkarsh Gupta | 20fa1dd | 2018-02-20 01:19:25 +0000 | [diff] [blame] | 798 | struct hab_hdr *cmd; |
| 799 | |
| 800 | cmd = (struct hab_hdr *)&csf_hdr[offset]; |
| 801 | |
| 802 | switch (cmd->tag) { |
| 803 | case (HAB_CMD_WRT_DAT): |
| 804 | puts("Error: Deprecated write command found\n"); |
| 805 | return false; |
| 806 | case (HAB_CMD_CHK_DAT): |
| 807 | puts("Error: Deprecated check command found\n"); |
| 808 | return false; |
| 809 | case (HAB_CMD_SET): |
| 810 | if (cmd->par == HAB_PAR_MID) { |
| 811 | puts("Error: Deprecated Set MID command found\n"); |
| 812 | return false; |
| 813 | } |
| 814 | default: |
| 815 | break; |
| 816 | } |
| 817 | |
Utkarsh Gupta | ed286bc | 2018-02-20 01:19:24 +0000 | [diff] [blame] | 818 | cmd_hdr_len = get_csf_cmd_hdr_len(&csf_hdr[offset]); |
| 819 | if (!cmd_hdr_len) { |
| 820 | puts("Error: Invalid command length\n"); |
| 821 | return false; |
| 822 | } |
| 823 | offset += cmd_hdr_len; |
| 824 | |
| 825 | } while (offset < csf_hdr_len); |
| 826 | |
| 827 | return true; |
| 828 | } |
| 829 | |
Utkarsh Gupta | fe8acf5 | 2021-03-25 17:30:08 +0800 | [diff] [blame] | 830 | /* |
| 831 | * Validate IVT structure of the image being authenticated |
| 832 | */ |
| 833 | static int validate_ivt(struct ivt *ivt_initial) |
| 834 | { |
| 835 | struct ivt_header *ivt_hdr = &ivt_initial->hdr; |
| 836 | |
| 837 | if ((ulong)ivt_initial & 0x3) { |
| 838 | puts("Error: Image's start address is not 4 byte aligned\n"); |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | /* Check IVT fields before allowing authentication */ |
| 843 | if ((!verify_ivt_header(ivt_hdr)) && \ |
| 844 | (ivt_initial->entry != 0x0) && \ |
| 845 | (ivt_initial->reserved1 == 0x0) && \ |
| 846 | (ivt_initial->self == \ |
| 847 | (uint32_t)((ulong)ivt_initial & 0xffffffff)) && \ |
| 848 | (ivt_initial->csf != 0x0) && \ |
| 849 | (ivt_initial->reserved2 == 0x0)) { |
| 850 | /* Report boot failure if DCD pointer is found in IVT */ |
| 851 | if (ivt_initial->dcd != 0x0) |
| 852 | puts("Error: DCD pointer must be 0\n"); |
| 853 | else |
| 854 | return 1; |
| 855 | } |
| 856 | |
| 857 | puts("Error: Invalid IVT structure\n"); |
| 858 | debug("\nAllowed IVT structure:\n"); |
| 859 | debug("IVT HDR = 0x4X2000D1\n"); |
| 860 | debug("IVT ENTRY = 0xXXXXXXXX\n"); |
| 861 | debug("IVT RSV1 = 0x0\n"); |
| 862 | debug("IVT DCD = 0x0\n"); /* Recommended */ |
| 863 | debug("IVT BOOT_DATA = 0xXXXXXXXX\n"); /* Commonly 0x0 */ |
| 864 | debug("IVT SELF = 0xXXXXXXXX\n"); /* = ddr_start + ivt_offset */ |
| 865 | debug("IVT CSF = 0xXXXXXXXX\n"); |
| 866 | debug("IVT RSV2 = 0x0\n"); |
| 867 | |
| 868 | /* Invalid IVT structure */ |
| 869 | return 0; |
| 870 | } |
| 871 | |
Bryan O'Donoghue | 07eefaf | 2018-01-12 12:40:16 +0000 | [diff] [blame] | 872 | bool imx_hab_is_enabled(void) |
Sven Ebenfeld | 15b505b | 2016-11-06 16:37:55 +0100 | [diff] [blame] | 873 | { |
| 874 | struct imx_sec_config_fuse_t *fuse = |
| 875 | (struct imx_sec_config_fuse_t *)&imx_sec_config_fuse; |
| 876 | uint32_t reg; |
| 877 | int ret; |
| 878 | |
| 879 | ret = fuse_read(fuse->bank, fuse->word, ®); |
| 880 | if (ret) { |
| 881 | puts("\nSecure boot fuse read error\n"); |
| 882 | return ret; |
| 883 | } |
| 884 | |
| 885 | return (reg & IS_HAB_ENABLED_BIT) == IS_HAB_ENABLED_BIT; |
| 886 | } |
| 887 | |
Bryan O'Donoghue | 57f6548 | 2018-01-12 12:40:13 +0000 | [diff] [blame] | 888 | int imx_hab_authenticate_image(uint32_t ddr_start, uint32_t image_size, |
| 889 | uint32_t ivt_offset) |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 890 | { |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 891 | ulong load_addr = 0; |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 892 | size_t bytes; |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 893 | ulong ivt_addr = 0; |
Bryan O'Donoghue | 9535b39 | 2018-01-12 12:39:56 +0000 | [diff] [blame] | 894 | int result = 1; |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 895 | ulong start; |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 896 | struct ivt *ivt; |
Bryan O'Donoghue | b7c3cae | 2018-01-12 12:40:10 +0000 | [diff] [blame] | 897 | enum hab_status status; |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 898 | |
Breno Lima | 1d756ad | 2021-03-25 17:30:10 +0800 | [diff] [blame] | 899 | if (!imx_hab_is_enabled()) |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 900 | puts("hab fuse not enabled\n"); |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 901 | |
Bryan O'Donoghue | d2c6180 | 2018-01-12 12:39:57 +0000 | [diff] [blame] | 902 | printf("\nAuthenticate image from DDR location 0x%x...\n", |
| 903 | ddr_start); |
| 904 | |
| 905 | hab_caam_clock_enable(1); |
| 906 | |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 907 | /* Calculate IVT address header */ |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 908 | ivt_addr = (ulong) (ddr_start + ivt_offset); |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 909 | ivt = (struct ivt *)ivt_addr; |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 910 | |
| 911 | /* Verify IVT header bugging out on error */ |
Utkarsh Gupta | fe8acf5 | 2021-03-25 17:30:08 +0800 | [diff] [blame] | 912 | if (!validate_ivt(ivt)) |
Breno Lima | 669f2d1 | 2018-02-20 01:19:22 +0000 | [diff] [blame] | 913 | goto hab_authentication_exit; |
Bryan O'Donoghue | 49b6d05 | 2018-01-12 12:40:03 +0000 | [diff] [blame] | 914 | |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 915 | start = ddr_start; |
Bryan O'Donoghue | c5800b2 | 2018-01-12 12:40:01 +0000 | [diff] [blame] | 916 | bytes = image_size; |
Bryan O'Donoghue | 04099e9 | 2018-01-12 12:40:05 +0000 | [diff] [blame] | 917 | |
Utkarsh Gupta | ed286bc | 2018-02-20 01:19:24 +0000 | [diff] [blame] | 918 | /* Verify CSF */ |
| 919 | if (!csf_is_valid(ivt, start, bytes)) |
| 920 | goto hab_authentication_exit; |
| 921 | |
Bryan O'Donoghue | 04099e9 | 2018-01-12 12:40:05 +0000 | [diff] [blame] | 922 | if (hab_rvt_entry() != HAB_SUCCESS) { |
| 923 | puts("hab entry function fail\n"); |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 924 | goto hab_exit_failure_print_status; |
Bryan O'Donoghue | 04099e9 | 2018-01-12 12:40:05 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 927 | status = hab_rvt_check_target(HAB_TGT_MEMORY, (void *)(ulong)ddr_start, bytes); |
Bryan O'Donoghue | b7c3cae | 2018-01-12 12:40:10 +0000 | [diff] [blame] | 928 | if (status != HAB_SUCCESS) { |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 929 | printf("HAB check target 0x%08x-0x%08lx fail\n", |
Breno Lima | e449e2d | 2021-03-25 17:30:14 +0800 | [diff] [blame] | 930 | ddr_start, ddr_start + (ulong)bytes); |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 931 | goto hab_exit_failure_print_status; |
Bryan O'Donoghue | b7c3cae | 2018-01-12 12:40:10 +0000 | [diff] [blame] | 932 | } |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 933 | #ifdef DEBUG |
Breno Lima | e449e2d | 2021-03-25 17:30:14 +0800 | [diff] [blame] | 934 | printf("\nivt_offset = 0x%x, ivt addr = 0x%lx\n", ivt_offset, ivt_addr); |
Bryan O'Donoghue | 824ef30 | 2018-01-12 12:40:07 +0000 | [diff] [blame] | 935 | printf("ivt entry = 0x%08x, dcd = 0x%08x, csf = 0x%08x\n", ivt->entry, |
| 936 | ivt->dcd, ivt->csf); |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 937 | puts("Dumping IVT\n"); |
Marek Vasut | 6a412fa | 2023-05-29 01:29:20 +0200 | [diff] [blame] | 938 | print_buffer(ivt_addr, (void *)(uintptr_t)(ivt_addr), 4, 0x8, 0); |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 939 | |
| 940 | puts("Dumping CSF Header\n"); |
Marek Vasut | 6a412fa | 2023-05-29 01:29:20 +0200 | [diff] [blame] | 941 | print_buffer(ivt->csf, (void *)(uintptr_t)(ivt->csf), 4, 0x10, 0); |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 942 | |
| 943 | #if !defined(CONFIG_SPL_BUILD) |
| 944 | get_hab_status(); |
| 945 | #endif |
| 946 | |
| 947 | puts("\nCalling authenticate_image in ROM\n"); |
| 948 | printf("\tivt_offset = 0x%x\n", ivt_offset); |
| 949 | printf("\tstart = 0x%08lx\n", start); |
Marek Vasut | 6a412fa | 2023-05-29 01:29:20 +0200 | [diff] [blame] | 950 | printf("\tbytes = 0x%lx\n", (ulong)bytes); |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 951 | #endif |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 952 | |
| 953 | #ifndef CONFIG_ARM64 |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 954 | /* |
| 955 | * If the MMU is enabled, we have to notify the ROM |
| 956 | * code, or it won't flush the caches when needed. |
| 957 | * This is done, by setting the "pu_irom_mmu_enabled" |
| 958 | * word to 1. You can find its address by looking in |
| 959 | * the ROM map. This is critical for |
| 960 | * authenticate_image(). If MMU is enabled, without |
| 961 | * setting this bit, authentication will fail and may |
| 962 | * crash. |
| 963 | */ |
| 964 | /* Check MMU enabled */ |
| 965 | if (is_soc_type(MXC_SOC_MX6) && get_cr() & CR_M) { |
| 966 | if (is_mx6dq()) { |
| 967 | /* |
| 968 | * This won't work on Rev 1.0.0 of |
| 969 | * i.MX6Q/D, since their ROM doesn't |
| 970 | * do cache flushes. don't think any |
| 971 | * exist, so we ignore them. |
| 972 | */ |
| 973 | if (!is_mx6dqp()) |
| 974 | writel(1, MX6DQ_PU_IROM_MMU_EN_VAR); |
| 975 | } else if (is_mx6sdl()) { |
| 976 | writel(1, MX6DLS_PU_IROM_MMU_EN_VAR); |
| 977 | } else if (is_mx6sl()) { |
| 978 | writel(1, MX6SL_PU_IROM_MMU_EN_VAR); |
| 979 | } |
| 980 | } |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 981 | #endif |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 982 | |
Peng Fan | 507da97 | 2021-03-25 17:30:07 +0800 | [diff] [blame] | 983 | load_addr = (ulong)hab_rvt_authenticate_image( |
Bryan O'Donoghue | 53c8a51 | 2018-01-12 12:39:58 +0000 | [diff] [blame] | 984 | HAB_CID_UBOOT, |
| 985 | ivt_offset, (void **)&start, |
| 986 | (size_t *)&bytes, NULL); |
| 987 | if (hab_rvt_exit() != HAB_SUCCESS) { |
| 988 | puts("hab exit function fail\n"); |
| 989 | load_addr = 0; |
| 990 | } |
| 991 | |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 992 | hab_exit_failure_print_status: |
Bryan O'Donoghue | d2c6180 | 2018-01-12 12:39:57 +0000 | [diff] [blame] | 993 | #if !defined(CONFIG_SPL_BUILD) |
| 994 | get_hab_status(); |
| 995 | #endif |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 996 | |
Breno Lima | 669f2d1 | 2018-02-20 01:19:22 +0000 | [diff] [blame] | 997 | hab_authentication_exit: |
Bryan O'Donoghue | 2c6c68d2 | 2018-01-12 12:40:11 +0000 | [diff] [blame] | 998 | |
Breno Lima | 1d756ad | 2021-03-25 17:30:10 +0800 | [diff] [blame] | 999 | if (load_addr != 0 || !imx_hab_is_enabled()) |
Bryan O'Donoghue | 9535b39 | 2018-01-12 12:39:56 +0000 | [diff] [blame] | 1000 | result = 0; |
Nitin Garg | 36c1ca4 | 2014-09-16 13:33:25 -0500 | [diff] [blame] | 1001 | |
| 1002 | return result; |
| 1003 | } |
Ye Li | c428ca8 | 2021-03-25 17:30:06 +0800 | [diff] [blame] | 1004 | |
| 1005 | int authenticate_image(u32 ddr_start, u32 raw_image_size) |
| 1006 | { |
| 1007 | u32 ivt_offset; |
| 1008 | size_t bytes; |
| 1009 | |
| 1010 | ivt_offset = (raw_image_size + ALIGN_SIZE - 1) & |
| 1011 | ~(ALIGN_SIZE - 1); |
| 1012 | bytes = ivt_offset + IVT_SIZE + CSF_PAD_SIZE; |
| 1013 | |
| 1014 | return imx_hab_authenticate_image(ddr_start, bytes, ivt_offset); |
| 1015 | } |