Amlogic proprietary eMMC partition table: Python parsing library

This is an unnamed and undocumented partition table format implemented by the bootloader and kernel that Amlogic provides for their Linux SoCs (Meson series at least, and probably others). They appear to use this rather than GPT, the industry standard, because their BootROM loads and executes the next stage loader from offset 512 (0x200) on the eMMC, which is exactly where the GPT header would have to start. So instead of changing their BootROM, Amlogic devised this partition table, which lives at an offset of 36MiB (0x240_0000) on the eMMC and so doesn't conflict. This parser expects as input just the partition table from that offset. The maximum number of partitions in a table is 32, which corresponds to a maximum table size of 1304 bytes (0x518).

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.9

This page hosts a formal specification of Amlogic proprietary eMMC partition table using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for Python generated by Kaitai Struct depends on the Python runtime library. You have to install it before you can parse data.

The Python runtime library can be installed from PyPI:

python3 -m pip install kaitaistruct

Code

Parse a local file and get structure in memory:

data = AmlogicEmmcPartitions.from_file("path/to/local/file.bin")

Or parse structure from a bytes:

from kaitaistruct import KaitaiStream, BytesIO

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

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

data.checksum # => To calculate this, treat the first (and only the first) partition
descriptor in the table below as an array of unsigned little-endian
32-bit integers. Sum all those integers mod 2^32, then multiply the
result by the total number of partitions, also mod 2^32. Amlogic
likely meant to include all the partition descriptors in the sum,
but their code as written instead repeatedly loops over the first
one, once for each partition in the table.

Python source code to parse Amlogic proprietary eMMC partition table

amlogic_emmc_partitions.py

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

import kaitaistruct
from kaitaistruct import KaitaiStruct, KaitaiStream, BytesIO


if getattr(kaitaistruct, 'API_VERSION', (0, 9)) < (0, 9):
    raise Exception("Incompatible Kaitai Struct Python API: 0.9 or later is required, but you have %s" % (kaitaistruct.__version__))

class AmlogicEmmcPartitions(KaitaiStruct):
    """This is an unnamed and undocumented partition table format implemented by
    the bootloader and kernel that Amlogic provides for their Linux SoCs (Meson
    series at least, and probably others). They appear to use this rather than GPT,
    the industry standard, because their BootROM loads and executes the next stage
    loader from offset 512 (0x200) on the eMMC, which is exactly where the GPT
    header would have to start. So instead of changing their BootROM, Amlogic
    devised this partition table, which lives at an offset of 36MiB (0x240_0000)
    on the eMMC and so doesn't conflict. This parser expects as input just the
    partition table from that offset. The maximum number of partitions in a table
    is 32, which corresponds to a maximum table size of 1304 bytes (0x518).
    
    .. seealso::
       Source - http://aml-code.amlogic.com/kernel/common.git/tree/include/linux/mmc/emmc_partitions.h?id=18a4a87072ababf76ea08c8539e939b5b8a440ef
    
    
    .. seealso::
       Source - http://aml-code.amlogic.com/kernel/common.git/tree/drivers/amlogic/mmc/emmc_partitions.c?id=18a4a87072ababf76ea08c8539e939b5b8a440ef
    """
    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.magic = self._io.read_bytes(4)
        if not self.magic == b"\x4D\x50\x54\x00":
            raise kaitaistruct.ValidationNotEqualError(b"\x4D\x50\x54\x00", self.magic, self._io, u"/seq/0")
        self.version = (KaitaiStream.bytes_terminate(self._io.read_bytes(12), 0, False)).decode(u"UTF-8")
        self.num_partitions = self._io.read_s4le()
        if not self.num_partitions >= 1:
            raise kaitaistruct.ValidationLessThanError(1, self.num_partitions, self._io, u"/seq/2")
        if not self.num_partitions <= 32:
            raise kaitaistruct.ValidationGreaterThanError(32, self.num_partitions, self._io, u"/seq/2")
        self.checksum = self._io.read_u4le()
        self.partitions = []
        for i in range(self.num_partitions):
            self.partitions.append(AmlogicEmmcPartitions.Partition(self._io, self, self._root))


    class Partition(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.name = (KaitaiStream.bytes_terminate(self._io.read_bytes(16), 0, False)).decode(u"UTF-8")
            self.size = self._io.read_u8le()
            self.offset = self._io.read_u8le()
            self._raw_flags = self._io.read_bytes(4)
            _io__raw_flags = KaitaiStream(BytesIO(self._raw_flags))
            self.flags = AmlogicEmmcPartitions.Partition.PartFlags(_io__raw_flags, self, self._root)
            self.padding = self._io.read_bytes(4)

        class PartFlags(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.is_code = self._io.read_bits_int_le(1) != 0
                self.is_cache = self._io.read_bits_int_le(1) != 0
                self.is_data = self._io.read_bits_int_le(1) != 0