Skip to content

Commit de32de2

Browse files
committed
* Added support to msdos partition table and fat32 file system
* Updated read28 function * added new command lspt
1 parent 86fcdf4 commit de32de2

File tree

10 files changed

+271
-14
lines changed

10 files changed

+271
-14
lines changed

Drivers/HDD-ATA.cpp

+4-10
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void AdvancedTechnologyAttachment::Identify()
6666
printf("\n");
6767
}
6868

69-
void AdvancedTechnologyAttachment::Read28(uint32_t sectorNum, int count)
69+
void AdvancedTechnologyAttachment::Read28(uint32_t sectorNum, uint8_t* data, int count)
7070
{
7171
if(sectorNum > 0x0FFFFFFF)
7272
return;
@@ -93,19 +93,13 @@ void AdvancedTechnologyAttachment::Read28(uint32_t sectorNum, int count)
9393

9494
printf("Reading ATA Drive: ");
9595

96-
for(int i = 0; i < count; i += 2)
96+
for(uint16_t i = 0; i < count; i += 2)
9797
{
9898
uint16_t wdata = dataPort.ReadFromPort();
9999

100-
char *text = " \0";
101-
text[0] = wdata & 0xFF;
102-
100+
data[i] = wdata & 0x00FF;
103101
if(i+1 < count)
104-
text[1] = (wdata >> 8) & 0xFF;
105-
else
106-
text[1] = '\0';
107-
108-
printf(text);
102+
data[i+1] = (wdata >> 8) & 0x00FF;
109103
}
110104

111105
for(int i = count + (count%2); i < 512; i += 2)

Drivers/HDD-ATA.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AdvancedTechnologyAttachment
2222
AdvancedTechnologyAttachment(bool master, uint16_t portBase);
2323
~AdvancedTechnologyAttachment();
2424
void Identify();
25-
void Read28(uint32_t sectorNum, int count = 512);
25+
void Read28(uint32_t sectorNum, uint8_t* data, int count = 512);
2626
void Write28(uint32_t sectorNum, uint8_t* data, uint32_t count);
2727
void Flush();
2828
};

Drivers/Keyboard.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void detect_cpu();
1616
void PrintMEM(const void* multiboot_structure);
1717
void PrintSATA();
1818
void PVGA();
19+
void PrintPartitions();
1920

2021
PowerControl power;
2122

@@ -307,6 +308,8 @@ void KeyboardDriver::CommandInterpreter() // SOSH v1.0.3 [SectorOS SHell]. 11 Co
307308
printf("Help page 2:\nadd1 <num1> <num2> :To add 2 numbers.This command only supports 1 digit number\nsub1 <num1> <num2> :to subtract 2 numbers.This command only supports 1 digit number\ntxt : To enter the text mode. You cannot save files\nmul1 <num1> <num2> : To multiply 2 numbers.");
308309
else if (key_buffer[5] == "3")
309310
printf("Help page 3:\nspi : To print the data in serial port 0x3F8.\nspo : To write data to serial port 0x3F8.\nsysinfo [option] : To get info about system.\nvga : To use experimental vga graphics.");
311+
else if (key_buffer[5] == "4")
312+
printf("Help page 4:\nlspt: To list partitions in a drive.");
310313
else
311314
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 ");
312315
}
@@ -453,6 +456,11 @@ void KeyboardDriver::CommandInterpreter() // SOSH v1.0.3 [SectorOS SHell]. 11 Co
453456
COMNAME = "vga";
454457
PVGA();
455458
}
459+
else if (key_buffer[0] == "l" && key_buffer[1] == "s" && key_buffer[2] == "p" && key_buffer[3] == "t")
460+
{
461+
COMNAME = "lspt";
462+
PrintPartitions();
463+
}
456464
else
457465
{
458466
printf("Unknown Command. Type help in console to get all the commands");

Filesystem/FATFS.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "FATFS.h"
2+
3+
void printf(char*);
4+
void printHex(uint8_t Key);
5+
6+
void ReadBiosBlock(AdvancedTechnologyAttachment *hd, uint32_t partitionOffset)
7+
{
8+
BiosParameterBlock32 bpb;
9+
hd->Read28(partitionOffset, (uint8_t*)&bpb, sizeof(BiosParameterBlock32));
10+
11+
12+
printf("sectors per cluster: ");
13+
printHex(bpb.sectorsPerCluster);
14+
printf("\n");
15+
16+
17+
uint32_t fatStart = partitionOffset + bpb.reservedSectors;
18+
uint32_t fatSize = bpb.tableSize;
19+
20+
uint32_t dataStart = fatStart + fatSize*bpb.fatCopies;
21+
22+
uint32_t rootStart = dataStart + bpb.sectorsPerCluster*(bpb.rootCluster - 2);
23+
24+
25+
DirectoryEntryFat32 dirent[16];
26+
hd->Read28(rootStart, (uint8_t*)&dirent[0], 16*sizeof(DirectoryEntryFat32));
27+
28+
for(int i = 0; i < 16; i++)
29+
{
30+
if(dirent[i].name[0] == 0x00)
31+
break;
32+
33+
if((dirent[i].attributes & 0x0F) == 0x0F)
34+
continue;
35+
36+
char* foo = " \n";
37+
for(int j = 0; j < 8; j++)
38+
foo[j] = dirent[i].name[j];
39+
printf(foo);
40+
41+
42+
if((dirent[i].attributes & 0x10) == 0x10) // directory
43+
continue;
44+
45+
uint32_t firstFileCluster = ((uint32_t)dirent[i].firstClusterHi) << 16
46+
| ((uint32_t)dirent[i].firstClusterLow);
47+
48+
49+
50+
51+
int32_t SIZE = dirent[i].size;
52+
int32_t nextFileCluster = firstFileCluster;
53+
uint8_t buffer[513];
54+
uint8_t fatbuffer[513];
55+
56+
57+
while(SIZE > 0)
58+
{
59+
uint32_t fileSector = dataStart + bpb.sectorsPerCluster * (nextFileCluster-2);
60+
int sectorOffset = 0;
61+
62+
for(; SIZE > 0; SIZE -= 512)
63+
{
64+
hd->Read28(fileSector+sectorOffset, buffer, 512);
65+
66+
buffer[SIZE > 512 ? 512 : SIZE] = '\0';
67+
printf((char*)buffer);
68+
69+
if(++sectorOffset > bpb.sectorsPerCluster)
70+
break;
71+
}
72+
73+
uint32_t fatSectorForCurrentCluster = nextFileCluster / (512/sizeof(uint32_t));
74+
hd->Read28(fatStart+fatSectorForCurrentCluster, fatbuffer, 512);
75+
uint32_t fatOffsetInSectorForCurrentCluster = nextFileCluster % (512/sizeof(uint32_t));
76+
nextFileCluster = ((uint32_t*)&fatbuffer)[fatOffsetInSectorForCurrentCluster] & 0x0FFFFFFF;
77+
78+
}
79+
80+
}
81+
82+
83+
}

Filesystem/FATFS.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef __FATFS_H
2+
#define __FATFS_H
3+
4+
#include "../Drivers/HDD-ATA.h"
5+
#include "../Includes/types.h"
6+
7+
struct BiosParameterBlock32
8+
{
9+
uint8_t jump[3];
10+
uint8_t softName[8];
11+
uint16_t bytesPerSector;
12+
uint8_t sectorsPerCluster;
13+
uint16_t reservedSectors;
14+
uint8_t fatCopies;
15+
uint16_t rootDirEntries;
16+
uint16_t totalSectors;
17+
uint8_t mediaType;
18+
uint16_t fatSectorCount;
19+
uint16_t sectorsPerTrack;
20+
uint16_t headCount;
21+
uint32_t hiddenSectors;
22+
uint32_t totalSectorCount;
23+
24+
uint32_t tableSize;
25+
uint16_t extFlags;
26+
uint16_t fatVersion;
27+
uint32_t rootCluster;
28+
uint16_t fatInfo;
29+
uint16_t backupSector;
30+
uint8_t reserved0[12];
31+
uint8_t driveNumber;
32+
uint8_t reserved;
33+
uint8_t bootSignature;
34+
uint32_t volumeId;
35+
uint8_t volumeLabel[11];
36+
uint8_t fatTypeLabel[8];
37+
} __attribute__((packed));
38+
39+
40+
struct DirectoryEntryFat32
41+
{
42+
uint8_t name[8];
43+
uint8_t ext[3];
44+
uint8_t attributes;
45+
uint8_t reserved;
46+
uint8_t cTimeTenth;
47+
uint16_t cTime;
48+
uint16_t cDate;
49+
uint16_t aTime;
50+
uint16_t firstClusterHi;
51+
uint16_t wTime;
52+
uint16_t wDate;
53+
uint16_t firstClusterLow;
54+
uint32_t size;
55+
} __attribute__((packed));
56+
57+
58+
void ReadBiosBlock(AdvancedTechnologyAttachment *hd, uint32_t partitionOffset);
59+
60+
#endif

Filesystem/MSDOSPT.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "MSDOSPT.h"
2+
3+
void printf(char*);
4+
void printHex(uint8_t);
5+
6+
void MSDOSPartitionTable::ReadPartitions(AdvancedTechnologyAttachment *hd)
7+
{
8+
9+
MasterBootRecord mbr;
10+
11+
printf("MBR: ");
12+
13+
hd->Read28(0, (uint8_t*)&mbr, sizeof(MasterBootRecord));
14+
15+
/*
16+
for(int i = 0x1BE; i <= 0x01FF; i++)
17+
{
18+
printHex(((uint8_t*)&mbr)[i]);
19+
printf(" ");
20+
}
21+
printf("\n");
22+
*/
23+
24+
if(mbr.magicnumber != 0xAA55)
25+
{
26+
printf("illegal MBR");
27+
return;
28+
}
29+
30+
31+
for(int i = 0; i < 4; i++)
32+
{
33+
if(mbr.primaryPartition[i].partition_id == 0x00)
34+
continue;
35+
36+
printf(" Partition ");
37+
printHex(i & 0xFF);
38+
39+
40+
if(mbr.primaryPartition[i].bootable == 0x80)
41+
printf(" bootable. Type");
42+
else
43+
printf(" not bootable. Type ");
44+
45+
46+
printHex(mbr.primaryPartition[i].partition_id);
47+
48+
49+
ReadBiosBlock(hd, mbr.primaryPartition[i].start_lba);
50+
}
51+
52+
}

Filesystem/MSDOSPT.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef __MSDOSPT_H
2+
#define __MSDOSPT_H
3+
4+
#include "../Drivers/HDD-ATA.h"
5+
#include "../Includes/types.h"
6+
#include "FATFS.h"
7+
8+
struct PartitionTableEntry
9+
{
10+
uint8_t bootable;
11+
uint8_t start_head;
12+
uint8_t start_sector : 6;
13+
uint16_t start_cylinder : 10;
14+
uint8_t partition_id;
15+
uint8_t end_head;
16+
uint8_t end_sector : 6;
17+
uint16_t end_cylinder : 10;
18+
19+
uint32_t start_lba;
20+
uint32_t length;
21+
} __attribute__((packed));
22+
23+
struct MasterBootRecord
24+
{
25+
uint8_t bootloader[440];
26+
uint32_t signature;
27+
uint16_t unused;
28+
29+
PartitionTableEntry primaryPartition[4];
30+
31+
uint16_t magicnumber;
32+
} __attribute__((packed));
33+
34+
35+
class MSDOSPartitionTable
36+
{
37+
public:
38+
static void ReadPartitions(AdvancedTechnologyAttachment *hd);
39+
};
40+
41+
#endif

Includes/Public_VAR.h

+3-3
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.5.0";
8-
inline char* KERNEL_BUILD = "Build: 2021-11-24";
7+
inline char* KERNEL_VERSION = "V2.1.0";
8+
inline char* KERNEL_BUILD = "Build: 2021-11-25";
99
inline char* KERNEL_ARCH = "x86";
1010

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

1414
#endif

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Drivers/Keyboard.o \
1616
Drivers/HDD-ATA.o \
1717
Drivers/VGADriver.o \
1818
CPU/syscall.o \
19+
Filesystem/MSDOSPT.o \
20+
Filesystem/FATFS.o \
1921
Drivers/Mouse.o \
2022
Drivers/RTC.o \
2123
Hardcom/SerialPort.o \

kernel/kernel.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../Includes/Debug.h"
1515
#include "../Drivers/HDD-ATA.h"
1616
#include "../Drivers/VGADriver.h"
17+
#include "../Filesystem/MSDOSPT.h"
1718

1819
static uint8_t cursory;
1920
static uint8_t cursorx;
@@ -460,6 +461,12 @@ void printHex(uint8_t Key)
460461
printf(foo);
461462
}
462463

464+
void PrintPartitions()
465+
{
466+
AdvancedTechnologyAttachment ata0s(false, 0x1F0);
467+
MSDOSPartitionTable::ReadPartitions(&ata0s);
468+
}
469+
463470
void PrintMEM(const void* multiboot_structure)
464471
{
465472
uint32_t* memupper = (uint32_t*)(((size_t)multiboot_structure) + 8);
@@ -792,6 +799,14 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
792799

793800
PrintSATA();
794801

802+
PrintPartitions();
803+
804+
/*
805+
AdvancedTechnologyAttachment ata0s(false, 0x1F0);
806+
ata0s.Write28(4, (uint8_t*)"Hello There", 12);
807+
ata0s.Flush();
808+
*/
809+
795810
printf("Allocating Memory....\n");
796811

797812
PrintMEM(multiboot_structure);
@@ -816,6 +831,8 @@ extern "C" void kernelMain(const void* multiboot_structure, uint32_t /*multiboot
816831

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

834+
//ata0s.Read28(4);
835+
819836
sp.logToSerialPort("\nKernel initialization surcessful.\nGiving execution access to the kernel.\nAwaiting user input...");
820837

821838
printf("$: ");

0 commit comments

Comments
 (0)