blob: f7c8801f92393469b0ddc3d7249564eb7e2c14ab [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;
33
Daniel Zheng1a01a1c2023-01-30 23:29:50 +000034class Task {
35 public:
36 Task() = default;
37 virtual void Run() = 0;
Daniel Zheng665a4602023-06-05 11:24:59 -070038 virtual std::string ToString() = 0;
39
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; }
44
Daniel Zheng1a01a1c2023-01-30 23:29:50 +000045 virtual ~Task() = default;
46};
Daniel Zheng0d307182023-01-31 21:07:53 +000047
48class FlashTask : public Task {
49 public:
Daniel Zheng403657d2023-03-10 07:37:25 +000050 FlashTask(const std::string& slot, const std::string& pname, const std::string& fname,
Daniel Zheng791a83b2023-05-23 10:42:39 -070051 const bool apply_vbmeta, const FlashingPlan* fp);
Daniel Zheng29a211c2023-03-10 07:23:49 +000052 virtual FlashTask* AsFlashTask() override { return this; }
Daniel Zheng0d307182023-01-31 21:07:53 +000053
Daniel Zheng665a4602023-06-05 11:24:59 -070054 void Run() override;
55 std::string ToString() override;
Daniel Zheng29a211c2023-03-10 07:23:49 +000056 std::string GetPartition() { return pname_; }
57 std::string GetImageName() { return fname_; }
Daniel Zheng29a211c2023-03-10 07:23:49 +000058 std::string GetSlot() { return slot_; }
Daniel Zheng665a4602023-06-05 11:24:59 -070059 std::string GetPartitionAndSlot();
Daniel Zheng0d307182023-01-31 21:07:53 +000060
61 private:
62 const std::string pname_;
63 const std::string fname_;
64 const std::string slot_;
Daniel Zheng403657d2023-03-10 07:37:25 +000065 const bool apply_vbmeta_;
Daniel Zheng791a83b2023-05-23 10:42:39 -070066 const FlashingPlan* fp_;
Daniel Zheng0d307182023-01-31 21:07:53 +000067};
Daniel Zheng71b3b432023-01-30 17:43:00 +000068
69class RebootTask : public Task {
70 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -070071 RebootTask(const FlashingPlan* fp);
72 RebootTask(const FlashingPlan* fp, const std::string& reboot_target);
Daniel Zheng29a211c2023-03-10 07:23:49 +000073 virtual RebootTask* AsRebootTask() override { return this; }
Daniel Zheng71b3b432023-01-30 17:43:00 +000074 void Run() override;
Daniel Zheng665a4602023-06-05 11:24:59 -070075 std::string ToString() override;
Daniel Zheng71b3b432023-01-30 17:43:00 +000076
77 private:
78 const std::string reboot_target_ = "";
Daniel Zheng43987c92023-03-07 18:20:53 +000079 const FlashingPlan* fp_;
Daniel Zheng47d70a52023-02-14 17:44:40 +000080};
81
Daniel Zheng3e048572023-06-21 15:21:06 -070082class OptimizedFlashSuperTask : public Task {
Daniel Zheng47d70a52023-02-14 17:44:40 +000083 public:
Daniel Zheng3e048572023-06-21 15:21:06 -070084 OptimizedFlashSuperTask(const std::string& super_name, std::unique_ptr<SuperFlashHelper> helper,
85 SparsePtr sparse_layout, uint64_t super_size, const FlashingPlan* fp);
86 static std::unique_ptr<OptimizedFlashSuperTask> Initialize(const FlashingPlan* fp,
87 std::vector<ImageEntry>& os_images);
88 static std::unique_ptr<OptimizedFlashSuperTask> InitializeFromTasks(
Daniel Zheng29a211c2023-03-10 07:23:49 +000089 const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks);
Daniel Zheng47d70a52023-02-14 17:44:40 +000090 using ImageEntry = std::pair<const Image*, std::string>;
91 void Run() override;
Daniel Zheng665a4602023-06-05 11:24:59 -070092 std::string ToString() override;
Daniel Zheng47d70a52023-02-14 17:44:40 +000093
94 private:
95 const std::string super_name_;
96 std::unique_ptr<SuperFlashHelper> helper_;
97 SparsePtr sparse_layout_;
David Anderson74c78072023-03-16 21:21:28 -070098 uint64_t super_size_;
Daniel Zheng5769b262023-06-21 14:41:11 -070099 const FlashingPlan* fp_;
Daniel Zheng47d70a52023-02-14 17:44:40 +0000100};
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000101
102class UpdateSuperTask : public Task {
103 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700104 UpdateSuperTask(const FlashingPlan* fp);
Daniel Zheng29a211c2023-03-10 07:23:49 +0000105 virtual UpdateSuperTask* AsUpdateSuperTask() override { return this; }
106
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000107 void Run() override;
Daniel Zheng665a4602023-06-05 11:24:59 -0700108 std::string ToString() override;
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000109
110 private:
Daniel Zheng43987c92023-03-07 18:20:53 +0000111 const FlashingPlan* fp_;
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000112};
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000113
114class ResizeTask : public Task {
115 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700116 ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000117 const std::string& slot);
118 void Run() override;
Daniel Zheng665a4602023-06-05 11:24:59 -0700119 std::string ToString() override;
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000120
121 private:
Daniel Zheng43987c92023-03-07 18:20:53 +0000122 const FlashingPlan* fp_;
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000123 const std::string pname_;
124 const std::string size_;
125 const std::string slot_;
126};
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000127
128class DeleteTask : public Task {
129 public:
Daniel Zheng5a9905a2023-05-23 10:51:01 -0700130 DeleteTask(const FlashingPlan* fp, const std::string& pname);
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000131 void Run() override;
Daniel Zheng665a4602023-06-05 11:24:59 -0700132 std::string ToString() override;
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000133
134 private:
Daniel Zheng43987c92023-03-07 18:20:53 +0000135 const FlashingPlan* fp_;
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000136 const std::string pname_;
137};
Daniel Zheng15e77832023-03-13 17:09:43 +0000138
139class WipeTask : public Task {
140 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700141 WipeTask(const FlashingPlan* fp, const std::string& pname);
Daniel Zheng29a211c2023-03-10 07:23:49 +0000142 virtual WipeTask* AsWipeTask() override { return this; }
Daniel Zheng15e77832023-03-13 17:09:43 +0000143 void Run() override;
Daniel Zheng665a4602023-06-05 11:24:59 -0700144 std::string ToString() override;
Daniel Zheng15e77832023-03-13 17:09:43 +0000145
146 private:
147 const FlashingPlan* fp_;
148 const std::string pname_;
149};