blob: 7d9bc3de61b37657434f6d03d89f6fca332bdcfe [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"
Alex Deymo5f528112016-01-27 23:32:36 -080044
45using android::binder::Status;
46
47namespace chromeos_update_engine {
48namespace internal {
49
50class UpdateEngineClientAndroid : public brillo::Daemon {
51 public:
Amin Hassani7cc8bb02019-01-14 16:29:47 -080052 UpdateEngineClientAndroid(int argc, char** argv) : argc_(argc), argv_(argv) {}
Alex Deymo5f528112016-01-27 23:32:36 -080053
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 Deymoe88e9fe2016-02-03 16:38:00 -080060 explicit UECallback(UpdateEngineClientAndroid* client) : client_(client) {}
Alex Deymo5f528112016-01-27 23:32:36 -080061
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 Deymo2130ee02016-02-02 18:35:50 -080072 // Called whenever the UpdateEngine daemon dies.
73 void UpdateEngineServiceDied();
74
Yifan Hong82cd9d32020-01-10 14:40:25 -080075 static std::vector<android::String16> ParseHeaders(const std::string& arg);
76
Alex Deymo5f528112016-01-27 23:32:36 -080077 // 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_;
83
84 brillo::BinderWatcher binder_watcher_;
85};
86
Amin Hassani7cc8bb02019-01-14 16:29:47 -080087Status UpdateEngineClientAndroid::UECallback::onStatusUpdate(int status_code,
88 float progress) {
Alex Deymo5f528112016-01-27 23:32:36 -080089 update_engine::UpdateStatus status =
90 static_cast<update_engine::UpdateStatus>(status_code);
91 LOG(INFO) << "onStatusUpdate(" << UpdateStatusToString(status) << " ("
92 << status_code << "), " << progress << ")";
93 return Status::ok();
94}
95
96Status UpdateEngineClientAndroid::UECallback::onPayloadApplicationComplete(
97 int error_code) {
98 ErrorCode code = static_cast<ErrorCode>(error_code);
Alex Deymoe88e9fe2016-02-03 16:38:00 -080099 LOG(INFO) << "onPayloadApplicationComplete(" << utils::ErrorCodeToString(code)
100 << " (" << error_code << "))";
Sen Jiang02c49422017-10-31 15:14:11 -0700101 client_->ExitWhenIdle(
102 (code == ErrorCode::kSuccess || code == ErrorCode::kUpdatedButNotActive)
103 ? EX_OK
104 : 1);
Alex Deymo5f528112016-01-27 23:32:36 -0800105 return Status::ok();
106}
107
108int UpdateEngineClientAndroid::OnInit() {
109 int ret = Daemon::OnInit();
110 if (ret != EX_OK)
111 return ret;
112
113 DEFINE_bool(update, false, "Start a new update, if no update in progress.");
114 DEFINE_string(payload,
115 "http://127.0.0.1:8080/payload",
116 "The URI to the update payload to use.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800117 DEFINE_int64(offset,
118 0,
Alex Deymo95b8f242016-01-28 16:06:57 -0800119 "The offset in the payload where the CrAU update starts. "
120 "Used when --update is passed.");
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800121 DEFINE_int64(size,
122 0,
Alex Deymo95b8f242016-01-28 16:06:57 -0800123 "The size of the CrAU part of the payload. If 0 is passed, it "
124 "will be autodetected. Used when --update is passed.");
Alex Deymo5f528112016-01-27 23:32:36 -0800125 DEFINE_string(headers,
126 "",
Alex Deymo95b8f242016-01-28 16:06:57 -0800127 "A list of key-value pairs, one element of the list per line. "
Yifan Hong82cd9d32020-01-10 14:40:25 -0800128 "Used when --update or --allocate is passed.");
Alex Deymo5f528112016-01-27 23:32:36 -0800129
Sen Jiang6f67d562018-09-19 14:55:50 -0700130 DEFINE_bool(verify,
131 false,
132 "Given payload metadata, verify if the payload is applicable.");
Yifan Hong82cd9d32020-01-10 14:40:25 -0800133 DEFINE_bool(allocate, false, "Given payload metadata, allocate space.");
Sen Jiang6f67d562018-09-19 14:55:50 -0700134 DEFINE_string(metadata,
135 "/data/ota_package/metadata",
136 "The path to the update payload metadata. "
Yifan Hong82cd9d32020-01-10 14:40:25 -0800137 "Used when --verify or --allocate is passed.");
Sen Jiang6f67d562018-09-19 14:55:50 -0700138
Alex Deymo5f528112016-01-27 23:32:36 -0800139 DEFINE_bool(suspend, false, "Suspend an ongoing update and exit.");
140 DEFINE_bool(resume, false, "Resume a suspended update.");
141 DEFINE_bool(cancel, false, "Cancel the ongoing update and exit.");
Alex Deymo3b678db2016-02-09 11:50:06 -0800142 DEFINE_bool(reset_status, false, "Reset an already applied update and exit.");
Alex Deymo5f528112016-01-27 23:32:36 -0800143 DEFINE_bool(follow,
144 false,
145 "Follow status update changes until a final state is reached. "
146 "Exit status is 0 if the update succeeded, and 1 otherwise.");
Yifan Honge5a8f232020-01-16 09:37:16 -0800147 DEFINE_bool(merge,
148 false,
149 "Wait for previous update to merge. "
150 "Only available after rebooting to new slot.");
Alex Deymo5f528112016-01-27 23:32:36 -0800151 // Boilerplate init commands.
152 base::CommandLine::Init(argc_, argv_);
153 brillo::FlagHelper::Init(argc_, argv_, "Android Update Engine Client");
154 if (argc_ == 1) {
155 LOG(ERROR) << "Nothing to do. Run with --help for help.";
156 return 1;
157 }
158
159 // Ensure there are no positional arguments.
160 const std::vector<std::string> positional_args =
161 base::CommandLine::ForCurrentProcess()->GetArgs();
162 if (!positional_args.empty()) {
163 LOG(ERROR) << "Found a positional argument '" << positional_args.front()
164 << "'. If you want to pass a value to a flag, pass it as "
165 "--flag=value.";
166 return 1;
167 }
168
169 bool keep_running = false;
Alex Deymo95224dd2016-01-29 11:18:35 -0800170 brillo::InitLog(brillo::kLogToStderr);
Alex Deymo2130ee02016-02-02 18:35:50 -0800171
172 // Initialize a binder watcher early in the process before any interaction
173 // with the binder driver.
174 binder_watcher_.Init();
175
Alex Deymo5f528112016-01-27 23:32:36 -0800176 android::status_t status = android::getService(
177 android::String16("android.os.UpdateEngineService"), &service_);
178 if (status != android::OK) {
179 LOG(ERROR) << "Failed to get IUpdateEngine binder from service manager: "
180 << Status::fromStatusT(status).toString8();
Alex Deymo2130ee02016-02-02 18:35:50 -0800181 return ExitWhenIdle(1);
Alex Deymo5f528112016-01-27 23:32:36 -0800182 }
183
184 if (FLAGS_suspend) {
185 return ExitWhenIdle(service_->suspend());
186 }
187
188 if (FLAGS_resume) {
189 return ExitWhenIdle(service_->resume());
190 }
191
192 if (FLAGS_cancel) {
193 return ExitWhenIdle(service_->cancel());
194 }
195
Alex Deymo3b678db2016-02-09 11:50:06 -0800196 if (FLAGS_reset_status) {
197 return ExitWhenIdle(service_->resetStatus());
198 }
199
Sen Jiang6f67d562018-09-19 14:55:50 -0700200 if (FLAGS_verify) {
201 bool applicable = false;
202 Status status = service_->verifyPayloadApplicable(
203 android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()},
204 &applicable);
205 LOG(INFO) << "Payload is " << (applicable ? "" : "not ") << "applicable.";
206 return ExitWhenIdle(status);
207 }
208
Yifan Hong82cd9d32020-01-10 14:40:25 -0800209 if (FLAGS_allocate) {
210 auto headers = ParseHeaders(FLAGS_headers);
211 int64_t ret = 0;
212 Status status = service_->allocateSpaceForPayload(
213 android::String16{FLAGS_metadata.data(), FLAGS_metadata.size()},
214 headers,
215 &ret);
216 if (status.isOk()) {
217 if (ret == 0) {
218 LOG(INFO) << "Successfully allocated space for payload.";
219 } else {
220 LOG(INFO) << "Insufficient space; required " << ret << " bytes.";
221 }
222 } else {
223 LOG(INFO) << "Allocation failed.";
224 }
225 return ExitWhenIdle(status);
226 }
227
Yifan Honge5a8f232020-01-16 09:37:16 -0800228 if (FLAGS_merge) {
229 int32_t ret = 0;
230 Status status = service_->cleanupSuccessfulUpdate(&ret);
231 if (status.isOk()) {
232 LOG(INFO) << "CleanupSuccessfulUpdate exits with "
233 << utils::ErrorCodeToString(static_cast<ErrorCode>(ret));
234 }
235 return ExitWhenIdle(status);
236 }
237
Alex Deymo5f528112016-01-27 23:32:36 -0800238 if (FLAGS_follow) {
239 // Register a callback object with the service.
240 callback_ = new UECallback(this);
241 bool bound;
242 if (!service_->bind(callback_, &bound).isOk() || !bound) {
243 LOG(ERROR) << "Failed to bind() the UpdateEngine daemon.";
244 return 1;
245 }
246 keep_running = true;
247 }
248
249 if (FLAGS_update) {
Yifan Hong82cd9d32020-01-10 14:40:25 -0800250 auto and_headers = ParseHeaders(FLAGS_headers);
Alex Deymo5f528112016-01-27 23:32:36 -0800251 Status status = service_->applyPayload(
252 android::String16{FLAGS_payload.data(), FLAGS_payload.size()},
Alex Deymo95b8f242016-01-28 16:06:57 -0800253 FLAGS_offset,
254 FLAGS_size,
Alex Deymo5f528112016-01-27 23:32:36 -0800255 and_headers);
256 if (!status.isOk())
257 return ExitWhenIdle(status);
258 }
259
260 if (!keep_running)
261 return ExitWhenIdle(EX_OK);
262
Alex Deymo2130ee02016-02-02 18:35:50 -0800263 // When following updates status changes, exit if the update_engine daemon
264 // dies.
265 android::BinderWrapper::Create();
266 android::BinderWrapper::Get()->RegisterForDeathNotifications(
267 android::os::IUpdateEngine::asBinder(service_),
268 base::Bind(&UpdateEngineClientAndroid::UpdateEngineServiceDied,
269 base::Unretained(this)));
270
Alex Deymo5f528112016-01-27 23:32:36 -0800271 return EX_OK;
272}
273
274int UpdateEngineClientAndroid::ExitWhenIdle(const Status& status) {
275 if (status.isOk())
276 return ExitWhenIdle(EX_OK);
277 LOG(ERROR) << status.toString8();
278 return ExitWhenIdle(status.exceptionCode());
279}
280
281int UpdateEngineClientAndroid::ExitWhenIdle(int return_code) {
282 auto delayed_exit = base::Bind(
283 &Daemon::QuitWithExitCode, base::Unretained(this), return_code);
284 if (!brillo::MessageLoop::current()->PostTask(delayed_exit))
285 return 1;
286 return EX_OK;
287}
288
Alex Deymo2130ee02016-02-02 18:35:50 -0800289void UpdateEngineClientAndroid::UpdateEngineServiceDied() {
290 LOG(ERROR) << "UpdateEngineService died.";
291 QuitWithExitCode(1);
292}
293
Yifan Hong82cd9d32020-01-10 14:40:25 -0800294std::vector<android::String16> UpdateEngineClientAndroid::ParseHeaders(
295 const std::string& arg) {
296 std::vector<std::string> headers = base::SplitString(
297 arg, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
298 std::vector<android::String16> and_headers;
299 for (const auto& header : headers) {
300 and_headers.push_back(android::String16{header.data(), header.size()});
301 }
302 return and_headers;
303}
304
Alex Deymo5f528112016-01-27 23:32:36 -0800305} // namespace internal
306} // namespace chromeos_update_engine
307
308int main(int argc, char** argv) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800309 chromeos_update_engine::internal::UpdateEngineClientAndroid client(argc,
310 argv);
Alex Deymo5f528112016-01-27 23:32:36 -0800311 return client.Run();
312}