Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 1 | # Copyright (C) 2009 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 15 | import re |
| 16 | |
| 17 | import common |
| 18 | |
| 19 | class EdifyGenerator(object): |
| 20 | """Class to generate scripts in the 'edify' recovery script language |
| 21 | used from donut onwards.""" |
| 22 | |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 23 | def __init__(self, version, info, fstab=None): |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 24 | self.script = [] |
| 25 | self.mounts = set() |
Tao Bao | d8d14be | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 26 | self._required_cache = 0 |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 27 | self.version = version |
Doug Zongker | b4c7d32 | 2010-07-01 15:30:11 -0700 | [diff] [blame] | 28 | self.info = info |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 29 | if fstab is None: |
| 30 | self.fstab = self.info.get("fstab", None) |
| 31 | else: |
| 32 | self.fstab = fstab |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 33 | |
| 34 | def MakeTemporary(self): |
| 35 | """Make a temporary script object whose commands can latter be |
| 36 | appended to the parent script with AppendScript(). Used when the |
| 37 | caller wants to generate script commands out-of-order.""" |
Doug Zongker | 6736998 | 2010-07-07 13:53:32 -0700 | [diff] [blame] | 38 | x = EdifyGenerator(self.version, self.info) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 39 | x.mounts = self.mounts |
| 40 | return x |
| 41 | |
Tao Bao | d8d14be | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 42 | @property |
| 43 | def required_cache(self): |
| 44 | """Return the minimum cache size to apply the update.""" |
| 45 | return self._required_cache |
| 46 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 47 | @staticmethod |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 48 | def WordWrap(cmd, linelen=80): |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 49 | """'cmd' should be a function call with null characters after each |
| 50 | parameter (eg, "somefun(foo,\0bar,\0baz)"). This function wraps cmd |
| 51 | to a given line length, replacing nulls with spaces and/or newlines |
| 52 | to format it nicely.""" |
| 53 | indent = cmd.index("(")+1 |
| 54 | out = [] |
| 55 | first = True |
| 56 | x = re.compile("^(.{,%d})\0" % (linelen-indent,)) |
| 57 | while True: |
| 58 | if not first: |
| 59 | out.append(" " * indent) |
| 60 | first = False |
| 61 | m = x.search(cmd) |
| 62 | if not m: |
| 63 | parts = cmd.split("\0", 1) |
| 64 | out.append(parts[0]+"\n") |
| 65 | if len(parts) == 1: |
| 66 | break |
| 67 | else: |
| 68 | cmd = parts[1] |
| 69 | continue |
| 70 | out.append(m.group(1)+"\n") |
| 71 | cmd = cmd[m.end():] |
| 72 | |
| 73 | return "".join(out).replace("\0", " ").rstrip("\n") |
| 74 | |
| 75 | def AppendScript(self, other): |
| 76 | """Append the contents of another script (which should be created |
| 77 | with temporary=True) to this one.""" |
| 78 | self.script.extend(other.script) |
| 79 | |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame^] | 80 | def AssertOemProperty(self, name, values): |
| 81 | """Assert that a property on the OEM paritition matches allowed values.""" |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 82 | if not name: |
| 83 | raise ValueError("must specify an OEM property") |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame^] | 84 | if not values: |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 85 | raise ValueError("must specify the OEM value") |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame^] | 86 | get_prop_command = None |
Tao Bao | 8608cde | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 87 | if common.OPTIONS.oem_no_mount: |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame^] | 88 | get_prop_command = 'getprop("%s")' % name |
Tao Bao | 8608cde | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 89 | else: |
Alain Vongsouvanh | 7f804ba | 2017-02-16 13:06:55 -0800 | [diff] [blame^] | 90 | get_prop_command = 'file_getprop("/oem/oem.prop", "%s")' % name |
| 91 | |
| 92 | cmd = '' |
| 93 | for value in values: |
| 94 | cmd += '%s == "%s" || ' % (get_prop_command, value) |
| 95 | cmd += ( |
| 96 | 'abort("E{code}: This package expects the value \\"{values}\\" for ' |
| 97 | '\\"{name}\\"; this has value \\"" + ' |
| 98 | '{get_prop_command} + "\\".");').format( |
| 99 | code=common.ErrorCode.OEM_PROP_MISMATCH, |
| 100 | get_prop_command=get_prop_command, name=name, |
| 101 | values='\\" or \\"'.join(values)) |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 102 | self.script.append(cmd) |
| 103 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 104 | def AssertSomeFingerprint(self, *fp): |
Doug Zongker | af84525 | 2014-05-09 08:29:05 -0700 | [diff] [blame] | 105 | """Assert that the current recovery build fingerprint is one of *fp.""" |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 106 | if not fp: |
| 107 | raise ValueError("must specify some fingerprints") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 108 | cmd = (' ||\n '.join([('getprop("ro.build.fingerprint") == "%s"') % i |
| 109 | for i in fp]) + |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 110 | ' ||\n abort("E%d: Package expects build fingerprint of %s; ' |
| 111 | 'this device has " + getprop("ro.build.fingerprint") + ".");') % ( |
| 112 | common.ErrorCode.FINGERPRINT_MISMATCH, " or ".join(fp)) |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 113 | self.script.append(cmd) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 114 | |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 115 | def AssertSomeThumbprint(self, *fp): |
Doug Zongker | af84525 | 2014-05-09 08:29:05 -0700 | [diff] [blame] | 116 | """Assert that the current recovery build thumbprint is one of *fp.""" |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 117 | if not fp: |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 118 | raise ValueError("must specify some thumbprints") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 119 | cmd = (' ||\n '.join([('getprop("ro.build.thumbprint") == "%s"') % i |
| 120 | for i in fp]) + |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 121 | ' ||\n abort("E%d: Package expects build thumbprint of %s; this ' |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 122 | 'device has " + getprop("ro.build.thumbprint") + ".");') % ( |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 123 | common.ErrorCode.THUMBPRINT_MISMATCH, " or ".join(fp)) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 124 | self.script.append(cmd) |
| 125 | |
Tao Bao | 3e30d97 | 2016-03-15 13:20:19 -0700 | [diff] [blame] | 126 | def AssertFingerprintOrThumbprint(self, fp, tp): |
| 127 | """Assert that the current recovery build fingerprint is fp, or thumbprint |
| 128 | is tp.""" |
| 129 | cmd = ('getprop("ro.build.fingerprint") == "{fp}" ||\n' |
| 130 | ' getprop("ro.build.thumbprint") == "{tp}" ||\n' |
| 131 | ' abort("Package expects build fingerprint of {fp} or ' |
| 132 | 'thumbprint of {tp}; this device has a fingerprint of " ' |
| 133 | '+ getprop("ro.build.fingerprint") and a thumbprint of " ' |
| 134 | '+ getprop("ro.build.thumbprint") + ".");').format(fp=fp, tp=tp) |
| 135 | self.script.append(cmd) |
| 136 | |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 137 | def AssertOlderBuild(self, timestamp, timestamp_text): |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 138 | """Assert that the build on the device is older (or the same as) |
| 139 | the given timestamp.""" |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 140 | self.script.append( |
| 141 | ('(!less_than_int(%s, getprop("ro.build.date.utc"))) || ' |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 142 | 'abort("E%d: Can\'t install this package (%s) over newer ' |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 143 | 'build (" + getprop("ro.build.date") + ").");') % (timestamp, |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 144 | common.ErrorCode.OLDER_BUILD, timestamp_text)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 145 | |
| 146 | def AssertDevice(self, device): |
| 147 | """Assert that the device identifier is the given string.""" |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 148 | cmd = ('getprop("ro.product.device") == "%s" || ' |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 149 | 'abort("E%d: This package is for \\"%s\\" devices; ' |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 150 | 'this is a \\"" + getprop("ro.product.device") + "\\".");') % ( |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 151 | device, common.ErrorCode.DEVICE_MISMATCH, device) |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 152 | self.script.append(cmd) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 153 | |
| 154 | def AssertSomeBootloader(self, *bootloaders): |
| 155 | """Asert that the bootloader version is one of *bootloaders.""" |
| 156 | cmd = ("assert(" + |
| 157 | " ||\0".join(['getprop("ro.bootloader") == "%s"' % (b,) |
| 158 | for b in bootloaders]) + |
| 159 | ");") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 160 | self.script.append(self.WordWrap(cmd)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 161 | |
| 162 | def ShowProgress(self, frac, dur): |
| 163 | """Update the progress bar, advancing it over 'frac' over the next |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 164 | 'dur' seconds. 'dur' may be zero to advance it via SetProgress |
| 165 | commands instead of by time.""" |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 166 | self.script.append("show_progress(%f, %d);" % (frac, int(dur))) |
| 167 | |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 168 | def SetProgress(self, frac): |
| 169 | """Set the position of the progress bar within the chunk defined |
| 170 | by the most recent ShowProgress call. 'frac' should be in |
| 171 | [0,1].""" |
| 172 | self.script.append("set_progress(%f);" % (frac,)) |
| 173 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 174 | def PatchCheck(self, filename, *sha1): |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 175 | """Check that the given file has one of the |
Doug Zongker | c8d446b | 2010-02-22 15:41:53 -0800 | [diff] [blame] | 176 | given *sha1 hashes, checking the version saved in cache if the |
| 177 | file does not match.""" |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 178 | self.script.append( |
| 179 | 'apply_patch_check("%s"' % (filename,) + |
| 180 | "".join([', "%s"' % (i,) for i in sha1]) + |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 181 | ') || abort("E%d: \\"%s\\" has unexpected contents.");' % ( |
| 182 | common.ErrorCode.BAD_PATCH_FILE, filename)) |
Doug Zongker | c8d446b | 2010-02-22 15:41:53 -0800 | [diff] [blame] | 183 | |
Tao Bao | 9bc6bb2 | 2015-11-09 16:58:28 -0800 | [diff] [blame] | 184 | def Verify(self, filename): |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 185 | """Check that the given file has one of the |
Tao Bao | 9bc6bb2 | 2015-11-09 16:58:28 -0800 | [diff] [blame] | 186 | given hashes (encoded in the filename).""" |
| 187 | self.script.append( |
| 188 | 'apply_patch_check("{filename}") && ' |
| 189 | 'ui_print(" Verified.") || ' |
| 190 | 'ui_print("\\"{filename}\\" has unexpected contents.");'.format( |
| 191 | filename=filename)) |
| 192 | |
Doug Zongker | c8d446b | 2010-02-22 15:41:53 -0800 | [diff] [blame] | 193 | def FileCheck(self, filename, *sha1): |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 194 | """Check that the given file has one of the |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 195 | given *sha1 hashes.""" |
Doug Zongker | 5a48209 | 2010-02-17 16:09:18 -0800 | [diff] [blame] | 196 | self.script.append('assert(sha1_check(read_file("%s")' % (filename,) + |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 197 | "".join([', "%s"' % (i,) for i in sha1]) + |
| 198 | '));') |
| 199 | |
| 200 | def CacheFreeSpaceCheck(self, amount): |
| 201 | """Check that there's at least 'amount' space that can be made |
| 202 | available on /cache.""" |
Tao Bao | d8d14be | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 203 | self._required_cache = max(self._required_cache, amount) |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 204 | self.script.append(('apply_patch_space(%d) || abort("E%d: Not enough free ' |
| 205 | 'space on /cache to apply patches.");') % ( |
| 206 | amount, |
| 207 | common.ErrorCode.INSUFFICIENT_CACHE_SPACE)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 208 | |
Michael Runge | 7cd99ba | 2014-10-22 17:21:48 -0700 | [diff] [blame] | 209 | def Mount(self, mount_point, mount_options_by_format=""): |
| 210 | """Mount the partition with the given mount_point. |
| 211 | mount_options_by_format: |
| 212 | [fs_type=option[,option]...[|fs_type=option[,option]...]...] |
| 213 | where option is optname[=optvalue] |
| 214 | E.g. ext4=barrier=1,nodelalloc,errors=panic|f2fs=errors=recover |
| 215 | """ |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 216 | fstab = self.fstab |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 217 | if fstab: |
| 218 | p = fstab[mount_point] |
Michael Runge | 7cd99ba | 2014-10-22 17:21:48 -0700 | [diff] [blame] | 219 | mount_dict = {} |
| 220 | if mount_options_by_format is not None: |
| 221 | for option in mount_options_by_format.split("|"): |
| 222 | if "=" in option: |
| 223 | key, value = option.split("=", 1) |
| 224 | mount_dict[key] = value |
Tao Bao | df06e96 | 2015-06-10 12:32:41 -0700 | [diff] [blame] | 225 | mount_flags = mount_dict.get(p.fs_type, "") |
| 226 | if p.context is not None: |
| 227 | mount_flags = p.context + ("," + mount_flags if mount_flags else "") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 228 | self.script.append('mount("%s", "%s", "%s", "%s", "%s");' % ( |
| 229 | p.fs_type, common.PARTITION_TYPES[p.fs_type], p.device, |
Tao Bao | df06e96 | 2015-06-10 12:32:41 -0700 | [diff] [blame] | 230 | p.mount_point, mount_flags)) |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 231 | self.mounts.add(p.mount_point) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 232 | |
| 233 | def UnpackPackageDir(self, src, dst): |
| 234 | """Unpack a given directory from the OTA package into the given |
| 235 | destination directory.""" |
| 236 | self.script.append('package_extract_dir("%s", "%s");' % (src, dst)) |
| 237 | |
| 238 | def Comment(self, comment): |
| 239 | """Write a comment into the update script.""" |
| 240 | self.script.append("") |
| 241 | for i in comment.split("\n"): |
| 242 | self.script.append("# " + i) |
| 243 | self.script.append("") |
| 244 | |
| 245 | def Print(self, message): |
| 246 | """Log a message to the screen (if the logs are visible).""" |
| 247 | self.script.append('ui_print("%s");' % (message,)) |
| 248 | |
Michael Runge | 3e28664 | 2014-11-21 00:46:03 -0800 | [diff] [blame] | 249 | def TunePartition(self, partition, *options): |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 250 | fstab = self.fstab |
Michael Runge | 3e28664 | 2014-11-21 00:46:03 -0800 | [diff] [blame] | 251 | if fstab: |
| 252 | p = fstab[partition] |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 253 | if p.fs_type not in ("ext2", "ext3", "ext4"): |
Michael Runge | 3e28664 | 2014-11-21 00:46:03 -0800 | [diff] [blame] | 254 | raise ValueError("Partition %s cannot be tuned\n" % (partition,)) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 255 | self.script.append( |
| 256 | 'tune2fs(' + "".join(['"%s", ' % (i,) for i in options]) + |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 257 | '"%s") || abort("E%d: Failed to tune partition %s");' % ( |
| 258 | p.device, common.ErrorCode.TUNE_PARTITION_FAILURE, partition)) |
Michael Runge | 3e28664 | 2014-11-21 00:46:03 -0800 | [diff] [blame] | 259 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 260 | def FormatPartition(self, partition): |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 261 | """Format the given partition, specified by its mount point (eg, |
| 262 | "/system").""" |
| 263 | |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 264 | fstab = self.fstab |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 265 | if fstab: |
| 266 | p = fstab[partition] |
Doug Zongker | df2056e | 2012-04-09 12:27:43 -0700 | [diff] [blame] | 267 | self.script.append('format("%s", "%s", "%s", "%s", "%s");' % |
Doug Zongker | 086cbb0 | 2011-02-17 15:54:20 -0800 | [diff] [blame] | 268 | (p.fs_type, common.PARTITION_TYPES[p.fs_type], |
Doug Zongker | df2056e | 2012-04-09 12:27:43 -0700 | [diff] [blame] | 269 | p.device, p.length, p.mount_point)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 270 | |
Doug Zongker | 5fad203 | 2014-02-24 08:13:45 -0800 | [diff] [blame] | 271 | def WipeBlockDevice(self, partition): |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 272 | if partition not in ("/system", "/vendor"): |
| 273 | raise ValueError(("WipeBlockDevice doesn't work on %s\n") % (partition,)) |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 274 | fstab = self.fstab |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 275 | size = self.info.get(partition.lstrip("/") + "_size", None) |
Doug Zongker | 5fad203 | 2014-02-24 08:13:45 -0800 | [diff] [blame] | 276 | device = fstab[partition].device |
| 277 | |
| 278 | self.script.append('wipe_block_device("%s", %s);' % (device, size)) |
| 279 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 280 | def DeleteFiles(self, file_list): |
| 281 | """Delete all files in file_list.""" |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 282 | if not file_list: |
| 283 | return |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 284 | cmd = "delete(" + ",\0".join(['"%s"' % (i,) for i in file_list]) + ");" |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 285 | self.script.append(self.WordWrap(cmd)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 286 | |
Tao Bao | 84006ea | 2015-09-02 10:28:08 -0700 | [diff] [blame] | 287 | def DeleteFilesIfNotMatching(self, file_list): |
| 288 | """Delete the file in file_list if not matching the checksum.""" |
| 289 | if not file_list: |
| 290 | return |
| 291 | for name, sha1 in file_list: |
| 292 | cmd = ('sha1_check(read_file("{name}"), "{sha1}") || ' |
| 293 | 'delete("{name}");'.format(name=name, sha1=sha1)) |
| 294 | self.script.append(self.WordWrap(cmd)) |
| 295 | |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 296 | def RenameFile(self, srcfile, tgtfile): |
| 297 | """Moves a file from one location to another.""" |
| 298 | if self.info.get("update_rename_support", False): |
| 299 | self.script.append('rename("%s", "%s");' % (srcfile, tgtfile)) |
| 300 | else: |
| 301 | raise ValueError("Rename not supported by update binary") |
| 302 | |
| 303 | def SkipNextActionIfTargetExists(self, tgtfile, tgtsha1): |
| 304 | """Prepend an action with an apply_patch_check in order to |
| 305 | skip the action if the file exists. Used when a patch |
| 306 | is later renamed.""" |
Tao Bao | 84006ea | 2015-09-02 10:28:08 -0700 | [diff] [blame] | 307 | cmd = ('sha1_check(read_file("%s"), %s) ||' % (tgtfile, tgtsha1)) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 308 | self.script.append(self.WordWrap(cmd)) |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 309 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 310 | def ApplyPatch(self, srcfile, tgtfile, tgtsize, tgtsha1, *patchpairs): |
| 311 | """Apply binary patches (in *patchpairs) to the given srcfile to |
| 312 | produce tgtfile (which may be "-" to indicate overwriting the |
| 313 | source file.""" |
| 314 | if len(patchpairs) % 2 != 0 or len(patchpairs) == 0: |
| 315 | raise ValueError("bad patches given to ApplyPatch") |
| 316 | cmd = ['apply_patch("%s",\0"%s",\0%s,\0%d' |
| 317 | % (srcfile, tgtfile, tgtsha1, tgtsize)] |
| 318 | for i in range(0, len(patchpairs), 2): |
Tao Bao | c386890 | 2015-12-01 17:46:46 -0800 | [diff] [blame] | 319 | cmd.append(',\0%s,\0package_extract_file("%s")' % patchpairs[i:i+2]) |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 320 | cmd.append(') ||\n abort("E%d: Failed to apply patch to %s");' % ( |
| 321 | common.ErrorCode.APPLY_PATCH_FAILURE, srcfile)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 322 | cmd = "".join(cmd) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 323 | self.script.append(self.WordWrap(cmd)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 324 | |
Doug Zongker | 5fad203 | 2014-02-24 08:13:45 -0800 | [diff] [blame] | 325 | def WriteRawImage(self, mount_point, fn, mapfn=None): |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 326 | """Write the given package file into the partition for the given |
| 327 | mount point.""" |
Doug Zongker | b4c7d32 | 2010-07-01 15:30:11 -0700 | [diff] [blame] | 328 | |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 329 | fstab = self.fstab |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 330 | if fstab: |
| 331 | p = fstab[mount_point] |
Doug Zongker | 96a57e7 | 2010-09-26 14:57:41 -0700 | [diff] [blame] | 332 | partition_type = common.PARTITION_TYPES[p.fs_type] |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 333 | args = {'device': p.device, 'fn': fn} |
Elliott Hughes | 305b088 | 2016-06-15 17:04:54 -0700 | [diff] [blame] | 334 | if partition_type == "EMMC": |
Doug Zongker | 5fad203 | 2014-02-24 08:13:45 -0800 | [diff] [blame] | 335 | if mapfn: |
| 336 | args["map"] = mapfn |
| 337 | self.script.append( |
| 338 | 'package_extract_file("%(fn)s", "%(device)s", "%(map)s");' % args) |
| 339 | else: |
| 340 | self.script.append( |
| 341 | 'package_extract_file("%(fn)s", "%(device)s");' % args) |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 342 | else: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 343 | raise ValueError( |
| 344 | "don't know how to write \"%s\" partitions" % p.fs_type) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 345 | |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 346 | def SetPermissions(self, fn, uid, gid, mode, selabel, capabilities): |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 347 | """Set file ownership and permissions.""" |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 348 | if not self.info.get("use_set_metadata", False): |
| 349 | self.script.append('set_perm(%d, %d, 0%o, "%s");' % (uid, gid, mode, fn)) |
| 350 | else: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 351 | if capabilities is None: |
| 352 | capabilities = "0x0" |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 353 | cmd = 'set_metadata("%s", "uid", %d, "gid", %d, "mode", 0%o, ' \ |
| 354 | '"capabilities", %s' % (fn, uid, gid, mode, capabilities) |
| 355 | if selabel is not None: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 356 | cmd += ', "selabel", "%s"' % selabel |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 357 | cmd += ');' |
| 358 | self.script.append(cmd) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 359 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 360 | def SetPermissionsRecursive(self, fn, uid, gid, dmode, fmode, selabel, |
| 361 | capabilities): |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 362 | """Recursively set path ownership and permissions.""" |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 363 | if not self.info.get("use_set_metadata", False): |
| 364 | self.script.append('set_perm_recursive(%d, %d, 0%o, 0%o, "%s");' |
| 365 | % (uid, gid, dmode, fmode, fn)) |
| 366 | else: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 367 | if capabilities is None: |
| 368 | capabilities = "0x0" |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 369 | cmd = 'set_metadata_recursive("%s", "uid", %d, "gid", %d, ' \ |
| 370 | '"dmode", 0%o, "fmode", 0%o, "capabilities", %s' \ |
| 371 | % (fn, uid, gid, dmode, fmode, capabilities) |
| 372 | if selabel is not None: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 373 | cmd += ', "selabel", "%s"' % selabel |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 374 | cmd += ');' |
| 375 | self.script.append(cmd) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 376 | |
| 377 | def MakeSymlinks(self, symlink_list): |
| 378 | """Create symlinks, given a list of (dest, link) pairs.""" |
| 379 | by_dest = {} |
| 380 | for d, l in symlink_list: |
| 381 | by_dest.setdefault(d, []).append(l) |
| 382 | |
| 383 | for dest, links in sorted(by_dest.iteritems()): |
| 384 | cmd = ('symlink("%s", ' % (dest,) + |
| 385 | ",\0".join(['"' + i + '"' for i in sorted(links)]) + ");") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 386 | self.script.append(self.WordWrap(cmd)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 387 | |
| 388 | def AppendExtra(self, extra): |
| 389 | """Append text verbatim to the output script.""" |
| 390 | self.script.append(extra) |
| 391 | |
Michael Runge | 63f01de | 2014-10-28 19:24:19 -0700 | [diff] [blame] | 392 | def Unmount(self, mount_point): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 393 | self.script.append('unmount("%s");' % mount_point) |
| 394 | self.mounts.remove(mount_point) |
Michael Runge | 63f01de | 2014-10-28 19:24:19 -0700 | [diff] [blame] | 395 | |
Doug Zongker | 1483360 | 2010-02-02 13:12:04 -0800 | [diff] [blame] | 396 | def UnmountAll(self): |
| 397 | for p in sorted(self.mounts): |
| 398 | self.script.append('unmount("%s");' % (p,)) |
| 399 | self.mounts = set() |
| 400 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 401 | def AddToZip(self, input_zip, output_zip, input_path=None): |
| 402 | """Write the accumulated script to the output_zip file. input_zip |
| 403 | is used as the source for the 'updater' binary needed to run |
| 404 | script. If input_path is not None, it will be used as a local |
| 405 | path for the binary instead of input_zip.""" |
| 406 | |
Doug Zongker | 1483360 | 2010-02-02 13:12:04 -0800 | [diff] [blame] | 407 | self.UnmountAll() |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 408 | |
| 409 | common.ZipWriteStr(output_zip, "META-INF/com/google/android/updater-script", |
| 410 | "\n".join(self.script) + "\n") |
| 411 | |
| 412 | if input_path is None: |
| 413 | data = input_zip.read("OTA/bin/updater") |
| 414 | else: |
Doug Zongker | 2556848 | 2014-03-03 10:21:27 -0800 | [diff] [blame] | 415 | data = open(input_path, "rb").read() |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 416 | common.ZipWriteStr(output_zip, "META-INF/com/google/android/update-binary", |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 417 | data, perms=0o755) |