| 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> | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 37 | #include <binder/Status.h> | 
| Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 38 |  | 
| San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 39 | #include <private/android_filesystem_config.h> | 
 | 40 |  | 
 | 41 | static void usage(char *progname); | 
| Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 42 |  | 
| Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 43 | static android::sp<android::IBinder> getServiceAggressive() { | 
 | 44 |     android::sp<android::IBinder> res; | 
 | 45 |     auto sm = android::defaultServiceManager(); | 
 | 46 |     auto name = android::String16("vold"); | 
 | 47 |     for (int i = 0; i < 500; i++) { | 
 | 48 |         res = sm->checkService(name); | 
 | 49 |         if (res) { | 
 | 50 |             LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold"; | 
 | 51 |             break; | 
 | 52 |         } | 
 | 53 |         usleep(10000); // 10ms | 
 | 54 |     } | 
 | 55 |     return res; | 
 | 56 | } | 
 | 57 |  | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 58 | static void checkStatus(android::binder::Status status) { | 
 | 59 |     if (status.isOk()) return; | 
 | 60 |     LOG(ERROR) << "Failed: " << status.toString8().string(); | 
 | 61 |     exit(ENOTTY); | 
 | 62 | } | 
| San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 63 |  | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 64 | int main(int argc, char** argv) { | 
| Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 65 |     setenv("ANDROID_LOG_TAGS", "*:v", 1); | 
| Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 66 |     if (getppid() == 1) { | 
 | 67 |         // If init is calling us then it's during boot and we should log to kmsg | 
 | 68 |         android::base::InitLogging(argv, &android::base::KernelLogger); | 
 | 69 |     } else { | 
 | 70 |         android::base::InitLogging(argv, &android::base::StderrLogger); | 
 | 71 |     } | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 72 |     std::vector<std::string> args(argv + 1, argv + argc); | 
| Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 73 |  | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 74 |     if (args.size() > 0 && args[0] == "--wait") { | 
 | 75 |         // Just ignore the --wait flag | 
 | 76 |         args.erase(args.begin()); | 
| San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 77 |     } | 
 | 78 |  | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 79 |     if (args.size() < 2) { | 
 | 80 |         usage(argv[0]); | 
| Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 81 |         exit(5); | 
| Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 82 |     } | 
| Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 83 |     android::sp<android::IBinder> binder = getServiceAggressive(); | 
| Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 84 |     if (!binder) { | 
 | 85 |         LOG(ERROR) << "Failed to obtain vold Binder"; | 
 | 86 |         exit(EINVAL); | 
 | 87 |     } | 
 | 88 |     auto vold = android::interface_cast<android::os::IVold>(binder); | 
 | 89 |  | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 90 |     if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") { | 
 | 91 |         checkStatus(vold->fbeEnable()); | 
 | 92 |     } else if (args[0] == "cryptfs" && args[1] == "init_user0") { | 
 | 93 |         checkStatus(vold->initUser0()); | 
 | 94 |     } else if (args[0] == "cryptfs" && args[1] == "enablecrypto") { | 
| Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 95 |         int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT; | 
| Paul Lawrence | 7ee87cf | 2017-12-22 10:12:06 -0800 | [diff] [blame] | 96 |         int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI; | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 97 |         checkStatus(vold->fdeEnable(passwordType, "", encryptionFlags)); | 
 | 98 |     } else if (args[0] == "cryptfs" && args[1] == "mountdefaultencrypted") { | 
 | 99 |         checkStatus(vold->mountDefaultEncrypted()); | 
 | 100 |     } else if (args[0] == "volume" && args[1] == "shutdown") { | 
 | 101 |         checkStatus(vold->shutdown()); | 
| Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 102 |     } else if (args[0] == "cryptfs" && args[1] == "checkEncryption" && args.size() == 3) { | 
 | 103 |         checkStatus(vold->checkEncryption(args[2])); | 
| Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 104 |     } else { | 
 | 105 |         LOG(ERROR) << "Raw commands are no longer supported"; | 
 | 106 |         exit(EINVAL); | 
 | 107 |     } | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 108 |     return 0; | 
| San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 109 | } | 
 | 110 |  | 
| San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 111 | static void usage(char *progname) { | 
| Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 112 |     LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]"; | 
| Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 113 | } |