Amlogic proprietary eMMC partition table: C++98/STL 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 C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.bin", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    amlogic_emmc_partitions_t data(&ks);
    

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.

C++98/STL source code to parse Amlogic proprietary eMMC partition table

amlogic_emmc_partitions.h

#ifndef AMLOGIC_EMMC_PARTITIONS_H_
#define AMLOGIC_EMMC_PARTITIONS_H_

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

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * 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).
 * \sa http://aml-code.amlogic.com/kernel/common.git/tree/include/linux/mmc/emmc_partitions.h?id=18a4a87072ababf76ea08c8539e939b5b8a440ef Source
 * \sa http://aml-code.amlogic.com/kernel/common.git/tree/drivers/amlogic/mmc/emmc_partitions.c?id=18a4a87072ababf76ea08c8539e939b5b8a440ef Source
 */

class amlogic_emmc_partitions_t : public kaitai::kstruct {

public:
    class partition_t;

    amlogic_emmc_partitions_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, amlogic_emmc_partitions_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~amlogic_emmc_partitions_t();

    class partition_t : public kaitai::kstruct {

    public:
        class part_flags_t;

        partition_t(kaitai::kstream* p__io, amlogic_emmc_partitions_t* p__parent = 0, amlogic_emmc_partitions_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~partition_t();

        class part_flags_t : public kaitai::kstruct {

        public:

            part_flags_t(kaitai::kstream* p__io, amlogic_emmc_partitions_t::partition_t* p__parent = 0, amlogic_emmc_partitions_t* p__root = 0);

        private:
            void _read();
            void _clean_up();

        public:
            ~part_flags_t();

        private:
            bool m_is_code;
            bool m_is_cache;
            bool m_is_data;
            amlogic_emmc_partitions_t* m__root;
            amlogic_emmc_partitions_t::partition_t* m__parent;

        public:
            bool is_code() const { return m_is_code; }
            bool is_cache() const { return m_is_cache; }
            bool is_data() const { return m_is_data; }
            amlogic_emmc_partitions_t* _root() const { return m__root; }
            amlogic_emmc_partitions_t::partition_t* _parent() const { return m__parent; }
        };

    private:
        std::string m_name;
        uint64_t m_size;
        uint64_t m_offset;
        part_flags_t* m_flags;
        std::string m_padding;
        amlogic_emmc_partitions_t* m__root;
        amlogic_emmc_partitions_t* m__parent;
        std::string m__raw_flags;
        kaitai::kstream* m__io__raw_flags;

    public:
        std::string name() const { return m_name; }
        uint64_t size() const { return m_size; }

        /**
         * The start of the partition relative to the start of the eMMC, in bytes
         */
        uint64_t offset() const { return m_offset; }
        part_flags_t* flags() const { return m_flags; }
        std::string padding() const { return m_padding; }
        amlogic_emmc_partitions_t* _root() const { return m__root; }
        amlogic_emmc_partitions_t* _parent() const { return m__parent; }
        std::string _raw_flags() const { return m__raw_flags; }
        kaitai::kstream* _io__raw_flags() const { return m__io__raw_flags; }
    };

private:
    std::string m_magic;
    std::string m_version;
    int32_t m_num_partitions;
    uint32_t m_checksum;
    std::vector<partition_t*>* m_partitions;
    amlogic_emmc_partitions_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string magic() const { return m_magic; }
    std::string version() const { return m_version; }
    int32_t num_partitions() const { return m_num_partitions; }

    /**
     * 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.
     */
    uint32_t checksum() const { return m_checksum; }
    std::vector<partition_t*>* partitions() const { return m_partitions; }
    amlogic_emmc_partitions_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // AMLOGIC_EMMC_PARTITIONS_H_

amlogic_emmc_partitions.cpp

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

#include "amlogic_emmc_partitions.h"
#include "kaitai/exceptions.h"

amlogic_emmc_partitions_t::amlogic_emmc_partitions_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, amlogic_emmc_partitions_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_partitions = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void amlogic_emmc_partitions_t::_read() {
    m_magic = m__io->read_bytes(4);
    if (!(magic() == std::string("\x4D\x50\x54\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x4D\x50\x54\x00", 4), magic(), _io(), std::string("/seq/0"));
    }
    m_version = kaitai::kstream::bytes_to_str(kaitai::kstream::bytes_terminate(m__io->read_bytes(12), 0, false), std::string("UTF-8"));
    m_num_partitions = m__io->read_s4le();
    if (!(num_partitions() >= 1)) {
        throw kaitai::validation_less_than_error<int32_t>(1, num_partitions(), _io(), std::string("/seq/2"));
    }
    if (!(num_partitions() <= 32)) {
        throw kaitai::validation_greater_than_error<int32_t>(32, num_partitions(), _io(), std::string("/seq/2"));
    }
    m_checksum = m__io->read_u4le();
    m_partitions = new std::vector<partition_t*>();
    const int l_partitions = num_partitions();
    for (int i = 0; i < l_partitions; i++) {
        m_partitions->push_back(new partition_t(m__io, this, m__root));
    }
}

amlogic_emmc_partitions_t::~amlogic_emmc_partitions_t() {
    _clean_up();
}

void amlogic_emmc_partitions_t::_clean_up() {
    if (m_partitions) {
        for (std::vector<partition_t*>::iterator it = m_partitions->begin(); it != m_partitions->end(); ++it) {
            delete *it;
        }
        delete m_partitions; m_partitions = 0;
    }
}

amlogic_emmc_partitions_t::partition_t::partition_t(kaitai::kstream* p__io, amlogic_emmc_partitions_t* p__parent, amlogic_emmc_partitions_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_flags = 0;
    m__io__raw_flags = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void amlogic_emmc_partitions_t::partition_t::_read() {
    m_name = kaitai::kstream::bytes_to_str(kaitai::kstream::bytes_terminate(m__io->read_bytes(16), 0, false), std::string("UTF-8"));
    m_size = m__io->read_u8le();
    m_offset = m__io->read_u8le();
    m__raw_flags = m__io->read_bytes(4);
    m__io__raw_flags = new kaitai::kstream(m__raw_flags);
    m_flags = new part_flags_t(m__io__raw_flags, this, m__root);
    m_padding = m__io->read_bytes(4);
}

amlogic_emmc_partitions_t::partition_t::~partition_t() {
    _clean_up();
}

void amlogic_emmc_partitions_t::partition_t::_clean_up() {
    if (m__io__raw_flags) {
        delete m__io__raw_flags; m__io__raw_flags = 0;
    }
    if (m_flags) {
        delete m_flags; m_flags = 0;
    }
}

amlogic_emmc_partitions_t::partition_t::part_flags_t::part_flags_t(kaitai::kstream* p__io, amlogic_emmc_partitions_t::partition_t* p__parent, amlogic_emmc_partitions_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void amlogic_emmc_partitions_t::partition_t::part_flags_t::_read() {
    m_is_code = m__io->read_bits_int_le(1);
    m_is_cache = m__io->read_bits_int_le(1);
    m_is_data = m__io->read_bits_int_le(1);
}

amlogic_emmc_partitions_t::partition_t::part_flags_t::~part_flags_t() {
    _clean_up();
}

void amlogic_emmc_partitions_t::partition_t::part_flags_t::_clean_up() {
}