Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2016 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 <sysexits.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <base/bind.h> |
| 24 | #include <base/callback.h> |
| 25 | #include <base/command_line.h> |
| 26 | #include <base/logging.h> |
| 27 | #include <base/strings/string_split.h> |
| 28 | #include <binder/IServiceManager.h> |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 29 | #include <binderwrapper/binder_wrapper.h> |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 30 | #include <brillo/binder_watcher.h> |
| 31 | #include <brillo/daemons/daemon.h> |
| 32 | #include <brillo/flag_helper.h> |
| 33 | #include <brillo/message_loops/message_loop.h> |
| 34 | #include <brillo/syslog_logging.h> |
| 35 | #include <utils/String16.h> |
| 36 | #include <utils/StrongPointer.h> |
| 37 | |
| 38 | #include "android/os/BnUpdateEngineCallback.h" |
| 39 | #include "android/os/IUpdateEngine.h" |
| 40 | #include "update_engine/client_library/include/update_engine/update_status.h" |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 41 | #include "update_engine/common/error_code.h" |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 42 | #include "update_engine/common/error_code_utils.h" |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 43 | #include "update_engine/update_status_utils.h" |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 44 | |
| 45 | using android::binder::Status; |
| 46 | |
| 47 | namespace chromeos_update_engine { |
| 48 | namespace internal { |
| 49 | |
| 50 | class UpdateEngineClientAndroid : public brillo::Daemon { |
| 51 | public: |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 52 | UpdateEngineClientAndroid(int argc, char** argv) : argc_(argc), argv_(argv) {} |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 53 | |
| 54 | int ExitWhenIdle(const Status& status); |
| 55 | int ExitWhenIdle(int return_code); |
| 56 | |
| 57 | private: |
| 58 | class UECallback : public android::os::BnUpdateEngineCallback { |
| 59 | public: |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 60 | explicit UECallback(UpdateEngineClientAndroid* client) : client_(client) {} |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 61 | |
| 62 | // android::os::BnUpdateEngineCallback overrides. |
| 63 | Status onStatusUpdate(int status_code, float progress) override; |
| 64 | Status onPayloadApplicationComplete(int error_code) override; |
| 65 | |
| 66 | private: |
| 67 | UpdateEngineClientAndroid* client_; |
| 68 | }; |
| 69 | |
| 70 | int OnInit() override; |
| 71 | |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 72 | // Called whenever the UpdateEngine daemon dies. |
| 73 | void UpdateEngineServiceDied(); |
| 74 | |
Yifan Hong | 82cd9d3 | 2020-01-10 14:40:25 -0800 | [diff] [blame] | 75 | static std::vector<android::String16> ParseHeaders(const std::string& arg); |
| 76 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 77 | // Copy of argc and argv passed to main(). |
| 78 | int argc_; |
| 79 | char** argv_; |
| 80 | |
| 81 | android::sp<android::os::IUpdateEngine> service_; |
| 82 | android::sp<android::os::BnUpdateEngineCallback> callback_; |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 83 | android::sp<android::os::BnUpdateEngineCallback> cleanup_callback_; |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 84 | |
| 85 | brillo::BinderWatcher binder_watcher_; |
| 86 | }; |
| 87 | |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 88 | Status UpdateEngineClientAndroid::UECallback::onStatusUpdate(int status_code, |
| 89 | float progress) { |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 90 | update_engine::UpdateStatus status = |
| 91 | static_cast<update_engine::UpdateStatus>(status_code); |
| 92 | LOG(INFO) << "onStatusUpdate(" << UpdateStatusToString(status) << " (" |
| 93 | << status_code << "), " << progress << ")"; |
| 94 | return Status::ok(); |
| 95 | } |
| 96 | |
| 97 | Status UpdateEngineClientAndroid::UECallback::onPayloadApplicationComplete( |
| 98 | int error_code) { |
| 99 | ErrorCode code = static_cast<ErrorCode>(error_code); |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 100 | LOG(INFO) << "onPayloadApplicationComplete(" << utils::ErrorCodeToString(code) |
| 101 | << " (" << error_code << "))"; |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 102 | client_->ExitWhenIdle( |
| 103 | (code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive) |
| 104 | ? EX_OK |
| 105 | : 1); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 106 | return Status::ok(); |
| 107 | } |
| 108 | |
| 109 | int UpdateEngineClientAndroid::OnInit() { |
| 110 | int ret = Daemon::OnInit(); |
| 111 | if (ret != EX_OK) |
| 112 | return ret; |
| 113 | |
| 114 | DEFINE_bool(update, false, "Start a new update, if no update in progress."); |
| 115 | DEFINE_string(payload, |
| 116 | "http://127.0.0.1:8080/payload", |
| 117 | "The URI to the update payload to use."); |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 118 | DEFINE_int64(offset, |
| 119 | 0, |
Alex Deymo | 95b8f24 | 2016-01-28 16:06:57 -0800 | [diff] [blame] | 120 | "The offset in the payload where the CrAU update starts. " |
| 121 | "Used when --update is passed."); |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 122 | DEFINE_int64(size, |
| 123 | 0, |
Alex Deymo | 95b8f24 | 2016-01-28 16:06:57 -0800 | [diff] [blame] | 124 | "The size of the CrAU part of the payload. If 0 is passed, it " |
| 125 | "will be autodetected. Used when --update is passed."); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 126 | DEFINE_string(headers, |
| 127 | "", |
Alex Deymo | 95b8f24 | 2016-01-28 16:06:57 -0800 | [diff] [blame] | 128 | "A list of key-value pairs, one element of the list per line. " |
Yifan Hong | 82cd9d3 | 2020-01-10 14:40:25 -0800 | [diff] [blame] | 129 | "Used when --update or --allocate is passed."); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 130 | |
Sen Jiang | 6f67d56 | 2018-09-19 14:55:50 -0700 | [diff] [blame] | 131 | DEFINE_bool(verify, |
| 132 | false, |
| 133 | "Given payload metadata, verify if the payload is applicable."); |
Yifan Hong | 82cd9d3 | 2020-01-10 14:40:25 -0800 | [diff] [blame] | 134 | DEFINE_bool(allocate, false, "Given payload metadata, allocate space."); |
Sen Jiang | 6f67d56 | 2018-09-19 14:55:50 -0700 | [diff] [blame] | 135 | DEFINE_string(metadata, |
| 136 | "/data/ota_package/metadata", |
| 137 | "The path to the update payload metadata. " |
Yifan Hong | 82cd9d3 | 2020-01-10 14:40:25 -0800 | [diff] [blame] | 138 | "Used when --verify or --allocate is passed."); |
Sen Jiang | 6f67d56 | 2018-09-19 14:55:50 -0700 | [diff] [blame] | 139 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 140 | DEFINE_bool(suspend, false, "Suspend an ongoing update and exit."); |
| 141 | DEFINE_bool(resume, false, "Resume a suspended update."); |
| 142 | DEFINE_bool(cancel, false, "Cancel the ongoing update and exit."); |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 143 | DEFINE_bool(reset_status, false, "Reset an already applied update and exit."); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 144 | DEFINE_bool(follow, |
| 145 | false, |
| 146 | "Follow status update changes until a final state is reached. " |
| 147 | "Exit status is 0 if the update succeeded, and 1 otherwise."); |
Yifan Hong | e5a8f23 | 2020-01-16 09:37:16 -0800 | [diff] [blame] | 148 | DEFINE_bool(merge, |
| 149 | false, |
| 150 | "Wait for previous update to merge. " |
| 151 | "Only available after rebooting to new slot."); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 152 | // Boilerplate init commands. |
| 153 | base::CommandLine::Init(argc_, argv_); |
| 154 | brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client"); |
| 155 | if (argc_ == 1) { |
| 156 | LOG(ERROR) << "Nothing to do. Run with --help for help."; |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | // Ensure there are no positional arguments. |
| 161 | const std::vector<std::string> positional_args = |
| 162 | base::CommandLine::ForCurrentProcess()->GetArgs(); |
| 163 | if (!positional_args.empty()) { |
| 164 | LOG(ERROR) << "Found a positional argument '" << positional_args.front() |
| 165 | << "'. If you want to pass a value to a flag, pass it as " |
| 166 | "--flag=value."; |
| 167 | return 1; |
| 168 | } |
| 169 | |
| 170 | bool keep_running = false; |
Alex Deymo | 95224dd | 2016-01-29 11:18:35 -0800 | [diff] [blame] | 171 | brillo::InitLog(brillo::kLogToStderr); |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 172 | |
| 173 | // Initialize a binder watcher early in the process before any interaction |
| 174 | // with the binder driver. |
| 175 | binder_watcher_.Init(); |
| 176 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 177 | android::status_t status = android::getService( |
| 178 | android::String16("android.os.UpdateEngineService"), &service_); |
| 179 | if (status != android::OK) { |
| 180 | LOG(ERROR) << "Failed to get IUpdateEngine binder from service manager: " |
| 181 | << Status::fromStatusT(status).toString8(); |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 182 | return ExitWhenIdle(1); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (FLAGS_suspend) { |
| 186 | return ExitWhenIdle(service_->suspend()); |
| 187 | } |
| 188 | |
| 189 | if (FLAGS_resume) { |
| 190 | return ExitWhenIdle(service_->resume()); |
| 191 | } |
| 192 | |
| 193 | if (FLAGS_cancel) { |
| 194 | return ExitWhenIdle(service_->cancel()); |
| 195 | } |
| 196 | |
Alex Deymo | 3b678db | 2016-02-09 11:50:06 -0800 | [diff] [blame] | 197 | if (FLAGS_reset_status) { |
| 198 | return ExitWhenIdle(service_->resetStatus()); |
| 199 | } |
| 200 | |
Sen Jiang | 6f67d56 | 2018-09-19 14:55:50 -0700 | [diff] [blame] | 201 | if (FLAGS_verify) { |
| 202 | bool applicable = false; |
| 203 | Status status = service_->verifyPayloadApplicable( |
| 204 | android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()}, |
| 205 | &applicable); |
| 206 | LOG(INFO) << "Payload is " << (applicable ? "" : "not ") << "applicable."; |
| 207 | return ExitWhenIdle(status); |
| 208 | } |
| 209 | |
Yifan Hong | 82cd9d3 | 2020-01-10 14:40:25 -0800 | [diff] [blame] | 210 | if (FLAGS_allocate) { |
| 211 | auto headers = ParseHeaders(FLAGS_headers); |
| 212 | int64_t ret = 0; |
| 213 | Status status = service_->allocateSpaceForPayload( |
| 214 | android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()}, |
| 215 | headers, |
| 216 | &ret); |
| 217 | if (status.isOk()) { |
| 218 | if (ret == 0) { |
| 219 | LOG(INFO) << "Successfully allocated space for payload."; |
| 220 | } else { |
| 221 | LOG(INFO) << "Insufficient space; required " << ret << " bytes."; |
| 222 | } |
| 223 | } else { |
| 224 | LOG(INFO) << "Allocation failed."; |
| 225 | } |
| 226 | return ExitWhenIdle(status); |
| 227 | } |
| 228 | |
Yifan Hong | e5a8f23 | 2020-01-16 09:37:16 -0800 | [diff] [blame] | 229 | if (FLAGS_merge) { |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 230 | // Register a callback object with the service. |
| 231 | cleanup_callback_ = new UECallback(this); |
| 232 | Status status = service_->cleanupSuccessfulUpdate(cleanup_callback_); |
| 233 | if (!status.isOk()) { |
| 234 | LOG(ERROR) << "Failed to call cleanupSuccessfulUpdate."; |
| 235 | return ExitWhenIdle(status); |
Yifan Hong | e5a8f23 | 2020-01-16 09:37:16 -0800 | [diff] [blame] | 236 | } |
Yifan Hong | 40bb0d0 | 2020-02-24 17:33:14 -0800 | [diff] [blame] | 237 | keep_running = true; |
Yifan Hong | e5a8f23 | 2020-01-16 09:37:16 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 240 | if (FLAGS_follow) { |
| 241 | // Register a callback object with the service. |
| 242 | callback_ = new UECallback(this); |
| 243 | bool bound; |
| 244 | if (!service_->bind(callback_, &bound).isOk() || !bound) { |
| 245 | LOG(ERROR) << "Failed to bind() the UpdateEngine daemon."; |
| 246 | return 1; |
| 247 | } |
| 248 | keep_running = true; |
| 249 | } |
| 250 | |
| 251 | if (FLAGS_update) { |
Yifan Hong | 82cd9d3 | 2020-01-10 14:40:25 -0800 | [diff] [blame] | 252 | auto and_headers = ParseHeaders(FLAGS_headers); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 253 | Status status = service_->applyPayload( |
| 254 | android::String16{FLAGS_payload.data(), FLAGS_payload.size()}, |
Alex Deymo | 95b8f24 | 2016-01-28 16:06:57 -0800 | [diff] [blame] | 255 | FLAGS_offset, |
| 256 | FLAGS_size, |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 257 | and_headers); |
| 258 | if (!status.isOk()) |
| 259 | return ExitWhenIdle(status); |
| 260 | } |
| 261 | |
| 262 | if (!keep_running) |
| 263 | return ExitWhenIdle(EX_OK); |
| 264 | |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 265 | // When following updates status changes, exit if the update_engine daemon |
| 266 | // dies. |
| 267 | android::BinderWrapper::Create(); |
| 268 | android::BinderWrapper::Get()->RegisterForDeathNotifications( |
| 269 | android::os::IUpdateEngine::asBinder(service_), |
| 270 | base::Bind(&UpdateEngineClientAndroid::UpdateEngineServiceDied, |
| 271 | base::Unretained(this))); |
| 272 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 273 | return EX_OK; |
| 274 | } |
| 275 | |
| 276 | int UpdateEngineClientAndroid::ExitWhenIdle(const Status& status) { |
| 277 | if (status.isOk()) |
| 278 | return ExitWhenIdle(EX_OK); |
| 279 | LOG(ERROR) << status.toString8(); |
| 280 | return ExitWhenIdle(status.exceptionCode()); |
| 281 | } |
| 282 | |
| 283 | int UpdateEngineClientAndroid::ExitWhenIdle(int return_code) { |
| 284 | auto delayed_exit = base::Bind( |
| 285 | &Daemon::QuitWithExitCode, base::Unretained(this), return_code); |
| 286 | if (!brillo::MessageLoop::current()->PostTask(delayed_exit)) |
| 287 | return 1; |
| 288 | return EX_OK; |
| 289 | } |
| 290 | |
Alex Deymo | 2130ee0 | 2016-02-02 18:35:50 -0800 | [diff] [blame] | 291 | void UpdateEngineClientAndroid::UpdateEngineServiceDied() { |
| 292 | LOG(ERROR) << "UpdateEngineService died."; |
| 293 | QuitWithExitCode(1); |
| 294 | } |
| 295 | |
Yifan Hong | 82cd9d3 | 2020-01-10 14:40:25 -0800 | [diff] [blame] | 296 | std::vector<android::String16> UpdateEngineClientAndroid::ParseHeaders( |
| 297 | const std::string& arg) { |
| 298 | std::vector<std::string> headers = base::SplitString( |
| 299 | arg, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 300 | std::vector<android::String16> and_headers; |
| 301 | for (const auto& header : headers) { |
| 302 | and_headers.push_back(android::String16{header.data(), header.size()}); |
| 303 | } |
| 304 | return and_headers; |
| 305 | } |
| 306 | |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 307 | } // namespace internal |
| 308 | } // namespace chromeos_update_engine |
| 309 | |
| 310 | int main(int argc, char** argv) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 311 | chromeos_update_engine::internal::UpdateEngineClientAndroid client(argc, |
| 312 | argv); |
Alex Deymo | 5f52811 | 2016-01-27 23:32:36 -0800 | [diff] [blame] | 313 | return client.Run(); |
| 314 | } |