From 8b26c06003c384b206ffebeb8e5b2cf43ab81b00 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Wed, 31 Jul 2013 21:09:58 -0700 Subject: [PATCH] Fix roots.c to use fs_mgr Change-Id: Ie68daba5e014dff0c4998da85576f35abe8ad270 --- Android.mk | 10 +- bootloader.c | 42 ++++----- common.h | 29 +----- extendedcommands.c | 30 +++--- minadbd/Android.mk | 2 + nandroid.c | 24 ++--- roots.c | 224 +++++++++++++-------------------------------- 7 files changed, 123 insertions(+), 238 deletions(-) diff --git a/Android.mk b/Android.mk index b9948225a..7f42877f8 100644 --- a/Android.mk +++ b/Android.mk @@ -63,7 +63,7 @@ BOARD_RECOVERY_CHAR_HEIGHT := $(shell echo $(BOARD_USE_CUSTOM_RECOVERY_FONT) | c LOCAL_CFLAGS += -DBOARD_RECOVERY_CHAR_WIDTH=$(BOARD_RECOVERY_CHAR_WIDTH) -DBOARD_RECOVERY_CHAR_HEIGHT=$(BOARD_RECOVERY_CHAR_HEIGHT) -BOARD_RECOVERY_DEFINES := BOARD_HAS_NO_SELECT_BUTTON BOARD_UMS_LUNFILE BOARD_RECOVERY_ALWAYS_WIPES BOARD_RECOVERY_HANDLES_MOUNT BOARD_TOUCH_RECOVERY RECOVERY_EXTEND_NANDROID_MENU TARGET_USE_CUSTOM_LUN_FILE_PATH TARGET_DEVICE +BOARD_RECOVERY_DEFINES := BOARD_HAS_NO_SELECT_BUTTON BOARD_UMS_LUNFILE BOARD_RECOVERY_ALWAYS_WIPES BOARD_RECOVERY_HANDLES_MOUNT BOARD_TOUCH_RECOVERY RECOVERY_EXTEND_NANDROID_MENU TARGET_USE_CUSTOM_LUN_FILE_PATH TARGET_DEVICE TARGET_RECOVERY_FSTAB $(foreach board_define,$(BOARD_RECOVERY_DEFINES), \ $(if $($(board_define)), \ @@ -74,7 +74,7 @@ $(foreach board_define,$(BOARD_RECOVERY_DEFINES), \ LOCAL_STATIC_LIBRARIES := LOCAL_CFLAGS += -DUSE_EXT4 -LOCAL_C_INCLUDES += system/extras/ext4_utils +LOCAL_C_INCLUDES += system/extras/ext4_utils system/core/fs_mgr/include LOCAL_STATIC_LIBRARIES += libext4_utils_static libz libsparse_static # This binary is in the recovery ramdisk, which is otherwise a copy of root. @@ -96,7 +96,7 @@ LOCAL_STATIC_LIBRARIES += libminzip libunz libmincrypt LOCAL_STATIC_LIBRARIES += libminizip libminadbd libedify libbusybox libmkyaffs2image libunyaffs liberase_image libdump_image libflash_image LOCAL_LDFLAGS += -Wl,--no-fatal-warnings -LOCAL_STATIC_LIBRARIES += libdedupe libcrypto_static libcrecovery libflashutils libmtdutils libmmcutils libbmlutils +LOCAL_STATIC_LIBRARIES += libfs_mgr libdedupe libcrypto_static libcrecovery libflashutils libmtdutils libmmcutils libbmlutils ifeq ($(BOARD_USES_BML_OVER_MTD),true) LOCAL_STATIC_LIBRARIES += libbml_over_mtd @@ -107,8 +107,6 @@ LOCAL_STATIC_LIBRARIES += libstdc++ libc LOCAL_STATIC_LIBRARIES += libselinux -LOCAL_C_INCLUDES += system/extras/ext4_utils - include $(BUILD_EXECUTABLE) RECOVERY_LINKS := bu make_ext4fs edify busybox flash_image dump_image mkyaffs2image unyaffs erase_image nandroid reboot volume setprop getprop dedupe minizip @@ -157,6 +155,8 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := verifier_test.c verifier.c +LOCAL_C_INCLUDES += system/extras/ext4_utils system/core/fs_mgr/include + LOCAL_MODULE := verifier_test LOCAL_FORCE_STATIC_EXECUTABLE := true diff --git a/bootloader.c b/bootloader.c index e88160d8c..588ea5560 100644 --- a/bootloader.c +++ b/bootloader.c @@ -71,22 +71,22 @@ static int get_bootloader_message_mtd(struct bootloader_message *out, const Volume* v) { size_t write_size; mtd_scan_partitions(); - const MtdPartition *part = mtd_find_partition_by_name(v->device); + const MtdPartition *part = mtd_find_partition_by_name(v->blk_device); if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { - LOGE("Can't find %s\n", v->device); + LOGE("Can't find %s\n", v->blk_device); return -1; } MtdReadContext *read = mtd_read_partition(part); if (read == NULL) { - LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno)); return -1; } const ssize_t size = write_size * MISC_PAGES; char data[size]; ssize_t r = mtd_read_data(read, data, size); - if (r != size) LOGE("Can't read %s\n(%s)\n", v->device, strerror(errno)); + if (r != size) LOGE("Can't read %s\n(%s)\n", v->blk_device, strerror(errno)); mtd_read_close(read); if (r != size) return -1; @@ -97,22 +97,22 @@ static int set_bootloader_message_mtd(const struct bootloader_message *in, const Volume* v) { size_t write_size; mtd_scan_partitions(); - const MtdPartition *part = mtd_find_partition_by_name(v->device); + const MtdPartition *part = mtd_find_partition_by_name(v->blk_device); if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { - LOGE("Can't find %s\n", v->device); + LOGE("Can't find %s\n", v->blk_device); return -1; } MtdReadContext *read = mtd_read_partition(part); if (read == NULL) { - LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Can't open %s\n(%s)\n", v->blk_device, 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) LOGE("Can't read %s\n(%s)\n", v->device, strerror(errno)); + if (r != size) LOGE("Can't read %s\n(%s)\n", v->blk_device, strerror(errno)); mtd_read_close(read); if (r != size) return -1; @@ -120,16 +120,16 @@ static int set_bootloader_message_mtd(const struct bootloader_message *in, MtdWriteContext *write = mtd_write_partition(part); if (write == NULL) { - LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno)); return -1; } if (mtd_write_data(write, data, size) != size) { - LOGE("Can't write %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Can't write %s\n(%s)\n", v->blk_device, strerror(errno)); mtd_write_close(write); return -1; } if (mtd_write_close(write)) { - LOGE("Can't finish %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Can't finish %s\n(%s)\n", v->blk_device, strerror(errno)); return -1; } @@ -161,20 +161,20 @@ static void wait_for_device(const char* fn) { static int get_bootloader_message_block(struct bootloader_message *out, const Volume* v) { - wait_for_device(v->device); - FILE* f = fopen(v->device, "rb"); + wait_for_device(v->blk_device); + FILE* f = fopen(v->blk_device, "rb"); if (f == NULL) { - LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Can't open %s\n(%s)\n", v->blk_device, 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", v->device, strerror(errno)); + LOGE("Failed reading %s\n(%s)\n", v->blk_device, strerror(errno)); return -1; } if (fclose(f) != 0) { - LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Failed closing %s\n(%s)\n", v->blk_device, strerror(errno)); return -1; } memcpy(out, &temp, sizeof(temp)); @@ -183,19 +183,19 @@ static int get_bootloader_message_block(struct bootloader_message *out, static int set_bootloader_message_block(const struct bootloader_message *in, const Volume* v) { - wait_for_device(v->device); - FILE* f = fopen(v->device, "wb"); + wait_for_device(v->blk_device); + FILE* f = fopen(v->blk_device, "wb"); if (f == NULL) { - LOGE("Can't open %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Can't open %s\n(%s)\n", v->blk_device, strerror(errno)); return -1; } int count = fwrite(in, sizeof(*in), 1, f); if (count != 1) { - LOGE("Failed writing %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Failed writing %s\n(%s)\n", v->blk_device, strerror(errno)); return -1; } if (fclose(f) != 0) { - LOGE("Failed closing %s\n(%s)\n", v->device, strerror(errno)); + LOGE("Failed closing %s\n(%s)\n", v->blk_device, strerror(errno)); return -1; } return 0; diff --git a/common.h b/common.h index bf81b48ad..195d67ab0 100644 --- a/common.h +++ b/common.h @@ -18,6 +18,7 @@ #define RECOVERY_COMMON_H #include +#include // Initialize the graphics system. void ui_init(); @@ -110,33 +111,7 @@ void ui_reset_progress(); #define STRINGIFY(x) #x #define EXPAND(x) STRINGIFY(x) -typedef struct { - const char* mount_point; // eg. "/cache". must live in the root directory. - - const char* fs_type; // "yaffs2" or "ext4" or "vfat" - - const char* device; // MTD partition name if fs_type == "yaffs" - // block device if fs_type == "ext4" or "vfat" - - const char* device2; // alternative device to try if fs_type - // == "ext4" or "vfat" and mounting - // 'device' fails - - long long length; // (ext4 partition only) when - // formatting, size to use for the - // partition. 0 or negative number - // means to format all but the last - // (that much). - - const char* fs_type2; - - const char* fs_options; - - const char* fs_options2; - - const char* lun; // (/sdcard, /emmc, /external_sd only) LUN file to - // use when mounting via USB mass storage -} Volume; +typedef struct fstab_rec Volume; typedef struct { // number of frames in indeterminate progress bar animation diff --git a/extendedcommands.c b/extendedcommands.c index 8bf922734..7e6c2f9a3 100644 --- a/extendedcommands.c +++ b/extendedcommands.c @@ -458,7 +458,7 @@ static struct lun_node *lun_head = NULL; static struct lun_node *lun_tail = NULL; int control_usb_storage_set_lun(Volume* vol, bool enable, const char *lun_file) { - const char *vol_device = enable ? vol->device : ""; + const char *vol_device = enable ? vol->blk_device : ""; int fd; struct lun_node *node; @@ -471,7 +471,7 @@ int control_usb_storage_set_lun(Volume* vol, bool enable, const char *lun_file) } // Open a handle to the LUN file - LOGI("Trying %s on LUN file %s\n", vol->device, lun_file); + LOGI("Trying %s on LUN file %s\n", vol->blk_device, lun_file); if ((fd = open(lun_file, O_WRONLY)) < 0) { LOGW("Unable to open ums lunfile %s (%s)\n", lun_file, strerror(errno)); return -1; @@ -479,7 +479,7 @@ int control_usb_storage_set_lun(Volume* vol, bool enable, const char *lun_file) // Write the volume path to the LUN file if ((write(fd, vol_device, strlen(vol_device) + 1) < 0) && - (!enable || !vol->device2 || (write(fd, vol->device2, strlen(vol->device2)) < 0))) { + (!enable || !vol->blk_device2 || (write(fd, vol->blk_device2, strlen(vol->blk_device2)) < 0))) { LOGW("Unable to write to ums lunfile %s (%s)\n", lun_file, strerror(errno)); close(fd); return -1; @@ -498,7 +498,7 @@ int control_usb_storage_set_lun(Volume* vol, bool enable, const char *lun_file) lun_tail = node; } - LOGI("Successfully %sshared %s on LUN file %s\n", enable ? "" : "un", vol->device, lun_file); + LOGI("Successfully %sshared %s on LUN file %s\n", enable ? "" : "un", vol->blk_device, lun_file); return 0; } } @@ -544,7 +544,7 @@ int control_usb_storage_for_lun(Volume* vol, bool enable) { } // All LUNs were exhausted and none worked - LOGW("Could not %sable %s on LUN %d\n", enable ? "en" : "dis", vol->device, lun_num); + LOGW("Could not %sable %s on LUN %d\n", enable ? "en" : "dis", vol->blk_device, lun_num); return -1; // -1 failure, 0 success } @@ -681,7 +681,7 @@ int format_device(const char *device, const char *path, const char *fs_type) { } if (strcmp(v->mount_point, path) != 0) { - return format_unknown_device(v->device, path, NULL); + return format_unknown_device(v->blk_device, path, NULL); } if (ensure_path_unmounted(path) != 0) { @@ -742,7 +742,7 @@ int format_unknown_device(const char *device, const char* path, const char *fs_t { struct stat st; Volume *vol = volume_for_path("/sd-ext"); - if (vol == NULL || 0 != stat(vol->device, &st)) + if (vol == NULL || 0 != stat(vol->blk_device, &st)) { ui_print("No app2sd partition found. Skipping format of /sd-ext.\n"); return 0; @@ -1275,7 +1275,7 @@ static void partition_sdcard(const char* volume) { char sddevice[256]; Volume *vol = volume_for_path(volume); - strcpy(sddevice, vol->device); + strcpy(sddevice, vol->blk_device); // we only want the mmcblk, not the partition sddevice[strlen("/dev/block/mmcblkX")] = NULL; char cmd[PATH_MAX]; @@ -1295,10 +1295,10 @@ int can_partition(const char* volume) { return 0; } - int vol_len = strlen(vol->device); + int vol_len = strlen(vol->blk_device); // do not allow partitioning of a device that isn't mmcblkX or mmcblkXp1 - if (vol->device[vol_len - 2] == 'p' && vol->device[vol_len - 1] != '1') { - LOGI("Can't partition unsafe device: %s\n", vol->device); + if (vol->blk_device[vol_len - 2] == 'p' && vol->blk_device[vol_len - 1] != '1') { + LOGI("Can't partition unsafe device: %s\n", vol->blk_device); return 0; } @@ -1432,10 +1432,10 @@ void write_fstab_root(char *path, FILE *file) } char device[200]; - if (vol->device[0] != '/') - get_partition_device(vol->device, device); + if (vol->blk_device[0] != '/') + get_partition_device(vol->blk_device, device); else - strcpy(device, vol->device); + strcpy(device, vol->blk_device); fprintf(file, "%s ", device); fprintf(file, "%s ", path); @@ -1483,7 +1483,7 @@ int bml_check_volume(const char *path) { ui_print("%s may be rfs. Checking...\n", path); char tmp[PATH_MAX]; - sprintf(tmp, "mount -t rfs %s %s", vol->device, path); + sprintf(tmp, "mount -t rfs %s %s", vol->blk_device, path); int ret = __system(tmp); printf("%d\n", ret); return ret == 0 ? 1 : 0; diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 5a4de6828..5734f875c 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -25,6 +25,8 @@ LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE LOCAL_MODULE := libminadbd +LOCAL_C_INCLUDES += system/extras/ext4_utils system/core/fs_mgr/include + LOCAL_STATIC_LIBRARIES := libcutils libc include $(BUILD_STATIC_LIBRARY) diff --git a/nandroid.c b/nandroid.c index 32b93f8b2..f86bce5eb 100644 --- a/nandroid.c +++ b/nandroid.c @@ -328,7 +328,7 @@ int nandroid_backup_partition(const char* backup_path, const char* root) { sprintf(tmp, "%s/%s.img", backup_path, name); ui_print("Backing up %s image...\n", name); - if (0 != (ret = backup_raw_partition(vol->fs_type, vol->device, tmp))) { + if (0 != (ret = backup_raw_partition(vol->fs_type, vol->blk_device, tmp))) { ui_print("Error while backing up %s image!", name); return ret; } @@ -350,11 +350,13 @@ int nandroid_backup(const char* backup_path) return print_and_error("Can't mount backup path.\n"); } - Volume* volume = volume_for_path(backup_path); + Volume* volume; + if (is_data_media()) + volume = volume_for_path("/data"); + else + volume = volume_for_path(backup_path); if (NULL == volume) return print_and_error("Unable to find volume for backup path.\n"); - if (is_data_media_volume_path(volume->mount_point)) - volume = volume_for_path("/data"); int ret; struct statfs sfs; struct stat s; @@ -379,14 +381,14 @@ int nandroid_backup(const char* backup_path) return ret; Volume *vol = volume_for_path("/wimax"); - if (vol != NULL && 0 == stat(vol->device, &s)) + if (vol != NULL && 0 == stat(vol->blk_device, &s)) { char serialno[PROPERTY_VALUE_MAX]; ui_print("Backing up WiMAX...\n"); serialno[0] = 0; property_get("ro.serialno", serialno, ""); sprintf(tmp, "%s/wimax.%s.img", backup_path, serialno); - ret = backup_raw_partition(vol->fs_type, vol->device, tmp); + ret = backup_raw_partition(vol->fs_type, vol->blk_device, tmp); if (0 != ret) return print_and_error("Error while dumping WiMAX image!\n"); } @@ -414,7 +416,7 @@ int nandroid_backup(const char* backup_path) return ret; vol = volume_for_path("/sd-ext"); - if (vol == NULL || 0 != stat(vol->device, &s)) + if (vol == NULL || 0 != stat(vol->blk_device, &s)) { ui_print("No sd-ext found. Skipping backup of sd-ext.\n"); } @@ -587,7 +589,7 @@ int nandroid_restore_partition_extended(const char* backup_path, const char* mou Volume *vol = volume_for_path(mount_point); const char *device = NULL; if (vol != NULL) - device = vol->device; + device = vol->blk_device; char tmp[PATH_MAX]; sprintf(tmp, "%s/%s.img", backup_path, name); @@ -721,7 +723,7 @@ int nandroid_restore_partition(const char* backup_path, const char* root) { sprintf(tmp, "%s%s.img", backup_path, root); ui_print("Restoring %s image...\n", name); - if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) { + if (0 != (ret = restore_raw_partition(vol->fs_type, vol->blk_device, tmp))) { ui_print("Error while flashing %s image!", name); return ret; } @@ -753,7 +755,7 @@ int nandroid_restore(const char* backup_path, int restore_boot, int restore_syst struct stat s; Volume *vol = volume_for_path("/wimax"); - if (restore_wimax && vol != NULL && 0 == stat(vol->device, &s)) + if (restore_wimax && vol != NULL && 0 == stat(vol->blk_device, &s)) { char serialno[PROPERTY_VALUE_MAX]; @@ -775,7 +777,7 @@ int nandroid_restore(const char* backup_path, int restore_boot, int restore_syst if (0 != (ret = format_volume("/wimax"))) return print_and_error("Error while formatting wimax!\n"); ui_print("Restoring WiMAX image...\n"); - if (0 != (ret = restore_raw_partition(vol->fs_type, vol->device, tmp))) + if (0 != (ret = restore_raw_partition(vol->fs_type, vol->blk_device, tmp))) return ret; } } diff --git a/roots.c b/roots.c index 44fa14ae6..7b9460b58 100644 --- a/roots.c +++ b/roots.c @@ -28,156 +28,51 @@ #include "common.h" #include "make_ext4fs.h" +#include +#include #include "flashutils/flashutils.h" #include "extendedcommands.h" -int num_volumes; -Volume* device_volumes; +static struct fstab *fstab = NULL; int get_num_volumes() { - return num_volumes; + return fstab->num_entries; } Volume* get_device_volumes() { - return device_volumes; -} - -static int is_null(const char* sz) { - if (sz == NULL) - return 1; - if (strcmp("NULL", sz) == 0) - return 1; - return 0; -} - -static char* dupe_string(const char* sz) { - if (is_null(sz)) - return NULL; - return strdup(sz); -} - -static int parse_options(char* options, Volume* volume) { - char* option; - while (option = strtok(options, ",")) { - options = NULL; - - if (strncmp(option, "length=", 7) == 0) { - volume->length = strtoll(option+7, NULL, 10); - } else if (strncmp(option, "fstype2=", 8) == 0) { - volume->fs_type2 = volume->fs_type; - volume->fs_type = strdup(option + 8); - } else if (strncmp(option, "fs_options=", 11) == 0) { - volume->fs_options = strdup(option + 11); - } else if (strncmp(option, "fs_options2=", 12) == 0) { - volume->fs_options2 = strdup(option + 12); - } else if (strncmp(option, "lun=", 4) == 0) { - volume->lun = strdup(option + 4); - } else { - LOGE("bad option \"%s\"\n", option); - return -1; - } - } - return 0; + return fstab->recs; } void load_volume_table() { - int alloc = 2; - device_volumes = malloc(alloc * sizeof(Volume)); - - // Insert an entry for /tmp, which is the ramdisk and is always mounted. - device_volumes[0].mount_point = "/tmp"; - device_volumes[0].fs_type = "ramdisk"; - device_volumes[0].device = NULL; - device_volumes[0].device2 = NULL; - device_volumes[0].fs_type2 = NULL; - device_volumes[0].fs_options = NULL; - device_volumes[0].fs_options2 = NULL; - device_volumes[0].lun = NULL; - device_volumes[0].length = 0; - num_volumes = 1; - - FILE* fstab = fopen("/etc/recovery.fstab", "r"); - if (fstab == NULL) { - LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno)); - return; - } - - char buffer[1024]; int i; - while (fgets(buffer, sizeof(buffer)-1, fstab)) { - for (i = 0; buffer[i] && isspace(buffer[i]); ++i); - if (buffer[i] == '\0' || buffer[i] == '#') continue; - - char* original = strdup(buffer); - - char* mount_point = strtok(buffer+i, " \t\n"); - char* fs_type = strtok(NULL, " \t\n"); - char* device = strtok(NULL, " \t\n"); - // lines may optionally have a second device, to use if - // mounting the first one fails. - char* options = NULL; - char* device2 = strtok(NULL, " \t\n"); - if (device2) { - if (device2[0] == '/') { - options = strtok(NULL, " \t\n"); - } else { - options = device2; - device2 = NULL; - } - } + int ret; - if (mount_point && fs_type && device) { - while (num_volumes >= alloc) { - alloc *= 2; - device_volumes = realloc(device_volumes, alloc*sizeof(Volume)); - } - device_volumes[num_volumes].mount_point = strdup(mount_point); - device_volumes[num_volumes].fs_type = strdup(fs_type); - device_volumes[num_volumes].device = strdup(device); - device_volumes[num_volumes].device2 = - device2 ? strdup(device2) : NULL; - - device_volumes[num_volumes].length = 0; - - device_volumes[num_volumes].fs_type2 = NULL; - device_volumes[num_volumes].fs_options = NULL; - device_volumes[num_volumes].fs_options2 = NULL; - device_volumes[num_volumes].lun = NULL; - - if (parse_options(options, device_volumes + num_volumes) != 0) { - LOGE("skipping malformed recovery.fstab line: %s\n", original); - } else { - ++num_volumes; - } - } else { - LOGE("skipping malformed recovery.fstab line: %s\n", original); - } - free(original); + fstab = fs_mgr_read_fstab("/etc/recovery.fstab"); + if (!fstab) { + LOGE("failed to read /etc/recovery.fstab\n"); + return; } - fclose(fstab); + ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk", 0); + if (ret < 0 ) { + LOGE("failed to add /tmp entry to fstab\n"); + fs_mgr_free_fstab(fstab); + fstab = NULL; + return; + } fprintf(stderr, "recovery filesystem table\n"); fprintf(stderr, "=========================\n"); - for (i = 0; i < num_volumes; ++i) { - Volume* v = &device_volumes[i]; - fprintf(stderr, " %d %s %s %s %s %lld\n", i, v->mount_point, v->fs_type, - v->device, v->device2, v->length); + for (i = 0; i < fstab->num_entries; ++i) { + Volume* v = &fstab->recs[i]; + fprintf(stderr, " %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, + v->blk_device, v->length); } - fprintf(stderr,"\n"); + fprintf(stderr, "\n"); } Volume* volume_for_path(const char* path) { - int i; - for (i = 0; i < num_volumes; ++i) { - Volume* v = device_volumes+i; - int len = strlen(v->mount_point); - if (strncmp(path, v->mount_point, len) == 0 && - (path[len] == '\0' || path[len] == '/')) { - return v; - } - } - return NULL; + return fs_mgr_get_entry_for_mount_point(fstab, path); } int try_mount(const char* device, const char* mount_point, const char* fs_type, const char* fs_options) { @@ -201,30 +96,43 @@ int try_mount(const char* device, const char* mount_point, const char* fs_type, int is_data_media() { int i; - for (i = 0; i < num_volumes; i++) { - Volume* vol = device_volumes + i; + int has_sdcard = 0; + for (i = 0; i < get_num_volumes(); i++) { + Volume* vol = get_device_volumes() + i; if (strcmp(vol->fs_type, "datamedia") == 0) return 1; + if (strcmp(vol->mount_point, "/sdcard") == 0) + has_sdcard = 1; } - return 0; + return !has_sdcard; } void setup_data_media() { int i; - for (i = 0; i < num_volumes; i++) { - Volume* vol = device_volumes + i; + char* mount_point = "/sdcard"; + for (i = 0; i < get_num_volumes(); i++) { + Volume* vol = get_device_volumes() + i; if (strcmp(vol->fs_type, "datamedia") == 0) { - rmdir(vol->mount_point); - mkdir("/data/media", 0755); - symlink("/data/media", vol->mount_point); - return; + mount_point = vol->mount_point; + break; } } + unlink(mount_point); + rmdir(mount_point); + mkdir("/data/media", 0755); + symlink("/data/media", mount_point); } int is_data_media_volume_path(const char* path) { Volume* v = volume_for_path(path); - return strcmp(v->fs_type, "datamedia") == 0; + if (v != NULL) + return strcmp(v->fs_type, "datamedia") == 0; + + if (!is_data_media()) { + return 0; + } + + return strcmp(path, "/sdcard") == 0 || path == strstr(path, "/sdcard/"); } int ensure_path_mounted(const char* path) { @@ -232,11 +140,6 @@ int ensure_path_mounted(const char* path) { } int ensure_path_mounted_at_mount_point(const char* path, const char* mount_point) { - Volume* v = volume_for_path(path); - if (v == NULL) { - LOGE("unknown volume for path [%s]\n", path); - return -1; - } if (is_data_media_volume_path(path)) { if (ui_should_log_stdout()) { LOGI("using /data/media for %s.\n", path); @@ -247,6 +150,11 @@ int ensure_path_mounted_at_mount_point(const char* path, const char* mount_point setup_data_media(); return 0; } + Volume* v = volume_for_path(path); + if (v == NULL) { + LOGE("unknown volume for path [%s]\n", path); + return -1; + } if (strcmp(v->fs_type, "ramdisk") == 0) { // the ramdisk is always mounted. return 0; @@ -275,10 +183,10 @@ int ensure_path_mounted_at_mount_point(const char* path, const char* mount_point // mount an MTD partition as a YAFFS2 filesystem. mtd_scan_partitions(); const MtdPartition* partition; - partition = mtd_find_partition_by_name(v->device); + partition = mtd_find_partition_by_name(v->blk_device); if (partition == NULL) { LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", - v->device, mount_point); + v->blk_device, mount_point); return -1; } return mtd_mount_partition(partition, mount_point, v->fs_type, 0); @@ -286,13 +194,11 @@ int ensure_path_mounted_at_mount_point(const char* path, const char* mount_point strcmp(v->fs_type, "ext3") == 0 || strcmp(v->fs_type, "rfs") == 0 || strcmp(v->fs_type, "vfat") == 0) { - if ((result = try_mount(v->device, mount_point, v->fs_type, v->fs_options)) == 0) - return 0; - if ((result = try_mount(v->device2, mount_point, v->fs_type, v->fs_options)) == 0) + if ((result = try_mount(v->blk_device, mount_point, v->fs_type, v->fs_options)) == 0) return 0; - if ((result = try_mount(v->device, mount_point, v->fs_type2, v->fs_options2)) == 0) + if ((result = try_mount(v->blk_device, mount_point, v->fs_type2, v->fs_options2)) == 0) return 0; - if ((result = try_mount(v->device2, mount_point, v->fs_type2, v->fs_options2)) == 0) + if ((result = try_mount(v->blk_device2, mount_point, v->fs_type2, v->fs_options2)) == 0) return 0; return result; } else { @@ -372,7 +278,7 @@ int format_volume(const char* volume) { LOGE("can't give path \"%s\" to format_volume\n", volume); return -1; #endif - return format_unknown_device(v->device, volume, NULL); + return format_unknown_device(v->blk_device, volume, NULL); } if (ensure_path_unmounted(volume) != 0) { @@ -382,31 +288,31 @@ int format_volume(const char* volume) { if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) { mtd_scan_partitions(); - const MtdPartition* partition = mtd_find_partition_by_name(v->device); + const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device); if (partition == NULL) { - LOGE("format_volume: no MTD partition \"%s\"\n", v->device); + LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device); return -1; } MtdWriteContext *write = mtd_write_partition(partition); if (write == NULL) { - LOGW("format_volume: can't open MTD \"%s\"\n", v->device); + LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device); return -1; } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { - LOGW("format_volume: can't erase MTD \"%s\"\n", v->device); + LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device); mtd_write_close(write); return -1; } else if (mtd_write_close(write)) { - LOGW("format_volume: can't close MTD \"%s\"\n", v->device); + LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device); return -1; } return 0; } if (strcmp(v->fs_type, "ext4") == 0) { - int result = make_ext4fs(v->device, v->length, volume, sehandle); + int result = make_ext4fs(v->blk_device, v->length, volume, sehandle); if (result != 0) { - LOGE("format_volume: make_extf4fs failed on %s\n", v->device); + LOGE("format_volume: make_extf4fs failed on %s\n", v->blk_device); return -1; } return 0; @@ -416,7 +322,7 @@ int format_volume(const char* volume) { LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type); return -1; #endif - return format_unknown_device(v->device, volume, v->fs_type); + return format_unknown_device(v->blk_device, volume, v->fs_type); } void handle_data_media_format(int handle) {