Skip to content

Commit 84882d3

Browse files
committed
* Perfected the ATA driver
1 parent 468a22d commit 84882d3

File tree

7 files changed

+109
-91
lines changed

7 files changed

+109
-91
lines changed

Drivers/HDD-ATA.cpp

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,64 @@
11
#include "HDD-ATA.h"
22

33
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)
4+
void printHex(uint8_t );
5+
6+
AdvancedTechnologyAttachment::AdvancedTechnologyAttachment(bool master, uint16_t portBase)
7+
: dataPort(portBase),
8+
errorPort(portBase + 0x1),
9+
sectorCountPort(portBase + 0x2),
10+
lbaLowPort(portBase + 0x3),
11+
lbaMidPort(portBase + 0x4),
12+
lbaHiPort(portBase + 0x5),
13+
devicePort(portBase + 0x6),
14+
commandPort(portBase + 0x7),
15+
controlPort(portBase + 0x206)
1516
{
16-
this->Master = master;
17-
BytesPerSector = 512;
18-
17+
this->master = master;
1918
}
2019

2120
AdvancedTechnologyAttachment::~AdvancedTechnologyAttachment()
2221
{
23-
2422
}
2523

26-
void AdvancedTechnologyAttachment::identify()
24+
void AdvancedTechnologyAttachment::Identify()
2725
{
28-
DevicePort.WriteToPort(Master ? 0xA0 : 0xB0);
29-
ControlPort.WriteToPort(0);
26+
devicePort.WriteToPort(master ? 0xA0 : 0xB0);
27+
controlPort.WriteToPort(0);
3028

31-
DevicePort.WriteToPort(0xA0);
32-
uint8_t status = CommandPort.ReadFromPort();
29+
devicePort.WriteToPort(0xA0);
30+
uint8_t status = commandPort.ReadFromPort();
3331
if(status == 0xFF)
3432
return;
3533

3634

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
35+
devicePort.WriteToPort(master ? 0xA0 : 0xB0);
36+
sectorCountPort.WriteToPort(0);
37+
lbaLowPort.WriteToPort(0);
38+
lbaMidPort.WriteToPort(0);
39+
lbaHiPort.WriteToPort(0);
40+
commandPort.WriteToPort(0xEC); // identify command
4341

4442

45-
status = CommandPort.ReadFromPort();
43+
status = commandPort.ReadFromPort();
4644
if(status == 0x00)
4745
return;
4846

4947
while(((status & 0x80) == 0x80)
5048
&& ((status & 0x01) != 0x01))
51-
status = CommandPort.ReadFromPort();
49+
status = commandPort.ReadFromPort();
5250

5351
if(status & 0x01)
5452
{
5553
printf("ERROR");
5654
return;
5755
}
5856

59-
for(int i = 0; i < 256; i++)
57+
for(int i = 0; i < 35; i++)
6058
{
61-
uint16_t data = DataPort.ReadFromPort();
62-
char *text = " \0";
59+
60+
uint16_t data = dataPort.ReadFromPort();
61+
char *text = "\0\0";
6362
text[0] = (data >> 8) & 0xFF;
6463
text[1] = data & 0xFF;
6564
printf(text);
@@ -72,18 +71,18 @@ void AdvancedTechnologyAttachment::Read28(uint32_t sectorNum, int count)
7271
if(sectorNum > 0x0FFFFFFF)
7372
return;
7473

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);
74+
devicePort.WriteToPort( (master ? 0xE0 : 0xF0) | ((sectorNum & 0x0F000000) >> 24) );
75+
errorPort.WriteToPort(0);
76+
sectorCountPort.WriteToPort(1);
77+
lbaLowPort.WriteToPort( sectorNum & 0x000000FF );
78+
lbaMidPort.WriteToPort( (sectorNum & 0x0000FF00) >> 8);
79+
lbaLowPort.WriteToPort( (sectorNum & 0x00FF0000) >> 16 );
80+
commandPort.WriteToPort(0x20);
8281

83-
uint8_t status = CommandPort.ReadFromPort();
82+
uint8_t status = commandPort.ReadFromPort();
8483
while(((status & 0x80) == 0x80)
8584
&& ((status & 0x01) != 0x01))
86-
status = CommandPort.ReadFromPort();
85+
status = commandPort.ReadFromPort();
8786

8887
if(status & 0x01)
8988
{
@@ -96,7 +95,7 @@ void AdvancedTechnologyAttachment::Read28(uint32_t sectorNum, int count)
9695

9796
for(int i = 0; i < count; i += 2)
9897
{
99-
uint16_t wdata = DataPort.ReadFromPort();
98+
uint16_t wdata = dataPort.ReadFromPort();
10099

101100
char *text = " \0";
102101
text[0] = wdata & 0xFF;
@@ -110,7 +109,7 @@ void AdvancedTechnologyAttachment::Read28(uint32_t sectorNum, int count)
110109
}
111110

112111
for(int i = count + (count%2); i < 512; i += 2)
113-
DataPort.ReadFromPort();
112+
dataPort.ReadFromPort();
114113
}
115114

116115
void AdvancedTechnologyAttachment::Write28(uint32_t sectorNum, uint8_t* data, uint32_t count)
@@ -121,13 +120,13 @@ void AdvancedTechnologyAttachment::Write28(uint32_t sectorNum, uint8_t* data, ui
121120
return;
122121

123122

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);
123+
devicePort.WriteToPort( (master ? 0xE0 : 0xF0) | ((sectorNum & 0x0F000000) >> 24) );
124+
errorPort.WriteToPort(0);
125+
sectorCountPort.WriteToPort(1);
126+
lbaLowPort.WriteToPort( sectorNum & 0x000000FF );
127+
lbaMidPort.WriteToPort( (sectorNum & 0x0000FF00) >> 8);
128+
lbaLowPort.WriteToPort( (sectorNum & 0x00FF0000) >> 16 );
129+
commandPort.WriteToPort(0x30);
131130

132131

133132
printf("Writing to ATA Drive: ");
@@ -137,7 +136,7 @@ void AdvancedTechnologyAttachment::Write28(uint32_t sectorNum, uint8_t* data, ui
137136
uint16_t wdata = data[i];
138137
if(i+1 < count)
139138
wdata |= ((uint16_t)data[i+1]) << 8;
140-
DataPort.WriteToPort(wdata);
139+
dataPort.WriteToPort(wdata);
141140

142141
char *text = " \0";
143142
text[0] = (wdata >> 8) & 0xFF;
@@ -146,22 +145,22 @@ void AdvancedTechnologyAttachment::Write28(uint32_t sectorNum, uint8_t* data, ui
146145
}
147146

148147
for(int i = count + (count%2); i < 512; i += 2)
149-
DataPort.WriteToPort(0x0000);
148+
dataPort.WriteToPort(0x0000);
150149

151150
}
152151

153152
void AdvancedTechnologyAttachment::Flush()
154153
{
155-
DevicePort.WriteToPort( Master ? 0xE0 : 0xF0 );
156-
CommandPort.WriteToPort(0xE7);
154+
devicePort.WriteToPort( master ? 0xE0 : 0xF0 );
155+
commandPort.WriteToPort(0xE7);
157156

158-
uint8_t status = CommandPort.ReadFromPort();
157+
uint8_t status = commandPort.ReadFromPort();
159158
if(status == 0x00)
160159
return;
161160

162161
while(((status & 0x80) == 0x80)
163162
&& ((status & 0x01) != 0x01))
164-
status = CommandPort.ReadFromPort();
163+
status = commandPort.ReadFromPort();
165164

166165
if(status & 0x01)
167166
{

Drivers/HDD-ATA.h

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,23 @@
88
class AdvancedTechnologyAttachment
99
{
1010
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-
11+
bool master;
12+
port16BIT dataPort;
13+
port8BIT errorPort;
14+
port8BIT sectorCountPort;
15+
port8BIT lbaLowPort;
16+
port8BIT lbaMidPort;
17+
port8BIT lbaHiPort;
18+
port8BIT devicePort;
19+
port8BIT commandPort;
20+
port8BIT controlPort;
2421
public:
25-
AdvancedTechnologyAttachment(uint16_t PortBase, bool master);
22+
AdvancedTechnologyAttachment(bool master, uint16_t portBase);
2623
~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-
24+
void Identify();
25+
void Read28(uint32_t sectorNum, int count = 512);
26+
void Write28(uint32_t sectorNum, uint8_t* data, uint32_t count);
27+
void Flush();
3528
};
3629

3730

Drivers/Keyboard.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const void* mb;
1414
void PrintHDD();
1515
void detect_cpu();
1616
void PrintMEM(const void* multiboot_structure);
17+
void PrintSATA();
1718

1819
PowerControl power;
1920

@@ -435,7 +436,7 @@ void KeyboardDriver::CommandInterpreter() // SOSH v1.0.3 [SectorOS SHell]. 11 Co
435436
}
436437
else if (key_buffer[8] == "-" && key_buffer[9] == "D")
437438
{
438-
PrintHDD();
439+
PrintSATA();
439440
}
440441
else if (key_buffer[8] == "-" && key_buffer[9] == "K")
441442
{

Drivers/Keyboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "../Includes/Public_VAR.h"
1313
#include "../Hardcom/pci.h"
1414
#include "../Includes/multiboot.h"
15+
#include "HDD-ATA.h"
1516

1617
class KeyboardDriver : public InterruptHandler, public Driver // Driver for keyboard
1718
{

Includes/Debug.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef __DEBUG_H
2+
#define __DEBUG_H
3+
4+
inline void Breakpoint()
5+
{
6+
asm("int $3");
7+
}
8+
9+
#endif

Includes/Public_VAR.h

Lines changed: 3 additions & 3 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.4.0";
8-
inline char* KERNEL_BUILD = "Build: 2021-11-16";
7+
inline char* KERNEL_VERSION = "V1.4.4";
8+
inline char* KERNEL_BUILD = "Build: 2021-11-24";
99
inline char* KERNEL_ARCH = "x86";
1010

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

1414
#endif

kernel/kernel.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../Includes/Public_VAR.h"
1212
#include "MultiTask.h"
1313
#include "../Memory/MemoryManagement.h"
14+
#include "../Includes/Debug.h"
1415
#include "../Drivers/HDD-ATA.h"
1516

1617
static uint8_t cursory;
@@ -404,11 +405,6 @@ void printTime()
404405

405406
}
406407

407-
void PrintHDD()
408-
{
409-
ata0s.identify();
410-
}
411-
412408
void printfchar(char st)
413409
{
414410
static uint8_t x=0, y=0;
@@ -601,6 +597,27 @@ int do_intel(void)
601597

602598
}
603599

600+
void PrintSATA()
601+
{
602+
printf("\nS-ATA primary master: ");
603+
AdvancedTechnologyAttachment ata0m(true, 0x1F0);
604+
ata0m.Identify();
605+
606+
printf("\nS-ATA primary slave: ");
607+
AdvancedTechnologyAttachment ata0s(false, 0x1F0);
608+
ata0s.Identify();
609+
printf("\n");
610+
611+
printf("\nS-ATA secondary master: ");
612+
AdvancedTechnologyAttachment ata1m(true, 0x170);
613+
ata1m.Identify();
614+
615+
printf("\nS-ATA secondary slave: ");
616+
AdvancedTechnologyAttachment ata1s(false, 0x170);
617+
ata1s.Identify();
618+
printf("\n");
619+
}
620+
604621
int detect_cpu(void)
605622
{
606623
unsigned long ebx, unused;
@@ -763,17 +780,14 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
763780

764781
printf("\nSYSMSG: Initializing Hardwares [Stage 3]...\n");
765782

783+
PrintSATA();
784+
766785
printf("Allocating Memory....\n");
767786

768787
PrintMEM(multiboot_structure);
769788

770789
sp.logToSerialPort("\nHardware initialising stage 3 finished");
771790

772-
ata0s.identify();
773-
774-
AdvancedTechnologyAttachment ata1m(0x170, true);
775-
AdvancedTechnologyAttachment ata1s(0x170, false);
776-
777791
detect_cpu();
778792

779793
printf("\5");
@@ -787,6 +801,7 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
787801
printf("Welcome to SectorOS Monolithic kernel ");PrintDate();printf(" Type: Shell\nhttps://github.com/Arun007coder/SectorOS \n");
788802

789803
printf("Initializing "); printf(SHELL_NAME); printf(" "); printf(SHELL_VER);
804+
790805
printf("\n\n");
791806

792807
printf("Welcome to SectorOS Shell\nRun help to get the list of commands which is implemented \n \n");

0 commit comments

Comments
 (0)