David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 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 | |
David Anderson | a64e392 | 2019-02-01 16:27:18 -0800 | [diff] [blame] | 18 | #include <map> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 19 | #include <memory> |
| 20 | #include <mutex> |
| 21 | #include <sstream> |
David Anderson | a64e392 | 2019-02-01 16:27:18 -0800 | [diff] [blame] | 22 | #include <string> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | #include <android-base/unique_fd.h> |
| 26 | #include <android/gsi/BnGsiService.h> |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 27 | #include <android/gsi/BnGsid.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 28 | #include <binder/BinderService.h> |
David Anderson | 9ca7728 | 2019-07-15 23:56:13 +0000 | [diff] [blame] | 29 | #include <libfiemap/split_fiemap_writer.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 30 | #include <liblp/builder.h> |
| 31 | #include "libgsi/libgsi.h" |
| 32 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 33 | #include "gsi_installer.h" |
| 34 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace gsi { |
| 37 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 38 | class Gsid : public BinderService<Gsid>, public BnGsid { |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 39 | public: |
| 40 | static void Register(); |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 41 | static char const* getServiceName() { return kGsiServiceName; } |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 42 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 43 | binder::Status getClient(android::sp<IGsiService>* _aidl_return) override; |
| 44 | |
| 45 | private: |
| 46 | friend class GsiService; |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 47 | friend class ImageService; |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 48 | |
| 49 | std::mutex& lock() { return lock_; } |
| 50 | |
| 51 | std::mutex lock_; |
| 52 | }; |
| 53 | |
| 54 | class GsiService : public BinderService<GsiService>, public BnGsiService { |
| 55 | public: |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 56 | ~GsiService() override; |
| 57 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 58 | static android::sp<IGsiService> Get(Gsid* parent); |
| 59 | |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 60 | binder::Status beginGsiInstall(const GsiInstallParams& params, int* _aidl_return) override; |
David Anderson | 1d94d26 | 2019-01-11 20:39:51 -0800 | [diff] [blame] | 61 | binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream, |
| 62 | int64_t bytes, bool* _aidl_return) override; |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 63 | binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override; |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 64 | binder::Status setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, int64_t size, |
| 65 | bool* _aidl_return) override; |
| 66 | binder::Status commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 67 | binder::Status cancelGsiInstall(bool* _aidl_return) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 68 | binder::Status enableGsi(bool oneShot, int* _aidl_return) override; |
Howard Chen | 670b306 | 2019-02-26 18:14:47 +0800 | [diff] [blame] | 69 | binder::Status isGsiEnabled(bool* _aidl_return) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 70 | binder::Status removeGsi(bool* _aidl_return) override; |
| 71 | binder::Status disableGsi(bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 72 | binder::Status isGsiInstalled(bool* _aidl_return) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 73 | binder::Status isGsiRunning(bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 74 | binder::Status isGsiInstallInProgress(bool* _aidl_return) override; |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 75 | binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override; |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 76 | binder::Status wipeGsiUserdata(int* _aidl_return) override; |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 77 | binder::Status openImageService(const std::string& prefix, |
| 78 | android::sp<IImageService>* _aidl_return) override; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 79 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 80 | // This is in GsiService, rather than GsiInstaller, since we need to access |
| 81 | // it outside of the main lock which protects the unique_ptr. |
| 82 | void StartAsyncOperation(const std::string& step, int64_t total_bytes); |
| 83 | void UpdateProgress(int status, int64_t bytes_processed); |
| 84 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 85 | // Helper methods for GsiInstaller. |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 86 | static bool RemoveGsiFiles(const std::string& install_dir, bool wipeUserdata); |
| 87 | bool should_abort() const { return should_abort_; } |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 88 | Gsid* parent() const { return parent_.get(); } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 89 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 90 | static void RunStartupTasks(); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 91 | static std::string GetInstalledImageDir(); |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame^] | 92 | std::string GetActiveInstalledImageDir(); |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 93 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 94 | private: |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 95 | GsiService(Gsid* parent); |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 96 | int ValidateInstallParams(GsiInstallParams* params); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 97 | bool DisableGsiInstall(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 98 | int ReenableGsi(bool one_shot); |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame^] | 99 | static void CleanCorruptedInstallation(); |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 100 | |
David Anderson | 5cc440c | 2019-04-16 14:34:27 -0700 | [diff] [blame] | 101 | enum class AccessLevel { System, SystemOrShell }; |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 102 | binder::Status CheckUid(AccessLevel level = AccessLevel::System); |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 103 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 104 | static android::wp<GsiService> sInstance; |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 105 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 106 | android::sp<Gsid> parent_; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 107 | std::unique_ptr<GsiInstaller> installer_; |
David Anderson | c86531e | 2019-02-25 18:50:35 -0800 | [diff] [blame] | 108 | |
| 109 | // These are initialized or set in StartInstall(). |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 110 | std::atomic<bool> should_abort_ = false; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 111 | |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 112 | // Progress bar state. |
| 113 | std::mutex progress_lock_; |
| 114 | GsiProgress progress_; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace gsi |
| 118 | } // namespace android |