Move all AOSP code out of recovery binary

Improves license compatibility between GPL and Apache

Change-Id: I2b165aa575bb6213af6b07936f99610c113443f0
diff --git a/bootloader.cpp b/bootloader.cpp
index 6f4f83a..6b42b94 100644
--- a/bootloader.cpp
+++ b/bootloader.cpp
@@ -33,7 +33,7 @@
 static int set_bootloader_message_block(const struct bootloader_message *in, const Volume* v);
 
 int get_bootloader_message(struct bootloader_message *out) {
-    Volume* v = volume_for_path("/misc");
+    Volume* v = NULL;//volume_for_path("/misc");
     if (v == NULL) {
       LOGE("Cannot load volume /misc!\n");
       return -1;
@@ -48,7 +48,7 @@
 }
 
 int set_bootloader_message(const struct bootloader_message *in) {
-    Volume* v = volume_for_path("/misc");
+    Volume* v = NULL;//volume_for_path("/misc");
     if (v == NULL) {
       LOGE("Cannot load volume /misc!\n");
       return -1;
@@ -139,6 +139,49 @@
     return 0;
 }
 
+int set_bootloader_message_mtd_name(const struct bootloader_message *in,
+                                      const char* mtd_name) {
+    size_t write_size;
+    mtd_scan_partitions();
+    const MtdPartition *part = mtd_find_partition_by_name(mtd_name);
+    if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
+        printf("Can't find %s\n", mtd_name);
+        return -1;
+    }
+
+    MtdReadContext *read = mtd_read_partition(part);
+    if (read == NULL) {
+        printf("Can't open %s\n(%s)\n", mtd_name, strerror(errno));
+        return -1;
+    }
+
+    ssize_t size = write_size * MISC_PAGES;
+    char data[size];
+    ssize_t r = mtd_read_data(read, data, size);
+    if (r != size) printf("Can't read %s\n(%s)\n", mtd_name, strerror(errno));
+    mtd_read_close(read);
+    if (r != size) return -1;
+
+    memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
+
+    MtdWriteContext *write = mtd_write_partition(part);
+    if (write == NULL) {
+        printf("Can't open %s\n(%s)\n", mtd_name, strerror(errno));
+        return -1;
+    }
+    if (mtd_write_data(write, data, size) != size) {
+        printf("Can't write %s\n(%s)\n", mtd_name, strerror(errno));
+        mtd_write_close(write);
+        return -1;
+    }
+    if (mtd_write_close(write)) {
+        printf("Can't finish %s\n(%s)\n", mtd_name, strerror(errno));
+        return -1;
+    }
+
+    printf("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
+    return 0;
+}
 
 // ------------------------------------
 // for misc partitions on block devices
@@ -202,3 +245,23 @@
     }
     return 0;
 }
+
+int set_bootloader_message_block_name(const struct bootloader_message *in,
+                                        const char* block_name) {
+    wait_for_device(block_name);
+    FILE* f = fopen(block_name, "wb");
+    if (f == NULL) {
+        printf("Can't open %s\n(%s)\n", block_name, strerror(errno));
+        return -1;
+    }
+    int count = fwrite(in, sizeof(*in), 1, f);
+    if (count != 1) {
+        printf("Failed writing %s\n(%s)\n", block_name, strerror(errno));
+        return -1;
+    }
+    if (fclose(f) != 0) {
+        printf("Failed closing %s\n(%s)\n", block_name, strerror(errno));
+        return -1;
+    }
+    return 0;
+}