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 | 67a2709 | 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; |
Yo Chiang | 36fdbc6 | 2020-08-20 19:40:31 +0800 | [diff] [blame] | 45 | binder::Status closePartition(int32_t* _aidl_return) override; |
David Anderson | 1d94d26 | 2019-01-11 20:39:51 -0800 | [diff] [blame] | 46 | binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream, |
| 47 | int64_t bytes, bool* _aidl_return) override; |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 48 | binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override; |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 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; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 52 | binder::Status cancelGsiInstall(bool* _aidl_return) override; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 53 | binder::Status enableGsi(bool oneShot, const std::string& dsuSlot, int* _aidl_return) override; |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 54 | binder::Status enableGsiAsync(bool oneShot, const ::std::string& dsuSlot, |
| 55 | const sp<IGsiServiceCallback>& resultCallback) override; |
Howard Chen | 670b306 | 2019-02-26 18:14:47 +0800 | [diff] [blame] | 56 | binder::Status isGsiEnabled(bool* _aidl_return) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 57 | binder::Status removeGsi(bool* _aidl_return) override; |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 58 | binder::Status removeGsiAsync(const sp<IGsiServiceCallback>& resultCallback) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 59 | binder::Status disableGsi(bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 60 | binder::Status isGsiInstalled(bool* _aidl_return) override; |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 61 | binder::Status isGsiRunning(bool* _aidl_return) override; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 62 | binder::Status isGsiInstallInProgress(bool* _aidl_return) override; |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 63 | binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 64 | binder::Status getActiveDsuSlot(std::string* _aidl_return) override; |
| 65 | binder::Status getInstalledDsuSlots(std::vector<std::string>* _aidl_return) override; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 66 | binder::Status zeroPartition(const std::string& name, int* _aidl_return) override; |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 67 | binder::Status openImageService(const std::string& prefix, |
| 68 | android::sp<IImageService>* _aidl_return) override; |
David Anderson | d614eca | 2019-09-09 17:57:06 -0700 | [diff] [blame] | 69 | binder::Status dumpDeviceMapperDevices(std::string* _aidl_return) override; |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 70 | binder::Status getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) override; |
Yo Chiang | 721a0e4 | 2020-12-28 19:29:47 +0800 | [diff] [blame] | 71 | binder::Status suggestScratchSize(int64_t* _aidl_return) override; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 72 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 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 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 78 | // Helper methods for GsiInstaller. |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 79 | static bool RemoveGsiFiles(const std::string& install_dir); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 80 | bool should_abort() const { return should_abort_; } |
| 81 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 82 | static void RunStartupTasks(); |
David Anderson | 73274df | 2022-01-10 02:37:33 +0000 | [diff] [blame] | 83 | static void VerifyImageMaps(); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 84 | static std::string GetInstalledImageDir(); |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 85 | std::string GetActiveDsuSlot(); |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 86 | std::string GetActiveInstalledImageDir(); |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 87 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 88 | static std::vector<std::string> GetInstalledDsuSlots(); |
| 89 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 90 | private: |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 91 | friend class ImageService; |
| 92 | |
| 93 | GsiService(); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 94 | static int ValidateInstallParams(std::string& install_dir); |
Yi-Yo Chiang | d646d79 | 2022-04-04 21:38:57 +0800 | [diff] [blame] | 95 | int EnableGsi(bool one_shot, const std::string& dsu_slot); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 96 | bool DisableGsiInstall(); |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 97 | static void CleanCorruptedInstallation(); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 98 | static int SaveInstallation(const std::string&); |
| 99 | static bool IsInstallationComplete(const std::string&); |
| 100 | static std::string GetCompleteIndication(const std::string&); |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 101 | |
David Anderson | 5cc440c | 2019-04-16 14:34:27 -0700 | [diff] [blame] | 102 | enum class AccessLevel { System, SystemOrShell }; |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 103 | binder::Status CheckUid(AccessLevel level = AccessLevel::System); |
Yi-Yo Chiang | 1ecc3af | 2021-12-02 18:30:24 +0800 | [diff] [blame] | 104 | |
| 105 | // Mark install completion, and reset boot attempt counter. |
| 106 | // Next boot will try to boot into DSU. |
| 107 | bool ResetBootAttemptCounter(); |
| 108 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 109 | bool SetBootMode(bool one_shot); |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 110 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 111 | static android::wp<GsiService> sInstance; |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 112 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 113 | std::string install_dir_ = {}; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 114 | std::unique_ptr<PartitionInstaller> installer_; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 115 | std::mutex lock_; |
| 116 | std::mutex& lock() { return lock_; } |
David Anderson | c86531e | 2019-02-25 18:50:35 -0800 | [diff] [blame] | 117 | // These are initialized or set in StartInstall(). |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 118 | std::atomic<bool> should_abort_ = false; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 119 | |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 120 | // Progress bar state. |
| 121 | std::mutex progress_lock_; |
| 122 | GsiProgress progress_; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | } // namespace gsi |
| 126 | } // namespace android |