2009-06-18 17:43:44 +02:00
|
|
|
# Copyright (C) 2009 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
import common
|
|
|
|
|
|
|
|
class EdifyGenerator(object):
|
|
|
|
"""Class to generate scripts in the 'edify' recovery script language
|
|
|
|
used from donut onwards."""
|
|
|
|
|
2015-06-23 04:17:41 +02:00
|
|
|
def __init__(self, version, info, fstab=None):
|
2009-06-18 17:43:44 +02:00
|
|
|
self.script = []
|
|
|
|
self.mounts = set()
|
2016-02-04 23:26:02 +01:00
|
|
|
self._required_cache = 0
|
2009-06-18 17:43:44 +02:00
|
|
|
self.version = version
|
2010-07-02 00:30:11 +02:00
|
|
|
self.info = info
|
2015-06-23 04:17:41 +02:00
|
|
|
if fstab is None:
|
|
|
|
self.fstab = self.info.get("fstab", None)
|
|
|
|
else:
|
|
|
|
self.fstab = fstab
|
2009-06-18 17:43:44 +02:00
|
|
|
|
2016-02-04 23:26:02 +01:00
|
|
|
@property
|
|
|
|
def required_cache(self):
|
|
|
|
"""Return the minimum cache size to apply the update."""
|
|
|
|
return self._required_cache
|
|
|
|
|
2009-06-18 17:43:44 +02:00
|
|
|
@staticmethod
|
2015-03-24 03:13:21 +01:00
|
|
|
def WordWrap(cmd, linelen=80):
|
2009-06-18 17:43:44 +02:00
|
|
|
"""'cmd' should be a function call with null characters after each
|
|
|
|
parameter (eg, "somefun(foo,\0bar,\0baz)"). This function wraps cmd
|
|
|
|
to a given line length, replacing nulls with spaces and/or newlines
|
|
|
|
to format it nicely."""
|
|
|
|
indent = cmd.index("(")+1
|
|
|
|
out = []
|
|
|
|
first = True
|
|
|
|
x = re.compile("^(.{,%d})\0" % (linelen-indent,))
|
|
|
|
while True:
|
|
|
|
if not first:
|
|
|
|
out.append(" " * indent)
|
|
|
|
first = False
|
|
|
|
m = x.search(cmd)
|
|
|
|
if not m:
|
|
|
|
parts = cmd.split("\0", 1)
|
|
|
|
out.append(parts[0]+"\n")
|
|
|
|
if len(parts) == 1:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
cmd = parts[1]
|
|
|
|
continue
|
|
|
|
out.append(m.group(1)+"\n")
|
|
|
|
cmd = cmd[m.end():]
|
|
|
|
|
|
|
|
return "".join(out).replace("\0", " ").rstrip("\n")
|
|
|
|
|
|
|
|
def AppendScript(self, other):
|
|
|
|
"""Append the contents of another script (which should be created
|
|
|
|
with temporary=True) to this one."""
|
|
|
|
self.script.extend(other.script)
|
|
|
|
|
2017-12-21 20:23:09 +01:00
|
|
|
def AssertOemProperty(self, name, values, oem_no_mount):
|
2017-02-16 22:06:55 +01:00
|
|
|
"""Assert that a property on the OEM paritition matches allowed values."""
|
2014-04-16 02:40:21 +02:00
|
|
|
if not name:
|
|
|
|
raise ValueError("must specify an OEM property")
|
2017-02-16 22:06:55 +01:00
|
|
|
if not values:
|
2014-04-16 02:40:21 +02:00
|
|
|
raise ValueError("must specify the OEM value")
|
2017-12-21 20:23:09 +01:00
|
|
|
|
|
|
|
if oem_no_mount:
|
2017-02-16 22:06:55 +01:00
|
|
|
get_prop_command = 'getprop("%s")' % name
|
2016-02-26 04:49:55 +01:00
|
|
|
else:
|
2017-02-16 22:06:55 +01:00
|
|
|
get_prop_command = 'file_getprop("/oem/oem.prop", "%s")' % name
|
|
|
|
|
|
|
|
cmd = ''
|
|
|
|
for value in values:
|
|
|
|
cmd += '%s == "%s" || ' % (get_prop_command, value)
|
|
|
|
cmd += (
|
|
|
|
'abort("E{code}: This package expects the value \\"{values}\\" for '
|
|
|
|
'\\"{name}\\"; this has value \\"" + '
|
|
|
|
'{get_prop_command} + "\\".");').format(
|
2023-11-08 03:21:39 +01:00
|
|
|
code=common.ErrorCode.OEM_PROP_MISMATCH,
|
2017-02-16 22:06:55 +01:00
|
|
|
get_prop_command=get_prop_command, name=name,
|
|
|
|
values='\\" or \\"'.join(values))
|
2014-04-16 02:40:21 +02:00
|
|
|
self.script.append(cmd)
|
|
|
|
|
2009-06-18 17:43:44 +02:00
|
|
|
def AssertSomeFingerprint(self, *fp):
|
2014-05-09 17:29:05 +02:00
|
|
|
"""Assert that the current recovery build fingerprint is one of *fp."""
|
2009-06-18 17:43:44 +02:00
|
|
|
if not fp:
|
|
|
|
raise ValueError("must specify some fingerprints")
|
2015-03-24 03:13:21 +01:00
|
|
|
cmd = (' ||\n '.join([('getprop("ro.build.fingerprint") == "%s"') % i
|
|
|
|
for i in fp]) +
|
2016-05-25 02:34:52 +02:00
|
|
|
' ||\n abort("E%d: Package expects build fingerprint of %s; '
|
|
|
|
'this device has " + getprop("ro.build.fingerprint") + ".");') % (
|
2023-11-08 03:21:39 +01:00
|
|
|
common.ErrorCode.FINGERPRINT_MISMATCH, " or ".join(fp))
|
2013-06-03 21:07:12 +02:00
|
|
|
self.script.append(cmd)
|
2009-06-18 17:43:44 +02:00
|
|
|
|
2014-04-16 02:40:21 +02:00
|
|
|
def AssertSomeThumbprint(self, *fp):
|
2014-05-09 17:29:05 +02:00
|
|
|
"""Assert that the current recovery build thumbprint is one of *fp."""
|
2014-02-07 04:45:10 +01:00
|
|
|
if not fp:
|
2014-04-16 02:40:21 +02:00
|
|
|
raise ValueError("must specify some thumbprints")
|
2015-03-24 03:13:21 +01:00
|
|
|
cmd = (' ||\n '.join([('getprop("ro.build.thumbprint") == "%s"') % i
|
|
|
|
for i in fp]) +
|
2016-05-25 02:34:52 +02:00
|
|
|
' ||\n abort("E%d: Package expects build thumbprint of %s; this '
|
2015-03-24 03:13:21 +01:00
|
|
|
'device has " + getprop("ro.build.thumbprint") + ".");') % (
|
2023-11-08 03:21:39 +01:00
|
|
|
common.ErrorCode.THUMBPRINT_MISMATCH, " or ".join(fp))
|
2014-02-07 04:45:10 +01:00
|
|
|
self.script.append(cmd)
|
|
|
|
|
2016-03-15 21:20:19 +01:00
|
|
|
def AssertFingerprintOrThumbprint(self, fp, tp):
|
|
|
|
"""Assert that the current recovery build fingerprint is fp, or thumbprint
|
|
|
|
is tp."""
|
|
|
|
cmd = ('getprop("ro.build.fingerprint") == "{fp}" ||\n'
|
|
|
|
' getprop("ro.build.thumbprint") == "{tp}" ||\n'
|
|
|
|
' abort("Package expects build fingerprint of {fp} or '
|
|
|
|
'thumbprint of {tp}; this device has a fingerprint of " '
|
2017-06-05 21:25:46 +02:00
|
|
|
'+ getprop("ro.build.fingerprint") + " and a thumbprint of " '
|
2016-03-15 21:20:19 +01:00
|
|
|
'+ getprop("ro.build.thumbprint") + ".");').format(fp=fp, tp=tp)
|
|
|
|
self.script.append(cmd)
|
|
|
|
|
2013-06-03 21:07:12 +02:00
|
|
|
def AssertOlderBuild(self, timestamp, timestamp_text):
|
2009-06-18 17:43:44 +02:00
|
|
|
"""Assert that the build on the device is older (or the same as)
|
|
|
|
the given timestamp."""
|
2013-06-03 21:07:12 +02:00
|
|
|
self.script.append(
|
|
|
|
('(!less_than_int(%s, getprop("ro.build.date.utc"))) || '
|
2016-05-25 02:34:52 +02:00
|
|
|
'abort("E%d: Can\'t install this package (%s) over newer '
|
2017-11-21 18:25:31 +01:00
|
|
|
'build (" + getprop("ro.build.date") + ").");') % (
|
2023-11-08 03:21:39 +01:00
|
|
|
timestamp, common.ErrorCode.OLDER_BUILD, timestamp_text))
|
2009-06-18 17:43:44 +02:00
|
|
|
|
|
|
|
def AssertDevice(self, device):
|
|
|
|
"""Assert that the device identifier is the given string."""
|
2013-06-03 21:07:12 +02:00
|
|
|
cmd = ('getprop("ro.product.device") == "%s" || '
|
2016-05-25 02:34:52 +02:00
|
|
|
'abort("E%d: This package is for \\"%s\\" devices; '
|
2015-03-24 03:13:21 +01:00
|
|
|
'this is a \\"" + getprop("ro.product.device") + "\\".");') % (
|
2023-11-08 03:21:39 +01:00
|
|
|
device, common.ErrorCode.DEVICE_MISMATCH, device)
|
2013-06-03 21:07:12 +02:00
|
|
|
self.script.append(cmd)
|
2009-06-18 17:43:44 +02:00
|
|
|
|
|
|
|
def AssertSomeBootloader(self, *bootloaders):
|
|
|
|
"""Asert that the bootloader version is one of *bootloaders."""
|
|
|
|
cmd = ("assert(" +
|
|
|
|
" ||\0".join(['getprop("ro.bootloader") == "%s"' % (b,)
|
|
|
|
for b in bootloaders]) +
|
|
|
|
");")
|
2015-03-24 03:13:21 +01:00
|
|
|
self.script.append(self.WordWrap(cmd))
|
2009-06-18 17:43:44 +02:00
|
|
|
|
|
|
|
def ShowProgress(self, frac, dur):
|
|
|
|
"""Update the progress bar, advancing it over 'frac' over the next
|
2009-09-20 23:03:55 +02:00
|
|
|
'dur' seconds. 'dur' may be zero to advance it via SetProgress
|
|
|
|
commands instead of by time."""
|
2009-06-18 17:43:44 +02:00
|
|
|
self.script.append("show_progress(%f, %d);" % (frac, int(dur)))
|
|
|
|
|
2009-09-20 23:03:55 +02:00
|
|
|
def SetProgress(self, frac):
|
|
|
|
"""Set the position of the progress bar within the chunk defined
|
|
|
|
by the most recent ShowProgress call. 'frac' should be in
|
|
|
|
[0,1]."""
|
|
|
|
self.script.append("set_progress(%f);" % (frac,))
|
|
|
|
|
2018-08-26 20:53:15 +02:00
|
|
|
def PatchCheck(self, filename, *sha1): # pylint: disable=unused-argument
|
|
|
|
"""Checks that the given partition has the desired checksum.
|
|
|
|
|
|
|
|
The call to this function is being deprecated in favor of
|
|
|
|
PatchPartitionCheck(). It will try to parse and handle the old format,
|
|
|
|
unless the format is unknown.
|
|
|
|
"""
|
|
|
|
tokens = filename.split(':')
|
|
|
|
assert len(tokens) == 6 and tokens[0] == 'EMMC', \
|
|
|
|
"Failed to handle unknown format. Use PatchPartitionCheck() instead."
|
|
|
|
source = '{}:{}:{}:{}'.format(tokens[0], tokens[1], tokens[2], tokens[3])
|
|
|
|
target = '{}:{}:{}:{}'.format(tokens[0], tokens[1], tokens[4], tokens[5])
|
|
|
|
self.PatchPartitionCheck(target, source)
|
|
|
|
|
|
|
|
def PatchPartitionCheck(self, target, source):
|
|
|
|
"""Checks whether updater can patch the given partitions.
|
|
|
|
|
|
|
|
It checks the checksums of the given partitions. If none of them matches the
|
|
|
|
expected checksum, updater will additionally look for a backup on /cache.
|
|
|
|
"""
|
2020-05-07 21:38:53 +02:00
|
|
|
self._CheckSecondTokenNotSlotSuffixed(target, "PatchPartitionExprCheck")
|
|
|
|
self._CheckSecondTokenNotSlotSuffixed(source, "PatchPartitionExprCheck")
|
|
|
|
self.PatchPartitionExprCheck('"%s"' % target, '"%s"' % source)
|
|
|
|
|
|
|
|
def PatchPartitionExprCheck(self, target_expr, source_expr):
|
|
|
|
"""Checks whether updater can patch the given partitions.
|
|
|
|
|
|
|
|
It checks the checksums of the given partitions. If none of them matches the
|
|
|
|
expected checksum, updater will additionally look for a backup on /cache.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_expr: an Edify expression that serves as the target arg to
|
|
|
|
patch_partition. Must be evaluated to a string in the form of
|
|
|
|
foo:bar:baz:quux
|
|
|
|
source_expr: an Edify expression that serves as the source arg to
|
|
|
|
patch_partition. Must be evaluated to a string in the form of
|
|
|
|
foo:bar:baz:quux
|
|
|
|
"""
|
2018-08-26 20:53:15 +02:00
|
|
|
self.script.append(self.WordWrap((
|
2020-05-07 21:38:53 +02:00
|
|
|
'patch_partition_check({target},\0{source}) ||\n abort('
|
|
|
|
'concat("E{code}: \\"",{target},"\\" or \\"",{source},"\\" has '
|
|
|
|
'unexpected contents."));').format(
|
|
|
|
target=target_expr,
|
|
|
|
source=source_expr,
|
2023-11-08 03:21:39 +01:00
|
|
|
code=common.ErrorCode.BAD_PATCH_FILE)))
|
2010-02-23 00:41:53 +01:00
|
|
|
|
2009-06-18 17:43:44 +02:00
|
|
|
def CacheFreeSpaceCheck(self, amount):
|
|
|
|
"""Check that there's at least 'amount' space that can be made
|
|
|
|
available on /cache."""
|
2016-02-04 23:26:02 +01:00
|
|
|
self._required_cache = max(self._required_cache, amount)
|
2016-05-25 02:34:52 +02:00
|
|
|
self.script.append(('apply_patch_space(%d) || abort("E%d: Not enough free '
|
|
|
|
'space on /cache to apply patches.");') % (
|
|
|
|
amount,
|
2023-11-08 03:21:39 +01:00
|
|
|
common.ErrorCode.INSUFFICIENT_CACHE_SPACE))
|
2009-06-18 17:43:44 +02:00
|
|
|
|
2014-10-23 02:21:48 +02:00
|
|
|
def Mount(self, mount_point, mount_options_by_format=""):
|
|
|
|
"""Mount the partition with the given mount_point.
|
|
|
|
mount_options_by_format:
|
|
|
|
[fs_type=option[,option]...[|fs_type=option[,option]...]...]
|
|
|
|
where option is optname[=optvalue]
|
|
|
|
E.g. ext4=barrier=1,nodelalloc,errors=panic|f2fs=errors=recover
|
|
|
|
"""
|
2015-06-23 04:17:41 +02:00
|
|
|
fstab = self.fstab
|
2010-09-21 03:04:41 +02:00
|
|
|
if fstab:
|
|
|
|
p = fstab[mount_point]
|
2014-10-23 02:21:48 +02:00
|
|
|
mount_dict = {}
|
|
|
|
if mount_options_by_format is not None:
|
|
|
|
for option in mount_options_by_format.split("|"):
|
|
|
|
if "=" in option:
|
|
|
|
key, value = option.split("=", 1)
|
|
|
|
mount_dict[key] = value
|
2015-06-10 21:32:41 +02:00
|
|
|
mount_flags = mount_dict.get(p.fs_type, "")
|
|
|
|
if p.context is not None:
|
|
|
|
mount_flags = p.context + ("," + mount_flags if mount_flags else "")
|
2020-05-07 21:38:53 +02:00
|
|
|
self.script.append('mount("%s", "%s", %s, "%s", "%s");' % (
|
2023-11-08 03:19:31 +01:00
|
|
|
p.fs_type, common.PARTITION_TYPES[p.fs_type],
|
2020-05-07 21:38:53 +02:00
|
|
|
self._GetSlotSuffixDeviceForEntry(p),
|
2015-06-10 21:32:41 +02:00
|
|
|
p.mount_point, mount_flags))
|
2010-09-21 03:04:41 +02:00
|
|
|
self.mounts.add(p.mount_point)
|
2009-06-18 17:43:44 +02:00
|
|
|
|
|
|
|
def Comment(self, comment):
|
|
|
|
"""Write a comment into the update script."""
|
|
|
|
self.script.append("")
|
|
|
|
for i in comment.split("\n"):
|
|
|
|
self.script.append("# " + i)
|
|
|
|
self.script.append("")
|
|
|
|
|
|
|
|
def Print(self, message):
|
|
|
|
"""Log a message to the screen (if the logs are visible)."""
|
|
|
|
self.script.append('ui_print("%s");' % (message,))
|
|
|
|
|
2014-11-21 09:46:03 +01:00
|
|
|
def TunePartition(self, partition, *options):
|
2015-06-23 04:17:41 +02:00
|
|
|
fstab = self.fstab
|
2014-11-21 09:46:03 +01:00
|
|
|
if fstab:
|
|
|
|
p = fstab[partition]
|
2015-03-24 03:13:21 +01:00
|
|
|
if p.fs_type not in ("ext2", "ext3", "ext4"):
|
2014-11-21 09:46:03 +01:00
|
|
|
raise ValueError("Partition %s cannot be tuned\n" % (partition,))
|
2015-03-24 03:13:21 +01:00
|
|
|
self.script.append(
|
|
|
|
'tune2fs(' + "".join(['"%s", ' % (i,) for i in options]) +
|
2020-05-07 21:38:53 +02:00
|
|
|
'%s) || abort("E%d: Failed to tune partition %s");' % (
|
|
|
|
self._GetSlotSuffixDeviceForEntry(p),
|
2023-11-08 03:21:39 +01:00
|
|
|
common.ErrorCode.TUNE_PARTITION_FAILURE, partition))
|
2014-11-21 09:46:03 +01:00
|
|
|
|
2009-06-18 17:43:44 +02:00
|
|
|
def FormatPartition(self, partition):
|
2010-09-21 03:04:41 +02:00
|
|
|
"""Format the given partition, specified by its mount point (eg,
|
|
|
|
"/system")."""
|
|
|
|
|
2015-06-23 04:17:41 +02:00
|
|
|
fstab = self.fstab
|
2010-09-21 03:04:41 +02:00
|
|
|
if fstab:
|
|
|
|
p = fstab[partition]
|
2020-05-07 21:38:53 +02:00
|
|
|
self.script.append('format("%s", "%s", %s, "%s", "%s");' %
|
2023-11-08 03:19:31 +01:00
|
|
|
(p.fs_type, common.PARTITION_TYPES[p.fs_type],
|
2020-05-07 21:38:53 +02:00
|
|
|
self._GetSlotSuffixDeviceForEntry(p),
|
|
|
|
p.length, p.mount_point))
|
2009-06-18 17:43:44 +02:00
|
|
|
|
2014-02-24 17:13:45 +01:00
|
|
|
def WipeBlockDevice(self, partition):
|
2014-06-17 00:16:31 +02:00
|
|
|
if partition not in ("/system", "/vendor"):
|
|
|
|
raise ValueError(("WipeBlockDevice doesn't work on %s\n") % (partition,))
|
2015-06-23 04:17:41 +02:00
|
|
|
fstab = self.fstab
|
2014-06-17 00:16:31 +02:00
|
|
|
size = self.info.get(partition.lstrip("/") + "_size", None)
|
2020-05-07 21:38:53 +02:00
|
|
|
device = self._GetSlotSuffixDeviceForEntry(fstab[partition])
|
2014-02-24 17:13:45 +01:00
|
|
|
|
2020-05-07 21:38:53 +02:00
|
|
|
self.script.append('wipe_block_device(%s, %s);' % (device, size))
|
2014-02-24 17:13:45 +01:00
|
|
|
|
2009-06-18 17:43:44 +02:00
|
|
|
def ApplyPatch(self, srcfile, tgtfile, tgtsize, tgtsha1, *patchpairs):
|
|
|
|
"""Apply binary patches (in *patchpairs) to the given srcfile to
|
|
|
|
produce tgtfile (which may be "-" to indicate overwriting the
|
2018-08-26 20:53:15 +02:00
|
|
|
source file.
|
|
|
|
|
|
|
|
This edify function is being deprecated in favor of PatchPartition(). It
|
|
|
|
will try to redirect calls to PatchPartition() if possible. On unknown /
|
|
|
|
invalid inputs, raises an exception.
|
|
|
|
"""
|
|
|
|
tokens = srcfile.split(':')
|
|
|
|
assert (len(tokens) == 6 and tokens[0] == 'EMMC' and tgtfile == '-' and
|
|
|
|
len(patchpairs) == 2), \
|
|
|
|
"Failed to handle unknown format. Use PatchPartition() instead."
|
|
|
|
|
2020-07-23 01:21:22 +02:00
|
|
|
# Also validity check the args.
|
2018-08-26 20:53:15 +02:00
|
|
|
assert tokens[3] == patchpairs[0], \
|
|
|
|
"Found mismatching values for source SHA-1: {} vs {}".format(
|
|
|
|
tokens[3], patchpairs[0])
|
|
|
|
assert int(tokens[4]) == tgtsize, \
|
|
|
|
"Found mismatching values for target size: {} vs {}".format(
|
|
|
|
tokens[4], tgtsize)
|
|
|
|
assert tokens[5] == tgtsha1, \
|
|
|
|
"Found mismatching values for target SHA-1: {} vs {}".format(
|
|
|
|
tokens[5], tgtsha1)
|
|
|
|
|
|
|
|
source = '{}:{}:{}:{}'.format(tokens[0], tokens[1], tokens[2], tokens[3])
|
|
|
|
target = '{}:{}:{}:{}'.format(tokens[0], tokens[1], tokens[4], tokens[5])
|
|
|
|
patch = patchpairs[1]
|
|
|
|
self.PatchPartition(target, source, patch)
|
|
|
|
|
|
|
|
def PatchPartition(self, target, source, patch):
|
2020-05-07 21:38:53 +02:00
|
|
|
"""
|
|
|
|
Applies the patch to the source partition and writes it to target.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target: the target arg to patch_partition. Must be in the form of
|
|
|
|
foo:bar:baz:quux
|
|
|
|
source: the source arg to patch_partition. Must be in the form of
|
|
|
|
foo:bar:baz:quux
|
|
|
|
patch: the patch arg to patch_partition. Must be an unquoted string.
|
|
|
|
"""
|
|
|
|
self._CheckSecondTokenNotSlotSuffixed(target, "PatchPartitionExpr")
|
|
|
|
self._CheckSecondTokenNotSlotSuffixed(source, "PatchPartitionExpr")
|
|
|
|
self.PatchPartitionExpr('"%s"' % target, '"%s"' % source, '"%s"' % patch)
|
|
|
|
|
|
|
|
def PatchPartitionExpr(self, target_expr, source_expr, patch_expr):
|
|
|
|
"""
|
|
|
|
Applies the patch to the source partition and writes it to target.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
target_expr: an Edify expression that serves as the target arg to
|
|
|
|
patch_partition. Must be evaluated to a string in the form of
|
|
|
|
foo:bar:baz:quux
|
|
|
|
source_expr: an Edify expression that serves as the source arg to
|
|
|
|
patch_partition. Must be evaluated to a string in the form of
|
|
|
|
foo:bar:baz:quux
|
|
|
|
patch_expr: an Edify expression that serves as the patch arg to
|
|
|
|
patch_partition. Must be evaluated to a string.
|
|
|
|
"""
|
2018-08-26 20:53:15 +02:00
|
|
|
self.script.append(self.WordWrap((
|
2020-05-07 21:38:53 +02:00
|
|
|
'patch_partition({target},\0{source},\0'
|
|
|
|
'package_extract_file({patch})) ||\n'
|
|
|
|
' abort(concat('
|
|
|
|
' "E{code}: Failed to apply patch to ",{source}));').format(
|
|
|
|
target=target_expr,
|
|
|
|
source=source_expr,
|
|
|
|
patch=patch_expr,
|
2023-11-08 03:21:39 +01:00
|
|
|
code=common.ErrorCode.APPLY_PATCH_FAILURE)))
|
2009-06-18 17:43:44 +02:00
|
|
|
|
2020-05-07 21:38:53 +02:00
|
|
|
def _GetSlotSuffixDeviceForEntry(self, entry=None):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
entry: the fstab entry of device "foo"
|
|
|
|
Returns:
|
|
|
|
An edify expression. Caller must not quote result.
|
|
|
|
If foo is slot suffixed, it returns
|
|
|
|
'add_slot_suffix("foo")'
|
|
|
|
Otherwise it returns
|
|
|
|
'"foo"' (quoted)
|
|
|
|
"""
|
|
|
|
assert entry is not None
|
|
|
|
if entry.slotselect:
|
|
|
|
return 'add_slot_suffix("%s")' % entry.device
|
|
|
|
return '"%s"' % entry.device
|
|
|
|
|
|
|
|
def _CheckSecondTokenNotSlotSuffixed(self, s, fn):
|
|
|
|
lst = s.split(':')
|
2020-06-30 21:00:59 +02:00
|
|
|
assert(len(lst) == 4), "{} does not contain 4 tokens".format(s)
|
2020-05-07 21:38:53 +02:00
|
|
|
if self.fstab:
|
2020-06-30 21:00:59 +02:00
|
|
|
entry = common.GetEntryForDevice(self.fstab, lst[1])
|
2020-05-07 21:38:53 +02:00
|
|
|
if entry is not None:
|
|
|
|
assert not entry.slotselect, \
|
2020-06-30 21:00:59 +02:00
|
|
|
"Use %s because %s is slot suffixed" % (fn, lst[1])
|
2020-05-07 21:38:53 +02:00
|
|
|
|
2014-02-24 17:13:45 +01:00
|
|
|
def WriteRawImage(self, mount_point, fn, mapfn=None):
|
2010-09-21 03:04:41 +02:00
|
|
|
"""Write the given package file into the partition for the given
|
|
|
|
mount point."""
|
2010-07-02 00:30:11 +02:00
|
|
|
|
2015-06-23 04:17:41 +02:00
|
|
|
fstab = self.fstab
|
2010-09-21 03:04:41 +02:00
|
|
|
if fstab:
|
|
|
|
p = fstab[mount_point]
|
2023-11-08 03:19:31 +01:00
|
|
|
partition_type = common.PARTITION_TYPES[p.fs_type]
|
2020-05-07 21:38:53 +02:00
|
|
|
device = self._GetSlotSuffixDeviceForEntry(p)
|
|
|
|
args = {'device': device, 'fn': fn}
|
2016-06-16 02:04:54 +02:00
|
|
|
if partition_type == "EMMC":
|
2014-02-24 17:13:45 +01:00
|
|
|
if mapfn:
|
|
|
|
args["map"] = mapfn
|
|
|
|
self.script.append(
|
2020-05-07 21:38:53 +02:00
|
|
|
'package_extract_file("%(fn)s", %(device)s, "%(map)s");' % args)
|
2014-02-24 17:13:45 +01:00
|
|
|
else:
|
|
|
|
self.script.append(
|
2020-05-07 21:38:53 +02:00
|
|
|
'package_extract_file("%(fn)s", %(device)s);' % args)
|
2010-09-21 03:04:41 +02:00
|
|
|
else:
|
2015-03-24 03:13:21 +01:00
|
|
|
raise ValueError(
|
|
|
|
"don't know how to write \"%s\" partitions" % p.fs_type)
|
2009-06-18 17:43:44 +02:00
|
|
|
|
|
|
|
def AppendExtra(self, extra):
|
|
|
|
"""Append text verbatim to the output script."""
|
|
|
|
self.script.append(extra)
|
|
|
|
|
2014-10-29 03:24:19 +01:00
|
|
|
def Unmount(self, mount_point):
|
2015-03-24 03:13:21 +01:00
|
|
|
self.script.append('unmount("%s");' % mount_point)
|
|
|
|
self.mounts.remove(mount_point)
|
2014-10-29 03:24:19 +01:00
|
|
|
|
2010-02-02 22:12:04 +01:00
|
|
|
def UnmountAll(self):
|
|
|
|
for p in sorted(self.mounts):
|
|
|
|
self.script.append('unmount("%s");' % (p,))
|
|
|
|
self.mounts = set()
|
|
|
|
|
2009-06-18 17:43:44 +02:00
|
|
|
def AddToZip(self, input_zip, output_zip, input_path=None):
|
|
|
|
"""Write the accumulated script to the output_zip file. input_zip
|
|
|
|
is used as the source for the 'updater' binary needed to run
|
|
|
|
script. If input_path is not None, it will be used as a local
|
|
|
|
path for the binary instead of input_zip."""
|
|
|
|
|
2010-02-02 22:12:04 +01:00
|
|
|
self.UnmountAll()
|
2009-06-18 17:43:44 +02:00
|
|
|
|
|
|
|
common.ZipWriteStr(output_zip, "META-INF/com/google/android/updater-script",
|
|
|
|
"\n".join(self.script) + "\n")
|
|
|
|
|
|
|
|
if input_path is None:
|
|
|
|
data = input_zip.read("OTA/bin/updater")
|
|
|
|
else:
|
2014-03-03 19:21:27 +01:00
|
|
|
data = open(input_path, "rb").read()
|
2009-06-18 17:43:44 +02:00
|
|
|
common.ZipWriteStr(output_zip, "META-INF/com/google/android/update-binary",
|
2015-03-24 03:13:21 +01:00
|
|
|
data, perms=0o755)
|