From 78367d56d6b847fe96e3cda1c67d5e84b0f0232e Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Tue, 9 Apr 2024 20:18:03 +0200 Subject: [PATCH] FINALLY Added Disk support!!! f_disk.c name explanation in README.md Disk reading only, but it works! --- README.md | 10 ++++------ docs/main.md | 5 +++-- fs/test.txt | 1 + src/kernel/disk.c | 1 - src/kernel/disk.h | 7 +++++-- src/kernel/entry.asm | 2 +- src/kernel/f_disk.c | 27 +++++++++++++++++++++++++++ src/kernel/main.c | 12 +++++++++++- src/kernel/system.c | 14 ++++++++++++-- src/kernel/system.h | 3 +++ 10 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 fs/test.txt delete mode 100644 src/kernel/disk.c create mode 100644 src/kernel/f_disk.c diff --git a/README.md b/README.md index 410f75d..0169987 100644 --- a/README.md +++ b/README.md @@ -29,19 +29,17 @@ If you don't know how to do something, try the 'help' command! ## Some images +Interactive Shell: \ +![Example Shell Output](other/shell.png) + Text editor **concept**: \ ![Vim like editor](other/editor.png) -Available character set: \ -![IBM VGA 8x16 Character set](other/charset.png) - -Available colors: \ -![16 VGA Colors](other/colors.png) - _These images may be outdated, Idk_ ## Known issues - If you run the kernel.bin, loading files sorta breaks, because the program is running at 0x8000, but it thinks that it's at 0x2000 (i think) - If you run anything in the code editor and return to the kernel, you won't be able to load any files +- For some unknown reason, i cannot name the f\_disk.c file just disk.c. It breaks the OS. No idea why. In fact, anything starting with a character that is before f doesn't work. Help me ## [Old Version](https://github.com/Tom-on64/old-kern) diff --git a/docs/main.md b/docs/main.md index 168bcfd..775fb5d 100644 --- a/docs/main.md +++ b/docs/main.md @@ -7,15 +7,16 @@ Well, 32-bit means that it can access $2^{32}$ different memory addresses. x86 is the processor architecture it is built upon. ## Command Line -Kern uses a command line (for now), so you'll have to type commands to do stuff. +Kern uses a shell (for now), so you'll have to type commands to do stuff. It is currently quite simple. It supports these commands: - clear: Clears the screen - echo : Prints msg to the screen -- exit: Shuts down the OS ***(QEMU only!)*** +- exit: Exits the shell - help: Prints all available commands - ls: Prints all files - reboot: Reboots the system +- test: Tests the newest feature _Debug_ You can also print/run files by typing their filename (More in **Filesystem**) diff --git a/fs/test.txt b/fs/test.txt new file mode 100644 index 0000000..8ab686e --- /dev/null +++ b/fs/test.txt @@ -0,0 +1 @@ +Hello, World! diff --git a/src/kernel/disk.c b/src/kernel/disk.c deleted file mode 100644 index f7ba18f..0000000 --- a/src/kernel/disk.c +++ /dev/null @@ -1 +0,0 @@ -#include "disk.h" diff --git a/src/kernel/disk.h b/src/kernel/disk.h index b23ec64..e64460b 100644 --- a/src/kernel/disk.h +++ b/src/kernel/disk.h @@ -1,8 +1,11 @@ -#ifndef DISK_H -#define DISK_H +#ifndef DRIVE_H +#define DRIVE_H #include "stdint.h" extern uint8_t driveNum; +void diskRead(uint8_t sector, uint8_t sectorCount, uint8_t drive, char* dst); +void diskWrite(uint8_t sector, uint8_t sectorCount, uint8_t drive, const char* src); + #endif diff --git a/src/kernel/entry.asm b/src/kernel/entry.asm index a68e800..15c290a 100644 --- a/src/kernel/entry.asm +++ b/src/kernel/entry.asm @@ -1,5 +1,6 @@ [bits 32] [global _start] +[global driveNum] [extern main] [extern idtp] @@ -19,7 +20,6 @@ _start: jmp $ ;; If we used cli, hlt we wouldn't have interrupts :/ -[global driveNum] driveNum: db 0 ;; diff --git a/src/kernel/f_disk.c b/src/kernel/f_disk.c new file mode 100644 index 0000000..8f0b73b --- /dev/null +++ b/src/kernel/f_disk.c @@ -0,0 +1,27 @@ +#include "disk.h" +#include "stdint.h" +#include "system.h" + +#define PIO_PORT 0x1f0 + +void diskRead(uint8_t sector, uint8_t sectorCount, uint8_t drive, char* dst) { + outb(PIO_PORT + 6, (drive & 0x0f) | 0xa0); // Head and drive number port + outb(PIO_PORT + 2, sectorCount); // Sector count + outb(PIO_PORT + 3, sector); // Sector to start reading at + outb(PIO_PORT + 4, 0); // Cylinder number low + outb(PIO_PORT + 5, 0); // Cylinder number high + outb(PIO_PORT + 7, 0x20); // Command register, 0x20 - Read with retry + + while (inb(PIO_PORT + 7) == 8); // Status register, wait until ready + + uint32_t i = 512 * sectorCount; + while (i > 0) { + uint16_t data = inw(PIO_PORT); // Data port + *dst++ = (uint8_t)(data); + *dst++ = (uint8_t)(data >> 8); + i--; + } +} + +void diskWrite(uint8_t sector, uint8_t sectorCount, uint8_t drive, const char* src); + diff --git a/src/kernel/main.c b/src/kernel/main.c index cfb6614..5669f2d 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -52,10 +52,20 @@ void main() { print("Available Commands:\n", 0x0f); print(" clear | Clears the screen\n", 0x0f); print(" echo | Prints a message to stdout\n", 0x0f); - print(" exit | Exits program\n", 0x0f); + print(" exit | Exits shell\n", 0x0f); print(" help | Prints this message\n", 0x0f); // TODO: print(" ls | Lists all available files\n", 0x0f); // TODO: print(" reboot | Reboots the system\n", 0x0f); + print(" test | Tests the latest feature [DEBUG]\n", 0x0f); + } else if (strcmp(input, "test") == 0) { + char buf[512]; + print("Reading from drive 0x", 0x0f); + print(itoa(driveNum, 16), 0x0f); + print("...\n", 0x0f); + diskRead(22, 1, driveNum, buf); + + print(buf, 0x0f); + putc('\n', 0x0f); } else { print("Command not found: ", 0x0c); print(input, 0x0c); diff --git a/src/kernel/system.c b/src/kernel/system.c index 46d3e2a..e926f48 100644 --- a/src/kernel/system.c +++ b/src/kernel/system.c @@ -3,12 +3,22 @@ uint8_t inb(uint16_t port) { uint8_t result; - asm volatile("in %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("out %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)); + return result; +} + +void outw(uint16_t port, uint16_t data) { + asm volatile("outw %0, %1" :: "a"(data), "Nd"(port)); } char* memcopy(char* src, char* dst, uint16_t length) { diff --git a/src/kernel/system.h b/src/kernel/system.h index 4fddf9c..78477d1 100644 --- a/src/kernel/system.h +++ b/src/kernel/system.h @@ -7,6 +7,9 @@ uint8_t inb(uint16_t port); void outb(uint16_t port, uint8_t data); +uint16_t inw(uint16_t port); +void outw(uint16_t port, uint16_t data); + // Memory functions char* memcopy(char* src, char* dst, uint16_t length); char* memset(char* dst, char data, uint16_t length);