blob: 1aaeb5afd7a20934f87346456f06a9e80c18f92a [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>
Sandeep Patil43772342019-04-04 09:35:51 -070035#include <android-base/strings.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070036#include <android-base/stringprintf.h>
Jeff Sharkey99f92682017-09-13 18:43:44 -060037#include <binder/IServiceManager.h>
Paul Crowley2d64b912017-10-27 13:37:24 -070038#include <binder/Status.h>
Jeff Sharkey47695b22016-02-01 17:02:29 -070039
San Mehatf1b736b2009-10-10 17:22:08 -070040#include <private/android_filesystem_config.h>
41
Paul Crowley14c8c072018-09-18 13:30:21 -070042static void usage(char* progname);
Jeff Sharkey99f92682017-09-13 18:43:44 -060043
Paul Crowley3c3e3602017-09-27 16:44:33 +000044static android::sp<android::IBinder> getServiceAggressive() {
45 android::sp<android::IBinder> res;
46 auto sm = android::defaultServiceManager();
47 auto name = android::String16("vold");
Paul Crowley0fd26262018-01-30 09:48:19 -080048 for (int i = 0; i < 5000; i++) {
Paul Crowley3c3e3602017-09-27 16:44:33 +000049 res = sm->checkService(name);
50 if (res) {
51 LOG(VERBOSE) << "Waited " << (i * 10) << "ms for vold";
52 break;
53 }
Paul Crowley14c8c072018-09-18 13:30:21 -070054 usleep(10000); // 10ms
Paul Crowley3c3e3602017-09-27 16:44:33 +000055 }
56 return res;
57}
58
Sandeep Patil43772342019-04-04 09:35:51 -070059static void checkStatus(std::vector<std::string>& cmd, android::binder::Status status) {
Paul Crowley2d64b912017-10-27 13:37:24 -070060 if (status.isOk()) return;
Sandeep Patil43772342019-04-04 09:35:51 -070061 std::string command = ::android::base::Join(cmd, " ");
62 LOG(ERROR) << "Command: " << command << " Failed: " << status.toString8().string();
Paul Crowley2d64b912017-10-27 13:37:24 -070063 exit(ENOTTY);
64}
San Mehatf1b736b2009-10-10 17:22:08 -070065
Paul Crowley2d64b912017-10-27 13:37:24 -070066int main(int argc, char** argv) {
Paul Crowley3c3e3602017-09-27 16:44:33 +000067 setenv("ANDROID_LOG_TAGS", "*:v", 1);
Tom Cherryf71511a2017-03-29 16:50:28 -070068 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 Crowley2d64b912017-10-27 13:37:24 -070074 std::vector<std::string> args(argv + 1, argv + argc);
Tom Cherryf71511a2017-03-29 16:50:28 -070075
Paul Crowley2d64b912017-10-27 13:37:24 -070076 if (args.size() > 0 && args[0] == "--wait") {
77 // Just ignore the --wait flag
78 args.erase(args.begin());
San Mehatf1b736b2009-10-10 17:22:08 -070079 }
80
Paul Crowley2d64b912017-10-27 13:37:24 -070081 if (args.size() < 2) {
82 usage(argv[0]);
Paul Lawrencef4faa572014-01-29 13:31:03 -080083 exit(5);
Mohamad Ayyash5e900ac2014-04-15 18:08:05 -070084 }
Paul Crowley3c3e3602017-09-27 16:44:33 +000085 android::sp<android::IBinder> binder = getServiceAggressive();
Jeff Sharkey99f92682017-09-13 18:43:44 -060086 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 Crowley2d64b912017-10-27 13:37:24 -070092 if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") {
Sandeep Patil43772342019-04-04 09:35:51 -070093 checkStatus(args, vold->fbeEnable());
Paul Crowley2d64b912017-10-27 13:37:24 -070094 } else if (args[0] == "cryptfs" && args[1] == "init_user0") {
Sandeep Patil43772342019-04-04 09:35:51 -070095 checkStatus(args, vold->initUser0());
Paul Crowley2d64b912017-10-27 13:37:24 -070096 } else if (args[0] == "cryptfs" && args[1] == "enablecrypto") {
Jeff Sharkey99f92682017-09-13 18:43:44 -060097 int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
Paul Lawrence7ee87cf2017-12-22 10:12:06 -080098 int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI;
Sandeep Patil43772342019-04-04 09:35:51 -070099 checkStatus(args, vold->fdeEnable(passwordType, "", encryptionFlags));
Paul Crowley2d64b912017-10-27 13:37:24 -0700100 } else if (args[0] == "cryptfs" && args[1] == "mountdefaultencrypted") {
Sandeep Patil43772342019-04-04 09:35:51 -0700101 checkStatus(args, vold->mountDefaultEncrypted());
Martijn Coenen23c04452020-04-29 07:49:41 +0200102 } else if (args[0] == "volume" && args[1] == "abort_fuse") {
103 checkStatus(args, vold->abortFuse());
Paul Crowley2d64b912017-10-27 13:37:24 -0700104 } else if (args[0] == "volume" && args[1] == "shutdown") {
Sandeep Patil43772342019-04-04 09:35:51 -0700105 checkStatus(args, vold->shutdown());
Nikita Ioffe75965812019-12-02 11:48:06 +0000106 } else if (args[0] == "volume" && args[1] == "reset") {
107 checkStatus(args, vold->reset());
Paul Lawrence236e5e82019-06-25 14:44:33 -0700108 } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 4) {
109 checkStatus(args, vold->mountFstab(args[2], args[3]));
110 } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 4) {
111 checkStatus(args, vold->encryptFstab(args[2], args[3]));
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800112 } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
113 bool supported = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700114 checkStatus(args, vold->supportsCheckpoint(&supported));
Daniel Rosenberg9b667fb2019-01-22 17:27:25 -0800115 return supported ? 1 : 0;
Nikita Ioffe75965812019-12-02 11:48:06 +0000116 } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" &&
117 args.size() == 2) {
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700118 bool supported = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700119 checkStatus(args, vold->supportsBlockCheckpoint(&supported));
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700120 return supported ? 1 : 0;
121 } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) {
122 bool supported = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700123 checkStatus(args, vold->supportsFileCheckpoint(&supported));
Paul Lawrencec5c79c52019-03-18 13:36:40 -0700124 return supported ? 1 : 0;
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700125 } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) {
126 int retry;
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700127 if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
Sandeep Patil43772342019-04-04 09:35:51 -0700128 checkStatus(args, vold->startCheckpoint(retry));
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700129 } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) {
130 bool enabled = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700131 checkStatus(args, vold->needsCheckpoint(&enabled));
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700132 return enabled ? 1 : 0;
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700133 } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) {
134 bool enabled = false;
Sandeep Patil43772342019-04-04 09:35:51 -0700135 checkStatus(args, vold->needsRollback(&enabled));
Daniel Rosenbergd3992492018-10-02 17:40:44 -0700136 return enabled ? 1 : 0;
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700137 } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) {
Sandeep Patil43772342019-04-04 09:35:51 -0700138 checkStatus(args, vold->commitChanges());
Daniel Rosenberg80d1ca52018-10-09 19:26:57 -0700139 } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) {
Sandeep Patil43772342019-04-04 09:35:51 -0700140 checkStatus(args, vold->prepareCheckpoint());
Paul Lawrence1abb2fe2018-09-21 10:49:57 -0700141 } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) {
Sandeep Patil43772342019-04-04 09:35:51 -0700142 checkStatus(args, vold->restoreCheckpoint(args[2]));
Daniel Rosenbergdda59812019-03-06 17:45:17 -0800143 } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) {
144 int count;
145 if (!android::base::ParseInt(args[3], &count)) exit(EINVAL);
Sandeep Patil43772342019-04-04 09:35:51 -0700146 checkStatus(args, vold->restoreCheckpointPart(args[2], count));
Daniel Rosenberg65f99c92018-08-28 01:58:49 -0700147 } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) {
Sandeep Patil43772342019-04-04 09:35:51 -0700148 checkStatus(args, vold->markBootAttempt());
Daniel Rosenberga59e4392019-03-20 17:02:47 -0700149 } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) {
150 int retry;
151 if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
Sandeep Patil43772342019-04-04 09:35:51 -0700152 checkStatus(args, vold->abortChanges(args[2], retry != 0));
Nikita Ioffea5798fc2019-10-11 16:38:21 +0100153 } else if (args[0] == "checkpoint" && args[1] == "resetCheckpoint") {
154 checkStatus(args, vold->resetCheckpoint());
Paul Crowleyed06b3e2020-12-01 14:36:06 -0800155 } else if (args[0] == "keymaster" && args[1] == "earlyBootEnded") {
Martijn Coeneneed957f2020-11-12 10:59:13 +0100156 checkStatus(args, vold->earlyBootEnded());
Jeff Sharkey99f92682017-09-13 18:43:44 -0600157 } else {
158 LOG(ERROR) << "Raw commands are no longer supported";
159 exit(EINVAL);
160 }
Paul Crowley2d64b912017-10-27 13:37:24 -0700161 return 0;
San Mehatf1b736b2009-10-10 17:22:08 -0700162}
163
Paul Crowley14c8c072018-09-18 13:30:21 -0700164static void usage(char* progname) {
Paul Crowley2d64b912017-10-27 13:37:24 -0700165 LOG(INFO) << "Usage: " << progname << " [--wait] <system> <subcommand> [args...]";
Jeff Sharkey47695b22016-02-01 17:02:29 -0700166}