MBR (Master Boot Record) partition table is a traditional way of MS-DOS to partition larger hard disc drives into distinct partitions.
This table is stored in the end of the boot sector (first sector) of the drive, after the bootstrap code. Original DOS 2.0 specification allowed only 4 partitions per disc, but DOS 3.2 introduced concept of "extended partitions", which work as nested extra "boot records" which are pointed to by original ("primary") partitions in MBR.
This page hosts a formal specification of MBR (Master Boot Record) partition table using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing 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
Parse a local file and get structure in memory:
data = MbrPartitionTable.from_file("path/to/local/file.bin")
Or parse structure from a string of bytes:
bytes = "\x00\x01\x02..."
data = MbrPartitionTable.new(Kaitai::Struct::Stream.new(bytes))
After that, one can get various attributes from the structure by invoking getter methods like:
data.bootstrap_code # => get bootstrap code
# 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
##
# MBR (Master Boot Record) partition table is a traditional way of
# MS-DOS to partition larger hard disc drives into distinct
# partitions.
#
# This table is stored in the end of the boot sector (first sector) of
# the drive, after the bootstrap code. Original DOS 2.0 specification
# allowed only 4 partitions per disc, but DOS 3.2 introduced concept
# of "extended partitions", which work as nested extra "boot records"
# which are pointed to by original ("primary") partitions in MBR.
class MbrPartitionTable < Kaitai::Struct::Struct
def initialize(_io, _parent = nil, _root = self)
super(_io, _parent, _root)
_read
end
def _read
@bootstrap_code = @_io.read_bytes(446)
@partitions = []
(4).times { |i|
@partitions << PartitionEntry.new(@_io, self, @_root)
}
@boot_signature = @_io.read_bytes(2)
raise Kaitai::Struct::ValidationNotEqualError.new([85, 170].pack('C*'), boot_signature, _io, "/seq/2") if not boot_signature == [85, 170].pack('C*')
self
end
class PartitionEntry < Kaitai::Struct::Struct
def initialize(_io, _parent = nil, _root = self)
super(_io, _parent, _root)
_read
end
def _read
@status = @_io.read_u1
@chs_start = Chs.new(@_io, self, @_root)
@partition_type = @_io.read_u1
@chs_end = Chs.new(@_io, self, @_root)
@lba_start = @_io.read_u4le
@num_sectors = @_io.read_u4le
self
end
attr_reader :status
attr_reader :chs_start
attr_reader :partition_type
attr_reader :chs_end
attr_reader :lba_start
attr_reader :num_sectors
end
class Chs < Kaitai::Struct::Struct
def initialize(_io, _parent = nil, _root = self)
super(_io, _parent, _root)
_read
end
def _read
@head = @_io.read_u1
@b2 = @_io.read_u1
@b3 = @_io.read_u1
self
end
def sector
return @sector unless @sector.nil?
@sector = (b2 & 63)
@sector
end
def cylinder
return @cylinder unless @cylinder.nil?
@cylinder = (b3 + ((b2 & 192) << 2))
@cylinder
end
attr_reader :head
attr_reader :b2
attr_reader :b3
end
attr_reader :bootstrap_code
attr_reader :partitions
attr_reader :boot_signature
end