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, |
Iliyan Malchev | 201aa88 | 2011-03-11 16:27:56 -0800 | [diff] [blame^] | 180 | MS_NOATIME | MS_NODEV | MS_NODIRATIME | MS_RDONLY, ""); |
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, |
Iliyan Malchev | 201aa88 | 2011-03-11 16:27:56 -0800 | [diff] [blame^] | 187 | MS_NOATIME | MS_NODEV | |
| 188 | MS_NODIRATIME | MS_RDONLY, ""); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 189 | if (result == 0) return 0; |
| 190 | } |
| 191 | |
| 192 | LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno)); |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | 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] | 197 | return -1; |
| 198 | } |
| 199 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 200 | int ensure_path_unmounted(const char* path) { |
| 201 | Volume* v = volume_for_path(path); |
| 202 | if (v == NULL) { |
| 203 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 204 | return -1; |
| 205 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 206 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 207 | // the ramdisk is always mounted; you can't unmount it. |
| 208 | return -1; |
| 209 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 210 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 211 | int result; |
| 212 | result = scan_mounted_volumes(); |
| 213 | if (result < 0) { |
| 214 | LOGE("failed to scan mounted volumes\n"); |
| 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | const MountedVolume* mv = |
| 219 | find_mounted_volume_by_mount_point(v->mount_point); |
| 220 | if (mv == NULL) { |
| 221 | // volume is already unmounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 225 | return unmount_mounted_volume(mv); |
| 226 | } |
| 227 | |
| 228 | int format_volume(const char* volume) { |
| 229 | Volume* v = volume_for_path(volume); |
| 230 | if (v == NULL) { |
| 231 | LOGE("unknown volume \"%s\"\n", volume); |
| 232 | return -1; |
| 233 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 234 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 235 | // you can't format the ramdisk. |
| 236 | LOGE("can't format_volume \"%s\"", volume); |
| 237 | return -1; |
| 238 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 239 | if (strcmp(v->mount_point, volume) != 0) { |
| 240 | LOGE("can't give path \"%s\" to format_volume\n", volume); |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | if (ensure_path_unmounted(volume) != 0) { |
| 245 | LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point); |
| 246 | return -1; |
| 247 | } |
| 248 | |
| 249 | 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] | 250 | mtd_scan_partitions(); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 251 | 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] | 252 | if (partition == NULL) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 253 | 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] | 254 | return -1; |
| 255 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 256 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 257 | MtdWriteContext *write = mtd_write_partition(partition); |
| 258 | if (write == NULL) { |
| 259 | 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] | 260 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 261 | } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { |
| 262 | LOGW("format_volume: can't erase MTD \"%s\"\n", v->device); |
| 263 | mtd_write_close(write); |
| 264 | return -1; |
| 265 | } else if (mtd_write_close(write)) { |
| 266 | 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] | 267 | return -1; |
| 268 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 272 | if (strcmp(v->fs_type, "ext4") == 0) { |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 273 | int result = make_ext4fs(v->device, v->length); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 274 | if (result != 0) { |
| 275 | 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] | 276 | return -1; |
| 277 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 278 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 279 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 280 | |
| 281 | 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] | 282 | return -1; |
| 283 | } |