feat: added printf
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <libk.h>
|
||||
#include <drivers/pci.h>
|
||||
#include <drivers/ide.h>
|
||||
#include <libc/kprintf.h>
|
||||
|
||||
#define BOOTLOADER_NAME_MAX_LENGTH 64
|
||||
#define CMDLINE_MAX_LENGTH 256
|
||||
@@ -44,12 +45,10 @@ cmd_handler cmd_handlers[] = {
|
||||
};
|
||||
|
||||
void print_bootinfo() {
|
||||
kprint("Bootloader name: ");
|
||||
kprint(bootloader_name[0] == 0 ? "NULL" : bootloader_name);
|
||||
kprint(newline);
|
||||
kprint("cmdline: ");
|
||||
kprint(cmdline[0] == 0 ? "NULL" : cmdline);
|
||||
kprint(newline);
|
||||
printf("Bootloader name: %s\n"
|
||||
"cmdline: %s\n",
|
||||
(bootloader_name[0] == 0 ? "NULL" : bootloader_name),
|
||||
(cmdline[0] == 0 ? "NULL" : cmdline));
|
||||
}
|
||||
|
||||
void help(const char* arg) {
|
||||
@@ -59,8 +58,7 @@ void help(const char* arg) {
|
||||
if (cmd_handlers[index].cmd == NULL) {
|
||||
break;
|
||||
}
|
||||
kprint(cmd_handlers[index].cmd);
|
||||
kprint(newline);
|
||||
printf("%s\n", cmd_handlers[index].cmd);
|
||||
index += 1;
|
||||
}
|
||||
|
||||
@@ -96,16 +94,14 @@ void store_bootloader_info(multiboot_info_t *multiboot_info) {
|
||||
if (multiboot_info->flags & MULTIBOOT_INFO_CMDLINE) {
|
||||
int cmdline_length = strlen((const char *) multiboot_info->cmdline);
|
||||
if (cmdline_length > CMDLINE_MAX_LENGTH) {
|
||||
kprint("cmdline to long!");
|
||||
k_panic();
|
||||
k_panics("cmdline to long!\n");
|
||||
}
|
||||
memcpy(cmdline, (char *) multiboot_info->cmdline, cmdline_length);
|
||||
}
|
||||
if (multiboot_info->flags & MULTIBOOT_INFO_BOOT_LOADER_NAME) {
|
||||
int bootloader_length = strlen((const char *) multiboot_info->boot_loader_name);
|
||||
if (bootloader_length > BOOTLOADER_NAME_MAX_LENGTH) {
|
||||
kprint("bootloader name to long!");
|
||||
k_panic();
|
||||
k_panics("bootloader name to long!\n");
|
||||
}
|
||||
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));
|
||||
// todo fallback on other mechanisms?
|
||||
} else {
|
||||
kprint("mmap invalid!\n");
|
||||
k_panic();
|
||||
k_panics("mmap invalid!\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,8 +151,10 @@ void kmain(multiboot_info_t *multiboot_info) {
|
||||
// setup PS/2 keyboard
|
||||
init_keyboard();
|
||||
|
||||
u8* tmp = malloc(4096);
|
||||
ide_read_access(0, 0, 0, 16, tmp);
|
||||
// print_gdt();
|
||||
|
||||
// u8* tmp = malloc(4096);
|
||||
// ide_read_access(0, 0, 0, 16, tmp);
|
||||
|
||||
// enter main loop
|
||||
while (true) {
|
||||
|
||||
84
kernel/libc/kprintf.c
Normal file
84
kernel/libc/kprintf.c
Normal 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
10
kernel/libc/kprintf.h
Normal 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
14
kernel/libc/va_list.h
Normal 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
|
||||
@@ -3,9 +3,8 @@
|
||||
//
|
||||
|
||||
#include <types.h>
|
||||
#include <kprint.h>
|
||||
#include <mem/mem.h>
|
||||
#include <libc/libc.h>
|
||||
#include <libc/kprintf.h>
|
||||
|
||||
#define MEMMAP_ENTRIES 16
|
||||
|
||||
@@ -28,22 +27,12 @@
|
||||
extern void *kernel_start;
|
||||
//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 {
|
||||
void *address;
|
||||
u32 length;
|
||||
u32 type;
|
||||
} __attribute((packed)) mmap_entry;
|
||||
|
||||
char *msg_lu = "0123456789ABCDEF";
|
||||
|
||||
int malloc_entries = 0;
|
||||
int malloc_used = 0;
|
||||
|
||||
@@ -149,7 +138,7 @@ void *malloc(unsigned int size) {
|
||||
}
|
||||
malloc_used++;
|
||||
current_map->type = MALLOC_TYPE_USED;
|
||||
return ((void*)current_map) + sizeof(malloc_map);
|
||||
return ((void *) current_map) + sizeof(malloc_map);
|
||||
|
||||
malloc_find_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() {
|
||||
kprint(msg_malloc_num_entries_avail);
|
||||
print_int(malloc_entries, 10);
|
||||
kprint(msg_nl);
|
||||
|
||||
kprint(msg_malloc_num_entries_used);
|
||||
print_int(malloc_used, 10);
|
||||
kprint(msg_nl);
|
||||
printf("Malloc avail entries: %d\n"
|
||||
"Malloc used entries: %d\n", malloc_entries, malloc_used);
|
||||
}
|
||||
|
||||
|
||||
@@ -191,18 +168,9 @@ void print_mmap_info() {
|
||||
if (memmap[i].type == 0) {
|
||||
break;
|
||||
}
|
||||
kprint(msg_index);
|
||||
print_int(i, 10);
|
||||
kprint(msg_nl);
|
||||
kprint(msg_addr);
|
||||
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);
|
||||
printf("Idx: %d\n"
|
||||
"Address: %8x\n"
|
||||
"Length: %8x\n"
|
||||
"Type: %d\n\n", i, memmap[i].address, memmap[i].length, memmap[i].type);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user