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. |
| 51 | |
Hristo Bojinov | dafb042 | 2010-08-26 14:35:16 -0700 | [diff] [blame] | 52 | -a (--aslr_mode) <on|off> |
| 53 | Specify whether to turn on ASLR for the package (on by default). |
Stephen Smalley | 56882bf | 2012-02-09 13:36:21 -0500 | [diff] [blame] | 54 | |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 55 | -2 (--two_step) |
| 56 | Generate a 'two-step' OTA package, where recovery is updated |
| 57 | first, so that any changes made to the system partition are done |
| 58 | using the new recovery (new kernel, etc.). |
| 59 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 60 | """ |
| 61 | |
| 62 | import sys |
| 63 | |
| 64 | if sys.hexversion < 0x02040000: |
| 65 | print >> sys.stderr, "Python 2.4 or newer is required." |
| 66 | sys.exit(1) |
| 67 | |
| 68 | import copy |
Doug Zongker | c18736b | 2009-09-30 09:20:32 -0700 | [diff] [blame] | 69 | import errno |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 70 | import os |
| 71 | import re |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 72 | import subprocess |
| 73 | import tempfile |
| 74 | import time |
| 75 | import zipfile |
| 76 | |
david | cad0bb9 | 2011-03-15 14:21:38 +0000 | [diff] [blame] | 77 | try: |
| 78 | from hashlib import sha1 as sha1 |
| 79 | except ImportError: |
| 80 | from sha import sha as sha1 |
| 81 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 82 | import common |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 83 | import edify_generator |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 84 | |
| 85 | OPTIONS = common.OPTIONS |
Doug Zongker | afb32ea | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 86 | OPTIONS.package_key = None |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 87 | OPTIONS.incremental_source = None |
| 88 | OPTIONS.require_verbatim = set() |
| 89 | OPTIONS.prohibit_verbatim = set(("system/build.prop",)) |
| 90 | OPTIONS.patch_threshold = 0.95 |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 91 | OPTIONS.wipe_user_data = False |
Doug Zongker | 962069c | 2009-04-23 11:41:58 -0700 | [diff] [blame] | 92 | OPTIONS.omit_prereq = False |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 93 | OPTIONS.extra_script = None |
Hristo Bojinov | dafb042 | 2010-08-26 14:35:16 -0700 | [diff] [blame] | 94 | OPTIONS.aslr_mode = True |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 95 | OPTIONS.worker_threads = 3 |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 96 | OPTIONS.two_step = False |
Takeshi Kanemoto | e153b34 | 2013-11-14 17:20:50 +0900 | [diff] [blame] | 97 | OPTIONS.no_signing = False |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 98 | |
| 99 | def MostPopularKey(d, default): |
| 100 | """Given a dict, return the key corresponding to the largest |
| 101 | value. Returns 'default' if the dict is empty.""" |
| 102 | x = [(v, k) for (k, v) in d.iteritems()] |
| 103 | if not x: return default |
| 104 | x.sort() |
| 105 | return x[-1][1] |
| 106 | |
| 107 | |
| 108 | def IsSymlink(info): |
| 109 | """Return true if the zipfile.ZipInfo object passed in represents a |
| 110 | symlink.""" |
| 111 | return (info.external_attr >> 16) == 0120777 |
| 112 | |
Hristo Bojinov | 96be720 | 2010-08-02 10:26:17 -0700 | [diff] [blame] | 113 | def IsRegular(info): |
| 114 | """Return true if the zipfile.ZipInfo object passed in represents a |
| 115 | symlink.""" |
| 116 | return (info.external_attr >> 28) == 010 |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 117 | |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 118 | def ClosestFileMatch(src, tgtfiles, existing): |
| 119 | """Returns the closest file match between a source file and list |
| 120 | of potential matches. The exact filename match is preferred, |
| 121 | then the sha1 is searched for, and finally a file with the same |
| 122 | basename is evaluated. Rename support in the updater-binary is |
| 123 | required for the latter checks to be used.""" |
| 124 | |
| 125 | result = tgtfiles.get("path:" + src.name) |
| 126 | if result is not None: |
| 127 | return result |
| 128 | |
| 129 | if not OPTIONS.target_info_dict.get("update_rename_support", False): |
| 130 | return None |
| 131 | |
| 132 | if src.size < 1000: |
| 133 | return None |
| 134 | |
| 135 | result = tgtfiles.get("sha1:" + src.sha1) |
| 136 | if result is not None and existing.get(result.name) is None: |
| 137 | return result |
| 138 | result = tgtfiles.get("file:" + src.name.split("/")[-1]) |
| 139 | if result is not None and existing.get(result.name) is None: |
| 140 | return result |
| 141 | return None |
| 142 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 143 | class Item: |
| 144 | """Items represent the metadata (user, group, mode) of files and |
| 145 | directories in the system image.""" |
| 146 | ITEMS = {} |
| 147 | def __init__(self, name, dir=False): |
| 148 | self.name = name |
| 149 | self.uid = None |
| 150 | self.gid = None |
| 151 | self.mode = None |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 152 | self.selabel = None |
| 153 | self.capabilities = None |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 154 | self.dir = dir |
| 155 | |
| 156 | if name: |
| 157 | self.parent = Item.Get(os.path.dirname(name), dir=True) |
| 158 | self.parent.children.append(self) |
| 159 | else: |
| 160 | self.parent = None |
| 161 | if dir: |
| 162 | self.children = [] |
| 163 | |
| 164 | def Dump(self, indent=0): |
| 165 | if self.uid is not None: |
| 166 | print "%s%s %d %d %o" % (" "*indent, self.name, self.uid, self.gid, self.mode) |
| 167 | else: |
| 168 | print "%s%s %s %s %s" % (" "*indent, self.name, self.uid, self.gid, self.mode) |
| 169 | if self.dir: |
| 170 | print "%s%s" % (" "*indent, self.descendants) |
| 171 | print "%s%s" % (" "*indent, self.best_subtree) |
| 172 | for i in self.children: |
| 173 | i.Dump(indent=indent+1) |
| 174 | |
| 175 | @classmethod |
| 176 | def Get(cls, name, dir=False): |
| 177 | if name not in cls.ITEMS: |
| 178 | cls.ITEMS[name] = Item(name, dir=dir) |
| 179 | return cls.ITEMS[name] |
| 180 | |
| 181 | @classmethod |
Doug Zongker | 283e2a1 | 2010-03-15 17:52:32 -0700 | [diff] [blame] | 182 | def GetMetadata(cls, input_zip): |
| 183 | |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 184 | # The target_files contains a record of what the uid, |
| 185 | # gid, and mode are supposed to be. |
| 186 | output = input_zip.read("META/filesystem_config.txt") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 187 | |
| 188 | for line in output.split("\n"): |
| 189 | if not line: continue |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 190 | columns = line.split() |
| 191 | name, uid, gid, mode = columns[:4] |
| 192 | selabel = None |
| 193 | capabilities = None |
| 194 | |
| 195 | # After the first 4 columns, there are a series of key=value |
| 196 | # pairs. Extract out the fields we care about. |
| 197 | for element in columns[4:]: |
| 198 | key, value = element.split("=") |
| 199 | if key == "selabel": |
| 200 | selabel = value |
| 201 | if key == "capabilities": |
| 202 | capabilities = value |
| 203 | |
Doug Zongker | 283e2a1 | 2010-03-15 17:52:32 -0700 | [diff] [blame] | 204 | i = cls.ITEMS.get(name, None) |
| 205 | if i is not None: |
| 206 | i.uid = int(uid) |
| 207 | i.gid = int(gid) |
| 208 | i.mode = int(mode, 8) |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 209 | i.selabel = selabel |
| 210 | i.capabilities = capabilities |
Doug Zongker | 283e2a1 | 2010-03-15 17:52:32 -0700 | [diff] [blame] | 211 | if i.dir: |
| 212 | i.children.sort(key=lambda i: i.name) |
| 213 | |
| 214 | # set metadata for the files generated by this script. |
| 215 | i = cls.ITEMS.get("system/recovery-from-boot.p", None) |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 216 | if i: i.uid, i.gid, i.mode, i.selabel, i.capabilities = 0, 0, 0644, None, None |
Doug Zongker | 283e2a1 | 2010-03-15 17:52:32 -0700 | [diff] [blame] | 217 | i = cls.ITEMS.get("system/etc/install-recovery.sh", None) |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 218 | if i: i.uid, i.gid, i.mode, i.selabel, i.capabilities = 0, 0, 0544, None, None |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 219 | |
| 220 | def CountChildMetadata(self): |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 221 | """Count up the (uid, gid, mode, selabel, capabilities) tuples for |
| 222 | all children and determine the best strategy for using set_perm_recursive and |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 223 | set_perm to correctly chown/chmod all the files to their desired |
| 224 | values. Recursively calls itself for all descendants. |
| 225 | |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 226 | Returns a dict of {(uid, gid, dmode, fmode, selabel, capabilities): count} counting up |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 227 | all descendants of this node. (dmode or fmode may be None.) Also |
| 228 | sets the best_subtree of each directory Item to the (uid, gid, |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 229 | dmode, fmode, selabel, capabilities) tuple that will match the most |
| 230 | descendants of that Item. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 231 | """ |
| 232 | |
| 233 | assert self.dir |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 234 | d = self.descendants = {(self.uid, self.gid, self.mode, None, self.selabel, self.capabilities): 1} |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 235 | for i in self.children: |
| 236 | if i.dir: |
| 237 | for k, v in i.CountChildMetadata().iteritems(): |
| 238 | d[k] = d.get(k, 0) + v |
| 239 | else: |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 240 | k = (i.uid, i.gid, None, i.mode, i.selabel, i.capabilities) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 241 | d[k] = d.get(k, 0) + 1 |
| 242 | |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 243 | # Find the (uid, gid, dmode, fmode, selabel, capabilities) |
| 244 | # tuple that matches the most descendants. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 245 | |
| 246 | # First, find the (uid, gid) pair that matches the most |
| 247 | # descendants. |
| 248 | ug = {} |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 249 | for (uid, gid, _, _, _, _), count in d.iteritems(): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 250 | ug[(uid, gid)] = ug.get((uid, gid), 0) + count |
| 251 | ug = MostPopularKey(ug, (0, 0)) |
| 252 | |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 253 | # Now find the dmode, fmode, selabel, and capabilities that match |
| 254 | # the most descendants with that (uid, gid), and choose those. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 255 | best_dmode = (0, 0755) |
| 256 | best_fmode = (0, 0644) |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 257 | best_selabel = (0, None) |
| 258 | best_capabilities = (0, None) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 259 | for k, count in d.iteritems(): |
| 260 | if k[:2] != ug: continue |
| 261 | if k[2] is not None and count >= best_dmode[0]: best_dmode = (count, k[2]) |
| 262 | if k[3] is not None and count >= best_fmode[0]: best_fmode = (count, k[3]) |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 263 | if k[4] is not None and count >= best_selabel[0]: best_selabel = (count, k[4]) |
| 264 | if k[5] is not None and count >= best_capabilities[0]: best_capabilities = (count, k[5]) |
| 265 | self.best_subtree = ug + (best_dmode[1], best_fmode[1], best_selabel[1], best_capabilities[1]) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 266 | |
| 267 | return d |
| 268 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 269 | def SetPermissions(self, script): |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 270 | """Append set_perm/set_perm_recursive commands to 'script' to |
| 271 | set all permissions, users, and groups for the tree of files |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 272 | rooted at 'self'.""" |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 273 | |
| 274 | self.CountChildMetadata() |
| 275 | |
| 276 | def recurse(item, current): |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 277 | # current is the (uid, gid, dmode, fmode, selabel, capabilities) tuple that the current |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 278 | # item (and all its children) have already been set to. We only |
| 279 | # need to issue set_perm/set_perm_recursive commands if we're |
| 280 | # supposed to be something different. |
| 281 | if item.dir: |
| 282 | if current != item.best_subtree: |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 283 | script.SetPermissionsRecursive("/"+item.name, *item.best_subtree) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 284 | current = item.best_subtree |
| 285 | |
| 286 | if item.uid != current[0] or item.gid != current[1] or \ |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 287 | item.mode != current[2] or item.selabel != current[4] or \ |
| 288 | item.capabilities != current[5]: |
| 289 | script.SetPermissions("/"+item.name, item.uid, item.gid, |
| 290 | item.mode, item.selabel, item.capabilities) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 291 | |
| 292 | for i in item.children: |
| 293 | recurse(i, current) |
| 294 | else: |
| 295 | if item.uid != current[0] or item.gid != current[1] or \ |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 296 | item.mode != current[3] or item.selabel != current[4] or \ |
| 297 | item.capabilities != current[5]: |
| 298 | script.SetPermissions("/"+item.name, item.uid, item.gid, |
| 299 | item.mode, item.selabel, item.capabilities) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 300 | |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 301 | recurse(self, (-1, -1, -1, -1, None, None)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 302 | |
| 303 | |
| 304 | def CopySystemFiles(input_zip, output_zip=None, |
| 305 | substitute=None): |
| 306 | """Copies files underneath system/ in the input zip to the output |
| 307 | zip. Populates the Item class with their metadata, and returns a |
Doug Zongker | 1807e70 | 2012-02-28 12:21:08 -0800 | [diff] [blame] | 308 | list of symlinks. output_zip may be None, in which case the copy is |
| 309 | skipped (but the other side effects still happen). substitute is an |
| 310 | optional dict of {output filename: contents} to be output instead of |
| 311 | certain input files. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 312 | """ |
| 313 | |
| 314 | symlinks = [] |
| 315 | |
| 316 | for info in input_zip.infolist(): |
| 317 | if info.filename.startswith("SYSTEM/"): |
| 318 | basefilename = info.filename[7:] |
| 319 | if IsSymlink(info): |
| 320 | symlinks.append((input_zip.read(info.filename), |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 321 | "/system/" + basefilename)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 322 | else: |
| 323 | info2 = copy.copy(info) |
| 324 | fn = info2.filename = "system/" + basefilename |
| 325 | if substitute and fn in substitute and substitute[fn] is None: |
| 326 | continue |
| 327 | if output_zip is not None: |
| 328 | if substitute and fn in substitute: |
| 329 | data = substitute[fn] |
| 330 | else: |
| 331 | data = input_zip.read(info.filename) |
| 332 | output_zip.writestr(info2, data) |
| 333 | if fn.endswith("/"): |
| 334 | Item.Get(fn[:-1], dir=True) |
| 335 | else: |
| 336 | Item.Get(fn, dir=False) |
| 337 | |
| 338 | symlinks.sort() |
Doug Zongker | 1807e70 | 2012-02-28 12:21:08 -0800 | [diff] [blame] | 339 | return symlinks |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 340 | |
| 341 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 342 | def SignOutput(temp_zip_name, output_zip_name): |
| 343 | key_passwords = common.GetKeyPasswords([OPTIONS.package_key]) |
| 344 | pw = key_passwords[OPTIONS.package_key] |
| 345 | |
Doug Zongker | 951495f | 2009-08-14 12:44:19 -0700 | [diff] [blame] | 346 | common.SignFile(temp_zip_name, output_zip_name, OPTIONS.package_key, pw, |
| 347 | whole_file=True) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 348 | |
| 349 | |
Doug Zongker | 1eb74dd | 2012-08-16 16:19:00 -0700 | [diff] [blame] | 350 | def AppendAssertions(script, info_dict): |
| 351 | device = GetBuildProp("ro.product.device", info_dict) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 352 | script.AssertDevice(device) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 353 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 354 | |
Doug Zongker | b32161a | 2012-08-21 10:33:44 -0700 | [diff] [blame] | 355 | def MakeRecoveryPatch(input_tmp, output_zip, recovery_img, boot_img): |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 356 | """Generate a binary patch that creates the recovery image starting |
| 357 | with the boot image. (Most of the space in these images is just the |
| 358 | kernel, which is identical for the two, so the resulting patch |
| 359 | should be efficient.) Add it to the output zip, along with a shell |
| 360 | script that is run from init.rc on first boot to actually do the |
| 361 | patching and install the new recovery image. |
| 362 | |
| 363 | recovery_img and boot_img should be File objects for the |
Doug Zongker | 6736998 | 2010-07-07 13:53:32 -0700 | [diff] [blame] | 364 | corresponding images. info should be the dictionary returned by |
| 365 | common.LoadInfoDict() on the input target_files. |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 366 | |
| 367 | Returns an Item for the shell script, which must be made |
| 368 | executable. |
| 369 | """ |
| 370 | |
Doug Zongker | b32161a | 2012-08-21 10:33:44 -0700 | [diff] [blame] | 371 | diff_program = ["imgdiff"] |
| 372 | path = os.path.join(input_tmp, "SYSTEM", "etc", "recovery-resource.dat") |
| 373 | if os.path.exists(path): |
| 374 | diff_program.append("-b") |
| 375 | diff_program.append(path) |
| 376 | bonus_args = "-b /system/etc/recovery-resource.dat" |
| 377 | else: |
| 378 | bonus_args = "" |
| 379 | |
| 380 | d = common.Difference(recovery_img, boot_img, diff_program=diff_program) |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 381 | _, _, patch = d.ComputePatch() |
Doug Zongker | cfd7db6 | 2009-10-07 11:35:53 -0700 | [diff] [blame] | 382 | common.ZipWriteStr(output_zip, "recovery/recovery-from-boot.p", patch) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 383 | Item.Get("system/recovery-from-boot.p", dir=False) |
| 384 | |
Doug Zongker | 96a57e7 | 2010-09-26 14:57:41 -0700 | [diff] [blame] | 385 | boot_type, boot_device = common.GetTypeAndDevice("/boot", OPTIONS.info_dict) |
| 386 | recovery_type, recovery_device = common.GetTypeAndDevice("/recovery", OPTIONS.info_dict) |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 387 | |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 388 | sh = """#!/system/bin/sh |
Doug Zongker | 0276d18 | 2011-12-02 10:46:59 -0800 | [diff] [blame] | 389 | 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] | 390 | log -t recovery "Installing new recovery image" |
Doug Zongker | b32161a | 2012-08-21 10:33:44 -0700 | [diff] [blame] | 391 | applypatch %(bonus_args)s %(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] | 392 | else |
| 393 | log -t recovery "Recovery image already installed" |
| 394 | fi |
| 395 | """ % { 'boot_size': boot_img.size, |
| 396 | 'boot_sha1': boot_img.sha1, |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 397 | 'recovery_size': recovery_img.size, |
Doug Zongker | 6736998 | 2010-07-07 13:53:32 -0700 | [diff] [blame] | 398 | 'recovery_sha1': recovery_img.sha1, |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 399 | 'boot_type': boot_type, |
| 400 | 'boot_device': boot_device, |
| 401 | 'recovery_type': recovery_type, |
| 402 | 'recovery_device': recovery_device, |
Doug Zongker | b32161a | 2012-08-21 10:33:44 -0700 | [diff] [blame] | 403 | 'bonus_args': bonus_args, |
Doug Zongker | 6736998 | 2010-07-07 13:53:32 -0700 | [diff] [blame] | 404 | } |
Doug Zongker | cfd7db6 | 2009-10-07 11:35:53 -0700 | [diff] [blame] | 405 | common.ZipWriteStr(output_zip, "recovery/etc/install-recovery.sh", sh) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 406 | return Item.Get("system/etc/install-recovery.sh", dir=False) |
| 407 | |
| 408 | |
Doug Zongker | c77a9ad | 2010-09-16 11:28:43 -0700 | [diff] [blame] | 409 | def WriteFullOTAPackage(input_zip, output_zip): |
Doug Zongker | 9ce2ebf | 2010-04-21 14:08:44 -0700 | [diff] [blame] | 410 | # TODO: how to determine this? We don't know what version it will |
| 411 | # be installed on top of. For now, we expect the API just won't |
| 412 | # change very often. |
Doug Zongker | c77a9ad | 2010-09-16 11:28:43 -0700 | [diff] [blame] | 413 | script = edify_generator.EdifyGenerator(3, OPTIONS.info_dict) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 414 | |
Doug Zongker | 1eb74dd | 2012-08-16 16:19:00 -0700 | [diff] [blame] | 415 | metadata = {"post-build": GetBuildProp("ro.build.fingerprint", |
| 416 | OPTIONS.info_dict), |
| 417 | "pre-device": GetBuildProp("ro.product.device", |
| 418 | OPTIONS.info_dict), |
| 419 | "post-timestamp": GetBuildProp("ro.build.date.utc", |
| 420 | OPTIONS.info_dict), |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 423 | device_specific = common.DeviceSpecificParams( |
| 424 | input_zip=input_zip, |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 425 | input_version=OPTIONS.info_dict["recovery_api_version"], |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 426 | output_zip=output_zip, |
| 427 | script=script, |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 428 | input_tmp=OPTIONS.input_tmp, |
Doug Zongker | 96a57e7 | 2010-09-26 14:57:41 -0700 | [diff] [blame] | 429 | metadata=metadata, |
| 430 | info_dict=OPTIONS.info_dict) |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 431 | |
Doug Zongker | 962069c | 2009-04-23 11:41:58 -0700 | [diff] [blame] | 432 | if not OPTIONS.omit_prereq: |
Doug Zongker | 1eb74dd | 2012-08-16 16:19:00 -0700 | [diff] [blame] | 433 | ts = GetBuildProp("ro.build.date.utc", OPTIONS.info_dict) |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 434 | ts_text = GetBuildProp("ro.build.date", OPTIONS.info_dict) |
| 435 | script.AssertOlderBuild(ts, ts_text) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 436 | |
Doug Zongker | 1eb74dd | 2012-08-16 16:19:00 -0700 | [diff] [blame] | 437 | AppendAssertions(script, OPTIONS.info_dict) |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 438 | device_specific.FullOTA_Assertions() |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 439 | |
| 440 | # Two-step package strategy (in chronological order, which is *not* |
| 441 | # the order in which the generated script has things): |
| 442 | # |
| 443 | # if stage is not "2/3" or "3/3": |
| 444 | # write recovery image to boot partition |
| 445 | # set stage to "2/3" |
| 446 | # reboot to boot partition and restart recovery |
| 447 | # else if stage is "2/3": |
| 448 | # write recovery image to recovery partition |
| 449 | # set stage to "3/3" |
| 450 | # reboot to recovery partition and restart recovery |
| 451 | # else: |
| 452 | # (stage must be "3/3") |
| 453 | # set stage to "" |
| 454 | # do normal full package installation: |
| 455 | # wipe and install system, boot image, etc. |
| 456 | # set up system to update recovery partition on first boot |
| 457 | # complete script normally (allow recovery to mark itself finished and reboot) |
| 458 | |
| 459 | recovery_img = common.GetBootableImage("recovery.img", "recovery.img", |
| 460 | OPTIONS.input_tmp, "RECOVERY") |
| 461 | if OPTIONS.two_step: |
| 462 | if not OPTIONS.info_dict.get("multistage_support", None): |
| 463 | assert False, "two-step packages not supported by this build" |
| 464 | fs = OPTIONS.info_dict["fstab"]["/misc"] |
| 465 | assert fs.fs_type.upper() == "EMMC", \ |
| 466 | "two-step packages only supported on devices with EMMC /misc partitions" |
| 467 | bcb_dev = {"bcb_dev": fs.device} |
| 468 | common.ZipWriteStr(output_zip, "recovery.img", recovery_img.data) |
| 469 | script.AppendExtra(""" |
| 470 | if get_stage("%(bcb_dev)s", "stage") == "2/3" then |
| 471 | """ % bcb_dev) |
| 472 | script.WriteRawImage("/recovery", "recovery.img") |
| 473 | script.AppendExtra(""" |
| 474 | set_stage("%(bcb_dev)s", "3/3"); |
| 475 | reboot_now("%(bcb_dev)s", "recovery"); |
| 476 | else if get_stage("%(bcb_dev)s", "stage") == "3/3" then |
| 477 | """ % bcb_dev) |
| 478 | |
Doug Zongker | e5ff590 | 2012-01-17 10:55:37 -0800 | [diff] [blame] | 479 | device_specific.FullOTA_InstallBegin() |
Doug Zongker | 171f1cd | 2009-06-15 22:36:37 -0700 | [diff] [blame] | 480 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 481 | script.ShowProgress(0.5, 0) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 482 | |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 483 | if OPTIONS.wipe_user_data: |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 484 | script.FormatPartition("/data") |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 485 | |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 486 | if "selinux_fc" in OPTIONS.info_dict: |
| 487 | WritePolicyConfig(OPTIONS.info_dict["selinux_fc"], output_zip) |
Stephen Smalley | 56882bf | 2012-02-09 13:36:21 -0500 | [diff] [blame] | 488 | |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 489 | script.FormatPartition("/system") |
| 490 | script.Mount("/system") |
Doug Zongker | cfd7db6 | 2009-10-07 11:35:53 -0700 | [diff] [blame] | 491 | script.UnpackPackageDir("recovery", "/system") |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 492 | script.UnpackPackageDir("system", "/system") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 493 | |
Doug Zongker | 1807e70 | 2012-02-28 12:21:08 -0800 | [diff] [blame] | 494 | symlinks = CopySystemFiles(input_zip, output_zip) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 495 | script.MakeSymlinks(symlinks) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 496 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 497 | boot_img = common.GetBootableImage("boot.img", "boot.img", |
| 498 | OPTIONS.input_tmp, "BOOT") |
Doug Zongker | b32161a | 2012-08-21 10:33:44 -0700 | [diff] [blame] | 499 | MakeRecoveryPatch(OPTIONS.input_tmp, output_zip, recovery_img, boot_img) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 500 | |
Doug Zongker | 283e2a1 | 2010-03-15 17:52:32 -0700 | [diff] [blame] | 501 | Item.GetMetadata(input_zip) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 502 | Item.Get("system").SetPermissions(script) |
| 503 | |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 504 | common.CheckSize(boot_img.data, "boot.img", OPTIONS.info_dict) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 505 | common.ZipWriteStr(output_zip, "boot.img", boot_img.data) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 506 | script.ShowProgress(0.2, 0) |
| 507 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 508 | script.ShowProgress(0.2, 10) |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 509 | script.WriteRawImage("/boot", "boot.img") |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 510 | |
| 511 | script.ShowProgress(0.1, 0) |
| 512 | device_specific.FullOTA_InstallEnd() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 513 | |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 514 | if OPTIONS.extra_script is not None: |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 515 | script.AppendExtra(OPTIONS.extra_script) |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 516 | |
Doug Zongker | 1483360 | 2010-02-02 13:12:04 -0800 | [diff] [blame] | 517 | script.UnmountAll() |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 518 | |
| 519 | if OPTIONS.two_step: |
| 520 | script.AppendExtra(""" |
| 521 | set_stage("%(bcb_dev)s", ""); |
| 522 | """ % bcb_dev) |
| 523 | script.AppendExtra("else\n") |
| 524 | script.WriteRawImage("/boot", "recovery.img") |
| 525 | script.AppendExtra(""" |
| 526 | set_stage("%(bcb_dev)s", "2/3"); |
| 527 | reboot_now("%(bcb_dev)s", ""); |
| 528 | endif; |
| 529 | endif; |
| 530 | """ % bcb_dev) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 531 | script.AddToZip(input_zip, output_zip) |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 532 | WriteMetadata(metadata, output_zip) |
| 533 | |
Stephen Smalley | 56882bf | 2012-02-09 13:36:21 -0500 | [diff] [blame] | 534 | def WritePolicyConfig(file_context, output_zip): |
| 535 | f = open(file_context, 'r'); |
| 536 | basename = os.path.basename(file_context) |
| 537 | common.ZipWriteStr(output_zip, basename, f.read()) |
| 538 | |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 539 | |
| 540 | def WriteMetadata(metadata, output_zip): |
| 541 | common.ZipWriteStr(output_zip, "META-INF/com/android/metadata", |
| 542 | "".join(["%s=%s\n" % kv |
| 543 | for kv in sorted(metadata.iteritems())])) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 544 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 545 | def LoadSystemFiles(z): |
| 546 | """Load all the files from SYSTEM/... in a given target-files |
| 547 | ZipFile, and return a dict of {filename: File object}.""" |
| 548 | out = {} |
| 549 | for info in z.infolist(): |
| 550 | if info.filename.startswith("SYSTEM/") and not IsSymlink(info): |
Hristo Bojinov | 96be720 | 2010-08-02 10:26:17 -0700 | [diff] [blame] | 551 | basefilename = info.filename[7:] |
| 552 | fn = "system/" + basefilename |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 553 | data = z.read(info.filename) |
Doug Zongker | ea5d7a9 | 2010-09-12 15:26:16 -0700 | [diff] [blame] | 554 | out[fn] = common.File(fn, data) |
Doug Zongker | 1807e70 | 2012-02-28 12:21:08 -0800 | [diff] [blame] | 555 | return out |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 556 | |
| 557 | |
Doug Zongker | 1eb74dd | 2012-08-16 16:19:00 -0700 | [diff] [blame] | 558 | def GetBuildProp(prop, info_dict): |
| 559 | """Return the fingerprint of the build of a given target-files info_dict.""" |
| 560 | try: |
| 561 | return info_dict.get("build.prop", {})[prop] |
| 562 | except KeyError: |
Doug Zongker | 9fc74c7 | 2009-06-23 16:27:38 -0700 | [diff] [blame] | 563 | raise common.ExternalError("couldn't find %s in build.prop" % (property,)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 564 | |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 565 | def AddToKnownPaths(filename, known_paths): |
| 566 | if filename[-1] == "/": |
| 567 | return |
| 568 | dirs = filename.split("/")[:-1] |
| 569 | while len(dirs) > 0: |
| 570 | path = "/".join(dirs) |
| 571 | if path in known_paths: |
| 572 | break; |
| 573 | known_paths.add(path) |
| 574 | dirs.pop() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 575 | |
Doug Zongker | c77a9ad | 2010-09-16 11:28:43 -0700 | [diff] [blame] | 576 | def WriteIncrementalOTAPackage(target_zip, source_zip, output_zip): |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 577 | source_version = OPTIONS.source_info_dict["recovery_api_version"] |
| 578 | target_version = OPTIONS.target_info_dict["recovery_api_version"] |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 579 | |
Doug Zongker | 9ce2ebf | 2010-04-21 14:08:44 -0700 | [diff] [blame] | 580 | if source_version == 0: |
| 581 | print ("WARNING: generating edify script for a source that " |
| 582 | "can't install it.") |
Doug Zongker | 1eb74dd | 2012-08-16 16:19:00 -0700 | [diff] [blame] | 583 | script = edify_generator.EdifyGenerator(source_version, |
| 584 | OPTIONS.target_info_dict) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 585 | |
Doug Zongker | 1eb74dd | 2012-08-16 16:19:00 -0700 | [diff] [blame] | 586 | metadata = {"pre-device": GetBuildProp("ro.product.device", |
| 587 | OPTIONS.source_info_dict), |
| 588 | "post-timestamp": GetBuildProp("ro.build.date.utc", |
| 589 | OPTIONS.target_info_dict), |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 592 | device_specific = common.DeviceSpecificParams( |
| 593 | source_zip=source_zip, |
Doug Zongker | 1483360 | 2010-02-02 13:12:04 -0800 | [diff] [blame] | 594 | source_version=source_version, |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 595 | target_zip=target_zip, |
Doug Zongker | 1483360 | 2010-02-02 13:12:04 -0800 | [diff] [blame] | 596 | target_version=target_version, |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 597 | output_zip=output_zip, |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 598 | script=script, |
Doug Zongker | 96a57e7 | 2010-09-26 14:57:41 -0700 | [diff] [blame] | 599 | metadata=metadata, |
| 600 | info_dict=OPTIONS.info_dict) |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 601 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 602 | print "Loading target..." |
Doug Zongker | 1807e70 | 2012-02-28 12:21:08 -0800 | [diff] [blame] | 603 | target_data = LoadSystemFiles(target_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 604 | print "Loading source..." |
Doug Zongker | 1807e70 | 2012-02-28 12:21:08 -0800 | [diff] [blame] | 605 | source_data = LoadSystemFiles(source_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 606 | |
| 607 | verbatim_targets = [] |
| 608 | patch_list = [] |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 609 | diffs = [] |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 610 | renames = {} |
| 611 | known_paths = set() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 612 | largest_source_size = 0 |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 613 | |
| 614 | matching_file_cache = {} |
| 615 | for fn, sf in source_data.items(): |
| 616 | assert fn == sf.name |
| 617 | matching_file_cache["path:" + fn] = sf |
| 618 | if fn in target_data.keys(): |
| 619 | AddToKnownPaths(fn, known_paths) |
| 620 | # Only allow eligibility for filename/sha matching |
| 621 | # if there isn't a perfect path match. |
| 622 | if target_data.get(sf.name) is None: |
| 623 | matching_file_cache["file:" + fn.split("/")[-1]] = sf |
| 624 | matching_file_cache["sha:" + sf.sha1] = sf |
| 625 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 626 | for fn in sorted(target_data.keys()): |
| 627 | tf = target_data[fn] |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 628 | assert fn == tf.name |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 629 | sf = ClosestFileMatch(tf, matching_file_cache, renames) |
| 630 | if sf is not None and sf.name != tf.name: |
| 631 | print "File has moved from " + sf.name + " to " + tf.name |
| 632 | renames[sf.name] = tf |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 633 | |
| 634 | if sf is None or fn in OPTIONS.require_verbatim: |
| 635 | # This file should be included verbatim |
| 636 | if fn in OPTIONS.prohibit_verbatim: |
Doug Zongker | 9fc74c7 | 2009-06-23 16:27:38 -0700 | [diff] [blame] | 637 | raise common.ExternalError("\"%s\" must be sent verbatim" % (fn,)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 638 | print "send", fn, "verbatim" |
| 639 | tf.AddToZip(output_zip) |
| 640 | verbatim_targets.append((fn, tf.size)) |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 641 | if fn in target_data.keys(): |
| 642 | AddToKnownPaths(fn, known_paths) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 643 | elif tf.sha1 != sf.sha1: |
| 644 | # File is different; consider sending as a patch |
Doug Zongker | ea5d7a9 | 2010-09-12 15:26:16 -0700 | [diff] [blame] | 645 | diffs.append(common.Difference(tf, sf)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 646 | else: |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 647 | # Target file data identical to source (may still be renamed) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 648 | pass |
| 649 | |
Doug Zongker | ea5d7a9 | 2010-09-12 15:26:16 -0700 | [diff] [blame] | 650 | common.ComputeDifferences(diffs) |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 651 | |
| 652 | for diff in diffs: |
| 653 | tf, sf, d = diff.GetPatch() |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 654 | path = "/".join(tf.name.split("/")[:-1]) |
| 655 | if d is None or len(d) > tf.size * OPTIONS.patch_threshold or \ |
| 656 | path not in known_paths: |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 657 | # patch is almost as big as the file; don't bother patching |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 658 | # or a patch + rename cannot take place due to the target |
| 659 | # directory not existing |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 660 | tf.AddToZip(output_zip) |
| 661 | verbatim_targets.append((tf.name, tf.size)) |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 662 | if sf.name in renames: |
| 663 | del renames[sf.name] |
| 664 | AddToKnownPaths(tf.name, known_paths) |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 665 | else: |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 666 | common.ZipWriteStr(output_zip, "patch/" + sf.name + ".p", d) |
| 667 | patch_list.append((tf, sf, tf.size, common.sha1(d).hexdigest())) |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 668 | largest_source_size = max(largest_source_size, sf.size) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 669 | |
Doug Zongker | 1eb74dd | 2012-08-16 16:19:00 -0700 | [diff] [blame] | 670 | source_fp = GetBuildProp("ro.build.fingerprint", OPTIONS.source_info_dict) |
| 671 | target_fp = GetBuildProp("ro.build.fingerprint", OPTIONS.target_info_dict) |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 672 | metadata["pre-build"] = source_fp |
| 673 | metadata["post-build"] = target_fp |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 674 | |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 675 | script.Mount("/system") |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 676 | script.AssertSomeFingerprint(source_fp, target_fp) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 677 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 678 | source_boot = common.GetBootableImage( |
Doug Zongker | d513160 | 2012-08-02 14:46:42 -0700 | [diff] [blame] | 679 | "/tmp/boot.img", "boot.img", OPTIONS.source_tmp, "BOOT", |
| 680 | OPTIONS.source_info_dict) |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 681 | target_boot = common.GetBootableImage( |
| 682 | "/tmp/boot.img", "boot.img", OPTIONS.target_tmp, "BOOT") |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 683 | updating_boot = (not OPTIONS.two_step and |
| 684 | (source_boot.data != target_boot.data)) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 685 | |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 686 | source_recovery = common.GetBootableImage( |
Doug Zongker | d513160 | 2012-08-02 14:46:42 -0700 | [diff] [blame] | 687 | "/tmp/recovery.img", "recovery.img", OPTIONS.source_tmp, "RECOVERY", |
| 688 | OPTIONS.source_info_dict) |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 689 | target_recovery = common.GetBootableImage( |
| 690 | "/tmp/recovery.img", "recovery.img", OPTIONS.target_tmp, "RECOVERY") |
Doug Zongker | f6a8bad | 2009-05-29 11:41:21 -0700 | [diff] [blame] | 691 | updating_recovery = (source_recovery.data != target_recovery.data) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 692 | |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 693 | # Here's how we divide up the progress bar: |
| 694 | # 0.1 for verifying the start state (PatchCheck calls) |
| 695 | # 0.8 for applying patches (ApplyPatch calls) |
| 696 | # 0.1 for unpacking verbatim files, symlinking, and doing the |
| 697 | # device-specific commands. |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 698 | |
Doug Zongker | 1eb74dd | 2012-08-16 16:19:00 -0700 | [diff] [blame] | 699 | AppendAssertions(script, OPTIONS.target_info_dict) |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 700 | device_specific.IncrementalOTA_Assertions() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 701 | |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 702 | # Two-step incremental package strategy (in chronological order, |
| 703 | # which is *not* the order in which the generated script has |
| 704 | # things): |
| 705 | # |
| 706 | # if stage is not "2/3" or "3/3": |
| 707 | # do verification on current system |
| 708 | # write recovery image to boot partition |
| 709 | # set stage to "2/3" |
| 710 | # reboot to boot partition and restart recovery |
| 711 | # else if stage is "2/3": |
| 712 | # write recovery image to recovery partition |
| 713 | # set stage to "3/3" |
| 714 | # reboot to recovery partition and restart recovery |
| 715 | # else: |
| 716 | # (stage must be "3/3") |
| 717 | # perform update: |
| 718 | # patch system files, etc. |
| 719 | # force full install of new boot image |
| 720 | # set up system to update recovery partition on first boot |
| 721 | # complete script normally (allow recovery to mark itself finished and reboot) |
| 722 | |
| 723 | if OPTIONS.two_step: |
| 724 | if not OPTIONS.info_dict.get("multistage_support", None): |
| 725 | assert False, "two-step packages not supported by this build" |
| 726 | fs = OPTIONS.info_dict["fstab"]["/misc"] |
| 727 | assert fs.fs_type.upper() == "EMMC", \ |
| 728 | "two-step packages only supported on devices with EMMC /misc partitions" |
| 729 | bcb_dev = {"bcb_dev": fs.device} |
| 730 | common.ZipWriteStr(output_zip, "recovery.img", target_recovery.data) |
| 731 | script.AppendExtra(""" |
| 732 | if get_stage("%(bcb_dev)s", "stage") == "2/3" then |
| 733 | """ % bcb_dev) |
| 734 | script.AppendExtra("sleep(20);\n"); |
| 735 | script.WriteRawImage("/recovery", "recovery.img") |
| 736 | script.AppendExtra(""" |
| 737 | set_stage("%(bcb_dev)s", "3/3"); |
| 738 | reboot_now("%(bcb_dev)s", "recovery"); |
| 739 | else if get_stage("%(bcb_dev)s", "stage") != "3/3" then |
| 740 | """ % bcb_dev) |
| 741 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 742 | script.Print("Verifying current system...") |
| 743 | |
Doug Zongker | e5ff590 | 2012-01-17 10:55:37 -0800 | [diff] [blame] | 744 | device_specific.IncrementalOTA_VerifyBegin() |
| 745 | |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 746 | script.ShowProgress(0.1, 0) |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 747 | total_verify_size = float(sum([i[1].size for i in patch_list]) + 1) |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 748 | if updating_boot: |
| 749 | total_verify_size += source_boot.size |
| 750 | so_far = 0 |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 751 | |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 752 | for tf, sf, size, patch_sha in patch_list: |
| 753 | if tf.name != sf.name: |
| 754 | script.SkipNextActionIfTargetExists(tf.name, tf.sha1) |
| 755 | script.PatchCheck("/"+sf.name, tf.sha1, sf.sha1) |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 756 | so_far += sf.size |
| 757 | script.SetProgress(so_far / total_verify_size) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 758 | |
Doug Zongker | 5da317e | 2009-06-02 13:38:17 -0700 | [diff] [blame] | 759 | if updating_boot: |
Doug Zongker | ea5d7a9 | 2010-09-12 15:26:16 -0700 | [diff] [blame] | 760 | d = common.Difference(target_boot, source_boot) |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 761 | _, _, d = d.ComputePatch() |
Doug Zongker | 5da317e | 2009-06-02 13:38:17 -0700 | [diff] [blame] | 762 | print "boot target: %d source: %d diff: %d" % ( |
| 763 | target_boot.size, source_boot.size, len(d)) |
| 764 | |
Doug Zongker | 048e7ca | 2009-06-15 14:31:53 -0700 | [diff] [blame] | 765 | common.ZipWriteStr(output_zip, "patch/boot.img.p", d) |
Doug Zongker | 5da317e | 2009-06-02 13:38:17 -0700 | [diff] [blame] | 766 | |
Doug Zongker | 96a57e7 | 2010-09-26 14:57:41 -0700 | [diff] [blame] | 767 | boot_type, boot_device = common.GetTypeAndDevice("/boot", OPTIONS.info_dict) |
Doug Zongker | f2ab290 | 2010-09-22 10:12:54 -0700 | [diff] [blame] | 768 | |
| 769 | script.PatchCheck("%s:%s:%d:%s:%d:%s" % |
| 770 | (boot_type, boot_device, |
Doug Zongker | 6736998 | 2010-07-07 13:53:32 -0700 | [diff] [blame] | 771 | source_boot.size, source_boot.sha1, |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 772 | target_boot.size, target_boot.sha1)) |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 773 | so_far += source_boot.size |
| 774 | script.SetProgress(so_far / total_verify_size) |
Doug Zongker | 5da317e | 2009-06-02 13:38:17 -0700 | [diff] [blame] | 775 | |
| 776 | if patch_list or updating_recovery or updating_boot: |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 777 | script.CacheFreeSpaceCheck(largest_source_size) |
Doug Zongker | 5a48209 | 2010-02-17 16:09:18 -0800 | [diff] [blame] | 778 | |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 779 | device_specific.IncrementalOTA_VerifyEnd() |
| 780 | |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 781 | if OPTIONS.two_step: |
| 782 | script.WriteRawImage("/boot", "recovery.img") |
| 783 | script.AppendExtra(""" |
| 784 | set_stage("%(bcb_dev)s", "2/3"); |
| 785 | reboot_now("%(bcb_dev)s", ""); |
| 786 | else |
| 787 | """ % bcb_dev) |
| 788 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 789 | script.Comment("---- start making changes here ----") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 790 | |
Doug Zongker | e5ff590 | 2012-01-17 10:55:37 -0800 | [diff] [blame] | 791 | device_specific.IncrementalOTA_InstallBegin() |
| 792 | |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 793 | if OPTIONS.two_step: |
| 794 | common.ZipWriteStr(output_zip, "boot.img", target_boot.data) |
| 795 | script.WriteRawImage("/boot", "boot.img") |
| 796 | print "writing full boot image (forced by two-step mode)" |
| 797 | |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 798 | if OPTIONS.wipe_user_data: |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 799 | script.Print("Erasing user data...") |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 800 | script.FormatPartition("/data") |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 801 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 802 | script.Print("Removing unneeded files...") |
Doug Zongker | 0f3298a | 2009-06-30 08:16:58 -0700 | [diff] [blame] | 803 | script.DeleteFiles(["/"+i[0] for i in verbatim_targets] + |
| 804 | ["/"+i for i in sorted(source_data) |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 805 | if i not in target_data and |
| 806 | i not in renames] + |
Doug Zongker | 3b949f0 | 2009-08-24 10:24:32 -0700 | [diff] [blame] | 807 | ["/system/recovery.img"]) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 808 | |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 809 | script.ShowProgress(0.8, 0) |
| 810 | total_patch_size = float(sum([i[1].size for i in patch_list]) + 1) |
| 811 | if updating_boot: |
| 812 | total_patch_size += target_boot.size |
| 813 | so_far = 0 |
| 814 | |
| 815 | script.Print("Patching system files...") |
Doug Zongker | e92f15a | 2011-08-26 13:46:40 -0700 | [diff] [blame] | 816 | deferred_patch_list = [] |
| 817 | for item in patch_list: |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 818 | tf, sf, size, _ = item |
Doug Zongker | e92f15a | 2011-08-26 13:46:40 -0700 | [diff] [blame] | 819 | if tf.name == "system/build.prop": |
| 820 | deferred_patch_list.append(item) |
| 821 | continue |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 822 | if (sf.name != tf.name): |
| 823 | script.SkipNextActionIfTargetExists(tf.name, tf.sha1) |
| 824 | script.ApplyPatch("/"+sf.name, "-", tf.size, tf.sha1, sf.sha1, "patch/"+sf.name+".p") |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 825 | so_far += tf.size |
| 826 | script.SetProgress(so_far / total_patch_size) |
| 827 | |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 828 | if not OPTIONS.two_step: |
| 829 | if updating_boot: |
| 830 | # Produce the boot image by applying a patch to the current |
| 831 | # contents of the boot partition, and write it back to the |
| 832 | # partition. |
| 833 | script.Print("Patching boot image...") |
| 834 | script.ApplyPatch("%s:%s:%d:%s:%d:%s" |
| 835 | % (boot_type, boot_device, |
| 836 | source_boot.size, source_boot.sha1, |
| 837 | target_boot.size, target_boot.sha1), |
| 838 | "-", |
| 839 | target_boot.size, target_boot.sha1, |
| 840 | source_boot.sha1, "patch/boot.img.p") |
| 841 | so_far += target_boot.size |
| 842 | script.SetProgress(so_far / total_patch_size) |
| 843 | print "boot image changed; including." |
| 844 | else: |
| 845 | print "boot image unchanged; skipping." |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 846 | |
| 847 | if updating_recovery: |
Doug Zongker | b32161a | 2012-08-21 10:33:44 -0700 | [diff] [blame] | 848 | # Recovery is generated as a patch using both the boot image |
| 849 | # (which contains the same linux kernel as recovery) and the file |
| 850 | # /system/etc/recovery-resource.dat (which contains all the images |
| 851 | # used in the recovery UI) as sources. This lets us minimize the |
| 852 | # size of the patch, which must be included in every OTA package. |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 853 | # |
Doug Zongker | b32161a | 2012-08-21 10:33:44 -0700 | [diff] [blame] | 854 | # For older builds where recovery-resource.dat is not present, we |
| 855 | # use only the boot image as the source. |
| 856 | |
| 857 | MakeRecoveryPatch(OPTIONS.target_tmp, output_zip, |
| 858 | target_recovery, target_boot) |
Doug Zongker | 4226539 | 2010-02-12 10:21:00 -0800 | [diff] [blame] | 859 | script.DeleteFiles(["/system/recovery-from-boot.p", |
| 860 | "/system/etc/install-recovery.sh"]) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 861 | print "recovery image changed; including as patch from boot." |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 862 | else: |
| 863 | print "recovery image unchanged; skipping." |
| 864 | |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 865 | script.ShowProgress(0.1, 10) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 866 | |
Doug Zongker | 1807e70 | 2012-02-28 12:21:08 -0800 | [diff] [blame] | 867 | target_symlinks = CopySystemFiles(target_zip, None) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 868 | |
| 869 | 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] | 870 | temp_script = script.MakeTemporary() |
Doug Zongker | 283e2a1 | 2010-03-15 17:52:32 -0700 | [diff] [blame] | 871 | Item.GetMetadata(target_zip) |
Doug Zongker | 73ef825 | 2009-07-23 15:12:53 -0700 | [diff] [blame] | 872 | Item.Get("system").SetPermissions(temp_script) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 873 | |
| 874 | # Note that this call will mess up the tree of Items, so make sure |
| 875 | # we're done with it. |
Doug Zongker | 1807e70 | 2012-02-28 12:21:08 -0800 | [diff] [blame] | 876 | source_symlinks = CopySystemFiles(source_zip, None) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 877 | source_symlinks_d = dict([(i[1], i[0]) for i in source_symlinks]) |
| 878 | |
| 879 | # Delete all the symlinks in source that aren't in target. This |
| 880 | # needs to happen before verbatim files are unpacked, in case a |
| 881 | # symlink in the source is replaced by a real file in the target. |
| 882 | to_delete = [] |
| 883 | for dest, link in source_symlinks: |
| 884 | if link not in target_symlinks_d: |
| 885 | to_delete.append(link) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 886 | script.DeleteFiles(to_delete) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 887 | |
| 888 | if verbatim_targets: |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 889 | script.Print("Unpacking new files...") |
| 890 | script.UnpackPackageDir("system", "/system") |
| 891 | |
Doug Zongker | 4226539 | 2010-02-12 10:21:00 -0800 | [diff] [blame] | 892 | if updating_recovery: |
| 893 | script.Print("Unpacking new recovery...") |
| 894 | script.UnpackPackageDir("recovery", "/system") |
| 895 | |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 896 | if len(renames) > 0: |
| 897 | script.Print("Renaming files...") |
| 898 | |
| 899 | for src in renames: |
| 900 | print "Renaming " + src + " to " + renames[src].name |
| 901 | script.RenameFile(src, renames[src].name) |
| 902 | |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 903 | script.Print("Symlinks and permissions...") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 904 | |
| 905 | # Create all the symlinks that don't already exist, or point to |
| 906 | # somewhere different than what we want. Delete each symlink before |
| 907 | # creating it, since the 'symlink' command won't overwrite. |
| 908 | to_create = [] |
| 909 | for dest, link in target_symlinks: |
| 910 | if link in source_symlinks_d: |
| 911 | if dest != source_symlinks_d[link]: |
| 912 | to_create.append((dest, link)) |
| 913 | else: |
| 914 | to_create.append((dest, link)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 915 | script.DeleteFiles([i[1] for i in to_create]) |
| 916 | script.MakeSymlinks(to_create) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 917 | |
| 918 | # Now that the symlinks are created, we can set all the |
| 919 | # permissions. |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 920 | script.AppendScript(temp_script) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 921 | |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 922 | # Do device-specific installation (eg, write radio image). |
Doug Zongker | 05d3dea | 2009-06-22 11:32:31 -0700 | [diff] [blame] | 923 | device_specific.IncrementalOTA_InstallEnd() |
| 924 | |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 925 | if OPTIONS.extra_script is not None: |
Doug Zongker | 6736998 | 2010-07-07 13:53:32 -0700 | [diff] [blame] | 926 | script.AppendExtra(OPTIONS.extra_script) |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 927 | |
Doug Zongker | e92f15a | 2011-08-26 13:46:40 -0700 | [diff] [blame] | 928 | # Patch the build.prop file last, so if something fails but the |
| 929 | # device can still come up, it appears to be the old build and will |
| 930 | # get set the OTA package again to retry. |
| 931 | script.Print("Patching remaining system files...") |
| 932 | for item in deferred_patch_list: |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 933 | tf, sf, size, _ = item |
| 934 | script.ApplyPatch("/"+sf.name, "-", tf.size, tf.sha1, sf.sha1, "patch/"+sf.name+".p") |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 935 | script.SetPermissions("/system/build.prop", 0, 0, 0644, None, None) |
Doug Zongker | e92f15a | 2011-08-26 13:46:40 -0700 | [diff] [blame] | 936 | |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 937 | if OPTIONS.two_step: |
| 938 | script.AppendExtra(""" |
| 939 | set_stage("%(bcb_dev)s", ""); |
| 940 | endif; |
| 941 | endif; |
| 942 | """ % bcb_dev) |
| 943 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 944 | script.AddToZip(target_zip, output_zip) |
Doug Zongker | 2ea2106 | 2010-04-28 16:05:21 -0700 | [diff] [blame] | 945 | WriteMetadata(metadata, output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 946 | |
| 947 | |
| 948 | def main(argv): |
| 949 | |
| 950 | def option_handler(o, a): |
| 951 | if o in ("-b", "--board_config"): |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 952 | pass # deprecated |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 953 | elif o in ("-k", "--package_key"): |
| 954 | OPTIONS.package_key = a |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 955 | elif o in ("-i", "--incremental_from"): |
| 956 | OPTIONS.incremental_source = a |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 957 | elif o in ("-w", "--wipe_user_data"): |
| 958 | OPTIONS.wipe_user_data = True |
Doug Zongker | 962069c | 2009-04-23 11:41:58 -0700 | [diff] [blame] | 959 | elif o in ("-n", "--no_prereq"): |
| 960 | OPTIONS.omit_prereq = True |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 961 | elif o in ("-e", "--extra_script"): |
| 962 | OPTIONS.extra_script = a |
Hristo Bojinov | dafb042 | 2010-08-26 14:35:16 -0700 | [diff] [blame] | 963 | elif o in ("-a", "--aslr_mode"): |
| 964 | if a in ("on", "On", "true", "True", "yes", "Yes"): |
| 965 | OPTIONS.aslr_mode = True |
| 966 | else: |
| 967 | OPTIONS.aslr_mode = False |
Doug Zongker | 761e642 | 2009-09-25 10:45:39 -0700 | [diff] [blame] | 968 | elif o in ("--worker_threads"): |
| 969 | OPTIONS.worker_threads = int(a) |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 970 | elif o in ("-2", "--two_step"): |
| 971 | OPTIONS.two_step = True |
Takeshi Kanemoto | e153b34 | 2013-11-14 17:20:50 +0900 | [diff] [blame] | 972 | elif o in ("--no_signing"): |
| 973 | OPTIONS.no_signing = True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 974 | else: |
| 975 | return False |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 976 | return True |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 977 | |
| 978 | args = common.ParseOptions(argv, __doc__, |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 979 | extra_opts="b:k:i:d:wne:a:2", |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 980 | extra_long_opts=["board_config=", |
| 981 | "package_key=", |
Doug Zongker | dbfaae5 | 2009-04-21 17:12:54 -0700 | [diff] [blame] | 982 | "incremental_from=", |
Doug Zongker | 962069c | 2009-04-23 11:41:58 -0700 | [diff] [blame] | 983 | "wipe_user_data", |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 984 | "no_prereq", |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 985 | "extra_script=", |
Hristo Bojinov | 96be720 | 2010-08-02 10:26:17 -0700 | [diff] [blame] | 986 | "worker_threads=", |
Doug Zongker | c60c1ba | 2010-09-03 13:22:38 -0700 | [diff] [blame] | 987 | "aslr_mode=", |
Doug Zongker | 9b23f2c | 2013-11-25 14:44:12 -0800 | [diff] [blame] | 988 | "two_step", |
Takeshi Kanemoto | e153b34 | 2013-11-14 17:20:50 +0900 | [diff] [blame] | 989 | "no_signing", |
Doug Zongker | c60c1ba | 2010-09-03 13:22:38 -0700 | [diff] [blame] | 990 | ], |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 991 | extra_option_handler=option_handler) |
| 992 | |
| 993 | if len(args) != 2: |
| 994 | common.Usage(__doc__) |
| 995 | sys.exit(1) |
| 996 | |
Doug Zongker | 1c390a2 | 2009-05-14 19:06:36 -0700 | [diff] [blame] | 997 | if OPTIONS.extra_script is not None: |
| 998 | OPTIONS.extra_script = open(OPTIONS.extra_script).read() |
| 999 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1000 | print "unzipping target target-files..." |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 1001 | OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0]) |
Doug Zongker | fdd8e69 | 2009-08-03 17:27:48 -0700 | [diff] [blame] | 1002 | |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1003 | OPTIONS.target_tmp = OPTIONS.input_tmp |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1004 | OPTIONS.info_dict = common.LoadInfoDict(input_zip) |
Kenny Root | e2e9f61 | 2013-05-29 12:59:35 -0700 | [diff] [blame] | 1005 | |
| 1006 | # If this image was originally labelled with SELinux contexts, make sure we |
| 1007 | # also apply the labels in our new image. During building, the "file_contexts" |
| 1008 | # is in the out/ directory tree, but for repacking from target-files.zip it's |
| 1009 | # in the root directory of the ramdisk. |
| 1010 | if "selinux_fc" in OPTIONS.info_dict: |
| 1011 | OPTIONS.info_dict["selinux_fc"] = os.path.join(OPTIONS.input_tmp, "BOOT", "RAMDISK", |
| 1012 | "file_contexts") |
| 1013 | |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1014 | if OPTIONS.verbose: |
| 1015 | print "--- target info ---" |
| 1016 | common.DumpInfoDict(OPTIONS.info_dict) |
| 1017 | |
Doug Zongker | eb0a78a | 2014-01-27 10:01:06 -0800 | [diff] [blame] | 1018 | # If the caller explicitly specified the device-specific extensions |
| 1019 | # path via -s/--device_specific, use that. Otherwise, use |
| 1020 | # META/releasetools.py if it is present in the target target_files. |
| 1021 | # Otherwise, take the path of the file from 'tool_extensions' in the |
| 1022 | # info dict and look for that in the local filesystem, relative to |
| 1023 | # the current directory. |
| 1024 | |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1025 | if OPTIONS.device_specific is None: |
Doug Zongker | eb0a78a | 2014-01-27 10:01:06 -0800 | [diff] [blame] | 1026 | from_input = os.path.join(OPTIONS.input_tmp, "META", "releasetools.py") |
| 1027 | if os.path.exists(from_input): |
| 1028 | print "(using device-specific extensions from target_files)" |
| 1029 | OPTIONS.device_specific = from_input |
| 1030 | else: |
| 1031 | OPTIONS.device_specific = OPTIONS.info_dict.get("tool_extensions", None) |
| 1032 | |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1033 | if OPTIONS.device_specific is not None: |
Doug Zongker | eb0a78a | 2014-01-27 10:01:06 -0800 | [diff] [blame] | 1034 | OPTIONS.device_specific = os.path.abspath(OPTIONS.device_specific) |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1035 | |
Takeshi Kanemoto | e153b34 | 2013-11-14 17:20:50 +0900 | [diff] [blame] | 1036 | if OPTIONS.no_signing: |
| 1037 | output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED) |
| 1038 | else: |
| 1039 | temp_zip_file = tempfile.NamedTemporaryFile() |
| 1040 | output_zip = zipfile.ZipFile(temp_zip_file, "w", |
| 1041 | compression=zipfile.ZIP_DEFLATED) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1042 | |
| 1043 | if OPTIONS.incremental_source is None: |
Doug Zongker | c77a9ad | 2010-09-16 11:28:43 -0700 | [diff] [blame] | 1044 | WriteFullOTAPackage(input_zip, output_zip) |
Doug Zongker | afb32ea | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 1045 | if OPTIONS.package_key is None: |
| 1046 | OPTIONS.package_key = OPTIONS.info_dict.get( |
| 1047 | "default_system_dev_certificate", |
| 1048 | "build/target/product/security/testkey") |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1049 | else: |
| 1050 | print "unzipping source target-files..." |
Doug Zongker | 55d9328 | 2011-01-25 17:03:34 -0800 | [diff] [blame] | 1051 | OPTIONS.source_tmp, source_zip = common.UnzipTemp(OPTIONS.incremental_source) |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1052 | OPTIONS.target_info_dict = OPTIONS.info_dict |
| 1053 | OPTIONS.source_info_dict = common.LoadInfoDict(source_zip) |
Doug Zongker | afb32ea | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 1054 | if OPTIONS.package_key is None: |
Doug Zongker | 91b4f8a | 2011-09-23 12:48:33 -0700 | [diff] [blame] | 1055 | OPTIONS.package_key = OPTIONS.source_info_dict.get( |
Doug Zongker | afb32ea | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 1056 | "default_system_dev_certificate", |
| 1057 | "build/target/product/security/testkey") |
Doug Zongker | 3797473 | 2010-09-16 17:44:38 -0700 | [diff] [blame] | 1058 | if OPTIONS.verbose: |
| 1059 | print "--- source info ---" |
| 1060 | common.DumpInfoDict(OPTIONS.source_info_dict) |
Doug Zongker | c77a9ad | 2010-09-16 11:28:43 -0700 | [diff] [blame] | 1061 | WriteIncrementalOTAPackage(input_zip, source_zip, output_zip) |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1062 | |
| 1063 | output_zip.close() |
Doug Zongker | afb32ea | 2011-09-22 10:28:04 -0700 | [diff] [blame] | 1064 | |
Takeshi Kanemoto | e153b34 | 2013-11-14 17:20:50 +0900 | [diff] [blame] | 1065 | if not OPTIONS.no_signing: |
| 1066 | SignOutput(temp_zip_file.name, args[1]) |
| 1067 | temp_zip_file.close() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1068 | |
| 1069 | common.Cleanup() |
| 1070 | |
| 1071 | print "done." |
| 1072 | |
| 1073 | |
| 1074 | if __name__ == '__main__': |
| 1075 | try: |
Ying Wang | 7e6d4e4 | 2010-12-13 16:25:36 -0800 | [diff] [blame] | 1076 | common.CloseInheritedPipes() |
Doug Zongker | eef3944 | 2009-04-02 12:14:19 -0700 | [diff] [blame] | 1077 | main(sys.argv[1:]) |
| 1078 | except common.ExternalError, e: |
| 1079 | print |
| 1080 | print " ERROR: %s" % (e,) |
| 1081 | print |
| 1082 | sys.exit(1) |