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