Allow recovery to return error codes

Write error code, cause code, and retry count into last_install. So we
can have more information about the reason of a failed OTA.

Example of new last_install:
@/cache/recovery/block.map     package name
0                              install result
retry: 1                       retry count (new)
error: 30                      error code (new)
cause: 12                      error cause (new)

Details in:
go/android-ota-errorcode

Bug: 28471955
Change-Id: I00e7153c821e7355c1be81a86c7f228108f3dc37
diff --git a/edify/expr.cpp b/edify/expr.cpp
index cd1e087..cc14fbe 100644
--- a/edify/expr.cpp
+++ b/edify/expr.cpp
@@ -21,6 +21,11 @@
 #include <stdarg.h>
 #include <unistd.h>
 
+#include <string>
+
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
+
 #include "expr.h"
 
 // Functions should:
@@ -36,7 +41,7 @@
     Value* v = expr->fn(expr->name, state, expr->argc, expr->argv);
     if (v == NULL) return NULL;
     if (v->type != VAL_STRING) {
-        ErrorAbort(state, "expecting string, got value type %d", v->type);
+        ErrorAbort(state, kArgsParsingFailure, "expecting string, got value type %d", v->type);
         FreeValue(v);
         return NULL;
     }
@@ -493,15 +498,29 @@
     return args;
 }
 
-// Use printf-style arguments to compose an error message to put into
-// *state.  Returns NULL.
-Value* ErrorAbort(State* state, const char* format, ...) {
-    char* buffer = reinterpret_cast<char*>(malloc(4096));
-    va_list v;
-    va_start(v, format);
-    vsnprintf(buffer, 4096, format, v);
-    va_end(v);
+static void ErrorAbortV(State* state, const char* format, va_list ap) {
+    std::string buffer;
+    android::base::StringAppendV(&buffer, format, ap);
     free(state->errmsg);
-    state->errmsg = buffer;
-    return NULL;
+    state->errmsg = strdup(buffer.c_str());
+    return;
+}
+
+// Use printf-style arguments to compose an error message to put into
+// *state.  Returns nullptr.
+Value* ErrorAbort(State* state, const char* format, ...) {
+    va_list ap;
+    va_start(ap, format);
+    ErrorAbortV(state, format, ap);
+    va_end(ap);
+    return nullptr;
+}
+
+Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
+    va_list ap;
+    va_start(ap, format);
+    ErrorAbortV(state, format, ap);
+    va_end(ap);
+    state->cause_code = cause_code;
+    return nullptr;
 }