[kvm-unit-tests PATCH v2 4/4] devicetree: Parse correctly the stdout-path

Nikos Nikoleris
2021-03-18T18:08:46Z

The stdout-path property in the device tree is a string of the form

"UART_NODE:OPTS"

Where UART_NODE is a compatible serial port and OPTS specify
parameters such as the baud. Fix the way we parse the node and, at
least for now, ignore options as we don't act on them.

Signed-off-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
---
lib/string.h | 1 +
lib/devicetree.c | 15 +++++++++------
lib/string.c | 7 +++++++
3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/lib/string.h b/lib/string.h
index 8da687e..e1febfe 100644
--- a/lib/string.h
+++ b/lib/string.h
@@ -15,6 +15,7 @@ extern int strcmp(const char *a, const char *b);
extern int strncmp(const char *a, const char *b, size_t n);
extern char *strchr(const char *s, int c);
extern char *strrchr(const char *s, int c);
+extern char *strchrnul(const char *s, int c);
extern char *strstr(const char *haystack, const char *needle);
extern void *memset(void *s, int c, size_t n);
extern void *memcpy(void *dest, const void *src, size_t n);
diff --git a/lib/devicetree.c b/lib/devicetree.c
index 1020324..409d18b 100644
--- a/lib/devicetree.c
+++ b/lib/devicetree.c
@@ -265,21 +265,24 @@ int dt_get_bootargs(const char **bootargs)

int dt_get_default_console_node(void)
{
- const struct fdt_property *prop;
+ const char *p, *q;
int node, len;

node = fdt_path_offset(fdt, "/chosen");
if (node < 0)
return node;

- prop = fdt_get_property(fdt, node, "stdout-path", &len);
- if (!prop) {
- prop = fdt_get_property(fdt, node, "linux,stdout-path", &len);
- if (!prop)
+ p = fdt_getprop(fdt, node, "stdout-path", &len);
+ if (!p) {
+ p = fdt_getprop(fdt, node, "linux,stdout-path", &len);
+ if (!p)
return len;
}

- return fdt_path_offset(fdt, prop->data);
+ q = strchrnul(p, ':');
+ len = q - p;
+
+ return fdt_path_offset_namelen(fdt, p, len);
}

int dt_get_initrd(const char **initrd, u32 *size)
diff --git a/lib/string.c b/lib/string.c
index f77881f..30592c5 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -75,6 +75,13 @@ char *strrchr(const char *s, int c)
return (char *)last;
}

+char *strchrnul(const char *s, int c)
+{
+ while (*s && *s != (char)c)
+ s++;
+ return (char *)s;
+}
+
char *strstr(const char *s1, const char *s2)
{
size_t l1, l2;
--
2.25.1