disk

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Copyright (c) 2025 Stefano Scafiti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

View Source
const (
	FAT1X_PART_NAME = 0x2B // Often for FAT12/16 partitions
	FAT32_PART_NAME = 0x47 // For FAT32 partitions
)

Partition type indicators

View Source
const (
	FAT_NAME1 = 0x36 // Likely a marker related to FAT12/16 detection
	FAT_NAME2 = 0x52 // FAT32 only marker
)

FAT filesystem specific names/markers

View Source
const (
	ATTR_RO     = 1  // Read-only
	ATTR_HIDDEN = 2  // Hidden
	ATTR_SYS    = 4  // System file
	ATTR_VOLUME = 8  // Volume label entry
	ATTR_DIR    = 16 // Directory
	ATTR_ARCH   = 32 // Archive bit
)

File/Directory Attributes (bit flags)

View Source
const (
	ATTR_NONE     = 0                                                                     // No attributes
	ATTR_UNUSED   = ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN                      // Attributes that are copied "as is" or potentially unused
	ATTR_EXT      = ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME                        // Extended attributes (Windows 95/NT)
	ATTR_EXT_MASK = ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME | ATTR_DIR | ATTR_ARCH // Mask for all extended attributes
)

Combined Attribute Masks

View Source
const (
	FAT12_BAD = 0x0FF7
	FAT12_EOC = 0x0FF8
	FAT16_BAD = 0xFFF7
	FAT16_EOC = 0xFFF8
	FAT32_BAD = 0x0FFFFFF7
	FAT32_EOC = 0x0FFFFFF8
)

FAT Chain End-Of-Cluster (EOC) and Bad Cluster Markers

View Source
const (
	DELETED_FLAG = 0xE5 // Marks a file/directory as deleted when in name[0]
)

File/Directory Entry Flags

View Source
const DefaultBlocksize = 512
View Source
const Fat1xBootSectorSize = 0x200 // 512 bytes

Common Boot Sector Size

Variables

This section is empty.

Functions

func EnforceAlignment

func EnforceAlignment(offsets []uint64, blockSize, offset uint64) (uint64, uint64, bool)

func GuessBlockSize

func GuessBlockSize(fileOffsets []uint64) (uint64, uint64)

func NormalizeVolumePath

func NormalizeVolumePath(path string) string

NormalizeVolumePath checks if a given path is a Windows volume path and normalizes it to \\.\C: format if running on Windows. Otherwise, returns the path unchanged.

Types

type FSType

type FSType uint8

type FatBootSector

type FatBootSector struct {
	Ignored           [3]byte // 0x00 Boot strap short or near jump
	SystemID          [8]int8 // 0x03 Name - can be used to special case partition manager volumes
	SectorSize        uint16  // 0x0B Bytes per logical sector (uint16_t)
	SectorsPerCluster uint8   // 0x0D Sectors/cluster
	Reserved          uint16  // 0x0E Reserved sectors (uint16_t)
	Fats              uint8   // 0x10 Number of FATs
	DirEntries        uint16  // 0x11 Root directory entries (uint16_t)
	Sectors           uint16  // 0x13 Number of sectors (uint16_t)
	Media             uint8   // 0x15 Media code (unused)
	FatLength         uint16  // 0x16 Sectors/FAT (uint16_t)
	SecsTrack         uint16  // 0x18 Sectors per track (uint16_t)
	Heads             uint16  // 0x1A Number of heads (uint16_t)
	Hidden            uint32  // 0x1C Hidden sectors (unused, uint32_t)
	TotalSect         uint32  // 0x20 Total number of sectors (if sectors == 0, uint32_t)

	// The following fields are only used by FAT32
	Fat32Length  uint32   // 0x24 Sectors/FAT (uint32_t)
	Flags        uint16   // 0x28 Bit 8: FAT mirroring, low 4: active FAT (uint16_t)
	Version      uint16   // 0x2A Major, minor filesystem version (uint8_t[2])
	RootCluster  [4]byte  // 0x2C First cluster in root directory (uint32_t)
	InfoSector   uint16   // 0x30 Filesystem info sector (uint16_t)
	BackupBoot   uint16   // 0x32 Backup boot sector (uint16_t)
	BPBReserved  [12]byte // 0x34 Unused (uint8_t[12])
	BSDrvNum     uint8    // 0x40 Drive number
	BSReserved1  uint8    // 0x41 Reserved
	BSBootSig    uint8    // 0x42 Extended boot signature (0x29)
	BSVolID      [4]byte  // 0x43 Volume serial number (uint32_t)
	BSVolLab     [11]byte // 0x47 Volume label
	BSFilSysType [8]byte  // 0x52 Filesystem type ("FAT12   ", "FAT16   ", "FAT32   ")

	// Rest of the boot sector padding and marker
	Nothing [420]byte // 0x5A Padding
	Marker  uint16    // 0x1FE Boot sector signature (0xAA55, uint16_t)
}

FatBootSector represents the FAT partition boot sector (BIOS Parameter Block - BPB). This struct maps to the C `struct fat_boot_sector`. Fields that are `uint16_t` or `uint32_t` in C are represented as byte arrays in Go, to explicitly handle endianness when reading from a raw byte slice.

func ReadFatBootSectorFrom

func ReadFatBootSectorFrom(data []byte) (*FatBootSector, error)

func (*FatBootSector) ReadRootCluster

func (b *FatBootSector) ReadRootCluster() uint32

ReadRootCluster returns the first cluster of the root directory (for FAT32).

type MBR

type MBR struct {
	BootCode         [440]byte            // 0x000-0x1B7: Bootstrap code
	DiskSignature    [4]byte              // 0x1B8-0x1BB: Optional 32-bit disk signature
	Reserved         [2]byte              // 0x1BC-0x1BD: Usually 0x0000
	PartitionEntries [4]MBRPartitionEntry // 0x1BE-0x1FD: Four 16-byte partition entries
	Signature        [2]byte              // 0x1FE-0x1FF: MBR signature (0x55AA)
}

MBR represents the Master Boot Record structure.

func ParseMBR

func ParseMBR(data []byte) (*MBR, error)

ParseMBR parses a 512-byte slice into an MBR struct. It assumes the input slice is exactly 512 bytes long and contains the raw binary data of an MBR in little-endian format.

func (*MBR) ReadDiskSignature

func (m *MBR) ReadDiskSignature() uint32

ReadDiskSignature returns the disk signature as a uint32.

func (*MBR) ReadSignature

func (m *MBR) ReadSignature() uint16

ReadSignature returns the MBR signature (should be 0xAA55).

func (*MBR) String

func (m *MBR) String() string

String provides a human-readable representation of the MBR.

type MBRPartition

type MBRPartition uint8
const (
	PartitionTypeEmpty MBRPartition = iota
	PartitionTypeFAT12
	PartitionTypeXENIXRoot
	PartitionTypeXENIXUsr
	PartitionTypeFAT16LessThan32MB
	PartitionTypeExtendedCHS
	PartitionTypeFAT16GreaterThan32MB
	PartitionTypeNTFSHPFSexFATQNX
	PartitionTypeAIX
	PartitionTypeAIXBootable
	PartitionTypeOs2BootManager
	PartitionTypeFAT32CHS
	PartitionTypeFAT32LBA
	PartitionTypeFAT16LBA
	PartitionTypeUnknown
	PartitionTypeExtendedLBA
	PartitionTypeLinuxSwap
	PartitionTypeLinuxFilesystem
	PartitionTypeGPTProtectiveMBR
	PartitionTypeEFISystemPartition
	PartitionTypeGPT = 0xEE
)

type MBRPartitionEntry

type MBRPartitionEntry struct {
	BootIndicator uint8        // 0x00: 0x80 for bootable, 0x00 for inactive
	StartCHS      [3]byte      // 0x01: Starting Cylinder-Head-Sector address
	PartitionType MBRPartition // 0x04: Partition type ID (e.g., 0x0B for FAT32, 0x83 for Linux)
	EndCHS        [3]byte      // 0x05: Ending Cylinder-Head-Sector address
	StartLBA      [4]byte      // 0x08: Starting Logical Block Address (LBA) - uint32, Little-Endian
	TotalSectors  [4]byte      // 0x0C: Total sectors in partition - uint32, Little-Endian
}

MBRPartitionEntry represents a single 16-byte entry in the MBR's partition table. All multi-byte fields are stored as byte arrays to explicitly handle little-endian conversion when reading from the raw MBR byte slice.

func (*MBRPartitionEntry) ReadStartLBA

func (p *MBRPartitionEntry) ReadStartLBA() uint32

ReadStartLBA returns the starting LBA of the partition.

func (*MBRPartitionEntry) ReadTotalSectors

func (p *MBRPartitionEntry) ReadTotalSectors() uint32

ReadTotalSectors returns the total number of sectors in the partition.

func (*MBRPartitionEntry) String

func (p *MBRPartitionEntry) String() string

String provides a human-readable representation of an MBRPartitionEntry.

type Partition

type Partition struct {
	FSType    FSType
	Num       int
	Offset    uint64 // Offset in bytes from the start of the disk
	Size      uint64 // Size in bytes of the partition
	BlockSize uint32 // Block size in bytes
}

type PartitionType

type PartitionType uint8

Jump to

Keyboard shortcuts

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