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> |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 34 | #include <android-base/parseint.h> |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 35 | #include <android-base/strings.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 36 | #include <android-base/stringprintf.h> |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 37 | #include <binder/IServiceManager.h> |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 38 | #include <binder/Status.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 39 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 40 | #include <private/android_filesystem_config.h> |
| 41 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 42 | static void usage(char* progname); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 43 | |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 44 | static android::sp<android::IBinder> getServiceAggressive() { |
| 45 | android::sp<android::IBinder> res; |
| 46 | auto sm = android::defaultServiceManager(); |
| 47 | auto name = android::String16("vold"); |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 48 | for (int i = 0; i < 5000; i++) { |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 49 | res = sm->checkService(name); |
| 50 | if (res) { |
| 51 | LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold"; |
| 52 | break; |
| 53 | } |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 54 | usleep(10000); // 10ms |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 55 | } |
| 56 | return res; |
| 57 | } |
| 58 | |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 59 | static void checkStatus(std::vector<std::string>& cmd, android::binder::Status status) { |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 60 | if (status.isOk()) return; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 61 | std::string command = ::android::base::Join(cmd, " "); |
| 62 | LOG(ERROR) << "Command: " << command << " Failed: " << status.toString8().string(); |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 63 | exit(ENOTTY); |
| 64 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 65 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 66 | int main(int argc, char** argv) { |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 67 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 68 | if (getppid() == 1) { |
| 69 | // If init is calling us then it's during boot and we should log to kmsg |
| 70 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 71 | } else { |
| 72 | android::base::InitLogging(argv, &android::base::StderrLogger); |
| 73 | } |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 74 | std::vector<std::string> args(argv + 1, argv + argc); |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 75 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 76 | if (args.size() > 0 && args[0] == "--wait") { |
| 77 | // Just ignore the --wait flag |
| 78 | args.erase(args.begin()); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 81 | if (args.size() < 2) { |
| 82 | usage(argv[0]); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 83 | exit(5); |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 84 | } |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 85 | android::sp<android::IBinder> binder = getServiceAggressive(); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 86 | if (!binder) { |
| 87 | LOG(ERROR) << "Failed to obtain vold Binder"; |
| 88 | exit(EINVAL); |
| 89 | } |
| 90 | auto vold = android::interface_cast<android::os::IVold>(binder); |
| 91 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 92 | if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 93 | checkStatus(args, vold->fbeEnable()); |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 94 | } else if (args[0] == "cryptfs" && args[1] == "init_user0") { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 95 | checkStatus(args, vold->initUser0()); |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 96 | } else if (args[0] == "cryptfs" && args[1] == "enablecrypto") { |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 97 | int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT; |
Paul Lawrence | 7ee87cf | 2017-12-22 10:12:06 -0800 | [diff] [blame] | 98 | int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 99 | checkStatus(args, vold->fdeEnable(passwordType, "", encryptionFlags)); |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 100 | } else if (args[0] == "cryptfs" && args[1] == "mountdefaultencrypted") { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 101 | checkStatus(args, vold->mountDefaultEncrypted()); |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 102 | } else if (args[0] == "volume" && args[1] == "shutdown") { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 103 | checkStatus(args, vold->shutdown()); |
Jeff Sharkey | 2048a28 | 2017-06-15 09:59:43 -0600 | [diff] [blame] | 104 | } else if (args[0] == "cryptfs" && args[1] == "checkEncryption" && args.size() == 3) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 105 | checkStatus(args, vold->checkEncryption(args[2])); |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 106 | } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 3) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 107 | checkStatus(args, vold->mountFstab(args[2])); |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 108 | } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 3) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 109 | checkStatus(args, vold->encryptFstab(args[2])); |
Daniel Rosenberg | 9b667fb | 2019-01-22 17:27:25 -0800 | [diff] [blame] | 110 | } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) { |
| 111 | bool supported = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 112 | checkStatus(args, vold->supportsCheckpoint(&supported)); |
Daniel Rosenberg | 9b667fb | 2019-01-22 17:27:25 -0800 | [diff] [blame] | 113 | return supported ? 1 : 0; |
Paul Lawrence | c5c79c5 | 2019-03-18 13:36:40 -0700 | [diff] [blame] | 114 | } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" && args.size() == 2) { |
| 115 | bool supported = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 116 | checkStatus(args, vold->supportsBlockCheckpoint(&supported)); |
Paul Lawrence | c5c79c5 | 2019-03-18 13:36:40 -0700 | [diff] [blame] | 117 | return supported ? 1 : 0; |
| 118 | } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) { |
| 119 | bool supported = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 120 | checkStatus(args, vold->supportsFileCheckpoint(&supported)); |
Paul Lawrence | c5c79c5 | 2019-03-18 13:36:40 -0700 | [diff] [blame] | 121 | return supported ? 1 : 0; |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 122 | } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) { |
| 123 | int retry; |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 124 | if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL); |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 125 | checkStatus(args, vold->startCheckpoint(retry)); |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 126 | } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) { |
| 127 | bool enabled = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 128 | checkStatus(args, vold->needsCheckpoint(&enabled)); |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 129 | return enabled ? 1 : 0; |
Daniel Rosenberg | d399249 | 2018-10-02 17:40:44 -0700 | [diff] [blame] | 130 | } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) { |
| 131 | bool enabled = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 132 | checkStatus(args, vold->needsRollback(&enabled)); |
Daniel Rosenberg | d399249 | 2018-10-02 17:40:44 -0700 | [diff] [blame] | 133 | return enabled ? 1 : 0; |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 134 | } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 135 | checkStatus(args, vold->commitChanges()); |
Daniel Rosenberg | 80d1ca5 | 2018-10-09 19:26:57 -0700 | [diff] [blame] | 136 | } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 137 | checkStatus(args, vold->prepareCheckpoint()); |
Paul Lawrence | 1abb2fe | 2018-09-21 10:49:57 -0700 | [diff] [blame] | 138 | } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 139 | checkStatus(args, vold->restoreCheckpoint(args[2])); |
Daniel Rosenberg | dda5981 | 2019-03-06 17:45:17 -0800 | [diff] [blame] | 140 | } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) { |
| 141 | int count; |
| 142 | if (!android::base::ParseInt(args[3], &count)) exit(EINVAL); |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 143 | checkStatus(args, vold->restoreCheckpointPart(args[2], count)); |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 144 | } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 145 | checkStatus(args, vold->markBootAttempt()); |
Daniel Rosenberg | a59e439 | 2019-03-20 17:02:47 -0700 | [diff] [blame] | 146 | } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) { |
| 147 | int retry; |
| 148 | if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL); |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame^] | 149 | checkStatus(args, vold->abortChanges(args[2], retry != 0)); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 150 | } else { |
| 151 | LOG(ERROR) << "Raw commands are no longer supported"; |
| 152 | exit(EINVAL); |
| 153 | } |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 154 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 157 | static void usage(char* progname) { |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 158 | LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 159 | } |