Esempio n. 1
0
    def init_partition_table(self, part_num):

        kb_per_bulk = INSTRUCTIONS.WRITE_PROTECT_BULK_SIZE_IN_KB
        sectors_per_bulk = pt.kb2sectors(kb_per_bulk)

        first_lba = 1
        last_lba = 1

        for i in range(part_num):

            part = PARTITIONS.part_list[i]
            last_wp_chunk = PARTITIONS.wp_chunk_list[-1]

            if part.first_lba_in_kb > 0:
                first_lba = pt.kb2sectors(part.first_lba_in_kb)
            if first_lba < last_lba:
                first_lba = last_lba

            part.readonly = True
            PARTITIONS.update_wp_chunk_list(first_lba, part.size_in_sec,
                                            sectors_per_bulk)

            entry = Entry()
            if part.bootable is True:
                entry.bootable = 0x80
            else:
                entry.bootable = 0x00
            entry.part_type = part._type
            entry.first_lba = first_lba
            entry.num_sectors = part.size_in_sec
            entry.toarray()

            self.add_entry(entry)

            last_lba = first_lba + part.size_in_sec
Esempio n. 2
0
  def create(self, output_directory, part_num, start_lba):
    print "About to make EBR: %i" % start_lba

    ebr_offset = 0
    first_lba = last_lba = (start_lba + part_num - 3)

    kb_per_bulk = INSTRUCTIONS.WRITE_PROTECT_BULK_SIZE_IN_KB
    sectors_per_bulk = pt.kb2sectors(kb_per_bulk)
    for i in range(3, part_num):
      part = PARTITIONS.part_list[i]
      last_wp_chunk = PARTITIONS.wp_chunk_list[-1]

      if first_lba < last_lba:
        first_lba = last_lba

      part.readonly = True
      PARTITIONS.update_wp_chunk_list(first_lba, part.size_in_sec, sectors_per_bulk)

      entry1 = Entry()
      if part.bootable is True:
        entry1.bootable = 0x80
      else:
        entry1.bootable  = 0x00
      entry1.part_type   = part._type
      entry1.first_lba   = first_lba - start_lba - ebr_offset
      entry1.num_sectors = part.size_in_sec
      entry1.toarray()
      mbr = MBR()
      mbr.add_entry(entry1)

      last_lba = first_lba + part.size_in_sec

      print "* %-10s: %-8i ~ %8i" % (part.label, first_lba, last_lba)

      entry2 = Entry()
      if i < (part_num - 1):
        entry2.bootable  = 0x00
        entry2.part_type = 0x05
        entry2.first_lba = i - 2
        entry2.num_sectors = 1
      entry2.toarray()
      mbr.add_entry(entry2)

      empty_entry  = Entry()
      empty_entry.toarray()
      mbr.add_entry(empty_entry)
      mbr.add_entry(empty_entry)

      mbr.toarray()
      self.items.append(mbr)
      ebr_offset += 1

    image_file = "%s/EBR.bin" % output_directory
    BUG.green("Create %s <-- Extented Boot Recorder" % image_file)
    with open(image_file, 'wb') as f:
      for e in self.items:
        for b in e.array:
          f.write(struct.pack('B', b))
      f.close()
Esempio n. 3
0
  def init_partition_table(self, part_num, needs_ebr):

    kb_per_bulk = INSTRUCTIONS.WRITE_PROTECT_BULK_SIZE_IN_KB
    sectors_per_bulk = pt.kb2sectors(kb_per_bulk)

    first_lba = 1
    last_lba  = 1

    for i in range(part_num):

      part = PARTITIONS.part_list[i]
      last_wp_chunk = PARTITIONS.wp_chunk_list[-1]

      if part.first_lba_in_kb > 0:
        first_lba = pt.kb2sectors(part.first_lba_in_kb)
      if first_lba < last_lba:
        first_lba = last_lba

      part.readonly = True
      PARTITIONS.update_wp_chunk_list(first_lba, part.size_in_sec, sectors_per_bulk)

      entry = Entry()
      if part.bootable is True:
        entry.bootable = 0x80
      else:
        entry.bootable = 0x00
      entry.part_type   = part._type
      entry.first_lba   = first_lba
      entry.num_sectors = part.size_in_sec
      entry.toarray()
      self.add_entry(entry)

      last_lba = first_lba + part.size_in_sec

      print "* %-10s: %-8i ~ %8i" % (part.label, first_lba, last_lba)

    if needs_ebr is True:
      entry = Entry()
      entry.bootable    = 0x00
      entry.part_type   = 0x05
      entry.first_lba   = last_lba
      entry.num_sectors = 0
      entry.toarray()
      self.add_entry(entry)

    return (first_lba, last_lba)
Esempio n. 4
0
  def xml2object(self, xml):
    config_count = 0
    instruct_count = 0
    phy_part_count = 0

    root     = ET.parse(xml)
    iterator = root.getiterator()

    for e in iterator:
      if e.tag == "configuration":
        config_count += 1
      elif e.tag == "parser_instructions":
        instruct_count += 1
        INSTRUCTIONS.text2expr(e.text)
      elif e.tag == "physical_partition":
        phy_part_count += 1
      elif e.tag == "partition":
        if e.keys():
          part = pt.Partition()
          part.items2expr(e.items())
          if part.is_gpt is True and part.is_mbr is False:
            PARTITIONS._type = PARTITIONS.GPT_TYPE
          elif part.is_gpt is False and part.is_mbr is True:
            PARTITIONS._type = PARTITIONS.MBR_TYPE
          else:
            BUG.error("Cannot defined the type of partition table.")

          # Now add this Partition object to PARTITIONS unless it's the
          # label EXT, which is a left over legacy tag
          if part.label != 'EXT':
            PARTITIONS.add_part(part)
          else:
            BUG.error("Invalidate label (EXT) for tag (partition).")
        else:
          BUG.info("Empty keys for tag (partition).")
      else:
        BUG.error("Invalidate tag (%s)." % e.tag)

      if config_count > 1 or instruct_count > 1 or phy_part_count > 1:
        BUG.error("Multiple defined tag (%s)." % e.tag)

    if len(PARTITIONS.part_list) == 0:
      BUG.error("Empty tag (physical_partition) was detected.")

    if (PARTITIONS._type is PARTITIONS.GPT_TYPE) and \
       (INSTRUCTIONS.WRITE_PROTECT_GPT is True) and \
       (INSTRUCTIONS.WRITE_PROTECT_BULK_SIZE_IN_KB != 0):
      sectors_per_bulk = pt.kb2sectors(INSTRUCTIONS.WRITE_PROTECT_BULK_SIZE_IN_KB)
      first_chunk = PARTITIONS.wp_chunk_list[0]
      first_chunk.start_sector = 0
      first_chunk.end_sector   = sectors_per_bulk - 1
      first_chunk.num_sectors  = sectors_per_bulk
      first_chunk.start_bulk   = first_chunk.start_sector / sectors_per_bulk
      first_chunk.num_bulk     = first_chunk.num_sectors / sectors_per_bulk
Esempio n. 5
0
    def init_primary_gpt(self):
        first_lba = self.primary_gpt.first_partition_lba
        last_lba = first_lba
        sectors_till_next_bulk = 0

        kb_per_bulk = INSTRUCTIONS.WRITE_PROTECT_BULK_SIZE_IN_KB
        sectors_per_bulk = pt.kb2sectors(kb_per_bulk)

        print "=" * 60
        print "| PartName    Size(KB)  Readonly FirstLBA  LastLBA"
        print "=" * 60

        for i in range(len(PARTITIONS.part_list)):

            part = PARTITIONS.part_list[i]
            last_wp_chunk = PARTITIONS.wp_chunk_list[-1]

            if kb_per_bulk > 0:
                sectors_till_next_bulk = pt.sectors_till_next_bulk(first_lba, kb_per_bulk)

            if part.readonly is True:
                # To be here means this partition is read-only, so see if
                # we need to move the start lba
                if first_lba > last_wp_chunk.end_sector:
                    first_lba += sectors_till_next_bulk
                PARTITIONS.update_wp_chunk_list(first_lba, part.size_in_sec, sectors_per_bulk)
            else:
                # To be here means this partition is writeable, so see if
                # we need to move the start
                if first_lba <= last_wp_chunk.end_sector:
                    first_lba += sectors_till_next_bulk

            # The last partition
            if (i + 1) == len(PARTITIONS.part_list) and INSTRUCTIONS.AUTO_GROW_LAST_PARTITION is True:
                part.size_in_kb = part.size_in_sec = 0  # Infinite huge

            # Increase by number of sectors, last lba inclusive, so add 1 for size.
            last_lba = first_lba + part.size_in_sec
            # Inclusive, meaning 0 to 3 is 4 sectors, or another way,
            # last lba must be odd.
            last_lba -= 1

            unique_guid = 0x0
            if OPTIONS.sequential_guid is True:
                unique_guid = i + 1
            elif part.uniqueguid != "":
                unique_guid = part.uniqueguid
            else:
                unique_guid = random.randint(0, 2 ** (128))

            attributes = 0x0
            if part.readonly is True:
                attributes |= 1 << 60
            if part.hidden is True:
                attributes |= 1 << 62
            if part.dontautomount is True:
                attributes |= 1 << 63
            if part.system is True:
                attributes |= 1

            entry = Entry()
            entry.set(part._type, unique_guid, first_lba, last_lba, attributes, part.label)
            entry.toarray()
            self.primary_gpt.add_entry(entry)

            print "| %-12s%-10d%-9s%-10d%-d" % (part.label, part.size_in_kb, str(part.readonly), first_lba, last_lba)
            print "-" * 60

            first_lba = last_lba + 1
            last_lba = first_lba

        if INSTRUCTIONS.AUTO_GROW_LAST_PARTITION is False:
            last_lba += 32  # 33 - 1 (Size of Secondary GPT - 1)
        else:
            last_lba = 0x0

        # Calculate to the numbers of entry items into array.
        real_entry_number = len(PARTITIONS.part_list)
        entry_number = 0
        if OPTIONS.all_128_partitions is True:
            entry_number = 128
        else:
            entry_number = (real_entry_number / 4) * 4
            if real_entry_number % 4 > 0:
                entry_number += 4
        entry_array_crc32 = self.primary_gpt.entry_array_crc32(entry_number)

        self.primary_gpt.update_gpt_header(last_lba, entry_number, entry_array_crc32)
        self.primary_gpt.toarray()
Esempio n. 6
0
    def init_primary_gpt(self):
        first_lba = self.primary_gpt.first_partition_lba
        last_lba = first_lba
        sectors_till_next_bulk = 0

        kb_per_bulk = INSTRUCTIONS.WRITE_PROTECT_BULK_SIZE_IN_KB
        sectors_per_bulk = pt.kb2sectors(kb_per_bulk)

        print '=' * 60
        print '| PartName    Size(KB)  Readonly FirstLBA  LastLBA'
        print '=' * 60

        for i in range(len(PARTITIONS.part_list)):

            part = PARTITIONS.part_list[i]
            last_wp_chunk = PARTITIONS.wp_chunk_list[-1]

            if kb_per_bulk > 0:
                sectors_till_next_bulk = pt.sectors_till_next_bulk(
                    first_lba, kb_per_bulk)

            if part.readonly is True:
                # To be here means this partition is read-only, so see if
                # we need to move the start lba
                if first_lba > last_wp_chunk.end_sector:
                    first_lba += sectors_till_next_bulk
                PARTITIONS.update_wp_chunk_list(first_lba, part.size_in_sec,
                                                sectors_per_bulk)
            else:
                # To be here means this partition is writeable, so see if
                # we need to move the start
                if first_lba <= last_wp_chunk.end_sector:
                    first_lba += sectors_till_next_bulk

            # The last partition
            if (i + 1) == len(PARTITIONS.part_list) and \
               INSTRUCTIONS.AUTO_GROW_LAST_PARTITION is True:
                part.size_in_kb = part.size_in_sec = 0  # Infinite huge

            # Increase by number of sectors, last lba inclusive, so add 1 for size.
            last_lba = first_lba + part.size_in_sec
            # Inclusive, meaning 0 to 3 is 4 sectors, or another way,
            # last lba must be odd.
            last_lba -= 1

            unique_guid = 0x0
            if OPTIONS.sequential_guid is True:
                unique_guid = i + 1
            elif part.uniqueguid != "":
                unique_guid = part.uniqueguid
            else:
                unique_guid = random.randint(0, 2**(128))

            attributes = 0x0
            if part.readonly is True:
                attributes |= 1 << 60
            if part.hidden is True:
                attributes |= 1 << 62
            if part.dontautomount is True:
                attributes |= 1 << 63
            if part.system is True:
                attributes |= 1

            entry = Entry()
            entry.set(part._type, unique_guid, first_lba, \
                      last_lba, attributes, part.label)
            entry.toarray()
            self.primary_gpt.add_entry(entry)

            print "| %-12s%-10d%-9s%-10d%-d" \
              % (part.label, part.size_in_kb, str(part.readonly), first_lba, last_lba)
            print '-' * 60

            first_lba = last_lba + 1
            last_lba = first_lba

        if INSTRUCTIONS.AUTO_GROW_LAST_PARTITION is False:
            last_lba += 32  # 33 - 1 (Size of Secondary GPT - 1)
        else:
            last_lba = 0x0

        # Calculate to the numbers of entry items into array.
        real_entry_number = len(PARTITIONS.part_list)
        entry_number = 0
        if OPTIONS.all_128_partitions is True:
            entry_number = 128
        else:
            entry_number = (real_entry_number / 4) * 4
            if real_entry_number % 4 > 0:
                entry_number += 4
        entry_array_crc32 = self.primary_gpt.entry_array_crc32(entry_number)

        self.primary_gpt.update_gpt_header(last_lba, entry_number, \
                                           entry_array_crc32)
        self.primary_gpt.toarray()