support using an EMMC misc partition to store recovery arguments

Change-Id: I9f912857cfc6afb8ba764f5541af7f01df029a77
diff --git a/bootloader.c b/bootloader.c
index 38b5651..1e66c3e 100644
--- a/bootloader.c
+++ b/bootloader.c
@@ -25,8 +25,6 @@
 
 static const char *CACHE_NAME = "CACHE:";
 static const char *MISC_NAME = "MISC:";
-static const int MISC_PAGES = 3;         // number of pages to save
-static const int MISC_COMMAND_PAGE = 1;  // bootloader command is this page
 
 #ifdef LOG_VERBOSE
 static void dump_data(const char *data, int len) {
@@ -41,6 +39,57 @@
 }
 #endif
 
+#ifdef USE_EXT4
+// Strictly speaking this doesn't have anything to do with ext4; we
+// really just mean "misc is an emmc partition".  We should have a
+// more configurable way have describing partitions, filesystems, etc.
+
+static const char* MISC_PARTITION =
+    "/dev/block/platform/sdhci-tegra.3/by-name/misc";
+
+int get_bootloader_message(struct bootloader_message* out) {
+    FILE* f = fopen(MISC_PARTITION, "rb");
+    if (f == NULL) {
+        LOGE("Can't open %s\n(%s)\n", MISC_PARTITION, strerror(errno));
+        return -1;
+    }
+    struct bootloader_message temp;
+    int count = fread(&temp, sizeof(temp), 1, f);
+    if (count != 1) {
+        LOGE("Failed reading %s\n(%s)\n", MISC_PARTITION, strerror(errno));
+        return -1;
+    }
+    if (fclose(f) != 0) {
+        LOGE("Failed closing %s\n(%s)\n", MISC_PARTITION, strerror(errno));
+        return -1;
+    }
+    memcpy(out, &temp, sizeof(temp));
+    return 0;
+}
+
+int set_bootloader_message(const struct bootloader_message* in) {
+    FILE* f = fopen(MISC_PARTITION, "wb");
+    if (f == NULL) {
+        LOGE("Can't open %s\n(%s)\n", MISC_PARTITION, strerror(errno));
+        return -1;
+    }
+    int count = fwrite(in, sizeof(*in), 1, f);
+    if (count != 1) {
+        LOGE("Failed writing %s\n(%s)\n", MISC_PARTITION, strerror(errno));
+        return -1;
+    }
+    if (fclose(f) != 0) {
+        LOGE("Failed closing %s\n(%s)\n", MISC_PARTITION, strerror(errno));
+        return -1;
+    }
+    return 0;
+}
+
+#else  // MTD partitions
+
+static const int MISC_PAGES = 3;         // number of pages to save
+static const int MISC_COMMAND_PAGE = 1;  // bootloader command is this page
+
 int get_bootloader_message(struct bootloader_message *out) {
     size_t write_size;
     const MtdPartition *part = get_root_mtd_partition(MISC_NAME);
@@ -119,3 +168,5 @@
     LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
     return 0;
 }
+
+#endif
diff --git a/roots.c b/roots.c
index 10b93d4..31f99ac 100644
--- a/roots.c
+++ b/roots.c
@@ -51,7 +51,6 @@
 
 static RootInfo g_roots[] = {
     { "BOOT:", g_mtd_device, NULL, "boot", NULL, g_raw },
-    { "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw },
     { "PACKAGE:", NULL, NULL, NULL, NULL, g_package_file },
     { "RECOVERY:", g_mtd_device, NULL, "recovery", "/", g_raw },
     { "SYSTEM:", g_mtd_device, NULL, "system", "/system", "yaffs2" },
@@ -68,6 +67,7 @@
     { "CACHE:", g_mtd_device, NULL, "cache", "/cache", "yaffs2" },
     { "DATA:", g_mtd_device, NULL, "userdata", "/data", "yaffs2" },
     { "EXT:", "/dev/block/mmcblk0p1", "/dev/block/mmcblk0", NULL, "/sdcard", "vfat" },
+    { "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw },
 #endif
 
 };
@@ -384,8 +384,10 @@
 
 #ifdef USE_EXT4
     if (strcmp(info->filesystem, "ext4") == 0) {
+        LOGW("starting to reformat ext4\n");
         reset_ext4fs_info();
         int result = make_ext4fs(info->device, NULL, NULL, 0, 0);
+        LOGW("finished reformat ext4: result = %d\n", result);
         if (result != 0) {
             LOGW("make_ext4fs failed: %d\n", result);
             return -1;