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