| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
 | 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_INSTALL_PLAN_H_ | 
 | 18 | #define UPDATE_ENGINE_PAYLOAD_CONSUMER_INSTALL_PLAN_H_ | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 |  | 
 | 20 | #include <string> | 
| Darin Petkov | 698d041 | 2010-10-13 10:59:44 -0700 | [diff] [blame] | 21 | #include <vector> | 
 | 22 |  | 
| Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 23 | #include <base/macros.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 24 | #include <brillo/secure_blob.h> | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 25 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/action.h" | 
 | 27 | #include "update_engine/common/boot_control_interface.h" | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 28 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 29 | // 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 31 | namespace chromeos_update_engine { | 
 | 32 |  | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 33 | enum class InstallPayloadType { | 
 | 34 |   kUnknown, | 
 | 35 |   kFull, | 
 | 36 |   kDelta, | 
 | 37 | }; | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 38 |  | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 39 | std::string InstallPayloadTypeToString(InstallPayloadType type); | 
 | 40 |  | 
 | 41 | struct InstallPlan { | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 42 |   InstallPlan() = default; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 43 |  | 
 | 44 |   bool operator==(const InstallPlan& that) const; | 
 | 45 |   bool operator!=(const InstallPlan& that) const; | 
 | 46 |  | 
 | 47 |   void Dump() const; | 
| Amin Hassani | 2379503 | 2020-11-24 14:38:55 -0800 | [diff] [blame] | 48 |   std::string ToString() const; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 49 |  | 
| Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 50 |  private: | 
| Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 51 |   // Loads the |source_path| and |target_path| of all |partitions| based on the | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 52 |   // |source_slot| and |target_slot| if available. Returns whether it succeeded | 
 | 53 |   // to load all the partitions for the valid slots. | 
| Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 54 |   bool LoadPartitionsFromSlots(BootControlInterface* boot_control); | 
| Kelvin Zhang | 20982a5 | 2021-08-13 12:31:16 -0700 | [diff] [blame] | 55 |   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 Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 74 |  | 
 | 75 |   bool is_resume{false}; | 
| Kelvin Zhang | a7407b5 | 2023-03-13 15:05:14 -0700 | [diff] [blame] | 76 |   bool vabc_none{false}; | 
| Daniel Zheng | 9fc62b8 | 2023-03-24 22:57:20 +0000 | [diff] [blame] | 77 |   bool disable_vabc{false}; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 78 |   std::string download_url;  // url to download from | 
| Chris Sosa | fb1020e | 2013-07-29 17:27:33 -0700 | [diff] [blame] | 79 |   std::string version;       // version we are installing. | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 80 |  | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 81 |   struct Payload { | 
| Jae Hoon Kim | 8da11e2 | 2019-12-23 11:26:17 -0800 | [diff] [blame] | 82 |     std::vector<std::string> payload_urls;  // URLs to download the payload | 
| Vyshu | 852f57d | 2020-10-09 17:35:14 +0000 | [diff] [blame] | 83 |     uint64_t size = 0;                      // size of the payload | 
 | 84 |     uint64_t metadata_size = 0;             // size of the metadata | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 85 |     std::string metadata_signature;  // signature of the metadata in base64 | 
 | 86 |     brillo::Blob hash;               // SHA256 hash of the payload | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 87 |     InstallPayloadType type{InstallPayloadType::kUnknown}; | 
| Vyshu Khota | 4c5413d | 2020-11-04 16:17:25 -0800 | [diff] [blame] | 88 |     std::string fp;      // fingerprint value unique to the payload | 
 | 89 |     std::string app_id;  // App ID of the payload | 
| Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 90 |     // Only download manifest and fill in partitions in install plan without | 
 | 91 |     // apply the payload if true. Will be set by DownloadAction when resuming | 
 | 92 |     // multi-payload. | 
 | 93 |     bool already_applied = false; | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 94 |  | 
 | 95 |     bool operator==(const Payload& that) const { | 
| Jae Hoon Kim | 8da11e2 | 2019-12-23 11:26:17 -0800 | [diff] [blame] | 96 |       return payload_urls == that.payload_urls && size == that.size && | 
 | 97 |              metadata_size == that.metadata_size && | 
| Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 98 |              metadata_signature == that.metadata_signature && | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 99 |              hash == that.hash && type == that.type && | 
| Vyshu Khota | 4c5413d | 2020-11-04 16:17:25 -0800 | [diff] [blame] | 100 |              already_applied == that.already_applied && fp == that.fp && | 
 | 101 |              app_id == that.app_id; | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 102 |     } | 
 | 103 |   }; | 
 | 104 |   std::vector<Payload> payloads; | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 105 |  | 
 | 106 |   // The partition slots used for the update. | 
 | 107 |   BootControlInterface::Slot source_slot{BootControlInterface::kInvalidSlot}; | 
 | 108 |   BootControlInterface::Slot target_slot{BootControlInterface::kInvalidSlot}; | 
 | 109 |  | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 110 |   // The vector below is used for partition verification. The flow is: | 
| Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 111 |   // | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 112 |   // 1. DownloadAction fills in the expected source and target partition sizes | 
 | 113 |   // and hashes based on the manifest. | 
| Darin Petkov | 3aefa86 | 2010-12-07 14:45:00 -0800 | [diff] [blame] | 114 |   // | 
| Sen Jiang | fef85fd | 2016-03-25 15:32:49 -0700 | [diff] [blame] | 115 |   // 2. FilesystemVerifierAction computes and verifies the partition sizes and | 
 | 116 |   // hashes against the expected values. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 117 |   struct Partition { | 
 | 118 |     bool operator==(const Partition& that) const; | 
 | 119 |  | 
 | 120 |     // The name of the partition. | 
 | 121 |     std::string name; | 
 | 122 |  | 
 | 123 |     std::string source_path; | 
 | 124 |     uint64_t source_size{0}; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 125 |     brillo::Blob source_hash; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 126 |  | 
| Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 127 |     // |target_path| is intended to be a path to block device, which you can | 
 | 128 |     // open with |open| syscall and perform regular unix style read/write. | 
 | 129 |     // For VABC, this will be empty. As you can't read/write VABC devices with | 
 | 130 |     // regular syscall. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 131 |     std::string target_path; | 
| Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 132 |     // |mountable_target_device| is intended to be a path to block device which | 
 | 133 |     // can be used for mounting this block device's underlying filesystem. | 
| Kelvin Zhang | a9b5d8c | 2021-05-05 09:17:46 -0400 | [diff] [blame] | 134 |     std::string readonly_target_path; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 135 |     uint64_t target_size{0}; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 136 |     brillo::Blob target_hash; | 
| Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 137 |  | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 138 |     uint32_t block_size{0}; | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 139 |  | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 140 |     // Whether we should run the postinstall script from this partition and the | 
 | 141 |     // postinstall parameters. | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 142 |     bool run_postinstall{false}; | 
| Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 143 |     std::string postinstall_path; | 
 | 144 |     std::string filesystem_type; | 
| Alex Deymo | 5b91c6b | 2016-08-04 20:33:36 -0700 | [diff] [blame] | 145 |     bool postinstall_optional{false}; | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 146 |  | 
 | 147 |     // Verity hash tree and FEC config. See update_metadata.proto for details. | 
 | 148 |     // All offsets and sizes are in bytes. | 
 | 149 |     uint64_t hash_tree_data_offset{0}; | 
 | 150 |     uint64_t hash_tree_data_size{0}; | 
 | 151 |     uint64_t hash_tree_offset{0}; | 
 | 152 |     uint64_t hash_tree_size{0}; | 
 | 153 |     std::string hash_tree_algorithm; | 
 | 154 |     brillo::Blob hash_tree_salt; | 
 | 155 |  | 
 | 156 |     uint64_t fec_data_offset{0}; | 
 | 157 |     uint64_t fec_data_size{0}; | 
 | 158 |     uint64_t fec_offset{0}; | 
 | 159 |     uint64_t fec_size{0}; | 
 | 160 |     uint32_t fec_roots{0}; | 
| Kelvin Zhang | 91e839c | 2022-04-05 14:25:00 -0700 | [diff] [blame] | 161 |  | 
 | 162 |     bool ParseVerityConfig(const PartitionUpdate&); | 
| Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 163 |   }; | 
 | 164 |   std::vector<Partition> partitions; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 165 |  | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 166 |   // True if payload hash checks are mandatory based on the system state and | 
 | 167 |   // the Omaha response. | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 168 |   bool hash_checks_mandatory{false}; | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 169 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 170 |   // True if Powerwash is required on reboot after applying the payload. | 
 | 171 |   // False otherwise. | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 172 |   bool powerwash_required{false}; | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 173 |  | 
| Kelvin Zhang | 85a6d99 | 2023-10-23 10:40:58 -0700 | [diff] [blame] | 174 |   // True if and only if this is an SPL downgrade OTA | 
 | 175 |   bool spl_downgrade{false}; | 
 | 176 |  | 
| Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 177 |   // True if the updated slot should be marked active on success. | 
 | 178 |   // False otherwise. | 
 | 179 |   bool switch_slot_on_reboot{true}; | 
 | 180 |  | 
 | 181 |   // True if the update should run its post-install step. | 
 | 182 |   // False otherwise. | 
 | 183 |   bool run_post_install{true}; | 
 | 184 |  | 
| Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 185 |   // True if this update is a rollback. | 
 | 186 |   bool is_rollback{false}; | 
 | 187 |  | 
| Zentaro Kavanagh | 28def4f | 2019-01-15 17:15:01 -0800 | [diff] [blame] | 188 |   // True if this rollback should preserve some system data. | 
 | 189 |   bool rollback_data_save_requested{false}; | 
 | 190 |  | 
| Sen Jiang | 3eeaf7d | 2018-10-11 13:55:32 -0700 | [diff] [blame] | 191 |   // True if the update should write verity. | 
 | 192 |   // False otherwise. | 
 | 193 |   bool write_verity{true}; | 
 | 194 |  | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 195 |   // If not blank, a base-64 encoded representation of the PEM-encoded | 
 | 196 |   // public key in the response. | 
 | 197 |   std::string public_key_rsa; | 
| Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 198 |  | 
 | 199 |   // The name of dynamic partitions not included in the payload. Only used | 
 | 200 |   // for partial updates. | 
 | 201 |   std::vector<std::string> untouched_dynamic_partitions; | 
| Kelvin Zhang | 6bef490 | 2023-02-22 12:43:27 -0800 | [diff] [blame] | 202 |  | 
 | 203 |   // Whether to batch write operations for COW | 
 | 204 |   bool batched_writes = false; | 
 | 205 |  | 
 | 206 |   // Whether to enable multi-threaded compression on COW writes | 
| Kelvin Zhang | 944cdac | 2023-12-12 12:55:27 -0800 | [diff] [blame] | 207 |   std::optional<bool> enable_threading; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 208 | }; | 
 | 209 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 210 | class InstallPlanAction; | 
 | 211 |  | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 212 | template <> | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 213 | class ActionTraits<InstallPlanAction> { | 
 | 214 |  public: | 
 | 215 |   // Takes the install plan as input | 
 | 216 |   typedef InstallPlan InputObjectType; | 
 | 217 |   // Passes the install plan as output | 
 | 218 |   typedef InstallPlan OutputObjectType; | 
 | 219 | }; | 
 | 220 |  | 
 | 221 | // Basic action that only receives and sends Install Plans. | 
 | 222 | // Can be used to construct an Install Plan to send to any other Action that | 
 | 223 | // accept an InstallPlan. | 
 | 224 | class InstallPlanAction : public Action<InstallPlanAction> { | 
 | 225 |  public: | 
 | 226 |   InstallPlanAction() {} | 
| Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 227 |   explicit InstallPlanAction(const InstallPlan& install_plan) | 
 | 228 |       : install_plan_(install_plan) {} | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 229 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 230 |   void PerformAction() override { | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 231 |     if (HasOutputPipe()) { | 
 | 232 |       SetOutputObject(install_plan_); | 
 | 233 |     } | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 234 |     processor_->ActionComplete(this, ErrorCode::kSuccess); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 235 |   } | 
 | 236 |  | 
| Chris Sosa | 76a29ae | 2013-07-11 17:59:24 -0700 | [diff] [blame] | 237 |   InstallPlan* install_plan() { return &install_plan_; } | 
 | 238 |  | 
 | 239 |   static std::string StaticType() { return "InstallPlanAction"; } | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 240 |   std::string Type() const override { return StaticType(); } | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 241 |  | 
 | 242 |   typedef ActionTraits<InstallPlanAction>::InputObjectType InputObjectType; | 
 | 243 |   typedef ActionTraits<InstallPlanAction>::OutputObjectType OutputObjectType; | 
 | 244 |  | 
| Kelvin Zhang | d2da7b1 | 2020-07-07 17:19:21 -0400 | [diff] [blame] | 245 |  protected: | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 246 |   InstallPlan install_plan_; | 
 | 247 |  | 
| Kelvin Zhang | d2da7b1 | 2020-07-07 17:19:21 -0400 | [diff] [blame] | 248 |  private: | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 249 |   DISALLOW_COPY_AND_ASSIGN(InstallPlanAction); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 250 | }; | 
 | 251 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 252 | }  // namespace chromeos_update_engine | 
 | 253 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 254 | #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_INSTALL_PLAN_H_ |