implement printf like function

This commit is contained in:
Mathieu Maret 2018-08-06 22:28:45 +02:00
parent 29f085b7aa
commit 9f1afe5f6b
5 changed files with 60 additions and 23 deletions

View File

@ -22,7 +22,7 @@ void kmain(unsigned long magic, unsigned long addr)
{ {
unsigned long upper_mem = 0; unsigned long upper_mem = 0;
initVGA(BLACK, GREEN); initVGA(BLACK, GREEN);
printString("Setting up IDT\n"); printf("Setting up IDT\n");
gdtSetup(); gdtSetup();
idtSetup(); idtSetup();
irqSetup(); irqSetup();
@ -32,39 +32,31 @@ void kmain(unsigned long magic, unsigned long addr)
multiboot_info_t *mbi = (multiboot_info_t *)addr; multiboot_info_t *mbi = (multiboot_info_t *)addr;
/* Are mem_* valid? */ /* Are mem_* valid? */
if (CHECK_FLAG(mbi->flags, 0)){ if (CHECK_FLAG(mbi->flags, 0)){
printString("mem_lower = "); printf("mem_lower = %dKB mem_upper %dKB\n", mbi->mem_lower, mbi->mem_upper);
printInt((unsigned)mbi->mem_lower);
printString("KB mem_upper = ");
printInt((unsigned)mbi->mem_upper);
printString("KB\n");
upper_mem = mbi->mem_upper; upper_mem = mbi->mem_upper;
} }
/* Is boot_device valid? */ /* Is boot_device valid? */
if (CHECK_FLAG(mbi->flags, 1)){ if (CHECK_FLAG(mbi->flags, 1)){
printString("boot_device = "); printf("boot_device = %d\n", mbi->boot_device);
printInt((unsigned)mbi->boot_device);
printString("\n");
} }
/* Is the command line passed? */ /* Is the command line passed? */
if (CHECK_FLAG(mbi->flags, 2)){ if (CHECK_FLAG(mbi->flags, 2)){
printString("cmdline = "); printf("cmdline = %s\n", (char *)mbi->cmdline);
printString((char *)mbi->cmdline);
printString("\n");
} }
} }
if(upper_mem == 0){ if(upper_mem == 0){
printString("Cannot get upper phy mem bound. Using default value 32MB\n"); printf("Cannot get upper phy mem bound. Using default value 32MB\n");
upper_mem = 32 * 1024; upper_mem = 32 * 1024;
} }
memInit(upper_mem); memInit(upper_mem);
printString("Setting up IRQ handlers\n"); printf("Setting up IRQ handlers\n");
irqSetRoutine(IRQ_KEYBOARD, keyboard_handler); irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);
irqSetRoutine(IRQ_TIMER, timer_handler); irqSetRoutine(IRQ_TIMER, timer_handler);
printString("Enabling HW interrupts\n"); printf("Enabling HW interrupts\n");
exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, print_handler); exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, print_handler);
// Enabling the HW interrupts // Enabling the HW interrupts
asm volatile("sti\n"); asm volatile("sti\n");
@ -72,5 +64,5 @@ void kmain(unsigned long magic, unsigned long addr)
while (1) { while (1) {
printIntDetails(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1); printIntDetails(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1);
} }
printString("exiting\n"); printf("exiting\n");
} }

View File

@ -5,11 +5,7 @@ struct mem_desc *page_desc = (struct mem_desc *)&__ld_kernel_end;
int memInit(unsigned long upper_mem) int memInit(unsigned long upper_mem)
{ {
printString("Free Mem going from "); printf("Free Mem going from %d to %d\n", &__ld_kernel_end, upper_mem *1024);
printInt((unsigned long)&__ld_kernel_end);
printString(" to ");
printInt(upper_mem * 1024);
printString("\n");
unsigned long memdesc_end = unsigned long memdesc_end =
(unsigned long)page_desc + (unsigned long)page_desc +
((upper_mem) / (PAGE_SIZE / 1024)) * sizeof(struct mem_desc); ((upper_mem) / (PAGE_SIZE / 1024)) * sizeof(struct mem_desc);

View File

@ -280,9 +280,9 @@ void keyboard_do_irq()
break; break;
default: default:
if (lshift || rshift) if (lshift || rshift)
printString(scancode_shift[(int)c]); printf(scancode_shift[(int)c]);
else else
printString(scancode[(int)c]); printf(scancode[(int)c]);
} }
}else { }else {
c = c - BREAK_CODE; c = c - BREAK_CODE;

View File

@ -85,6 +85,53 @@ void vgaScrollUp(void)
} }
} }
void vprintf(const char *format, va_list ap)
{
int i = 0;
while (format[i] != '\0') {
switch (format[i]) {
case '%':
switch (format[i + 1]) {
case 'i':
case 'd': {
int d = va_arg(ap, int);
printInt(d);
break;
}
case 'c': {
int c = va_arg(ap, int);
printChar((char)c);
break;
}
case 's': {
char *str = va_arg(ap, char *);
if(!str)
str = "[NULL STR]";
printString(str);
break;
}
case '%':
printChar('%');
break;
}
i++;
break;
default:
printChar(format[i]);
}
i++;
}
}
void printf(const char *format, ...){
va_list ap;
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
}
void printString(const char *str) void printString(const char *str)
{ {
while (*str) { while (*str) {

View File

@ -16,6 +16,8 @@
#define VGA_WIDTH 80 #define VGA_WIDTH 80
#define VGA_HEIGHT 25 #define VGA_HEIGHT 25
void vprintf(const char *format, va_list ap);
void printf(const char *format, ...);
int initVGA(uint bgColor, uint color); int initVGA(uint bgColor, uint color);
void clearScreen(uint bgColor); void clearScreen(uint bgColor);
void printInt(int integer); void printInt(int integer);