| Daniel Zheng | 1a01a1c | 2023-01-30 23:29:50 +0000 | [diff] [blame] | 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 <sstream> | 
|  | 19 | #include <string> | 
|  | 20 |  | 
| Daniel Zheng | 1a01a1c | 2023-01-30 23:29:50 +0000 | [diff] [blame] | 21 | #include "fastboot_driver.h" | 
| Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 22 | #include "super_flash_helper.h" | 
|  | 23 | #include "util.h" | 
| Daniel Zheng | 1a01a1c | 2023-01-30 23:29:50 +0000 | [diff] [blame] | 24 |  | 
| Daniel Zheng | 0897526 | 2023-04-17 11:34:00 -0700 | [diff] [blame] | 25 | struct FlashingPlan; | 
|  | 26 | struct Image; | 
|  | 27 | using ImageEntry = std::pair<const Image*, std::string>; | 
|  | 28 |  | 
| Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 29 | class FlashTask; | 
|  | 30 | class RebootTask; | 
|  | 31 | class UpdateSuperTask; | 
|  | 32 | class WipeTask; | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 33 | class ResizeTask; | 
| Daniel Zheng | 1a01a1c | 2023-01-30 23:29:50 +0000 | [diff] [blame] | 34 | class Task { | 
|  | 35 | public: | 
|  | 36 | Task() = default; | 
|  | 37 | virtual void Run() = 0; | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 38 | virtual std::string ToString() const = 0; | 
| Daniel Zheng | 665a460 | 2023-06-05 11:24:59 -0700 | [diff] [blame] | 39 |  | 
| Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 40 | virtual FlashTask* AsFlashTask() { return nullptr; } | 
|  | 41 | virtual RebootTask* AsRebootTask() { return nullptr; } | 
|  | 42 | virtual UpdateSuperTask* AsUpdateSuperTask() { return nullptr; } | 
|  | 43 | virtual WipeTask* AsWipeTask() { return nullptr; } | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 44 | virtual ResizeTask* AsResizeTask() { return nullptr; } | 
| Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 45 |  | 
| Daniel Zheng | 1a01a1c | 2023-01-30 23:29:50 +0000 | [diff] [blame] | 46 | virtual ~Task() = default; | 
|  | 47 | }; | 
| Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 48 |  | 
|  | 49 | class FlashTask : public Task { | 
|  | 50 | public: | 
| Daniel Zheng | 403657d | 2023-03-10 07:37:25 +0000 | [diff] [blame] | 51 | FlashTask(const std::string& slot, const std::string& pname, const std::string& fname, | 
| Daniel Zheng | 791a83b | 2023-05-23 10:42:39 -0700 | [diff] [blame] | 52 | const bool apply_vbmeta, const FlashingPlan* fp); | 
| Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 53 | virtual FlashTask* AsFlashTask() override { return this; } | 
| Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 54 |  | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 55 | static bool IsDynamicParitition(const ImageSource* source, const FlashTask* task); | 
| Daniel Zheng | 665a460 | 2023-06-05 11:24:59 -0700 | [diff] [blame] | 56 | void Run() override; | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 57 | std::string ToString() const override; | 
|  | 58 | std::string GetPartition() const { return pname_; } | 
|  | 59 | std::string GetImageName() const { return fname_; } | 
|  | 60 | std::string GetSlot() const { return slot_; } | 
|  | 61 | std::string GetPartitionAndSlot() const; | 
| Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 62 |  | 
|  | 63 | private: | 
|  | 64 | const std::string pname_; | 
|  | 65 | const std::string fname_; | 
|  | 66 | const std::string slot_; | 
| Daniel Zheng | 403657d | 2023-03-10 07:37:25 +0000 | [diff] [blame] | 67 | const bool apply_vbmeta_; | 
| Daniel Zheng | 791a83b | 2023-05-23 10:42:39 -0700 | [diff] [blame] | 68 | const FlashingPlan* fp_; | 
| Daniel Zheng | 0d30718 | 2023-01-31 21:07:53 +0000 | [diff] [blame] | 69 | }; | 
| Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 70 |  | 
|  | 71 | class RebootTask : public Task { | 
|  | 72 | public: | 
| Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 73 | RebootTask(const FlashingPlan* fp); | 
|  | 74 | RebootTask(const FlashingPlan* fp, const std::string& reboot_target); | 
| Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 75 | virtual RebootTask* AsRebootTask() override { return this; } | 
| Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 76 | void Run() override; | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 77 | std::string ToString() const override; | 
|  | 78 | std::string GetTarget() const { return reboot_target_; }; | 
| Daniel Zheng | 71b3b43 | 2023-01-30 17:43:00 +0000 | [diff] [blame] | 79 |  | 
|  | 80 | private: | 
|  | 81 | const std::string reboot_target_ = ""; | 
| Daniel Zheng | 43987c9 | 2023-03-07 18:20:53 +0000 | [diff] [blame] | 82 | const FlashingPlan* fp_; | 
| Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 83 | }; | 
|  | 84 |  | 
| Daniel Zheng | 3e04857 | 2023-06-21 15:21:06 -0700 | [diff] [blame] | 85 | class OptimizedFlashSuperTask : public Task { | 
| Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 86 | public: | 
| Daniel Zheng | 3e04857 | 2023-06-21 15:21:06 -0700 | [diff] [blame] | 87 | OptimizedFlashSuperTask(const std::string& super_name, std::unique_ptr<SuperFlashHelper> helper, | 
|  | 88 | SparsePtr sparse_layout, uint64_t super_size, const FlashingPlan* fp); | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 89 |  | 
|  | 90 | static std::unique_ptr<OptimizedFlashSuperTask> Initialize( | 
| Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 91 | const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks); | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 92 | static bool CanOptimize(const ImageSource* source, | 
|  | 93 | const std::vector<std::unique_ptr<Task>>& tasks); | 
|  | 94 |  | 
| Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 95 | using ImageEntry = std::pair<const Image*, std::string>; | 
|  | 96 | void Run() override; | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 97 | std::string ToString() const override; | 
| Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 98 |  | 
|  | 99 | private: | 
|  | 100 | const std::string super_name_; | 
|  | 101 | std::unique_ptr<SuperFlashHelper> helper_; | 
|  | 102 | SparsePtr sparse_layout_; | 
| David Anderson | 74c7807 | 2023-03-16 21:21:28 -0700 | [diff] [blame] | 103 | uint64_t super_size_; | 
| Daniel Zheng | 5769b26 | 2023-06-21 14:41:11 -0700 | [diff] [blame] | 104 | const FlashingPlan* fp_; | 
| Daniel Zheng | 47d70a5 | 2023-02-14 17:44:40 +0000 | [diff] [blame] | 105 | }; | 
| Daniel Zheng | 6bb8baa | 2023-03-03 07:14:23 +0000 | [diff] [blame] | 106 |  | 
|  | 107 | class UpdateSuperTask : public Task { | 
|  | 108 | public: | 
| Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 109 | UpdateSuperTask(const FlashingPlan* fp); | 
| Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 110 | virtual UpdateSuperTask* AsUpdateSuperTask() override { return this; } | 
|  | 111 |  | 
| Daniel Zheng | 6bb8baa | 2023-03-03 07:14:23 +0000 | [diff] [blame] | 112 | void Run() override; | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 113 | std::string ToString() const override; | 
| Daniel Zheng | 6bb8baa | 2023-03-03 07:14:23 +0000 | [diff] [blame] | 114 |  | 
|  | 115 | private: | 
| Daniel Zheng | 43987c9 | 2023-03-07 18:20:53 +0000 | [diff] [blame] | 116 | const FlashingPlan* fp_; | 
| Daniel Zheng | 6bb8baa | 2023-03-03 07:14:23 +0000 | [diff] [blame] | 117 | }; | 
| Daniel Zheng | 9f7bf7e | 2023-03-03 07:16:46 +0000 | [diff] [blame] | 118 |  | 
|  | 119 | class ResizeTask : public Task { | 
|  | 120 | public: | 
| Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 121 | ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size, | 
| Daniel Zheng | 9f7bf7e | 2023-03-03 07:16:46 +0000 | [diff] [blame] | 122 | const std::string& slot); | 
|  | 123 | void Run() override; | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 124 | std::string ToString() const override; | 
|  | 125 | virtual ResizeTask* AsResizeTask() override { return this; } | 
| Daniel Zheng | 9f7bf7e | 2023-03-03 07:16:46 +0000 | [diff] [blame] | 126 |  | 
|  | 127 | private: | 
| Daniel Zheng | 43987c9 | 2023-03-07 18:20:53 +0000 | [diff] [blame] | 128 | const FlashingPlan* fp_; | 
| Daniel Zheng | 9f7bf7e | 2023-03-03 07:16:46 +0000 | [diff] [blame] | 129 | const std::string pname_; | 
|  | 130 | const std::string size_; | 
|  | 131 | const std::string slot_; | 
|  | 132 | }; | 
| Daniel Zheng | aa70f4c | 2023-03-07 18:59:51 +0000 | [diff] [blame] | 133 |  | 
|  | 134 | class DeleteTask : public Task { | 
|  | 135 | public: | 
| Daniel Zheng | 5a9905a | 2023-05-23 10:51:01 -0700 | [diff] [blame] | 136 | DeleteTask(const FlashingPlan* fp, const std::string& pname); | 
| Daniel Zheng | aa70f4c | 2023-03-07 18:59:51 +0000 | [diff] [blame] | 137 | void Run() override; | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 138 | std::string ToString() const override; | 
| Daniel Zheng | aa70f4c | 2023-03-07 18:59:51 +0000 | [diff] [blame] | 139 |  | 
|  | 140 | private: | 
| Daniel Zheng | 43987c9 | 2023-03-07 18:20:53 +0000 | [diff] [blame] | 141 | const FlashingPlan* fp_; | 
| Daniel Zheng | aa70f4c | 2023-03-07 18:59:51 +0000 | [diff] [blame] | 142 | const std::string pname_; | 
|  | 143 | }; | 
| Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 144 |  | 
|  | 145 | class WipeTask : public Task { | 
|  | 146 | public: | 
| Daniel Zheng | e6dbd4f | 2023-04-10 09:53:08 -0700 | [diff] [blame] | 147 | WipeTask(const FlashingPlan* fp, const std::string& pname); | 
| Daniel Zheng | 29a211c | 2023-03-10 07:23:49 +0000 | [diff] [blame] | 148 | virtual WipeTask* AsWipeTask() override { return this; } | 
| Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 149 | void Run() override; | 
| Daniel Zheng | 1ef66b7 | 2023-08-24 15:44:56 -0700 | [diff] [blame] | 150 | std::string ToString() const override; | 
| Daniel Zheng | 15e7783 | 2023-03-13 17:09:43 +0000 | [diff] [blame] | 151 |  | 
|  | 152 | private: | 
|  | 153 | const FlashingPlan* fp_; | 
|  | 154 | const std::string pname_; | 
|  | 155 | }; |