refactor(measured boot): revisit error handling (2/3)

- In add_event2():

  Turn the first error condition checking whether there is room for an
  extra event2 data structure into an assertion. The platform layer is
  responsible for choosing an appropriate event log buffer size based
  on the number of measurements it expects. If this assertion fires,
  the platform macro EVENT_LOG_SIZE should be adjusted and the
  firmware recompiled.

  Call this assumption out in the function documentation.

  Also remove the second error condition check, which is a subset of
  the first one and thus is redundant.

  As a result of these changes, add_event2() can no longer fail. Thus,
  change its return type from int to void.

  Also, the 'size_of_event' local variable is now unused in release
  builds so remove it and move its value into the assertion.

Change-Id: I113fc141de59708b20435a0c7126255561ab7786
Signed-off-by: Sandrine Bailleux <sandrine.bailleux@arm.com>
diff --git a/drivers/measured_boot/event_log.c b/drivers/measured_boot/event_log.c
index 2b94caa..7ba7165 100644
--- a/drivers/measured_boot/event_log.c
+++ b/drivers/measured_boot/event_log.c
@@ -84,27 +84,22 @@
  *
  * @param[in] hash	Pointer to hash data of TCG_DIGEST_SIZE bytes
  * @param[in] image_ptr	Pointer to image_data_t structure
- * @return:
- *	0 = success
- *    < 0 = error code
+ *
+ * There must be room for storing this new event into the event log buffer.
  */
-static int add_event2(const uint8_t *hash, const image_data_t *image_ptr)
+static void add_event2(const uint8_t *hash, const image_data_t *image_ptr)
 {
 	void *ptr = log_ptr;
 	uint32_t name_len;
-	uint32_t size_of_event;
 
 	assert(image_ptr != NULL);
 	assert(image_ptr->name != NULL);
 
 	name_len = (uint32_t)strlen(image_ptr->name) + 1U;
-	size_of_event = name_len + (uint32_t)EVENT2_HDR_SIZE;
 
 	/* Check for space in Event Log buffer */
-	if (((uintptr_t)ptr + size_of_event) > EVENT_LOG_END) {
-		ERROR("%s(): Event Log is short of memory", __func__);
-		return -ENOMEM;
-	}
+	assert(((uintptr_t)ptr + (uint32_t)EVENT2_HDR_SIZE + name_len) <=
+	       EVENT_LOG_END);
 
 	/*
 	 * As per TCG specifications, firmware components that are measured
@@ -131,12 +126,6 @@
 	/* TCG_PCR_EVENT2.Digests[].Digest[] */
 	ptr = (uint8_t *)((uintptr_t)ptr + offsetof(tpmt_ha, digest));
 
-	/* Check for space in Event Log buffer */
-	if (((uintptr_t)ptr + TCG_DIGEST_SIZE) > EVENT_LOG_END) {
-		ERROR("%s(): Event Log is short of memory", __func__);
-		return -ENOMEM;
-	}
-
 	if (hash == NULL) {
 		/* Get BL2 hash from DTB */
 		bl2_plat_get_hash(ptr);
@@ -156,8 +145,6 @@
 	/* End of event data */
 	log_ptr = (uint8_t *)((uintptr_t)ptr +
 			offsetof(event2_data_t, event) + name_len);
-
-	return 0;
 }
 
 /*
@@ -235,9 +222,7 @@
 	log_ptr = (uint8_t *)ptr;
 
 	/* Add BL2 event */
-	if (add_event2(NULL, plat_data_ptr->images_data) != 0) {
-		panic();
-	}
+	add_event2(NULL, plat_data_ptr->images_data);
 }
 
 /*
@@ -282,7 +267,8 @@
 		return rc;
 	}
 
-	return add_event2(hash_data, data_ptr);
+	add_event2(hash_data, data_ptr);
+	return 0;
 }
 
 /*