blob: 4a37836f0fb4a94781df3ea7d4b19d68df9a3b1d [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2013 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//
Jay Srinivasanae4697c2013-03-18 17:08:08 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/install_plan.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070018
Alex Deymoe5e5fe92015-10-05 09:28:19 -070019#include <base/format_macros.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080020#include <base/logging.h>
Sen Jiang2703ef42017-03-16 13:36:21 -070021#include <base/strings/string_number_conversions.h>
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080022#include <base/strings/string_util.h>
Alex Deymoe5e5fe92015-10-05 09:28:19 -070023#include <base/strings/stringprintf.h>
Jay Srinivasanae4697c2013-03-18 17:08:08 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/utils.h"
26#include "update_engine/payload_consumer/payload_constants.h"
Jay Srinivasanae4697c2013-03-18 17:08:08 -070027
28using std::string;
29
30namespace chromeos_update_engine {
31
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080032string PayloadUrlsToString(
33 const decltype(InstallPlan::Payload::payload_urls)& payload_urls) {
34 return "(" + base::JoinString(payload_urls, ",") + ")";
35}
36
Alex Deymo64d98782016-02-05 18:03:48 -080037string InstallPayloadTypeToString(InstallPayloadType type) {
38 switch (type) {
39 case InstallPayloadType::kUnknown:
40 return "unknown";
41 case InstallPayloadType::kFull:
42 return "full";
43 case InstallPayloadType::kDelta:
44 return "delta";
45 }
46 return "invalid type";
47}
Jay Srinivasanae4697c2013-03-18 17:08:08 -070048
49bool InstallPlan::operator==(const InstallPlan& that) const {
50 return ((is_resume == that.is_resume) &&
Sen Jiang0affc2c2017-02-10 15:55:05 -080051 (download_url == that.download_url) && (payloads == that.payloads) &&
Alex Deymo763e7db2015-08-27 21:08:08 -070052 (source_slot == that.source_slot) &&
Sen Jiang0affc2c2017-02-10 15:55:05 -080053 (target_slot == that.target_slot) && (partitions == that.partitions));
Jay Srinivasanae4697c2013-03-18 17:08:08 -070054}
55
56bool InstallPlan::operator!=(const InstallPlan& that) const {
57 return !((*this) == that);
58}
59
60void InstallPlan::Dump() const {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070061 string partitions_str;
62 for (const auto& partition : partitions) {
Alex Deymo390efed2016-02-18 11:00:40 -080063 partitions_str +=
64 base::StringPrintf(", part: %s (source_size: %" PRIu64
65 ", target_size %" PRIu64 ", postinst:%s)",
66 partition.name.c_str(),
67 partition.source_size,
68 partition.target_size,
69 utils::ToString(partition.run_postinstall).c_str());
Alex Deymoe5e5fe92015-10-05 09:28:19 -070070 }
Sen Jiang0affc2c2017-02-10 15:55:05 -080071 string payloads_str;
72 for (const auto& payload : payloads) {
73 payloads_str += base::StringPrintf(
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080074 ", payload: (urls: %s, size: %" PRIu64 ", metadata_size: %" PRIu64
Sen Jiangcdd52062017-05-18 15:33:10 -070075 ", metadata signature: %s, hash: %s, payload type: %s)",
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080076 PayloadUrlsToString(payload.payload_urls).c_str(),
Sen Jiang0affc2c2017-02-10 15:55:05 -080077 payload.size,
78 payload.metadata_size,
79 payload.metadata_signature.c_str(),
Sen Jiangcdd52062017-05-18 15:33:10 -070080 base::HexEncode(payload.hash.data(), payload.hash.size()).c_str(),
81 InstallPayloadTypeToString(payload.type).c_str());
Sen Jiang0affc2c2017-02-10 15:55:05 -080082 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -070083
Aaron Wood7dcdedf2017-09-06 17:17:41 -070084 string version_str = base::StringPrintf(", version: %s", version.c_str());
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090085 string url_str = download_url;
86 if (base::StartsWith(
87 url_str, "fd://", base::CompareCase::INSENSITIVE_ASCII)) {
88 int fd = std::stoi(url_str.substr(strlen("fd://")));
89 url_str = utils::GetFilePath(fd);
90 }
91
Sen Jiang2703ef42017-03-16 13:36:21 -070092 LOG(INFO) << "InstallPlan: " << (is_resume ? "resume" : "new_update")
Aaron Wood7dcdedf2017-09-06 17:17:41 -070093 << version_str
Alex Deymo763e7db2015-08-27 21:08:08 -070094 << ", source_slot: " << BootControlInterface::SlotName(source_slot)
95 << ", target_slot: " << BootControlInterface::SlotName(target_slot)
Amin Hassanie53b39b2020-09-16 11:19:28 -070096 << ", initial url: " << url_str << payloads_str << partitions_str
97 << ", hash_checks_mandatory: "
Sen Jiang2703ef42017-03-16 13:36:21 -070098 << utils::ToString(hash_checks_mandatory)
Sen Jiang02c49422017-10-31 15:14:11 -070099 << ", powerwash_required: " << utils::ToString(powerwash_required)
100 << ", switch_slot_on_reboot: "
101 << utils::ToString(switch_slot_on_reboot)
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700102 << ", run_post_install: " << utils::ToString(run_post_install)
103 << ", is_rollback: " << utils::ToString(is_rollback)
104 << ", write_verity: " << utils::ToString(write_verity);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700105}
106
Alex Deymo706a5ab2015-11-23 17:48:30 -0300107bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700108 bool result = true;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700109 for (Partition& partition : partitions) {
Hridya Valsarajue69ca5f2019-02-25 22:33:56 -0800110 if (source_slot != BootControlInterface::kInvalidSlot &&
111 partition.source_size > 0) {
Alex Deymo706a5ab2015-11-23 17:48:30 -0300112 result = boot_control->GetPartitionDevice(
Yifan Hong1d9077f2018-12-07 12:09:37 -0800113 partition.name, source_slot, &partition.source_path) &&
114 result;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700115 } else {
116 partition.source_path.clear();
117 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700118
Yifan Hong537802d2018-08-15 13:15:42 -0700119 if (target_slot != BootControlInterface::kInvalidSlot &&
120 partition.target_size > 0) {
Alex Deymo706a5ab2015-11-23 17:48:30 -0300121 result = boot_control->GetPartitionDevice(
Yifan Hong1d9077f2018-12-07 12:09:37 -0800122 partition.name, target_slot, &partition.target_path) &&
123 result;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700124 } else {
125 partition.target_path.clear();
126 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700127 }
128 return result;
129}
130
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700131bool InstallPlan::Partition::operator==(
132 const InstallPlan::Partition& that) const {
Amin Hassani008c4582019-01-13 16:22:47 -0800133 return (name == that.name && source_path == that.source_path &&
134 source_size == that.source_size && source_hash == that.source_hash &&
135 target_path == that.target_path && target_size == that.target_size &&
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700136 target_hash == that.target_hash &&
Alex Deymo390efed2016-02-18 11:00:40 -0800137 run_postinstall == that.run_postinstall &&
138 postinstall_path == that.postinstall_path &&
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700139 filesystem_type == that.filesystem_type &&
140 postinstall_optional == that.postinstall_optional);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700141}
142
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700143} // namespace chromeos_update_engine