Merge "init: write bootloader message directly." into nyc-dev
am: a3a7a88

* commit 'a3a7a886f5277b6223140761e58cea0753854225':
  init: write bootloader message directly.

Change-Id: I764e0288b40264b8e48fc09011c3c0475b71a04e
diff --git a/init/Android.mk b/init/Android.mk
index d8b574f..4827fa3 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -83,6 +83,7 @@
 
 LOCAL_STATIC_LIBRARIES := \
     libinit \
+    libbootloader_message_writer \
     libfs_mgr \
     libfec \
     libfec_rs \
diff --git a/init/builtins.cpp b/init/builtins.cpp
index b520444..66133fb 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -45,6 +45,7 @@
 #include <android-base/file.h>
 #include <android-base/parseint.h>
 #include <android-base/stringprintf.h>
+#include <bootloader_message_writer.h>
 #include <cutils/partition_utils.h>
 #include <cutils/android_reboot.h>
 #include <logwrap/logwrap.h>
@@ -451,14 +452,10 @@
 }
 
 static int wipe_data_via_recovery() {
-    mkdir("/cache/recovery", 0700);
-    int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
-    if (fd >= 0) {
-        write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
-        write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
-        close(fd);
-    } else {
-        ERROR("could not open /cache/recovery/command\n");
+    const std::vector<std::string> options = {"--wipe_data", "--reason=wipe_data_via_recovery"};
+    std::string err;
+    if (!write_bootloader_message(options, &err)) {
+        ERROR("failed to set bootloader message: %s", err.c_str());
         return -1;
     }
     android_reboot(ANDROID_RB_RESTART2, 0, "recovery");