mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <ahmad@a3f.at>
To: barebox@lists.infradead.org
Cc: Jules Maselbas <jmaselbas@kalray.eu>, Ahmad Fatoum <ahmad@a3f.at>
Subject: [PATCH v2 1/4] PBL: fdt: implement fdt_device_get_match_data
Date: Sat, 10 Apr 2021 13:06:35 +0200	[thread overview]
Message-ID: <20210410110638.2106658-1-ahmad@a3f.at> (raw)

Currently, the generic DT image can't properly have a PBL console,
because it's only known at runtime what system we are running on.

As we already parse the FDT in the PBL to get the memory regions, we
could extract the board compatible as well and determine which UART to
use. Add a helper to achieve this.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
v1 -> v2:
  - use strcasecmp (Jules)
---
 include/pbl.h |  9 +++++++++
 pbl/fdt.c     | 35 +++++++++++++++++++++++++++++++++++
 pbl/string.c  | 12 ++++++++++++
 3 files changed, 56 insertions(+)

diff --git a/include/pbl.h b/include/pbl.h
index 194d5e750839..f58daec7351a 100644
--- a/include/pbl.h
+++ b/include/pbl.h
@@ -34,4 +34,13 @@ ssize_t pbl_fat_load(struct pbl_bio *, const char *filename, void *dest, size_t
 
 void fdt_find_mem(const void *fdt, unsigned long *membase, unsigned long *memsize);
 
+struct fdt_device_id {
+	const char *compatible;
+	const void *data;
+};
+
+const void *
+fdt_device_get_match_data(const void *fdt, const char *nodepath,
+			  const struct fdt_device_id ids[]);
+
 #endif /* __PBL_H__ */
diff --git a/pbl/fdt.c b/pbl/fdt.c
index b4a40a514b8b..18ddb9f48a6e 100644
--- a/pbl/fdt.c
+++ b/pbl/fdt.c
@@ -68,3 +68,38 @@ err:
 	pr_err("No memory, cannot continue\n");
 	while (1);
 }
+
+const void *fdt_device_get_match_data(const void *fdt, const char *nodepath,
+				      const struct fdt_device_id ids[])
+{
+	int node, length;
+	const char *list, *end;
+	const struct fdt_device_id *id;
+
+	node = fdt_path_offset(fdt, nodepath);
+	if (node < 0)
+		return NULL;
+
+	list = fdt_getprop(fdt, node, "compatible", &length);
+	if (!list)
+		return NULL;
+
+	end = list + length;
+
+	while (list < end) {
+		length = strnlen(list, end - list) + 1;
+
+		/* Abort if the last string isn't properly NUL-terminated. */
+		if (list + length > end)
+			return NULL;
+
+		for (id = ids; id->compatible; id++) {
+			if (!strcasecmp(list, id->compatible))
+				return id->data;
+		}
+
+		list += length;
+	}
+
+	return NULL;
+}
diff --git a/pbl/string.c b/pbl/string.c
index e6c0997ebc6d..e96eb99fc22d 100644
--- a/pbl/string.c
+++ b/pbl/string.c
@@ -7,6 +7,7 @@
 #include <linux/types.h>
 #include <linux/string.h>
 #include <linux/compiler.h>
+#include <linux/ctype.h>
 
 void *memcpy(void *__dest, __const void *__src, size_t __n)
 {
@@ -98,6 +99,17 @@ int strcmp(const char *cs, const char *ct)
 	return res;
 }
 
+int strcasecmp(const char *s1, const char *s2)
+{
+	int c1, c2;
+
+	do {
+		c1 = tolower(*s1++);
+		c2 = tolower(*s2++);
+	} while (c1 == c2 && c1 != 0);
+	return c1 - c2;
+}
+
 void *memchr(const void *s, int c, size_t count)
 {
 	const unsigned char *p = s;
-- 
2.30.0


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


             reply	other threads:[~2021-04-10 11:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-10 11:06 Ahmad Fatoum [this message]
2021-04-10 11:06 ` [PATCH v2 2/4] RISC-V: debug_ll: ns16550: split off debug_ll from generic parts Ahmad Fatoum
2021-04-10 11:06 ` [PATCH v2 3/4] RISC-V: board-dt-2nd: add PBL console support for virt Ahmad Fatoum
2021-04-15  7:15   ` Sascha Hauer
2021-04-15  7:53     ` Ahmad Fatoum
2021-04-10 11:06 ` [PATCH v2 4/4] RISC-V: delete unused mach-virt subdirectory Ahmad Fatoum

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=20210410110638.2106658-1-ahmad@a3f.at \
    --to=ahmad@a3f.at \
    --cc=barebox@lists.infradead.org \
    --cc=jmaselbas@kalray.eu \
    /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