[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:
202
Kernel/hal/filesys/initrd/initrd.c
Normal file
202
Kernel/hal/filesys/initrd/initrd.c
Normal 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 ¤t->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];
|
||||
}
|
||||
|
56
Kernel/hal/filesys/initrd/initrd.h
Normal file
56
Kernel/hal/filesys/initrd/initrd.h
Normal 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_ */
|
Reference in New Issue
Block a user