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