fat12

package
v1.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package fat12 provides utilities to interact with, manipulate and create a FAT12 filesystem on a block device or disk image. It also exports the shared BPB types and the FATTable interface used by the fat16 package.

References:

https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system
https://www.cs.fsu.edu/~cop4610t/assignments/project3/spec/fatspec.pdf
https://wiki.osdev.org/FAT

Index

Constants

View Source
const (
	// KB represents one KB
	KB int64 = 1024
	// MB represents one MB
	MB int64 = 1024 * KB
	// GB represents one GB
	GB int64 = 1024 * MB

	// Fat12MaxSize is the maximum size of a FAT12 filesystem in bytes.
	// FAT12 supports at most 4084 clusters; at 64 sectors/cluster × 512 bytes = 32 KB/cluster
	// that gives ~128 MB, but in practice FAT12 is used only on small volumes.
	Fat12MaxSize int64 = 128 * MB
	// Fat16MaxSize is the maximum size of a FAT16 filesystem in bytes (2 GB).
	Fat16MaxSize int64 = 2 * GB
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Directory

type Directory struct {
	// contains filtered or unexported fields
}

Directory represents a single directory in a FAT12/FAT16 filesystem

func (*Directory) Info

func (de *Directory) Info() (iofs.FileInfo, error)

func (*Directory) IsDir

func (de *Directory) IsDir() bool

func (*Directory) Name

func (de *Directory) Name() string

func (*Directory) Type

func (de *Directory) Type() iofs.FileMode

type DiskRange

type DiskRange struct {
	Offset uint64
	Length uint64
}

type Dos20BPB

type Dos20BPB struct {
	BytesPerSector       SectorSize // always 512 for FAT12/16
	SectorsPerCluster    uint8
	ReservedSectors      uint16
	FatCount             uint8
	RootDirectoryEntries uint16 // non-zero for FAT12/FAT16; zero for FAT32
	TotalSectors         uint16 // non-zero if total sectors fit in 16 bits; else see Dos331BPB
	MediaType            uint8
	SectorsPerFat        uint16 // for FAT12/16; zero for FAT32 (see dos71EBPB)
}

Dos20BPB is a DOS 2.0 BIOS Parameter Block structure, shared by FAT12, FAT16, and FAT32.

func Dos20BPBFromBytes

func Dos20BPBFromBytes(b []byte) (*Dos20BPB, error)

Dos20BPBFromBytes reads the DOS 2.0 BPB from exactly 13 bytes.

func (*Dos20BPB) ToBytes

func (bpb *Dos20BPB) ToBytes() []byte

ToBytes serialises the DOS 2.0 BPB to 13 bytes.

type Dos40EBPB

type Dos40EBPB struct {
	Dos331BPB          *Dos331BPB
	DriveNumber        uint8
	ReservedFlags      uint8
	ExtBootSignature   uint8 // 0x28 (short) or 0x29 (long, includes label+type)
	VolumeSerialNumber uint32
	VolumeLabel        string // 11 bytes, padded with spaces; only present when ExtBootSignature == 0x29
	FileSystemType     string // 8 bytes, e.g. "FAT12   " or "FAT16   "; only when 0x29
}

Dos40EBPB is the Extended BIOS Parameter Block used by FAT12 and FAT16. It wraps the DOS 3.31 BPB and adds drive identification, volume serial number, volume label, and filesystem type string.

func Dos40EBPBFromBytes

func Dos40EBPBFromBytes(b []byte) (*Dos40EBPB, int, error)

Dos40EBPBFromBytes parses a Dos40EBPB from a byte slice. The slice must be at least 26 bytes (short form) or 51 bytes (long form). Returns the parsed struct and the number of bytes consumed.

func Dos40EBPBFromBytesOnly

func Dos40EBPBFromBytesOnly(b []byte) (*Dos40EBPB, error)

Dos40EBPBFromBytesOnly is a convenience wrapper around Dos40EBPBFromBytes that discards the consumed-byte count. Useful when the caller does not need to know where the BPB ends (e.g. when only reading, not writing).

func (*Dos40EBPB) ToBytes

func (bpb *Dos40EBPB) ToBytes() ([]byte, error)

ToBytes serialises the Dos40EBPB. Returns an error if the volume label or filesystem type strings are too long or contain non-ASCII characters.

func (*Dos40EBPB) TotalSectors

func (bpb *Dos40EBPB) TotalSectors() uint32

TotalSectors returns the effective total sector count, preferring the 32-bit field.

type Dos331BPB

type Dos331BPB struct {
	Dos20BPB        *Dos20BPB
	SectorsPerTrack uint16
	Heads           uint16
	HiddenSectors   uint32
	TotalSectors32  uint32 // used when TotalSectors in Dos20BPB is 0
}

Dos331BPB is the DOS 3.31 BIOS Parameter Block, shared by FAT12, FAT16, and FAT32.

func Dos331BPBFromBytes

func Dos331BPBFromBytes(b []byte) (*Dos331BPB, error)

Dos331BPBFromBytes reads the DOS 3.31 BPB from exactly 25 bytes.

func (*Dos331BPB) Equal

func (bpb *Dos331BPB) Equal(a *Dos331BPB) bool

Equal compares two Dos331BPB values.

func (*Dos331BPB) ToBytes

func (bpb *Dos331BPB) ToBytes() []byte

ToBytes serialises the DOS 3.31 BPB to 25 bytes.

type FATTable

type FATTable interface {
	// ClusterValue returns the value stored for cluster n.
	ClusterValue(n uint32) uint32
	// SetCluster sets the value for cluster n.
	SetCluster(n uint32, val uint32)
	// IsEOC reports whether val is an end-of-chain marker.
	IsEOC(val uint32) bool
	// EOCMarker returns the canonical end-of-chain value to write.
	EOCMarker() uint32
	// UnusedMarker returns the value that marks a free cluster.
	UnusedMarker() uint32
	// MaxCluster returns the highest valid cluster index.
	MaxCluster() uint32
	// FATID returns the media-descriptor word stored at FAT[0].
	FATID() uint32
	// RootDirCluster returns the cluster that holds the root directory.
	// For FAT12/16 this is always 2 (though the root dir is stored in
	// a fixed region, not via the cluster chain).
	RootDirCluster() uint32
	// Size returns the on-disk size of one FAT copy in bytes.
	Size() uint32
	// FromBytes populates the table from raw FAT bytes read from disk.
	FromBytes(b []byte)
	// Bytes serialises the table to raw FAT bytes ready to write to disk.
	Bytes() []byte
}

FATTable is the interface that abstracts on-disk FAT entry encoding. fat12 provides a 12-bit implementation; fat16 and fat32 each provide their own implementation in their respective packages. All implementations use uint32 as the in-memory cluster value for uniformity.

type File

type File struct {
	// contains filtered or unexported fields
}

File represents a single file in a FAT32 filesystem

func (*File) Close

func (fl *File) Close() error

Close close the file

func (*File) GetClusterChain

func (fl *File) GetClusterChain() ([]uint32, error)

Get the full cluster chain of the File. Getting this file system internal info can be beneficial for some low-level operations, such as: - Performing secure erase. - Detecting file fragmentation. - Passing Disk locations to a different tool that can work with it.

func (*File) GetDiskRanges

func (fl *File) GetDiskRanges() ([]DiskRange, error)

Get the disk ranges occupied by the File. Returns an array of disk ranges, where each entry is a contiguous area on disk. This information is similar to that returned by GetClusterChain, just in a different format, directly returning disk ranges instead of FAT clusters.

func (File) Info

func (de File) Info() (iofs.FileInfo, error)

func (File) IsDir

func (de File) IsDir() bool

func (*File) IsHidden

func (fl *File) IsHidden() bool

func (*File) IsReadOnly

func (fl *File) IsReadOnly() bool

func (*File) IsSystem

func (fl *File) IsSystem() bool

func (File) Name

func (de File) Name() string

func (*File) Read

func (fl *File) Read(b []byte) (int, error)

Read reads up to len(b) bytes from the File. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF reads from the last known offset in the file from last read or write and increments the offset by the number of bytes read. Use Seek() to set at a particular point

func (*File) Seek

func (fl *File) Seek(offset int64, whence int) (int64, error)

Seek set the offset to a particular point in the file

func (*File) SetHidden

func (fl *File) SetHidden(on bool) error

func (*File) SetReadOnly

func (fl *File) SetReadOnly(on bool) error

func (*File) SetSystem

func (fl *File) SetSystem(on bool) error

func (*File) Stat

func (fl *File) Stat() (iofs.FileInfo, error)

Stat returns a fs.FileInfo structure describing file

func (File) Type

func (de File) Type() iofs.FileMode

func (*File) Write

func (fl *File) Write(p []byte) (int, error)

Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. returns a non-nil error when n != len(b) writes to the last known offset in the file from last read or write and increments the offset by the number of bytes read. Use Seek() to set at a particular point

type FileInfo

type FileInfo struct {
	// contains filtered or unexported fields
}

FileInfo represents the information for an individual file it fulfills os.FileInfo interface

func (FileInfo) IsDir

func (fi FileInfo) IsDir() bool

IsDir abbreviation for Mode().IsDir()

func (FileInfo) ModTime

func (fi FileInfo) ModTime() time.Time

ModTime modification time

func (FileInfo) Mode

func (fi FileInfo) Mode() os.FileMode

Mode returns file mode

func (FileInfo) Name

func (fi FileInfo) Name() string

Name base name of the file

will return the long name of the file. If none exists, returns the shortname and extension

func (FileInfo) ShortName

func (fi FileInfo) ShortName() string

ShortName just the 8.3 short name of the file

func (FileInfo) Size

func (fi FileInfo) Size() int64

Size length in bytes for regular files

func (FileInfo) Sys

func (fi FileInfo) Sys() interface{}

Sys underlying data source - not supported yet and so will return nil

type FileSystem

type FileSystem struct {

	// WriteBootSectorFn, when non-nil, is called by WriteBootSector instead of
	// the default fat12/16 boot-sector writer. FAT32 uses this to write the
	// dos71EBPB-format boot sector (including backup sector).
	WriteBootSectorFn func() error
	// AfterWriteFAT, when non-nil, is called at the end of WriteFat. FAT32
	// uses this to flush the FSInformationSector after every FAT change.
	AfterWriteFAT func() error
	// contains filtered or unexported fields
}

FileSystem implements filesystem.FileSystem for FAT12 (and, via embedding, FAT16 and FAT32).

func Create

func Create(b backend.Storage, size, start, blocksize int64, volumeLabel string, reproducible bool) (*FileSystem, error)

Create creates a FAT12 filesystem.

func NewFileSystem

func NewFileSystem(
	b backend.Storage,
	bpb *Dos40EBPB,
	tbl FATTable,
	dataStart uint32,
	bytesPerCluster int,
	size, start int64,
	rootDirOffset int64,
	rootDirMaxEntries int,
	fatPrimaryStart, fatSecondaryStart uint64,
) *FileSystem

NewFileSystem constructs a FileSystem with caller-supplied table and layout parameters. Called by fat16 and fat32 to reuse all high-level methods.

bpb may be nil when the caller (fat32) manages its own boot-sector format and supplies WriteBootSectorFn / AfterWriteFAT hooks after construction.

func Read

func Read(b backend.Storage, size, start, blocksize int64) (*FileSystem, error)

Read reads a FAT12 filesystem from the backend. Returns an error if the image is not a valid FAT12 filesystem (e.g. it is FAT16/FAT32), so that disk.GetFilesystem() can fall through to the next candidate.

func (*FileSystem) Backend

func (fs *FileSystem) Backend() backend.Storage

Backend returns the storage backend. Used by fat32 to write FSIS.

func (*FileSystem) BytesPerCluster

func (fs *FileSystem) BytesPerCluster() int

BytesPerCluster returns the cluster size in bytes.

func (*FileSystem) Chmod

func (fs *FileSystem) Chmod(_ string, _ os.FileMode) error

func (*FileSystem) Chown

func (fs *FileSystem) Chown(_ string, _, _ int) error

func (*FileSystem) Chtimes

func (fs *FileSystem) Chtimes(p string, ctime, atime, mtime time.Time) error

func (*FileSystem) Close

func (fs *FileSystem) Close() error

func (*FileSystem) DataStart

func (fs *FileSystem) DataStart() uint32

DataStart returns the byte offset of the first data cluster.

func (*FileSystem) Equal

func (fs *FileSystem) Equal(a *FileSystem) bool

Equal compares two FileSystems.

func (*FileSystem) GetArchiveBit added in v1.9.1

func (fs *FileSystem) GetArchiveBit(p string) (bool, error)

GetArchiveBit returns the current state of the FAT archive attribute.

func (*FileSystem) Label

func (fs *FileSystem) Label() string
func (fs *FileSystem) Link(_, _ string) error

func (*FileSystem) Mkdir

func (fs *FileSystem) Mkdir(p string) error

func (*FileSystem) Mknod

func (fs *FileSystem) Mknod(_ string, _ uint32, _ int) error

func (*FileSystem) Open

func (fs *FileSystem) Open(p string) (iofs.File, error)

func (*FileSystem) OpenFile

func (fs *FileSystem) OpenFile(p string, flag int) (filesystem.File, error)

func (*FileSystem) ReadDir

func (fs *FileSystem) ReadDir(p string) ([]iofs.DirEntry, error)

func (*FileSystem) ReadFile

func (fs *FileSystem) ReadFile(name string) ([]byte, error)

func (*FileSystem) Remove

func (fs *FileSystem) Remove(pathname string) error

func (*FileSystem) Rename

func (fs *FileSystem) Rename(oldpath, newpath string) error

func (*FileSystem) SetArchiveBit added in v1.9.1

func (fs *FileSystem) SetArchiveBit(p string, set bool) error

SetArchiveBit sets or clears the FAT archive attribute on the named file or directory.

func (*FileSystem) SetLabel

func (fs *FileSystem) SetLabel(volumeLabel string) error

func (*FileSystem) SetRootDirLabel

func (fs *FileSystem) SetRootDirLabel(volumeLabel string) error

SetRootDirLabel updates the volume-label directory entry in the root directory without touching the boot sector. Used by fat32, which manages its own boot sector format, to reuse the root-directory update logic.

func (*FileSystem) Start

func (fs *FileSystem) Start() int64

Start returns the filesystem start offset within the backend. Used by fat32.

func (*FileSystem) Stat

func (fs *FileSystem) Stat(name string) (iofs.FileInfo, error)
func (fs *FileSystem) Symlink(_, _ string) error

func (*FileSystem) Type

func (fs *FileSystem) Type() filesystem.Type

Type returns filesystem.TypeFat12. fat16.FileSystem overrides this.

func (*FileSystem) WriteBootSector

func (fs *FileSystem) WriteBootSector() error

WriteBootSector writes the boot sector to disk. When WriteBootSectorFn is set (by fat32), that function is called instead of the default fat12/16 writer.

func (*FileSystem) WriteFat

func (fs *FileSystem) WriteFat() error

WriteFat writes both FAT copies to disk. When AfterWriteFAT is set (by fat32), it is called afterwards to flush the FSInformationSector.

type MsdosMediaType

type MsdosMediaType uint8

MsdosMediaType is the (mostly unused) media type.

const (
	// Media8InchDrDos for single-sided 250KB DR-DOS disks
	Media8InchDrDos MsdosMediaType = 0xe5
	// Media525InchTandy for 5.25 inch floppy disks for Tandy
	Media525InchTandy MsdosMediaType = 0xed
	// MediaCustomPartitionsDrDos for non-standard custom DR-DOS partitions
	MediaCustomPartitionsDrDos MsdosMediaType = 0xee
	// MediaCustomSuperFloppyDrDos for non-standard custom superfloppy disks for DR-DOS
	MediaCustomSuperFloppyDrDos MsdosMediaType = 0xef
	// Media35Inch for standard 1.44MB and 2.88MB 3.5 inch floppy disks
	Media35Inch MsdosMediaType = 0xf0
	// MediaDoubleDensityAltos for double-density floppy disks for Altos only
	MediaDoubleDensityAltos MsdosMediaType = 0xf4
	// MediaFixedDiskAltos for fixed disk 1.95MB for Altos only
	MediaFixedDiskAltos MsdosMediaType = 0xf5
	// MediaFixedDisk for standard fixed disks
	MediaFixedDisk MsdosMediaType = 0xf8
)

type SectorSize

type SectorSize uint16

SectorSize indicates what the sector size in bytes is

const (
	// SectorSize512 is a sector size of 512 bytes
	SectorSize512 SectorSize = 512
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL