summaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorstefan <stefan@s00.xyz>2023-04-21 19:53:04 -0400
committerstefan <stefan@s00.xyz>2023-04-21 19:53:04 -0400
commiteff82c45c589b42061344039d5f2efc8ad7c52df (patch)
tree4c5d89eea3fa74caba5d3dbfb8567643f3747e6d /lib/libc
parent821706a3fea34c18a6171cf5169d5d6d852966de (diff)
downloadsv-eff82c45c589b42061344039d5f2efc8ad7c52df.tar.gz
libfdt port and the required libc functions
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/Makefile25
-rw-r--r--lib/libc/string/memchr.c11
-rw-r--r--lib/libc/string/memcmp.c15
-rw-r--r--lib/libc/string/memcpy.c11
-rw-r--r--lib/libc/string/memmove.c28
-rw-r--r--lib/libc/string/memset.c14
-rw-r--r--lib/libc/string/strchr.c20
7 files changed, 124 insertions, 0 deletions
diff --git a/lib/libc/Makefile b/lib/libc/Makefile
new file mode 100644
index 0000000..b2aea30
--- /dev/null
+++ b/lib/libc/Makefile
@@ -0,0 +1,25 @@
+BUILDDIR=build/${TARGET}
+TARGETDIR=../../mainboard/${TARGET}
+
+.if !exists(${TARGETDIR}/conf/mainboard.conf)
+.error 'ERROR: invalid target "${TARGET}"'
+.else
+.include <${TARGETDIR}/conf/mainboard.conf>
+.endif
+
+SRC=\
+ string/memcpy.c \
+ string/memmove.c \
+ string/memset.c \
+ string/memcmp.c \
+ string/memchr.c \
+
+
+${BUILDDIR}/libc.a: ${SRC}
+ mkdir -p ${BUILDDIR}
+ cd ${BUILDDIR} && ${CROSS_COMPILE}gcc -c ${CFLAGS} ${SRC:%=../../%}
+ cd ${BUILDDIR} && ${CROSS_COMPILE}ar -rcs ${@:T} ${SRC:T:%.c=%.o}
+
+
+clean:
+ rm -rf ${BUILDDIR}
diff --git a/lib/libc/string/memchr.c b/lib/libc/string/memchr.c
new file mode 100644
index 0000000..9da3265
--- /dev/null
+++ b/lib/libc/string/memchr.c
@@ -0,0 +1,11 @@
+#include <string.h>
+
+void *memchr(const void *s, int c, size_t n) {
+ const char *p = s;
+ while (n--) {
+ if ((char)c == *p++) {
+ return (void*)(p-1);
+ }
+ }
+ return NULL;
+}
diff --git a/lib/libc/string/memcmp.c b/lib/libc/string/memcmp.c
new file mode 100644
index 0000000..a3d47fb
--- /dev/null
+++ b/lib/libc/string/memcmp.c
@@ -0,0 +1,15 @@
+#include <string.h>
+
+int
+memcmp(const void *s1, const void *s2, size_t n)
+{
+ const char *str1 = (const char *)s1;
+ const char *str2 = (const char *)s2;
+ while (n--) {
+ if (*str1 != *str2)
+ return *str1 - *str2;
+ str1++; str2++;
+ }
+ return 0;
+}
+
diff --git a/lib/libc/string/memcpy.c b/lib/libc/string/memcpy.c
new file mode 100644
index 0000000..7573c01
--- /dev/null
+++ b/lib/libc/string/memcpy.c
@@ -0,0 +1,11 @@
+#include <string.h>
+
+void *memcpy(void *__restrict s1, const void *__restrict s2, size_t n)
+{
+ char *tmp = s1;
+ const char *s = s2;
+
+ while (n--)
+ *tmp++ = *s++;
+ return s1;
+}
diff --git a/lib/libc/string/memmove.c b/lib/libc/string/memmove.c
new file mode 100644
index 0000000..16be329
--- /dev/null
+++ b/lib/libc/string/memmove.c
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * linux/lib/string.c
+ *
+ * Copyright (C) 1991, 1992 Linus Torvalds
+ */
+#include <string.h>
+
+void *memmove(void *dest, const void *src, size_t count)
+{
+ char *tmp;
+ const char *s;
+
+ if (dest <= src) {
+ tmp = dest;
+ s = src;
+ while (count--)
+ *tmp++ = *s++;
+ } else {
+ tmp = dest;
+ tmp += count;
+ s = src;
+ s += count;
+ while (count--)
+ *--tmp = *--s;
+ }
+ return dest;
+}
diff --git a/lib/libc/string/memset.c b/lib/libc/string/memset.c
new file mode 100644
index 0000000..59e3bea
--- /dev/null
+++ b/lib/libc/string/memset.c
@@ -0,0 +1,14 @@
+#include <stddef.h>
+#include <string.h>
+
+void *
+memset(void *s, int c, size_t n)
+{
+ char *xs = s;
+
+ while (n--)
+ *xs++ = c;
+
+ return s;
+}
+
diff --git a/lib/libc/string/strchr.c b/lib/libc/string/strchr.c
new file mode 100644
index 0000000..09b4d43
--- /dev/null
+++ b/lib/libc/string/strchr.c
@@ -0,0 +1,20 @@
+#include <string.h>
+
+char *strchr(const char *s, int c) {
+ do {
+ if ((char)c == *s) {
+ return (char*)s;
+ }
+ } while (*s++ != '\0');
+ return NULL;
+}
+
+char *strrchr(const char *s, int c)
+{
+ const char *last = NULL;
+ do {
+ if (*s == (char)c)
+ last = s;
+ } while (*s++);
+ return (char *)last;
+}