aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/Makefile6
-rw-r--r--kernel/include/printf.h3
-rw-r--r--kernel/include/stdlib.h7
-rw-r--r--kernel/include/string.h13
-rw-r--r--kernel/l0/dbglog.c2
-rw-r--r--kernel/lib/stdlib.c8
-rw-r--r--kernel/lib/string.c68
7 files changed, 88 insertions, 19 deletions
diff --git a/kernel/Makefile b/kernel/Makefile
index 6e973dd..04721ba 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -5,11 +5,11 @@ ASFLAGS = -felf
CC = i586-elf-gcc
CFLAGS = -ffreestanding -O2 -std=gnu99 -Wall -Wextra -I . -I ./include
# CXX = i586-elf-g++
-# CXFLAGS = -ffreestanding -O2 -Wall -Wextra -I . -I ./include -fno-exceptions -fno-rtti
+# CXFLAGS = -ffreestanding -O3 -Wall -Wextra -I . -I ./include -fno-exceptions -fno-rtti
LD = i586-elf-gcc
LDFLAGS = -T linker.ld -ffreestanding -O2 -nostdlib -lgcc
-OBJ = lib/stdlib.o lib/printf.o l0/loader.o l0/kmain.o l0/dbglog.o l0/sys.o
+OBJ = lib/string.o lib/printf.o l0/loader.o l0/kmain.o l0/dbglog.o l0/sys.o
OUT = kernel.bin
all: $(OUT)
@@ -30,3 +30,5 @@ clean:
rm */*.o || true
mrproper: clean
rm $(OUT)
+
+rebuild: mrproper all
diff --git a/kernel/include/printf.h b/kernel/include/printf.h
index 4569827..b37816d 100644
--- a/kernel/include/printf.h
+++ b/kernel/include/printf.h
@@ -1,6 +1,7 @@
#pragma once
-#include <stdlib.h>
+#include <stddef.h>
+#include <stdint.h>
#include <stdarg.h>
int snprintf(char* s, size_t n, const char* format, ...);
diff --git a/kernel/include/stdlib.h b/kernel/include/stdlib.h
deleted file mode 100644
index 601e7c4..0000000
--- a/kernel/include/stdlib.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#pragma once
-
-#include <stddef.h>
-#include <stdint.h>
-
-size_t strlen(const char*);
-
diff --git a/kernel/include/string.h b/kernel/include/string.h
new file mode 100644
index 0000000..1eb3eef
--- /dev/null
+++ b/kernel/include/string.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <stddef.h>
+#include <stdint.h>
+
+void *memcpy(void *dest, const void *src, int count);
+void *memset(void *dest, int val, int count);
+
+size_t strlen(const char *str);
+char *strchr(const char *str, char c);
+char *strcpy(char *dest, const char *src);
+char *strcat(char *dest, const char *src);
+int strcmp(const char *s1, const char *s2);
diff --git a/kernel/l0/dbglog.c b/kernel/l0/dbglog.c
index 0285002..47098a0 100644
--- a/kernel/l0/dbglog.c
+++ b/kernel/l0/dbglog.c
@@ -1,5 +1,5 @@
#include <stdarg.h>
-#include <stdlib.h>
+#include <string.h>
#include <printf.h>
#include <dbglog.h>
#include <config.h>
diff --git a/kernel/lib/stdlib.c b/kernel/lib/stdlib.c
deleted file mode 100644
index 6710da2..0000000
--- a/kernel/lib/stdlib.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#include <stdlib.h>
-
-size_t strlen(const char* str) {
- size_t ret = 0;
- while (str[ret] != 0)
- ret++;
- return ret;
-}
diff --git a/kernel/lib/string.c b/kernel/lib/string.c
new file mode 100644
index 0000000..da8f60e
--- /dev/null
+++ b/kernel/lib/string.c
@@ -0,0 +1,68 @@
+#include <string.h>
+
+
+size_t strlen(const char* str) {
+ size_t ret = 0;
+ while (str[ret] != 0)
+ ret++;
+ return ret;
+}
+
+char *strchr(const char *str, char c) {
+ while (*str) {
+ if (*str == c) return (char*)str;
+ str++;
+ }
+ return NULL;
+}
+
+char *strcpy(char *dest, const char *src) {
+ memcpy(dest, src, strlen(src) + 1);
+ return (char*)src;
+}
+
+char *strcat(char *dest, const char *src) {
+ char *dest2 = dest;
+ dest2 += strlen(dest) - 1;
+ while (*src) {
+ *dest2 = *src;
+ src++;
+ dest2++;
+ }
+ *dest2 = 0;
+ return dest;
+}
+
+int strcmp(const char *s1, const char *s2) {
+ while ((*s1) && (*s1 == *s2)) {
+ s1++;
+ s2++;
+ }
+ return (* (unsigned char*)s1 - *(unsigned char*)s2);
+}
+
+void *memcpy(void *vd, const void *vs, int count) {
+ uint8_t *dest = (uint8_t*)vd, *src = (uint8_t*)vs;
+ int f = count % 4, n = count / 4, i;
+ const uint32_t* s = (uint32_t*)src;
+ uint32_t* d = (uint32_t*)dest;
+ for (i = 0; i < n; i++) {
+ d[i] = s[i];
+ }
+ if (f != 0) {
+ for (i = count - f; i < count; i++) {
+ dest[i] = src[i];
+ }
+ }
+ return vd;
+}
+
+void *memset(void *dest, int val, int count) {
+ uint8_t *dest_c = (uint8_t*)dest;
+ int i;
+ for (i = 0; i < count; i++) {
+ dest_c[i] = val;
+ }
+ return dest;
+}
+