139 lines
6.6 KiB
C
139 lines
6.6 KiB
C
#pragma once
|
|
// io_file.h - VFS layer
|
|
// Filesystem drivers register an IO_FILESYSTEM vtable via IoRegisterFilesystem.
|
|
// Kernel and user code open files through IoOpenFile.
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <ke_status.h>
|
|
#include <ob_object.h>
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// KDIR_ENTRY - one directory entry returned by ReadDir / SYS_READ_DIR
|
|
// Attribute flags match FAT32/Win32: ATTR_DIRECTORY=0x10, ATTR_ARCHIVE=0x20
|
|
// ---------------------------------------------------------------------------
|
|
#define KDIR_NAME_MAX 260
|
|
|
|
typedef struct KDIR_ENTRY {
|
|
char Name[KDIR_NAME_MAX]; // null-terminated filename
|
|
uint8_t Attr; // FAT attribute byte (0x10=dir, 0x20=file)
|
|
uint32_t FileSize; // 0 for directories
|
|
} KDIR_ENTRY;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Filesystem driver vtable - filled in by fat32.sys (or any future fs driver)
|
|
// ---------------------------------------------------------------------------
|
|
typedef struct IO_FILESYSTEM {
|
|
// Open a file by absolute path (e.g. "apps\\shell.exe").
|
|
// Returns an opaque per-file driver cookie in* fileCtx.
|
|
KSTATUS (*Open)(struct IO_FILESYSTEM* fs,
|
|
const char* path, void **fileCtx);
|
|
|
|
// Read up to 'size' bytes at 'offset' into 'buf'.
|
|
// Sets* bytesRead to actual bytes copied.
|
|
KSTATUS (*Read)(struct IO_FILESYSTEM* fs, void* fileCtx,
|
|
uint64_t offset, void* buf,
|
|
size_t size, size_t* bytesRead);
|
|
|
|
KSTATUS (*Write)(struct IO_FILESYSTEM* fs, void* fileCtx,
|
|
uint64_t offset, const void* buf,
|
|
size_t size, size_t* bytesWritten);
|
|
|
|
// Create a new file at 'path'; returns an open fileCtx on success.
|
|
KSTATUS (*Create)(struct IO_FILESYSTEM* fs, const char* path,
|
|
void** fileCtx);
|
|
|
|
// Delete the file at 'path'.
|
|
KSTATUS (*Delete)(struct IO_FILESYSTEM* fs, const char* path);
|
|
|
|
// Truncate or extend the file to exactly 'newSize' bytes.
|
|
KSTATUS (*Truncate)(struct IO_FILESYSTEM* fs, void* fileCtx,
|
|
uint64_t newSize);
|
|
|
|
// Return the file size in bytes.
|
|
uint64_t (*GetSize)(struct IO_FILESYSTEM* fs, void* fileCtx);
|
|
|
|
// Release any resources the driver allocated for fileCtx.
|
|
void (*Close)(struct IO_FILESYSTEM* fs, void* fileCtx);
|
|
|
|
// Optional driver-private data.
|
|
void* DriverCtx;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Directory enumeration (optional - may be NULL if not supported)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Open a directory iterator for 'dirPath' (relative or drive-prefixed).
|
|
// Returns an opaque per-iteration driver cookie in *dirCtx.
|
|
KSTATUS (*OpenDir)(struct IO_FILESYSTEM* fs,
|
|
const char* dirPath, void** dirCtx);
|
|
|
|
// Advance to the next entry.
|
|
// Fills 'out' with name, attributes, and size.
|
|
// Returns STATUS_NO_MORE_FILES when the directory is exhausted.
|
|
KSTATUS (*ReadDir)(struct IO_FILESYSTEM* fs, void* dirCtx,
|
|
struct KDIR_ENTRY* out);
|
|
|
|
// Release resources allocated by OpenDir.
|
|
void (*CloseDir)(struct IO_FILESYSTEM* fs, void* dirCtx);
|
|
} IO_FILESYSTEM;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// FILE_OBJECT - the kernel object returned to callers
|
|
// ---------------------------------------------------------------------------
|
|
typedef struct FILE_OBJECT {
|
|
OBJECT Header; // must be first
|
|
IO_FILESYSTEM* Fs;
|
|
void* FileCtx; // driver cookie
|
|
uint64_t Size;
|
|
uint64_t Offset; // current position for sequential reads
|
|
} FILE_OBJECT;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// DIR_OBJECT - kernel object for an open directory iterator
|
|
// ---------------------------------------------------------------------------
|
|
typedef struct DIR_OBJECT {
|
|
OBJECT Header; // must be first
|
|
IO_FILESYSTEM* Fs;
|
|
void* DirCtx; // driver cookie (opaque iteration state)
|
|
} DIR_OBJECT;
|
|
|
|
typedef struct IO_VOLUME_PARAMS {
|
|
uint8_t DiskIndex; // which disk in HAL_DISK table
|
|
uint64_t PartitionLba; // starting LBA of this partition
|
|
uint64_t PartitionSize; // sectors, for bounds checking
|
|
} IO_VOLUME_PARAMS;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Registration - called by fat32.sys from DriverEntry
|
|
// ---------------------------------------------------------------------------
|
|
// driveIndex matches the index passed to IoRegisterDisk.
|
|
__declspec(dllexport) KSTATUS IoRegisterFilesystem(uint8_t driveIndex, IO_FILESYSTEM* fs, const IO_VOLUME_PARAMS *vol);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Kernel-side API
|
|
// ---------------------------------------------------------------------------
|
|
__declspec(dllexport) KSTATUS IoOpenFile(const char* path, FILE_OBJECT **out);
|
|
__declspec(dllexport) KSTATUS IoReadFile(FILE_OBJECT* file, void* buf,
|
|
size_t size, size_t* bytesRead);
|
|
__declspec(dllexport) KSTATUS IoWriteFile (FILE_OBJECT* file, const void* buf,
|
|
size_t size, size_t* bytesWritten);
|
|
__declspec(dllexport) KSTATUS IoCreateFile (const char* path, FILE_OBJECT** out);
|
|
__declspec(dllexport) KSTATUS IoDeleteFile (const char* path);
|
|
__declspec(dllexport) KSTATUS IoTruncateFile(FILE_OBJECT* file, uint64_t newSize);
|
|
__declspec(dllexport) KSTATUS IoSeekFile(FILE_OBJECT* file, uint64_t offset);
|
|
__declspec(dllexport) uint64_t IoGetFileSize(FILE_OBJECT* file);
|
|
__declspec(dllexport) void IoCloseFile(FILE_OBJECT* file);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Directory enumeration API
|
|
// ---------------------------------------------------------------------------
|
|
__declspec(dllexport) KSTATUS IoOpenDir (const char* dirPath, DIR_OBJECT** out);
|
|
__declspec(dllexport) KSTATUS IoReadDir (DIR_OBJECT* dir, KDIR_ENTRY* entryOut);
|
|
__declspec(dllexport) void IoCloseDir(DIR_OBJECT* dir);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Current Working Directory
|
|
// ---------------------------------------------------------------------------
|
|
__declspec(dllexport) KSTATUS IoSetCwd(const char* path);
|
|
__declspec(dllexport) KSTATUS IoGetCwd(char* buf, size_t size);
|