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" |
| 26 | #include "mtdutils/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 | |
Kenny Root | 41dda82 | 2012-03-30 20:48:34 -0700 | [diff] [blame] | 34 | extern struct selabel_handle *sehandle; |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 35 | |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 36 | static int parse_options(char* options, Volume* volume) { |
| 37 | char* option; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 38 | while ((option = strtok(options, ","))) { |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 39 | options = NULL; |
| 40 | |
| 41 | if (strncmp(option, "length=", 7) == 0) { |
| 42 | volume->length = strtoll(option+7, NULL, 10); |
| 43 | } else { |
| 44 | LOGE("bad option \"%s\"\n", option); |
| 45 | return -1; |
| 46 | } |
| 47 | } |
| 48 | return 0; |
| 49 | } |
| 50 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 51 | void load_volume_table() { |
| 52 | int alloc = 2; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 53 | device_volumes = (Volume*)malloc(alloc * sizeof(Volume)); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 54 | |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 55 | // Insert an entry for /tmp, which is the ramdisk and is always mounted. |
| 56 | device_volumes[0].mount_point = "/tmp"; |
| 57 | device_volumes[0].fs_type = "ramdisk"; |
| 58 | device_volumes[0].device = NULL; |
| 59 | device_volumes[0].device2 = NULL; |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 60 | device_volumes[0].length = 0; |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 61 | num_volumes = 1; |
| 62 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 63 | FILE* fstab = fopen("/etc/recovery.fstab", "r"); |
| 64 | if (fstab == NULL) { |
| 65 | LOGE("failed to open /etc/recovery.fstab (%s)\n", strerror(errno)); |
| 66 | return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 67 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 68 | |
| 69 | char buffer[1024]; |
| 70 | int i; |
| 71 | while (fgets(buffer, sizeof(buffer)-1, fstab)) { |
| 72 | for (i = 0; buffer[i] && isspace(buffer[i]); ++i); |
| 73 | if (buffer[i] == '\0' || buffer[i] == '#') continue; |
| 74 | |
| 75 | char* original = strdup(buffer); |
| 76 | |
| 77 | char* mount_point = strtok(buffer+i, " \t\n"); |
| 78 | char* fs_type = strtok(NULL, " \t\n"); |
| 79 | char* device = strtok(NULL, " \t\n"); |
| 80 | // lines may optionally have a second device, to use if |
| 81 | // mounting the first one fails. |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 82 | char* options = NULL; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 83 | char* device2 = strtok(NULL, " \t\n"); |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 84 | if (device2) { |
| 85 | if (device2[0] == '/') { |
| 86 | options = strtok(NULL, " \t\n"); |
| 87 | } else { |
| 88 | options = device2; |
| 89 | device2 = NULL; |
| 90 | } |
| 91 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 92 | |
| 93 | if (mount_point && fs_type && device) { |
| 94 | while (num_volumes >= alloc) { |
| 95 | alloc *= 2; |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 96 | device_volumes = (Volume*)realloc(device_volumes, alloc*sizeof(Volume)); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 97 | } |
| 98 | device_volumes[num_volumes].mount_point = strdup(mount_point); |
| 99 | device_volumes[num_volumes].fs_type = strdup(fs_type); |
| 100 | device_volumes[num_volumes].device = strdup(device); |
| 101 | device_volumes[num_volumes].device2 = |
| 102 | device2 ? strdup(device2) : NULL; |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 103 | |
| 104 | device_volumes[num_volumes].length = 0; |
| 105 | if (parse_options(options, device_volumes + num_volumes) != 0) { |
| 106 | LOGE("skipping malformed recovery.fstab line: %s\n", original); |
| 107 | } else { |
| 108 | ++num_volumes; |
| 109 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 110 | } else { |
| 111 | LOGE("skipping malformed recovery.fstab line: %s\n", original); |
| 112 | } |
| 113 | free(original); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 114 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 115 | |
| 116 | fclose(fstab); |
| 117 | |
| 118 | printf("recovery filesystem table\n"); |
| 119 | printf("=========================\n"); |
| 120 | for (i = 0; i < num_volumes; ++i) { |
| 121 | Volume* v = &device_volumes[i]; |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 122 | printf(" %d %s %s %s %s %lld\n", i, v->mount_point, v->fs_type, |
| 123 | v->device, v->device2, v->length); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 124 | } |
| 125 | printf("\n"); |
| 126 | } |
| 127 | |
| 128 | Volume* volume_for_path(const char* path) { |
| 129 | int i; |
| 130 | for (i = 0; i < num_volumes; ++i) { |
| 131 | Volume* v = device_volumes+i; |
| 132 | int len = strlen(v->mount_point); |
| 133 | if (strncmp(path, v->mount_point, len) == 0 && |
| 134 | (path[len] == '\0' || path[len] == '/')) { |
| 135 | return v; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | return NULL; |
| 139 | } |
| 140 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 141 | int ensure_path_mounted(const char* path) { |
| 142 | Volume* v = volume_for_path(path); |
| 143 | if (v == NULL) { |
| 144 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 145 | return -1; |
| 146 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 147 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 148 | // the ramdisk is always mounted. |
| 149 | return 0; |
| 150 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 151 | |
| 152 | int result; |
| 153 | result = scan_mounted_volumes(); |
| 154 | if (result < 0) { |
| 155 | LOGE("failed to scan mounted volumes\n"); |
| 156 | return -1; |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 157 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 158 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 159 | const MountedVolume* mv = |
| 160 | find_mounted_volume_by_mount_point(v->mount_point); |
| 161 | if (mv) { |
| 162 | // volume is already mounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 163 | return 0; |
| 164 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 165 | |
| 166 | mkdir(v->mount_point, 0755); // in case it doesn't already exist |
| 167 | |
| 168 | if (strcmp(v->fs_type, "yaffs2") == 0) { |
| 169 | // mount an MTD partition as a YAFFS2 filesystem. |
| 170 | mtd_scan_partitions(); |
| 171 | const MtdPartition* partition; |
| 172 | partition = mtd_find_partition_by_name(v->device); |
| 173 | if (partition == NULL) { |
| 174 | LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", |
| 175 | v->device, v->mount_point); |
| 176 | return -1; |
| 177 | } |
| 178 | return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0); |
| 179 | } else if (strcmp(v->fs_type, "ext4") == 0 || |
| 180 | strcmp(v->fs_type, "vfat") == 0) { |
| 181 | result = mount(v->device, v->mount_point, v->fs_type, |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 182 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 183 | if (result == 0) return 0; |
| 184 | |
| 185 | if (v->device2) { |
| 186 | LOGW("failed to mount %s (%s); trying %s\n", |
| 187 | v->device, strerror(errno), v->device2); |
| 188 | result = mount(v->device2, v->mount_point, v->fs_type, |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 189 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 190 | if (result == 0) return 0; |
| 191 | } |
| 192 | |
| 193 | LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno)); |
| 194 | return -1; |
| 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) { |
| 232 | LOGE("unknown volume \"%s\"\n", volume); |
| 233 | return -1; |
| 234 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 235 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 236 | // you can't format the ramdisk. |
| 237 | LOGE("can't format_volume \"%s\"", volume); |
| 238 | return -1; |
| 239 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 240 | if (strcmp(v->mount_point, volume) != 0) { |
| 241 | LOGE("can't give path \"%s\" to format_volume\n", volume); |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | if (ensure_path_unmounted(volume) != 0) { |
| 246 | LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | 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] | 251 | mtd_scan_partitions(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 252 | 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] | 253 | if (partition == NULL) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 254 | 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] | 255 | return -1; |
| 256 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 257 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 258 | MtdWriteContext *write = mtd_write_partition(partition); |
| 259 | if (write == NULL) { |
| 260 | 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] | 261 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 262 | } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { |
| 263 | LOGW("format_volume: can't erase MTD \"%s\"\n", v->device); |
| 264 | mtd_write_close(write); |
| 265 | return -1; |
| 266 | } else if (mtd_write_close(write)) { |
| 267 | 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] | 268 | return -1; |
| 269 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 273 | if (strcmp(v->fs_type, "ext4") == 0) { |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 274 | int result = make_ext4fs(v->device, v->length, volume, sehandle); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 275 | if (result != 0) { |
| 276 | 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] | 277 | return -1; |
| 278 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 279 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 280 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 281 | |
| 282 | 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] | 283 | return -1; |
| 284 | } |