| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 1 | // | 
 | 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 Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 25 | #include <base/strings/stringprintf.h> | 
 | 26 | #include <brillo/asynchronous_signal_handler.h> | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 27 | #include <brillo/flag_helper.h> | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 28 | #include <brillo/make_unique_ptr.h> | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 29 | #include <brillo/message_loops/base_message_loop.h> | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 30 | #include <brillo/streams/file_stream.h> | 
 | 31 | #include <brillo/streams/stream.h> | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 32 |  | 
 | 33 | #include "update_engine/common/boot_control.h" | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 34 | #include "update_engine/common/error_code_utils.h" | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 35 | #include "update_engine/common/hardware.h" | 
 | 36 | #include "update_engine/common/prefs.h" | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 37 | #include "update_engine/common/subprocess.h" | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 38 | #include "update_engine/common/terminator.h" | 
 | 39 | #include "update_engine/common/utils.h" | 
 | 40 | #include "update_engine/update_attempter_android.h" | 
 | 41 |  | 
 | 42 | using std::string; | 
 | 43 | using std::vector; | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 44 | using update_engine::UpdateStatus; | 
| Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 45 | using update_engine::UpdateEngineStatus; | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 46 |  | 
| Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 47 | namespace { | 
 | 48 | // The root directory used for temporary files in update_engine_sideload. | 
 | 49 | const char kSideloadRootTempDir[] = "/tmp/update_engine_sideload"; | 
 | 50 | }  // namespace | 
 | 51 |  | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 52 | namespace chromeos_update_engine { | 
 | 53 | namespace { | 
 | 54 |  | 
 | 55 | void SetupLogging() { | 
 | 56 |   string log_file; | 
 | 57 |   logging::LoggingSettings log_settings; | 
 | 58 |   log_settings.lock_log = logging::DONT_LOCK_LOG_FILE; | 
 | 59 |   log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE; | 
 | 60 |   log_settings.log_file = nullptr; | 
 | 61 |   log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; | 
 | 62 |  | 
 | 63 |   logging::InitLogging(log_settings); | 
 | 64 | } | 
 | 65 |  | 
 | 66 | class SideloadDaemonState : public DaemonStateInterface, | 
 | 67 |                             public ServiceObserverInterface { | 
 | 68 |  public: | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 69 |   explicit SideloadDaemonState(brillo::StreamPtr status_stream) | 
 | 70 |       : status_stream_(std::move(status_stream)) { | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 71 |     // Add this class as the only observer. | 
 | 72 |     observers_.insert(this); | 
 | 73 |   } | 
 | 74 |   ~SideloadDaemonState() override = default; | 
 | 75 |  | 
 | 76 |   // DaemonStateInterface overrides. | 
 | 77 |   bool StartUpdater() override { return true; } | 
 | 78 |   void AddObserver(ServiceObserverInterface* observer) override {} | 
 | 79 |   void RemoveObserver(ServiceObserverInterface* observer) override {} | 
 | 80 |   const std::set<ServiceObserverInterface*>& service_observers() override { | 
 | 81 |     return observers_; | 
 | 82 |   } | 
 | 83 |  | 
 | 84 |   // ServiceObserverInterface overrides. | 
| Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 85 |   void SendStatusUpdate( | 
 | 86 |       const UpdateEngineStatus& update_engine_status) override { | 
 | 87 |     UpdateStatus status = update_engine_status.status; | 
 | 88 |     double progress = update_engine_status.progress; | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 89 |     if (status_ != status && (status == UpdateStatus::DOWNLOADING || | 
 | 90 |                               status == UpdateStatus::FINALIZING)) { | 
 | 91 |       // Split the progress bar in two parts for the two stages DOWNLOADING and | 
 | 92 |       // FINALIZING. | 
 | 93 |       ReportStatus(base::StringPrintf( | 
 | 94 |           "ui_print Step %d/2", status == UpdateStatus::DOWNLOADING ? 1 : 2)); | 
 | 95 |       ReportStatus(base::StringPrintf("progress 0.5 0")); | 
 | 96 |     } | 
 | 97 |     if (status_ != status || fabs(progress - progress_) > 0.005) { | 
 | 98 |       ReportStatus(base::StringPrintf("set_progress %.lf", progress)); | 
 | 99 |     } | 
 | 100 |     progress_ = progress; | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 101 |     status_ = status; | 
 | 102 |   } | 
 | 103 |  | 
 | 104 |   void SendPayloadApplicationComplete(ErrorCode error_code) override { | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 105 |     if (error_code != ErrorCode::kSuccess) { | 
 | 106 |       ReportStatus( | 
 | 107 |           base::StringPrintf("ui_print Error applying update: %d (%s)", | 
 | 108 |                              error_code, | 
 | 109 |                              utils::ErrorCodeToString(error_code).c_str())); | 
 | 110 |     } | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 111 |     error_code_ = error_code; | 
 | 112 |     brillo::MessageLoop::current()->BreakLoop(); | 
 | 113 |   } | 
 | 114 |  | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 115 |   // Getters. | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 116 |   UpdateStatus status() { return status_; } | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 117 |   ErrorCode error_code() { return error_code_; } | 
 | 118 |  | 
 | 119 |  private: | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 120 |   // Report a status message in the status_stream_, if any. These messages | 
 | 121 |   // should conform to the specification defined in the Android recovery. | 
 | 122 |   void ReportStatus(const string& message) { | 
 | 123 |     if (!status_stream_) | 
 | 124 |       return; | 
 | 125 |     string status_line = message + "\n"; | 
 | 126 |     status_stream_->WriteAllBlocking( | 
 | 127 |         status_line.data(), status_line.size(), nullptr); | 
 | 128 |   } | 
 | 129 |  | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 130 |   std::set<ServiceObserverInterface*> observers_; | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 131 |   brillo::StreamPtr status_stream_; | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 132 |  | 
 | 133 |   // The last status and error code reported. | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 134 |   UpdateStatus status_{UpdateStatus::IDLE}; | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 135 |   ErrorCode error_code_{ErrorCode::kSuccess}; | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 136 |   double progress_{-1.}; | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 137 | }; | 
 | 138 |  | 
 | 139 | // Apply an update payload directly from the given payload URI. | 
 | 140 | bool ApplyUpdatePayload(const string& payload, | 
 | 141 |                         int64_t payload_offset, | 
 | 142 |                         int64_t payload_size, | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 143 |                         const vector<string>& headers, | 
 | 144 |                         int64_t status_fd) { | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 145 |   brillo::BaseMessageLoop loop; | 
 | 146 |   loop.SetAsCurrent(); | 
 | 147 |  | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 148 |   // Setup the subprocess handler. | 
 | 149 |   brillo::AsynchronousSignalHandler handler; | 
 | 150 |   handler.Init(); | 
 | 151 |   Subprocess subprocess; | 
 | 152 |   subprocess.Init(&handler); | 
 | 153 |  | 
 | 154 |   SideloadDaemonState sideload_daemon_state( | 
 | 155 |       brillo::FileStream::FromFileDescriptor(status_fd, true, nullptr)); | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 156 |  | 
 | 157 |   // During the sideload we don't access the prefs persisted on disk but instead | 
 | 158 |   // use a temporary memory storage. | 
 | 159 |   MemoryPrefs prefs; | 
 | 160 |  | 
 | 161 |   std::unique_ptr<BootControlInterface> boot_control = | 
 | 162 |       boot_control::CreateBootControl(); | 
 | 163 |   if (!boot_control) { | 
 | 164 |     LOG(ERROR) << "Error initializing the BootControlInterface."; | 
 | 165 |     return false; | 
 | 166 |   } | 
 | 167 |  | 
 | 168 |   std::unique_ptr<HardwareInterface> hardware = hardware::CreateHardware(); | 
 | 169 |   if (!hardware) { | 
 | 170 |     LOG(ERROR) << "Error initializing the HardwareInterface."; | 
 | 171 |     return false; | 
 | 172 |   } | 
 | 173 |  | 
 | 174 |   UpdateAttempterAndroid update_attempter( | 
 | 175 |       &sideload_daemon_state, &prefs, boot_control.get(), hardware.get()); | 
 | 176 |   update_attempter.Init(); | 
 | 177 |  | 
 | 178 |   TEST_AND_RETURN_FALSE(update_attempter.ApplyPayload( | 
 | 179 |       payload, payload_offset, payload_size, headers, nullptr)); | 
 | 180 |  | 
 | 181 |   loop.Run(); | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 182 |   return sideload_daemon_state.status() == UpdateStatus::UPDATED_NEED_REBOOT; | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 183 | } | 
 | 184 |  | 
 | 185 | }  // namespace | 
 | 186 | }  // namespace chromeos_update_engine | 
 | 187 |  | 
 | 188 | int main(int argc, char** argv) { | 
 | 189 |   DEFINE_string(payload, | 
 | 190 |                 "file:///data/payload.bin", | 
 | 191 |                 "The URI to the update payload to use."); | 
 | 192 |   DEFINE_int64( | 
 | 193 |       offset, 0, "The offset in the payload where the CrAU update starts. "); | 
 | 194 |   DEFINE_int64(size, | 
 | 195 |                0, | 
 | 196 |                "The size of the CrAU part of the payload. If 0 is passed, it " | 
 | 197 |                "will be autodetected."); | 
 | 198 |   DEFINE_string(headers, | 
 | 199 |                 "", | 
 | 200 |                 "A list of key-value pairs, one element of the list per line."); | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 201 |   DEFINE_int64(status_fd, -1, "A file descriptor to notify the update status."); | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 202 |  | 
 | 203 |   chromeos_update_engine::Terminator::Init(); | 
 | 204 |   chromeos_update_engine::SetupLogging(); | 
 | 205 |   brillo::FlagHelper::Init(argc, argv, "Update Engine Sideload"); | 
 | 206 |  | 
 | 207 |   LOG(INFO) << "Update Engine Sideloading starting"; | 
 | 208 |  | 
 | 209 |   // xz-embedded requires to initialize its CRC-32 table once on startup. | 
 | 210 |   xz_crc32_init(); | 
 | 211 |  | 
| Alex Deymo | 3295102 | 2016-08-10 17:02:49 -0700 | [diff] [blame] | 212 |   // When called from recovery, /data is not accessible, so we need to use | 
 | 213 |   // /tmp for temporary files. | 
 | 214 |   chromeos_update_engine::utils::SetRootTempDir(kSideloadRootTempDir); | 
 | 215 |  | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 216 |   vector<string> headers = base::SplitString( | 
 | 217 |       FLAGS_headers, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 
 | 218 |  | 
 | 219 |   if (!chromeos_update_engine::ApplyUpdatePayload( | 
| Alex Deymo | 40a017d | 2016-08-02 17:27:02 -0700 | [diff] [blame] | 220 |           FLAGS_payload, FLAGS_offset, FLAGS_size, headers, FLAGS_status_fd)) | 
| Alex Deymo | 03a4de7 | 2016-07-20 16:08:23 -0700 | [diff] [blame] | 221 |     return 1; | 
 | 222 |  | 
 | 223 |   return 0; | 
 | 224 | } |