blob: 1717dd78acc1982f9c0943e8ff5d731bf479dfa3 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 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//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/postinstall_runner_action.h"
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070018
Alex Deymo0d298542016-03-30 18:31:49 -070019#include <fcntl.h>
Alex Deymod15c5462016-03-09 18:11:12 -080020#include <signal.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <stdlib.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070022#include <sys/mount.h>
Alex Deymod15c5462016-03-09 18:11:12 -080023#include <sys/types.h>
Alex Deymo0d298542016-03-30 18:31:49 -070024#include <unistd.h>
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070025
Alex Deymo44b35672016-04-05 17:57:48 -070026#include <cmath>
Kelvin Zhangddc25802021-12-30 13:05:27 -080027#include <fstream>
28#include <string>
Alex Deymo44b35672016-04-05 17:57:48 -070029
Alex Deymoe5e5fe92015-10-05 09:28:19 -070030#include <base/files/file_path.h>
31#include <base/files/file_util.h>
Alex Deymod15c5462016-03-09 18:11:12 -080032#include <base/logging.h>
Alex Deymo0d298542016-03-30 18:31:49 -070033#include <base/strings/string_split.h>
Alex Deymo461b2592015-07-24 20:10:52 -070034
Alex Deymo39910dc2015-11-09 17:04:30 -080035#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030036#include "update_engine/common/boot_control_interface.h"
Kelvin Zhang99cbbe72024-01-18 14:50:01 -080037#include "update_engine/common/error_code_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080038#include "update_engine/common/subprocess.h"
39#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000040
Alex Deymo0d298542016-03-30 18:31:49 -070041namespace {
42
43// The file descriptor number from the postinstall program's perspective where
44// it can report status updates. This can be any number greater than 2 (stderr),
45// but must be kept in sync with the "bin/postinst_progress" defined in the
46// sample_images.sh file.
47const int kPostinstallStatusFd = 3;
48
Kelvin Zhangddc25802021-12-30 13:05:27 -080049static constexpr bool Contains(std::string_view haystack,
50 std::string_view needle) {
51 return haystack.find(needle) != std::string::npos;
52}
53
54static void LogBuildInfoForPartition(std::string_view mount_point) {
55 static constexpr std::array<std::string_view, 3> kBuildPropFiles{
56 "build.prop", "etc/build.prop", "system/build.prop"};
57 for (const auto& file : kBuildPropFiles) {
58 auto path = std::string(mount_point);
59 if (path.back() != '/') {
60 path.push_back('/');
61 }
62 path += file;
63 LOG(INFO) << "Trying to read " << path;
64 std::ifstream infile(path);
65 std::string line;
66 while (std::getline(infile, line)) {
67 if (Contains(line, "ro.build")) {
68 LOG(INFO) << line;
69 }
70 }
71 }
72}
73
Alex Deymo0d298542016-03-30 18:31:49 -070074} // namespace
75
adlr@google.com3defe6a2009-12-04 20:57:17 +000076namespace chromeos_update_engine {
77
78using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070079using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000080
Kelvin Zhange9def4e2020-12-02 14:04:09 -050081PostinstallRunnerAction::PostinstallRunnerAction(
82 BootControlInterface* boot_control, HardwareInterface* hardware)
83 : boot_control_(boot_control), hardware_(hardware) {
84#ifdef __ANDROID__
85 fs_mount_dir_ = "/postinstall";
86#else // __ANDROID__
87 base::FilePath temp_dir;
88 TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir));
89 fs_mount_dir_ = temp_dir.value();
90#endif // __ANDROID__
Kelvin Zhang2379fa92020-12-09 14:39:04 -050091 CHECK(!fs_mount_dir_.empty());
Kelvin Zhang1df000a2022-02-09 16:00:17 -080092 EnsureUnmounted();
Kelvin Zhang2379fa92020-12-09 14:39:04 -050093 LOG(INFO) << "postinstall mount point: " << fs_mount_dir_;
Kelvin Zhange9def4e2020-12-02 14:04:09 -050094}
95
Kelvin Zhang1df000a2022-02-09 16:00:17 -080096void PostinstallRunnerAction::EnsureUnmounted() {
97 if (utils::IsMountpoint(fs_mount_dir_)) {
98 LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_;
99 utils::UnmountFilesystem(fs_mount_dir_);
100 }
101}
102
adlr@google.com3defe6a2009-12-04 20:57:17 +0000103void PostinstallRunnerAction::PerformAction() {
104 CHECK(HasInputObject());
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400105 CHECK(boot_control_);
Chris Sosad317e402013-06-12 13:47:09 -0700106 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800107
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400108 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
109 CHECK(dynamic_control);
110
Kelvin Zhangb88b4ba2025-01-31 11:39:30 -0800111 // If we are switching slots, then we are required to MapAllPartitions,
112 // as FinishUpdate() requires all partitions to be mapped.
113 // And switching slots requires FinishUpdate() to be called first
Daniel Chapin2f8976b2025-02-06 11:44:08 -0800114 if (!install_plan_.partitions.empty() ||
115 install_plan_.switch_slot_on_reboot) {
116 if (!dynamic_control->MapAllPartitions()) {
117 LOG(ERROR) << "Failed to map all partitions, this would cause "
118 "FinishUpdate to fail. Abort early.";
119 return CompletePostinstall(ErrorCode::kPostInstallMountError);
Kelvin Zhang8b1e0dc2020-10-26 12:27:53 -0400120 }
121 }
122
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800123 // We always powerwash when rolling back, however policy can determine
124 // if this is a full/normal powerwash, or a special rollback powerwash
125 // that retains a small amount of system state such as enrollment and
126 // network configuration. In both cases all user accounts are deleted.
Daniel Zhengad1eaea2024-07-31 16:09:34 -0700127 if (install_plan_.powerwash_required) {
Kelvin Zhang399bd4d2024-12-03 11:08:30 -0800128 if (hardware_->SchedulePowerwash()) {
Alex Deymofb905d92016-06-03 19:26:58 -0700129 powerwash_scheduled_ = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700130 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700131 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700132 }
133 }
134
Alex Deymo0d298542016-03-30 18:31:49 -0700135 // Initialize all the partition weights.
136 partition_weight_.resize(install_plan_.partitions.size());
137 total_weight_ = 0;
138 for (size_t i = 0; i < install_plan_.partitions.size(); ++i) {
Tianjie Xu087de9d2019-11-01 17:11:22 -0700139 auto& partition = install_plan_.partitions[i];
140 if (!install_plan_.run_post_install && partition.postinstall_optional) {
141 partition.run_postinstall = false;
142 LOG(INFO) << "Skipping optional post-install for partition "
143 << partition.name << " according to install plan.";
144 }
145
Alex Deymo0d298542016-03-30 18:31:49 -0700146 // TODO(deymo): This code sets the weight to all the postinstall commands,
147 // but we could remember how long they took in the past and use those
148 // values.
Tianjie Xu087de9d2019-11-01 17:11:22 -0700149 partition_weight_[i] = partition.run_postinstall;
Alex Deymo0d298542016-03-30 18:31:49 -0700150 total_weight_ += partition_weight_[i];
151 }
152 accumulated_weight_ = 0;
153 ReportProgress(0);
154
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700155 PerformPartitionPostinstall();
156}
157
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700158bool PostinstallRunnerAction::MountPartition(
159 const InstallPlan::Partition& partition) noexcept {
160 // Perform post-install for the current_partition_ partition. At this point we
161 // need to call CompletePartitionPostinstall to complete the operation and
162 // cleanup.
163 const auto mountable_device = partition.readonly_target_path;
164 if (!utils::FileExists(mountable_device.c_str())) {
165 LOG(ERROR) << "Mountable device " << mountable_device << " for partition "
166 << partition.name << " does not exist";
167 return false;
168 }
169
170 if (!utils::FileExists(fs_mount_dir_.c_str())) {
171 LOG(ERROR) << "Mount point " << fs_mount_dir_
172 << " does not exist, mount call will fail";
173 return false;
174 }
175 // Double check that the fs_mount_dir is not busy with a previous mounted
176 // filesystem from a previous crashed postinstall step.
Kelvin Zhang1df000a2022-02-09 16:00:17 -0800177 EnsureUnmounted();
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700178
179#ifdef __ANDROID__
180 // In Chromium OS, the postinstall step is allowed to write to the block
181 // device on the target image, so we don't mark it as read-only and should
182 // be read-write since we just wrote to it during the update.
183
184 // Mark the block device as read-only before mounting for post-install.
185 if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) {
186 return false;
187 }
188#endif // __ANDROID__
189
190 if (!utils::MountFilesystem(
191 mountable_device,
192 fs_mount_dir_,
193 MS_RDONLY,
194 partition.filesystem_type,
195 hardware_->GetPartitionMountOptions(partition.name))) {
196 return false;
197 }
198 return true;
199}
200
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700201void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -0800202 if (install_plan_.download_url.empty()) {
Kelvin Zhang24287c32023-03-09 10:13:26 -0800203 LOG(INFO) << "Skipping post-install";
Alex Deymo390efed2016-02-18 11:00:40 -0800204 return CompletePostinstall(ErrorCode::kSuccess);
205 }
206
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700207 // Skip all the partitions that don't have a post-install step.
208 while (current_partition_ < install_plan_.partitions.size() &&
209 !install_plan_.partitions[current_partition_].run_postinstall) {
210 VLOG(1) << "Skipping post-install on partition "
211 << install_plan_.partitions[current_partition_].name;
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700212 // Attempt to mount a device if it has postinstall script configured, even
213 // if we want to skip running postinstall script.
214 // This is because we've seen bugs like b/198787355 which is only triggered
215 // when you attempt to mount a device. If device fails to mount, it will
216 // likely fail to mount during boot anyway, so it's better to catch any
217 // issues earlier.
218 // It's possible that some of the partitions aren't mountable, but these
219 // partitions shouldn't have postinstall configured. Therefore we guard this
220 // logic with |postinstall_path.empty()|.
221 const auto& partition = install_plan_.partitions[current_partition_];
222 if (!partition.postinstall_path.empty()) {
223 const auto mountable_device = partition.readonly_target_path;
224 if (!MountPartition(partition)) {
225 return CompletePostinstall(ErrorCode::kPostInstallMountError);
226 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800227 LogBuildInfoForPartition(fs_mount_dir_);
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700228 if (!utils::UnmountFilesystem(fs_mount_dir_)) {
229 return CompletePartitionPostinstall(
230 1, "Error unmounting the device " + mountable_device);
231 }
232 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700233 current_partition_++;
234 }
235 if (current_partition_ == install_plan_.partitions.size())
236 return CompletePostinstall(ErrorCode::kSuccess);
237
238 const InstallPlan::Partition& partition =
239 install_plan_.partitions[current_partition_];
240
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400241 const string mountable_device = partition.readonly_target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700242 // Perform post-install for the current_partition_ partition. At this point we
243 // need to call CompletePartitionPostinstall to complete the operation and
244 // cleanup.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700245
Kelvin Zhang06e654a2021-09-10 13:21:00 -0700246 if (!MountPartition(partition)) {
247 CompletePostinstall(ErrorCode::kPostInstallMountError);
248 return;
Kelvin Zhang4ce01102020-11-16 09:32:08 -0500249 }
Kelvin Zhangddc25802021-12-30 13:05:27 -0800250 LogBuildInfoForPartition(fs_mount_dir_);
Alex Deymocbc22742016-03-04 17:53:02 -0800251 base::FilePath postinstall_path(partition.postinstall_path);
252 if (postinstall_path.IsAbsolute()) {
253 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
254 "path instead: "
255 << partition.postinstall_path;
256 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
257 }
258
259 string abs_path =
260 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800261 if (!base::StartsWith(
262 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
263 LOG(ERROR) << "Invalid relative postinstall path: "
264 << partition.postinstall_path;
265 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
266 }
267
Alex Deymo390efed2016-02-18 11:00:40 -0800268 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
Kelvin Zhangbe1c1802021-06-21 10:03:36 -0400269 << abs_path << ") installed on mountable device "
270 << mountable_device;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700271
Alex Deymo032e7722014-03-25 17:53:56 -0700272 // Logs the file format of the postinstall script we are about to run. This
273 // will help debug when the postinstall script doesn't match the architecture
274 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800275 LOG(INFO) << "Format file for new " << partition.postinstall_path
276 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700277
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800278 // Runs the postinstall script asynchronously to free up the main loop while
279 // it's running.
Alex Deymo0d298542016-03-30 18:31:49 -0700280 vector<string> command = {abs_path};
Alex Deymo0d298542016-03-30 18:31:49 -0700281 // In Brillo and Android, we pass the slot number and status fd.
282 command.push_back(std::to_string(install_plan_.target_slot));
283 command.push_back(std::to_string(kPostinstallStatusFd));
Kelvin Zhang44bcf1f2024-12-03 10:54:14 -0800284 // If install plan only contains one partition, notify the script. Most likely
285 // we are scheduled by `triggerPostinstall` API. Certain scripts might want
286 // different behaviors when triggered by `triggerPostinstall` API. For
287 // example, call scheduler API to schedule a postinstall run during
288 // applyPayload(), and only run actual postinstall work if scheduled by
289 // external async scheduler.
290 if (install_plan_.partitions.size() == 1 &&
291 !install_plan_.switch_slot_on_reboot &&
292 install_plan_.download_url.starts_with(kPrefsManifestBytes)) {
293 command.push_back("1");
294 }
Alex Deymo0d298542016-03-30 18:31:49 -0700295
296 current_command_ = Subprocess::Get().ExecFlags(
Alex Deymod15c5462016-03-09 18:11:12 -0800297 command,
Alex Deymo0d298542016-03-30 18:31:49 -0700298 Subprocess::kRedirectStderrToStdout,
299 {kPostinstallStatusFd},
Alex Deymod15c5462016-03-09 18:11:12 -0800300 base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall,
301 base::Unretained(this)));
302 // Subprocess::Exec should never return a negative process id.
303 CHECK_GE(current_command_, 0);
304
Alex Deymo0d298542016-03-30 18:31:49 -0700305 if (!current_command_) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700306 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Alex Deymo0d298542016-03-30 18:31:49 -0700307 return;
308 }
309
310 // Monitor the status file descriptor.
311 progress_fd_ =
312 Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd);
313 int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK;
314 if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) {
315 PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_;
316 }
317
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900318 progress_controller_ = base::FileDescriptorWatcher::WatchReadable(
Alex Deymo0d298542016-03-30 18:31:49 -0700319 progress_fd_,
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900320 base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady,
321 base::Unretained(this)));
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800322}
323
Alex Deymo0d298542016-03-30 18:31:49 -0700324void PostinstallRunnerAction::OnProgressFdReady() {
325 char buf[1024];
Kelvin Zhangb88b4ba2025-01-31 11:39:30 -0800326 size_t bytes_read{};
Alex Deymo0d298542016-03-30 18:31:49 -0700327 do {
328 bytes_read = 0;
Kelvin Zhangb88b4ba2025-01-31 11:39:30 -0800329 bool eof = false;
Alex Deymo0d298542016-03-30 18:31:49 -0700330 bool ok =
Kokoa Matsuda91aa2172024-10-16 16:01:04 +0900331 utils::ReadAll(progress_fd_, buf, std::size(buf), &bytes_read, &eof);
Alex Deymo0d298542016-03-30 18:31:49 -0700332 progress_buffer_.append(buf, bytes_read);
333 // Process every line.
334 vector<string> lines = base::SplitString(
335 progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
336 if (!lines.empty()) {
337 progress_buffer_ = lines.back();
338 lines.pop_back();
339 for (const auto& line : lines) {
340 ProcessProgressLine(line);
341 }
342 }
343 if (!ok || eof) {
344 // There was either an error or an EOF condition, so we are done watching
345 // the file descriptor.
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900346 progress_controller_.reset();
Alex Deymo0d298542016-03-30 18:31:49 -0700347 return;
348 }
349 } while (bytes_read);
350}
351
352bool PostinstallRunnerAction::ProcessProgressLine(const string& line) {
353 double frac = 0;
Alex Deymoa2ea1c22016-08-24 17:26:19 -0700354 if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 &&
355 !std::isnan(frac)) {
Alex Deymo0d298542016-03-30 18:31:49 -0700356 ReportProgress(frac);
357 return true;
358 }
359
360 return false;
361}
362
363void PostinstallRunnerAction::ReportProgress(double frac) {
364 if (!delegate_)
365 return;
Yoshitaka Ishida128936f2018-02-16 18:20:07 +0900366 if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) {
Alex Deymo0d298542016-03-30 18:31:49 -0700367 delegate_->ProgressUpdate(1.);
368 return;
369 }
Alex Deymo44b35672016-04-05 17:57:48 -0700370 if (!std::isfinite(frac) || frac < 0)
Alex Deymo0d298542016-03-30 18:31:49 -0700371 frac = 0;
372 if (frac > 1)
373 frac = 1;
374 double postinst_action_progress =
375 (accumulated_weight_ + partition_weight_[current_partition_] * frac) /
376 total_weight_;
377 delegate_->ProgressUpdate(postinst_action_progress);
378}
379
380void PostinstallRunnerAction::Cleanup() {
Alex Deymo390efed2016-02-18 11:00:40 -0800381 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800382#ifndef __ANDROID__
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500383#if BASE_VER < 800000
384 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) {
385#else
hscham043355b2020-11-17 16:50:10 +0900386 if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) {
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500387#endif
Alex Deymo390efed2016-02-18 11:00:40 -0800388 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700389 }
Kelvin Zhang4aeaa122020-12-04 13:28:47 -0500390#endif
Alex Deymo0d298542016-03-30 18:31:49 -0700391
392 progress_fd_ = -1;
Hidehiko Abe493fecb2019-07-10 23:30:50 +0900393 progress_controller_.reset();
Tianjie55abd3c2020-06-19 00:22:59 -0700394
Alex Deymo0d298542016-03-30 18:31:49 -0700395 progress_buffer_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800396}
397
398void PostinstallRunnerAction::CompletePartitionPostinstall(
399 int return_code, const string& output) {
400 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700401 Cleanup();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700402
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800403 if (return_code != 0) {
404 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700405 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700406
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700407 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700408 // This special return code means that we tried to update firmware,
409 // but couldn't because we booted from FW B, and we need to reboot
410 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700411 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700412 }
Don Garrett81018e02013-07-30 18:46:31 -0700413
414 if (return_code == 4) {
415 // This special return code means that we tried to update firmware,
416 // but couldn't because we booted from FW B, and we need to reboot
417 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700418 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700419 }
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700420
421 // If postinstall script for this partition is optional we can ignore the
422 // result.
423 if (install_plan_.partitions[current_partition_].postinstall_optional) {
424 LOG(INFO) << "Ignoring postinstall failure since it is optional";
425 } else {
426 return CompletePostinstall(error_code);
427 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700428 }
Alex Deymo0d298542016-03-30 18:31:49 -0700429 accumulated_weight_ += partition_weight_[current_partition_];
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700430 current_partition_++;
Alex Deymo0d298542016-03-30 18:31:49 -0700431 ReportProgress(0);
432
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700433 PerformPartitionPostinstall();
434}
435
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000436PostinstallRunnerAction::~PostinstallRunnerAction() {
437 if (!install_plan_.partitions.empty()) {
438 auto dynamic_control = boot_control_->GetDynamicPartitionControl();
439 CHECK(dynamic_control);
440 dynamic_control->UnmapAllPartitions();
441 LOG(INFO) << "Unmapped all partitions.";
442 }
443}
444
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700445void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
446 // We only attempt to mark the new slot as active if all the postinstall
447 // steps succeeded.
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000448 DEFER {
449 if (error_code != ErrorCode::kSuccess &&
450 error_code != ErrorCode::kUpdatedButNotActive) {
Kelvin Zhang99cbbe72024-01-18 14:50:01 -0800451 LOG(ERROR) << "Postinstall action failed. "
452 << utils::ErrorCodeToString(error_code);
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000453
454 // Undo any changes done to trigger Powerwash.
455 if (powerwash_scheduled_)
456 hardware_->CancelPowerwash();
457 }
458 processor_->ActionComplete(this, error_code);
459 };
Sen Jiang02c49422017-10-31 15:14:11 -0700460 if (error_code == ErrorCode::kSuccess) {
461 if (install_plan_.switch_slot_on_reboot) {
Kelvin Zhang19acf4f2024-01-08 21:18:28 +0000462 if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate(
463 install_plan_.powerwash_required) ||
464 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Sen Jiang02c49422017-10-31 15:14:11 -0700465 error_code = ErrorCode::kPostinstallRunnerError;
Tianjie Xud6aa91f2019-11-14 11:55:10 -0800466 } else {
467 // Schedules warm reset on next reboot, ignores the error.
468 hardware_->SetWarmReset(true);
Tianjie838793d2021-01-14 22:05:13 -0800469 // Sets the vbmeta digest for the other slot to boot into.
470 hardware_->SetVbmetaDigestForInactiveSlot(false);
Sen Jiang02c49422017-10-31 15:14:11 -0700471 }
472 } else {
473 error_code = ErrorCode::kUpdatedButNotActive;
474 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700475 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700476
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700477 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700478 if (HasOutputPipe()) {
479 SetOutputObject(install_plan_);
480 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000481}
482
Alex Deymod15c5462016-03-09 18:11:12 -0800483void PostinstallRunnerAction::SuspendAction() {
484 if (!current_command_)
485 return;
486 if (kill(current_command_, SIGSTOP) != 0) {
487 PLOG(ERROR) << "Couldn't pause child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800488 } else {
489 is_current_command_suspended_ = true;
Alex Deymod15c5462016-03-09 18:11:12 -0800490 }
491}
492
493void PostinstallRunnerAction::ResumeAction() {
494 if (!current_command_)
495 return;
496 if (kill(current_command_, SIGCONT) != 0) {
497 PLOG(ERROR) << "Couldn't resume child process " << current_command_;
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800498 } else {
499 is_current_command_suspended_ = false;
Alex Deymod15c5462016-03-09 18:11:12 -0800500 }
501}
502
503void PostinstallRunnerAction::TerminateProcessing() {
504 if (!current_command_)
505 return;
506 // Calling KillExec() will discard the callback we registered and therefore
507 // the unretained reference to this object.
508 Subprocess::Get().KillExec(current_command_);
Ben Chan7f4bc3f2017-01-10 15:32:11 -0800509
510 // If the command has been suspended, resume it after KillExec() so that the
511 // process can process the SIGTERM sent by KillExec().
512 if (is_current_command_suspended_) {
513 ResumeAction();
514 }
515
Alex Deymod15c5462016-03-09 18:11:12 -0800516 current_command_ = 0;
Alex Deymo0d298542016-03-30 18:31:49 -0700517 Cleanup();
Alex Deymod15c5462016-03-09 18:11:12 -0800518}
519
adlr@google.com3defe6a2009-12-04 20:57:17 +0000520} // namespace chromeos_update_engine