1 /*
2  * Copyright (c) 2022, Arm Limited. All rights reserved.
3  * Copyright (c) 2022, Linaro.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include <string.h>
9 
10 #include "./include/imx8m_measured_boot.h"
11 #include <drivers/measured_boot/event_log/event_log.h>
12 #include <plat/arm/common/plat_arm.h>
13 
14 /* Event Log data */
15 static uint8_t event_log[PLAT_IMX_EVENT_LOG_MAX_SIZE];
16 
17 /* FVP table with platform specific image IDs, names and PCRs */
18 static const event_log_metadata_t imx8m_event_log_metadata[] = {
19 	{ BL31_IMAGE_ID, EVLOG_BL31_STRING, PCR_0 },
20 	{ BL32_IMAGE_ID, EVLOG_BL32_STRING, PCR_0 },
21 	{ BL32_EXTRA1_IMAGE_ID, EVLOG_BL32_EXTRA1_STRING, PCR_0 },
22 	{ BL32_EXTRA2_IMAGE_ID, EVLOG_BL32_EXTRA2_STRING, PCR_0 },
23 	{ BL33_IMAGE_ID, EVLOG_BL33_STRING, PCR_0 },
24 	{ EVLOG_INVALID_ID, NULL, (unsigned int)(-1) }	/* Terminator */
25 };
26 
plat_event_log_get_metadata(void)27 const event_log_metadata_t *plat_event_log_get_metadata(void)
28 {
29 	return imx8m_event_log_metadata;
30 }
31 
plat_mboot_measure_image(unsigned int image_id,image_info_t * image_data)32 int plat_mboot_measure_image(unsigned int image_id, image_info_t *image_data)
33 {
34 	/* Calculate image hash and record data in Event Log */
35 	int err = event_log_measure_and_record(image_data->image_base,
36 					       image_data->image_size,
37 					       image_id);
38 	if (err != 0) {
39 		ERROR("%s%s image id %u (%i)\n",
40 		      "Failed to ", "record", image_id, err);
41 		return err;
42 	}
43 
44 	return 0;
45 }
46 
bl2_plat_mboot_init(void)47 void bl2_plat_mboot_init(void)
48 {
49 	event_log_init(event_log, event_log + sizeof(event_log));
50 	event_log_write_header();
51 }
52 
bl2_plat_mboot_finish(void)53 void bl2_plat_mboot_finish(void)
54 {
55 	int rc = 0;
56 
57 	/* Event Log address in Non-Secure memory */
58 	uintptr_t ns_log_addr;
59 
60 	/* Event Log filled size */
61 	size_t event_log_cur_size;
62 
63 	event_log_cur_size = event_log_get_cur_size(event_log);
64 
65 	rc = imx8m_set_nt_fw_info(event_log_cur_size, &ns_log_addr);
66 	if (rc != 0) {
67 		ERROR("%s(): Unable to update %s_FW_CONFIG\n",
68 		      __func__, "NT");
69 		/*
70 		 * It is a fatal error because on i.MX U-boot assumes that
71 		 * a valid event log exists and will use it to record the
72 		 * measurements into the fTPM.
73 		 */
74 		panic();
75 	}
76 
77 	/* Copy Event Log to Non-secure memory */
78 	(void)memcpy((void *)ns_log_addr, (const void *)event_log,
79 		     event_log_cur_size);
80 
81 	/* Ensure that the Event Log is visible in Non-secure memory */
82 	flush_dcache_range(ns_log_addr, event_log_cur_size);
83 
84 	dump_event_log((uint8_t *)event_log, event_log_cur_size);
85 }
86