mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Juergen Borleis <jbe@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 2/2] HABv4: fix ROM code API usage
Date: Mon,  2 Dec 2019 11:24:49 +0100	[thread overview]
Message-ID: <20191202102449.23535-2-jbe@pengutronix.de> (raw)
In-Reply-To: <20191202102449.23535-1-jbe@pengutronix.de>

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@pengutronix.de>
---
 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@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2019-12-02 10:24 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-02 10:24 [PATCH 1/2] HABv4: remove useless error message Juergen Borleis
2019-12-02 10:24 ` Juergen Borleis [this message]
2019-12-02 13:07 ` Roland Hieber
2019-12-02 13:24   ` Marc Kleine-Budde
2019-12-02 13:33     ` Roland Hieber
2019-12-02 13:38       ` Marc Kleine-Budde
2019-12-02 14:30     ` Juergen Borleis
2019-12-03 14:04       ` Marc Kleine-Budde
2019-12-03 14:36         ` Sascha Hauer
2019-12-03 14:47           ` Marc Kleine-Budde
2019-12-03 14:51         ` Juergen Borleis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191202102449.23535-2-jbe@pengutronix.de \
    --to=jbe@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox