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