blob: 6ebe381230d3fa2104d608443b47b261ddbaff79 [file] [log] [blame]
Daniel Zheng1a01a1c2023-01-30 23:29:50 +00001//
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 Zheng1a01a1c2023-01-30 23:29:50 +000021#include "fastboot_driver.h"
Daniel Zheng47d70a52023-02-14 17:44:40 +000022#include "super_flash_helper.h"
23#include "util.h"
Daniel Zheng1a01a1c2023-01-30 23:29:50 +000024
Daniel Zheng08975262023-04-17 11:34:00 -070025struct FlashingPlan;
26struct Image;
27using ImageEntry = std::pair<const Image*, std::string>;
28
Daniel Zheng29a211c2023-03-10 07:23:49 +000029class FlashTask;
30class RebootTask;
31class UpdateSuperTask;
32class WipeTask;
Daniel Zheng1ef66b72023-08-24 15:44:56 -070033class ResizeTask;
Daniel Zheng1a01a1c2023-01-30 23:29:50 +000034class Task {
35 public:
36 Task() = default;
37 virtual void Run() = 0;
Daniel Zheng1ef66b72023-08-24 15:44:56 -070038 virtual std::string ToString() const = 0;
Daniel Zheng665a4602023-06-05 11:24:59 -070039
Daniel Zheng29a211c2023-03-10 07:23:49 +000040 virtual FlashTask* AsFlashTask() { return nullptr; }
41 virtual RebootTask* AsRebootTask() { return nullptr; }
42 virtual UpdateSuperTask* AsUpdateSuperTask() { return nullptr; }
43 virtual WipeTask* AsWipeTask() { return nullptr; }
Daniel Zheng1ef66b72023-08-24 15:44:56 -070044 virtual ResizeTask* AsResizeTask() { return nullptr; }
Daniel Zheng29a211c2023-03-10 07:23:49 +000045
Daniel Zheng1a01a1c2023-01-30 23:29:50 +000046 virtual ~Task() = default;
47};
Daniel Zheng0d307182023-01-31 21:07:53 +000048
49class FlashTask : public Task {
50 public:
Daniel Zheng403657d2023-03-10 07:37:25 +000051 FlashTask(const std::string& slot, const std::string& pname, const std::string& fname,
Daniel Zheng791a83b2023-05-23 10:42:39 -070052 const bool apply_vbmeta, const FlashingPlan* fp);
Daniel Zheng29a211c2023-03-10 07:23:49 +000053 virtual FlashTask* AsFlashTask() override { return this; }
Daniel Zheng0d307182023-01-31 21:07:53 +000054
Daniel Zheng1ef66b72023-08-24 15:44:56 -070055 static bool IsDynamicParitition(const ImageSource* source, const FlashTask* task);
Daniel Zheng665a4602023-06-05 11:24:59 -070056 void Run() override;
Daniel Zheng1ef66b72023-08-24 15:44:56 -070057 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 Zheng0d307182023-01-31 21:07:53 +000062
63 private:
64 const std::string pname_;
65 const std::string fname_;
66 const std::string slot_;
Daniel Zheng403657d2023-03-10 07:37:25 +000067 const bool apply_vbmeta_;
Daniel Zheng791a83b2023-05-23 10:42:39 -070068 const FlashingPlan* fp_;
Daniel Zheng0d307182023-01-31 21:07:53 +000069};
Daniel Zheng71b3b432023-01-30 17:43:00 +000070
71class RebootTask : public Task {
72 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -070073 RebootTask(const FlashingPlan* fp);
74 RebootTask(const FlashingPlan* fp, const std::string& reboot_target);
Daniel Zheng29a211c2023-03-10 07:23:49 +000075 virtual RebootTask* AsRebootTask() override { return this; }
Daniel Zheng71b3b432023-01-30 17:43:00 +000076 void Run() override;
Daniel Zheng1ef66b72023-08-24 15:44:56 -070077 std::string ToString() const override;
78 std::string GetTarget() const { return reboot_target_; };
Daniel Zheng71b3b432023-01-30 17:43:00 +000079
80 private:
81 const std::string reboot_target_ = "";
Daniel Zheng43987c92023-03-07 18:20:53 +000082 const FlashingPlan* fp_;
Daniel Zheng47d70a52023-02-14 17:44:40 +000083};
84
Daniel Zheng3e048572023-06-21 15:21:06 -070085class OptimizedFlashSuperTask : public Task {
Daniel Zheng47d70a52023-02-14 17:44:40 +000086 public:
Daniel Zheng3e048572023-06-21 15:21:06 -070087 OptimizedFlashSuperTask(const std::string& super_name, std::unique_ptr<SuperFlashHelper> helper,
88 SparsePtr sparse_layout, uint64_t super_size, const FlashingPlan* fp);
Daniel Zheng1ef66b72023-08-24 15:44:56 -070089
90 static std::unique_ptr<OptimizedFlashSuperTask> Initialize(
Daniel Zheng29a211c2023-03-10 07:23:49 +000091 const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks);
Daniel Zheng1ef66b72023-08-24 15:44:56 -070092 static bool CanOptimize(const ImageSource* source,
93 const std::vector<std::unique_ptr<Task>>& tasks);
94
Daniel Zheng47d70a52023-02-14 17:44:40 +000095 using ImageEntry = std::pair<const Image*, std::string>;
96 void Run() override;
Daniel Zheng1ef66b72023-08-24 15:44:56 -070097 std::string ToString() const override;
Daniel Zheng47d70a52023-02-14 17:44:40 +000098
99 private:
100 const std::string super_name_;
101 std::unique_ptr<SuperFlashHelper> helper_;
102 SparsePtr sparse_layout_;
David Anderson74c78072023-03-16 21:21:28 -0700103 uint64_t super_size_;
Daniel Zheng5769b262023-06-21 14:41:11 -0700104 const FlashingPlan* fp_;
Daniel Zheng47d70a52023-02-14 17:44:40 +0000105};
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000106
107class UpdateSuperTask : public Task {
108 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700109 UpdateSuperTask(const FlashingPlan* fp);
Daniel Zheng29a211c2023-03-10 07:23:49 +0000110 virtual UpdateSuperTask* AsUpdateSuperTask() override { return this; }
111
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000112 void Run() override;
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700113 std::string ToString() const override;
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000114
115 private:
Daniel Zheng43987c92023-03-07 18:20:53 +0000116 const FlashingPlan* fp_;
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000117};
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000118
119class ResizeTask : public Task {
120 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700121 ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000122 const std::string& slot);
123 void Run() override;
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700124 std::string ToString() const override;
125 virtual ResizeTask* AsResizeTask() override { return this; }
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000126
127 private:
Daniel Zheng43987c92023-03-07 18:20:53 +0000128 const FlashingPlan* fp_;
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000129 const std::string pname_;
130 const std::string size_;
131 const std::string slot_;
132};
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000133
134class DeleteTask : public Task {
135 public:
Daniel Zheng5a9905a2023-05-23 10:51:01 -0700136 DeleteTask(const FlashingPlan* fp, const std::string& pname);
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000137 void Run() override;
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700138 std::string ToString() const override;
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000139
140 private:
Daniel Zheng43987c92023-03-07 18:20:53 +0000141 const FlashingPlan* fp_;
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000142 const std::string pname_;
143};
Daniel Zheng15e77832023-03-13 17:09:43 +0000144
145class WipeTask : public Task {
146 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700147 WipeTask(const FlashingPlan* fp, const std::string& pname);
Daniel Zheng29a211c2023-03-10 07:23:49 +0000148 virtual WipeTask* AsWipeTask() override { return this; }
Daniel Zheng15e77832023-03-13 17:09:43 +0000149 void Run() override;
Daniel Zheng1ef66b72023-08-24 15:44:56 -0700150 std::string ToString() const override;
Daniel Zheng15e77832023-03-13 17:09:43 +0000151
152 private:
153 const FlashingPlan* fp_;
154 const std::string pname_;
155};