Android系统Recovery工作原理3-- 升级包的制作

 ㈡ 下面我们分析ota_from_target_files这个python脚本是怎样生成最终zip包的。先讲这个脚本的代码贴出来如下:

  1. import sys  
  2.   
  3. if sys.hexversion 0x02040000 
  4.   print >> sys.stderr, "Python 2.4 or newer is required."  
  5.   sys.exit(1 
  6.   
  7. import copy  
  8. import errno  
  9. import os  
  10. import re  
  11. import sha  
  12. import subprocess  
  13. import tempfile  
  14. import time  
  15. import zipfile  
  16.   
  17. import common  
  18. import edify_generator  
  19.   
  20. OPTIONS common.OPTIONS  
  21. OPTIONS.package_key "build/target/product/security/testkey"  
  22. OPTIONS.incremental_source None  
  23. OPTIONS.require_verbatim set()  
  24. OPTIONS.prohibit_verbatim set(("system/build.prop",))  
  25. OPTIONS.patch_threshold 0.95  
  26. OPTIONS.wipe_user_data False  
  27. OPTIONS.omit_prereq False  
  28. OPTIONS.extra_script None  
  29. OPTIONS.worker_threads 3  
  30.   
  31. def MostPopularKey(d, default):  
  32.   """Given dict, return the key corresponding to the largest 
  33.   value.  Returns 'default' if the dict is empty."""  
  34.   [(v, k) for (k, v) in d.iteritems()]  
  35.   if not x: return default  
  36.   x.sort()  
  37.   return x[-1][1 
  38.   
  39.   
  40. def IsSymlink(info):  
  41.   """Return true if the zipfile.ZipInfo object passed in represents a 
  42.   symlink."""  
  43.   return (info.external_attr >> 16== 0120777  
  44.   
  45.   
  46. class Item:  
  47.   """Items represent the metadata (user, group, mode) of files and 
  48.   directories in the system image."""  
  49.   ITEMS {}  
  50.   def __init__(selfname, dir=False):  
  51.     self.name name  
  52.     self.uid None  
  53.     self.gid None  
  54.     self.mode None  
  55.     self.dir dir  
  56.   
  57.     if name:  
  58.       self.parent Item.Get(os.path.dirname(name), dir=True 
  59.       self.parent.children.append(self 
  60.     else 
  61.       self.parent None  
  62.     if dir:  
  63.       self.children []  
  64.   
  65.   def Dump(selfindent=0):  
  66.     if self.uid is not None 
  67.       print "%s%s %d %d %o" ( "*indent, self.name, self.uid, self.gid, self.mode)  
  68.     else 
  69.       print "%s%s %s %s %s" ( "*indent, self.name, self.uid, self.gid, self.mode)  
  70.     if self.dir:  
  71.       print "%s%s" ( "*indent, self.descendants)  
  72.       print "%s%s" ( "*indent, self.best_subtree)  
  73.       for in self.children:  
  74.         i.Dump(indent=indent+1 
  75.  
  76.   @classmethod  
  77.   def Get(clsname, dir=False):  
  78.     if name not in cls.ITEMS:  
  79.       cls.ITEMS[name] Item(name, dir=dir)  
  80.     return cls.ITEMS[name]  
  81.  
  82.   @classmethod  
  83.   def GetMetadata(clsinput_zip):  
  84.   
  85.     try 
  86.       See if the target_files contains record of what the uid,  
  87.       gid, and mode is supposed to be.  
  88.       output input_zip.read("META/filesystem_config.txt" 
  89.     except KeyError:  
  90.       Run the external 'fs_config' program to determine the desired  
  91.       uid, gid, and mode for every Item object.  Note this uses the  
  92.       one in the client now, which might not be the same as the one  
  93.       used when this target_files was built.  
  94.       common.Run(["fs_config"], stdin=subprocess.PIPE,  
  95.                      stdout=subprocess.PIPE, stderr=subprocess.PIPE)  
  96.       suffix False"", True: "/"  
  97.       input "".join(["%s%s\n" (i.name, suffix[i.dir])  
  98.                        for in cls.ITEMS.itervalues() if i.name])  
  99.       output, error p.communicate(input)  
  100.       assert not error  
  101.   
  102.     for line in output.split("\n"):  
  103.       if not line: continue  
  104.       name, uid, gid, mode line.split()  
  105.       cls.ITEMS.get(name, None 
  106.       if is not None 
  107.         i.uid int(uid)  
  108.         i.gid int(gid)  
  109.         i.mode int(mode, 8 
  110.         if i.dir:  
  111.           i.children.sort(key=lambda i: i.name)  
  112.   
  113.     set metadata for the files generated by this script.  
  114.     cls.ITEMS.get("system/recovery-from-boot.p"None 
  115.     if i: i.uid, i.gid, i.mode 000644  
  116.     cls.ITEMS.get("system/etc/install-recovery.sh"None 
  117.     if i: i.uid, i.gid, i.mode 000544  
  118.   
  119.   def CountChildMetadata(self):  
  120.     """Count up the (uid, gid, mode) tuples for all children and 
  121.     determine the best strategy for using set_perm_recursive and 
  122.     set_perm to correctly chown/chmod all the files to their desired 
  123.     values.  Recursively calls itself for all descendants. 
  124.  
  125.     Returns dict of {(uid, gid, dmode, fmode): count} counting up 
  126.     all descendants of this node.  (dmode or fmode may be None.)  Also 
  127.     sets the best_subtree of each directory Item to the (uid, gid, 
  128.     dmode, fmode) tuple that will match the most descendants of that 
  129.     Item. 
  130.     """  
  131.   
  132.     assert self.dir  
  133.     self.descendants {(self.uid, self.gid, self.mode, None): 1 
  134.     for in self.children:  
  135.       if i.dir:  
  136.         for k, in i.CountChildMetadata().iteritems():  
  137.           d[k] d.get(k, 0 
  138.       else 
  139.         (i.uid, i.gid, Nonei.mode)  
  140.         d[k] d.get(k, 01  
  141.   
  142.     Find the (uid, gid, dmode, fmode) tuple that matches the most  
  143.     descendants.  
  144.   
  145.     First, find the (uid, gid) pair that matches the most  
  146.     descendants.  
  147.     ug {}  
  148.     for (uid, gid, _, _), count in d.iteritems():  
  149.       ug[(uid, gid)] ug.get((uid, gid), 0count  
  150.     ug MostPopularKey(ug, (00))  
  151.   
  152.     Now find the dmode and fmode that match the most descendants  
  153.     with that (uid, gid), and choose those.  
  154.     best_dmode (00755 
  155.     best_fmode (00644 
  156.     for k, count in d.iteritems():  
  157.       if k[:2!= ug: continue  
  158.       if k[2is not None and count >= best_dmode[0]: best_dmode (count, k[2])  
  159.       if k[3is not None and count >= best_fmode[0]: best_fmode (count, k[3])  
  160.     self.best_subtree ug (best_dmode[1], best_fmode[1])  
  161.   
  162.     return  
  163.   
  164.   def SetPermissions(selfscript):  
  165.     """Append set_perm/set_perm_recursive commands to 'script' to 
  166.     set all permissions, users, and groups for the tree of files 
  167.     rooted at 'self'."""  
  168.   
  169.     self.CountChildMetadata()  
  170.   
  171.     def recurse(item, current):  
  172.       current is the (uid, gid, dmode, fmode) tuple that the current  
  173.       item (and all its children) have already been set to.  We only  
  174.       need to issue set_perm/set_perm_recursive commands if we're  
  175.       supposed to be something different.  
  176.       if item.dir:  
  177.         if current != item.best_subtree:  
  178.           script.SetPermissionsRecursive("/"+item.name, *item.best_subtree)  
  179.           current item.best_subtree  
  180.   
  181.         if item.uid != current[0or item.gid != current[1or  
  182.            item.mode != current[2]:  
  183.           script.SetPermissions("/"+item.name, item.uid, item.gid, item.mode)  
  184.   
  185.         for in item.children:  
  186.           recurse(i, current)  
  187.       else 
  188.         if item.uid != current[0or item.gid != current[1or  
  189.                item.mode != current[3]:  
  190.           script.SetPermissions("/"+item.name, item.uid, item.gid, item.mode)  
  191.   
  192.     recurse(self(-1-1-1-1))  
  193.   
  194.   
  195. def CopySystemFiles(input_zip, output_zip=None 
  196.                     substitute=None):  
  197.   """Copies files underneath system/ in the input zip to the output 
  198.   zip.  Populates the Item class with their metadata, and returns a 
  199.   list of symlinks.  output_zip may be None, in which case the copy is 
  200.   skipped (but the other side effects still happen).  substitute is an 
  201.   optional dict of {output filename: contents} to be output instead of 
  202.   certain input files. 
  203.   """  
  204.   
  205.   symlinks []  
  206.   
  207.   for info in input_zip.infolist():  
  208.     if info.filename.startswith("SYSTEM/"):  
  209.       basefilename info.filename[7:]  
  210.       if IsSymlink(info):  
  211.         symlinks.append((input_zip.read(info.filename),  
  212.                          "/system/" basefilename))  
  213.       else 
  214.         info2 copy.copy(info)  
  215.         fn info2.filename "system/" basefilename  
  216.         if substitute and fn in substitute and substitute[fn] is None 
  217.           continue  
  218.         if output_zip is not None 
  219.           if substitute and fn in substitute:  
  220.             data substitute[fn]  
  221.           else 
  222.             data input_zip.read(info.filename)  
  223.           output_zip.writestr(info2, data)  
  224.         if fn.endswith("/"):  
  225.           Item.Get(fn[:-1], dir=True 
  226.         else 
  227.           Item.Get(fn, dir=False 
  228.   
  229.   symlinks.sort()  
  230.   return symlinks  
  231.   
  232.   
  233. def SignOutput(temp_zip_name, output_zip_name):  
  234.   key_passwords common.GetKeyPasswords([OPTIONS.package_key])  
  235.   pw key_passwords[OPTIONS.package_key]  
  236.   
  237.   common.SignFile(temp_zip_name, output_zip_name, OPTIONS.package_key, pw,  
  238.                   whole_file=True 
  239.   
  240.   
  241. def AppendAssertions(script, input_zip):  
  242.   device GetBuildProp("ro.product.device"input_zip)  
  243.   script.AssertDevice(device)  
  244.   
  245.   
  246. def MakeRecoveryPatch(output_zip, recovery_img, boot_img):  
  247.   """Generate binary patch that creates the recovery image starting 
  248.   with the boot image.  (Most of the space in these images is just the 
  249.   kernel, which is identical for the two, so the resulting patch 
  250.   should be efficient.)  Add it to the output zip, along with shell 
  251.   script that is run from init.rc on first boot to actually do the 
  252.   patching and install the new recovery image. 
  253.  
  254.   recovery_img and boot_img should be File objects for the 
  255.   corresponding images. 
  256.  
  257.   Returns an Item for the shell script, which must be made 
  258.   executable. 
  259.   """  
  260.   
  261.   common.Difference(recovery_img, boot_img)  
  262.   _, _, patch d.ComputePatch()  
  263.   common.ZipWriteStr(output_zip, "recovery/recovery-from-boot.p"patch)  
  264.   Item.Get("system/recovery-from-boot.p"dir=False 
  265.   
  266.   boot_type, boot_device common.GetTypeAndDevice("/boot"OPTIONS.info_dict)  
  267.   recovery_type, recovery_device common.GetTypeAndDevice("/recovery"OPTIONS.info_dict)  
  268.   
  269.   Images with different content will have different first page, so  
  270.   we check to see if this recovery has already been installed by  
  271.   testing just the first 2k.  
  272.   HEADER_SIZE 2048  
  273.   header_sha1 sha.sha(recovery_img.data[:HEADER_SIZE]).hexdigest()  
  274.   sh """#!/system/bin/sh 
  275. if applypatch -c %(recovery_type)s:%(recovery_device)s:%(header_size)d:%(header_sha1)s; then 
  276.   log -t recovery "Installing new recovery image" 
  277.   applypatch %(boot_type)s:%(boot_device)s:%(boot_size)d:%(boot_sha1)s %(recovery_type)s:%(recovery_device)s %(recovery_sha1)s %(recovery_size)d %(boot_sha1)s:/system/recovery-from-boot.p 
  278. else 
  279.   log -t recovery "Recovery image already installed" 
  280. fi 
  281. """ 'boot_size'boot_img.size,  
  282.         'boot_sha1'boot_img.sha1,  
  283.         'header_size'HEADER_SIZE,  
  284.         'header_sha1'header_sha1,  
  285.         'recovery_size'recovery_img.size,  
  286.         'recovery_sha1'recovery_img.sha1,  
  287.         'boot_type'boot_type,  
  288.         'boot_device'boot_device,  
  289.         'recovery_type'recovery_type,  
  290.         'recovery_device'recovery_device,  
  291.          
  292.   common.ZipWriteStr(output_zip, "recovery/etc/install-recovery.sh"sh)  
  293.   return Item.Get("system/etc/install-recovery.sh"dir=False 
  294.   
  295.   
  296. def WriteFullOTAPackage(input_zip, output_zip):  
  297.   TODO: how to determine this?  We don't know what version it will  
  298.   be installed on top of.  For now, we expect the API just won't  
  299.   change very often.  
  300.   script edify_generator.EdifyGenerator(3OPTIONS.info_dict)  
  301.   
  302.   metadata {"post-build"GetBuildProp("ro.build.fingerprint"input_zip),  
  303.               "pre-device"GetBuildProp("ro.product.device"input_zip),  
  304.               "post-timestamp"GetBuildProp("ro.build.date.utc"input_zip),  
  305.                
  306.   
  307.   device_specific common.DeviceSpecificParams(  
  308.       input_zip=input_zip,  
  309.       input_version=OPTIONS.info_dict["recovery_api_version"],  
  310.       output_zip=output_zip,  
  311.       script=script,  
  312.       input_tmp=OPTIONS.input_tmp,  
  313.       metadata=metadata,  
  314.       info_dict=OPTIONS.info_dict)  
  315.   
  316.   if not OPTIONS.omit_prereq:  
  317.     ts GetBuildProp("ro.build.date.utc"input_zip)  
  318.     script.AssertOlderBuild(ts)  
  319.   
  320.   AppendAssertions(script, input_zip)  
  321.   device_specific.FullOTA_Assertions()  
  322.   
  323.   script.ShowProgress(0.50 
  324.   
  325.   if OPTIONS.wipe_user_data:  
  326.     script.FormatPartition("/data" 
  327.   
  328.   script.FormatPartition("/system" 
  329.   script.Mount("/system" 
  330.   script.UnpackPackageDir("recovery""/system" 
  331.   script.UnpackPackageDir("system""/system" 
  332.   
  333.   symlinks CopySystemFiles(input_zip, output_zip)  
  334.   script.MakeSymlinks(symlinks)  
  335.   
  336.   boot_img common.File("boot.img"common.BuildBootableImage(  
  337.       os.path.join(OPTIONS.input_tmp, "BOOT")))  
  338.   recovery_img common.File("recovery.img"common.BuildBootableImage(  
  339.       os.path.join(OPTIONS.input_tmp, "RECOVERY")))  
  340.   MakeRecoveryPatch(output_zip, recovery_img, boot_img)  
  341.   
  342.   Item.GetMetadata(input_zip)  
  343.   Item.Get("system").SetPermissions(script)  
  344.   
  345.   common.CheckSize(boot_img.data, "boot.img"OPTIONS.info_dict)  
  346.   common.ZipWriteStr(output_zip, "boot.img"boot_img.data)  
  347.   script.ShowProgress(0.20 
  348.   
  349.   script.ShowProgress(0.210 
  350.   script.WriteRawImage("/boot""boot.img" 
  351.   
  352.   script.ShowProgress(0.10 
  353.   device_specific.FullOTA_InstallEnd()  
  354.   
  355.   if OPTIONS.extra_script is not None 
  356.     script.AppendExtra(OPTIONS.extra_script)  
  357.   
  358.   script.UnmountAll()  
  359.   script.AddToZip(input_zip, output_zip)  
  360.   WriteMetadata(metadata, output_zip)  
  361.   
  362.   
  363. def WriteMetadata(metadata, output_zip):  
  364.   common.ZipWriteStr(output_zip, "META-INF/com/android/metadata" 
  365.                      "".join(["%s=%s\n" kv  
  366.                               for kv in sorted(metadata.iteritems())]))  
  367.   
  368.   
  369.   
  370.   
  371. def LoadSystemFiles(z):  
  372.   """Load all the files from SYSTEM/... in given target-files 
  373.   ZipFile, and return dict of {filename: File object}."""  
  374.   out {}  
  375.   for info in z.infolist():  
  376.     if info.filename.startswith("SYSTEM/"and not IsSymlink(info):  
  377.       fn "system/" info.filename[7:]  
  378.       data z.read(info.filename)  
  379.       out[fn] common.File(fn, data)  
  380.   return out  
  381.   
  382.   
  383. def GetBuildProp(property, z):  
  384.   """Return the fingerprint of the build of given target-files 
  385.   ZipFile object."""  
  386.   bp z.read("SYSTEM/build.prop" 
  387.   if not property:  
  388.     return bp  
  389.   re.search(re.escape(property) r"=(.*)\n"bp)  
  390.   if not m:  
  391.     raise common.ExternalError("couldn't find %s in build.prop" (property,))  
  392.   return m.group(1).strip()  
  393.   
  394.   
  395. def WriteIncrementalOTAPackage(target_zip, source_zip, output_zip):  
  396.   source_version OPTIONS.source_info_dict["recovery_api_version" 
  397.   target_version OPTIONS.target_info_dict["recovery_api_version" 
  398.   
  399.   if source_version == 0 
  400.     print ("WARNING: generating edify script for source that "  
  401.            "can't install it." 
  402.   script edify_generator.EdifyGenerator(source_version, OPTIONS.info_dict)  
  403.   
  404.   metadata {"pre-device"GetBuildProp("ro.product.device"source_zip),  
  405.               "post-timestamp"GetBuildProp("ro.build.date.utc"target_zip),  
  406.                
  407.   
  408.   device_specific common.DeviceSpecificParams(  
  409.       source_zip=source_zip,  
  410.       source_version=source_version,  
  411.       target_zip=target_zip,  
  412.       target_version=target_version,  
  413.       output_zip=output_zip,  
  414.       script=script,  
  415.       metadata=metadata,  
  416.       info_dict=OPTIONS.info_dict)  
  417.   
  418.   print "Loading target..."  
  419.   target_data LoadSystemFiles(target_zip)  
  420.   print "Loading source..."  
  421.   source_data LoadSystemFiles(source_zip)  
  422.   
  423.   verbatim_targets []  
  424.   patch_list []  
  425.   diffs []  
  426.   largest_source_size 0  
  427.   for fn in sorted(target_data.keys()):  
  428.     tf target_data[fn]  
  429.     assert fn == tf.name  
  430.     sf source_data.get(fn, None 
  431.   
  432.     if sf is None or fn in OPTIONS.require_verbatim:  
  433.       This file should be included verbatim  
  434.       if fn in OPTIONS.prohibit_verbatim:  
  435.         raise common.ExternalError(""%s" must be sent verbatim" (fn,))  
  436.       print "send"fn, "verbatim"  
  437.       tf.AddToZip(output_zip)  
  438.       verbatim_targets.append((fn, tf.size))  
  439.     elif tf.sha1 != sf.sha1:  
  440.       File is different; consider sending as patch  
  441.       diffs.append(common.Difference(tf, sf))  
  442.     else 
  443.       Target file identical to source.  
  444.       pass  
  445.   
  446.   common.ComputeDifferences(diffs)  
  447.   
  448.   for diff in diffs:  
  449.     tf, sf, diff.GetPatch()  
  450.     if is None or len(d) tf.size OPTIONS.patch_threshold:  
  451.       patch is almost as big as the file; don't bother patching  
  452.       tf.AddToZip(output_zip)  
  453.       verbatim_targets.append((tf.name, tf.size))  
  454.     else 
  455.       common.ZipWriteStr(output_zip, "patch/" tf.name ".p"d)  
  456.       patch_list.append((tf.name, tf, sf, tf.size, sha.sha(d).hexdigest()))  
  457.       largest_source_size max(largest_source_size, sf.size)  
  458.   
  459.   source_fp GetBuildProp("ro.build.fingerprint"source_zip)  
  460.   target_fp GetBuildProp("ro.build.fingerprint"target_zip)  
  461.   metadata["pre-build"source_fp  
  462.   metadata["post-build"target_fp  
  463.   
  464.   script.Mount("/system" 
  465.   script.AssertSomeFingerprint(source_fp, target_fp)  
  466.   
  467.   source_boot common.File("/tmp/boot.img" 
  468.                             common.BuildBootableImage(  
  469.                                 os.path.join(OPTIONS.source_tmp, "BOOT")))  
  470.   target_boot common.File("/tmp/boot.img" 
  471.                             common.BuildBootableImage(  
  472.                                 os.path.join(OPTIONS.target_tmp, "BOOT")))  
  473.   updating_boot (source_boot.data != target_boot.data)  
  474.   
  475.   source_recovery common.File("system/recovery.img" 
  476.                                 common.BuildBootableImage(  
  477.                                     os.path.join(OPTIONS.source_tmp, "RECOVERY")))  
  478.   target_recovery common.File("system/recovery.img" 
  479.                                 common.BuildBootableImage(  
  480.                                     os.path.join(OPTIONS.target_tmp, "RECOVERY")))  
  481.   updating_recovery (source_recovery.data != target_recovery.data)  
  482.   
  483.   Here's how we divide up the progress bar:  
  484.    0.1 for verifying the start state (PatchCheck calls)  
  485.    0.8 for applying patches (ApplyPatch calls)  
  486.    0.1 for unpacking verbatim files, symlinking, and doing the  
  487.        device-specific commands.  
  488.   
  489.   AppendAssertions(script, target_zip)  
  490.   device_specific.IncrementalOTA_Assertions()  
  491.   
  492.   script.Print("Verifying current system..." 
  493.   
  494.   script.ShowProgress(0.10 
  495.   total_verify_size float(sum([i[2].size for in patch_list]) 1 
  496.   if updating_boot:  
  497.     total_verify_size += source_boot.size  
  498.   so_far 0  
  499.   
  500.   for fn, tf, sf, size, patch_sha in patch_list:  
  501.     script.PatchCheck("/"+fn, tf.sha1, sf.sha1)  
  502.     so_far += sf.size  
  503.     script.SetProgress(so_far total_verify_size)  
  504.   
  505.   if updating_boot:  
  506.     common.Difference(target_boot, source_boot)  
  507.     _, _, d.ComputePatch()  
  508.     print "boot      target: %d  source: %d  diff: %d"  
  509.         target_boot.size, source_boot.size, len(d))  
  510.   
  511.     common.ZipWriteStr(output_zip, "patch/boot.img.p"d)  
  512.   
  513.     boot_type, boot_device common.GetTypeAndDevice("/boot"OPTIONS.info_dict)  
  514.   
  515.     script.PatchCheck("%s:%s:%d:%s:%d:%s"  
  516.                       (boot_type, boot_device,  
  517.                        source_boot.size, source_boot.sha1,  
  518.                        target_boot.size, target_boot.sha1))  
  519.     so_far += source_boot.size  
  520.     script.SetProgress(so_far total_verify_size)  
  521.   
  522.   if patch_list or updating_recovery or updating_boot:  
  523.     script.CacheFreeSpaceCheck(largest_source_size)  
  524.   
  525.   device_specific.IncrementalOTA_VerifyEnd()  
  526.   
  527.   script.Comment("---- start making changes here ----" 
  528.   
  529.   if OPTIONS.wipe_user_data:  
  530.     script.Print("Erasing user data..." 
  531.     script.FormatPartition("/data" 
  532.   
  533.   script.Print("Removing unneeded files..." 
  534.   script.DeleteFiles(["/"+i[0for in verbatim_targets]  
  535.                      ["/"+i for in sorted(source_data)  
  536.                             if not in target_data]  
  537.                      ["/system/recovery.img"])  
  538.   
  539.   script.ShowProgress(0.80 
  540.   total_patch_size float(sum([i[1].size for in patch_list]) 1 
  541.   if updating_boot:  
  542.     total_patch_size += target_boot.size  
  543.   so_far 0  
  544.   
  545.   script.Print("Patching system files..." 
  546.   for fn, tf, sf, size, in patch_list:  
  547.     script.ApplyPatch("/"+fn, "-"tf.size, tf.sha1, sf.sha1, "patch/"+fn+".p" 
  548.     so_far += tf.size  
  549.     script.SetProgress(so_far total_patch_size)  
  550.   
  551.   if updating_boot:  
  552.     Produce the boot image by applying patch to the current  
  553.     contents of the boot partition, and write it back to the  
  554.     partition.  
  555.     script.Print("Patching boot image..." 
  556.     script.ApplyPatch("%s:%s:%d:%s:%d:%s"  
  557.                       (boot_type, boot_device,  
  558.                          source_boot.size, source_boot.sha1,  
  559.                          target_boot.size, target_boot.sha1),  
  560.                       "-" 
  561.                       target_boot.size, target_boot.sha1,  
  562.                       source_boot.sha1, "patch/boot.img.p" 
  563.     so_far += target_boot.size  
  564.     script.SetProgress(so_far total_patch_size)  
  565.     print "boot image changed; including."  
  566.   else 
  567.     print "boot image unchanged; skipping."  
  568.   
  569.   if updating_recovery:  
  570.     Is it better to generate recovery as patch from the current  
  571.     boot image, or from the previous recovery image?  For large  
  572.     updates with significant kernel changes, probably the former.  
  573.     For small updates where the kernel hasn't changed, almost  
  574.     certainly the latter.  We pick the first option.  Future  
  575.     complicated schemes may let us effectively use both.  
  576.     #  
  577.     wacky possibility: as long as there is room in the boot  
  578.     partition, include the binaries and image files from recovery in  
  579.     the boot image (though not in the ramdisk) so they can be used  
  580.     as fodder for constructing the recovery image.  
  581.     MakeRecoveryPatch(output_zip, target_recovery, target_boot)  
  582.     script.DeleteFiles(["/system/recovery-from-boot.p" 
  583.                         "/system/etc/install-recovery.sh"])  
  584.     print "recovery image changed; including as patch from boot."  
  585.   else 
  586.     print "recovery image unchanged; skipping."  
  587.   
  588.   script.ShowProgress(0.110 
  589.   
  590.   target_symlinks CopySystemFiles(target_zip, None 
  591.   
  592.   target_symlinks_d dict([(i[1], i[0]) for in target_symlinks])  
  593.   temp_script script.MakeTemporary()  
  594.   Item.GetMetadata(target_zip)  
  595.   Item.Get("system").SetPermissions(temp_script)  
  596.   
  597.   Note that this call will mess up the tree of Items, so make sure  
  598.   we're done with it.  
  599.   source_symlinks CopySystemFiles(source_zip, None 
  600.   source_symlinks_d dict([(i[1], i[0]) for in source_symlinks])  
  601.   
  602.   Delete all the symlinks in source that aren't in target.  This  
  603.   needs to happen before verbatim files are unpacked, in case a  
  604.   symlink in the source is replaced by real file in the target.  
  605.   to_delete []  
  606.   for dest, link in source_symlinks:  
  607.     if link not in target_symlinks_d:  
  608.       to_delete.append(link)  
  609.   script.DeleteFiles(to_delete)  
  610.   
  611.   if verbatim_targets:  
  612.     script.Print("Unpacking new files..." 
  613.     script.UnpackPackageDir("system""/system" 
  614.   
  615.   if updating_recovery:  
  616.     script.Print("Unpacking new recovery..." 
  617.     script.UnpackPackageDir("recovery""/system" 
  618.   
  619.   script.Print("Symlinks and permissions..." 
  620.   
  621.   Create all the symlinks that don't already exist, or point to  
  622.   somewhere different than what we want.  Delete each symlink before  
  623.   creating it, since the 'symlink' command won't overwrite.  
  624.   to_create []  
  625.   for dest, link in target_symlinks:  
  626.     if link in source_symlinks_d:  
  627.       if dest != source_symlinks_d[link]:  
  628.         to_create.append((dest, link))  
  629.     else 
  630.       to_create.append((dest, link))  
  631.   script.DeleteFiles([i[1for in to_create])  
  632.   script.MakeSymlinks(to_create)  
  633.   
  634.   Now that the symlinks are created, we can set all the  
  635.   permissions.  
  636.   script.AppendScript(temp_script)  
  637.   
  638.   Do device-specific installation (eg, write radio image).  
  639.   device_specific.IncrementalOTA_InstallEnd()  
  640.   
  641.   if OPTIONS.extra_script is not None 
  642.     scirpt.AppendExtra(OPTIONS.extra_script)  
  643.   
  644.   script.AddToZip(target_zip, output_zip)  
  645.   WriteMetadata(metadata, output_zip)  
  646.   
  647.   
  648. def main(argv):  
  649.   
  650.   def option_handler(o, a):  
  651.     if in ("-b""--board_config"):  
  652.       pass   deprecated  
  653.     elif in ("-k""--package_key"):  
  654.       OPTIONS.package_key  
  655.     elif in ("-i""--incremental_from"):  
  656.       OPTIONS.incremental_source  
  657.     elif in ("-w""--wipe_user_data"):  
  658.       OPTIONS.wipe_user_data True  
  659.     elif in ("-n""--no_prereq"):  
  660.       OPTIONS.omit_prereq True  
  661.     elif in ("-e""--extra_script"):  
  662.       OPTIONS.extra_script  
  663.     elif in ("--worker_threads"):  
  664.       OPTIONS.worker_threads int(a)  
  665.     else 
  666.       return False  
  667.     return True  
  668.   
  669.   args common.ParseOptions(argv, __doc__,  
  670.                              extra_opts="b:k:i:d:wne:" 
  671.                              extra_long_opts=["board_config=" 
  672.                                               "package_key=" 
  673.                                               "incremental_from=" 
  674.                                               "wipe_user_data" 
  675.                                               "no_prereq" 
  676.                                               "extra_script=" 
  677.                                               "worker_threads="],  
  678.                              extra_option_handler=option_handler)  
  679.   
  680.   if len(args) != 2 
  681.     common.Usage(__doc__)  
  682.     sys.exit(1 
  683.   
  684.   if OPTIONS.extra_script is not None 
  685.     OPTIONS.extra_script open(OPTIONS.extra_script).read()  
  686.   
  687.   print "unzipping target target-files..."  
  688.   OPTIONS.input_tmp common.UnzipTemp(args[0])  
  689.   
  690.   OPTIONS.target_tmp OPTIONS.input_tmp  
  691.   input_zip zipfile.ZipFile(args[0], "r" 
  692.   OPTIONS.info_dict common.LoadInfoDict(input_zip)  
  693.   if OPTIONS.verbose:  
  694.     print "--- target info ---"  
  695.     common.DumpInfoDict(OPTIONS.info_dict)  
  696.   
  697.   if OPTIONS.device_specific is None 
  698.     OPTIONS.device_specific OPTIONS.info_dict.get("tool_extensions"None 
  699.   if OPTIONS.device_specific is not None 
  700.     OPTIONS.device_specific os.path.normpath(OPTIONS.device_specific)  
  701.     print "using device-specific extensions in"OPTIONS.device_specific  
  702.   
  703.   if OPTIONS.package_key:  
  704.     temp_zip_file tempfile.NamedTemporaryFile()  
  705.     output_zip zipfile.ZipFile(temp_zip_file, "w" 
  706.                                  compression=zipfile.ZIP_DEFLATED)  
  707.   else 
  708.     output_zip zipfile.ZipFile(args[1], "w" 
  709.                                  compression=zipfile.ZIP_DEFLATED)  
  710.   
  711.   if OPTIONS.incremental_source is None 
  712.     WriteFullOTAPackage(input_zip, output_zip)  
  713.   else 
  714.     print "unzipping source target-files..."  
  715.     OPTIONS.source_tmp common.UnzipTemp(OPTIONS.incremental_source)  
  716.     source_zip zipfile.ZipFile(OPTIONS.incremental_source, "r" 
  717.     OPTIONS.target_info_dict OPTIONS.info_dict  
  718.     OPTIONS.source_info_dict common.LoadInfoDict(source_zip)  
  719.     if OPTIONS.verbose:  
  720.       print "--- source info ---"  
  721.       common.DumpInfoDict(OPTIONS.source_info_dict)  
  722.     WriteIncrementalOTAPackage(input_zip, source_zip, output_zip)  
  723.   
  724.   output_zip.close()  
  725.   if OPTIONS.package_key:  
  726.     SignOutput(temp_zip_file.name, args[1])  
  727.     temp_zip_file.close()  
  728.   
  729.   common.Cleanup()  
  730.   
  731.   print "done."  
  732.   
  733.   
  734. if __name__ == '__main__' 
  735.   try 
  736.     common.CloseInheritedPipes()  
  737.     main(sys.argv[1:])  
  738.   except common.ExternalError, e:  
  739.     print  
  740.     print   ERROR: %s" (e,)  
  741.     print  
  742.     sys.exit(1 

                       主函数main是python的入口函数,我们从main函数开始看,大概看一下main函数(脚本最后)里的流程就能知道脚本的执行过程了。

                                   ① 在main函数的开头,首先将用户设定的option选项存入OPTIONS变量中,它是一个python中的类。紧接着判断有没有额外的脚本,如果有就读入到OPTIONS变量中。
                                   ② 解压缩输入的zip包,即我们在上文生成的原始zip包。然后判断是否用到device-specific extensions(设备扩展)如果用到,随即读入到OPTIONS变量中。
                                   ③ 判断是否签名,然后判断是否有新内容的增量源,有的话就解压该增量源包放入一个临时变量中(source_zip)。自此,所有的准备工作已完毕,随即会调用该 脚本中 最主要的函数WriteFullOTAPackag e(input_zip,output_zip)
                                   ④ WriteFullOTAPackage函数的处理过程是先获得 脚本的生成器。默认格式是edify。然后获得metadata元数据 ,此数据来至于Android的一些环境变量。然后获得设备配置参数比如api函数的版本。然后判断是否忽略时间戳。
                                   ⑤ WriteFullOTAPackage函数做完准备工作后就开始 生成升级用的脚本文件(updater-script)了 。生成脚本文件后将上一步获得的metadata元数据写入到输出包out_zip。
                                   ⑥至此一个完整的update.zip升级包就生成了。生成位置在:out/target/product/tcc8800 /full_tcc8800_evm-ota-eng.mumu.20120315.155326.zip。将升级包拷贝到SD卡中就可以用来升级了。

四、 Android OTA增量包update.zip的生成

         在上面的过程中生成的update.zip升级包是全部系统的升级包。大小有95M多,在实际升级中,我们只希望能够升级我们改变的那部分内容。这就需要使用增量包来升级。生成增量包的过程也需要上文中提到的ota_from_target_files的参与。

         下面是制作update.zip增量包的过程。

               ① 在源码根目录下依次执行下列命令
                 $ . build/envsetup.sh
                 $ lunch 选择17
                 $ make
                 $ make otapackage
                 执行上面的命令后会在out/target/product/tcc8800/下生成我们第一个系统升级包。我们先将其命名为A.zip
               ② 在源码中修改我们需要改变的部分,比如修改内核配置,增加新的驱动等等。修改后再一次执行上面的命令。就会生成第二个我们修改后生成的update.zip升级包。将   其命名为B.zip。

          ③ 在上文中我们看了ota_from_target_files.py脚本的使用帮助,其中选项-i就是用来生成差分增量包的。使用方法是以上面的 A.zip 和B.zip包作为输入,以update.zip包作  为输出。生成的update.zip就是我们最后需要的增量包。

              具体使用方式是:将上述两个包拷贝到源码根目录下,然后执行下面的命令。

                     $ ./build/tools/releasetools/ota_from_target_files -i A.zip B.zip update.zip。

              在执行上述命令时会出现未找到recovery_api_version的错误。原因是在执行上面的脚本时如果使用选项i则会调用 WriteIncrementalOTAPackage会从A包和B包中的META目录下搜索misc_info.txt来读取 recovery_api_version的值。但是在执行make  otapackage命令时生成的update.zip包中没有这个目录更没有这个文档。

              此时我们就需要使用执行make otapackage生成的原始的zip包。这个包的位置在out/target/product/tcc8800/obj/PACKAGING/target_files_intermediates/目录下,它是在用命令make otapackage之后的中间生产物,是最原始的升级包。我们将两次编译的生成的原始包分别重命名为A.zip和B.zip,并拷贝到SD卡根目录下重复执行上面的命令:

               $ ./build/tools/releasetools/ota_form_target_files -i A.zip B.zip update.zip。

              在上述命令即将执行完毕时,device/telechips/common/releasetools.py会调用IncrementalOTA_InstallEnd,在这个函数中 读取包中的RADIO/bootloader.img。

                     而包中是没有这个目录和bootloader.img的。所以执行失败,未能生成对应的update.zip。可能与我们未修改bootloader(升级firmware)有关。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值