Skip to content

Commit 468a22d

Browse files
committed
* Partialy implemented ATA driver
1 parent 894bf5b commit 468a22d

File tree

6 files changed

+233
-5
lines changed

6 files changed

+233
-5
lines changed

Drivers/HDD-ATA.cpp

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#include "HDD-ATA.h"
2+
3+
void printf(char* );
4+
5+
AdvancedTechnologyAttachment::AdvancedTechnologyAttachment(uint16_t PortBase, bool master)
6+
:DataPort(PortBase),
7+
ErrorPort(PortBase + 1),
8+
SectorCountPort(PortBase + 2),
9+
LBALowPort(PortBase + 3),
10+
LBAMidPort(PortBase + 4),
11+
LBAHighPort(PortBase + 5),
12+
DevicePort(PortBase + 6),
13+
CommandPort(PortBase + 7),
14+
ControlPort(PortBase + 0x206)
15+
{
16+
this->Master = master;
17+
BytesPerSector = 512;
18+
19+
}
20+
21+
AdvancedTechnologyAttachment::~AdvancedTechnologyAttachment()
22+
{
23+
24+
}
25+
26+
void AdvancedTechnologyAttachment::identify()
27+
{
28+
DevicePort.WriteToPort(Master ? 0xA0 : 0xB0);
29+
ControlPort.WriteToPort(0);
30+
31+
DevicePort.WriteToPort(0xA0);
32+
uint8_t status = CommandPort.ReadFromPort();
33+
if(status == 0xFF)
34+
return;
35+
36+
37+
DevicePort.WriteToPort(Master ? 0xA0 : 0xB0);
38+
SectorCountPort.WriteToPort(0);
39+
LBALowPort.WriteToPort(0);
40+
LBAMidPort.WriteToPort(0);
41+
LBAHighPort.WriteToPort(0);
42+
CommandPort.WriteToPort(0xEC); // identify command
43+
44+
45+
status = CommandPort.ReadFromPort();
46+
if(status == 0x00)
47+
return;
48+
49+
while(((status & 0x80) == 0x80)
50+
&& ((status & 0x01) != 0x01))
51+
status = CommandPort.ReadFromPort();
52+
53+
if(status & 0x01)
54+
{
55+
printf("ERROR");
56+
return;
57+
}
58+
59+
for(int i = 0; i < 256; i++)
60+
{
61+
uint16_t data = DataPort.ReadFromPort();
62+
char *text = " \0";
63+
text[0] = (data >> 8) & 0xFF;
64+
text[1] = data & 0xFF;
65+
printf(text);
66+
}
67+
printf("\n");
68+
}
69+
70+
void AdvancedTechnologyAttachment::Read28(uint32_t sectorNum, int count)
71+
{
72+
if(sectorNum > 0x0FFFFFFF)
73+
return;
74+
75+
DevicePort.WriteToPort( (Master ? 0xE0 : 0xF0) | ((sectorNum & 0x0F000000) >> 24) );
76+
ErrorPort.WriteToPort(0);
77+
SectorCountPort.WriteToPort(1);
78+
LBALowPort.WriteToPort( sectorNum & 0x000000FF );
79+
LBAMidPort.WriteToPort( (sectorNum & 0x0000FF00) >> 8);
80+
LBALowPort.WriteToPort( (sectorNum & 0x00FF0000) >> 16 );
81+
CommandPort.WriteToPort(0x20);
82+
83+
uint8_t status = CommandPort.ReadFromPort();
84+
while(((status & 0x80) == 0x80)
85+
&& ((status & 0x01) != 0x01))
86+
status = CommandPort.ReadFromPort();
87+
88+
if(status & 0x01)
89+
{
90+
printf("ERROR");
91+
return;
92+
}
93+
94+
95+
printf("Reading ATA Drive: ");
96+
97+
for(int i = 0; i < count; i += 2)
98+
{
99+
uint16_t wdata = DataPort.ReadFromPort();
100+
101+
char *text = " \0";
102+
text[0] = wdata & 0xFF;
103+
104+
if(i+1 < count)
105+
text[1] = (wdata >> 8) & 0xFF;
106+
else
107+
text[1] = '\0';
108+
109+
printf(text);
110+
}
111+
112+
for(int i = count + (count%2); i < 512; i += 2)
113+
DataPort.ReadFromPort();
114+
}
115+
116+
void AdvancedTechnologyAttachment::Write28(uint32_t sectorNum, uint8_t* data, uint32_t count)
117+
{
118+
if(sectorNum > 0x0FFFFFFF)
119+
return;
120+
if(count > 512)
121+
return;
122+
123+
124+
DevicePort.WriteToPort( (Master ? 0xE0 : 0xF0) | ((sectorNum & 0x0F000000) >> 24) );
125+
ErrorPort.WriteToPort(0);
126+
SectorCountPort.WriteToPort(1);
127+
LBALowPort.WriteToPort( sectorNum & 0x000000FF );
128+
LBAMidPort.WriteToPort( (sectorNum & 0x0000FF00) >> 8);
129+
LBALowPort.WriteToPort( (sectorNum & 0x00FF0000) >> 16 );
130+
CommandPort.WriteToPort(0x30);
131+
132+
133+
printf("Writing to ATA Drive: ");
134+
135+
for(int i = 0; i < count; i += 2)
136+
{
137+
uint16_t wdata = data[i];
138+
if(i+1 < count)
139+
wdata |= ((uint16_t)data[i+1]) << 8;
140+
DataPort.WriteToPort(wdata);
141+
142+
char *text = " \0";
143+
text[0] = (wdata >> 8) & 0xFF;
144+
text[1] = wdata & 0xFF;
145+
printf(text);
146+
}
147+
148+
for(int i = count + (count%2); i < 512; i += 2)
149+
DataPort.WriteToPort(0x0000);
150+
151+
}
152+
153+
void AdvancedTechnologyAttachment::Flush()
154+
{
155+
DevicePort.WriteToPort( Master ? 0xE0 : 0xF0 );
156+
CommandPort.WriteToPort(0xE7);
157+
158+
uint8_t status = CommandPort.ReadFromPort();
159+
if(status == 0x00)
160+
return;
161+
162+
while(((status & 0x80) == 0x80)
163+
&& ((status & 0x01) != 0x01))
164+
status = CommandPort.ReadFromPort();
165+
166+
if(status & 0x01)
167+
{
168+
printf("ERROR");
169+
return;
170+
}
171+
}

Drivers/HDD-ATA.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef __HDD_ATA_H
2+
#define __HDD_ATA_H
3+
4+
#include "IOPorts.h"
5+
#include "../Includes/types.h"
6+
7+
8+
class AdvancedTechnologyAttachment
9+
{
10+
protected:
11+
port8BIT DataPort; // To send data to the drive
12+
port8BIT ErrorPort; // To read error information
13+
port8BIT SectorCountPort; // To send the number of sectors to read/write
14+
port8BIT LBALowPort; // To send the low byte of the LBA
15+
port8BIT LBAMidPort; // To send the middle byte of the LBA
16+
port8BIT LBAHighPort; // To send the high byte of the LBA
17+
port8BIT DevicePort; // To send which device to use
18+
port8BIT CommandPort; // To send the command
19+
port8BIT ControlPort; // To send the control information
20+
bool Master; // Is this the master or slave drive?
21+
22+
uint16_t BytesPerSector; // The number of bytes per sector
23+
24+
public:
25+
AdvancedTechnologyAttachment(uint16_t PortBase, bool master);
26+
~AdvancedTechnologyAttachment();
27+
28+
void identify();
29+
30+
void Read28(uint32_t Sector, int count = 512); // To read data from the drive
31+
void Write28(uint32_t Sector, uint8_t* Data, uint32_t SectorCount); // To write data to the drive
32+
void Flush(); // To flush the cache of the drive
33+
34+
35+
};
36+
37+
38+
#endif

Drivers/Keyboard.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ char* INTTOCHARPOINT(int num);
1111
void ColourPrint(int type);
1212
bool txtcolor;
1313
const void* mb;
14+
void PrintHDD();
1415
void detect_cpu();
1516
void PrintMEM(const void* multiboot_structure);
1617

@@ -296,7 +297,7 @@ void KeyboardDriver::CommandInterpreter() // SOSH v1.0.3 [SectorOS SHell]. 11 Co
296297
}
297298
else if (key_buffer[5] == "s" && key_buffer[6] == "y" && key_buffer[7] == "s" && key_buffer[8] == "i" && key_buffer[9] == "n" && key_buffer[10] == "f" && key_buffer[11] == "o")
298299
{
299-
printf("sysinfo [options] : \n-C : To get CPU information\n-M : To get Memory Info\n-A : To get the Kernel architecture\n-K : To get kernel information\n-O : To get OS name\n-D : To get kernel build date\n-V : To get Kernel Version");
300+
printf("sysinfo [options] : \n-C : To get CPU information\n-M : To get Memory Info\n-A : To get the Kernel architecture\n-K : To get kernel information\n-O : To get OS name\n-B : To get kernel build date\n-D : To identify a ATA drive\n-V : To get Kernel Version");
300301
}
301302
else if(key_buffer[5] == "1")
302303
printf("Help page 1:\necho <message> : to print the message in the console \nhelp : to show this message \nclear : to clear the screen \nsd <options> : controls the power of the computer ");
@@ -420,18 +421,22 @@ void KeyboardDriver::CommandInterpreter() // SOSH v1.0.3 [SectorOS SHell]. 11 Co
420421
{
421422
printf(KERNEL_VERSION);
422423
}
423-
else if (key_buffer[8] == "-" && key_buffer[9] == "D")
424+
else if (key_buffer[8] == "-" && key_buffer[9] == "B")
424425
{
425426
printf(KERNEL_BUILD);
426427
}
427428
else if (key_buffer[8] == "-" && key_buffer[9] == "H")
428429
{
429-
printf("sysinfo [options] : \n-C : To get CPU information\n-M : To get Memory Info\n-A : To get the Kernel architecture\n-K : To get kernel information\n-O : To get OS name\n-D : To get kernel build date\n-V : To get Kernel Version\n-H : To print this message");
430+
printf("sysinfo [options] : \n-C : To get CPU information\n-M : To get Memory Info\n-A : To get the Kernel architecture\n-K : To get kernel information\n-O : To get OS name\n-B : To get kernel build date\n-V : To get Kernel Version\n-D : To identify a ATA drive\n-H : To print this message");
430431
}
431432
else if (key_buffer[8] == "-" && key_buffer[9] == "O")
432433
{
433434
printf(OS_NAME);
434435
}
436+
else if (key_buffer[8] == "-" && key_buffer[9] == "D")
437+
{
438+
PrintHDD();
439+
}
435440
else if (key_buffer[8] == "-" && key_buffer[9] == "K")
436441
{
437442
printf("SectorOS Kernel "); printf(KERNEL_VERSION); printf(" "); printf(KERNEL_BUILD);

Includes/Public_VAR.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
inline char* OS_NAME = "SectorOS";
55

66
inline char* KERNEL_NAME = "SectorOS";
7-
inline char* KERNEL_VERSION = "V1.3.1";
7+
inline char* KERNEL_VERSION = "V1.4.0";
88
inline char* KERNEL_BUILD = "Build: 2021-11-16";
99
inline char* KERNEL_ARCH = "x86";
1010

1111
inline char* SHELL_NAME = "SOSH";
12-
inline char* SHELL_VER = "V1.0.3";
12+
inline char* SHELL_VER = "V1.0.4";
1313

1414
#endif

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ kernel/MultiTask.o \
1313
Drivers/Driver.o \
1414
CPU/PowerControl.o \
1515
Drivers/Keyboard.o \
16+
Drivers/HDD-ATA.o \
1617
Drivers/Mouse.o \
1718
Drivers/RTC.o \
1819
Hardcom/SerialPort.o \

kernel/kernel.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
#include "../Includes/Public_VAR.h"
1212
#include "MultiTask.h"
1313
#include "../Memory/MemoryManagement.h"
14+
#include "../Drivers/HDD-ATA.h"
1415

1516
static uint8_t cursory;
1617
static uint8_t cursorx;
1718
static bool useMouse = true;
1819
bool isused;
1920
bool isTxtMode;
2021
extern const void* mb;
22+
AdvancedTechnologyAttachment ata0m(0x1F0, true);
23+
AdvancedTechnologyAttachment ata0s(0x1F0, false);
2124

2225
port8BIT port43(0x43);
2326
port8BIT port42(0x42);
@@ -401,6 +404,11 @@ void printTime()
401404

402405
}
403406

407+
void PrintHDD()
408+
{
409+
ata0s.identify();
410+
}
411+
404412
void printfchar(char st)
405413
{
406414
static uint8_t x=0, y=0;
@@ -761,6 +769,11 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
761769

762770
sp.logToSerialPort("\nHardware initialising stage 3 finished");
763771

772+
ata0s.identify();
773+
774+
AdvancedTechnologyAttachment ata1m(0x170, true);
775+
AdvancedTechnologyAttachment ata1s(0x170, false);
776+
764777
detect_cpu();
765778

766779
printf("\5");

0 commit comments

Comments
 (0)