From patchwork Thu Mar 18 18:07:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12148953 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A6DCC433E9 for ; Thu, 18 Mar 2021 18:08:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2911964D74 for ; Thu, 18 Mar 2021 18:08:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232474AbhCRSIM (ORCPT ); Thu, 18 Mar 2021 14:08:12 -0400 Received: from foss.arm.com ([217.140.110.172]:45654 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230365AbhCRSIE (ORCPT ); Thu, 18 Mar 2021 14:08:04 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DB220101E; Thu, 18 Mar 2021 11:08:03 -0700 (PDT) Received: from localhost.localdomain (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3736B3F70D; Thu, 18 Mar 2021 11:08:02 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, drjones@redhat.com, alexandru.elisei@arm.com, andre.przywara@arm.com, Nikos Nikoleris Subject: [kvm-unit-tests PATCH v2 1/4] lib/string: Add strnlen, strrchr and strtoul Date: Thu, 18 Mar 2021 18:07:24 +0000 Message-Id: <20210318180727.116004-2-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210318180727.116004-1-nikos.nikoleris@arm.com> References: <20210318180727.116004-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This change adds two functions from and one from in preparation for an update in libfdt. Signed-off-by: Nikos Nikoleris --- lib/stdlib.h | 12 +++++++++ lib/string.h | 4 ++- lib/string.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 lib/stdlib.h diff --git a/lib/stdlib.h b/lib/stdlib.h new file mode 100644 index 0000000..e8abe75 --- /dev/null +++ b/lib/stdlib.h @@ -0,0 +1,12 @@ +/* + * Header for libc stdlib functions + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU Library General Public License version 2. + */ +#ifndef _STDLIB_H_ +#define _STDLIB_H_ + +unsigned long int strtoul(const char *nptr, char **endptr, int base); + +#endif /* _STDLIB_H_ */ diff --git a/lib/string.h b/lib/string.h index 493d51b..8da687e 100644 --- a/lib/string.h +++ b/lib/string.h @@ -7,12 +7,14 @@ #ifndef __STRING_H #define __STRING_H -extern unsigned long strlen(const char *buf); +extern size_t strlen(const char *buf); +extern size_t strnlen(const char *buf, size_t maxlen); extern char *strcat(char *dest, const char *src); extern char *strcpy(char *dest, const char *src); 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 *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/string.c b/lib/string.c index 75257f5..f77881f 100644 --- a/lib/string.c +++ b/lib/string.c @@ -6,16 +6,26 @@ */ #include "libcflat.h" +#include "stdlib.h" -unsigned long strlen(const char *buf) +size_t strlen(const char *buf) { - unsigned long len = 0; + size_t len = 0; while (*buf++) ++len; return len; } +size_t strnlen(const char *buf, size_t maxlen) +{ + const char *sc; + + for (sc = buf; maxlen-- && *sc != '\0'; ++sc) + /* nothing */; + return sc - buf; +} + char *strcat(char *dest, const char *src) { char *p = dest; @@ -55,6 +65,16 @@ char *strchr(const char *s, int c) return (char *)s; } +char *strrchr(const char *s, int c) +{ + const char *last = NULL; + do { + if (*s == (char)c) + last = s; + } while (*s++); + return (char *)last; +} + char *strstr(const char *s1, const char *s2) { size_t l1, l2; @@ -135,13 +155,21 @@ void *memchr(const void *s, int c, size_t n) return NULL; } -long atol(const char *ptr) +static int isspace(int c) +{ + return c == ' ' || c == '\t' || c == '\f' || c == '\n' || c == '\r'; +} + +unsigned long int strtoul(const char *nptr, char **endptr, int base) { long acc = 0; - const char *s = ptr; + const char *s = nptr; int neg, c; - while (*s == ' ' || *s == '\t') + if (base < 0 || base == 1 || base > 32) + goto out; // errno = EINVAL + + while (isspace(*s)) s++; if (*s == '-'){ neg = 1; @@ -152,20 +180,46 @@ long atol(const char *ptr) s++; } + if (base == 0 || base == 16) { + if (*s == '0') { + s++; + if (*s == 'x') { + s++; + base = 16; + } else if (base == 0) + base = 8; + } else if (base == 0) + base = 10; + } + while (*s) { - if (*s < '0' || *s > '9') + if (*s >= '0' && *s < '0' + base && *s <= '9') + c = *s - '0'; + else if (*s >= 'a' && *s < 'a' + base - 10) + c = *s - 'a' + 10; + else if (*s >= 'A' && *s < 'A' + base - 10) + c = *s - 'A' + 10; + else break; - c = *s - '0'; - acc = acc * 10 + c; + acc = acc * base + c; s++; } if (neg) acc = -acc; + out: + if (endptr) + *endptr = (char *)s; + return acc; } +long atol(const char *ptr) +{ + return strtoul(ptr, NULL, 10); +} + extern char **environ; char *getenv(const char *name) From patchwork Thu Mar 18 18:07:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12148951 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B1F5C433DB for ; Thu, 18 Mar 2021 18:08:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C522064F2A for ; Thu, 18 Mar 2021 18:08:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232476AbhCRSIN (ORCPT ); Thu, 18 Mar 2021 14:08:13 -0400 Received: from foss.arm.com ([217.140.110.172]:45668 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231590AbhCRSII (ORCPT ); Thu, 18 Mar 2021 14:08:08 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8414231B; Thu, 18 Mar 2021 11:08:08 -0700 (PDT) Received: from localhost.localdomain (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 741DC3F70D; Thu, 18 Mar 2021 11:08:07 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, drjones@redhat.com, alexandru.elisei@arm.com, andre.przywara@arm.com, Nikos Nikoleris Subject: [kvm-unit-tests PATCH v2 3/4] Makefile: Remove overriding recipe for libfdt_clean Date: Thu, 18 Mar 2021 18:07:26 +0000 Message-Id: <20210318180727.116004-4-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210318180727.116004-1-nikos.nikoleris@arm.com> References: <20210318180727.116004-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org libfdt_clean is now defined in the libfdt Makefile and as a result, we don't need to redefine it in the top Makefile. In addition we define some variables for the remaining libfdt_clean rule. Signed-off-by: Nikos Nikoleris --- Makefile | 16 +++++++--------- arm/Makefile.common | 2 +- powerpc/Makefile.common | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index e0828fe..24b7917 100644 --- a/Makefile +++ b/Makefile @@ -40,8 +40,6 @@ cflatobjs := \ LIBFDT_objdir = lib/libfdt LIBFDT_srcdir = $(SRCDIR)/lib/libfdt LIBFDT_archive = $(LIBFDT_objdir)/libfdt.a -LIBFDT_include = $(addprefix $(LIBFDT_srcdir)/,$(LIBFDT_INCLUDES)) -LIBFDT_version = $(addprefix $(LIBFDT_srcdir)/,$(LIBFDT_VERSION)) OBJDIRS += $(LIBFDT_objdir) @@ -90,6 +88,10 @@ $(LIBFDT_archive): CFLAGS += -ffreestanding -I $(SRCDIR)/lib -I $(SRCDIR)/lib/li $(LIBFDT_archive): $(addprefix $(LIBFDT_objdir)/,$(LIBFDT_OBJS)) $(AR) rcs $@ $^ +libfdt_clean: VECHO = echo " " +libfdt_clean: STD_CLEANFILES = *.o .*.d +libfdt_clean: LIBFDT_dir = $(LIBFDT_objdir) +libfdt_clean: SHAREDLIB_EXT = so # Build directory target .PHONY: directories @@ -110,15 +112,11 @@ install: standalone mkdir -p $(DESTDIR) install tests/* $(DESTDIR) -clean: arch_clean +clean: arch_clean libfdt_clean + $(RM) $(LIBFDT_archive) $(RM) lib/.*.d $(libcflat) $(cflatobjs) -libfdt_clean: - $(RM) $(LIBFDT_archive) \ - $(addprefix $(LIBFDT_objdir)/,$(LIBFDT_OBJS)) \ - $(LIBFDT_objdir)/.*.d - -distclean: clean libfdt_clean +distclean: clean $(RM) lib/asm lib/config.h config.mak $(TEST_DIR)-run msr.out cscope.* build-head $(RM) -r tests logs logs.old diff --git a/arm/Makefile.common b/arm/Makefile.common index a123e85..55478ec 100644 --- a/arm/Makefile.common +++ b/arm/Makefile.common @@ -78,7 +78,7 @@ FLATLIBS = $(libcflat) $(LIBFDT_archive) $(libgcc) $(libeabi) $(libeabi): $(eabiobjs) $(AR) rcs $@ $^ -arm_clean: libfdt_clean asm_offsets_clean +arm_clean: asm_offsets_clean $(RM) $(TEST_DIR)/*.{o,flat,elf} $(libeabi) $(eabiobjs) \ $(TEST_DIR)/.*.d lib/arm/.*.d diff --git a/powerpc/Makefile.common b/powerpc/Makefile.common index ac3cab6..4c3121a 100644 --- a/powerpc/Makefile.common +++ b/powerpc/Makefile.common @@ -73,7 +73,7 @@ $(TEST_DIR)/boot_rom.elf: $(TEST_DIR)/boot_rom.o $(LD) -EB -nostdlib -Ttext=0x100 --entry=start --build-id=none -o $@ $< @chmod a-x $@ -powerpc_clean: libfdt_clean asm_offsets_clean +powerpc_clean: asm_offsets_clean $(RM) $(TEST_DIR)/*.{o,elf} $(TEST_DIR)/boot_rom.bin \ $(TEST_DIR)/.*.d lib/powerpc/.*.d From patchwork Thu Mar 18 18:07:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikos Nikoleris X-Patchwork-Id: 12148955 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B3E7C43381 for ; Thu, 18 Mar 2021 18:08:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 41A8264F2A for ; Thu, 18 Mar 2021 18:08:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232481AbhCRSIN (ORCPT ); Thu, 18 Mar 2021 14:08:13 -0400 Received: from foss.arm.com ([217.140.110.172]:45674 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231701AbhCRSIL (ORCPT ); Thu, 18 Mar 2021 14:08:11 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 27DAE31B; Thu, 18 Mar 2021 11:08:11 -0700 (PDT) Received: from localhost.localdomain (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2B4983F70D; Thu, 18 Mar 2021 11:08:09 -0700 (PDT) From: Nikos Nikoleris To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, drjones@redhat.com, alexandru.elisei@arm.com, andre.przywara@arm.com, Nikos Nikoleris Subject: [kvm-unit-tests PATCH v2 4/4] devicetree: Parse correctly the stdout-path Date: Thu, 18 Mar 2021 18:07:27 +0000 Message-Id: <20210318180727.116004-5-nikos.nikoleris@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210318180727.116004-1-nikos.nikoleris@arm.com> References: <20210318180727.116004-1-nikos.nikoleris@arm.com> MIME-Version: 1.0 X-ARM-No-Footer: FoSSMail Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 --- 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;