blob: f49d6b85814cc795fe6d01bd4b3afc2afb6d7a62 [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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
San Mehatf1b736b2009-10-10 17:22:08 -070017#include <errno.h>
18#include <fcntl.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070019#include <poll.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070020#include <signal.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
San Mehatf1b736b2009-10-10 17:22:08 -070025
San Mehatf1b736b2009-10-10 17:22:08 -070026#include <sys/select.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <sys/un.h>
30
Jeff Sharkey99f92682017-09-13 18:43:44 -060031#include "android/os/IVold.h"
32
Tom Cherryf71511a2017-03-29 16:50:28 -070033#include <android-base/logging.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070034#include <android-base/stringprintf.h>
Jeff Sharkey99f92682017-09-13 18:43:44 -060035#include <binder/IServiceManager.h>
Paul Crowley2d64b912017-10-27 13:37:24 -070036#include <binder/Status.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070037
San Mehatf1b736b2009-10-10 17:22:08 -070038#include <private/android_filesystem_config.h>
39
Paul Crowley14c8c072018-09-18 13:30:21 -070040static void usage(char* progname);
Jeff Sharkey99f92682017-09-13 18:43:44 -060041
Paul Crowley3c3e3602017-09-27 16:44:33 +000042static android::sp<android::IBinder> getServiceAggressive() {
43 android::sp<android::IBinder> res;
44 auto sm = android::defaultServiceManager();
45 auto name = android::String16("vold");
Paul Crowley0fd26262018-01-30 09:48:19 -080046 for (int i = 0; i < 5000; i++) {
Paul Crowley3c3e3602017-09-27 16:44:33 +000047 res = sm->checkService(name);
48 if (res) {
49 LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold";
50 break;
51 }
Paul Crowley14c8c072018-09-18 13:30:21 -070052 usleep(10000); // 10ms
Paul Crowley3c3e3602017-09-27 16:44:33 +000053 }
54 return res;
55}
56
Paul Crowley2d64b912017-10-27 13:37:24 -070057static void checkStatus(android::binder::Status status) {
58 if (status.isOk()) return;
59 LOG(ERROR) << "Failed: " << status.toString8().string();
60 exit(ENOTTY);
61}
San Mehatf1b736b2009-10-10 17:22:08 -070062
Paul Crowley2d64b912017-10-27 13:37:24 -070063int main(int argc, char** argv) {
Paul Crowley3c3e3602017-09-27 16:44:33 +000064 setenv("ANDROID_LOG_TAGS", "*:v", 1);
Tom Cherryf71511a2017-03-29 16:50:28 -070065 if (getppid() == 1) {
66 // If init is calling us then it's during boot and we should log to kmsg
67 android::base::InitLogging(argv, &android::base::KernelLogger);
68 } else {
69 android::base::InitLogging(argv, &android::base::StderrLogger);
70 }
Paul Crowley2d64b912017-10-27 13:37:24 -070071 std::vector<std::string> args(argv + 1, argv + argc);
Tom Cherryf71511a2017-03-29 16:50:28 -070072
Paul Crowley2d64b912017-10-27 13:37:24 -070073 if (args.size() > 0 && args[0] == "--wait") {
74 // Just ignore the --wait flag
75 args.erase(args.begin());
San Mehatf1b736b2009-10-10 17:22:08 -070076 }
77
Paul Crowley2d64b912017-10-27 13:37:24 -070078 if (args.size() < 2) {
79 usage(argv[0]);
Paul Lawrencef4faa572014-01-29 13:31:03 -080080 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070081 }
Paul Crowley3c3e3602017-09-27 16:44:33 +000082 android::sp<android::IBinder> binder = getServiceAggressive();
Jeff Sharkey99f92682017-09-13 18:43:44 -060083 if (!binder) {
84 LOG(ERROR) << "Failed to obtain vold Binder";
85 exit(EINVAL);
86 }
87 auto vold = android::interface_cast<android::os::IVold>(binder);
88
Paul Crowley2d64b912017-10-27 13:37:24 -070089 if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") {
90 checkStatus(vold->fbeEnable());
91 } else if (args[0] == "cryptfs" && args[1] == "init_user0") {
92 checkStatus(vold->initUser0());
93 } else if (args[0] == "cryptfs" && args[1] == "enablecrypto") {
Jeff Sharkey99f92682017-09-13 18:43:44 -060094 int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
Paul Lawrence7ee87cf2017-12-22 10:12:06 -080095 int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI;
Paul Crowley2d64b912017-10-27 13:37:24 -070096 checkStatus(vold->fdeEnable(passwordType, "", encryptionFlags));
97 } else if (args[0] == "cryptfs" && args[1] == "mountdefaultencrypted") {
98 checkStatus(vold->mountDefaultEncrypted());
99 } else if (args[0] == "volume" && args[1] == "shutdown") {
100 checkStatus(vold->shutdown());
Jeff Sharkey2048a282017-06-15 09:59:43 -0600101 } else if (args[0] == "cryptfs" && args[1] == "checkEncryption" && args.size() == 3) {
102 checkStatus(vold->checkEncryption(args[2]));
Paul Crowley0fd26262018-01-30 09:48:19 -0800103 } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 3) {
104 checkStatus(vold->mountFstab(args[2]));
105 } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 3) {
106 checkStatus(vold->encryptFstab(args[2]));
Jeff Sharkey99f92682017-09-13 18:43:44 -0600107 } else {
108 LOG(ERROR) << "Raw commands are no longer supported";
109 exit(EINVAL);
110 }
Paul Crowley2d64b912017-10-27 13:37:24 -0700111 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700112}
113
Paul Crowley14c8c072018-09-18 13:30:21 -0700114static void usage(char* progname) {
Paul Crowley2d64b912017-10-27 13:37:24 -0700115 LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700116}