WKern
Loading...
Searching...
No Matches
fileio.h File Reference

Definitions and declarations for FAT16 filesystem and ATA I/O. More...

#include <global.h>
#include <types/nums.h>

Go to the source code of this file.

Data Structures

struct  FaT16Bpb
 FAT16 BIOS Parameter Block (BPB) structure — describes volume layout. More...
struct  FaT16Info
 Parsed FAT16 filesystem layout and parameters. More...

Macros

#define ATA_IO_BASE   0x1F0
 ATA IO Base port for primary IDE channel.
#define ATA_CTRL   0x3F6
 ATA Control port.
#define ATA_STATUS   (ATA_IO_BASE + 7)
 ATA Status port (read)
#define ATA_COMMAND   (ATA_IO_BASE + 7)
 ATA Command port (write)
#define ATA_DRIVE   (ATA_IO_BASE + 6)
 ATA Drive/head select port.
#define ATA_ERROR   (ATA_IO_BASE + 1)
 ATA Error register port (read)
#define ATA_SECCNT   (ATA_IO_BASE + 2)
 ATA Sector count register port.
#define ATA_LBA_LOW   (ATA_IO_BASE + 3)
 ATA LBA low byte port.
#define ATA_LBA_MID   (ATA_IO_BASE + 4)
 ATA LBA mid byte port.
#define ATA_LBA_HI   (ATA_IO_BASE + 5)
 ATA LBA high byte port.
#define ATA_DATA   (ATA_IO_BASE + 0)
 ATA Data port (read/write 16-bit)
#define ATA_CMD_READ_SECTORS   0x20
 ATA command to read sectors.
#define ATA_SR_BSY   0x80
 ATA status register BSY (busy) bit.
#define ATA_SR_DRQ   0x08
 ATA status register DRQ (data request) bit.
#define ATA_SR_ERR   0x01
 ATA status register ERR (error) bit.
#define ATA_WRITE_CMD   0x30
 ATA command to write sectors.

Functions

void AtaReadSector (u32 lba, u8 *buffer)
 Reads a 512-byte sector from ATA disk into buffer.
void AtaWriteSector (u32 lba, const u8 *buffer)
 Writes a 512-byte sector to ATA disk from buffer.
u32 FindFat16Partition (void)
 Finds the FAT16 partition on disk.
void Writefile (const char *filename, const char *ext, const char *data, u32 size)
 Writes data to a FAT16 file, replacing its contents.
void Fat16Ls (void)
 Lists files in the FAT16 root directory.
u32 Fat16Mount (u32 partition_lba)
 Mounts FAT16 partition and reads filesystem info.
void Fileconts (const char *filename, const char *ext)
 Prints contents of a FAT16 file.
void Mkfile (const char *filename, const char *ext)
 Creates a new empty file in the FAT16 root directory.
void Fat16RemoveFile (const char *filename, const char *ext)
 Removes a file from the FAT16 root directory and frees clusters.

Variables

struct FaT16Info fat16
 Global FAT16 filesystem info struct.
volatile u8 irq14stat
 IRQ status flags for IRQ14 and IRQ15 (primary and secondary IDE interrupts)
volatile u8 irq15stat

Detailed Description

Definitions and declarations for FAT16 filesystem and ATA I/O.

Definition in file fileio.h.

Macro Definition Documentation

◆ ATA_CMD_READ_SECTORS

#define ATA_CMD_READ_SECTORS   0x20

ATA command to read sectors.

Definition at line 61 of file fileio.h.

◆ ATA_COMMAND

#define ATA_COMMAND   (ATA_IO_BASE + 7)

ATA Command port (write)

Definition at line 37 of file fileio.h.

◆ ATA_CTRL

#define ATA_CTRL   0x3F6

ATA Control port.

Definition at line 31 of file fileio.h.

◆ ATA_DATA

#define ATA_DATA   (ATA_IO_BASE + 0)

ATA Data port (read/write 16-bit)

Definition at line 58 of file fileio.h.

◆ ATA_DRIVE

#define ATA_DRIVE   (ATA_IO_BASE + 6)

ATA Drive/head select port.

Definition at line 40 of file fileio.h.

◆ ATA_ERROR

#define ATA_ERROR   (ATA_IO_BASE + 1)

ATA Error register port (read)

Definition at line 43 of file fileio.h.

◆ ATA_IO_BASE

#define ATA_IO_BASE   0x1F0

ATA IO Base port for primary IDE channel.

Definition at line 28 of file fileio.h.

◆ ATA_LBA_HI

#define ATA_LBA_HI   (ATA_IO_BASE + 5)

ATA LBA high byte port.

Definition at line 55 of file fileio.h.

◆ ATA_LBA_LOW

#define ATA_LBA_LOW   (ATA_IO_BASE + 3)

ATA LBA low byte port.

Definition at line 49 of file fileio.h.

◆ ATA_LBA_MID

#define ATA_LBA_MID   (ATA_IO_BASE + 4)

ATA LBA mid byte port.

Definition at line 52 of file fileio.h.

◆ ATA_SECCNT

#define ATA_SECCNT   (ATA_IO_BASE + 2)

ATA Sector count register port.

Definition at line 46 of file fileio.h.

◆ ATA_SR_BSY

#define ATA_SR_BSY   0x80

ATA status register BSY (busy) bit.

Definition at line 64 of file fileio.h.

◆ ATA_SR_DRQ

#define ATA_SR_DRQ   0x08

ATA status register DRQ (data request) bit.

Definition at line 67 of file fileio.h.

◆ ATA_SR_ERR

#define ATA_SR_ERR   0x01

ATA status register ERR (error) bit.

Definition at line 70 of file fileio.h.

◆ ATA_STATUS

#define ATA_STATUS   (ATA_IO_BASE + 7)

ATA Status port (read)

Definition at line 34 of file fileio.h.

◆ ATA_WRITE_CMD

#define ATA_WRITE_CMD   0x30

ATA command to write sectors.

Definition at line 73 of file fileio.h.

Function Documentation

◆ AtaReadSector()

void AtaReadSector ( u32 lba,
u8 * buffer )

Reads a 512-byte sector from ATA disk into buffer.

Parameters
lbaLogical block address of sector.
bufferPointer to buffer to store sector data (must be >= 512 bytes).

Reads a 512-byte sector from ATA disk into buffer.

Sends commands to the ATA device to read one sector specified by the logical block address (LBA). The read sector data is stored in the provided buffer.

Parameters
lbaThe logical block address of the sector to read.
bufferPointer to a 512-byte buffer where the read data will be stored.
Note
Assumes the buffer is valid and large enough for one sector.
Interrupts and device control signals are managed internally.

Definition at line 65 of file ATA.c.

◆ AtaWriteSector()

void AtaWriteSector ( u32 lba,
const u8 * buffer )

Writes a 512-byte sector to ATA disk from buffer.

Parameters
lbaLogical block address of sector.
bufferPointer to buffer containing data to write (must be >= 512 bytes).

Writes a 512-byte sector to ATA disk from buffer.

Sends commands to the ATA device to write one sector specified by the logical block address (LBA). The data to write is taken from the provided buffer.

Parameters
lbaThe logical block address of the sector to write.
bufferPointer to a 512-byte buffer containing the data to write.
Note
Assumes the buffer is valid and contains at least one sector of data.
This function blocks until the write completes and the cache is flushed.

Definition at line 100 of file ATA.c.

◆ Fat16Ls()

void Fat16Ls ( void )

Lists files in the FAT16 root directory.

Lists files in the FAT16 root directory.

Reads the root directory sectors of the FAT16 filesystem and prints the file names, their starting cluster numbers, and file sizes.

It handles:

  • End of directory detection
  • Skipping deleted entries
  • Decoding little-endian cluster and size fields

Output format per file: File: [filename].[ext] | Cluster: [cluster] | Size: [size in bytes]

Definition at line 37 of file ls.c.

◆ Fat16Mount()

u32 Fat16Mount ( u32 partition_lba)

Mounts FAT16 partition and reads filesystem info.

Parameters
partition_lbaLBA address of FAT16 partition start.
Returns
1 on success, 0 on failure.

Mounts FAT16 partition and reads filesystem info.

Reads the boot sector, validates the FAT16 signature, and initializes the FAT16 info struct with important filesystem layout details.

Parameters
partition_lbaLogical block address of the FAT16 partition.
Returns
1 on success, 0 on failure (invalid boot sector signature).

Prints mount status and key filesystem layout info via Kprintf.

Definition at line 67 of file fat16_mnt.c.

◆ Fat16RemoveFile()

void Fat16RemoveFile ( const char * filename,
const char * ext )

Removes a file from the FAT16 root directory and frees clusters.

Parameters
filenameFile name (8 chars).
extFile extension (3 chars).

Removes a file from the FAT16 root directory and frees clusters.

This function searches the FAT16 root directory for a file matching the specified filename and extension. If found, it marks the directory entry as deleted and frees all associated FAT clusters by clearing their entries in the FAT table.

Parameters
filenameThe 8-character filename (without extension).
extThe 3-character file extension.
Note
The function assumes the FAT16 filesystem structure is accessible through the global fat16 variable.

Definition at line 38 of file rmfile.c.

◆ Fileconts()

void Fileconts ( const char * filename,
const char * ext )

Prints contents of a FAT16 file.

Parameters
filenameName of the file (8 chars).
extFile extension (3 chars).

Prints contents of a FAT16 file.

This function searches the FAT16 root directory for a file matching the specified filename and extension. If found, it reads the first data cluster of the file and prints its contents as ASCII characters to the console. Non-printable characters are displayed as dots ('.'). The reading stops either at the end of the cluster or when a FAT16 EOF marker (0x1A) is found.

Parameters
filenameThe 8-character filename (without extension).
extThe 3-character file extension.
Note
The function assumes the FAT16 filesystem structure is accessible through the global fat16 variable.

Definition at line 40 of file printconts.c.

◆ FindFat16Partition()

u32 FindFat16Partition ( void )

Finds the FAT16 partition on disk.

Returns
LBA of FAT16 partition start.

Finds the FAT16 partition on disk.

Reads the MBR sector, verifies the signature, then scans all partitions for a FAT16 type (0x04 or 0x06).

Prints debug info about partitions to kernel log.

Returns
LBA of the FAT16 partition start or 0 if none found or invalid MBR.

< Invalid MBR signature

< Return FAT16 partition start LBA

< No FAT16 partition found

Definition at line 69 of file MBR.c.

◆ Mkfile()

void Mkfile ( const char * filename,
const char * ext )

Creates a new empty file in the FAT16 root directory.

Parameters
filenameFile name (8 chars).
extFile extension (3 chars).

Creates a new empty file in the FAT16 root directory.

This function searches for a free root directory entry and a free cluster, then creates a new directory entry for the specified filename and extension. It initializes the directory entry with default attributes and zero file size.

Parameters
filenameThe 8-character filename (without extension).
extThe 3-character file extension.
Note
The function assumes FAT16 filesystem data is accessible through the global fat16 structure.
Warning
If no free directory entry or cluster is available, the function prints an error message and returns without creating the file.

Definition at line 41 of file mkfile.c.

◆ Writefile()

void Writefile ( const char * filename,
const char * ext,
const char * data,
u32 size )

Writes data to a FAT16 file, replacing its contents.

Parameters
filename8.3 filename (name only).
extFile extension (3 chars).
dataPointer to data buffer to write.
sizeSize of data to write in bytes.

Writes data to a FAT16 file, replacing its contents.

Searches for the file specified by filename and extension in the FAT16 root directory. If found, writes up to 512 bytes of data to the file's first cluster sector and updates the file size in the directory entry.

Parameters
filenameThe 8-character filename (without extension).
extThe 3-character file extension.
dataPointer to the data buffer to write.
sizeNumber of bytes to write from the data buffer (max 512 bytes).
Note
This function only writes to the first cluster of the file. Writing beyond the first cluster or appending is not handled.
Assumes the global fat16 structure is properly initialized and accessible.

Definition at line 41 of file write.c.

Variable Documentation

◆ fat16

struct FaT16Info fat16
extern

Global FAT16 filesystem info struct.

Global FAT16 filesystem info struct.

Definition at line 25 of file fat16_mnt.c.

◆ irq14stat

volatile u8 irq14stat
extern

IRQ status flags for IRQ14 and IRQ15 (primary and secondary IDE interrupts)

Definition at line 22 of file irqflags.c.

◆ irq15stat

volatile u8 irq15stat
extern

Definition at line 23 of file irqflags.c.