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