refactor(measured-boot): split out a few Event Log driver functions

Reorganized a few Event Log functions into multiple functions so that
they can be used for the upcoming DRTM feature. This change mainly
implements below new functions -
1. event_log_buf_init - called by 'event_log_init' to initialise Event
   Log buffer
2. event_log_write_specid_event - called by 'event_log_fixed_header' to
   write specification id event to Event Log buffer
3. event_log_measure and event_log_record - called by
   'event_log_measure_and_record' to measure and record the measurement
   to the Event Log buffer

Change-Id: I1aabb57f79bead726fcf36d59839702cd6a3521d
Signed-off-by: Manish V Badarkhe <Manish.Badarkhe@arm.com>
This commit is contained in:
Manish V Badarkhe 2022-06-15 15:06:43 +01:00
parent e9467afb2d
commit b9b175086c
2 changed files with 61 additions and 28 deletions

View file

@ -84,23 +84,26 @@ static const event2_header_t locality_event_header = {
* Record a measurement as a TCG_PCR_EVENT2 event * Record a measurement as a TCG_PCR_EVENT2 event
* *
* @param[in] hash Pointer to hash data of TCG_DIGEST_SIZE bytes * @param[in] hash Pointer to hash data of TCG_DIGEST_SIZE bytes
* @param[in] event_type Type of Event, Various Event Types are
* mentioned in tcg.h header
* @param[in] metadata_ptr Pointer to event_log_metadata_t structure * @param[in] metadata_ptr Pointer to event_log_metadata_t structure
* *
* There must be room for storing this new event into the event log buffer. * There must be room for storing this new event into the event log buffer.
*/ */
static void event_log_record(const uint8_t *hash, void event_log_record(const uint8_t *hash, uint32_t event_type,
const event_log_metadata_t *metadata_ptr) const event_log_metadata_t *metadata_ptr)
{ {
void *ptr = log_ptr; void *ptr = log_ptr;
uint32_t name_len; uint32_t name_len = 0U;
assert(hash != NULL); assert(hash != NULL);
assert(metadata_ptr != NULL); assert(metadata_ptr != NULL);
assert(metadata_ptr->name != NULL); /* event_log_buf_init() must have been called prior to this. */
/* event_log_init() must have been called prior to this. */
assert(log_ptr != NULL); assert(log_ptr != NULL);
if (metadata_ptr->name != NULL) {
name_len = (uint32_t)strlen(metadata_ptr->name) + 1U; name_len = (uint32_t)strlen(metadata_ptr->name) + 1U;
}
/* Check for space in Event Log buffer */ /* Check for space in Event Log buffer */
assert(((uintptr_t)ptr + (uint32_t)EVENT2_HDR_SIZE + name_len) < assert(((uintptr_t)ptr + (uint32_t)EVENT2_HDR_SIZE + name_len) <
@ -115,7 +118,7 @@ static void event_log_record(const uint8_t *hash,
((event2_header_t *)ptr)->pcr_index = metadata_ptr->pcr; ((event2_header_t *)ptr)->pcr_index = metadata_ptr->pcr;
/* TCG_PCR_EVENT2.EventType */ /* TCG_PCR_EVENT2.EventType */
((event2_header_t *)ptr)->event_type = EV_POST_CODE; ((event2_header_t *)ptr)->event_type = event_type;
/* TCG_PCR_EVENT2.Digests.Count */ /* TCG_PCR_EVENT2.Digests.Count */
ptr = (uint8_t *)ptr + offsetof(event2_header_t, digests); ptr = (uint8_t *)ptr + offsetof(event2_header_t, digests);
@ -139,14 +142,25 @@ static void event_log_record(const uint8_t *hash,
((event2_data_t *)ptr)->event_size = name_len; ((event2_data_t *)ptr)->event_size = name_len;
/* Copy event data to TCG_PCR_EVENT2.Event */ /* Copy event data to TCG_PCR_EVENT2.Event */
if (metadata_ptr->name != NULL) {
(void)memcpy((void *)(((event2_data_t *)ptr)->event), (void)memcpy((void *)(((event2_data_t *)ptr)->event),
(const void *)metadata_ptr->name, name_len); (const void *)metadata_ptr->name, name_len);
}
/* End of event data */ /* End of event data */
log_ptr = (uint8_t *)((uintptr_t)ptr + log_ptr = (uint8_t *)((uintptr_t)ptr +
offsetof(event2_data_t, event) + name_len); offsetof(event2_data_t, event) + name_len);
} }
void event_log_buf_init(uint8_t *event_log_start, uint8_t *event_log_finish)
{
assert(event_log_start != NULL);
assert(event_log_finish > event_log_start);
log_ptr = event_log_start;
log_end = (uintptr_t)event_log_finish;
}
/* /*
* Initialise Event Log global variables, used during the recording * Initialise Event Log global variables, used during the recording
* of various payload measurements into the Event Log buffer * of various payload measurements into the Event Log buffer
@ -158,30 +172,20 @@ static void event_log_record(const uint8_t *hash,
*/ */
void event_log_init(uint8_t *event_log_start, uint8_t *event_log_finish) void event_log_init(uint8_t *event_log_start, uint8_t *event_log_finish)
{ {
assert(event_log_start != NULL); event_log_buf_init(event_log_start, event_log_finish);
assert(event_log_finish > event_log_start);
log_ptr = event_log_start;
log_end = (uintptr_t)event_log_finish;
/* Get pointer to platform's event_log_metadata_t structure */ /* Get pointer to platform's event_log_metadata_t structure */
plat_metadata_ptr = plat_event_log_get_metadata(); plat_metadata_ptr = plat_event_log_get_metadata();
assert(plat_metadata_ptr != NULL); assert(plat_metadata_ptr != NULL);
} }
/* void event_log_write_specid_event(void)
* Initialises Event Log by writing Specification ID and
* Startup Locality events
*/
void event_log_write_header(void)
{ {
const char locality_signature[] = TCG_STARTUP_LOCALITY_SIGNATURE;
void *ptr = log_ptr; void *ptr = log_ptr;
/* event_log_init() must have been called prior to this. */ /* event_log_buf_init() must have been called prior to this. */
assert(log_ptr != NULL); assert(log_ptr != NULL);
assert(((uintptr_t)log_ptr + ID_EVENT_SIZE + LOC_EVENT_SIZE) < assert(((uintptr_t)log_ptr + ID_EVENT_SIZE) < log_end);
log_end);
/* /*
* Add Specification ID Event first * Add Specification ID Event first
@ -202,8 +206,23 @@ void event_log_write_header(void)
* No vendor data * No vendor data
*/ */
((id_event_struct_data_t *)ptr)->vendor_info_size = 0; ((id_event_struct_data_t *)ptr)->vendor_info_size = 0;
ptr = (uint8_t *)((uintptr_t)ptr + log_ptr = (uint8_t *)((uintptr_t)ptr +
offsetof(id_event_struct_data_t, vendor_info)); offsetof(id_event_struct_data_t, vendor_info));
}
/*
* Initialises Event Log by writing Specification ID and
* Startup Locality events
*/
void event_log_write_header(void)
{
const char locality_signature[] = TCG_STARTUP_LOCALITY_SIGNATURE;
void *ptr;
event_log_write_specid_event();
ptr = log_ptr;
assert(((uintptr_t)log_ptr + LOC_EVENT_SIZE) < log_end);
/* /*
* The Startup Locality event should be placed in the log before * The Startup Locality event should be placed in the log before
@ -242,6 +261,14 @@ void event_log_write_header(void)
log_ptr = (uint8_t *)((uintptr_t)ptr + sizeof(startup_locality_event_t)); log_ptr = (uint8_t *)((uintptr_t)ptr + sizeof(startup_locality_event_t));
} }
int event_log_measure(uintptr_t data_base, uint32_t data_size,
unsigned char hash_data[CRYPTO_MD_MAX_SIZE])
{
/* Calculate hash */
return crypto_mod_calc_hash(CRYPTO_MD_ID,
(void *)data_base, data_size, hash_data);
}
/* /*
* Calculate and write hash of image, configuration data, etc. * Calculate and write hash of image, configuration data, etc.
* to Event Log. * to Event Log.
@ -267,14 +294,13 @@ int event_log_measure_and_record(uintptr_t data_base, uint32_t data_size,
} }
assert(metadata_ptr->id != EVLOG_INVALID_ID); assert(metadata_ptr->id != EVLOG_INVALID_ID);
/* Calculate hash */ /* Measure the payload with algorithm selected by EventLog driver */
rc = crypto_mod_calc_hash(CRYPTO_MD_ID, rc = event_log_measure(data_base, data_size, hash_data);
(void *)data_base, data_size, hash_data);
if (rc != 0) { if (rc != 0) {
return rc; return rc;
} }
event_log_record(hash_data, metadata_ptr); event_log_record(hash_data, EV_POST_CODE, metadata_ptr);
return 0; return 0;
} }

View file

@ -11,6 +11,7 @@
#include <common/debug.h> #include <common/debug.h>
#include <common/tbbr/tbbr_img_def.h> #include <common/tbbr/tbbr_img_def.h>
#include <drivers/auth/crypto_mod.h>
#include <drivers/measured_boot/event_log/tcg.h> #include <drivers/measured_boot/event_log/tcg.h>
/* /*
@ -109,10 +110,16 @@ typedef struct {
sizeof(event2_data_t)) sizeof(event2_data_t))
/* Functions' declarations */ /* Functions' declarations */
void event_log_buf_init(uint8_t *event_log_start, uint8_t *event_log_finish);
void event_log_init(uint8_t *event_log_start, uint8_t *event_log_finish); void event_log_init(uint8_t *event_log_start, uint8_t *event_log_finish);
void event_log_write_specid_event(void);
void event_log_write_header(void); void event_log_write_header(void);
void dump_event_log(uint8_t *log_addr, size_t log_size); void dump_event_log(uint8_t *log_addr, size_t log_size);
const event_log_metadata_t *plat_event_log_get_metadata(void); const event_log_metadata_t *plat_event_log_get_metadata(void);
int event_log_measure(uintptr_t data_base, uint32_t data_size,
unsigned char hash_data[CRYPTO_MD_MAX_SIZE]);
void event_log_record(const uint8_t *hash, uint32_t event_type,
const event_log_metadata_t *metadata_ptr);
int event_log_measure_and_record(uintptr_t data_base, uint32_t data_size, int event_log_measure_and_record(uintptr_t data_base, uint32_t data_size,
uint32_t data_id); uint32_t data_id);
size_t event_log_get_cur_size(uint8_t *event_log_start); size_t event_log_get_cur_size(uint8_t *event_log_start);