From c354930e2f3a5a85f650baadb9aeb31f1ec3f944 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Wed, 1 Nov 2017 12:57:56 +0000 Subject: [PATCH] Move shit to separate file --- arch/Makefile | 2 +- arch/main.c | 145 +--------------------------------------------- arch/multiboot2.c | 142 +++++++++++++++++++++++++++++++++++++++++++++ arch/multiboot2.h | 2 + 4 files changed, 146 insertions(+), 145 deletions(-) create mode 100644 arch/multiboot2.c diff --git a/arch/Makefile b/arch/Makefile index 8cfdceb..a193465 100644 --- a/arch/Makefile +++ b/arch/Makefile @@ -7,7 +7,7 @@ CCPREFIX = i686-elf- AS = $(CCPREFIX)as CC = $(CCPREFIX)gcc -OBJS = boot.s.o main.c.o logger.c.o console.c.o gdt.c.o gdt.asm.o idt.c.o idt.asm.o isr.c.o isr.asm.o +OBJS = boot.s.o main.c.o logger.c.o console.c.o gdt.c.o gdt.asm.o idt.c.o idt.asm.o isr.c.o isr.asm.o multiboot2.c.o run: kernel qemu-system-i386 -kernel kernel -d guest_errors diff --git a/arch/main.c b/arch/main.c index 59953b1..d3c72d1 100644 --- a/arch/main.c +++ b/arch/main.c @@ -1,11 +1,10 @@ #include #include "logger.h" +#include "multiboot2.h" #include "gdt.h" #include "idt.h" -static void print_multiboot2_info(unsigned long addr); - void main(unsigned int multiboot_magic, unsigned long multiboot_info) { logger_initialize(); @@ -36,145 +35,3 @@ void main(unsigned int multiboot_magic, unsigned long multiboot_info) logger_warn("Nothing to do."); logger_fail("Halt."); } - -#include "console.h" -#include "multiboot2.h" - -static void itoa(char *buf, int base, int d); -static void printf(const char *format, ...); - -void print_multiboot2_info(unsigned long addr) -{ - struct multiboot_tag *tag; - unsigned size; - - if (addr & 7) - { - printf ("Unaligned mbi: 0x%x\n", addr); - return; - } - - size = *(unsigned *) addr; - - printf ("Announced mbi size 0x%x\n", size); - - for ( - tag = (struct multiboot_tag *) (addr + 8); - tag->type != MULTIBOOT_TAG_TYPE_END; - tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag + ((tag->size + 7) & ~7))) - { - switch (tag->type) - { - case MULTIBOOT_TAG_TYPE_MODULE: - printf( - "Module at 0x%x-0x%x. Command line %s\n", - ((struct multiboot_tag_module *) tag)->mod_start, - ((struct multiboot_tag_module *) tag)->mod_end, - ((struct multiboot_tag_module *) tag)->cmdline - ); - break; - } - } - - tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag + ((tag->size + 7) & ~7)); - - printf ("Total mbi size 0x%x\n", (unsigned) tag - addr); -} - -void itoa(char *buf, int base, int d) -{ - char *p = buf; - char *p1, *p2; - unsigned long ud = d; - int divisor = 10; - /* - * If %d is specified and D is minus, put '-' in the head. - * */ - if (base == 'd' && d < 0) - { - *p++ = '-'; - buf++; - ud = -d; - } - else if (base == 'x') - divisor = 16; - /* - * Divide UD by DIVISOR until UD == 0. - * */ - do - { - int remainder = ud % divisor; - *p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10; - } - while (ud /= divisor); - /* - * Terminate BUF. - * */ - *p = 0; - /* - * Reverse BUF. - * */ - p1 = buf; - p2 = p - 1; - while (p1 < p2) - { - char tmp = *p1; - *p1 = *p2; - *p2 = tmp; - p1++; - p2--; - } -} - -void printf(const char *format, ...) -{ - char **arg = (char **) &format; - int c; - char buf[20]; - arg++; - while ((c = *format++) != 0) - { - if (c != '%') - console_putc(c); - else - { - char *p, *p2; - int pad0 = 0, pad = 0; - c = *format++; - if (c == '0') - { - pad0 = 1; - c = *format++; - } - if (c >= '0' && c <= '9') - { - pad = c - '0'; - c = *format++; - } - switch (c) - { - case 'd': - case 'u': - case 'x': - itoa (buf, c, *((int *) arg++)); - p = buf; - goto string; - break; - case 's': - p = *arg++; - if (! p) - p = "(null)"; -string: - for (p2 = p; *p2; p2++); - for (; p2 < p + pad; p2++) - console_putc(pad0 ? '0' : ' '); - while (*p) - console_putc(*p++); - break; - default: - console_putc(*((int *) arg++)); - break; - } - } - } -} diff --git a/arch/multiboot2.c b/arch/multiboot2.c new file mode 100644 index 0000000..e38cfd5 --- /dev/null +++ b/arch/multiboot2.c @@ -0,0 +1,142 @@ +#include "multiboot2.h" + +#include "console.h" + +static void itoa(char *buf, int base, int d); +static void printf(const char *format, ...); + +void print_multiboot2_info(unsigned long addr) +{ + struct multiboot_tag *tag; + unsigned size; + + if (addr & 7) + { + printf ("Unaligned mbi: 0x%x\n", addr); + return; + } + + size = *(unsigned *) addr; + + printf ("Announced mbi size 0x%x\n", size); + + for ( + tag = (struct multiboot_tag *) (addr + 8); + tag->type != MULTIBOOT_TAG_TYPE_END; + tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag + ((tag->size + 7) & ~7))) + { + switch (tag->type) + { + case MULTIBOOT_TAG_TYPE_MODULE: + printf( + "Module at 0x%x-0x%x. Command line %s\n", + ((struct multiboot_tag_module *) tag)->mod_start, + ((struct multiboot_tag_module *) tag)->mod_end, + ((struct multiboot_tag_module *) tag)->cmdline + ); + break; + } + } + + tag = (struct multiboot_tag *) ((multiboot_uint8_t *) tag + ((tag->size + 7) & ~7)); + + printf ("Total mbi size 0x%x\n", (unsigned) tag - addr); +} + +void itoa(char *buf, int base, int d) +{ + char *p = buf; + char *p1, *p2; + unsigned long ud = d; + int divisor = 10; + /* + * If %d is specified and D is minus, put '-' in the head. + * */ + if (base == 'd' && d < 0) + { + *p++ = '-'; + buf++; + ud = -d; + } + else if (base == 'x') + divisor = 16; + /* + * Divide UD by DIVISOR until UD == 0. + * */ + do + { + int remainder = ud % divisor; + *p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10; + } + while (ud /= divisor); + /* + * Terminate BUF. + * */ + *p = 0; + /* + * Reverse BUF. + * */ + p1 = buf; + p2 = p - 1; + while (p1 < p2) + { + char tmp = *p1; + *p1 = *p2; + *p2 = tmp; + p1++; + p2--; + } +} + +void printf(const char *format, ...) +{ + char **arg = (char **) &format; + int c; + char buf[20]; + arg++; + while ((c = *format++) != 0) + { + if (c != '%') + console_putc(c); + else + { + char *p, *p2; + int pad0 = 0, pad = 0; + c = *format++; + if (c == '0') + { + pad0 = 1; + c = *format++; + } + if (c >= '0' && c <= '9') + { + pad = c - '0'; + c = *format++; + } + switch (c) + { + case 'd': + case 'u': + case 'x': + itoa (buf, c, *((int *) arg++)); + p = buf; + goto string; + break; + case 's': + p = *arg++; + if (! p) + p = "(null)"; +string: + for (p2 = p; *p2; p2++); + for (; p2 < p + pad; p2++) + console_putc(pad0 ? '0' : ' '); + while (*p) + console_putc(*p++); + break; + default: + console_putc(*((int *) arg++)); + break; + } + } + } +} diff --git a/arch/multiboot2.h b/arch/multiboot2.h index c14ba17..3b7ed43 100644 --- a/arch/multiboot2.h +++ b/arch/multiboot2.h @@ -1,6 +1,8 @@ #ifndef MULTIBOOT_HEADER #define MULTIBOOT_HEADER 1 +void print_multiboot2_info(unsigned long addr); + /* * How many bytes from the start of the file we search for the header. * */