blob: 0278ea517023a6f7eaace099b71a90e4960ab4f4 [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#ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_INSTALL_PLAN_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_INSTALL_PLAN_H_
adlr@google.com3defe6a2009-12-04 20:57:17 +000019
20#include <string>
Darin Petkov698d0412010-10-13 10:59:44 -070021#include <vector>
22
Ben Chan05735a12014-09-03 07:48:22 -070023#include <base/macros.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024#include <brillo/secure_blob.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/action.h"
27#include "update_engine/common/boot_control_interface.h"
Chris Sosad317e402013-06-12 13:47:09 -070028
adlr@google.com3defe6a2009-12-04 20:57:17 +000029// InstallPlan is a simple struct that contains relevant info for many
30// parts of the update system about the install that should happen.
adlr@google.com3defe6a2009-12-04 20:57:17 +000031namespace chromeos_update_engine {
32
Alex Deymo64d98782016-02-05 18:03:48 -080033enum class InstallPayloadType {
34 kUnknown,
35 kFull,
36 kDelta,
37};
Jay Srinivasan738fdf32012-12-07 17:40:54 -080038
Alex Deymo64d98782016-02-05 18:03:48 -080039std::string InstallPayloadTypeToString(InstallPayloadType type);
40
41struct InstallPlan {
Alex Deymo763e7db2015-08-27 21:08:08 -070042 InstallPlan() = default;
Jay Srinivasanae4697c2013-03-18 17:08:08 -070043
44 bool operator==(const InstallPlan& that) const;
45 bool operator!=(const InstallPlan& that) const;
46
47 void Dump() const;
Amin Hassani23795032020-11-24 14:38:55 -080048 std::string ToString() const;
adlr@google.com3defe6a2009-12-04 20:57:17 +000049
Kelvin Zhang20982a52021-08-13 12:31:16 -070050 private:
Yifan Hong1d9077f2018-12-07 12:09:37 -080051 // Loads the |source_path| and |target_path| of all |partitions| based on the
Alex Deymoe5e5fe92015-10-05 09:28:19 -070052 // |source_slot| and |target_slot| if available. Returns whether it succeeded
53 // to load all the partitions for the valid slots.
Alex Deymo706a5ab2015-11-23 17:48:30 -030054 bool LoadPartitionsFromSlots(BootControlInterface* boot_control);
Kelvin Zhang20982a52021-08-13 12:31:16 -070055 template <typename PartitinoUpdateArray>
56 static bool ParseManifestToInstallPlan(const PartitinoUpdateArray& partitions,
57 BootControlInterface* boot_control,
58 size_t block_size,
59 InstallPlan* install_plan,
60 ErrorCode* error);
61
62 public:
63 // Load all partitions in |partitions| into this install plan, will also
64 // populate |source_path|, |target_pathh|, fec information, partition sizes.
65 bool ParsePartitions(const std::vector<PartitionUpdate>& partitions,
66 BootControlInterface* boot_control,
67 size_t block_size,
68 ErrorCode* error);
69 bool ParsePartitions(
70 const google::protobuf::RepeatedPtrField<PartitionUpdate>& partitions,
71 BootControlInterface* boot_control,
72 size_t block_size,
73 ErrorCode* error);
Alex Deymo763e7db2015-08-27 21:08:08 -070074
75 bool is_resume{false};
adlr@google.com3defe6a2009-12-04 20:57:17 +000076 std::string download_url; // url to download from
Chris Sosafb1020e2013-07-29 17:27:33 -070077 std::string version; // version we are installing.
Jay Srinivasan51dcf262012-09-13 17:24:32 -070078
Sen Jiang0affc2c2017-02-10 15:55:05 -080079 struct Payload {
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080080 std::vector<std::string> payload_urls; // URLs to download the payload
Vyshu852f57d2020-10-09 17:35:14 +000081 uint64_t size = 0; // size of the payload
82 uint64_t metadata_size = 0; // size of the metadata
Sen Jiang0affc2c2017-02-10 15:55:05 -080083 std::string metadata_signature; // signature of the metadata in base64
84 brillo::Blob hash; // SHA256 hash of the payload
Sen Jiangcdd52062017-05-18 15:33:10 -070085 InstallPayloadType type{InstallPayloadType::kUnknown};
Vyshu Khota4c5413d2020-11-04 16:17:25 -080086 std::string fp; // fingerprint value unique to the payload
87 std::string app_id; // App ID of the payload
Sen Jiang5ae865b2017-04-18 14:24:40 -070088 // Only download manifest and fill in partitions in install plan without
89 // apply the payload if true. Will be set by DownloadAction when resuming
90 // multi-payload.
91 bool already_applied = false;
Sen Jiang0affc2c2017-02-10 15:55:05 -080092
93 bool operator==(const Payload& that) const {
Jae Hoon Kim8da11e22019-12-23 11:26:17 -080094 return payload_urls == that.payload_urls && size == that.size &&
95 metadata_size == that.metadata_size &&
Sen Jiang5ae865b2017-04-18 14:24:40 -070096 metadata_signature == that.metadata_signature &&
Sen Jiangcdd52062017-05-18 15:33:10 -070097 hash == that.hash && type == that.type &&
Vyshu Khota4c5413d2020-11-04 16:17:25 -080098 already_applied == that.already_applied && fp == that.fp &&
99 app_id == that.app_id;
Sen Jiang0affc2c2017-02-10 15:55:05 -0800100 }
101 };
102 std::vector<Payload> payloads;
Alex Deymo763e7db2015-08-27 21:08:08 -0700103
104 // The partition slots used for the update.
105 BootControlInterface::Slot source_slot{BootControlInterface::kInvalidSlot};
106 BootControlInterface::Slot target_slot{BootControlInterface::kInvalidSlot};
107
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700108 // The vector below is used for partition verification. The flow is:
Darin Petkov3aefa862010-12-07 14:45:00 -0800109 //
Sen Jiangfef85fd2016-03-25 15:32:49 -0700110 // 1. DownloadAction fills in the expected source and target partition sizes
111 // and hashes based on the manifest.
Darin Petkov3aefa862010-12-07 14:45:00 -0800112 //
Sen Jiangfef85fd2016-03-25 15:32:49 -0700113 // 2. FilesystemVerifierAction computes and verifies the partition sizes and
114 // hashes against the expected values.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700115 struct Partition {
116 bool operator==(const Partition& that) const;
117
118 // The name of the partition.
119 std::string name;
120
121 std::string source_path;
122 uint64_t source_size{0};
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700123 brillo::Blob source_hash;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700124
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500125 // |target_path| is intended to be a path to block device, which you can
126 // open with |open| syscall and perform regular unix style read/write.
127 // For VABC, this will be empty. As you can't read/write VABC devices with
128 // regular syscall.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700129 std::string target_path;
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500130 // |mountable_target_device| is intended to be a path to block device which
131 // can be used for mounting this block device's underlying filesystem.
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -0400132 std::string readonly_target_path;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700133 uint64_t target_size{0};
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700134 brillo::Blob target_hash;
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500135
Sen Jiang57f91802017-11-14 17:42:13 -0800136 uint32_t block_size{0};
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700137
Alex Deymo390efed2016-02-18 11:00:40 -0800138 // Whether we should run the postinstall script from this partition and the
139 // postinstall parameters.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700140 bool run_postinstall{false};
Alex Deymo390efed2016-02-18 11:00:40 -0800141 std::string postinstall_path;
142 std::string filesystem_type;
Alex Deymo5b91c6b2016-08-04 20:33:36 -0700143 bool postinstall_optional{false};
Sen Jiang57f91802017-11-14 17:42:13 -0800144
145 // Verity hash tree and FEC config. See update_metadata.proto for details.
146 // All offsets and sizes are in bytes.
147 uint64_t hash_tree_data_offset{0};
148 uint64_t hash_tree_data_size{0};
149 uint64_t hash_tree_offset{0};
150 uint64_t hash_tree_size{0};
151 std::string hash_tree_algorithm;
152 brillo::Blob hash_tree_salt;
153
154 uint64_t fec_data_offset{0};
155 uint64_t fec_data_size{0};
156 uint64_t fec_offset{0};
157 uint64_t fec_size{0};
158 uint32_t fec_roots{0};
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700159 };
160 std::vector<Partition> partitions;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000161
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800162 // True if payload hash checks are mandatory based on the system state and
163 // the Omaha response.
Alex Deymo763e7db2015-08-27 21:08:08 -0700164 bool hash_checks_mandatory{false};
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800165
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700166 // True if Powerwash is required on reboot after applying the payload.
167 // False otherwise.
Alex Deymo763e7db2015-08-27 21:08:08 -0700168 bool powerwash_required{false};
David Zeuthene7f89172013-10-31 10:21:04 -0700169
Sen Jiang02c49422017-10-31 15:14:11 -0700170 // True if the updated slot should be marked active on success.
171 // False otherwise.
172 bool switch_slot_on_reboot{true};
173
174 // True if the update should run its post-install step.
175 // False otherwise.
176 bool run_post_install{true};
177
Marton Hunyady199152d2018-05-07 19:08:48 +0200178 // True if this update is a rollback.
179 bool is_rollback{false};
180
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800181 // True if this rollback should preserve some system data.
182 bool rollback_data_save_requested{false};
183
Sen Jiang3eeaf7d2018-10-11 13:55:32 -0700184 // True if the update should write verity.
185 // False otherwise.
186 bool write_verity{true};
187
David Zeuthene7f89172013-10-31 10:21:04 -0700188 // If not blank, a base-64 encoded representation of the PEM-encoded
189 // public key in the response.
190 std::string public_key_rsa;
Tianjie24f96092020-06-30 12:26:25 -0700191
192 // The name of dynamic partitions not included in the payload. Only used
193 // for partial updates.
194 std::vector<std::string> untouched_dynamic_partitions;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000195};
196
Chris Sosad317e402013-06-12 13:47:09 -0700197class InstallPlanAction;
198
Amin Hassani008c4582019-01-13 16:22:47 -0800199template <>
Chris Sosad317e402013-06-12 13:47:09 -0700200class ActionTraits<InstallPlanAction> {
201 public:
202 // Takes the install plan as input
203 typedef InstallPlan InputObjectType;
204 // Passes the install plan as output
205 typedef InstallPlan OutputObjectType;
206};
207
208// Basic action that only receives and sends Install Plans.
209// Can be used to construct an Install Plan to send to any other Action that
210// accept an InstallPlan.
211class InstallPlanAction : public Action<InstallPlanAction> {
212 public:
213 InstallPlanAction() {}
Amin Hassani008c4582019-01-13 16:22:47 -0800214 explicit InstallPlanAction(const InstallPlan& install_plan)
215 : install_plan_(install_plan) {}
Chris Sosad317e402013-06-12 13:47:09 -0700216
Alex Deymo610277e2014-11-11 21:18:11 -0800217 void PerformAction() override {
Chris Sosad317e402013-06-12 13:47:09 -0700218 if (HasOutputPipe()) {
219 SetOutputObject(install_plan_);
220 }
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700221 processor_->ActionComplete(this, ErrorCode::kSuccess);
Chris Sosad317e402013-06-12 13:47:09 -0700222 }
223
Chris Sosa76a29ae2013-07-11 17:59:24 -0700224 InstallPlan* install_plan() { return &install_plan_; }
225
226 static std::string StaticType() { return "InstallPlanAction"; }
Alex Deymo610277e2014-11-11 21:18:11 -0800227 std::string Type() const override { return StaticType(); }
Chris Sosad317e402013-06-12 13:47:09 -0700228
229 typedef ActionTraits<InstallPlanAction>::InputObjectType InputObjectType;
230 typedef ActionTraits<InstallPlanAction>::OutputObjectType OutputObjectType;
231
Kelvin Zhangd2da7b12020-07-07 17:19:21 -0400232 protected:
Chris Sosad317e402013-06-12 13:47:09 -0700233 InstallPlan install_plan_;
234
Kelvin Zhangd2da7b12020-07-07 17:19:21 -0400235 private:
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700236 DISALLOW_COPY_AND_ASSIGN(InstallPlanAction);
Chris Sosad317e402013-06-12 13:47:09 -0700237};
238
adlr@google.com3defe6a2009-12-04 20:57:17 +0000239} // namespace chromeos_update_engine
240
Alex Deymo39910dc2015-11-09 17:04:30 -0800241#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_INSTALL_PLAN_H_