1 /*
2  * Copyright (C) 2019 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 <map>
19 #include <memory>
20 #include <mutex>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24 
25 #include <android-base/unique_fd.h>
26 #include <android/gsi/BnGsiService.h>
27 #include <binder/BinderService.h>
28 #include <libfiemap/split_fiemap_writer.h>
29 #include <liblp/builder.h>
30 #include "libgsi/libgsi.h"
31 
32 #include "partition_installer.h"
33 
34 namespace android {
35 namespace gsi {
36 
37 class GsiService : public BinderService<GsiService>, public BnGsiService {
38   public:
39     static void Register();
40 
41     binder::Status openInstall(const std::string& install_dir, int* _aidl_return) override;
42     binder::Status closeInstall(int32_t* _aidl_return) override;
43     binder::Status createPartition(const ::std::string& name, int64_t size, bool readOnly,
44                                    int32_t* _aidl_return) override;
45     binder::Status closePartition(int32_t* _aidl_return) override;
46     binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream,
47                                             int64_t bytes, bool* _aidl_return) override;
48     binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override;
49     binder::Status setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, int64_t size,
50                                 bool* _aidl_return) override;
51     binder::Status commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) override;
52     binder::Status cancelGsiInstall(bool* _aidl_return) override;
53     binder::Status enableGsi(bool oneShot, const std::string& dsuSlot, int* _aidl_return) override;
54     binder::Status enableGsiAsync(bool oneShot, const ::std::string& dsuSlot,
55                                   const sp<IGsiServiceCallback>& resultCallback) override;
56     binder::Status isGsiEnabled(bool* _aidl_return) override;
57     binder::Status removeGsi(bool* _aidl_return) override;
58     binder::Status removeGsiAsync(const sp<IGsiServiceCallback>& resultCallback) override;
59     binder::Status disableGsi(bool* _aidl_return) override;
60     binder::Status isGsiInstalled(bool* _aidl_return) override;
61     binder::Status isGsiRunning(bool* _aidl_return) override;
62     binder::Status isGsiInstallInProgress(bool* _aidl_return) override;
63     binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override;
64     binder::Status getActiveDsuSlot(std::string* _aidl_return) override;
65     binder::Status getInstalledDsuSlots(std::vector<std::string>* _aidl_return) override;
66     binder::Status zeroPartition(const std::string& name, int* _aidl_return) override;
67     binder::Status openImageService(const std::string& prefix,
68                                     android::sp<IImageService>* _aidl_return) override;
69     binder::Status dumpDeviceMapperDevices(std::string* _aidl_return) override;
70     binder::Status getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) override;
71     binder::Status suggestScratchSize(int64_t* _aidl_return) override;
72 
73     // This is in GsiService, rather than GsiInstaller, since we need to access
74     // it outside of the main lock which protects the unique_ptr.
75     void StartAsyncOperation(const std::string& step, int64_t total_bytes);
76     void UpdateProgress(int status, int64_t bytes_processed);
77 
78     // Helper methods for GsiInstaller.
79     static bool RemoveGsiFiles(const std::string& install_dir);
should_abort()80     bool should_abort() const { return should_abort_; }
81 
82     static void RunStartupTasks();
83     static std::string GetInstalledImageDir();
84     std::string GetActiveDsuSlot();
85     std::string GetActiveInstalledImageDir();
86 
87     static std::vector<std::string> GetInstalledDsuSlots();
88 
89   private:
90     friend class ImageService;
91 
92     GsiService();
93     static int ValidateInstallParams(std::string& install_dir);
94     bool DisableGsiInstall();
95     int ReenableGsi(bool one_shot);
96     static void CleanCorruptedInstallation();
97     static int SaveInstallation(const std::string&);
98     static bool IsInstallationComplete(const std::string&);
99     static std::string GetCompleteIndication(const std::string&);
100 
101     enum class AccessLevel { System, SystemOrShell };
102     binder::Status CheckUid(AccessLevel level = AccessLevel::System);
103     bool CreateInstallStatusFile();
104     bool SetBootMode(bool one_shot);
105 
106     static android::wp<GsiService> sInstance;
107 
108     std::string install_dir_ = {};
109     std::unique_ptr<PartitionInstaller> installer_;
110     std::mutex lock_;
lock()111     std::mutex& lock() { return lock_; }
112     // These are initialized or set in StartInstall().
113     std::atomic<bool> should_abort_ = false;
114 
115     // Progress bar state.
116     std::mutex progress_lock_;
117     GsiProgress progress_;
118 };
119 
120 }  // namespace gsi
121 }  // namespace android
122