1 /*
2 * Copyright (C) 2008 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 <fcntl.h>
19 #include <poll.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <sys/select.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <sys/un.h>
30
31 #include "android/os/IVold.h"
32
33 #include <android-base/logging.h>
34 #include <android-base/parseint.h>
35 #include <android-base/strings.h>
36 #include <android-base/stringprintf.h>
37 #include <binder/IServiceManager.h>
38 #include <binder/Status.h>
39
40 #include <private/android_filesystem_config.h>
41
42 static void usage(char* progname);
43
getServiceAggressive()44 static android::sp<android::IBinder> getServiceAggressive() {
45 android::sp<android::IBinder> res;
46 auto sm = android::defaultServiceManager();
47 auto name = android::String16("vold");
48 for (int i = 0; i < 5000; i++) {
49 res = sm->checkService(name);
50 if (res) {
51 LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold";
52 break;
53 }
54 usleep(10000); // 10ms
55 }
56 return res;
57 }
58
checkStatus(std::vector<std::string> & cmd,android::binder::Status status)59 static void checkStatus(std::vector<std::string>& cmd, android::binder::Status status) {
60 if (status.isOk()) return;
61 std::string command = ::android::base::Join(cmd, " ");
62 LOG(ERROR) << "Command: " << command << " Failed: " << status.toString8().string();
63 exit(ENOTTY);
64 }
65
main(int argc,char ** argv)66 int main(int argc, char** argv) {
67 setenv("ANDROID_LOG_TAGS", "*:v", 1);
68 if (getppid() == 1) {
69 // If init is calling us then it's during boot and we should log to kmsg
70 android::base::InitLogging(argv, &android::base::KernelLogger);
71 } else {
72 android::base::InitLogging(argv, &android::base::StderrLogger);
73 }
74 std::vector<std::string> args(argv + 1, argv + argc);
75
76 if (args.size() > 0 && args[0] == "--wait") {
77 // Just ignore the --wait flag
78 args.erase(args.begin());
79 }
80
81 if (args.size() < 2) {
82 usage(argv[0]);
83 exit(5);
84 }
85 android::sp<android::IBinder> binder = getServiceAggressive();
86 if (!binder) {
87 LOG(ERROR) << "Failed to obtain vold Binder";
88 exit(EINVAL);
89 }
90 auto vold = android::interface_cast<android::os::IVold>(binder);
91
92 if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") {
93 checkStatus(args, vold->fbeEnable());
94 } else if (args[0] == "cryptfs" && args[1] == "init_user0") {
95 checkStatus(args, vold->initUser0());
96 } else if (args[0] == "cryptfs" && args[1] == "enablecrypto") {
97 int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
98 int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI;
99 checkStatus(args, vold->fdeEnable(passwordType, "", encryptionFlags));
100 } else if (args[0] == "cryptfs" && args[1] == "mountdefaultencrypted") {
101 checkStatus(args, vold->mountDefaultEncrypted());
102 } else if (args[0] == "volume" && args[1] == "abort_fuse") {
103 checkStatus(args, vold->abortFuse());
104 } else if (args[0] == "volume" && args[1] == "shutdown") {
105 checkStatus(args, vold->shutdown());
106 } else if (args[0] == "volume" && args[1] == "reset") {
107 checkStatus(args, vold->reset());
108 } else if (args[0] == "cryptfs" && args[1] == "checkEncryption" && args.size() == 3) {
109 checkStatus(args, vold->checkEncryption(args[2]));
110 } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 4) {
111 checkStatus(args, vold->mountFstab(args[2], args[3]));
112 } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 4) {
113 checkStatus(args, vold->encryptFstab(args[2], args[3]));
114 } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
115 bool supported = false;
116 checkStatus(args, vold->supportsCheckpoint(&supported));
117 return supported ? 1 : 0;
118 } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" &&
119 args.size() == 2) {
120 bool supported = false;
121 checkStatus(args, vold->supportsBlockCheckpoint(&supported));
122 return supported ? 1 : 0;
123 } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) {
124 bool supported = false;
125 checkStatus(args, vold->supportsFileCheckpoint(&supported));
126 return supported ? 1 : 0;
127 } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) {
128 int retry;
129 if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
130 checkStatus(args, vold->startCheckpoint(retry));
131 } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) {
132 bool enabled = false;
133 checkStatus(args, vold->needsCheckpoint(&enabled));
134 return enabled ? 1 : 0;
135 } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) {
136 bool enabled = false;
137 checkStatus(args, vold->needsRollback(&enabled));
138 return enabled ? 1 : 0;
139 } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) {
140 checkStatus(args, vold->commitChanges());
141 } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) {
142 checkStatus(args, vold->prepareCheckpoint());
143 } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) {
144 checkStatus(args, vold->restoreCheckpoint(args[2]));
145 } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) {
146 int count;
147 if (!android::base::ParseInt(args[3], &count)) exit(EINVAL);
148 checkStatus(args, vold->restoreCheckpointPart(args[2], count));
149 } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) {
150 checkStatus(args, vold->markBootAttempt());
151 } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) {
152 int retry;
153 if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
154 checkStatus(args, vold->abortChanges(args[2], retry != 0));
155 } else if (args[0] == "checkpoint" && args[1] == "resetCheckpoint") {
156 checkStatus(args, vold->resetCheckpoint());
157 } else {
158 LOG(ERROR) << "Raw commands are no longer supported";
159 exit(EINVAL);
160 }
161 return 0;
162 }
163
usage(char * progname)164 static void usage(char* progname) {
165 LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]";
166 }
167