1 /* 2 * Copyright (c) 2022, Linaro. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 9 #include <common/bl_common.h> 10 #if MEASURED_BOOT 11 #include <common/desc_image_load.h> 12 #endif 13 #include <common/fdt_wrappers.h> 14 #include <platform_def.h> 15 16 #include <libfdt.h> 17 18 #ifdef SPD_opteed 19 /* 20 * Currently OP-TEE does not support reading DTBs from Secure memory 21 * and this property should be removed when this feature is supported. 22 */ 23 #define DTB_PROP_HW_SM_LOG_ADDR "tpm_event_log_sm_addr" 24 #endif 25 26 #define DTB_PROP_HW_LOG_ADDR "tpm_event_log_addr" 27 #define DTB_PROP_HW_LOG_SIZE "tpm_event_log_size" 28 29 #if MEASURED_BOOT 30 31 #ifdef SPD_opteed 32 int qemu_set_tee_fw_info(uintptr_t config_base, uintptr_t log_addr, 33 size_t log_size) 34 { 35 int offs, err = 0; 36 void *dtb = (void *)config_base; 37 const char *compatible = "arm,tpm_event_log"; 38 uint64_t sec_base = cpu_to_fdt64(log_addr); 39 uint32_t sz = cpu_to_fdt32(log_size); 40 41 offs = fdtw_find_or_add_subnode(dtb, 0, "tpm-event-log"); 42 if (offs < 0) { 43 ERROR("Failed to add node tpm-event-log %d\n", offs); 44 return offs; 45 } 46 47 if (fdt_appendprop(dtb, offs, "compatible", compatible, 48 strlen(compatible) + 1) < 0) { 49 return -1; 50 } 51 52 err = fdt_setprop(dtb, offs, DTB_PROP_HW_SM_LOG_ADDR, &sec_base, 8); 53 if (err < 0) { 54 ERROR("Failed to add log addr err %d\n", err); 55 return err; 56 } 57 58 err = fdt_setprop(dtb, offs, DTB_PROP_HW_LOG_SIZE, &sz, 4); 59 if (err < 0) { 60 ERROR("Failed to add log addr err %d\n", err); 61 return err; 62 } 63 64 return err; 65 } 66 #endif 67 68 /* 69 * Write the Event Log address and its size in the DTB. 70 * 71 * This function is supposed to be called only by BL2. 72 * 73 * Returns: 74 * 0 = success 75 * < 0 = error 76 */ 77 static int qemu_set_event_log_info(uintptr_t config_base, 78 #ifdef SPD_opteed 79 uintptr_t sm_log_addr, 80 #endif 81 uintptr_t log_addr, size_t log_size) 82 { 83 /* As libfdt uses void *, we can't avoid this cast */ 84 void *dtb = (void *)config_base; 85 const char *compatible_tpm = "tcg,tpm-tis-mmio"; 86 uint64_t base = cpu_to_fdt64(log_addr); 87 uint32_t sz = cpu_to_fdt32(log_size); 88 int err, node; 89 90 err = fdt_open_into(dtb, dtb, PLAT_QEMU_DT_MAX_SIZE); 91 if (err < 0) { 92 ERROR("Invalid Device Tree at %p: error %d\n", dtb, err); 93 return err; 94 } 95 96 /* 97 * Verify that the DTB is valid, before attempting to write to it, 98 * and get the DTB root node. 99 */ 100 101 /* Check if the pointer to DT is correct */ 102 err = fdt_check_header(dtb); 103 if (err < 0) { 104 WARN("Invalid DTB file passed\n"); 105 return err; 106 } 107 108 /* 109 * Find the TPM node in device tree. On qemu, we assume it will 110 * be sw-tpm. 111 */ 112 node = fdt_node_offset_by_compatible(dtb, -1, compatible_tpm); 113 if (node < 0) { 114 ERROR("The compatible property '%s' not%s", compatible_tpm, 115 " found in the config\n"); 116 return node; 117 } 118 119 err = fdt_setprop(dtb, node, DTB_PROP_HW_LOG_ADDR, &base, 8); 120 if (err < 0) { 121 ERROR("Failed to add log addr err %d\n", err); 122 return err; 123 } 124 125 err = fdt_setprop(dtb, node, DTB_PROP_HW_LOG_SIZE, &sz, 4); 126 if (err < 0) { 127 ERROR("Failed to add log addr err %d\n", err); 128 return err; 129 } 130 131 #ifdef SPD_opteed 132 err = qemu_set_tee_fw_info(config_base, sm_log_addr, log_size); 133 if (err < 0) { 134 ERROR("Failed to add tpm-event-node at %p: err %d\n", dtb, err); 135 return err; 136 } 137 #endif 138 139 err = fdt_pack(dtb); 140 if (err < 0) { 141 ERROR("Failed to pack Device Tree at %p: err %d\n", dtb, err); 142 return err; 143 } 144 145 /* 146 * Ensure that the info written to the DTB is visible 147 * to other images. 148 */ 149 flush_dcache_range(config_base, fdt_totalsize(dtb)); 150 151 return err; 152 } 153 154 /* 155 * This function writes the Event Log address and its size 156 * in the TOS_FW_CONFIG DTB. 157 * 158 * This function is supposed to be called only by BL2. 159 * 160 * Returns: 161 * 0 = success 162 * < 0 = error 163 */ 164 int qemu_set_tos_fw_info(uintptr_t config_base, uintptr_t log_addr, 165 size_t log_size) 166 { 167 int err = 0; 168 169 assert(config_base != 0UL); 170 assert(log_addr != 0UL); 171 172 /* 173 * FIXME - add code to add/update Log address and it's 174 * size in TOS FW CONFIG. 175 * For now we don't have support for TOS FW config in OP-TEE. 176 * So leave this function blank 177 */ 178 179 return err; 180 } 181 182 /* 183 * This function writes the Event Log address and its size 184 * in the QEMU DTB. 185 * 186 * This function is supposed to be called only by BL2. 187 * 188 * Returns: 189 * 0 = success 190 * < 0 = error 191 */ 192 int qemu_set_nt_fw_info( 193 #ifdef SPD_opteed 194 uintptr_t log_addr, 195 #endif 196 size_t log_size, uintptr_t *ns_log_addr) 197 { 198 uintptr_t ns_addr; 199 int err; 200 201 assert(ns_log_addr != NULL); 202 203 ns_addr = PLAT_QEMU_DT_BASE + PLAT_QEMU_DT_MAX_SIZE; 204 205 /* Write the Event Log address and its size in the DTB */ 206 err = qemu_set_event_log_info(PLAT_QEMU_DT_BASE, 207 #ifdef SPD_opteed 208 log_addr, 209 #endif 210 ns_addr, log_size); 211 212 /* Return Event Log address in Non-secure memory */ 213 *ns_log_addr = (err < 0) ? 0UL : ns_addr; 214 return err; 215 } 216 #endif /* MEASURED_BOOT */ 217