blob: 2d35c207587915ea17041b8a258e75e6d6858fed [file] [log] [blame]
Tom Marshalldd9b45d2019-01-04 14:37:31 -08001/*
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
Alessandro675df2d2020-04-06 23:55:07 +020028#include <iostream>
Tom Marshalldd9b45d2019-01-04 14:37:31 -080029#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 Marshall0e6de072019-07-24 21:12:07 +020036#include <unistd.h>
Tom Marshalldd9b45d2019-01-04 14:37:31 -080037
38using android::base::StringPrintf;
39
40namespace android {
41namespace volmgr {
42
43static const std::string kStagingPath = "/mnt/staging/emulated";
Tom Marshall0e6de072019-07-24 21:12:07 +020044static const std::string kFbeKeyVersion = kStagingPath + "/unencrypted/key/version";
Tom Marshalldd9b45d2019-01-04 14:37:31 -080045
46EmulatedVolume::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
58EmulatedVolume::~EmulatedVolume() {}
59
60status_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) {
Alessandro675df2d2020-04-06 23:55:07 +020074 // 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 Marshalldd9b45d2019-01-04 14:37:31 -080077 return -EIO;
78 }
79 if (BindMount(bindPath, getPath()) != OK) {
Alessandro675df2d2020-04-06 23:55:07 +020080 PLOG(WARNING) << getId() << " failed to bind mount " << bindPath << " on " << getPath();
Tom Marshalldd9b45d2019-01-04 14:37:31 -080081 ForceUnmount(kStagingPath);
82 return -EIO;
83 }
84
85 return OK;
86}
87
88status_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 Marshall0e6de072019-07-24 21:12:07 +020098bool 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 Marshalldd9b45d2019-01-04 14:37:31 -0800108} // namespace volmgr
109} // namespace android