[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:
80
Kernel/hal/filesys/fat/fat.c
Normal file
80
Kernel/hal/filesys/fat/fat.c
Normal 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);
|
||||
}
|
135
Kernel/hal/filesys/fat/fat.h
Normal file
135
Kernel/hal/filesys/fat/fat.h
Normal 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_ */
|
25
Kernel/hal/filesys/fat/fat12.c
Normal file
25
Kernel/hal/filesys/fat/fat12.c
Normal 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);
|
||||
}
|
26
Kernel/hal/filesys/fat/fat16.c
Normal file
26
Kernel/hal/filesys/fat/fat16.c
Normal 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);
|
||||
}
|
||||
|
26
Kernel/hal/filesys/fat/fat32.c
Normal file
26
Kernel/hal/filesys/fat/fat32.c
Normal 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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user