Home | History | Annotate | Download | only in filesystems
      1 // Copyright 2014 The Android Open Source Project
      2 //
      3 // This software is licensed under the terms of the GNU General Public
      4 // License version 2, as published by the Free Software Foundation, and
      5 // may be copied, distributed, and modified under those terms.
      6 //
      7 // This program is distributed in the hope that it will be useful,
      8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 // GNU General Public License for more details.
     11 
     12 #include "android/filesystems/partition_types.h"
     13 
     14 #include "android/filesystems/ext4_utils.h"
     15 #include "android/utils/panic.h"
     16 #include "android/utils/path.h"
     17 
     18 #include <errno.h>
     19 
     20 namespace {
     21 
     22 const struct {
     23     const char* name;
     24     AndroidPartitionType value;
     25 } kPartitionTypeMap[] = {
     26     { "unknown", ANDROID_PARTITION_TYPE_UNKNOWN },
     27     { "yaffs2", ANDROID_PARTITION_TYPE_YAFFS2 },
     28     { "ext4", ANDROID_PARTITION_TYPE_EXT4 },
     29 };
     30 
     31 const size_t kPartitionTypeMapSize =
     32         sizeof(kPartitionTypeMap) / sizeof(kPartitionTypeMap[0]);
     33 
     34 }  // namespace
     35 
     36 const char* androidPartitionType_toString(AndroidPartitionType part_type) {
     37     for (size_t n = 0; n < kPartitionTypeMapSize; ++n) {
     38         if (kPartitionTypeMap[n].value == part_type) {
     39             return kPartitionTypeMap[n].name;
     40         }
     41     }
     42     APANIC("Invalid partition type value %d", part_type);
     43     return "unknown";
     44 }
     45 
     46 AndroidPartitionType androidPartitionType_fromString(const char* part_type) {
     47     for (size_t n = 0; n < kPartitionTypeMapSize; ++n) {
     48         if (!strcmp(kPartitionTypeMap[n].name, part_type)) {
     49             return kPartitionTypeMap[n].value;
     50         }
     51     }
     52     return ANDROID_PARTITION_TYPE_UNKNOWN;
     53 }
     54 
     55 AndroidPartitionType androidPartitionType_probeFile(const char* image_file) {
     56     if (!path_exists(image_file)) {
     57         return ANDROID_PARTITION_TYPE_UNKNOWN;
     58     }
     59     if (android_pathIsExt4PartitionImage(image_file)) {
     60         return ANDROID_PARTITION_TYPE_EXT4;
     61     }
     62     // Assume YAFFS2, since there is little way to be sure for now.
     63     // NOTE: An empty file is a valid Yaffs2 file!
     64     return ANDROID_PARTITION_TYPE_YAFFS2;
     65 }
     66 
     67 
     68 int androidPartitionType_makeEmptyFile(AndroidPartitionType part_type,
     69                                        uint64_t part_size,
     70                                        const char* part_file) {
     71     switch (part_type) {
     72         case ANDROID_PARTITION_TYPE_YAFFS2:
     73             // Any empty file is a valid YAFFS2 partition, |part_size|
     74             // can be ignored here.
     75             if (path_empty_file(part_file) < 0) {
     76                 return -errno;
     77             }
     78             return 0;
     79 
     80         case ANDROID_PARTITION_TYPE_EXT4:
     81             return android_createEmptyExt4Image(part_file, part_size, NULL);
     82 
     83         default:
     84             return -EINVAL;
     85     }
     86 }
     87