def AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path, dtbo_img_path, prefix="IMAGES/"): """Create a VBMeta image and store it in output_zip.""" img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img") avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] cmd = [avbtool, "make_vbmeta_image", "--output", img.name] common.AppendAVBSigningArgs(cmd, "vbmeta") public_key_dir = tempfile.mkdtemp(prefix="avbpubkey-") OPTIONS.tempfiles.append(public_key_dir) AppendVBMetaArgsForPartition(cmd, "boot", boot_img_path, public_key_dir) AppendVBMetaArgsForPartition(cmd, "system", system_img_path, public_key_dir) AppendVBMetaArgsForPartition(cmd, "vendor", vendor_img_path, public_key_dir) AppendVBMetaArgsForPartition(cmd, "dtbo", dtbo_img_path, public_key_dir) args = OPTIONS.info_dict.get("avb_vbmeta_args") if args and args.strip(): cmd.extend(shlex.split(args)) p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() assert p.returncode == 0, "avbtool make_vbmeta_image failed" img.Write()
def AddDtbo(output_zip): """Adds the DTBO image. Uses the image under IMAGES/ if it already exists. Otherwise looks for the image under PREBUILT_IMAGES/, signs it as needed, and returns the image name. """ img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "dtbo.img") if os.path.exists(img.name): logger.info("dtbo.img already exists; no need to rebuild...") return img.name dtbo_prebuilt_path = os.path.join(OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img") assert os.path.exists(dtbo_prebuilt_path) shutil.copy(dtbo_prebuilt_path, img.name) # AVB-sign the image as needed. if OPTIONS.info_dict.get("avb_enable") == "true": avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] part_size = OPTIONS.info_dict["dtbo_size"] # The AVB hash footer will be replaced if already present. cmd = [ avbtool, "add_hash_footer", "--image", img.name, "--partition_size", str(part_size), "--partition_name", "dtbo" ] common.AppendAVBSigningArgs(cmd, "dtbo") args = OPTIONS.info_dict.get("avb_dtbo_add_hash_footer_args") if args and args.strip(): cmd.extend(shlex.split(args)) common.RunAndCheckOutput(cmd) img.Write() return img.name
def AddDtbo(output_zip, prefix="IMAGES/"): """Adds the DTBO image. Uses the image under prefix if it already exists. Otherwise looks for the image under PREBUILT_IMAGES/, signs it as needed, and returns the image name. """ img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "dtbo.img") if os.path.exists(img.input_name): print("dtbo.img already exists in %s, no need to rebuild..." % (prefix,)) return img.input_name dtbo_prebuilt_path = os.path.join( OPTIONS.input_tmp, "PREBUILT_IMAGES", "dtbo.img") assert os.path.exists(dtbo_prebuilt_path) shutil.copy(dtbo_prebuilt_path, img.name) # AVB-sign the image as needed. if OPTIONS.info_dict.get("avb_enable") == "true": avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] part_size = OPTIONS.info_dict["dtbo_size"] # The AVB hash footer will be replaced if already present. cmd = [avbtool, "add_hash_footer", "--image", img.name, "--partition_size", str(part_size), "--partition_name", "dtbo"] common.AppendAVBSigningArgs(cmd, "dtbo") args = OPTIONS.info_dict.get("avb_dtbo_add_hash_footer_args") if args and args.strip(): cmd.extend(shlex.split(args)) p = common.Run(cmd, stdout=subprocess.PIPE) p.communicate() assert p.returncode == 0, \ "avbtool add_hash_footer of %s failed" % (img.name,) img.Write() return img.name
def AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path, prefix="IMAGES/"): """Create a VBMeta image and store it in output_zip.""" img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img") avbtool = os.getenv('AVBTOOL') or "avbtool" cmd = [ avbtool, "make_vbmeta_image", "--output", img.name, "--include_descriptors_from_image", boot_img_path, "--include_descriptors_from_image", system_img_path ] if vendor_img_path is not None: cmd.extend(["--include_descriptors_from_image", vendor_img_path]) if OPTIONS.info_dict.get("system_root_image", None) == "true": cmd.extend(["--setup_rootfs_from_kernel", system_img_path]) common.AppendAVBSigningArgs(cmd) args = OPTIONS.info_dict.get("board_avb_make_vbmeta_image_args", None) if args and args.strip(): cmd.extend(shlex.split(args)) p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() assert p.returncode == 0, "avbtool make_vbmeta_image failed" img.Write()
def AddOdm(output_zip): """Turn the contents of ODM into an odm image and store it in output_zip.""" img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "odm.img") if os.path.exists(img.name): logger.info("odm.img already exists; no need to rebuild...") # AVB-sign the image as needed. if OPTIONS.info_dict.get("avb_enable") == "true": logger.info("updating avb hash for prebuilt odm.img...") avbtool = OPTIONS.info_dict["avb_avbtool"] # The AVB hash footer will be replaced if already present. cmd = [avbtool, "add_hashtree_footer", "--image", img.name, "--partition_name", "odm"] common.AppendAVBSigningArgs(cmd, "odm") args = OPTIONS.info_dict.get("avb_odm_add_hash_footer_args") if args and args.strip(): cmd.extend(shlex.split(args)) common.RunAndCheckOutput(cmd) return img.name block_list = OutputFile( output_zip, OPTIONS.input_tmp, "IMAGES", "odm.map") CreateImage( OPTIONS.input_tmp, OPTIONS.info_dict, "odm", img, block_list=block_list) return img.name
def AddVBMeta(output_zip, partitions, prefix="IMAGES/"): """Creates a VBMeta image and store it in output_zip. Args: output_zip: The output zip file, which needs to be already open. partitions: A dict that's keyed by partition names with image paths as values. Only valid partition names are accepted, which include 'boot', 'recovery', 'system', 'vendor', 'dtbo'. """ img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img") if os.path.exists(img.input_name): print("vbmeta.img already exists in %s; not rebuilding..." % (prefix, )) return img.input_name avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] cmd = [avbtool, "make_vbmeta_image", "--output", img.name] common.AppendAVBSigningArgs(cmd, "vbmeta") public_key_dir = tempfile.mkdtemp(prefix="avbpubkey-") OPTIONS.tempfiles.append(public_key_dir) for partition, path in partitions.items(): assert partition in common.AVB_PARTITIONS, 'Unknown partition: %s' % ( partition, ) assert os.path.exists( path), 'Failed to find %s for partition %s' % (path, partition) AppendVBMetaArgsForPartition(cmd, partition, path, public_key_dir) args = OPTIONS.info_dict.get("avb_vbmeta_args") if args and args.strip(): split_args = shlex.split(args) for index, arg in enumerate(split_args[:-1]): # Sanity check that the image file exists. Some images might be defined # as a path relative to source tree, which may not be available at the # same location when running this script (we have the input target_files # zip only). For such cases, we additionally scan other locations (e.g. # IMAGES/, RADIO/, etc) before bailing out. if arg == '--include_descriptors_from_image': image_path = split_args[index + 1] if os.path.exists(image_path): continue found = False for dir in [ 'IMAGES', 'RADIO', 'VENDOR_IMAGES', 'PREBUILT_IMAGES' ]: alt_path = os.path.join(OPTIONS.input_tmp, dir, os.path.basename(image_path)) if os.path.exists(alt_path): split_args[index + 1] = alt_path found = True break assert found, 'failed to find %s' % (image_path, ) cmd.extend(split_args) p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() assert p.returncode == 0, "avbtool make_vbmeta_image failed" img.Write()
def AddVendor(output_zip, recovery_img=None, boot_img=None): """Turn the contents of VENDOR into a vendor image and store in it output_zip.""" img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "vendor.img") if os.path.exists(img.name): logger.info("vendor.img already exists; no need to rebuild...") # AVB-sign the image as needed. if OPTIONS.info_dict.get("avb_enable") == "true": logger.info("updating avb hash for prebuilt vendor.img...") avbtool = OPTIONS.info_dict["avb_avbtool"] # The AVB hash footer will be replaced if already present. cmd = [ avbtool, "add_hashtree_footer", "--image", img.name, "--partition_name", "vendor" ] common.AppendAVBSigningArgs(cmd, "vendor") args = OPTIONS.info_dict.get("avb_vendor_add_hash_footer_args") if args and args.strip(): cmd.extend(shlex.split(args)) common.RunAndCheckOutput(cmd) return img.name def output_sink(fn, data): ofile = open(os.path.join(OPTIONS.input_tmp, "VENDOR", fn), "w") ofile.write(data) ofile.close() if output_zip: arc_name = "VENDOR/" + fn if arc_name in output_zip.namelist(): OPTIONS.replace_updated_files_list.append(arc_name) else: common.ZipWrite(output_zip, ofile.name, arc_name) board_uses_vendorimage = OPTIONS.info_dict.get( "board_uses_vendorimage") == "true" if (OPTIONS.rebuild_recovery and board_uses_vendorimage and recovery_img is not None and boot_img is not None): logger.info("Building new recovery patch on vendor") common.MakeRecoveryPatch(OPTIONS.input_tmp, output_sink, recovery_img, boot_img, info_dict=OPTIONS.info_dict) block_list = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "vendor.map") CreateImage(OPTIONS.input_tmp, OPTIONS.info_dict, "vendor", img, block_list=block_list) return img.name
def AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path, dtbo_img_path, prefix="IMAGES/"): """Create a VBMeta image and store it in output_zip.""" img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img") avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] cmd = [avbtool, "make_vbmeta_image", "--output", img.name] common.AppendAVBSigningArgs(cmd, "vbmeta") public_key_dir = tempfile.mkdtemp(prefix="avbpubkey-") OPTIONS.tempfiles.append(public_key_dir) AppendVBMetaArgsForPartition(cmd, "boot", boot_img_path, public_key_dir) AppendVBMetaArgsForPartition(cmd, "system", system_img_path, public_key_dir) AppendVBMetaArgsForPartition(cmd, "vendor", vendor_img_path, public_key_dir) AppendVBMetaArgsForPartition(cmd, "dtbo", dtbo_img_path, public_key_dir) args = OPTIONS.info_dict.get("avb_vbmeta_args") if args and args.strip(): split_args = shlex.split(args) for index, arg in enumerate(split_args[:-1]): # Sanity check that the image file exists. Some images might be defined # as a path relative to source tree, which may not be available at the # same location when running this script (we have the input target_files # zip only). For such cases, we additionally scan other locations (e.g. # IMAGES/, RADIO/, etc) before bailing out. if arg == '--include_descriptors_from_image': image_path = split_args[index + 1] if os.path.exists(image_path): continue found = False for dir in [ 'IMAGES', 'RADIO', 'VENDOR_IMAGES', 'PREBUILT_IMAGES' ]: alt_path = os.path.join(OPTIONS.input_tmp, dir, os.path.basename(image_path)) if os.path.exists(alt_path): split_args[index + 1] = alt_path found = True break assert found, 'failed to find %s' % (image_path, ) cmd.extend(split_args) p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() assert p.returncode == 0, "avbtool make_vbmeta_image failed" img.Write()
def AddVBMeta(output_zip, boot_img_path, system_img_path, prefix="IMAGES/"): """Create a VBMeta image and store it in output_zip.""" _, img_file_name = tempfile.mkstemp() avbtool = os.getenv('AVBTOOL') or "avbtool" cmd = [avbtool, "make_vbmeta_image", "--output", img_file_name, "--include_descriptors_from_image", boot_img_path, "--include_descriptors_from_image", system_img_path, "--generate_dm_verity_cmdline_from_hashtree", system_img_path] common.AppendAVBSigningArgs(cmd) args = OPTIONS.info_dict.get("board_avb_make_vbmeta_image_args", None) if args and args.strip(): cmd.extend(shlex.split(args)) p = common.Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() assert p.returncode == 0, "avbtool make_vbmeta_image failed" common.ZipWrite(output_zip, img_file_name, prefix + "vbmeta.img")
def AddVBMeta(output_zip, partitions, name, needed_partitions): """Creates a VBMeta image and stores it in output_zip. It generates the requested VBMeta image. The requested image could be for top-level or chained VBMeta image, which is determined based on the name. Args: output_zip: The output zip file, which needs to be already open. partitions: A dict that's keyed by partition names with image paths as values. Only valid partition names are accepted, as listed in common.AVB_PARTITIONS. name: Name of the VBMeta partition, e.g. 'vbmeta', 'vbmeta_system'. needed_partitions: Partitions whose descriptors should be included into the generated VBMeta image. Returns: Path to the created image. Raises: AssertionError: On invalid input args. """ assert needed_partitions, "Needed partitions must be specified" img = OutputFile(output_zip, OPTIONS.input_tmp, "IMAGES", "{}.img".format(name)) if os.path.exists(img.name): logger.info("%s.img already exists; not rebuilding...", name) return img.name avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"] cmd = [avbtool, "make_vbmeta_image", "--output", img.name] common.AppendAVBSigningArgs(cmd, name) for partition, path in partitions.items(): if partition not in needed_partitions: continue assert (partition in common.AVB_PARTITIONS or partition.startswith('vbmeta_')), \ 'Unknown partition: {}'.format(partition) assert os.path.exists(path), \ 'Failed to find {} for {}'.format(path, partition) AppendVBMetaArgsForPartition(cmd, partition, path) args = OPTIONS.info_dict.get("avb_{}_args".format(name)) if args and args.strip(): split_args = shlex.split(args) for index, arg in enumerate(split_args[:-1]): # Sanity check that the image file exists. Some images might be defined # as a path relative to source tree, which may not be available at the # same location when running this script (we have the input target_files # zip only). For such cases, we additionally scan other locations (e.g. # IMAGES/, RADIO/, etc) before bailing out. if arg == '--include_descriptors_from_image': image_path = split_args[index + 1] if os.path.exists(image_path): continue found = False for dir_name in ['IMAGES', 'RADIO', 'PREBUILT_IMAGES']: alt_path = os.path.join(OPTIONS.input_tmp, dir_name, os.path.basename(image_path)) if os.path.exists(alt_path): split_args[index + 1] = alt_path found = True break assert found, 'Failed to find {}'.format(image_path) cmd.extend(split_args) common.RunAndCheckOutput(cmd) img.Write() return img.name