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 "mtdutils/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 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 49 void load_volume_table() { 50 int alloc = 2; 51 device_volumes = malloc(alloc * sizeof(Volume)); 52 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; 58 device_volumes[0].length = 0; 59 num_volumes = 1; 60 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; 65 } 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. 80 char* options = NULL; 81 char* device2 = strtok(NULL, " \t\n"); 82 if (device2) { 83 if (device2[0] == '/') { 84 options = strtok(NULL, " \t\n"); 85 } else { 86 options = device2; 87 device2 = NULL; 88 } 89 } 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; 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 } 108 } else { 109 LOGE("skipping malformed recovery.fstab line: %s\n", original); 110 } 111 free(original); 112 } 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]; 120 printf(" %d %s %s %s %s %lld\n", i, v->mount_point, v->fs_type, 121 v->device, v->device2, v->length); 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; 134 } 135 } 136 return NULL; 137 } 138 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); 143 return -1; 144 } 145 if (strcmp(v->fs_type, "ramdisk") == 0) { 146 // the ramdisk is always mounted. 147 return 0; 148 } 149 150 int result; 151 result = scan_mounted_volumes(); 152 if (result < 0) { 153 LOGE("failed to scan mounted volumes\n"); 154 return -1; 155 } 156 157 const MountedVolume* mv = 158 find_mounted_volume_by_mount_point(v->mount_point); 159 if (mv) { 160 // volume is already mounted 161 return 0; 162 } 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, 180 MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); 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, 187 MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); 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); 196 return -1; 197 } 198 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); 203 return -1; 204 } 205 if (strcmp(v->fs_type, "ramdisk") == 0) { 206 // the ramdisk is always mounted; you can't unmount it. 207 return -1; 208 } 209 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 221 return 0; 222 } 223 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 } 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 } 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) { 249 mtd_scan_partitions(); 250 const MtdPartition* partition = mtd_find_partition_by_name(v->device); 251 if (partition == NULL) { 252 LOGE("format_volume: no MTD partition \"%s\"\n", v->device); 253 return -1; 254 } 255 256 MtdWriteContext *write = mtd_write_partition(partition); 257 if (write == NULL) { 258 LOGW("format_volume: can't open MTD \"%s\"\n", v->device); 259 return -1; 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); 266 return -1; 267 } 268 return 0; 269 } 270 271 if (strcmp(v->fs_type, "ext4") == 0) { 272 int result = make_ext4fs(v->device, v->length); 273 if (result != 0) { 274 LOGE("format_volume: make_extf4fs failed on %s\n", v->device); 275 return -1; 276 } 277 return 0; 278 } 279 280 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type); 281 return -1; 282 } 283