[PATCH 2/2] HABv4: fix ROM code API usage

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Even if the provided buffer is too small, the call returns with an
HAB_STATUS_SUCCESS status, but the buffer doesn't contain any data in this
case. Instead the required buffer length is reported back.
This change re-organizes the event reporting by first calling the ROM code
for the event's size and then providing the required buffer. Handling for
both classes of reports (errors and warnings) is the same, so use one
function for requesting the event data.

Signed-off-by: Juergen Borleis <jbe@xxxxxxxxxxxxxx>
---
 drivers/hab/habv4.c | 54 +++++++++++++++++++++++++++++++++------------
 1 file changed, 40 insertions(+), 14 deletions(-)

diff --git a/drivers/hab/habv4.c b/drivers/hab/habv4.c
index f1f45648f5..6e4736e927 100644
--- a/drivers/hab/habv4.c
+++ b/drivers/hab/habv4.c
@@ -503,11 +503,37 @@ static bool is_known_rng_fail_event(const uint8_t *data, size_t len)
 	return false;
 }
 
+static int habv4_get_event(uint8_t **buf, uint32_t *len, const struct habv4_rvt *rvt,
+			   enum hab_status class, uint32_t idx)
+{
+	uint8_t *data;
+	enum hab_status stat;
+
+	*len = 0;
+	/* call for the event and its size */
+	stat = rvt->report_event(class, idx, NULL, len);
+	if (stat != HAB_STATUS_SUCCESS)
+		return -ENODATA; /* regular use case to detect the last available event */
+
+	data = xmalloc(*len);
+	/* now get the data */
+	stat = rvt->report_event(class, idx, data, len);
+	if (stat != HAB_STATUS_SUCCESS) {
+		pr_err("HAB API misbehaviour detected\n");
+		free(data);
+		return -EINVAL;
+	}
+
+	*buf = data;
+	return 0;
+}
+
 static int habv4_get_status(const struct habv4_rvt *rvt)
 {
-	uint8_t data[256];
+	uint8_t *data;
 	uint32_t len;
-	uint32_t index = 0;
+	uint32_t index;
+	int rc;
 	enum hab_status status;
 	enum hab_config config = 0x0;
 	enum hab_state state = 0x0;
@@ -527,32 +553,32 @@ static int habv4_get_status(const struct habv4_rvt *rvt)
 		return 0;
 	}
 
-	len = sizeof(data);
-	while (rvt->report_event(HAB_STATUS_WARNING, index, data, &len) == HAB_STATUS_SUCCESS) {
+	for (index = 0; ; index++) {
+		rc = habv4_get_event(&data, &len, rvt, HAB_STATUS_WARNING, index);
+		if (rc != 0)
+			break;
 
 		/* suppress RNG self-test fail events if they can be handled in software */
 		if (IS_ENABLED(CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_SELF_TEST) &&
 		    is_known_rng_fail_event(data, len)) {
 			pr_debug("RNG self-test failure detected, will run software self-test\n");
 		} else {
-			pr_err("-------- HAB warning Event %d --------\n", index);
+			pr_err("-------- HAB warning Event %u --------\n", index);
 			pr_err("event data:\n");
 			habv4_display_event(data, len);
 		}
-
-		len = sizeof(data);
-		index++;
+		free(data);
 	}
 
-	len = sizeof(data);
-	index = 0;
-	while (rvt->report_event(HAB_STATUS_FAILURE, index, data, &len) == HAB_STATUS_SUCCESS) {
-		pr_err("-------- HAB failure Event %d --------\n", index);
+	for (index = 0; ; index++) {
+		rc = habv4_get_event(&data, &len, rvt, HAB_STATUS_FAILURE, index);
+		if (rc != 0)
+			break;
+		pr_err("-------- HAB failure Event %u --------\n", index);
 		pr_err("event data:\n");
 
 		habv4_display_event(data, len);
-		len = sizeof(data);
-		index++;
+		free(data);
 	}
 
 	return -EPERM;
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux