blob: 4e0c3a94aabbf9938391ddb9d124a236848bfd27 [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
17#include <stdio.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <string.h>
21#include <signal.h>
22#include <errno.h>
23#include <fcntl.h>
Peter Bohm092aa1c2011-04-01 12:35:25 +020024#include <stdlib.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070025#include <poll.h>
San Mehatf1b736b2009-10-10 17:22:08 -070026
San Mehatf1b736b2009-10-10 17:22:08 -070027#include <sys/select.h>
28#include <sys/time.h>
29#include <sys/types.h>
30#include <sys/un.h>
31
Jeff Sharkey99f92682017-09-13 18:43:44 -060032#include "android/os/IVold.h"
33
Tom Cherryf71511a2017-03-29 16:50:28 -070034#include <android-base/logging.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070035#include <android-base/stringprintf.h>
Jeff Sharkey99f92682017-09-13 18:43:44 -060036#include <binder/IServiceManager.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070037
San Mehatf1b736b2009-10-10 17:22:08 -070038#include <private/android_filesystem_config.h>
39
40static 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");
46 for (int i = 0; i < 500; i++) {
47 res = sm->checkService(name);
48 if (res) {
49 LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold";
50 break;
51 }
52 usleep(10000); // 10ms
53 }
54 return res;
55}
56
San Mehatf1b736b2009-10-10 17:22:08 -070057int main(int argc, char **argv) {
58 int sock;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060059 int wait;
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070060 char *progname;
San Mehatf1b736b2009-10-10 17:22:08 -070061
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070062 progname = argv[0];
San Mehatf1b736b2009-10-10 17:22:08 -070063
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 }
71
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060072 wait = argc > 1 && strcmp(argv[1], "--wait") == 0;
73 if (wait) {
Paul Lawrencef4faa572014-01-29 13:31:03 -080074 argv++;
75 argc--;
San Mehatf1b736b2009-10-10 17:22:08 -070076 }
77
Jeff Sharkey99f92682017-09-13 18:43:44 -060078 if (argc < 3) {
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070079 usage(progname);
Paul Lawrencef4faa572014-01-29 13:31:03 -080080 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070081 }
Paul Lawrencef4faa572014-01-29 13:31:03 -080082
Jeff Sharkey99f92682017-09-13 18:43:44 -060083 std::string arg1 = argv[1];
84 std::string arg2 = argv[2];
85
Paul Crowley3c3e3602017-09-27 16:44:33 +000086 android::sp<android::IBinder> binder = getServiceAggressive();
Jeff Sharkey99f92682017-09-13 18:43:44 -060087 if (!binder) {
88 LOG(ERROR) << "Failed to obtain vold Binder";
89 exit(EINVAL);
90 }
91 auto vold = android::interface_cast<android::os::IVold>(binder);
92
93 if (arg1 == "cryptfs" && arg2 == "enablefilecrypto") {
94 exit(vold->fbeEnable().isOk() ? 0 : ENOTTY);
95 } else if (arg1 == "cryptfs" && arg2 == "init_user0") {
96 exit(vold->initUser0().isOk() ? 0 : ENOTTY);
97 } else if (arg1 == "cryptfs" && arg2 == "enablecrypto") {
98 int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
99 int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_IN_PLACE
100 | android::os::IVold::ENCRYPTION_FLAG_NO_UI;
101 exit(vold->fdeEnable(passwordType, "", encryptionFlags).isOk() ? 0 : ENOTTY);
Jeff Sharkey57b18742017-09-18 13:49:51 -0600102 } else if (arg1 == "cryptfs" && arg2 == "mountdefaultencrypted") {
103 exit(vold->mountDefaultEncrypted().isOk() ? 0 : ENOTTY);
Jeff Sharkey99f92682017-09-13 18:43:44 -0600104 } else if (arg1 == "volume" && arg2 == "shutdown") {
105 exit(vold->shutdown().isOk() ? 0 : ENOTTY);
106 } else {
107 LOG(ERROR) << "Raw commands are no longer supported";
108 exit(EINVAL);
109 }
San Mehatf1b736b2009-10-10 17:22:08 -0700110}
111
San Mehatf1b736b2009-10-10 17:22:08 -0700112static void usage(char *progname) {
Tom Cherryf71511a2017-03-29 16:50:28 -0700113 LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700114}