feat: added printf

This commit is contained in:
2021-02-06 16:44:35 +01:00
parent a4651ca9d9
commit 7ff33d611c
5 changed files with 129 additions and 56 deletions

View File

@@ -14,6 +14,7 @@
#include <libk.h> #include <libk.h>
#include <drivers/pci.h> #include <drivers/pci.h>
#include <drivers/ide.h> #include <drivers/ide.h>
#include <libc/kprintf.h>
#define BOOTLOADER_NAME_MAX_LENGTH 64 #define BOOTLOADER_NAME_MAX_LENGTH 64
#define CMDLINE_MAX_LENGTH 256 #define CMDLINE_MAX_LENGTH 256
@@ -44,12 +45,10 @@ cmd_handler cmd_handlers[] = {
}; };
void print_bootinfo() { void print_bootinfo() {
kprint("Bootloader name: "); printf("Bootloader name: %s\n"
kprint(bootloader_name[0] == 0 ? "NULL" : bootloader_name); "cmdline: %s\n",
kprint(newline); (bootloader_name[0] == 0 ? "NULL" : bootloader_name),
kprint("cmdline: "); (cmdline[0] == 0 ? "NULL" : cmdline));
kprint(cmdline[0] == 0 ? "NULL" : cmdline);
kprint(newline);
} }
void help(const char* arg) { void help(const char* arg) {
@@ -59,8 +58,7 @@ void help(const char* arg) {
if (cmd_handlers[index].cmd == NULL) { if (cmd_handlers[index].cmd == NULL) {
break; break;
} }
kprint(cmd_handlers[index].cmd); printf("%s\n", cmd_handlers[index].cmd);
kprint(newline);
index += 1; index += 1;
} }
@@ -96,16 +94,14 @@ void store_bootloader_info(multiboot_info_t *multiboot_info) {
if (multiboot_info->flags & MULTIBOOT_INFO_CMDLINE) { if (multiboot_info->flags & MULTIBOOT_INFO_CMDLINE) {
int cmdline_length = strlen((const char *) multiboot_info->cmdline); int cmdline_length = strlen((const char *) multiboot_info->cmdline);
if (cmdline_length > CMDLINE_MAX_LENGTH) { if (cmdline_length > CMDLINE_MAX_LENGTH) {
kprint("cmdline to long!"); k_panics("cmdline to long!\n");
k_panic();
} }
memcpy(cmdline, (char *) multiboot_info->cmdline, cmdline_length); memcpy(cmdline, (char *) multiboot_info->cmdline, cmdline_length);
} }
if (multiboot_info->flags & MULTIBOOT_INFO_BOOT_LOADER_NAME) { if (multiboot_info->flags & MULTIBOOT_INFO_BOOT_LOADER_NAME) {
int bootloader_length = strlen((const char *) multiboot_info->boot_loader_name); int bootloader_length = strlen((const char *) multiboot_info->boot_loader_name);
if (bootloader_length > BOOTLOADER_NAME_MAX_LENGTH) { if (bootloader_length > BOOTLOADER_NAME_MAX_LENGTH) {
kprint("bootloader name to long!"); k_panics("bootloader name to long!\n");
k_panic();
} }
memcpy(bootloader_name, (char *) multiboot_info->boot_loader_name, bootloader_length); memcpy(bootloader_name, (char *) multiboot_info->boot_loader_name, bootloader_length);
} }
@@ -117,8 +113,7 @@ void init_mmap(multiboot_info_t *multiboot_info) {
multiboot_info->mmap_length / sizeof(struct multiboot_mmap_entry)); multiboot_info->mmap_length / sizeof(struct multiboot_mmap_entry));
// todo fallback on other mechanisms? // todo fallback on other mechanisms?
} else { } else {
kprint("mmap invalid!\n"); k_panics("mmap invalid!\n");
k_panic();
} }
} }
@@ -156,8 +151,10 @@ void kmain(multiboot_info_t *multiboot_info) {
// setup PS/2 keyboard // setup PS/2 keyboard
init_keyboard(); init_keyboard();
u8* tmp = malloc(4096); // print_gdt();
ide_read_access(0, 0, 0, 16, tmp);
// u8* tmp = malloc(4096);
// ide_read_access(0, 0, 0, 16, tmp);
// enter main loop // enter main loop
while (true) { while (true) {

84
kernel/libc/kprintf.c Normal file
View File

@@ -0,0 +1,84 @@
//
// Created by rick on 06-02-21.
//
#include "kprintf.h"
#include "libc.h"
#include <libc/string.h>
#include <libc/va_list.h>
#include <kprint.h>
#include <libk.h>
#include <types.h>
/*
* Integer to string
*/
void print_int(u32 value, u32 width, char* buf, u32 *ptr, u8 base) {
// u32 in binary is 32 digits, so never longer than 33 digits
char msg[33];
itoa(value, msg, base);
u32 digits = strlen(msg);
if (digits < width) {
for (u32 i = 0; i < (width - digits); ++i) {
buf[*ptr] = '0';
*ptr += 1;
}
}
for (u32 i = 0; i < digits; ++i) {
buf[*ptr] = msg[i];
*ptr += 1;
}
}
u32 vasprintf(char *buf, const char *fmt, va_list args) {
u32 ptr = 0;
for (int i = 0; i < strlen(fmt); ++i) {
if (fmt[i] != '%') {
buf[ptr++] = fmt[i];
continue;
}
++i;
u32 arg_width = 0;
while (fmt[i] >= '0' && fmt[i] <= '9') {
arg_width *= 10;
arg_width += fmt[i] - '0';
++i;
}
switch (fmt[i]) {
case 's': { // string
char *s_fmt = (char *) va_arg(args, char*);
while (*s_fmt) {
buf[ptr++] = *s_fmt++;
}
break;
}
case 'c':
buf[ptr++] = (char)va_arg(args, int);
break;
case 'x':
print_int((u32)va_arg(args, u32), arg_width, buf, &ptr, 16);
break;
case 'd':
print_int((u32)va_arg(args, u32), arg_width, buf, &ptr, 10);
break;
case '%':
buf[ptr++] = '%';
break;
default:
printf("FMT '%c' is not supported\n", fmt[i]);
k_panic();
break;
}
}
buf[ptr] = 0;
return ptr;
}
void printf(const char *fmt, ...) {
char buf[1024] = {-1};
va_list args;
va_start(args, fmt);
vasprintf(buf, fmt, args);
va_end(args);
kprint(buf);
}

10
kernel/libc/kprintf.h Normal file
View File

@@ -0,0 +1,10 @@
//
// Created by rick on 06-02-21.
//
#ifndef NEW_KERNEL_KPRINTF_H
#define NEW_KERNEL_KPRINTF_H
void printf(const char *fmt, ...);
#endif //NEW_KERNEL_KPRINTF_H

14
kernel/libc/va_list.h Normal file
View File

@@ -0,0 +1,14 @@
//
// Created by rick on 06-02-21.
//
#ifndef NEW_KERNEL_VA_LIST_H
#define NEW_KERNEL_VA_LIST_H
typedef __builtin_va_list va_list;
#define va_start(ap,last) __builtin_va_start(ap, last)
#define va_end(ap) __builtin_va_end(ap)
#define va_arg(ap,type) __builtin_va_arg(ap,type)
#define va_copy(dest, src) __builtin_va_copy(dest,src)
#endif //NEW_KERNEL_VA_LIST_H

View File

@@ -3,9 +3,8 @@
// //
#include <types.h> #include <types.h>
#include <kprint.h>
#include <mem/mem.h> #include <mem/mem.h>
#include <libc/libc.h> #include <libc/kprintf.h>
#define MEMMAP_ENTRIES 16 #define MEMMAP_ENTRIES 16
@@ -28,22 +27,12 @@
extern void *kernel_start; extern void *kernel_start;
//extern void *kernel_end; //extern void *kernel_end;
char *msg_index = "Idx: ";
char *msg_addr = "Address: ";
char *msg_len = "Length: ";
char *msg_type = "Type: ";
char *msg_nl = "\n";
char *msg_malloc_num_entries_avail = "Malloc avail entries: ";
char *msg_malloc_num_entries_used = "Malloc used entries: ";
typedef struct { typedef struct {
void *address; void *address;
u32 length; u32 length;
u32 type; u32 type;
} __attribute((packed)) mmap_entry; } __attribute((packed)) mmap_entry;
char *msg_lu = "0123456789ABCDEF";
int malloc_entries = 0; int malloc_entries = 0;
int malloc_used = 0; int malloc_used = 0;
@@ -149,7 +138,7 @@ void *malloc(unsigned int size) {
} }
malloc_used++; malloc_used++;
current_map->type = MALLOC_TYPE_USED; current_map->type = MALLOC_TYPE_USED;
return ((void*)current_map) + sizeof(malloc_map); return ((void *) current_map) + sizeof(malloc_map);
malloc_find_next: malloc_find_next:
current_map = current_map->next; current_map = current_map->next;
@@ -168,21 +157,9 @@ void free(void *mem) {
}; };
void print_int(int val, int base) {
char tmp_str[32];
memset(tmp_str, 0, 32);
itoa(val, tmp_str, base);
kprint(tmp_str);
}
void print_malloc_info() { void print_malloc_info() {
kprint(msg_malloc_num_entries_avail); printf("Malloc avail entries: %d\n"
print_int(malloc_entries, 10); "Malloc used entries: %d\n", malloc_entries, malloc_used);
kprint(msg_nl);
kprint(msg_malloc_num_entries_used);
print_int(malloc_used, 10);
kprint(msg_nl);
} }
@@ -191,18 +168,9 @@ void print_mmap_info() {
if (memmap[i].type == 0) { if (memmap[i].type == 0) {
break; break;
} }
kprint(msg_index); printf("Idx: %d\n"
print_int(i, 10); "Address: %8x\n"
kprint(msg_nl); "Length: %8x\n"
kprint(msg_addr); "Type: %d\n\n", i, memmap[i].address, memmap[i].length, memmap[i].type);
print_int((int) memmap[i].address, 16);
kprint(msg_nl);
kprint(msg_len);
print_int(memmap[i].length, 16);
kprint(msg_nl);
kprint(msg_type);
print_int(memmap[i].type, 10);
kprint(msg_nl);
kprint(msg_nl);
} }
} }