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 | |
Sean Keys | 8452f41 | 2021-07-07 22:38:04 +0000 | [diff] [blame] | 31 | #include "Utils.h" |
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> |
Jaegeuk Kim | 0c52c71 | 2020-12-15 09:00:49 -0800 | [diff] [blame] | 35 | #include <android-base/parsebool.h> |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
Jaegeuk Kim | 0c52c71 | 2020-12-15 09:00:49 -0800 | [diff] [blame] | 38 | #include <android-base/strings.h> |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 39 | #include <binder/IServiceManager.h> |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 40 | #include <binder/Status.h> |
Sean Keys | 8452f41 | 2021-07-07 22:38:04 +0000 | [diff] [blame] | 41 | #include <utils/Errors.h> |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 42 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 43 | #include <private/android_filesystem_config.h> |
| 44 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 45 | static void usage(char* progname); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 46 | |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 47 | static android::sp<android::IBinder> getServiceAggressive() { |
| 48 | android::sp<android::IBinder> res; |
| 49 | auto sm = android::defaultServiceManager(); |
| 50 | auto name = android::String16("vold"); |
Paul Crowley | 0fd2626 | 2018-01-30 09:48:19 -0800 | [diff] [blame] | 51 | for (int i = 0; i < 5000; i++) { |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 52 | res = sm->checkService(name); |
| 53 | if (res) { |
| 54 | LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold"; |
| 55 | break; |
| 56 | } |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 57 | usleep(10000); // 10ms |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 58 | } |
| 59 | return res; |
| 60 | } |
| 61 | |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 62 | static void checkStatus(std::vector<std::string>& cmd, android::binder::Status status) { |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 63 | if (status.isOk()) return; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 64 | std::string command = ::android::base::Join(cmd, " "); |
Tomasz Wasilczyk | fa1b397 | 2023-08-11 16:12:17 +0000 | [diff] [blame] | 65 | LOG(ERROR) << "Command: " << command << " Failed: " << status.toString8().c_str(); |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 66 | exit(ENOTTY); |
| 67 | } |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 68 | |
Sean Keys | 8452f41 | 2021-07-07 22:38:04 +0000 | [diff] [blame] | 69 | static void bindkeys(std::vector<std::string>& args, const android::sp<android::os::IVold>& vold) { |
| 70 | std::string raw_bytes; |
| 71 | const char* seed_value; |
| 72 | |
| 73 | seed_value = getenv("SEED_VALUE"); |
| 74 | if (seed_value == NULL) { |
| 75 | LOG(ERROR) << "Empty seed"; |
| 76 | exit(EINVAL); |
| 77 | } |
| 78 | |
| 79 | android::status_t status = android::vold::HexToStr(seed_value, raw_bytes); |
| 80 | if (status != android::OK) { |
| 81 | LOG(ERROR) << "Extraction of seed failed: " << status; |
| 82 | exit(status); |
| 83 | } |
| 84 | |
| 85 | std::vector<uint8_t> seed{raw_bytes.begin(), raw_bytes.end()}; |
| 86 | checkStatus(args, vold->setStorageBindingSeed(seed)); |
| 87 | } |
| 88 | |
Jaegeuk Kim | 2091c87 | 2024-04-23 19:01:26 -0700 | [diff] [blame] | 89 | static void mountFstab(std::vector<std::string>& args, |
| 90 | const android::sp<android::os::IVold>& vold) { |
| 91 | auto isZoned = android::base::ParseBool(args[4]); |
| 92 | if (isZoned == android::base::ParseBoolResult::kError) exit(EINVAL); |
| 93 | |
| 94 | std::vector<std::string> userDevices = {}; |
| 95 | if (args[5] != "") { |
| 96 | userDevices = android::base::Split(args[5], " "); |
| 97 | } |
| 98 | checkStatus(args, |
| 99 | vold->mountFstab(args[2], args[3], isZoned == android::base::ParseBoolResult::kTrue, |
| 100 | userDevices)); |
| 101 | } |
| 102 | |
| 103 | static void encryptFstab(std::vector<std::string>& args, |
| 104 | const android::sp<android::os::IVold>& vold) { |
| 105 | auto shouldFormat = android::base::ParseBool(args[4]); |
| 106 | if (shouldFormat == android::base::ParseBoolResult::kError) exit(EINVAL); |
| 107 | |
| 108 | auto isZoned = android::base::ParseBool(args[6]); |
| 109 | if (isZoned == android::base::ParseBoolResult::kError) exit(EINVAL); |
| 110 | |
| 111 | std::vector<std::string> userDevices = {}; |
Ashok Mutyala | 8a39878 | 2024-05-28 09:31:44 +0000 | [diff] [blame] | 112 | int64_t length; |
| 113 | if (!android::base::ParseInt(args[7], &length)) exit(EINVAL); |
| 114 | if (args[8] != "") { |
| 115 | userDevices = android::base::Split(args[8], " "); |
Jaegeuk Kim | 2091c87 | 2024-04-23 19:01:26 -0700 | [diff] [blame] | 116 | } |
Ashok Mutyala | 8a39878 | 2024-05-28 09:31:44 +0000 | [diff] [blame] | 117 | checkStatus(args, vold->encryptFstab(args[2], args[3], |
| 118 | shouldFormat == android::base::ParseBoolResult::kTrue, |
| 119 | args[5], isZoned == android::base::ParseBoolResult::kTrue, |
| 120 | userDevices, length)); |
Jaegeuk Kim | 2091c87 | 2024-04-23 19:01:26 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 123 | int main(int argc, char** argv) { |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 124 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 125 | if (getppid() == 1) { |
| 126 | // If init is calling us then it's during boot and we should log to kmsg |
| 127 | android::base::InitLogging(argv, &android::base::KernelLogger); |
| 128 | } else { |
| 129 | android::base::InitLogging(argv, &android::base::StderrLogger); |
| 130 | } |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 131 | std::vector<std::string> args(argv + 1, argv + argc); |
Tom Cherry | f71511a | 2017-03-29 16:50:28 -0700 | [diff] [blame] | 132 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 133 | if (args.size() > 0 && args[0] == "--wait") { |
| 134 | // Just ignore the --wait flag |
| 135 | args.erase(args.begin()); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 138 | if (args.size() < 2) { |
| 139 | usage(argv[0]); |
Paul Lawrence | f4faa57 | 2014-01-29 13:31:03 -0800 | [diff] [blame] | 140 | exit(5); |
Mohamad Ayyash | 5e900ac | 2014-04-15 18:08:05 -0700 | [diff] [blame] | 141 | } |
Paul Crowley | 3c3e360 | 2017-09-27 16:44:33 +0000 | [diff] [blame] | 142 | android::sp<android::IBinder> binder = getServiceAggressive(); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 143 | if (!binder) { |
| 144 | LOG(ERROR) << "Failed to obtain vold Binder"; |
| 145 | exit(EINVAL); |
| 146 | } |
| 147 | auto vold = android::interface_cast<android::os::IVold>(binder); |
| 148 | |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 149 | if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 150 | checkStatus(args, vold->fbeEnable()); |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 151 | } else if (args[0] == "cryptfs" && args[1] == "init_user0") { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 152 | checkStatus(args, vold->initUser0()); |
Martijn Coenen | 23c0445 | 2020-04-29 07:49:41 +0200 | [diff] [blame] | 153 | } else if (args[0] == "volume" && args[1] == "abort_fuse") { |
| 154 | checkStatus(args, vold->abortFuse()); |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 155 | } else if (args[0] == "volume" && args[1] == "shutdown") { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 156 | checkStatus(args, vold->shutdown()); |
Nikita Ioffe | 7596581 | 2019-12-02 11:48:06 +0000 | [diff] [blame] | 157 | } else if (args[0] == "volume" && args[1] == "reset") { |
| 158 | checkStatus(args, vold->reset()); |
Paul Lawrence | d73dfd4 | 2023-08-11 10:27:24 -0700 | [diff] [blame] | 159 | } else if (args[0] == "volume" && args[1] == "getStorageSize") { |
| 160 | int64_t size; |
| 161 | checkStatus(args, vold->getStorageSize(&size)); |
| 162 | LOG(INFO) << size; |
Sean Keys | 8452f41 | 2021-07-07 22:38:04 +0000 | [diff] [blame] | 163 | } else if (args[0] == "cryptfs" && args[1] == "bindkeys") { |
| 164 | bindkeys(args, vold); |
Jaegeuk Kim | 2091c87 | 2024-04-23 19:01:26 -0700 | [diff] [blame] | 165 | } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 6) { |
| 166 | mountFstab(args, vold); |
Ashok Mutyala | 8a39878 | 2024-05-28 09:31:44 +0000 | [diff] [blame] | 167 | } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 9) { |
Jaegeuk Kim | 2091c87 | 2024-04-23 19:01:26 -0700 | [diff] [blame] | 168 | encryptFstab(args, vold); |
Daniel Rosenberg | 9b667fb | 2019-01-22 17:27:25 -0800 | [diff] [blame] | 169 | } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) { |
| 170 | bool supported = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 171 | checkStatus(args, vold->supportsCheckpoint(&supported)); |
Daniel Rosenberg | 9b667fb | 2019-01-22 17:27:25 -0800 | [diff] [blame] | 172 | return supported ? 1 : 0; |
Nikita Ioffe | 7596581 | 2019-12-02 11:48:06 +0000 | [diff] [blame] | 173 | } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" && |
| 174 | args.size() == 2) { |
Paul Lawrence | c5c79c5 | 2019-03-18 13:36:40 -0700 | [diff] [blame] | 175 | bool supported = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 176 | checkStatus(args, vold->supportsBlockCheckpoint(&supported)); |
Paul Lawrence | c5c79c5 | 2019-03-18 13:36:40 -0700 | [diff] [blame] | 177 | return supported ? 1 : 0; |
| 178 | } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) { |
| 179 | bool supported = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 180 | checkStatus(args, vold->supportsFileCheckpoint(&supported)); |
Paul Lawrence | c5c79c5 | 2019-03-18 13:36:40 -0700 | [diff] [blame] | 181 | return supported ? 1 : 0; |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 182 | } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) { |
| 183 | int retry; |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 184 | if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL); |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 185 | checkStatus(args, vold->startCheckpoint(retry)); |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 186 | } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) { |
| 187 | bool enabled = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 188 | checkStatus(args, vold->needsCheckpoint(&enabled)); |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 189 | return enabled ? 1 : 0; |
Daniel Rosenberg | d399249 | 2018-10-02 17:40:44 -0700 | [diff] [blame] | 190 | } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) { |
| 191 | bool enabled = false; |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 192 | checkStatus(args, vold->needsRollback(&enabled)); |
Daniel Rosenberg | d399249 | 2018-10-02 17:40:44 -0700 | [diff] [blame] | 193 | return enabled ? 1 : 0; |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 194 | } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 195 | checkStatus(args, vold->commitChanges()); |
Daniel Rosenberg | 80d1ca5 | 2018-10-09 19:26:57 -0700 | [diff] [blame] | 196 | } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 197 | checkStatus(args, vold->prepareCheckpoint()); |
Paul Lawrence | 1abb2fe | 2018-09-21 10:49:57 -0700 | [diff] [blame] | 198 | } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 199 | checkStatus(args, vold->restoreCheckpoint(args[2])); |
Daniel Rosenberg | dda5981 | 2019-03-06 17:45:17 -0800 | [diff] [blame] | 200 | } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) { |
| 201 | int count; |
| 202 | if (!android::base::ParseInt(args[3], &count)) exit(EINVAL); |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 203 | checkStatus(args, vold->restoreCheckpointPart(args[2], count)); |
Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 204 | } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) { |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 205 | checkStatus(args, vold->markBootAttempt()); |
Daniel Rosenberg | a59e439 | 2019-03-20 17:02:47 -0700 | [diff] [blame] | 206 | } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) { |
| 207 | int retry; |
| 208 | if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL); |
Sandeep Patil | 4377234 | 2019-04-04 09:35:51 -0700 | [diff] [blame] | 209 | checkStatus(args, vold->abortChanges(args[2], retry != 0)); |
Nikita Ioffe | a5798fc | 2019-10-11 16:38:21 +0100 | [diff] [blame] | 210 | } else if (args[0] == "checkpoint" && args[1] == "resetCheckpoint") { |
| 211 | checkStatus(args, vold->resetCheckpoint()); |
Paul Crowley | ed06b3e | 2020-12-01 14:36:06 -0800 | [diff] [blame] | 212 | } else if (args[0] == "keymaster" && args[1] == "earlyBootEnded") { |
Martijn Coenen | eed957f | 2020-11-12 10:59:13 +0100 | [diff] [blame] | 213 | checkStatus(args, vold->earlyBootEnded()); |
Jeff Sharkey | 99f9268 | 2017-09-13 18:43:44 -0600 | [diff] [blame] | 214 | } else { |
| 215 | LOG(ERROR) << "Raw commands are no longer supported"; |
| 216 | exit(EINVAL); |
| 217 | } |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 218 | return 0; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 221 | static void usage(char* progname) { |
Paul Crowley | 2d64b91 | 2017-10-27 13:37:24 -0700 | [diff] [blame] | 222 | LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]"; |
Jeff Sharkey | 47695b2 | 2016-02-01 17:02:29 -0700 | [diff] [blame] | 223 | } |