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 | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 59 | void load_volume_table() { |
| 60 | int alloc = 2; |
| 61 | device_volumes = malloc(alloc * sizeof(Volume)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 62 | |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 63 | // Insert an entry for /tmp, which is the ramdisk and is always mounted. |
| 64 | device_volumes[0].mount_point = "/tmp"; |
| 65 | device_volumes[0].fs_type = "ramdisk"; |
| 66 | device_volumes[0].device = NULL; |
| 67 | device_volumes[0].device2 = NULL; |
Koushik Dutta | e2b929d | 2011-03-01 21:41:54 -0800 | [diff] [blame] | 68 | device_volumes[0].fs_options = NULL; |
| 69 | device_volumes[0].fs_options2 = NULL; |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 70 | num_volumes = 1; |
| 71 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 72 | FILE* fstab = fopen("/etc/recovery.fstab", "r"); |
| 73 | if (fstab == NULL) { |
| 74 | LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno)); |
| 75 | return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 76 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 77 | |
| 78 | char buffer[1024]; |
| 79 | int i; |
| 80 | while (fgets(buffer, sizeof(buffer)-1, fstab)) { |
| 81 | for (i = 0; buffer[i] && isspace(buffer[i]); ++i); |
| 82 | if (buffer[i] == '\0' || buffer[i] == '#') continue; |
| 83 | |
| 84 | char* original = strdup(buffer); |
| 85 | |
| 86 | char* mount_point = strtok(buffer+i, " \t\n"); |
| 87 | char* fs_type = strtok(NULL, " \t\n"); |
| 88 | char* device = strtok(NULL, " \t\n"); |
| 89 | // lines may optionally have a second device, to use if |
| 90 | // mounting the first one fails. |
| 91 | char* device2 = strtok(NULL, " \t\n"); |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 92 | char* fs_type2 = strtok(NULL, " \t\n"); |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 93 | char* fs_options = strtok(NULL, " \t\n"); |
| 94 | char* fs_options2 = strtok(NULL, " \t\n"); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 95 | |
| 96 | if (mount_point && fs_type && device) { |
| 97 | while (num_volumes >= alloc) { |
| 98 | alloc *= 2; |
| 99 | device_volumes = realloc(device_volumes, alloc*sizeof(Volume)); |
| 100 | } |
| 101 | device_volumes[num_volumes].mount_point = strdup(mount_point); |
Koushik Dutta | e734dad | 2011-03-02 12:32:13 -0800 | [diff] [blame] | 102 | device_volumes[num_volumes].fs_type = !is_null(fs_type2) ? strdup(fs_type2) : strdup(fs_type); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 103 | device_volumes[num_volumes].device = strdup(device); |
| 104 | device_volumes[num_volumes].device2 = |
Koushik Dutta | 13ac7a4 | 2011-03-02 12:36:10 -0800 | [diff] [blame] | 105 | !is_null(device2) ? strdup(device2) : NULL; |
Koushik Dutta | e734dad | 2011-03-02 12:32:13 -0800 | [diff] [blame] | 106 | device_volumes[num_volumes].fs_type2 = !is_null(fs_type2) ? strdup(fs_type) : NULL; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 107 | |
Koushik Dutta | e734dad | 2011-03-02 12:32:13 -0800 | [diff] [blame] | 108 | if (!is_null(fs_type2)) { |
Koushik Dutta | 13ac7a4 | 2011-03-02 12:36:10 -0800 | [diff] [blame] | 109 | device_volumes[num_volumes].fs_options2 = dupe_string(fs_options); |
| 110 | device_volumes[num_volumes].fs_options = dupe_string(fs_options2); |
Koushik Dutta | e734dad | 2011-03-02 12:32:13 -0800 | [diff] [blame] | 111 | } |
| 112 | else { |
Koushik Dutta | 13ac7a4 | 2011-03-02 12:36:10 -0800 | [diff] [blame] | 113 | device_volumes[num_volumes].fs_options2 = NULL; |
| 114 | device_volumes[num_volumes].fs_options = dupe_string(fs_options); |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 115 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 116 | ++num_volumes; |
| 117 | } else { |
| 118 | LOGE("skipping malformed recovery.fstab line: %s\n", original); |
| 119 | } |
| 120 | free(original); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 121 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 122 | |
| 123 | fclose(fstab); |
| 124 | |
| 125 | printf("recovery filesystem table\n"); |
| 126 | printf("=========================\n"); |
| 127 | for (i = 0; i < num_volumes; ++i) { |
| 128 | Volume* v = &device_volumes[i]; |
| 129 | printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type, |
| 130 | v->device, v->device2); |
| 131 | } |
| 132 | printf("\n"); |
| 133 | } |
| 134 | |
| 135 | Volume* volume_for_path(const char* path) { |
| 136 | int i; |
| 137 | for (i = 0; i < num_volumes; ++i) { |
| 138 | Volume* v = device_volumes+i; |
| 139 | int len = strlen(v->mount_point); |
| 140 | if (strncmp(path, v->mount_point, len) == 0 && |
| 141 | (path[len] == '\0' || path[len] == '/')) { |
| 142 | return v; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | return NULL; |
| 146 | } |
| 147 | |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 148 | 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] | 149 | if (device == NULL || mount_point == NULL || fs_type == NULL) |
| 150 | return -1; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 151 | int ret = 0; |
| 152 | if (fs_options == NULL) { |
| 153 | ret = mount(device, mount_point, fs_type, |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 154 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 155 | } |
| 156 | else { |
| 157 | char mount_cmd[PATH_MAX]; |
Koushik Dutta | 7161f35 | 2011-02-27 17:00:47 -0800 | [diff] [blame] | 158 | 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] | 159 | ret = __system(mount_cmd); |
| 160 | } |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 161 | if (ret == 0) |
| 162 | return 0; |
| 163 | LOGW("failed to mount %s (%s)\n", device, strerror(errno)); |
| 164 | return ret; |
| 165 | } |
| 166 | |
Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 167 | int is_data_media() { |
| 168 | Volume *data = volume_for_path("/data"); |
| 169 | return data != NULL && strcmp(data->fs_type, "auto") == 0 && volume_for_path("/sdcard") == NULL; |
| 170 | } |
| 171 | |
| 172 | void setup_data_media() { |
| 173 | rmdir("/sdcard"); |
| 174 | mkdir("/data/media", 0755); |
| 175 | symlink("/data/media", "/sdcard"); |
| 176 | } |
| 177 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 178 | int ensure_path_mounted(const char* path) { |
| 179 | Volume* v = volume_for_path(path); |
| 180 | if (v == NULL) { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 181 | // no /sdcard? let's assume /data/media |
Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 182 | if (strstr(path, "/sdcard") == path && is_data_media()) { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 183 | LOGW("using /data/media, no /sdcard found.\n"); |
| 184 | int ret; |
| 185 | if (0 != (ret = ensure_path_mounted("/data"))) |
| 186 | return ret; |
Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 187 | setup_data_media(); |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 188 | return 0; |
| 189 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 190 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 191 | return -1; |
| 192 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 193 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 194 | // the ramdisk is always mounted. |
| 195 | return 0; |
| 196 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 197 | |
| 198 | int result; |
| 199 | result = scan_mounted_volumes(); |
| 200 | if (result < 0) { |
| 201 | LOGE("failed to scan mounted volumes\n"); |
| 202 | return -1; |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 203 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 204 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 205 | const MountedVolume* mv = |
| 206 | find_mounted_volume_by_mount_point(v->mount_point); |
| 207 | if (mv) { |
| 208 | // volume is already mounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 209 | return 0; |
| 210 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 211 | |
| 212 | mkdir(v->mount_point, 0755); // in case it doesn't already exist |
| 213 | |
| 214 | if (strcmp(v->fs_type, "yaffs2") == 0) { |
| 215 | // mount an MTD partition as a YAFFS2 filesystem. |
| 216 | mtd_scan_partitions(); |
| 217 | const MtdPartition* partition; |
| 218 | partition = mtd_find_partition_by_name(v->device); |
| 219 | if (partition == NULL) { |
| 220 | LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", |
| 221 | v->device, v->mount_point); |
| 222 | return -1; |
| 223 | } |
| 224 | return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0); |
| 225 | } else if (strcmp(v->fs_type, "ext4") == 0 || |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 226 | strcmp(v->fs_type, "ext3") == 0 || |
Koushik Dutta | ab1f8b8 | 2011-02-27 17:28:30 -0800 | [diff] [blame] | 227 | strcmp(v->fs_type, "rfs") == 0 || |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 228 | strcmp(v->fs_type, "vfat") == 0) { |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 229 | if ((result = try_mount(v->device, v->mount_point, v->fs_type, v->fs_options)) == 0) |
Koushik Dutta | 49ee9a8 | 2011-02-09 16:15:54 -0800 | [diff] [blame] | 230 | return 0; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 231 | if ((result = try_mount(v->device2, v->mount_point, v->fs_type, v->fs_options)) == 0) |
Koushik Dutta | 49ee9a8 | 2011-02-09 16:15:54 -0800 | [diff] [blame] | 232 | return 0; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 233 | if ((result = try_mount(v->device, v->mount_point, v->fs_type2, v->fs_options2)) == 0) |
Koushik Dutta | c7ef9af | 2011-02-09 16:32:42 -0800 | [diff] [blame] | 234 | return 0; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 235 | if ((result = try_mount(v->device2, v->mount_point, v->fs_type2, v->fs_options2)) == 0) |
Koushik Dutta | c7ef9af | 2011-02-09 16:32:42 -0800 | [diff] [blame] | 236 | return 0; |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 237 | return result; |
Koushik Dutta | 4995114 | 2010-12-18 21:58:43 -0800 | [diff] [blame] | 238 | } else { |
Koushik Dutta | 4196e0f | 2010-12-19 03:38:29 -0800 | [diff] [blame] | 239 | // let's try mounting with the mount binary and hope for the best. |
| 240 | char mount_cmd[PATH_MAX]; |
| 241 | sprintf(mount_cmd, "mount %s", path); |
| 242 | return __system(mount_cmd); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 246 | return -1; |
| 247 | } |
| 248 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 249 | int ensure_path_unmounted(const char* path) { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 250 | // if we are using /data/media, do not ever unmount volumes /data or /sdcard |
| 251 | if (volume_for_path("/sdcard") == NULL && (strstr(path, "/sdcard") == path || strstr(path, "/data") == path)) { |
| 252 | return 0; |
| 253 | } |
| 254 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 255 | Volume* v = volume_for_path(path); |
| 256 | if (v == NULL) { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 257 | // no /sdcard? let's assume /data/media |
Koushik Dutta | 38a9214 | 2011-06-10 09:45:52 -0700 | [diff] [blame] | 258 | if (strstr(path, "/sdcard") == path && is_data_media()) { |
Koushik Dutta | 5082299 | 2011-06-08 19:03:27 -0700 | [diff] [blame] | 259 | return ensure_path_unmounted("/data"); |
| 260 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 261 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 262 | return -1; |
| 263 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 264 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 265 | // the ramdisk is always mounted; you can't unmount it. |
| 266 | return -1; |
| 267 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 268 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 269 | int result; |
| 270 | result = scan_mounted_volumes(); |
| 271 | if (result < 0) { |
| 272 | LOGE("failed to scan mounted volumes\n"); |
| 273 | return -1; |
| 274 | } |
| 275 | |
| 276 | const MountedVolume* mv = |
| 277 | find_mounted_volume_by_mount_point(v->mount_point); |
| 278 | if (mv == NULL) { |
| 279 | // volume is already unmounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 283 | return unmount_mounted_volume(mv); |
| 284 | } |
| 285 | |
| 286 | int format_volume(const char* volume) { |
| 287 | Volume* v = volume_for_path(volume); |
| 288 | if (v == NULL) { |
Koushik Dutta | 55e5e7b | 2011-06-14 23:39:59 -0700 | [diff] [blame] | 289 | // no /sdcard? let's assume /data/media |
Koushik Dutta | 7905c80 | 2011-06-15 00:00:55 -0700 | [diff] [blame^] | 290 | if (strstr(volume, "/sdcard") == volume && is_data_media()) { |
| 291 | return format_unknown_device(NULL, volume, NULL); |
Koushik Dutta | 55e5e7b | 2011-06-14 23:39:59 -0700 | [diff] [blame] | 292 | } |
Koushik Dutta | bf4444f | 2010-12-18 18:57:47 -0800 | [diff] [blame] | 293 | // silent failure for sd-ext |
| 294 | if (strcmp(volume, "/sd-ext") == 0) |
| 295 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 296 | LOGE("unknown volume \"%s\"\n", volume); |
| 297 | return -1; |
| 298 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 299 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 300 | // you can't format the ramdisk. |
| 301 | LOGE("can't format_volume \"%s\"", volume); |
| 302 | return -1; |
| 303 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 304 | if (strcmp(v->mount_point, volume) != 0) { |
Koushik Dutta | 9f52e5f | 2011-01-02 14:11:24 -0800 | [diff] [blame] | 305 | #if 0 |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 306 | LOGE("can't give path \"%s\" to format_volume\n", volume); |
| 307 | return -1; |
Koushik Dutta | 9f52e5f | 2011-01-02 14:11:24 -0800 | [diff] [blame] | 308 | #endif |
| 309 | return format_unknown_device(v->device, volume, NULL); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (ensure_path_unmounted(volume) != 0) { |
| 313 | LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point); |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | 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] | 318 | mtd_scan_partitions(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 319 | 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] | 320 | if (partition == NULL) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 321 | 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] | 322 | return -1; |
| 323 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 324 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 325 | MtdWriteContext *write = mtd_write_partition(partition); |
| 326 | if (write == NULL) { |
| 327 | 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] | 328 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 329 | } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { |
| 330 | LOGW("format_volume: can't erase MTD \"%s\"\n", v->device); |
| 331 | mtd_write_close(write); |
| 332 | return -1; |
| 333 | } else if (mtd_write_close(write)) { |
| 334 | 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] | 335 | return -1; |
| 336 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 340 | if (strcmp(v->fs_type, "ext4") == 0) { |
| 341 | reset_ext4fs_info(); |
| 342 | int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0); |
| 343 | if (result != 0) { |
| 344 | 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] | 345 | return -1; |
| 346 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 347 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 348 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 349 | |
Koushik Dutta | 5d80817 | 2010-12-18 22:29:27 -0800 | [diff] [blame] | 350 | #if 0 |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 351 | 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] | 352 | return -1; |
Koushik Dutta | 5d80817 | 2010-12-18 22:29:27 -0800 | [diff] [blame] | 353 | #endif |
Koushik Dutta | a8708c6 | 2011-01-01 18:00:27 -0800 | [diff] [blame] | 354 | 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] | 355 | } |