mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 2/2] ratp: Test if console exists before using it
Date: Wed, 20 May 2020 10:44:02 +0200	[thread overview]
Message-ID: <20200520084402.24585-2-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20200520084402.24585-1-s.hauer@pengutronix.de>

Consoles can be unregistered, this happens for example when barebox
implements the barebox serial USB gadget. Before using the console
we have to explicitly test if the pointer is still valid each time.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/ratp/ratp.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index ca751a30eb..def1ceb020 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -97,11 +97,24 @@ static int init_ratp_command_list(void)
 
 late_initcall(init_ratp_command_list);
 
+static bool console_exists(struct console_device *cdev)
+{
+	struct console_device *cs;
+
+	list_for_each_entry(cs, &console_list, list)
+		if (cs == cdev)
+			return true;
+	return false;
+}
+
 static int console_recv(struct ratp *r, uint8_t *data)
 {
 	struct ratp_ctx *ctx = container_of(r, struct ratp_ctx, ratp);
 	struct console_device *cdev = ctx->cdev;
 
+	if (!console_exists(cdev))
+		return -ENODEV;
+
 	if (ctx->have_synch) {
 		ctx->have_synch = 0;
 		*data = 0x01;
@@ -123,6 +136,9 @@ static int console_send(struct ratp *r, void *pkt, int len)
 	const uint8_t *buf = pkt;
 	int i;
 
+	if (!console_exists(cdev))
+		return -ENODEV;
+
 	for (i = 0; i < len; i++)
 		cdev->putc(cdev, buf[i]);
 
-- 
2.26.2


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

      reply	other threads:[~2020-05-20  8:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-20  8:44 [PATCH 1/2] ratp: Free buffers Sascha Hauer
2020-05-20  8:44 ` Sascha Hauer [this message]

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=20200520084402.24585-2-s.hauer@pengutronix.de \
    --to=s.hauer@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