blob: 400bc5de5a96c7cb79847c25e8fc6edc944849ab [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
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 Mehatf1b736b2009-10-10 17:22:08 -070017#include <errno.h>
18#include <fcntl.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070019#include <poll.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070020#include <signal.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
San Mehatf1b736b2009-10-10 17:22:08 -070025
San Mehatf1b736b2009-10-10 17:22:08 -070026#include <sys/select.h>
27#include <sys/time.h>
28#include <sys/types.h>
29#include <sys/un.h>
30
Sean Keys8452f412021-07-07 22:38:04 +000031#include "Utils.h"
Jeff Sharkey99f92682017-09-13 18:43:44 -060032#include "android/os/IVold.h"
33
Tom Cherryf71511a2017-03-29 16:50:28 -070034#include <android-base/logging.h>
Jaegeuk Kim0c52c712020-12-15 09:00:49 -080035#include <android-base/parsebool.h>
Daniel Rosenberg65f99c92018-08-28 01:58:49 -070036#include <android-base/parseint.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070037#include <android-base/stringprintf.h>
Jaegeuk Kim0c52c712020-12-15 09:00:49 -080038#include <android-base/strings.h>
Jeff Sharkey99f92682017-09-13 18:43:44 -060039#include <binder/IServiceManager.h>
Paul Crowley2d64b912017-10-27 13:37:24 -070040#include <binder/Status.h>
Sean Keys8452f412021-07-07 22:38:04 +000041#include <utils/Errors.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070042
San Mehatf1b736b2009-10-10 17:22:08 -070043#include <private/android_filesystem_config.h>
44
Paul Crowley14c8c072018-09-18 13:30:21 -070045static void usage(char* progname);
Jeff Sharkey99f92682017-09-13 18:43:44 -060046
Paul Crowley3c3e3602017-09-27 16:44:33 +000047static android::sp<android::IBinder> getServiceAggressive() {
48 android::sp<android::IBinder> res;
49 auto sm = android::defaultServiceManager();
50 auto name = android::String16("vold");
Paul Crowley0fd26262018-01-30 09:48:19 -080051 for (int i = 0; i < 5000; i++) {
Paul Crowley3c3e3602017-09-27 16:44:33 +000052 res = sm->checkService(name);
53 if (res) {
54 LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold";
55 break;
56 }
Paul Crowley14c8c072018-09-18 13:30:21 -070057 usleep(10000); // 10ms
Paul Crowley3c3e3602017-09-27 16:44:33 +000058 }
59 return res;
60}
61
Sandeep Patil43772342019-04-04 09:35:51 -070062static void checkStatus(std::vector<std::string>& cmd, android::binder::Status status) {
Paul Crowley2d64b912017-10-27 13:37:24 -070063 if (status.isOk()) return;
Sandeep Patil43772342019-04-04 09:35:51 -070064 std::string command = ::android::base::Join(cmd, " ");
Tomasz Wasilczykfa1b3972023-08-11 16:12:17 +000065 LOG(ERROR) << "Command: " << command << " Failed: " << status.toString8().c_str();
Paul Crowley2d64b912017-10-27 13:37:24 -070066 exit(ENOTTY);
67}
San Mehatf1b736b2009-10-10 17:22:08 -070068
Sean Keys8452f412021-07-07 22:38:04 +000069static 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 Kim2091c872024-04-23 19:01:26 -070089static 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
103static 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 Mutyala8a398782024-05-28 09:31:44 +0000112 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 Kim2091c872024-04-23 19:01:26 -0700116 }
Daeho Jeong0c841552024-10-21 14:05:31 -0700117 std::vector<std::string> deviceAliasedStr = {};
118 std::vector<bool> deviceAliased = {};
119 if (args[9] != "") {
120 deviceAliasedStr = android::base::Split(args[9], " ");
121 for (auto aliased : deviceAliasedStr) {
122 if (aliased == "0") {
123 deviceAliased.push_back(false);
124 } else {
125 deviceAliased.push_back(true);
126 }
127 }
128 }
Ashok Mutyala8a398782024-05-28 09:31:44 +0000129 checkStatus(args, vold->encryptFstab(args[2], args[3],
130 shouldFormat == android::base::ParseBoolResult::kTrue,
131 args[5], isZoned == android::base::ParseBoolResult::kTrue,
Daeho Jeong0c841552024-10-21 14:05:31 -0700132 userDevices, deviceAliased, length));
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700133}
134
Paul Crowley2d64b912017-10-27 13:37:24 -0700135int main(int argc, char** argv) {
Paul Crowley3c3e3602017-09-27 16:44:33 +0000136 setenv("ANDROID_LOG_TAGS", "*:v", 1);
Tom Cherryf71511a2017-03-29 16:50:28 -0700137 if (getppid() == 1) {
138 // If init is calling us then it's during boot and we should log to kmsg
139 android::base::InitLogging(argv, &android::base::KernelLogger);
140 } else {
141 android::base::InitLogging(argv, &android::base::StderrLogger);
142 }
Paul Crowley2d64b912017-10-27 13:37:24 -0700143 std::vector<std::string> args(argv + 1, argv + argc);
Tom Cherryf71511a2017-03-29 16:50:28 -0700144
Paul Crowley2d64b912017-10-27 13:37:24 -0700145 if (args.size() > 0 && args[0] == "--wait") {
146 // Just ignore the --wait flag
147 args.erase(args.begin());
San Mehatf1b736b2009-10-10 17:22:08 -0700148 }
149
Paul Crowley2d64b912017-10-27 13:37:24 -0700150 if (args.size() < 2) {
151 usage(argv[0]);
Paul Lawrencef4faa572014-01-29 13:31:03 -0800152 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -0700153 }
Paul Crowley3c3e3602017-09-27 16:44:33 +0000154 android::sp<android::IBinder> binder = getServiceAggressive();
Jeff Sharkey99f92682017-09-13 18:43:44 -0600155 if (!binder) {
156 LOG(ERROR) << "Failed to obtain vold Binder";
157 exit(EINVAL);
158 }
159 auto vold = android::interface_cast<android::os::IVold>(binder);
160
Paul Crowley2d64b912017-10-27 13:37:24 -0700161 if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") {
Sandeep Patil43772342019-04-04 09:35:51 -0700162 checkStatus(args, vold->fbeEnable());
Paul Crowley2d64b912017-10-27 13:37:24 -0700163 } else if (args[0] == "cryptfs" && args[1] == "init_user0") {
Sandeep Patil43772342019-04-04 09:35:51 -0700164 checkStatus(args, vold->initUser0());
Martijn Coenen23c04452020-04-29 07:49:41 +0200165 } else if (args[0] == "volume" && args[1] == "abort_fuse") {
166 checkStatus(args, vold->abortFuse());
Paul Crowley2d64b912017-10-27 13:37:24 -0700167 } else if (args[0] == "volume" && args[1] == "shutdown") {
Sandeep Patil43772342019-04-04 09:35:51 -0700168 checkStatus(args, vold->shutdown());
Nikita Ioffe75965812019-12-02 11:48:06 +0000169 } else if (args[0] == "volume" && args[1] == "reset") {
170 checkStatus(args, vold->reset());
Paul Lawrenced73dfd42023-08-11 10:27:24 -0700171 } else if (args[0] == "volume" && args[1] == "getStorageSize") {
172 int64_t size;
173 checkStatus(args, vold->getStorageSize(&size));
174 LOG(INFO) << size;
Sean Keys8452f412021-07-07 22:38:04 +0000175 } else if (args[0] == "cryptfs" && args[1] == "bindkeys") {
176 bindkeys(args, vold);
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700177 } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 6) {
178 mountFstab(args, vold);
Daeho Jeong0c841552024-10-21 14:05:31 -0700179 } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 10) {
Jaegeuk Kim2091c872024-04-23 19:01:26 -0700180 encryptFstab(args, vold);
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800181 } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
182 bool supported = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700183 checkStatus(args, vold->supportsCheckpoint(&supported));
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800184 return supported ? 1 : 0;
Nikita Ioffe75965812019-12-02 11:48:06 +0000185 } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" &&
186 args.size() == 2) {
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700187 bool supported = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700188 checkStatus(args, vold->supportsBlockCheckpoint(&supported));
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700189 return supported ? 1 : 0;
190 } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) {
191 bool supported = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700192 checkStatus(args, vold->supportsFileCheckpoint(&supported));
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700193 return supported ? 1 : 0;
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700194 } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) {
195 int retry;
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700196 if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
Sandeep Patil43772342019-04-04 09:35:51 -0700197 checkStatus(args, vold->startCheckpoint(retry));
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700198 } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) {
199 bool enabled = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700200 checkStatus(args, vold->needsCheckpoint(&enabled));
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700201 return enabled ? 1 : 0;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700202 } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) {
203 bool enabled = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700204 checkStatus(args, vold->needsRollback(&enabled));
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700205 return enabled ? 1 : 0;
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700206 } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) {
Sandeep Patil43772342019-04-04 09:35:51 -0700207 checkStatus(args, vold->commitChanges());
Daniel Rosenberg80d1ca52018-10-09 19:26:57 -0700208 } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) {
Sandeep Patil43772342019-04-04 09:35:51 -0700209 checkStatus(args, vold->prepareCheckpoint());
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700210 } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) {
Sandeep Patil43772342019-04-04 09:35:51 -0700211 checkStatus(args, vold->restoreCheckpoint(args[2]));
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800212 } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) {
213 int count;
214 if (!android::base::ParseInt(args[3], &count)) exit(EINVAL);
Sandeep Patil43772342019-04-04 09:35:51 -0700215 checkStatus(args, vold->restoreCheckpointPart(args[2], count));
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700216 } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) {
Sandeep Patil43772342019-04-04 09:35:51 -0700217 checkStatus(args, vold->markBootAttempt());
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700218 } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) {
219 int retry;
220 if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
Sandeep Patil43772342019-04-04 09:35:51 -0700221 checkStatus(args, vold->abortChanges(args[2], retry != 0));
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100222 } else if (args[0] == "checkpoint" && args[1] == "resetCheckpoint") {
223 checkStatus(args, vold->resetCheckpoint());
Paul Crowleyed06b3e2020-12-01 14:36:06 -0800224 } else if (args[0] == "keymaster" && args[1] == "earlyBootEnded") {
Martijn Coeneneed957f2020-11-12 10:59:13 +0100225 checkStatus(args, vold->earlyBootEnded());
Jeff Sharkey99f92682017-09-13 18:43:44 -0600226 } else {
227 LOG(ERROR) << "Raw commands are no longer supported";
228 exit(EINVAL);
229 }
Paul Crowley2d64b912017-10-27 13:37:24 -0700230 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700231}
232
Paul Crowley14c8c072018-09-18 13:30:21 -0700233static void usage(char* progname) {
Paul Crowley2d64b912017-10-27 13:37:24 -0700234 LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700235}