adding reference impementation for reading files from fat
This commit is contained in:
parent
2c44bea9b1
commit
da1a2345a9
3 changed files with 167 additions and 10 deletions
117
src/reference_implementations/fat.zig
Normal file
117
src/reference_implementations/fat.zig
Normal file
|
@ -0,0 +1,117 @@
|
|||
const std = @import("std");
|
||||
|
||||
const Errors = error{ InvalidSyntax, ReadSectors, FileNotFound };
|
||||
|
||||
var buffer: [1024]u8 = undefined;
|
||||
|
||||
var g_Fat: []u8 = undefined;
|
||||
|
||||
const BootSector = extern struct {
|
||||
BootJumpInstruction: [3]u8 align(1),
|
||||
OemIdentifier: [8]u8 align(1),
|
||||
BytesPerSector: u16 align(1),
|
||||
SectorsPerCluster: u8 align(1),
|
||||
ReservedSectors: u16 align(1),
|
||||
FatCount: u8 align(1),
|
||||
DirEntryCount: u16 align(1),
|
||||
TotalSectors: u16 align(1),
|
||||
MediaDescriptorType: u8 align(1),
|
||||
SectorsPerFat: u16 align(1),
|
||||
SectorsPerTrack: u16 align(1),
|
||||
Heads: u16 align(1),
|
||||
HiddenSectors: u32 align(1),
|
||||
LargeSectorCount: u32 align(1),
|
||||
|
||||
// ebr
|
||||
DriveNumber: u8 align(1),
|
||||
_Reserved: u8 align(1),
|
||||
Signature: u8 align(1),
|
||||
VolumeId: u32 align(1),
|
||||
VolumeLaber: [11]u8 align(1),
|
||||
SystemId: [8]u8 align(1),
|
||||
};
|
||||
|
||||
var g_BootSector: BootSector = undefined;
|
||||
|
||||
const DirectoryEntry = extern struct {
|
||||
Name: [11]u8 align(1),
|
||||
Attributes: u8 align(1),
|
||||
_Reserved: u8 align(1),
|
||||
CreatedTimeTenths: u8 align(1),
|
||||
CreatedTime: u16 align(1),
|
||||
CreatedDate: u16 align(1),
|
||||
AccessedDate: u16 align(1),
|
||||
FirstClusterHigh: u16 align(1),
|
||||
ModifiedTime: u16 align(1),
|
||||
ModifiedDate: u16 align(1),
|
||||
FirstClusterLow: u16 align(1),
|
||||
Size: u32 align(1),
|
||||
};
|
||||
|
||||
var g_RootDirectory: []DirectoryEntry = undefined;
|
||||
|
||||
fn readStruct(reader: *std.fs.File.Reader, T: type) !T {
|
||||
var ret: [@sizeOf(T)]u8 = undefined;
|
||||
_ = try reader.*.read(&ret);
|
||||
return @bitCast(ret);
|
||||
}
|
||||
|
||||
fn readBootSector(disk: *std.fs.File) !void {
|
||||
var reader = disk.*.reader(&buffer);
|
||||
g_BootSector = try readStruct(&reader, BootSector);
|
||||
}
|
||||
|
||||
fn readSectors(disk: *std.fs.File, lba: u32, count: u32, bufferOut: *[]anyopaque) !void {
|
||||
const dest: []u8 = try std.heap.smp_allocator.alloc(u8, g_BootSector.BytesPerSector * count);
|
||||
defer std.heap.smp_allocator.free(dest);
|
||||
var stream = disk.readerStreaming(&buffer);
|
||||
try stream.seekTo(lba * g_BootSector.BytesPerSector);
|
||||
if (try stream.readStreaming(dest) != g_BootSector.BytesPerSector * count) return Errors.ReadSectors;
|
||||
@memcpy(@as(*[]u8, @ptrCast(bufferOut)).*, dest);
|
||||
}
|
||||
|
||||
fn readFat(disk: *std.fs.File) !void {
|
||||
g_Fat = try std.heap.smp_allocator.alloc(u8, g_BootSector.SectorsPerFat * g_BootSector.BytesPerSector);
|
||||
errdefer std.heap.smp_allocator.free(g_Fat);
|
||||
try readSectors(disk, g_BootSector.ReservedSectors, g_BootSector.SectorsPerFat, &g_Fat);
|
||||
}
|
||||
|
||||
fn readRootDirectory(disk: std.fs.File) !void {
|
||||
const lba: u32 = g_BootSector.ReservedSectors + g_BootSector.SectorsPerFat + g_BootSector.FatCount;
|
||||
const size: u32 = @sizeOf(DirectoryEntry) * g_BootSector.DirEntryCount;
|
||||
const sectors: u32 = (size / g_BootSector.BytesPerSector);
|
||||
if (@rem(size, g_BootSector.BytesPerSector > 0))
|
||||
sectors += 1;
|
||||
|
||||
g_RootDirectory = try std.heap.smp_allocator.alloc(DirectoryEntry, sectors * g_BootSector.BytesPerSector);
|
||||
errdefer std.heap.smp_allocator.free(g_RootDirectory);
|
||||
try readSectors(disk, lba, sectors, &g_RootDirectory);
|
||||
}
|
||||
|
||||
fn findFile(name: []const u8) !*DirectoryEntry {
|
||||
for (g_RootDirectory, 0..) |Entry, i|
|
||||
if (std.mem.eql(u8, Entry.Name, name))
|
||||
return &g_RootDirectory[i];
|
||||
return Errors.FileNotFound;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
if (std.os.argv.len < 3) {
|
||||
std.log.err("syntax: {s} <disk image> <file name>", .{std.os.argv[0]});
|
||||
return Errors.InvalidSyntax;
|
||||
}
|
||||
|
||||
var disk = try std.fs.cwd().openFileZ(std.os.argv[1], .{ .mode = .read_only });
|
||||
|
||||
_ = &disk;
|
||||
try readBootSector(&disk);
|
||||
|
||||
try readFat(&disk);
|
||||
defer std.heap.smp_allocator.free(g_Fat);
|
||||
|
||||
try readRootDirectory(disk);
|
||||
defer std.heap.smp_allocator.free(g_RootDirectory);
|
||||
|
||||
const fileEntry = try findFile(std.os.argv[2]);
|
||||
_ = fileEntry;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue