blob: 43d0f93b2df37bd1a3b211b0636bc8930041d362 [file] [log] [blame]
Alex Deymo578b7872016-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 Deymo5462f452016-08-02 17:27:02 -070025#include <base/strings/stringprintf.h>
26#include <brillo/asynchronous_signal_handler.h>
Alex Deymo578b7872016-07-20 16:08:23 -070027#include <brillo/flag_helper.h>
Alex Deymo5462f452016-08-02 17:27:02 -070028#include <brillo/make_unique_ptr.h>
Alex Deymo578b7872016-07-20 16:08:23 -070029#include <brillo/message_loops/base_message_loop.h>
Alex Deymo5462f452016-08-02 17:27:02 -070030#include <brillo/streams/file_stream.h>
31#include <brillo/streams/stream.h>
Alex Deymo578b7872016-07-20 16:08:23 -070032
33#include "update_engine/common/boot_control.h"
Alex Deymo5462f452016-08-02 17:27:02 -070034#include "update_engine/common/error_code_utils.h"
Alex Deymo578b7872016-07-20 16:08:23 -070035#include "update_engine/common/hardware.h"
36#include "update_engine/common/prefs.h"
Alex Deymo5462f452016-08-02 17:27:02 -070037#include "update_engine/common/subprocess.h"
Alex Deymo578b7872016-07-20 16:08:23 -070038#include "update_engine/common/terminator.h"
39#include "update_engine/common/utils.h"
40#include "update_engine/update_attempter_android.h"
41
42using std::string;
43using std::vector;
Alex Deymo5462f452016-08-02 17:27:02 -070044using update_engine::UpdateStatus;
Alex Deymo578b7872016-07-20 16:08:23 -070045
46namespace chromeos_update_engine {
47namespace {
48
49void SetupLogging() {
50 string log_file;
51 logging::LoggingSettings log_settings;
52 log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
53 log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
54 log_settings.log_file = nullptr;
55 log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
56
57 logging::InitLogging(log_settings);
58}
59
60class SideloadDaemonState : public DaemonStateInterface,
61 public ServiceObserverInterface {
62 public:
Alex Deymo5462f452016-08-02 17:27:02 -070063 explicit SideloadDaemonState(brillo::StreamPtr status_stream)
64 : status_stream_(std::move(status_stream)) {
Alex Deymo578b7872016-07-20 16:08:23 -070065 // Add this class as the only observer.
66 observers_.insert(this);
67 }
68 ~SideloadDaemonState() override = default;
69
70 // DaemonStateInterface overrides.
71 bool StartUpdater() override { return true; }
72 void AddObserver(ServiceObserverInterface* observer) override {}
73 void RemoveObserver(ServiceObserverInterface* observer) override {}
74 const std::set<ServiceObserverInterface*>& service_observers() override {
75 return observers_;
76 }
77
78 // ServiceObserverInterface overrides.
79 void SendStatusUpdate(int64_t last_checked_time,
80 double progress,
Alex Deymo5462f452016-08-02 17:27:02 -070081 UpdateStatus status,
Alex Deymo578b7872016-07-20 16:08:23 -070082 const string& new_version,
83 int64_t new_size) override {
Alex Deymo5462f452016-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 Deymo578b7872016-07-20 16:08:23 -070096 status_ = status;
97 }
98
99 void SendPayloadApplicationComplete(ErrorCode error_code) override {
Alex Deymo5462f452016-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 Deymo578b7872016-07-20 16:08:23 -0700106 error_code_ = error_code;
107 brillo::MessageLoop::current()->BreakLoop();
108 }
109
110 void SendChannelChangeUpdate(const string& tracking_channel) override {}
111
112 // Getters.
Alex Deymo5462f452016-08-02 17:27:02 -0700113 UpdateStatus status() { return status_; }
Alex Deymo578b7872016-07-20 16:08:23 -0700114 ErrorCode error_code() { return error_code_; }
115
116 private:
Alex Deymo5462f452016-08-02 17:27:02 -0700117 // Report a status message in the status_stream_, if any. These messages
118 // should conform to the specification defined in the Android recovery.
119 void ReportStatus(const string& message) {
120 if (!status_stream_)
121 return;
122 string status_line = message + "\n";
123 status_stream_->WriteAllBlocking(
124 status_line.data(), status_line.size(), nullptr);
125 }
126
Alex Deymo578b7872016-07-20 16:08:23 -0700127 std::set<ServiceObserverInterface*> observers_;
Alex Deymo5462f452016-08-02 17:27:02 -0700128 brillo::StreamPtr status_stream_;
Alex Deymo578b7872016-07-20 16:08:23 -0700129
130 // The last status and error code reported.
Alex Deymo5462f452016-08-02 17:27:02 -0700131 UpdateStatus status_{UpdateStatus::IDLE};
Alex Deymo578b7872016-07-20 16:08:23 -0700132 ErrorCode error_code_{ErrorCode::kSuccess};
Alex Deymo5462f452016-08-02 17:27:02 -0700133 double progress_{-1.};
Alex Deymo578b7872016-07-20 16:08:23 -0700134};
135
136// Apply an update payload directly from the given payload URI.
137bool ApplyUpdatePayload(const string& payload,
138 int64_t payload_offset,
139 int64_t payload_size,
Alex Deymo5462f452016-08-02 17:27:02 -0700140 const vector<string>& headers,
141 int64_t status_fd) {
Alex Deymoa386d052016-08-01 13:09:36 -0700142 base::MessageLoopForIO base_loop;
143 brillo::BaseMessageLoop loop(&base_loop);
Alex Deymo578b7872016-07-20 16:08:23 -0700144 loop.SetAsCurrent();
145
Alex Deymo5462f452016-08-02 17:27:02 -0700146 // Setup the subprocess handler.
147 brillo::AsynchronousSignalHandler handler;
148 handler.Init();
149 Subprocess subprocess;
150 subprocess.Init(&handler);
151
152 SideloadDaemonState sideload_daemon_state(
153 brillo::FileStream::FromFileDescriptor(status_fd, true, nullptr));
Alex Deymo578b7872016-07-20 16:08:23 -0700154
155 // During the sideload we don't access the prefs persisted on disk but instead
156 // use a temporary memory storage.
157 MemoryPrefs prefs;
158
159 std::unique_ptr<BootControlInterface> boot_control =
160 boot_control::CreateBootControl();
161 if (!boot_control) {
162 LOG(ERROR) << "Error initializing the BootControlInterface.";
163 return false;
164 }
165
166 std::unique_ptr<HardwareInterface> hardware = hardware::CreateHardware();
167 if (!hardware) {
168 LOG(ERROR) << "Error initializing the HardwareInterface.";
169 return false;
170 }
171
172 UpdateAttempterAndroid update_attempter(
173 &sideload_daemon_state, &prefs, boot_control.get(), hardware.get());
174 update_attempter.Init();
175
176 TEST_AND_RETURN_FALSE(update_attempter.ApplyPayload(
177 payload, payload_offset, payload_size, headers, nullptr));
178
179 loop.Run();
Alex Deymo5462f452016-08-02 17:27:02 -0700180 return sideload_daemon_state.status() == UpdateStatus::UPDATED_NEED_REBOOT;
Alex Deymo578b7872016-07-20 16:08:23 -0700181}
182
183} // namespace
184} // namespace chromeos_update_engine
185
186int main(int argc, char** argv) {
187 DEFINE_string(payload,
188 "file:///data/payload.bin",
189 "The URI to the update payload to use.");
190 DEFINE_int64(
191 offset, 0, "The offset in the payload where the CrAU update starts. ");
192 DEFINE_int64(size,
193 0,
194 "The size of the CrAU part of the payload. If 0 is passed, it "
195 "will be autodetected.");
196 DEFINE_string(headers,
197 "",
198 "A list of key-value pairs, one element of the list per line.");
Alex Deymo5462f452016-08-02 17:27:02 -0700199 DEFINE_int64(status_fd, -1, "A file descriptor to notify the update status.");
Alex Deymo578b7872016-07-20 16:08:23 -0700200
201 chromeos_update_engine::Terminator::Init();
202 chromeos_update_engine::SetupLogging();
203 brillo::FlagHelper::Init(argc, argv, "Update Engine Sideload");
204
205 LOG(INFO) << "Update Engine Sideloading starting";
206
207 // xz-embedded requires to initialize its CRC-32 table once on startup.
208 xz_crc32_init();
209
210 vector<string> headers = base::SplitString(
211 FLAGS_headers, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
212
213 if (!chromeos_update_engine::ApplyUpdatePayload(
Alex Deymo5462f452016-08-02 17:27:02 -0700214 FLAGS_payload, FLAGS_offset, FLAGS_size, headers, FLAGS_status_fd))
Alex Deymo578b7872016-07-20 16:08:23 -0700215 return 1;
216
217 return 0;
218}