blob: 8628463bb2a04ac49b1afca3df1e8af192f42eb6 [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
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 Deymo2130ee02016-02-02 18:35:50 -080029#include <binderwrapper/binder_wrapper.h>
Alex Deymo5f528112016-01-27 23:32:36 -080030#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 Deymo5f528112016-01-27 23:32:36 -080041#include "update_engine/common/error_code.h"
Alex Deymoe88e9fe2016-02-03 16:38:00 -080042#include "update_engine/common/error_code_utils.h"
Alex Deymo2130ee02016-02-02 18:35:50 -080043#include "update_engine/update_status_utils.h"
Kelvin Zhangbe1c1802021-06-21 10:03:36 -040044#include "utils/String8.h"
Alex Deymo5f528112016-01-27 23:32:36 -080045
46using android::binder::Status;
47
48namespace chromeos_update_engine {
49namespace internal {
50
51class UpdateEngineClientAndroid : public brillo::Daemon {
52 public:
Amin Hassani7cc8bb02019-01-14 16:29:47 -080053 UpdateEngineClientAndroid(int argc, char** argv) : argc_(argc), argv_(argv) {}
Alex Deymo5f528112016-01-27 23:32:36 -080054
55 int ExitWhenIdle(const Status& status);
56 int ExitWhenIdle(int return_code);
57
58 private:
59 class UECallback : public android::os::BnUpdateEngineCallback {
60 public:
Alex Deymoe88e9fe2016-02-03 16:38:00 -080061 explicit UECallback(UpdateEngineClientAndroid* client) : client_(client) {}
Alex Deymo5f528112016-01-27 23:32:36 -080062
63 // android::os::BnUpdateEngineCallback overrides.
64 Status onStatusUpdate(int status_code, float progress) override;
65 Status onPayloadApplicationComplete(int error_code) override;
66
67 private:
68 UpdateEngineClientAndroid* client_;
69 };
70
71 int OnInit() override;
72
Alex Deymo2130ee02016-02-02 18:35:50 -080073 // Called whenever the UpdateEngine daemon dies.
74 void UpdateEngineServiceDied();
Kelvin Zhangbe1c1802021-06-21 10:03:36 -040075 // Register callback to watch for death notification from update_engine.
76 void RegisterDeathNotification();
Alex Deymo2130ee02016-02-02 18:35:50 -080077
Yifan Hong82cd9d32020-01-10 14:40:25 -080078 static std::vector<android::String16> ParseHeaders(const std::string& arg);
79
Alex Deymo5f528112016-01-27 23:32:36 -080080 // Copy of argc and argv passed to main().
81 int argc_;
82 char** argv_;
83
84 android::sp<android::os::IUpdateEngine> service_;
85 android::sp<android::os::BnUpdateEngineCallback> callback_;
Yifan Hong40bb0d02020-02-24 17:33:14 -080086 android::sp<android::os::BnUpdateEngineCallback> cleanup_callback_;
Alex Deymo5f528112016-01-27 23:32:36 -080087
88 brillo::BinderWatcher binder_watcher_;
89};
90
Amin Hassani7cc8bb02019-01-14 16:29:47 -080091Status UpdateEngineClientAndroid::UECallback::onStatusUpdate(int status_code,
92 float progress) {
Alex Deymo5f528112016-01-27 23:32:36 -080093 update_engine::UpdateStatus status =
94 static_cast<update_engine::UpdateStatus>(status_code);
95 LOG(INFO) << "onStatusUpdate(" << UpdateStatusToString(status) << " ("
96 << status_code << "), " << progress << ")";
97 return Status::ok();
98}
99
100Status UpdateEngineClientAndroid::UECallback::onPayloadApplicationComplete(
101 int error_code) {
102 ErrorCode code = static_cast<ErrorCode>(error_code);
Alex Deymoe88e9fe2016-02-03 16:38:00 -0800103 LOG(INFO) << "onPayloadApplicationComplete(" << utils::ErrorCodeToString(code)
104 << " (" << error_code << "))";
Sen Jiang02c49422017-10-31 15:14:11 -0700105 client_->ExitWhenIdle(
106 (code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive)
107 ? EX_OK
108 : 1);
Alex Deymo5f528112016-01-27 23:32:36 -0800109 return Status::ok();
110}
111
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400112constexpr auto&& UNSPECIFIED_FLAG = "unspecified";
113
114void UpdateEngineClientAndroid::RegisterDeathNotification() {
115 // When following updates status changes, exit if the update_engine daemon
116 // dies.
117 android::BinderWrapper::Create();
118 android::BinderWrapper::Get()->RegisterForDeathNotifications(
119 android::os::IUpdateEngine::asBinder(service_),
120 base::Bind(&UpdateEngineClientAndroid::UpdateEngineServiceDied,
121 base::Unretained(this)));
122}
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
205 if (FLAGS_suspend) {
206 return ExitWhenIdle(service_->suspend());
207 }
208
209 if (FLAGS_resume) {
210 return ExitWhenIdle(service_->resume());
211 }
212
213 if (FLAGS_cancel) {
214 return ExitWhenIdle(service_->cancel());
215 }
216
Alex Deymo3b678db2016-02-09 11:50:06 -0800217 if (FLAGS_reset_status) {
218 return ExitWhenIdle(service_->resetStatus());
219 }
220
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400221 if (FLAGS_switch_slot != UNSPECIFIED_FLAG) {
222 if (FLAGS_switch_slot != "true" && FLAGS_switch_slot != "false") {
223 LOG(ERROR) << "--switch_slot should be either true or false, got "
224 << FLAGS_switch_slot;
225 return 1;
226 }
227 const bool should_switch = FLAGS_switch_slot == "true";
228 ::android::binder::Status status;
229 if (should_switch) {
230 status = service_->setShouldSwitchSlotOnReboot(
231 android::String16(FLAGS_metadata.c_str(), FLAGS_metadata.size()));
232 } else {
233 status = service_->resetShouldSwitchSlotOnReboot();
234 }
235 return ExitWhenIdle(status);
236 }
237
Sen Jiang6f67d562018-09-19 14:55:50 -0700238 if (FLAGS_verify) {
239 bool applicable = false;
240 Status status = service_->verifyPayloadApplicable(
241 android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()},
242 &applicable);
243 LOG(INFO) << "Payload is " << (applicable ? "" : "not ") << "applicable.";
244 return ExitWhenIdle(status);
245 }
246
Yifan Hong82cd9d32020-01-10 14:40:25 -0800247 if (FLAGS_allocate) {
248 auto headers = ParseHeaders(FLAGS_headers);
249 int64_t ret = 0;
250 Status status = service_->allocateSpaceForPayload(
251 android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()},
252 headers,
253 &ret);
254 if (status.isOk()) {
255 if (ret == 0) {
256 LOG(INFO) << "Successfully allocated space for payload.";
257 } else {
258 LOG(INFO) << "Insufficient space; required " << ret << " bytes.";
259 }
260 } else {
261 LOG(INFO) << "Allocation failed.";
262 }
263 return ExitWhenIdle(status);
264 }
265
Yifan Honge5a8f232020-01-16 09:37:16 -0800266 if (FLAGS_merge) {
Yifan Hong40bb0d02020-02-24 17:33:14 -0800267 // Register a callback object with the service.
268 cleanup_callback_ = new UECallback(this);
269 Status status = service_->cleanupSuccessfulUpdate(cleanup_callback_);
270 if (!status.isOk()) {
271 LOG(ERROR) << "Failed to call cleanupSuccessfulUpdate.";
272 return ExitWhenIdle(status);
Yifan Honge5a8f232020-01-16 09:37:16 -0800273 }
Yifan Hong40bb0d02020-02-24 17:33:14 -0800274 keep_running = true;
Yifan Honge5a8f232020-01-16 09:37:16 -0800275 }
276
Alex Deymo5f528112016-01-27 23:32:36 -0800277 if (FLAGS_follow) {
278 // Register a callback object with the service.
279 callback_ = new UECallback(this);
280 bool bound;
281 if (!service_->bind(callback_, &bound).isOk() || !bound) {
282 LOG(ERROR) << "Failed to bind() the UpdateEngine daemon.";
283 return 1;
284 }
285 keep_running = true;
286 }
287
288 if (FLAGS_update) {
Yifan Hong82cd9d32020-01-10 14:40:25 -0800289 auto and_headers = ParseHeaders(FLAGS_headers);
Alex Deymo5f528112016-01-27 23:32:36 -0800290 Status status = service_->applyPayload(
291 android::String16{FLAGS_payload.data(), FLAGS_payload.size()},
Alex Deymo95b8f242016-01-28 16:06:57 -0800292 FLAGS_offset,
293 FLAGS_size,
Alex Deymo5f528112016-01-27 23:32:36 -0800294 and_headers);
295 if (!status.isOk())
296 return ExitWhenIdle(status);
297 }
298
299 if (!keep_running)
300 return ExitWhenIdle(EX_OK);
301
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400302 RegisterDeathNotification();
Alex Deymo5f528112016-01-27 23:32:36 -0800303 return EX_OK;
304}
305
306int UpdateEngineClientAndroid::ExitWhenIdle(const Status& status) {
307 if (status.isOk())
308 return ExitWhenIdle(EX_OK);
309 LOG(ERROR) << status.toString8();
310 return ExitWhenIdle(status.exceptionCode());
311}
312
313int UpdateEngineClientAndroid::ExitWhenIdle(int return_code) {
314 auto delayed_exit = base::Bind(
315 &Daemon::QuitWithExitCode, base::Unretained(this), return_code);
316 if (!brillo::MessageLoop::current()->PostTask(delayed_exit))
317 return 1;
318 return EX_OK;
319}
320
Alex Deymo2130ee02016-02-02 18:35:50 -0800321void UpdateEngineClientAndroid::UpdateEngineServiceDied() {
322 LOG(ERROR) << "UpdateEngineService died.";
323 QuitWithExitCode(1);
324}
325
Yifan Hong82cd9d32020-01-10 14:40:25 -0800326std::vector<android::String16> UpdateEngineClientAndroid::ParseHeaders(
327 const std::string& arg) {
328 std::vector<std::string> headers = base::SplitString(
329 arg, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
330 std::vector<android::String16> and_headers;
331 for (const auto& header : headers) {
332 and_headers.push_back(android::String16{header.data(), header.size()});
333 }
334 return and_headers;
335}
336
Alex Deymo5f528112016-01-27 23:32:36 -0800337} // namespace internal
338} // namespace chromeos_update_engine
339
340int main(int argc, char** argv) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800341 chromeos_update_engine::internal::UpdateEngineClientAndroid client(argc,
342 argv);
Alex Deymo5f528112016-01-27 23:32:36 -0800343 return client.Run();
344}