Report error codes in the OTA update script

Modify the abort() function in the OTA update generation script to
report an error code. The recoveryimage will parse the code and write it
into last_install for further data analysis.

Bug: 28934032
Change-Id: I2d62f81fd352c3102fb84f054972ac0ecb965a21
diff --git a/tools/releasetools/edify_generator.py b/tools/releasetools/edify_generator.py
index 57f8cda..2ecc5cb 100644
--- a/tools/releasetools/edify_generator.py
+++ b/tools/releasetools/edify_generator.py
@@ -85,14 +85,17 @@
       raise ValueError("must specify the OEM value")
     if common.OPTIONS.oem_no_mount:
       cmd = ('getprop("{name}") == "{value}" || '
-             'abort("This package expects the value \\"{value}\\" for '
+             'abort("E{code}: This package expects the value \\"{value}\\" for '
              '\\"{name}\\"; this has value \\"" + '
-             'getprop("{name}") + "\\".");').format(name=name, value=value)
+             'getprop("{name}") + "\\".");').format(
+                 code=common.ErrorCode.OEM_PROP_MISMATCH,
+                 name=name, value=value)
     else:
       cmd = ('file_getprop("/oem/oem.prop", "{name}") == "{value}" || '
-             'abort("This package expects the value \\"{value}\\" for '
+             'abort("E{code}: This package expects the value \\"{value}\\" for '
              '\\"{name}\\" on the OEM partition; this has value \\"" + '
              'file_getprop("/oem/oem.prop", "{name}") + "\\".");').format(
+                 code=common.ErrorCode.OEM_PROP_MISMATCH,
                  name=name, value=value)
     self.script.append(cmd)
 
@@ -102,9 +105,9 @@
       raise ValueError("must specify some fingerprints")
     cmd = (' ||\n    '.join([('getprop("ro.build.fingerprint") == "%s"') % i
                              for i in fp]) +
-           ' ||\n    abort("Package expects build fingerprint of %s; this '
-           'device has " + getprop("ro.build.fingerprint") + ".");') % (
-               " or ".join(fp))
+           ' ||\n    abort("E%d: Package expects build fingerprint of %s; '
+           'this device has " + getprop("ro.build.fingerprint") + ".");') % (
+               common.ErrorCode.FINGERPRINT_MISMATCH, " or ".join(fp))
     self.script.append(cmd)
 
   def AssertSomeThumbprint(self, *fp):
@@ -113,9 +116,9 @@
       raise ValueError("must specify some thumbprints")
     cmd = (' ||\n    '.join([('getprop("ro.build.thumbprint") == "%s"') % i
                              for i in fp]) +
-           ' ||\n    abort("Package expects build thumbprint of %s; this '
+           ' ||\n    abort("E%d: Package expects build thumbprint of %s; this '
            'device has " + getprop("ro.build.thumbprint") + ".");') % (
-               " or ".join(fp))
+               common.ErrorCode.THUMBPRINT_MISMATCH, " or ".join(fp))
     self.script.append(cmd)
 
   def AssertOlderBuild(self, timestamp, timestamp_text):
@@ -123,16 +126,16 @@
     the given timestamp."""
     self.script.append(
         ('(!less_than_int(%s, getprop("ro.build.date.utc"))) || '
-         'abort("Can\'t install this package (%s) over newer '
+         'abort("E%d: Can\'t install this package (%s) over newer '
          'build (" + getprop("ro.build.date") + ").");') % (timestamp,
-                                                            timestamp_text))
+             common.ErrorCode.OLDER_BUILD, timestamp_text))
 
   def AssertDevice(self, device):
     """Assert that the device identifier is the given string."""
     cmd = ('getprop("ro.product.device") == "%s" || '
-           'abort("This package is for \\"%s\\" devices; '
+           'abort("E%d: This package is for \\"%s\\" devices; '
            'this is a \\"" + getprop("ro.product.device") + "\\".");') % (
-               device, device)
+               device, common.ErrorCode.DEVICE_MISMATCH, device)
     self.script.append(cmd)
 
   def AssertSomeBootloader(self, *bootloaders):
@@ -162,7 +165,8 @@
     self.script.append(
         'apply_patch_check("%s"' % (filename,) +
         "".join([', "%s"' % (i,) for i in sha1]) +
-        ') || abort("\\"%s\\" has unexpected contents.");' % (filename,))
+        ') || abort("E%d: \\"%s\\" has unexpected contents.");' % (
+            common.ErrorCode.BAD_PATCH_FILE, filename))
 
   def Verify(self, filename):
     """Check that the given file (or MTD reference) has one of the
@@ -184,8 +188,10 @@
     """Check that there's at least 'amount' space that can be made
     available on /cache."""
     self._required_cache = max(self._required_cache, amount)
-    self.script.append(('apply_patch_space(%d) || abort("Not enough free space '
-                        'on /cache to apply patches.");') % (amount,))
+    self.script.append(('apply_patch_space(%d) || abort("E%d: Not enough free '
+                        'space on /cache to apply patches.");') % (
+                            amount,
+                            common.ErrorCode.INSUFFICIENT_CACHE_SPACE))
 
   def Mount(self, mount_point, mount_options_by_format=""):
     """Mount the partition with the given mount_point.
@@ -235,8 +241,8 @@
         raise ValueError("Partition %s cannot be tuned\n" % (partition,))
     self.script.append(
         'tune2fs(' + "".join(['"%s", ' % (i,) for i in options]) +
-        '"%s") || abort("Failed to tune partition %s");' % (
-            p.device, partition))
+        '"%s") || abort("E%d: Failed to tune partition %s");' % (
+            p.device, common.ErrorCode.TUNE_PARTITION_FAILURE, partition))
 
   def FormatPartition(self, partition):
     """Format the given partition, specified by its mount point (eg,
@@ -298,7 +304,8 @@
            % (srcfile, tgtfile, tgtsha1, tgtsize)]
     for i in range(0, len(patchpairs), 2):
       cmd.append(',\0%s,\0package_extract_file("%s")' % patchpairs[i:i+2])
-    cmd.append(') ||\n    abort("Failed to apply patch to %s");' % (srcfile,))
+    cmd.append(') ||\n    abort("E%d: Failed to apply patch to %s");' % (
+        common.ErrorCode.APPLY_PATCH_FAILURE, srcfile))
     cmd = "".join(cmd)
     self.script.append(self.WordWrap(cmd))