ASUS Fugu bootloader.img format (version 2 and later): C++98/STL parsing library

A bootloader image which only seems to have been used on a few ASUS devices. The encoding is ASCII, because the releasetools.py script is written using Python 2, where the default encoding is ASCII.

A test file can be found in the firmware files for the "fugu" device, which can be downloaded from https://developers.google.com/android/images

File extension

img

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.9

This page hosts a formal specification of ASUS Fugu bootloader.img format (version 2 and later) 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.img", 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:
    android_bootldr_asus_t data(&ks);
    

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

data.images() // => Only three images are included: `ifwi.bin`, `droidboot.img`
and `splashscreen.img`

C++98/STL source code to parse ASUS Fugu bootloader.img format (version 2 and later)

android_bootldr_asus.h

#ifndef ANDROID_BOOTLDR_ASUS_H_
#define ANDROID_BOOTLDR_ASUS_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

/**
 * A bootloader image which only seems to have been used on a few ASUS
 * devices. The encoding is ASCII, because the `releasetools.py` script
 * is written using Python 2, where the default encoding is ASCII.
 * 
 * A test file can be found in the firmware files for the "fugu" device,
 * which can be downloaded from <https://developers.google.com/android/images>
 * \sa https://android.googlesource.com/device/asus/fugu/+/android-8.1.0_r5/releasetools.py Source
 */

class android_bootldr_asus_t : public kaitai::kstruct {

public:
    class image_t;

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

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

public:
    ~android_bootldr_asus_t();

    class image_t : public kaitai::kstruct {

    public:

        image_t(kaitai::kstream* p__io, android_bootldr_asus_t* p__parent = 0, android_bootldr_asus_t* p__root = 0);

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

    public:
        ~image_t();

    private:
        bool f_file_name;
        std::string m_file_name;

    public:
        std::string file_name();

    private:
        std::string m_chunk_id;
        uint32_t m_len_body;
        uint8_t m_flags;
        uint8_t m_reserved1;
        uint8_t m_reserved2;
        uint8_t m_reserved3;
        std::string m_body;
        android_bootldr_asus_t* m__root;
        android_bootldr_asus_t* m__parent;

    public:
        std::string chunk_id() const { return m_chunk_id; }
        uint32_t len_body() const { return m_len_body; }
        uint8_t flags() const { return m_flags; }
        uint8_t reserved1() const { return m_reserved1; }
        uint8_t reserved2() const { return m_reserved2; }
        uint8_t reserved3() const { return m_reserved3; }
        std::string body() const { return m_body; }
        android_bootldr_asus_t* _root() const { return m__root; }
        android_bootldr_asus_t* _parent() const { return m__parent; }
    };

private:
    std::string m_magic;
    uint16_t m_revision;
    uint16_t m_reserved1;
    uint32_t m_reserved2;
    std::vector<image_t*>* m_images;
    android_bootldr_asus_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string magic() const { return m_magic; }
    uint16_t revision() const { return m_revision; }
    uint16_t reserved1() const { return m_reserved1; }
    uint32_t reserved2() const { return m_reserved2; }

    /**
     * Only three images are included: `ifwi.bin`, `droidboot.img`
     * and `splashscreen.img`
     */
    std::vector<image_t*>* images() const { return m_images; }
    android_bootldr_asus_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // ANDROID_BOOTLDR_ASUS_H_

android_bootldr_asus.cpp

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

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

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

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

void android_bootldr_asus_t::_read() {
    m_magic = m__io->read_bytes(8);
    if (!(magic() == std::string("\x42\x4F\x4F\x54\x4C\x44\x52\x21", 8))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x42\x4F\x4F\x54\x4C\x44\x52\x21", 8), magic(), _io(), std::string("/seq/0"));
    }
    m_revision = m__io->read_u2le();
    if (!(revision() >= 2)) {
        throw kaitai::validation_less_than_error<uint16_t>(2, revision(), _io(), std::string("/seq/1"));
    }
    m_reserved1 = m__io->read_u2le();
    m_reserved2 = m__io->read_u4le();
    m_images = new std::vector<image_t*>();
    const int l_images = 3;
    for (int i = 0; i < l_images; i++) {
        m_images->push_back(new image_t(m__io, this, m__root));
    }
}

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

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

android_bootldr_asus_t::image_t::image_t(kaitai::kstream* p__io, android_bootldr_asus_t* p__parent, android_bootldr_asus_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_file_name = false;

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

void android_bootldr_asus_t::image_t::_read() {
    m_chunk_id = kaitai::kstream::bytes_to_str(m__io->read_bytes(8), std::string("ASCII"));
    if (!( ((chunk_id() == (std::string("IFWI!!!!"))) || (chunk_id() == (std::string("DROIDBT!"))) || (chunk_id() == (std::string("SPLASHS!")))) )) {
        throw kaitai::validation_not_any_of_error<std::string>(chunk_id(), _io(), std::string("/types/image/seq/0"));
    }
    m_len_body = m__io->read_u4le();
    m_flags = m__io->read_u1();
    {
        uint8_t _ = flags();
        if (!((_ & 1) != 0)) {
            throw kaitai::validation_expr_error<uint8_t>(flags(), _io(), std::string("/types/image/seq/2"));
        }
    }
    m_reserved1 = m__io->read_u1();
    m_reserved2 = m__io->read_u1();
    m_reserved3 = m__io->read_u1();
    m_body = m__io->read_bytes(len_body());
}

android_bootldr_asus_t::image_t::~image_t() {
    _clean_up();
}

void android_bootldr_asus_t::image_t::_clean_up() {
}

std::string android_bootldr_asus_t::image_t::file_name() {
    if (f_file_name)
        return m_file_name;
    m_file_name = ((chunk_id() == (std::string("IFWI!!!!"))) ? (std::string("ifwi.bin")) : (((chunk_id() == (std::string("DROIDBT!"))) ? (std::string("droidboot.img")) : (((chunk_id() == (std::string("SPLASHS!"))) ? (std::string("splashscreen.img")) : (std::string("")))))));
    f_file_name = true;
    return m_file_name;
}