preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [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> |
| 23 | #include <ctype.h> |
| 24 | |
| 25 | #include "mtdutils/mtdutils.h" |
| 26 | #include "mounts.h" |
| 27 | #include "roots.h" |
| 28 | #include "common.h" |
| 29 | #include "make_ext4fs.h" |
| 30 | |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 31 | int num_volumes; |
| 32 | Volume* device_volumes; |
| 33 | |
| 34 | int get_num_volumes() { |
| 35 | return num_volumes; |
| 36 | } |
| 37 | |
| 38 | Volume* get_device_volumes() { |
| 39 | return device_volumes; |
| 40 | } |
| 41 | |
| 42 | static int is_null(const char* sz) { |
| 43 | if (sz == NULL) |
| 44 | return 1; |
| 45 | if (strcmp("NULL", sz) == 0) |
| 46 | return 1; |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static char* dupe_string(const char* sz) { |
| 51 | if (is_null(sz)) |
| 52 | return NULL; |
| 53 | return strdup(sz); |
| 54 | } |
preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [diff] [blame] | 55 | |
| 56 | void load_volume_table() { |
| 57 | int alloc = 2; |
| 58 | device_volumes = malloc(alloc * sizeof(Volume)); |
| 59 | |
| 60 | // Insert an entry for /tmp, which is the ramdisk and is always mounted. |
| 61 | device_volumes[0].mount_point = "/tmp"; |
| 62 | device_volumes[0].fs_type = "ramdisk"; |
| 63 | device_volumes[0].device = NULL; |
| 64 | device_volumes[0].device2 = NULL; |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 65 | device_volumes[0].fs_options = NULL; |
| 66 | device_volumes[0].fs_options2 = NULL; |
preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [diff] [blame] | 67 | num_volumes = 1; |
| 68 | |
| 69 | FILE* fstab = fopen("/etc/recovery.fstab", "r"); |
| 70 | if (fstab == NULL) { |
| 71 | LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno)); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | char buffer[1024]; |
| 76 | int i; |
| 77 | while (fgets(buffer, sizeof(buffer)-1, fstab)) { |
| 78 | for (i = 0; buffer[i] && isspace(buffer[i]); ++i); |
| 79 | if (buffer[i] == '\0' || buffer[i] == '#') continue; |
| 80 | |
| 81 | char* original = strdup(buffer); |
| 82 | |
| 83 | char* mount_point = strtok(buffer+i, " \t\n"); |
| 84 | char* fs_type = strtok(NULL, " \t\n"); |
| 85 | char* device = strtok(NULL, " \t\n"); |
| 86 | // lines may optionally have a second device, to use if |
| 87 | // mounting the first one fails. |
| 88 | char* device2 = strtok(NULL, " \t\n"); |
preludedrew | f1ec34a | 2011-02-17 22:42:33 -0700 | [diff] [blame] | 89 | char* fs_type2 = strtok(NULL, " \t\n"); |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 90 | char* fs_options = strtok(NULL, " \t\n"); |
| 91 | char* fs_options2 = strtok(NULL, " \t\n"); |
preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [diff] [blame] | 92 | |
| 93 | if (mount_point && fs_type && device) { |
| 94 | while (num_volumes >= alloc) { |
| 95 | alloc *= 2; |
| 96 | device_volumes = realloc(device_volumes, alloc*sizeof(Volume)); |
| 97 | } |
| 98 | device_volumes[num_volumes].mount_point = strdup(mount_point); |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 99 | device_volumes[num_volumes].fs_type = !is_null(fs_type2) ? strdup(fs_type2) : strdup(fs_type); |
preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [diff] [blame] | 100 | device_volumes[num_volumes].device = strdup(device); |
| 101 | device_volumes[num_volumes].device2 = |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 102 | !is_null(device2) ? strdup(device2) : NULL; |
| 103 | device_volumes[num_volumes].fs_type2 = !is_null(fs_type2) ? strdup(fs_type) : NULL; |
| 104 | |
| 105 | if (!is_null(fs_type2)) { |
| 106 | device_volumes[num_volumes].fs_options2 = dupe_string(fs_options); |
| 107 | device_volumes[num_volumes].fs_options = dupe_string(fs_options2); |
| 108 | } |
| 109 | else { |
| 110 | device_volumes[num_volumes].fs_options2 = NULL; |
| 111 | device_volumes[num_volumes].fs_options = dupe_string(fs_options); |
| 112 | } |
preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [diff] [blame] | 113 | ++num_volumes; |
| 114 | } else { |
| 115 | LOGE("skipping malformed recovery.fstab line: %s\n", original); |
| 116 | } |
| 117 | free(original); |
| 118 | } |
| 119 | |
| 120 | fclose(fstab); |
| 121 | |
| 122 | printf("recovery filesystem table\n"); |
| 123 | printf("=========================\n"); |
| 124 | for (i = 0; i < num_volumes; ++i) { |
| 125 | Volume* v = &device_volumes[i]; |
| 126 | printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type, |
| 127 | v->device, v->device2); |
| 128 | } |
| 129 | printf("\n"); |
| 130 | } |
| 131 | |
| 132 | Volume* volume_for_path(const char* path) { |
| 133 | int i; |
| 134 | for (i = 0; i < num_volumes; ++i) { |
| 135 | Volume* v = device_volumes+i; |
| 136 | int len = strlen(v->mount_point); |
| 137 | if (strncmp(path, v->mount_point, len) == 0 && |
| 138 | (path[len] == '\0' || path[len] == '/')) { |
| 139 | return v; |
| 140 | } |
| 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 145 | int try_mount(const char* device, const char* mount_point, const char* fs_type, const char* fs_options) { |
preludedrew | f1ec34a | 2011-02-17 22:42:33 -0700 | [diff] [blame] | 146 | if (device == NULL || mount_point == NULL || fs_type == NULL) |
| 147 | return -1; |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 148 | int ret = 0; |
| 149 | if (fs_options == NULL) { |
| 150 | ret = mount(device, mount_point, fs_type, |
preludedrew | f1ec34a | 2011-02-17 22:42:33 -0700 | [diff] [blame] | 151 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 152 | } |
| 153 | else { |
| 154 | char mount_cmd[PATH_MAX]; |
| 155 | sprintf(mount_cmd, "mount -t %s -o%s %s %s", fs_type, fs_options, device, mount_point); |
| 156 | LOGE("%s\n", mount_cmd); |
| 157 | ret = __system(mount_cmd); |
| 158 | } |
preludedrew | f1ec34a | 2011-02-17 22:42:33 -0700 | [diff] [blame] | 159 | if (ret == 0) |
| 160 | return 0; |
| 161 | LOGW("failed to mount %s (%s)\n", device, strerror(errno)); |
| 162 | return ret; |
| 163 | } |
| 164 | |
preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [diff] [blame] | 165 | int ensure_path_mounted(const char* path) { |
| 166 | Volume* v = volume_for_path(path); |
| 167 | if (v == NULL) { |
| 168 | LOGE("unknown volume for path [%s]\n", path); |
| 169 | return -1; |
| 170 | } |
| 171 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 172 | // the ramdisk is always mounted. |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | int result; |
| 177 | result = scan_mounted_volumes(); |
| 178 | if (result < 0) { |
| 179 | LOGE("failed to scan mounted volumes\n"); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | const MountedVolume* mv = |
| 184 | find_mounted_volume_by_mount_point(v->mount_point); |
| 185 | if (mv) { |
| 186 | // volume is already mounted |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | mkdir(v->mount_point, 0755); // in case it doesn't already exist |
| 191 | |
| 192 | if (strcmp(v->fs_type, "yaffs2") == 0) { |
| 193 | // mount an MTD partition as a YAFFS2 filesystem. |
| 194 | mtd_scan_partitions(); |
| 195 | const MtdPartition* partition; |
| 196 | partition = mtd_find_partition_by_name(v->device); |
| 197 | if (partition == NULL) { |
| 198 | LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", |
| 199 | v->device, v->mount_point); |
| 200 | return -1; |
| 201 | } |
| 202 | return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0); |
| 203 | } else if (strcmp(v->fs_type, "ext4") == 0 || |
preludedrew | f1ec34a | 2011-02-17 22:42:33 -0700 | [diff] [blame] | 204 | strcmp(v->fs_type, "ext3") == 0 || |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 205 | strcmp(v->fs_type, "rfs") == 0 || |
preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [diff] [blame] | 206 | strcmp(v->fs_type, "vfat") == 0) { |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 207 | if ((result = try_mount(v->device, v->mount_point, v->fs_type, v->fs_options)) == 0) |
preludedrew | f1ec34a | 2011-02-17 22:42:33 -0700 | [diff] [blame] | 208 | return 0; |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 209 | if ((result = try_mount(v->device2, v->mount_point, v->fs_type, v->fs_options)) == 0) |
preludedrew | f1ec34a | 2011-02-17 22:42:33 -0700 | [diff] [blame] | 210 | return 0; |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 211 | if ((result = try_mount(v->device, v->mount_point, v->fs_type2, v->fs_options2)) == 0) |
preludedrew | f1ec34a | 2011-02-17 22:42:33 -0700 | [diff] [blame] | 212 | return 0; |
andrew.boren | 1acd15e | 2011-03-31 21:41:22 -0700 | [diff] [blame^] | 213 | if ((result = try_mount(v->device2, v->mount_point, v->fs_type2, v->fs_options2)) == 0) |
preludedrew | f1ec34a | 2011-02-17 22:42:33 -0700 | [diff] [blame] | 214 | return 0; |
| 215 | return result; |
preludedrew | 38058dc | 2011-01-29 23:30:44 -0700 | [diff] [blame] | 216 | } else { |
| 217 | // let's try mounting with the mount binary and hope for the best. |
| 218 | char mount_cmd[PATH_MAX]; |
| 219 | sprintf(mount_cmd, "mount %s", path); |
| 220 | return __system(mount_cmd); |
| 221 | } |
| 222 | |
| 223 | LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point); |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | int ensure_path_unmounted(const char* path) { |
| 228 | Volume* v = volume_for_path(path); |
| 229 | if (v == NULL) { |
| 230 | LOGE("unknown volume for path [%s]\n", path); |
| 231 | return -1; |
| 232 | } |
| 233 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 234 | // the ramdisk is always mounted; you can't unmount it. |
| 235 | return -1; |
| 236 | } |
| 237 | |
| 238 | int result; |
| 239 | result = scan_mounted_volumes(); |
| 240 | if (result < 0) { |
| 241 | LOGE("failed to scan mounted volumes\n"); |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | const MountedVolume* mv = |
| 246 | find_mounted_volume_by_mount_point(v->mount_point); |
| 247 | if (mv == NULL) { |
| 248 | // volume is already unmounted |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | return unmount_mounted_volume(mv); |
| 253 | } |
| 254 | |
| 255 | int format_volume(const char* volume) { |
| 256 | Volume* v = volume_for_path(volume); |
| 257 | if (v == NULL) { |
| 258 | // silent failure for sd-ext |
| 259 | if (strcmp(volume, "/sd-ext") == 0) |
| 260 | return -1; |
| 261 | LOGE("unknown volume \"%s\"\n", volume); |
| 262 | return -1; |
| 263 | } |
| 264 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 265 | // you can't format the ramdisk. |
| 266 | LOGE("can't format_volume \"%s\"", volume); |
| 267 | return -1; |
| 268 | } |
| 269 | if (strcmp(v->mount_point, volume) != 0) { |
| 270 | #if 0 |
| 271 | LOGE("can't give path \"%s\" to format_volume\n", volume); |
| 272 | return -1; |
| 273 | #endif |
| 274 | return format_unknown_device(v->device, volume, NULL); |
| 275 | } |
| 276 | |
| 277 | if (ensure_path_unmounted(volume) != 0) { |
| 278 | LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point); |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) { |
| 283 | mtd_scan_partitions(); |
| 284 | const MtdPartition* partition = mtd_find_partition_by_name(v->device); |
| 285 | if (partition == NULL) { |
| 286 | LOGE("format_volume: no MTD partition \"%s\"\n", v->device); |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | MtdWriteContext *write = mtd_write_partition(partition); |
| 291 | if (write == NULL) { |
| 292 | LOGW("format_volume: can't open MTD \"%s\"\n", v->device); |
| 293 | return -1; |
| 294 | } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { |
| 295 | LOGW("format_volume: can't erase MTD \"%s\"\n", v->device); |
| 296 | mtd_write_close(write); |
| 297 | return -1; |
| 298 | } else if (mtd_write_close(write)) { |
| 299 | LOGW("format_volume: can't close MTD \"%s\"\n", v->device); |
| 300 | return -1; |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | if (strcmp(v->fs_type, "emmc") == 0) { |
| 306 | return erase_raw_partition(v->device); |
| 307 | } |
| 308 | |
| 309 | if (strcmp(v->fs_type, "ext4") == 0) { |
| 310 | reset_ext4fs_info(); |
| 311 | int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0); |
| 312 | if (result != 0) { |
| 313 | LOGE("format_volume: make_extf4fs failed on %s\n", v->device); |
| 314 | return -1; |
| 315 | } |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | #if 0 |
| 320 | LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type); |
| 321 | return -1; |
| 322 | #endif |
| 323 | return format_unknown_device(v->device, volume, v->fs_type); |
| 324 | } |