示例#1
0
def main(argv):
    # Get sparse image
    input_image = sparse_img.SparseImage(INPUT_IMAGE,
                                         tempfile.mkstemp()[1], '0')

    # Generate output files
    b = blockimgdiff.BlockImageDiff(input_image, None)
    b.Compute('system')

    print('Done! Output files: %s' % os.path.dirname(__file__))
    return
def GetCareMap(which, imgname):
    """Generate care_map of system (or vendor) partition"""

    assert which in ("system", "vendor")
    _, blk_device = common.GetTypeAndDevice("/" + which, OPTIONS.info_dict)

    simg = sparse_img.SparseImage(imgname)
    care_map_list = []
    care_map_list.append(blk_device)
    care_map_list.append(simg.care_map.to_string_raw())
    return care_map_list
def _GetImage(which, tmpdir):
    assert which in ('system', 'vendor')

    path = os.path.join(tmpdir, 'IMAGES', which + '.img')
    mappath = os.path.join(tmpdir, 'IMAGES', which + '.map')

    # Map file must exist (allowed to be empty).
    assert os.path.exists(path) and os.path.exists(mappath)

    clobbered_blocks = '0'
    return sparse_img.SparseImage(path, mappath, clobbered_blocks)
示例#4
0
  def test_Generate(self):
    image_file = sparse_img.SparseImage(self._generate_image())
    generator = CreateHashtreeInfoGenerator('system', 4096, self.prop_dict)
    info = generator.Generate(image_file)

    self.assertEqual(RangeSet(data=[0, 991232 / 4096]), info.filesystem_range)
    self.assertEqual(RangeSet(data=[991232 / 4096, (991232 + 12288) / 4096]),
                     info.hashtree_range)
    self.assertEqual(self.hash_algorithm, info.hash_algorithm)
    self.assertEqual(self.fixed_salt, info.salt)
    self.assertEqual(self.expected_root_hash, info.root_hash)
示例#5
0
  def test_ParseHashtreeMetadata(self):
    image_file = sparse_img.SparseImage(self._generate_image())
    generator = VerifiedBootVersion1HashtreeInfoGenerator(
        self.partition_size, 4096, True)
    generator.DecomposeSparseImage(image_file)

    # pylint: disable=protected-access
    generator._ParseHashtreeMetadata()

    self.assertEqual(
        self.hash_algorithm, generator.hashtree_info.hash_algorithm)
    self.assertEqual(self.fixed_salt, generator.hashtree_info.salt)
    self.assertEqual(self.expected_root_hash, generator.hashtree_info.root_hash)
示例#6
0
    def test_ValidateHashtree_failure(self):
        generator = VerifiedBootVersion1HashtreeInfoGenerator(
            self.partition_size, 4096, True)
        generator.image = sparse_img.SparseImage(self._GenerateImage())

        generator.hashtree_info = info = HashtreeInfo()
        info.filesystem_range = RangeSet(data=[0, 991232 // 4096])
        info.hashtree_range = RangeSet(
            data=[991232 // 4096, (991232 + 12288) // 4096])
        info.hash_algorithm = self.hash_algorithm
        info.salt = self.fixed_salt
        info.root_hash = "a" + self.expected_root_hash[1:]

        self.assertFalse(generator.ValidateHashtree())
def GetCareMap(which, imgname):
  """Generates the care_map for the given partition."""
  assert which in PARTITIONS_WITH_CARE_MAP

  simg = sparse_img.SparseImage(imgname)
  care_map_ranges = simg.care_map
  key = which + "_adjusted_partition_size"
  adjusted_blocks = OPTIONS.info_dict.get(key)
  if adjusted_blocks:
    assert adjusted_blocks > 0, "blocks should be positive for " + which
    care_map_ranges = care_map_ranges.intersect(rangelib.RangeSet(
        "0-%d" % (adjusted_blocks,)))

  return [which, care_map_ranges.to_string_raw()]
示例#8
0
def main(argv):
    global input

    if len(sys.argv) < 3:
        outdir = './system'
    else:
        outdir = sys.argv[2] + '/system'

    if len(sys.argv) < 4:
        version = 4
        item = True
        while item:
            print('''            1. Android Lollipop 5.0
            2. Android Lollipop 5.1
            3. Android Marshmallow 6.0
            4. Android Nougat 7.0/7.1/8.0
            ''')
            try:
                input = raw_input
            except NameError:
                pass
            item = input('Choose system version: ')
            if item == '1':
                version = 1
                break
            elif item == '2':
                version = 2
                break
            elif item == '3':
                version = 3
                break
            elif item == '4':
                version = 4
                break
            else:
                return
    else:
        version = int(sys.argv[3])

    # Get sparse image
    image = sparse_img.SparseImage(INPUT_IMAGE, tempfile.mkstemp()[1], '0')

    # Generate output files
    b = blockimgdiff.BlockImageDiff(image, None, version)
    b.Compute(outdir)

    print('Done! Output files: %s' % os.path.dirname(outdir))
    return
def GetCareMap(which, imgname):
    """Generate care_map of system (or vendor) partition"""

    assert which in ("system", "vendor")

    simg = sparse_img.SparseImage(imgname)
    care_map_list = [which]

    care_map_ranges = simg.care_map
    key = which + "_adjusted_partition_size"
    adjusted_blocks = OPTIONS.info_dict.get(key)
    if adjusted_blocks:
        assert adjusted_blocks > 0, "blocks should be positive for " + which
        care_map_ranges = care_map_ranges.intersect(
            rangelib.RangeSet("0-%d" % (adjusted_blocks, )))

    care_map_list.append(care_map_ranges.to_string_raw())
    return care_map_list
def GetImage(which, tmpdir, info_dict):
    # Return an image object (suitable for passing to BlockImageDiff)
    # for the 'which' partition (most be "system" or "vendor").  If a
    # prebuilt image and file map are found in tmpdir they are used,
    # otherwise they are reconstructed from the individual files.

    path = os.path.join(tmpdir, "IMAGES", which + ".img")
    mappath = os.path.join(tmpdir, "IMAGES", which + ".map")
    if os.path.exists(path) and os.path.exists(mappath):
        print "using %s.img from target-files" % (which, )
        # This is a 'new' target-files, which already has the image in it.
    else:
        print "building %s.img from target-files" % (which, )
        # This is an 'old' target-files, which does not contain images
        # already built.  Build them.
        mappath = tempfile.mkstemp()[1]
        common.OPTIONS.tempfiles.append(mappath)
        path = BuildExt4(which, tmpdir, info_dict, block_list=mappath)

    return sparse_img.SparseImage(path, mappath)
def GetSystemImage(info, tmpdir):
    path = os.path.join(tmpdir, "IMAGES", "system.img")
    mappath = os.path.join(tmpdir, "IMAGES", "system.map")
    if os.path.exists(path) and os.path.exists(mappath):
        print "using system.img from target-files"
        # This is a 'new' target-files, which already has the image in it.

    else:
        print "building system.img from target-files"

        # This is an 'old' target-files, which does not contain images
        # already built.  Build them.

        mappath = tempfile.mkstemp()[1]
        OPTIONS.tempfiles.append(mappath)

        import add_img_to_target_files
        path = add_img_to_target_files.BuildSystem(tmpdir,
                                                   info,
                                                   block_list=mappath)

    return sparse_img.SparseImage(path, mappath)
示例#12
0
def GetCareMap(which, imgname):
    """Returns the care_map string for the given partition.

  Args:
    which: The partition name, must be listed in PARTITIONS_WITH_CARE_MAP.
    imgname: The filename of the image.

  Returns:
    (which, care_map_ranges): care_map_ranges is the raw string of the care_map
    RangeSet.
  """
    assert which in PARTITIONS_WITH_CARE_MAP

    simg = sparse_img.SparseImage(imgname)
    care_map_ranges = simg.care_map
    key = which + "_image_blocks"
    image_blocks = OPTIONS.info_dict.get(key)
    if image_blocks:
        assert image_blocks > 0, "blocks for {} must be positive".format(which)
        care_map_ranges = care_map_ranges.intersect(
            rangelib.RangeSet("0-{}".format(image_blocks)))

    return [which, care_map_ranges.to_string_raw()]
def GetCareMap(which, imgname):
    """Returns the care_map string for the given partition.

  Args:
    which: The partition name, must be listed in PARTITIONS_WITH_CARE_MAP.
    imgname: The filename of the image.

  Returns:
    (which, care_map_ranges): care_map_ranges is the raw string of the care_map
    RangeSet.
  """
    assert which in PARTITIONS_WITH_CARE_MAP

    simg = sparse_img.SparseImage(imgname)
    care_map_ranges = simg.care_map
    key = which + "_adjusted_partition_size"
    adjusted_blocks = OPTIONS.info_dict.get(key)
    if adjusted_blocks:
        assert adjusted_blocks > 0, "blocks should be positive for " + which
        care_map_ranges = care_map_ranges.intersect(
            rangelib.RangeSet("0-%d" % (adjusted_blocks, )))

    return [which, care_map_ranges.to_string_raw()]
示例#14
0
def main(argv):
    if len(sys.argv) < 3:
        version = 4
        item = True
        while item:
            print('''            1. Android Lollipop 5.0
            2. Android Lollipop 5.1
            3. Android Marshmallow 6.0
            4. Android Nougat 7.0
            ''')
            item = raw_input('Choose system version: ')
            if item == '1':
                version = 1
                break
            elif item == '2':
                version = 2
                break
            elif item == '3':
                version = 3
                break
            elif item == '4':
                version = 4
                break
            else:
                return
    else:
        version = int(sys.argv[2])

    # Get sparse image
    image = sparse_img.SparseImage(INPUT_IMAGE, tempfile.mkstemp()[1], '0')

    # Generate output files
    b = blockimgdiff.BlockImageDiff(image, None, version)
    b.Compute('system')

    print('Done! Output files: %s' % os.path.dirname(__file__))
    return
def GetCareMap(which, imgname):
  """Returns the care_map string for the given partition.

  Args:
    which: The partition name, must be listed in PARTITIONS_WITH_CARE_MAP.
    imgname: The filename of the image.

  Returns:
    (which, care_map_ranges): care_map_ranges is the raw string of the care_map
    RangeSet; or None.
  """
  assert which in common.PARTITIONS_WITH_CARE_MAP

  # which + "_image_size" contains the size that the actual filesystem image
  # resides in, which is all that needs to be verified. The additional blocks in
  # the image file contain verity metadata, by reading which would trigger
  # invalid reads.
  image_size = OPTIONS.info_dict.get(which + "_image_size")
  if not image_size:
    return None

  image_blocks = int(image_size) // 4096 - 1
  assert image_blocks > 0, "blocks for {} must be positive".format(which)

  # For sparse images, we will only check the blocks that are listed in the care
  # map, i.e. the ones with meaningful data.
  if "extfs_sparse_flag" in OPTIONS.info_dict:
    simg = sparse_img.SparseImage(imgname)
    care_map_ranges = simg.care_map.intersect(
        rangelib.RangeSet("0-{}".format(image_blocks)))

  # Otherwise for non-sparse images, we read all the blocks in the filesystem
  # image.
  else:
    care_map_ranges = rangelib.RangeSet("0-{}".format(image_blocks))

  return [which, care_map_ranges.to_string_raw()]
示例#16
0
def ZeroPadSimg(image_file, pad_size):
    blocks = pad_size // BLOCK_SIZE
    logger.info("Padding %d blocks (%d bytes)", blocks, pad_size)
    simg = sparse_img.SparseImage(image_file, mode="r+b", build_map=False)
    simg.AppendFillChunk(0, blocks)
示例#17
0
def GetSimgSize(image_file):
    simg = sparse_img.SparseImage(image_file, build_map=False)
    return simg.blocksize * simg.total_blocks
 def GetPartitionSizeFromImage(img):
     try:
         simg = sparse_img.SparseImage(img)
         return simg.blocksize * simg.total_blocks
     except ValueError:
         return os.path.getsize(img)
示例#19
0
def main(sysimg, outdir):
    tgt = sparse_img.SparseImage(sysimg)
    bif = blockimgdiff.BlockImageDiff(tgt, None)
    bif.Compute(outdir)
    return