aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarvin Borner2020-05-28 23:02:51 +0200
committerMarvin Borner2020-05-28 23:02:51 +0200
commitf600c79559892b73b019102478af501cf71fe6a4 (patch)
treedb5bca15169c9990e3c6ab52568d526d1e25bac5 /src
parent731ea56513bd199597a905eb1937e67cbd762b32 (diff)
Added vsprintf support for serial connections
Diffstat (limited to 'src')
-rw-r--r--src/kernel/lib/lib.h1
-rw-r--r--src/kernel/lib/memory.c13
-rw-r--r--src/kernel/lib/stdio.h4
-rw-r--r--src/kernel/lib/stdio/debug.c47
-rw-r--r--src/kernel/lib/stdio/sprintf.c58
-rw-r--r--src/kernel/lib/stdio/vprintf.c51
-rw-r--r--src/kernel/lib/stdio/vsprintf.c63
-rw-r--r--src/kernel/lib/stdlib.h1
-rw-r--r--src/kernel/lib/stdlib/itoa.c29
9 files changed, 121 insertions, 146 deletions
diff --git a/src/kernel/lib/lib.h b/src/kernel/lib/lib.h
index bdf0794..a71c929 100644
--- a/src/kernel/lib/lib.h
+++ b/src/kernel/lib/lib.h
@@ -38,5 +38,6 @@ int memory_init();
void memory_print();
u32 memory_get_all();
+void bss_clean();
#endif \ No newline at end of file
diff --git a/src/kernel/lib/memory.c b/src/kernel/lib/memory.c
index 0519492..40f6a1f 100644
--- a/src/kernel/lib/memory.c
+++ b/src/kernel/lib/memory.c
@@ -35,6 +35,7 @@ int memcmp(const void *a_ptr, const void *b_ptr, u32 size)
return 0;
}
+// TODO: Move memory lib!
u32 total = 0;
struct multiboot_tag_basic_meminfo *meminfo = NULL;
@@ -52,7 +53,7 @@ u32 memory_get_all()
u32 memory_get_free()
{
- return memory_get_all() /*- paging_get_used_pages() * 4*/;
+ return memory_get_all() - paging_get_used_pages() * 4;
}
void memory_print()
@@ -63,6 +64,7 @@ void memory_print()
info("Mem upper: 0x%x", meminfo->mem_upper);
}
info("Total memory found: %dMiB", (memory_get_all() >> 10) + 1);
+ info("Total used memory: %dMiB", ((memory_get_all() - memory_get_free()) >> 10) + 1);
info("Total free memory: %dMiB", (memory_get_free() >> 10) + 1);
}
@@ -116,4 +118,13 @@ int memory_init()
}
}
return ret;
+}
+
+void bss_clean()
+{
+ extern u8 bss_start, kernel_end;
+ u32 start = &bss_start;
+ u32 end = &kernel_end;
+ log("0x%x: 0x%x", start, end - start);
+ memset(start, 0, end - start);
} \ No newline at end of file
diff --git a/src/kernel/lib/stdio.h b/src/kernel/lib/stdio.h
index dff4044..3318ef9 100644
--- a/src/kernel/lib/stdio.h
+++ b/src/kernel/lib/stdio.h
@@ -6,13 +6,11 @@
void putch(char c);
int sprintf(char *str, const char *fmt, ...);
-
void vprintf(const char *fmt, va_list args);
-
+int vsprintf(char *str, const char *fmt, va_list args);
void printf(const char *fmt, ...);
void serial_vprintf(const char *fmt, va_list args);
-
void serial_printf(const char *fmt, ...);
#endif \ No newline at end of file
diff --git a/src/kernel/lib/stdio/debug.c b/src/kernel/lib/stdio/debug.c
index 3acb987..879329e 100644
--- a/src/kernel/lib/stdio/debug.c
+++ b/src/kernel/lib/stdio/debug.c
@@ -1,8 +1,7 @@
#include <io/io.h>
+#include <lib/lib.h>
+#include <lib/stdio.h>
#include <lib/stdlib.h>
-#include <lib/string.h>
-#include <memory/alloc.h>
-#include <stdarg.h>
#include <stdint.h>
void serial_print(const char *data)
@@ -13,44 +12,10 @@ void serial_print(const char *data)
void serial_vprintf(const char *fmt, va_list args)
{
- u8 readyToFormat = 0;
-
- char buff = 0;
-
- for (; *fmt; fmt++) {
- if (readyToFormat) {
- if (*fmt == '%') {
- serial_put('%');
- readyToFormat = 0;
- continue;
- }
-
- buff = *fmt;
- if (buff == 's') {
- const char *str = va_arg(args, const char *);
- serial_print(str);
- readyToFormat = 0;
- } else if (buff == 'x') {
- char *p = htoa((u32)va_arg(args, int));
- serial_print(p);
- free(p);
- readyToFormat = 0;
- } else if (buff == 'd') {
- char *p = itoa(va_arg(args, int));
- serial_print(p);
- free(p);
- readyToFormat = 0;
- } else if (buff == 'c') {
- serial_put((char)va_arg(args, int));
- readyToFormat = 0;
- }
- } else {
- if (*fmt == '%')
- readyToFormat = 1;
- else
- serial_put(*fmt);
- }
- }
+ char buf[1024];
+ memset(buf, 0, 1024);
+ vsprintf(buf, fmt, args);
+ serial_print(buf);
}
void serial_printf(const char *fmt, ...)
diff --git a/src/kernel/lib/stdio/sprintf.c b/src/kernel/lib/stdio/sprintf.c
index 0fb6eb5..8492363 100644
--- a/src/kernel/lib/stdio/sprintf.c
+++ b/src/kernel/lib/stdio/sprintf.c
@@ -1,63 +1,11 @@
-#include <lib/stdlib.h>
+#include <lib/stdio.h>
#include <stdarg.h>
-#include <stdint.h>
-
-static void append(char *dest, char *src, int index)
-{
- for (int i = index; i < strlen(src) + index; i++)
- dest[i] = src[i - index];
- dest[index + strlen(src)] = 0;
-}
int sprintf(char *str, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
-
- u8 ready_to_format = 0;
-
- int i = 0;
- char buf = 0;
- char format_buffer[20] = "\0";
-
- for (; *fmt; fmt++) {
- if (ready_to_format) {
- ready_to_format = 0;
-
- if (*fmt == '%') {
- str[i] = '%';
- continue;
- }
-
- buf = *fmt;
- if (buf == 's') {
- char *string = va_arg(args, char *);
- append(str, string, i);
- i = strlen(str);
- } else if (buf == 'x') {
- itoa_base((u32)va_arg(args, int), format_buffer, 16);
- append(str, format_buffer, i);
- i = strlen(str);
- } else if (buf == 'd') {
- itoa_base(va_arg(args, int), format_buffer, 10);
- append(str, format_buffer, i);
- i = strlen(str);
- } else if (buf == 'c') {
- str[i] = (char)va_arg(args, int);
- i++;
- }
- } else {
- if (*fmt == '%')
- ready_to_format = 1;
- else {
- str[i] = *fmt;
- i++;
- }
- }
-
- format_buffer[0] = '\0';
- }
-
+ int ret = vsprintf(str, fmt, args);
va_end(args);
- return strlen(str);
+ return ret;
} \ No newline at end of file
diff --git a/src/kernel/lib/stdio/vprintf.c b/src/kernel/lib/stdio/vprintf.c
index 4c0c432..2351d39 100644
--- a/src/kernel/lib/stdio/vprintf.c
+++ b/src/kernel/lib/stdio/vprintf.c
@@ -1,54 +1,13 @@
#include <lib/stdio.h>
#include <lib/stdlib.h>
-#include <lib/string.h>
-#include <memory/alloc.h>
-#include <stdarg.h>
#include <stdint.h>
-void _puts(const char *data)
-{
- for (u32 i = 0; i < strlen(data); i++)
- putch(data[i]);
-}
-
+// TODO: Use global formatting function
void vprintf(const char *fmt, va_list args)
{
- u8 readyToFormat = 0;
-
- char buff = 0;
-
- for (; *fmt; fmt++) {
- if (readyToFormat) {
- if (*fmt == '%') {
- putch('%');
- readyToFormat = 0;
- continue;
- }
+ char buf[1024];
+ vsprintf(buf, fmt, args);
- buff = *fmt;
- if (buff == 's') {
- const char *str = va_arg(args, const char *);
- _puts(str);
- readyToFormat = 0;
- } else if (buff == 'x') {
- char *p = htoa((u32)va_arg(args, int));
- _puts(p);
- free(p);
- readyToFormat = 0;
- } else if (buff == 'd') {
- char *p = itoa(va_arg(args, int));
- _puts(p);
- free(p);
- readyToFormat = 0;
- } else if (buff == 'c') {
- putch((char)va_arg(args, int));
- readyToFormat = 0;
- }
- } else {
- if (*fmt == '%')
- readyToFormat = 1;
- else
- putch(*fmt);
- }
- }
+ for (u32 i = 0; i < strlen(buf); i++)
+ putch(buf[i]);
} \ No newline at end of file
diff --git a/src/kernel/lib/stdio/vsprintf.c b/src/kernel/lib/stdio/vsprintf.c
new file mode 100644
index 0000000..6584ed1
--- /dev/null
+++ b/src/kernel/lib/stdio/vsprintf.c
@@ -0,0 +1,63 @@
+#include <lib/stdlib.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+static void append(char *dest, char *src, int index)
+{
+ for (int i = index; i < strlen(src) + index; i++)
+ dest[i] = src[i - index];
+ dest[index + strlen(src)] = 0;
+}
+
+int vsprintf(char *str, const char *fmt, va_list args)
+{
+ u8 ready_to_format = 0;
+
+ int i = 0;
+ char buf = 0;
+ char format_buffer[20] = "\0";
+
+ for (; *fmt; fmt++) {
+ if (ready_to_format) {
+ ready_to_format = 0;
+
+ if (*fmt == '%') {
+ str[i] = '%';
+ continue;
+ }
+
+ buf = *fmt;
+ if (buf == 's') {
+ char *string = va_arg(args, char *);
+ append(str, string, i);
+ i = strlen(str);
+ } else if (buf == 'x') {
+ itoa_base((u32)va_arg(args, int), format_buffer, 16);
+ append(str, format_buffer, i);
+ i = strlen(str);
+ } else if (buf == 'd') {
+ itoa_base(va_arg(args, int), format_buffer, 10);
+ append(str, format_buffer, i);
+ i = strlen(str);
+ } else if (buf == 'o') {
+ itoa_base(va_arg(args, int), format_buffer, 8);
+ append(str, format_buffer, i);
+ i = strlen(str);
+ } else if (buf == 'c') {
+ str[i] = (char)va_arg(args, int);
+ i++;
+ }
+ } else {
+ if (*fmt == '%')
+ ready_to_format = 1;
+ else {
+ str[i] = *fmt;
+ i++;
+ }
+ }
+
+ format_buffer[0] = '\0';
+ }
+
+ return strlen(str);
+} \ No newline at end of file
diff --git a/src/kernel/lib/stdlib.h b/src/kernel/lib/stdlib.h
index 62bd51c..77254b6 100644
--- a/src/kernel/lib/stdlib.h
+++ b/src/kernel/lib/stdlib.h
@@ -10,6 +10,7 @@
#endif
char *itoa(int n);
+char *itoa_base(int value, char *result, int base);
int atoi(char *str);
diff --git a/src/kernel/lib/stdlib/itoa.c b/src/kernel/lib/stdlib/itoa.c
index 2020ca6..2d2243a 100644
--- a/src/kernel/lib/stdlib/itoa.c
+++ b/src/kernel/lib/stdlib/itoa.c
@@ -41,4 +41,33 @@ char *itoa(int n)
strinv(ret);
return ret;
+}
+
+// TODO: Rename itoa_base
+char *itoa_base(int value, char *result, int base)
+{
+ if (base < 2 || base > 36) {
+ *result = '\0';
+ return result;
+ }
+
+ char *ptr = result, *ptr1 = result, tmp_char;
+ int tmp_value;
+
+ do {
+ tmp_value = value;
+ value /= base;
+ *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"
+ [35 + (tmp_value - value * base)];
+ } while (value);
+
+ if (tmp_value < 0)
+ *ptr++ = '-';
+ *ptr-- = '\0';
+ while (ptr1 < ptr) {
+ tmp_char = *ptr;
+ *ptr-- = *ptr1;
+ *ptr1++ = tmp_char;
+ }
+ return result;
} \ No newline at end of file