blob: 76eca3e6b0857da38322d3f004551f0230559d5f [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
Jeff Sharkey99f92682017-09-13 18:43:44 -060031#include "android/os/IVold.h"
32
Tom Cherryf71511a2017-03-29 16:50:28 -070033#include <android-base/logging.h>
Daniel Rosenberg65f99c92018-08-28 01:58:49 -070034#include <android-base/parseint.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070035#include <android-base/stringprintf.h>
Jeff Sharkey99f92682017-09-13 18:43:44 -060036#include <binder/IServiceManager.h>
Paul Crowley2d64b912017-10-27 13:37:24 -070037#include <binder/Status.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070038
San Mehatf1b736b2009-10-10 17:22:08 -070039#include <private/android_filesystem_config.h>
40
Paul Crowley14c8c072018-09-18 13:30:21 -070041static void usage(char* progname);
Jeff Sharkey99f92682017-09-13 18:43:44 -060042
Paul Crowley3c3e3602017-09-27 16:44:33 +000043static android::sp<android::IBinder> getServiceAggressive() {
44 android::sp<android::IBinder> res;
45 auto sm = android::defaultServiceManager();
46 auto name = android::String16("vold");
Paul Crowley0fd26262018-01-30 09:48:19 -080047 for (int i = 0; i < 5000; i++) {
Paul Crowley3c3e3602017-09-27 16:44:33 +000048 res = sm->checkService(name);
49 if (res) {
50 LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold";
51 break;
52 }
Paul Crowley14c8c072018-09-18 13:30:21 -070053 usleep(10000); // 10ms
Paul Crowley3c3e3602017-09-27 16:44:33 +000054 }
55 return res;
56}
57
Paul Crowley2d64b912017-10-27 13:37:24 -070058static void checkStatus(android::binder::Status status) {
59 if (status.isOk()) return;
60 LOG(ERROR) << "Failed: " << status.toString8().string();
61 exit(ENOTTY);
62}
San Mehatf1b736b2009-10-10 17:22:08 -070063
Paul Crowley2d64b912017-10-27 13:37:24 -070064int main(int argc, char** argv) {
Paul Crowley3c3e3602017-09-27 16:44:33 +000065 setenv("ANDROID_LOG_TAGS", "*:v", 1);
Tom Cherryf71511a2017-03-29 16:50:28 -070066 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 Crowley2d64b912017-10-27 13:37:24 -070072 std::vector<std::string> args(argv + 1, argv + argc);
Tom Cherryf71511a2017-03-29 16:50:28 -070073
Paul Crowley2d64b912017-10-27 13:37:24 -070074 if (args.size() > 0 && args[0] == "--wait") {
75 // Just ignore the --wait flag
76 args.erase(args.begin());
San Mehatf1b736b2009-10-10 17:22:08 -070077 }
78
Paul Crowley2d64b912017-10-27 13:37:24 -070079 if (args.size() < 2) {
80 usage(argv[0]);
Paul Lawrencef4faa572014-01-29 13:31:03 -080081 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070082 }
Paul Crowley3c3e3602017-09-27 16:44:33 +000083 android::sp<android::IBinder> binder = getServiceAggressive();
Jeff Sharkey99f92682017-09-13 18:43:44 -060084 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 Crowley2d64b912017-10-27 13:37:24 -070090 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 Sharkey99f92682017-09-13 18:43:44 -060095 int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
Paul Lawrence7ee87cf2017-12-22 10:12:06 -080096 int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI;
Paul Crowley2d64b912017-10-27 13:37:24 -070097 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 Sharkey2048a282017-06-15 09:59:43 -0600102 } else if (args[0] == "cryptfs" && args[1] == "checkEncryption" && args.size() == 3) {
103 checkStatus(vold->checkEncryption(args[2]));
Paul Crowley0fd26262018-01-30 09:48:19 -0800104 } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 3) {
105 checkStatus(vold->mountFstab(args[2]));
106 } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 3) {
107 checkStatus(vold->encryptFstab(args[2]));
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800108 } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
109 bool supported = false;
110 checkStatus(vold->supportsCheckpoint(&supported));
111 return supported ? 1 : 0;
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700112 } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" && args.size() == 2) {
113 bool supported = false;
114 checkStatus(vold->supportsBlockCheckpoint(&supported));
115 return supported ? 1 : 0;
116 } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) {
117 bool supported = false;
118 checkStatus(vold->supportsFileCheckpoint(&supported));
119 return supported ? 1 : 0;
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700120 } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) {
121 int retry;
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700122 if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700123 checkStatus(vold->startCheckpoint(retry));
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700124 } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) {
125 bool enabled = false;
126 checkStatus(vold->needsCheckpoint(&enabled));
127 return enabled ? 1 : 0;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700128 } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) {
129 bool enabled = false;
130 checkStatus(vold->needsRollback(&enabled));
131 return enabled ? 1 : 0;
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700132 } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) {
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700133 checkStatus(vold->commitChanges());
Daniel Rosenberg80d1ca52018-10-09 19:26:57 -0700134 } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) {
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700135 checkStatus(vold->prepareCheckpoint());
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700136 } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) {
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700137 checkStatus(vold->restoreCheckpoint(args[2]));
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800138 } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) {
139 int count;
140 if (!android::base::ParseInt(args[3], &count)) exit(EINVAL);
141 checkStatus(vold->restoreCheckpointPart(args[2], count));
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700142 } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) {
Daniel Rosenberg73680ec2018-10-10 18:52:04 -0700143 checkStatus(vold->markBootAttempt());
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700144 } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) {
145 int retry;
146 if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
147 checkStatus(vold->abortChanges(args[2], retry != 0));
Jeff Sharkey99f92682017-09-13 18:43:44 -0600148 } else {
149 LOG(ERROR) << "Raw commands are no longer supported";
150 exit(EINVAL);
151 }
Paul Crowley2d64b912017-10-27 13:37:24 -0700152 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700153}
154
Paul Crowley14c8c072018-09-18 13:30:21 -0700155static void usage(char* progname) {
Paul Crowley2d64b912017-10-27 13:37:24 -0700156 LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700157}