Ruchika Gupta | 5c17253 | 2022-04-08 13:14:44 +0530 | [diff] [blame] | 1 | /* |
Tamas Ban | b979600 | 2023-06-05 11:08:47 +0200 | [diff] [blame] | 2 | * Copyright (c) 2022-2024, Arm Limited. All rights reserved. |
Jens Wiklander | b1aa91b | 2023-07-19 10:37:39 +0200 | [diff] [blame] | 3 | * Copyright (c) 2022-2023, Linaro. |
Ruchika Gupta | 5c17253 | 2022-04-08 13:14:44 +0530 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <drivers/measured_boot/event_log/event_log.h> |
Tamas Ban | b979600 | 2023-06-05 11:08:47 +0200 | [diff] [blame] | 11 | #include <drivers/measured_boot/metadata.h> |
Ruchika Gupta | 5c17253 | 2022-04-08 13:14:44 +0530 | [diff] [blame] | 12 | #include <plat/common/common_def.h> |
Manish V Badarkhe | 95197b5 | 2022-11-18 20:43:07 +0000 | [diff] [blame] | 13 | #include <plat/common/platform.h> |
Ruchika Gupta | 5c17253 | 2022-04-08 13:14:44 +0530 | [diff] [blame] | 14 | #include <tools_share/tbbr_oid.h> |
| 15 | |
| 16 | #include "../common/qemu_private.h" |
| 17 | |
| 18 | /* Event Log data */ |
| 19 | static uint8_t event_log[PLAT_EVENT_LOG_MAX_SIZE]; |
| 20 | static uint64_t event_log_base; |
| 21 | |
Manish V Badarkhe | 95197b5 | 2022-11-18 20:43:07 +0000 | [diff] [blame] | 22 | /* QEMU table with platform specific image IDs, names and PCRs */ |
| 23 | static const event_log_metadata_t qemu_event_log_metadata[] = { |
Tamas Ban | b979600 | 2023-06-05 11:08:47 +0200 | [diff] [blame] | 24 | { BL31_IMAGE_ID, MBOOT_BL31_IMAGE_STRING, PCR_0 }, |
| 25 | { BL32_IMAGE_ID, MBOOT_BL32_IMAGE_STRING, PCR_0 }, |
| 26 | { BL32_EXTRA1_IMAGE_ID, MBOOT_BL32_EXTRA1_IMAGE_STRING, PCR_0 }, |
| 27 | { BL32_EXTRA2_IMAGE_ID, MBOOT_BL32_EXTRA2_IMAGE_STRING, PCR_0 }, |
| 28 | { BL33_IMAGE_ID, MBOOT_BL33_IMAGE_STRING, PCR_0 }, |
| 29 | { HW_CONFIG_ID, MBOOT_HW_CONFIG_STRING, PCR_0 }, |
| 30 | { NT_FW_CONFIG_ID, MBOOT_NT_FW_CONFIG_STRING, PCR_0 }, |
| 31 | { SCP_BL2_IMAGE_ID, MBOOT_SCP_BL2_IMAGE_STRING, PCR_0 }, |
| 32 | { SOC_FW_CONFIG_ID, MBOOT_SOC_FW_CONFIG_STRING, PCR_0 }, |
| 33 | { TOS_FW_CONFIG_ID, MBOOT_TOS_FW_CONFIG_STRING, PCR_0 }, |
Ruchika Gupta | 5c17253 | 2022-04-08 13:14:44 +0530 | [diff] [blame] | 34 | |
| 35 | { EVLOG_INVALID_ID, NULL, (unsigned int)(-1) } /* Terminator */ |
| 36 | }; |
| 37 | |
| 38 | void bl2_plat_mboot_init(void) |
| 39 | { |
| 40 | /* |
| 41 | * Here we assume that BL1/ROM code doesn't have the driver |
| 42 | * to measure the BL2 code which is a common case for |
| 43 | * already existing platforms |
| 44 | */ |
| 45 | event_log_init(event_log, event_log + sizeof(event_log)); |
| 46 | event_log_write_header(); |
| 47 | |
| 48 | /* |
| 49 | * TBD - Add code to do self measurement of BL2 code and add an |
| 50 | * event for BL2 measurement |
| 51 | */ |
| 52 | |
| 53 | event_log_base = (uintptr_t)event_log; |
| 54 | } |
| 55 | |
| 56 | void bl2_plat_mboot_finish(void) |
| 57 | { |
| 58 | int rc; |
| 59 | |
| 60 | /* Event Log address in Non-Secure memory */ |
| 61 | uintptr_t ns_log_addr; |
| 62 | |
| 63 | /* Event Log filled size */ |
| 64 | size_t event_log_cur_size; |
| 65 | |
| 66 | event_log_cur_size = event_log_get_cur_size((uint8_t *)event_log_base); |
| 67 | |
| 68 | rc = qemu_set_nt_fw_info( |
| 69 | #ifdef SPD_opteed |
| 70 | (uintptr_t)event_log_base, |
| 71 | #endif |
| 72 | event_log_cur_size, &ns_log_addr); |
| 73 | if (rc != 0) { |
| 74 | ERROR("%s(): Unable to update %s_FW_CONFIG\n", |
| 75 | __func__, "NT"); |
| 76 | /* |
| 77 | * It is a fatal error because on QEMU secure world software |
| 78 | * assumes that a valid event log exists and will use it to |
| 79 | * record the measurements into the fTPM or sw-tpm. |
| 80 | * Note: In QEMU platform, OP-TEE uses nt_fw_config to get the |
| 81 | * secure Event Log buffer address. |
| 82 | */ |
| 83 | panic(); |
| 84 | } |
| 85 | |
| 86 | /* Copy Event Log to Non-secure memory */ |
| 87 | (void)memcpy((void *)ns_log_addr, (const void *)event_log_base, |
| 88 | event_log_cur_size); |
| 89 | |
| 90 | /* Ensure that the Event Log is visible in Non-secure memory */ |
| 91 | flush_dcache_range(ns_log_addr, event_log_cur_size); |
| 92 | |
| 93 | #if defined(SPD_tspd) || defined(SPD_spmd) |
| 94 | /* Set Event Log data in TOS_FW_CONFIG */ |
| 95 | rc = qemu_set_tos_fw_info((uintptr_t)event_log_base, |
| 96 | event_log_cur_size); |
| 97 | if (rc != 0) { |
| 98 | ERROR("%s(): Unable to update %s_FW_CONFIG\n", |
| 99 | __func__, "TOS"); |
| 100 | panic(); |
| 101 | } |
| 102 | #endif /* defined(SPD_tspd) || defined(SPD_spmd) */ |
| 103 | |
| 104 | dump_event_log((uint8_t *)event_log_base, event_log_cur_size); |
| 105 | } |
Manish V Badarkhe | 95197b5 | 2022-11-18 20:43:07 +0000 | [diff] [blame] | 106 | |
| 107 | int plat_mboot_measure_image(unsigned int image_id, image_info_t *image_data) |
| 108 | { |
| 109 | /* Calculate image hash and record data in Event Log */ |
| 110 | int err = event_log_measure_and_record(image_data->image_base, |
| 111 | image_data->image_size, |
| 112 | image_id, |
| 113 | qemu_event_log_metadata); |
| 114 | if (err != 0) { |
| 115 | ERROR("%s%s image id %u (%i)\n", |
| 116 | "Failed to ", "record", image_id, err); |
| 117 | return err; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
Jens Wiklander | b1aa91b | 2023-07-19 10:37:39 +0200 | [diff] [blame] | 122 | |
| 123 | int plat_mboot_measure_key(const void *pk_oid, const void *pk_ptr, |
| 124 | size_t pk_len) |
| 125 | { |
| 126 | return 0; |
| 127 | } |