diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 1cd8c79..7e4b85c 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -9,7 +9,6 @@ #define OFFSET_2 40 void remapPIC(int offset1, int offset2) { - printf("remapping PIC\n"); outb(0x20, 0x11); outb(0xA0, 0x11); outb(0x21, 0x20); @@ -23,7 +22,6 @@ } void setupIRQs() { - printf("setting IRQs\n"); setInterrupt(OFFSET_1 + 0, &irqHandler0); setInterrupt(OFFSET_1 + 1, &irqHandler1); setInterrupt(OFFSET_1 + 2, &irqHandler2); diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 1cd8c79..7e4b85c 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -9,7 +9,6 @@ #define OFFSET_2 40 void remapPIC(int offset1, int offset2) { - printf("remapping PIC\n"); outb(0x20, 0x11); outb(0xA0, 0x11); outb(0x21, 0x20); @@ -23,7 +22,6 @@ } void setupIRQs() { - printf("setting IRQs\n"); setInterrupt(OFFSET_1 + 0, &irqHandler0); setInterrupt(OFFSET_1 + 1, &irqHandler1); setInterrupt(OFFSET_1 + 2, &irqHandler2); diff --git a/src/kernel/drivers/pci/hardDrive/hardDrive.c b/src/kernel/drivers/pci/hardDrive/hardDrive.c index 47205be..6ff03fd 100644 --- a/src/kernel/drivers/pci/hardDrive/hardDrive.c +++ b/src/kernel/drivers/pci/hardDrive/hardDrive.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -21,12 +22,5 @@ break; } } - void *buffer = malloc(512); - for (ListElement *current = drives; current; current = current->next) { - HardDrive *hardDrive = current->data; - hardDrive->access(hardDrive, 0, buffer, 1, 0); - printf("data: %x %x %s\n", *(uint32_t *)buffer, - *((uint32_t *)buffer + 1), buffer); - } - free(buffer); + mountDisk(listGet(&drives, 1)); } diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 1cd8c79..7e4b85c 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -9,7 +9,6 @@ #define OFFSET_2 40 void remapPIC(int offset1, int offset2) { - printf("remapping PIC\n"); outb(0x20, 0x11); outb(0xA0, 0x11); outb(0x21, 0x20); @@ -23,7 +22,6 @@ } void setupIRQs() { - printf("setting IRQs\n"); setInterrupt(OFFSET_1 + 0, &irqHandler0); setInterrupt(OFFSET_1 + 1, &irqHandler1); setInterrupt(OFFSET_1 + 2, &irqHandler2); diff --git a/src/kernel/drivers/pci/hardDrive/hardDrive.c b/src/kernel/drivers/pci/hardDrive/hardDrive.c index 47205be..6ff03fd 100644 --- a/src/kernel/drivers/pci/hardDrive/hardDrive.c +++ b/src/kernel/drivers/pci/hardDrive/hardDrive.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -21,12 +22,5 @@ break; } } - void *buffer = malloc(512); - for (ListElement *current = drives; current; current = current->next) { - HardDrive *hardDrive = current->data; - hardDrive->access(hardDrive, 0, buffer, 1, 0); - printf("data: %x %x %s\n", *(uint32_t *)buffer, - *((uint32_t *)buffer + 1), buffer); - } - free(buffer); + mountDisk(listGet(&drives, 1)); } diff --git a/src/kernel/drivers/pci/hardDrive/ideController.c b/src/kernel/drivers/pci/hardDrive/ideController.c index f7d2c5d..e2f65be 100644 --- a/src/kernel/drivers/pci/hardDrive/ideController.c +++ b/src/kernel/drivers/pci/hardDrive/ideController.c @@ -100,6 +100,7 @@ uint16_t status = ideReadRegister(channel, IDE_STATUS_REGISTER); while (status & IDE_STATUS_BUSY) { status = ideReadRegister(channel, IDE_STATUS_REGISTER); + yields(); } if (checkErrors && status & IDE_STATUS_ERROR) { printf("encountered an error\n"); @@ -126,19 +127,11 @@ lbaIo[1] = (lba & 0x0000FF00) >> 8; lbaIo[2] = (lba & 0x00FF0000) >> 16; lbaIo[3] = (lba & 0xFF000000) >> 24; - } else if (interface->capabilities & 0x200) { + } else { accessMode = 1; - lbaIo[0] = (uint8_t)lba; + lbaIo[0] = lba & 0xFF; lbaIo[1] = (lba & 0x000FF00) >> 8; lbaIo[2] = (lba & 0x0FF0000) >> 16; - head = (lba & 0xF000000) >> 24; - } else { - sector = (lba % 63) + 1; - cylinder = (lba + 1 - sector) / (16 * 63); - lbaIo[0] = sector; - lbaIo[1] = (uint8_t)cylinder; - lbaIo[2] = (cylinder >> 8) & 0xFF; - head = (lba + 1 - sector) % (16 * 63) / (63); } ideWaitUntilNotBusy(interface->channels, true); if (accessMode == 0) { @@ -181,7 +174,7 @@ void *current = buffer; for (uint8_t i = 0; i < sectorCount; i++) { ideWaitUntilNotBusy(interface->channels, true); - ideReadBuffer(interface->channels, (void *)buffer + 256 * i); + ideReadBuffer(interface->channels, (void *)buffer + 512 * i); } } } diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 1cd8c79..7e4b85c 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -9,7 +9,6 @@ #define OFFSET_2 40 void remapPIC(int offset1, int offset2) { - printf("remapping PIC\n"); outb(0x20, 0x11); outb(0xA0, 0x11); outb(0x21, 0x20); @@ -23,7 +22,6 @@ } void setupIRQs() { - printf("setting IRQs\n"); setInterrupt(OFFSET_1 + 0, &irqHandler0); setInterrupt(OFFSET_1 + 1, &irqHandler1); setInterrupt(OFFSET_1 + 2, &irqHandler2); diff --git a/src/kernel/drivers/pci/hardDrive/hardDrive.c b/src/kernel/drivers/pci/hardDrive/hardDrive.c index 47205be..6ff03fd 100644 --- a/src/kernel/drivers/pci/hardDrive/hardDrive.c +++ b/src/kernel/drivers/pci/hardDrive/hardDrive.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -21,12 +22,5 @@ break; } } - void *buffer = malloc(512); - for (ListElement *current = drives; current; current = current->next) { - HardDrive *hardDrive = current->data; - hardDrive->access(hardDrive, 0, buffer, 1, 0); - printf("data: %x %x %s\n", *(uint32_t *)buffer, - *((uint32_t *)buffer + 1), buffer); - } - free(buffer); + mountDisk(listGet(&drives, 1)); } diff --git a/src/kernel/drivers/pci/hardDrive/ideController.c b/src/kernel/drivers/pci/hardDrive/ideController.c index f7d2c5d..e2f65be 100644 --- a/src/kernel/drivers/pci/hardDrive/ideController.c +++ b/src/kernel/drivers/pci/hardDrive/ideController.c @@ -100,6 +100,7 @@ uint16_t status = ideReadRegister(channel, IDE_STATUS_REGISTER); while (status & IDE_STATUS_BUSY) { status = ideReadRegister(channel, IDE_STATUS_REGISTER); + yields(); } if (checkErrors && status & IDE_STATUS_ERROR) { printf("encountered an error\n"); @@ -126,19 +127,11 @@ lbaIo[1] = (lba & 0x0000FF00) >> 8; lbaIo[2] = (lba & 0x00FF0000) >> 16; lbaIo[3] = (lba & 0xFF000000) >> 24; - } else if (interface->capabilities & 0x200) { + } else { accessMode = 1; - lbaIo[0] = (uint8_t)lba; + lbaIo[0] = lba & 0xFF; lbaIo[1] = (lba & 0x000FF00) >> 8; lbaIo[2] = (lba & 0x0FF0000) >> 16; - head = (lba & 0xF000000) >> 24; - } else { - sector = (lba % 63) + 1; - cylinder = (lba + 1 - sector) / (16 * 63); - lbaIo[0] = sector; - lbaIo[1] = (uint8_t)cylinder; - lbaIo[2] = (cylinder >> 8) & 0xFF; - head = (lba + 1 - sector) % (16 * 63) / (63); } ideWaitUntilNotBusy(interface->channels, true); if (accessMode == 0) { @@ -181,7 +174,7 @@ void *current = buffer; for (uint8_t i = 0; i < sectorCount; i++) { ideWaitUntilNotBusy(interface->channels, true); - ideReadBuffer(interface->channels, (void *)buffer + 256 * i); + ideReadBuffer(interface->channels, (void *)buffer + 512 * i); } } } diff --git a/src/kernel/drivers/pci/pci.c b/src/kernel/drivers/pci/pci.c index 96cbfc3..8068f5d 100644 --- a/src/kernel/drivers/pci/pci.c +++ b/src/kernel/drivers/pci/pci.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -143,4 +144,5 @@ } } printf("enumerated %i pci devices\n", listCount(pciDevices)); + scanHardDrives(); } diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 1cd8c79..7e4b85c 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -9,7 +9,6 @@ #define OFFSET_2 40 void remapPIC(int offset1, int offset2) { - printf("remapping PIC\n"); outb(0x20, 0x11); outb(0xA0, 0x11); outb(0x21, 0x20); @@ -23,7 +22,6 @@ } void setupIRQs() { - printf("setting IRQs\n"); setInterrupt(OFFSET_1 + 0, &irqHandler0); setInterrupt(OFFSET_1 + 1, &irqHandler1); setInterrupt(OFFSET_1 + 2, &irqHandler2); diff --git a/src/kernel/drivers/pci/hardDrive/hardDrive.c b/src/kernel/drivers/pci/hardDrive/hardDrive.c index 47205be..6ff03fd 100644 --- a/src/kernel/drivers/pci/hardDrive/hardDrive.c +++ b/src/kernel/drivers/pci/hardDrive/hardDrive.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -21,12 +22,5 @@ break; } } - void *buffer = malloc(512); - for (ListElement *current = drives; current; current = current->next) { - HardDrive *hardDrive = current->data; - hardDrive->access(hardDrive, 0, buffer, 1, 0); - printf("data: %x %x %s\n", *(uint32_t *)buffer, - *((uint32_t *)buffer + 1), buffer); - } - free(buffer); + mountDisk(listGet(&drives, 1)); } diff --git a/src/kernel/drivers/pci/hardDrive/ideController.c b/src/kernel/drivers/pci/hardDrive/ideController.c index f7d2c5d..e2f65be 100644 --- a/src/kernel/drivers/pci/hardDrive/ideController.c +++ b/src/kernel/drivers/pci/hardDrive/ideController.c @@ -100,6 +100,7 @@ uint16_t status = ideReadRegister(channel, IDE_STATUS_REGISTER); while (status & IDE_STATUS_BUSY) { status = ideReadRegister(channel, IDE_STATUS_REGISTER); + yields(); } if (checkErrors && status & IDE_STATUS_ERROR) { printf("encountered an error\n"); @@ -126,19 +127,11 @@ lbaIo[1] = (lba & 0x0000FF00) >> 8; lbaIo[2] = (lba & 0x00FF0000) >> 16; lbaIo[3] = (lba & 0xFF000000) >> 24; - } else if (interface->capabilities & 0x200) { + } else { accessMode = 1; - lbaIo[0] = (uint8_t)lba; + lbaIo[0] = lba & 0xFF; lbaIo[1] = (lba & 0x000FF00) >> 8; lbaIo[2] = (lba & 0x0FF0000) >> 16; - head = (lba & 0xF000000) >> 24; - } else { - sector = (lba % 63) + 1; - cylinder = (lba + 1 - sector) / (16 * 63); - lbaIo[0] = sector; - lbaIo[1] = (uint8_t)cylinder; - lbaIo[2] = (cylinder >> 8) & 0xFF; - head = (lba + 1 - sector) % (16 * 63) / (63); } ideWaitUntilNotBusy(interface->channels, true); if (accessMode == 0) { @@ -181,7 +174,7 @@ void *current = buffer; for (uint8_t i = 0; i < sectorCount; i++) { ideWaitUntilNotBusy(interface->channels, true); - ideReadBuffer(interface->channels, (void *)buffer + 256 * i); + ideReadBuffer(interface->channels, (void *)buffer + 512 * i); } } } diff --git a/src/kernel/drivers/pci/pci.c b/src/kernel/drivers/pci/pci.c index 96cbfc3..8068f5d 100644 --- a/src/kernel/drivers/pci/pci.c +++ b/src/kernel/drivers/pci/pci.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -143,4 +144,5 @@ } } printf("enumerated %i pci devices\n", listCount(pciDevices)); + scanHardDrives(); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 167035c..240870a 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -159,11 +159,3 @@ listAdd(&getPrinterTask()->messages, message); schedule(getPrinterTask()); } - -uint32_t strlen(char *string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 1cd8c79..7e4b85c 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -9,7 +9,6 @@ #define OFFSET_2 40 void remapPIC(int offset1, int offset2) { - printf("remapping PIC\n"); outb(0x20, 0x11); outb(0xA0, 0x11); outb(0x21, 0x20); @@ -23,7 +22,6 @@ } void setupIRQs() { - printf("setting IRQs\n"); setInterrupt(OFFSET_1 + 0, &irqHandler0); setInterrupt(OFFSET_1 + 1, &irqHandler1); setInterrupt(OFFSET_1 + 2, &irqHandler2); diff --git a/src/kernel/drivers/pci/hardDrive/hardDrive.c b/src/kernel/drivers/pci/hardDrive/hardDrive.c index 47205be..6ff03fd 100644 --- a/src/kernel/drivers/pci/hardDrive/hardDrive.c +++ b/src/kernel/drivers/pci/hardDrive/hardDrive.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -21,12 +22,5 @@ break; } } - void *buffer = malloc(512); - for (ListElement *current = drives; current; current = current->next) { - HardDrive *hardDrive = current->data; - hardDrive->access(hardDrive, 0, buffer, 1, 0); - printf("data: %x %x %s\n", *(uint32_t *)buffer, - *((uint32_t *)buffer + 1), buffer); - } - free(buffer); + mountDisk(listGet(&drives, 1)); } diff --git a/src/kernel/drivers/pci/hardDrive/ideController.c b/src/kernel/drivers/pci/hardDrive/ideController.c index f7d2c5d..e2f65be 100644 --- a/src/kernel/drivers/pci/hardDrive/ideController.c +++ b/src/kernel/drivers/pci/hardDrive/ideController.c @@ -100,6 +100,7 @@ uint16_t status = ideReadRegister(channel, IDE_STATUS_REGISTER); while (status & IDE_STATUS_BUSY) { status = ideReadRegister(channel, IDE_STATUS_REGISTER); + yields(); } if (checkErrors && status & IDE_STATUS_ERROR) { printf("encountered an error\n"); @@ -126,19 +127,11 @@ lbaIo[1] = (lba & 0x0000FF00) >> 8; lbaIo[2] = (lba & 0x00FF0000) >> 16; lbaIo[3] = (lba & 0xFF000000) >> 24; - } else if (interface->capabilities & 0x200) { + } else { accessMode = 1; - lbaIo[0] = (uint8_t)lba; + lbaIo[0] = lba & 0xFF; lbaIo[1] = (lba & 0x000FF00) >> 8; lbaIo[2] = (lba & 0x0FF0000) >> 16; - head = (lba & 0xF000000) >> 24; - } else { - sector = (lba % 63) + 1; - cylinder = (lba + 1 - sector) / (16 * 63); - lbaIo[0] = sector; - lbaIo[1] = (uint8_t)cylinder; - lbaIo[2] = (cylinder >> 8) & 0xFF; - head = (lba + 1 - sector) % (16 * 63) / (63); } ideWaitUntilNotBusy(interface->channels, true); if (accessMode == 0) { @@ -181,7 +174,7 @@ void *current = buffer; for (uint8_t i = 0; i < sectorCount; i++) { ideWaitUntilNotBusy(interface->channels, true); - ideReadBuffer(interface->channels, (void *)buffer + 256 * i); + ideReadBuffer(interface->channels, (void *)buffer + 512 * i); } } } diff --git a/src/kernel/drivers/pci/pci.c b/src/kernel/drivers/pci/pci.c index 96cbfc3..8068f5d 100644 --- a/src/kernel/drivers/pci/pci.c +++ b/src/kernel/drivers/pci/pci.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -143,4 +144,5 @@ } } printf("enumerated %i pci devices\n", listCount(pciDevices)); + scanHardDrives(); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 167035c..240870a 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -159,11 +159,3 @@ listAdd(&getPrinterTask()->messages, message); schedule(getPrinterTask()); } - -uint32_t strlen(char *string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index c7c1d50..6edb5e1 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -20,11 +21,10 @@ getCurrentTask()->ticksLeft = 1000; drawLogo(); yields(); - setupDevices(); - setupInterrupts(); printCPUData(); - scanPCIDevices(); - scanHardDrives(); + setupDevices(); + printf("file system tree:\n"); + printFileSystemTree(); while (1) { for (Message *message = popBeginning(&(getCurrentTask()->messages)); message; message = popBeginning(&getCurrentTask()->messages)) { diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 1cd8c79..7e4b85c 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -9,7 +9,6 @@ #define OFFSET_2 40 void remapPIC(int offset1, int offset2) { - printf("remapping PIC\n"); outb(0x20, 0x11); outb(0xA0, 0x11); outb(0x21, 0x20); @@ -23,7 +22,6 @@ } void setupIRQs() { - printf("setting IRQs\n"); setInterrupt(OFFSET_1 + 0, &irqHandler0); setInterrupt(OFFSET_1 + 1, &irqHandler1); setInterrupt(OFFSET_1 + 2, &irqHandler2); diff --git a/src/kernel/drivers/pci/hardDrive/hardDrive.c b/src/kernel/drivers/pci/hardDrive/hardDrive.c index 47205be..6ff03fd 100644 --- a/src/kernel/drivers/pci/hardDrive/hardDrive.c +++ b/src/kernel/drivers/pci/hardDrive/hardDrive.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -21,12 +22,5 @@ break; } } - void *buffer = malloc(512); - for (ListElement *current = drives; current; current = current->next) { - HardDrive *hardDrive = current->data; - hardDrive->access(hardDrive, 0, buffer, 1, 0); - printf("data: %x %x %s\n", *(uint32_t *)buffer, - *((uint32_t *)buffer + 1), buffer); - } - free(buffer); + mountDisk(listGet(&drives, 1)); } diff --git a/src/kernel/drivers/pci/hardDrive/ideController.c b/src/kernel/drivers/pci/hardDrive/ideController.c index f7d2c5d..e2f65be 100644 --- a/src/kernel/drivers/pci/hardDrive/ideController.c +++ b/src/kernel/drivers/pci/hardDrive/ideController.c @@ -100,6 +100,7 @@ uint16_t status = ideReadRegister(channel, IDE_STATUS_REGISTER); while (status & IDE_STATUS_BUSY) { status = ideReadRegister(channel, IDE_STATUS_REGISTER); + yields(); } if (checkErrors && status & IDE_STATUS_ERROR) { printf("encountered an error\n"); @@ -126,19 +127,11 @@ lbaIo[1] = (lba & 0x0000FF00) >> 8; lbaIo[2] = (lba & 0x00FF0000) >> 16; lbaIo[3] = (lba & 0xFF000000) >> 24; - } else if (interface->capabilities & 0x200) { + } else { accessMode = 1; - lbaIo[0] = (uint8_t)lba; + lbaIo[0] = lba & 0xFF; lbaIo[1] = (lba & 0x000FF00) >> 8; lbaIo[2] = (lba & 0x0FF0000) >> 16; - head = (lba & 0xF000000) >> 24; - } else { - sector = (lba % 63) + 1; - cylinder = (lba + 1 - sector) / (16 * 63); - lbaIo[0] = sector; - lbaIo[1] = (uint8_t)cylinder; - lbaIo[2] = (cylinder >> 8) & 0xFF; - head = (lba + 1 - sector) % (16 * 63) / (63); } ideWaitUntilNotBusy(interface->channels, true); if (accessMode == 0) { @@ -181,7 +174,7 @@ void *current = buffer; for (uint8_t i = 0; i < sectorCount; i++) { ideWaitUntilNotBusy(interface->channels, true); - ideReadBuffer(interface->channels, (void *)buffer + 256 * i); + ideReadBuffer(interface->channels, (void *)buffer + 512 * i); } } } diff --git a/src/kernel/drivers/pci/pci.c b/src/kernel/drivers/pci/pci.c index 96cbfc3..8068f5d 100644 --- a/src/kernel/drivers/pci/pci.c +++ b/src/kernel/drivers/pci/pci.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -143,4 +144,5 @@ } } printf("enumerated %i pci devices\n", listCount(pciDevices)); + scanHardDrives(); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 167035c..240870a 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -159,11 +159,3 @@ listAdd(&getPrinterTask()->messages, message); schedule(getPrinterTask()); } - -uint32_t strlen(char *string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index c7c1d50..6edb5e1 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -20,11 +21,10 @@ getCurrentTask()->ticksLeft = 1000; drawLogo(); yields(); - setupDevices(); - setupInterrupts(); printCPUData(); - scanPCIDevices(); - scanHardDrives(); + setupDevices(); + printf("file system tree:\n"); + printFileSystemTree(); while (1) { for (Message *message = popBeginning(&(getCurrentTask()->messages)); message; message = popBeginning(&getCurrentTask()->messages)) { diff --git a/src/kernel/memory/fileSystem.c b/src/kernel/memory/fileSystem.c new file mode 100644 index 0000000..88589ef --- /dev/null +++ b/src/kernel/memory/fileSystem.c @@ -0,0 +1,215 @@ +#include <_stdio.h> +#include +#include +#include + +FileSystemFolder rootFolder = { + .name = "/", .type = FS_FOLDER, .children = NULL}; + +void printFolderTree(FileSystemFolder *folder, char *prefix) { + char *nextPrefix = stringCombine(prefix, " "); + for (ListElement *current = *folder->children; current; + current = current->next) { + FileSystemTreeNode *child = current->data; + printf("%s%s\n", prefix, child->name); + if (child->type == FS_FOLDER) { + if (child->name[0] == '.') { + continue; + } + printFolderTree(current->data, nextPrefix); + } + } + free(nextPrefix); +} + +void printFileSystemTree() { printFolderTree(&rootFolder, ""); } + +void setupFatInfo(FatInfo *info, FatBootSector *boot, HardDrive *hardDrive) { + info->bootSector = boot; + info->rootDirectorySize = + (boot->rootEntryCount * 32 + boot->sectorSize - 1) / boot->sectorSize; + info->fatSize = boot->fatSize16Bit ? boot->fatSize16Bit : 0; + info->totalSectorCount = boot->totalSectorCount16Bit + ? boot->totalSectorCount16Bit + : boot->totalSectorCount32Bit; + uint32_t dataSectorCount = + info->totalSectorCount - + (boot->reservedSectorCount + (boot->FATCount * info->fatSize) + + info->rootDirectorySize); + uint32_t clusterCount = dataSectorCount / boot->sectorsPerCluster; + if (clusterCount < 4085) { + info->version = 12; + } else if (clusterCount < 65525) { + info->version = 16; + } else { + info->version = 32; + } + info->firstDataSector = boot->reservedSectorCount + + (boot->FATCount * info->fatSize) + + info->rootDirectorySize; + info->firstFatSector = boot->reservedSectorCount; + info->firstRootSector = info->firstDataSector - info->rootDirectorySize; + info->fat = malloc(info->fatSize * boot->sectorSize); + info->hardDrive = hardDrive; + hardDrive->access(hardDrive, info->firstFatSector, info->fat, info->fatSize, + 0); + printf("size: %i\n", info->fatSize * info->bootSector->sectorSize * + sizeof(void *) / info->version / 8); + info->knownFolders = malloc(info->fatSize * info->bootSector->sectorSize / + sizeof(void *) * info->version / 8); +} + +uint32_t getNextCluster(FatInfo *info, uint32_t cluster) { + switch (info->version) { + case 16: + return ((uint16_t *)info->fat)[cluster]; + } + return 00; +} + +bool isClusterEnd(FatInfo *info, uint32_t cluster) { + switch (info->version) { + case 16: + return cluster == 0xFFFF; + } + return true; +} + +char *getFullString(LongNameEntry *name) { + char *string = malloc(14); + string[14] = 0; + for (uint8_t i = 0; i < 5; i++) { + string[i] = name->name1[i * 2]; + } + for (uint8_t i = 0; i < 6; i++) { + string[5 + i] = name->name2[i * 2]; + } + for (uint8_t i = 0; i < 2; i++) { + string[11 + i] = name->name3[i * 2]; + } + return string; +} + +char *getCombinedName(ListElement **longName) { + char *current = "", *new; + while (*longName != NULL) { + new = stringCombine(popBeginning(longName), current); + if (strlen(current)) { + free(current); + } + current = new; + } + return current; +} + +char *getName(ListElement **longName, FatDirectoryEntry *directory) { + if (listCount(*longName)) { + return getCombinedName(longName); + } + char *name = malloc(12); // 8:3 short name + name[11] = 0; + for (uint8_t i = 0; i < 11; i++) { + name[i] = directory->name[i]; + } + return name; +} + +uint32_t getClusterSector(FatInfo *info, uint32_t cluster) { + return info->firstDataSector + + ((cluster - 2) * info->bootSector->sectorsPerCluster); +} + +void folderScanCluster(FatInfo *info, uint32_t cluster, + FileSystemFolder *parent); + +void setupFolder(char *name, FatDirectoryEntry *directory, FatInfo *info, + FileSystemFolder *parent) { + FileSystemFolder *folder = malloc(sizeof(FileSystemFolder)); + folder->name = name; + folder->type = FS_FOLDER; + uint32_t cluster = directory->firstDataClusterLower | + directory->fistDataClusterUpper << 16; + listAdd(parent->children, folder); + if (info->knownFolders[cluster]) { + folder->children = info->knownFolders[cluster]; + return; + } + if (cluster == 0) { + folder->children = rootFolder.children; + return; + } + folder->children = malloc(sizeof(ListElement **)); + info->knownFolders[cluster] = folder->children; + *folder->children = NULL; + folderScanCluster(info, getClusterSector(info, cluster), folder); + while (!isClusterEnd(info, cluster)) { + cluster = getNextCluster(info, cluster); + if (cluster > 0xFF00) { + return; + } + folderScanCluster(info, getClusterSector(info, cluster), folder); + } +} + +void setupFile(char *name, FileSystemFolder *parent) { + FileSystemFile *file = malloc(sizeof(FileSystemFile)); + file->name = name; + file->type = FS_FILE; + file->subType = 0; + listAdd(parent->children, file); +} + +void setupChild(char *name, FatDirectoryEntry *directoryEntry, FatInfo *info, + FileSystemFolder *parent) { + if (directoryEntry->attributes & FAT_ATTRIBUTE_VOLUME_ID) { + info->volumeID = name; + return; + } + if (directoryEntry->attributes & FAT_ATTRIBUTE_DIRECTORY) { + setupFolder(name, directoryEntry, info, parent); + } else { + setupFile(name, parent); + } +} + +void folderScanCluster(FatInfo *info, uint32_t sector, + FileSystemFolder *parent) { + void *clusterData = malloc(info->bootSector->sectorSize * + info->bootSector->sectorsPerCluster); + info->hardDrive->access(info->hardDrive, sector, clusterData, + info->bootSector->sectorsPerCluster, 0); + FatDirectoryEntry *directory = clusterData; + ListElement *longName = NULL; + uint32_t i; + for (i = 0; i < info->bootSector->sectorSize * + info->bootSector->sectorsPerCluster / + sizeof(FatDirectoryEntry); + i++) { + if (directory[i].name[0] == 0) { + break; + } + if (directory[i].name[0] == (char)0xE5 || + directory[i].name[0] == (char)0xFF) { + continue; + } + if (directory[i].attributes == FAT_ATTRIBUTE_LONG_NAME) { + listAdd(&longName, getFullString((void *)&(directory[i]))); + continue; + } + setupChild(getName(&longName, &directory[i]), &directory[i], info, + parent); + } + listClear(&longName, true); + free(clusterData); +} + +void mountDisk(HardDrive *drive) { + void *bootSector = malloc(512); + drive->access(drive, 0, bootSector, 1, 0); + FatBootSector *boot = bootSector; + FatInfo *info = malloc(sizeof(FatInfo)); + setupFatInfo(info, boot, drive); + printf("type: FAT%i\n", info->version); + rootFolder.children = malloc(sizeof(ListElement *)); + folderScanCluster(info, info->firstRootSector, &rootFolder); +} diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 1cd8c79..7e4b85c 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -9,7 +9,6 @@ #define OFFSET_2 40 void remapPIC(int offset1, int offset2) { - printf("remapping PIC\n"); outb(0x20, 0x11); outb(0xA0, 0x11); outb(0x21, 0x20); @@ -23,7 +22,6 @@ } void setupIRQs() { - printf("setting IRQs\n"); setInterrupt(OFFSET_1 + 0, &irqHandler0); setInterrupt(OFFSET_1 + 1, &irqHandler1); setInterrupt(OFFSET_1 + 2, &irqHandler2); diff --git a/src/kernel/drivers/pci/hardDrive/hardDrive.c b/src/kernel/drivers/pci/hardDrive/hardDrive.c index 47205be..6ff03fd 100644 --- a/src/kernel/drivers/pci/hardDrive/hardDrive.c +++ b/src/kernel/drivers/pci/hardDrive/hardDrive.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -21,12 +22,5 @@ break; } } - void *buffer = malloc(512); - for (ListElement *current = drives; current; current = current->next) { - HardDrive *hardDrive = current->data; - hardDrive->access(hardDrive, 0, buffer, 1, 0); - printf("data: %x %x %s\n", *(uint32_t *)buffer, - *((uint32_t *)buffer + 1), buffer); - } - free(buffer); + mountDisk(listGet(&drives, 1)); } diff --git a/src/kernel/drivers/pci/hardDrive/ideController.c b/src/kernel/drivers/pci/hardDrive/ideController.c index f7d2c5d..e2f65be 100644 --- a/src/kernel/drivers/pci/hardDrive/ideController.c +++ b/src/kernel/drivers/pci/hardDrive/ideController.c @@ -100,6 +100,7 @@ uint16_t status = ideReadRegister(channel, IDE_STATUS_REGISTER); while (status & IDE_STATUS_BUSY) { status = ideReadRegister(channel, IDE_STATUS_REGISTER); + yields(); } if (checkErrors && status & IDE_STATUS_ERROR) { printf("encountered an error\n"); @@ -126,19 +127,11 @@ lbaIo[1] = (lba & 0x0000FF00) >> 8; lbaIo[2] = (lba & 0x00FF0000) >> 16; lbaIo[3] = (lba & 0xFF000000) >> 24; - } else if (interface->capabilities & 0x200) { + } else { accessMode = 1; - lbaIo[0] = (uint8_t)lba; + lbaIo[0] = lba & 0xFF; lbaIo[1] = (lba & 0x000FF00) >> 8; lbaIo[2] = (lba & 0x0FF0000) >> 16; - head = (lba & 0xF000000) >> 24; - } else { - sector = (lba % 63) + 1; - cylinder = (lba + 1 - sector) / (16 * 63); - lbaIo[0] = sector; - lbaIo[1] = (uint8_t)cylinder; - lbaIo[2] = (cylinder >> 8) & 0xFF; - head = (lba + 1 - sector) % (16 * 63) / (63); } ideWaitUntilNotBusy(interface->channels, true); if (accessMode == 0) { @@ -181,7 +174,7 @@ void *current = buffer; for (uint8_t i = 0; i < sectorCount; i++) { ideWaitUntilNotBusy(interface->channels, true); - ideReadBuffer(interface->channels, (void *)buffer + 256 * i); + ideReadBuffer(interface->channels, (void *)buffer + 512 * i); } } } diff --git a/src/kernel/drivers/pci/pci.c b/src/kernel/drivers/pci/pci.c index 96cbfc3..8068f5d 100644 --- a/src/kernel/drivers/pci/pci.c +++ b/src/kernel/drivers/pci/pci.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -143,4 +144,5 @@ } } printf("enumerated %i pci devices\n", listCount(pciDevices)); + scanHardDrives(); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 167035c..240870a 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -159,11 +159,3 @@ listAdd(&getPrinterTask()->messages, message); schedule(getPrinterTask()); } - -uint32_t strlen(char *string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index c7c1d50..6edb5e1 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -20,11 +21,10 @@ getCurrentTask()->ticksLeft = 1000; drawLogo(); yields(); - setupDevices(); - setupInterrupts(); printCPUData(); - scanPCIDevices(); - scanHardDrives(); + setupDevices(); + printf("file system tree:\n"); + printFileSystemTree(); while (1) { for (Message *message = popBeginning(&(getCurrentTask()->messages)); message; message = popBeginning(&getCurrentTask()->messages)) { diff --git a/src/kernel/memory/fileSystem.c b/src/kernel/memory/fileSystem.c new file mode 100644 index 0000000..88589ef --- /dev/null +++ b/src/kernel/memory/fileSystem.c @@ -0,0 +1,215 @@ +#include <_stdio.h> +#include +#include +#include + +FileSystemFolder rootFolder = { + .name = "/", .type = FS_FOLDER, .children = NULL}; + +void printFolderTree(FileSystemFolder *folder, char *prefix) { + char *nextPrefix = stringCombine(prefix, " "); + for (ListElement *current = *folder->children; current; + current = current->next) { + FileSystemTreeNode *child = current->data; + printf("%s%s\n", prefix, child->name); + if (child->type == FS_FOLDER) { + if (child->name[0] == '.') { + continue; + } + printFolderTree(current->data, nextPrefix); + } + } + free(nextPrefix); +} + +void printFileSystemTree() { printFolderTree(&rootFolder, ""); } + +void setupFatInfo(FatInfo *info, FatBootSector *boot, HardDrive *hardDrive) { + info->bootSector = boot; + info->rootDirectorySize = + (boot->rootEntryCount * 32 + boot->sectorSize - 1) / boot->sectorSize; + info->fatSize = boot->fatSize16Bit ? boot->fatSize16Bit : 0; + info->totalSectorCount = boot->totalSectorCount16Bit + ? boot->totalSectorCount16Bit + : boot->totalSectorCount32Bit; + uint32_t dataSectorCount = + info->totalSectorCount - + (boot->reservedSectorCount + (boot->FATCount * info->fatSize) + + info->rootDirectorySize); + uint32_t clusterCount = dataSectorCount / boot->sectorsPerCluster; + if (clusterCount < 4085) { + info->version = 12; + } else if (clusterCount < 65525) { + info->version = 16; + } else { + info->version = 32; + } + info->firstDataSector = boot->reservedSectorCount + + (boot->FATCount * info->fatSize) + + info->rootDirectorySize; + info->firstFatSector = boot->reservedSectorCount; + info->firstRootSector = info->firstDataSector - info->rootDirectorySize; + info->fat = malloc(info->fatSize * boot->sectorSize); + info->hardDrive = hardDrive; + hardDrive->access(hardDrive, info->firstFatSector, info->fat, info->fatSize, + 0); + printf("size: %i\n", info->fatSize * info->bootSector->sectorSize * + sizeof(void *) / info->version / 8); + info->knownFolders = malloc(info->fatSize * info->bootSector->sectorSize / + sizeof(void *) * info->version / 8); +} + +uint32_t getNextCluster(FatInfo *info, uint32_t cluster) { + switch (info->version) { + case 16: + return ((uint16_t *)info->fat)[cluster]; + } + return 00; +} + +bool isClusterEnd(FatInfo *info, uint32_t cluster) { + switch (info->version) { + case 16: + return cluster == 0xFFFF; + } + return true; +} + +char *getFullString(LongNameEntry *name) { + char *string = malloc(14); + string[14] = 0; + for (uint8_t i = 0; i < 5; i++) { + string[i] = name->name1[i * 2]; + } + for (uint8_t i = 0; i < 6; i++) { + string[5 + i] = name->name2[i * 2]; + } + for (uint8_t i = 0; i < 2; i++) { + string[11 + i] = name->name3[i * 2]; + } + return string; +} + +char *getCombinedName(ListElement **longName) { + char *current = "", *new; + while (*longName != NULL) { + new = stringCombine(popBeginning(longName), current); + if (strlen(current)) { + free(current); + } + current = new; + } + return current; +} + +char *getName(ListElement **longName, FatDirectoryEntry *directory) { + if (listCount(*longName)) { + return getCombinedName(longName); + } + char *name = malloc(12); // 8:3 short name + name[11] = 0; + for (uint8_t i = 0; i < 11; i++) { + name[i] = directory->name[i]; + } + return name; +} + +uint32_t getClusterSector(FatInfo *info, uint32_t cluster) { + return info->firstDataSector + + ((cluster - 2) * info->bootSector->sectorsPerCluster); +} + +void folderScanCluster(FatInfo *info, uint32_t cluster, + FileSystemFolder *parent); + +void setupFolder(char *name, FatDirectoryEntry *directory, FatInfo *info, + FileSystemFolder *parent) { + FileSystemFolder *folder = malloc(sizeof(FileSystemFolder)); + folder->name = name; + folder->type = FS_FOLDER; + uint32_t cluster = directory->firstDataClusterLower | + directory->fistDataClusterUpper << 16; + listAdd(parent->children, folder); + if (info->knownFolders[cluster]) { + folder->children = info->knownFolders[cluster]; + return; + } + if (cluster == 0) { + folder->children = rootFolder.children; + return; + } + folder->children = malloc(sizeof(ListElement **)); + info->knownFolders[cluster] = folder->children; + *folder->children = NULL; + folderScanCluster(info, getClusterSector(info, cluster), folder); + while (!isClusterEnd(info, cluster)) { + cluster = getNextCluster(info, cluster); + if (cluster > 0xFF00) { + return; + } + folderScanCluster(info, getClusterSector(info, cluster), folder); + } +} + +void setupFile(char *name, FileSystemFolder *parent) { + FileSystemFile *file = malloc(sizeof(FileSystemFile)); + file->name = name; + file->type = FS_FILE; + file->subType = 0; + listAdd(parent->children, file); +} + +void setupChild(char *name, FatDirectoryEntry *directoryEntry, FatInfo *info, + FileSystemFolder *parent) { + if (directoryEntry->attributes & FAT_ATTRIBUTE_VOLUME_ID) { + info->volumeID = name; + return; + } + if (directoryEntry->attributes & FAT_ATTRIBUTE_DIRECTORY) { + setupFolder(name, directoryEntry, info, parent); + } else { + setupFile(name, parent); + } +} + +void folderScanCluster(FatInfo *info, uint32_t sector, + FileSystemFolder *parent) { + void *clusterData = malloc(info->bootSector->sectorSize * + info->bootSector->sectorsPerCluster); + info->hardDrive->access(info->hardDrive, sector, clusterData, + info->bootSector->sectorsPerCluster, 0); + FatDirectoryEntry *directory = clusterData; + ListElement *longName = NULL; + uint32_t i; + for (i = 0; i < info->bootSector->sectorSize * + info->bootSector->sectorsPerCluster / + sizeof(FatDirectoryEntry); + i++) { + if (directory[i].name[0] == 0) { + break; + } + if (directory[i].name[0] == (char)0xE5 || + directory[i].name[0] == (char)0xFF) { + continue; + } + if (directory[i].attributes == FAT_ATTRIBUTE_LONG_NAME) { + listAdd(&longName, getFullString((void *)&(directory[i]))); + continue; + } + setupChild(getName(&longName, &directory[i]), &directory[i], info, + parent); + } + listClear(&longName, true); + free(clusterData); +} + +void mountDisk(HardDrive *drive) { + void *bootSector = malloc(512); + drive->access(drive, 0, bootSector, 1, 0); + FatBootSector *boot = bootSector; + FatInfo *info = malloc(sizeof(FatInfo)); + setupFatInfo(info, boot, drive); + printf("type: FAT%i\n", info->version); + rootFolder.children = malloc(sizeof(ListElement *)); + folderScanCluster(info, info->firstRootSector, &rootFolder); +} diff --git a/src/kernel/util/list.c b/src/kernel/util/list.c index 76110c4..c051fce 100644 --- a/src/kernel/util/list.c +++ b/src/kernel/util/list.c @@ -60,7 +60,7 @@ return result; } -void *popEnd(ListElement **list) { +void *listPopEnd(ListElement **list) { if (list == NULL) { return NULL; } @@ -78,3 +78,24 @@ } return result; } + +void *listGet(ListElement **list, uint32_t index) { + ListElement *current = *list; + for (uint32_t i = 0; i < index; i++) { + current = current->next; + } + return current->data; +} + +void listClear(ListElement **list, bool freeData) { + ListElement *current = *list, *next; + while (current) { + next = current->next; + if (freeData) { + free(current->data); + } + free(current); + current = next; + } + *list = NULL; +} diff --git a/compile_flags.txt b/compile_flags.txt index 73386c5..f16cf85 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,3 +1,4 @@ clang -I./src/include -fms-extensions +-Wno-microsoft-anon-tag diff --git a/src/include/_stdio.h b/src/include/_stdio.h index efd084f..81d2663 100644 --- a/src/include/_stdio.h +++ b/src/include/_stdio.h @@ -5,7 +5,6 @@ extern void printf(const char *format, ...); -extern uint32_t strlen(char *string); extern void setTextStyle(uint8_t newFormat); #endif diff --git a/src/include/fileSystem.h b/src/include/fileSystem.h new file mode 100644 index 0000000..65bff6f --- /dev/null +++ b/src/include/fileSystem.h @@ -0,0 +1,92 @@ +#ifndef FILE_SYSTEM_H +#define FILE_SYSTEM_H + +#include + +enum FileSystemEntryTypes { FS_FOLDER, FS_FILE }; + +typedef struct { + uint8_t type; + char *name; +} FileSystemTreeNode; + +typedef struct { + FileSystemTreeNode; + uint8_t subType; +} FileSystemFile; + +typedef struct { + FileSystemTreeNode; + ListElement **children; +} FileSystemFolder; + +typedef struct { + uint8_t jmp[3]; + char oemName[8]; + uint16_t sectorSize; // this is on an odd offset, that's why the packed + // attribute is needed here. + uint8_t sectorsPerCluster; + uint16_t reservedSectorCount; + uint8_t FATCount; + uint16_t rootEntryCount, totalSectorCount16Bit; + uint8_t mediaType; + uint16_t fatSize16Bit, sectorsPerTrack, numberOfHeads; + uint32_t hiddenSectorCount, totalSectorCount32Bit; +} __attribute__((packed)) FatBootSector; + +typedef struct { + FatBootSector; + // todo +} __attribute__((packed)) Fat32BootSector; + +typedef struct { + FatBootSector; + uint8_t driveNumber, reserved, bootSignature; + uint32_t serialNumber; + char volumeLabel[11], fileSystemType[8]; +} __attribute__((packed)) Fat16BootSector; + +typedef struct { + FatBootSector *bootSector; + uint8_t version; + uint32_t fatSize, totalSectorCount, rootDirectorySize, firstDataSector, + firstFatSector, firstRootSector; + char *volumeID; + HardDrive *hardDrive; + void *fat; + ListElement ***knownFolders; +} FatInfo; + +typedef struct { + char name[11]; + uint8_t attributes, reserved, creationTimeTenthsOfSecond; + uint16_t creationTime, creationDate, lastAccessDate; + uint16_t fistDataClusterUpper, lastModifiedTime, lastModifiedDate, + firstDataClusterLower; + uint32_t size; +} __attribute__((packed)) FatDirectoryEntry; + +typedef struct { + uint8_t index; + char name1[10]; + uint8_t attributes, reserved, checksum; + char name2[12]; + uint16_t reserved_; + char name3[4]; +} __attribute__((packed)) LongNameEntry; + +enum FAT_ATTRIBUTES { + FAT_ATTRIBUTE_READ_ONLY = 0x01, + FAT_ATTRIBUTE_HIDDEN = 0x02, + FAT_ATTRIBUTE_SYSTEM_FILE = 0x04, + FAT_ATTRIBUTE_VOLUME_ID = 0x08, + FAT_ATTRIBUTE_DIRECTORY = 0x10, + FAT_ATTRIBUTE_ARCHIVE = 0x20, + FAT_ATTRIBUTE_LONG_NAME = 0x0F, +}; + +extern void mountDisk(HardDrive *drive); + +extern void printFileSystemTree(); + +#endif diff --git a/src/include/list.h b/src/include/list.h index f61708e..54b7560 100644 --- a/src/include/list.h +++ b/src/include/list.h @@ -1,10 +1,14 @@ #ifndef LIST_H #define LIST_H +#include #include #define NULL (void *)0 +#define foreach(list) \ + for (ListElement *current = list; current; current = current->next) + typedef struct ListElement { struct ListElement *next; void *data; @@ -18,4 +22,8 @@ extern void memset(void *buffer, uint8_t byte, uint32_t count); +extern void *listGet(ListElement **list, uint32_t index); +extern void *listPopEnd(ListElement **list); +extern void listClear(ListElement **list, bool freeData); + #endif diff --git a/src/include/util.h b/src/include/util.h index 9a7c7aa..3e3fa2f 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -1,6 +1,11 @@ #ifndef UTIL_H #define UTIL_H +#include + #define max(x, y) (x > y ? (x) : (y)) +extern char *stringCombine(char *string1, char *string2); +extern uint32_t strlen(char *string); + #endif diff --git a/src/kernel/drivers/devices.c b/src/kernel/drivers/devices.c index d4638a7..a6cd8f6 100644 --- a/src/kernel/drivers/devices.c +++ b/src/kernel/drivers/devices.c @@ -1,8 +1,12 @@ #include +#include #include +#include #include void setupDevices() { setupTimer(); setupKeyboard(); -} \ No newline at end of file + setupInterrupts(); + scanPCIDevices(); +} diff --git a/src/kernel/drivers/interrupts/exceptions.c b/src/kernel/drivers/interrupts/exceptions.c index 8d82af9..8c1e7f7 100644 --- a/src/kernel/drivers/interrupts/exceptions.c +++ b/src/kernel/drivers/interrupts/exceptions.c @@ -1,44 +1,41 @@ +#include <_stdio.h> #include #include #include -#include <_stdio.h> -char* EXCEPTION_MESSAGES[] = { - "DIVIDE BY ZERO", - "DEBUG", - "NON-MASKABLE INTERRUPT", - "BREAKPOINT", - "OVERFLOW", - "BOUND RAGE EXCEEDED", - "INVALID OPCODE", - "DEVICE NOT AVAILABLE", - "DOUBLE FAULT", - "COPROCESSOR SEGMENT OVERRUN", - "INVALID TSS", - "SEGMENT NOT PRESENT", - "STACK SEGMENT FAULT", - "GENERAL PROTECTION FAULT", - "PAGE FAULT", - "RESERVED", - "x87 FLOATING-POINT-EXCEPTION", - "ALIGNMENT CHECK", - "MACHINE CHECK", - "SMID FLOATING-POINT-EXCEPTION", - "VIRTUALIZATION EXCEPTION", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "RESERVED", - "SECURITY EXCEPTION", - "RESERVED" -}; +char *EXCEPTION_MESSAGES[] = {"DIVIDE BY ZERO", + "DEBUG", + "NON-MASKABLE INTERRUPT", + "BREAKPOINT", + "OVERFLOW", + "BOUND RAGE EXCEEDED", + "INVALID OPCODE", + "DEVICE NOT AVAILABLE", + "DOUBLE FAULT", + "COPROCESSOR SEGMENT OVERRUN", + "INVALID TSS", + "SEGMENT NOT PRESENT", + "STACK SEGMENT FAULT", + "GENERAL PROTECTION FAULT", + "PAGE FAULT", + "RESERVED", + "x87 FLOATING-POINT-EXCEPTION", + "ALIGNMENT CHECK", + "MACHINE CHECK", + "SMID FLOATING-POINT-EXCEPTION", + "VIRTUALIZATION EXCEPTION", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "RESERVED", + "SECURITY EXCEPTION", + "RESERVED"}; void setupExceptions() { - printf("setting exceptions\n"); setInterrupt(0, &isrHandler0); setInterrupt(1, &isrHandler1); setInterrupt(2, &isrHandler2); @@ -73,7 +70,8 @@ setInterrupt(31, &isrHandler31); } -void handleException(regs* registers) { - printf("exception %s was raised with error code 0x%x!\n", EXCEPTION_MESSAGES[registers->int_no], registers->err_code); +void handleException(regs *registers) { + printf("exception %s was raised with error code 0x%x!\n", + EXCEPTION_MESSAGES[registers->int_no], registers->err_code); yields(); -} \ No newline at end of file +} diff --git a/src/kernel/drivers/interrupts/irqs.c b/src/kernel/drivers/interrupts/irqs.c index 1cd8c79..7e4b85c 100644 --- a/src/kernel/drivers/interrupts/irqs.c +++ b/src/kernel/drivers/interrupts/irqs.c @@ -9,7 +9,6 @@ #define OFFSET_2 40 void remapPIC(int offset1, int offset2) { - printf("remapping PIC\n"); outb(0x20, 0x11); outb(0xA0, 0x11); outb(0x21, 0x20); @@ -23,7 +22,6 @@ } void setupIRQs() { - printf("setting IRQs\n"); setInterrupt(OFFSET_1 + 0, &irqHandler0); setInterrupt(OFFSET_1 + 1, &irqHandler1); setInterrupt(OFFSET_1 + 2, &irqHandler2); diff --git a/src/kernel/drivers/pci/hardDrive/hardDrive.c b/src/kernel/drivers/pci/hardDrive/hardDrive.c index 47205be..6ff03fd 100644 --- a/src/kernel/drivers/pci/hardDrive/hardDrive.c +++ b/src/kernel/drivers/pci/hardDrive/hardDrive.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -21,12 +22,5 @@ break; } } - void *buffer = malloc(512); - for (ListElement *current = drives; current; current = current->next) { - HardDrive *hardDrive = current->data; - hardDrive->access(hardDrive, 0, buffer, 1, 0); - printf("data: %x %x %s\n", *(uint32_t *)buffer, - *((uint32_t *)buffer + 1), buffer); - } - free(buffer); + mountDisk(listGet(&drives, 1)); } diff --git a/src/kernel/drivers/pci/hardDrive/ideController.c b/src/kernel/drivers/pci/hardDrive/ideController.c index f7d2c5d..e2f65be 100644 --- a/src/kernel/drivers/pci/hardDrive/ideController.c +++ b/src/kernel/drivers/pci/hardDrive/ideController.c @@ -100,6 +100,7 @@ uint16_t status = ideReadRegister(channel, IDE_STATUS_REGISTER); while (status & IDE_STATUS_BUSY) { status = ideReadRegister(channel, IDE_STATUS_REGISTER); + yields(); } if (checkErrors && status & IDE_STATUS_ERROR) { printf("encountered an error\n"); @@ -126,19 +127,11 @@ lbaIo[1] = (lba & 0x0000FF00) >> 8; lbaIo[2] = (lba & 0x00FF0000) >> 16; lbaIo[3] = (lba & 0xFF000000) >> 24; - } else if (interface->capabilities & 0x200) { + } else { accessMode = 1; - lbaIo[0] = (uint8_t)lba; + lbaIo[0] = lba & 0xFF; lbaIo[1] = (lba & 0x000FF00) >> 8; lbaIo[2] = (lba & 0x0FF0000) >> 16; - head = (lba & 0xF000000) >> 24; - } else { - sector = (lba % 63) + 1; - cylinder = (lba + 1 - sector) / (16 * 63); - lbaIo[0] = sector; - lbaIo[1] = (uint8_t)cylinder; - lbaIo[2] = (cylinder >> 8) & 0xFF; - head = (lba + 1 - sector) % (16 * 63) / (63); } ideWaitUntilNotBusy(interface->channels, true); if (accessMode == 0) { @@ -181,7 +174,7 @@ void *current = buffer; for (uint8_t i = 0; i < sectorCount; i++) { ideWaitUntilNotBusy(interface->channels, true); - ideReadBuffer(interface->channels, (void *)buffer + 256 * i); + ideReadBuffer(interface->channels, (void *)buffer + 512 * i); } } } diff --git a/src/kernel/drivers/pci/pci.c b/src/kernel/drivers/pci/pci.c index 96cbfc3..8068f5d 100644 --- a/src/kernel/drivers/pci/pci.c +++ b/src/kernel/drivers/pci/pci.c @@ -1,5 +1,6 @@ #include <_stdio.h> #include +#include #include #include #include @@ -143,4 +144,5 @@ } } printf("enumerated %i pci devices\n", listCount(pciDevices)); + scanHardDrives(); } diff --git a/src/kernel/drivers/textMode/_stdio.c b/src/kernel/drivers/textMode/_stdio.c index 167035c..240870a 100644 --- a/src/kernel/drivers/textMode/_stdio.c +++ b/src/kernel/drivers/textMode/_stdio.c @@ -159,11 +159,3 @@ listAdd(&getPrinterTask()->messages, message); schedule(getPrinterTask()); } - -uint32_t strlen(char *string) { - uint32_t length = 0; - while (string[length] != 0) { - length++; - } - return length; -} diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index c7c1d50..6edb5e1 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -20,11 +21,10 @@ getCurrentTask()->ticksLeft = 1000; drawLogo(); yields(); - setupDevices(); - setupInterrupts(); printCPUData(); - scanPCIDevices(); - scanHardDrives(); + setupDevices(); + printf("file system tree:\n"); + printFileSystemTree(); while (1) { for (Message *message = popBeginning(&(getCurrentTask()->messages)); message; message = popBeginning(&getCurrentTask()->messages)) { diff --git a/src/kernel/memory/fileSystem.c b/src/kernel/memory/fileSystem.c new file mode 100644 index 0000000..88589ef --- /dev/null +++ b/src/kernel/memory/fileSystem.c @@ -0,0 +1,215 @@ +#include <_stdio.h> +#include +#include +#include + +FileSystemFolder rootFolder = { + .name = "/", .type = FS_FOLDER, .children = NULL}; + +void printFolderTree(FileSystemFolder *folder, char *prefix) { + char *nextPrefix = stringCombine(prefix, " "); + for (ListElement *current = *folder->children; current; + current = current->next) { + FileSystemTreeNode *child = current->data; + printf("%s%s\n", prefix, child->name); + if (child->type == FS_FOLDER) { + if (child->name[0] == '.') { + continue; + } + printFolderTree(current->data, nextPrefix); + } + } + free(nextPrefix); +} + +void printFileSystemTree() { printFolderTree(&rootFolder, ""); } + +void setupFatInfo(FatInfo *info, FatBootSector *boot, HardDrive *hardDrive) { + info->bootSector = boot; + info->rootDirectorySize = + (boot->rootEntryCount * 32 + boot->sectorSize - 1) / boot->sectorSize; + info->fatSize = boot->fatSize16Bit ? boot->fatSize16Bit : 0; + info->totalSectorCount = boot->totalSectorCount16Bit + ? boot->totalSectorCount16Bit + : boot->totalSectorCount32Bit; + uint32_t dataSectorCount = + info->totalSectorCount - + (boot->reservedSectorCount + (boot->FATCount * info->fatSize) + + info->rootDirectorySize); + uint32_t clusterCount = dataSectorCount / boot->sectorsPerCluster; + if (clusterCount < 4085) { + info->version = 12; + } else if (clusterCount < 65525) { + info->version = 16; + } else { + info->version = 32; + } + info->firstDataSector = boot->reservedSectorCount + + (boot->FATCount * info->fatSize) + + info->rootDirectorySize; + info->firstFatSector = boot->reservedSectorCount; + info->firstRootSector = info->firstDataSector - info->rootDirectorySize; + info->fat = malloc(info->fatSize * boot->sectorSize); + info->hardDrive = hardDrive; + hardDrive->access(hardDrive, info->firstFatSector, info->fat, info->fatSize, + 0); + printf("size: %i\n", info->fatSize * info->bootSector->sectorSize * + sizeof(void *) / info->version / 8); + info->knownFolders = malloc(info->fatSize * info->bootSector->sectorSize / + sizeof(void *) * info->version / 8); +} + +uint32_t getNextCluster(FatInfo *info, uint32_t cluster) { + switch (info->version) { + case 16: + return ((uint16_t *)info->fat)[cluster]; + } + return 00; +} + +bool isClusterEnd(FatInfo *info, uint32_t cluster) { + switch (info->version) { + case 16: + return cluster == 0xFFFF; + } + return true; +} + +char *getFullString(LongNameEntry *name) { + char *string = malloc(14); + string[14] = 0; + for (uint8_t i = 0; i < 5; i++) { + string[i] = name->name1[i * 2]; + } + for (uint8_t i = 0; i < 6; i++) { + string[5 + i] = name->name2[i * 2]; + } + for (uint8_t i = 0; i < 2; i++) { + string[11 + i] = name->name3[i * 2]; + } + return string; +} + +char *getCombinedName(ListElement **longName) { + char *current = "", *new; + while (*longName != NULL) { + new = stringCombine(popBeginning(longName), current); + if (strlen(current)) { + free(current); + } + current = new; + } + return current; +} + +char *getName(ListElement **longName, FatDirectoryEntry *directory) { + if (listCount(*longName)) { + return getCombinedName(longName); + } + char *name = malloc(12); // 8:3 short name + name[11] = 0; + for (uint8_t i = 0; i < 11; i++) { + name[i] = directory->name[i]; + } + return name; +} + +uint32_t getClusterSector(FatInfo *info, uint32_t cluster) { + return info->firstDataSector + + ((cluster - 2) * info->bootSector->sectorsPerCluster); +} + +void folderScanCluster(FatInfo *info, uint32_t cluster, + FileSystemFolder *parent); + +void setupFolder(char *name, FatDirectoryEntry *directory, FatInfo *info, + FileSystemFolder *parent) { + FileSystemFolder *folder = malloc(sizeof(FileSystemFolder)); + folder->name = name; + folder->type = FS_FOLDER; + uint32_t cluster = directory->firstDataClusterLower | + directory->fistDataClusterUpper << 16; + listAdd(parent->children, folder); + if (info->knownFolders[cluster]) { + folder->children = info->knownFolders[cluster]; + return; + } + if (cluster == 0) { + folder->children = rootFolder.children; + return; + } + folder->children = malloc(sizeof(ListElement **)); + info->knownFolders[cluster] = folder->children; + *folder->children = NULL; + folderScanCluster(info, getClusterSector(info, cluster), folder); + while (!isClusterEnd(info, cluster)) { + cluster = getNextCluster(info, cluster); + if (cluster > 0xFF00) { + return; + } + folderScanCluster(info, getClusterSector(info, cluster), folder); + } +} + +void setupFile(char *name, FileSystemFolder *parent) { + FileSystemFile *file = malloc(sizeof(FileSystemFile)); + file->name = name; + file->type = FS_FILE; + file->subType = 0; + listAdd(parent->children, file); +} + +void setupChild(char *name, FatDirectoryEntry *directoryEntry, FatInfo *info, + FileSystemFolder *parent) { + if (directoryEntry->attributes & FAT_ATTRIBUTE_VOLUME_ID) { + info->volumeID = name; + return; + } + if (directoryEntry->attributes & FAT_ATTRIBUTE_DIRECTORY) { + setupFolder(name, directoryEntry, info, parent); + } else { + setupFile(name, parent); + } +} + +void folderScanCluster(FatInfo *info, uint32_t sector, + FileSystemFolder *parent) { + void *clusterData = malloc(info->bootSector->sectorSize * + info->bootSector->sectorsPerCluster); + info->hardDrive->access(info->hardDrive, sector, clusterData, + info->bootSector->sectorsPerCluster, 0); + FatDirectoryEntry *directory = clusterData; + ListElement *longName = NULL; + uint32_t i; + for (i = 0; i < info->bootSector->sectorSize * + info->bootSector->sectorsPerCluster / + sizeof(FatDirectoryEntry); + i++) { + if (directory[i].name[0] == 0) { + break; + } + if (directory[i].name[0] == (char)0xE5 || + directory[i].name[0] == (char)0xFF) { + continue; + } + if (directory[i].attributes == FAT_ATTRIBUTE_LONG_NAME) { + listAdd(&longName, getFullString((void *)&(directory[i]))); + continue; + } + setupChild(getName(&longName, &directory[i]), &directory[i], info, + parent); + } + listClear(&longName, true); + free(clusterData); +} + +void mountDisk(HardDrive *drive) { + void *bootSector = malloc(512); + drive->access(drive, 0, bootSector, 1, 0); + FatBootSector *boot = bootSector; + FatInfo *info = malloc(sizeof(FatInfo)); + setupFatInfo(info, boot, drive); + printf("type: FAT%i\n", info->version); + rootFolder.children = malloc(sizeof(ListElement *)); + folderScanCluster(info, info->firstRootSector, &rootFolder); +} diff --git a/src/kernel/util/list.c b/src/kernel/util/list.c index 76110c4..c051fce 100644 --- a/src/kernel/util/list.c +++ b/src/kernel/util/list.c @@ -60,7 +60,7 @@ return result; } -void *popEnd(ListElement **list) { +void *listPopEnd(ListElement **list) { if (list == NULL) { return NULL; } @@ -78,3 +78,24 @@ } return result; } + +void *listGet(ListElement **list, uint32_t index) { + ListElement *current = *list; + for (uint32_t i = 0; i < index; i++) { + current = current->next; + } + return current->data; +} + +void listClear(ListElement **list, bool freeData) { + ListElement *current = *list, *next; + while (current) { + next = current->next; + if (freeData) { + free(current->data); + } + free(current); + current = next; + } + *list = NULL; +} diff --git a/src/kernel/util/util.c b/src/kernel/util/util.c new file mode 100644 index 0000000..bc19cc5 --- /dev/null +++ b/src/kernel/util/util.c @@ -0,0 +1,24 @@ +#include +#include +#include + +uint32_t strlen(char *string) { + uint32_t length = 0; + while (string[length] != 0) { + length++; + } + return length; +} + +char *stringCombine(char *string1, char *string2) { + uint16_t length1 = strlen(string1), length2 = strlen(string2); + char *result = malloc(length1 + length2 + 1); + result[length1 + length2] = 0; + for (uint32_t i = 0; i < length1; i++) { + result[i] = string1[i]; + } + for (uint32_t i = 0; i < length2; i++) { + result[length1 + i] = string2[i]; + } + return result; +}