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