androidPartitionType_fromString(): new function.

Change-Id: I6bbc2e3149dd8710afef241a3811c0ff659f33f9
diff --git a/android/filesystems/partition_types.cpp b/android/filesystems/partition_types.cpp
index f0dfba7..40d59ce 100644
--- a/android/filesystems/partition_types.cpp
+++ b/android/filesystems/partition_types.cpp
@@ -17,19 +17,40 @@
 
 #include <errno.h>
 
+namespace {
+
+const struct {
+    const char* name;
+    AndroidPartitionType value;
+} kPartitionTypeMap[] = {
+    { "unknown", ANDROID_PARTITION_TYPE_UNKNOWN },
+    { "yaffs2", ANDROID_PARTITION_TYPE_YAFFS2 },
+    { "ext4", ANDROID_PARTITION_TYPE_EXT4 },
+};
+
+const size_t kPartitionTypeMapSize =
+        sizeof(kPartitionTypeMap) / sizeof(kPartitionTypeMap[0]);
+
+}  // namespace
+
 const char* androidPartitionType_toString(AndroidPartitionType part_type) {
-    switch (part_type) {
-        case ANDROID_PARTITION_TYPE_UNKNOWN:
-            return "unknown";
-        case ANDROID_PARTITION_TYPE_YAFFS2:
-            return "yaffs2";
-        case ANDROID_PARTITION_TYPE_EXT4:
-            return "ext4";
-        default:
-            APANIC("Invalid partition type value %d", part_type);
+    for (size_t n = 0; n < kPartitionTypeMapSize; ++n) {
+        if (kPartitionTypeMap[n].value == part_type) {
+            return kPartitionTypeMap[n].name;
+        }
     }
+    APANIC("Invalid partition type value %d", part_type);
+    return "unknown";
 }
 
+AndroidPartitionType androidPartitionType_fromString(const char* part_type) {
+    for (size_t n = 0; n < kPartitionTypeMapSize; ++n) {
+        if (!strcmp(kPartitionTypeMap[n].name, part_type)) {
+            return kPartitionTypeMap[n].value;
+        }
+    }
+    return ANDROID_PARTITION_TYPE_UNKNOWN;
+}
 
 AndroidPartitionType androidPartitionType_probeFile(const char* image_file) {
     if (!path_exists(image_file)) {
diff --git a/android/filesystems/partition_types.h b/android/filesystems/partition_types.h
index 9bbdcf4..853b389 100644
--- a/android/filesystems/partition_types.h
+++ b/android/filesystems/partition_types.h
@@ -29,6 +29,9 @@
 // Note: this will panic if |part_type| is an invalid value.
 const char* androidPartitionType_toString(AndroidPartitionType part_type);
 
+// Return an AndroidPartitionType from a string description.
+AndroidPartitionType androidPartitionType_fromString(const char* part_type);
+
 // Probe a given image file and return its partition image type.
 // Note: this returns ANDROID_PARTITION_TYPE_UNKNOWN if the file does
 // not exist or cannot be read.
diff --git a/android/filesystems/partition_types_unittest.cpp b/android/filesystems/partition_types_unittest.cpp
index 5bdddd9..a51a3dd 100644
--- a/android/filesystems/partition_types_unittest.cpp
+++ b/android/filesystems/partition_types_unittest.cpp
@@ -52,9 +52,26 @@
 }  // namespace
 
 TEST(AndroidPartitionType, ToString) {
-    EXPECT_STREQ("unknown", androidPartitionType_toString(ANDROID_PARTITION_TYPE_UNKNOWN));
-    EXPECT_STREQ("yaffs2", androidPartitionType_toString(ANDROID_PARTITION_TYPE_YAFFS2));
-    EXPECT_STREQ("ext4", androidPartitionType_toString(ANDROID_PARTITION_TYPE_EXT4));
+    EXPECT_STREQ(
+            "unknown",
+            androidPartitionType_toString(ANDROID_PARTITION_TYPE_UNKNOWN));
+    EXPECT_STREQ(
+            "yaffs2",
+            androidPartitionType_toString(ANDROID_PARTITION_TYPE_YAFFS2));
+    EXPECT_STREQ(
+            "ext4",
+            androidPartitionType_toString(ANDROID_PARTITION_TYPE_EXT4));
+}
+
+TEST(AndroidPartitionType, FromString) {
+    EXPECT_EQ(ANDROID_PARTITION_TYPE_YAFFS2,
+              androidPartitionType_fromString("yaffs2"));
+    EXPECT_EQ(ANDROID_PARTITION_TYPE_EXT4,
+              androidPartitionType_fromString("ext4"));
+    EXPECT_EQ(ANDROID_PARTITION_TYPE_UNKNOWN,
+              androidPartitionType_fromString("unknown"));
+    EXPECT_EQ(ANDROID_PARTITION_TYPE_UNKNOWN,
+              androidPartitionType_fromString("foobar"));
 }
 
 TEST(AndroidPartitionType, ProbeFileYaffs2) {