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 | |
| 17 | #include "gsi_service.h" |
| 18 | |
Yo Chiang | 721a0e4 | 2020-12-28 19:29:47 +0800 | [diff] [blame] | 19 | #include <sys/statvfs.h> |
David Anderson | e7a8ade | 2019-03-13 19:21:22 -0700 | [diff] [blame] | 20 | #include <sys/vfs.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 21 | #include <unistd.h> |
| 22 | |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 23 | #include <array> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 24 | #include <chrono> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Yifan Hong | d66695b | 2019-12-23 16:07:45 -0800 | [diff] [blame] | 28 | #include <android-base/errors.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 31 | #include <android-base/properties.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 33 | #include <android-base/strings.h> |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 34 | #include <android/gsi/BnImageService.h> |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 35 | #include <android/gsi/IGsiService.h> |
Yo Chiang | 1f08091 | 2020-10-07 17:29:57 +0800 | [diff] [blame] | 36 | #include <android/os/IVold.h> |
| 37 | #include <binder/IServiceManager.h> |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 38 | #include <binder/LazyServiceRegistrar.h> |
David Anderson | 73274df | 2022-01-10 02:37:33 +0000 | [diff] [blame] | 39 | #include <cutils/android_reboot.h> |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 40 | #include <ext4_utils/ext4_utils.h> |
David Anderson | e7a8ade | 2019-03-13 19:21:22 -0700 | [diff] [blame] | 41 | #include <fs_mgr.h> |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 42 | #include <libavb/libavb.h> |
David Anderson | d614eca | 2019-09-09 17:57:06 -0700 | [diff] [blame] | 43 | #include <libdm/dm.h> |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 44 | #include <libfiemap/image_manager.h> |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 45 | #include <openssl/sha.h> |
David Anderson | 3c819cb | 2019-02-08 17:50:50 -0800 | [diff] [blame] | 46 | #include <private/android_filesystem_config.h> |
Yi-Yo Chiang | f72d88a | 2021-03-22 13:45:39 +0800 | [diff] [blame] | 47 | #include <selinux/android.h> |
Yo Chiang | 721a0e4 | 2020-12-28 19:29:47 +0800 | [diff] [blame] | 48 | #include <storage_literals/storage_literals.h> |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 49 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 50 | #include "file_paths.h" |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 51 | #include "libgsi_private.h" |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 52 | |
| 53 | namespace android { |
| 54 | namespace gsi { |
| 55 | |
| 56 | using namespace std::literals; |
| 57 | using namespace android::fs_mgr; |
David Anderson | 9ca7728 | 2019-07-15 23:56:13 +0000 | [diff] [blame] | 58 | using namespace android::fiemap; |
Yo Chiang | 721a0e4 | 2020-12-28 19:29:47 +0800 | [diff] [blame] | 59 | using namespace android::storage_literals; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 60 | using android::base::ReadFileToString; |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 61 | using android::base::ReadFullyAtOffset; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 62 | using android::base::RemoveFileIfExists; |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 63 | using android::base::SetProperty; |
David Anderson | 3c819cb | 2019-02-08 17:50:50 -0800 | [diff] [blame] | 64 | using android::base::StringPrintf; |
David Anderson | c86531e | 2019-02-25 18:50:35 -0800 | [diff] [blame] | 65 | using android::base::unique_fd; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 66 | using android::base::WriteStringToFd; |
| 67 | using android::base::WriteStringToFile; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 68 | using android::binder::LazyServiceRegistrar; |
David Anderson | d614eca | 2019-09-09 17:57:06 -0700 | [diff] [blame] | 69 | using android::dm::DeviceMapper; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 70 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 71 | // Default userdata image size. |
| 72 | static constexpr int64_t kDefaultUserdataSize = int64_t(2) * 1024 * 1024 * 1024; |
| 73 | |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 74 | static bool GetAvbPublicKeyFromFd(int fd, AvbPublicKey* dst); |
| 75 | |
Yi-Yo Chiang | f72d88a | 2021-03-22 13:45:39 +0800 | [diff] [blame] | 76 | // Fix the file contexts of dsu metadata files. |
| 77 | // By default, newly created files inherit the file contexts of their parent |
| 78 | // directory. Since globally readable public metadata files are labeled with a |
| 79 | // different context, gsi_public_metadata_file, we need to call this function to |
| 80 | // fix their contexts after creating them. |
| 81 | static void RestoreconMetadataFiles() { |
| 82 | auto flags = SELINUX_ANDROID_RESTORECON_RECURSE | SELINUX_ANDROID_RESTORECON_SKIP_SEHASH; |
| 83 | selinux_android_restorecon(DSU_METADATA_PREFIX, flags); |
| 84 | } |
| 85 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 86 | GsiService::GsiService() { |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 87 | progress_ = {}; |
| 88 | } |
| 89 | |
Yi-Yo Chiang | 457b9c7 | 2022-08-09 20:09:19 +0800 | [diff] [blame] | 90 | void GsiService::Register(const std::string& name) { |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 91 | auto lazyRegistrar = LazyServiceRegistrar::getInstance(); |
| 92 | android::sp<GsiService> service = new GsiService(); |
Yi-Yo Chiang | 457b9c7 | 2022-08-09 20:09:19 +0800 | [diff] [blame] | 93 | auto ret = lazyRegistrar.registerService(service, name); |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 94 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 95 | if (ret != android::OK) { |
| 96 | LOG(FATAL) << "Could not register gsi service: " << ret; |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
David Anderson | 5cc440c | 2019-04-16 14:34:27 -0700 | [diff] [blame] | 100 | #define ENFORCE_SYSTEM \ |
| 101 | do { \ |
| 102 | binder::Status status = CheckUid(); \ |
| 103 | if (!status.isOk()) return status; \ |
David Anderson | 3c819cb | 2019-02-08 17:50:50 -0800 | [diff] [blame] | 104 | } while (0) |
| 105 | |
David Anderson | 5cc440c | 2019-04-16 14:34:27 -0700 | [diff] [blame] | 106 | #define ENFORCE_SYSTEM_OR_SHELL \ |
| 107 | do { \ |
| 108 | binder::Status status = CheckUid(AccessLevel::SystemOrShell); \ |
| 109 | if (!status.isOk()) return status; \ |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 110 | } while (0) |
| 111 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 112 | int GsiService::SaveInstallation(const std::string& installation) { |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 113 | auto dsu_slot = GetDsuSlot(installation); |
| 114 | auto install_dir_file = DsuInstallDirFile(dsu_slot); |
| 115 | auto metadata_dir = android::base::Dirname(install_dir_file); |
| 116 | if (access(metadata_dir.c_str(), F_OK) != 0) { |
| 117 | if (mkdir(metadata_dir.c_str(), 0777) != 0) { |
| 118 | PLOG(ERROR) << "Failed to mkdir " << metadata_dir; |
| 119 | return INSTALL_ERROR_GENERIC; |
| 120 | } |
| 121 | } |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 122 | auto fd = android::base::unique_fd( |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 123 | open(install_dir_file.c_str(), O_RDWR | O_SYNC | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 124 | if (!WriteStringToFd(installation, fd)) { |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 125 | PLOG(ERROR) << "write failed: " << DsuInstallDirFile(dsu_slot); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 126 | return INSTALL_ERROR_GENERIC; |
| 127 | } |
| 128 | return INSTALL_OK; |
| 129 | } |
| 130 | |
Howard Chen | 3a5e19a | 2021-01-07 16:07:52 +0800 | [diff] [blame] | 131 | static bool IsExternalStoragePath(const std::string& path); |
| 132 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 133 | binder::Status GsiService::openInstall(const std::string& install_dir, int* _aidl_return) { |
| 134 | ENFORCE_SYSTEM; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 135 | std::lock_guard<std::mutex> guard(lock_); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 136 | if (IsGsiRunning()) { |
| 137 | *_aidl_return = IGsiService::INSTALL_ERROR_GENERIC; |
| 138 | return binder::Status::ok(); |
| 139 | } |
| 140 | install_dir_ = install_dir; |
| 141 | if (int status = ValidateInstallParams(install_dir_)) { |
| 142 | *_aidl_return = status; |
| 143 | return binder::Status::ok(); |
| 144 | } |
| 145 | std::string message; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 146 | auto dsu_slot = GetDsuSlot(install_dir_); |
| 147 | if (!RemoveFileIfExists(GetCompleteIndication(dsu_slot), &message)) { |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 148 | LOG(ERROR) << message; |
| 149 | } |
| 150 | // Remember the installation directory before allocate any resource |
| 151 | *_aidl_return = SaveInstallation(install_dir_); |
| 152 | return binder::Status::ok(); |
| 153 | } |
| 154 | |
| 155 | binder::Status GsiService::closeInstall(int* _aidl_return) { |
| 156 | ENFORCE_SYSTEM; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 157 | std::lock_guard<std::mutex> guard(lock_); |
Yi-Yo Chiang | 1ecc3af | 2021-12-02 18:30:24 +0800 | [diff] [blame] | 158 | |
| 159 | installer_ = {}; |
| 160 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 161 | auto dsu_slot = GetDsuSlot(install_dir_); |
| 162 | std::string file = GetCompleteIndication(dsu_slot); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 163 | if (!WriteStringToFile("OK", file)) { |
| 164 | PLOG(ERROR) << "write failed: " << file; |
Yi-Yo Chiang | 1ecc3af | 2021-12-02 18:30:24 +0800 | [diff] [blame] | 165 | *_aidl_return = IGsiService::INSTALL_ERROR_GENERIC; |
| 166 | return binder::Status::ok(); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 167 | } |
Yi-Yo Chiang | 1ecc3af | 2021-12-02 18:30:24 +0800 | [diff] [blame] | 168 | |
| 169 | // Create installation complete marker files, but set disabled immediately. |
| 170 | if (!WriteStringToFile(dsu_slot, kDsuActiveFile)) { |
| 171 | PLOG(ERROR) << "cannot write active DSU slot (" << dsu_slot << "): " << kDsuActiveFile; |
| 172 | *_aidl_return = IGsiService::INSTALL_ERROR_GENERIC; |
| 173 | return binder::Status::ok(); |
| 174 | } |
| 175 | RestoreconMetadataFiles(); |
| 176 | |
| 177 | // DisableGsi() creates the DSU install status file and mark it as "disabled". |
| 178 | if (!DisableGsi()) { |
| 179 | PLOG(ERROR) << "cannot write DSU status file: " << kDsuInstallStatusFile; |
| 180 | *_aidl_return = IGsiService::INSTALL_ERROR_GENERIC; |
| 181 | return binder::Status::ok(); |
| 182 | } |
| 183 | |
| 184 | SetProperty(kGsiInstalledProp, "1"); |
| 185 | *_aidl_return = IGsiService::INSTALL_OK; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 186 | return binder::Status::ok(); |
| 187 | } |
| 188 | |
| 189 | binder::Status GsiService::createPartition(const ::std::string& name, int64_t size, bool readOnly, |
| 190 | int32_t* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 191 | ENFORCE_SYSTEM; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 192 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 193 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 194 | if (install_dir_.empty()) { |
| 195 | PLOG(ERROR) << "open is required for createPartition"; |
| 196 | *_aidl_return = INSTALL_ERROR_GENERIC; |
| 197 | return binder::Status::ok(); |
| 198 | } |
| 199 | |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 200 | // Do some precursor validation on the arguments before diving into the |
| 201 | // install process. |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 202 | if (size % LP_SECTOR_SIZE) { |
| 203 | LOG(ERROR) << " size " << size << " is not a multiple of " << LP_SECTOR_SIZE; |
| 204 | *_aidl_return = INSTALL_ERROR_GENERIC; |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 205 | return binder::Status::ok(); |
| 206 | } |
| 207 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 208 | if (size == 0 && name == "userdata") { |
| 209 | size = kDefaultUserdataSize; |
| 210 | } |
Yo Chiang | 1f08091 | 2020-10-07 17:29:57 +0800 | [diff] [blame] | 211 | |
| 212 | if (name == "userdata") { |
| 213 | auto dsu_slot = GetDsuSlot(install_dir_); |
| 214 | auto key_dir = DefaultDsuMetadataKeyDir(dsu_slot); |
| 215 | auto key_dir_file = DsuMetadataKeyDirFile(dsu_slot); |
| 216 | if (!android::base::WriteStringToFile(key_dir, key_dir_file)) { |
| 217 | PLOG(ERROR) << "write failed: " << key_dir_file; |
| 218 | *_aidl_return = INSTALL_ERROR_GENERIC; |
| 219 | return binder::Status::ok(); |
| 220 | } |
Yi-Yo Chiang | f72d88a | 2021-03-22 13:45:39 +0800 | [diff] [blame] | 221 | RestoreconMetadataFiles(); |
Yo Chiang | 1f08091 | 2020-10-07 17:29:57 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 224 | installer_ = std::make_unique<PartitionInstaller>(this, install_dir_, name, |
| 225 | GetDsuSlot(install_dir_), size, readOnly); |
Howard Chen | 9f40baf | 2019-12-16 13:31:12 +0800 | [diff] [blame] | 226 | progress_ = {}; |
Yi-Yo Chiang | c4d6400 | 2021-02-12 02:17:03 +0800 | [diff] [blame] | 227 | *_aidl_return = installer_->StartInstall(); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 228 | return binder::Status::ok(); |
| 229 | } |
| 230 | |
Yo Chiang | 36fdbc6 | 2020-08-20 19:40:31 +0800 | [diff] [blame] | 231 | binder::Status GsiService::closePartition(int32_t* _aidl_return) { |
| 232 | ENFORCE_SYSTEM; |
| 233 | std::lock_guard<std::mutex> guard(lock_); |
| 234 | |
| 235 | if (installer_ == nullptr) { |
| 236 | LOG(ERROR) << "createPartition() has to be called before closePartition()"; |
| 237 | *_aidl_return = IGsiService::INSTALL_ERROR_GENERIC; |
| 238 | return binder::Status::ok(); |
| 239 | } |
| 240 | // It is important to not reset |installer_| here because other methods such |
Yi-Yo Chiang | 1ecc3af | 2021-12-02 18:30:24 +0800 | [diff] [blame] | 241 | // as isGsiInstallInProgress() relies on the state of |installer_|. |
| 242 | // TODO: Maybe don't do this, use a dedicated |in_progress_| flag? |
Yo Chiang | 36fdbc6 | 2020-08-20 19:40:31 +0800 | [diff] [blame] | 243 | *_aidl_return = installer_->FinishInstall(); |
| 244 | return binder::Status::ok(); |
| 245 | } |
| 246 | |
David Anderson | 1d94d26 | 2019-01-11 20:39:51 -0800 | [diff] [blame] | 247 | binder::Status GsiService::commitGsiChunkFromStream(const android::os::ParcelFileDescriptor& stream, |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 248 | int64_t bytes, bool* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 249 | ENFORCE_SYSTEM; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 250 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 251 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 252 | if (!installer_) { |
| 253 | *_aidl_return = false; |
| 254 | return binder::Status::ok(); |
| 255 | } |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 256 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 257 | *_aidl_return = installer_->CommitGsiChunk(stream.get(), bytes); |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 258 | return binder::Status::ok(); |
| 259 | } |
| 260 | |
| 261 | void GsiService::StartAsyncOperation(const std::string& step, int64_t total_bytes) { |
| 262 | std::lock_guard<std::mutex> guard(progress_lock_); |
| 263 | |
| 264 | progress_.step = step; |
| 265 | progress_.status = STATUS_WORKING; |
| 266 | progress_.bytes_processed = 0; |
| 267 | progress_.total_bytes = total_bytes; |
| 268 | } |
| 269 | |
| 270 | void GsiService::UpdateProgress(int status, int64_t bytes_processed) { |
| 271 | std::lock_guard<std::mutex> guard(progress_lock_); |
| 272 | |
| 273 | progress_.status = status; |
David Anderson | a64e392 | 2019-02-01 16:27:18 -0800 | [diff] [blame] | 274 | if (status == STATUS_COMPLETE) { |
| 275 | progress_.bytes_processed = progress_.total_bytes; |
| 276 | } else { |
| 277 | progress_.bytes_processed = bytes_processed; |
| 278 | } |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | binder::Status GsiService::getInstallProgress(::android::gsi::GsiProgress* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 282 | ENFORCE_SYSTEM; |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 283 | std::lock_guard<std::mutex> guard(progress_lock_); |
| 284 | |
Howard Chen | 9f40baf | 2019-12-16 13:31:12 +0800 | [diff] [blame] | 285 | if (installer_ == nullptr) { |
| 286 | progress_ = {}; |
| 287 | } |
David Anderson | 6a5b8a7 | 2019-01-16 16:24:48 -0800 | [diff] [blame] | 288 | *_aidl_return = progress_; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 289 | return binder::Status::ok(); |
| 290 | } |
| 291 | |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 292 | binder::Status GsiService::commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 293 | ENFORCE_SYSTEM; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 294 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 295 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 296 | if (!installer_) { |
| 297 | *_aidl_return = false; |
| 298 | return binder::Status::ok(); |
| 299 | } |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 300 | *_aidl_return = installer_->CommitGsiChunk(bytes); |
| 301 | return binder::Status::ok(); |
| 302 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 303 | |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 304 | binder::Status GsiService::setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, |
| 305 | int64_t size, bool* _aidl_return) { |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 306 | ENFORCE_SYSTEM; |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 307 | if (!installer_) { |
| 308 | *_aidl_return = false; |
| 309 | return binder::Status::ok(); |
| 310 | } |
| 311 | *_aidl_return = installer_->MapAshmem(ashmem.get(), size); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 312 | return binder::Status::ok(); |
| 313 | } |
| 314 | |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 315 | binder::Status GsiService::enableGsiAsync(bool one_shot, const std::string& dsuSlot, |
| 316 | const sp<IGsiServiceCallback>& resultCallback) { |
Yi-Yo Chiang | d646d79 | 2022-04-04 21:38:57 +0800 | [diff] [blame] | 317 | ENFORCE_SYSTEM_OR_SHELL; |
| 318 | std::lock_guard<std::mutex> guard(lock_); |
| 319 | |
| 320 | const auto result = EnableGsi(one_shot, dsuSlot); |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 321 | resultCallback->onResult(result); |
| 322 | return binder::Status::ok(); |
| 323 | } |
| 324 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 325 | binder::Status GsiService::enableGsi(bool one_shot, const std::string& dsuSlot, int* _aidl_return) { |
Yi-Yo Chiang | 1ecc3af | 2021-12-02 18:30:24 +0800 | [diff] [blame] | 326 | ENFORCE_SYSTEM_OR_SHELL; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 327 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 328 | |
Yi-Yo Chiang | d646d79 | 2022-04-04 21:38:57 +0800 | [diff] [blame] | 329 | *_aidl_return = EnableGsi(one_shot, dsuSlot); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 330 | return binder::Status::ok(); |
| 331 | } |
| 332 | |
Howard Chen | 670b306 | 2019-02-26 18:14:47 +0800 | [diff] [blame] | 333 | binder::Status GsiService::isGsiEnabled(bool* _aidl_return) { |
| 334 | ENFORCE_SYSTEM_OR_SHELL; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 335 | std::lock_guard<std::mutex> guard(lock_); |
Howard Chen | 670b306 | 2019-02-26 18:14:47 +0800 | [diff] [blame] | 336 | std::string boot_key; |
| 337 | if (!GetInstallStatus(&boot_key)) { |
| 338 | *_aidl_return = false; |
| 339 | } else { |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 340 | *_aidl_return = (boot_key != kInstallStatusDisabled); |
Howard Chen | 670b306 | 2019-02-26 18:14:47 +0800 | [diff] [blame] | 341 | } |
| 342 | return binder::Status::ok(); |
| 343 | } |
| 344 | |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 345 | binder::Status GsiService::removeGsiAsync(const sp<IGsiServiceCallback>& resultCallback) { |
Chih-Hung Hsieh | 03478f2 | 2022-12-20 13:27:31 -0800 | [diff] [blame] | 346 | bool result = false; |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 347 | auto status = removeGsi(&result); |
| 348 | if (!status.isOk()) { |
| 349 | LOG(ERROR) << "Could not removeGsi: " << status.exceptionMessage().string(); |
| 350 | result = IGsiService::INSTALL_ERROR_GENERIC; |
| 351 | } |
| 352 | resultCallback->onResult(result); |
| 353 | return binder::Status::ok(); |
| 354 | } |
| 355 | |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 356 | binder::Status GsiService::removeGsi(bool* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 357 | ENFORCE_SYSTEM_OR_SHELL; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 358 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 359 | |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 360 | std::string install_dir = GetActiveInstalledImageDir(); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 361 | if (IsGsiRunning()) { |
| 362 | // Can't remove gsi files while running. |
| 363 | *_aidl_return = UninstallGsi(); |
| 364 | } else { |
Howard Chen | dee5d30 | 2020-02-05 10:58:29 +0800 | [diff] [blame] | 365 | installer_ = {}; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 366 | *_aidl_return = RemoveGsiFiles(install_dir); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 367 | } |
| 368 | return binder::Status::ok(); |
| 369 | } |
| 370 | |
Howard Chen | 25b18cc | 2019-08-02 11:21:58 +0800 | [diff] [blame] | 371 | binder::Status GsiService::disableGsi(bool* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 372 | ENFORCE_SYSTEM_OR_SHELL; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 373 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 374 | |
| 375 | *_aidl_return = DisableGsiInstall(); |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 376 | return binder::Status::ok(); |
| 377 | } |
| 378 | |
| 379 | binder::Status GsiService::isGsiRunning(bool* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 380 | ENFORCE_SYSTEM_OR_SHELL; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 381 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 382 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 383 | *_aidl_return = IsGsiRunning(); |
| 384 | return binder::Status::ok(); |
| 385 | } |
| 386 | |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 387 | binder::Status GsiService::isGsiInstalled(bool* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 388 | ENFORCE_SYSTEM_OR_SHELL; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 389 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 390 | |
| 391 | *_aidl_return = IsGsiInstalled(); |
| 392 | return binder::Status::ok(); |
| 393 | } |
| 394 | |
| 395 | binder::Status GsiService::isGsiInstallInProgress(bool* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 396 | ENFORCE_SYSTEM_OR_SHELL; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 397 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 398 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 399 | *_aidl_return = !!installer_; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 400 | return binder::Status::ok(); |
| 401 | } |
| 402 | |
| 403 | binder::Status GsiService::cancelGsiInstall(bool* _aidl_return) { |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 404 | ENFORCE_SYSTEM; |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 405 | should_abort_ = true; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 406 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 407 | |
Howard Chen | ecbc019 | 2019-02-25 18:51:26 +0800 | [diff] [blame] | 408 | should_abort_ = false; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 409 | installer_ = nullptr; |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 410 | |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 411 | *_aidl_return = true; |
| 412 | return binder::Status::ok(); |
| 413 | } |
| 414 | |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 415 | binder::Status GsiService::getInstalledGsiImageDir(std::string* _aidl_return) { |
| 416 | ENFORCE_SYSTEM; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 417 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 418 | |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 419 | *_aidl_return = GetActiveInstalledImageDir(); |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 420 | return binder::Status::ok(); |
| 421 | } |
| 422 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 423 | binder::Status GsiService::getActiveDsuSlot(std::string* _aidl_return) { |
| 424 | ENFORCE_SYSTEM_OR_SHELL; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 425 | std::lock_guard<std::mutex> guard(lock_); |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 426 | |
| 427 | *_aidl_return = GetActiveDsuSlot(); |
| 428 | return binder::Status::ok(); |
| 429 | } |
| 430 | |
| 431 | binder::Status GsiService::getInstalledDsuSlots(std::vector<std::string>* _aidl_return) { |
| 432 | ENFORCE_SYSTEM; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 433 | std::lock_guard<std::mutex> guard(lock_); |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 434 | *_aidl_return = GetInstalledDsuSlots(); |
| 435 | return binder::Status::ok(); |
| 436 | } |
| 437 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 438 | binder::Status GsiService::zeroPartition(const std::string& name, int* _aidl_return) { |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 439 | ENFORCE_SYSTEM_OR_SHELL; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 440 | std::lock_guard<std::mutex> guard(lock_); |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 441 | |
| 442 | if (IsGsiRunning() || !IsGsiInstalled()) { |
| 443 | *_aidl_return = IGsiService::INSTALL_ERROR_GENERIC; |
| 444 | return binder::Status::ok(); |
| 445 | } |
| 446 | |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 447 | std::string install_dir = GetActiveInstalledImageDir(); |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 448 | *_aidl_return = PartitionInstaller::WipeWritable(GetDsuSlot(install_dir), install_dir, name); |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 449 | |
| 450 | return binder::Status::ok(); |
| 451 | } |
| 452 | |
Yifan Hong | d66695b | 2019-12-23 16:07:45 -0800 | [diff] [blame] | 453 | static binder::Status BinderError(const std::string& message, |
| 454 | FiemapStatus::ErrorCode status = FiemapStatus::ErrorCode::ERROR) { |
| 455 | return binder::Status::fromServiceSpecificError(static_cast<int32_t>(status), message.c_str()); |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 456 | } |
| 457 | |
David Anderson | d614eca | 2019-09-09 17:57:06 -0700 | [diff] [blame] | 458 | binder::Status GsiService::dumpDeviceMapperDevices(std::string* _aidl_return) { |
| 459 | ENFORCE_SYSTEM_OR_SHELL; |
| 460 | |
| 461 | auto& dm = DeviceMapper::Instance(); |
| 462 | |
| 463 | std::vector<DeviceMapper::DmBlockDevice> devices; |
| 464 | if (!dm.GetAvailableDevices(&devices)) { |
| 465 | return BinderError("Could not list devices"); |
| 466 | } |
| 467 | |
| 468 | std::stringstream text; |
| 469 | for (const auto& device : devices) { |
| 470 | text << "Device " << device.name() << " (" << device.Major() << ":" << device.Minor() |
| 471 | << ")\n"; |
| 472 | |
| 473 | std::vector<DeviceMapper::TargetInfo> table; |
| 474 | if (!dm.GetTableInfo(device.name(), &table)) { |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | for (const auto& target : table) { |
| 479 | const auto& spec = target.spec; |
| 480 | auto target_type = DeviceMapper::GetTargetType(spec); |
| 481 | text << " " << target_type << " " << spec.sector_start << " " << spec.length << " " |
| 482 | << target.data << "\n"; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | *_aidl_return = text.str(); |
| 487 | return binder::Status::ok(); |
| 488 | } |
| 489 | |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 490 | binder::Status GsiService::getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) { |
| 491 | ENFORCE_SYSTEM; |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 492 | std::lock_guard<std::mutex> guard(lock_); |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 493 | |
| 494 | if (!installer_) { |
| 495 | *_aidl_return = INSTALL_ERROR_GENERIC; |
| 496 | return binder::Status::ok(); |
| 497 | } |
| 498 | int fd = installer_->GetPartitionFd(); |
| 499 | if (!GetAvbPublicKeyFromFd(fd, dst)) { |
| 500 | LOG(ERROR) << "Failed to extract AVB public key"; |
| 501 | *_aidl_return = INSTALL_ERROR_GENERIC; |
| 502 | return binder::Status::ok(); |
| 503 | } |
| 504 | *_aidl_return = INSTALL_OK; |
| 505 | return binder::Status::ok(); |
| 506 | } |
| 507 | |
Yo Chiang | 721a0e4 | 2020-12-28 19:29:47 +0800 | [diff] [blame] | 508 | binder::Status GsiService::suggestScratchSize(int64_t* _aidl_return) { |
| 509 | ENFORCE_SYSTEM; |
| 510 | |
Yi-Yo Chiang | 645cded | 2021-02-12 02:10:58 +0800 | [diff] [blame] | 511 | static constexpr uint64_t kMinScratchSize = 512_MiB; |
| 512 | static constexpr uint64_t kMaxScratchSize = 2_GiB; |
Yo Chiang | 721a0e4 | 2020-12-28 19:29:47 +0800 | [diff] [blame] | 513 | |
Yi-Yo Chiang | 645cded | 2021-02-12 02:10:58 +0800 | [diff] [blame] | 514 | uint64_t size = 0; |
Yo Chiang | 721a0e4 | 2020-12-28 19:29:47 +0800 | [diff] [blame] | 515 | struct statvfs info; |
| 516 | if (statvfs(install_dir_.c_str(), &info)) { |
| 517 | PLOG(ERROR) << "Could not statvfs(" << install_dir_ << ")"; |
| 518 | } else { |
Yi-Yo Chiang | 81af6b2 | 2021-11-03 20:26:32 +0800 | [diff] [blame] | 519 | uint64_t free_space = static_cast<uint64_t>(info.f_bavail) * info.f_frsize; |
| 520 | const auto free_space_threshold = |
| 521 | PartitionInstaller::GetMinimumFreeSpaceThreshold(install_dir_); |
| 522 | if (free_space_threshold.has_value() && free_space > *free_space_threshold) { |
| 523 | // Round down to multiples of filesystem block size. |
| 524 | size = (free_space - *free_space_threshold) / info.f_frsize * info.f_frsize; |
Yi-Yo Chiang | 645cded | 2021-02-12 02:10:58 +0800 | [diff] [blame] | 525 | } |
Yo Chiang | 721a0e4 | 2020-12-28 19:29:47 +0800 | [diff] [blame] | 526 | } |
| 527 | |
Yi-Yo Chiang | 645cded | 2021-02-12 02:10:58 +0800 | [diff] [blame] | 528 | // We can safely downcast the result here, since we clamped the result within int64_t range. |
Yo Chiang | 721a0e4 | 2020-12-28 19:29:47 +0800 | [diff] [blame] | 529 | *_aidl_return = std::clamp(size, kMinScratchSize, kMaxScratchSize); |
| 530 | return binder::Status::ok(); |
| 531 | } |
| 532 | |
Yi-Yo Chiang | 1ecc3af | 2021-12-02 18:30:24 +0800 | [diff] [blame] | 533 | bool GsiService::ResetBootAttemptCounter() { |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 534 | if (!android::base::WriteStringToFile("0", kDsuInstallStatusFile)) { |
| 535 | PLOG(ERROR) << "write " << kDsuInstallStatusFile; |
| 536 | return false; |
| 537 | } |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 538 | SetProperty(kGsiInstalledProp, "1"); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 539 | return true; |
| 540 | } |
| 541 | |
| 542 | bool GsiService::SetBootMode(bool one_shot) { |
| 543 | if (one_shot) { |
| 544 | if (!android::base::WriteStringToFile("1", kDsuOneShotBootFile)) { |
| 545 | PLOG(ERROR) << "write " << kDsuOneShotBootFile; |
| 546 | return false; |
| 547 | } |
| 548 | } else if (!access(kDsuOneShotBootFile, F_OK)) { |
| 549 | std::string error; |
| 550 | if (!android::base::RemoveFileIfExists(kDsuOneShotBootFile, &error)) { |
| 551 | LOG(ERROR) << error; |
| 552 | return false; |
| 553 | } |
| 554 | } |
| 555 | return true; |
| 556 | } |
| 557 | |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 558 | static binder::Status UidSecurityError() { |
| 559 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 560 | auto message = StringPrintf("UID %d is not allowed", uid); |
| 561 | return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(message.c_str())); |
| 562 | } |
| 563 | |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 564 | class ImageService : public BinderService<ImageService>, public BnImageService { |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 565 | public: |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 566 | ImageService(GsiService* service, std::unique_ptr<ImageManager>&& impl, uid_t uid); |
Howard Chen | 96c31fd | 2019-08-23 17:38:51 +0800 | [diff] [blame] | 567 | binder::Status getAllBackingImages(std::vector<std::string>* _aidl_return); |
Yifan Hong | d66695b | 2019-12-23 16:07:45 -0800 | [diff] [blame] | 568 | binder::Status createBackingImage(const std::string& name, int64_t size, int flags, |
| 569 | const sp<IProgressCallback>& on_progress) override; |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 570 | binder::Status deleteBackingImage(const std::string& name) override; |
| 571 | binder::Status mapImageDevice(const std::string& name, int32_t timeout_ms, |
| 572 | MappedImage* mapping) override; |
| 573 | binder::Status unmapImageDevice(const std::string& name) override; |
David Anderson | 394f1f2 | 2019-07-29 12:49:02 -0700 | [diff] [blame] | 574 | binder::Status backingImageExists(const std::string& name, bool* _aidl_return) override; |
David Anderson | bee55a0 | 2019-08-01 15:01:55 -0700 | [diff] [blame] | 575 | binder::Status isImageMapped(const std::string& name, bool* _aidl_return) override; |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 576 | binder::Status getAvbPublicKey(const std::string& name, AvbPublicKey* dst, |
| 577 | int32_t* _aidl_return) override; |
Alessio Balsini | 0829fb2 | 2019-08-27 17:50:18 +0100 | [diff] [blame] | 578 | binder::Status zeroFillNewImage(const std::string& name, int64_t bytes) override; |
David Anderson | 86846bd | 2019-11-26 16:30:40 -0800 | [diff] [blame] | 579 | binder::Status removeAllImages() override; |
Yi-Yo Chiang | 6c1043f | 2022-08-08 19:03:09 +0800 | [diff] [blame] | 580 | binder::Status disableImage(const std::string& name) override; |
David Anderson | 7016a0d | 2019-12-16 22:06:47 -0800 | [diff] [blame] | 581 | binder::Status removeDisabledImages() override; |
David Anderson | 14aa72a | 2019-12-17 21:27:59 -0800 | [diff] [blame] | 582 | binder::Status getMappedImageDevice(const std::string& name, std::string* device) override; |
David Anderson | f116bec | 2022-02-10 23:35:03 -0800 | [diff] [blame] | 583 | binder::Status isImageDisabled(const std::string& name, bool* _aidl_return) override; |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 584 | |
| 585 | private: |
| 586 | bool CheckUid(); |
| 587 | |
David Anderson | 4c75673 | 2019-07-12 14:18:37 -0700 | [diff] [blame] | 588 | android::sp<GsiService> service_; |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 589 | std::unique_ptr<ImageManager> impl_; |
| 590 | uid_t uid_; |
| 591 | }; |
| 592 | |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 593 | ImageService::ImageService(GsiService* service, std::unique_ptr<ImageManager>&& impl, uid_t uid) |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 594 | : service_(service), impl_(std::move(impl)), uid_(uid) {} |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 595 | |
Howard Chen | 96c31fd | 2019-08-23 17:38:51 +0800 | [diff] [blame] | 596 | binder::Status ImageService::getAllBackingImages(std::vector<std::string>* _aidl_return) { |
| 597 | *_aidl_return = impl_->GetAllBackingImages(); |
| 598 | return binder::Status::ok(); |
| 599 | } |
| 600 | |
Yifan Hong | d66695b | 2019-12-23 16:07:45 -0800 | [diff] [blame] | 601 | binder::Status ImageService::createBackingImage(const std::string& name, int64_t size, int flags, |
| 602 | const sp<IProgressCallback>& on_progress) { |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 603 | if (!CheckUid()) return UidSecurityError(); |
| 604 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 605 | std::lock_guard<std::mutex> guard(service_->lock()); |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 606 | |
Yifan Hong | d66695b | 2019-12-23 16:07:45 -0800 | [diff] [blame] | 607 | std::function<bool(uint64_t, uint64_t)> callback; |
| 608 | if (on_progress) { |
| 609 | callback = [on_progress](uint64_t current, uint64_t total) -> bool { |
| 610 | auto status = on_progress->onProgress(static_cast<int64_t>(current), |
| 611 | static_cast<int64_t>(total)); |
| 612 | if (!status.isOk()) { |
| 613 | LOG(ERROR) << "progress callback returned: " << status.toString8().string(); |
| 614 | return false; |
| 615 | } |
| 616 | return true; |
| 617 | }; |
| 618 | } |
| 619 | |
| 620 | auto res = impl_->CreateBackingImage(name, size, flags, std::move(callback)); |
| 621 | if (!res.is_ok()) { |
| 622 | return BinderError("Failed to create: " + res.string(), res.error_code()); |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 623 | } |
| 624 | return binder::Status::ok(); |
| 625 | } |
| 626 | |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 627 | binder::Status ImageService::deleteBackingImage(const std::string& name) { |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 628 | if (!CheckUid()) return UidSecurityError(); |
| 629 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 630 | std::lock_guard<std::mutex> guard(service_->lock()); |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 631 | |
| 632 | if (!impl_->DeleteBackingImage(name)) { |
| 633 | return BinderError("Failed to delete"); |
| 634 | } |
| 635 | return binder::Status::ok(); |
| 636 | } |
| 637 | |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 638 | binder::Status ImageService::mapImageDevice(const std::string& name, int32_t timeout_ms, |
| 639 | MappedImage* mapping) { |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 640 | if (!CheckUid()) return UidSecurityError(); |
| 641 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 642 | std::lock_guard<std::mutex> guard(service_->lock()); |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 643 | |
| 644 | if (!impl_->MapImageDevice(name, std::chrono::milliseconds(timeout_ms), &mapping->path)) { |
| 645 | return BinderError("Failed to map"); |
| 646 | } |
| 647 | return binder::Status::ok(); |
| 648 | } |
| 649 | |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 650 | binder::Status ImageService::unmapImageDevice(const std::string& name) { |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 651 | if (!CheckUid()) return UidSecurityError(); |
| 652 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 653 | std::lock_guard<std::mutex> guard(service_->lock()); |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 654 | |
| 655 | if (!impl_->UnmapImageDevice(name)) { |
| 656 | return BinderError("Failed to unmap"); |
| 657 | } |
| 658 | return binder::Status::ok(); |
| 659 | } |
| 660 | |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 661 | binder::Status ImageService::backingImageExists(const std::string& name, bool* _aidl_return) { |
David Anderson | 394f1f2 | 2019-07-29 12:49:02 -0700 | [diff] [blame] | 662 | if (!CheckUid()) return UidSecurityError(); |
| 663 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 664 | std::lock_guard<std::mutex> guard(service_->lock()); |
David Anderson | 394f1f2 | 2019-07-29 12:49:02 -0700 | [diff] [blame] | 665 | |
| 666 | *_aidl_return = impl_->BackingImageExists(name); |
| 667 | return binder::Status::ok(); |
| 668 | } |
| 669 | |
David Anderson | bee55a0 | 2019-08-01 15:01:55 -0700 | [diff] [blame] | 670 | binder::Status ImageService::isImageMapped(const std::string& name, bool* _aidl_return) { |
| 671 | if (!CheckUid()) return UidSecurityError(); |
| 672 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 673 | std::lock_guard<std::mutex> guard(service_->lock()); |
David Anderson | bee55a0 | 2019-08-01 15:01:55 -0700 | [diff] [blame] | 674 | |
| 675 | *_aidl_return = impl_->IsImageMapped(name); |
| 676 | return binder::Status::ok(); |
| 677 | } |
| 678 | |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 679 | binder::Status ImageService::getAvbPublicKey(const std::string& name, AvbPublicKey* dst, |
| 680 | int32_t* _aidl_return) { |
| 681 | if (!CheckUid()) return UidSecurityError(); |
| 682 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 683 | std::lock_guard<std::mutex> guard(service_->lock()); |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 684 | |
| 685 | std::string device_path; |
| 686 | std::unique_ptr<MappedDevice> mapped_device; |
| 687 | if (!impl_->IsImageMapped(name)) { |
| 688 | mapped_device = MappedDevice::Open(impl_.get(), 10s, name); |
| 689 | if (!mapped_device) { |
| 690 | PLOG(ERROR) << "Fail to map image: " << name; |
| 691 | *_aidl_return = IMAGE_ERROR; |
| 692 | return binder::Status::ok(); |
| 693 | } |
| 694 | device_path = mapped_device->path(); |
| 695 | } else { |
| 696 | if (!impl_->GetMappedImageDevice(name, &device_path)) { |
| 697 | PLOG(ERROR) << "GetMappedImageDevice() failed"; |
| 698 | *_aidl_return = IMAGE_ERROR; |
| 699 | return binder::Status::ok(); |
| 700 | } |
| 701 | } |
| 702 | android::base::unique_fd fd(open(device_path.c_str(), O_RDONLY | O_CLOEXEC)); |
| 703 | if (!fd.ok()) { |
| 704 | PLOG(ERROR) << "Fail to open mapped device: " << device_path; |
| 705 | *_aidl_return = IMAGE_ERROR; |
| 706 | return binder::Status::ok(); |
| 707 | } |
| 708 | bool ok = GetAvbPublicKeyFromFd(fd.get(), dst); |
| 709 | fd = {}; |
| 710 | if (!ok) { |
| 711 | LOG(ERROR) << "Failed to extract AVB public key"; |
| 712 | *_aidl_return = IMAGE_ERROR; |
| 713 | return binder::Status::ok(); |
| 714 | } |
| 715 | *_aidl_return = IMAGE_OK; |
| 716 | return binder::Status::ok(); |
| 717 | } |
| 718 | |
Alessio Balsini | 0829fb2 | 2019-08-27 17:50:18 +0100 | [diff] [blame] | 719 | binder::Status ImageService::zeroFillNewImage(const std::string& name, int64_t bytes) { |
| 720 | if (!CheckUid()) return UidSecurityError(); |
| 721 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 722 | std::lock_guard<std::mutex> guard(service_->lock()); |
Alessio Balsini | 0829fb2 | 2019-08-27 17:50:18 +0100 | [diff] [blame] | 723 | |
| 724 | if (bytes < 0) { |
| 725 | return BinderError("Cannot use negative values"); |
| 726 | } |
Yifan Hong | d66695b | 2019-12-23 16:07:45 -0800 | [diff] [blame] | 727 | auto res = impl_->ZeroFillNewImage(name, bytes); |
| 728 | if (!res.is_ok()) { |
| 729 | return BinderError("Failed to fill image with zeros: " + res.string(), res.error_code()); |
Alessio Balsini | 0829fb2 | 2019-08-27 17:50:18 +0100 | [diff] [blame] | 730 | } |
| 731 | return binder::Status::ok(); |
| 732 | } |
| 733 | |
David Anderson | 86846bd | 2019-11-26 16:30:40 -0800 | [diff] [blame] | 734 | binder::Status ImageService::removeAllImages() { |
| 735 | if (!CheckUid()) return UidSecurityError(); |
| 736 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 737 | std::lock_guard<std::mutex> guard(service_->lock()); |
David Anderson | 86846bd | 2019-11-26 16:30:40 -0800 | [diff] [blame] | 738 | if (!impl_->RemoveAllImages()) { |
| 739 | return BinderError("Failed to remove all images"); |
| 740 | } |
| 741 | return binder::Status::ok(); |
| 742 | } |
| 743 | |
Yi-Yo Chiang | 6c1043f | 2022-08-08 19:03:09 +0800 | [diff] [blame] | 744 | binder::Status ImageService::disableImage(const std::string& name) { |
| 745 | if (!CheckUid()) return UidSecurityError(); |
| 746 | |
| 747 | std::lock_guard<std::mutex> guard(service_->lock()); |
| 748 | if (!impl_->DisableImage(name)) { |
| 749 | return BinderError("Failed to disable image: " + name); |
| 750 | } |
| 751 | return binder::Status::ok(); |
| 752 | } |
| 753 | |
David Anderson | 7016a0d | 2019-12-16 22:06:47 -0800 | [diff] [blame] | 754 | binder::Status ImageService::removeDisabledImages() { |
| 755 | if (!CheckUid()) return UidSecurityError(); |
| 756 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 757 | std::lock_guard<std::mutex> guard(service_->lock()); |
David Anderson | 7016a0d | 2019-12-16 22:06:47 -0800 | [diff] [blame] | 758 | if (!impl_->RemoveDisabledImages()) { |
| 759 | return BinderError("Failed to remove disabled images"); |
| 760 | } |
| 761 | return binder::Status::ok(); |
| 762 | } |
| 763 | |
David Anderson | f116bec | 2022-02-10 23:35:03 -0800 | [diff] [blame] | 764 | binder::Status ImageService::isImageDisabled(const std::string& name, bool* _aidl_return) { |
| 765 | if (!CheckUid()) return UidSecurityError(); |
| 766 | |
| 767 | std::lock_guard<std::mutex> guard(service_->lock()); |
| 768 | *_aidl_return = impl_->IsImageDisabled(name); |
| 769 | return binder::Status::ok(); |
| 770 | } |
| 771 | |
David Anderson | 14aa72a | 2019-12-17 21:27:59 -0800 | [diff] [blame] | 772 | binder::Status ImageService::getMappedImageDevice(const std::string& name, std::string* device) { |
| 773 | if (!CheckUid()) return UidSecurityError(); |
| 774 | |
Howard Chen | 67a2709 | 2020-02-15 17:32:02 +0800 | [diff] [blame] | 775 | std::lock_guard<std::mutex> guard(service_->lock()); |
David Anderson | 14aa72a | 2019-12-17 21:27:59 -0800 | [diff] [blame] | 776 | if (!impl_->GetMappedImageDevice(name, device)) { |
| 777 | *device = ""; |
| 778 | } |
| 779 | return binder::Status::ok(); |
| 780 | } |
| 781 | |
David Anderson | bee55a0 | 2019-08-01 15:01:55 -0700 | [diff] [blame] | 782 | bool ImageService::CheckUid() { |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 783 | return uid_ == IPCThreadState::self()->getCallingUid(); |
| 784 | } |
| 785 | |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 786 | binder::Status GsiService::openImageService(const std::string& prefix, |
| 787 | android::sp<IImageService>* _aidl_return) { |
Howard Chen | 3a5e19a | 2021-01-07 16:07:52 +0800 | [diff] [blame] | 788 | using android::base::StartsWith; |
| 789 | |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 790 | static constexpr char kImageMetadataPrefix[] = "/metadata/gsi/"; |
| 791 | static constexpr char kImageDataPrefix[] = "/data/gsi/"; |
| 792 | |
David Anderson | 6ad45ba | 2019-07-29 12:51:23 -0700 | [diff] [blame] | 793 | auto in_metadata_dir = kImageMetadataPrefix + prefix; |
| 794 | auto in_data_dir = kImageDataPrefix + prefix; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 795 | auto install_dir_file = DsuInstallDirFile(GetDsuSlot(prefix)); |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 796 | |
Alessio Balsini | bb774d0 | 2020-01-21 23:34:39 +0000 | [diff] [blame] | 797 | std::string in_data_dir_tmp; |
| 798 | if (android::base::ReadFileToString(install_dir_file, &in_data_dir_tmp)) { |
| 799 | in_data_dir = in_data_dir_tmp; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 800 | LOG(INFO) << "load " << install_dir_file << ":" << in_data_dir; |
| 801 | } |
David Anderson | 6ad45ba | 2019-07-29 12:51:23 -0700 | [diff] [blame] | 802 | std::string metadata_dir, data_dir; |
| 803 | if (!android::base::Realpath(in_metadata_dir, &metadata_dir)) { |
Alessio Balsini | 4a3bcc6 | 2020-02-03 23:17:14 +0000 | [diff] [blame] | 804 | PLOG(ERROR) << "realpath failed for metadata: " << in_metadata_dir; |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 805 | return BinderError("Invalid path"); |
| 806 | } |
David Anderson | 6ad45ba | 2019-07-29 12:51:23 -0700 | [diff] [blame] | 807 | if (!android::base::Realpath(in_data_dir, &data_dir)) { |
Alessio Balsini | 4a3bcc6 | 2020-02-03 23:17:14 +0000 | [diff] [blame] | 808 | PLOG(ERROR) << "realpath failed for data: " << in_data_dir; |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 809 | return BinderError("Invalid path"); |
| 810 | } |
Howard Chen | 3a5e19a | 2021-01-07 16:07:52 +0800 | [diff] [blame] | 811 | if (!StartsWith(metadata_dir, kImageMetadataPrefix)) { |
| 812 | return BinderError("Invalid metadata path"); |
| 813 | } |
| 814 | if (!StartsWith(data_dir, kImageDataPrefix) && !StartsWith(data_dir, kDsuSDPrefix)) { |
| 815 | return BinderError("Invalid data path"); |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 819 | if (uid != AID_ROOT) { |
| 820 | return UidSecurityError(); |
| 821 | } |
| 822 | |
| 823 | auto impl = ImageManager::Open(metadata_dir, data_dir); |
| 824 | if (!impl) { |
| 825 | return BinderError("Unknown error"); |
| 826 | } |
| 827 | |
David Anderson | 551ae3a | 2019-08-01 12:53:06 -0700 | [diff] [blame] | 828 | *_aidl_return = new ImageService(this, std::move(impl), uid); |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 829 | return binder::Status::ok(); |
| 830 | } |
| 831 | |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 832 | binder::Status GsiService::CheckUid(AccessLevel level) { |
| 833 | std::vector<uid_t> allowed_uids{AID_ROOT, AID_SYSTEM}; |
| 834 | if (level == AccessLevel::SystemOrShell) { |
| 835 | allowed_uids.push_back(AID_SHELL); |
David Anderson | 3c819cb | 2019-02-08 17:50:50 -0800 | [diff] [blame] | 836 | } |
| 837 | |
David Anderson | b5f44b3 | 2019-02-20 18:04:37 -0800 | [diff] [blame] | 838 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 839 | for (const auto& allowed_uid : allowed_uids) { |
| 840 | if (allowed_uid == uid) { |
| 841 | return binder::Status::ok(); |
| 842 | } |
| 843 | } |
David Anderson | 6f373b7 | 2019-06-05 15:04:00 -0700 | [diff] [blame] | 844 | return UidSecurityError(); |
David Anderson | 3c819cb | 2019-02-08 17:50:50 -0800 | [diff] [blame] | 845 | } |
| 846 | |
David Anderson | e7a8ade | 2019-03-13 19:21:22 -0700 | [diff] [blame] | 847 | static bool IsExternalStoragePath(const std::string& path) { |
Howard Chen | 3a5e19a | 2021-01-07 16:07:52 +0800 | [diff] [blame] | 848 | if (!android::base::StartsWith(path, kDsuSDPrefix)) { |
David Anderson | e7a8ade | 2019-03-13 19:21:22 -0700 | [diff] [blame] | 849 | return false; |
| 850 | } |
| 851 | unique_fd fd(open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 852 | if (fd < 0) { |
| 853 | PLOG(ERROR) << "open failed: " << path; |
| 854 | return false; |
| 855 | } |
| 856 | struct statfs info; |
| 857 | if (fstatfs(fd, &info)) { |
| 858 | PLOG(ERROR) << "statfs failed: " << path; |
| 859 | return false; |
| 860 | } |
| 861 | LOG(ERROR) << "fs type: " << info.f_type; |
| 862 | return info.f_type == MSDOS_SUPER_MAGIC; |
| 863 | } |
| 864 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 865 | int GsiService::ValidateInstallParams(std::string& install_dir) { |
David Anderson | 49eea5f | 2019-05-23 13:27:01 -0700 | [diff] [blame] | 866 | // If no install path was specified, use the default path. We also allow |
| 867 | // specifying the top-level folder, and then we choose the correct location |
| 868 | // underneath. |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 869 | if (install_dir.empty() || install_dir == "/data/gsi") { |
| 870 | install_dir = kDefaultDsuImageFolder; |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Howard Chen | e187074 | 2021-02-18 18:32:20 +0800 | [diff] [blame] | 873 | if (access(install_dir.c_str(), F_OK) != 0 && (errno == ENOENT)) { |
| 874 | if (android::base::StartsWith(install_dir, kDsuSDPrefix)) { |
| 875 | if (mkdir(install_dir.c_str(), 0755) != 0) { |
| 876 | PLOG(ERROR) << "Failed to create " << install_dir; |
| 877 | return INSTALL_ERROR_GENERIC; |
| 878 | } |
| 879 | } |
| 880 | } |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 881 | // Normalize the path and add a trailing slash. |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 882 | std::string origInstallDir = install_dir; |
| 883 | if (!android::base::Realpath(origInstallDir, &install_dir)) { |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 884 | PLOG(ERROR) << "realpath failed: " << origInstallDir; |
| 885 | return INSTALL_ERROR_GENERIC; |
| 886 | } |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 887 | // Ensure the path ends in / for consistency. |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 888 | if (!android::base::EndsWith(install_dir, "/")) { |
| 889 | install_dir += "/"; |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 890 | } |
| 891 | |
David Anderson | e7a8ade | 2019-03-13 19:21:22 -0700 | [diff] [blame] | 892 | // Currently, we can only install to /data/gsi/ or external storage. |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 893 | if (IsExternalStoragePath(install_dir)) { |
David Anderson | e7a8ade | 2019-03-13 19:21:22 -0700 | [diff] [blame] | 894 | Fstab fstab; |
| 895 | if (!ReadDefaultFstab(&fstab)) { |
| 896 | LOG(ERROR) << "cannot read default fstab"; |
| 897 | return INSTALL_ERROR_GENERIC; |
| 898 | } |
| 899 | FstabEntry* system = GetEntryForMountPoint(&fstab, "/system"); |
| 900 | if (!system) { |
| 901 | LOG(ERROR) << "cannot find /system fstab entry"; |
| 902 | return INSTALL_ERROR_GENERIC; |
| 903 | } |
| 904 | if (fs_mgr_verity_is_check_at_most_once(*system)) { |
| 905 | LOG(ERROR) << "cannot install GSIs to external media if verity uses check_at_most_once"; |
| 906 | return INSTALL_ERROR_GENERIC; |
| 907 | } |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 908 | } else if (install_dir != kDefaultDsuImageFolder) { |
| 909 | LOG(ERROR) << "cannot install DSU to " << install_dir; |
David Anderson | eb30ac2 | 2019-03-12 15:24:53 -0700 | [diff] [blame] | 910 | return INSTALL_ERROR_GENERIC; |
| 911 | } |
| 912 | return INSTALL_OK; |
| 913 | } |
| 914 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 915 | std::string GsiService::GetActiveDsuSlot() { |
| 916 | if (!install_dir_.empty()) { |
| 917 | return GetDsuSlot(install_dir_); |
| 918 | } else { |
| 919 | std::string active_dsu; |
| 920 | return GetActiveDsu(&active_dsu) ? active_dsu : ""; |
| 921 | } |
| 922 | } |
| 923 | |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 924 | std::string GsiService::GetActiveInstalledImageDir() { |
| 925 | // Just in case an install was left hanging. |
| 926 | if (installer_) { |
| 927 | return installer_->install_dir(); |
| 928 | } else { |
| 929 | return GetInstalledImageDir(); |
| 930 | } |
| 931 | } |
| 932 | |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 933 | std::string GsiService::GetInstalledImageDir() { |
| 934 | // If there's no install left, just return /data/gsi since that's where |
| 935 | // installs go by default. |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 936 | std::string active_dsu; |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 937 | std::string dir; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 938 | if (GetActiveDsu(&active_dsu) && |
| 939 | android::base::ReadFileToString(DsuInstallDirFile(active_dsu), &dir)) { |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 940 | return dir; |
| 941 | } |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 942 | return kDefaultDsuImageFolder; |
David Anderson | 5f80591 | 2019-03-07 12:41:15 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Yo Chiang | 1f08091 | 2020-10-07 17:29:57 +0800 | [diff] [blame] | 945 | static android::sp<android::os::IVold> GetVoldService() { |
| 946 | return android::waitForService<android::os::IVold>(android::String16("vold")); |
| 947 | } |
| 948 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 949 | bool GsiService::RemoveGsiFiles(const std::string& install_dir) { |
David Anderson | 563e29a | 2019-03-06 19:20:45 -0800 | [diff] [blame] | 950 | bool ok = true; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 951 | auto active_dsu = GetDsuSlot(install_dir); |
| 952 | if (auto manager = ImageManager::Open(MetadataDir(active_dsu), install_dir)) { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 953 | std::vector<std::string> images = manager->GetAllBackingImages(); |
| 954 | for (auto&& image : images) { |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 955 | if (!android::base::EndsWith(image, kDsuPostfix)) { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 956 | continue; |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 957 | } |
| 958 | if (manager->IsImageMapped(image)) { |
| 959 | ok &= manager->UnmapImageDevice(image); |
| 960 | } |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 961 | ok &= manager->DeleteBackingImage(image); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 962 | } |
David Anderson | 563e29a | 2019-03-06 19:20:45 -0800 | [diff] [blame] | 963 | } |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 964 | auto dsu_slot = GetDsuSlot(install_dir); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 965 | std::vector<std::string> files{ |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 966 | kDsuInstallStatusFile, |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 967 | kDsuOneShotBootFile, |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 968 | DsuInstallDirFile(dsu_slot), |
| 969 | GetCompleteIndication(dsu_slot), |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 970 | }; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 971 | for (const auto& file : files) { |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 972 | std::string message; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 973 | if (!RemoveFileIfExists(file, &message)) { |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 974 | LOG(ERROR) << message; |
| 975 | ok = false; |
| 976 | } |
| 977 | } |
Yo Chiang | 1f08091 | 2020-10-07 17:29:57 +0800 | [diff] [blame] | 978 | if (auto vold = GetVoldService()) { |
| 979 | auto status = vold->destroyDsuMetadataKey(dsu_slot); |
| 980 | if (status.isOk()) { |
| 981 | std::string message; |
| 982 | if (!RemoveFileIfExists(DsuMetadataKeyDirFile(dsu_slot), &message)) { |
| 983 | LOG(ERROR) << message; |
| 984 | ok = false; |
| 985 | } |
| 986 | } else { |
| 987 | LOG(ERROR) << "Failed to destroy DSU metadata encryption key."; |
| 988 | ok = false; |
| 989 | } |
| 990 | } else { |
| 991 | LOG(ERROR) << "Failed to retrieve vold service."; |
| 992 | ok = false; |
| 993 | } |
Howard Chen | 7885d3c | 2020-02-26 12:48:41 +0800 | [diff] [blame] | 994 | if (ok) { |
| 995 | SetProperty(kGsiInstalledProp, "0"); |
| 996 | } |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 997 | return ok; |
| 998 | } |
| 999 | |
Yi-Yo Chiang | d646d79 | 2022-04-04 21:38:57 +0800 | [diff] [blame] | 1000 | int GsiService::EnableGsi(bool one_shot, const std::string& dsu_slot) { |
| 1001 | if (!android::gsi::IsGsiInstalled()) { |
| 1002 | LOG(ERROR) << "no gsi installed - cannot enable"; |
| 1003 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 1004 | } |
| 1005 | if (installer_) { |
| 1006 | LOG(ERROR) << "cannot enable an ongoing installation, was closeInstall() called?"; |
| 1007 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 1008 | } |
| 1009 | |
| 1010 | if (!DisableGsi()) { |
| 1011 | PLOG(ERROR) << "cannot write DSU status file"; |
| 1012 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 1013 | } |
| 1014 | if (!SetBootMode(one_shot)) { |
| 1015 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 1016 | } |
| 1017 | if (!ResetBootAttemptCounter()) { |
| 1018 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 1019 | } |
| 1020 | |
| 1021 | if (!WriteStringToFile(dsu_slot, kDsuActiveFile)) { |
| 1022 | PLOG(ERROR) << "cannot write active DSU slot (" << dsu_slot << "): " << kDsuActiveFile; |
| 1023 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 1024 | } |
| 1025 | RestoreconMetadataFiles(); |
| 1026 | return IGsiService::INSTALL_OK; |
| 1027 | } |
| 1028 | |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 1029 | bool GsiService::DisableGsiInstall() { |
| 1030 | if (!android::gsi::IsGsiInstalled()) { |
| 1031 | LOG(ERROR) << "cannot disable gsi install - no install detected"; |
| 1032 | return false; |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 1033 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 1034 | if (installer_) { |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 1035 | LOG(ERROR) << "cannot disable gsi during GSI installation"; |
| 1036 | return false; |
| 1037 | } |
| 1038 | if (!DisableGsi()) { |
| 1039 | PLOG(ERROR) << "could not write gsi status"; |
| 1040 | return false; |
| 1041 | } |
| 1042 | return true; |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 1045 | std::string GsiService::GetCompleteIndication(const std::string& dsu_slot) { |
| 1046 | return DSU_METADATA_PREFIX + dsu_slot + "/complete"; |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 1049 | bool GsiService::IsInstallationComplete(const std::string& dsu_slot) { |
| 1050 | if (access(kDsuInstallStatusFile, F_OK) != 0) { |
| 1051 | return false; |
| 1052 | } |
| 1053 | std::string file = GetCompleteIndication(dsu_slot); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 1054 | std::string content; |
| 1055 | if (!ReadFileToString(file, &content)) { |
| 1056 | return false; |
| 1057 | } |
| 1058 | return content == "OK"; |
| 1059 | } |
| 1060 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 1061 | std::vector<std::string> GsiService::GetInstalledDsuSlots() { |
| 1062 | std::vector<std::string> dsu_slots; |
| 1063 | auto d = std::unique_ptr<DIR, decltype(&closedir)>(opendir(DSU_METADATA_PREFIX), closedir); |
| 1064 | if (d != nullptr) { |
| 1065 | struct dirent* de; |
| 1066 | while ((de = readdir(d.get())) != nullptr) { |
| 1067 | if (de->d_name[0] == '.') { |
| 1068 | continue; |
| 1069 | } |
| 1070 | auto dsu_slot = std::string(de->d_name); |
| 1071 | if (access(DsuInstallDirFile(dsu_slot).c_str(), F_OK) != 0) { |
| 1072 | continue; |
| 1073 | } |
| 1074 | dsu_slots.push_back(dsu_slot); |
| 1075 | } |
| 1076 | } |
| 1077 | return dsu_slots; |
| 1078 | } |
| 1079 | |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 1080 | void GsiService::CleanCorruptedInstallation() { |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 1081 | for (auto&& slot : GetInstalledDsuSlots()) { |
| 1082 | bool is_complete = IsInstallationComplete(slot); |
| 1083 | if (!is_complete) { |
| 1084 | LOG(INFO) << "CleanCorruptedInstallation for slot: " << slot; |
| 1085 | std::string install_dir; |
| 1086 | if (!android::base::ReadFileToString(DsuInstallDirFile(slot), &install_dir) || |
| 1087 | !RemoveGsiFiles(install_dir)) { |
| 1088 | LOG(ERROR) << "Failed to CleanCorruptedInstallation on " << slot; |
| 1089 | } |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 1090 | } |
Howard Chen | 3f6d5a6 | 2019-08-22 15:26:33 +0800 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 1094 | void GsiService::RunStartupTasks() { |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 1095 | CleanCorruptedInstallation(); |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 1096 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 1097 | std::string active_dsu; |
| 1098 | if (!GetActiveDsu(&active_dsu)) { |
| 1099 | PLOG(INFO) << "no DSU"; |
| 1100 | return; |
| 1101 | } |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 1102 | std::string boot_key; |
| 1103 | if (!GetInstallStatus(&boot_key)) { |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 1104 | PLOG(ERROR) << "read " << kDsuInstallStatusFile; |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | if (!IsGsiRunning()) { |
| 1109 | // Check if a wipe was requested from fastboot or adb-in-gsi. |
| 1110 | if (boot_key == kInstallStatusWipe) { |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 1111 | RemoveGsiFiles(GetInstalledImageDir()); |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 1112 | } |
| 1113 | } else { |
David Anderson | 564a04c | 2019-02-27 13:33:44 -0800 | [diff] [blame] | 1114 | // NB: When single-boot is enabled, init will write "disabled" into the |
| 1115 | // install_status file, which will cause GetBootAttempts to return |
| 1116 | // false. Thus, we won't write "ok" here. |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 1117 | int ignore; |
| 1118 | if (GetBootAttempts(boot_key, &ignore)) { |
| 1119 | // Mark the GSI as having successfully booted. |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 1120 | if (!android::base::WriteStringToFile(kInstallStatusOk, kDsuInstallStatusFile)) { |
| 1121 | PLOG(ERROR) << "write " << kDsuInstallStatusFile; |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
David Anderson | 73274df | 2022-01-10 02:37:33 +0000 | [diff] [blame] | 1127 | void GsiService::VerifyImageMaps() { |
| 1128 | std::vector<std::pair<std::string, std::string>> paths = { |
| 1129 | {"/metadata/gsi/remount", "/data/gsi/remount"}, |
| 1130 | {"/metadata/gsi/ota", "/data/gsi/ota"}, |
| 1131 | }; |
| 1132 | |
| 1133 | for (const auto& [metadata_dir, data_dir] : paths) { |
| 1134 | auto impl = ImageManager::Open(metadata_dir, data_dir); |
| 1135 | if (!impl) { |
| 1136 | LOG(ERROR) << "Could not open ImageManager for " << metadata_dir << " and " << data_dir; |
| 1137 | continue; |
| 1138 | } |
| 1139 | if (!impl->ValidateImageMaps()) { |
| 1140 | LOG(ERROR) << "ImageManager for " << metadata_dir |
| 1141 | << " failed validation, device data is at risk. Rebooting."; |
| 1142 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot"); |
| 1143 | continue; |
| 1144 | } |
| 1145 | LOG(INFO) << "ImageManager verification passed for " << metadata_dir; |
| 1146 | } |
| 1147 | } |
| 1148 | |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 1149 | static bool GetAvbPublicKeyFromFd(int fd, AvbPublicKey* dst) { |
| 1150 | // Read the AVB footer from EOF. |
| 1151 | int64_t total_size = get_block_device_size(fd); |
| 1152 | int64_t footer_offset = total_size - AVB_FOOTER_SIZE; |
| 1153 | std::array<uint8_t, AVB_FOOTER_SIZE> footer_bytes; |
| 1154 | if (!ReadFullyAtOffset(fd, footer_bytes.data(), AVB_FOOTER_SIZE, footer_offset)) { |
| 1155 | PLOG(ERROR) << "cannot read AVB footer"; |
| 1156 | return false; |
| 1157 | } |
| 1158 | // Validate the AVB footer data and byte swap to native byte order. |
| 1159 | AvbFooter footer; |
| 1160 | if (!avb_footer_validate_and_byteswap((const AvbFooter*)footer_bytes.data(), &footer)) { |
| 1161 | LOG(ERROR) << "invalid AVB footer"; |
| 1162 | return false; |
| 1163 | } |
| 1164 | // Read the VBMeta image. |
| 1165 | std::vector<uint8_t> vbmeta_bytes(footer.vbmeta_size); |
| 1166 | if (!ReadFullyAtOffset(fd, vbmeta_bytes.data(), vbmeta_bytes.size(), footer.vbmeta_offset)) { |
| 1167 | PLOG(ERROR) << "cannot read VBMeta image"; |
| 1168 | return false; |
| 1169 | } |
| 1170 | // Validate the VBMeta image and retrieve AVB public key. |
| 1171 | // After a successful call to avb_vbmeta_image_verify(), public_key_data |
| 1172 | // will point to the serialized AVB public key, in the same format generated |
| 1173 | // by the `avbtool extract_public_key` command. |
| 1174 | const uint8_t* public_key_data; |
| 1175 | size_t public_key_size; |
| 1176 | AvbVBMetaVerifyResult result = avb_vbmeta_image_verify(vbmeta_bytes.data(), vbmeta_bytes.size(), |
| 1177 | &public_key_data, &public_key_size); |
| 1178 | if (result != AVB_VBMETA_VERIFY_RESULT_OK) { |
| 1179 | LOG(ERROR) << "invalid VBMeta image: " << avb_vbmeta_verify_result_to_string(result); |
| 1180 | return false; |
| 1181 | } |
| 1182 | if (public_key_data != nullptr) { |
| 1183 | dst->bytes.resize(public_key_size); |
| 1184 | memcpy(dst->bytes.data(), public_key_data, public_key_size); |
| 1185 | dst->sha1.resize(SHA_DIGEST_LENGTH); |
| 1186 | SHA1(public_key_data, public_key_size, dst->sha1.data()); |
| 1187 | } |
| 1188 | return true; |
| 1189 | } |
| 1190 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 1191 | } // namespace gsi |
| 1192 | } // namespace android |