David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2019 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 | |
| 17 | #include <getopt.h> |
| 18 | #include <stdio.h> |
| 19 | #include <sysexits.h> |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 20 | #include <unistd.h> |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 21 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 22 | #include <chrono> |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 23 | #include <condition_variable> |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 24 | #include <functional> |
| 25 | #include <iostream> |
| 26 | #include <map> |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 27 | #include <mutex> |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 28 | #include <string> |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 29 | #include <thread> |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 30 | |
David Anderson | 83fdeca | 2019-09-09 17:56:22 -0700 | [diff] [blame] | 31 | #include <android-base/logging.h> |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 32 | #include <android-base/parseint.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 33 | #include <android-base/properties.h> |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 35 | #include <android-base/strings.h> |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 36 | #include <android-base/unique_fd.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 37 | #include <android/gsi/IGsiService.h> |
David Anderson | b33f2b1 | 2019-01-22 14:14:10 -0800 | [diff] [blame] | 38 | #include <cutils/android_reboot.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 39 | #include <libgsi/libgsi.h> |
David Anderson | 83fdeca | 2019-09-09 17:56:22 -0700 | [diff] [blame] | 40 | #include <libgsi/libgsid.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 41 | |
| 42 | using namespace android::gsi; |
| 43 | using namespace std::chrono_literals; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 44 | |
| 45 | using android::sp; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 46 | using android::base::Split; |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 47 | using android::base::StringPrintf; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 48 | using CommandCallback = std::function<int(sp<IGsiService>, int, char**)>; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 49 | |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 50 | static int Disable(sp<IGsiService> gsid, int argc, char** argv); |
| 51 | static int Enable(sp<IGsiService> gsid, int argc, char** argv); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 52 | static int Install(sp<IGsiService> gsid, int argc, char** argv); |
Yo Chiang | c443f4f | 2020-10-21 21:49:20 +0800 | [diff] [blame] | 53 | static int CreatePartition(sp<IGsiService> gsid, int argc, char** argv); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 54 | static int Wipe(sp<IGsiService> gsid, int argc, char** argv); |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 55 | static int WipeData(sp<IGsiService> gsid, int argc, char** argv); |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 56 | static int Status(sp<IGsiService> gsid, int argc, char** argv); |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 57 | static int Cancel(sp<IGsiService> gsid, int argc, char** argv); |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 58 | |
| 59 | static const std::map<std::string, CommandCallback> kCommandMap = { |
David Anderson | 5cc440c | 2019-04-16 14:34:27 -0700 | [diff] [blame] | 60 | // clang-format off |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 61 | {"disable", Disable}, |
| 62 | {"enable", Enable}, |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 63 | {"install", Install}, |
Yo Chiang | c443f4f | 2020-10-21 21:49:20 +0800 | [diff] [blame] | 64 | {"create-partition", CreatePartition}, |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 65 | {"wipe", Wipe}, |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 66 | {"wipe-data", WipeData}, |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 67 | {"status", Status}, |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 68 | {"cancel", Cancel}, |
David Anderson | 5cc440c | 2019-04-16 14:34:27 -0700 | [diff] [blame] | 69 | // clang-format on |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
David Anderson | 5cc440c | 2019-04-16 14:34:27 -0700 | [diff] [blame] | 72 | static std::string ErrorMessage(const android::binder::Status& status, |
| 73 | int error_code = IGsiService::INSTALL_ERROR_GENERIC) { |
David Anderson | 646b4b2 | 2019-02-27 18:26:54 -0800 | [diff] [blame] | 74 | if (!status.isOk()) { |
| 75 | return status.exceptionMessage().string(); |
| 76 | } |
| 77 | return "error code " + std::to_string(error_code); |
| 78 | } |
| 79 | |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 80 | class ProgressBar { |
| 81 | public: |
| 82 | explicit ProgressBar(sp<IGsiService> gsid) : gsid_(gsid) {} |
| 83 | |
| 84 | ~ProgressBar() { Stop(); } |
| 85 | |
| 86 | void Display() { |
David Anderson | b0f3c82 | 2019-01-31 19:19:02 -0800 | [diff] [blame] | 87 | Finish(); |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 88 | done_ = false; |
| 89 | last_update_ = {}; |
| 90 | worker_ = std::make_unique<std::thread>([this]() { Worker(); }); |
| 91 | } |
| 92 | |
| 93 | void Stop() { |
| 94 | if (!worker_) { |
| 95 | return; |
| 96 | } |
| 97 | SignalDone(); |
| 98 | worker_->join(); |
| 99 | worker_ = nullptr; |
| 100 | } |
| 101 | |
| 102 | void Finish() { |
David Anderson | b0f3c82 | 2019-01-31 19:19:02 -0800 | [diff] [blame] | 103 | if (!worker_) { |
| 104 | return; |
| 105 | } |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 106 | Stop(); |
| 107 | FinishLastBar(); |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | void Worker() { |
| 112 | std::unique_lock<std::mutex> lock(mutex_); |
| 113 | while (!done_) { |
| 114 | if (!UpdateProgress()) { |
| 115 | return; |
| 116 | } |
| 117 | cv_.wait_for(lock, 500ms, [this] { return done_; }); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | bool UpdateProgress() { |
| 122 | GsiProgress latest; |
| 123 | auto status = gsid_->getInstallProgress(&latest); |
| 124 | if (!status.isOk()) { |
| 125 | std::cout << std::endl; |
| 126 | return false; |
| 127 | } |
| 128 | if (latest.status == IGsiService::STATUS_NO_OPERATION) { |
| 129 | return true; |
| 130 | } |
| 131 | if (last_update_.step != latest.step) { |
| 132 | FinishLastBar(); |
| 133 | } |
| 134 | Display(latest); |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | void FinishLastBar() { |
David Anderson | b0f3c82 | 2019-01-31 19:19:02 -0800 | [diff] [blame] | 139 | // If no bar was in progress, don't do anything. |
| 140 | if (last_update_.total_bytes == 0) { |
| 141 | return; |
| 142 | } |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 143 | // Ensure we finish the display at 100%. |
| 144 | last_update_.bytes_processed = last_update_.total_bytes; |
| 145 | Display(last_update_); |
| 146 | std::cout << std::endl; |
| 147 | } |
| 148 | |
| 149 | void Display(const GsiProgress& progress) { |
| 150 | if (progress.total_bytes == 0) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | static constexpr int kColumns = 80; |
| 155 | static constexpr char kRedColor[] = "\x1b[31m"; |
| 156 | static constexpr char kGreenColor[] = "\x1b[32m"; |
| 157 | static constexpr char kResetColor[] = "\x1b[0m"; |
| 158 | |
| 159 | int percentage = (progress.bytes_processed * 100) / progress.total_bytes; |
| 160 | int64_t bytes_per_col = progress.total_bytes / kColumns; |
| 161 | uint32_t fill_count = progress.bytes_processed / bytes_per_col; |
| 162 | uint32_t dash_count = kColumns - fill_count; |
| 163 | std::string fills = std::string(fill_count, '='); |
| 164 | std::string dashes = std::string(dash_count, '-'); |
| 165 | |
| 166 | // Give the end of the bar some flare. |
| 167 | if (!fills.empty() && !dashes.empty()) { |
| 168 | fills[fills.size() - 1] = '>'; |
| 169 | } |
| 170 | |
| 171 | fprintf(stdout, "\r%-15s%6d%% ", progress.step.c_str(), percentage); |
| 172 | fprintf(stdout, "%s[%s%s%s", kGreenColor, fills.c_str(), kRedColor, dashes.c_str()); |
| 173 | fprintf(stdout, "%s]%s", kGreenColor, kResetColor); |
| 174 | fflush(stdout); |
| 175 | |
| 176 | last_update_ = progress; |
| 177 | } |
| 178 | |
| 179 | void SignalDone() { |
| 180 | std::lock_guard<std::mutex> guard(mutex_); |
| 181 | done_ = true; |
| 182 | cv_.notify_all(); |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | sp<IGsiService> gsid_; |
| 187 | std::unique_ptr<std::thread> worker_; |
| 188 | std::condition_variable cv_; |
| 189 | std::mutex mutex_; |
| 190 | GsiProgress last_update_; |
| 191 | bool done_ = false; |
| 192 | }; |
| 193 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 194 | static int Install(sp<IGsiService> gsid, int argc, char** argv) { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 195 | constexpr const char* kDefaultPartition = "system"; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 196 | struct option options[] = { |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 197 | {"install-dir", required_argument, nullptr, 'i'}, |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 198 | {"gsi-size", required_argument, nullptr, 's'}, |
David Anderson | b33f2b1 | 2019-01-22 14:14:10 -0800 | [diff] [blame] | 199 | {"no-reboot", no_argument, nullptr, 'n'}, |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 200 | {"userdata-size", required_argument, nullptr, 'u'}, |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 201 | {"partition-name", required_argument, nullptr, 'p'}, |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 202 | {"wipe", no_argument, nullptr, 'w'}, |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 203 | {nullptr, 0, nullptr, 0}, |
| 204 | }; |
| 205 | |
Paul Trautrim | 3c23df2 | 2019-12-11 16:18:22 +0900 | [diff] [blame] | 206 | int64_t gsiSize = 0; |
| 207 | int64_t userdataSize = 0; |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 208 | bool wipeUserdata = false; |
David Anderson | b33f2b1 | 2019-01-22 14:14:10 -0800 | [diff] [blame] | 209 | bool reboot = true; |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 210 | std::string installDir = ""; |
| 211 | std::string partition = kDefaultPartition; |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 212 | if (getuid() != 0) { |
| 213 | std::cerr << "must be root to install a GSI" << std::endl; |
| 214 | return EX_NOPERM; |
| 215 | } |
| 216 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 217 | int rv, index; |
| 218 | while ((rv = getopt_long_only(argc, argv, "", options, &index)) != -1) { |
| 219 | switch (rv) { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 220 | case 'p': |
| 221 | partition = optarg; |
| 222 | break; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 223 | case 's': |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 224 | if (!android::base::ParseInt(optarg, &gsiSize) || gsiSize <= 0) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 225 | std::cerr << "Could not parse image size: " << optarg << std::endl; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 226 | return EX_USAGE; |
| 227 | } |
| 228 | break; |
| 229 | case 'u': |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 230 | if (!android::base::ParseInt(optarg, &userdataSize) || userdataSize < 0) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 231 | std::cerr << "Could not parse image size: " << optarg << std::endl; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 232 | return EX_USAGE; |
| 233 | } |
| 234 | break; |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 235 | case 'i': |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 236 | installDir = optarg; |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 237 | break; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 238 | case 'w': |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 239 | wipeUserdata = true; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 240 | break; |
David Anderson | b33f2b1 | 2019-01-22 14:14:10 -0800 | [diff] [blame] | 241 | case 'n': |
| 242 | reboot = false; |
| 243 | break; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 247 | if (gsiSize <= 0) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 248 | std::cerr << "Must specify --gsi-size." << std::endl; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 249 | return EX_USAGE; |
| 250 | } |
| 251 | |
David Anderson | a78f811 | 2019-01-17 11:50:50 -0800 | [diff] [blame] | 252 | bool running_gsi = false; |
| 253 | gsid->isGsiRunning(&running_gsi); |
| 254 | if (running_gsi) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 255 | std::cerr << "Cannot install a GSI within a live GSI." << std::endl; |
| 256 | std::cerr << "Use gsi_tool disable or wipe and reboot first." << std::endl; |
David Anderson | a78f811 | 2019-01-17 11:50:50 -0800 | [diff] [blame] | 257 | return EX_SOFTWARE; |
| 258 | } |
| 259 | |
Yo Chiang | 1b466d2 | 2020-08-28 22:00:42 +0800 | [diff] [blame] | 260 | android::base::unique_fd input(dup(STDIN_FILENO)); |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 261 | if (input < 0) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 262 | std::cerr << "Error duplicating descriptor: " << strerror(errno) << std::endl; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 263 | return EX_SOFTWARE; |
| 264 | } |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 265 | // Note: the progress bar needs to be re-started in between each call. |
| 266 | ProgressBar progress(gsid); |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 267 | progress.Display(); |
David Anderson | 868dea5 | 2019-01-17 13:34:38 -0800 | [diff] [blame] | 268 | int error; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 269 | auto status = gsid->openInstall(installDir, &error); |
| 270 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
Yo Chiang | 99b12d0 | 2019-12-02 15:47:57 +0800 | [diff] [blame] | 271 | std::cerr << "Could not open DSU installation: " << ErrorMessage(status, error) << "\n"; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 272 | return EX_SOFTWARE; |
| 273 | } |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 274 | if (partition == kDefaultPartition) { |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 275 | auto status = gsid->createPartition("userdata", userdataSize, false, &error); |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 276 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
| 277 | std::cerr << "Could not start live image install: " << ErrorMessage(status, error) |
| 278 | << "\n"; |
| 279 | return EX_SOFTWARE; |
| 280 | } |
Yo Chiang | f9f5dbb | 2020-12-28 20:14:34 +0800 | [diff] [blame] | 281 | status = gsid->closePartition(&error); |
| 282 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
| 283 | std::cerr << "Could not closePartition(userdata): " << ErrorMessage(status, error) |
| 284 | << std::endl; |
| 285 | return EX_SOFTWARE; |
| 286 | } |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 287 | } |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 288 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 289 | status = gsid->createPartition(partition, gsiSize, true, &error); |
David Anderson | 868dea5 | 2019-01-17 13:34:38 -0800 | [diff] [blame] | 290 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
David Anderson | 646b4b2 | 2019-02-27 18:26:54 -0800 | [diff] [blame] | 291 | std::cerr << "Could not start live image install: " << ErrorMessage(status, error) << "\n"; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 292 | return EX_SOFTWARE; |
| 293 | } |
David Anderson | 1d94d26 | 2019-01-11 20:39:51 -0800 | [diff] [blame] | 294 | android::os::ParcelFileDescriptor stream(std::move(input)); |
| 295 | |
David Anderson | 868dea5 | 2019-01-17 13:34:38 -0800 | [diff] [blame] | 296 | bool ok = false; |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 297 | progress.Display(); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 298 | status = gsid->commitGsiChunkFromStream(stream, gsiSize, &ok); |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 299 | if (!ok) { |
David Anderson | 646b4b2 | 2019-02-27 18:26:54 -0800 | [diff] [blame] | 300 | std::cerr << "Could not commit live image data: " << ErrorMessage(status) << "\n"; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 301 | return EX_SOFTWARE; |
| 302 | } |
| 303 | |
Yo Chiang | f9f5dbb | 2020-12-28 20:14:34 +0800 | [diff] [blame] | 304 | status = gsid->closePartition(&error); |
| 305 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
| 306 | std::cerr << "Could not closePartition(" << partition |
| 307 | << "): " << ErrorMessage(status, error) << std::endl; |
| 308 | return EX_SOFTWARE; |
| 309 | } |
| 310 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 311 | status = gsid->closeInstall(&error); |
| 312 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
| 313 | std::cerr << "Could not close DSU installation: " << ErrorMessage(status, error) << "\n"; |
| 314 | return EX_SOFTWARE; |
| 315 | } |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 316 | progress.Finish(); |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 317 | std::string dsuSlot; |
| 318 | status = gsid->getActiveDsuSlot(&dsuSlot); |
| 319 | if (!status.isOk()) { |
| 320 | std::cerr << "Could not get the active DSU slot: " << ErrorMessage(status) << "\n"; |
| 321 | return EX_SOFTWARE; |
| 322 | } |
| 323 | status = gsid->enableGsi(true, dsuSlot, &error); |
David Anderson | 868dea5 | 2019-01-17 13:34:38 -0800 | [diff] [blame] | 324 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
David Anderson | 646b4b2 | 2019-02-27 18:26:54 -0800 | [diff] [blame] | 325 | std::cerr << "Could not make live image bootable: " << ErrorMessage(status, error) << "\n"; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 326 | return EX_SOFTWARE; |
| 327 | } |
David Anderson | b33f2b1 | 2019-01-22 14:14:10 -0800 | [diff] [blame] | 328 | |
| 329 | if (reboot) { |
| 330 | if (!android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,adb")) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 331 | std::cerr << "Failed to reboot automatically" << std::endl; |
David Anderson | b33f2b1 | 2019-01-22 14:14:10 -0800 | [diff] [blame] | 332 | return EX_SOFTWARE; |
| 333 | } |
| 334 | } else { |
| 335 | std::cout << "Please reboot to use the GSI." << std::endl; |
| 336 | } |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
Yo Chiang | c443f4f | 2020-10-21 21:49:20 +0800 | [diff] [blame] | 340 | // Experimental API |
| 341 | static int CreatePartition(sp<IGsiService> gsid, int argc, char** argv) { |
| 342 | std::string installDir; |
| 343 | std::string partitionName; |
| 344 | bool readOnly = true; |
| 345 | int64_t partitionSize = 0; |
| 346 | |
| 347 | struct option options[] = { |
| 348 | {"install-dir", required_argument, nullptr, 'i'}, |
| 349 | {"partition-name", required_argument, nullptr, 'p'}, |
| 350 | {"readwrite", no_argument, nullptr, 'r'}, |
| 351 | {"size", required_argument, nullptr, 's'}, |
| 352 | {nullptr, 0, nullptr, 0}, |
| 353 | }; |
| 354 | |
| 355 | int rv = 0; |
| 356 | while ((rv = getopt_long_only(argc, argv, "", options, nullptr)) != -1) { |
| 357 | switch (rv) { |
| 358 | case 'i': |
| 359 | installDir = optarg; |
| 360 | break; |
| 361 | case 'p': |
| 362 | partitionName = optarg; |
| 363 | break; |
| 364 | case 'r': |
| 365 | readOnly = false; |
| 366 | break; |
| 367 | case 's': |
| 368 | if (!android::base::ParseInt(optarg, &partitionSize)) { |
| 369 | std::cerr << "Could not parse partition size: " << optarg << std::endl; |
| 370 | return EX_USAGE; |
| 371 | } |
| 372 | break; |
| 373 | default: |
| 374 | return EX_USAGE; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | if (getuid() != 0) { |
| 379 | std::cerr << "must be root to install a DSU" << std::endl; |
| 380 | return EX_NOPERM; |
| 381 | } |
| 382 | |
| 383 | bool gsiRunning = false; |
| 384 | auto status = gsid->isGsiRunning(&gsiRunning); |
| 385 | if (!status.isOk()) { |
| 386 | std::cerr << "Could not get DSU running status: " << ErrorMessage(status) << std::endl; |
| 387 | return EX_SOFTWARE; |
| 388 | } |
| 389 | if (gsiRunning) { |
| 390 | std::cerr << "Could not install DSU within an active DSU." << std::endl; |
| 391 | return EX_SOFTWARE; |
| 392 | } |
| 393 | |
| 394 | if (partitionSize <= 0) { |
| 395 | std::cerr << "Partition size must be greater than zero: " << partitionSize << std::endl; |
| 396 | return EX_USAGE; |
| 397 | } |
| 398 | |
| 399 | // Note: the progress bar needs to be re-started in between each call. |
| 400 | ProgressBar progress(gsid); |
| 401 | progress.Display(); |
| 402 | |
| 403 | int error; |
| 404 | status = gsid->openInstall(installDir, &error); |
| 405 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
| 406 | std::cerr << "Could not open DSU installation: " << ErrorMessage(status, error) |
| 407 | << std::endl; |
| 408 | return EX_SOFTWARE; |
| 409 | } |
| 410 | |
| 411 | status = gsid->createPartition(partitionName, partitionSize, readOnly, &error); |
| 412 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
| 413 | std::cerr << "Could not create DSU partition: " << ErrorMessage(status, error) << std::endl; |
| 414 | return EX_SOFTWARE; |
| 415 | } |
| 416 | |
| 417 | if (readOnly) { |
| 418 | android::base::unique_fd input(dup(STDIN_FILENO)); |
| 419 | if (input < 0) { |
| 420 | std::cerr << "Error duplicating descriptor: " << strerror(errno) << std::endl; |
| 421 | return EX_SOFTWARE; |
| 422 | } |
| 423 | android::os::ParcelFileDescriptor stream(std::move(input)); |
| 424 | |
| 425 | bool ok = false; |
| 426 | status = gsid->commitGsiChunkFromStream(stream, partitionSize, &ok); |
| 427 | if (!ok) { |
| 428 | std::cerr << "Could not commit data from stdin: " << ErrorMessage(status) << std::endl; |
| 429 | return EX_SOFTWARE; |
| 430 | } |
| 431 | } |
| 432 | |
Yo Chiang | f9f5dbb | 2020-12-28 20:14:34 +0800 | [diff] [blame] | 433 | status = gsid->closePartition(&error); |
| 434 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
| 435 | std::cerr << "Could not close DSU partition:" << ErrorMessage(status, error) << std::endl; |
| 436 | return EX_SOFTWARE; |
| 437 | } |
| 438 | |
Yo Chiang | c443f4f | 2020-10-21 21:49:20 +0800 | [diff] [blame] | 439 | status = gsid->closeInstall(&error); |
| 440 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
| 441 | std::cerr << "Could not close DSU installation: " << ErrorMessage(status, error) |
| 442 | << std::endl; |
| 443 | return EX_SOFTWARE; |
| 444 | } |
| 445 | |
| 446 | progress.Finish(); |
| 447 | |
| 448 | std::string dsuSlot; |
| 449 | status = gsid->getActiveDsuSlot(&dsuSlot); |
| 450 | if (!status.isOk()) { |
| 451 | std::cerr << "Could not get the active DSU slot: " << ErrorMessage(status) << std::endl; |
| 452 | return EX_SOFTWARE; |
| 453 | } |
| 454 | |
| 455 | // Immediately enable DSU after a partition is installed to ensure the installation status file |
| 456 | // is created. |
| 457 | status = gsid->enableGsi(/* one_shot = */ true, dsuSlot, &error); |
| 458 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
| 459 | std::cerr << "Could not make DSU bootable: " << ErrorMessage(status, error) << std::endl; |
| 460 | return EX_SOFTWARE; |
| 461 | } |
| 462 | |
| 463 | std::cout << "Enabled DSU slot: " << dsuSlot << std::endl; |
| 464 | std::cout << "Please reboot to use the DSU." << std::endl; |
| 465 | return 0; |
| 466 | } |
| 467 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 468 | static int Wipe(sp<IGsiService> gsid, int argc, char** /* argv */) { |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 469 | if (argc > 1) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 470 | std::cerr << "Unrecognized arguments to wipe." << std::endl; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 471 | return EX_USAGE; |
| 472 | } |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 473 | bool ok; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 474 | auto status = gsid->removeGsi(&ok); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 475 | if (!status.isOk() || !ok) { |
David Anderson | 646b4b2 | 2019-02-27 18:26:54 -0800 | [diff] [blame] | 476 | std::cerr << "Could not remove GSI install: " << ErrorMessage(status) << "\n"; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 477 | return EX_SOFTWARE; |
| 478 | } |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 479 | |
| 480 | bool running = false; |
| 481 | if (gsid->isGsiRunning(&running).isOk() && running) { |
| 482 | std::cout << "Live image install will be removed next reboot." << std::endl; |
| 483 | } else { |
| 484 | std::cout << "Live image install successfully removed." << std::endl; |
| 485 | } |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 489 | static int WipeData(sp<IGsiService> gsid, int argc, char** /* argv */) { |
| 490 | if (argc > 1) { |
| 491 | std::cerr << "Unrecognized arguments to wipe-data.\n"; |
| 492 | return EX_USAGE; |
| 493 | } |
| 494 | |
| 495 | bool running; |
| 496 | auto status = gsid->isGsiRunning(&running); |
| 497 | if (!status.isOk()) { |
| 498 | std::cerr << "error: " << status.exceptionMessage().string() << std::endl; |
| 499 | return EX_SOFTWARE; |
| 500 | } |
| 501 | if (running) { |
| 502 | std::cerr << "Cannot wipe GSI userdata while running a GSI.\n"; |
| 503 | return EX_USAGE; |
| 504 | } |
| 505 | |
| 506 | bool installed; |
| 507 | status = gsid->isGsiInstalled(&installed); |
| 508 | if (!status.isOk()) { |
| 509 | std::cerr << "error: " << status.exceptionMessage().string() << std::endl; |
| 510 | return EX_SOFTWARE; |
| 511 | } |
| 512 | if (!installed) { |
| 513 | std::cerr << "No GSI is installed.\n"; |
| 514 | return EX_USAGE; |
| 515 | } |
| 516 | |
| 517 | int error; |
Howard Chen | 46cc752 | 2020-03-03 13:28:37 +0800 | [diff] [blame] | 518 | status = gsid->zeroPartition("userdata" + std::string(kDsuPostfix), &error); |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 519 | if (!status.isOk() || error) { |
| 520 | std::cerr << "Could not wipe GSI userdata: " << ErrorMessage(status, error) << "\n"; |
| 521 | return EX_SOFTWARE; |
| 522 | } |
| 523 | return 0; |
| 524 | } |
| 525 | |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 526 | static int Status(sp<IGsiService> gsid, int argc, char** /* argv */) { |
| 527 | if (argc > 1) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 528 | std::cerr << "Unrecognized arguments to status." << std::endl; |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 529 | return EX_USAGE; |
| 530 | } |
| 531 | bool running; |
| 532 | auto status = gsid->isGsiRunning(&running); |
| 533 | if (!status.isOk()) { |
David Anderson | 646b4b2 | 2019-02-27 18:26:54 -0800 | [diff] [blame] | 534 | std::cerr << "error: " << status.exceptionMessage().string() << std::endl; |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 535 | return EX_SOFTWARE; |
| 536 | } else if (running) { |
| 537 | std::cout << "running" << std::endl; |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 538 | } |
| 539 | bool installed; |
| 540 | status = gsid->isGsiInstalled(&installed); |
| 541 | if (!status.isOk()) { |
David Anderson | 646b4b2 | 2019-02-27 18:26:54 -0800 | [diff] [blame] | 542 | std::cerr << "error: " << status.exceptionMessage().string() << std::endl; |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 543 | return EX_SOFTWARE; |
| 544 | } else if (installed) { |
| 545 | std::cout << "installed" << std::endl; |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 546 | } |
Howard Chen | 670b306 | 2019-02-26 18:14:47 +0800 | [diff] [blame] | 547 | bool enabled; |
| 548 | status = gsid->isGsiEnabled(&enabled); |
| 549 | if (!status.isOk()) { |
| 550 | std::cerr << status.exceptionMessage().string() << std::endl; |
| 551 | return EX_SOFTWARE; |
| 552 | } else if (running || installed) { |
| 553 | std::cout << (enabled ? "enabled" : "disabled") << std::endl; |
| 554 | } else { |
| 555 | std::cout << "normal" << std::endl; |
| 556 | } |
Howard Chen | 96c31fd | 2019-08-23 17:38:51 +0800 | [diff] [blame] | 557 | if (getuid() != 0) { |
| 558 | return 0; |
| 559 | } |
Howard Chen | 96c31fd | 2019-08-23 17:38:51 +0800 | [diff] [blame] | 560 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 561 | std::vector<std::string> dsu_slots; |
| 562 | status = gsid->getInstalledDsuSlots(&dsu_slots); |
| 563 | if (!status.isOk()) { |
| 564 | std::cerr << status.exceptionMessage().string() << std::endl; |
| 565 | return EX_SOFTWARE; |
| 566 | } |
| 567 | int n = 0; |
| 568 | for (auto&& dsu_slot : dsu_slots) { |
| 569 | std::cout << "[" << n++ << "] " << dsu_slot << std::endl; |
| 570 | sp<IImageService> image_service = nullptr; |
| 571 | status = gsid->openImageService("dsu/" + dsu_slot + "/", &image_service); |
| 572 | if (!status.isOk()) { |
| 573 | std::cerr << "error: " << status.exceptionMessage().string() << std::endl; |
| 574 | return EX_SOFTWARE; |
| 575 | } |
| 576 | std::vector<std::string> images; |
| 577 | status = image_service->getAllBackingImages(&images); |
| 578 | if (!status.isOk()) { |
| 579 | std::cerr << "error: " << status.exceptionMessage().string() << std::endl; |
| 580 | return EX_SOFTWARE; |
| 581 | } |
| 582 | for (auto&& image : images) { |
| 583 | std::cout << "installed: " << image << std::endl; |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 584 | AvbPublicKey public_key; |
| 585 | int err = 0; |
| 586 | status = image_service->getAvbPublicKey(image, &public_key, &err); |
| 587 | std::cout << "AVB public key (sha1): "; |
| 588 | if (!public_key.bytes.empty()) { |
| 589 | for (auto b : public_key.sha1) { |
| 590 | std::cout << StringPrintf("%02x", b & 255); |
| 591 | } |
| 592 | std::cout << std::endl; |
| 593 | } else { |
| 594 | std::cout << "[NONE]" << std::endl; |
| 595 | } |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 596 | } |
Howard Chen | 96c31fd | 2019-08-23 17:38:51 +0800 | [diff] [blame] | 597 | } |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 601 | static int Cancel(sp<IGsiService> gsid, int /* argc */, char** /* argv */) { |
| 602 | bool cancelled = false; |
| 603 | auto status = gsid->cancelGsiInstall(&cancelled); |
| 604 | if (!status.isOk()) { |
| 605 | std::cerr << status.exceptionMessage().string() << std::endl; |
| 606 | return EX_SOFTWARE; |
| 607 | } |
| 608 | if (!cancelled) { |
| 609 | std::cout << "Fail to cancel the installation." << std::endl; |
| 610 | return EX_SOFTWARE; |
| 611 | } |
| 612 | return 0; |
| 613 | } |
| 614 | |
David Anderson | 564a04c | 2019-02-27 13:33:44 -0800 | [diff] [blame] | 615 | static int Enable(sp<IGsiService> gsid, int argc, char** argv) { |
| 616 | bool one_shot = false; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 617 | std::string dsuSlot = {}; |
David Anderson | 564a04c | 2019-02-27 13:33:44 -0800 | [diff] [blame] | 618 | struct option options[] = { |
| 619 | {"single-boot", no_argument, nullptr, 's'}, |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 620 | {"dsuslot", required_argument, nullptr, 'd'}, |
David Anderson | 564a04c | 2019-02-27 13:33:44 -0800 | [diff] [blame] | 621 | {nullptr, 0, nullptr, 0}, |
| 622 | }; |
| 623 | int rv, index; |
| 624 | while ((rv = getopt_long_only(argc, argv, "", options, &index)) != -1) { |
| 625 | switch (rv) { |
| 626 | case 's': |
| 627 | one_shot = true; |
| 628 | break; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 629 | case 'd': |
| 630 | dsuSlot = optarg; |
| 631 | break; |
David Anderson | 564a04c | 2019-02-27 13:33:44 -0800 | [diff] [blame] | 632 | default: |
| 633 | std::cerr << "Unrecognized argument to enable\n"; |
| 634 | return EX_USAGE; |
| 635 | } |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | bool installed = false; |
| 639 | gsid->isGsiInstalled(&installed); |
| 640 | if (!installed) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 641 | std::cerr << "Could not find GSI install to re-enable" << std::endl; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 642 | return EX_SOFTWARE; |
| 643 | } |
| 644 | |
| 645 | bool installing = false; |
| 646 | gsid->isGsiInstallInProgress(&installing); |
| 647 | if (installing) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 648 | std::cerr << "Cannot enable or disable while an installation is in progress." << std::endl; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 649 | return EX_SOFTWARE; |
| 650 | } |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 651 | if (dsuSlot.empty()) { |
| 652 | auto status = gsid->getActiveDsuSlot(&dsuSlot); |
| 653 | if (!status.isOk()) { |
| 654 | std::cerr << "Could not get the active DSU slot: " << ErrorMessage(status) << "\n"; |
| 655 | return EX_SOFTWARE; |
| 656 | } |
| 657 | } |
David Anderson | 868dea5 | 2019-01-17 13:34:38 -0800 | [diff] [blame] | 658 | int error; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 659 | auto status = gsid->enableGsi(one_shot, dsuSlot, &error); |
David Anderson | 868dea5 | 2019-01-17 13:34:38 -0800 | [diff] [blame] | 660 | if (!status.isOk() || error != IGsiService::INSTALL_OK) { |
David Anderson | 646b4b2 | 2019-02-27 18:26:54 -0800 | [diff] [blame] | 661 | std::cerr << "Error re-enabling GSI: " << ErrorMessage(status, error) << "\n"; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 662 | return EX_SOFTWARE; |
| 663 | } |
| 664 | std::cout << "Live image install successfully enabled." << std::endl; |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static int Disable(sp<IGsiService> gsid, int argc, char** /* argv */) { |
| 669 | if (argc > 1) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 670 | std::cerr << "Unrecognized arguments to disable." << std::endl; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 671 | return EX_USAGE; |
| 672 | } |
| 673 | |
| 674 | bool installing = false; |
| 675 | gsid->isGsiInstallInProgress(&installing); |
| 676 | if (installing) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 677 | std::cerr << "Cannot enable or disable while an installation is in progress." << std::endl; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 678 | return EX_SOFTWARE; |
| 679 | } |
| 680 | |
| 681 | bool ok = false; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 682 | gsid->disableGsi(&ok); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 683 | if (!ok) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 684 | std::cerr << "Error disabling GSI" << std::endl; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 685 | return EX_SOFTWARE; |
| 686 | } |
| 687 | std::cout << "Live image install successfully disabled." << std::endl; |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static int usage(int /* argc */, char* argv[]) { |
| 692 | fprintf(stderr, |
| 693 | "%s - command-line tool for installing GSI images.\n" |
| 694 | "\n" |
| 695 | "Usage:\n" |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 696 | " %s <disable|install|wipe|status> [options]\n" |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 697 | "\n" |
| 698 | " disable Disable the currently installed GSI.\n" |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 699 | " enable [-s, --single-boot]\n" |
| 700 | " [-d, --dsuslot slotname]\n" |
David Anderson | 564a04c | 2019-02-27 13:33:44 -0800 | [diff] [blame] | 701 | " Enable a previously disabled GSI.\n" |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 702 | " install Install a new GSI. Specify the image size with\n" |
| 703 | " --gsi-size and the desired userdata size with\n" |
| 704 | " --userdata-size (the latter defaults to 8GiB)\n" |
| 705 | " --wipe (remove old gsi userdata first)\n" |
Howard Chen | f08428e | 2019-01-23 14:54:51 +0800 | [diff] [blame] | 706 | " wipe Completely remove a GSI and its associated data\n" |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 707 | " wipe-data Ensure the GSI's userdata will be formatted\n" |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 708 | " cancel Cancel the installation\n" |
| 709 | " status Show status\n", |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 710 | argv[0], argv[0]); |
| 711 | return EX_USAGE; |
| 712 | } |
| 713 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 714 | int main(int argc, char** argv) { |
David Anderson | 83fdeca | 2019-09-09 17:56:22 -0700 | [diff] [blame] | 715 | android::base::InitLogging(argv, android::base::StdioLogger, android::base::DefaultAborter); |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 716 | |
David Anderson | 83fdeca | 2019-09-09 17:56:22 -0700 | [diff] [blame] | 717 | android::sp<IGsiService> service = GetGsiService(); |
| 718 | if (!service) { |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 719 | return EX_SOFTWARE; |
| 720 | } |
| 721 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 722 | if (1 >= argc) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 723 | std::cerr << "Expected command." << std::endl; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 724 | return EX_USAGE; |
| 725 | } |
| 726 | |
| 727 | std::string command = argv[1]; |
David Anderson | 34502be | 2019-02-01 18:06:20 -0800 | [diff] [blame] | 728 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 729 | auto iter = kCommandMap.find(command); |
| 730 | if (iter == kCommandMap.end()) { |
David Anderson | 550253b | 2019-02-08 17:55:27 -0800 | [diff] [blame] | 731 | std::cerr << "Unrecognized command: " << command << std::endl; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 732 | return usage(argc, argv); |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 733 | } |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 734 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 735 | int rc = iter->second(service, argc - 1, argv + 1); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 736 | return rc; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 737 | } |