gpt

package
v0.0.0-...-bbc9ce3 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2021 License: BSD-2-Clause Imports: 15 Imported by: 1

Documentation

Overview

Package gpt is an implementation of GUID Partition Table read and write. It is based on the UEFI specification v2.6.

Index

Constants

View Source
const (
	RequiredPartition  = 0
	NoBlockIOProtocol  = 1
	LegacyBIOSBootable = 2

	MicrosoftReadOnly      = 60
	MicrosoftShadowCopy    = 61
	MicrosoftHidden        = 62
	MicrosoftNoDriveLetter = 63
)

Partition Attributes

View Source
const (
	// IOCTLPhysical = BLKBSZGET is the linux ioctl flag for physical block size
	IOCTLPhysical = 2148012656

	// IOCTLLogical = BLKSSZGET is the linux ioctl flag for logical block size
	IOCTLLogical = 4712

	// IOCTLOptimal = BLKIOOPT is the linux ioctl flag for optimal transfer size
	IOCTLOptimal = 4729

	// IOCTLSize = BLKGETSIZE is the linux ioctl flag for getting disk block count
	IOCTLSize = 4704
)
View Source
const FallbackLogicalBlockSize = 512

FallbackLogicalBlockSize of 512 is returned (with error) from GetLogicalBlockSize as a sensible default assumption.

View Source
const FallbackPhysicalBlockSize = 4096

FallbackPhysicalBlockSize of 4096 is returned (with error) from GetPhysicalBlockSize as a sensible default assumption. Devices are tending toward this as a common physical sector size, and there isn't much to lose from this alignment as opposed to 512, the prior common value. By contrast, write performance can be significantly negatively affected by an alignment less than this on such disks.

View Source
const HeaderSize = 92

HeaderSize is the byte size of a GPT Header

View Source
const MinPartitionEntryArraySize = 16384

MinPartitionEntryArraySize is the minimum allowed size of a GPT partition array

View Source
const PartitionEntrySize = 128

PartitionEntrySize is the size of the partition entry structure

Variables

View Source
var (
	// ErrInvalidSignature indicates that the GPT contained an invalid signature
	ErrInvalidSignature = errors.New("gpt: invalid signature")
	// ErrInvalidAddress indicates that an LBA address in a header or partition
	// table points to an invalid location, either overlapping or out of range.
	ErrInvalidAddress = errors.New("gpt: invalid address")
	// ErrHeaderCRC indicates that a header contained an invalid CRC
	ErrHeaderCRC = errors.New("gpt: bad header CRC")

	// ErrUnsupportedPlatform is returned by functions that are not implemented on
	// the host plaform
	ErrUnsupportedPlatform = errors.New("gpt: unsupported platform")
)
View Source
var (
	GUIDUnused           = mustNewGUID("00000000-0000-0000-0000-000000000000")
	GUIDMBR              = mustNewGUID("024DEE41-33E7-11D3-9D69-0008C781F39F")
	GUIDEFI              = mustNewGUID("C12A7328-F81F-11D2-BA4B-00A0C93EC93B")
	GUIDBIOS             = mustNewGUID("21686148-6449-6E6F-744E-656564454649")
	GUIDIntelFastFlash   = mustNewGUID("D3BFE2DE-3DAF-11DF-BA40-E3A556D89593")
	GUIDSonyBoot         = mustNewGUID("F4019732-066E-4E12-8273-346C5641494F")
	GUIDLenovoBoot       = mustNewGUID("BFBFAFE7-A34F-448A-9A5B-6213EB736C22")
	GUIDAppleHFSPlus     = mustNewGUID("48465300-0000-11AA-AA11-00306543ECAC")
	GUIDAppleUFS         = mustNewGUID("55465300-0000-11AA-AA11-00306543ECAC")
	GUIDAppleBoot        = mustNewGUID("426F6F74-0000-11AA-AA11-00306543ECAC")
	GUIDAppleRaid        = mustNewGUID("52414944-0000-11AA-AA11-00306543ECAC")
	GUIDAppleOfflineRAID = mustNewGUID("52414944-5F4F-11AA-AA11-00306543ECAC")
	GUIDAppleLabel       = mustNewGUID("4C616265-6C00-11AA-AA11-00306543ECAC")
	GUIDFuchsiaSystem    = mustNewGUID("606B000B-B7C7-4653-A7D5-B737332C899D")
	GUIDFuchsiaData      = mustNewGUID("08185F0C-892D-428A-A789-DBEEC8F55E6A")
	GUIDFuchsiaBlob      = mustNewGUID("2967380E-134C-4CBB-B6DA-17E7CE1CA45D")
	GUIDFuchsiaFVM       = mustNewGUID("41D0E340-57E3-954E-8C1E-17ECAC44CFF5")
	GUIDFuchsiaInstaller = mustNewGUID("4DCE98CE-E77E-45C1-A863-CAF92F1330C1")
	GUIDFuchsiaZirconA   = mustNewGUID("DE30CC86-1F4A-4A31-93C4-66F147D33E05")
	GUIDFuchsiaZirconB   = mustNewGUID("23CC04DF-C278-4CE7-8471-897D1A4BCDF7")
	GUIDFuchsiaZirconR   = mustNewGUID("A0E5CF57-2DEF-46BE-A80C-A2067C37CD49")
	GUIDFuchsiaVbmetaA   = mustNewGUID("A13B4D9A-EC5F-11E8-97D8-6C3BE52705BF")
	GUIDFuchsiaVbmetaB   = mustNewGUID("A288ABF2-EC5F-11E8-97D8-6C3BE52705BF")
	GUIDFuchsiaVbmetaR   = mustNewGUID("6A2460C3-CD11-4E8B-80A8-12CCE268ED0A")
	GUIDFuchsiaMisc      = mustNewGUID("1D75395D-F2C6-476B-A8B7-45CC1C97B476")
)

PartitionGUIDs:

View Source
var EFIRevision = Revision{0, 0, 1, 0}

EFIRevision is the 1.0 EFI revision

View Source
var EFISignature = Signature{'E', 'F', 'I', ' ', 'P', 'A', 'R', 'T'}

EFISignature is the default EFI Signature for GPT `EFI PART`

View Source
var GUIDS = map[string]GUID{

	"unused":             GUIDUnused,
	"mbr":                GUIDMBR,
	"efi":                GUIDEFI,
	"bios":               GUIDBIOS,
	"intel-fast-flash":   GUIDIntelFastFlash,
	"sony-boot":          GUIDSonyBoot,
	"lenovo-boot":        GUIDLenovoBoot,
	"apple-hfs-plus":     GUIDAppleHFSPlus,
	"apple-ufs":          GUIDAppleUFS,
	"apple-boot":         GUIDAppleBoot,
	"apple-raid":         GUIDAppleRaid,
	"apple-offline-raid": GUIDAppleOfflineRAID,
	"apple-label":        GUIDAppleLabel,
	"fuchsia-system":     GUIDFuchsiaSystem,
	"fuchsia-data":       GUIDFuchsiaData,
	"fuchsia-blob":       GUIDFuchsiaBlob,
	"zircon-a":           GUIDFuchsiaZirconA,
	"zircon-b":           GUIDFuchsiaZirconB,
	"zircon-r":           GUIDFuchsiaZirconR,
	"vbmeta_a":           GUIDFuchsiaVbmetaA,
	"vbmeta_b":           GUIDFuchsiaVbmetaB,
	"vbmeta_r":           GUIDFuchsiaVbmetaR,
}

GUIDS contains a map of known GUIDS to their names.

Functions

func AlignedRange

func AlignedRange(nextLBA, size, logical, physical, optimal uint64) (uint64, uint64)

AlignedRange takes a starting lba, size (in bytes), logical, physical and optimal block sizes and computes a well-aligned maximal range of block addresses starting after nextLBA, and ending to be at most one block larger than the requested size.

func GetDiskSize

func GetDiskSize(f *os.File) (uint64, error)

GetDiskSize fetches the byte size of the given disk.

func GetLogicalBlockSize

func GetLogicalBlockSize(f *os.File) (uint64, error)

GetLogicalBlockSize fetches the physical block size of the given file. It requires elevated process priviliges to execute on most platforms. Currently only supported on Linux and Darwin.

func GetOptimalTransferSize

func GetOptimalTransferSize(f *os.File) (uint64, error)

GetOptimalTransferSize returns the optimal transfer size of the given disk.

func GetPhysicalBlockSize

func GetPhysicalBlockSize(f *os.File) (uint64, error)

GetPhysicalBlockSize fetches the physical block size of the given file. It requires elevated process priviliges to execute on most platforms. Currently only supported on Linux and Darwin.

func NextAlignedLBA

func NextAlignedLBA(nextLBA, logical, physical, optimal uint64) uint64

NextAlignedLBA takes a starting LBA, the logical, physical and optimal block sizes, and returns the next LBA that conforms the UEFI alignment specifications. Unknown values may be passed as 0, but logcal must have a reasonable value.

Types

type ErrInvalidGPT

type ErrInvalidGPT []error

ErrInvalidGPT aggregates zero or more errors that occur during validation

func (ErrInvalidGPT) Error

func (e ErrInvalidGPT) Error() string

type GPT

type GPT struct {
	MBR     mbr.MBR
	Primary PartitionTable
	Backup  PartitionTable
	// contains filtered or unexported fields
}

GPT is a wrapper around a disks MBR, Primary and Backup PartitionTable and block size metadata.

func ReadGPT

func ReadGPT(r io.ReadSeeker, blockSize uint64, diskSize uint64) (GPT, error)

ReadGPT reads a GPT from r using the given logical block size.

func (GPT) String

func (g GPT) String() string

func (*GPT) Update

func (g *GPT) Update(blockSize, physicalBlockSize, optimalTransferLengthGranularity, diskSize uint64) error

Update uses the provided geometry information, combined with the values already present in g to update the geometry and CRC fields for the tables and partitions in g. If the changes make laying out the partitions impossible, an error is returned. Subsequent calls to WriteTo will use the given values if non-zero. Note that partition re-alignment will only move partition starts forward, it will not preserve size, so callers must set partition starting positions appropriately using NextAlignedLBA to avoid this.

func (*GPT) Validate

func (g *GPT) Validate() error

Validate runs a set of validation operations on the entire GPT, and if errors are found, returns them inside ErrInvalidGPT

func (*GPT) WriteTo

func (g *GPT) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo for writing out the GPT to a target writer. It expects that update was called with correct values beforehand, but it will, if otherwise un-set try to detect reasonable values for logical and physical block sizes. If the target writer does not support detection of these sizes, and they were not supplied to Update, the write will make conventional assumptions (logical block size: 512, physical block size: 4096). Note: If the given writer does not implement io.Seeker, then the write will zero out all non-partition data regions. If the argument implements io.Seeker, then all non-partition table data will remain untouched.

type GUID

type GUID struct {
	TimeLow uint32
	TimeMid uint16
	TimeHi  uint16
	SeqHi   byte
	SeqLo   byte
	Node    [6]byte
}

GUID is a 128 bit globally unique identifier

func NewGUID

func NewGUID(s string) (GUID, error)

NewGUID constructs a GUID from a string in hexidecimal form, with arbitrary splits containing hyphens.

func NewRandomGUID

func NewRandomGUID() GUID

NewRandomGUID generates a new entirely random GUID

func (GUID) IsZero

func (g GUID) IsZero() bool

IsZero returns true if all fields in the GUID are zero

func (GUID) Name

func (g GUID) Name() string

Name looks up the guid in the GUIDS map, and returns the name if found, otherwise it returns the standard string representation.

func (GUID) String

func (g GUID) String() string
type Header struct {
	Signature                Signature
	Revision                 Revision
	HeaderSize               uint32
	HeaderCRC32              uint32
	Reserved                 [4]byte
	MyLBA                    uint64
	AlternateLBA             uint64
	FirstUsableLBA           uint64
	LastUsableLBA            uint64
	DiskGUID                 GUID
	PartitionEntryLBA        uint64
	NumberOfPartitionEntries uint32
	SizeOfPartitionEntry     uint32
	PartitionEntryArrayCRC32 uint32
}

Header is the Go struct represtation of a GPT header

func ReadHeader

func ReadHeader(r io.Reader) (Header, error)

ReadHeader reads a single GPT header from r.

func (*Header) ReadFrom

func (h *Header) ReadFrom(r io.Reader) (int64, error)

ReadFrom reads from the given reader into the reciever header. If an error occurs, the returned bytes read may be incorrect.

func (Header) String

func (h Header) String() string

func (*Header) WriteTo

func (h *Header) WriteTo(w io.Writer) (int64, error)

WriteTo writes the header to the given writer. If an error ocurrs, the returned bytes written may be incorrect.

type PartitionArray

type PartitionArray []PartitionEntry

PartitionArray is an array of PartitionEntry

func (PartitionArray) WriteTo

func (pa PartitionArray) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo for PartitionArray. Note that it writes only the partition entries stored in the receiver array. Callers are responsible for any necessary zero-ing of non-present zero partition entries.

type PartitionEntry

type PartitionEntry struct {
	PartitionTypeGUID   GUID
	UniquePartitionGUID GUID
	StartingLBA         uint64
	EndingLBA           uint64
	Attributes          uint64
	PartitionName       PartitionName
}

PartitionEntry is the Go structure representation of a partition in GPT

func ReadPartitionEntry

func ReadPartitionEntry(r io.Reader) (PartitionEntry, error)

ReadPartitionEntry reads a single GPT PartitionEntry from r.

func (*PartitionEntry) IsZero

func (p *PartitionEntry) IsZero() bool

IsZero returns true if the partition entry is unused

func (*PartitionEntry) ReadFrom

func (p *PartitionEntry) ReadFrom(r io.Reader) (int64, error)

ReadFrom reads from the given reader into the reciever PartitionEntry. If an error occurs, the returned bytes read may be incorrect.

func (PartitionEntry) String

func (p PartitionEntry) String() string

func (*PartitionEntry) WriteTo

func (p *PartitionEntry) WriteTo(w io.Writer) (int64, error)

WriteTo writes the PartitionEntry to the given writer.

type PartitionName

type PartitionName [72]byte

PartitionName is a 32 character string in utf-16

func NewPartitionName

func NewPartitionName(s string) PartitionName

NewPartitionName constructs a partition name from the given string (encodes it in utf16 and truncates it to 32 characters.

func (PartitionName) String

func (pn PartitionName) String() string

type PartitionTable

type PartitionTable struct {
	Header
	Partitions PartitionArray
}

PartitionTable is a header followed by an array of partiton entries

func (PartitionTable) ComputeHeaderCRC32

func (pt PartitionTable) ComputeHeaderCRC32() uint32

ComputeHeaderCRC32 calculates the CRC32 of the header. Users may need to call ComputePartitionArrayCRC32 before calling this method. It does not write the result back to the partition table.

func (PartitionTable) ComputePartitionArrayCRC32

func (pt PartitionTable) ComputePartitionArrayCRC32() uint32

ComputePartitionArrayCRC32 calculates the CRC32 of the contained partition array. It does not write the result back to the partition table.

func (PartitionTable) PartitionArrayPad

func (pt PartitionTable) PartitionArrayPad() int64

PartitionArrayPad calculates the amount of space that must be zero-written between the last partition contained in the PartitionArray and the end of the on-disk partition array size.

func (PartitionTable) String

func (pt PartitionTable) String() string

type Revision

type Revision [4]byte

Revision is the 2 byte representation of a GPT revision number

func (Revision) String

func (r Revision) String() string

type Signature

type Signature [8]byte

Signature is the representation of a GPT signature, normally `EFI PART`

func (Signature) String

func (s Signature) String() string

Jump to

Keyboard shortcuts

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