Version: v0.0.0-...-c97c800 Latest Latest

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

Go to latest
Published: Feb 6, 2019 License: BSD-3-Clause Imports: 7 Imported by: 0



gpt implements reading and writing of GUID Partition tables. GPTs are dumped in JSON format and written in same. One complication is that we frequently only want to write a very small subset of a GPT. For example, we might only want to change the GUID. As it happens it is simpler (and more useful) just to read and write the whole thing. In for a penny, in for a pound.



View Source
const (
	BlockSize         = 512
	HeaderOff         = 0x200
	HeaderSize        = 0x5c               // They claim it can vary. Give me a break.
	Signature  uint64 = 0x5452415020494645 // ("EFI PART", 45h 46h 49h 20h 50h 41h 52h 54h on little-endian machines)
	Revision          = 0x10000
	MaxNPart          = 0x80


This section is empty.


func EqualHeader

func EqualHeader(p, b Header) error

EqualHeader compares two headers and returns true if they match. Those fields which by definition must differ are ignored.

func EqualPart

func EqualPart(p, b Part) (err error)

func EqualParts

func EqualParts(p, b *GPT) (err error)

EqualParts compares the Parts arrays from two GPTs and returns an error if they differ. If they length differs we just give up, since there's no way to know which should have matched. Otherwise, we do a 1:1 comparison.

func GetBlockSize

func GetBlockSize(device string) (int, error)

GetBlockSize returns the block size of device.

func Write

func Write(w io.WriterAt, p *PartitionTable) error

Write writes the MBR and primary and backup GPTs to w.


type GPT

type GPT struct {
	Parts []Part

func Table

func Table(r io.ReaderAt, off int64) (*GPT, error)

Table reads a GPT table at the given offset. It checks that the Signature, Revision, HeaderSize, and MaxPart are reasonable. It also verifies the header and partition table CRC32 values.

func (*GPT) String

func (g *GPT) String() string

type GUID

type GUID struct {
	L  uint32
	W1 uint16
	W2 uint16
	B  [8]byte

func (*GUID) String

func (g *GUID) String() string
type Header struct {
	Signature  uint64
	Revision   uint32 // (for GPT version 1.0 (through at least UEFI version 2.7 (May 2017)), the value is 00h 00h 01h 00h)
	HeaderSize uint32 // size in little endian (in bytes, usually 5Ch 00h 00h 00h or 92 bytes)
	CRC        uint32 // CRC32/zlib of header (offset +0 up to header size) in little endian, with this field zeroed during calculation
	Reserved   uint32 // ; must be zero
	CurrentLBA uint64 // (location of this header copy)
	BackupLBA  uint64 // (location of the other header copy)
	FirstLBA   uint64 // usable LBA for partitions (primary partition table last LBA + 1)
	LastLBA    uint64 // usable LBA (secondary partition table first LBA - 1)
	DiskGUID   GUID   // (also referred as UUID on UNIXes)
	PartStart  uint64 // LBA of array of partition entries (always 2 in primary copy)
	NPart      uint32 // Number of partition entries in array
	PartSize   uint32 // Size of a single partition entry (usually 80h or 128)
	PartCRC    uint32 // CRC32/zlib of partition array in little endian

type MBR

type MBR [BlockSize]byte

func (*MBR) String

func (m *MBR) String() string

type Part

type Part struct {
	PartGUID   GUID     // Partition type GUID
	UniqueGUID GUID     // Unique partition GUID
	FirstLBA   uint64   // LBA (little endian)
	LastLBA    uint64   // LBA (inclusive, usually odd)
	Attribute  PartAttr // flags (e.g. bit 60 denotes read-only)
	Name       PartName // Partition name (36 UTF-16LE code units)

type PartAttr

type PartAttr uint64

type PartName

type PartName [72]byte

type PartitionTable

type PartitionTable struct {
	MasterBootRecord *MBR
	Primary          *GPT
	Backup           *GPT

PartitionTable defines all the partition table information. This includes the MBR and two GPTs. The GPTs are similar but not identical, as they contain "pointers" to each other in the BackupLBA in the Header. The design is defective in that if a given Header has an error, you are supposed to just assume that the BackupLBA is intact, which is a pretty bogus assumption. This is why you do standards like this in the open, not in hiding. I hope someone from Intel is reading this.

func New

func New(r io.ReaderAt) (*PartitionTable, error)

New reads in the MBR, primary and backup GPT from a disk and returns a pointer to them. There are some checks it can apply. It can return with a one or more headers AND an error. Sorry. Experience with some real USB sticks is showing that we need to return data even if there are some things wrong.

func (*PartitionTable) String

func (p *PartitionTable) String() string

Source Files

Jump to

Keyboard shortcuts

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