blob: 29d6f2ce809df5334577656445c03c132399262e [file] [log] [blame]
Alex Deymo03a4de72016-07-20 16:08:23 -07001//
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 <xz.h>
18
19#include <string>
20#include <vector>
21
22#include <base/command_line.h>
23#include <base/logging.h>
24#include <base/strings/string_split.h>
Alex Deymo40a017d2016-08-02 17:27:02 -070025#include <base/strings/stringprintf.h>
26#include <brillo/asynchronous_signal_handler.h>
Alex Deymo03a4de72016-07-20 16:08:23 -070027#include <brillo/flag_helper.h>
28#include <brillo/message_loops/base_message_loop.h>
Alex Deymo40a017d2016-08-02 17:27:02 -070029#include <brillo/streams/file_stream.h>
30#include <brillo/streams/stream.h>
Alex Deymo03a4de72016-07-20 16:08:23 -070031
32#include "update_engine/common/boot_control.h"
Alex Deymo40a017d2016-08-02 17:27:02 -070033#include "update_engine/common/error_code_utils.h"
Alex Deymo03a4de72016-07-20 16:08:23 -070034#include "update_engine/common/hardware.h"
35#include "update_engine/common/prefs.h"
Alex Deymo40a017d2016-08-02 17:27:02 -070036#include "update_engine/common/subprocess.h"
Alex Deymo03a4de72016-07-20 16:08:23 -070037#include "update_engine/common/terminator.h"
38#include "update_engine/common/utils.h"
Yifan Hongc73cb8c2019-10-24 18:28:31 -070039#include "update_engine/sideload_logging_android.h"
Alex Deymo03a4de72016-07-20 16:08:23 -070040#include "update_engine/update_attempter_android.h"
41
42using std::string;
43using std::vector;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070044using update_engine::UpdateEngineStatus;
Amin Hassani7cc8bb02019-01-14 16:29:47 -080045using update_engine::UpdateStatus;
Alex Deymo03a4de72016-07-20 16:08:23 -070046
47namespace chromeos_update_engine {
48namespace {
49
50void SetupLogging() {
51 string log_file;
52 logging::LoggingSettings log_settings;
53 log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
54 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
55 log_settings.log_file = nullptr;
56 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
57
58 logging::InitLogging(log_settings);
59}
60
61class SideloadDaemonState : public DaemonStateInterface,
62 public ServiceObserverInterface {
63 public:
Alex Deymo40a017d2016-08-02 17:27:02 -070064 explicit SideloadDaemonState(brillo::StreamPtr status_stream)
65 : status_stream_(std::move(status_stream)) {
Alex Deymo03a4de72016-07-20 16:08:23 -070066 // Add this class as the only observer.
67 observers_.insert(this);
68 }
69 ~SideloadDaemonState() override = default;
70
71 // DaemonStateInterface overrides.
72 bool StartUpdater() override { return true; }
73 void AddObserver(ServiceObserverInterface* observer) override {}
74 void RemoveObserver(ServiceObserverInterface* observer) override {}
75 const std::set<ServiceObserverInterface*>& service_observers() override {
76 return observers_;
77 }
78
79 // ServiceObserverInterface overrides.
Aaron Wood7f92e2b2017-08-28 14:51:21 -070080 void SendStatusUpdate(
81 const UpdateEngineStatus& update_engine_status) override {
82 UpdateStatus status = update_engine_status.status;
83 double progress = update_engine_status.progress;
Alex Deymo40a017d2016-08-02 17:27:02 -070084 if (status_ != status && (status == UpdateStatus::DOWNLOADING ||
85 status == UpdateStatus::FINALIZING)) {
86 // Split the progress bar in two parts for the two stages DOWNLOADING and
87 // FINALIZING.
88 ReportStatus(base::StringPrintf(
89 "ui_print Step %d/2", status == UpdateStatus::DOWNLOADING ? 1 : 2));
90 ReportStatus(base::StringPrintf("progress 0.5 0"));
91 }
92 if (status_ != status || fabs(progress - progress_) > 0.005) {
93 ReportStatus(base::StringPrintf("set_progress %.lf", progress));
94 }
95 progress_ = progress;
Alex Deymo03a4de72016-07-20 16:08:23 -070096 status_ = status;
97 }
98
99 void SendPayloadApplicationComplete(ErrorCode error_code) override {
Alex Deymo40a017d2016-08-02 17:27:02 -0700100 if (error_code != ErrorCode::kSuccess) {
101 ReportStatus(
102 base::StringPrintf("ui_print Error applying update: %d (%s)",
103 error_code,
104 utils::ErrorCodeToString(error_code).c_str()));
105 }
Alex Deymo03a4de72016-07-20 16:08:23 -0700106 error_code_ = error_code;
107 brillo::MessageLoop::current()->BreakLoop();
108 }
109
Alex Deymo03a4de72016-07-20 16:08:23 -0700110 // Getters.
Alex Deymo40a017d2016-08-02 17:27:02 -0700111 UpdateStatus status() { return status_; }
Alex Deymo03a4de72016-07-20 16:08:23 -0700112 ErrorCode error_code() { return error_code_; }
113
114 private:
Alex Deymo40a017d2016-08-02 17:27:02 -0700115 // Report a status message in the status_stream_, if any. These messages
116 // should conform to the specification defined in the Android recovery.
117 void ReportStatus(const string& message) {
118 if (!status_stream_)
119 return;
120 string status_line = message + "\n";
121 status_stream_->WriteAllBlocking(
122 status_line.data(), status_line.size(), nullptr);
123 }
124
Alex Deymo03a4de72016-07-20 16:08:23 -0700125 std::set<ServiceObserverInterface*> observers_;
Alex Deymo40a017d2016-08-02 17:27:02 -0700126 brillo::StreamPtr status_stream_;
Alex Deymo03a4de72016-07-20 16:08:23 -0700127
128 // The last status and error code reported.
Alex Deymo40a017d2016-08-02 17:27:02 -0700129 UpdateStatus status_{UpdateStatus::IDLE};
Alex Deymo03a4de72016-07-20 16:08:23 -0700130 ErrorCode error_code_{ErrorCode::kSuccess};
Alex Deymo40a017d2016-08-02 17:27:02 -0700131 double progress_{-1.};
Alex Deymo03a4de72016-07-20 16:08:23 -0700132};
133
134// Apply an update payload directly from the given payload URI.
135bool ApplyUpdatePayload(const string& payload,
136 int64_t payload_offset,
137 int64_t payload_size,
Alex Deymo40a017d2016-08-02 17:27:02 -0700138 const vector<string>& headers,
139 int64_t status_fd) {
Alex Deymo03a4de72016-07-20 16:08:23 -0700140 brillo::BaseMessageLoop loop;
141 loop.SetAsCurrent();
142
Alex Deymo40a017d2016-08-02 17:27:02 -0700143 // Setup the subprocess handler.
144 brillo::AsynchronousSignalHandler handler;
145 handler.Init();
146 Subprocess subprocess;
147 subprocess.Init(&handler);
148
149 SideloadDaemonState sideload_daemon_state(
150 brillo::FileStream::FromFileDescriptor(status_fd, true, nullptr));
Alex Deymo03a4de72016-07-20 16:08:23 -0700151
152 // During the sideload we don't access the prefs persisted on disk but instead
153 // use a temporary memory storage.
154 MemoryPrefs prefs;
155
156 std::unique_ptr<BootControlInterface> boot_control =
157 boot_control::CreateBootControl();
158 if (!boot_control) {
159 LOG(ERROR) << "Error initializing the BootControlInterface.";
160 return false;
161 }
162
163 std::unique_ptr<HardwareInterface> hardware = hardware::CreateHardware();
164 if (!hardware) {
165 LOG(ERROR) << "Error initializing the HardwareInterface.";
166 return false;
167 }
168
169 UpdateAttempterAndroid update_attempter(
170 &sideload_daemon_state, &prefs, boot_control.get(), hardware.get());
171 update_attempter.Init();
172
173 TEST_AND_RETURN_FALSE(update_attempter.ApplyPayload(
174 payload, payload_offset, payload_size, headers, nullptr));
175
176 loop.Run();
Alex Deymo40a017d2016-08-02 17:27:02 -0700177 return sideload_daemon_state.status() == UpdateStatus::UPDATED_NEED_REBOOT;
Alex Deymo03a4de72016-07-20 16:08:23 -0700178}
179
180} // namespace
181} // namespace chromeos_update_engine
182
183int main(int argc, char** argv) {
184 DEFINE_string(payload,
185 "file:///data/payload.bin",
186 "The URI to the update payload to use.");
187 DEFINE_int64(
188 offset, 0, "The offset in the payload where the CrAU update starts. ");
189 DEFINE_int64(size,
190 0,
191 "The size of the CrAU part of the payload. If 0 is passed, it "
192 "will be autodetected.");
193 DEFINE_string(headers,
194 "",
195 "A list of key-value pairs, one element of the list per line.");
Alex Deymo40a017d2016-08-02 17:27:02 -0700196 DEFINE_int64(status_fd, -1, "A file descriptor to notify the update status.");
Alex Deymo03a4de72016-07-20 16:08:23 -0700197
198 chromeos_update_engine::Terminator::Init();
199 chromeos_update_engine::SetupLogging();
Yifan Hongc73cb8c2019-10-24 18:28:31 -0700200 chromeos_update_engine::SetupAndroidLogging(argv);
Alex Deymo03a4de72016-07-20 16:08:23 -0700201 brillo::FlagHelper::Init(argc, argv, "Update Engine Sideload");
202
203 LOG(INFO) << "Update Engine Sideloading starting";
204
205 // xz-embedded requires to initialize its CRC-32 table once on startup.
206 xz_crc32_init();
207
208 vector<string> headers = base::SplitString(
209 FLAGS_headers, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
210
211 if (!chromeos_update_engine::ApplyUpdatePayload(
Alex Deymo40a017d2016-08-02 17:27:02 -0700212 FLAGS_payload, FLAGS_offset, FLAGS_size, headers, FLAGS_status_fd))
Alex Deymo03a4de72016-07-20 16:08:23 -0700213 return 1;
214
215 return 0;
216}