From 0dc869244335912fecc8e9ad62815e3398297b09 Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Tue, 15 Mar 2016 21:38:25 +0100 Subject: [PATCH] Added a very small ATA detector. More to come --- .travis.yml | 6 ++- buildcrosscompiler.sh | 2 +- kernel/hal/x86/Makefile | 4 +- kernel/hal/x86/blk/BlockDevice.cpp | 69 +++++++++++++++++++++++++++++ kernel/hal/x86/c_include/io.h | 10 ----- kernel/hal/x86/c_include/string.h | 5 ++- kernel/hal/x86/include/base.hpp | 2 + kernel/hal/x86/include/blockdev.hpp | 21 +++++++++ kernel/hal/x86/init/init.cpp | 3 +- kernel/hal/x86/string.c | 9 ++++ kernel/kernel/Makefile | 6 +-- 11 files changed, 116 insertions(+), 21 deletions(-) create mode 100644 kernel/hal/x86/blk/BlockDevice.cpp create mode 100644 kernel/hal/x86/include/blockdev.hpp diff --git a/.travis.yml b/.travis.yml index e3e6c94..bf32013 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,5 +4,7 @@ install: - wget --secure-protocol=TLSv1 dark32.cf/crosscomp-x64.tar.xz - tar -xf crosscomp-x64.tar.xz - popd -script: make -after_success: curl --ftp-create-dirs -T mtgos -u $USER:$PASS ftp://dark32.cf/ +script: + - make + - mv mtgos mtgos-i686 +after_success: curl --ftp-create-dirs -T mtgos-i686 -u $USER:$PASS ftp://dark32.cf/ diff --git a/buildcrosscompiler.sh b/buildcrosscompiler.sh index e897b2d..1ec85a2 100644 --- a/buildcrosscompiler.sh +++ b/buildcrosscompiler.sh @@ -1,7 +1,7 @@ -set -o errexit CROSSPATH=$HOME/opt # You can change it to whatever you want export PATH=$CROSSPATH/bin:$PATH mkdir $CROSSPATH +set -o errexit function buildscript() { echo "[$(date +%c)] Building binutils for $1." | tee -a buildlog mkdir build-binutils diff --git a/kernel/hal/x86/Makefile b/kernel/hal/x86/Makefile index 04e9200..dab8c17 100644 --- a/kernel/hal/x86/Makefile +++ b/kernel/hal/x86/Makefile @@ -5,8 +5,8 @@ OBJS = $(addsuffix .o,$(basename $(SRCS))) CPP = $(PREFIX)g++ CC = $(PREFIX)gcc ASFLAGS = -m32 -CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -march=native -std=c11 -fno-builtin -Werror -nostdlib -g -fpie -CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fpie +CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie +CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -fno-rtti -fno-exceptions -ffreestanding -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fno-pie LDFLAGS = -r -melf_i386 diff --git a/kernel/hal/x86/blk/BlockDevice.cpp b/kernel/hal/x86/blk/BlockDevice.cpp new file mode 100644 index 0000000..0faa353 --- /dev/null +++ b/kernel/hal/x86/blk/BlockDevice.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#define ATAPIO_ERR 0x01 +#define ATAPIO_DRQ 0x08 +#define ATAPIO_SRV 0x10 +#define ATAPIO_DF 0x20 +#define ATAPIO_RDY 0x40 +#define ATAPIO_BSY 0x80 +#define DATA 0 +#define ATAPI_RESV 1 +#define SECTOR_CNT 2 +#define LBAlo 3 +#define LBAmid 4 +#define LBAhi 5 +#define DRV 6 +#define CMD 7 +namespace MTGosHAL { + uint16_t ataports[4]={0x1F0,0x170,0x1E8,0x168}; + uint16_t commports[4]={0x3F6,0x376,0x3E6,0x366}; + BlockDevice::BlockDevice(): numDevices(0), existent(0) { + for(int i=0;i<4;i++) { + if(inb(ataports[i]+CMD)!=0xFF) { + outb(ataports[i]+DRV, 0xE0); + outb(ataports[i]+LBAlo,0x55); + if(inb(ataports[i]+LBAlo)==0x55) { + existent|=1<<(i<<1); + numDevices++; + } + outb(ataports[i]+DRV, 0xF0); + outb(ataports[i]+LBAlo,0x55); + if(inb(ataports[i]+LBAlo)==0x55) { + existent|=1<<((i<<1)+1); + numDevices++; + } + } + } + if(numDevices==0) { + err<<"Not a single device was found!\n"; + } + for(int i=0;i<8;i++) { + if(!(existent&(1< uint8_t {return numDevices;} + auto BlockDevice::getDriveNumByName(const char * name) -> uint8_t { + if(strlen(name)!=5) + return -1; //Format is ATA[0-3][sl] (regex) + if((name[0]!=name[2])||(name[2]!='A')) + return -1; + if(name[1]!='T') + return -1; + uint8_t drivenum=name[3]-0x30; + if(drivenum>3) + return -1; + if((name[4]!='s')&&(name[4]!='l')) + return -1; + drivenum<<=1; + drivenum+=(name[4]=='s')?1:0; + if(!(existent&(1< -static inline void outb(uint16_t port, uint8_t val) __attribute__((always_inline)); static inline void outb(uint16_t port, uint8_t val) { asm volatile("outb %0, %1" : : "a"(val), "Nd"(port)); } -static inline void outw(uint16_t port, uint16_t val) __attribute__((always_inline)); static inline void outw(uint16_t port, uint16_t val) { asm volatile("outw %0, %1" : : "a"(val), "Nd"(port)); } -static inline void outl(uint16_t port, uint32_t val) __attribute__((always_inline)); static inline void outl(uint16_t port, uint32_t val) { asm volatile("outl %0, %1" : : "a"(val), "Nd"(port)); } -static inline uint8_t inb(uint16_t port) __attribute__((always_inline)); static inline uint8_t inb(uint16_t port) { uint8_t ret; asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port)); return ret; } -static inline uint16_t inw(uint16_t port) __attribute__((always_inline)); static inline uint16_t inw(uint16_t port) { uint16_t ret; asm volatile("inw %1, %0" : "=a"(ret) : "Nd"(port)); return ret; } -static inline uint32_t inl(uint16_t port) __attribute__((always_inline)); static inline uint32_t inl(uint16_t port) { uint32_t ret; asm volatile("inl %1, %0" : "=a"(ret) : "Nd"(port)); return ret; } -static inline void io_wait() __attribute__((always_inline)); static inline void io_wait() { asm volatile("outb %%al, $0x80" : : "a"(0)); } -static inline void cli() __attribute__((always_inline)); static inline void cli() { asm volatile("cli"); } -static inline void sti() __attribute__((always_inline)); static inline void sti() { asm volatile("sti"); } -static inline uint64_t rdtsc() __attribute__((always_inline)); static inline uint64_t rdtsc() { uint64_t ret; diff --git a/kernel/hal/x86/c_include/string.h b/kernel/hal/x86/c_include/string.h index f31a22e..0cd8e3c 100644 --- a/kernel/hal/x86/c_include/string.h +++ b/kernel/hal/x86/c_include/string.h @@ -1,11 +1,12 @@ #ifndef _STRING_H #define _STRING_H -#ifdef _cplusplus +#ifdef __cplusplus extern "C" { #endif #include void memmove(void* dst, void* src, uint32_t size); -#ifdef _cplusplus +uint32_t strlen(const char* str); +#ifdef __cplusplus } #endif #endif diff --git a/kernel/hal/x86/include/base.hpp b/kernel/hal/x86/include/base.hpp index 06cf933..e29e5d8 100644 --- a/kernel/hal/x86/include/base.hpp +++ b/kernel/hal/x86/include/base.hpp @@ -10,6 +10,7 @@ namespace MTGosHAL { class GDT; class IDT; class Multitasking; + class BlockDevice; enum class BG_color: uint16_t; enum class FG_color: uint16_t; extern Serial debug; @@ -19,5 +20,6 @@ namespace MTGosHAL { extern GDT gdt; extern IDT idt; extern Multitasking tasks; + extern BlockDevice disk; } #endif diff --git a/kernel/hal/x86/include/blockdev.hpp b/kernel/hal/x86/include/blockdev.hpp new file mode 100644 index 0000000..635e0e1 --- /dev/null +++ b/kernel/hal/x86/include/blockdev.hpp @@ -0,0 +1,21 @@ +#ifndef __HAL_BLOCKDEV_HPP +#define __HAL_BLOCKDEV_HPP +#include +namespace MTGosHAL { + class BlockDevice { + private: + uint8_t numDevices; + uint8_t existent; //Bitmap showing which of the up to 8 drives are actually existent. + public: + BlockDevice(); + ~BlockDevice(); + auto getDriveCnt() -> uint8_t; + auto getDriveNumByName(const char *) -> uint8_t; //Returns -1 if device is not existent + auto readSector(uint32_t drv, uint64_t sectorNum, uint8_t *buf); //Has to be at least 512 bytes big! + auto readSectors(uint32_t drv, uint64_t sectorNum, uint32_t num, uint8_t *buf); //Has to be at least num*512 bytes big! + + }; +} + + +#endif /* end of include guard: __HAL_BLOCKDEV_HPP */ diff --git a/kernel/hal/x86/init/init.cpp b/kernel/hal/x86/init/init.cpp index 99141f2..b8e20e3 100644 --- a/kernel/hal/x86/init/init.cpp +++ b/kernel/hal/x86/init/init.cpp @@ -7,6 +7,7 @@ #include #include #include +#include extern "C" void intr_stub_0(void); void main(); namespace MTGosHAL { @@ -17,6 +18,7 @@ namespace MTGosHAL { IDT idt; GDT gdt; Multitasking tasks; + BlockDevice disk; void main(int eax, struct multiboot_info* ebx) { out << BG_color::BLACK << FG_color::WHITE << "Loading MTGos...\n"; err << BG_color::BLACK << FG_color::RED; @@ -59,4 +61,3 @@ extern "C" void init(int eax, struct multiboot_info* ebx) { extern "C" void __cxa_pure_virtual() { MTGosHAL::debug << "A pure virtual function just got called.\n"; } - diff --git a/kernel/hal/x86/string.c b/kernel/hal/x86/string.c index 82ca146..d524b40 100644 --- a/kernel/hal/x86/string.c +++ b/kernel/hal/x86/string.c @@ -13,4 +13,13 @@ void memmove(void* dst, void* src, uint32_t size) { to[i]=from[i]; //This would get optimized by gcc to memmove(dst, src, size); if optimizations are enabled. } } +uint32_t strlen(const char* str) { + uint32_t i=0; + char* str2=(char*)((int)str); + while(*str2) { + i++; + str2++; + } + return i; +} #pragma GCC pop_options diff --git a/kernel/kernel/Makefile b/kernel/kernel/Makefile index a671a1d..f89888d 100644 --- a/kernel/kernel/Makefile +++ b/kernel/kernel/Makefile @@ -4,8 +4,8 @@ OBJS = $(addsuffix .o,$(basename $(SRCS))) CPP = $(PREFIX)g++ CC = $(PREFIX)gcc ASFLAGS = -m32 -CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -I../hal/dummy/c_include -ffreestanding -march=native -std=c11 -fno-builtin -Werror -nostdlib -g -fpie -CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -I../hal/dummy/include -I../hal/dummy/c_include -fno-rtti -fno-exceptions -ffreestanding -march=native -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fpie +CFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -Ic_include/ -I../hal/dummy/c_include -ffreestanding -std=c11 -fno-builtin -Werror -nostdlib -g -fpie +CPPFLAGS = -m32 -Wall -fno-stack-protector -nostdinc -std=c++14 -Iinclude/ -Ic_include/ -I../hal/dummy/include -I../hal/dummy/c_include -fno-rtti -fno-exceptions -ffreestanding -fno-builtin -Werror -nostdlib -fno-use-cxa-atexit -Wextra -Wno-unused -g -fpie all: $(OBJS) %.o: %.cpp @@ -20,4 +20,4 @@ all: $(OBJS) clean: rm -rf $(OBJS) -.PHONY: clean all \ No newline at end of file +.PHONY: clean all