blob: 229db36735d47df64d8e23f1a8d6e8792a84e560 [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 Chenf2a0a822020-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;
David Anderson1d94d262019-01-11 20:39:51 -080045 binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream,
46 int64_t bytes, bool* _aidl_return) override;
David Anderson6a5b8a72019-01-16 16:24:48 -080047 binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override;
Howard Chen5676d962019-08-05 16:21:00 +080048 binder::Status setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, int64_t size,
49 bool* _aidl_return) override;
50 binder::Status commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080051 binder::Status cancelGsiInstall(bool* _aidl_return) override;
Howard Chenee5c2b12019-11-08 11:57:47 +080052 binder::Status enableGsi(bool oneShot, const std::string& dsuSlot, int* _aidl_return) override;
Howard Chen0e188062020-02-26 12:48:41 +080053 binder::Status enableGsiAsync(bool oneShot, const ::std::string& dsuSlot,
54 const sp<IGsiServiceCallback>& resultCallback) override;
Howard Chen670b3062019-02-26 18:14:47 +080055 binder::Status isGsiEnabled(bool* _aidl_return) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080056 binder::Status removeGsi(bool* _aidl_return) override;
Howard Chen0e188062020-02-26 12:48:41 +080057 binder::Status removeGsiAsync(const sp<IGsiServiceCallback>& resultCallback) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080058 binder::Status disableGsi(bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080059 binder::Status isGsiInstalled(bool* _aidl_return) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080060 binder::Status isGsiRunning(bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080061 binder::Status isGsiInstallInProgress(bool* _aidl_return) override;
David Anderson5f805912019-03-07 12:41:15 -080062 binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override;
Howard Chenee5c2b12019-11-08 11:57:47 +080063 binder::Status getActiveDsuSlot(std::string* _aidl_return) override;
64 binder::Status getInstalledDsuSlots(std::vector<std::string>* _aidl_return) override;
Howard Chen4663de62019-11-05 20:46:20 +080065 binder::Status zeroPartition(const std::string& name, int* _aidl_return) override;
David Anderson551ae3a2019-08-01 12:53:06 -070066 binder::Status openImageService(const std::string& prefix,
67 android::sp<IImageService>* _aidl_return) override;
David Andersond614eca2019-09-09 17:57:06 -070068 binder::Status dumpDeviceMapperDevices(std::string* _aidl_return) override;
Yo Chiang53bed1c2020-01-01 16:25:19 +080069 binder::Status getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) override;
David Andersonc053b3b2019-01-08 18:22:07 -080070
David Andersonb2988ab2019-04-16 17:14:09 -070071 // This is in GsiService, rather than GsiInstaller, since we need to access
72 // it outside of the main lock which protects the unique_ptr.
73 void StartAsyncOperation(const std::string& step, int64_t total_bytes);
74 void UpdateProgress(int status, int64_t bytes_processed);
75
David Andersonb2988ab2019-04-16 17:14:09 -070076 // Helper methods for GsiInstaller.
Howard Chen4663de62019-11-05 20:46:20 +080077 static bool RemoveGsiFiles(const std::string& install_dir);
David Andersonb2988ab2019-04-16 17:14:09 -070078 bool should_abort() const { return should_abort_; }
79
David Andersonb3aff182019-01-11 14:37:51 -080080 static void RunStartupTasks();
David Anderson64b53fb2019-07-01 19:05:35 -070081 static std::string GetInstalledImageDir();
Howard Chenee5c2b12019-11-08 11:57:47 +080082 std::string GetActiveDsuSlot();
Howard Chen3f6d5a62019-08-22 15:26:33 +080083 std::string GetActiveInstalledImageDir();
David Andersonb3aff182019-01-11 14:37:51 -080084
Howard Chenee5c2b12019-11-08 11:57:47 +080085 static std::vector<std::string> GetInstalledDsuSlots();
86
David Andersonc053b3b2019-01-08 18:22:07 -080087 private:
Howard Chenf2a0a822020-02-15 17:32:02 +080088 friend class ImageService;
89
90 GsiService();
Howard Chen4663de62019-11-05 20:46:20 +080091 static int ValidateInstallParams(std::string& install_dir);
David Andersona141ba82019-01-14 19:09:27 -080092 bool DisableGsiInstall();
David Andersonb2988ab2019-04-16 17:14:09 -070093 int ReenableGsi(bool one_shot);
Howard Chen3f6d5a62019-08-22 15:26:33 +080094 static void CleanCorruptedInstallation();
Howard Chen4663de62019-11-05 20:46:20 +080095 static int SaveInstallation(const std::string&);
96 static bool IsInstallationComplete(const std::string&);
97 static std::string GetCompleteIndication(const std::string&);
David Andersonb5f44b32019-02-20 18:04:37 -080098
David Anderson5cc440c2019-04-16 14:34:27 -070099 enum class AccessLevel { System, SystemOrShell };
David Andersonb5f44b32019-02-20 18:04:37 -0800100 binder::Status CheckUid(AccessLevel level = AccessLevel::System);
Howard Chen4663de62019-11-05 20:46:20 +0800101 bool CreateInstallStatusFile();
102 bool SetBootMode(bool one_shot);
David Anderson6a5b8a72019-01-16 16:24:48 -0800103
David Anderson4c756732019-07-12 14:18:37 -0700104 static android::wp<GsiService> sInstance;
David Anderson6f373b72019-06-05 15:04:00 -0700105
Howard Chen4663de62019-11-05 20:46:20 +0800106 std::string install_dir_ = {};
Howard Chen4663de62019-11-05 20:46:20 +0800107 std::unique_ptr<PartitionInstaller> installer_;
Howard Chenf2a0a822020-02-15 17:32:02 +0800108 std::mutex lock_;
109 std::mutex& lock() { return lock_; }
David Andersonc86531e2019-02-25 18:50:35 -0800110 // These are initialized or set in StartInstall().
Howard Chenecbc0192019-02-25 18:51:26 +0800111 std::atomic<bool> should_abort_ = false;
David Andersonc053b3b2019-01-08 18:22:07 -0800112
David Anderson6a5b8a72019-01-16 16:24:48 -0800113 // Progress bar state.
114 std::mutex progress_lock_;
115 GsiProgress progress_;
David Andersonc053b3b2019-01-08 18:22:07 -0800116};
117
118} // namespace gsi
119} // namespace android