blob: 2ecc5cbb86df960c68df383d6dd0e5a3bd0cd583 [file] [log] [blame]
Doug Zongkerc494d7c2009-06-18 08:43:44 -07001# 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 Zongkerc494d7c2009-06-18 08:43:44 -070015import re
16
17import common
18
19class EdifyGenerator(object):
20 """Class to generate scripts in the 'edify' recovery script language
21 used from donut onwards."""
22
Tao Bao34b47bf2015-06-22 19:17:41 -070023 def __init__(self, version, info, fstab=None):
Doug Zongkerc494d7c2009-06-18 08:43:44 -070024 self.script = []
25 self.mounts = set()
Tao Baob4cfca52016-02-04 14:26:02 -080026 self._required_cache = 0
Doug Zongkerc494d7c2009-06-18 08:43:44 -070027 self.version = version
Doug Zongkerb4c7d322010-07-01 15:30:11 -070028 self.info = info
Tao Bao34b47bf2015-06-22 19:17:41 -070029 if fstab is None:
30 self.fstab = self.info.get("fstab", None)
31 else:
32 self.fstab = fstab
Doug Zongkerc494d7c2009-06-18 08:43:44 -070033
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 Zongker67369982010-07-07 13:53:32 -070038 x = EdifyGenerator(self.version, self.info)
Doug Zongkerc494d7c2009-06-18 08:43:44 -070039 x.mounts = self.mounts
40 return x
41
Tao Baob4cfca52016-02-04 14:26:02 -080042 @property
43 def required_cache(self):
44 """Return the minimum cache size to apply the update."""
45 return self._required_cache
46
Doug Zongkerc494d7c2009-06-18 08:43:44 -070047 @staticmethod
Dan Albert8b72aef2015-03-23 19:13:21 -070048 def WordWrap(cmd, linelen=80):
Doug Zongkerc494d7c2009-06-18 08:43:44 -070049 """'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 Runge6e836112014-04-15 17:40:21 -070080 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 Baodf4cb0b2016-02-25 19:49:55 -080086 if common.OPTIONS.oem_no_mount:
87 cmd = ('getprop("{name}") == "{value}" || '
Tianjie Xu209db462016-05-24 17:34:52 -070088 'abort("E{code}: This package expects the value \\"{value}\\" for '
Tao Baodf4cb0b2016-02-25 19:49:55 -080089 '\\"{name}\\"; this has value \\"" + '
Tianjie Xu209db462016-05-24 17:34:52 -070090 'getprop("{name}") + "\\".");').format(
91 code=common.ErrorCode.OEM_PROP_MISMATCH,
92 name=name, value=value)
Tao Baodf4cb0b2016-02-25 19:49:55 -080093 else:
94 cmd = ('file_getprop("/oem/oem.prop", "{name}") == "{value}" || '
Tianjie Xu209db462016-05-24 17:34:52 -070095 'abort("E{code}: This package expects the value \\"{value}\\" for '
Tao Baodf4cb0b2016-02-25 19:49:55 -080096 '\\"{name}\\" on the OEM partition; this has value \\"" + '
97 'file_getprop("/oem/oem.prop", "{name}") + "\\".");').format(
Tianjie Xu209db462016-05-24 17:34:52 -070098 code=common.ErrorCode.OEM_PROP_MISMATCH,
Tao Baodf4cb0b2016-02-25 19:49:55 -080099 name=name, value=value)
Michael Runge6e836112014-04-15 17:40:21 -0700100 self.script.append(cmd)
101
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700102 def AssertSomeFingerprint(self, *fp):
Doug Zongkeraf845252014-05-09 08:29:05 -0700103 """Assert that the current recovery build fingerprint is one of *fp."""
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700104 if not fp:
105 raise ValueError("must specify some fingerprints")
Dan Albert8b72aef2015-03-23 19:13:21 -0700106 cmd = (' ||\n '.join([('getprop("ro.build.fingerprint") == "%s"') % i
107 for i in fp]) +
Tianjie Xu209db462016-05-24 17:34:52 -0700108 ' ||\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 Zongker0d92f1f2013-06-03 12:07:12 -0700111 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700112
Michael Runge6e836112014-04-15 17:40:21 -0700113 def AssertSomeThumbprint(self, *fp):
Doug Zongkeraf845252014-05-09 08:29:05 -0700114 """Assert that the current recovery build thumbprint is one of *fp."""
Geremy Condra36bd3652014-02-06 19:45:10 -0800115 if not fp:
Michael Runge6e836112014-04-15 17:40:21 -0700116 raise ValueError("must specify some thumbprints")
Dan Albert8b72aef2015-03-23 19:13:21 -0700117 cmd = (' ||\n '.join([('getprop("ro.build.thumbprint") == "%s"') % i
118 for i in fp]) +
Tianjie Xu209db462016-05-24 17:34:52 -0700119 ' ||\n abort("E%d: Package expects build thumbprint of %s; this '
Dan Albert8b72aef2015-03-23 19:13:21 -0700120 'device has " + getprop("ro.build.thumbprint") + ".");') % (
Tianjie Xu209db462016-05-24 17:34:52 -0700121 common.ErrorCode.THUMBPRINT_MISMATCH, " or ".join(fp))
Geremy Condra36bd3652014-02-06 19:45:10 -0800122 self.script.append(cmd)
123
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700124 def AssertOlderBuild(self, timestamp, timestamp_text):
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700125 """Assert that the build on the device is older (or the same as)
126 the given timestamp."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700127 self.script.append(
128 ('(!less_than_int(%s, getprop("ro.build.date.utc"))) || '
Tianjie Xu209db462016-05-24 17:34:52 -0700129 'abort("E%d: Can\'t install this package (%s) over newer '
Dan Albert8b72aef2015-03-23 19:13:21 -0700130 'build (" + getprop("ro.build.date") + ").");') % (timestamp,
Tianjie Xu209db462016-05-24 17:34:52 -0700131 common.ErrorCode.OLDER_BUILD, timestamp_text))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700132
133 def AssertDevice(self, device):
134 """Assert that the device identifier is the given string."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700135 cmd = ('getprop("ro.product.device") == "%s" || '
Tianjie Xu209db462016-05-24 17:34:52 -0700136 'abort("E%d: This package is for \\"%s\\" devices; '
Dan Albert8b72aef2015-03-23 19:13:21 -0700137 'this is a \\"" + getprop("ro.product.device") + "\\".");') % (
Tianjie Xu209db462016-05-24 17:34:52 -0700138 device, common.ErrorCode.DEVICE_MISMATCH, device)
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700139 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700140
141 def AssertSomeBootloader(self, *bootloaders):
142 """Asert that the bootloader version is one of *bootloaders."""
143 cmd = ("assert(" +
144 " ||\0".join(['getprop("ro.bootloader") == "%s"' % (b,)
145 for b in bootloaders]) +
146 ");")
Dan Albert8b72aef2015-03-23 19:13:21 -0700147 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700148
149 def ShowProgress(self, frac, dur):
150 """Update the progress bar, advancing it over 'frac' over the next
Doug Zongker881dd402009-09-20 14:03:55 -0700151 'dur' seconds. 'dur' may be zero to advance it via SetProgress
152 commands instead of by time."""
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700153 self.script.append("show_progress(%f, %d);" % (frac, int(dur)))
154
Doug Zongker881dd402009-09-20 14:03:55 -0700155 def SetProgress(self, frac):
156 """Set the position of the progress bar within the chunk defined
157 by the most recent ShowProgress call. 'frac' should be in
158 [0,1]."""
159 self.script.append("set_progress(%f);" % (frac,))
160
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700161 def PatchCheck(self, filename, *sha1):
162 """Check that the given file (or MTD reference) has one of the
Doug Zongkerc8d446b2010-02-22 15:41:53 -0800163 given *sha1 hashes, checking the version saved in cache if the
164 file does not match."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700165 self.script.append(
166 'apply_patch_check("%s"' % (filename,) +
167 "".join([', "%s"' % (i,) for i in sha1]) +
Tianjie Xu209db462016-05-24 17:34:52 -0700168 ') || abort("E%d: \\"%s\\" has unexpected contents.");' % (
169 common.ErrorCode.BAD_PATCH_FILE, filename))
Doug Zongkerc8d446b2010-02-22 15:41:53 -0800170
Tao Bao9bc6bb22015-11-09 16:58:28 -0800171 def Verify(self, filename):
172 """Check that the given file (or MTD reference) has one of the
173 given hashes (encoded in the filename)."""
174 self.script.append(
175 'apply_patch_check("{filename}") && '
176 'ui_print(" Verified.") || '
177 'ui_print("\\"{filename}\\" has unexpected contents.");'.format(
178 filename=filename))
179
Doug Zongkerc8d446b2010-02-22 15:41:53 -0800180 def FileCheck(self, filename, *sha1):
181 """Check that the given file (or MTD reference) has one of the
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700182 given *sha1 hashes."""
Doug Zongker5a482092010-02-17 16:09:18 -0800183 self.script.append('assert(sha1_check(read_file("%s")' % (filename,) +
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700184 "".join([', "%s"' % (i,) for i in sha1]) +
185 '));')
186
187 def CacheFreeSpaceCheck(self, amount):
188 """Check that there's at least 'amount' space that can be made
189 available on /cache."""
Tao Baob4cfca52016-02-04 14:26:02 -0800190 self._required_cache = max(self._required_cache, amount)
Tianjie Xu209db462016-05-24 17:34:52 -0700191 self.script.append(('apply_patch_space(%d) || abort("E%d: Not enough free '
192 'space on /cache to apply patches.");') % (
193 amount,
194 common.ErrorCode.INSUFFICIENT_CACHE_SPACE))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700195
Michael Runge7cd99ba2014-10-22 17:21:48 -0700196 def Mount(self, mount_point, mount_options_by_format=""):
197 """Mount the partition with the given mount_point.
198 mount_options_by_format:
199 [fs_type=option[,option]...[|fs_type=option[,option]...]...]
200 where option is optname[=optvalue]
201 E.g. ext4=barrier=1,nodelalloc,errors=panic|f2fs=errors=recover
202 """
Tao Bao34b47bf2015-06-22 19:17:41 -0700203 fstab = self.fstab
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700204 if fstab:
205 p = fstab[mount_point]
Michael Runge7cd99ba2014-10-22 17:21:48 -0700206 mount_dict = {}
207 if mount_options_by_format is not None:
208 for option in mount_options_by_format.split("|"):
209 if "=" in option:
210 key, value = option.split("=", 1)
211 mount_dict[key] = value
Tao Baodf06e962015-06-10 12:32:41 -0700212 mount_flags = mount_dict.get(p.fs_type, "")
213 if p.context is not None:
214 mount_flags = p.context + ("," + mount_flags if mount_flags else "")
Dan Albert8b72aef2015-03-23 19:13:21 -0700215 self.script.append('mount("%s", "%s", "%s", "%s", "%s");' % (
216 p.fs_type, common.PARTITION_TYPES[p.fs_type], p.device,
Tao Baodf06e962015-06-10 12:32:41 -0700217 p.mount_point, mount_flags))
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700218 self.mounts.add(p.mount_point)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700219
220 def UnpackPackageDir(self, src, dst):
221 """Unpack a given directory from the OTA package into the given
222 destination directory."""
223 self.script.append('package_extract_dir("%s", "%s");' % (src, dst))
224
225 def Comment(self, comment):
226 """Write a comment into the update script."""
227 self.script.append("")
228 for i in comment.split("\n"):
229 self.script.append("# " + i)
230 self.script.append("")
231
232 def Print(self, message):
233 """Log a message to the screen (if the logs are visible)."""
234 self.script.append('ui_print("%s");' % (message,))
235
Michael Runge3e286642014-11-21 00:46:03 -0800236 def TunePartition(self, partition, *options):
Tao Bao34b47bf2015-06-22 19:17:41 -0700237 fstab = self.fstab
Michael Runge3e286642014-11-21 00:46:03 -0800238 if fstab:
239 p = fstab[partition]
Dan Albert8b72aef2015-03-23 19:13:21 -0700240 if p.fs_type not in ("ext2", "ext3", "ext4"):
Michael Runge3e286642014-11-21 00:46:03 -0800241 raise ValueError("Partition %s cannot be tuned\n" % (partition,))
Dan Albert8b72aef2015-03-23 19:13:21 -0700242 self.script.append(
243 'tune2fs(' + "".join(['"%s", ' % (i,) for i in options]) +
Tianjie Xu209db462016-05-24 17:34:52 -0700244 '"%s") || abort("E%d: Failed to tune partition %s");' % (
245 p.device, common.ErrorCode.TUNE_PARTITION_FAILURE, partition))
Michael Runge3e286642014-11-21 00:46:03 -0800246
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700247 def FormatPartition(self, partition):
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700248 """Format the given partition, specified by its mount point (eg,
249 "/system")."""
250
Tao Bao34b47bf2015-06-22 19:17:41 -0700251 fstab = self.fstab
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700252 if fstab:
253 p = fstab[partition]
Doug Zongkerdf2056e2012-04-09 12:27:43 -0700254 self.script.append('format("%s", "%s", "%s", "%s", "%s");' %
Doug Zongker086cbb02011-02-17 15:54:20 -0800255 (p.fs_type, common.PARTITION_TYPES[p.fs_type],
Doug Zongkerdf2056e2012-04-09 12:27:43 -0700256 p.device, p.length, p.mount_point))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700257
Doug Zongker5fad2032014-02-24 08:13:45 -0800258 def WipeBlockDevice(self, partition):
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700259 if partition not in ("/system", "/vendor"):
260 raise ValueError(("WipeBlockDevice doesn't work on %s\n") % (partition,))
Tao Bao34b47bf2015-06-22 19:17:41 -0700261 fstab = self.fstab
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700262 size = self.info.get(partition.lstrip("/") + "_size", None)
Doug Zongker5fad2032014-02-24 08:13:45 -0800263 device = fstab[partition].device
264
265 self.script.append('wipe_block_device("%s", %s);' % (device, size))
266
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700267 def DeleteFiles(self, file_list):
268 """Delete all files in file_list."""
Dan Albert8b72aef2015-03-23 19:13:21 -0700269 if not file_list:
270 return
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700271 cmd = "delete(" + ",\0".join(['"%s"' % (i,) for i in file_list]) + ");"
Dan Albert8b72aef2015-03-23 19:13:21 -0700272 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700273
Tao Bao84006ea2015-09-02 10:28:08 -0700274 def DeleteFilesIfNotMatching(self, file_list):
275 """Delete the file in file_list if not matching the checksum."""
276 if not file_list:
277 return
278 for name, sha1 in file_list:
279 cmd = ('sha1_check(read_file("{name}"), "{sha1}") || '
280 'delete("{name}");'.format(name=name, sha1=sha1))
281 self.script.append(self.WordWrap(cmd))
282
Michael Runge4038aa82013-12-13 18:06:28 -0800283 def RenameFile(self, srcfile, tgtfile):
284 """Moves a file from one location to another."""
285 if self.info.get("update_rename_support", False):
286 self.script.append('rename("%s", "%s");' % (srcfile, tgtfile))
287 else:
288 raise ValueError("Rename not supported by update binary")
289
290 def SkipNextActionIfTargetExists(self, tgtfile, tgtsha1):
291 """Prepend an action with an apply_patch_check in order to
292 skip the action if the file exists. Used when a patch
293 is later renamed."""
Tao Bao84006ea2015-09-02 10:28:08 -0700294 cmd = ('sha1_check(read_file("%s"), %s) ||' % (tgtfile, tgtsha1))
Dan Albert8b72aef2015-03-23 19:13:21 -0700295 self.script.append(self.WordWrap(cmd))
Michael Runge4038aa82013-12-13 18:06:28 -0800296
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700297 def ApplyPatch(self, srcfile, tgtfile, tgtsize, tgtsha1, *patchpairs):
298 """Apply binary patches (in *patchpairs) to the given srcfile to
299 produce tgtfile (which may be "-" to indicate overwriting the
300 source file."""
301 if len(patchpairs) % 2 != 0 or len(patchpairs) == 0:
302 raise ValueError("bad patches given to ApplyPatch")
303 cmd = ['apply_patch("%s",\0"%s",\0%s,\0%d'
304 % (srcfile, tgtfile, tgtsha1, tgtsize)]
305 for i in range(0, len(patchpairs), 2):
Tao Baoc3868902015-12-01 17:46:46 -0800306 cmd.append(',\0%s,\0package_extract_file("%s")' % patchpairs[i:i+2])
Tianjie Xu209db462016-05-24 17:34:52 -0700307 cmd.append(') ||\n abort("E%d: Failed to apply patch to %s");' % (
308 common.ErrorCode.APPLY_PATCH_FAILURE, srcfile))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700309 cmd = "".join(cmd)
Dan Albert8b72aef2015-03-23 19:13:21 -0700310 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700311
Doug Zongker5fad2032014-02-24 08:13:45 -0800312 def WriteRawImage(self, mount_point, fn, mapfn=None):
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700313 """Write the given package file into the partition for the given
314 mount point."""
Doug Zongkerb4c7d322010-07-01 15:30:11 -0700315
Tao Bao34b47bf2015-06-22 19:17:41 -0700316 fstab = self.fstab
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700317 if fstab:
318 p = fstab[mount_point]
Doug Zongker96a57e72010-09-26 14:57:41 -0700319 partition_type = common.PARTITION_TYPES[p.fs_type]
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700320 args = {'device': p.device, 'fn': fn}
321 if partition_type == "MTD":
322 self.script.append(
Doug Zongker02da2102011-04-12 15:50:17 -0700323 'write_raw_image(package_extract_file("%(fn)s"), "%(device)s");'
324 % args)
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700325 elif partition_type == "EMMC":
Doug Zongker5fad2032014-02-24 08:13:45 -0800326 if mapfn:
327 args["map"] = mapfn
328 self.script.append(
329 'package_extract_file("%(fn)s", "%(device)s", "%(map)s");' % args)
330 else:
331 self.script.append(
332 'package_extract_file("%(fn)s", "%(device)s");' % args)
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700333 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700334 raise ValueError(
335 "don't know how to write \"%s\" partitions" % p.fs_type)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700336
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700337 def SetPermissions(self, fn, uid, gid, mode, selabel, capabilities):
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700338 """Set file ownership and permissions."""
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700339 if not self.info.get("use_set_metadata", False):
340 self.script.append('set_perm(%d, %d, 0%o, "%s");' % (uid, gid, mode, fn))
341 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700342 if capabilities is None:
343 capabilities = "0x0"
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700344 cmd = 'set_metadata("%s", "uid", %d, "gid", %d, "mode", 0%o, ' \
345 '"capabilities", %s' % (fn, uid, gid, mode, capabilities)
346 if selabel is not None:
Dan Albert8b72aef2015-03-23 19:13:21 -0700347 cmd += ', "selabel", "%s"' % selabel
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700348 cmd += ');'
349 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700350
Dan Albert8b72aef2015-03-23 19:13:21 -0700351 def SetPermissionsRecursive(self, fn, uid, gid, dmode, fmode, selabel,
352 capabilities):
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700353 """Recursively set path ownership and permissions."""
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700354 if not self.info.get("use_set_metadata", False):
355 self.script.append('set_perm_recursive(%d, %d, 0%o, 0%o, "%s");'
356 % (uid, gid, dmode, fmode, fn))
357 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700358 if capabilities is None:
359 capabilities = "0x0"
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700360 cmd = 'set_metadata_recursive("%s", "uid", %d, "gid", %d, ' \
361 '"dmode", 0%o, "fmode", 0%o, "capabilities", %s' \
362 % (fn, uid, gid, dmode, fmode, capabilities)
363 if selabel is not None:
Dan Albert8b72aef2015-03-23 19:13:21 -0700364 cmd += ', "selabel", "%s"' % selabel
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700365 cmd += ');'
366 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700367
368 def MakeSymlinks(self, symlink_list):
369 """Create symlinks, given a list of (dest, link) pairs."""
370 by_dest = {}
371 for d, l in symlink_list:
372 by_dest.setdefault(d, []).append(l)
373
374 for dest, links in sorted(by_dest.iteritems()):
375 cmd = ('symlink("%s", ' % (dest,) +
376 ",\0".join(['"' + i + '"' for i in sorted(links)]) + ");")
Dan Albert8b72aef2015-03-23 19:13:21 -0700377 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700378
379 def AppendExtra(self, extra):
380 """Append text verbatim to the output script."""
381 self.script.append(extra)
382
Michael Runge63f01de2014-10-28 19:24:19 -0700383 def Unmount(self, mount_point):
Dan Albert8b72aef2015-03-23 19:13:21 -0700384 self.script.append('unmount("%s");' % mount_point)
385 self.mounts.remove(mount_point)
Michael Runge63f01de2014-10-28 19:24:19 -0700386
Doug Zongker14833602010-02-02 13:12:04 -0800387 def UnmountAll(self):
388 for p in sorted(self.mounts):
389 self.script.append('unmount("%s");' % (p,))
390 self.mounts = set()
391
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700392 def AddToZip(self, input_zip, output_zip, input_path=None):
393 """Write the accumulated script to the output_zip file. input_zip
394 is used as the source for the 'updater' binary needed to run
395 script. If input_path is not None, it will be used as a local
396 path for the binary instead of input_zip."""
397
Doug Zongker14833602010-02-02 13:12:04 -0800398 self.UnmountAll()
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700399
400 common.ZipWriteStr(output_zip, "META-INF/com/google/android/updater-script",
401 "\n".join(self.script) + "\n")
402
403 if input_path is None:
404 data = input_zip.read("OTA/bin/updater")
405 else:
Doug Zongker25568482014-03-03 10:21:27 -0800406 data = open(input_path, "rb").read()
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700407 common.ZipWriteStr(output_zip, "META-INF/com/google/android/update-binary",
Dan Albert8b72aef2015-03-23 19:13:21 -0700408 data, perms=0o755)