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> |
| 27 | #include <binder/BinderService.h> |
David Anderson | 9ca7728 | 2019-07-15 23:56:13 +0000 | [diff] [blame] | 28 | #include <libfiemap/split_fiemap_writer.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 29 | #include <liblp/builder.h> |
| 30 | #include "libgsi/libgsi.h" |
| 31 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 32 | #include "partition_installer.h" |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 33 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace gsi { |
| 36 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 37 | class GsiService : public BinderService<GsiService>, public BnGsiService { |
| 38 | public: |
Howard Chen | f2a0a82 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 39 | static void Register(); |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 40 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 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; |
David Anderson | 1d94d26 | 2019-01-11 20:39:51 -0800 | [diff] [blame] | 45 | binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream, |
| 46 | int64_t bytes, bool* _aidl_return) override; |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 47 | binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override; |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 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; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 51 | binder::Status cancelGsiInstall(bool* _aidl_return) override; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 52 | binder::Status enableGsi(bool oneShot, const std::string& dsuSlot, int* _aidl_return) override; |
Howard Chen | 0e18806 | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 53 | binder::Status enableGsiAsync(bool oneShot, const ::std::string& dsuSlot, |
| 54 | const sp<IGsiServiceCallback>& resultCallback) override; |
Howard Chen | 670b306 | 2019-02-26 18:14:47 +0800 | [diff] [blame] | 55 | binder::Status isGsiEnabled(bool* _aidl_return) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 56 | binder::Status removeGsi(bool* _aidl_return) override; |
Howard Chen | 0e18806 | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 57 | binder::Status removeGsiAsync(const sp<IGsiServiceCallback>& resultCallback) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 58 | binder::Status disableGsi(bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 59 | binder::Status isGsiInstalled(bool* _aidl_return) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 60 | binder::Status isGsiRunning(bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 61 | binder::Status isGsiInstallInProgress(bool* _aidl_return) override; |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 62 | binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 63 | binder::Status getActiveDsuSlot(std::string* _aidl_return) override; |
| 64 | binder::Status getInstalledDsuSlots(std::vector<std::string>* _aidl_return) override; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 65 | binder::Status zeroPartition(const std::string& name, int* _aidl_return) override; |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 66 | binder::Status openImageService(const std::string& prefix, |
| 67 | android::sp<IImageService>* _aidl_return) override; |
David Anderson | d614eca | 2019-09-09 17:57:06 -0700 | [diff] [blame] | 68 | binder::Status dumpDeviceMapperDevices(std::string* _aidl_return) override; |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 69 | binder::Status getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) override; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 70 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 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 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 76 | // Helper methods for GsiInstaller. |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 77 | static bool RemoveGsiFiles(const std::string& install_dir); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 78 | bool should_abort() const { return should_abort_; } |
| 79 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 80 | static void RunStartupTasks(); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 81 | static std::string GetInstalledImageDir(); |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 82 | std::string GetActiveDsuSlot(); |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 83 | std::string GetActiveInstalledImageDir(); |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 84 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 85 | static std::vector<std::string> GetInstalledDsuSlots(); |
| 86 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 87 | private: |
Howard Chen | f2a0a82 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 88 | friend class ImageService; |
| 89 | |
| 90 | GsiService(); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 91 | static int ValidateInstallParams(std::string& install_dir); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 92 | bool DisableGsiInstall(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 93 | int ReenableGsi(bool one_shot); |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 94 | static void CleanCorruptedInstallation(); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 95 | static int SaveInstallation(const std::string&); |
| 96 | static bool IsInstallationComplete(const std::string&); |
| 97 | static std::string GetCompleteIndication(const std::string&); |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 98 | |
David Anderson | 5cc440c | 2019-04-16 14:34:27 -0700 | [diff] [blame] | 99 | enum class AccessLevel { System, SystemOrShell }; |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 100 | binder::Status CheckUid(AccessLevel level = AccessLevel::System); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 101 | bool CreateInstallStatusFile(); |
| 102 | bool SetBootMode(bool one_shot); |
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 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 106 | std::string install_dir_ = {}; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 107 | std::unique_ptr<PartitionInstaller> installer_; |
Howard Chen | f2a0a82 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 108 | std::mutex lock_; |
| 109 | std::mutex& lock() { return lock_; } |
David Anderson | c86531e | 2019-02-25 18:50:35 -0800 | [diff] [blame] | 110 | // These are initialized or set in StartInstall(). |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 111 | std::atomic<bool> should_abort_ = false; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 112 | |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 113 | // Progress bar state. |
| 114 | std::mutex progress_lock_; |
| 115 | GsiProgress progress_; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // namespace gsi |
| 119 | } // namespace android |