aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2019-11-30 16:21:57 +0100
committerMarvin Borner2019-11-30 16:21:57 +0100
commitac947d45c288f62e927895afe7cd6a722ffdd8f8 (patch)
treecb9ed1bc1713c484b6846f068dca98e0dfc0cbc0
parent0e94325936030990ac95b9ca90382aa265c95818 (diff)
Replaced vesa print functions with printf
-rw-r--r--src/kernel/cmos/rtc.c15
-rw-r--r--src/kernel/commands/command.c18
-rw-r--r--src/kernel/graphics/vesa.c29
-rw-r--r--src/kernel/kernel.c6
-rw-r--r--src/kernel/paging/paging.c2
-rw-r--r--src/kernel/syscall/syscall.c10
-rw-r--r--src/kernel/syscall/syscall.h4
-rw-r--r--src/kernel/system.c48
-rw-r--r--src/kernel/system.h6
-rw-r--r--src/mlibc/stdio.h4
-rw-r--r--src/mlibc/stdio/printf.c49
-rw-r--r--src/mlibc/stdio/vprintf.c52
12 files changed, 115 insertions, 128 deletions
diff --git a/src/kernel/cmos/rtc.c b/src/kernel/cmos/rtc.c
index 044c58b..0a67e64 100644
--- a/src/kernel/cmos/rtc.c
+++ b/src/kernel/cmos/rtc.c
@@ -1,6 +1,7 @@
#include <kernel/io/io.h>
#include <kernel/graphics/vesa.h>
#include <kernel/acpi/acpi.h>
+#include <mlibc/stdio.h>
unsigned char second;
unsigned char minute;
@@ -87,17 +88,5 @@ void read_rtc()
void write_time()
{
read_rtc();
- vesa_draw_string("Current time: ");
- vesa_draw_number(hour);
- vesa_draw_string(":");
- vesa_draw_number(minute);
- vesa_draw_string(":");
- vesa_draw_number(second);
- vesa_draw_string(" ");
- vesa_draw_number(month);
- vesa_draw_string("/");
- vesa_draw_number(day);
- vesa_draw_string("/");
- vesa_draw_number(year);
- vesa_draw_string("\n");
+ printf("Current time: %d:%d:%d %d/%d/%d\n", hour, minute, second, month, day, year);
} \ No newline at end of file
diff --git a/src/kernel/commands/command.c b/src/kernel/commands/command.c
index 9b6060e..c85356b 100644
--- a/src/kernel/commands/command.c
+++ b/src/kernel/commands/command.c
@@ -5,6 +5,7 @@
#include <kernel/cmos/rtc.h>
#include <kernel/timer/timer.h>
#include <mlibc/string.h>
+#include <mlibc/stdio.h>
int32_t starts_with(const char *a, const char *b)
{
@@ -16,24 +17,23 @@ int32_t starts_with(const char *a, const char *b)
void exec_command(char *command)
{
if (starts_with(command, "ls"))
- vesa_draw_string("Listing files\n");
+ printf("Listing files\n");
else if (starts_with(command, "help"))
- vesa_draw_string("I can't help you write now\n");
+ printf("I can't help you write now\n");
else if (starts_with(command, "ping"))
- vesa_draw_string("pong!\n");
+ printf("pong!\n");
else if (starts_with(command, "clear"))
vesa_clear();
else if (starts_with(command, "shutdown") || starts_with(command, "exit"))
acpi_poweroff();
else if (starts_with(command, "zzz"))
- vesa_draw_string("Not implemented\n");
- else if (starts_with(command, "time")) {
- vesa_draw_number((int) get_time());
- vesa_draw_string("\n");
- } else if (starts_with(command, "date"))
+ printf("Not implemented\n");
+ else if (starts_with(command, "time"))
+ printf("%d\n", (int) get_time());
+ else if (starts_with(command, "date"))
write_time();
else if (starts_with(command, "reboot"))
reboot();
else if (command[0] != 0)
- vesa_draw_string("Command not found!\n");
+ warn("Command not found!");
}
diff --git a/src/kernel/graphics/vesa.c b/src/kernel/graphics/vesa.c
index 8dc5929..6bc6e35 100644
--- a/src/kernel/graphics/vesa.c
+++ b/src/kernel/graphics/vesa.c
@@ -6,6 +6,7 @@
#include <kernel/system.h>
#include <mlibc/stdlib.h>
#include <kernel/commands/command.h>
+#include <mlibc/stdio.h>
void switch_to_vga()
{
@@ -67,7 +68,6 @@ uint16_t *vbe_get_modes()
uint16_t *mode_ptr = (uint16_t *) info->video_modes;
size_t number_modes = 1;
for (uint16_t *p = mode_ptr; *p != 0xFFFF; p++) number_modes++;
- serial_write_dec(number_modes);
uint16_t *ret = kmalloc(sizeof(uint16_t) * number_modes);
for (size_t i = 0; i < number_modes; i++)
@@ -199,19 +199,14 @@ void set_optimal_resolution()
vesa_clear();
vesa_set_color(vesa_blue);
- vesa_draw_string(vga_buffer);
+ printf(vga_buffer);
vesa_set_color(default_text_color);
info("Successfully switched to video mode!");
serial_write("Using mode: ");
serial_write_hex(highest);
- log("Using mode: ");
- vesa_draw_number(vbe_width);
- vesa_draw_string("x");
- vesa_draw_number(vbe_height);
- vesa_draw_string("x");
- vesa_draw_number(vbe_bpl << 3);
+ debug("Using mode: %dx%dx%d", vbe_width, vbe_height, vbe_bpl << 3);
}
const uint32_t default_text_color = vesa_white;
@@ -336,12 +331,12 @@ void vesa_keyboard_char(char ch)
} else if (ch == '\r') {
terminal_x = 0;
} else if (ch == '\n') {
- vesa_draw_char(ch);
+ writec(ch);
exec_command(text);
memset(text, 0, sizeof(text));
// terminal_scroll();
} else if (ch >= ' ') {
- vesa_draw_char(ch);
+ writec(ch);
char tmp[2] = {ch};
strcat(text, tmp);
}
@@ -351,20 +346,6 @@ void vesa_keyboard_char(char ch)
terminal_color);
}
-void vesa_draw_string(const char *data)
-{
- int i = 0;
- while (data[i] != '\0') {
- vesa_draw_char(data[i]);
- i++;
- }
-}
-
-void vesa_draw_number(int n)
-{
- vesa_draw_string(itoa(n));
-}
-
char *prev = 0;
int prev_coords[2] = {};
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index f63bd24..ef58ade 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -10,12 +10,14 @@
#include <kernel/smbios/smbios.h>
#include <kernel/fs/install.h>
#include <kernel/lib/lib.h>
+#include <mlibc/stdio.h>
extern void switch_to_user();
void kernel_main()
{
vga_log("Installing basic features of Melvix...", 0);
+
// Install features
memory_init();
gdt_install();
@@ -38,8 +40,8 @@ void kernel_main()
// Get hardware information
get_smbios();
- // Booting process complete - emulate newline key
- vesa_keyboard_char('\n');
+ // Print total memory
+ info("Total memory found: %dMiB", memory_get_all() >> 10);
uint8_t boot_drive_id = (uint8_t) (*((uint8_t *) 0x9000));
if (boot_drive_id == 0xE0) {
diff --git a/src/kernel/paging/paging.c b/src/kernel/paging/paging.c
index 68784ee..8540bc6 100644
--- a/src/kernel/paging/paging.c
+++ b/src/kernel/paging/paging.c
@@ -24,8 +24,6 @@ void paging_install()
paging_set_used(0, ((uint32_t) ASM_KERNEL_END >> 12) + 1); // /4096
paging_enable();
- serial_write_dec(memory_get_all() >> 10);
-
vga_log("Installed paging", 4);
}
diff --git a/src/kernel/syscall/syscall.c b/src/kernel/syscall/syscall.c
index 1a8de52..5acd525 100644
--- a/src/kernel/syscall/syscall.c
+++ b/src/kernel/syscall/syscall.c
@@ -4,16 +4,10 @@
#include <kernel/graphics/vesa.h>
#include <kernel/io/io.h>
-DEFN_SYSCALL1(vesa_draw_string, 0, const char *);
-
-DEFN_SYSCALL1(vesa_draw_number, 1, int);
-
-DEFN_SYSCALL1(serial_write, 2, const char *);
+DEFN_SYSCALL1(serial_write, 0, const char *);
static void *syscalls[3] = {
- &vesa_draw_string,
- &vesa_draw_number,
- &serial_write,
+ &serial_write
};
uint32_t num_syscalls = 3;
diff --git a/src/kernel/syscall/syscall.h b/src/kernel/syscall/syscall.h
index d35c4e6..78a1ab5 100644
--- a/src/kernel/syscall/syscall.h
+++ b/src/kernel/syscall/syscall.h
@@ -35,10 +35,6 @@ int syscall_##fn(P1 p1, P2 p2) { \
return a; \
}
-DECL_SYSCALL1(vesa_draw_string, const char *)
-
-DECL_SYSCALL1(vesa_draw_number, int)
-
DECL_SYSCALL1(serial_write, const char *)
#endif
diff --git a/src/kernel/system.c b/src/kernel/system.c
index 2c9a6d2..c5cf4ce 100644
--- a/src/kernel/system.c
+++ b/src/kernel/system.c
@@ -5,6 +5,8 @@
#include <mlibc/stdlib.h>
#include <kernel/paging/paging.h>
#include <kernel/interrupts/interrupts.h>
+#include <mlibc/stdio.h>
+#include <stdarg.h>
char *vga_buffer = (char *) 0x500;
@@ -26,7 +28,7 @@ void vga_log(char *msg, int line)
strcpy(string, "[");
strcat(string, itoa((int) get_time()));
strcat(string, "] ");
- strcat(string, "INFORMATION: ");
+ strcat(string, "INFO: ");
strcat(string, msg);
strcat(string, "\n");
strcat(vga_buffer, string);
@@ -34,36 +36,52 @@ void vga_log(char *msg, int line)
void kernel_time()
{
- vesa_draw_string("\n");
- vesa_draw_string("[");
- vesa_draw_number((int) get_time());
- vesa_draw_string("] ");
+ printf("[%d] ", (int) get_time());
}
-void log(char *msg)
+void debug(const char *fmt, ...)
{
vesa_set_color(vesa_dark_white);
kernel_time();
- vesa_draw_string(msg);
+ printf("DEBG: ");
+
+ va_list args;
+ va_start(args, fmt);
+ vprintf(fmt, args);
+ va_end(args);
+
vesa_set_color(default_text_color);
+ writec('\n');
}
-void info(char *msg)
+void info(const char *fmt, ...)
{
vesa_set_color(vesa_blue);
kernel_time();
- vesa_draw_string("INFORMATION: ");
- vesa_draw_string(msg);
+ printf("INFO: ");
+
+ va_list args;
+ va_start(args, fmt);
+ vprintf(fmt, args);
+ va_end(args);
+
vesa_set_color(default_text_color);
+ writec('\n');
}
-void warn(char *msg)
+void warn(const char *fmt, ...)
{
vesa_set_color(vesa_dark_yellow);
kernel_time();
- vesa_draw_string("WARNING: ");
- vesa_draw_string(msg);
+ printf("WARN: ");
+
+ va_list args;
+ va_start(args, fmt);
+ vprintf(fmt, args);
+ va_end(args);
+
vesa_set_color(default_text_color);
+ writec('\n');
}
void panic(char *msg)
@@ -74,9 +92,7 @@ void panic(char *msg)
serial_write("\nPANIC: ");
serial_write(msg);
serial_write(" - System halted!\n");
- vesa_draw_string("PANIC: ");
- vesa_draw_string(msg);
- vesa_draw_string(" - System halted!\n");
+ printf("PANIC: %s - System halted!\n", msg);
halt_loop();
}
diff --git a/src/kernel/system.h b/src/kernel/system.h
index 1fe2026..26530f9 100644
--- a/src/kernel/system.h
+++ b/src/kernel/system.h
@@ -47,20 +47,20 @@ void kernel_time();
* Display a general log message
* @param msg The message
*/
-void log(char *msg);
+void debug(const char *fmt, ...);
/**
* Display an information message
* @param msg The information
*/
-void info(char *msg);
+void info(const char *fmt, ...);
/**
* Display a warning message
* TODO: Add line number and file name
* @param msg The warning cause/reason
*/
-void warn(char *msg);
+void warn(const char *fmt, ...);
/**
* Halt the entire system and display a message
diff --git a/src/mlibc/stdio.h b/src/mlibc/stdio.h
index ae377a2..82f1cfb 100644
--- a/src/mlibc/stdio.h
+++ b/src/mlibc/stdio.h
@@ -3,8 +3,12 @@
// TODO: Input methods
+#include <stdarg.h>
+
void writec(char c);
+void vprintf(const char *format, va_list args);
+
void printf(const char *format, ...);
#endif
diff --git a/src/mlibc/stdio/printf.c b/src/mlibc/stdio/printf.c
index 23d8e6e..5617d03 100644
--- a/src/mlibc/stdio/printf.c
+++ b/src/mlibc/stdio/printf.c
@@ -1,55 +1,10 @@
#include <stdarg.h>
-#include <stdint.h>
#include <mlibc/stdio.h>
-#include <mlibc/string.h>
-#include <mlibc/stdlib.h>
-
-void __writes(const char *data)
-{
- for (size_t i = 0; i < strlen(data); i++) writec(data[i]);
-}
void printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
-
- uint8_t readyToFormat = 0;
-
- char buff = 0;
-
- for (; *fmt; fmt++) {
- if (readyToFormat) {
- if (*fmt == '%') {
- writec('%');
- readyToFormat = 0;
- continue;
- }
-
- buff = *fmt;
- if (buff == 's') {
- const char *str = va_arg(args, const char*);
- __writes(str);
- readyToFormat = 0;
- } else if (buff == 'x') {
- char *p = htoa((uint32_t) va_arg(args, int));
- __writes(p);
- kfree(p);
- readyToFormat = 0;
- } else if (buff == 'd') {
- char *p = itoa(va_arg(args, int));
- __writes(p);
- kfree(p);
- readyToFormat = 0;
- } else if (buff == 'c') {
- writec((char) va_arg(args, int));
- readyToFormat = 0;
- }
- } else {
- if (*fmt == '%')
- readyToFormat = 1;
- else
- writec(*fmt);
- }
- }
+ vprintf(fmt, args);
+ va_end(args);
} \ No newline at end of file
diff --git a/src/mlibc/stdio/vprintf.c b/src/mlibc/stdio/vprintf.c
new file mode 100644
index 0000000..81efce2
--- /dev/null
+++ b/src/mlibc/stdio/vprintf.c
@@ -0,0 +1,52 @@
+#include <stdarg.h>
+#include <stdint.h>
+#include <mlibc/stdio.h>
+#include <mlibc/string.h>
+#include <mlibc/stdlib.h>
+
+void __writes(const char *data)
+{
+ for (size_t i = 0; i < strlen(data); i++) writec(data[i]);
+}
+
+void vprintf(const char *fmt, va_list args)
+{
+ uint8_t readyToFormat = 0;
+
+ char buff = 0;
+
+ for (; *fmt; fmt++) {
+ if (readyToFormat) {
+ if (*fmt == '%') {
+ writec('%');
+ readyToFormat = 0;
+ continue;
+ }
+
+ buff = *fmt;
+ if (buff == 's') {
+ const char *str = va_arg(args, const char*);
+ __writes(str);
+ readyToFormat = 0;
+ } else if (buff == 'x') {
+ char *p = htoa((uint32_t) va_arg(args, int));
+ __writes(p);
+ kfree(p);
+ readyToFormat = 0;
+ } else if (buff == 'd') {
+ char *p = itoa(va_arg(args, int));
+ __writes(p);
+ kfree(p);
+ readyToFormat = 0;
+ } else if (buff == 'c') {
+ writec((char) va_arg(args, int));
+ readyToFormat = 0;
+ }
+ } else {
+ if (*fmt == '%')
+ readyToFormat = 1;
+ else
+ writec(*fmt);
+ }
+ }
+} \ No newline at end of file