bunch of stuff
This commit is contained in:
parent
4ca1f9f68e
commit
f1f3d95769
@ -1,3 +1,8 @@
|
|||||||
|
/*----------------*\
|
||||||
|
|Nanite OS |
|
||||||
|
|Copyright (C) 2024|
|
||||||
|
|Tyler McGurrin |
|
||||||
|
\*----------------*/
|
||||||
#include "disk.h"
|
#include "disk.h"
|
||||||
#include "x86.h"
|
#include "x86.h"
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
|
|||||||
@ -1,9 +1,19 @@
|
|||||||
|
/*----------------*\
|
||||||
|
|Nanite OS |
|
||||||
|
|Copyright (C) 2024|
|
||||||
|
|Tyler McGurrin |
|
||||||
|
\*----------------*/
|
||||||
#include "fat.h"
|
#include "fat.h"
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
#include "memdefs.h"
|
#include "memdefs.h"
|
||||||
#include "utility.h"
|
#include "utility.h"
|
||||||
|
#include "string.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
#define SECTOR_SIZE 512
|
#define SECTOR_SIZE 512
|
||||||
|
#define MAX_PATH_SIZE 256
|
||||||
|
#define MAX_FILE_HANDLES 10
|
||||||
|
#define ROOT_DIRECTORY_HANDLE -1
|
||||||
|
|
||||||
#pragma pack(push,1)
|
#pragma pack(push,1)
|
||||||
typedef struct
|
typedef struct
|
||||||
@ -34,36 +44,40 @@ typedef struct
|
|||||||
} FAT_BootSector;
|
} FAT_BootSector;
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
FAT_File Public;
|
||||||
|
bool Opened;
|
||||||
|
uint32_t FirstCluster;
|
||||||
|
uint32_t CurrentCluster;
|
||||||
|
uint32_t CurrentSectorInCluster;
|
||||||
|
uint8_t Buffer[SECTOR_SIZE];
|
||||||
|
|
||||||
|
} FAT_FileData;
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
union {
|
union {
|
||||||
FAT_BootSector Bootsector;
|
FAT_BootSector BootSector;
|
||||||
uint8_t BootsectorBytes[SECTOR_SIZE];
|
uint8_t BootSectorBytes[SECTOR_SIZE];
|
||||||
} BS;
|
} BS;
|
||||||
|
|
||||||
|
FAT_FileData RootDirectory;
|
||||||
|
|
||||||
|
FAT_FileData OpenedFiles[MAX_FILE_HANDLES];
|
||||||
|
|
||||||
} FAT_Data;
|
} FAT_Data;
|
||||||
|
|
||||||
static FAT_Data far* g_Data;
|
static FAT_Data far* g_Data;
|
||||||
static uint8_t far* g_Fat = NULL;
|
static uint8_t far* g_Fat = NULL;
|
||||||
static FAT_DirectoryEntry far* g_RootDirectory = NULL;
|
static uint32_t g_DataSectionLba;
|
||||||
static uint32_t g_RootDirectoryEnd;
|
|
||||||
|
|
||||||
bool FAT_ReadBootSector(FILE* disk) {
|
bool FAT_ReadBootSector(DISK* disk) {
|
||||||
return DISK_ReadSectors(disk, 0, 1, &g_Data->BS.BootSectorBytes);
|
return DISK_ReadSectors(disk, 0, 1, &g_Data->BS.BootSectorBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FAT_ReadFat(DISK* disk)
|
bool FAT_ReadFat(DISK* disk)
|
||||||
{
|
{
|
||||||
return DISK_ReadSectors(disk, g_Data->g_BootSector.ReservedSectors, g_Data->g_BootSector.SectorsPerFat, g_Fat);
|
return DISK_ReadSectors(disk, g_Data->BS.BootSector.ReservedSectors, g_Data->BS.BootSector.SectorsPerFat, g_Fat);
|
||||||
}
|
|
||||||
|
|
||||||
bool FAT_ReadRootDirectory(DISK* disk)
|
|
||||||
{
|
|
||||||
uint32_t lba = g_Data->g_BootSector.ReservedSectors + g_Data->g_BootSector.SectorsPerFat * g_Data->g_BootSector.FatCount;
|
|
||||||
uint32_t size = sizeof(FAT_DirectoryEntry) * g_Data->g_BootSector.DirEntryCount;
|
|
||||||
uint32_t sectors = (size + g_Data->g_BootSector.BytesPerSector - 1) / g_Data->g_BootSector.BytesPerSector;
|
|
||||||
|
|
||||||
g_RootDirectoryEnd = lba + sectors;
|
|
||||||
return DISK_ReadSectors(disk, lba, sectors, g_RootDirectory);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FAT_Initialize(DISK* disk) {
|
bool FAT_Initialize(DISK* disk) {
|
||||||
@ -77,7 +91,7 @@ bool FAT_Initialize(DISK* disk) {
|
|||||||
|
|
||||||
// read FAT
|
// read FAT
|
||||||
g_Fat = (uint8_t far*)(g_Data + sizeof(FAT_Data));
|
g_Fat = (uint8_t far*)(g_Data + sizeof(FAT_Data));
|
||||||
uint32_t fatSize = g_Data->BS.Bootsector.BytesPerSector * g_Data->BS.BootSector.SectorsPerFat;
|
uint32_t fatSize = g_Data->BS.BootSector.BytesPerSector * g_Data->BS.BootSector.SectorsPerFat;
|
||||||
if(sizeof(FAT_Data) + fatSize >= MEMORY_FAT_SIZE) {
|
if(sizeof(FAT_Data) + fatSize >= MEMORY_FAT_SIZE) {
|
||||||
printf("FAT: Not Enough Memory to Read FAT!\r\nNeeded %u, Only Have %u\r\n", sizeof(FAT_Data) + fatSize, MEMORY_FAT_SIZE);
|
printf("FAT: Not Enough Memory to Read FAT!\r\nNeeded %u, Only Have %u\r\n", sizeof(FAT_Data) + fatSize, MEMORY_FAT_SIZE);
|
||||||
return false;
|
return false;
|
||||||
@ -87,24 +101,94 @@ bool FAT_Initialize(DISK* disk) {
|
|||||||
printf("FAT: Failed to Read FAT!\r\n");
|
printf("FAT: Failed to Read FAT!\r\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// read root dir (/)
|
|
||||||
g_RootDirectory = (FAT_DirectoryEntry far*)(g_Fat + fatSize);
|
// open root dir
|
||||||
|
uint32_t rootDirLba = g_Data->BS.BootSector.ReservedSectors + g_Data->BS.BootSector.SectorsPerFat * g_Data->BS.BootSector.FatCount;
|
||||||
uint32_t rootDirSize = sizeof(FAT_DirectoryEntry) * g_Data->BS.BootSector.DirEntryCount;
|
uint32_t rootDirSize = sizeof(FAT_DirectoryEntry) * g_Data->BS.BootSector.DirEntryCount;
|
||||||
rootDirSize = align(rootDirSize, g_Data->BS.BootSector.BytesPerSector);
|
|
||||||
|
|
||||||
if (sizeof(FAT_Data) + fatSize + rootDirSize >= MEMORY_FAT_SIZE) {
|
g_Data->RootDirectory.Public.Handle = ROOT_DIRECTORY_HANDLE;
|
||||||
printf("FAT: Not Enough Memory to Read Root Directory!\r\nNeeded %u, Only Have %u\r\n", sizeof(FAT_Data) + fatSize + rootDirSize, MEMORY_FAT_SIZE);
|
g_Data->RootDirectory.Public.IsDirectory = true;
|
||||||
return false;
|
g_Data->RootDirectory.Public.Position = 0;
|
||||||
}
|
g_Data->RootDirectory.Public.Size = sizeof(FAT_DirectoryEntry) * g_Data->BS.BootSector.DirEntryCount;
|
||||||
|
g_Data->RootDirectory.Opened = true;
|
||||||
|
g_Data->RootDirectory.FirstCluster = 0;
|
||||||
|
g_Data->RootDirectory.CurrentCluster = 0;
|
||||||
|
g_Data->RootDirectory.CurrentSectorInCluster = 0;
|
||||||
|
|
||||||
if(!FAT_ReadRootDirectory(disk)) {
|
if (!DISK_ReadSectors(disk, rootDirLba, 1, g_Data->RootDirectory.Buffer)) {
|
||||||
printf("FAT: Failed to Read Root Directory!\r\n");
|
printf("FAT: Failed to Read Root Directory!\r\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
//calculate data
|
||||||
|
uint32_t rootDirSectors = (rootDirSize + g_Data->BS.BootSector.BytesPerSector - 1) / g_Data->BS.BootSector.BytesPerSector;
|
||||||
|
g_DataSectionLba = rootDirLba + rootDirSectors;
|
||||||
|
|
||||||
|
// reset opened files
|
||||||
|
for (int i = 0; i < MAX_FILE_HANDLES; i++)
|
||||||
|
g_Data->OpenedFiles[i].Opened = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
FAT_File far* FAT_Open(DISK* disk, const char* path) {
|
uint32_t FAT_ClusterToLba(uint32_t cluster) {
|
||||||
|
return g_DataSectionLba + (cluster -2) * g_Data->BS.BootSector.SectorsPerCluster;
|
||||||
|
}
|
||||||
|
|
||||||
|
FAT_File far* FAT_OpenEntry(DISK* disk, FAT_DirectoryEntry* entry) {
|
||||||
|
// find empty handle
|
||||||
|
int handle = -1;
|
||||||
|
for (int i = 0; i < MAX_FILE_HANDLES && handle < 0; i++) {
|
||||||
|
if (!g_Data->OpenedFiles[i].Opened)
|
||||||
|
handle = i;
|
||||||
|
}
|
||||||
|
// out of handles
|
||||||
|
if (handle < 0) {
|
||||||
|
printf("FAT: Out of File Handles!\r\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// setup vars
|
||||||
|
FAT_FileData far* fd = &g_Data->OpenedFiles[handle];
|
||||||
|
fd->Public.Handle = handle;
|
||||||
|
fd->Public.IsDirectory = (entry->Attributes & FAT_ATTRIBUTE_DIRECTORY) != 0;\
|
||||||
|
fd->Public.Position = 0;
|
||||||
|
fd->Public.Size = 0;
|
||||||
|
fd->FirstCluster = entry->FirstClusterLow + ((uint32_t)entry->FirstClusterHigh << 16);
|
||||||
|
fd->CurrentCluster = fd->FirstCluster;
|
||||||
|
fd->CurrentSectorInCluster = 0;
|
||||||
|
|
||||||
|
if (!DISK_ReadSectors(disk, FAT_ClusterToLba(fd->CurrentCluster), 1, fd->Buffer)) {
|
||||||
|
printf("FAT: Read Error!\r\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fd->Opened = true;
|
||||||
|
return &fd->Public;
|
||||||
|
}
|
||||||
|
|
||||||
|
FAT_File* FAT_Open(DISK* disk, const char* path) {
|
||||||
|
char buffer[MAX_PATH_SIZE];
|
||||||
|
|
||||||
|
// ignore leading slash
|
||||||
|
if (path[0] == '/')
|
||||||
|
path++;
|
||||||
|
|
||||||
|
FAT_File far* parent = NULL;
|
||||||
|
FAT_File far* current = g_Data->RootDirectory.Public;
|
||||||
|
|
||||||
|
while (*path) {
|
||||||
|
// extract next file name from path
|
||||||
|
const char * delim = strchr(path, '/');
|
||||||
|
if (delim != NULL) {
|
||||||
|
memcpy(name, path, delim - path);
|
||||||
|
name[delim - path + 1] = '\0';
|
||||||
|
path = delim + 1
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
unsigned len = strlen(path);
|
||||||
|
memcpy(name, path, len);
|
||||||
|
name[len + 1] = '\0';
|
||||||
|
path += len;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DirectoryEntry* findFile(const char* name)
|
DirectoryEntry* findFile(const char* name)
|
||||||
|
|||||||
@ -21,7 +21,7 @@ typedef struct
|
|||||||
} FAT_DirectoryEntry;
|
} FAT_DirectoryEntry;
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
typdef struct {
|
typedef struct {
|
||||||
int Handle;
|
int Handle;
|
||||||
bool IsDirectory;
|
bool IsDirectory;
|
||||||
uint32_t Position;
|
uint32_t Position;
|
||||||
@ -39,7 +39,6 @@ enum FAT_Attributes {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool FAT_Initialize(DISK* disk);
|
bool FAT_Initialize(DISK* disk);
|
||||||
FAT_File far* FAT_Open(DISK* disk, const char* path);
|
FAT_File far* FAT_Open(DISK* disk, const char* path);
|
||||||
uint32_t FAT_Read(DISK* disk, FAT_File far* file, uint32_t byteCount, void* dataOut);
|
uint32_t FAT_Read(DISK* disk, FAT_File far* file, uint32_t byteCount, void* dataOut);
|
||||||
|
|||||||
@ -1,3 +1,8 @@
|
|||||||
|
/*----------------*\
|
||||||
|
|Nanite OS |
|
||||||
|
|Copyright (C) 2024|
|
||||||
|
|Tyler McGurrin |
|
||||||
|
\*----------------*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// 0x00000000 - 0x000003FF - int vector table
|
// 0x00000000 - 0x000003FF - int vector table
|
||||||
|
|||||||
14
src/bootloader/stage2/memory.c
Normal file
14
src/bootloader/stage2/memory.c
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
/*----------------*\
|
||||||
|
|Nanite OS |
|
||||||
|
|Copyright (C) 2024|
|
||||||
|
|Tyler McGurrin |
|
||||||
|
\*----------------*/
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
int memcpy(void far* dst, const void far* src, uint16_t num) {
|
||||||
|
uint8_t far* u8Dst = (uint8_t far *)dst;
|
||||||
|
const uint8_t far* u8Src = (const uint8_t far *)src;
|
||||||
|
|
||||||
|
for (uint16_t i = 0; i < num; i++)
|
||||||
|
u8Dst[i] = u8Src[i];
|
||||||
|
}
|
||||||
9
src/bootloader/stage2/memory.h
Normal file
9
src/bootloader/stage2/memory.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
/*----------------*\
|
||||||
|
|Nanite OS |
|
||||||
|
|Copyright (C) 2024|
|
||||||
|
|Tyler McGurrin |
|
||||||
|
\*----------------*/
|
||||||
|
#pragma once
|
||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
|
int memcpy(void far* dst, const void far* src, uint16_t num);
|
||||||
@ -1,3 +1,8 @@
|
|||||||
|
/*----------------*\
|
||||||
|
|Nanite OS |
|
||||||
|
|Copyright (C) 2024|
|
||||||
|
|Tyler McGurrin |
|
||||||
|
\*----------------*/
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
|
|
||||||
@ -34,3 +39,11 @@ char* strcpy(char* dst, const char* src) {
|
|||||||
*dst = '\0';
|
*dst = '\0';
|
||||||
return origDst;
|
return origDst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned strlen(const char* str) {
|
||||||
|
unsigned len = 0;
|
||||||
|
while (*str) {
|
||||||
|
++len; ++str;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
@ -1,4 +1,10 @@
|
|||||||
|
/*----------------*\
|
||||||
|
|Nanite OS |
|
||||||
|
|Copyright (C) 2024|
|
||||||
|
|Tyler McGurrin |
|
||||||
|
\*----------------*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
const char* strchr(const char* str, char chr);
|
const char* strchr(const char* str, char chr);
|
||||||
char* strcpy(char* dst, const char* src);
|
char* strcpy(char* dst, const char* src);
|
||||||
|
unsigned strlen(const char* str);
|
||||||
@ -1,3 +1,8 @@
|
|||||||
|
/*----------------*\
|
||||||
|
|Nanite OS |
|
||||||
|
|Copyright (C) 2024|
|
||||||
|
|Tyler McGurrin |
|
||||||
|
\*----------------*/
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user