Permalink
Browse files

Files for Part 19, https://www.youtube.com/watch?v=uS02rOvLgak

  • Loading branch information...
AlgorithMan-de committed Jul 29, 2016
1 parent c2fd644 commit fa076f542ab5f16a32bd141e4b44c62ab0bac9b8
Showing with 244 additions and 2 deletions.
  1. +43 −0 include/drivers/ata.h
  2. +1 −0 makefile
  3. +175 −0 src/drivers/ata.cpp
  4. +25 −2 src/kernel.cpp
View
@@ -0,0 +1,43 @@
#ifndef __MYOS__DRIVERS__ATA_H
#define __MYOS__DRIVERS__ATA_H
#include <common/types.h>
#include <hardwarecommunication/interrupts.h>
#include <hardwarecommunication/port.h>
namespace myos
{
namespace drivers
{
class AdvancedTechnologyAttachment
{
protected:
bool master;
hardwarecommunication::Port16Bit dataPort;
hardwarecommunication::Port8Bit errorPort;
hardwarecommunication::Port8Bit sectorCountPort;
hardwarecommunication::Port8Bit lbaLowPort;
hardwarecommunication::Port8Bit lbaMidPort;
hardwarecommunication::Port8Bit lbaHiPort;
hardwarecommunication::Port8Bit devicePort;
hardwarecommunication::Port8Bit commandPort;
hardwarecommunication::Port8Bit controlPort;
public:
AdvancedTechnologyAttachment(bool master, common::uint16_t portBase);
~AdvancedTechnologyAttachment();
void Identify();
void Read28(common::uint32_t sectorNum, int count = 512);
void Write28(common::uint32_t sectorNum, common::uint8_t* data, common::uint32_t count);
void Flush();
};
}
}
#endif
View
@@ -19,6 +19,7 @@ objects = obj/loader.o \
obj/drivers/keyboard.o \
obj/drivers/mouse.o \
obj/drivers/vga.o \
obj/drivers/ata.o \
obj/gui/widget.o \
obj/gui/window.o \
obj/gui/desktop.o \
View
@@ -0,0 +1,175 @@
#include <drivers/ata.h>
using namespace myos;
using namespace myos::common;
using namespace myos::drivers;
void printf(char* str);
void printfHex(uint8_t);
AdvancedTechnologyAttachment::AdvancedTechnologyAttachment(bool master, common::uint16_t portBase)
: dataPort(portBase),
errorPort(portBase + 0x1),
sectorCountPort(portBase + 0x2),
lbaLowPort(portBase + 0x3),
lbaMidPort(portBase + 0x4),
lbaHiPort(portBase + 0x5),
devicePort(portBase + 0x6),
commandPort(portBase + 0x7),
controlPort(portBase + 0x206)
{
this->master = master;
}
AdvancedTechnologyAttachment::~AdvancedTechnologyAttachment()
{
}
void AdvancedTechnologyAttachment::Identify()
{
devicePort.Write(master ? 0xA0 : 0xB0);
controlPort.Write(0);
devicePort.Write(0xA0);
uint8_t status = commandPort.Read();
if(status == 0xFF)
return;
devicePort.Write(master ? 0xA0 : 0xB0);
sectorCountPort.Write(0);
lbaLowPort.Write(0);
lbaMidPort.Write(0);
lbaHiPort.Write(0);
commandPort.Write(0xEC); // identify command
status = commandPort.Read();
if(status == 0x00)
return;
while(((status & 0x80) == 0x80)
&& ((status & 0x01) != 0x01))
status = commandPort.Read();
if(status & 0x01)
{
printf("ERROR");
return;
}
for(int i = 0; i < 256; i++)
{
uint16_t data = dataPort.Read();
char *text = " \0";
text[0] = (data >> 8) & 0xFF;
text[1] = data & 0xFF;
printf(text);
}
printf("\n");
}
void AdvancedTechnologyAttachment::Read28(common::uint32_t sectorNum, int count)
{
if(sectorNum > 0x0FFFFFFF)
return;
devicePort.Write( (master ? 0xE0 : 0xF0) | ((sectorNum & 0x0F000000) >> 24) );
errorPort.Write(0);
sectorCountPort.Write(1);
lbaLowPort.Write( sectorNum & 0x000000FF );
lbaMidPort.Write( (sectorNum & 0x0000FF00) >> 8);
lbaLowPort.Write( (sectorNum & 0x00FF0000) >> 16 );
commandPort.Write(0x20);
uint8_t status = commandPort.Read();
while(((status & 0x80) == 0x80)
&& ((status & 0x01) != 0x01))
status = commandPort.Read();
if(status & 0x01)
{
printf("ERROR");
return;
}
printf("Reading ATA Drive: ");
for(int i = 0; i < count; i += 2)
{
uint16_t wdata = dataPort.Read();
char *text = " \0";
text[0] = wdata & 0xFF;
if(i+1 < count)
text[1] = (wdata >> 8) & 0xFF;
else
text[1] = '\0';
printf(text);
}
for(int i = count + (count%2); i < 512; i += 2)
dataPort.Read();
}
void AdvancedTechnologyAttachment::Write28(common::uint32_t sectorNum, common::uint8_t* data, common::uint32_t count)
{
if(sectorNum > 0x0FFFFFFF)
return;
if(count > 512)
return;
devicePort.Write( (master ? 0xE0 : 0xF0) | ((sectorNum & 0x0F000000) >> 24) );
errorPort.Write(0);
sectorCountPort.Write(1);
lbaLowPort.Write( sectorNum & 0x000000FF );
lbaMidPort.Write( (sectorNum & 0x0000FF00) >> 8);
lbaLowPort.Write( (sectorNum & 0x00FF0000) >> 16 );
commandPort.Write(0x30);
printf("Writing to ATA Drive: ");
for(int i = 0; i < count; i += 2)
{
uint16_t wdata = data[i];
if(i+1 < count)
wdata |= ((uint16_t)data[i+1]) << 8;
dataPort.Write(wdata);
char *text = " \0";
text[0] = (wdata >> 8) & 0xFF;
text[1] = wdata & 0xFF;
printf(text);
}
for(int i = count + (count%2); i < 512; i += 2)
dataPort.Write(0x0000);
}
void AdvancedTechnologyAttachment::Flush()
{
devicePort.Write( master ? 0xE0 : 0xF0 );
commandPort.Write(0xE7);
uint8_t status = commandPort.Read();
if(status == 0x00)
return;
while(((status & 0x80) == 0x80)
&& ((status & 0x01) != 0x01))
status = commandPort.Read();
if(status & 0x01)
{
printf("ERROR");
return;
}
}
View
@@ -8,6 +8,7 @@
#include <drivers/keyboard.h>
#include <drivers/mouse.h>
#include <drivers/vga.h>
#include <drivers/ata.h>
#include <gui/desktop.h>
#include <gui/window.h>
#include <multitasking.h>
@@ -190,7 +191,7 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
printfHex(((size_t)allocated >> 8 ) & 0xFF);
printfHex(((size_t)allocated ) & 0xFF);
printf("\n");
TaskManager taskManager;
/*
Task task1(&gdt, taskA);
@@ -245,7 +246,29 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
#endif
printf("\nS-ATA primary master: ");
AdvancedTechnologyAttachment ata0m(true, 0x1F0);
ata0m.Identify();
printf("\nS-ATA primary slave: ");
AdvancedTechnologyAttachment ata0s(false, 0x1F0);
ata0s.Identify();
ata0s.Write28(0, (uint8_t*)"http://www.AlgorithMan.de", 25);
ata0s.Flush();
ata0s.Read28(0);
printf("\nS-ATA secondary master: ");
AdvancedTechnologyAttachment ata1m(true, 0x170);
ata1m.Identify();
printf("\nS-ATA secondary slave: ");
AdvancedTechnologyAttachment ata1s(false, 0x170);
ata1s.Identify();
// third: 0x1E8
// fourth: 0x168
amd_am79c973* eth0 = (amd_am79c973*)(drvManager.drivers[2]);
eth0->Send((uint8_t*)"Hello Network", 13);

0 comments on commit fa076f5

Please sign in to comment.