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 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 31 | static int num_volumes = 0; |
| 32 | static Volume* device_volumes = NULL; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 33 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 34 | void load_volume_table() { |
| 35 | int alloc = 2; |
| 36 | device_volumes = malloc(alloc * sizeof(Volume)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 37 | |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 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 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 49 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 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"); |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 65 | char* fs_type2 = strtok(NULL, " \t\n"); |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 66 | char* fs_options = strtok(NULL, " \t\n"); |
| 67 | char* fs_options2 = strtok(NULL, " \t\n"); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 68 | |
| 69 | if (mount_point && fs_type && device) { |
| 70 | while (num_volumes >= alloc) { |
| 71 | alloc *= 2; |
| 72 | device_volumes = realloc(device_volumes, alloc*sizeof(Volume)); |
| 73 | } |
| 74 | device_volumes[num_volumes].mount_point = strdup(mount_point); |
Koushik Dutta | c7ef9af | 2011-02-09 16:32:42 -0800 | [diff] [blame] | 75 | device_volumes[num_volumes].fs_type = fs_type2 != NULL ? strdup(fs_type2) : strdup(fs_type); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 76 | device_volumes[num_volumes].device = strdup(device); |
| 77 | device_volumes[num_volumes].device2 = |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 78 | (device2 != NULL && strcmp(device2, "NULL") != 0) ? strdup(device2) : NULL; |
Koushik Dutta | c7ef9af | 2011-02-09 16:32:42 -0800 | [diff] [blame] | 79 | device_volumes[num_volumes].fs_type2 = fs_type2 != NULL ? strdup(fs_type) : NULL; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 80 | |
| 81 | if (fs_type2 != NULL) { |
| 82 | char *temp; |
| 83 | temp = fs_options2; |
| 84 | fs_options2 = fs_options; |
| 85 | fs_options = temp; |
| 86 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 87 | ++num_volumes; |
| 88 | } else { |
| 89 | LOGE("skipping malformed recovery.fstab line: %s\n", original); |
| 90 | } |
| 91 | free(original); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 92 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 93 | |
| 94 | fclose(fstab); |
| 95 | |
| 96 | printf("recovery filesystem table\n"); |
| 97 | printf("=========================\n"); |
| 98 | for (i = 0; i < num_volumes; ++i) { |
| 99 | Volume* v = &device_volumes[i]; |
| 100 | printf(" %d %s %s %s %s\n", i, v->mount_point, v->fs_type, |
| 101 | v->device, v->device2); |
| 102 | } |
| 103 | printf("\n"); |
| 104 | } |
| 105 | |
| 106 | Volume* volume_for_path(const char* path) { |
| 107 | int i; |
| 108 | for (i = 0; i < num_volumes; ++i) { |
| 109 | Volume* v = device_volumes+i; |
| 110 | int len = strlen(v->mount_point); |
| 111 | if (strncmp(path, v->mount_point, len) == 0 && |
| 112 | (path[len] == '\0' || path[len] == '/')) { |
| 113 | return v; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 119 | 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] | 120 | if (device == NULL || mount_point == NULL || fs_type == NULL) |
| 121 | return -1; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 122 | int ret = 0; |
| 123 | if (fs_options == NULL) { |
| 124 | ret = mount(device, mount_point, fs_type, |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 125 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 126 | } |
| 127 | else { |
| 128 | char mount_cmd[PATH_MAX]; |
Koushik Dutta | 7161f35 | 2011-02-27 17:00:47 -0800 | [diff] [blame] | 129 | 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] | 130 | ret = __system(mount_cmd); |
| 131 | } |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 132 | if (ret == 0) |
| 133 | return 0; |
| 134 | LOGW("failed to mount %s (%s)\n", device, strerror(errno)); |
| 135 | return ret; |
| 136 | } |
| 137 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 138 | int ensure_path_mounted(const char* path) { |
| 139 | Volume* v = volume_for_path(path); |
| 140 | if (v == NULL) { |
| 141 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 142 | return -1; |
| 143 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 144 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 145 | // the ramdisk is always mounted. |
| 146 | return 0; |
| 147 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 148 | |
| 149 | int result; |
| 150 | result = scan_mounted_volumes(); |
| 151 | if (result < 0) { |
| 152 | LOGE("failed to scan mounted volumes\n"); |
| 153 | return -1; |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 154 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 155 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 156 | const MountedVolume* mv = |
| 157 | find_mounted_volume_by_mount_point(v->mount_point); |
| 158 | if (mv) { |
| 159 | // volume is already mounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 160 | return 0; |
| 161 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 162 | |
| 163 | mkdir(v->mount_point, 0755); // in case it doesn't already exist |
| 164 | |
| 165 | if (strcmp(v->fs_type, "yaffs2") == 0) { |
| 166 | // mount an MTD partition as a YAFFS2 filesystem. |
| 167 | mtd_scan_partitions(); |
| 168 | const MtdPartition* partition; |
| 169 | partition = mtd_find_partition_by_name(v->device); |
| 170 | if (partition == NULL) { |
| 171 | LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", |
| 172 | v->device, v->mount_point); |
| 173 | return -1; |
| 174 | } |
| 175 | return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0); |
| 176 | } else if (strcmp(v->fs_type, "ext4") == 0 || |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 177 | strcmp(v->fs_type, "ext3") == 0 || |
Koushik Dutta | ab1f8b8 | 2011-02-27 17:28:30 -0800 | [diff] [blame^] | 178 | strcmp(v->fs_type, "rfs") == 0 || |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 179 | strcmp(v->fs_type, "vfat") == 0) { |
Koushik Dutta | 49ee9a8 | 2011-02-09 16:15:54 -0800 | [diff] [blame] | 180 | // try fs type 2 first |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 181 | 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] | 182 | return 0; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 183 | 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] | 184 | return 0; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 185 | 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] | 186 | return 0; |
Koushik Dutta | e8bc2c8 | 2011-02-27 14:00:19 -0800 | [diff] [blame] | 187 | 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] | 188 | return 0; |
Koushik Dutta | 64d79c6 | 2011-02-09 06:12:44 -0800 | [diff] [blame] | 189 | return result; |
Koushik Dutta | 4995114 | 2010-12-18 21:58:43 -0800 | [diff] [blame] | 190 | } else { |
Koushik Dutta | 4196e0f | 2010-12-19 03:38:29 -0800 | [diff] [blame] | 191 | // let's try mounting with the mount binary and hope for the best. |
| 192 | char mount_cmd[PATH_MAX]; |
| 193 | sprintf(mount_cmd, "mount %s", path); |
| 194 | return __system(mount_cmd); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | 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] | 198 | return -1; |
| 199 | } |
| 200 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 201 | int ensure_path_unmounted(const char* path) { |
| 202 | Volume* v = volume_for_path(path); |
| 203 | if (v == NULL) { |
| 204 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 205 | return -1; |
| 206 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 207 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 208 | // the ramdisk is always mounted; you can't unmount it. |
| 209 | return -1; |
| 210 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 211 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 212 | int result; |
| 213 | result = scan_mounted_volumes(); |
| 214 | if (result < 0) { |
| 215 | LOGE("failed to scan mounted volumes\n"); |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | const MountedVolume* mv = |
| 220 | find_mounted_volume_by_mount_point(v->mount_point); |
| 221 | if (mv == NULL) { |
| 222 | // volume is already unmounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 226 | return unmount_mounted_volume(mv); |
| 227 | } |
| 228 | |
| 229 | int format_volume(const char* volume) { |
| 230 | Volume* v = volume_for_path(volume); |
| 231 | if (v == NULL) { |
Koushik Dutta | bf4444f | 2010-12-18 18:57:47 -0800 | [diff] [blame] | 232 | // silent failure for sd-ext |
| 233 | if (strcmp(volume, "/sd-ext") == 0) |
| 234 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 235 | LOGE("unknown volume \"%s\"\n", volume); |
| 236 | return -1; |
| 237 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 238 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 239 | // you can't format the ramdisk. |
| 240 | LOGE("can't format_volume \"%s\"", volume); |
| 241 | return -1; |
| 242 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 243 | if (strcmp(v->mount_point, volume) != 0) { |
Koushik Dutta | 9f52e5f | 2011-01-02 14:11:24 -0800 | [diff] [blame] | 244 | #if 0 |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 245 | LOGE("can't give path \"%s\" to format_volume\n", volume); |
| 246 | return -1; |
Koushik Dutta | 9f52e5f | 2011-01-02 14:11:24 -0800 | [diff] [blame] | 247 | #endif |
| 248 | return format_unknown_device(v->device, volume, NULL); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | if (ensure_path_unmounted(volume) != 0) { |
| 252 | LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | 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] | 257 | mtd_scan_partitions(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 258 | 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] | 259 | if (partition == NULL) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 260 | 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] | 261 | return -1; |
| 262 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 263 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 264 | MtdWriteContext *write = mtd_write_partition(partition); |
| 265 | if (write == NULL) { |
| 266 | 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] | 267 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 268 | } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { |
| 269 | LOGW("format_volume: can't erase MTD \"%s\"\n", v->device); |
| 270 | mtd_write_close(write); |
| 271 | return -1; |
| 272 | } else if (mtd_write_close(write)) { |
| 273 | 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] | 274 | return -1; |
| 275 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
Christopher Lais | 066a102 | 2011-01-16 06:02:34 -0600 | [diff] [blame] | 279 | if (strcmp(v->fs_type, "emmc") == 0) { |
| 280 | return erase_raw_partition(v->device); |
| 281 | } |
| 282 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 283 | if (strcmp(v->fs_type, "ext4") == 0) { |
| 284 | reset_ext4fs_info(); |
| 285 | int result = make_ext4fs(v->device, NULL, NULL, 0, 0, 0); |
| 286 | if (result != 0) { |
| 287 | 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] | 288 | return -1; |
| 289 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 290 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 291 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 292 | |
Koushik Dutta | 5d80817 | 2010-12-18 22:29:27 -0800 | [diff] [blame] | 293 | #if 0 |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 294 | 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] | 295 | return -1; |
Koushik Dutta | 5d80817 | 2010-12-18 22:29:27 -0800 | [diff] [blame] | 296 | #endif |
Koushik Dutta | a8708c6 | 2011-01-01 18:00:27 -0800 | [diff] [blame] | 297 | 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] | 298 | } |