• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
16 
17 #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_INSTALL_PLAN_H_
18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_INSTALL_PLAN_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include <base/macros.h>
24 #include <brillo/secure_blob.h>
25 
26 #include "update_engine/common/action.h"
27 #include "update_engine/common/boot_control_interface.h"
28 
29 // InstallPlan is a simple struct that contains relevant info for many
30 // parts of the update system about the install that should happen.
31 namespace chromeos_update_engine {
32 
33 enum class InstallPayloadType {
34   kUnknown,
35   kFull,
36   kDelta,
37 };
38 
39 std::string InstallPayloadTypeToString(InstallPayloadType type);
40 
41 struct InstallPlan {
42   InstallPlan() = default;
43 
44   bool operator==(const InstallPlan& that) const;
45   bool operator!=(const InstallPlan& that) const;
46 
47   void Dump() const;
48   std::string ToString() const;
49 
50  private:
51   // Loads the |source_path| and |target_path| of all |partitions| based on the
52   // |source_slot| and |target_slot| if available. Returns whether it succeeded
53   // to load all the partitions for the valid slots.
54   bool LoadPartitionsFromSlots(BootControlInterface* boot_control);
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);
74 
75   bool is_resume{false};
76   std::string download_url;  // url to download from
77   std::string version;       // version we are installing.
78 
79   struct Payload {
80     std::vector<std::string> payload_urls;  // URLs to download the payload
81     uint64_t size = 0;                      // size of the payload
82     uint64_t metadata_size = 0;             // size of the metadata
83     std::string metadata_signature;  // signature of the metadata in base64
84     brillo::Blob hash;               // SHA256 hash of the payload
85     InstallPayloadType type{InstallPayloadType::kUnknown};
86     std::string fp;      // fingerprint value unique to the payload
87     std::string app_id;  // App ID of the payload
88     // 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;
92 
93     bool operator==(const Payload& that) const {
94       return payload_urls == that.payload_urls && size == that.size &&
95              metadata_size == that.metadata_size &&
96              metadata_signature == that.metadata_signature &&
97              hash == that.hash && type == that.type &&
98              already_applied == that.already_applied && fp == that.fp &&
99              app_id == that.app_id;
100     }
101   };
102   std::vector<Payload> payloads;
103 
104   // The partition slots used for the update.
105   BootControlInterface::Slot source_slot{BootControlInterface::kInvalidSlot};
106   BootControlInterface::Slot target_slot{BootControlInterface::kInvalidSlot};
107 
108   // The vector below is used for partition verification. The flow is:
109   //
110   // 1. DownloadAction fills in the expected source and target partition sizes
111   // and hashes based on the manifest.
112   //
113   // 2. FilesystemVerifierAction computes and verifies the partition sizes and
114   // hashes against the expected values.
115   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};
123     brillo::Blob source_hash;
124 
125     // |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.
129     std::string target_path;
130     // |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.
132     std::string readonly_target_path;
133     uint64_t target_size{0};
134     brillo::Blob target_hash;
135 
136     uint32_t block_size{0};
137 
138     // Whether we should run the postinstall script from this partition and the
139     // postinstall parameters.
140     bool run_postinstall{false};
141     std::string postinstall_path;
142     std::string filesystem_type;
143     bool postinstall_optional{false};
144 
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};
159 
160     bool ParseVerityConfig(const PartitionUpdate&);
161   };
162   std::vector<Partition> partitions;
163 
164   // True if payload hash checks are mandatory based on the system state and
165   // the Omaha response.
166   bool hash_checks_mandatory{false};
167 
168   // True if Powerwash is required on reboot after applying the payload.
169   // False otherwise.
170   bool powerwash_required{false};
171 
172   // True if the updated slot should be marked active on success.
173   // False otherwise.
174   bool switch_slot_on_reboot{true};
175 
176   // True if the update should run its post-install step.
177   // False otherwise.
178   bool run_post_install{true};
179 
180   // True if this update is a rollback.
181   bool is_rollback{false};
182 
183   // True if this rollback should preserve some system data.
184   bool rollback_data_save_requested{false};
185 
186   // True if the update should write verity.
187   // False otherwise.
188   bool write_verity{true};
189 
190   // If not blank, a base-64 encoded representation of the PEM-encoded
191   // public key in the response.
192   std::string public_key_rsa;
193 
194   // The name of dynamic partitions not included in the payload. Only used
195   // for partial updates.
196   std::vector<std::string> untouched_dynamic_partitions;
197 };
198 
199 class InstallPlanAction;
200 
201 template <>
202 class ActionTraits<InstallPlanAction> {
203  public:
204   // Takes the install plan as input
205   typedef InstallPlan InputObjectType;
206   // Passes the install plan as output
207   typedef InstallPlan OutputObjectType;
208 };
209 
210 // Basic action that only receives and sends Install Plans.
211 // Can be used to construct an Install Plan to send to any other Action that
212 // accept an InstallPlan.
213 class InstallPlanAction : public Action<InstallPlanAction> {
214  public:
InstallPlanAction()215   InstallPlanAction() {}
InstallPlanAction(const InstallPlan & install_plan)216   explicit InstallPlanAction(const InstallPlan& install_plan)
217       : install_plan_(install_plan) {}
218 
PerformAction()219   void PerformAction() override {
220     if (HasOutputPipe()) {
221       SetOutputObject(install_plan_);
222     }
223     processor_->ActionComplete(this, ErrorCode::kSuccess);
224   }
225 
install_plan()226   InstallPlan* install_plan() { return &install_plan_; }
227 
StaticType()228   static std::string StaticType() { return "InstallPlanAction"; }
Type()229   std::string Type() const override { return StaticType(); }
230 
231   typedef ActionTraits<InstallPlanAction>::InputObjectType InputObjectType;
232   typedef ActionTraits<InstallPlanAction>::OutputObjectType OutputObjectType;
233 
234  protected:
235   InstallPlan install_plan_;
236 
237  private:
238   DISALLOW_COPY_AND_ASSIGN(InstallPlanAction);
239 };
240 
241 }  // namespace chromeos_update_engine
242 
243 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_INSTALL_PLAN_H_
244