Andes Firmware Image layout as seen in MT76 Wifi Chipsets: Ruby parsing library

Firmware image found with MediaTek MT76xx wifi chipsets.

Application

Firmware Image wifi chipset

KS implementation details

License: CC0-1.0

This page hosts a formal specification of Andes Firmware Image layout as seen in MT76 Wifi Chipsets 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 Ruby generated by Kaitai Struct depends on the Ruby runtime library. You have to install it before you can parse data.

The Ruby runtime library can be installed from RubyGems:

gem install kaitai-struct

Code

Parse a local file and get structure in memory:

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

Or parse structure from a string of bytes:

bytes = "\x00\x01\x02..."
data = AndesFirmware.new(Kaitai::Struct::Stream.new(bytes))

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

data.image_header # => get image header

Ruby source code to parse Andes Firmware Image layout as seen in MT76 Wifi Chipsets

andes_firmware.rb

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

require 'kaitai/struct/struct'

unless Gem::Version.new(Kaitai::Struct::VERSION) >= Gem::Version.new('0.9')
  raise "Incompatible Kaitai Struct Ruby API: 0.9 or later is required, but you have #{Kaitai::Struct::VERSION}"
end


##
# Firmware image found with MediaTek MT76xx wifi chipsets.
class AndesFirmware < Kaitai::Struct::Struct
  def initialize(_io, _parent = nil, _root = self)
    super(_io, _parent, _root)
    _read
  end

  def _read
    @_raw_image_header = @_io.read_bytes(32)
    _io__raw_image_header = Kaitai::Struct::Stream.new(@_raw_image_header)
    @image_header = ImageHeader.new(_io__raw_image_header, self, @_root)
    @ilm = @_io.read_bytes(image_header.ilm_len)
    @dlm = @_io.read_bytes(image_header.dlm_len)
    self
  end
  class ImageHeader < Kaitai::Struct::Struct
    def initialize(_io, _parent = nil, _root = self)
      super(_io, _parent, _root)
      _read
    end

    def _read
      @ilm_len = @_io.read_u4le
      @dlm_len = @_io.read_u4le
      @fw_ver = @_io.read_u2le
      @build_ver = @_io.read_u2le
      @extra = @_io.read_u4le
      @build_time = (@_io.read_bytes(16)).force_encoding("UTF-8")
      self
    end
    attr_reader :ilm_len
    attr_reader :dlm_len
    attr_reader :fw_ver
    attr_reader :build_ver
    attr_reader :extra
    attr_reader :build_time
  end
  attr_reader :image_header
  attr_reader :ilm
  attr_reader :dlm
  attr_reader :_raw_image_header
end