[GOOD] BUILD 0.1.0.551 DATE 9/03/2011 AT 9:25 AM

====================================================
Mainly changed: HAL.FSs
+ Updated 'mount' call in floppy driver, now done after controller is
initialized
+ Added 'detect' function for FAT file systems
+ Implemented 'initdr' driver, however still bugged
+ Improved logger
This commit is contained in:
2021-09-14 18:51:43 +03:00
parent 0372dcee81
commit caa7718af9
59 changed files with 991 additions and 112 deletions

View File

@ -0,0 +1,80 @@
/*
* fat.c
*
* Created on: Aug 31, 2011
* Author: Tiberiu
*/
#include <fileio.h>
#include <debugio.h>
#include "fat.h"
// Buffer should contain boot sector
uint32 FatDetect (void* buffer)
{
FatBootSector* boot = (FatBootSector*)buffer;
uint32 correct_features = 0;
uint32 total_features = 0;
// Try the asm 'jmp' instruction
if ((boot->bpb.AsmJmp[0] == 0x6B || boot->bpb.AsmJmp[0]==0xEB) && boot->bpb.AsmJmp[2] == 0x90)
correct_features++;
total_features ++;
// Try the OemId. First char is often 'm' or 'M' (mkdosfs or MSDOS??/MSWIN??)
if (boot->bpb.OemId[0] == 'm' || boot->bpb.OemId[0] == 'M') correct_features++;
total_features ++;
// Try the bytes per sector. Most often, it is 512.
if (boot->bpb.BytesPerSector == 512) correct_features++;
total_features ++;
// Try the sectors per cluster. This is always a power of 2.
uint8 sectclust = boot->bpb.SectorsPerCluster;
uint8 count = 0;
while (sectclust) { count += sectclust&1; sectclust>>=1; }
if (count != 1) return 0;
// Number of fats, can be 2 or (rarely) 1. Important feature.
if (boot->bpb.FatsCount == 1 || boot->bpb.FatsCount == 2) correct_features+=2;
total_features+=2;
// This tells us what type of disk this is. Usually values are above 0xf0.
if (boot->bpb.MediaDescriptorType >= 0xF0) correct_features++;
total_features++;
// Calculate number of clusters
uint32 clusters = (boot->bpb.TotalSectors * boot->bpb.SectorsPerCluster);
if (clusters == 0) clusters = (boot->bpb.TotalSectorsLarge * boot->bpb.SectorsPerCluster);
// Calculate fat type
uint32 fat = (clusters < 4085) ? 12 : 16 ;
if (clusters > 65524) fat = 32;
// Try the extended info
if (fat == 32) {
if (boot->ext.fat32.Signature == 0x28 || boot->ext.fat32.Signature == 0x29) correct_features++;
total_features++;
}
else {
if (boot->ext.fat16.Signature == 0x28 || boot->ext.fat16.Signature == 0x29) correct_features++;
total_features++;
}
Log("Fat", "Correct features: %u out of %u. Type should be fat%u.\n", correct_features, total_features, fat);
// See what we have.
if (correct_features > (total_features/2)) return 0; // insuficcient correct features?
return fat; // return FAT type
}
void FatInstall()
{
FileSystem fat12 = {0, "fat12", Fat12Detect, 0,0,0,0,0,0,0,0};
FileSystem fat16 = {0, "fat16", Fat16Detect, 0,0,0,0,0,0,0,0};
FileSystem fat32 = {0, "fat32", Fat32Detect, 0,0,0,0,0,0,0,0};
VfsInstallFs(&fat12); VfsInstallFs(&fat16); VfsInstallFs(&fat32);
}

View File

@ -0,0 +1,135 @@
/*
* fat.h
*
* Created on: Aug 31, 2011
* Author: Tiberiu
*/
#ifndef FAT_H_
#define FAT_H_
#include <fileio.h>
/*******************************************************
* FAT: Bios Parameter Block *
*******************************************************/
typedef struct {
uint8 AsmJmp[3];
char OemId[8];
uint16 BytesPerSector;
uint8 SectorsPerCluster;
uint16 ReservedSectors;
uint8 FatsCount;
uint16 DirectoryEntriesCount;
uint16 TotalSectors;
uint8 MediaDescriptorType;
uint32 SectorsPerFat;
uint16 SectorsPerTrack;
uint16 Heads;
uint32 HiddenSectors;
uint32 TotalSectorsLarge;
} __attribute__((packed)) FatBiosParameterBlock ;
/*******************************************************
* FAT12/FAT16: Extended boot record *
*******************************************************/
typedef struct {
uint8 DriveNumber; // Useless
uint8 NtFlags;
uint8 Signature; // Must be 0x28/0x29
uint32 VolumeIdSerial;
char VolumeLabel[11];
char SystemId[8];
} __attribute__((packed)) Fat16ExtendedBootRecord; // fat12 or 16
/*******************************************************
* FAT32: Extended boot record *
*******************************************************/
typedef struct {
uint32 SectorsPerFat;
uint16 Flags;
uint16 FatVersion; // High byte=major version, low=minor
uint32 RootDirectoryCluster;
uint16 FsInfoCluster;
uint16 BackupBootSectorCluster;
uint8 Reserved[12]; // Should be set to 0 when formatting
uint8 DriveNumber;
uint8 NtFlags;
uint8 Signature; // Must be 0x28/0x29
uint32 VolumeIdSerial;
char VolumeLabel[11];
char SystemId[8]; // Always "FAT32 "
} __attribute__((packed)) Fat32ExtendedBootRecord;
/*******************************************************
* FAT: Extended boot record (union of above 2 fields) *
*******************************************************/
union FatExtendedBootRecord
{
Fat16ExtendedBootRecord fat16;
Fat32ExtendedBootRecord fat32;
};
/*******************************************************
* FAT: Complete boot sector info *
*******************************************************/
typedef struct {
FatBiosParameterBlock bpb;
union FatExtendedBootRecord ext;
} __attribute__((packed)) FatBootSector;
/*******************************************************
* FAT: Directory entry *
*******************************************************/
typedef struct {
char FileName[8];
char FileExt[3];
uint8 Attributes;
uint8 _reserved;
uint8 CreateTimeFine; // hundreds (0.01) of a second, up to 199
uint16 CreateTime; // 15-11 hour; 10-5 mins; 4-0 secs/2;
uint16 CreateDate; // 15-9 year (since 1980); 8-5 mon; 4-0 day
uint16 AccessDate;
uint16 FirstClusterHigh; // Fat32; EA-index in fat12,fat16
uint16 LastModifiedTime;
uint16 LastModifiedDate;
uint16 FirstClusterLow;
uint32 Size;
} __attribute__((packed)) FatDirectoryEntry;
/*******************************************************
* FAT: Long file name entry *
*******************************************************/
typedef struct {
uint8 SequenceNo;
uint16 Name1[5]; // Encoded in UTF-16
uint8 Attributes; // Should be 0xF;
uint8 _reserved;
uint16 Name2[6];
uint16 FirstCluster;// Should be 0x0000
uint16 Name3[2];
} FatLongFileName;
/*******************************************************
* FAT: Directory entry (union of above 2 fields) *
*******************************************************/
union FatDirEntryUnion {
FatDirectoryEntry d;
FatLongFileName lfn;
};
extern uint32 FatDetect (void* buffer);
extern void FatInstall();
extern uint32 Fat12Detect (DevReadRoutine r, uint32 blocksz);
extern uint32 Fat16Detect (DevReadRoutine r, uint32 blocksz);
extern uint32 Fat32Detect (DevReadRoutine r, uint32 blocksz);
#endif /* FAT_H_ */

View File

@ -0,0 +1,25 @@
/*
* fat12.c
*
* Created on: Aug 29, 2011
* Author: Tiberiu
*/
#include <fileio.h>
#include <memory.h>
#include "fat.h"
uint32 Fat12Detect (DevReadRoutine r, uint32 blocksz)
{
if (!r) return 0;
// Allocate buffer and call read routine
void* buffer = kmalloc(blocksz);
if (!(*r)(0,buffer)) return 0;
// Result of detection
uint32 res = FatDetect(buffer);
// Cleanup
kfree(buffer);
return (res == 12);
}

View File

@ -0,0 +1,26 @@
/*
* fat16.c
*
* Created on: Aug 31, 2011
* Author: Tiberiu
*/
#include <fileio.h>
#include <memory.h>
#include "fat.h"
uint32 Fat16Detect (DevReadRoutine r, uint32 blocksz)
{
if (!r) return 0;
// Allocate buffer and call read routine
void* buffer = kmalloc(blocksz);
if (!(*r)(0,buffer)) return 0;
// Result of detection
uint32 res = FatDetect(buffer);
// Cleanup
kfree(buffer);
return (res == 16);
}

View File

@ -0,0 +1,26 @@
/*
* fat32.c
*
* Created on: Aug 31, 2011
* Author: Tiberiu
*/
#include <fileio.h>
#include <memory.h>
#include "fat.h"
uint32 Fat32Detect (DevReadRoutine r, uint32 blocksz)
{
if (!r) return 0;
// Allocate buffer and call read routine
void* buffer = kmalloc(blocksz);
if (!(*r)(0,buffer)) return 0;
// Result of detection
uint32 res = FatDetect(buffer);
// Cleanup
kfree(buffer);
return (res == 32);
}

View File

@ -1,8 +0,0 @@
/*
* fat12.c
*
* Created on: Aug 29, 2011
* Author: Tiberiu
*/

View File

@ -0,0 +1,202 @@
/*
* initrd.c
*
* Created on: Sep 1, 2011
* Author: Tiberiu
*/
#define LUXMAGIC 0xCC23AA90
#define OEMSTR "luxram"
#include <fileio.h>
#include <stdlib.h>
#include <multiboot.h>
#include <memory.h>
#include <debugio.h>
#include "initrd.h"
luxDEVICE *luxDevices;
uint32 luxDeviceCount;
luxFILE *luxFiles;
uint32 luxFileCount;
uint32 luxFilesAllocated;
/**************************************
* DEVICE 'FAKE' READ ROUTINE *
**************************************/
uint32 luxDevRead (uint32 UNUSED(offset), void* UNUSED(buffer))
{
return LUXMAGIC;
}
/**************************************
* INITIALIZATION ROUTINE *
**************************************/
void luxInitrdInstall (MultibootInfo* info)
{
// Install filesystem
FileSystem fs = {0, "luxinitrd", luxDetect, 0, 0,
luxOpen, luxClose, luxRead, 0, luxTest, luxReadDir};
VfsInstallFs(&fs);
// See what modules are loaded in the multiboot info
if ((info->Flags & 8) == 0 || info->ModulesCount == 0) return; // nothing was loaded
uint32 i;
MultibootModule* modules = (MultibootModule*) info->ModulesAddress;
luxDevices = kmalloc(sizeof(luxDEVICE) * info->ModulesCount);
luxDeviceCount = 0;
for (i = 0; i < info->ModulesCount; i++) {
// Check magic number, to make sure module is a initrd image
luxHeader* head = (luxHeader*) modules[i].ModuleStart;
if (head->Magic != LUXMAGIC) {
Log("Initrd", "Magic = 0x%x [bad] ModuleStart = 0x%x\n", head->Magic, modules[i].ModuleStart);
continue;
}
// Set up entry
luxDevices[luxDeviceCount].Data = (void*) modules[i].ModuleStart;
luxDevices[luxDeviceCount].Size = modules[i].ModuleEnd - modules[i].ModuleStart;
// Register virtual device. Instead of blocksize, we give the dev no, so we can identify it later
VfsMount("initrd", luxDevRead, 0, luxDeviceCount);
++luxDeviceCount;
}
}
/**************************************
* OTHER USEFUL ROUTINES *
**************************************/
uint32 luxGetFileSlot()
{
// Find an empty slot
uint32 i = 0;
for (i = 0; i < luxFileCount; i++)
if (luxFiles[i].Id == 0xffffffff) return i;
// Nothing found? Allocate more slots if necessary
if (luxFileCount >= luxFilesAllocated)
{
luxFilesAllocated += 4;
luxFiles = kmrealloc(luxFiles, luxFilesAllocated);
}
// Give last slot
++luxFileCount;
return (luxFileCount-1);
}
// Navigates and returns the directory entry that contains the file/folder
luxDirectoryEntry* luxGetFile (string path, uint32 dev)
{
luxHeader* root = (luxHeader*) luxDevices[dev].Data;
luxDirectory* current = root->Root;
luxDirectoryEntry rt = {"root", 0xB68, 0, 0, 0, {0,0}, {0,0}, {0,0}, (uint32)&root->Root};
if (path[0] != '\\')
return &rt;
path = path + 1;
while (path)
{
// Trim the next path separator
string tmp = strchr(path, '\\');
if (tmp) {
*tmp = 0; tmp = tmp + 1;
if (!*tmp) tmp = 0;
}
// Find the folder/file in current directory
uint32 i, found = 0xffffffff;
for (i = 0; i < current->Count && found == 0xffffffff; i++)
if (strcmp(path, current->Entries[i].Name) == 0) found = i;
// Check if the file/folder was found
if (found == 0xffffffff) return NULL;
// Return entry pointer if done
if (!tmp) return &current->Entries[found];
// Go inside
current = (luxDirectory*) (current->Entries[found].Offset + (uint32)root);
path = tmp;
}
// We shouldn't get here
return NULL;
}
/**************************************
* FILE SYSTEM INTERFACE ROUTINES *
**************************************/
uint32 luxDetect (DevReadRoutine rd, uint32 blocksz)
{
// Check magic result, which is returned by our device read function
void* buffer = kmalloc(blocksz);
uint32 result = (*rd)(0, buffer);
kfree(buffer);
return (result == LUXMAGIC);
}
// Returns element count read
uint32 luxRead (const MountPoint* UNUSED(mp), FILE* f, uint32 elemsz, uint32 n, uint8* buffer)
{
uint32 temp = Min(n*elemsz, luxFiles[f->Id].Size);
memcpy(buffer, luxFiles[f->Id].Pos, temp);
luxFiles[f->Id].Size -= temp;
return temp;
}
FILE* luxOpen (const MountPoint* mp, FILE* f, string path)
{
f->Id = luxGetFileSlot();
luxDirectoryEntry* entry = luxGetFile(path, mp->BlockSize);
f->Name = entry->Name;
f->Flags = entry->Flags;
f->GroupId = entry->GroupId;
f->OwnerId = entry->OwnerId;
f->Size = entry->Size;
luxFiles[f->Id].Id = f->Id;
luxFiles[f->Id].Pos = luxFiles[f->Id].Start = entry->Offset + luxDevices[mp->BlockSize].Data;
luxFiles[f->Id].Size = entry->Size;
return f;
}
DirectoryEntry* luxTest (const MountPoint* mp, string path)
{
return (DirectoryEntry*) luxGetFile(path, mp->BlockSize);
}
FILE* luxClose (const MountPoint* UNUSED(mp), FILE* f)
{
luxFiles[f->Id].Id = NULL;
return f;
}
DirectoryEntry* luxReadDir (const MountPoint* UNUSED(mp), FILE* f, uint32 index)
{
luxDirectory* dir = luxFiles[f->Id].Start;
if (index > dir->Count) return NULL;
return (DirectoryEntry*) &dir->Entries[index];
}

View File

@ -0,0 +1,56 @@
/*
* initdr.h
*
* Created on: Sep 1, 2011
* Author: Tiberiu
*/
#ifndef INITDR_H_
#define INITDR_H_
#include <types.h>
#include <fileio.h>
typedef struct {
char Name[256];
uint32 Flags, OwnerId, GroupId, Size;
TimeSystem TimeCreated, TimeModified, TimeAccessed;
uint32 Offset;
} luxDirectoryEntry;
typedef struct {
uint32 Count;
luxDirectoryEntry* Entries;
} luxDirectory;
typedef struct {
uint32 Magic;
char Oem[6];
luxDirectory* Root;
} luxHeader;
typedef struct {
uint32 DeviceId;
uint32 Size;
void* Data;
} luxDEVICE;
typedef struct {
uint32 Id;
uint32 Size;
void* Start;
void* Pos;
} luxFILE;
extern uint32 luxDevRead (uint32 offset, void* buffer);
extern void luxInitrdInstall (MultibootInfo* info);
extern uint32 luxDetect (DevReadRoutine rd, uint32 blocksz);
extern uint32 luxRead (const MountPoint* mp, FILE* f, uint32 elemsz, uint32 n, uint8* buffer);
extern FILE* luxOpen (const MountPoint* mp, FILE* f, string path);
extern DirectoryEntry* luxTest (const MountPoint* mp, string path);
extern FILE* luxClose (const MountPoint* mp, FILE* f);
extern DirectoryEntry* luxReadDir (const MountPoint* mp, FILE* f, uint32 index);
#endif /* INITDR_H_ */

View File

@ -8,32 +8,35 @@
#include "keyboard/keyboard.h"
#include "mouse/mouse.h"
#include "filesys/fat/fat.h"
#include <fileio.h>
#include <debugio.h>
void HalInitialize()
{
// Initialize cpu
GdtInstall(); Log("%#[HAL] %#Installed GDT\n", ColorYellow, ColorLightGreen);
IdtInstall(); Log("%#[HAL] %#Installed IDT\n", ColorYellow, ColorLightGreen);
IsrsInstall(); Log("%#[HAL] %#Installed ISRs\n", ColorYellow, ColorLightGreen);
IrqInstall(); Log("%#[HAL] %#Installed IRQs\n", ColorYellow, ColorLightGreen);
GdtInstall(); Log("HAL", "Installed GDT\n");
IdtInstall(); Log("HAL", "Installed IDT\n");
IsrsInstall(); Log("HAL", "Installed ISRs\n");
IrqInstall(); Log("HAL", "Installed IRQs\n");
// Start interrupts
asm volatile ("sti");
Log("%#[HAL] %#Interrupts are started...\n", ColorYellow, ColorLightMagenta);
Log("HAL", "%#Interrupts are started...\n", ColorLightMagenta);
// Install keyboard
IrqInstallHandler(0, TimeHandler);
IrqInstallHandler(1, KeyboardHandler);
IrqInstallHandler(12, MouseHandler);
KeyboardInstallA(); Log("%#[HAL] %#Installing keyboard... %#[1/2] ", ColorYellow, ColorLightGray, ColorLightGreen);
KeyboardInstallB(); Log("%#[2/2]\n", ColorLightGreen);
KeyboardInstallA(); Log("HAL", "Installing keyboard... %#[1/2] ", ColorLightGreen);
KeyboardInstallB(); Log("HAL", "%#[2/2]\n", ColorLightGreen);
// Install mouse driver
MouseInstall(); Log("%#[HAL] %#Installed mouse driver\n", ColorYellow, ColorLightGreen);
MouseInstall(); Log("HAL", "Installed mouse driver\n");
// Install VFS
VfsInstall();
FatInstall();
}

View File

@ -11,13 +11,20 @@
#include <debugio.h>
#include "keyboard/keyboard.h"
void SystemPanic()
{
asm("cli");
asm("hlt");
}
void SystemReboot()
{
Log("Rebooting system...\n");
Log("System", "Rebooting system...\n");
KeyboardWaitOutport();
outportb (0x64, 0xFE);
asm("cli");
asm("hlt");
SystemPanic();
}

View File

@ -13,9 +13,6 @@ MountPoint* mpArray;
uint32 mpCount;
uint32 mpAllocated;
#define LogVfs(...) { Log("%#[Vfs] ", ColorLightBlue); Log(__VA_ARGS__); }
#define ErrorVfs(...) { Error("%#[Vfs] ", ColorLightBlue); Error(__VA_ARGS__); }
void VfsInstall ()
{
fsArray = (FileSystem*) kmalloc(MAX_FS_COUNT * sizeof(FileSystem));
@ -25,13 +22,13 @@ void VfsInstall ()
mpCount = 0;
mpAllocated = 32;
LogVfs("%#VFS now in business.\n", ColorLightGreen);
Log("VFS", "%#VFS now in business.\n", ColorLightGreen);
}
uint8 VfsInstallFs (FileSystem* fs)
{
if (fsCount >= MAX_FS_COUNT) {
ErrorVfs("%#Failed to install file system '%s': FS count reached.\n", ColorLightRed, fs->Name);
Error("VFS", "%#Failed to install file system '%s': FS count reached.\n", ColorLightRed, fs->Name);
return 0;
}
@ -39,7 +36,7 @@ uint8 VfsInstallFs (FileSystem* fs)
fsArray[fsCount].Id = fsCount;
++fsCount;
LogVfs("Installed file system %#.\n", ColorWhite, fs->Name);
Log("VFS", "Installed file system %#%s.\n", ColorWhite, fs->Name);
return 1;
}
@ -47,7 +44,7 @@ uint32 VfsFindDevice (string dev)
{
uint32 i;
for (i = 0; i < mpCount; i++)
if (strcmp(dev, mpArray[i].Name) == 0)
if (mpArray[i].Id != BAD && strcmp(dev, mpArray[i].Name) == 0)
return i;
return BAD;
@ -75,10 +72,11 @@ uint8 VfsMount (string Name, DevReadRoutine R, DevWriteRoutine W, uint32 BlockSi
// Try to figure out the file system
for (i = 0; i < fsCount && fsId == BAD; i++)
if (fsArray->Detect && fsArray->Detect(R, BlockSize)) fsId = i;
if (fsArray[i].Detect && fsArray[i].Detect(R, BlockSize))
fsId = i;
if (fsId == BAD) {
ErrorVfs("%#Failed to mount device %s: no file system found.\n", ColorLightRed, Name)
Error("VFS", "%#Failed to mount device %s: no file system found.\n", ColorLightRed, Name)
return 0; // No file system, no good
}
@ -131,14 +129,22 @@ uint8 VfsMount (string Name, DevReadRoutine R, DevWriteRoutine W, uint32 BlockSi
if (!success) return 0;
}
memcpy(mpArray[mpCount].Name, Name, sizeof(char) * 128);
memcpy(mpArray[mpIndex].Name, Name, sizeof(char) * MAX_MOUNTPOINTNAME_LEN);
LogVfs("Mounted device %#%s", ColorWhite, Name);
// Tell file system to mount the device
if (fsArray[mpArray[mpIndex].FsId].MountDevice)
fsArray[mpArray[mpIndex].FsId].MountDevice(&mpArray[mpIndex]);
Log("VFS", "Mounted device %#%s%# using the %#%s%# file system.\n", ColorWhite, Name,
ColorLightGray, ColorWhite, fsArray[fsId].Name, ColorLightGray);
return 1;
}
void VfsUnmount (uint32 dev_id)
{
if (fsArray[mpArray[dev_id].FsId].UnmountDevice)
fsArray[mpArray[dev_id].FsId].UnmountDevice(&mpArray[dev_id]);
mpArray[dev_id].Id = BAD;
mpCount--;
}