1
0
Fork 0
mirror of https://github.com/tailix/kernel.git synced 2024-10-30 12:03:52 -04:00
kernel/arch/multiboot.c

185 lines
4.4 KiB
C
Raw Normal View History

#include <kernelmq/multiboot.h>
2017-11-01 08:57:56 -04:00
#include "console.h"
2017-11-01 10:45:01 -04:00
#define MULTIBOOT_1_MAGIC 0x2BADB002
#define MULTIBOOT_2_MAGIC 0x36d76289
2017-11-01 11:54:52 -04:00
#define MULTIBOOT_TAG_TYPE_END 0
#define MULTIBOOT_TAG_TYPE_CMDLINE 1
#define MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME 2
#define MULTIBOOT_TAG_TYPE_MODULE 3
struct multiboot_tag
{
unsigned int type;
unsigned int size;
};
struct multiboot_tag_string
{
unsigned int type;
unsigned int size;
char string[0];
};
struct multiboot_tag_module
{
unsigned int type;
unsigned int size;
unsigned int mod_start;
unsigned int mod_end;
char cmdline[0];
};
2017-11-01 08:57:56 -04:00
static void itoa(char *buf, int base, int d);
static void printf(const char *format, ...);
2017-11-01 10:45:01 -04:00
void print_multiboot_info(struct KernelMQ_Multiboot_Info info)
2017-11-01 08:57:56 -04:00
{
2017-11-01 10:45:01 -04:00
if (info.magic == MULTIBOOT_1_MAGIC) {
printf("Old Multiboot specification does not support modules.");
return;
}
if (info.magic != MULTIBOOT_2_MAGIC) {
printf("No Multiboot-compliant bootloader is not supported.");
return;
}
2017-11-01 08:57:56 -04:00
2017-11-01 10:45:01 -04:00
if (info.addr & 7) {
printf("Unaligned Multiboot information address: 0x%x\n", info.addr);
2017-11-01 08:57:56 -04:00
return;
}
for (
2017-11-01 10:45:01 -04:00
struct multiboot_tag *tag = (struct multiboot_tag *) (info.addr + 8);
2017-11-01 08:57:56 -04:00
tag->type != MULTIBOOT_TAG_TYPE_END;
2017-11-01 11:54:52 -04:00
tag = (struct multiboot_tag *) ((unsigned char *) tag + ((tag->size + 7) & ~7)))
2017-11-01 08:57:56 -04:00
{
switch (tag->type)
{
2017-11-01 09:54:53 -04:00
case MULTIBOOT_TAG_TYPE_CMDLINE:
printf(
"Command line = %s\n",
((struct multiboot_tag_string *) tag)->string
);
break;
case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME:
printf(
"Boot loader name = %s\n",
((struct multiboot_tag_string *) tag)->string
);
break;
2017-11-01 08:57:56 -04:00
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;
}
}
}
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;
}
}
}
}