| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include <errno.h> | 
|  | 18 | #include <stdlib.h> | 
|  | 19 | #include <sys/mount.h> | 
|  | 20 | #include <sys/stat.h> | 
|  | 21 | #include <sys/types.h> | 
|  | 22 | #include <unistd.h> | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 23 | #include <ctype.h> | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 24 |  | 
|  | 25 | #include "mtdutils/mtdutils.h" | 
| Koushik Dutta | df1e406 | 2010-12-18 17:42:31 -0800 | [diff] [blame] | 26 | #include "mounts.h" | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 27 | #include "roots.h" | 
|  | 28 | #include "common.h" | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 29 | #include "make_ext4fs.h" | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 30 |  | 
| Koushik Dutta | 7adeadc | 2011-05-26 11:47:56 -0700 | [diff] [blame] | 31 | #include "flashutils/flashutils.h" | 
|  | 32 | #include "extendedcommands.h" | 
|  | 33 |  | 
| Kolja Dummann | 92796ec | 2011-02-13 20:59:30 +0100 | [diff] [blame] | 34 | int num_volumes; | 
|  | 35 | Volume* device_volumes; | 
|  | 36 |  | 
|  | 37 | int get_num_volumes() { | 
|  | 38 | return num_volumes; | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | Volume* get_device_volumes() { | 
|  | 42 | return device_volumes; | 
|  | 43 | } | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 44 |  | 
| Koushik Dutta | e734dad | 2011-03-02 12:32:13 -0800 | [diff] [blame] | 45 | static int is_null(const char* sz) { | 
|  | 46 | if (sz == NULL) | 
|  | 47 | return 1; | 
|  | 48 | if (strcmp("NULL", sz) == 0) | 
|  | 49 | return 1; | 
|  | 50 | return 0; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | static char* dupe_string(const char* sz) { | 
|  | 54 | if (is_null(sz)) | 
|  | 55 | return NULL; | 
|  | 56 | return strdup(sz); | 
|  | 57 | } | 
|  | 58 |  | 
| Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 59 | static int parse_options(char* options, Volume* volume) { | 
|  | 60 | char* option; | 
|  | 61 | while (option = strtok(options, ",")) { | 
|  | 62 | options = NULL; | 
|  | 63 |  | 
|  | 64 | if (strncmp(option, "length=", 7) == 0) { | 
|  | 65 | volume->length = strtoll(option+7, NULL, 10); | 
| Koushik Dutta | 0df56f4 | 2011-11-16 16:00:35 -0800 | [diff] [blame] | 66 | } else if (strncmp(option, "fstype2=", 8) == 0) { | 
|  | 67 | volume->fs_type2 = volume->fs_type; | 
|  | 68 | volume->fs_type = strdup(option); | 
|  | 69 | } else if (strncmp(option, "fs_options=", 11) == 0) { | 
|  | 70 | volume->fs_options = strdup(option); | 
|  | 71 | } else if (strncmp(option, "fs_options2=", 12) == 0) { | 
|  | 72 | volume->fs_options2 = strdup(option); | 
| Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 73 | } else { | 
|  | 74 | LOGE("bad option \"%s\"\n", option); | 
|  | 75 | return -1; | 
|  | 76 | } | 
|  | 77 | } | 
|  | 78 | return 0; | 
|  | 79 | } | 
|  | 80 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 81 | void load_volume_table() { | 
|  | 82 | int alloc = 2; | 
|  | 83 | device_volumes = malloc(alloc * sizeof(Volume)); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 84 |  | 
| Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 85 | // Insert an entry for /tmp, which is the ramdisk and is always mounted. | 
|  | 86 | device_volumes[0].mount_point = "/tmp"; | 
|  | 87 | device_volumes[0].fs_type = "ramdisk"; | 
|  | 88 | device_volumes[0].device = NULL; | 
|  | 89 | device_volumes[0].device2 = NULL; | 
| Koushik Dutta | 0df56f4 | 2011-11-16 16:00:35 -0800 | [diff] [blame] | 90 | device_volumes[0].fs_type2 = NULL; | 
| Koushik Dutta | e2b929d | 2011-03-01 21:41:54 -0800 | [diff] [blame] | 91 | device_volumes[0].fs_options = NULL; | 
|  | 92 | device_volumes[0].fs_options2 = NULL; | 
| Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 93 | device_volumes[0].length = 0; | 
| Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 94 | num_volumes = 1; | 
|  | 95 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 96 | FILE* fstab = fopen("/etc/recovery.fstab", "r"); | 
|  | 97 | if (fstab == NULL) { | 
|  | 98 | LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno)); | 
|  | 99 | return; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 100 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 101 |  | 
|  | 102 | char buffer[1024]; | 
|  | 103 | int i; | 
|  | 104 | while (fgets(buffer, sizeof(buffer)-1, fstab)) { | 
|  | 105 | for (i = 0; buffer[i] && isspace(buffer[i]); ++i); | 
|  | 106 | if (buffer[i] == '\0' || buffer[i] == '#') continue; | 
|  | 107 |  | 
|  | 108 | char* original = strdup(buffer); | 
|  | 109 |  | 
|  | 110 | char* mount_point = strtok(buffer+i, " \t\n"); | 
|  | 111 | char* fs_type = strtok(NULL, " \t\n"); | 
|  | 112 | char* device = strtok(NULL, " \t\n"); | 
|  | 113 | // lines may optionally have a second device, to use if | 
|  | 114 | // mounting the first one fails. | 
| Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 115 | char* options = NULL; | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 116 | char* device2 = strtok(NULL, " \t\n"); | 
| Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 117 | if (device2) { | 
|  | 118 | if (device2[0] == '/') { | 
|  | 119 | options = strtok(NULL, " \t\n"); | 
|  | 120 | } else { | 
|  | 121 | options = device2; | 
|  | 122 | device2 = NULL; | 
|  | 123 | } | 
|  | 124 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 125 |  | 
|  | 126 | if (mount_point && fs_type && device) { | 
|  | 127 | while (num_volumes >= alloc) { | 
|  | 128 | alloc *= 2; | 
|  | 129 | device_volumes = realloc(device_volumes, alloc*sizeof(Volume)); | 
|  | 130 | } | 
|  | 131 | device_volumes[num_volumes].mount_point = strdup(mount_point); | 
|  | 132 | device_volumes[num_volumes].fs_type = strdup(fs_type); | 
|  | 133 | device_volumes[num_volumes].device = strdup(device); | 
|  | 134 | device_volumes[num_volumes].device2 = | 
|  | 135 | device2 ? strdup(device2) : NULL; | 
| Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 136 |  | 
| Pawit Pornkitprasan | ff316eb | 2011-11-19 13:31:21 +0700 | [diff] [blame] | 137 | device_volumes[num_volumes].fs_type2 = NULL; | 
|  | 138 | device_volumes[num_volumes].fs_options = NULL; | 
|  | 139 | device_volumes[num_volumes].fs_options2 = NULL; | 
| Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 140 | device_volumes[num_volumes].length = 0; | 
|  | 141 | if (parse_options(options, device_volumes + num_volumes) != 0) { | 
|  | 142 | LOGE("skipping malformed recovery.fstab line: %s\n", original); | 
|  | 143 | } else { | 
|  | 144 | ++num_volumes; | 
| Koushik Dutta | e734dad | 2011-03-02 12:32:13 -0800 | [diff] [blame] | 145 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 146 | } else { | 
|  | 147 | LOGE("skipping malformed recovery.fstab line: %s\n", original); | 
|  | 148 | } | 
|  | 149 | free(original); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 150 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 151 |  | 
|  | 152 | fclose(fstab); | 
|  | 153 |  | 
|  | 154 | printf("recovery filesystem table\n"); | 
|  | 155 | printf("=========================\n"); | 
|  | 156 | for (i = 0; i < num_volumes; ++i) { | 
|  | 157 | Volume* v = &device_volumes[i]; | 
| Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 158 | printf("  %d %s %s %s %s %lld\n", i, v->mount_point, v->fs_type, | 
|  | 159 | v->device, v->device2, v->length); | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 160 | } | 
|  | 161 | printf("\n"); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | Volume* volume_for_path(const char* path) { | 
|  | 165 | int i; | 
|  | 166 | for (i = 0; i < num_volumes; ++i) { | 
|  | 167 | Volume* v = device_volumes+i; | 
|  | 168 | int len = strlen(v->mount_point); | 
|  | 169 | if (strncmp(path, v->mount_point, len) == 0 && | 
|  | 170 | (path[len] == '\0' || path[len] == '/')) { | 
|  | 171 | return v; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 172 | } | 
|  | 173 | } | 
|  | 174 | return NULL; | 
|  | 175 | } | 
|  | 176 |  | 
| Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 177 | int try_mount(const char* device, const char* mount_point, const char* fs_type, const char* fs_options) { | 
| Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 178 | if (device == NULL || mount_point == NULL || fs_type == NULL) | 
|  | 179 | return -1; | 
| Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 180 | int ret = 0; | 
|  | 181 | if (fs_options == NULL) { | 
|  | 182 | ret = mount(device, mount_point, fs_type, | 
| Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 183 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); | 
| Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 184 | } | 
|  | 185 | else { | 
|  | 186 | char mount_cmd[PATH_MAX]; | 
| Koushik Dutta | 7161f35 | 2011-02-27 17:00:47 -0800 | [diff] [blame] | 187 | sprintf(mount_cmd, "mount -t %s -o%s %s %s", fs_type, fs_options, device, mount_point); | 
| Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 188 | ret = __system(mount_cmd); | 
|  | 189 | } | 
| Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 190 | if (ret == 0) | 
|  | 191 | return 0; | 
|  | 192 | LOGW("failed to mount %s (%s)\n", device, strerror(errno)); | 
|  | 193 | return ret; | 
|  | 194 | } | 
|  | 195 |  | 
| Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 196 | int is_data_media() { | 
|  | 197 | Volume *data = volume_for_path("/data"); | 
|  | 198 | return data != NULL && strcmp(data->fs_type, "auto") == 0 && volume_for_path("/sdcard") == NULL; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | void setup_data_media() { | 
|  | 202 | rmdir("/sdcard"); | 
|  | 203 | mkdir("/data/media", 0755); | 
|  | 204 | symlink("/data/media", "/sdcard"); | 
|  | 205 | } | 
|  | 206 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 207 | int ensure_path_mounted(const char* path) { | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 208 | return ensure_path_mounted_at_mount_point(path, NULL); | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | int ensure_path_mounted_at_mount_point(const char* path, const char* mount_point) { | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 212 | Volume* v = volume_for_path(path); | 
|  | 213 | if (v == NULL) { | 
| Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 214 | // no /sdcard? let's assume /data/media | 
| Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 215 | if (strstr(path, "/sdcard") == path && is_data_media()) { | 
| Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 216 | LOGW("using /data/media, no /sdcard found.\n"); | 
|  | 217 | int ret; | 
|  | 218 | if (0 != (ret = ensure_path_mounted("/data"))) | 
|  | 219 | return ret; | 
| Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 220 | setup_data_media(); | 
| Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 221 | return 0; | 
|  | 222 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 223 | LOGE("unknown volume for path [%s]\n", path); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 224 | return -1; | 
|  | 225 | } | 
| Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 226 | if (strcmp(v->fs_type, "ramdisk") == 0) { | 
|  | 227 | // the ramdisk is always mounted. | 
|  | 228 | return 0; | 
|  | 229 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 230 |  | 
|  | 231 | int result; | 
|  | 232 | result = scan_mounted_volumes(); | 
|  | 233 | if (result < 0) { | 
|  | 234 | LOGE("failed to scan mounted volumes\n"); | 
|  | 235 | return -1; | 
| Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 236 | } | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 237 |  | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 238 | if (NULL == mount_point) | 
|  | 239 | mount_point = v->mount_point; | 
|  | 240 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 241 | const MountedVolume* mv = | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 242 | find_mounted_volume_by_mount_point(mount_point); | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 243 | if (mv) { | 
|  | 244 | // volume is already mounted | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 245 | return 0; | 
|  | 246 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 247 |  | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 248 | mkdir(mount_point, 0755);  // in case it doesn't already exist | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 249 |  | 
|  | 250 | if (strcmp(v->fs_type, "yaffs2") == 0) { | 
|  | 251 | // mount an MTD partition as a YAFFS2 filesystem. | 
|  | 252 | mtd_scan_partitions(); | 
|  | 253 | const MtdPartition* partition; | 
|  | 254 | partition = mtd_find_partition_by_name(v->device); | 
|  | 255 | if (partition == NULL) { | 
|  | 256 | LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 257 | v->device, mount_point); | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 258 | return -1; | 
|  | 259 | } | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 260 | return mtd_mount_partition(partition, mount_point, v->fs_type, 0); | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 261 | } else if (strcmp(v->fs_type, "ext4") == 0 || | 
| Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 262 | strcmp(v->fs_type, "ext3") == 0 || | 
| Koushik Dutta | ab1f8b8 | 2011-02-27 17:28:30 -0800 | [diff] [blame] | 263 | strcmp(v->fs_type, "rfs") == 0 || | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 264 | strcmp(v->fs_type, "vfat") == 0) { | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 265 | if ((result = try_mount(v->device, mount_point, v->fs_type, v->fs_options)) == 0) | 
| Koushik Dutta | 49ee9a8 | 2011-02-09 16:15:54 -0800 | [diff] [blame] | 266 | return 0; | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 267 | if ((result = try_mount(v->device2, mount_point, v->fs_type, v->fs_options)) == 0) | 
| Koushik Dutta | 49ee9a8 | 2011-02-09 16:15:54 -0800 | [diff] [blame] | 268 | return 0; | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 269 | if ((result = try_mount(v->device, mount_point, v->fs_type2, v->fs_options2)) == 0) | 
| Koushik Dutta | c7ef9af | 2011-02-09 16:32:42 -0800 | [diff] [blame] | 270 | return 0; | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 271 | if ((result = try_mount(v->device2, mount_point, v->fs_type2, v->fs_options2)) == 0) | 
| Koushik Dutta | c7ef9af | 2011-02-09 16:32:42 -0800 | [diff] [blame] | 272 | return 0; | 
| Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 273 | return result; | 
| Koushik Dutta | 4995114 | 2010-12-18 21:58:43 -0800 | [diff] [blame] | 274 | } else { | 
| Koushik Dutta | 4196e0f | 2010-12-19 03:38:29 -0800 | [diff] [blame] | 275 | // let's try mounting with the mount binary and hope for the best. | 
|  | 276 | char mount_cmd[PATH_MAX]; | 
|  | 277 | sprintf(mount_cmd, "mount %s", path); | 
|  | 278 | return __system(mount_cmd); | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 279 | } | 
|  | 280 |  | 
| Koushik Dutta | 5268131 | 2011-08-31 23:26:45 -0700 | [diff] [blame] | 281 | LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, mount_point); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 282 | return -1; | 
|  | 283 | } | 
|  | 284 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 285 | int ensure_path_unmounted(const char* path) { | 
| Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 286 | // if we are using /data/media, do not ever unmount volumes /data or /sdcard | 
|  | 287 | if (volume_for_path("/sdcard") == NULL && (strstr(path, "/sdcard") == path || strstr(path, "/data") == path)) { | 
|  | 288 | return 0; | 
|  | 289 | } | 
|  | 290 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 291 | Volume* v = volume_for_path(path); | 
|  | 292 | if (v == NULL) { | 
| Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 293 | // no /sdcard? let's assume /data/media | 
| Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 294 | if (strstr(path, "/sdcard") == path && is_data_media()) { | 
| Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 295 | return ensure_path_unmounted("/data"); | 
|  | 296 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 297 | LOGE("unknown volume for path [%s]\n", path); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 298 | return -1; | 
|  | 299 | } | 
| Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 300 | if (strcmp(v->fs_type, "ramdisk") == 0) { | 
|  | 301 | // the ramdisk is always mounted; you can't unmount it. | 
|  | 302 | return -1; | 
|  | 303 | } | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 304 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 305 | int result; | 
|  | 306 | result = scan_mounted_volumes(); | 
|  | 307 | if (result < 0) { | 
|  | 308 | LOGE("failed to scan mounted volumes\n"); | 
|  | 309 | return -1; | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | const MountedVolume* mv = | 
|  | 313 | find_mounted_volume_by_mount_point(v->mount_point); | 
|  | 314 | if (mv == NULL) { | 
|  | 315 | // volume is already unmounted | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 316 | return 0; | 
|  | 317 | } | 
|  | 318 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 319 | return unmount_mounted_volume(mv); | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | int format_volume(const char* volume) { | 
|  | 323 | Volume* v = volume_for_path(volume); | 
|  | 324 | if (v == NULL) { | 
| Koushik Dutta | 55e5e7b | 2011-06-14 23:39:59 -0700 | [diff] [blame] | 325 | // no /sdcard? let's assume /data/media | 
| Koushik Dutta | 7905c80 | 2011-06-15 00:00:55 -0700 | [diff] [blame] | 326 | if (strstr(volume, "/sdcard") == volume && is_data_media()) { | 
|  | 327 | return format_unknown_device(NULL, volume, NULL); | 
| Koushik Dutta | 55e5e7b | 2011-06-14 23:39:59 -0700 | [diff] [blame] | 328 | } | 
| Koushik Dutta | bf4444f | 2010-12-18 18:57:47 -0800 | [diff] [blame] | 329 | // silent failure for sd-ext | 
|  | 330 | if (strcmp(volume, "/sd-ext") == 0) | 
|  | 331 | return -1; | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 332 | LOGE("unknown volume \"%s\"\n", volume); | 
|  | 333 | return -1; | 
|  | 334 | } | 
| Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 335 | if (strcmp(v->fs_type, "ramdisk") == 0) { | 
|  | 336 | // you can't format the ramdisk. | 
|  | 337 | LOGE("can't format_volume \"%s\"", volume); | 
|  | 338 | return -1; | 
|  | 339 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 340 | if (strcmp(v->mount_point, volume) != 0) { | 
| Koushik Dutta | 9f52e5f | 2011-01-02 14:11:24 -0800 | [diff] [blame] | 341 | #if 0 | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 342 | LOGE("can't give path \"%s\" to format_volume\n", volume); | 
|  | 343 | return -1; | 
| Koushik Dutta | 9f52e5f | 2011-01-02 14:11:24 -0800 | [diff] [blame] | 344 | #endif | 
|  | 345 | return format_unknown_device(v->device, volume, NULL); | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 346 | } | 
|  | 347 |  | 
|  | 348 | if (ensure_path_unmounted(volume) != 0) { | 
|  | 349 | LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point); | 
|  | 350 | return -1; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) { | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 354 | mtd_scan_partitions(); | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 355 | const MtdPartition* partition = mtd_find_partition_by_name(v->device); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 356 | if (partition == NULL) { | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 357 | LOGE("format_volume: no MTD partition \"%s\"\n", v->device); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 358 | return -1; | 
|  | 359 | } | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 360 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 361 | MtdWriteContext *write = mtd_write_partition(partition); | 
|  | 362 | if (write == NULL) { | 
|  | 363 | LOGW("format_volume: can't open MTD \"%s\"\n", v->device); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 364 | return -1; | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 365 | } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { | 
|  | 366 | LOGW("format_volume: can't erase MTD \"%s\"\n", v->device); | 
|  | 367 | mtd_write_close(write); | 
|  | 368 | return -1; | 
|  | 369 | } else if (mtd_write_close(write)) { | 
|  | 370 | LOGW("format_volume: can't close MTD \"%s\"\n", v->device); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 371 | return -1; | 
|  | 372 | } | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 373 | return 0; | 
|  | 374 | } | 
|  | 375 |  | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 376 | if (strcmp(v->fs_type, "ext4") == 0) { | 
| Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 377 | int result = make_ext4fs(v->device, v->length); | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 378 | if (result != 0) { | 
|  | 379 | LOGE("format_volume: make_extf4fs failed on %s\n", v->device); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 380 | return -1; | 
|  | 381 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 382 | return 0; | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 383 | } | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 384 |  | 
| Koushik Dutta | 5d80817 | 2010-12-18 22:29:27 -0800 | [diff] [blame] | 385 | #if 0 | 
| Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 386 | LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 387 | return -1; | 
| Koushik Dutta | 5d80817 | 2010-12-18 22:29:27 -0800 | [diff] [blame] | 388 | #endif | 
| Koushik Dutta | a8708c6 | 2011-01-01 18:00:27 -0800 | [diff] [blame] | 389 | return format_unknown_device(v->device, volume, v->fs_type); | 
| The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 390 | } |