• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const std::string& name);
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 void VerifyImageMaps();
84     static std::string GetInstalledImageDir();
85     std::string GetActiveDsuSlot();
86     std::string GetActiveInstalledImageDir();
87 
88     static std::vector<std::string> GetInstalledDsuSlots();
89 
90   private:
91     friend class ImageService;
92 
93     GsiService();
94     static int ValidateInstallParams(std::string& install_dir);
95     int EnableGsi(bool one_shot, const std::string& dsu_slot);
96     bool DisableGsiInstall();
97     static void CleanCorruptedInstallation();
98     static int SaveInstallation(const std::string&);
99     static bool IsInstallationComplete(const std::string&);
100     static std::string GetCompleteIndication(const std::string&);
101 
102     enum class AccessLevel { System, SystemOrShell };
103     binder::Status CheckUid(AccessLevel level = AccessLevel::System);
104 
105     // Mark install completion, and reset boot attempt counter.
106     // Next boot will try to boot into DSU.
107     bool ResetBootAttemptCounter();
108 
109     bool SetBootMode(bool one_shot);
110 
111     static android::wp<GsiService> sInstance;
112 
113     std::string install_dir_ = {};
114     std::unique_ptr<PartitionInstaller> installer_;
115     std::mutex lock_;
lock()116     std::mutex& lock() { return lock_; }
117     // These are initialized or set in StartInstall().
118     std::atomic<bool> should_abort_ = false;
119 
120     // Progress bar state.
121     std::mutex progress_lock_;
122     GsiProgress progress_;
123 };
124 
125 }  // namespace gsi
126 }  // namespace android
127