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