diff --git a/Makefile b/Makefile index 2855285..71990e8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,4 @@ - -CFLAGS = -m32 -nostdlib -fno-builtin -fno-exceptions -fno-leading-underscore -fno-pie +CFLAGS = -m32 -nostdlib -fno-builtin -fno-exceptions -fno-leading-underscore -fno-pie -fno-stack-protector LDFLAGS = disk.img: boot.sector kernel.bin hello.bin print.bin hello.txt boot32.bin kernel32.bin @@ -26,8 +25,11 @@ print.bin: print.asm boot32.bin: boot32.asm nasm $< -o $@ -kernel32.bin: kernel32.ld kernel32.o - ld $(LDFLAGS) -T $< -o $@ kernel32.o +kernel32.bin: kernel32.ld kernel32_strap.o kernel32.o + ld $(LDFLAGS) -T $< kernel32.o: kernel32.c - gcc $(CFLAGS) -o $@ -c $< \ No newline at end of file + gcc $(CFLAGS) -o $@ -c $< + +kernel32_strap.o: kernel32_strap.asm + nasm -felf32 $< -o $@ \ No newline at end of file diff --git a/kernel32.c b/kernel32.c index 67d6fba..5cad07c 100644 --- a/kernel32.c +++ b/kernel32.c @@ -1,28 +1,36 @@ -void clear_screen(); -void set_curpos(int x, int y); -void print(const char* string); +#include "types.h" -unsigned char* VMEM_ADDR = (unsigned char*)0xb8000; +uint8_t* VMEM_ADDR = (uint8_t*)0xb8000; const int TERM_WIDTH = 80; const int TERM_HEIGHT = 25; int cursor_x = 0; int cursor_y = 0; -int main() { - asm( - "mov $0x10, %ax\n" - "mov %ax, %ds\n" - "mov %ax, %ss\n" - "mov $0x90000, %esp" - ); +void outb(uint16_t portnumber, uint8_t data) { + asm volatile("outb %0, %1" : : "a" (data), "Nd" (portnumber)); +} +uint8_t inb(uint16_t portnumber) { + uint8_t result; + asm volatile("inb %1, %0" : "=a" (result) : "Nd" (portnumber)); + return result; +} - clear_screen(); - set_curpos(0, 0); +uint16_t get_cursor_position() { + uint16_t cursor_position = 0; + uint8_t* cursor_position_split = (uint8_t*)&cursor_position; + outb(0x3D4, 0x0F); + cursor_position_split[0] = inb(0x3D5); + outb(0x3D4, 0x0E); + cursor_position_split[1] = inb(0x3D5); + return cursor_position; +} - print("Hello, World!\n\nWe are running XnoeOS Code in C now, Protected Mode has been achieved and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~"); +void init_term() { + uint16_t cursor_position = get_cursor_position(); - while (1) {} + cursor_y = cursor_position / TERM_WIDTH; + cursor_x = cursor_position % TERM_WIDTH; } void clear_screen() { @@ -55,4 +63,15 @@ void print(const char* string) { cursor_y++; } } +} + + +int main() { + init_term(); + + print("KERNEL32 OK!\n\n"); + + print("Hello, World!\n\nWe are running XnoeOS Code in C now, Protected Mode has been achieved and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~"); + + while (1) {} } \ No newline at end of file diff --git a/kernel32.ld b/kernel32.ld index 014eb3e..36d1670 100644 --- a/kernel32.ld +++ b/kernel32.ld @@ -1,9 +1,14 @@ -ENTRY(main) OUTPUT_FORMAT(binary) OUTPUT_ARCH(i386:i386) +INPUT(kernel32_strap.o kernel32.o) +OUTPUT(kernel32.bin) + SECTIONS { . = 0x80000; - .text : { *(.text*) } + .text : { + kernel32_strap.o(.text) + kernel32.o(.text) + } } \ No newline at end of file diff --git a/kernel32_strap.asm b/kernel32_strap.asm new file mode 100644 index 0000000..802d6b8 --- /dev/null +++ b/kernel32_strap.asm @@ -0,0 +1,11 @@ +[BITS 32] + +_start: + mov ax, 10h + mov ds, ax + mov ss, ax + mov esp, 90000h + + call main + +extern main \ No newline at end of file diff --git a/types.h b/types.h new file mode 100644 index 0000000..8706872 --- /dev/null +++ b/types.h @@ -0,0 +1,11 @@ +#ifndef TYPES_H +#define TYPES_H + +typedef char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef long long int int32_t; +typedef unsigned long long int uint32_t; + +#endif \ No newline at end of file