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 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | extern "C" { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 26 | #include <fs_mgr.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 27 | #include "mtdutils/mtdutils.h" |
| 28 | #include "mtdutils/mounts.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 30 | #include "roots.h" |
| 31 | #include "common.h" |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 32 | #include "make_ext4fs.h" |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 33 | #include "partitions.hpp" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 34 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 35 | static struct fstab *fstab = NULL; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 36 | |
Kenny Root | 41dda82 | 2012-03-30 20:48:34 -0700 | [diff] [blame] | 37 | extern struct selabel_handle *sehandle; |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 38 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 39 | void load_volume_table() |
| 40 | { |
| 41 | int i; |
| 42 | int ret; |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 43 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 44 | fstab = fs_mgr_read_fstab("/etc/recovery.fstab"); |
| 45 | if (!fstab) { |
| 46 | LOGE("failed to read /etc/recovery.fstab\n"); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 47 | return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 48 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 49 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 50 | ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk", 0); |
| 51 | if (ret < 0 ) { |
| 52 | LOGE("failed to add /tmp entry to fstab\n"); |
| 53 | fs_mgr_free_fstab(fstab); |
| 54 | fstab = NULL; |
| 55 | return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 56 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 57 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 58 | printf("recovery filesystem table\n"); |
| 59 | printf("=========================\n"); |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 60 | for (i = 0; i < fstab->num_entries; ++i) { |
| 61 | Volume* v = &fstab->recs[i]; |
| 62 | printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, |
| 63 | v->blk_device, v->length); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 64 | } |
| 65 | printf("\n"); |
| 66 | } |
| 67 | |
| 68 | Volume* volume_for_path(const char* path) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 69 | return fs_mgr_get_entry_for_mount_point(fstab, path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 72 | int ensure_path_mounted(const char* path) { |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 73 | if (PartitionManager.Mount_By_Path(path, true)) |
| 74 | return 0; |
| 75 | else |
| 76 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 77 | Volume* v = volume_for_path(path); |
| 78 | if (v == NULL) { |
| 79 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 80 | return -1; |
| 81 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 82 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 83 | // the ramdisk is always mounted. |
| 84 | return 0; |
| 85 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 86 | |
| 87 | int result; |
| 88 | result = scan_mounted_volumes(); |
| 89 | if (result < 0) { |
| 90 | LOGE("failed to scan mounted volumes\n"); |
| 91 | return -1; |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 92 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 93 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 94 | const MountedVolume* mv = |
| 95 | find_mounted_volume_by_mount_point(v->mount_point); |
| 96 | if (mv) { |
| 97 | // volume is already mounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 100 | |
| 101 | mkdir(v->mount_point, 0755); // in case it doesn't already exist |
| 102 | |
| 103 | if (strcmp(v->fs_type, "yaffs2") == 0) { |
| 104 | // mount an MTD partition as a YAFFS2 filesystem. |
| 105 | mtd_scan_partitions(); |
| 106 | const MtdPartition* partition; |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 107 | partition = mtd_find_partition_by_name(v->blk_device); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 108 | if (partition == NULL) { |
| 109 | LOGE("failed to find \"%s\" partition to mount at \"%s\"\n", |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 110 | v->blk_device, v->mount_point); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 111 | return -1; |
| 112 | } |
| 113 | return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0); |
| 114 | } else if (strcmp(v->fs_type, "ext4") == 0 || |
| 115 | strcmp(v->fs_type, "vfat") == 0) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 116 | result = mount(v->blk_device, v->mount_point, v->fs_type, |
Doug Zongker | 469243e | 2011-04-12 09:28:10 -0700 | [diff] [blame] | 117 | MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 118 | if (result == 0) return 0; |
| 119 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 120 | LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno)); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | 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] | 125 | return -1; |
| 126 | } |
| 127 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 128 | int ensure_path_unmounted(const char* path) { |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 129 | if (PartitionManager.UnMount_By_Path(path, true)) |
| 130 | return 0; |
| 131 | else |
| 132 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 133 | Volume* v = volume_for_path(path); |
| 134 | if (v == NULL) { |
| 135 | LOGE("unknown volume for path [%s]\n", path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 136 | return -1; |
| 137 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 138 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 139 | // the ramdisk is always mounted; you can't unmount it. |
| 140 | return -1; |
| 141 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 142 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 143 | int result; |
| 144 | result = scan_mounted_volumes(); |
| 145 | if (result < 0) { |
| 146 | LOGE("failed to scan mounted volumes\n"); |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | const MountedVolume* mv = |
| 151 | find_mounted_volume_by_mount_point(v->mount_point); |
| 152 | if (mv == NULL) { |
| 153 | // volume is already unmounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 157 | return unmount_mounted_volume(mv); |
| 158 | } |
| 159 | |
| 160 | int format_volume(const char* volume) { |
Dees_Troy | c51f1f9 | 2012-09-20 15:32:13 -0400 | [diff] [blame] | 161 | if (PartitionManager.Wipe_By_Path(volume)) |
| 162 | return 0; |
| 163 | else |
| 164 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 165 | Volume* v = volume_for_path(volume); |
| 166 | if (v == NULL) { |
| 167 | LOGE("unknown volume \"%s\"\n", volume); |
| 168 | return -1; |
| 169 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 170 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 171 | // you can't format the ramdisk. |
| 172 | LOGE("can't format_volume \"%s\"", volume); |
| 173 | return -1; |
| 174 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 175 | if (strcmp(v->mount_point, volume) != 0) { |
| 176 | LOGE("can't give path \"%s\" to format_volume\n", volume); |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | if (ensure_path_unmounted(volume) != 0) { |
| 181 | LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point); |
| 182 | return -1; |
| 183 | } |
| 184 | |
| 185 | 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] | 186 | mtd_scan_partitions(); |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 187 | const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 188 | if (partition == NULL) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 189 | LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 190 | return -1; |
| 191 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 192 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 193 | MtdWriteContext *write = mtd_write_partition(partition); |
| 194 | if (write == NULL) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 195 | LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 196 | return -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 197 | } else if (mtd_erase_blocks(write, -1) == (off_t) -1) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 198 | LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 199 | mtd_write_close(write); |
| 200 | return -1; |
| 201 | } else if (mtd_write_close(write)) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 202 | LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 203 | return -1; |
| 204 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 208 | if (strcmp(v->fs_type, "ext4") == 0) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 209 | int result = make_ext4fs(v->blk_device, v->length, volume, sehandle); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 210 | if (result != 0) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 211 | LOGE("format_volume: make_extf4fs failed on %s\n", v->blk_device); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 212 | return -1; |
| 213 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 214 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 215 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 216 | |
| 217 | 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] | 218 | return -1; |
| 219 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 220 | |
| 221 | int setup_install_mounts() { |
| 222 | if (fstab == NULL) { |
| 223 | LOGE("can't set up install mounts: no fstab loaded\n"); |
| 224 | return -1; |
| 225 | } |
| 226 | for (int i = 0; i < fstab->num_entries; ++i) { |
| 227 | Volume* v = fstab->recs + i; |
| 228 | |
| 229 | if (strcmp(v->mount_point, "/tmp") == 0 || |
| 230 | strcmp(v->mount_point, "/cache") == 0) { |
| 231 | if (ensure_path_mounted(v->mount_point) != 0) return -1; |
| 232 | |
| 233 | } else { |
| 234 | if (ensure_path_unmounted(v->mount_point) != 0) return -1; |
| 235 | } |
| 236 | } |
| 237 | return 0; |
| 238 | } |