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 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 19 | #include <poll.h> |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 20 | #include <signal.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 25 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 26 | #include <sys/select.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/un.h> |
| 30 | |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 31 | #include "android/os/IVold.h" |
| 32 | |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 35 | #include <binder/IServiceManager.h> |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 36 | #include <binder/Status.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 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 40 | static void usage(char* progname); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 41 | |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 42 | static android::sp<android::IBinder> getServiceAggressive() { |
| 43 | android::sp<android::IBinder> res; |
| 44 | auto sm = android::defaultServiceManager(); |
| 45 | auto name = android::String16("vold"); |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 46 | for (int i = 0; i < 5000; i++) { |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 47 | res = sm->checkService(name); |
| 48 | if (res) { |
| 49 | LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold"; |
| 50 | break; |
| 51 | } |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 52 | usleep(10000); // 10ms |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 53 | } |
| 54 | return res; |
| 55 | } |
| 56 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 57 | static void checkStatus(android::binder::Status status) { |
| 58 | if (status.isOk()) return; |
| 59 | LOG(ERROR) << "Failed: " << status.toString8().string(); |
| 60 | exit(ENOTTY); |
| 61 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 62 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 63 | int main(int argc, char** argv) { |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 64 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 65 | 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 Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 71 | std::vector<std::string> args(argv + 1, argv + argc); |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 72 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 73 | if (args.size() > 0 && args[0] == "--wait") { |
| 74 | // Just ignore the --wait flag |
| 75 | args.erase(args.begin()); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 78 | if (args.size() < 2) { |
| 79 | usage(argv[0]); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 80 | exit(5); |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 81 | } |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 82 | android::sp<android::IBinder> binder = getServiceAggressive(); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 83 | 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 Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 89 | 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 Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 94 | int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT; |
Paul Lawrence | 7ee87cf | 2017-12-22 10:12:06 -0800 | [diff] [blame] | 95 | int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI; |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 96 | 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 Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 101 | } else if (args[0] == "cryptfs" && args[1] == "checkEncryption" && args.size() == 3) { |
| 102 | checkStatus(vold->checkEncryption(args[2])); |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 103 | } 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 Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 107 | } else { |
| 108 | LOG(ERROR) << "Raw commands are no longer supported"; |
| 109 | exit(EINVAL); |
| 110 | } |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 111 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 114 | static void usage(char* progname) { |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 115 | LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 116 | } |