blob: 4f42a59a4b59540f9e962cf744df35fd77b6c6dd [file] [log] [blame]
Alex Deymo5f528112016-01-27 23:32:36 -08001//
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
Kelvin Zhang8ddadd32023-12-12 12:54:30 -080020#include <chrono>
Alex Deymo5f528112016-01-27 23:32:36 -080021#include <string>
22#include <vector>
23
24#include <base/bind.h>
25#include <base/callback.h>
26#include <base/command_line.h>
27#include <base/logging.h>
28#include <base/strings/string_split.h>
29#include <binder/IServiceManager.h>
Alex Deymo2130ee02016-02-02 18:35:50 -080030#include <binderwrapper/binder_wrapper.h>
Alex Deymo5f528112016-01-27 23:32:36 -080031#include <brillo/binder_watcher.h>
32#include <brillo/daemons/daemon.h>
33#include <brillo/flag_helper.h>
34#include <brillo/message_loops/message_loop.h>
35#include <brillo/syslog_logging.h>
36#include <utils/String16.h>
37#include <utils/StrongPointer.h>
38
39#include "android/os/BnUpdateEngineCallback.h"
40#include "android/os/IUpdateEngine.h"
41#include "update_engine/client_library/include/update_engine/update_status.h"
Alex Deymo5f528112016-01-27 23:32:36 -080042#include "update_engine/common/error_code.h"
Alex Deymoe88e9fe2016-02-03 16:38:00 -080043#include "update_engine/common/error_code_utils.h"
Alex Deymo2130ee02016-02-02 18:35:50 -080044#include "update_engine/update_status_utils.h"
Kelvin Zhangbe1c1802021-06-21 10:03:36 -040045#include "utils/String8.h"
Alex Deymo5f528112016-01-27 23:32:36 -080046
47using android::binder::Status;
48
49namespace chromeos_update_engine {
50namespace internal {
51
52class UpdateEngineClientAndroid : public brillo::Daemon {
53 public:
Amin Hassani7cc8bb02019-01-14 16:29:47 -080054 UpdateEngineClientAndroid(int argc, char** argv) : argc_(argc), argv_(argv) {}
Alex Deymo5f528112016-01-27 23:32:36 -080055
56 int ExitWhenIdle(const Status& status);
57 int ExitWhenIdle(int return_code);
58
59 private:
60 class UECallback : public android::os::BnUpdateEngineCallback {
61 public:
Alex Deymoe88e9fe2016-02-03 16:38:00 -080062 explicit UECallback(UpdateEngineClientAndroid* client) : client_(client) {}
Alex Deymo5f528112016-01-27 23:32:36 -080063
64 // android::os::BnUpdateEngineCallback overrides.
65 Status onStatusUpdate(int status_code, float progress) override;
66 Status onPayloadApplicationComplete(int error_code) override;
67
68 private:
69 UpdateEngineClientAndroid* client_;
70 };
71
72 int OnInit() override;
73
Alex Deymo2130ee02016-02-02 18:35:50 -080074 // Called whenever the UpdateEngine daemon dies.
75 void UpdateEngineServiceDied();
Kelvin Zhangbe1c1802021-06-21 10:03:36 -040076 // Register callback to watch for death notification from update_engine.
77 void RegisterDeathNotification();
Alex Deymo2130ee02016-02-02 18:35:50 -080078
Yifan Hong82cd9d32020-01-10 14:40:25 -080079 static std::vector<android::String16> ParseHeaders(const std::string& arg);
80
Alex Deymo5f528112016-01-27 23:32:36 -080081 // Copy of argc and argv passed to main().
82 int argc_;
83 char** argv_;
84
85 android::sp<android::os::IUpdateEngine> service_;
86 android::sp<android::os::BnUpdateEngineCallback> callback_;
Yifan Hong40bb0d02020-02-24 17:33:14 -080087 android::sp<android::os::BnUpdateEngineCallback> cleanup_callback_;
Alex Deymo5f528112016-01-27 23:32:36 -080088
89 brillo::BinderWatcher binder_watcher_;
90};
91
Amin Hassani7cc8bb02019-01-14 16:29:47 -080092Status UpdateEngineClientAndroid::UECallback::onStatusUpdate(int status_code,
93 float progress) {
Alex Deymo5f528112016-01-27 23:32:36 -080094 update_engine::UpdateStatus status =
95 static_cast<update_engine::UpdateStatus>(status_code);
96 LOG(INFO) << "onStatusUpdate(" << UpdateStatusToString(status) << " ("
97 << status_code << "), " << progress << ")";
98 return Status::ok();
99}
100
101Status UpdateEngineClientAndroid::UECallback::onPayloadApplicationComplete(
102 int error_code) {
103 ErrorCode code = static_cast<ErrorCode>(error_code);
Alex Deymoe88e9fe2016-02-03 16:38:00 -0800104 LOG(INFO) << "onPayloadApplicationComplete(" << utils::ErrorCodeToString(code)
105 << " (" << error_code << "))";
Sen Jiang02c49422017-10-31 15:14:11 -0700106 client_->ExitWhenIdle(
107 (code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive)
108 ? EX_OK
109 : 1);
Alex Deymo5f528112016-01-27 23:32:36 -0800110 return Status::ok();
111}
112
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400113constexpr auto&& UNSPECIFIED_FLAG = "unspecified";
114
115void UpdateEngineClientAndroid::RegisterDeathNotification() {
116 // When following updates status changes, exit if the update_engine daemon
117 // dies.
118 android::BinderWrapper::Create();
119 android::BinderWrapper::Get()->RegisterForDeathNotifications(
120 android::os::IUpdateEngine::asBinder(service_),
Kelvin Zhang2f6c25a2023-07-05 12:49:34 -0700121 [this]() { UpdateEngineServiceDied(); });
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400122}
123
Alex Deymo5f528112016-01-27 23:32:36 -0800124int UpdateEngineClientAndroid::OnInit() {
125 int ret = Daemon::OnInit();
126 if (ret != EX_OK)
127 return ret;
128
129 DEFINE_bool(update, false, "Start a new update, if no update in progress.");
130 DEFINE_string(payload,
131 "http://127.0.0.1:8080/payload",
132 "The URI to the update payload to use.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800133 DEFINE_int64(offset,
134 0,
Alex Deymo95b8f242016-01-28 16:06:57 -0800135 "The offset in the payload where the CrAU update starts. "
136 "Used when --update is passed.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800137 DEFINE_int64(size,
138 0,
Alex Deymo95b8f242016-01-28 16:06:57 -0800139 "The size of the CrAU part of the payload. If 0 is passed, it "
140 "will be autodetected. Used when --update is passed.");
Alex Deymo5f528112016-01-27 23:32:36 -0800141 DEFINE_string(headers,
142 "",
Alex Deymo95b8f242016-01-28 16:06:57 -0800143 "A list of key-value pairs, one element of the list per line. "
Yifan Hong82cd9d32020-01-10 14:40:25 -0800144 "Used when --update or --allocate is passed.");
Alex Deymo5f528112016-01-27 23:32:36 -0800145
Sen Jiang6f67d562018-09-19 14:55:50 -0700146 DEFINE_bool(verify,
147 false,
148 "Given payload metadata, verify if the payload is applicable.");
Yifan Hong82cd9d32020-01-10 14:40:25 -0800149 DEFINE_bool(allocate, false, "Given payload metadata, allocate space.");
Sen Jiang6f67d562018-09-19 14:55:50 -0700150 DEFINE_string(metadata,
151 "/data/ota_package/metadata",
152 "The path to the update payload metadata. "
Yifan Hong82cd9d32020-01-10 14:40:25 -0800153 "Used when --verify or --allocate is passed.");
Sen Jiang6f67d562018-09-19 14:55:50 -0700154
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400155 DEFINE_string(switch_slot,
156 UNSPECIFIED_FLAG,
157 "Perform just the slow switching part of OTA. "
158 "Used to revert a slot switch or re-do slot switch. Valid "
159 "values are 'true' and 'false'");
Alex Deymo5f528112016-01-27 23:32:36 -0800160 DEFINE_bool(suspend, false, "Suspend an ongoing update and exit.");
161 DEFINE_bool(resume, false, "Resume a suspended update.");
162 DEFINE_bool(cancel, false, "Cancel the ongoing update and exit.");
Alex Deymo3b678db2016-02-09 11:50:06 -0800163 DEFINE_bool(reset_status, false, "Reset an already applied update and exit.");
Alex Deymo5f528112016-01-27 23:32:36 -0800164 DEFINE_bool(follow,
165 false,
166 "Follow status update changes until a final state is reached. "
167 "Exit status is 0 if the update succeeded, and 1 otherwise.");
Yifan Honge5a8f232020-01-16 09:37:16 -0800168 DEFINE_bool(merge,
169 false,
170 "Wait for previous update to merge. "
171 "Only available after rebooting to new slot.");
Alex Deymo5f528112016-01-27 23:32:36 -0800172 // Boilerplate init commands.
173 base::CommandLine::Init(argc_, argv_);
174 brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client");
175 if (argc_ == 1) {
176 LOG(ERROR) << "Nothing to do. Run with --help for help.";
177 return 1;
178 }
179
180 // Ensure there are no positional arguments.
181 const std::vector<std::string> positional_args =
182 base::CommandLine::ForCurrentProcess()->GetArgs();
183 if (!positional_args.empty()) {
184 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
185 << "'. If you want to pass a value to a flag, pass it as "
186 "--flag=value.";
187 return 1;
188 }
189
190 bool keep_running = false;
Alex Deymo95224dd2016-01-29 11:18:35 -0800191 brillo::InitLog(brillo::kLogToStderr);
Alex Deymo2130ee02016-02-02 18:35:50 -0800192
193 // Initialize a binder watcher early in the process before any interaction
194 // with the binder driver.
195 binder_watcher_.Init();
196
Alex Deymo5f528112016-01-27 23:32:36 -0800197 android::status_t status = android::getService(
198 android::String16("android.os.UpdateEngineService"), &service_);
199 if (status != android::OK) {
200 LOG(ERROR) << "Failed to get IUpdateEngine binder from service manager: "
201 << Status::fromStatusT(status).toString8();
Alex Deymo2130ee02016-02-02 18:35:50 -0800202 return ExitWhenIdle(1);
Alex Deymo5f528112016-01-27 23:32:36 -0800203 }
204
Kelvin Zhangc56afa32021-08-13 12:32:31 -0700205 // Other commands, such as |setShouldSwitchSlotOnReboot|, might rely on the
206 // follow behavior, so created callback before running these commands.
207 if (FLAGS_follow) {
208 // Register a callback object with the service.
209 callback_ = new UECallback(this);
Kelvin Zhang2f6c25a2023-07-05 12:49:34 -0700210 bool bound = false;
Kelvin Zhangc56afa32021-08-13 12:32:31 -0700211 if (!service_->bind(callback_, &bound).isOk() || !bound) {
212 LOG(ERROR) << "Failed to bind() the UpdateEngine daemon.";
213 return 1;
214 }
215 keep_running = true;
216 }
217
Alex Deymo5f528112016-01-27 23:32:36 -0800218 if (FLAGS_suspend) {
219 return ExitWhenIdle(service_->suspend());
220 }
221
222 if (FLAGS_resume) {
223 return ExitWhenIdle(service_->resume());
224 }
225
226 if (FLAGS_cancel) {
227 return ExitWhenIdle(service_->cancel());
228 }
229
Alex Deymo3b678db2016-02-09 11:50:06 -0800230 if (FLAGS_reset_status) {
231 return ExitWhenIdle(service_->resetStatus());
232 }
233
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400234 if (FLAGS_switch_slot != UNSPECIFIED_FLAG) {
235 if (FLAGS_switch_slot != "true" && FLAGS_switch_slot != "false") {
236 LOG(ERROR) << "--switch_slot should be either true or false, got "
237 << FLAGS_switch_slot;
238 return 1;
239 }
240 const bool should_switch = FLAGS_switch_slot == "true";
241 ::android::binder::Status status;
242 if (should_switch) {
243 status = service_->setShouldSwitchSlotOnReboot(
244 android::String16(FLAGS_metadata.c_str(), FLAGS_metadata.size()));
Kelvin Zhangc56afa32021-08-13 12:32:31 -0700245 if (!FLAGS_follow) {
246 return ExitWhenIdle(status);
247 }
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400248 } else {
Kelvin Zhangc56afa32021-08-13 12:32:31 -0700249 // resetShouldSwitchSlotOnReboot() is a synchronous call, no need to
250 // follow
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400251 status = service_->resetShouldSwitchSlotOnReboot();
Kelvin Zhangc56afa32021-08-13 12:32:31 -0700252 return ExitWhenIdle(status);
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400253 }
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400254 }
255
Sen Jiang6f67d562018-09-19 14:55:50 -0700256 if (FLAGS_verify) {
257 bool applicable = false;
258 Status status = service_->verifyPayloadApplicable(
259 android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()},
260 &applicable);
261 LOG(INFO) << "Payload is " << (applicable ? "" : "not ") << "applicable.";
262 return ExitWhenIdle(status);
263 }
264
Yifan Hong82cd9d32020-01-10 14:40:25 -0800265 if (FLAGS_allocate) {
266 auto headers = ParseHeaders(FLAGS_headers);
267 int64_t ret = 0;
268 Status status = service_->allocateSpaceForPayload(
269 android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()},
270 headers,
271 &ret);
272 if (status.isOk()) {
273 if (ret == 0) {
274 LOG(INFO) << "Successfully allocated space for payload.";
275 } else {
276 LOG(INFO) << "Insufficient space; required " << ret << " bytes.";
277 }
278 } else {
279 LOG(INFO) << "Allocation failed.";
280 }
281 return ExitWhenIdle(status);
282 }
283
Yifan Honge5a8f232020-01-16 09:37:16 -0800284 if (FLAGS_merge) {
Yifan Hong40bb0d02020-02-24 17:33:14 -0800285 // Register a callback object with the service.
286 cleanup_callback_ = new UECallback(this);
287 Status status = service_->cleanupSuccessfulUpdate(cleanup_callback_);
288 if (!status.isOk()) {
289 LOG(ERROR) << "Failed to call cleanupSuccessfulUpdate.";
290 return ExitWhenIdle(status);
Yifan Honge5a8f232020-01-16 09:37:16 -0800291 }
Yifan Hong40bb0d02020-02-24 17:33:14 -0800292 keep_running = true;
Yifan Honge5a8f232020-01-16 09:37:16 -0800293 }
294
Alex Deymo5f528112016-01-27 23:32:36 -0800295 if (FLAGS_update) {
Yifan Hong82cd9d32020-01-10 14:40:25 -0800296 auto and_headers = ParseHeaders(FLAGS_headers);
Alex Deymo5f528112016-01-27 23:32:36 -0800297 Status status = service_->applyPayload(
298 android::String16{FLAGS_payload.data(), FLAGS_payload.size()},
Alex Deymo95b8f242016-01-28 16:06:57 -0800299 FLAGS_offset,
300 FLAGS_size,
Alex Deymo5f528112016-01-27 23:32:36 -0800301 and_headers);
302 if (!status.isOk())
303 return ExitWhenIdle(status);
304 }
305
306 if (!keep_running)
307 return ExitWhenIdle(EX_OK);
308
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400309 RegisterDeathNotification();
Alex Deymo5f528112016-01-27 23:32:36 -0800310 return EX_OK;
311}
312
313int UpdateEngineClientAndroid::ExitWhenIdle(const Status& status) {
314 if (status.isOk())
315 return ExitWhenIdle(EX_OK);
316 LOG(ERROR) << status.toString8();
317 return ExitWhenIdle(status.exceptionCode());
318}
319
320int UpdateEngineClientAndroid::ExitWhenIdle(int return_code) {
321 auto delayed_exit = base::Bind(
322 &Daemon::QuitWithExitCode, base::Unretained(this), return_code);
323 if (!brillo::MessageLoop::current()->PostTask(delayed_exit))
324 return 1;
325 return EX_OK;
326}
327
Alex Deymo2130ee02016-02-02 18:35:50 -0800328void UpdateEngineClientAndroid::UpdateEngineServiceDied() {
329 LOG(ERROR) << "UpdateEngineService died.";
330 QuitWithExitCode(1);
331}
332
Yifan Hong82cd9d32020-01-10 14:40:25 -0800333std::vector<android::String16> UpdateEngineClientAndroid::ParseHeaders(
334 const std::string& arg) {
335 std::vector<std::string> headers = base::SplitString(
336 arg, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
337 std::vector<android::String16> and_headers;
338 for (const auto& header : headers) {
339 and_headers.push_back(android::String16{header.data(), header.size()});
340 }
341 return and_headers;
342}
343
Alex Deymo5f528112016-01-27 23:32:36 -0800344} // namespace internal
345} // namespace chromeos_update_engine
346
347int main(int argc, char** argv) {
Kelvin Zhang8ddadd32023-12-12 12:54:30 -0800348 const auto start = std::chrono::system_clock::now();
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800349 chromeos_update_engine::internal::UpdateEngineClientAndroid client(argc,
350 argv);
Kelvin Zhang8ddadd32023-12-12 12:54:30 -0800351 const auto ret = client.Run();
352 const auto end = std::chrono::system_clock::now();
353 const auto duration = end - start;
354 LOG(INFO)
355 << "Command took "
356 << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count()
357 << " ms";
358 return ret;
Alex Deymo5f528112016-01-27 23:32:36 -0800359}