def AddImagesToTargetFiles(filename):if os.path.isdir(filename):OPTIONS.input_tmp = os.path.abspath(filename)input_zip = Noneelse:OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)if not OPTIONS.add_missing:if os.path.isdir(os.path.join(OPTIONS.input_tmp, "IMAGES")):print("target_files appears to already contain images.")sys.exit(1)# vendor.img is unlike system.img or system_other.img. Because it could be# built from source, or dropped into target_files.zip as a prebuilt blob. We# consider either of them as vendor.img being available, which could be used# when generating vbmeta.img for AVB.has_vendor = (os.path.isdir(os.path.join(OPTIONS.input_tmp, "VENDOR")) oros.path.exists(os.path.join(OPTIONS.input_tmp, "IMAGES","vendor.img")))has_oem = (os.path.isdir(os.path.join(OPTIONS.input_tmp, "OEM")) oros.path.exists(os.path.join(OPTIONS.input_tmp, "IMAGES","oem.img")))has_system_other = os.path.isdir(os.path.join(OPTIONS.input_tmp,"SYSTEM_OTHER"))if input_zip:OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)common.ZipClose(input_zip)output_zip = zipfile.ZipFile(filename, "a",compression=zipfile.ZIP_DEFLATED,allowZip64=True)else:OPTIONS.info_dict = common.LoadInfoDict(filename, filename)output_zip = Noneimages_dir = os.path.join(OPTIONS.input_tmp, "IMAGES")if not os.path.isdir(images_dir):os.makedirs(images_dir)images_dir = Nonehas_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")if OPTIONS.info_dict.get("avb_enable") == "true":fp = Noneif "build.prop" in OPTIONS.info_dict:build_prop = OPTIONS.info_dict["build.prop"]if "ro.build.fingerprint" in build_prop:fp = build_prop["ro.build.fingerprint"]elif "ro.build.thumbprint" in build_prop:fp = build_prop["ro.build.thumbprint"]if fp:OPTIONS.info_dict["avb_salt"] = hashlib.sha256(fp).hexdigest()def banner(s):print("\n\n++++ " + s + " ++++\n\n")prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img.sig")boot_image = Noneif os.path.exists(prebuilt_path):banner("boot")print("boot.img already exists in IMAGES/, no need to rebuild...")if OPTIONS.rebuild_recovery:boot_image = common.GetBootableImage("IMAGES/boot.img.sig", "boot.img.sig", OPTIONS.input_tmp, "BOOT")else:banner("boot")boot_image = common.GetBootableImage("IMAGES/boot.img.sig", "boot.img.sig", OPTIONS.input_tmp, "BOOT")if boot_image:if output_zip:boot_image.AddToZip(output_zip)else:boot_image.WriteToDir(OPTIONS.input_tmp)recovery_image = Noneif has_recovery:banner("recovery")prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img.sig")if os.path.exists(prebuilt_path):print("recovery.img already exists in IMAGES/, no need to rebuild...")if OPTIONS.rebuild_recovery:recovery_image = common.GetBootableImage("IMAGES/recovery.img.sig", "recovery.img.sig", OPTIONS.input_tmp,"RECOVERY")else:recovery_image = common.GetBootableImage("IMAGES/recovery.img.sig", "recovery.img.sig", OPTIONS.input_tmp, "RECOVERY")if recovery_image:if output_zip:recovery_image.AddToZip(output_zip)else:recovery_image.WriteToDir(OPTIONS.input_tmp)banner("recovery (two-step image)")# The special recovery.img for two-step package use.recovery_two_step_image = common.GetBootableImage("IMAGES/recovery-two-step.img", "recovery-two-step.img",OPTIONS.input_tmp, "RECOVERY", two_step_image=True)if recovery_two_step_image:if output_zip:recovery_two_step_image.AddToZip(output_zip)else:recovery_two_step_image.WriteToDir(OPTIONS.input_tmp)banner("system")system_img_path = AddSystem(output_zip, recovery_img=recovery_image, boot_img=boot_image)vendor_img_path = Noneoem_img_path = Noneif has_vendor:banner("vendor")vendor_img_path = AddVendor(output_zip)if has_oem:banner("oem")oem_img_path = AddOem(output_zip)if has_system_other:banner("system_other")AddSystemOther(output_zip)if not OPTIONS.is_signing:banner("userdata")AddUserdata(output_zip)banner("cache")AddCache(output_zip)if OPTIONS.info_dict.get("board_bpt_enable") == "true":banner("partition-table")AddPartitionTable(output_zip)dtbo_img_path = Noneif OPTIONS.info_dict.get("has_dtbo") == "true":banner("dtbo")dtbo_img_path = AddDtbo(output_zip)if OPTIONS.info_dict.get("avb_enable") == "true":banner("vbmeta")boot_contents = boot_image.WriteToTemp()AddVBMeta(output_zip, boot_contents.name, system_img_path,vendor_img_path, oem_img_path, dtbo_img_path)# For devices using A/B update, copy over images from RADIO/ and/or# VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed# images ready under IMAGES/. All images should have '.img' as extension.banner("radio")ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")if os.path.exists(ab_partitions):with open(ab_partitions, 'r') as f:lines = f.readlines()# For devices using A/B update, generate care_map for system and vendor# partitions (if present), then write this file to target_files package.care_map_list = []for line in lines:if line.strip() == "system" and ("system_verity_block_device" in OPTIONS.info_dict orOPTIONS.info_dict.get("avb_system_hashtree_enable") == "true"):assert os.path.exists(system_img_path)care_map_list += GetCareMap("system", system_img_path)if line.strip() == "vendor" and ("vendor_verity_block_device" in OPTIONS.info_dict orOPTIONS.info_dict.get("avb_vendor_hashtree_enable") == "true"):assert os.path.exists(vendor_img_path)care_map_list += GetCareMap("vendor", vendor_img_path)if line.strip() == "oem" and ("oem_verity_block_device" in OPTIONS.info_dict orOPTIONS.info_dict.get("avb_oem_hashtree_enable") == "true"):assert os.path.exists(oem_img_path)care_map_list += GetCareMap("oem", oem_img_path)img_name = line.strip() + ".img"prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)if os.path.exists(prebuilt_path):print("%s already exists, no need to overwrite..." % (img_name,))continueimg_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)img_vendor_dir = os.path.join(OPTIONS.input_tmp, "VENDOR_IMAGES")img_oem_dir = os.path.join(OPTIONS.input_tmp, "OEM_IMAGES")if os.path.exists(img_radio_path):if output_zip:common.ZipWrite(output_zip, img_radio_path,os.path.join("IMAGES", img_name))else:shutil.copy(img_radio_path, prebuilt_path)else:for root, _, files in os.walk(img_vendor_dir,img_oem_dir):if img_name in files:if output_zip:common.ZipWrite(output_zip, os.path.join(root, img_name),os.path.join("IMAGES", img_name))else:shutil.copy(os.path.join(root, img_name), prebuilt_path)breakif output_zip:# Zip spec says: All slashes MUST be forward slashes.img_path = 'IMAGES/' + img_nameassert img_path in output_zip.namelist(), "cannot find " + img_nameelse:img_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)assert os.path.exists(img_path), "cannot find " + img_nameif care_map_list:care_map_path = "META/care_map.txt"if output_zip and care_map_path not in output_zip.namelist():common.ZipWriteStr(output_zip, care_map_path, '\n'.join(care_map_list))else:with open(os.path.join(OPTIONS.input_tmp, care_map_path), 'w') as fp:fp.write('\n'.join(care_map_list))if output_zip:OPTIONS.replace_updated_files_list.append(care_map_path)# Radio images that need to be packed into IMAGES/, and product-img.zip.pack_radioimages = os.path.join(OPTIONS.input_tmp, "META", "pack_radioimages.txt")if os.path.exists(pack_radioimages):with open(pack_radioimages, 'r') as f:lines = f.readlines()for line in lines:img_name = line.strip()_, ext = os.path.splitext(img_name)if not ext:img_name += ".img"prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", img_name)if os.path.exists(prebuilt_path):print("%s already exists, no need to overwrite..." % (img_name,))continueimg_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)assert os.path.exists(img_radio_path), \"Failed to find %s at %s" % (img_name, img_radio_path)if output_zip:common.ZipWrite(output_zip, img_radio_path,os.path.join("IMAGES", img_name))else:shutil.copy(img_radio_path, prebuilt_path)if output_zip:common.ZipClose(output_zip)if OPTIONS.replace_updated_files_list:ReplaceUpdatedFiles(output_zip.filename,OPTIONS.replace_updated_files_list)
我們逐段介紹: (1)
if os.path.isdir(filename):OPTIONS.input_tmp = os.path.abspath(filename)input_zip = Noneelse:OPTIONS.input_tmp, input_zip = common.UnzipTemp(filename)
如果input_tmp目錄下有VENDOR目錄 or input_tmp目錄下有IMAGE/vendor.img文件,則has_vendor 為True; 如果input_tmp目錄下有OEM目錄 or input_tmp目錄下有IMAGE/oem.img文件,則has_oem 為True; 如果input_tmp目錄下有SYSTEM_OTHER,則has_system_other 為True