os/kernel/kernel.c

214 lines
4.6 KiB
C
Raw Normal View History

#include "multiboot.h"
#include "psf.h"
#include <stdint.h>
2022-05-02 21:59:24 +00:00
#include <stdarg.h>
#if defined (__linux__)
#error "You are not using a cross-compiler, you will most certainly run into trouble"
#endif
2022-05-02 14:26:07 +00:00
#if !defined (__x86_64__)
#error "The kernel needs to be compiled with a x86_64-elf compiler"
#endif
/* graphics framebuffer */
char *fb;
/* number of bytes in each line */
int scanline;
/* extern the symbols in the psf object */
extern char consolefonts_binary__start;
extern char consolefonts_binary__end;
multiboot_info_t *mbi;
uint32_t console_y;
uint32_t console_x;
2022-05-03 22:26:53 +00:00
uint32_t console_rows;
uint32_t console_cols;
2022-05-02 21:59:24 +00:00
#define PIXEL uint32_t
2022-05-02 19:56:36 +00:00
#define CONSOLE_BG 0
#define CONSOLE_FG 0xFFFFFFFF
void putchar (uint16_t c, int32_t cx, int32_t cy, uint32_t fg, uint32_t bg)
{
psf_font_t *font = (psf_font_t *)&consolefonts_binary__start;
uint32_t bytesperline = (font->width + 7) / 8;
unsigned char *glyph = (unsigned char *)&consolefonts_binary__start + font->headersize +
(c > 0 && c < font->numglyphs ? c : 0) * font->bytesperglyph;
uint32_t offs = (cy * font->height * scanline) + (cx * (font->width) * sizeof (PIXEL));
uint32_t x, y, line, mask;
for (y = 0; y < font->height; y++) {
line = offs;
mask = 1 << (font->width -1);
for (x = 0; x < font->width; x++) {
*((PIXEL *)(fb+line)) = *((uint32_t *)glyph) & mask ? fg : bg;
mask >>= 1;
line += sizeof (PIXEL);
}
glyph += bytesperline;
offs += scanline;
}
}
2022-05-03 22:26:53 +00:00
void set_pixel(uint64_t x, uint64_t y, PIXEL color)
2022-05-02 21:59:24 +00:00
{
2022-05-03 22:26:53 +00:00
uint32_t pos = (y * sizeof (PIXEL) * mbi->framebuffer_width) + (x * sizeof (PIXEL));
*((PIXEL *)(fb+pos)) = color;
}
PIXEL get_pixel(uint64_t x, uint64_t y)
{
uint32_t pos = (y * sizeof (PIXEL) * mbi->framebuffer_width) + (x * sizeof (PIXEL));
return *((PIXEL *)(fb+pos));
}
void console_scroll()
{
const uint32_t height = mbi->framebuffer_height;
const uint32_t width = mbi->framebuffer_width;
const uint32_t font_height = ((psf_font_t *)&consolefonts_binary__start)->height;
/* Move each row to the previous one */
for (int row = 1; row < console_rows; row++) {
for (int y = 0; y < font_height; y++ ) {
for (int x = 0; x < width; x++ ) {
PIXEL p = get_pixel(x, (row * font_height) + y);
set_pixel(x, ((row - 1) * font_height) + y, p);
}
}
}
/* Reset bottom row*/
for (int y = 0; y < font_height; y++ ) {
for (int x = 0; x < width; x++ ) {
set_pixel(x, ((console_rows - 1) * font_height) + y, CONSOLE_BG);
}
}
2022-05-02 21:59:24 +00:00
}
void console_puts(uint16_t ch)
{
if (ch == '\n') {
2022-05-02 21:59:24 +00:00
console_x = 0;
console_y++;
return;
}
2022-05-03 22:26:53 +00:00
if (console_x >= console_cols) {
console_x = 0;
console_y++;
}
2022-05-03 22:26:53 +00:00
if (console_y >= console_rows)
console_scroll();
2022-05-02 21:59:24 +00:00
putchar((uint16_t) ch, console_x, console_y, CONSOLE_FG, CONSOLE_BG);
console_x++;
}
2022-05-02 19:56:36 +00:00
void console_write(char *str)
{
for (int idx = 0; str[idx] != '\0'; idx++ ) {
2022-05-02 21:59:24 +00:00
console_puts(str[idx]);
2022-05-02 19:56:36 +00:00
}
}
2022-05-02 21:59:24 +00:00
void console_init(multiboot_info_t *mbi)
2022-05-02 19:56:36 +00:00
{
2022-05-02 21:59:24 +00:00
psf_font_t *font = (psf_font_t *)&consolefonts_binary__start;
2022-05-03 22:26:53 +00:00
console_rows = mbi->framebuffer_height / font->height;
console_cols = mbi->framebuffer_width / font->width;
2022-05-02 21:59:24 +00:00
2022-05-02 19:56:36 +00:00
console_x = 0;
console_y = 0;
}
void convert(uint64_t num, int base, char *buf, int bufsize)
2022-05-02 21:59:24 +00:00
{
int idx = 0;
buf[idx] = '\0';
do {
/* Avoid buffer overrun */
if (idx == bufsize - 1)
break;
2022-05-03 20:24:05 +00:00
int remainder = num % base;
if (remainder > 9)
buf[idx] = remainder - 10 + 'A';
else
buf[idx] = remainder + '0';
2022-05-02 21:59:24 +00:00
num /= base;
idx++;
} while(num != 0);
buf[idx] = '\0';
for (int i = 0; i < idx / 2; i++) {
int tmp = buf[i];
2022-05-03 18:47:18 +00:00
buf[i] = buf[idx-1-i];
buf[idx-1-i] = tmp;
2022-05-02 21:59:24 +00:00
}
}
void kprintf(char *format, ...)
{
va_list arg;
va_start(arg, format);
char buffer[50];
long i;
2022-05-02 21:59:24 +00:00
for (int idx = 0; format[idx] != '\0'; idx++) {
while (format[idx] != '%') {
2022-05-03 20:01:43 +00:00
/* Never overrun the buffer */
2022-05-02 21:59:24 +00:00
if (format[idx] == '\0')
2022-05-03 20:01:43 +00:00
return;
2022-05-02 21:59:24 +00:00
console_puts(format[idx]);
idx++;
}
idx++;
switch (format[idx]) {
case 'd':
i = va_arg(arg, long);
2022-05-02 21:59:24 +00:00
if (i < 0) {
i = -i;
console_puts('-');
}
convert(i, 10, buffer, 50);
console_write(buffer);
break;
2022-05-03 20:24:05 +00:00
case 'X':
i = va_arg(arg, long);
2022-05-03 20:24:05 +00:00
convert(i, 16, buffer, 50);
console_write(buffer);
break;
2022-05-02 21:59:24 +00:00
}
}
va_end(arg);
}
void kernel_main (uint32_t multiboot_struct_addr)
{
mbi = (multiboot_info_t *)multiboot_struct_addr;
fb = (char *)mbi->framebuffer_addr;
scanline = mbi->framebuffer_pitch;
2022-05-02 21:59:24 +00:00
console_init(mbi);
kprintf("Console width is %d\n", mbi->framebuffer_width);
}