kern/arch/x86/boot/boot.c

241 lines
5.7 KiB
C

/* See the end of this file for copyright and license terms. */
#include <string.h>
#include <arch/multiboot.h>
#include <gay/cdefs.h>
#include <gay/kprintf.h>
#include <gay/types.h>
enum vga_color {
VGA_COLOR_BLACK = 0,
VGA_COLOR_BLUE = 1,
VGA_COLOR_GREEN = 2,
VGA_COLOR_CYAN = 3,
VGA_COLOR_RED = 4,
VGA_COLOR_MAGENTA = 5,
VGA_COLOR_BROWN = 6,
VGA_COLOR_LIGHT_GREY = 7,
VGA_COLOR_DARK_GREY = 8,
VGA_COLOR_LIGHT_BLUE = 9,
VGA_COLOR_LIGHT_GREEN = 10,
VGA_COLOR_LIGHT_CYAN = 11,
VGA_COLOR_LIGHT_RED = 12,
VGA_COLOR_LIGHT_MAGENTA = 13,
VGA_COLOR_LIGHT_BROWN = 14,
VGA_COLOR_WHITE = 15,
};
#define FB_ADDRESS 0xb8000
#define FB_LINES 24
#define FB_COLS 80
struct fb_cell {
u8 c;
enum vga_color fg:4;
enum vga_color bg:4;
};
/* struct fb_cell gets aligned to 4 bytes for some reason, so we need to use u16 internally */
static volatile u16 *const framebuffer = (volatile u16 *)FB_ADDRESS;
/** @brief current line in the framebuffer */
static unsigned int fb_line;
/** @brief current column in the framebuffer */
static unsigned int fb_col;
/** @brief current background color */
enum vga_color fb_background;
/** @brief current foreground color */
enum vga_color fb_foreground;
static volatile struct fb_cell *cell_at(unsigned int line, unsigned int col);
#define current_cell (cell_at(fb_line, fb_col))
static ssize_t fb_write(struct kprintf_renderer *renderer, const void *buf, size_t size);
static ssize_t fb_flush(struct kprintf_renderer *renderer);
static struct kprintf_renderer fb_kprintf_renderer = {
.write = fb_write,
.flush = fb_flush,
};
static void fb_newline(void);
static void fb_clear(void);
static void fb_init(enum vga_color fg, enum vga_color bg);
static void print_gay_propaganda(void);
static struct mb2_tag *next_tag(struct mb2_tag *tag);
static void handle_tag(struct mb2_tag *tag);
static void handle_mmap_tag(struct mb2_tag_mmap *tag);
void _boot(u32 magic, void *address)
{
kprintf_set_renderer(&fb_kprintf_renderer);
fb_init(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
if (magic != MB2_BOOTLOADER_MAGIC) {
kprintf("Error: invalid bootloader magic, aborting\n");
return;
}
print_gay_propaganda();
for (struct mb2_tag *tag = address + 8; tag != NULL; tag = next_tag(tag))
handle_tag(tag);
while (1);
}
static inline void handle_tag(struct mb2_tag *tag)
{
switch (tag->type) {
case MB2_TAG_TYPE_END:
break;
case MB2_TAG_TYPE_MMAP:
handle_mmap_tag((struct mb2_tag_mmap *)tag);
break;
default:
//kprintf("Unknown tag %u\n", tag->type);
break;
}
}
static inline void handle_mmap_tag(struct mb2_tag_mmap *tag)
{
kprintf("Memory map:\n");
struct mb2_mmap_entry *entry = tag->entries;
while ((void *)entry < (void *)tag + tag->tag.size) {
unsigned int type = entry->type;
// kprintf("start = %p, ", (void *)entry->addr);
// kprintf("len = %p, ", (void *)entry->len);
// kprintf("type = %u\n", type);
kprintf("start = %p, len = %p, type = %u\n",
(void *)entry->addr, (void *)entry->len, type);
entry = (void *)entry + tag->entry_size;
}
}
static inline struct mb2_tag *next_tag(struct mb2_tag *tag)
{
if (tag->type == MB2_TAG_TYPE_END)
return NULL;
else
return (void *)tag + ( (tag->size + 7) & ~7 );
}
static void fb_newline(void)
{
fb_col = 0;
if (fb_line == FB_LINES - 1) {
u8 *first_row = (u8 *)cell_at(0, 0);
u8 *second_row = (u8 *)cell_at(1, 0);
u8 *last_row_end = (u8 *)cell_at(FB_LINES, FB_COLS);
memmove(first_row, second_row, last_row_end - second_row);
} else {
fb_line++;
}
}
static ssize_t fb_write(struct kprintf_renderer *renderer, const void *buf, size_t size)
{
ssize_t ret = 0;
const u8 *s = buf;
while (size > s - (const u8 *)buf) {
u8 c = *s++;
ret++;
if (fb_col == FB_COLS)
fb_newline();
if (c == '\n') {
fb_newline();
continue;
}
current_cell->c = c;
current_cell->fg = fb_foreground;
current_cell->bg = fb_background;
fb_col++;
}
return ret;
}
static ssize_t fb_flush(struct kprintf_renderer *renderer)
{
return 0;
}
static void fb_clear(void)
{
for (unsigned int l = 0; l < FB_LINES; l++) {
for (unsigned int c = 0; c < FB_COLS; c++) {
cell_at(l, c)->c = '\0';
cell_at(l, c)->fg = fb_foreground;
cell_at(l, c)->bg = fb_background;
}
}
}
static void fb_init(enum vga_color fg, enum vga_color bg)
{
fb_line = 0;
fb_col = 0;
fb_foreground = VGA_COLOR_LIGHT_GREY;
fb_background = VGA_COLOR_BLACK;
fb_clear();
}
static void print_gay_propaganda(void)
{
static const enum vga_color rainbow[] = {
VGA_COLOR_RED,
VGA_COLOR_LIGHT_RED,
VGA_COLOR_LIGHT_BROWN,
VGA_COLOR_GREEN,
VGA_COLOR_BLUE,
VGA_COLOR_MAGENTA,
};
enum vga_color bg_before = fb_background;
enum vga_color fg_before = fb_foreground;
for (int i = 0; i < ARRAY_SIZE(rainbow); i++) {
fb_background = rainbow[i];
for (int i = 0; i < FB_COLS; i++)
fb_write(NULL, " ", 1);
}
fb_background = bg_before;
kprintf("\nWelcome to ");
const char *gaybsd = "GayBSD";
for (const char *tmp = gaybsd; *tmp != '\0'; tmp++) {
fb_foreground = rainbow[tmp - gaybsd];
kprintf("%c", *tmp);
}
fb_foreground = fg_before;
kprintf(", be gay do crime!\n\n");
}
static inline volatile struct fb_cell *cell_at(unsigned int line, unsigned int col)
{
return (volatile struct fb_cell *)&framebuffer[line * FB_COLS + col];
}
/*
* This file is part of GayBSD.
* Copyright (c) 2021 fef <owo@fef.moe>.
*
* GayBSD is nonviolent software: you may only use, redistribute, and/or
* modify it under the terms of the Cooperative Nonviolent Public License
* (CNPL) as found in the LICENSE file in the source code root directory
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
* of the license, or (at your option) any later version.
*
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPL for details.
*/