summaryrefslogtreecommitdiff
path: root/Source
diff options
context:
space:
mode:
authorAlexis211 <alexis211@gmail.com>2009-11-29 16:22:01 +0100
committerAlexis211 <alexis211@gmail.com>2009-11-29 16:22:01 +0100
commit41874c455c755f00ca73c2e2ee5ab8c4b0dbe61e (patch)
tree95986807bde312286c28297059873a6c66b72566 /Source
parenta913d4c2cb4daf10c0eac4d548fccb26b2a9f099 (diff)
downloadMelon-41874c455c755f00ca73c2e2ee5ab8c4b0dbe61e.tar.gz
Melon-41874c455c755f00ca73c2e2ee5ab8c4b0dbe61e.zip
Reading from HDD is now possible !
Diffstat (limited to 'Source')
-rw-r--r--Source/Kernel/Core/Sys.ns.cpp4
-rw-r--r--Source/Kernel/Core/Sys.ns.h1
-rw-r--r--Source/Kernel/Core/kmain.wtf.cpp2
-rw-r--r--Source/Kernel/Devices/ATA/ATAController.class.cpp53
-rw-r--r--Source/Kernel/Devices/ATA/ATAController.class.h47
-rw-r--r--Source/Kernel/Devices/ATA/ATADrive.class.cpp71
-rw-r--r--Source/Kernel/Devices/ATA/ATADrive.class.h30
-rw-r--r--Source/Kernel/Makefile2
-rw-r--r--Source/Kernel/VFS/Part.ns.cpp25
-rw-r--r--Source/Kernel/VFS/Part.ns.h13
-rw-r--r--Source/Kernel/VFS/Partition.class.cpp4
11 files changed, 244 insertions, 8 deletions
diff --git a/Source/Kernel/Core/Sys.ns.cpp b/Source/Kernel/Core/Sys.ns.cpp
index 49b2725..c5af3e8 100644
--- a/Source/Kernel/Core/Sys.ns.cpp
+++ b/Source/Kernel/Core/Sys.ns.cpp
@@ -18,6 +18,10 @@ void outb (u16int port, u8int value) {
asm volatile ("outb %1, %0" : : "dN" (port), "a" (value));
}
+void outw (u16int port, u16int value) {
+ asm volatile ("outw %1, %0" : : "dN" (port), "a" (value));
+}
+
u8int inb (u16int port) {
u8int ret;
asm volatile ("inb %1, %0" : "=a" (ret) : "dN" (port));
diff --git a/Source/Kernel/Core/Sys.ns.h b/Source/Kernel/Core/Sys.ns.h
index 9b34dbf..26f04e6 100644
--- a/Source/Kernel/Core/Sys.ns.h
+++ b/Source/Kernel/Core/Sys.ns.h
@@ -26,6 +26,7 @@ extern SimpleVT* kvt;
namespace Sys {
void outb(u16int port, u8int value);
+ void outw(u16int port, u8int value);
u8int inb(u16int port);
u16int inw(u16int port);
void dumpRegs(registers_t *regs, VirtualTerminal& vt);
diff --git a/Source/Kernel/Core/kmain.wtf.cpp b/Source/Kernel/Core/kmain.wtf.cpp
index a028457..3eee14b 100644
--- a/Source/Kernel/Core/kmain.wtf.cpp
+++ b/Source/Kernel/Core/kmain.wtf.cpp
@@ -7,6 +7,7 @@
#include <Devices/Display/VESADisplay.class.h>
#include <Devices/Keyboard/PS2Keyboard.class.h>
#include <Devices/Floppy/FloppyDrive.class.h>
+#include <Devices/ATA/ATAController.class.h>
#include <Devices/Timer.class.h>
#include <DeviceManager/Disp.ns.h>
#include <DeviceManager/Dev.ns.h>
@@ -186,6 +187,7 @@ void kmain(multiboot_info_t* mbd, u32int magic) {
Kbd::setFocus(kvt); //Set focus to virtual terminal
if (enableVESA) Dev::registerDevice(new VESADisplay());
FloppyController::detect();
+ ATAController::detect();
//*************************************** MOUNT FILESYSTEMS
diff --git a/Source/Kernel/Devices/ATA/ATAController.class.cpp b/Source/Kernel/Devices/ATA/ATAController.class.cpp
new file mode 100644
index 0000000..69a18a4
--- /dev/null
+++ b/Source/Kernel/Devices/ATA/ATAController.class.cpp
@@ -0,0 +1,53 @@
+#include "ATADrive.class.h"
+
+#include <DeviceManager/Dev.ns.h>
+#include <VFS/Part.ns.h>
+
+String ATAController::getClass() {
+ return "controller.ata";
+}
+
+String ATAController::getName() {
+ return String("ATA controller #") + String::number(m_number);
+}
+
+void ATAController::detect() {
+ ATAController *c[2];
+ c[0] = new ATAController(ATA_BUS1_BASE, 0);
+ c[1] = new ATAController(ATA_BUS2_BASE, 1);
+ Dev::registerDevice(c[0]); Dev::registerDevice(c[1]);
+ for (u32int d = 0; d < 2; d++) {
+ for (u32int r = 0; r < 2; r++) {
+ ATADrive* drv = c[d]->m_drives[r];
+ if (drv != 0) {
+ Dev::registerDevice(drv);
+ Part::registerDevice(drv);
+ }
+ }
+ }
+}
+
+ATAController::ATAController(u32int base, u8int number) {
+ m_base = base; m_number = number;
+ m_drives[0] = NULL; m_drives[1] = NULL;
+ identify(false); //Identify master device
+ identify(true); //Identify slave device
+}
+
+void ATAController::identify(bool slave) {
+ if (m_drives[(slave ? 1 : 0)] != NULL) return;
+ writeByte(ATA_PORT_DRIVE_SELECT, (slave ? 0xB0 : 0xA0));
+ writeByte(ATA_PORT_COMMAND, ATA_CMD_IDENTIFY);
+ u8int ret = readByte(ATA_PORT_COMMAND);
+ if (ret == 0) return; //Drive does not exist
+ while ((ret & 0x88) != 0x08 and (ret & 1) != 1) {
+ ret = readByte(ATA_PORT_COMMAND);
+ }
+ if ((ret & 1) == 1) return; //Error while IDENTIFY
+ u16int data[256];
+ for (u32int i = 0; i < 256; i++) data[i] = readWord(ATA_PORT_DATA);
+ u32int blocks = (data[61] << 16) | data[60];
+ if (blocks != 0) { //Drive supports LBA28
+ m_drives[(slave ? 1 : 0)] = new ATADrive(this, slave, blocks, data);
+ }
+}
diff --git a/Source/Kernel/Devices/ATA/ATAController.class.h b/Source/Kernel/Devices/ATA/ATAController.class.h
new file mode 100644
index 0000000..a12b835
--- /dev/null
+++ b/Source/Kernel/Devices/ATA/ATAController.class.h
@@ -0,0 +1,47 @@
+#ifndef DEF_ATACONTROLLER_CLASS_H
+#define DEF_ATACONTROLLER_CLASS_H
+
+#include <Devices/Device.proto.h>
+
+#define ATA_BUS1_BASE 0x1F0
+#define ATA_BUS2_BASE 0x170
+
+#define ATA_PORT_DATA 0
+#define ATA_PORT_FEATURES_ERROR 1
+#define ATA_PORT_SECT_COUNT 2
+#define ATA_PORT_PARTIAL1 3
+#define ATA_PORT_PARTIAL2 4
+#define ATA_PORT_PARTIAL3 5
+#define ATA_PORT_DRIVE_SELECT 6
+#define ATA_PORT_COMMAND 7
+
+#define ATA_CMD_IDENTIFY 0xEC
+#define ATA_CMD_READ 0x20
+#define ATA_CMD_WRITE 0x30
+
+class ATADrive;
+
+class ATAController : public Device {
+ friend class ATADrive;
+ private:
+ ATAController(u32int base, u8int number);
+
+ u32int m_base;
+ u8int m_number;
+
+ ATADrive* m_drives[2];
+
+ void writeWord(u16int port, u16int word) { Sys::outw(m_base + port, word); }
+ void writeByte(u16int port, u8int byte) { Sys::outb(m_base + port, byte); }
+ u8int readByte(u16int port) { return Sys::inb(m_base + port); }
+ u16int readWord(u16int port) { return Sys::inw(m_base + port); }
+
+ void identify(bool slave); //Identifies a drive and adds it to m_drives
+
+ public:
+ static void detect();
+ String getClass();
+ String getName();
+};
+
+#endif
diff --git a/Source/Kernel/Devices/ATA/ATADrive.class.cpp b/Source/Kernel/Devices/ATA/ATADrive.class.cpp
new file mode 100644
index 0000000..d9cc4a7
--- /dev/null
+++ b/Source/Kernel/Devices/ATA/ATADrive.class.cpp
@@ -0,0 +1,71 @@
+#include "ATADrive.class.h"
+
+String ATADrive::getClass() {
+ return "block.ata";
+}
+
+String ATADrive::getName() {
+ return String("ATA drive ") += String::number(m_ctrlr->m_number) += String(m_isSlave ? ".slave " : ".master ")
+ += String::number(m_blockCount / (1024 * 1024 / blockSize())) += "MB";
+}
+
+ATADrive::ATADrive(ATAController* ctrlr, bool isSlave, u32int blockCount, u16int* identifyData) {
+ m_ctrlr = ctrlr;
+ m_isSlave = isSlave;
+ m_blockCount = blockCount;
+ for (u32int i = 0; i < 256; i++) m_identifyData[i] = identifyData[i];
+}
+
+bool ATADrive::readOnly() {
+ return true;
+}
+
+u64int ATADrive::blocks() {
+ return m_blockCount;
+}
+
+void ATADrive::cmdCommon(u32int numblock, u32int count) {
+ m_ctrlr->writeByte(ATA_PORT_FEATURES_ERROR, 0);
+ m_ctrlr->writeByte(ATA_PORT_SECT_COUNT, count);
+ m_ctrlr->writeByte(ATA_PORT_PARTIAL1, numblock);
+ m_ctrlr->writeByte(ATA_PORT_PARTIAL2, numblock >> 8);
+ m_ctrlr->writeByte(ATA_PORT_PARTIAL3, numblock >> 16);
+
+ m_ctrlr->writeByte(ATA_PORT_DRIVE_SELECT, 0xE0 | (m_isSlave ? 0x10 : 0) | ((numblock >> 24) & 0x0F));
+}
+
+bool ATADrive::readBlocks(u64int start, u32int count, u8int* data) {
+ if (start + count >= m_blockCount) return false;
+
+ cmdCommon(start, count);
+ m_ctrlr->writeByte(ATA_PORT_COMMAND, ATA_CMD_READ);
+
+ while (!(m_ctrlr->readByte(ATA_PORT_COMMAND) & 0x08)); //Wait for the drive to be ready
+
+ for (u32int idx = 0; idx < count * 256; idx++) {
+ u16int tmpword = m_ctrlr->readWord(ATA_PORT_DATA);
+ data[idx * 2] = (u8int)tmpword;
+ data[idx * 2 + 1] = (u8int)(tmpword >> 8);
+ }
+ return true;
+}
+
+bool ATADrive::writeBlocks(u64int start, u32int count, u8int* data) {
+ if (start + count >= m_blockCount) return false;
+ if (readOnly()) return false;
+
+ cmdCommon(start, count);
+ m_ctrlr->writeByte(ATA_PORT_COMMAND, ATA_CMD_WRITE);
+
+ while (!(m_ctrlr->readByte(ATA_PORT_COMMAND) & 0x08)); //Wait for the drive to be ready
+
+ for (u32int idx = 0; idx < count * 256; idx++) {
+ u16int tmpword = (data[idx * 2]) | (data[idx * 2 + 1] << 8);
+ m_ctrlr->writeByte(ATA_PORT_DATA, tmpword);
+ }
+ return true;
+}
+
+u32int ATADrive::blockSize() {
+ return 512;
+}
diff --git a/Source/Kernel/Devices/ATA/ATADrive.class.h b/Source/Kernel/Devices/ATA/ATADrive.class.h
new file mode 100644
index 0000000..b6087bf
--- /dev/null
+++ b/Source/Kernel/Devices/ATA/ATADrive.class.h
@@ -0,0 +1,30 @@
+#ifndef DEF_ATADRIVE_CLASS_H
+#define DEF_ATADRIVE_CLASS_H
+
+#include "ATAController.class.h"
+#include <Devices/BlockDevice.proto.h>
+
+class ATADrive : public BlockDevice {
+ friend class ATAController;
+ private:
+ ATADrive(ATAController* ctrlr, bool isSlave, u32int blockCount, u16int* identifyData);
+
+ ATAController* m_ctrlr;
+ bool m_isSlave;
+ u32int m_blockCount;
+ u16int m_identifyData[256];
+
+ void cmdCommon(u32int numblock, u32int count);
+
+ public:
+ String getClass();
+ String getName();
+
+ bool readOnly();
+ u64int blocks();
+ bool readBlocks(u64int start, u32int count, u8int* data);
+ bool writeBlocks(u64int start, u32int count, u8int* data);
+ u32int blockSize();
+};
+
+#endif
diff --git a/Source/Kernel/Makefile b/Source/Kernel/Makefile
index 2005cd2..660017a 100644
--- a/Source/Kernel/Makefile
+++ b/Source/Kernel/Makefile
@@ -77,6 +77,8 @@ Objects = Core/loader.wtf.o \
Devices/Keyboard/PS2Keyboard.class.o \
Devices/Floppy/FloppyController.class.o \
Devices/Floppy/FloppyDrive.class.o \
+ Devices/ATA/ATAController.class.o \
+ Devices/ATA/ATADrive.class.o \
Devices/Timer.class.o
all: $(OutFile)
diff --git a/Source/Kernel/VFS/Part.ns.cpp b/Source/Kernel/VFS/Part.ns.cpp
index 9ead6b5..415a150 100644
--- a/Source/Kernel/VFS/Part.ns.cpp
+++ b/Source/Kernel/VFS/Part.ns.cpp
@@ -1,5 +1,7 @@
#include "Part.ns.h"
+#include <VTManager/SimpleVT.class.h>
+
namespace Part {
Vector<BlockDevice*> devices;
@@ -7,6 +9,21 @@ Vector<Partition*> partitions;
void readPartitionTable(BlockDevice *dev) { //TODO : read partition table from device
partitions.push(new Partition(dev, 0, 0, dev->blocks())); //Insert whole device as a partition
+
+ u8int* mbr = (u8int*)Mem::alloc(dev->blockSize());
+ if (!dev->readBlocks(0, 1, mbr)) return;
+
+ mbr_entry_t* entries = (mbr_entry_t*)((u32int)mbr + 0x1BE);
+
+ for (u32int i = 0; i < 4; i++) {
+ if ((entries[i].bootable == 0 or entries[i].bootable == 0x80) and entries[i].id != 0
+ and entries[i].s_lba != 0 and entries[i].size != 0
+ and entries[i].s_lba < dev->blocks() and entries[i].size < dev->blocks()) {
+ partitions.push(new Partition(dev, i + 1, entries[i].s_lba, entries[i].size));
+ }
+ }
+
+ Mem::free(mbr);
}
void registerDevice(BlockDevice *dev) {
@@ -73,12 +90,8 @@ BlockDevice* dev(String _class, u32int idx) {
Partition* part(BlockDevice* dev, u32int idx) {
for (u32int i = 0; i < partitions.size(); i++) {
- if (partitions[i]->getDevice() == dev) {
- if (idx == 0) {
- return partitions[i];
- } else {
- idx--;
- }
+ if (partitions[i]->getDevice() == dev && partitions[i]->getPartNumber() == idx) {
+ return partitions[i];
}
}
return NULL;
diff --git a/Source/Kernel/VFS/Part.ns.h b/Source/Kernel/VFS/Part.ns.h
index 992f6f3..e6a358c 100644
--- a/Source/Kernel/VFS/Part.ns.h
+++ b/Source/Kernel/VFS/Part.ns.h
@@ -6,6 +6,19 @@
#include <VFS/Partition.class.h>
namespace Part {
+ struct mbr_entry_t {
+ u8int bootable; /* 0 = no, 0x80 = bootable */
+ u8int s_head; /* Starting head */
+ u16int s_sector : 6; /* Starting sector */
+ u16int s_cyl : 10; /* Starting cylinder */
+ u8int id; /* System ID */
+ u8int e_head; /* Ending head */
+ u16int e_sector : 6; /* Ending sector */
+ u16int e_cyl : 10; /* Ending cylinder */
+ u32int s_lba; /* Starting sector (LBA value) */
+ u32int size; /* Total sector number */
+ } __attribute__ ((packed));
+
extern Vector<BlockDevice*> devices;
extern Vector<Partition*> partitions;
diff --git a/Source/Kernel/VFS/Partition.class.cpp b/Source/Kernel/VFS/Partition.class.cpp
index 8d7de9b..9da3461 100644
--- a/Source/Kernel/VFS/Partition.class.cpp
+++ b/Source/Kernel/VFS/Partition.class.cpp
@@ -13,12 +13,12 @@ Partition::Partition(BlockDevice* dev, u8int partnumber, u64int startblock, u64i
bool Partition::readBlocks(u64int startblock, u32int count, u8int *data) {
if (startblock + count > m_startblock + m_blockcount) return false;
- return m_cache.readBlocks(startblock - m_startblock, count, data);
+ return m_cache.readBlocks(startblock + m_startblock, count, data);
}
bool Partition::writeBlocks(u64int startblock, u32int count, u8int *data) {
if (startblock + count > m_startblock + m_blockcount) return false;
- return m_cache.writeBlocks(startblock - m_startblock, count, data);
+ return m_cache.writeBlocks(startblock + m_startblock, count, data);
}
bool Partition::read(u64int start, u32int length, u8int *data) {