Tom Marshall | dd9b45d | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * Copyright (C) 2019 The LineageOS Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "EmulatedVolume.h" |
| 19 | #include <volume_manager/ResponseCode.h> |
| 20 | #include <volume_manager/VolumeManager.h> |
| 21 | #include "Utils.h" |
| 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <cutils/fs.h> |
| 26 | #include <private/android_filesystem_config.h> |
| 27 | |
Alessandro | 675df2d | 2020-04-06 23:55:07 +0200 | [diff] [blame] | 28 | #include <iostream> |
Tom Marshall | dd9b45d | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <sys/mount.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/sysmacros.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <sys/wait.h> |
Tom Marshall | 0e6de07 | 2019-07-24 21:12:07 +0200 | [diff] [blame] | 36 | #include <unistd.h> |
Tom Marshall | dd9b45d | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 37 | |
| 38 | using android::base::StringPrintf; |
| 39 | |
| 40 | namespace android { |
| 41 | namespace volmgr { |
| 42 | |
| 43 | static const std::string kStagingPath = "/mnt/staging/emulated"; |
Tom Marshall | 0e6de07 | 2019-07-24 21:12:07 +0200 | [diff] [blame] | 44 | static const std::string kFbeKeyVersion = kStagingPath + "/unencrypted/key/version"; |
Tom Marshall | dd9b45d | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 45 | |
| 46 | EmulatedVolume::EmulatedVolume(FstabEntry* rec, const std::string& subdir) |
| 47 | : VolumeBase(Type::kEmulated), |
| 48 | mSubdir(subdir), |
| 49 | mDevPath(rec->blk_device), |
| 50 | mFsType(rec->fs_type), |
| 51 | mFlags(rec->flags), |
| 52 | mFsOptions(rec->fs_options) { |
| 53 | setId("emulated"); |
| 54 | setPartLabel("internal storage"); |
| 55 | setPath("/storage/emulated"); |
| 56 | } |
| 57 | |
| 58 | EmulatedVolume::~EmulatedVolume() {} |
| 59 | |
| 60 | status_t EmulatedVolume::doMount() { |
| 61 | if (fs_prepare_dir(kStagingPath.c_str(), 0700, AID_ROOT, AID_ROOT)) { |
| 62 | PLOG(ERROR) << getId() << " failed to create mount points"; |
| 63 | return -errno; |
| 64 | } |
| 65 | if (fs_prepare_dir(getPath().c_str(), 0700, AID_ROOT, AID_ROOT)) { |
| 66 | PLOG(ERROR) << getId() << " failed to create mount points"; |
| 67 | return -errno; |
| 68 | } |
| 69 | |
| 70 | std::string bindPath = kStagingPath + "/" + mSubdir; |
| 71 | |
| 72 | if (::mount(mDevPath.c_str(), kStagingPath.c_str(), mFsType.c_str(), mFlags, |
| 73 | mFsOptions.c_str()) != 0) { |
Alessandro | 675df2d | 2020-04-06 23:55:07 +0200 | [diff] [blame] | 74 | // It's ok to fail mounting if we're encrytped, so avoid printing to recovery's UiLogger |
| 75 | std::cout << getId() << " failed to mount " << mDevPath << " on " << kStagingPath |
| 76 | << ": " << std::strerror(errno); |
Tom Marshall | dd9b45d | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 77 | return -EIO; |
| 78 | } |
| 79 | if (BindMount(bindPath, getPath()) != OK) { |
Alessandro | 675df2d | 2020-04-06 23:55:07 +0200 | [diff] [blame] | 80 | PLOG(WARNING) << getId() << " failed to bind mount " << bindPath << " on " << getPath(); |
Tom Marshall | dd9b45d | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 81 | ForceUnmount(kStagingPath); |
| 82 | return -EIO; |
| 83 | } |
| 84 | |
| 85 | return OK; |
| 86 | } |
| 87 | |
| 88 | status_t EmulatedVolume::doUnmount(bool detach /* = false */) { |
| 89 | ForceUnmount(getPath(), detach); |
| 90 | ForceUnmount(kStagingPath, detach); |
| 91 | |
| 92 | rmdir(getPath().c_str()); |
| 93 | rmdir(kStagingPath.c_str()); |
| 94 | |
| 95 | return OK; |
| 96 | } |
| 97 | |
Tom Marshall | 0e6de07 | 2019-07-24 21:12:07 +0200 | [diff] [blame] | 98 | bool EmulatedVolume::detectMountable() { |
| 99 | bool mountable = false; |
| 100 | if (doMount() == OK) { |
| 101 | // Check if FBE encrypted |
| 102 | mountable = access(kFbeKeyVersion.c_str(), F_OK) != 0; |
| 103 | doUnmount(); |
| 104 | } |
| 105 | return mountable; |
| 106 | } |
| 107 | |
Tom Marshall | dd9b45d | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 108 | } // namespace volmgr |
| 109 | } // namespace android |