blob: 766b27cf6e874593a2ea41856fa55ef8bb267cc2 [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>
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090022#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
Alex Deymo64d98782016-02-05 18:03:48 -080032string InstallPayloadTypeToString(InstallPayloadType type) {
33 switch (type) {
34 case InstallPayloadType::kUnknown:
35 return "unknown";
36 case InstallPayloadType::kFull:
37 return "full";
38 case InstallPayloadType::kDelta:
39 return "delta";
40 }
41 return "invalid type";
42}
Jay Srinivasanae4697c2013-03-18 17:08:08 -070043
44bool InstallPlan::operator==(const InstallPlan& that) const {
45 return ((is_resume == that.is_resume) &&
Sen Jiang0affc2c2017-02-10 15:55:05 -080046 (download_url == that.download_url) && (payloads == that.payloads) &&
Alex Deymo763e7db2015-08-27 21:08:08 -070047 (source_slot == that.source_slot) &&
Sen Jiang0affc2c2017-02-10 15:55:05 -080048 (target_slot == that.target_slot) && (partitions == that.partitions));
Jay Srinivasanae4697c2013-03-18 17:08:08 -070049}
50
51bool InstallPlan::operator!=(const InstallPlan& that) const {
52 return !((*this) == that);
53}
54
55void InstallPlan::Dump() const {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070056 string partitions_str;
57 for (const auto& partition : partitions) {
Alex Deymo390efed2016-02-18 11:00:40 -080058 partitions_str +=
59 base::StringPrintf(", part: %s (source_size: %" PRIu64
60 ", target_size %" PRIu64 ", postinst:%s)",
61 partition.name.c_str(),
62 partition.source_size,
63 partition.target_size,
64 utils::ToString(partition.run_postinstall).c_str());
Alex Deymoe5e5fe92015-10-05 09:28:19 -070065 }
Sen Jiang0affc2c2017-02-10 15:55:05 -080066 string payloads_str;
67 for (const auto& payload : payloads) {
68 payloads_str += base::StringPrintf(
69 ", payload: (size: %" PRIu64 ", metadata_size: %" PRIu64
Sen Jiangcdd52062017-05-18 15:33:10 -070070 ", metadata signature: %s, hash: %s, payload type: %s)",
Sen Jiang0affc2c2017-02-10 15:55:05 -080071 payload.size,
72 payload.metadata_size,
73 payload.metadata_signature.c_str(),
Sen Jiangcdd52062017-05-18 15:33:10 -070074 base::HexEncode(payload.hash.data(), payload.hash.size()).c_str(),
75 InstallPayloadTypeToString(payload.type).c_str());
Sen Jiang0affc2c2017-02-10 15:55:05 -080076 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -070077
Aaron Wood7dcdedf2017-09-06 17:17:41 -070078 string version_str = base::StringPrintf(", version: %s", version.c_str());
79 if (!system_version.empty()) {
80 version_str +=
81 base::StringPrintf(", system_version: %s", system_version.c_str());
82 }
83
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090084 string url_str = download_url;
85 if (base::StartsWith(
86 url_str, "fd://", base::CompareCase::INSENSITIVE_ASCII)) {
87 int fd = std::stoi(url_str.substr(strlen("fd://")));
88 url_str = utils::GetFilePath(fd);
89 }
90
Sen Jiang2703ef42017-03-16 13:36:21 -070091 LOG(INFO) << "InstallPlan: " << (is_resume ? "resume" : "new_update")
Aaron Wood7dcdedf2017-09-06 17:17:41 -070092 << version_str
Alex Deymo763e7db2015-08-27 21:08:08 -070093 << ", source_slot: " << BootControlInterface::SlotName(source_slot)
94 << ", target_slot: " << BootControlInterface::SlotName(target_slot)
Kyeongkab.Nam500ca132019-06-26 13:48:07 +090095 << ", url: " << url_str << payloads_str << partitions_str
Sen Jiang2703ef42017-03-16 13:36:21 -070096 << ", hash_checks_mandatory: "
97 << utils::ToString(hash_checks_mandatory)
Sen Jiang02c49422017-10-31 15:14:11 -070098 << ", powerwash_required: " << utils::ToString(powerwash_required)
99 << ", switch_slot_on_reboot: "
100 << utils::ToString(switch_slot_on_reboot)
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700101 << ", run_post_install: " << utils::ToString(run_post_install)
102 << ", is_rollback: " << utils::ToString(is_rollback)
103 << ", write_verity: " << utils::ToString(write_verity);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700104}
105
Alex Deymo706a5ab2015-11-23 17:48:30 -0300106bool InstallPlan::LoadPartitionsFromSlots(BootControlInterface* boot_control) {
Alex Deymo763e7db2015-08-27 21:08:08 -0700107 bool result = true;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700108 for (Partition& partition : partitions) {
Hridya Valsarajue69ca5f2019-02-25 22:33:56 -0800109 if (source_slot != BootControlInterface::kInvalidSlot &&
110 partition.source_size > 0) {
Alex Deymo706a5ab2015-11-23 17:48:30 -0300111 result = boot_control->GetPartitionDevice(
Yifan Hong1d9077f2018-12-07 12:09:37 -0800112 partition.name, source_slot, &partition.source_path) &&
113 result;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700114 } else {
115 partition.source_path.clear();
116 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700117
Yifan Hong537802d2018-08-15 13:15:42 -0700118 if (target_slot != BootControlInterface::kInvalidSlot &&
119 partition.target_size > 0) {
Alex Deymo706a5ab2015-11-23 17:48:30 -0300120 result = boot_control->GetPartitionDevice(
Yifan Hong1d9077f2018-12-07 12:09:37 -0800121 partition.name, target_slot, &partition.target_path) &&
122 result;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700123 } else {
124 partition.target_path.clear();
125 }
Alex Deymo763e7db2015-08-27 21:08:08 -0700126 }
127 return result;
128}
129
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700130bool InstallPlan::Partition::operator==(
131 const InstallPlan::Partition& that) const {
Amin Hassani008c4582019-01-13 16:22:47 -0800132 return (name == that.name && source_path == that.source_path &&
133 source_size == that.source_size && source_hash == that.source_hash &&
134 target_path == that.target_path && target_size == that.target_size &&
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700135 target_hash == that.target_hash &&
Alex Deymo390efed2016-02-18 11:00:40 -0800136 run_postinstall == that.run_postinstall &&
137 postinstall_path == that.postinstall_path &&
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700138 filesystem_type == that.filesystem_type &&
139 postinstall_optional == that.postinstall_optional);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700140}
141
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700142} // namespace chromeos_update_engine