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 "Utils.h"
32 #include "android/os/IVold.h"
33 
34 #include <android-base/logging.h>
35 #include <android-base/parsebool.h>
36 #include <android-base/parseint.h>
37 #include <android-base/stringprintf.h>
38 #include <android-base/strings.h>
39 #include <binder/IServiceManager.h>
40 #include <binder/Status.h>
41 #include <utils/Errors.h>
42 
43 #include <private/android_filesystem_config.h>
44 
45 static void usage(char* progname);
46 
getServiceAggressive()47 static android::sp<android::IBinder> getServiceAggressive() {
48     android::sp<android::IBinder> res;
49     auto sm = android::defaultServiceManager();
50     auto name = android::String16("vold");
51     for (int i = 0; i < 5000; i++) {
52         res = sm->checkService(name);
53         if (res) {
54             LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold";
55             break;
56         }
57         usleep(10000);  // 10ms
58     }
59     return res;
60 }
61 
checkStatus(std::vector<std::string> & cmd,android::binder::Status status)62 static void checkStatus(std::vector<std::string>& cmd, android::binder::Status status) {
63     if (status.isOk()) return;
64     std::string command = ::android::base::Join(cmd, " ");
65     LOG(ERROR) << "Command: " << command << " Failed: " << status.toString8().c_str();
66     exit(ENOTTY);
67 }
68 
bindkeys(std::vector<std::string> & args,const android::sp<android::os::IVold> & vold)69 static void bindkeys(std::vector<std::string>& args, const android::sp<android::os::IVold>& vold) {
70     std::string raw_bytes;
71     const char* seed_value;
72 
73     seed_value = getenv("SEED_VALUE");
74     if (seed_value == NULL) {
75         LOG(ERROR) << "Empty seed";
76         exit(EINVAL);
77     }
78 
79     android::status_t status = android::vold::HexToStr(seed_value, raw_bytes);
80     if (status != android::OK) {
81         LOG(ERROR) << "Extraction of seed failed: " << status;
82         exit(status);
83     }
84 
85     std::vector<uint8_t> seed{raw_bytes.begin(), raw_bytes.end()};
86     checkStatus(args, vold->setStorageBindingSeed(seed));
87 }
88 
mountFstab(std::vector<std::string> & args,const android::sp<android::os::IVold> & vold)89 static void mountFstab(std::vector<std::string>& args,
90                        const android::sp<android::os::IVold>& vold) {
91     auto isZoned = android::base::ParseBool(args[4]);
92     if (isZoned == android::base::ParseBoolResult::kError) exit(EINVAL);
93 
94     std::vector<std::string> userDevices = {};
95     if (args[5] != "") {
96         userDevices = android::base::Split(args[5], " ");
97     }
98     checkStatus(args,
99                 vold->mountFstab(args[2], args[3], isZoned == android::base::ParseBoolResult::kTrue,
100                                  userDevices));
101 }
102 
encryptFstab(std::vector<std::string> & args,const android::sp<android::os::IVold> & vold)103 static void encryptFstab(std::vector<std::string>& args,
104                          const android::sp<android::os::IVold>& vold) {
105     auto shouldFormat = android::base::ParseBool(args[4]);
106     if (shouldFormat == android::base::ParseBoolResult::kError) exit(EINVAL);
107 
108     auto isZoned = android::base::ParseBool(args[6]);
109     if (isZoned == android::base::ParseBoolResult::kError) exit(EINVAL);
110 
111     std::vector<std::string> userDevices = {};
112     if (args[7] != "") {
113         userDevices = android::base::Split(args[7], " ");
114     }
115     checkStatus(args,
116                 vold->encryptFstab(args[2], args[3],
117                                    shouldFormat == android::base::ParseBoolResult::kTrue, args[5],
118                                    isZoned == android::base::ParseBoolResult::kTrue, userDevices));
119 }
120 
main(int argc,char ** argv)121 int main(int argc, char** argv) {
122     setenv("ANDROID_LOG_TAGS", "*:v", 1);
123     if (getppid() == 1) {
124         // If init is calling us then it's during boot and we should log to kmsg
125         android::base::InitLogging(argv, &android::base::KernelLogger);
126     } else {
127         android::base::InitLogging(argv, &android::base::StderrLogger);
128     }
129     std::vector<std::string> args(argv + 1, argv + argc);
130 
131     if (args.size() > 0 && args[0] == "--wait") {
132         // Just ignore the --wait flag
133         args.erase(args.begin());
134     }
135 
136     if (args.size() < 2) {
137         usage(argv[0]);
138         exit(5);
139     }
140     android::sp<android::IBinder> binder = getServiceAggressive();
141     if (!binder) {
142         LOG(ERROR) << "Failed to obtain vold Binder";
143         exit(EINVAL);
144     }
145     auto vold = android::interface_cast<android::os::IVold>(binder);
146 
147     if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") {
148         checkStatus(args, vold->fbeEnable());
149     } else if (args[0] == "cryptfs" && args[1] == "init_user0") {
150         checkStatus(args, vold->initUser0());
151     } else if (args[0] == "volume" && args[1] == "abort_fuse") {
152         checkStatus(args, vold->abortFuse());
153     } else if (args[0] == "volume" && args[1] == "shutdown") {
154         checkStatus(args, vold->shutdown());
155     } else if (args[0] == "volume" && args[1] == "reset") {
156         checkStatus(args, vold->reset());
157     } else if (args[0] == "volume" && args[1] == "getStorageSize") {
158         int64_t size;
159         checkStatus(args, vold->getStorageSize(&size));
160         LOG(INFO) << size;
161     } else if (args[0] == "cryptfs" && args[1] == "bindkeys") {
162         bindkeys(args, vold);
163     } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 6) {
164         mountFstab(args, vold);
165     } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 8) {
166         encryptFstab(args, vold);
167     } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
168         bool supported = false;
169         checkStatus(args, vold->supportsCheckpoint(&supported));
170         return supported ? 1 : 0;
171     } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" &&
172                args.size() == 2) {
173         bool supported = false;
174         checkStatus(args, vold->supportsBlockCheckpoint(&supported));
175         return supported ? 1 : 0;
176     } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) {
177         bool supported = false;
178         checkStatus(args, vold->supportsFileCheckpoint(&supported));
179         return supported ? 1 : 0;
180     } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) {
181         int retry;
182         if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
183         checkStatus(args, vold->startCheckpoint(retry));
184     } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) {
185         bool enabled = false;
186         checkStatus(args, vold->needsCheckpoint(&enabled));
187         return enabled ? 1 : 0;
188     } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) {
189         bool enabled = false;
190         checkStatus(args, vold->needsRollback(&enabled));
191         return enabled ? 1 : 0;
192     } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) {
193         checkStatus(args, vold->commitChanges());
194     } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) {
195         checkStatus(args, vold->prepareCheckpoint());
196     } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) {
197         checkStatus(args, vold->restoreCheckpoint(args[2]));
198     } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) {
199         int count;
200         if (!android::base::ParseInt(args[3], &count)) exit(EINVAL);
201         checkStatus(args, vold->restoreCheckpointPart(args[2], count));
202     } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) {
203         checkStatus(args, vold->markBootAttempt());
204     } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) {
205         int retry;
206         if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
207         checkStatus(args, vold->abortChanges(args[2], retry != 0));
208     } else if (args[0] == "checkpoint" && args[1] == "resetCheckpoint") {
209         checkStatus(args, vold->resetCheckpoint());
210     } else if (args[0] == "keymaster" && args[1] == "earlyBootEnded") {
211         checkStatus(args, vold->earlyBootEnded());
212     } else {
213         LOG(ERROR) << "Raw commands are no longer supported";
214         exit(EINVAL);
215     }
216     return 0;
217 }
218 
usage(char * progname)219 static void usage(char* progname) {
220     LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]";
221 }
222