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