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>
19 #include <sys/mman.h>
20 
21 #include <memory>
22 #include <string>
23 
24 #include <android-base/unique_fd.h>
25 #include <android/gsi/IGsiService.h>
26 #include <android/gsi/MappedImage.h>
27 #include <libfiemap/image_manager.h>
28 #include <liblp/builder.h>
29 
30 namespace android {
31 namespace gsi {
32 
33 class GsiService;
34 
35 class PartitionInstaller final {
36     using ImageManager = android::fiemap::ImageManager;
37     using MappedDevice = android::fiemap::MappedDevice;
38 
39   public:
40     // Constructor for a new GSI installation.
41     PartitionInstaller(GsiService* service, const std::string& installDir, const std::string& name,
42                        const std::string& active_dsu, int64_t size, bool read_only);
43     ~PartitionInstaller();
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);
49     bool MapAshmem(int fd, size_t size);
50     bool CommitGsiChunk(size_t bytes);
51     int GetPartitionFd();
52 
53     static int WipeWritable(const std::string& active_dsu, const std::string& install_dir,
54                             const std::string& name);
55 
56     // Finish a partition installation and release resources.
57     // If the installation is incomplete or corrupted, the backing image would
58     // be cleaned up and an error code is returned.
59     // No method other than FinishInstall() and ~PartitionInstaller() should be
60     // called after calling this method.
61     // This method is also called by the destructor to free up resources.
62     int FinishInstall();
63 
install_dir()64     const std::string& install_dir() const { return install_dir_; }
65 
66   private:
67     int PerformSanityChecks();
68     int Preallocate();
69     bool Format();
70     bool CreateImage(const std::string& name, uint64_t size);
71     std::unique_ptr<MappedDevice> OpenPartition(const std::string& name);
72     int CheckInstallState();
73     static const std::string GetBackingFile(std::string name);
74     bool IsFinishedWriting();
75     bool IsAshmemMapped();
76     void UnmapAshmem();
77 
78     GsiService* service_;
79 
80     std::string install_dir_;
81     std::string name_;
82     std::string active_dsu_;
83     std::unique_ptr<ImageManager> images_;
84     uint64_t size_ = 0;
85     bool readOnly_;
86     // Remaining data we're waiting to receive for the GSI image.
87     uint64_t gsi_bytes_written_ = 0;
88     uint64_t ashmem_size_ = -1;
89     void* ashmem_data_ = MAP_FAILED;
90 
91     bool finished_ = false;
92     int finished_status_ = 0;
93 
94     std::unique_ptr<MappedDevice> system_device_;
95 };
96 
97 }  // namespace gsi
98 }  // namespace android
99