def AddCache(output_zip):
    """Create an empty cache image and store it in output_zip."""

    img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "cache.img")
    if os.path.exists(img.input_name):
        print("cache.img already exists; no need to rebuild...")
        return

    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "cache")
    # The build system has to explicitly request for cache.img.
    if "fs_type" not in image_props:
        return

    print("creating cache.img...")

    # Use a fixed timestamp (01/01/2009) when packaging the image.
    # Bug: 24377993
    epoch = datetime.datetime.fromtimestamp(0)
    timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
    image_props["timestamp"] = int(timestamp)

    user_dir = common.MakeTempDir()

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/cache"].fs_type
    succ = build_image.BuildImage(user_dir, image_props, img.name)
    assert succ, "build cache.img image failed"

    common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
    img.Write()
def CreateImage(input_dir, info_dict, what, output_file, block_list=None):
  print("creating " + what + ".img...")

  image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
  fstab = info_dict["fstab"]
  mount_point = "/" + what
  if fstab and mount_point in fstab:
    image_props["fs_type"] = fstab[mount_point].fs_type

  image_props["timestamp"] = FIXED_FILE_TIMESTAMP

  if what == "system":
    fs_config_prefix = ""
  else:
    fs_config_prefix = what + "_"

  fs_config = os.path.join(
      input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
  if not os.path.exists(fs_config):
    fs_config = None

  # Override values loaded from info_dict.
  if fs_config:
    image_props["fs_config"] = fs_config
  if block_list:
    image_props["block_list"] = block_list.name

  # Use repeatable ext4 FS UUID and hash_seed UUID (based on partition name and
  # build fingerprint).
  uuid_seed = what + "-"
  if "build.prop" in info_dict:
    build_prop = info_dict["build.prop"]
    if "ro.build.fingerprint" in build_prop:
      uuid_seed += build_prop["ro.build.fingerprint"]
    elif "ro.build.thumbprint" in build_prop:
      uuid_seed += build_prop["ro.build.thumbprint"]
  image_props["uuid"] = str(uuid.uuid5(uuid.NAMESPACE_URL, uuid_seed))
  hash_seed = "hash_seed-" + uuid_seed
  image_props["hash_seed"] = str(uuid.uuid5(uuid.NAMESPACE_URL, hash_seed))

  succ = build_image.BuildImage(os.path.join(input_dir, what.upper()),
                                image_props, output_file.name)
  assert succ, "build " + what + ".img image failed"

  output_file.Write()
  if block_list:
    block_list.Write()

  # Set the 'adjusted_partition_size' that excludes the verity blocks of the
  # given image. When avb is enabled, this size is the max image size returned
  # by the avb tool.
  is_verity_partition = "verity_block_device" in image_props
  verity_supported = (image_props.get("verity") == "true" or
                      image_props.get("avb_enable") == "true")
  is_avb_enable = image_props.get("avb_hashtree_enable") == "true"
  if verity_supported and (is_verity_partition or is_avb_enable):
    adjusted_blocks_value = image_props.get("partition_size")
    if adjusted_blocks_value:
      adjusted_blocks_key = what + "_adjusted_partition_size"
      info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1
Example #3
0
def CreateImage(input_dir, info_dict, what, block_list=None):
  print("creating " + what + ".img...")

  img = common.MakeTempFile(prefix=what + "-", suffix=".img")

  # The name of the directory it is making an image out of matters to
  # mkyaffs2image.  It wants "system" but we have a directory named
  # "SYSTEM", so create a symlink.
  try:
    os.symlink(os.path.join(input_dir, what.upper()),
               os.path.join(input_dir, what))
  except OSError as e:
    # bogus error on my mac version?
    #   File "./build/tools/releasetools/img_from_target_files"
    #     os.path.join(OPTIONS.input_tmp, "system"))
    # OSError: [Errno 17] File exists
    if e.errno == errno.EEXIST:
      pass

  image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
  fstab = info_dict["fstab"]
  mount_point = "/" + what
  if fstab and mount_point in fstab:
    image_props["fs_type"] = fstab[mount_point].fs_type

  # Use a fixed timestamp (01/01/2009) when packaging the image.
  # Bug: 24377993
  epoch = datetime.datetime.fromtimestamp(0)
  timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
  image_props["timestamp"] = int(timestamp)

  if what == "system":
    fs_config_prefix = ""
  else:
    fs_config_prefix = what + "_"

  fs_config = os.path.join(
      input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
  if not os.path.exists(fs_config):
    fs_config = None

  # Override values loaded from info_dict.
  if fs_config:
    image_props["fs_config"] = fs_config
  if block_list:
    image_props["block_list"] = block_list

  succ = build_image.BuildImage(os.path.join(input_dir, what),
                                image_props, img)
  assert succ, "build " + what + ".img image failed"

  is_verity_partition = "verity_block_device" in image_props
  verity_supported = image_props.get("verity") == "true"
  if is_verity_partition and verity_supported:
    adjusted_blocks_value = image_props.get("partition_size")
    if adjusted_blocks_value:
      adjusted_blocks_key = what + "_adjusted_partition_size"
      info_dict[adjusted_blocks_key] = int(adjusted_blocks_value)/4096 - 1

  return img
Example #4
0
def AddUserdataExtra(output_zip):
    """Create extra userdata image and store it in output_zip."""

    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "data_extra")
    # If no userdataextra_size is provided for extfs, skip userdata_extra.img.
    if (image_props.get("fs_type", "").startswith("ext")
            and not image_props.get("partition_size")):
        return

    extra_name = image_props.get("partition_name", "extra")

    print "creating userdata_%s.img..." % extra_name

    # The name of the directory it is making an image out of matters to
    # mkyaffs2image.  So we create a temp dir, and within it we create an
    # empty dir named "data", and build the image from that.
    temp_dir = tempfile.mkdtemp()
    user_dir = os.path.join(temp_dir, "data")
    os.mkdir(user_dir)
    img = tempfile.NamedTemporaryFile()

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/data"].fs_type
    succ = build_image.BuildImage(user_dir, image_props, img.name)
    assert succ, "build userdata_%s.img image failed" % extra_name

    # Disable size check since this fetches original data partition size
    #common.CheckSize(img.name, "userdata_extra.img", OPTIONS.info_dict)
    output_zip.write(img.name, "userdata_%s.img" % extra_name)
    img.close()
    os.rmdir(user_dir)
    os.rmdir(temp_dir)
Example #5
0
def AddCache(output_zip, prefix="IMAGES/"):
  """Create an empty cache image and store it in output_zip."""

  image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                    "cache")
  # The build system has to explicitly request for cache.img.
  if "fs_type" not in image_props:
    return

  print "creating cache.img..."

  # The name of the directory it is making an image out of matters to
  # mkyaffs2image.  So we create a temp dir, and within it we create an
  # empty dir named "cache", and build the image from that.
  temp_dir = tempfile.mkdtemp()
  user_dir = os.path.join(temp_dir, "cache")
  os.mkdir(user_dir)
  img = tempfile.NamedTemporaryFile()

  fstab = OPTIONS.info_dict["fstab"]
  if fstab:
    image_props["fs_type" ] = fstab["/cache"].fs_type
  succ = build_image.BuildImage(user_dir, image_props, img.name)
  assert succ, "build cache.img image failed"

  common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
  output_zip.write(img.name, prefix + "cache.img")
  img.close()
  os.rmdir(user_dir)
  os.rmdir(temp_dir)
Example #6
0
def AddUserdata(output_zip, prefix="IMAGES/"):
  """Create an empty userdata image and store it in output_zip."""

  image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                    "data")
  # We only allow yaffs to have a 0/missing partition_size.
  # Extfs, f2fs must have a size. Skip userdata.img if no size.
  if (not image_props.get("fs_type", "").startswith("yaffs") and
      not image_props.get("partition_size")):
    return

  print "creating userdata.img..."

  # The name of the directory it is making an image out of matters to
  # mkyaffs2image.  So we create a temp dir, and within it we create an
  # empty dir named "data", and build the image from that.
  temp_dir = tempfile.mkdtemp()
  user_dir = os.path.join(temp_dir, "data")
  os.mkdir(user_dir)
  img = tempfile.NamedTemporaryFile()

  fstab = OPTIONS.info_dict["fstab"]
  if fstab:
    image_props["fs_type" ] = fstab["/data"].fs_type
  succ = build_image.BuildImage(user_dir, image_props, img.name)
  assert succ, "build userdata.img image failed"

  common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
  output_zip.write(img.name, prefix + "userdata.img")
  img.close()
  os.rmdir(user_dir)
  os.rmdir(temp_dir)
def AddCache(output_zip):
    """Create an empty cache image and store it in output_zip."""

    img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "cache.img")
    if os.path.exists(img.input_name):
        logger.info("cache.img already exists; no need to rebuild...")
        return

    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "cache")
    # The build system has to explicitly request for cache.img.
    if "fs_type" not in image_props:
        return

    logger.info("creating cache.img...")

    image_props["timestamp"] = FIXED_FILE_TIMESTAMP

    user_dir = common.MakeTempDir()

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/cache"].fs_type
    build_image.BuildImage(user_dir, image_props, img.name)

    common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
    img.Write()
Example #8
0
def AddUserdata(output_zip):
  """Create a userdata image and store it in output_zip.

  In most case we just create and store an empty userdata.img;
  But the invoker can also request to create userdata.img with real
  data from the target files, by setting "userdata_img_with_data=true"
  in OPTIONS.info_dict.
  """

  img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "userdata.img")
  if os.path.exists(img.name):
    logger.info("userdata.img already exists; no need to rebuild...")
    return

  # Skip userdata.img if no size.
  image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict, "data")
  if not image_props.get("partition_size"):
    return

  logger.info("creating userdata.img...")

  image_props["timestamp"] = FIXED_FILE_TIMESTAMP

  if OPTIONS.info_dict.get("userdata_img_with_data") == "true":
    user_dir = os.path.join(OPTIONS.input_tmp, "DATA")
  else:
    user_dir = common.MakeTempDir()

  fstab = OPTIONS.info_dict["fstab"]
  if fstab:
    image_props["fs_type"] = fstab["/data"].fs_type
  build_image.BuildImage(user_dir, image_props, img.name)

  common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
  img.Write()
def CreateImage(input_dir, info_dict, what, output_file, block_list=None):
    logger.info("creating %s.img...", what)

    image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
    image_props["timestamp"] = FIXED_FILE_TIMESTAMP

    if what == "system":
        fs_config_prefix = ""
    else:
        fs_config_prefix = what + "_"

    fs_config = os.path.join(
        input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
    if not os.path.exists(fs_config):
        fs_config = None

    # Override values loaded from info_dict.
    if fs_config:
        image_props["fs_config"] = fs_config
    if block_list:
        image_props["block_list"] = block_list.name

    # Use repeatable ext4 FS UUID and hash_seed UUID (based on partition name and
    # build fingerprint).
    build_info = common.BuildInfo(info_dict)
    uuid_seed = what
    if what != "modules":
        uuid_seed += "-" + build_info.GetPartitionFingerprint(what)
    image_props["uuid"] = str(uuid.uuid5(uuid.NAMESPACE_URL, uuid_seed))
    hash_seed = "hash_seed-" + uuid_seed
    image_props["hash_seed"] = str(uuid.uuid5(uuid.NAMESPACE_URL, hash_seed))

    build_image.BuildImage(os.path.join(input_dir, what.upper()), image_props,
                           output_file.name)

    output_file.Write()
    if block_list:
        block_list.Write()

    # Set the '_image_size' for given image size.
    is_verity_partition = "verity_block_device" in image_props
    verity_supported = (image_props.get("verity") == "true"
                        or image_props.get("avb_enable") == "true")
    is_avb_enable = image_props.get("avb_hashtree_enable") == "true"
    if verity_supported and (is_verity_partition or is_avb_enable):
        image_size = image_props.get("image_size")
        if image_size:
            image_size_key = what + "_image_size"
            info_dict[image_size_key] = int(image_size)

    use_dynamic_size = (info_dict.get("use_dynamic_partition_size") == "true"
                        and what in shlex.split(
                            info_dict.get("dynamic_partition_list",
                                          "").strip()))
    if use_dynamic_size:
        info_dict.update(build_image.GlobalDictFromImageProp(
            image_props, what))
Example #10
0
def AddUserdata(output_zip, prefix="IMAGES/"):
    """Create a userdata image and store it in output_zip.

  In most case we just create and store an empty userdata.img;
  But the invoker can also request to create userdata.img with real
  data from the target files, by setting "userdata_img_with_data=true"
  in OPTIONS.info_dict.
  """

    prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
    if os.path.exists(prebuilt_path):
        print("userdata.img already exists in %s, no need to rebuild..." %
              prefix)
        return

    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "data")
    # We only allow yaffs to have a 0/missing partition_size.
    # Extfs, f2fs must have a size. Skip userdata.img if no size.
    if (not image_props.get("fs_type", "").startswith("yaffs")
            and not image_props.get("partition_size")):
        return

    print("creating userdata.img...")

    # Use a fixed timestamp (01/01/2009) when packaging the image.
    # Bug: 24377993
    epoch = datetime.datetime.fromtimestamp(0)
    timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
    image_props["timestamp"] = int(timestamp)

    # The name of the directory it is making an image out of matters to
    # mkyaffs2image.  So we create a temp dir, and within it we create an
    # empty dir named "data", or a symlink to the DATA dir,
    # and build the image from that.
    temp_dir = tempfile.mkdtemp()
    user_dir = os.path.join(temp_dir, "data")
    empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
    if empty:
        # Create an empty dir.
        os.mkdir(user_dir)
    else:
        # Symlink to the DATA dir.
        os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"), user_dir)

    img = tempfile.NamedTemporaryFile()

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/data"].fs_type
    succ = build_image.BuildImage(user_dir, image_props, img.name)
    assert succ, "build userdata.img image failed"

    common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
    common.ZipWrite(output_zip, img.name, prefix + "userdata.img")
    img.close()
    shutil.rmtree(temp_dir)
def CreateImage(input_dir, info_dict, what, block_list=None):
    print "creating " + what + ".img..."

    img = common.MakeTempFile(prefix=what + "-", suffix=".img")

    # The name of the directory it is making an image out of matters to
    # mkyaffs2image.  It wants "system" but we have a directory named
    # "SYSTEM", so create a symlink.
    try:
        os.symlink(os.path.join(input_dir, what.upper()),
                   os.path.join(input_dir, what))
    except OSError as e:
        # bogus error on my mac version?
        #   File "./build/tools/releasetools/img_from_target_files"
        #     os.path.join(OPTIONS.input_tmp, "system"))
        # OSError: [Errno 17] File exists
        if e.errno == errno.EEXIST:
            pass

    image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
    fstab = info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/" + what].fs_type

    if what == "system":
        fs_config_prefix = ""
    else:
        fs_config_prefix = what + "_"

    fs_config = os.path.join(
        input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
    if not os.path.exists(fs_config):
        fs_config = None

    fc_config = os.path.join(input_dir, "BOOT/RAMDISK/file_contexts")
    if not os.path.exists(fc_config):
        fc_config = None

    # Override values loaded from info_dict.
    if fs_config:
        image_props["fs_config"] = fs_config
    if fc_config:
        image_props["selinux_fc"] = fc_config
    if block_list:
        image_props["block_list"] = block_list
    if image_props.get("system_root_image") == "true":
        image_props["ramdisk_dir"] = os.path.join(input_dir, "BOOT/RAMDISK")
        image_props["ramdisk_fs_config"] = os.path.join(
            input_dir, "META/boot_filesystem_config.txt")

    succ = build_image.BuildImage(os.path.join(input_dir, what), image_props,
                                  img)
    assert succ, "build " + what + ".img image failed"

    return img
Example #12
0
def AddUserdataExtra(output_zip, prefix="IMAGES/"):
    """Create extra userdata image and store it in output_zip."""

    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "data_extra")

    # The build system has to explicitly request extra userdata.
    if "fs_type" not in image_props:
        return

    extra_name = image_props.get("partition_name", "extra")

    prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix,
                                 "userdata_%s.img" % extra_name)
    if os.path.exists(prebuilt_path):
        print("userdata_%s.img already exists in %s, no need to rebuild..." % (
            extra_name,
            prefix,
        ))
        return

    # We only allow yaffs to have a 0/missing partition_size.
    # Extfs, f2fs must have a size. Skip userdata_extra.img if no size.
    if (not image_props.get("fs_type", "").startswith("yaffs")
            and not image_props.get("partition_size")):
        return

    print("creating userdata_%s.img..." % extra_name)

    # The name of the directory it is making an image out of matters to
    # mkyaffs2image.  So we create a temp dir, and within it we create an
    # empty dir named "data", and build the image from that.
    temp_dir = tempfile.mkdtemp()
    user_dir = os.path.join(temp_dir, "data")
    os.mkdir(user_dir)
    img = tempfile.NamedTemporaryFile()

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/data"].fs_type
    succ = build_image.BuildImage(user_dir, image_props, img.name)
    assert succ, "build userdata_%s.img image failed" % extra_name

    # Disable size check since this fetches original data partition size
    #common.CheckSize(img.name, "userdata_extra.img", OPTIONS.info_dict)
    output_zip.write(img.name, prefix + "userdata_%s.img" % extra_name)
    img.close()
    os.rmdir(user_dir)
    os.rmdir(temp_dir)
def AddUserdata(output_zip, prefix="IMAGES/"):
    """Create an empty userdata image and store it in output_zip."""

    prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "userdata.img")
    if os.path.exists(prebuilt_path):
        print "userdata.img already exists in %s, no need to rebuild..." % (
            prefix, )
        return

    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "data")
    # We only allow yaffs to have a 0/missing partition_size.
    # Extfs, f2fs must have a size. Skip userdata.img if no size.
    if (not image_props.get("fs_type", "").startswith("yaffs")
            and not image_props.get("partition_size")):
        return

    print "creating userdata.img..."

    # Use a fixed timestamp (01/01/2009) when packaging the image.
    # Bug: 24377993
    epoch = datetime.datetime.fromtimestamp(0)
    timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
    image_props["timestamp"] = int(timestamp)

    # The name of the directory it is making an image out of matters to
    # mkyaffs2image.  So we create a temp dir, and within it we create an
    # empty dir named "data", and build the image from that.
    temp_dir = tempfile.mkdtemp()
    user_dir = os.path.join(temp_dir, "data")
    os.mkdir(user_dir)
    img = tempfile.NamedTemporaryFile()

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/data"].fs_type
    succ = build_image.BuildImage(user_dir, image_props, img.name)
    assert succ, "build userdata.img image failed"

    common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
    common.ZipWrite(output_zip, img.name, prefix + "userdata.img")
    # overlay userdata image in product out folder
    if outpath:
        shutil.copy(img.name, outpath + '/userdata.img')
        os.chmod(outpath + '/userdata.img',
                 stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
    img.close()
    shutil.rmtree(temp_dir)
def AddCache(output_zip, prefix="IMAGES/"):
    """Create an empty cache image and store it in output_zip."""

    prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "cache.img")
    if os.path.exists(prebuilt_path):
        print "cache.img already exists in %s, no need to rebuild..." % (
            prefix, )
        return

    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "cache")
    # The build system has to explicitly request for cache.img.
    if "fs_type" not in image_props:
        return

    print "creating cache.img..."

    # Use a fixed timestamp (01/01/2009) when packaging the image.
    # Bug: 24377993
    epoch = datetime.datetime.fromtimestamp(0)
    timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
    image_props["timestamp"] = int(timestamp)

    # The name of the directory it is making an image out of matters to
    # mkyaffs2image.  So we create a temp dir, and within it we create an
    # empty dir named "cache", and build the image from that.
    temp_dir = tempfile.mkdtemp()
    user_dir = os.path.join(temp_dir, "cache")
    os.mkdir(user_dir)
    img = tempfile.NamedTemporaryFile()

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/cache"].fs_type
    succ = build_image.BuildImage(user_dir, image_props, img.name)
    assert succ, "build cache.img image failed"

    common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
    common.ZipWrite(output_zip, img.name, prefix + "cache.img")
    # overlay cache image in product out folder
    if outpath:
        shutil.copy(img.name, outpath + '/cache.img')
        os.chmod(outpath + '/cache.img',
                 stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
    img.close()
    os.rmdir(user_dir)
    os.rmdir(temp_dir)
def AddUserdata(output_zip, prefix="IMAGES/"):
    """Create a userdata image and store it in output_zip.

  In most case we just create and store an empty userdata.img;
  But the invoker can also request to create userdata.img with real
  data from the target files, by setting "userdata_img_with_data=true"
  in OPTIONS.info_dict.
  """

    img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "userdata.img")
    if os.path.exists(img.input_name):
        print("userdata.img already exists in %s, no need to rebuild..." %
              (prefix, ))
        return

    # Skip userdata.img if no size.
    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "data")
    if not image_props.get("partition_size"):
        return

    print("creating userdata.img...")

    # Use a fixed timestamp (01/01/2009) when packaging the image.
    # Bug: 24377993
    epoch = datetime.datetime.fromtimestamp(0)
    timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
    image_props["timestamp"] = int(timestamp)

    if OPTIONS.info_dict.get("userdata_img_with_data") == "true":
        user_dir = os.path.join(OPTIONS.input_tmp, "DATA")
    else:
        user_dir = tempfile.mkdtemp()
        OPTIONS.tempfiles.append(user_dir)

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/data"].fs_type
    succ = build_image.BuildImage(user_dir, image_props, img.name)
    assert succ, "build userdata.img image failed"

    common.CheckSize(img.name, "userdata.img", OPTIONS.info_dict)
    img.Write()
def AddCache(output_zip, prefix="IMAGES/"):
    """Create an empty cache image and store it in output_zip."""

    img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "cache.img")
    if os.path.exists(img.input_name):
        print("cache.img already exists in %s, no need to rebuild..." %
              (prefix, ))
        return

    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "cache")
    # The build system has to explicitly request for cache.img.
    if "fs_type" not in image_props:
        return

    print("creating cache.img...")

    # Use a fixed timestamp (01/01/2009) when packaging the image.
    # Bug: 24377993
    epoch = datetime.datetime.fromtimestamp(0)
    timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
    image_props["timestamp"] = int(timestamp)

    # The name of the directory it is making an image out of matters to
    # mkyaffs2image.  So we create a temp dir, and within it we create an
    # empty dir named "cache", and build the image from that.
    temp_dir = tempfile.mkdtemp()
    OPTIONS.tempfiles.append(temp_dir)
    user_dir = os.path.join(temp_dir, "cache")
    os.mkdir(user_dir)

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/cache"].fs_type
    succ = build_image.BuildImage(user_dir, image_props, img.name)
    assert succ, "build cache.img image failed"

    common.CheckSize(img.name, "cache.img", OPTIONS.info_dict)
    img.Write()
def AddUsbmsc(output_zip, prefix="IMAGES/"):
    """Create an empty usbmsc image and store it in output_zip."""

    prebuilt_path = os.path.join(OPTIONS.input_tmp, prefix, "usbmsc.img")
    if os.path.exists(prebuilt_path):
        print "usbmsc.img already exists in %s, no need to rebuild..." % (
            prefix, )
        return

    image_props = build_image.ImagePropFromGlobalDict(OPTIONS.info_dict,
                                                      "usbmsc")
    # The build system has to explicitly request for usbmsc.img.
    if "fs_type" not in image_props:
        return

    print "creating usbmsc.img..."

    # The name of the directory it is making an image out of matters to
    # mkyaffs2image.  So we create a temp dir, and within it we create an
    # empty dir named "usbmsc", and build the image from that.
    temp_dir = tempfile.mkdtemp()
    user_dir = os.path.join(temp_dir, "usbmsc")
    os.mkdir(user_dir)
    img = tempfile.NamedTemporaryFile()

    fstab = OPTIONS.info_dict["fstab"]
    if fstab:
        image_props["fs_type"] = fstab["/usbmsc"].fs_type
    succ = build_image.BuildImage(user_dir, image_props, img.name)
    assert succ, "build usbmsc.img image failed"

    common.CheckSize(img.name, "usbmsc.img", OPTIONS.info_dict)
    common.ZipWrite(output_zip, img.name, prefix + "usbmsc.img")
    img.close()
    os.rmdir(user_dir)
    os.rmdir(temp_dir)
Example #18
0
def CreateImage(input_dir, info_dict, what, output_file, block_list=None):
    print("creating " + what + ".img...")

    image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
    fstab = info_dict["fstab"]
    mount_point = "/" + what
    if fstab and mount_point in fstab:
        image_props["fs_type"] = fstab[mount_point].fs_type

    # Use a fixed timestamp (01/01/2009) when packaging the image.
    # Bug: 24377993
    epoch = datetime.datetime.fromtimestamp(0)
    timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
    image_props["timestamp"] = int(timestamp)

    if what == "system":
        fs_config_prefix = ""
    else:
        fs_config_prefix = what + "_"

    fs_config = os.path.join(
        input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
    if not os.path.exists(fs_config):
        fs_config = None

    # Override values loaded from info_dict.
    if fs_config:
        image_props["fs_config"] = fs_config
    if block_list:
        image_props["block_list"] = block_list.name

    # Use repeatable ext4 FS UUID and hash_seed UUID (based on partition name and
    # build fingerprint).
    uuid_seed = what + "-"
    if "build.prop" in info_dict:
        build_prop = info_dict["build.prop"]
        if "ro.build.fingerprint" in build_prop:
            uuid_seed += build_prop["ro.build.fingerprint"]
        elif "ro.build.thumbprint" in build_prop:
            uuid_seed += build_prop["ro.build.thumbprint"]
    image_props["uuid"] = str(uuid.uuid5(uuid.NAMESPACE_URL, uuid_seed))
    hash_seed = "hash_seed-" + uuid_seed
    image_props["hash_seed"] = str(uuid.uuid5(uuid.NAMESPACE_URL, hash_seed))

    succ = build_image.BuildImage(os.path.join(input_dir, what.upper()),
                                  image_props, output_file.name)
    assert succ, "build " + what + ".img image failed"

    output_file.Write()
    if block_list:
        block_list.Write()

    # Set the '_image_blocks' that excludes the verity metadata blocks of the
    # given image. When AVB is enabled, this size is the max image size returned
    # by the AVB tool.
    is_verity_partition = "verity_block_device" in image_props
    verity_supported = (image_props.get("verity") == "true"
                        or image_props.get("avb_enable") == "true")
    is_avb_enable = image_props.get("avb_hashtree_enable") == "true"
    if verity_supported and (is_verity_partition or is_avb_enable):
        image_size = image_props.get("image_size")
        if image_size:
            image_blocks_key = what + "_image_blocks"
            info_dict[image_blocks_key] = int(image_size) / 4096 - 1
def CreateImage(input_dir, info_dict, what, output_file, block_list=None):
    print("creating " + what + ".img...")

    # The name of the directory it is making an image out of matters to
    # mkyaffs2image.  It wants "system" but we have a directory named
    # "SYSTEM", so create a symlink.
    temp_dir = tempfile.mkdtemp()
    OPTIONS.tempfiles.append(temp_dir)
    try:
        os.symlink(os.path.join(input_dir, what.upper()),
                   os.path.join(temp_dir, what))
    except OSError as e:
        # bogus error on my mac version?
        #   File "./build/tools/releasetools/img_from_target_files"
        #     os.path.join(OPTIONS.input_tmp, "system"))
        # OSError: [Errno 17] File exists
        if e.errno == errno.EEXIST:
            pass

    image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
    fstab = info_dict["fstab"]
    mount_point = "/" + what
    if fstab and mount_point in fstab:
        image_props["fs_type"] = fstab[mount_point].fs_type

    # Use a fixed timestamp (01/01/2009) when packaging the image.
    # Bug: 24377993
    epoch = datetime.datetime.fromtimestamp(0)
    timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
    image_props["timestamp"] = int(timestamp)

    if what == "system":
        fs_config_prefix = ""
    else:
        fs_config_prefix = what + "_"

    fs_config = os.path.join(
        input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
    if not os.path.exists(fs_config):
        fs_config = None

    # Override values loaded from info_dict.
    if fs_config:
        image_props["fs_config"] = fs_config
    if block_list:
        image_props["block_list"] = block_list.name

    succ = build_image.BuildImage(os.path.join(temp_dir, what), image_props,
                                  output_file.name)
    assert succ, "build " + what + ".img image failed"

    output_file.Write()
    if block_list:
        block_list.Write()

    # Set the 'adjusted_partition_size' that excludes the verity blocks of the
    # given image. When avb is enabled, this size is the max image size returned
    # by the avb tool.
    is_verity_partition = "verity_block_device" in image_props
    verity_supported = (image_props.get("verity") == "true"
                        or image_props.get("avb_enable") == "true")
    is_avb_enable = image_props.get("avb_hashtree_enable") == "true"
    if verity_supported and (is_verity_partition or is_avb_enable):
        adjusted_blocks_value = image_props.get("partition_size")
        if adjusted_blocks_value:
            adjusted_blocks_key = what + "_adjusted_partition_size"
            info_dict[
                adjusted_blocks_key] = int(adjusted_blocks_value) / 4096 - 1
  # The name of the directory it is making an image out of matters to
  # mkyaffs2image.  It wants "system" but we have a directory named
  # "SYSTEM", so create a symlink.
  try:
    os.symlink(os.path.join(input_dir, what.upper()),
               os.path.join(input_dir, what))
  except OSError, e:
      # bogus error on my mac version?
      #   File "./build/tools/releasetools/img_from_target_files", line 86, in AddSystem
      #     os.path.join(OPTIONS.input_tmp, "system"))
      # OSError: [Errno 17] File exists
    if (e.errno == errno.EEXIST):
      pass

  image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
  fstab = info_dict["fstab"]
  if fstab:
    image_props["fs_type" ] = fstab["/" + what].fs_type

  if what == "system":
    fs_config_prefix = ""
  else:
    fs_config_prefix = what + "_"

  fs_config = os.path.join(
      input_dir, "META/" + fs_config_prefix + "filesystem_config.txt")
  if not os.path.exists(fs_config): fs_config = None

  fc_config = os.path.join(input_dir, "BOOT/RAMDISK/file_contexts")
  if not os.path.exists(fc_config): fc_config = None