blob: 34e3e92aaebd55d63574b54181df53bd98004c63 [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 Zheng29a211c2023-03-10 07:23:49 +000038 virtual FlashTask* AsFlashTask() { return nullptr; }
39 virtual RebootTask* AsRebootTask() { return nullptr; }
40 virtual UpdateSuperTask* AsUpdateSuperTask() { return nullptr; }
41 virtual WipeTask* AsWipeTask() { return nullptr; }
42
Daniel Zheng1a01a1c2023-01-30 23:29:50 +000043 virtual ~Task() = default;
44};
Daniel Zheng0d307182023-01-31 21:07:53 +000045
46class FlashTask : public Task {
47 public:
Daniel Zheng403657d2023-03-10 07:37:25 +000048 FlashTask(const std::string& slot, const std::string& pname, const std::string& fname,
49 const bool apply_vbmeta);
Daniel Zheng29a211c2023-03-10 07:23:49 +000050 virtual FlashTask* AsFlashTask() override { return this; }
Daniel Zheng0d307182023-01-31 21:07:53 +000051
Daniel Zheng29a211c2023-03-10 07:23:49 +000052 std::string GetPartition() { return pname_; }
53 std::string GetImageName() { return fname_; }
54 std::string GetPartitionAndSlot();
55 std::string GetSlot() { return slot_; }
Daniel Zheng0d307182023-01-31 21:07:53 +000056 void Run() override;
Daniel Zheng0d307182023-01-31 21:07:53 +000057
58 private:
59 const std::string pname_;
60 const std::string fname_;
61 const std::string slot_;
Daniel Zheng403657d2023-03-10 07:37:25 +000062 const bool apply_vbmeta_;
Daniel Zheng0d307182023-01-31 21:07:53 +000063};
Daniel Zheng71b3b432023-01-30 17:43:00 +000064
65class RebootTask : public Task {
66 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -070067 RebootTask(const FlashingPlan* fp);
68 RebootTask(const FlashingPlan* fp, const std::string& reboot_target);
Daniel Zheng29a211c2023-03-10 07:23:49 +000069 virtual RebootTask* AsRebootTask() override { return this; }
Daniel Zheng71b3b432023-01-30 17:43:00 +000070 void Run() override;
Daniel Zheng71b3b432023-01-30 17:43:00 +000071
72 private:
73 const std::string reboot_target_ = "";
Daniel Zheng43987c92023-03-07 18:20:53 +000074 const FlashingPlan* fp_;
Daniel Zheng47d70a52023-02-14 17:44:40 +000075};
76
77class FlashSuperLayoutTask : public Task {
78 public:
79 FlashSuperLayoutTask(const std::string& super_name, std::unique_ptr<SuperFlashHelper> helper,
David Anderson74c78072023-03-16 21:21:28 -070080 SparsePtr sparse_layout, uint64_t super_size);
Daniel Zheng29a211c2023-03-10 07:23:49 +000081 static std::unique_ptr<FlashSuperLayoutTask> Initialize(const FlashingPlan* fp,
Daniel Zheng47d70a52023-02-14 17:44:40 +000082 std::vector<ImageEntry>& os_images);
Daniel Zheng29a211c2023-03-10 07:23:49 +000083 static std::unique_ptr<FlashSuperLayoutTask> InitializeFromTasks(
84 const FlashingPlan* fp, std::vector<std::unique_ptr<Task>>& tasks);
Daniel Zheng47d70a52023-02-14 17:44:40 +000085 using ImageEntry = std::pair<const Image*, std::string>;
86 void Run() override;
87
88 private:
89 const std::string super_name_;
90 std::unique_ptr<SuperFlashHelper> helper_;
91 SparsePtr sparse_layout_;
David Anderson74c78072023-03-16 21:21:28 -070092 uint64_t super_size_;
Daniel Zheng47d70a52023-02-14 17:44:40 +000093};
Daniel Zheng6bb8baa2023-03-03 07:14:23 +000094
95class UpdateSuperTask : public Task {
96 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -070097 UpdateSuperTask(const FlashingPlan* fp);
Daniel Zheng29a211c2023-03-10 07:23:49 +000098 virtual UpdateSuperTask* AsUpdateSuperTask() override { return this; }
99
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000100 void Run() override;
101
102 private:
Daniel Zheng43987c92023-03-07 18:20:53 +0000103 const FlashingPlan* fp_;
Daniel Zheng6bb8baa2023-03-03 07:14:23 +0000104};
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000105
106class ResizeTask : public Task {
107 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700108 ResizeTask(const FlashingPlan* fp, const std::string& pname, const std::string& size,
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000109 const std::string& slot);
110 void Run() override;
111
112 private:
Daniel Zheng43987c92023-03-07 18:20:53 +0000113 const FlashingPlan* fp_;
Daniel Zheng9f7bf7e2023-03-03 07:16:46 +0000114 const std::string pname_;
115 const std::string size_;
116 const std::string slot_;
117};
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000118
119class DeleteTask : public Task {
120 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700121 DeleteTask(const FlashingPlan* _fp, const std::string& _pname);
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000122 void Run() override;
123
124 private:
Daniel Zheng43987c92023-03-07 18:20:53 +0000125 const FlashingPlan* fp_;
Daniel Zhengaa70f4c2023-03-07 18:59:51 +0000126 const std::string pname_;
127};
Daniel Zheng15e77832023-03-13 17:09:43 +0000128
129class WipeTask : public Task {
130 public:
Daniel Zhenge6dbd4f2023-04-10 09:53:08 -0700131 WipeTask(const FlashingPlan* fp, const std::string& pname);
Daniel Zheng29a211c2023-03-10 07:23:49 +0000132 virtual WipeTask* AsWipeTask() override { return this; }
133
Daniel Zheng15e77832023-03-13 17:09:43 +0000134 void Run() override;
135
136 private:
137 const FlashingPlan* fp_;
138 const std::string pname_;
139};