SpecialistOff.NET / Вопросы / Статьи / Фрагменты кода / Резюме / Метки / Помощь / Файлы

Назад

mbr.py


Метки: [python]; [mbr];

Оригинал

--- mbr.py
mbr.py Скачать
""" MBR data structure starts on page 88 of FSFA.
Table 5.1 Data structures for the DOS partition table.
Byte range              Description                     Essential
0-445                   Boot Code                       No
446-461                 Partition Table Entry #1        Yes
462-477                 Partition Table Enrty #2        Yes
478-493                 Partition Table Enrty #3        Yes
494-509                 Partition Table Enrty #4        Yes
510-511                 Signature value (0xAA55)        No

Table 5.2 Data structure for DOS partition entries.
Byte Range      Description                     Essential
0-0             Bootable Flag                   No
1-3             Starting CHS Address            Yes
4-4             Partition Type (see table 5.3)  No
5-7             Ending CHS Address              Yes
8-11            Starting LBA Address            Yes
12-15           Size in Sectors                 Yes
"""
import struct
import json


# Table 5.3 Some of the type values for DOS partitions.
# More partition values can be found here:
# http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
DOS_PARTITIONS = {
    0x00: "Empty",
    0x01: "FAT12, CHS",
    0x04: "FAT16, 16-32 MB, CHS",
    0x05: "Microsoft Extended, CHS",
    0x06: "FAT16, 32 MB-2GB, CHS",
    0x07: "NTFS",
    0x0b: "FAT32, CHS",
    0x0c: "FAT32, LBA",
    0x0e: "FAT16, 32 MB-2GB, LBA",
    0x0f: "Microsoft Extended, LBA",
    0x11: "Hidden Fat12, CHS",
    0x14: "Hidden FAT16, 16-32 MB, CHS",
    0x16: "Hidden FAT16, 32 MB-2GB, CHS",
    0x1b: "Hidden FAT32, CHS",
    0x1c: "Hidden FAT32, LBA",
    0x1e: "Hidden FAT16, 32 MB-2GB, LBA",
    0x42: "Microsoft MBR, Dynamic Disk",
    0x82: "Solaris x86 -or- Linux Swap",
    0x83: "Linux",
    0x84: "Hibernation",
    0x85: "Linux Extended",
    0x86: "NTFS Volume Set",
    0x87: "NTFS Volume SET",
    0xa0: "Hibernation",
    0xa1: "Hibernation",
    0xa5: "FreeBSD",
    0xa6: "OpenBSD",
    0xa8: "Mac OSX",
    0xa9: "NetBSD",
    0xab: "Mac OSX Boot",
    0xb7: "BSDI",
    0xb8: "BSDI swap",
    # FIXME: I'm pretty sure 0xdb is a recovery partition
    0xdb: "Recovery Partition",
    0xde: "Dell Diagnostic Partition",
    0xee: "EFI GPT Disk",
    0xef: "EFI System Partition",
    0xfb: "Vmware File System",
    0xfc: "Vmware swap",
    # FIXME Add flag for VirtualBox Partitions
}

# FIXME find way to determine sector size
SECTOR_SIZE = 512


class Partition(object):
    """
    Object for storing Partition Data
    """

    def __init__(self, data, parent=None):
        """
        To get the correct lba value for extended partitions, we need to add
        the lba value from the extended partition. For example, if you read the
        first 4 partitions and the fourth is an extended partition with an lba
        of 1000, we seek to the 1000th sector. Then we read the next mbr,
        adding the 1000 from the extended partition to each lba.
        """
        self.parent = parent
        self.bootable_flag = struct.unpack("