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