blob: a853e2bc81b76a5170dcd16e950a3daf5a30e290 [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>
David Anderson4c756732019-07-12 14:18:37 -070027#include <android/gsi/BnGsid.h>
David Andersonc053b3b2019-01-08 18:22:07 -080028#include <binder/BinderService.h>
David Anderson9ca77282019-07-15 23:56:13 +000029#include <libfiemap/split_fiemap_writer.h>
David Andersonc053b3b2019-01-08 18:22:07 -080030#include <liblp/builder.h>
31#include "libgsi/libgsi.h"
32
Howard Chen4663de62019-11-05 20:46:20 +080033#include "partition_installer.h"
David Andersonb2988ab2019-04-16 17:14:09 -070034
David Andersonc053b3b2019-01-08 18:22:07 -080035namespace android {
36namespace gsi {
37
David Anderson4c756732019-07-12 14:18:37 -070038class Gsid : public BinderService<Gsid>, public BnGsid {
David Andersonc053b3b2019-01-08 18:22:07 -080039 public:
40 static void Register();
David Anderson4c756732019-07-12 14:18:37 -070041 static char const* getServiceName() { return kGsiServiceName; }
David Andersonc053b3b2019-01-08 18:22:07 -080042
David Anderson4c756732019-07-12 14:18:37 -070043 binder::Status getClient(android::sp<IGsiService>* _aidl_return) override;
44
45 private:
46 friend class GsiService;
David Anderson551ae3a2019-08-01 12:53:06 -070047 friend class ImageService;
David Anderson4c756732019-07-12 14:18:37 -070048
49 std::mutex& lock() { return lock_; }
50
51 std::mutex lock_;
52};
53
54class GsiService : public BinderService<GsiService>, public BnGsiService {
55 public:
David Andersonc053b3b2019-01-08 18:22:07 -080056 ~GsiService() override;
57
David Anderson4c756732019-07-12 14:18:37 -070058 static android::sp<IGsiService> Get(Gsid* parent);
59
Howard Chen4663de62019-11-05 20:46:20 +080060 binder::Status openInstall(const std::string& install_dir, int* _aidl_return) override;
61 binder::Status closeInstall(int32_t* _aidl_return) override;
62 binder::Status createPartition(const ::std::string& name, int64_t size, bool readOnly,
63 int32_t* _aidl_return) override;
David Anderson1d94d262019-01-11 20:39:51 -080064 binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream,
65 int64_t bytes, bool* _aidl_return) override;
David Anderson6a5b8a72019-01-16 16:24:48 -080066 binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override;
Howard Chen5676d962019-08-05 16:21:00 +080067 binder::Status setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, int64_t size,
68 bool* _aidl_return) override;
69 binder::Status commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080070 binder::Status cancelGsiInstall(bool* _aidl_return) override;
Howard Chenee5c2b12019-11-08 11:57:47 +080071 binder::Status enableGsi(bool oneShot, const std::string& dsuSlot, int* _aidl_return) override;
Howard Chen670b3062019-02-26 18:14:47 +080072 binder::Status isGsiEnabled(bool* _aidl_return) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080073 binder::Status removeGsi(bool* _aidl_return) override;
74 binder::Status disableGsi(bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080075 binder::Status isGsiInstalled(bool* _aidl_return) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080076 binder::Status isGsiRunning(bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080077 binder::Status isGsiInstallInProgress(bool* _aidl_return) override;
David Anderson5f805912019-03-07 12:41:15 -080078 binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override;
Howard Chenee5c2b12019-11-08 11:57:47 +080079 binder::Status getActiveDsuSlot(std::string* _aidl_return) override;
80 binder::Status getInstalledDsuSlots(std::vector<std::string>* _aidl_return) override;
Howard Chen4663de62019-11-05 20:46:20 +080081 binder::Status zeroPartition(const std::string& name, int* _aidl_return) override;
David Anderson551ae3a2019-08-01 12:53:06 -070082 binder::Status openImageService(const std::string& prefix,
83 android::sp<IImageService>* _aidl_return) override;
David Andersond614eca2019-09-09 17:57:06 -070084 binder::Status dumpDeviceMapperDevices(std::string* _aidl_return) override;
Yo Chiang53bed1c2020-01-01 16:25:19 +080085 binder::Status getAvbPublicKey(AvbPublicKey* dst, int32_t* _aidl_return) override;
David Andersonc053b3b2019-01-08 18:22:07 -080086
David Andersonb2988ab2019-04-16 17:14:09 -070087 // This is in GsiService, rather than GsiInstaller, since we need to access
88 // it outside of the main lock which protects the unique_ptr.
89 void StartAsyncOperation(const std::string& step, int64_t total_bytes);
90 void UpdateProgress(int status, int64_t bytes_processed);
91
David Andersonb2988ab2019-04-16 17:14:09 -070092 // Helper methods for GsiInstaller.
Howard Chen4663de62019-11-05 20:46:20 +080093 static bool RemoveGsiFiles(const std::string& install_dir);
David Andersonb2988ab2019-04-16 17:14:09 -070094 bool should_abort() const { return should_abort_; }
David Anderson4c756732019-07-12 14:18:37 -070095 Gsid* parent() const { return parent_.get(); }
David Andersonb2988ab2019-04-16 17:14:09 -070096
David Andersonb3aff182019-01-11 14:37:51 -080097 static void RunStartupTasks();
David Anderson64b53fb2019-07-01 19:05:35 -070098 static std::string GetInstalledImageDir();
Howard Chenee5c2b12019-11-08 11:57:47 +080099 std::string GetActiveDsuSlot();
Howard Chen3f6d5a62019-08-22 15:26:33 +0800100 std::string GetActiveInstalledImageDir();
David Andersonb3aff182019-01-11 14:37:51 -0800101
Howard Chenee5c2b12019-11-08 11:57:47 +0800102 static std::vector<std::string> GetInstalledDsuSlots();
103
David Andersonc053b3b2019-01-08 18:22:07 -0800104 private:
David Anderson4c756732019-07-12 14:18:37 -0700105 GsiService(Gsid* parent);
Howard Chen4663de62019-11-05 20:46:20 +0800106 static int ValidateInstallParams(std::string& install_dir);
David Andersona141ba82019-01-14 19:09:27 -0800107 bool DisableGsiInstall();
David Andersonb2988ab2019-04-16 17:14:09 -0700108 int ReenableGsi(bool one_shot);
Howard Chen3f6d5a62019-08-22 15:26:33 +0800109 static void CleanCorruptedInstallation();
Howard Chen4663de62019-11-05 20:46:20 +0800110 static int SaveInstallation(const std::string&);
111 static bool IsInstallationComplete(const std::string&);
112 static std::string GetCompleteIndication(const std::string&);
David Andersonb5f44b32019-02-20 18:04:37 -0800113
David Anderson5cc440c2019-04-16 14:34:27 -0700114 enum class AccessLevel { System, SystemOrShell };
David Andersonb5f44b32019-02-20 18:04:37 -0800115 binder::Status CheckUid(AccessLevel level = AccessLevel::System);
Howard Chen4663de62019-11-05 20:46:20 +0800116 bool CreateInstallStatusFile();
117 bool SetBootMode(bool one_shot);
David Anderson6a5b8a72019-01-16 16:24:48 -0800118
David Anderson4c756732019-07-12 14:18:37 -0700119 static android::wp<GsiService> sInstance;
David Anderson6f373b72019-06-05 15:04:00 -0700120
Howard Chen4663de62019-11-05 20:46:20 +0800121 std::string install_dir_ = {};
David Anderson4c756732019-07-12 14:18:37 -0700122 android::sp<Gsid> parent_;
Howard Chen4663de62019-11-05 20:46:20 +0800123 std::unique_ptr<PartitionInstaller> installer_;
David Andersonc86531e2019-02-25 18:50:35 -0800124
125 // These are initialized or set in StartInstall().
Howard Chenecbc0192019-02-25 18:51:26 +0800126 std::atomic<bool> should_abort_ = false;
David Andersonc053b3b2019-01-08 18:22:07 -0800127
David Anderson6a5b8a72019-01-16 16:24:48 -0800128 // Progress bar state.
129 std::mutex progress_lock_;
130 GsiProgress progress_;
David Andersonc053b3b2019-01-08 18:22:07 -0800131};
132
133} // namespace gsi
134} // namespace android