San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 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 <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 Bohm | 092aa1c | 2011-04-01 12:35:25 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 25 | #include <poll.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 26 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 27 | #include <sys/select.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/un.h> |
| 31 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 32 | #include "android/os/IVold.h" |
| 33 | |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 34 | #include <android-base/logging.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 36 | #include <binder/IServiceManager.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 37 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
| 39 | |
| 40 | static void usage(char *progname); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 41 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 42 | int main(int argc, char **argv) { |
| 43 | int sock; |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 44 | int wait; |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 45 | char *progname; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 46 | |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 47 | progname = argv[0]; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 48 | |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 49 | 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 Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 56 | wait = argc > 1 && strcmp(argv[1], "--wait") == 0; |
| 57 | if (wait) { |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 58 | argv++; |
| 59 | argc--; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 62 | if (argc < 3) { |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 63 | usage(progname); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 64 | exit(5); |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 65 | } |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 66 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 67 | 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 Sharkey | 57b1874 | 2017-09-18 13:49:51 -0600 | [diff] [blame] | 87 | } else if (arg1 == "cryptfs" && arg2 == "mountdefaultencrypted") { |
| 88 | exit(vold->mountDefaultEncrypted().isOk() ? 0 : ENOTTY); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 89 | } 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 Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 95 | } |
| 96 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 97 | static void usage(char *progname) { |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 98 | LOG(INFO) << "Usage: " << progname << " [--wait] <monitor>|<cmd> [arg1] [arg2...]"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 99 | } |