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 | b4cfca5 | 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 | b4cfca5 | 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 | |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 80 | def AssertOemProperty(self, name, value): |
| 81 | """Assert that a property on the OEM paritition matches a value.""" |
| 82 | if not name: |
| 83 | raise ValueError("must specify an OEM property") |
| 84 | if not value: |
| 85 | raise ValueError("must specify the OEM value") |
Tao Bao | df4cb0b | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 86 | if common.OPTIONS.oem_no_mount: |
| 87 | cmd = ('getprop("{name}") == "{value}" || ' |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 88 | 'abort("E{code}: This package expects the value \\"{value}\\" for ' |
Tao Bao | df4cb0b | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 89 | '\\"{name}\\"; this has value \\"" + ' |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 90 | 'getprop("{name}") + "\\".");').format( |
| 91 | code=common.ErrorCode.OEM_PROP_MISMATCH, |
| 92 | name=name, value=value) |
Tao Bao | df4cb0b | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 93 | else: |
| 94 | cmd = ('file_getprop("/oem/oem.prop", "{name}") == "{value}" || ' |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 95 | 'abort("E{code}: This package expects the value \\"{value}\\" for ' |
Tao Bao | df4cb0b | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 96 | '\\"{name}\\" on the OEM partition; this has value \\"" + ' |
| 97 | 'file_getprop("/oem/oem.prop", "{name}") + "\\".");').format( |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 98 | code=common.ErrorCode.OEM_PROP_MISMATCH, |
Tao Bao | df4cb0b | 2016-02-25 19:49:55 -0800 | [diff] [blame] | 99 | name=name, value=value) |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 100 | self.script.append(cmd) |
| 101 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 102 | def AssertSomeFingerprint(self, *fp): |
Doug Zongker | af84525 | 2014-05-09 08:29:05 -0700 | [diff] [blame] | 103 | """Assert that the current recovery build fingerprint is one of *fp.""" |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 104 | if not fp: |
| 105 | raise ValueError("must specify some fingerprints") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 106 | cmd = (' ||\n '.join([('getprop("ro.build.fingerprint") == "%s"') % i |
| 107 | for i in fp]) + |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 108 | ' ||\n abort("E%d: Package expects build fingerprint of %s; ' |
| 109 | 'this device has " + getprop("ro.build.fingerprint") + ".");') % ( |
| 110 | common.ErrorCode.FINGERPRINT_MISMATCH, " or ".join(fp)) |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 111 | self.script.append(cmd) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 112 | |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 113 | def AssertSomeThumbprint(self, *fp): |
Doug Zongker | af84525 | 2014-05-09 08:29:05 -0700 | [diff] [blame] | 114 | """Assert that the current recovery build thumbprint is one of *fp.""" |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 115 | if not fp: |
Michael Runge | 6e83611 | 2014-04-15 17:40:21 -0700 | [diff] [blame] | 116 | raise ValueError("must specify some thumbprints") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 117 | cmd = (' ||\n '.join([('getprop("ro.build.thumbprint") == "%s"') % i |
| 118 | for i in fp]) + |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 119 | ' ||\n abort("E%d: Package expects build thumbprint of %s; this ' |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 120 | 'device has " + getprop("ro.build.thumbprint") + ".");') % ( |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 121 | common.ErrorCode.THUMBPRINT_MISMATCH, " or ".join(fp)) |
Geremy Condra | 36bd365 | 2014-02-06 19:45:10 -0800 | [diff] [blame] | 122 | self.script.append(cmd) |
| 123 | |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 124 | def AssertOlderBuild(self, timestamp, timestamp_text): |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 125 | """Assert that the build on the device is older (or the same as) |
| 126 | the given timestamp.""" |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 127 | self.script.append( |
| 128 | ('(!less_than_int(%s, getprop("ro.build.date.utc"))) || ' |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 129 | 'abort("E%d: Can\'t install this package (%s) over newer ' |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 130 | 'build (" + getprop("ro.build.date") + ").");') % (timestamp, |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 131 | common.ErrorCode.OLDER_BUILD, timestamp_text)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 132 | |
| 133 | def AssertDevice(self, device): |
| 134 | """Assert that the device identifier is the given string.""" |
Steve Kondik | e848e4d | 2010-04-21 11:39:48 -0400 | [diff] [blame] | 135 | cmd = ('assert(' + |
Ricardo Cerqueira | 40ef609 | 2016-09-08 00:13:56 +0100 | [diff] [blame] | 136 | ' || '.join(['getprop("ro.product.device") == "%s" || getprop("ro.build.product") == "%s"' |
| 137 | % (i, i) for i in device.split(",")]) + |
Steve Kondik | e848e4d | 2010-04-21 11:39:48 -0400 | [diff] [blame] | 138 | ' || abort("E%d: This package is for device: %s; ' + |
| 139 | 'this device is " + getprop("ro.product.device") + ".");' + |
| 140 | ');') % (common.ErrorCode.DEVICE_MISMATCH, device) |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 141 | self.script.append(cmd) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 142 | |
| 143 | def AssertSomeBootloader(self, *bootloaders): |
Matt Mower | 6ced9b2 | 2014-08-28 18:29:14 -0500 | [diff] [blame] | 144 | """Assert that the bootloader version is one of *bootloaders.""" |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 145 | cmd = ("assert(" + |
Ricardo Cerqueira | eb15b11 | 2014-11-08 23:03:53 +0000 | [diff] [blame] | 146 | " || ".join(['getprop("ro.bootloader") == "%s"' % (b,) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 147 | for b in bootloaders]) + |
Matt Mower | 6ced9b2 | 2014-08-28 18:29:14 -0500 | [diff] [blame] | 148 | ' || abort("This package supports bootloader(s): ' + |
| 149 | ", ".join(["%s" % (b,) for b in bootloaders]) + |
| 150 | '; this device has bootloader " + getprop("ro.bootloader") + ".");' + |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 151 | ");") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 152 | self.script.append(self.WordWrap(cmd)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 153 | |
Matt Mower | 534366b | 2014-08-28 16:51:02 -0500 | [diff] [blame] | 154 | def AssertSomeBaseband(self, *basebands): |
| 155 | """Assert that the baseband version is one of *basebands.""" |
| 156 | cmd = ("assert(" + |
Ricardo Cerqueira | eb15b11 | 2014-11-08 23:03:53 +0000 | [diff] [blame] | 157 | " || ".join(['getprop("ro.baseband") == "%s"' % (b,) |
Matt Mower | 534366b | 2014-08-28 16:51:02 -0500 | [diff] [blame] | 158 | for b in basebands]) + |
Matt Mower | 6ced9b2 | 2014-08-28 18:29:14 -0500 | [diff] [blame] | 159 | ' || abort("This package supports baseband(s): ' + |
| 160 | ", ".join(["%s" % (b,) for b in basebands]) + |
| 161 | '; this device has baseband " + getprop("ro.baseband") + ".");' + |
Matt Mower | 534366b | 2014-08-28 16:51:02 -0500 | [diff] [blame] | 162 | ");") |
Matt Mower | dce0b29 | 2017-02-16 01:09:19 -0600 | [diff] [blame] | 163 | self.script.append(self.WordWrap(cmd)) |
Matt Mower | 534366b | 2014-08-28 16:51:02 -0500 | [diff] [blame] | 164 | |
Chris Soyars | 84f40c8 | 2010-12-23 00:44:33 +0100 | [diff] [blame] | 165 | def RunBackup(self, command): |
Tom Marshall | 4786996 | 2014-12-12 11:51:33 -0800 | [diff] [blame] | 166 | self.script.append(('run_program("/tmp/install/bin/backuptool.sh", "%s");' % command)) |
Chris Soyars | 84f40c8 | 2010-12-23 00:44:33 +0100 | [diff] [blame] | 167 | |
Josh Chasky | 9383fb3 | 2016-10-09 01:07:02 +0000 | [diff] [blame] | 168 | def FlashSuperSU(self): |
| 169 | self.script.append('package_extract_dir("supersu", "/tmp/supersu");') |
| 170 | self.script.append('run_program("/sbin/busybox", "unzip", "/tmp/supersu/supersu.zip", "META-INF/com/google/android/*", "-d", "/tmp/supersu");') |
| 171 | self.script.append('run_program("/sbin/busybox", "sh", "/tmp/supersu/META-INF/com/google/android/update-binary", "dummy", "1", "/tmp/supersu/supersu.zip");') |
| 172 | |
LorDClockaN | 29273ea | 2017-01-31 21:15:02 -0700 | [diff] [blame^] | 173 | def FlashMagisk(self): |
| 174 | self.script.append('package_extract_dir("magisk", "/tmp/magisk");') |
| 175 | self.script.append('run_program("/sbin/busybox", "unzip", "/tmp/magisk/magisk.zip", "META-INF/com/google/android/*", "-d", "/tmp/magisk");') |
| 176 | self.script.append('run_program("/sbin/sh", "/tmp/magisk/META-INF/com/google/android/update-binary", "dummy", "1", "/tmp/magisk/magisk.zip");') |
| 177 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 178 | def ShowProgress(self, frac, dur): |
| 179 | """Update the progress bar, advancing it over 'frac' over the next |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 180 | 'dur' seconds. 'dur' may be zero to advance it via SetProgress |
| 181 | commands instead of by time.""" |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 182 | self.script.append("show_progress(%f, %d);" % (frac, int(dur))) |
| 183 | |
Doug Zongker | 881dd40 | 2009-09-20 14:03:55 -0700 | [diff] [blame] | 184 | def SetProgress(self, frac): |
| 185 | """Set the position of the progress bar within the chunk defined |
| 186 | by the most recent ShowProgress call. 'frac' should be in |
| 187 | [0,1].""" |
| 188 | self.script.append("set_progress(%f);" % (frac,)) |
| 189 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 190 | def PatchCheck(self, filename, *sha1): |
| 191 | """Check that the given file (or MTD reference) has one of the |
Doug Zongker | c8d446b | 2010-02-22 15:41:53 -0800 | [diff] [blame] | 192 | given *sha1 hashes, checking the version saved in cache if the |
| 193 | file does not match.""" |
Doug Zongker | 0d92f1f | 2013-06-03 12:07:12 -0700 | [diff] [blame] | 194 | self.script.append( |
| 195 | 'apply_patch_check("%s"' % (filename,) + |
| 196 | "".join([', "%s"' % (i,) for i in sha1]) + |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 197 | ') || abort("E%d: \\"%s\\" has unexpected contents.");' % ( |
| 198 | common.ErrorCode.BAD_PATCH_FILE, filename)) |
Doug Zongker | c8d446b | 2010-02-22 15:41:53 -0800 | [diff] [blame] | 199 | |
Tao Bao | 9bc6bb2 | 2015-11-09 16:58:28 -0800 | [diff] [blame] | 200 | def Verify(self, filename): |
| 201 | """Check that the given file (or MTD reference) has one of the |
| 202 | given hashes (encoded in the filename).""" |
| 203 | self.script.append( |
| 204 | 'apply_patch_check("{filename}") && ' |
| 205 | 'ui_print(" Verified.") || ' |
| 206 | 'ui_print("\\"{filename}\\" has unexpected contents.");'.format( |
| 207 | filename=filename)) |
| 208 | |
Doug Zongker | c8d446b | 2010-02-22 15:41:53 -0800 | [diff] [blame] | 209 | def FileCheck(self, filename, *sha1): |
| 210 | """Check that the given file (or MTD reference) has one of the |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 211 | given *sha1 hashes.""" |
Doug Zongker | 5a48209 | 2010-02-17 16:09:18 -0800 | [diff] [blame] | 212 | self.script.append('assert(sha1_check(read_file("%s")' % (filename,) + |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 213 | "".join([', "%s"' % (i,) for i in sha1]) + |
| 214 | '));') |
| 215 | |
| 216 | def CacheFreeSpaceCheck(self, amount): |
| 217 | """Check that there's at least 'amount' space that can be made |
| 218 | available on /cache.""" |
Tao Bao | b4cfca5 | 2016-02-04 14:26:02 -0800 | [diff] [blame] | 219 | self._required_cache = max(self._required_cache, amount) |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 220 | self.script.append(('apply_patch_space(%d) || abort("E%d: Not enough free ' |
| 221 | 'space on /cache to apply patches.");') % ( |
| 222 | amount, |
| 223 | common.ErrorCode.INSUFFICIENT_CACHE_SPACE)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 224 | |
Michael Runge | 7cd99ba | 2014-10-22 17:21:48 -0700 | [diff] [blame] | 225 | def Mount(self, mount_point, mount_options_by_format=""): |
| 226 | """Mount the partition with the given mount_point. |
| 227 | mount_options_by_format: |
| 228 | [fs_type=option[,option]...[|fs_type=option[,option]...]...] |
| 229 | where option is optname[=optvalue] |
| 230 | E.g. ext4=barrier=1,nodelalloc,errors=panic|f2fs=errors=recover |
| 231 | """ |
Mattia D'Alleva | c0d78fd | 2015-10-26 22:51:11 +0100 | [diff] [blame] | 232 | fstab = self.info.get("fstab", None) |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 233 | if fstab: |
| 234 | p = fstab[mount_point] |
Michael Runge | 7cd99ba | 2014-10-22 17:21:48 -0700 | [diff] [blame] | 235 | mount_dict = {} |
| 236 | if mount_options_by_format is not None: |
| 237 | for option in mount_options_by_format.split("|"): |
| 238 | if "=" in option: |
| 239 | key, value = option.split("=", 1) |
| 240 | mount_dict[key] = value |
Mattia D'Alleva | cfdba0a | 2015-05-22 02:24:24 +0200 | [diff] [blame] | 241 | self.script.append('run_program("/sbin/busybox", "mount", "%s");' % |
| 242 | (p.mount_point)) |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 243 | self.mounts.add(p.mount_point) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 244 | |
Chris Soyars | 84f40c8 | 2010-12-23 00:44:33 +0100 | [diff] [blame] | 245 | def Unmount(self, mount_point): |
| 246 | """Unmount the partition with the given mount_point.""" |
| 247 | if mount_point in self.mounts: |
| 248 | self.mounts.remove(mount_point) |
| 249 | self.script.append('unmount("%s");' % (mount_point,)) |
| 250 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 251 | def UnpackPackageDir(self, src, dst): |
| 252 | """Unpack a given directory from the OTA package into the given |
| 253 | destination directory.""" |
| 254 | self.script.append('package_extract_dir("%s", "%s");' % (src, dst)) |
| 255 | |
| 256 | def Comment(self, comment): |
| 257 | """Write a comment into the update script.""" |
| 258 | self.script.append("") |
| 259 | for i in comment.split("\n"): |
| 260 | self.script.append("# " + i) |
| 261 | self.script.append("") |
| 262 | |
| 263 | def Print(self, message): |
| 264 | """Log a message to the screen (if the logs are visible).""" |
| 265 | self.script.append('ui_print("%s");' % (message,)) |
| 266 | |
Michael Runge | 3e28664 | 2014-11-21 00:46:03 -0800 | [diff] [blame] | 267 | def TunePartition(self, partition, *options): |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 268 | fstab = self.fstab |
Michael Runge | 3e28664 | 2014-11-21 00:46:03 -0800 | [diff] [blame] | 269 | if fstab: |
| 270 | p = fstab[partition] |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 271 | if p.fs_type not in ("ext2", "ext3", "ext4"): |
Michael Runge | 3e28664 | 2014-11-21 00:46:03 -0800 | [diff] [blame] | 272 | raise ValueError("Partition %s cannot be tuned\n" % (partition,)) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 273 | self.script.append( |
| 274 | 'tune2fs(' + "".join(['"%s", ' % (i,) for i in options]) + |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 275 | '"%s") || abort("E%d: Failed to tune partition %s");' % ( |
| 276 | p.device, common.ErrorCode.TUNE_PARTITION_FAILURE, partition)) |
Michael Runge | 3e28664 | 2014-11-21 00:46:03 -0800 | [diff] [blame] | 277 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 278 | def FormatPartition(self, partition): |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 279 | """Format the given partition, specified by its mount point (eg, |
| 280 | "/system").""" |
| 281 | |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 282 | fstab = self.fstab |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 283 | if fstab: |
| 284 | p = fstab[partition] |
Doug Zongker | df2056e | 2012-04-09 12:27:43 -0700 | [diff] [blame] | 285 | self.script.append('format("%s", "%s", "%s", "%s", "%s");' % |
Doug Zongker | 086cbb0 | 2011-02-17 15:54:20 -0800 | [diff] [blame] | 286 | (p.fs_type, common.PARTITION_TYPES[p.fs_type], |
Doug Zongker | df2056e | 2012-04-09 12:27:43 -0700 | [diff] [blame] | 287 | p.device, p.length, p.mount_point)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 288 | |
Doug Zongker | 5fad203 | 2014-02-24 08:13:45 -0800 | [diff] [blame] | 289 | def WipeBlockDevice(self, partition): |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 290 | if partition not in ("/system", "/vendor"): |
| 291 | raise ValueError(("WipeBlockDevice doesn't work on %s\n") % (partition,)) |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 292 | fstab = self.fstab |
Doug Zongker | c8b4e84 | 2014-06-16 15:16:31 -0700 | [diff] [blame] | 293 | size = self.info.get(partition.lstrip("/") + "_size", None) |
Doug Zongker | 5fad203 | 2014-02-24 08:13:45 -0800 | [diff] [blame] | 294 | device = fstab[partition].device |
| 295 | |
| 296 | self.script.append('wipe_block_device("%s", %s);' % (device, size)) |
| 297 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 298 | def DeleteFiles(self, file_list): |
| 299 | """Delete all files in file_list.""" |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 300 | if not file_list: |
| 301 | return |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 302 | cmd = "delete(" + ",\0".join(['"%s"' % (i,) for i in file_list]) + ");" |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 303 | self.script.append(self.WordWrap(cmd)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 304 | |
Tao Bao | 84006ea | 2015-09-02 10:28:08 -0700 | [diff] [blame] | 305 | def DeleteFilesIfNotMatching(self, file_list): |
| 306 | """Delete the file in file_list if not matching the checksum.""" |
| 307 | if not file_list: |
| 308 | return |
| 309 | for name, sha1 in file_list: |
| 310 | cmd = ('sha1_check(read_file("{name}"), "{sha1}") || ' |
| 311 | 'delete("{name}");'.format(name=name, sha1=sha1)) |
| 312 | self.script.append(self.WordWrap(cmd)) |
| 313 | |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 314 | def RenameFile(self, srcfile, tgtfile): |
| 315 | """Moves a file from one location to another.""" |
| 316 | if self.info.get("update_rename_support", False): |
| 317 | self.script.append('rename("%s", "%s");' % (srcfile, tgtfile)) |
| 318 | else: |
| 319 | raise ValueError("Rename not supported by update binary") |
| 320 | |
| 321 | def SkipNextActionIfTargetExists(self, tgtfile, tgtsha1): |
| 322 | """Prepend an action with an apply_patch_check in order to |
| 323 | skip the action if the file exists. Used when a patch |
| 324 | is later renamed.""" |
Tao Bao | 84006ea | 2015-09-02 10:28:08 -0700 | [diff] [blame] | 325 | cmd = ('sha1_check(read_file("%s"), %s) ||' % (tgtfile, tgtsha1)) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 326 | self.script.append(self.WordWrap(cmd)) |
Michael Runge | 4038aa8 | 2013-12-13 18:06:28 -0800 | [diff] [blame] | 327 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 328 | def ApplyPatch(self, srcfile, tgtfile, tgtsize, tgtsha1, *patchpairs): |
| 329 | """Apply binary patches (in *patchpairs) to the given srcfile to |
| 330 | produce tgtfile (which may be "-" to indicate overwriting the |
| 331 | source file.""" |
| 332 | if len(patchpairs) % 2 != 0 or len(patchpairs) == 0: |
| 333 | raise ValueError("bad patches given to ApplyPatch") |
| 334 | cmd = ['apply_patch("%s",\0"%s",\0%s,\0%d' |
| 335 | % (srcfile, tgtfile, tgtsha1, tgtsize)] |
| 336 | for i in range(0, len(patchpairs), 2): |
Tao Bao | c386890 | 2015-12-01 17:46:46 -0800 | [diff] [blame] | 337 | cmd.append(',\0%s,\0package_extract_file("%s")' % patchpairs[i:i+2]) |
Tianjie Xu | 209db46 | 2016-05-24 17:34:52 -0700 | [diff] [blame] | 338 | cmd.append(') ||\n abort("E%d: Failed to apply patch to %s");' % ( |
| 339 | common.ErrorCode.APPLY_PATCH_FAILURE, srcfile)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 340 | cmd = "".join(cmd) |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 341 | self.script.append(self.WordWrap(cmd)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 342 | |
Doug Zongker | 5fad203 | 2014-02-24 08:13:45 -0800 | [diff] [blame] | 343 | def WriteRawImage(self, mount_point, fn, mapfn=None): |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 344 | """Write the given package file into the partition for the given |
| 345 | mount point.""" |
Doug Zongker | b4c7d32 | 2010-07-01 15:30:11 -0700 | [diff] [blame] | 346 | |
Tao Bao | 34b47bf | 2015-06-22 19:17:41 -0700 | [diff] [blame] | 347 | fstab = self.fstab |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 348 | if fstab: |
| 349 | p = fstab[mount_point] |
Doug Zongker | 96a57e7 | 2010-09-26 14:57:41 -0700 | [diff] [blame] | 350 | partition_type = common.PARTITION_TYPES[p.fs_type] |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 351 | args = {'device': p.device, 'fn': fn} |
| 352 | if partition_type == "MTD": |
| 353 | self.script.append( |
Doug Zongker | 02da210 | 2011-04-12 15:50:17 -0700 | [diff] [blame] | 354 | 'write_raw_image(package_extract_file("%(fn)s"), "%(device)s");' |
| 355 | % args) |
Artefvck 07 | c25f7bd | 2016-02-04 09:37:15 -0800 | [diff] [blame] | 356 | elif partition_type == "OSIP": |
| 357 | self.script.append( |
| 358 | 'write_osip_image(package_extract_file("%(fn)s"), "%(device)s");' |
| 359 | % args) |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 360 | elif partition_type == "EMMC": |
Doug Zongker | 5fad203 | 2014-02-24 08:13:45 -0800 | [diff] [blame] | 361 | if mapfn: |
| 362 | args["map"] = mapfn |
| 363 | self.script.append( |
| 364 | 'package_extract_file("%(fn)s", "%(device)s", "%(map)s");' % args) |
| 365 | else: |
| 366 | self.script.append( |
| 367 | 'package_extract_file("%(fn)s", "%(device)s");' % args) |
Doug Zongker | 9ce0fb6 | 2010-09-20 18:04:41 -0700 | [diff] [blame] | 368 | else: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 369 | raise ValueError( |
| 370 | "don't know how to write \"%s\" partitions" % p.fs_type) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 371 | |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 372 | def SetPermissions(self, fn, uid, gid, mode, selabel, capabilities): |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 373 | """Set file ownership and permissions.""" |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 374 | if not self.info.get("use_set_metadata", False): |
| 375 | self.script.append('set_perm(%d, %d, 0%o, "%s");' % (uid, gid, mode, fn)) |
| 376 | else: |
Tom Marshall | 27b1f53 | 2014-12-16 09:58:47 -0800 | [diff] [blame] | 377 | cmd = 'set_metadata("%s", "uid", %d, "gid", %d, "mode", 0%o' \ |
| 378 | % (fn, uid, gid, mode) |
| 379 | if capabilities is not None: |
| 380 | cmd += ', "capabilities", %s' % ( capabilities ) |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 381 | if selabel is not None: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 382 | cmd += ', "selabel", "%s"' % selabel |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 383 | cmd += ');' |
| 384 | self.script.append(cmd) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 385 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 386 | def SetPermissionsRecursive(self, fn, uid, gid, dmode, fmode, selabel, |
| 387 | capabilities): |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 388 | """Recursively set path ownership and permissions.""" |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 389 | if not self.info.get("use_set_metadata", False): |
| 390 | self.script.append('set_perm_recursive(%d, %d, 0%o, 0%o, "%s");' |
| 391 | % (uid, gid, dmode, fmode, fn)) |
| 392 | else: |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 393 | cmd = 'set_metadata_recursive("%s", "uid", %d, "gid", %d, ' \ |
Tom Marshall | 27b1f53 | 2014-12-16 09:58:47 -0800 | [diff] [blame] | 394 | '"dmode", 0%o, "fmode", 0%o' \ |
| 395 | % (fn, uid, gid, dmode, fmode) |
| 396 | if capabilities is not None: |
| 397 | cmd += ', "capabilities", "%s"' % ( capabilities ) |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 398 | if selabel is not None: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 399 | cmd += ', "selabel", "%s"' % selabel |
Nick Kralevich | 0eb17d9 | 2013-09-07 17:10:29 -0700 | [diff] [blame] | 400 | cmd += ');' |
| 401 | self.script.append(cmd) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 402 | |
| 403 | def MakeSymlinks(self, symlink_list): |
| 404 | """Create symlinks, given a list of (dest, link) pairs.""" |
| 405 | by_dest = {} |
| 406 | for d, l in symlink_list: |
| 407 | by_dest.setdefault(d, []).append(l) |
| 408 | |
| 409 | for dest, links in sorted(by_dest.iteritems()): |
| 410 | cmd = ('symlink("%s", ' % (dest,) + |
| 411 | ",\0".join(['"' + i + '"' for i in sorted(links)]) + ");") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 412 | self.script.append(self.WordWrap(cmd)) |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 413 | |
| 414 | def AppendExtra(self, extra): |
| 415 | """Append text verbatim to the output script.""" |
| 416 | self.script.append(extra) |
| 417 | |
Michael Runge | 63f01de | 2014-10-28 19:24:19 -0700 | [diff] [blame] | 418 | def Unmount(self, mount_point): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 419 | self.script.append('unmount("%s");' % mount_point) |
| 420 | self.mounts.remove(mount_point) |
Michael Runge | 63f01de | 2014-10-28 19:24:19 -0700 | [diff] [blame] | 421 | |
Doug Zongker | 1483360 | 2010-02-02 13:12:04 -0800 | [diff] [blame] | 422 | def UnmountAll(self): |
| 423 | for p in sorted(self.mounts): |
| 424 | self.script.append('unmount("%s");' % (p,)) |
| 425 | self.mounts = set() |
| 426 | |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 427 | def AddToZip(self, input_zip, output_zip, input_path=None): |
| 428 | """Write the accumulated script to the output_zip file. input_zip |
| 429 | is used as the source for the 'updater' binary needed to run |
| 430 | script. If input_path is not None, it will be used as a local |
| 431 | path for the binary instead of input_zip.""" |
| 432 | |
Doug Zongker | 1483360 | 2010-02-02 13:12:04 -0800 | [diff] [blame] | 433 | self.UnmountAll() |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 434 | |
| 435 | common.ZipWriteStr(output_zip, "META-INF/com/google/android/updater-script", |
| 436 | "\n".join(self.script) + "\n") |
| 437 | |
| 438 | if input_path is None: |
| 439 | data = input_zip.read("OTA/bin/updater") |
| 440 | else: |
Doug Zongker | 2556848 | 2014-03-03 10:21:27 -0800 | [diff] [blame] | 441 | data = open(input_path, "rb").read() |
Doug Zongker | c494d7c | 2009-06-18 08:43:44 -0700 | [diff] [blame] | 442 | common.ZipWriteStr(output_zip, "META-INF/com/google/android/update-binary", |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 443 | data, perms=0o755) |