From 98aaf8317b74414afa78078bc056670f46c45f41 Mon Sep 17 00:00:00 2001
From: Atish Patra <atish.patra@wdc.com>
Date: Tue, 18 Jun 2019 14:54:02 -0700
Subject: lib: Include helper libc functions directly in libsbi.

libsbi needs some of the custom libc functions. It should be directly
included in libsbi instead of platform specific libraries.

Signed-off-by: Atish Patra <atish.patra@wdc.com>
Acked-by: Anup Patel <anup.patel@wdc.com>
---
 include/sbi/string.h                  |  39 ++++++++
 lib/sbi/libc/objects.mk               |  15 +++
 lib/sbi/libc/string.c                 | 175 ++++++++++++++++++++++++++++++++++
 lib/sbi/objects.mk                    |   3 -
 lib/sbi/sbi_fifo.c                    |   2 +-
 lib/sbi/sbi_ipi.c                     |   2 +-
 lib/sbi/sbi_tlb.c                     |   2 +-
 platform/common/include/plat/string.h |  39 --------
 platform/common/irqchip/plic.c        |   2 +-
 platform/common/libc/objects.mk       |  15 ---
 platform/common/libc/string.c         | 175 ----------------------------------
 platform/common/libfdt/libfdt_env.h   |   2 +-
 platform/common/tinyfdt.c             |   2 +-
 13 files changed, 235 insertions(+), 238 deletions(-)
 create mode 100644 include/sbi/string.h
 create mode 100644 lib/sbi/libc/objects.mk
 create mode 100644 lib/sbi/libc/string.c
 delete mode 100644 platform/common/include/plat/string.h
 delete mode 100644 platform/common/libc/objects.mk
 delete mode 100644 platform/common/libc/string.c

diff --git a/include/sbi/string.h b/include/sbi/string.h
new file mode 100644
index 0000000..8259ef5
--- /dev/null
+++ b/include/sbi/string.h
@@ -0,0 +1,39 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Atish Patra <atish.patra@wdc.com>
+ */
+
+#ifndef __STRING_H__
+#define __STRING_H__
+
+#include <sbi/sbi_types.h>
+
+int strcmp(const char *a, const char *b);
+
+size_t strlen(const char *str);
+
+size_t strnlen(const char *str, size_t count);
+
+char *strcpy(char *dest, const char *src);
+
+char *strncpy(char *dest, const char *src, size_t count);
+
+char *strchr(const char *s, int c);
+
+char *strrchr(const char *s, int c);
+
+void *memset(void *s, int c, size_t count);
+
+void *memcpy(void *dest, const void *src, size_t count);
+
+void *memmove(void *dest, const void *src, size_t count);
+
+int memcmp(const void *s1, const void *s2, size_t count);
+
+void *memchr(const void *s, int c, size_t count);
+
+#endif
diff --git a/lib/sbi/libc/objects.mk b/lib/sbi/libc/objects.mk
new file mode 100644
index 0000000..7a6ebbe
--- /dev/null
+++ b/lib/sbi/libc/objects.mk
@@ -0,0 +1,15 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2019 Western Digital Corporation or its affiliates.
+#
+# Authors:
+#   Atish Patra<atish.patra@wdc.com>
+#
+
+libc_files = string.o
+
+$(foreach file, $(libc_files), \
+	$(eval CFLAGS_$(file) = -I$(src)/../../sbi/libc))
+
+libsbi-objs-y += $(addprefix libc/,$(libc_files))
diff --git a/lib/sbi/libc/string.c b/lib/sbi/libc/string.c
new file mode 100644
index 0000000..b5743fe
--- /dev/null
+++ b/lib/sbi/libc/string.c
@@ -0,0 +1,175 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ *   Atish Patra <atish.patra@wdc.com>
+ */
+
+/*
+ * Simple libc functions. These are not optimized at all and might have some
+ * bugs as well. Use any optimized routines from newlib or glibc if required.
+ */
+
+#include <sbi/string.h>
+
+int strcmp(const char *a, const char *b)
+{
+	/* search first diff or end of string */
+	for (; *a == *b && *a != '\0'; a++, b++)
+		;
+
+	return *a - *b;
+}
+
+size_t strlen(const char *str)
+{
+	unsigned long ret = 0;
+
+	while (*str != '\0') {
+		ret++;
+		str++;
+	}
+
+	return ret;
+}
+
+size_t strnlen(const char *str, size_t count)
+{
+	unsigned long ret = 0;
+
+	while (*str != '\0' && ret < count) {
+		ret++;
+		str++;
+		count--;
+	}
+
+	return ret;
+}
+
+char *strcpy(char *dest, const char *src)
+{
+	char *ret = dest;
+
+	while (*src != '\0') {
+		*dest++ = *src++;
+	}
+
+	return ret;
+}
+
+char *strncpy(char *dest, const char *src, size_t count)
+{
+	char *ret = dest;
+
+	while (count-- && *src != '\0') {
+		*dest++ = *src++;
+	}
+
+	return ret;
+}
+
+char *strchr(const char *s, int c)
+{
+	while (*s != '\0' && *s != (char)c)
+		s++;
+
+	if (*s == '\0')
+		return NULL;
+	else
+		return (char *)s;
+}
+
+char *strrchr(const char *s, int c)
+{
+	const char *last = s + strlen(s);
+
+	while (last > s && *last != (char)c)
+		last--;
+
+	if (*last != (char)c)
+		return NULL;
+	else
+		return (char *)last;
+}
+void *memset(void *s, int c, size_t count)
+{
+	char *temp = s;
+
+	while (count > 0) {
+		count--;
+		*temp++ = c;
+	}
+
+	return s;
+}
+
+void *memcpy(void *dest, const void *src, size_t count)
+{
+	char *temp1	  = dest;
+	const char *temp2 = src;
+
+	while (count > 0) {
+		*temp1++ = *temp2++;
+		count--;
+	}
+
+	return dest;
+}
+
+void *memmove(void *dest, const void *src, size_t count)
+{
+	char *temp1	  = (char *)dest;
+	const char *temp2 = (char *)src;
+
+	if (src == dest)
+		return dest;
+
+	if (dest < src) {
+		while (count > 0) {
+			*temp1++ = *temp2++;
+			count--;
+		}
+	} else {
+		temp1 = dest + count - 1;
+		temp2 = src + count - 1;
+
+		while (count > 0) {
+			*temp1-- = *temp2--;
+			count--;
+		}
+	}
+
+	return dest;
+}
+
+int memcmp(const void *s1, const void *s2, size_t count)
+{
+	const char *temp1 = s1;
+	const char *temp2 = s2;
+
+	for (; count > 0 && (*temp1 == *temp2); count--) {
+		temp1++;
+		temp2++;
+	}
+
+	if (count > 0)
+		return *(unsigned char *)temp1 - *(unsigned char *)temp2;
+	else
+		return 0;
+}
+
+void *memchr(const void *s, int c, size_t count)
+{
+	const unsigned char *temp = s;
+
+	while (count > 0) {
+		if ((unsigned char)c == *temp++) {
+			return (void *)(temp - 1);
+		}
+		count--;
+	}
+
+	return NULL;
+}
diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk
index 817e805..d0fd856 100644
--- a/lib/sbi/objects.mk
+++ b/lib/sbi/objects.mk
@@ -27,6 +27,3 @@ libsbi-objs-y += sbi_system.o
 libsbi-objs-y += sbi_timer.o
 libsbi-objs-y += sbi_tlb.o
 libsbi-objs-y += sbi_trap.o
-
-# External Libraries to include
-PLATFORM_INCLUDE_LIBC=y
diff --git a/lib/sbi/sbi_fifo.c b/lib/sbi/sbi_fifo.c
index a92b46c..e823d2a 100644
--- a/lib/sbi/sbi_fifo.c
+++ b/lib/sbi/sbi_fifo.c
@@ -10,7 +10,7 @@
 #include <sbi/riscv_locks.h>
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_fifo.h>
-#include <plat/string.h>
+#include <sbi/string.h>
 
 void sbi_fifo_init(struct sbi_fifo *fifo, void *queue_mem, u16 entries,
 		   u16 entry_size)
diff --git a/lib/sbi/sbi_ipi.c b/lib/sbi/sbi_ipi.c
index 48d5b22..19da029 100644
--- a/lib/sbi/sbi_ipi.c
+++ b/lib/sbi/sbi_ipi.c
@@ -19,7 +19,7 @@
 #include <sbi/sbi_platform.h>
 #include <sbi/sbi_timer.h>
 #include <sbi/sbi_tlb.h>
-#include <plat/string.h>
+#include <sbi/string.h>
 
 static unsigned long ipi_data_off;
 
diff --git a/lib/sbi/sbi_tlb.c b/lib/sbi/sbi_tlb.c
index 814d402..1031df3 100644
--- a/lib/sbi/sbi_tlb.c
+++ b/lib/sbi/sbi_tlb.c
@@ -16,7 +16,7 @@
 #include <sbi/sbi_bitops.h>
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_tlb.h>
-#include <plat/string.h>
+#include <sbi/string.h>
 
 static unsigned long ipi_tlb_fifo_off;
 static unsigned long ipi_tlb_fifo_mem_off;
diff --git a/platform/common/include/plat/string.h b/platform/common/include/plat/string.h
deleted file mode 100644
index 8259ef5..0000000
--- a/platform/common/include/plat/string.h
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 Western Digital Corporation or its affiliates.
- *
- * Authors:
- *   Atish Patra <atish.patra@wdc.com>
- */
-
-#ifndef __STRING_H__
-#define __STRING_H__
-
-#include <sbi/sbi_types.h>
-
-int strcmp(const char *a, const char *b);
-
-size_t strlen(const char *str);
-
-size_t strnlen(const char *str, size_t count);
-
-char *strcpy(char *dest, const char *src);
-
-char *strncpy(char *dest, const char *src, size_t count);
-
-char *strchr(const char *s, int c);
-
-char *strrchr(const char *s, int c);
-
-void *memset(void *s, int c, size_t count);
-
-void *memcpy(void *dest, const void *src, size_t count);
-
-void *memmove(void *dest, const void *src, size_t count);
-
-int memcmp(const void *s1, const void *s2, size_t count);
-
-void *memchr(const void *s, int c, size_t count);
-
-#endif
diff --git a/platform/common/irqchip/plic.c b/platform/common/irqchip/plic.c
index 90a7820..e3cdb17 100644
--- a/platform/common/irqchip/plic.c
+++ b/platform/common/irqchip/plic.c
@@ -10,7 +10,7 @@
 #include <sbi/riscv_io.h>
 #include <sbi/riscv_encoding.h>
 #include <sbi/sbi_console.h>
-#include <plat/string.h>
+#include <sbi/string.h>
 #include <plat/tinyfdt.h>
 #include <plat/irqchip/plic.h>
 
diff --git a/platform/common/libc/objects.mk b/platform/common/libc/objects.mk
deleted file mode 100644
index 1dcfe8e..0000000
--- a/platform/common/libc/objects.mk
+++ /dev/null
@@ -1,15 +0,0 @@
-#
-# SPDX-License-Identifier: BSD-2-Clause
-#
-# Copyright (c) 2019 Western Digital Corporation or its affiliates.
-#
-# Authors:
-#   Atish Patra<atish.patra@wdc.com>
-#
-
-libc_files = string.o
-
-$(foreach file, $(libc_files), \
-	$(eval CFLAGS_$(file) = -I$(src)/../../common/libc))
-
-platform-common-objs-$(PLATFORM_INCLUDE_LIBC) += $(addprefix libc/,$(libc_files))
diff --git a/platform/common/libc/string.c b/platform/common/libc/string.c
deleted file mode 100644
index 6e273d0..0000000
--- a/platform/common/libc/string.c
+++ /dev/null
@@ -1,175 +0,0 @@
-/*
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019 Western Digital Corporation or its affiliates.
- *
- * Authors:
- *   Atish Patra <atish.patra@wdc.com>
- */
-
-/*
- * Simple libc functions. These are not optimized at all and might have some
- * bugs as well. Use any optimized routines from newlib or glibc if required.
- */
-
-#include <plat/string.h>
-
-int strcmp(const char *a, const char *b)
-{
-	/* search first diff or end of string */
-	for (; *a == *b && *a != '\0'; a++, b++)
-		;
-
-	return *a - *b;
-}
-
-size_t strlen(const char *str)
-{
-	unsigned long ret = 0;
-
-	while (*str != '\0') {
-		ret++;
-		str++;
-	}
-
-	return ret;
-}
-
-size_t strnlen(const char *str, size_t count)
-{
-	unsigned long ret = 0;
-
-	while (*str != '\0' && ret < count) {
-		ret++;
-		str++;
-		count--;
-	}
-
-	return ret;
-}
-
-char *strcpy(char *dest, const char *src)
-{
-	char *ret = dest;
-
-	while (*src != '\0') {
-		*dest++ = *src++;
-	}
-
-	return ret;
-}
-
-char *strncpy(char *dest, const char *src, size_t count)
-{
-	char *ret = dest;
-
-	while (count-- && *src != '\0') {
-		*dest++ = *src++;
-	}
-
-	return ret;
-}
-
-char *strchr(const char *s, int c)
-{
-	while (*s != '\0' && *s != (char)c)
-		s++;
-
-	if (*s == '\0')
-		return NULL;
-	else
-		return (char *)s;
-}
-
-char *strrchr(const char *s, int c)
-{
-	const char *last = s + strlen(s);
-
-	while (last > s && *last != (char)c)
-		last--;
-
-	if (*last != (char)c)
-		return NULL;
-	else
-		return (char *)last;
-}
-void *memset(void *s, int c, size_t count)
-{
-	char *temp = s;
-
-	while (count > 0) {
-		count--;
-		*temp++ = c;
-	}
-
-	return s;
-}
-
-void *memcpy(void *dest, const void *src, size_t count)
-{
-	char *temp1	  = dest;
-	const char *temp2 = src;
-
-	while (count > 0) {
-		*temp1++ = *temp2++;
-		count--;
-	}
-
-	return dest;
-}
-
-void *memmove(void *dest, const void *src, size_t count)
-{
-	char *temp1	  = (char *)dest;
-	const char *temp2 = (char *)src;
-
-	if (src == dest)
-		return dest;
-
-	if (dest < src) {
-		while (count > 0) {
-			*temp1++ = *temp2++;
-			count--;
-		}
-	} else {
-		temp1 = dest + count - 1;
-		temp2 = src + count - 1;
-
-		while (count > 0) {
-			*temp1-- = *temp2--;
-			count--;
-		}
-	}
-
-	return dest;
-}
-
-int memcmp(const void *s1, const void *s2, size_t count)
-{
-	const char *temp1 = s1;
-	const char *temp2 = s2;
-
-	for (; count > 0 && (*temp1 == *temp2); count--) {
-		temp1++;
-		temp2++;
-	}
-
-	if (count > 0)
-		return *(unsigned char *)temp1 - *(unsigned char *)temp2;
-	else
-		return 0;
-}
-
-void *memchr(const void *s, int c, size_t count)
-{
-	const unsigned char *temp = s;
-
-	while (count > 0) {
-		if ((unsigned char)c == *temp++) {
-			return (void *)(temp - 1);
-		}
-		count--;
-	}
-
-	return NULL;
-}
diff --git a/platform/common/libfdt/libfdt_env.h b/platform/common/libfdt/libfdt_env.h
index 2428fd5..adcd315 100644
--- a/platform/common/libfdt/libfdt_env.h
+++ b/platform/common/libfdt/libfdt_env.h
@@ -52,7 +52,7 @@
  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <plat/string.h>
+#include <sbi/string.h>
 #include <sbi/sbi_types.h>
 
 #define INT_MAX		((int)(~0U >> 1))
diff --git a/platform/common/tinyfdt.c b/platform/common/tinyfdt.c
index e62a809..73dd131 100644
--- a/platform/common/tinyfdt.c
+++ b/platform/common/tinyfdt.c
@@ -7,7 +7,7 @@
  *   Anup Patel <anup.patel@wdc.com>
  */
 
-#include <plat/string.h>
+#include <sbi/string.h>
 #include <plat/tinyfdt.h>
 
 #define FDT_MAGIC 0xd00dfeed
-- 
cgit v1.2.3