blob: 33bebfbe203f43b2337a0d480d4f395bd1aa25ad [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
35class GsiInstaller 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.
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 Chen5676d962019-08-05 16:21:00 +080050 bool MapAshmem(int fd, size_t size);
51 bool CommitGsiChunk(size_t bytes);
David Andersonb2988ab2019-04-16 17:14:09 -070052 int SetGsiBootable(bool one_shot);
53
David Anderson8bdf6252019-06-11 16:43:24 -070054 // Methods for interacting with an existing install.
David Andersonb2988ab2019-04-16 17:14:09 -070055 int ReenableGsi(bool one_shot);
David Anderson8bdf6252019-06-11 16:43:24 -070056 int WipeUserdata();
David Andersonb2988ab2019-04-16 17:14:09 -070057
58 // Clean up install state if gsid crashed and restarted.
59 static void PostInstallCleanup();
David Anderson64b53fb2019-07-01 19:05:35 -070060 static void PostInstallCleanup(ImageManager* manager);
David Andersonb2988ab2019-04-16 17:14:09 -070061
62 const std::string& install_dir() const { return install_dir_; }
63 uint64_t userdata_size() const { return userdata_size_; }
64
65 private:
David Andersonb2988ab2019-04-16 17:14:09 -070066 int PerformSanityChecks();
67 int PreallocateFiles();
68 int PreallocateUserdata();
69 int PreallocateSystem();
David Andersonb2988ab2019-04-16 17:14:09 -070070 bool FormatUserdata();
David Anderson64b53fb2019-07-01 19:05:35 -070071 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 Andersonb2988ab2019-04-16 17:14:09 -070074 bool CreateInstallStatusFile();
David Andersonb2988ab2019-04-16 17:14:09 -070075 bool SetBootMode(bool one_shot);
Howard Chen5676d962019-08-05 16:21:00 +080076 bool IsFinishedWriting();
77 bool IsAshmemMapped();
78 void UnmapAshmem();
David Andersonb2988ab2019-04-16 17:14:09 -070079
80 GsiService* service_;
81
82 std::string install_dir_;
David Anderson64b53fb2019-07-01 19:05:35 -070083 std::unique_ptr<ImageManager> images_;
David Andersonb2988ab2019-04-16 17:14:09 -070084 uint64_t gsi_size_ = 0;
85 uint64_t userdata_size_ = 0;
David Andersonb2988ab2019-04-16 17:14:09 -070086 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 Chen5676d962019-08-05 16:21:00 +080091 uint64_t ashmem_size_ = -1;
92 void* ashmem_data_ = MAP_FAILED;
David Andersonb2988ab2019-04-16 17:14:09 -070093
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