blob: 4eb988d9ce449d16d947fc31f2ad15285ca1da2c [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
San Mehatf1b736b2009-10-10 17:22:08 -070042int main(int argc, char **argv) {
43 int sock;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060044 int wait;
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070045 char *progname;
San Mehatf1b736b2009-10-10 17:22:08 -070046
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070047 progname = argv[0];
San Mehatf1b736b2009-10-10 17:22:08 -070048
Tom Cherryf71511a2017-03-29 16:50:28 -070049 if (getppid() == 1) {
50 // If init is calling us then it's during boot and we should log to kmsg
51 android::base::InitLogging(argv, &android::base::KernelLogger);
52 } else {
53 android::base::InitLogging(argv, &android::base::StderrLogger);
54 }
55
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060056 wait = argc > 1 && strcmp(argv[1], "--wait") == 0;
57 if (wait) {
Paul Lawrencef4faa572014-01-29 13:31:03 -080058 argv++;
59 argc--;
San Mehatf1b736b2009-10-10 17:22:08 -070060 }
61
Jeff Sharkey99f92682017-09-13 18:43:44 -060062 if (argc < 3) {
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070063 usage(progname);
Paul Lawrencef4faa572014-01-29 13:31:03 -080064 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070065 }
Paul Lawrencef4faa572014-01-29 13:31:03 -080066
Jeff Sharkey99f92682017-09-13 18:43:44 -060067 std::string arg1 = argv[1];
68 std::string arg2 = argv[2];
69
70 android::sp<android::IBinder> binder = android::defaultServiceManager()->getService(
71 android::String16("vold"));
72 if (!binder) {
73 LOG(ERROR) << "Failed to obtain vold Binder";
74 exit(EINVAL);
75 }
76 auto vold = android::interface_cast<android::os::IVold>(binder);
77
78 if (arg1 == "cryptfs" && arg2 == "enablefilecrypto") {
79 exit(vold->fbeEnable().isOk() ? 0 : ENOTTY);
80 } else if (arg1 == "cryptfs" && arg2 == "init_user0") {
81 exit(vold->initUser0().isOk() ? 0 : ENOTTY);
82 } else if (arg1 == "cryptfs" && arg2 == "enablecrypto") {
83 int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
84 int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_IN_PLACE
85 | android::os::IVold::ENCRYPTION_FLAG_NO_UI;
86 exit(vold->fdeEnable(passwordType, "", encryptionFlags).isOk() ? 0 : ENOTTY);
Jeff Sharkey57b18742017-09-18 13:49:51 -060087 } else if (arg1 == "cryptfs" && arg2 == "mountdefaultencrypted") {
88 exit(vold->mountDefaultEncrypted().isOk() ? 0 : ENOTTY);
Jeff Sharkey99f92682017-09-13 18:43:44 -060089 } else if (arg1 == "volume" && arg2 == "shutdown") {
90 exit(vold->shutdown().isOk() ? 0 : ENOTTY);
91 } else {
92 LOG(ERROR) << "Raw commands are no longer supported";
93 exit(EINVAL);
94 }
San Mehatf1b736b2009-10-10 17:22:08 -070095}
96
San Mehatf1b736b2009-10-10 17:22:08 -070097static void usage(char *progname) {
Tom Cherryf71511a2017-03-29 16:50:28 -070098 LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]";
Jeff Sharkey47695b22016-02-01 17:02:29 -070099}