GPT (GUID) partition table: Python parsing library

KS implementation details

License: CC0-1.0

This page hosts a formal specification of GPT (GUID) partition table using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Parse a local file and get structure in memory:

data = GptPartitionTable.from_file("path/to/local/file.GPT (GUID) partition table")

Or parse structure from a bytes:

from kaitaistruct import KaitaiStream, BytesIO

raw = b"\x00\x01\x02..."
data = GptPartitionTable(KaitaiStream(BytesIO(raw)))

After that, one can get various attributes from the structure by invoking getter methods like:

data.sectorSize # => get sector size

Python source code to parse GPT (GUID) partition table

gpt_partition_table.py

# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

from pkg_resources import parse_version
from kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO


if parse_version(ks_version) < parse_version('0.7'):
    raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))

class GptPartitionTable(KaitaiStruct):
    """
    .. seealso::
       Specification taken from https://en.wikipedia.org/wiki/GUID_Partition_Table
    """
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self
        self._read()

    def _read(self):
        pass

    class PartitionEntry(KaitaiStruct):
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._read()

        def _read(self):
            self.type_guid = self._io.read_bytes(16)
            self.guid = self._io.read_bytes(16)
            self.first_lba = self._io.read_u8le()
            self.last_lba = self._io.read_u8le()
            self.attributes = self._io.read_u8le()
            self.name = (self._io.read_bytes(72)).decode(u"UTF-16LE")


    class PartitionHeader(KaitaiStruct):
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._read()

        def _read(self):
            self.signature = self._io.ensure_fixed_contents(b"\x45\x46\x49\x20\x50\x41\x52\x54")
            self.revision = self._io.read_u4le()
            self.header_size = self._io.read_u4le()
            self.crc32_header = self._io.read_u4le()
            self.reserved = self._io.read_u4le()
            self.current_lba = self._io.read_u8le()
            self.backup_lba = self._io.read_u8le()
            self.first_usable_lba = self._io.read_u8le()
            self.last_usable_lba = self._io.read_u8le()
            self.disk_guid = self._io.read_bytes(16)
            self.entries_start = self._io.read_u8le()
            self.entries_count = self._io.read_u4le()
            self.entries_size = self._io.read_u4le()
            self.crc32_array = self._io.read_u4le()

        @property
        def entries(self):
            if hasattr(self, '_m_entries'):
                return self._m_entries if hasattr(self, '_m_entries') else None

            io = self._root._io
            _pos = io.pos()
            io.seek((self.entries_start * self._root.sector_size))
            self._raw__m_entries = [None] * (self.entries_count)
            self._m_entries = [None] * (self.entries_count)
            for i in range(self.entries_count):
                self._raw__m_entries[i] = io.read_bytes(self.entries_size)
                io = KaitaiStream(BytesIO(self._raw__m_entries[i]))
                self._m_entries[i] = self._root.PartitionEntry(io, self, self._root)

            io.seek(_pos)
            return self._m_entries if hasattr(self, '_m_entries') else None


    @property
    def sector_size(self):
        if hasattr(self, '_m_sector_size'):
            return self._m_sector_size if hasattr(self, '_m_sector_size') else None

        self._m_sector_size = 512
        return self._m_sector_size if hasattr(self, '_m_sector_size') else None

    @property
    def primary(self):
        if hasattr(self, '_m_primary'):
            return self._m_primary if hasattr(self, '_m_primary') else None

        io = self._root._io
        _pos = io.pos()
        io.seek(self._root.sector_size)
        self._m_primary = self._root.PartitionHeader(io, self, self._root)
        io.seek(_pos)
        return self._m_primary if hasattr(self, '_m_primary') else None

    @property
    def backup(self):
        if hasattr(self, '_m_backup'):
            return self._m_backup if hasattr(self, '_m_backup') else None

        io = self._root._io
        _pos = io.pos()
        io.seek((self._io.size() - self._root.sector_size))
        self._m_backup = self._root.PartitionHeader(io, self, self._root)
        io.seek(_pos)
        return self._m_backup if hasattr(self, '_m_backup') else None