#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (c) Hisilicon Technologies Co., Ltd.. 2020-2022. All rights reserved. # Description: add the full ota or Incremental ota upgrade partitions # Author: Huanglong # Create: 2020-11-25 import common import os.path import sparse_img from blockimgdiff import EmptyImage # add for find the image exist or not def HasTheImage(target_files_zip, partition): try: target_files_zip.getinfo("IMAGES/" + partition) return True except KeyError: return False def HuanglongAddImageToZipOutputHasTheImage(info, image_name): if HasTheImage(info.input_zip, image_name): common.ZipWriteStr(info.output_zip, image_name, info.input_zip.read("IMAGES/" + image_name)) def HuanglongUpdateHasTheImage(info, mount_point, image_name): if HasTheImage(info.input_zip, image_name): info.script.Print("update " + mount_point + " ...") info.script.WriteRawImage("/" + mount_point, image_name) def HuanglongUpdateContainsBakHasTheImage(info, mount_point, bak_mount_point, image_name): if HasTheImage(info.input_zip, image_name): info.script.Print("update " + mount_point + " and " + bak_mount_point + " ...") info.script.WriteRawImage("/" + mount_point, image_name) info.script.WriteRawImage("/" + bak_mount_point, image_name) def GetCustomPartitionImages(input_tmp, zipfile): dp_dict = {"odm": "IMAGES/odm.img", "product": "IMAGES/product.img"} return {partition: sparse_img.SparseImage(os.path.join(input_tmp, path)) for (partition, path) in dp_dict.items()} def FullOTA_GetCustomBlockDifferences(info): images = GetCustomPartitionImages(info.input_tmp, info.input_zip) for partition, image in images.items(): info.script.Print("update %s ..." % (partition,)) image.ResetFileMap() image_diff = common.BlockDifference(partition, image) image_diff.WriteScript(info.script, info.output_zip) def IncrementalOTA_GetCustomBlockDifferences(info): source_images = GetCustomPartitionImages(info.source_tmp, info.source_zip) target_images = GetCustomPartitionImages(info.target_tmp, info.target_zip) for partition in source_images: target_images.setdefault(partition, EmptyImage()) for partition, target_image in target_images.items(): info.script.Print("update %s ..." % (partition,)) target_image.ResetFileMap() image_diff = common.BlockDifference(partition, target_image, source_images.get(partition)) image_diff.WriteScript(info.script, info.output_zip) def ZipWriteOutput(info): HuanglongAddImageToZipOutputHasTheImage(info, "fastboot.bin") HuanglongAddImageToZipOutputHasTheImage(info, "sbl.bin") HuanglongAddImageToZipOutputHasTheImage(info, "slaveboot.bin") HuanglongAddImageToZipOutputHasTheImage(info, "trustedcore.img") HuanglongAddImageToZipOutputHasTheImage(info, "deviceinfo.img") HuanglongAddImageToZipOutputHasTheImage(info, "panel.img") HuanglongAddImageToZipOutputHasTheImage(info, "vbmeta_system.img") HuanglongAddImageToZipOutputHasTheImage(info, "vbmeta_vendor.img") HuanglongAddImageToZipOutputHasTheImage(info, "bootargs.bin") HuanglongAddImageToZipOutputHasTheImage(info, "recovery.img") HuanglongAddImageToZipOutputHasTheImage(info, "dtbo.img") HuanglongAddImageToZipOutputHasTheImage(info, "baseparam.img") HuanglongAddImageToZipOutputHasTheImage(info, "logo.img") HuanglongAddImageToZipOutputHasTheImage(info, "preload.img") def HuanglongUpdateHasTheExt4Image(info, mount_point, image_name): if HasTheImage(info.input_zip, image_name): info.script.Print("update " + mount_point + " ...") info.script.AppendExtra('format("%s", "%s","%s", "%s", "%s");' % ("ext4", "EMMC", "/dev/block/by-name/" + mount_point, "0", "/"+mount_point)) info.script.AppendExtra('package_extract_file("%s", "%s");' % (image_name, "/tmp/"+mount_point+".ext4")) info.script.AppendExtra('write_ext4sp_img("%s", "%s");' % ("/tmp/"+mount_point+".ext4","/dev/block/by-name/"+mount_point)) info.script.AppendExtra('delete("%s");' % ("/tmp/"+mount_point+".ext4")) def FullOTA_InstallBegin(info): ZipWriteOutput(info) type, misc_block_device= common.GetTypeAndDevice("/misc", info.info_dict) info.script.AppendExtra(('if (get_stage("%s") == "" ) then' ) % (misc_block_device)) info.script.Print("update recovery... stage: 1/2......") info.script.Print("update recoverybak bootargsbak dtbobak......") info.script.AppendExtra(('if (get_recovery_state("%s") != "b" ) then' ) % (misc_block_device)) HuanglongUpdateHasTheImage(info, "recoverybak", "recovery.img") HuanglongUpdateHasTheImage(info, "bootargsbak", "bootargs.bin") HuanglongUpdateHasTheImage(info, "dtbobak", "dtbo.img") info.script.AppendExtra('set_recovery_state("%s", "%s");' % (misc_block_device, "b")) info.script.AppendExtra('endif;') info.script.Print("update recovery bootargs dtbo(not bak)......") HuanglongUpdateHasTheImage(info, "recovery", "recovery.img") HuanglongUpdateHasTheImage(info, "bootargs", "bootargs.bin") HuanglongUpdateHasTheImage(info, "dtbo", "dtbo.img") info.script.AppendExtra('set_recovery_state("%s", "%s");' % (misc_block_device, "a")) info.script.AppendExtra('set_stage("%s", "%s");' % (misc_block_device, "2/2")) info.script.AppendExtra('endif;') info.script.AppendExtra(('if (get_stage("%s") == "2/2" ) then' ) % (misc_block_device)) info.script.Print("update recovery...stage 2/2......"); HuanglongUpdateHasTheImage(info, "vbmeta_system", "vbmeta_system.img") HuanglongUpdateHasTheImage(info, "vbmeta_vendor", "vbmeta_vendor.img") HuanglongUpdateHasTheImage(info, "fastboot", "fastboot.bin") if(CheckPartition(info,"fastboot_sda")): HuanglongUpdateHasTheImage(info, "fastboot_sda", "fastboot.bin") HuanglongUpdateContainsBakHasTheImage(info, "sbl", "sblbak", "sbl.bin") HuanglongUpdateContainsBakHasTheImage(info, "slaveboot", "slavebootbak", "slaveboot.bin") HuanglongUpdateHasTheImage(info, "deviceinfo", "deviceinfo.img") HuanglongUpdateContainsBakHasTheImage(info, "trustedcore", "trustedcorebak", "trustedcore.img") HuanglongUpdateHasTheImage(info, "panel", "panel.img") HuanglongUpdateHasTheImage(info, "baseparam", "baseparam.img") HuanglongUpdateHasTheImage(info, "logo", "logo.img") if(CheckPartition(info, "preload")): HuanglongUpdateHasTheExt4Image(info, "preload", "preload.img") FullOTA_GetCustomBlockDifferences(info) info.script.AppendExtra('endif;') def CheckPartition(info,partition): try: type, partition_block_device= common.GetTypeAndDevice("/"+partition, info.info_dict) if(len(partition_block_device)==0): return False else: return True except KeyError: return False def FullOTA_InstallEnd(info): info.script.Print("FullOTA_InstallEnd01...") info.script.Print("FullOTA_InstallEnd02...") def GetSrcAndTagBootableImage(info, image_full_name): source = common.GetBootableImage( "/tmp/" + image_full_name, image_full_name, common.OPTIONS.source_tmp, "IMAGES", info.info_dict) target = common.GetBootableImage( "/tmp/" + image_full_name, image_full_name, common.OPTIONS.target_tmp, "IMAGES", info.info_dict) return (source, target) def CheckUpdatingImage(info, image, suffix): image_full_name = image + suffix source, target = GetSrcAndTagBootableImage(info, image_full_name) if source.data != target.data: type, device = common.GetTypeAndDevice( "/" + image, info.info_dict) d = common.Difference(target, source) _, _, d = d.ComputePatch() if d is None: info.script.Print("%s do not change, including to update" % (image,)) common.ZipWriteStr(info.output_zip, image_full_name, target.data) else: info.script.Print("%s target: %d source: %d diff: %d" % (image, target.size, source.size, len(d))) common.ZipWriteStr(info.output_zip, "patch/" + image_full_name + ".p", d) info.script.PatchPartitionCheck( "{}:{}:{}:{}".format( type, device, target.size, target.sha1), "{}:{}:{}:{}".format( type, device, source.size, source.sha1)) info.script.CacheFreeSpaceCheck(target.size) def IncrementalOTA_VerifyBegin(info): info.script.Print("IncrementalOTA_VerifyBegin...") def IncrementalOTA_VerifyEnd(info): info.script.Print("IncrementalOTA_VerifyEnd...") def IncrementalUpdatingImage(info, image, suffix): image_full_name = image + suffix source, target = GetSrcAndTagBootableImage(info, image_full_name) if source.data != target.data: type, device = common.GetTypeAndDevice( "/" + image, info.info_dict) d = common.Difference(target, source) _, _, d = d.ComputePatch() if d is None: info.script.Print("%s do not change, including full update" % (image,)) info.script.WriteRawImage(device, image_full_name) else: info.script.Print("%s image changed; including patch." % (image,)) info.script.PatchPartition( '{}:{}:{}:{}'.format( type, device, target.size, target.sha1), '{}:{}:{}:{}'.format( type, device, source.size, source.sha1), "patch/" + image_full_name + ".p") def HuanglongAddFullImage(info, device, image, suffix): image_full_name = image + suffix source, target = GetSrcAndTagBootableImage(info, image_full_name) common.ZipWriteStr(info.output_zip, image_full_name, target.data) info.script.WriteRawImage(device, image_full_name) def HuanglongAddFullImageToZip(info, image_full_name): if HasTheImage(info.target_zip, image_full_name): source, target = GetSrcAndTagBootableImage(info, image_full_name) common.ZipWriteStr(info.output_zip, image_full_name, target.data) def HuanglongUpdateFullImage(info, mount_point, image_name): if HasTheImage(info.target_zip, image_name): info.script.Print("update " + mount_point + " ...") info.script.WriteRawImage("/" + mount_point, image_name) def HuanglongUpdateFullImageAndBak(info, mount_point, bak_mount_point, image_name): if HasTheImage(info.target_zip, image_name): info.script.Print("update " + mount_point + " and " + bak_mount_point + " ...") info.script.WriteRawImage("/" + mount_point, image_name) info.script.WriteRawImage("/" + bak_mount_point, image_name) def IncrementalOTA_InstallBegin(info): HuanglongAddFullImageToZip(info, "recovery.img") HuanglongAddFullImageToZip(info, "bootargs.bin") HuanglongAddFullImageToZip(info, "dtbo.img") HuanglongAddFullImageToZip(info, "vbmeta_system.img") HuanglongAddFullImageToZip(info, "vbmeta_vendor.img") HuanglongAddFullImageToZip(info, "fastboot.bin") HuanglongAddFullImageToZip(info, "sbl.bin") HuanglongAddFullImageToZip(info, "slaveboot.bin") HuanglongAddFullImageToZip(info, "deviceinfo.img") HuanglongAddFullImageToZip(info, "trustedcore.img") HuanglongAddFullImageToZip(info, "panel.img") HuanglongAddFullImageToZip(info, "baseparam.img") HuanglongAddFullImageToZip(info, "logo.img") type, misc_block_device= common.GetTypeAndDevice("/misc", info.info_dict) info.script.AppendExtra(('if (get_stage("%s") == "" ) then' ) % (misc_block_device)) info.script.Print("update recovery... stage: 1/2......") info.script.Print("update recoverybak and bootargsbak and dtbobak......") info.script.AppendExtra(('if (get_recovery_state("%s") != "b" ) then' ) % (misc_block_device)) HuanglongUpdateFullImage(info, "recoverybak", "recovery.img") HuanglongUpdateFullImage(info, "bootargsbak", "bootargs.bin") HuanglongUpdateFullImage(info, "dtbobak", "dtbo.img") info.script.AppendExtra('set_recovery_state("%s", "%s");' % (misc_block_device, "b")) info.script.AppendExtra('endif;') info.script.Print("update recovery and bootargs and dtbo(not bak)......") HuanglongUpdateFullImage(info, "recovery", "recovery.img") HuanglongUpdateFullImage(info, "bootargs", "bootargs.bin") HuanglongUpdateFullImage(info, "dtbo", "dtbo.img") info.script.AppendExtra('set_recovery_state("%s", "%s");' % (misc_block_device, "a")) info.script.AppendExtra('set_stage("%s", "%s");' % (misc_block_device, "2/2")) info.script.AppendExtra('endif;') info.script.AppendExtra(('if (get_stage("%s") == "2/2" ) then' ) % (misc_block_device)) info.script.Print("update recovery...stage 2/2......") HuanglongUpdateFullImage(info, "vbmeta_system", "vbmeta_system.img") HuanglongUpdateFullImage(info, "vbmeta_vendor", "vbmeta_vendor.img") HuanglongUpdateFullImage(info, "fastboot", "fastboot.bin") if(CheckPartition(info,"fastboot_sda")): HuanglongUpdateHasTheImage(info, "fastboot_sda", "fastboot.bin") HuanglongUpdateFullImageAndBak(info, "sbl", "sblbak", "sbl.bin") HuanglongUpdateFullImageAndBak(info, "slaveboot", "slavebootbak", "slaveboot.bin") HuanglongUpdateFullImage(info, "deviceinfo", "deviceinfo.img") HuanglongUpdateFullImageAndBak(info, "trustedcore", "trustedcorebak", "trustedcore.img") HuanglongUpdateFullImage(info, "panel", "panel.img") HuanglongUpdateFullImage(info, "baseparam", "baseparam.img") HuanglongUpdateFullImage(info, "logo", "logo.img") IncrementalOTA_GetCustomBlockDifferences(info) info.script.AppendExtra('endif;') def IncrementalOTA_InstallEnd(info): info.script.Print("IncrementalOTA_InstallEnd...")