blob: 920af4739d8cb16f5337d198e6955fcbd61d103f [file] [log] [blame]
David Andersonb2988ab2019-04-16 17:14:09 -07001/*
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 Chen5676d962019-08-05 16:21:00 +080019#include <sys/mman.h>
David Andersonb2988ab2019-04-16 17:14:09 -070020
21#include <memory>
22#include <string>
23
Howard Chen5676d962019-08-05 16:21:00 +080024#include <android-base/unique_fd.h>
David Andersonb2988ab2019-04-16 17:14:09 -070025#include <android/gsi/IGsiService.h>
David Anderson64b53fb2019-07-01 19:05:35 -070026#include <android/gsi/MappedImage.h>
27#include <libfiemap/image_manager.h>
David Andersonb2988ab2019-04-16 17:14:09 -070028#include <liblp/builder.h>
29
30namespace android {
31namespace gsi {
32
33class GsiService;
34
Howard Chen4663de62019-11-05 20:46:20 +080035class PartitionInstaller final {
David Anderson64b53fb2019-07-01 19:05:35 -070036 using ImageManager = android::fiemap::ImageManager;
37 using MappedDevice = android::fiemap::MappedDevice;
38
David Andersonb2988ab2019-04-16 17:14:09 -070039 public:
40 // Constructor for a new GSI installation.
Howard Chen4663de62019-11-05 20:46:20 +080041 PartitionInstaller(GsiService* service, const std::string& installDir, const std::string& name,
Howard Chenee5c2b12019-11-08 11:57:47 +080042 const std::string& active_dsu, int64_t size, bool read_only);
Howard Chen4663de62019-11-05 20:46:20 +080043 ~PartitionInstaller();
David Andersonb2988ab2019-04-16 17:14:09 -070044
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 Chen5676d962019-08-05 16:21:00 +080049 bool MapAshmem(int fd, size_t size);
50 bool CommitGsiChunk(size_t bytes);
Yo Chiang53bed1c2020-01-01 16:25:19 +080051 int GetPartitionFd();
David Andersonb2988ab2019-04-16 17:14:09 -070052
Howard Chenee5c2b12019-11-08 11:57:47 +080053 static int WipeWritable(const std::string& active_dsu, const std::string& install_dir,
54 const std::string& name);
David Andersonb2988ab2019-04-16 17:14:09 -070055
Yo Chiangf194dce2020-08-24 17:21:10 +080056 // 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();
David Andersonb2988ab2019-04-16 17:14:09 -070063
64 const std::string& install_dir() const { return install_dir_; }
David Andersonb2988ab2019-04-16 17:14:09 -070065
66 private:
David Andersonb2988ab2019-04-16 17:14:09 -070067 int PerformSanityChecks();
Howard Chen18109b12019-08-13 17:00:44 +080068 int Preallocate();
69 bool Format();
70 bool CreateImage(const std::string& name, uint64_t size);
David Anderson64b53fb2019-07-01 19:05:35 -070071 std::unique_ptr<MappedDevice> OpenPartition(const std::string& name);
72 int CheckInstallState();
Howard Chen18109b12019-08-13 17:00:44 +080073 static const std::string GetBackingFile(std::string name);
Howard Chen5676d962019-08-05 16:21:00 +080074 bool IsFinishedWriting();
75 bool IsAshmemMapped();
76 void UnmapAshmem();
David Andersonb2988ab2019-04-16 17:14:09 -070077
78 GsiService* service_;
79
80 std::string install_dir_;
Howard Chen18109b12019-08-13 17:00:44 +080081 std::string name_;
Howard Chenee5c2b12019-11-08 11:57:47 +080082 std::string active_dsu_;
David Anderson64b53fb2019-07-01 19:05:35 -070083 std::unique_ptr<ImageManager> images_;
Howard Chen18109b12019-08-13 17:00:44 +080084 uint64_t size_ = 0;
85 bool readOnly_;
David Andersonb2988ab2019-04-16 17:14:09 -070086 // Remaining data we're waiting to receive for the GSI image.
87 uint64_t gsi_bytes_written_ = 0;
Howard Chen5676d962019-08-05 16:21:00 +080088 uint64_t ashmem_size_ = -1;
89 void* ashmem_data_ = MAP_FAILED;
David Andersonb2988ab2019-04-16 17:14:09 -070090
Yo Chiangf194dce2020-08-24 17:21:10 +080091 bool finished_ = false;
92 int finished_status_ = 0;
93
David Anderson64b53fb2019-07-01 19:05:35 -070094 std::unique_ptr<MappedDevice> system_device_;
David Andersonb2988ab2019-04-16 17:14:09 -070095};
96
97} // namespace gsi
98} // namespace android