xref: /aosp_15_r20/system/gsid/gsi_service.h (revision 4e2b41f188908a2ae9d9a2089f1f10779d080021)
1*4e2b41f1SAndroid Build Coastguard Worker /*
2*4e2b41f1SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*4e2b41f1SAndroid Build Coastguard Worker  *
4*4e2b41f1SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*4e2b41f1SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*4e2b41f1SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*4e2b41f1SAndroid Build Coastguard Worker  *
8*4e2b41f1SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*4e2b41f1SAndroid Build Coastguard Worker  *
10*4e2b41f1SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*4e2b41f1SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*4e2b41f1SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4e2b41f1SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*4e2b41f1SAndroid Build Coastguard Worker  * limitations under the License.
15*4e2b41f1SAndroid Build Coastguard Worker  */
16*4e2b41f1SAndroid Build Coastguard Worker #pragma once
17*4e2b41f1SAndroid Build Coastguard Worker 
18*4e2b41f1SAndroid Build Coastguard Worker #include <map>
19*4e2b41f1SAndroid Build Coastguard Worker #include <memory>
20*4e2b41f1SAndroid Build Coastguard Worker #include <mutex>
21*4e2b41f1SAndroid Build Coastguard Worker #include <sstream>
22*4e2b41f1SAndroid Build Coastguard Worker #include <string>
23*4e2b41f1SAndroid Build Coastguard Worker #include <vector>
24*4e2b41f1SAndroid Build Coastguard Worker 
25*4e2b41f1SAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
26*4e2b41f1SAndroid Build Coastguard Worker #include <android/gsi/BnGsiService.h>
27*4e2b41f1SAndroid Build Coastguard Worker #include <binder/BinderService.h>
28*4e2b41f1SAndroid Build Coastguard Worker #include <libfiemap/split_fiemap_writer.h>
29*4e2b41f1SAndroid Build Coastguard Worker #include <liblp/builder.h>
30*4e2b41f1SAndroid Build Coastguard Worker #include "libgsi/libgsi.h"
31*4e2b41f1SAndroid Build Coastguard Worker 
32*4e2b41f1SAndroid Build Coastguard Worker #include "partition_installer.h"
33*4e2b41f1SAndroid Build Coastguard Worker 
34*4e2b41f1SAndroid Build Coastguard Worker namespace android {
35*4e2b41f1SAndroid Build Coastguard Worker namespace gsi {
36*4e2b41f1SAndroid Build Coastguard Worker 
37*4e2b41f1SAndroid Build Coastguard Worker class GsiService : public BinderService<GsiService>, public BnGsiService {
38*4e2b41f1SAndroid Build Coastguard Worker   public:
39*4e2b41f1SAndroid Build Coastguard Worker     static void Register(const std::string& name);
40*4e2b41f1SAndroid Build Coastguard Worker 
41*4e2b41f1SAndroid Build Coastguard Worker     binder::Status openInstall(const std::string& install_dir, int* _aidl_return) override;
42*4e2b41f1SAndroid Build Coastguard Worker     binder::Status closeInstall(int32_t* _aidl_return) override;
43*4e2b41f1SAndroid Build Coastguard Worker     binder::Status createPartition(const ::std::string& name, int64_t size, bool readOnly,
44*4e2b41f1SAndroid Build Coastguard Worker                                    int32_t* _aidl_return) override;
45*4e2b41f1SAndroid Build Coastguard Worker     binder::Status closePartition(int32_t* _aidl_return) override;
46*4e2b41f1SAndroid Build Coastguard Worker     binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream,
47*4e2b41f1SAndroid Build Coastguard Worker                                             int64_t bytes, bool* _aidl_return) override;
48*4e2b41f1SAndroid Build Coastguard Worker     binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override;
49*4e2b41f1SAndroid Build Coastguard Worker     binder::Status setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, int64_t size,
50*4e2b41f1SAndroid Build Coastguard Worker                                 bool* _aidl_return) override;
51*4e2b41f1SAndroid Build Coastguard Worker     binder::Status commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) override;
52*4e2b41f1SAndroid Build Coastguard Worker     binder::Status cancelGsiInstall(bool* _aidl_return) override;
53*4e2b41f1SAndroid Build Coastguard Worker     binder::Status enableGsi(bool oneShot, const std::string& dsuSlot, int* _aidl_return) override;
54*4e2b41f1SAndroid Build Coastguard Worker     binder::Status enableGsiAsync(bool oneShot, const ::std::string& dsuSlot,
55*4e2b41f1SAndroid Build Coastguard Worker                                   const sp<IGsiServiceCallback>& resultCallback) override;
56*4e2b41f1SAndroid Build Coastguard Worker     binder::Status isGsiEnabled(bool* _aidl_return) override;
57*4e2b41f1SAndroid Build Coastguard Worker     binder::Status removeGsi(bool* _aidl_return) override;
58*4e2b41f1SAndroid Build Coastguard Worker     binder::Status removeGsiAsync(const sp<IGsiServiceCallback>& resultCallback) override;
59*4e2b41f1SAndroid Build Coastguard Worker     binder::Status disableGsi(bool* _aidl_return) override;
60*4e2b41f1SAndroid Build Coastguard Worker     binder::Status isGsiInstalled(bool* _aidl_return) override;
61*4e2b41f1SAndroid Build Coastguard Worker     binder::Status isGsiRunning(bool* _aidl_return) override;
62*4e2b41f1SAndroid Build Coastguard Worker     binder::Status isGsiInstallInProgress(bool* _aidl_return) override;
63*4e2b41f1SAndroid Build Coastguard Worker     binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override;
64*4e2b41f1SAndroid Build Coastguard Worker     binder::Status getActiveDsuSlot(std::string* _aidl_return) override;
65*4e2b41f1SAndroid Build Coastguard Worker     binder::Status getInstalledDsuSlots(std::vector<std::string>* _aidl_return) override;
66*4e2b41f1SAndroid Build Coastguard Worker     binder::Status zeroPartition(const std::string& name, int* _aidl_return) override;
67*4e2b41f1SAndroid Build Coastguard Worker     binder::Status openImageService(const std::string& prefix,
68*4e2b41f1SAndroid Build Coastguard Worker                                     android::sp<IImageService>* _aidl_return) override;
69*4e2b41f1SAndroid Build Coastguard Worker     binder::Status dumpDeviceMapperDevices(std::string* _aidl_return) override;
70*4e2b41f1SAndroid Build Coastguard Worker     binder::Status getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) override;
71*4e2b41f1SAndroid Build Coastguard Worker     binder::Status suggestScratchSize(int64_t* _aidl_return) override;
72*4e2b41f1SAndroid Build Coastguard Worker 
73*4e2b41f1SAndroid Build Coastguard Worker     // This is in GsiService, rather than GsiInstaller, since we need to access
74*4e2b41f1SAndroid Build Coastguard Worker     // it outside of the main lock which protects the unique_ptr.
75*4e2b41f1SAndroid Build Coastguard Worker     void StartAsyncOperation(const std::string& step, int64_t total_bytes);
76*4e2b41f1SAndroid Build Coastguard Worker     void UpdateProgress(int status, int64_t bytes_processed);
77*4e2b41f1SAndroid Build Coastguard Worker 
78*4e2b41f1SAndroid Build Coastguard Worker     // Helper methods for GsiInstaller.
79*4e2b41f1SAndroid Build Coastguard Worker     static bool RemoveGsiFiles(const std::string& install_dir);
should_abort()80*4e2b41f1SAndroid Build Coastguard Worker     bool should_abort() const { return should_abort_; }
81*4e2b41f1SAndroid Build Coastguard Worker 
82*4e2b41f1SAndroid Build Coastguard Worker     static void RunStartupTasks();
83*4e2b41f1SAndroid Build Coastguard Worker     static void VerifyImageMaps();
84*4e2b41f1SAndroid Build Coastguard Worker     static std::string GetInstalledImageDir();
85*4e2b41f1SAndroid Build Coastguard Worker     std::string GetActiveDsuSlot();
86*4e2b41f1SAndroid Build Coastguard Worker     std::string GetActiveInstalledImageDir();
87*4e2b41f1SAndroid Build Coastguard Worker 
88*4e2b41f1SAndroid Build Coastguard Worker     static std::vector<std::string> GetInstalledDsuSlots();
89*4e2b41f1SAndroid Build Coastguard Worker     GsiService();
90*4e2b41f1SAndroid Build Coastguard Worker 
91*4e2b41f1SAndroid Build Coastguard Worker   private:
92*4e2b41f1SAndroid Build Coastguard Worker     friend class ImageService;
93*4e2b41f1SAndroid Build Coastguard Worker 
94*4e2b41f1SAndroid Build Coastguard Worker     static int ValidateInstallParams(std::string& install_dir);
95*4e2b41f1SAndroid Build Coastguard Worker     int EnableGsi(bool one_shot, const std::string& dsu_slot);
96*4e2b41f1SAndroid Build Coastguard Worker     bool DisableGsiInstall();
97*4e2b41f1SAndroid Build Coastguard Worker     static void CleanCorruptedInstallation();
98*4e2b41f1SAndroid Build Coastguard Worker     static int SaveInstallation(const std::string&);
99*4e2b41f1SAndroid Build Coastguard Worker     static bool IsInstallationComplete(const std::string&);
100*4e2b41f1SAndroid Build Coastguard Worker     static std::string GetCompleteIndication(const std::string&);
101*4e2b41f1SAndroid Build Coastguard Worker 
102*4e2b41f1SAndroid Build Coastguard Worker     enum class AccessLevel { System, SystemOrShell };
103*4e2b41f1SAndroid Build Coastguard Worker     binder::Status CheckUid(AccessLevel level = AccessLevel::System);
104*4e2b41f1SAndroid Build Coastguard Worker 
105*4e2b41f1SAndroid Build Coastguard Worker     // Mark install completion, and reset boot attempt counter.
106*4e2b41f1SAndroid Build Coastguard Worker     // Next boot will try to boot into DSU.
107*4e2b41f1SAndroid Build Coastguard Worker     bool ResetBootAttemptCounter();
108*4e2b41f1SAndroid Build Coastguard Worker 
109*4e2b41f1SAndroid Build Coastguard Worker     bool SetBootMode(bool one_shot);
110*4e2b41f1SAndroid Build Coastguard Worker 
111*4e2b41f1SAndroid Build Coastguard Worker     static android::wp<GsiService> sInstance;
112*4e2b41f1SAndroid Build Coastguard Worker 
113*4e2b41f1SAndroid Build Coastguard Worker     std::string install_dir_ = {};
114*4e2b41f1SAndroid Build Coastguard Worker     std::unique_ptr<PartitionInstaller> installer_;
115*4e2b41f1SAndroid Build Coastguard Worker     std::mutex lock_;
lock()116*4e2b41f1SAndroid Build Coastguard Worker     std::mutex& lock() { return lock_; }
117*4e2b41f1SAndroid Build Coastguard Worker     // These are initialized or set in StartInstall().
118*4e2b41f1SAndroid Build Coastguard Worker     std::atomic<bool> should_abort_ = false;
119*4e2b41f1SAndroid Build Coastguard Worker 
120*4e2b41f1SAndroid Build Coastguard Worker     // Progress bar state.
121*4e2b41f1SAndroid Build Coastguard Worker     std::mutex progress_lock_;
122*4e2b41f1SAndroid Build Coastguard Worker     GsiProgress progress_;
123*4e2b41f1SAndroid Build Coastguard Worker };
124*4e2b41f1SAndroid Build Coastguard Worker 
125*4e2b41f1SAndroid Build Coastguard Worker }  // namespace gsi
126*4e2b41f1SAndroid Build Coastguard Worker }  // namespace android
127