blob: a2a32a7a303f57b32cc4827472991ebe18767133 [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()
26 self.version = version
Doug Zongkerb4c7d322010-07-01 15:30:11 -070027 self.info = info
Tao Bao34b47bf2015-06-22 19:17:41 -070028 if fstab is None:
29 self.fstab = self.info.get("fstab", None)
30 else:
31 self.fstab = fstab
Doug Zongkerc494d7c2009-06-18 08:43:44 -070032
33 def MakeTemporary(self):
34 """Make a temporary script object whose commands can latter be
35 appended to the parent script with AppendScript(). Used when the
36 caller wants to generate script commands out-of-order."""
Doug Zongker67369982010-07-07 13:53:32 -070037 x = EdifyGenerator(self.version, self.info)
Doug Zongkerc494d7c2009-06-18 08:43:44 -070038 x.mounts = self.mounts
39 return x
40
41 @staticmethod
Dan Albert8b72aef2015-03-23 19:13:21 -070042 def WordWrap(cmd, linelen=80):
Doug Zongkerc494d7c2009-06-18 08:43:44 -070043 """'cmd' should be a function call with null characters after each
44 parameter (eg, "somefun(foo,\0bar,\0baz)"). This function wraps cmd
45 to a given line length, replacing nulls with spaces and/or newlines
46 to format it nicely."""
47 indent = cmd.index("(")+1
48 out = []
49 first = True
50 x = re.compile("^(.{,%d})\0" % (linelen-indent,))
51 while True:
52 if not first:
53 out.append(" " * indent)
54 first = False
55 m = x.search(cmd)
56 if not m:
57 parts = cmd.split("\0", 1)
58 out.append(parts[0]+"\n")
59 if len(parts) == 1:
60 break
61 else:
62 cmd = parts[1]
63 continue
64 out.append(m.group(1)+"\n")
65 cmd = cmd[m.end():]
66
67 return "".join(out).replace("\0", " ").rstrip("\n")
68
69 def AppendScript(self, other):
70 """Append the contents of another script (which should be created
71 with temporary=True) to this one."""
72 self.script.extend(other.script)
73
Michael Runge6e836112014-04-15 17:40:21 -070074 def AssertOemProperty(self, name, value):
75 """Assert that a property on the OEM paritition matches a value."""
76 if not name:
77 raise ValueError("must specify an OEM property")
78 if not value:
79 raise ValueError("must specify the OEM value")
Tao Bao3910ebf2015-03-22 14:20:48 -070080 cmd = ('file_getprop("/oem/oem.prop", "{name}") == "{value}" || '
81 'abort("This package expects the value \\"{value}\\" for '
82 '\\"{name}\\" on the OEM partition; this has value \\"" + '
Dan Albert8b72aef2015-03-23 19:13:21 -070083 'file_getprop("/oem/oem.prop", "{name}") + "\\".");').format(
84 name=name, value=value)
Michael Runge6e836112014-04-15 17:40:21 -070085 self.script.append(cmd)
86
Doug Zongkerc494d7c2009-06-18 08:43:44 -070087 def AssertSomeFingerprint(self, *fp):
Doug Zongkeraf845252014-05-09 08:29:05 -070088 """Assert that the current recovery build fingerprint is one of *fp."""
Doug Zongkerc494d7c2009-06-18 08:43:44 -070089 if not fp:
90 raise ValueError("must specify some fingerprints")
Dan Albert8b72aef2015-03-23 19:13:21 -070091 cmd = (' ||\n '.join([('getprop("ro.build.fingerprint") == "%s"') % i
92 for i in fp]) +
Doug Zongker0d92f1f2013-06-03 12:07:12 -070093 ' ||\n abort("Package expects build fingerprint of %s; this '
Dan Albert8b72aef2015-03-23 19:13:21 -070094 'device has " + getprop("ro.build.fingerprint") + ".");') % (
95 " or ".join(fp))
Doug Zongker0d92f1f2013-06-03 12:07:12 -070096 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -070097
Michael Runge6e836112014-04-15 17:40:21 -070098 def AssertSomeThumbprint(self, *fp):
Doug Zongkeraf845252014-05-09 08:29:05 -070099 """Assert that the current recovery build thumbprint is one of *fp."""
Geremy Condra36bd3652014-02-06 19:45:10 -0800100 if not fp:
Michael Runge6e836112014-04-15 17:40:21 -0700101 raise ValueError("must specify some thumbprints")
Dan Albert8b72aef2015-03-23 19:13:21 -0700102 cmd = (' ||\n '.join([('getprop("ro.build.thumbprint") == "%s"') % i
103 for i in fp]) +
Michael Runge6e836112014-04-15 17:40:21 -0700104 ' ||\n abort("Package expects build thumbprint of %s; this '
Dan Albert8b72aef2015-03-23 19:13:21 -0700105 'device has " + getprop("ro.build.thumbprint") + ".");') % (
106 " or ".join(fp))
Geremy Condra36bd3652014-02-06 19:45:10 -0800107 self.script.append(cmd)
108
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700109 def AssertOlderBuild(self, timestamp, timestamp_text):
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700110 """Assert that the build on the device is older (or the same as)
111 the given timestamp."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700112 self.script.append(
113 ('(!less_than_int(%s, getprop("ro.build.date.utc"))) || '
114 'abort("Can\'t install this package (%s) over newer '
Dan Albert8b72aef2015-03-23 19:13:21 -0700115 'build (" + getprop("ro.build.date") + ").");') % (timestamp,
116 timestamp_text))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700117
118 def AssertDevice(self, device):
119 """Assert that the device identifier is the given string."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700120 cmd = ('getprop("ro.product.device") == "%s" || '
121 'abort("This package is for \\"%s\\" devices; '
Dan Albert8b72aef2015-03-23 19:13:21 -0700122 'this is a \\"" + getprop("ro.product.device") + "\\".");') % (
123 device, device)
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700124 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700125
126 def AssertSomeBootloader(self, *bootloaders):
127 """Asert that the bootloader version is one of *bootloaders."""
128 cmd = ("assert(" +
129 " ||\0".join(['getprop("ro.bootloader") == "%s"' % (b,)
130 for b in bootloaders]) +
131 ");")
Dan Albert8b72aef2015-03-23 19:13:21 -0700132 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700133
134 def ShowProgress(self, frac, dur):
135 """Update the progress bar, advancing it over 'frac' over the next
Doug Zongker881dd402009-09-20 14:03:55 -0700136 'dur' seconds. 'dur' may be zero to advance it via SetProgress
137 commands instead of by time."""
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700138 self.script.append("show_progress(%f, %d);" % (frac, int(dur)))
139
Doug Zongker881dd402009-09-20 14:03:55 -0700140 def SetProgress(self, frac):
141 """Set the position of the progress bar within the chunk defined
142 by the most recent ShowProgress call. 'frac' should be in
143 [0,1]."""
144 self.script.append("set_progress(%f);" % (frac,))
145
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700146 def PatchCheck(self, filename, *sha1):
147 """Check that the given file (or MTD reference) has one of the
Doug Zongkerc8d446b2010-02-22 15:41:53 -0800148 given *sha1 hashes, checking the version saved in cache if the
149 file does not match."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700150 self.script.append(
151 'apply_patch_check("%s"' % (filename,) +
152 "".join([', "%s"' % (i,) for i in sha1]) +
153 ') || abort("\\"%s\\" has unexpected contents.");' % (filename,))
Doug Zongkerc8d446b2010-02-22 15:41:53 -0800154
155 def FileCheck(self, filename, *sha1):
156 """Check that the given file (or MTD reference) has one of the
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700157 given *sha1 hashes."""
Doug Zongker5a482092010-02-17 16:09:18 -0800158 self.script.append('assert(sha1_check(read_file("%s")' % (filename,) +
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700159 "".join([', "%s"' % (i,) for i in sha1]) +
160 '));')
161
162 def CacheFreeSpaceCheck(self, amount):
163 """Check that there's at least 'amount' space that can be made
164 available on /cache."""
Doug Zongker0d92f1f2013-06-03 12:07:12 -0700165 self.script.append(('apply_patch_space(%d) || abort("Not enough free space '
Tao Baoe7b10372015-06-03 09:24:08 -0700166 'on /cache to apply patches.");') % (amount,))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700167
Michael Runge7cd99ba2014-10-22 17:21:48 -0700168 def Mount(self, mount_point, mount_options_by_format=""):
169 """Mount the partition with the given mount_point.
170 mount_options_by_format:
171 [fs_type=option[,option]...[|fs_type=option[,option]...]...]
172 where option is optname[=optvalue]
173 E.g. ext4=barrier=1,nodelalloc,errors=panic|f2fs=errors=recover
174 """
Tao Bao34b47bf2015-06-22 19:17:41 -0700175 fstab = self.fstab
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700176 if fstab:
177 p = fstab[mount_point]
Michael Runge7cd99ba2014-10-22 17:21:48 -0700178 mount_dict = {}
179 if mount_options_by_format is not None:
180 for option in mount_options_by_format.split("|"):
181 if "=" in option:
182 key, value = option.split("=", 1)
183 mount_dict[key] = value
Dan Albert8b72aef2015-03-23 19:13:21 -0700184 self.script.append('mount("%s", "%s", "%s", "%s", "%s");' % (
185 p.fs_type, common.PARTITION_TYPES[p.fs_type], p.device,
186 p.mount_point, mount_dict.get(p.fs_type, "")))
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700187 self.mounts.add(p.mount_point)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700188
189 def UnpackPackageDir(self, src, dst):
190 """Unpack a given directory from the OTA package into the given
191 destination directory."""
192 self.script.append('package_extract_dir("%s", "%s");' % (src, dst))
193
194 def Comment(self, comment):
195 """Write a comment into the update script."""
196 self.script.append("")
197 for i in comment.split("\n"):
198 self.script.append("# " + i)
199 self.script.append("")
200
201 def Print(self, message):
202 """Log a message to the screen (if the logs are visible)."""
203 self.script.append('ui_print("%s");' % (message,))
204
Michael Runge3e286642014-11-21 00:46:03 -0800205 def TunePartition(self, partition, *options):
Tao Bao34b47bf2015-06-22 19:17:41 -0700206 fstab = self.fstab
Michael Runge3e286642014-11-21 00:46:03 -0800207 if fstab:
208 p = fstab[partition]
Dan Albert8b72aef2015-03-23 19:13:21 -0700209 if p.fs_type not in ("ext2", "ext3", "ext4"):
Michael Runge3e286642014-11-21 00:46:03 -0800210 raise ValueError("Partition %s cannot be tuned\n" % (partition,))
Dan Albert8b72aef2015-03-23 19:13:21 -0700211 self.script.append(
212 'tune2fs(' + "".join(['"%s", ' % (i,) for i in options]) +
213 '"%s") || abort("Failed to tune partition %s");' % (
214 p.device, partition))
Michael Runge3e286642014-11-21 00:46:03 -0800215
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700216 def FormatPartition(self, partition):
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700217 """Format the given partition, specified by its mount point (eg,
218 "/system")."""
219
Tao Bao34b47bf2015-06-22 19:17:41 -0700220 fstab = self.fstab
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700221 if fstab:
222 p = fstab[partition]
Doug Zongkerdf2056e2012-04-09 12:27:43 -0700223 self.script.append('format("%s", "%s", "%s", "%s", "%s");' %
Doug Zongker086cbb02011-02-17 15:54:20 -0800224 (p.fs_type, common.PARTITION_TYPES[p.fs_type],
Doug Zongkerdf2056e2012-04-09 12:27:43 -0700225 p.device, p.length, p.mount_point))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700226
Doug Zongker5fad2032014-02-24 08:13:45 -0800227 def WipeBlockDevice(self, partition):
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700228 if partition not in ("/system", "/vendor"):
229 raise ValueError(("WipeBlockDevice doesn't work on %s\n") % (partition,))
Tao Bao34b47bf2015-06-22 19:17:41 -0700230 fstab = self.fstab
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700231 size = self.info.get(partition.lstrip("/") + "_size", None)
Doug Zongker5fad2032014-02-24 08:13:45 -0800232 device = fstab[partition].device
233
234 self.script.append('wipe_block_device("%s", %s);' % (device, size))
235
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700236 def DeleteFiles(self, file_list):
237 """Delete all files in file_list."""
Dan Albert8b72aef2015-03-23 19:13:21 -0700238 if not file_list:
239 return
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700240 cmd = "delete(" + ",\0".join(['"%s"' % (i,) for i in file_list]) + ");"
Dan Albert8b72aef2015-03-23 19:13:21 -0700241 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700242
Michael Runge4038aa82013-12-13 18:06:28 -0800243 def RenameFile(self, srcfile, tgtfile):
244 """Moves a file from one location to another."""
245 if self.info.get("update_rename_support", False):
246 self.script.append('rename("%s", "%s");' % (srcfile, tgtfile))
247 else:
248 raise ValueError("Rename not supported by update binary")
249
250 def SkipNextActionIfTargetExists(self, tgtfile, tgtsha1):
251 """Prepend an action with an apply_patch_check in order to
252 skip the action if the file exists. Used when a patch
253 is later renamed."""
254 cmd = ('sha1_check(read_file("%s"), %s) || ' % (tgtfile, tgtsha1))
Dan Albert8b72aef2015-03-23 19:13:21 -0700255 self.script.append(self.WordWrap(cmd))
Michael Runge4038aa82013-12-13 18:06:28 -0800256
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700257 def ApplyPatch(self, srcfile, tgtfile, tgtsize, tgtsha1, *patchpairs):
258 """Apply binary patches (in *patchpairs) to the given srcfile to
259 produce tgtfile (which may be "-" to indicate overwriting the
260 source file."""
261 if len(patchpairs) % 2 != 0 or len(patchpairs) == 0:
262 raise ValueError("bad patches given to ApplyPatch")
263 cmd = ['apply_patch("%s",\0"%s",\0%s,\0%d'
264 % (srcfile, tgtfile, tgtsha1, tgtsize)]
265 for i in range(0, len(patchpairs), 2):
Doug Zongkerc8d446b2010-02-22 15:41:53 -0800266 cmd.append(',\0%s, package_extract_file("%s")' % patchpairs[i:i+2])
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700267 cmd.append(');')
268 cmd = "".join(cmd)
Dan Albert8b72aef2015-03-23 19:13:21 -0700269 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700270
Doug Zongker5fad2032014-02-24 08:13:45 -0800271 def WriteRawImage(self, mount_point, fn, mapfn=None):
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700272 """Write the given package file into the partition for the given
273 mount point."""
Doug Zongkerb4c7d322010-07-01 15:30:11 -0700274
Tao Bao34b47bf2015-06-22 19:17:41 -0700275 fstab = self.fstab
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700276 if fstab:
277 p = fstab[mount_point]
Doug Zongker96a57e72010-09-26 14:57:41 -0700278 partition_type = common.PARTITION_TYPES[p.fs_type]
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700279 args = {'device': p.device, 'fn': fn}
280 if partition_type == "MTD":
281 self.script.append(
Doug Zongker02da2102011-04-12 15:50:17 -0700282 'write_raw_image(package_extract_file("%(fn)s"), "%(device)s");'
283 % args)
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700284 elif partition_type == "EMMC":
Doug Zongker5fad2032014-02-24 08:13:45 -0800285 if mapfn:
286 args["map"] = mapfn
287 self.script.append(
288 'package_extract_file("%(fn)s", "%(device)s", "%(map)s");' % args)
289 else:
290 self.script.append(
291 'package_extract_file("%(fn)s", "%(device)s");' % args)
Doug Zongker9ce0fb62010-09-20 18:04:41 -0700292 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700293 raise ValueError(
294 "don't know how to write \"%s\" partitions" % p.fs_type)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700295
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700296 def SetPermissions(self, fn, uid, gid, mode, selabel, capabilities):
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700297 """Set file ownership and permissions."""
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700298 if not self.info.get("use_set_metadata", False):
299 self.script.append('set_perm(%d, %d, 0%o, "%s");' % (uid, gid, mode, fn))
300 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700301 if capabilities is None:
302 capabilities = "0x0"
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700303 cmd = 'set_metadata("%s", "uid", %d, "gid", %d, "mode", 0%o, ' \
304 '"capabilities", %s' % (fn, uid, gid, mode, capabilities)
305 if selabel is not None:
Dan Albert8b72aef2015-03-23 19:13:21 -0700306 cmd += ', "selabel", "%s"' % selabel
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700307 cmd += ');'
308 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700309
Dan Albert8b72aef2015-03-23 19:13:21 -0700310 def SetPermissionsRecursive(self, fn, uid, gid, dmode, fmode, selabel,
311 capabilities):
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700312 """Recursively set path ownership and permissions."""
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700313 if not self.info.get("use_set_metadata", False):
314 self.script.append('set_perm_recursive(%d, %d, 0%o, 0%o, "%s");'
315 % (uid, gid, dmode, fmode, fn))
316 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700317 if capabilities is None:
318 capabilities = "0x0"
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700319 cmd = 'set_metadata_recursive("%s", "uid", %d, "gid", %d, ' \
320 '"dmode", 0%o, "fmode", 0%o, "capabilities", %s' \
321 % (fn, uid, gid, dmode, fmode, capabilities)
322 if selabel is not None:
Dan Albert8b72aef2015-03-23 19:13:21 -0700323 cmd += ', "selabel", "%s"' % selabel
Nick Kralevich0eb17d92013-09-07 17:10:29 -0700324 cmd += ');'
325 self.script.append(cmd)
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700326
327 def MakeSymlinks(self, symlink_list):
328 """Create symlinks, given a list of (dest, link) pairs."""
329 by_dest = {}
330 for d, l in symlink_list:
331 by_dest.setdefault(d, []).append(l)
332
333 for dest, links in sorted(by_dest.iteritems()):
334 cmd = ('symlink("%s", ' % (dest,) +
335 ",\0".join(['"' + i + '"' for i in sorted(links)]) + ");")
Dan Albert8b72aef2015-03-23 19:13:21 -0700336 self.script.append(self.WordWrap(cmd))
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700337
338 def AppendExtra(self, extra):
339 """Append text verbatim to the output script."""
340 self.script.append(extra)
341
Michael Runge63f01de2014-10-28 19:24:19 -0700342 def Unmount(self, mount_point):
Dan Albert8b72aef2015-03-23 19:13:21 -0700343 self.script.append('unmount("%s");' % mount_point)
344 self.mounts.remove(mount_point)
Michael Runge63f01de2014-10-28 19:24:19 -0700345
Doug Zongker14833602010-02-02 13:12:04 -0800346 def UnmountAll(self):
347 for p in sorted(self.mounts):
348 self.script.append('unmount("%s");' % (p,))
349 self.mounts = set()
350
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700351 def AddToZip(self, input_zip, output_zip, input_path=None):
352 """Write the accumulated script to the output_zip file. input_zip
353 is used as the source for the 'updater' binary needed to run
354 script. If input_path is not None, it will be used as a local
355 path for the binary instead of input_zip."""
356
Doug Zongker14833602010-02-02 13:12:04 -0800357 self.UnmountAll()
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700358
359 common.ZipWriteStr(output_zip, "META-INF/com/google/android/updater-script",
360 "\n".join(self.script) + "\n")
361
362 if input_path is None:
363 data = input_zip.read("OTA/bin/updater")
364 else:
Doug Zongker25568482014-03-03 10:21:27 -0800365 data = open(input_path, "rb").read()
Doug Zongkerc494d7c2009-06-18 08:43:44 -0700366 common.ZipWriteStr(output_zip, "META-INF/com/google/android/update-binary",
Dan Albert8b72aef2015-03-23 19:13:21 -0700367 data, perms=0o755)