From bc6b1dde4d5a9fd2779c6328b43553488f46fa45 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 13 Aug 2013 20:31:28 +1200 Subject: Initial Commit --- Makefile | 28 ++++++++++++++++++++++ README | 0 boot.s | 1 + console.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ console.h | 41 +++++++++++++++++++++++++++++++ kernel.c | 16 +++++++++++++ lowlevel.c | 24 +++++++++++++++++++ lowlevel.h | 6 +++++ string.c | 14 +++++++++++ string.h | 6 +++++ toast.h | 17 +++++++++++++ 11 files changed, 234 insertions(+) create mode 100644 Makefile create mode 100644 README create mode 120000 boot.s create mode 100644 console.c create mode 100644 console.h create mode 100644 kernel.c create mode 100644 lowlevel.c create mode 100644 lowlevel.h create mode 100644 string.c create mode 100644 string.h create mode 100644 toast.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..449916f --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +CC = i586-elf-gcc +LD = i586-elf-ld +AS = nasm + +CFLAGS = -I. -std=gnu99 -ffreestanding -O2 -Wall -Wextra +ASFLAGS = -felf +LDFLAGS = -Tlink.ld + +TOAST_TARGET = ~/toast/toast_img/ +SOURCES = boot.o kernel.o console.o string.o lowlevel.o + +all: $(SOURCES) link + +clean: + - rm *.o kernel + +link: + - $(LD) $(LDFLAGS) -o kernel $(SOURCES) + +.s.o: + - nasm $(ASFLAGS) $< + +install: +# sudo umount $(TOAST_TARGET) + sudo mount /dev/loop11p1 $(TOAST_TARGET) + sudo cp kernel $(TOAST_TARGET) + sudo umount $(TOAST_TARGET) + bochs -qf bochsrc diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/boot.s b/boot.s new file mode 120000 index 0000000..320d590 --- /dev/null +++ b/boot.s @@ -0,0 +1 @@ +boot.asm \ No newline at end of file diff --git a/console.c b/console.c new file mode 100644 index 0000000..9b51a75 --- /dev/null +++ b/console.c @@ -0,0 +1,81 @@ +#ifndef CONSOLE_C +#define CONSOLE_C + +#include + +/********************************************************* + * Set up the console buffer and pointer + ********************************************************/ +void console_init() +{ + console_buffer = (char*)0xB8000; + console_pointer = 0; +} + +/********************************************************* + * Update flashing cursor to match console_position + * Note: Does _NOT_ dictate the pos for next printed byte + ********************************************************/ +void console_update_cursor() +{ + outb(0x3D4, 0x0F); + outb(0x3D5, ((console_pointer/2) & 0xFF)); + outb(0x3D4, 0x0E); + outb(0x3D5, (((console_pointer/2)>>8)&0xFF)); +} + +/********************************************************* + * Clear the console with the current colour scheme + ********************************************************/ +void console_clear() +{ + int console_pointer; + for (console_pointer = 0; console_pointer < 4000; console_pointer++) + { + console_buffer[console_pointer] = console_color; + console_buffer[console_pointer++] = 0; + } + //console_update_cursor(); +} + + +/********************************************************* + * Set the console foreground and background colours + ********************************************************/ +void console_set_colors(uint8_t fg, uint8_t bg) +{ + console_color = (bg << 4) | fg; +} + + +/********************************************************* + * Set the console foreground colour + ********************************************************/ +void console_set_color(uint8_t fg) +{ + console_color &= 0xF0; // Zero-out foreground color + console_color |= fg; // Apply foreground color +} + +/********************************************************* + * Print a null-terminated string to the text screen + ********************************************************/ +void console_print(char *string) +{ + uint16_t i; + for (i = 0; i < strlen(string); i++) + { + // If special char, handle appropriately + if (string[i] < 32) + { + switch (string[i]) + case 10: + console_pointer = 160 + (console_pointer / 160) * 160; + } else { + console_buffer[console_pointer++] = string[i]; + console_buffer[console_pointer++] = console_color; + } + } + console_update_cursor(); +} +#endif \ No newline at end of file diff --git a/console.h b/console.h new file mode 100644 index 0000000..85dd5d6 --- /dev/null +++ b/console.h @@ -0,0 +1,41 @@ +#ifndef __CONSOLE_H +#define __CONSOLE_H + +#define console_width 80 +#define console_height 25 +#define console_x_pos (console_pointer%(2*console_width)) +#define console_y_pos (uint8_t)(console_pointer/(2*console_width)) + +#define COLOR_BLACK 0x0 +#define COLOR_BLUE 0x1 +#define COLOR_GREEN 0x2 +#define COLOR_TEAL 0x3 +#define COLOR_BROWN 0x4 +#define COLOR_PURPLE 0x5 +#define COLOR_GOLD 0x6 +#define COLOR_BRIGHT_GREY 0x7 +#define COLOR_BRIGHT_GRAY 0x7 // Alias for grey vs gray +#define COLOR_GRAY 0x8 +#define COLOR_GREY 0x8 // Alias for grey vs gray +#define COLOR_BRIGHT_BLUE 0x9 +#define COLOR_BRIGHT_GREEN 0xA +#define COLOR_LIME 0xA // Alias for lime vs bright green +#define COLOR_AQUA 0xB +#define COLOR_RED 0xC +#define COLOR_PINK 0xD +#define COLOR_YELLOW 0xE +#define COLOR_WHITE 0xF + + +char* console_buffer; +uint16_t console_pointer; +uint8_t console_color; + +void console_init(); +void console_update_cursor(); +void console_clear(); +void console_set_colors(uint8_t fg, uint8_t bg); +void console_set_color(uint8_t fg); +void console_print(char *string); + +#endif \ No newline at end of file diff --git a/kernel.c b/kernel.c new file mode 100644 index 0000000..84d5587 --- /dev/null +++ b/kernel.c @@ -0,0 +1,16 @@ +// Complain if some pleb's not using a cross-compiler +#ifdef __linux__ + #error "You're not using a cross-compiler. Good luck with that." +#endif + +#include + +void kernel_main() +{ + console_init(); + console_set_colors(COLOR_BRIGHT_GRAY,COLOR_BLACK); + console_clear(); + console_print("Welcome to Toast "KERNEL_VERSION" (nickname '"KERNEL_NICKNAME"')\n"); + console_set_color(COLOR_RED); + console_print("Here is kernel_main()\n"); +} \ No newline at end of file diff --git a/lowlevel.c b/lowlevel.c new file mode 100644 index 0000000..b305ea4 --- /dev/null +++ b/lowlevel.c @@ -0,0 +1,24 @@ +#ifndef LOWLEVEL_C +#define LOWLEVEL_C + +#include + +void outb(uint16_t p,uint8_t val) +{ + asm("outb %%al,%%dx;" + : + :"d"(p),"a"(val) + ); +} + +void disable_ints() +{ + asm("cli"); +} + +void enable_ints() +{ + asm("sti"); +} + +#endif \ No newline at end of file diff --git a/lowlevel.h b/lowlevel.h new file mode 100644 index 0000000..8551568 --- /dev/null +++ b/lowlevel.h @@ -0,0 +1,6 @@ +#ifndef LOWLEVEL_H +#define LOWLEVEL_H + +void outb(uint16_t p,uint8_t val); + +#endif \ No newline at end of file diff --git a/string.c b/string.c new file mode 100644 index 0000000..14bee28 --- /dev/null +++ b/string.c @@ -0,0 +1,14 @@ +#ifndef STRING_C +#define STRING_C + +#include + +uint64_t strlen(char *string) +{ + uint64_t l = 0; + while (string[l] != 0) + l++; + return l; +} + +#endif diff --git a/string.h b/string.h new file mode 100644 index 0000000..41956c5 --- /dev/null +++ b/string.h @@ -0,0 +1,6 @@ +#ifndef STRING_H +#define STRING_H + +uint64_t strlen(char *string); + +#endif \ No newline at end of file diff --git a/toast.h b/toast.h new file mode 100644 index 0000000..6299fb9 --- /dev/null +++ b/toast.h @@ -0,0 +1,17 @@ +#ifndef __TOAST_H +#define __TOAST_H + +#define KERNEL_VERSION "0.1" +#define KERNEL_NICKNAME "Unstable" + +#include // Get our booleans defined +#include // uint_8 etc + +#include +#include +#include + +// KERNEL.C +void kernel_main(); + +#endif \ No newline at end of file -- cgit v1.1