David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [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 | |
| 18 | #include <stdint.h> |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 19 | #include <sys/mman.h> |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 20 | |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 24 | #include <android-base/unique_fd.h> |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 25 | #include <android/gsi/IGsiService.h> |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 26 | #include <android/gsi/MappedImage.h> |
| 27 | #include <libfiemap/image_manager.h> |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 28 | #include <liblp/builder.h> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace gsi { |
| 32 | |
| 33 | class GsiService; |
| 34 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 35 | class PartitionInstaller final { |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 36 | using ImageManager = android::fiemap::ImageManager; |
| 37 | using MappedDevice = android::fiemap::MappedDevice; |
| 38 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 39 | public: |
| 40 | // Constructor for a new GSI installation. |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 41 | PartitionInstaller(GsiService* service, const std::string& installDir, const std::string& name, |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 42 | const std::string& active_dsu, int64_t size, bool read_only); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 43 | ~PartitionInstaller(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 44 | |
| 45 | // Methods for a clean GSI install. |
| 46 | int StartInstall(); |
| 47 | bool CommitGsiChunk(int stream_fd, int64_t bytes); |
| 48 | bool CommitGsiChunk(const void* data, size_t bytes); |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 49 | bool MapAshmem(int fd, size_t size); |
| 50 | bool CommitGsiChunk(size_t bytes); |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 51 | int GetPartitionFd(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 52 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 53 | static int WipeWritable(const std::string& active_dsu, const std::string& install_dir, |
| 54 | const std::string& name); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 55 | |
Yi-Yo Chiang | 81af6b2 | 2021-11-03 20:26:32 +0800 | [diff] [blame] | 56 | // Returns the minimum free space to reserve for /data. |
| 57 | static std::optional<uint64_t> GetMinimumFreeSpaceThreshold(const std::string& install_dir); |
| 58 | |
Yo Chiang | f194dce | 2020-08-24 17:21:10 +0800 | [diff] [blame] | 59 | // Finish a partition installation and release resources. |
| 60 | // If the installation is incomplete or corrupted, the backing image would |
| 61 | // be cleaned up and an error code is returned. |
| 62 | // No method other than FinishInstall() and ~PartitionInstaller() should be |
| 63 | // called after calling this method. |
| 64 | // This method is also called by the destructor to free up resources. |
| 65 | int FinishInstall(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 66 | |
| 67 | const std::string& install_dir() const { return install_dir_; } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 68 | |
| 69 | private: |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 70 | int PerformSanityChecks(); |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 71 | int Preallocate(); |
| 72 | bool Format(); |
| 73 | bool CreateImage(const std::string& name, uint64_t size); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 74 | std::unique_ptr<MappedDevice> OpenPartition(const std::string& name); |
| 75 | int CheckInstallState(); |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 76 | static const std::string GetBackingFile(std::string name); |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 77 | bool IsFinishedWriting(); |
| 78 | bool IsAshmemMapped(); |
| 79 | void UnmapAshmem(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 80 | |
| 81 | GsiService* service_; |
| 82 | |
| 83 | std::string install_dir_; |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 84 | std::string name_; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 85 | std::string active_dsu_; |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 86 | std::unique_ptr<ImageManager> images_; |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 87 | uint64_t size_ = 0; |
| 88 | bool readOnly_; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 89 | // Remaining data we're waiting to receive for the GSI image. |
| 90 | uint64_t gsi_bytes_written_ = 0; |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 91 | uint64_t ashmem_size_ = -1; |
| 92 | void* ashmem_data_ = MAP_FAILED; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 93 | |
Yo Chiang | f194dce | 2020-08-24 17:21:10 +0800 | [diff] [blame] | 94 | bool finished_ = false; |
| 95 | int finished_status_ = 0; |
| 96 | |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 97 | std::unique_ptr<MappedDevice> system_device_; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace gsi |
| 101 | } // namespace android |