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 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 33 | #include "partition_installer.h" |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 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 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 60 | binder::Status openInstall(const std::string& install_dir, int* _aidl_return) override; |
| 61 | binder::Status closeInstall(int32_t* _aidl_return) override; |
| 62 | binder::Status createPartition(const ::std::string& name, int64_t size, bool readOnly, |
| 63 | int32_t* _aidl_return) override; |
David Anderson | 1d94d26 | 2019-01-11 20:39:51 -0800 | [diff] [blame] | 64 | binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream, |
| 65 | int64_t bytes, bool* _aidl_return) override; |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 66 | binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override; |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 67 | binder::Status setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, int64_t size, |
| 68 | bool* _aidl_return) override; |
| 69 | binder::Status commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 70 | binder::Status cancelGsiInstall(bool* _aidl_return) override; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 71 | binder::Status enableGsi(bool oneShot, const std::string& dsuSlot, int* _aidl_return) override; |
Howard Chen | 670b306 | 2019-02-26 18:14:47 +0800 | [diff] [blame] | 72 | binder::Status isGsiEnabled(bool* _aidl_return) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 73 | binder::Status removeGsi(bool* _aidl_return) override; |
| 74 | binder::Status disableGsi(bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 75 | binder::Status isGsiInstalled(bool* _aidl_return) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 76 | binder::Status isGsiRunning(bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 77 | binder::Status isGsiInstallInProgress(bool* _aidl_return) override; |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 78 | binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 79 | binder::Status getActiveDsuSlot(std::string* _aidl_return) override; |
| 80 | binder::Status getInstalledDsuSlots(std::vector<std::string>* _aidl_return) override; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 81 | binder::Status zeroPartition(const std::string& name, int* _aidl_return) override; |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 82 | binder::Status openImageService(const std::string& prefix, |
| 83 | android::sp<IImageService>* _aidl_return) override; |
David Anderson | d614eca | 2019-09-09 17:57:06 -0700 | [diff] [blame] | 84 | binder::Status dumpDeviceMapperDevices(std::string* _aidl_return) override; |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 85 | binder::Status getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) override; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 86 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 87 | // This is in GsiService, rather than GsiInstaller, since we need to access |
| 88 | // it outside of the main lock which protects the unique_ptr. |
| 89 | void StartAsyncOperation(const std::string& step, int64_t total_bytes); |
| 90 | void UpdateProgress(int status, int64_t bytes_processed); |
| 91 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 92 | // Helper methods for GsiInstaller. |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 93 | static bool RemoveGsiFiles(const std::string& install_dir); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 94 | bool should_abort() const { return should_abort_; } |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 95 | Gsid* parent() const { return parent_.get(); } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 96 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 97 | static void RunStartupTasks(); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 98 | static std::string GetInstalledImageDir(); |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 99 | std::string GetActiveDsuSlot(); |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 100 | std::string GetActiveInstalledImageDir(); |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 101 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 102 | static std::vector<std::string> GetInstalledDsuSlots(); |
| 103 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 104 | private: |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 105 | GsiService(Gsid* parent); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 106 | static int ValidateInstallParams(std::string& install_dir); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 107 | bool DisableGsiInstall(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 108 | int ReenableGsi(bool one_shot); |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 109 | static void CleanCorruptedInstallation(); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 110 | static int SaveInstallation(const std::string&); |
| 111 | static bool IsInstallationComplete(const std::string&); |
| 112 | static std::string GetCompleteIndication(const std::string&); |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 113 | |
David Anderson | 5cc440c | 2019-04-16 14:34:27 -0700 | [diff] [blame] | 114 | enum class AccessLevel { System, SystemOrShell }; |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 115 | binder::Status CheckUid(AccessLevel level = AccessLevel::System); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 116 | bool CreateInstallStatusFile(); |
| 117 | bool SetBootMode(bool one_shot); |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 118 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 119 | static android::wp<GsiService> sInstance; |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 120 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 121 | std::string install_dir_ = {}; |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 122 | android::sp<Gsid> parent_; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 123 | std::unique_ptr<PartitionInstaller> installer_; |
David Anderson | c86531e | 2019-02-25 18:50:35 -0800 | [diff] [blame] | 124 | |
| 125 | // These are initialized or set in StartInstall(). |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 126 | std::atomic<bool> should_abort_ = false; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 127 | |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 128 | // Progress bar state. |
| 129 | std::mutex progress_lock_; |
| 130 | GsiProgress progress_; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | } // namespace gsi |
| 134 | } // namespace android |