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 | |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 17 | #include "roots.h" |
| 18 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
| 20 | #include <fcntl.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <sys/mount.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 25 | #include <sys/wait.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 32 | #include <android-base/logging.h> |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 33 | #include <android-base/properties.h> |
| 34 | #include <android-base/stringprintf.h> |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 35 | #include <android-base/unique_fd.h> |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 36 | #include <cryptfs.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 37 | #include <ext4_utils/wipe.h> |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 38 | #include <fs_mgr.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 39 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 40 | #include "common.h" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 41 | #include "mounts.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 42 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 43 | static struct fstab* fstab = nullptr; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 44 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 45 | extern struct selabel_handle* sehandle; |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 46 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 47 | void load_volume_table() { |
| 48 | fstab = fs_mgr_read_fstab_default(); |
| 49 | if (!fstab) { |
| 50 | LOG(ERROR) << "Failed to read default fstab"; |
| 51 | return; |
| 52 | } |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 53 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 54 | int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk"); |
| 55 | if (ret == -1) { |
| 56 | LOG(ERROR) << "Failed to add /tmp entry to fstab"; |
| 57 | fs_mgr_free_fstab(fstab); |
| 58 | fstab = nullptr; |
| 59 | return; |
| 60 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 61 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 62 | printf("recovery filesystem table\n"); |
| 63 | printf("=========================\n"); |
| 64 | for (int i = 0; i < fstab->num_entries; ++i) { |
| 65 | const Volume* v = &fstab->recs[i]; |
| 66 | printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, v->blk_device, v->length); |
| 67 | } |
| 68 | printf("\n"); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | Volume* volume_for_path(const char* path) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 72 | 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] | 73 | } |
| 74 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 75 | // Mount the volume specified by path at the given mount_point. |
| 76 | int ensure_path_mounted_at(const char* path, const char* mount_point) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 77 | Volume* v = volume_for_path(path); |
| 78 | if (v == nullptr) { |
| 79 | LOG(ERROR) << "unknown volume for path [" << path << "]"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 80 | return -1; |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 81 | } |
| 82 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 83 | // The ramdisk is always mounted. |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | if (!scan_mounted_volumes()) { |
| 88 | LOG(ERROR) << "Failed to scan mounted volumes"; |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | if (!mount_point) { |
| 93 | mount_point = v->mount_point; |
| 94 | } |
| 95 | |
| 96 | const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point); |
| 97 | if (mv != nullptr) { |
| 98 | // Volume is already mounted. |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | mkdir(mount_point, 0755); // in case it doesn't already exist |
| 103 | |
| 104 | if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 || |
| 105 | strcmp(v->fs_type, "vfat") == 0) { |
| 106 | int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options); |
| 107 | if (result == -1 && fs_mgr_is_formattable(v)) { |
| 108 | PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting"; |
| 109 | bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer"); |
| 110 | if (fs_mgr_do_format(v, crypt_footer) == 0) { |
| 111 | result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options); |
| 112 | } else { |
| 113 | PLOG(ERROR) << "Failed to format " << mount_point; |
| 114 | return -1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (result == -1) { |
| 119 | PLOG(ERROR) << "Failed to mount " << mount_point; |
| 120 | return -1; |
| 121 | } |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point; |
| 126 | return -1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 129 | int ensure_path_mounted(const char* path) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 130 | // Mount at the default mount point. |
| 131 | return ensure_path_mounted_at(path, nullptr); |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 134 | int ensure_path_unmounted(const char* path) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 135 | const Volume* v = volume_for_path(path); |
| 136 | if (v == nullptr) { |
| 137 | LOG(ERROR) << "unknown volume for path [" << path << "]"; |
| 138 | return -1; |
| 139 | } |
| 140 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 141 | // The ramdisk is always mounted; you can't unmount it. |
| 142 | return -1; |
| 143 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 144 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 145 | if (!scan_mounted_volumes()) { |
| 146 | LOG(ERROR) << "Failed to scan mounted volumes"; |
| 147 | return -1; |
| 148 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 149 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 150 | MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point); |
| 151 | if (mv == nullptr) { |
| 152 | // Volume is already unmounted. |
| 153 | return 0; |
| 154 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 155 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 156 | return unmount_mounted_volume(mv); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 159 | static int exec_cmd(const std::vector<std::string>& args) { |
| 160 | CHECK_NE(static_cast<size_t>(0), args.size()); |
| 161 | |
| 162 | std::vector<char*> argv(args.size()); |
| 163 | std::transform(args.cbegin(), args.cend(), argv.begin(), |
| 164 | [](const std::string& arg) { return const_cast<char*>(arg.c_str()); }); |
| 165 | argv.push_back(nullptr); |
| 166 | |
| 167 | pid_t child; |
| 168 | if ((child = vfork()) == 0) { |
| 169 | execv(argv[0], argv.data()); |
| 170 | _exit(EXIT_FAILURE); |
| 171 | } |
| 172 | |
| 173 | int status; |
| 174 | waitpid(child, &status, 0); |
| 175 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 176 | LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status); |
| 177 | } |
| 178 | return WEXITSTATUS(status); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 181 | static ssize_t get_file_size(int fd, uint64_t reserve_len) { |
| 182 | struct stat buf; |
| 183 | int ret = fstat(fd, &buf); |
| 184 | if (ret) return 0; |
| 185 | |
| 186 | ssize_t computed_size; |
| 187 | if (S_ISREG(buf.st_mode)) { |
| 188 | computed_size = buf.st_size - reserve_len; |
| 189 | } else if (S_ISBLK(buf.st_mode)) { |
| 190 | computed_size = get_block_device_size(fd) - reserve_len; |
| 191 | } else { |
| 192 | computed_size = 0; |
| 193 | } |
| 194 | |
| 195 | return computed_size; |
| 196 | } |
| 197 | |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 198 | int format_volume(const char* volume, const char* directory) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 199 | const Volume* v = volume_for_path(volume); |
| 200 | if (v == nullptr) { |
| 201 | LOG(ERROR) << "unknown volume \"" << volume << "\""; |
| 202 | return -1; |
| 203 | } |
| 204 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 205 | LOG(ERROR) << "can't format_volume \"" << volume << "\""; |
| 206 | return -1; |
| 207 | } |
| 208 | if (strcmp(v->mount_point, volume) != 0) { |
| 209 | LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume"; |
| 210 | return -1; |
| 211 | } |
| 212 | if (ensure_path_unmounted(volume) != 0) { |
| 213 | LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\""; |
| 214 | return -1; |
| 215 | } |
| 216 | if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 217 | LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 218 | return -1; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // If there's a key_loc that looks like a path, it should be a block device for storing encryption |
| 222 | // metadata. Wipe it too. |
| 223 | if (v->key_loc != nullptr && v->key_loc[0] == '/') { |
| 224 | LOG(INFO) << "Wiping " << v->key_loc; |
| 225 | int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644); |
| 226 | if (fd == -1) { |
| 227 | PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc; |
| 228 | return -1; |
| 229 | } |
| 230 | wipe_block_device(fd, get_file_size(fd)); |
| 231 | close(fd); |
| 232 | } |
| 233 | |
| 234 | ssize_t length = 0; |
| 235 | if (v->length != 0) { |
| 236 | length = v->length; |
| 237 | } else if (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0) { |
| 238 | android::base::unique_fd fd(open(v->blk_device, O_RDONLY)); |
| 239 | if (fd == -1) { |
| 240 | PLOG(ERROR) << "get_file_size: failed to open " << v->blk_device; |
| 241 | return -1; |
| 242 | } |
| 243 | length = get_file_size(fd.get(), CRYPT_FOOTER_OFFSET); |
| 244 | if (length <= 0) { |
| 245 | LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device; |
| 246 | return -1; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if (strcmp(v->fs_type, "ext4") == 0) { |
| 251 | static constexpr int kBlockSize = 4096; |
| 252 | std::vector<std::string> mke2fs_args = { |
| 253 | "/sbin/mke2fs_static", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize), |
| 254 | }; |
| 255 | |
| 256 | int raid_stride = v->logical_blk_size / kBlockSize; |
| 257 | int raid_stripe_width = v->erase_blk_size / kBlockSize; |
| 258 | // stride should be the max of 8KB and logical block size |
| 259 | if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) { |
| 260 | raid_stride = 8192 / kBlockSize; |
| 261 | } |
| 262 | if (v->erase_blk_size != 0 && v->logical_blk_size != 0) { |
| 263 | mke2fs_args.push_back("-E"); |
| 264 | mke2fs_args.push_back( |
| 265 | android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width)); |
| 266 | } |
| 267 | mke2fs_args.push_back(v->blk_device); |
| 268 | if (length != 0) { |
| 269 | mke2fs_args.push_back(std::to_string(length / kBlockSize)); |
| 270 | } |
| 271 | |
| 272 | int result = exec_cmd(mke2fs_args); |
| 273 | if (result == 0 && directory != nullptr) { |
| 274 | std::vector<std::string> e2fsdroid_args = { |
| 275 | "/sbin/e2fsdroid_static", |
| 276 | "-e", |
| 277 | "-f", |
| 278 | directory, |
| 279 | "-a", |
| 280 | volume, |
| 281 | v->blk_device, |
| 282 | }; |
| 283 | result = exec_cmd(e2fsdroid_args); |
| 284 | } |
| 285 | |
| 286 | if (result != 0) { |
| 287 | PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device; |
| 288 | return -1; |
| 289 | } |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | // Has to be f2fs because we checked earlier. |
| 294 | std::vector<std::string> f2fs_args = { "/sbin/mkfs.f2fs", "-t", "-d1", v->blk_device }; |
| 295 | if (length >= 512) { |
| 296 | f2fs_args.push_back(std::to_string(length / 512)); |
| 297 | } |
| 298 | |
| 299 | int result = exec_cmd(f2fs_args); |
| 300 | if (result != 0) { |
| 301 | PLOG(ERROR) << "format_volume: Failed to make f2fs on " << v->blk_device; |
| 302 | return -1; |
| 303 | } |
| 304 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 305 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 306 | |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 307 | int format_volume(const char* volume) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 308 | return format_volume(volume, nullptr); |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 311 | int setup_install_mounts() { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 312 | if (fstab == nullptr) { |
| 313 | LOG(ERROR) << "can't set up install mounts: no fstab loaded"; |
| 314 | return -1; |
| 315 | } |
| 316 | for (int i = 0; i < fstab->num_entries; ++i) { |
| 317 | const Volume* v = fstab->recs + i; |
| 318 | |
| 319 | // We don't want to do anything with "/". |
| 320 | if (strcmp(v->mount_point, "/") == 0) { |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) { |
| 325 | if (ensure_path_mounted(v->mount_point) != 0) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 326 | LOG(ERROR) << "Failed to mount " << v->mount_point; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 327 | return -1; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 328 | } |
| 329 | } else { |
| 330 | if (ensure_path_unmounted(v->mount_point) != 0) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 331 | LOG(ERROR) << "Failed to unmount " << v->mount_point; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 332 | return -1; |
| 333 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 334 | } |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 335 | } |
| 336 | return 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 337 | } |