edify_generator.py revision c494d7cee85d980647ca915ea64355b71fe817eb
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
15import os
16import re
17
18import common
19
20class EdifyGenerator(object):
21  """Class to generate scripts in the 'edify' recovery script language
22  used from donut onwards."""
23
24  def __init__(self, version):
25    self.script = []
26    self.mounts = set()
27    self.version = version
28
29  def MakeTemporary(self):
30    """Make a temporary script object whose commands can latter be
31    appended to the parent script with AppendScript().  Used when the
32    caller wants to generate script commands out-of-order."""
33    x = EdifyGenerator(self.version)
34    x.mounts = self.mounts
35    return x
36
37  @staticmethod
38  def _WordWrap(cmd, linelen=80):
39    """'cmd' should be a function call with null characters after each
40    parameter (eg, "somefun(foo,\0bar,\0baz)").  This function wraps cmd
41    to a given line length, replacing nulls with spaces and/or newlines
42    to format it nicely."""
43    indent = cmd.index("(")+1
44    out = []
45    first = True
46    x = re.compile("^(.{,%d})\0" % (linelen-indent,))
47    while True:
48      if not first:
49        out.append(" " * indent)
50      first = False
51      m = x.search(cmd)
52      if not m:
53        parts = cmd.split("\0", 1)
54        out.append(parts[0]+"\n")
55        if len(parts) == 1:
56          break
57        else:
58          cmd = parts[1]
59          continue
60      out.append(m.group(1)+"\n")
61      cmd = cmd[m.end():]
62
63    return "".join(out).replace("\0", " ").rstrip("\n")
64
65  def AppendScript(self, other):
66    """Append the contents of another script (which should be created
67    with temporary=True) to this one."""
68    self.script.extend(other.script)
69
70  def AssertSomeFingerprint(self, *fp):
71    """Assert that the current system build fingerprint is one of *fp."""
72    if not fp:
73      raise ValueError("must specify some fingerprints")
74    cmd = ('assert(' +
75           ' ||\0'.join([('file_getprop("/system/build.prop", '
76                         '"ro.build.fingerprint") == "%s"')
77                        % i for i in fp]) +
78           ');')
79    self.script.append(self._WordWrap(cmd))
80
81  def AssertOlderBuild(self, timestamp):
82    """Assert that the build on the device is older (or the same as)
83    the given timestamp."""
84    self.script.append(('assert(!less_than_int(%s, '
85                        'getprop("ro.build.date.utc")));') % (timestamp,))
86
87  def AssertDevice(self, device):
88    """Assert that the device identifier is the given string."""
89    cmd = ('assert(getprop("ro.product.device") == "%s" ||\0'
90           'getprop("ro.build.product") == "%s");' % (device, device))
91    self.script.append(self._WordWrap(cmd))
92
93  def AssertSomeBootloader(self, *bootloaders):
94    """Asert that the bootloader version is one of *bootloaders."""
95    cmd = ("assert(" +
96           " ||\0".join(['getprop("ro.bootloader") == "%s"' % (b,)
97                         for b in bootloaders]) +
98           ");")
99    self.script.append(self._WordWrap(cmd))
100
101  def ShowProgress(self, frac, dur):
102    """Update the progress bar, advancing it over 'frac' over the next
103    'dur' seconds."""
104    self.script.append("show_progress(%f, %d);" % (frac, int(dur)))
105
106  def PatchCheck(self, filename, *sha1):
107    """Check that the given file (or MTD reference) has one of the
108    given *sha1 hashes."""
109    self.script.append('assert(apply_patch_check("%s"' % (filename,) +
110                       "".join([', "%s"' % (i,) for i in sha1]) +
111                       '));')
112
113  def CacheFreeSpaceCheck(self, amount):
114    """Check that there's at least 'amount' space that can be made
115    available on /cache."""
116    self.script.append("assert(apply_patch_space(%d));" % (amount,))
117
118  def Mount(self, kind, what, path):
119    """Mount the given 'what' at the given path.  'what' should be a
120    partition name if kind is "MTD", or a block device if kind is
121    "vfat".  No other values of 'kind' are supported."""
122    self.script.append('mount("%s", "%s", "%s");' % (kind, what, path))
123    self.mounts.add(path)
124
125  def UnpackPackageDir(self, src, dst):
126    """Unpack a given directory from the OTA package into the given
127    destination directory."""
128    self.script.append('package_extract_dir("%s", "%s");' % (src, dst))
129
130  def Comment(self, comment):
131    """Write a comment into the update script."""
132    self.script.append("")
133    for i in comment.split("\n"):
134      self.script.append("# " + i)
135    self.script.append("")
136
137  def Print(self, message):
138    """Log a message to the screen (if the logs are visible)."""
139    self.script.append('ui_print("%s");' % (message,))
140
141  def FormatPartition(self, partition):
142    """Format the given MTD partition."""
143    self.script.append('format("MTD", "%s");' % (partition,))
144
145  def DeleteFiles(self, file_list):
146    """Delete all files in file_list."""
147    if not file_list: return
148    cmd = "delete(" + ",\0".join(['"%s"' % (i,) for i in file_list]) + ");"
149    self.script.append(self._WordWrap(cmd))
150
151  def ApplyPatch(self, srcfile, tgtfile, tgtsize, tgtsha1, *patchpairs):
152    """Apply binary patches (in *patchpairs) to the given srcfile to
153    produce tgtfile (which may be "-" to indicate overwriting the
154    source file."""
155    if len(patchpairs) % 2 != 0 or len(patchpairs) == 0:
156      raise ValueError("bad patches given to ApplyPatch")
157    cmd = ['apply_patch("%s",\0"%s",\0%s,\0%d'
158           % (srcfile, tgtfile, tgtsha1, tgtsize)]
159    for i in range(0, len(patchpairs), 2):
160      cmd.append(',\0"%s:%s"' % patchpairs[i:i+2])
161    cmd.append(');')
162    cmd = "".join(cmd)
163    self.script.append(self._WordWrap(cmd))
164
165  def WriteFirmwareImage(self, kind, fn):
166    """Arrange to update the given firmware image (kind must be
167    "hboot" or "radio") when recovery finishes."""
168    if self.version == 1:
169      self.script.append(
170          ('assert(package_extract_file("%(fn)s", "/tmp/%(kind)s.img"),\n'
171           '       write_firmware_image("/tmp/%(kind)s.img", "%(kind)s"));')
172          % {'kind': kind, 'fn': fn})
173    else:
174      self.script.append(
175          'write_firmware_image("PACKAGE:%s", "%s");' % (fn, kind))
176
177  def WriteRawImage(self, partition, fn):
178    """Write the given package file into the given MTD partition."""
179    self.script.append(
180        ('assert(package_extract_file("%(fn)s", "/tmp/%(partition)s.img"),\n'
181         '       write_raw_image("/tmp/%(partition)s.img", "%(partition)s"),\n'
182         '       delete("/tmp/%(partition)s.img"));')
183        % {'partition': partition, 'fn': fn})
184
185  def SetPermissions(self, fn, uid, gid, mode):
186    """Set file ownership and permissions."""
187    self.script.append('set_perm(%d, %d, 0%o, "%s");' % (uid, gid, mode, fn))
188
189  def SetPermissionsRecursive(self, fn, uid, gid, dmode, fmode):
190    """Recursively set path ownership and permissions."""
191    self.script.append('set_perm_recursive(%d, %d, 0%o, 0%o, "%s");'
192                       % (uid, gid, dmode, fmode, fn))
193
194  def MakeSymlinks(self, symlink_list):
195    """Create symlinks, given a list of (dest, link) pairs."""
196    by_dest = {}
197    for d, l in symlink_list:
198      by_dest.setdefault(d, []).append(l)
199
200    for dest, links in sorted(by_dest.iteritems()):
201      cmd = ('symlink("%s", ' % (dest,) +
202             ",\0".join(['"' + i + '"' for i in sorted(links)]) + ");")
203      self.script.append(self._WordWrap(cmd))
204
205  def AppendExtra(self, extra):
206    """Append text verbatim to the output script."""
207    self.script.append(extra)
208
209  def AddToZip(self, input_zip, output_zip, input_path=None):
210    """Write the accumulated script to the output_zip file.  input_zip
211    is used as the source for the 'updater' binary needed to run
212    script.  If input_path is not None, it will be used as a local
213    path for the binary instead of input_zip."""
214
215    for p in sorted(self.mounts):
216      self.script.append('unmount("%s");' % (p,))
217
218    common.ZipWriteStr(output_zip, "META-INF/com/google/android/updater-script",
219                       "\n".join(self.script) + "\n")
220
221    if input_path is None:
222      data = input_zip.read("OTA/bin/updater")
223    else:
224      data = open(os.path.join(input_path, "updater")).read()
225    common.ZipWriteStr(output_zip, "META-INF/com/google/android/update-binary",
226                       data, perms=0755)
227