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 commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream,
46                                             int64_t bytes, bool* _aidl_return) override;
47     binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override;
48     binder::Status setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, int64_t size,
49                                 bool* _aidl_return) override;
50     binder::Status commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) override;
51     binder::Status cancelGsiInstall(bool* _aidl_return) override;
52     binder::Status enableGsi(bool oneShot, const std::string& dsuSlot, int* _aidl_return) override;
53     binder::Status enableGsiAsync(bool oneShot, const ::std::string& dsuSlot,
54                                   const sp<IGsiServiceCallback>& resultCallback) override;
55     binder::Status isGsiEnabled(bool* _aidl_return) override;
56     binder::Status removeGsi(bool* _aidl_return) override;
57     binder::Status removeGsiAsync(const sp<IGsiServiceCallback>& resultCallback) override;
58     binder::Status disableGsi(bool* _aidl_return) override;
59     binder::Status isGsiInstalled(bool* _aidl_return) override;
60     binder::Status isGsiRunning(bool* _aidl_return) override;
61     binder::Status isGsiInstallInProgress(bool* _aidl_return) override;
62     binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override;
63     binder::Status getActiveDsuSlot(std::string* _aidl_return) override;
64     binder::Status getInstalledDsuSlots(std::vector<std::string>* _aidl_return) override;
65     binder::Status zeroPartition(const std::string& name, int* _aidl_return) override;
66     binder::Status openImageService(const std::string& prefix,
67                                     android::sp<IImageService>* _aidl_return) override;
68     binder::Status dumpDeviceMapperDevices(std::string* _aidl_return) override;
69     binder::Status getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) override;
70 
71     // This is in GsiService, rather than GsiInstaller, since we need to access
72     // it outside of the main lock which protects the unique_ptr.
73     void StartAsyncOperation(const std::string& step, int64_t total_bytes);
74     void UpdateProgress(int status, int64_t bytes_processed);
75 
76     // Helper methods for GsiInstaller.
77     static bool RemoveGsiFiles(const std::string& install_dir);
should_abort()78     bool should_abort() const { return should_abort_; }
79 
80     static void RunStartupTasks();
81     static std::string GetInstalledImageDir();
82     std::string GetActiveDsuSlot();
83     std::string GetActiveInstalledImageDir();
84 
85     static std::vector<std::string> GetInstalledDsuSlots();
86 
87   private:
88     friend class ImageService;
89 
90     GsiService();
91     static int ValidateInstallParams(std::string& install_dir);
92     bool DisableGsiInstall();
93     int ReenableGsi(bool one_shot);
94     static void CleanCorruptedInstallation();
95     static int SaveInstallation(const std::string&);
96     static bool IsInstallationComplete(const std::string&);
97     static std::string GetCompleteIndication(const std::string&);
98 
99     enum class AccessLevel { System, SystemOrShell };
100     binder::Status CheckUid(AccessLevel level = AccessLevel::System);
101     bool CreateInstallStatusFile();
102     bool SetBootMode(bool one_shot);
103 
104     static android::wp<GsiService> sInstance;
105 
106     std::string install_dir_ = {};
107     std::unique_ptr<PartitionInstaller> installer_;
108     std::mutex lock_;
lock()109     std::mutex& lock() { return lock_; }
110     // These are initialized or set in StartInstall().
111     std::atomic<bool> should_abort_ = false;
112 
113     // Progress bar state.
114     std::mutex progress_lock_;
115     GsiProgress progress_;
116 };
117 
118 }  // namespace gsi
119 }  // namespace android
120