diff --git a/Makefile b/Makefile index 73fc60d..02d31dc 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ CC = i386-elf-gcc LD = i386-elf-ld AS = nasm -CFLAGS = -ffreestanding -fno-builtin -fno-stack-protector -nostdinc -mgeneral-regs-only -m32 -march=i386 -Wall -Iinclude/c +CFLAGS = -std=c17 -ffreestanding -fno-builtin -fno-stack-protector -fno-pie \ + -Os -nostdinc -mgeneral-regs-only -m32 -march=i386 -Wall -Iinclude/c SRC = ./src BUILD = ./build @@ -43,18 +44,16 @@ bootloader: $(SRC)/stage1.asm $(SRC)/stage2.asm @cat $(BUILD)/stage1.bin $(BUILD)/stage2.bin > $(BUILD)/boot.bin # Kernel: This target is responsible for creating kernel.bin -kernel: $(SRC)/kernel.c $(SRC)/entry.asm - @echo "Assembling $(SRC)/entry.asm..." - @$(AS) -felf32 -o $(BUILD)/entry.o $(SRC)/entry.asm +kernel: $(SRC)/kernel.c @echo "Compiling $(SRC)/kernel.c..." @$(CC) $(CFLAGS) -Iinclude -o $(BUILD)/kernel.o -c $(SRC)/kernel.c - @$(LD) -T$(SRC)/kernel.ld --oformat binary -o $(BUILD)/kernel.bin $(BUILD)/*.o + @$(LD) -T$(SRC)/kernel.ld -z notext --oformat binary -o $(BUILD)/kernel.bin $(BUILD)/kernel.o @rm $(BUILD)/*.o $(C_FILES): @echo "Compiling $(SRC)/$@.c..." @$(CC) $(CFLAGS) -o $(BUILD)/$@.o -c $(SRC)/$@.c - @$(LD) -T$(SRC)/$@.ld --oformat binary -o $(BUILD)/$@.bin $(BUILD)/$@.o + @$(LD) -T$(SRC)/$@.ld -z notext --oformat binary -o $(BUILD)/$@.bin $(BUILD)/$@.o @rm $(BUILD)/$@.o $(FONTS): diff --git a/include/disk/disk.h b/include/disk/disk.h index 23c0c54..59780fa 100644 --- a/include/disk/disk.h +++ b/include/disk/disk.h @@ -9,8 +9,6 @@ #define WRITE_COMMAND 0x30 #define CACHE_FLUSH 0xE7 -extern uint8_t driveNum; - void diskRead(uint32_t lbaAddress, uint8_t sectorCount, char* dst) { outb(PIO_PORT + 6, ((lbaAddress >> 24) & 0x0f) | 0xe0); // 6 - Use LBA; 4 - Drive number; 3-0 - Highest lba address nibble outb(PIO_PORT + 2, sectorCount); // Sector count register diff --git a/include/ports/io.h b/include/ports/io.h index eb07383..3479560 100644 --- a/include/ports/io.h +++ b/include/ports/io.h @@ -6,27 +6,27 @@ // Port functions uint8_t inb(uint16_t port) { uint8_t result; - asm volatile("inb %1, %0" : "=a"(result) : "Nd"(port)); + __asm__ volatile("inb %1, %0" : "=a"(result) : "Nd"(port)); return result; } void outb(uint16_t port, uint8_t data) { - asm volatile("outb %0, %1" :: "a"(data), "Nd"(port)); + __asm__ volatile("outb %0, %1" :: "a"(data), "Nd"(port)); } uint16_t inw(uint16_t port) { uint16_t result; - asm volatile("inw %1, %0" : "=a"(result) : "Nd"(port)); + __asm__ volatile("inw %1, %0" : "=a"(result) : "Nd"(port)); return result; } void outw(uint16_t port, uint16_t data) { - asm volatile("outw %0, %1" :: "a"(data), "Nd"(port)); + __asm__ volatile("outw %0, %1" :: "a"(data), "Nd"(port)); } void ioWait() { // Waits ~1 I/O Cycle (Linux uses this so it should be good) - asm volatile("outb %%al, $0x80" : : "a"(0)); + __asm__ volatile("outb %%al, $0x80" : : "a"(0)); } #endif diff --git a/src/calc.c b/src/calc.c index a0137f5..742ff21 100644 --- a/src/calc.c +++ b/src/calc.c @@ -1,8 +1,9 @@ #include +#include +__attribute__ ((section("entry"))) void main() { - char msg[8] = { 'H','e','l','l','o','!','\n',0 }; - print(msg); + print("Hello, World!\n"); return; } diff --git a/src/calc.ld b/src/calc.ld index 70eec33..afa48f2 100644 --- a/src/calc.ld +++ b/src/calc.ld @@ -1,6 +1,6 @@ -ENTRY("main"); SECTIONS { .text : { + *(entry); *(.text*); } .data : { diff --git a/src/entry.asm b/src/entry.asm deleted file mode 100644 index f9b973b..0000000 --- a/src/entry.asm +++ /dev/null @@ -1,22 +0,0 @@ -[bits 32] -[global _start] -[global driveNum] - -[extern main] - -;; Calls the main function in 'main.c' -_start: - pop ax - mov [driveNum], al - - call main - - ; Exit QEMU - mov ax, 0x2000 - mov dx, 0x0604 - out dx, ax - - jmp $ ;; If we used cli, hlt we wouldn't have interrupts :/ - -driveNum: db 0 - diff --git a/src/kernel.c b/src/kernel.c index 2b31f03..e08a289 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -17,6 +17,7 @@ #include #include #include +#include #define PROMPT "#> " @@ -26,6 +27,7 @@ char* filetable = (char*)FILETABLE_LOC; // Pretty sure we don't have to care abo void printFiletable(char* ft); void printPhysicalMemmap(); +__attribute__ ((section("entry"))) void main() { // Interrupt setup setupIdt(); @@ -248,7 +250,7 @@ void main() { diskRead(0, 1, (char*)buf); if (buf[510] == 0x55 && buf[511] == 0xaa) { print("[ DONE ]\n"); - } else { + } else { print("[ FAIL ]\n"); } print("Write Test ......... "); @@ -260,6 +262,10 @@ void main() { } else { print("[ FAIL ]\n"); } + print("Malloc Test ........ "); + void* buffer = malloc(100); + free(buffer); + print("[ DONE ]\n"); print("\nTests Finished!\n"); } else { diff --git a/src/kernel.ld b/src/kernel.ld index b3174ba..d96c31e 100644 --- a/src/kernel.ld +++ b/src/kernel.ld @@ -1,5 +1,6 @@ SECTIONS { .text 0x50000 : { + *(entry); *(.text*); } .data : { diff --git a/src/stage2.asm b/src/stage2.asm index d237ab1..f8c3dd0 100644 --- a/src/stage2.asm +++ b/src/stage2.asm @@ -324,9 +324,6 @@ protected_start: rep movsd ; Perform The far jump - mov al, [driveNum] - push ax - jmp CODE_SEG:KERNEL_LOC ;; Data