summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--kc/core/Makefile6
-rw-r--r--kc/core/cpu/cpu.c2
-rw-r--r--kc/core/kprint.h6
-rw-r--r--kc/core/kstdio.c16
-rw-r--r--kc/core/memory/memory.c2
-rw-r--r--lib/kprintf.c (renamed from kc/core/kprint.c)70
-rw-r--r--lib/kstdio.h18
-rw-r--r--loader/main_efi.c36
8 files changed, 110 insertions, 46 deletions
diff --git a/kc/core/Makefile b/kc/core/Makefile
index dede157..4d373cd 100644
--- a/kc/core/Makefile
+++ b/kc/core/Makefile
@@ -6,15 +6,15 @@ VPATH := ../../lib ../ cpu/ memory/
TARGET := kernel.os
.DEFAULT: $(TARGET)
-CPPFLAGS += -I../../api -I../api -I.
+CPPFLAGS += -I../../api -I../api -I../../lib -I.
CFLAGS += -Wno-unused-const-variable -Wno-unused-function -fvisibility=hidden -g
SOBJS :=
GOBJS := entry_x86_64.o reloc_x86_64.o dynamic_x86_64.o \
- kprint.o serial.o port.o mmu.o kc_main.o memory.o memset.o memcpy.o \
+ kprintf.o serial.o port.o mmu.o kc_main.o memory.o memset.o memcpy.o \
memmove.o memcmp.o cpu.o vm_tree.o exceptions.o panic.o msr.o task.o \
cpu_task.o irq.o kcc_memory.o string.o pic8259.o pic8259_isr.o \
- pit8253.o page_early.o page_stack.o strtoull.o
+ pit8253.o page_early.o page_stack.o strtoull.o kstdio.o
IOBJS :=
# __attribute__((interrupt)) requires -mgeneral-regs-only
diff --git a/kc/core/cpu/cpu.c b/kc/core/cpu/cpu.c
index d33d8a4..f0e7844 100644
--- a/kc/core/cpu/cpu.c
+++ b/kc/core/cpu/cpu.c
@@ -37,7 +37,7 @@ uint64_t *get_tss_rsp0(void)
static void syscall_entry(void)
{
- kputs("system call\n");
+ kprintf("system call\n");
halt();
}
diff --git a/kc/core/kprint.h b/kc/core/kprint.h
index 0c547ea..d023799 100644
--- a/kc/core/kprint.h
+++ b/kc/core/kprint.h
@@ -1,9 +1,5 @@
#pragma once
-#include <stdarg.h>
+#include <kstdio.h>
-int kputchar(int c);
-int kputs(const char *s);
-int kprintf(const char *restrict format, ...);
-int kvprintf(const char *restrict format, va_list arguments);
diff --git a/kc/core/kstdio.c b/kc/core/kstdio.c
new file mode 100644
index 0000000..2971b18
--- /dev/null
+++ b/kc/core/kstdio.c
@@ -0,0 +1,16 @@
+#include "kprint.h"
+#include "serial.h"
+
+#include <lib.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+
+FILE *kstdout;
+
+int kfputc(int c, FILE *f)
+{
+ (void)f;
+ return serial_putchar(c);
+}
+
diff --git a/kc/core/memory/memory.c b/kc/core/memory/memory.c
index 9a1f42f..df48271 100644
--- a/kc/core/memory/memory.c
+++ b/kc/core/memory/memory.c
@@ -630,7 +630,7 @@ void page_fault_handler(struct isr_context *context)
void general_protection_handler(struct isr_context *context)
{
//TODO: implement #gp handler
- kputs("general protection violation\n");
+ kprintf("general protection violation\n");
print_exception_context(context);
PANIC(UNHANDLED_FAULT);
}
diff --git a/kc/core/kprint.c b/lib/kprintf.c
index 4acbddd..075a547 100644
--- a/kc/core/kprint.c
+++ b/lib/kprintf.c
@@ -1,28 +1,8 @@
-#include "kprint.h"
-#include "serial.h"
-
-#include <lib.h>
-
+#include <kstdio.h>
+#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
-int kputchar(int c)
-{
- return serial_putchar(c);
-}
-
-int kputs(const char *s)
-{
- int count = 0;
- while (*s)
- {
- count++;
- kputchar(*s++);
- }
-
- return count;
-}
-
enum specifier_type
{
INVALID_PRINT,
@@ -66,6 +46,7 @@ struct method
{
int (*write_character)(struct method *m, char c);
int (*write_string)(struct method *m, const char *s);
+ void *output;
int count;
};
@@ -573,28 +554,34 @@ static int printf_internal(
return r;
}
-static int kpm_write_character(struct method *m, char c)
+static int kfp_write_character(struct method *m, char c)
{
- kputchar(c);
+ kfputc((int)c, (FILE *)m->output);
m->count++;
return 0;
}
-static int kpm_write_string(struct method *m, const char *c)
+static int kfp_write_string(struct method *m, const char *c)
{
- m->count += kputs(c);
+ while (*c)
+ {
+ m->write_character(m, *c++);
+ }
return 0;
}
-int kvprintf(const char *restrict format, va_list arguments)
+int kvfprintf(FILE *f, const char *restrict format, va_list arguments)
{
struct method m = {
- kpm_write_character,
- kpm_write_string,
+ kfp_write_character,
+ kfp_write_string,
+ (void *)f,
0};
va_list acopy;
va_copy(acopy, arguments);
int r = printf_internal(&m, format, &acopy);
+ va_end(acopy);
+
if (!r)
{
return m.count;
@@ -603,13 +590,36 @@ int kvprintf(const char *restrict format, va_list arguments)
{
return -1;
}
+}
+
+int kfprintf(FILE *f, const char *restrict format, ...)
+{
+ va_list arguments;
+ va_start(arguments, format);
+
+ int count = kvfprintf(f, format, arguments);
+
+ va_end(arguments);
+
+ return count;
+}
+
+int kvprintf(const char *restrict format, va_list arguments)
+{
+ va_list acopy;
+ va_copy(acopy, arguments);
+
+ int count = kvfprintf(kstdout, format, arguments);
+
va_end(acopy);
+
+ return count;
}
int kprintf(const char *restrict format, ...)
{
va_list arguments;
- va_start (arguments, format);
+ va_start(arguments, format);
int count = kvprintf(format, arguments);
diff --git a/lib/kstdio.h b/lib/kstdio.h
new file mode 100644
index 0000000..79ed357
--- /dev/null
+++ b/lib/kstdio.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <lib.h>
+
+#include <stdarg.h>
+
+typedef struct FILE FILE;
+
+extern FILE *kstdout;
+extern FILE *kstderr;
+
+extern int kfputc(int c, FILE *f);
+
+int kvfprintf(FILE *f, const char *restrict format, va_list arguments);
+int kfprintf(FILE *f, const char *restrict format, ...);
+int kvprintf(const char *restrict format, va_list arguments);
+int kprintf(const char *restrict format, ...);
+
diff --git a/loader/main_efi.c b/loader/main_efi.c
index 907cedb..4da259b 100644
--- a/loader/main_efi.c
+++ b/loader/main_efi.c
@@ -100,24 +100,48 @@ EFI_STATUS free_page(EFI_PHYSICAL_ADDRESS base, UINTN size)
EFI_STATUS open_image(struct efi_loader_image *image)
{
EFI_FILE_PROTOCOL *root;
+ EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *root_fs;
+ EFI_GUID root_fs_guid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
EFI_LOADED_IMAGE_PROTOCOL *loader_image;
+ EFI_GUID loader_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
EFI_STATUS status;
- status = gBS->OpenProtocol(loader_interface.image_handle,
- &LoadedImageProtocol,
+ status = gBS->OpenProtocol(
+ loader_interface.image_handle,
+ &loader_image_guid,
(void *)&loader_image,
loader_interface.image_handle,
NULL,
EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+ if (EFI_ERROR(status))
+ {
+ Print(L"failed locating image protocol: %r\r\n", status);
+ return status;
+ }
+
+ status = gBS->OpenProtocol(
+ loader_image->DeviceHandle,
+ &root_fs_guid,
+ (void *)&root_fs,
+ loader_interface.image_handle,
+ NULL,
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL);
+
+ if (EFI_ERROR(status))
+ {
+ Print(L"failed locating ESP file system: %r\r\n", status);
+ return status;
+ }
+
+ status = root_fs->OpenVolume(root_fs, &root);
- if (EFI_ERROR(status) ||
- !(root = LibOpenRoot(loader_image->DeviceHandle)))
+ if (EFI_ERROR(status))
{
- Print(L"failed opening root device\r\n");
- return EFI_NOT_FOUND;
+ Print(L"failed opening root device: %r\r\n", status);
+ return status;
}
Print(L"opening image %s\r\n", image->path);