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;
initVGA(BLACK, GREEN);
printString("Setting up IDT\n");
printf("Setting up IDT\n");
gdtSetup();
idtSetup();
irqSetup();
@ -32,39 +32,31 @@ void kmain(unsigned long magic, unsigned long addr)
multiboot_info_t *mbi = (multiboot_info_t *)addr;
/* Are mem_* valid? */
if (CHECK_FLAG(mbi->flags, 0)){
printString("mem_lower = ");
printInt((unsigned)mbi->mem_lower);
printString("KB mem_upper = ");
printInt((unsigned)mbi->mem_upper);
printString("KB\n");
printf("mem_lower = %dKB mem_upper %dKB\n", mbi->mem_lower, mbi->mem_upper);
upper_mem = mbi->mem_upper;
}
/* Is boot_device valid? */
if (CHECK_FLAG(mbi->flags, 1)){
printString("boot_device = ");
printInt((unsigned)mbi->boot_device);
printString("\n");
printf("boot_device = %d\n", mbi->boot_device);
}
/* Is the command line passed? */
if (CHECK_FLAG(mbi->flags, 2)){
printString("cmdline = ");
printString((char *)mbi->cmdline);
printString("\n");
printf("cmdline = %s\n", (char *)mbi->cmdline);
}
}
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;
}
memInit(upper_mem);
printString("Setting up IRQ handlers\n");
printf("Setting up IRQ handlers\n");
irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);
irqSetRoutine(IRQ_TIMER, timer_handler);
printString("Enabling HW interrupts\n");
printf("Enabling HW interrupts\n");
exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, print_handler);
// Enabling the HW interrupts
asm volatile("sti\n");
@ -72,5 +64,5 @@ void kmain(unsigned long magic, unsigned long addr)
while (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)
{
printString("Free Mem going from ");
printInt((unsigned long)&__ld_kernel_end);
printString(" to ");
printInt(upper_mem * 1024);
printString("\n");
printf("Free Mem going from %d to %d\n", &__ld_kernel_end, upper_mem *1024);
unsigned long memdesc_end =
(unsigned long)page_desc +
((upper_mem) / (PAGE_SIZE / 1024)) * sizeof(struct mem_desc);

View File

@ -280,9 +280,9 @@ void keyboard_do_irq()
break;
default:
if (lshift || rshift)
printString(scancode_shift[(int)c]);
printf(scancode_shift[(int)c]);
else
printString(scancode[(int)c]);
printf(scancode[(int)c]);
}
}else {
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)
{
while (*str) {

View File

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