我们在这里主要分析OTA 升级过程中对ota_from_target_files.py脚本的调用,这个脚本主的作用是生成各种OTA完整包和增量包。

 

以生成完整OTA包为例,假设我们的命令为:./build/tools/releasetools/ota_from_target_files -n  input_file_dir/target_file.zip  output_file_dir/output.zip 

从main函数开始,

将用户设定的option选项存入OPTIONS变量中,然后再判断有没有额外的脚本,如果有就读入到OPTIONS变量中。

2 解压缩输入的zip包,即/target_files.zip ,然后判断OPTIONS变量中的device_specific是否为空,如果为空就从解压的META/releasetools.py读入到device_specific中。

3 判断是否签名,然后判断是否有新内容的增量源,有的话就解压该增量源包放入一个临时变量source_zip。

4 调用WriteFullOTAPackage函数,获得metadata元数据,读取解压的临时文件中的几个重要的配置文件,如META/misc_info.txt,SYSTEM/build.prop等。

生成升级用的脚本文件updater-script,将上一步获得的metadata元数据写入到输出包output.zip


WriteFullOTAPackage函数的具体分析: 


1 首先,我们获得脚本生成器,


script            =            edify_generator.EdifyGenerator(           3           , OPTIONS.info_dict)


2  获得一些环境变量


metadata            =            {          


                      "post-build"           : CalculateFingerprint(oem_props, oem_dict,            


                      OPTIONS.info_dict),          


                      "pre-device"           : GetOemProperty(           "ro.product.device"           , oem_props, oem_dict,          


                      OPTIONS.info_dict),          


                      "post-timestamp"           : GetBuildProp(           "ro.build.date.utc"           , OPTIONS.info_dict),          


           }


Assert语句,检测目标设备的 ro.product.device 是否跟update.zip中的相同


AppendAssertions(script, input_zip)


4 这是一个comm.py中定义的回调函数,用于调用设备相关代码


device_specific.FullOTA_Assertions()

5 在升级脚本中加入显示进度的语句


script.ShowProgress(           0.5           ,            0           ) 


因为在edify_gnerator.py中有


def            ShowProgress(           self           , frac, dur):          


                      """Update the progress bar, advancing it over 'frac' over the next          


                      'dur' seconds. 'dur' may be zero to advance it via SetProgress          


                      commands instead of by time."""          


                      self           .script.append(           "show_progress(%f, %d);"            %            (frac,            int           (dur)))


 如果需要,在脚本中增加语句,擦除 userdata 分区等。


if            OPTIONS.wipe_user_data:          


                      script.FormatPartition(           "userdata"           )