blob: c50c10193f83ff0da56ef2874ed2c00750951155 [file] [log] [blame]
David Andersonc053b3b2019-01-08 18:22:07 -08001/*
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
David Andersona64e3922019-02-01 16:27:18 -080018#include <map>
David Andersonc053b3b2019-01-08 18:22:07 -080019#include <memory>
20#include <mutex>
21#include <sstream>
David Andersona64e3922019-02-01 16:27:18 -080022#include <string>
David Andersonc053b3b2019-01-08 18:22:07 -080023#include <vector>
24
25#include <android-base/unique_fd.h>
26#include <android/gsi/BnGsiService.h>
27#include <binder/BinderService.h>
David Anderson9ca77282019-07-15 23:56:13 +000028#include <libfiemap/split_fiemap_writer.h>
David Andersonc053b3b2019-01-08 18:22:07 -080029#include <liblp/builder.h>
30#include "libgsi/libgsi.h"
31
Howard Chen4663de62019-11-05 20:46:20 +080032#include "partition_installer.h"
David Andersonb2988ab2019-04-16 17:14:09 -070033
David Andersonc053b3b2019-01-08 18:22:07 -080034namespace android {
35namespace gsi {
36
David Anderson4c756732019-07-12 14:18:37 -070037class GsiService : public BinderService<GsiService>, public BnGsiService {
38 public:
Howard Chen67a27092020-02-15 17:32:02 +080039 static void Register();
David Anderson4c756732019-07-12 14:18:37 -070040
Howard Chen4663de62019-11-05 20:46:20 +080041 binder::Status openInstall(const std::string& install_dir, int* _aidl_return) override;
42 binder::Status closeInstall(int32_t* _aidl_return) override;
43 binder::Status createPartition(const ::std::string& name, int64_t size, bool readOnly,
44 int32_t* _aidl_return) override;
Yo Chiang36fdbc62020-08-20 19:40:31 +080045 binder::Status closePartition(int32_t* _aidl_return) override;
David Anderson1d94d262019-01-11 20:39:51 -080046 binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream,
47 int64_t bytes, bool* _aidl_return) override;
David Anderson6a5b8a72019-01-16 16:24:48 -080048 binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override;
Howard Chen5676d962019-08-05 16:21:00 +080049 binder::Status setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, int64_t size,
50 bool* _aidl_return) override;
51 binder::Status commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080052 binder::Status cancelGsiInstall(bool* _aidl_return) override;
Howard Chenee5c2b12019-11-08 11:57:47 +080053 binder::Status enableGsi(bool oneShot, const std::string& dsuSlot, int* _aidl_return) override;
Howard Chen7885d3c2020-02-26 12:48:41 +080054 binder::Status enableGsiAsync(bool oneShot, const ::std::string& dsuSlot,
55 const sp<IGsiServiceCallback>& resultCallback) override;
Howard Chen670b3062019-02-26 18:14:47 +080056 binder::Status isGsiEnabled(bool* _aidl_return) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080057 binder::Status removeGsi(bool* _aidl_return) override;
Howard Chen7885d3c2020-02-26 12:48:41 +080058 binder::Status removeGsiAsync(const sp<IGsiServiceCallback>& resultCallback) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080059 binder::Status disableGsi(bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080060 binder::Status isGsiInstalled(bool* _aidl_return) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080061 binder::Status isGsiRunning(bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080062 binder::Status isGsiInstallInProgress(bool* _aidl_return) override;
David Anderson5f805912019-03-07 12:41:15 -080063 binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override;
Howard Chenee5c2b12019-11-08 11:57:47 +080064 binder::Status getActiveDsuSlot(std::string* _aidl_return) override;
65 binder::Status getInstalledDsuSlots(std::vector<std::string>* _aidl_return) override;
Howard Chen4663de62019-11-05 20:46:20 +080066 binder::Status zeroPartition(const std::string& name, int* _aidl_return) override;
David Anderson551ae3a2019-08-01 12:53:06 -070067 binder::Status openImageService(const std::string& prefix,
68 android::sp<IImageService>* _aidl_return) override;
David Andersond614eca2019-09-09 17:57:06 -070069 binder::Status dumpDeviceMapperDevices(std::string* _aidl_return) override;
Yo Chiang53bed1c2020-01-01 16:25:19 +080070 binder::Status getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) override;
Yo Chiang721a0e42020-12-28 19:29:47 +080071 binder::Status suggestScratchSize(int64_t* _aidl_return) override;
David Andersonc053b3b2019-01-08 18:22:07 -080072
David Andersonb2988ab2019-04-16 17:14:09 -070073 // This is in GsiService, rather than GsiInstaller, since we need to access
74 // it outside of the main lock which protects the unique_ptr.
75 void StartAsyncOperation(const std::string& step, int64_t total_bytes);
76 void UpdateProgress(int status, int64_t bytes_processed);
77
David Andersonb2988ab2019-04-16 17:14:09 -070078 // Helper methods for GsiInstaller.
Howard Chen4663de62019-11-05 20:46:20 +080079 static bool RemoveGsiFiles(const std::string& install_dir);
David Andersonb2988ab2019-04-16 17:14:09 -070080 bool should_abort() const { return should_abort_; }
81
David Andersonb3aff182019-01-11 14:37:51 -080082 static void RunStartupTasks();
David Anderson73274df2022-01-10 02:37:33 +000083 static void VerifyImageMaps();
David Anderson64b53fb2019-07-01 19:05:35 -070084 static std::string GetInstalledImageDir();
Howard Chenee5c2b12019-11-08 11:57:47 +080085 std::string GetActiveDsuSlot();
Howard Chen3f6d5a62019-08-22 15:26:33 +080086 std::string GetActiveInstalledImageDir();
David Andersonb3aff182019-01-11 14:37:51 -080087
Howard Chenee5c2b12019-11-08 11:57:47 +080088 static std::vector<std::string> GetInstalledDsuSlots();
89
David Andersonc053b3b2019-01-08 18:22:07 -080090 private:
Howard Chen67a27092020-02-15 17:32:02 +080091 friend class ImageService;
92
93 GsiService();
Howard Chen4663de62019-11-05 20:46:20 +080094 static int ValidateInstallParams(std::string& install_dir);
Yi-Yo Chiangd646d792022-04-04 21:38:57 +080095 int EnableGsi(bool one_shot, const std::string& dsu_slot);
David Andersona141ba82019-01-14 19:09:27 -080096 bool DisableGsiInstall();
Howard Chen3f6d5a62019-08-22 15:26:33 +080097 static void CleanCorruptedInstallation();
Howard Chen4663de62019-11-05 20:46:20 +080098 static int SaveInstallation(const std::string&);
99 static bool IsInstallationComplete(const std::string&);
100 static std::string GetCompleteIndication(const std::string&);
David Andersonb5f44b32019-02-20 18:04:37 -0800101
David Anderson5cc440c2019-04-16 14:34:27 -0700102 enum class AccessLevel { System, SystemOrShell };
David Andersonb5f44b32019-02-20 18:04:37 -0800103 binder::Status CheckUid(AccessLevel level = AccessLevel::System);
Yi-Yo Chiang1ecc3af2021-12-02 18:30:24 +0800104
105 // Mark install completion, and reset boot attempt counter.
106 // Next boot will try to boot into DSU.
107 bool ResetBootAttemptCounter();
108
Howard Chen4663de62019-11-05 20:46:20 +0800109 bool SetBootMode(bool one_shot);
David Anderson6a5b8a72019-01-16 16:24:48 -0800110
David Anderson4c756732019-07-12 14:18:37 -0700111 static android::wp<GsiService> sInstance;
David Anderson6f373b72019-06-05 15:04:00 -0700112
Howard Chen4663de62019-11-05 20:46:20 +0800113 std::string install_dir_ = {};
Howard Chen4663de62019-11-05 20:46:20 +0800114 std::unique_ptr<PartitionInstaller> installer_;
Howard Chen67a27092020-02-15 17:32:02 +0800115 std::mutex lock_;
116 std::mutex& lock() { return lock_; }
David Andersonc86531e2019-02-25 18:50:35 -0800117 // These are initialized or set in StartInstall().
Howard Chenecbc0192019-02-25 18:51:26 +0800118 std::atomic<bool> should_abort_ = false;
David Andersonc053b3b2019-01-08 18:22:07 -0800119
David Anderson6a5b8a72019-01-16 16:24:48 -0800120 // Progress bar state.
121 std::mutex progress_lock_;
122 GsiProgress progress_;
David Andersonc053b3b2019-01-08 18:22:07 -0800123};
124
125} // namespace gsi
126} // namespace android