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 | |
| 35 | class GsiInstaller 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. |
| 41 | GsiInstaller(GsiService* service, const GsiInstallParams& params); |
| 42 | // Constructor for re-enabling a previous GSI installation. |
| 43 | GsiInstaller(GsiService* service, const std::string& install_dir); |
| 44 | ~GsiInstaller(); |
| 45 | |
| 46 | // Methods for a clean GSI install. |
| 47 | int StartInstall(); |
| 48 | bool CommitGsiChunk(int stream_fd, int64_t bytes); |
| 49 | bool CommitGsiChunk(const void* data, size_t bytes); |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 50 | bool MapAshmem(int fd, size_t size); |
| 51 | bool CommitGsiChunk(size_t bytes); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 52 | int SetGsiBootable(bool one_shot); |
| 53 | |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 54 | // Methods for interacting with an existing install. |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 55 | int ReenableGsi(bool one_shot); |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 56 | int WipeUserdata(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 57 | |
| 58 | // Clean up install state if gsid crashed and restarted. |
| 59 | static void PostInstallCleanup(); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 60 | static void PostInstallCleanup(ImageManager* manager); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 61 | |
| 62 | const std::string& install_dir() const { return install_dir_; } |
| 63 | uint64_t userdata_size() const { return userdata_size_; } |
| 64 | |
| 65 | private: |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 66 | int PerformSanityChecks(); |
| 67 | int PreallocateFiles(); |
| 68 | int PreallocateUserdata(); |
| 69 | int PreallocateSystem(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 70 | bool FormatUserdata(); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 71 | bool CreateImage(const std::string& name, uint64_t size, bool readonly); |
| 72 | std::unique_ptr<MappedDevice> OpenPartition(const std::string& name); |
| 73 | int CheckInstallState(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 74 | bool CreateInstallStatusFile(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 75 | bool SetBootMode(bool one_shot); |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 76 | bool IsFinishedWriting(); |
| 77 | bool IsAshmemMapped(); |
| 78 | void UnmapAshmem(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 79 | |
| 80 | GsiService* service_; |
| 81 | |
| 82 | std::string install_dir_; |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 83 | std::unique_ptr<ImageManager> images_; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 84 | uint64_t gsi_size_ = 0; |
| 85 | uint64_t userdata_size_ = 0; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 86 | bool wipe_userdata_ = false; |
| 87 | bool wipe_userdata_on_failure_ = false; |
| 88 | // Remaining data we're waiting to receive for the GSI image. |
| 89 | uint64_t gsi_bytes_written_ = 0; |
| 90 | bool succeeded_ = false; |
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 | |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 94 | std::unique_ptr<MappedDevice> system_device_; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | } // namespace gsi |
| 98 | } // namespace android |