1 //
2 // Copyright (C) 2020 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 #pragma once
17 
18 #include <string>
19 
20 #include "super_flash_helper.h"
21 #include "util.h"
22 
23 struct FlashingPlan;
24 struct Image;
25 using ImageEntry = std::pair<const Image*, std::string>;
26 
27 class FlashTask;
28 class RebootTask;
29 class UpdateSuperTask;
30 class OptimizedFlashSuperTask;
31 class WipeTask;
32 class ResizeTask;
33 class Task {
34   public:
35     Task() = default;
36     virtual void Run() = 0;
37     virtual std::string ToString() const = 0;
38 
AsFlashTask()39     virtual FlashTask* AsFlashTask() { return nullptr; }
AsRebootTask()40     virtual RebootTask* AsRebootTask() { return nullptr; }
AsUpdateSuperTask()41     virtual UpdateSuperTask* AsUpdateSuperTask() { return nullptr; }
AsOptimizedFlashSuperTask()42     virtual OptimizedFlashSuperTask* AsOptimizedFlashSuperTask() { return nullptr; }
AsWipeTask()43     virtual WipeTask* AsWipeTask() { return nullptr; }
AsResizeTask()44     virtual ResizeTask* AsResizeTask() { return nullptr; }
45 
46     virtual ~Task() = default;
47 };
48 
49 class FlashTask : public Task {
50   public:
51     FlashTask(const std::string& slot, const std::string& pname, const std::string& fname,
52               const bool apply_vbmeta, const FlashingPlan* fp);
AsFlashTask()53     virtual FlashTask* AsFlashTask() override { return this; }
54 
55     static bool IsDynamicPartition(const ImageSource* source, const FlashTask* task);
56     void Run() override;
57     std::string ToString() const override;
GetPartition()58     std::string GetPartition() const { return pname_; }
GetImageName()59     std::string GetImageName() const { return fname_; }
GetSlot()60     std::string GetSlot() const { return slot_; }
61     std::string GetPartitionAndSlot() const;
62 
63   private:
64     const std::string pname_;
65     const std::string fname_;
66     const std::string slot_;
67     const bool apply_vbmeta_;
68     const FlashingPlan* fp_;
69 };
70 
71 class RebootTask : public Task {
72   public:
73     RebootTask(const FlashingPlan* fp);
74     RebootTask(const FlashingPlan* fp, const std::string& reboot_target);
AsRebootTask()75     virtual RebootTask* AsRebootTask() override { return this; }
76     void Run() override;
77     std::string ToString() const override;
GetTarget()78     std::string GetTarget() const { return reboot_target_; };
79 
80   private:
81     const std::string reboot_target_ = "";
82     const FlashingPlan* fp_;
83 };
84 
85 class OptimizedFlashSuperTask : public Task {
86   public:
87     OptimizedFlashSuperTask(const std::string& super_name, std::unique_ptr<SuperFlashHelper> helper,
88                             SparsePtr sparse_layout, uint64_t super_size, const FlashingPlan* fp);
AsOptimizedFlashSuperTask()89     virtual OptimizedFlashSuperTask* AsOptimizedFlashSuperTask() override { return this; }
90 
91     static std::unique_ptr<OptimizedFlashSuperTask> Initialize(
92             const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks);
93     static bool CanOptimize(const ImageSource* source,
94                             const std::vector<std::unique_ptr<Task>>& tasks);
95 
96     void Run() override;
97     std::string ToString() const override;
98 
99   private:
100     const std::string super_name_;
101     std::unique_ptr<SuperFlashHelper> helper_;
102     SparsePtr sparse_layout_;
103     uint64_t super_size_;
104     const FlashingPlan* fp_;
105 };
106 
107 class UpdateSuperTask : public Task {
108   public:
109     UpdateSuperTask(const FlashingPlan* fp);
AsUpdateSuperTask()110     virtual UpdateSuperTask* AsUpdateSuperTask() override { return this; }
111 
112     void Run() override;
113     std::string ToString() const override;
114 
115   private:
116     const FlashingPlan* fp_;
117 };
118 
119 class ResizeTask : public Task {
120   public:
121     ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
122                const std::string& slot);
123     void Run() override;
124     std::string ToString() const override;
AsResizeTask()125     virtual ResizeTask* AsResizeTask() override { return this; }
126 
127   private:
128     const FlashingPlan* fp_;
129     const std::string pname_;
130     const std::string size_;
131     const std::string slot_;
132 };
133 
134 class DeleteTask : public Task {
135   public:
136     DeleteTask(const FlashingPlan* fp, const std::string& pname);
137     void Run() override;
138     std::string ToString() const override;
139 
140   private:
141     const FlashingPlan* fp_;
142     const std::string pname_;
143 };
144 
145 class WipeTask : public Task {
146   public:
147     WipeTask(const FlashingPlan* fp, const std::string& pname);
AsWipeTask()148     virtual WipeTask* AsWipeTask() override { return this; }
149     void Run() override;
150     std::string ToString() const override;
151 
152   private:
153     const FlashingPlan* fp_;
154     const std::string pname_;
155 };
156