console vga stuff
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
.set ALIGN, 1<<0 /* align loaded modules on page boundaries */
|
||||
.set MEMINFO, 1<<1 /* provide memory map */
|
||||
.set GRAPHICS, 1<<2 /* <-- NEW: Request video mode table/framebuffer from GRUB */
|
||||
.set FLAGS, ALIGN | MEMINFO | GRAPHICS /* Updated to include GRAPHICS */
|
||||
.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */
|
||||
.set CHECKSUM, -(MAGIC + FLAGS) /* recalculated checksum of the new flags */
|
||||
.set ALIGN, 1<<0
|
||||
.set MEMINFO, 1<<1
|
||||
.set GRAPHICS, 1<<2
|
||||
|
||||
.set FLAGS, ALIGN | MEMINFO | GRAPHICS
|
||||
.set MAGIC, 0x1BADB002
|
||||
.set CHECKSUM, -(MAGIC + FLAGS)
|
||||
|
||||
.section .multiboot, "a"
|
||||
.align 4
|
||||
@@ -46,7 +47,6 @@ higher:
|
||||
mov $0xC0000000, %eax
|
||||
add %eax, %ebx
|
||||
push %ebx
|
||||
push %eax
|
||||
call kmain
|
||||
loop:
|
||||
hlt
|
||||
|
||||
49
kernel/drivers/console/console.c
Normal file
49
kernel/drivers/console/console.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <drivers/console/console.h>
|
||||
|
||||
#include <lib/font.h>
|
||||
#include <drivers/vga/vga.h>
|
||||
|
||||
static psf_font_t* font;
|
||||
|
||||
static uint32_t console_row;
|
||||
static uint32_t console_col;
|
||||
static uint32_t console_text_color;
|
||||
static uint32_t console_backgrnd_color;
|
||||
|
||||
static void write_vga_char(int row, int col, char c) {
|
||||
uint32_t vga_pitch = vga_get_pitch();
|
||||
uint8_t* glyph = (uint8_t*)font->glyphs + (c * font->hdr->charsz);
|
||||
|
||||
for (uint32_t y = 0; y < font->hdr->charsz; y++) {
|
||||
uint8_t bits = glyph[y];
|
||||
uint32_t* row = (uint32_t*)((uintptr_t)VGA_FRAMEBUFFER + ((console_row + y) * vga_pitch));
|
||||
for (int x = 0; x < 8; x++) {
|
||||
if (bits & (0b10000000 >> x)) {
|
||||
row[console_col + x] = console_text_color;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void set_console_font(const char* path) {
|
||||
font = load_font(path);
|
||||
}
|
||||
|
||||
void set_console_fg(uint32_t color) {
|
||||
console_text_color = color;
|
||||
}
|
||||
|
||||
void set_console_bg(uint32_t color) {
|
||||
console_backgrnd_color = color;
|
||||
}
|
||||
|
||||
void console_init() {
|
||||
console_row = 0;
|
||||
console_col = 0;
|
||||
set_console_fg(0xFFFFFFFF);
|
||||
set_console_bg(0x00000000);
|
||||
}
|
||||
|
||||
void console_putchar(char c) {
|
||||
|
||||
}
|
||||
13
kernel/drivers/console/console.h
Normal file
13
kernel/drivers/console/console.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef D_CONSOLE_H
|
||||
#define D_CONSOLE_H
|
||||
|
||||
#include <lib/font.h>
|
||||
|
||||
void console_putchar(char c);
|
||||
|
||||
void set_console_font(const char* path);
|
||||
void set_console_fg(uint32_t color);
|
||||
void set_console_bg(uint32_t color);
|
||||
void console_init();
|
||||
|
||||
#endif
|
||||
37
kernel/drivers/serial/serial.c
Normal file
37
kernel/drivers/serial/serial.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <drivers/serial/serial.h>
|
||||
#include <common.h>
|
||||
|
||||
#define COM1 0x3F8
|
||||
|
||||
int is_transmit_empty() {
|
||||
return inb(COM1 + 5) & 0x20;
|
||||
}
|
||||
|
||||
void write_serial(char a) {
|
||||
while (is_transmit_empty() == 0);
|
||||
outb(COM1, a);
|
||||
}
|
||||
|
||||
void print_serial(const char* str) {
|
||||
for (int i = 0; str[i] != '\0'; i++) {
|
||||
write_serial(str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void print_serial_hex(uint32_t val) {
|
||||
char hex_chars[] = "0123456789ABCDEF";
|
||||
print_serial("0x");
|
||||
for (int i = 28; i >= 0; i -= 4) {
|
||||
write_serial(hex_chars[(val >> i) & 0x0F]);
|
||||
}
|
||||
}
|
||||
|
||||
void init_serial() {
|
||||
outb(COM1 + 1, 0x00); // Disable all interrupts
|
||||
outb(COM1 + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
||||
outb(COM1 + 0, 0x01); // Set divisor to 1 (lo byte) -> 115200 baud
|
||||
outb(COM1 + 1, 0x00); // (hi byte)
|
||||
outb(COM1 + 3, 0x03); // 8 bits, no parity, one stop bit
|
||||
outb(COM1 + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
||||
outb(COM1 + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
||||
}
|
||||
12
kernel/drivers/serial/serial.h
Normal file
12
kernel/drivers/serial/serial.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef D_SERIAL_H
|
||||
#define D_SERIAL_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void init_serial();
|
||||
void print_serial_hex(uint32_t val);
|
||||
void print_serial(const char* str);
|
||||
void write_serial(char a);
|
||||
int is_transmit_empty();
|
||||
|
||||
#endif
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <drivers/vga/vga.h>
|
||||
|
||||
#include <common.h>
|
||||
#include <memory/mm.h>
|
||||
|
||||
static uint32_t framebuffer_address;
|
||||
static uint32_t framebuffer_width;
|
||||
@@ -14,76 +15,51 @@ static uint32_t framebuffer_total_pixels;
|
||||
static uint32_t framebuffer_pitch;
|
||||
static uint32_t framebuffer_bpp;
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
void vga_init(
|
||||
uint32_t fb_vaddr,
|
||||
uint32_t fb_paddr,
|
||||
uint32_t fb_width,
|
||||
uint32_t fb_height,
|
||||
uint32_t fb_pitch,
|
||||
uint8_t fb_bpp
|
||||
) {
|
||||
framebuffer_address = fb_vaddr;
|
||||
uint32_t fb_size_bytes = fb_height * fb_pitch;
|
||||
uint32_t num_pages = (fb_size_bytes + 4095) / 4096;
|
||||
for (uint32_t i = 0; i < num_pages; i++) {
|
||||
uint32_t phys_addr = fb_paddr + (i * 4096);
|
||||
uint32_t virt_addr = VGA_FRAMEBUFFER + (i * 4096);
|
||||
map_page(virt_addr, phys_addr, PAGE_PRESENT | PAGE_WRITABLE);
|
||||
}
|
||||
|
||||
framebuffer_address = VGA_FRAMEBUFFER;
|
||||
framebuffer_width = fb_width;
|
||||
framebuffer_height = fb_height;
|
||||
framebuffer_pitch = fb_pitch;
|
||||
framebuffer_bpp = fb_bpp;
|
||||
|
||||
framebuffer_total_pixels = framebuffer_width * framebuffer_height;
|
||||
|
||||
vga_clear_scrn(0xFFFFFFFF);
|
||||
vga_clear_scrn(0x000000FF);
|
||||
}
|
||||
|
||||
uint32_t vga_get_pitch() {
|
||||
return framebuffer_pitch;
|
||||
}
|
||||
|
||||
void vga_clear_scrn(uint32_t color) {
|
||||
if (framebuffer_address == 0) return;
|
||||
|
||||
// Completely ignore the framebuffer address for a split second.
|
||||
// Let's force raw ASCII text into the legacy VGA text console area.
|
||||
uint16_t* text_mode_buffer = (uint16_t*)0xB8000;
|
||||
|
||||
// Fill the screen with 'A' characters on a red background
|
||||
for (int i = 0; i < 80 * 25; i++) {
|
||||
text_mode_buffer[i] = (0x4F << 8) | 'A';
|
||||
for (uint32_t y = 0; y < framebuffer_height; y++) {
|
||||
uint32_t* row = (uint32_t*)((uintptr_t)framebuffer_address + (y * framebuffer_pitch));
|
||||
for (uint32_t x = 0; x < framebuffer_width; x++) {
|
||||
row[x] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vga_putchar(char c) {
|
||||
if (c == '\n') {
|
||||
return;
|
||||
}
|
||||
void vga_drawchar(char c, int x, int y, psf_font_t* font) {
|
||||
|
||||
if (c == '\t') {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
vga_putchar(' ');
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t vga_seek(vfs_node_t* node, uint32_t offset, int whence) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t vga_write(vfs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buf) {
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
vga_putchar((char)buf[i]);
|
||||
}
|
||||
// probably just memcpy here
|
||||
return size;
|
||||
}
|
||||
@@ -2,19 +2,42 @@
|
||||
#define DVGA_H
|
||||
|
||||
#include <vfs.h>
|
||||
#include <lib/font.h>
|
||||
|
||||
#define VGA_FRAMEBUFFER 0xFF700000
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
void vga_init(
|
||||
uint32_t fb_vaddr,
|
||||
uint32_t fb_paddr,
|
||||
uint32_t fb_width,
|
||||
uint32_t fb_height,
|
||||
uint32_t fb_pitch,
|
||||
uint8_t fb_bpp
|
||||
);
|
||||
|
||||
uint32_t vga_get_pitch();
|
||||
|
||||
void vga_clear_scrn(uint32_t color);
|
||||
void vga_putchar(char c);
|
||||
void vga_drawchar(char c, int x, int y, psf_font_t* font);
|
||||
|
||||
uint32_t vga_write(vfs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buf);
|
||||
uint32_t vga_seek(vfs_node_t* node, uint32_t offset, int whence);
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "lib/font.h"
|
||||
#include "multiboot.h"
|
||||
#include "interrupts.h"
|
||||
#include "gdt.h"
|
||||
@@ -22,6 +23,8 @@
|
||||
#include <drivers/vga/vga.h>
|
||||
#include <drivers/cpio/cpio.h>
|
||||
#include <drivers/devfs/devfs.h>
|
||||
#include <drivers/serial/serial.h>
|
||||
#include <drivers/console/console.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -29,6 +32,8 @@
|
||||
extern char __kernel_start;
|
||||
extern char __kernel_end;
|
||||
|
||||
#define SERIAL_DBG
|
||||
|
||||
extern uint32_t page_directory[1024];
|
||||
void* kernel_cr3 = page_directory;
|
||||
|
||||
@@ -95,37 +100,41 @@ void load_root_program(const char* path) {
|
||||
flush_tlb();
|
||||
}
|
||||
|
||||
void kmain(uint32_t magic, multiboot_info* mbi) {
|
||||
void kmain(multiboot_info* mbi) {
|
||||
disable_interrupts();
|
||||
|
||||
vga_init(mbi->framebuffer_addr,
|
||||
mbi->framebuffer_width,
|
||||
mbi->framebuffer_height,
|
||||
mbi->framebuffer_pitch,
|
||||
mbi->framebuffer_bpp);
|
||||
|
||||
if (magic != 0x2BADB002) {
|
||||
return;
|
||||
}
|
||||
init_serial();
|
||||
|
||||
// need mmap and fb info
|
||||
if (!(mbi->flags & (1 << 6)) || !(mbi->flags & (1 << 12))) {
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
uint32_t mmap_virtual_addr = mbi->mmap_addr + 0xC0000000;
|
||||
init_pmm((void*)mmap_virtual_addr, mbi->mmap_length);
|
||||
kprintf("memory detection complete. total free memory: %dMB\n", total_free_memory() / 1024 / 1024);
|
||||
parse_multiboot_modules((uint32_t)mbi);
|
||||
|
||||
uint32_t vga_fb_address = mbi->framebuffer_addr;
|
||||
uint32_t vga_fb_width = mbi->framebuffer_width;
|
||||
uint32_t vga_fb_height = mbi->framebuffer_height;
|
||||
uint32_t vga_fb_pitch = mbi->framebuffer_pitch;
|
||||
uint32_t vga_fb_bpp = mbi->framebuffer_bpp;
|
||||
|
||||
init_gdt();
|
||||
init_idt();
|
||||
init_pic();
|
||||
init_pit();
|
||||
|
||||
|
||||
uint32_t mmap_virtual_addr = mbi->mmap_addr + 0xC0000000;
|
||||
init_pmm((void*)mmap_virtual_addr, mbi->mmap_length);
|
||||
init_page_tables();
|
||||
|
||||
parse_multiboot_modules((uint32_t)mbi);
|
||||
vga_init(vga_fb_address,
|
||||
vga_fb_width,
|
||||
vga_fb_height,
|
||||
vga_fb_pitch,
|
||||
vga_fb_bpp);
|
||||
|
||||
console_init();
|
||||
set_console_font("/fonts/cp850-8x16.psf");
|
||||
|
||||
kprintf("basic initializations complete\n");
|
||||
|
||||
|
||||
44
kernel/lib/font.c
Normal file
44
kernel/lib/font.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "vfs.h"
|
||||
#include <lib/font.h>
|
||||
#include <lib/memory.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
psf_font_t* load_font(const char* path) {
|
||||
int fd = vfs_open(path, 0);
|
||||
if (fd == -1) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
psf_hdr_t* header = kalloc(sizeof(psf_hdr_t));
|
||||
int rd = vfs_read(fd, header, sizeof(psf_hdr_t));
|
||||
if (rd <= 0) {
|
||||
kfree(header);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t num_glyphs = (header->mode & (1 << 0)) ? 512 : 256;
|
||||
uint32_t glyph_sz = header->charsz;
|
||||
uint32_t glyph_buf_sz = num_glyphs * glyph_sz;
|
||||
|
||||
void* glyphs = kalloc(glyph_buf_sz);
|
||||
|
||||
rd = 0;
|
||||
rd = vfs_read(fd, glyphs, glyph_buf_sz);
|
||||
if (rd <= 0) {
|
||||
kfree(header);
|
||||
kfree(glyphs);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
psf_font_t* font = kalloc(sizeof(psf_font_t));
|
||||
font->hdr = header;
|
||||
font->glyphs = glyphs;
|
||||
return font;
|
||||
}
|
||||
|
||||
void free_font(psf_font_t* font) {
|
||||
kfree(font->glyphs);
|
||||
kfree(font->hdr);
|
||||
kfree(font);
|
||||
}
|
||||
20
kernel/lib/font.h
Normal file
20
kernel/lib/font.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef KFONT_H
|
||||
#define KFONT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t magic[2];
|
||||
uint8_t mode;
|
||||
uint8_t charsz;
|
||||
} psf_hdr_t;
|
||||
|
||||
typedef struct {
|
||||
psf_hdr_t* hdr;
|
||||
void* glyphs;
|
||||
} psf_font_t;
|
||||
|
||||
psf_font_t* load_font(const char* path);
|
||||
void free_font(psf_font_t* font);
|
||||
|
||||
#endif
|
||||
@@ -5,21 +5,24 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <drivers/vga/vga.h>
|
||||
#include <drivers/console/console.h>
|
||||
|
||||
spinlock_t kwrite_lock = {0};
|
||||
|
||||
extern psf_font_t* kfont;
|
||||
|
||||
static char* hex_chars = "0123456789abcdef";
|
||||
|
||||
static void kprint(const char *str) {
|
||||
while (*str) {
|
||||
vga_putchar(*str);
|
||||
console_putchar(*str);
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
static void kputd(int d) {
|
||||
if (d == 0) {
|
||||
vga_putchar('0');
|
||||
console_putchar('0');
|
||||
return;
|
||||
}
|
||||
char buffer[12];
|
||||
@@ -29,13 +32,13 @@ static void kputd(int d) {
|
||||
d /= 10;
|
||||
}
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
vga_putchar(buffer[j]);
|
||||
console_putchar(buffer[j]);
|
||||
}
|
||||
}
|
||||
|
||||
static void kputx(unsigned int x) {
|
||||
if (x == 0) {
|
||||
vga_putchar('0');
|
||||
console_putchar('0');
|
||||
return;
|
||||
}
|
||||
char buffer[8];
|
||||
@@ -46,13 +49,13 @@ static void kputx(unsigned int x) {
|
||||
x >>= 4;
|
||||
}
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
vga_putchar(buffer[j]);
|
||||
console_putchar(buffer[j]);
|
||||
}
|
||||
}
|
||||
|
||||
void kputc(const char c) {
|
||||
spin_lock(&kwrite_lock);
|
||||
vga_putchar(c);
|
||||
console_putchar(c);
|
||||
spin_unlock(&kwrite_lock);
|
||||
}
|
||||
|
||||
@@ -82,20 +85,20 @@ void kprintf(const char* fmt, ...) {
|
||||
}
|
||||
case 'c': {
|
||||
int c = va_arg(args, int);
|
||||
vga_putchar((char)c);
|
||||
console_putchar((char)c);
|
||||
break;
|
||||
}
|
||||
case '%':
|
||||
vga_putchar('%');
|
||||
console_putchar('%');
|
||||
break;
|
||||
default:
|
||||
vga_putchar('%');
|
||||
vga_putchar(*fmt);
|
||||
console_putchar('%');
|
||||
console_putchar(*fmt);
|
||||
break;
|
||||
}
|
||||
fmt++;
|
||||
} else {
|
||||
vga_putchar(*fmt);
|
||||
console_putchar(*fmt);
|
||||
fmt++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user