blob: 56ce8c610368001927a2854cdbeaf5eff7aa9bdb [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
David Andersonb2988ab2019-04-16 17:14:09 -070033#include "gsi_installer.h"
34
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
David Andersoneb30ac22019-03-12 15:24:53 -070060 binder::Status beginGsiInstall(const GsiInstallParams& params, int* _aidl_return) override;
David Anderson1d94d262019-01-11 20:39:51 -080061 binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream,
62 int64_t bytes, bool* _aidl_return) override;
David Anderson6a5b8a72019-01-16 16:24:48 -080063 binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override;
Howard Chen5676d962019-08-05 16:21:00 +080064 binder::Status setGsiAshmem(const ::android::os::ParcelFileDescriptor& ashmem, int64_t size,
65 bool* _aidl_return) override;
66 binder::Status commitGsiChunkFromAshmem(int64_t bytes, bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080067 binder::Status cancelGsiInstall(bool* _aidl_return) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080068 binder::Status enableGsi(bool oneShot, int* _aidl_return) override;
Howard Chen670b3062019-02-26 18:14:47 +080069 binder::Status isGsiEnabled(bool* _aidl_return) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080070 binder::Status removeGsi(bool* _aidl_return) override;
71 binder::Status disableGsi(bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080072 binder::Status isGsiInstalled(bool* _aidl_return) override;
Howard Chen25b18cc2019-08-02 11:21:58 +080073 binder::Status isGsiRunning(bool* _aidl_return) override;
David Andersona141ba82019-01-14 19:09:27 -080074 binder::Status isGsiInstallInProgress(bool* _aidl_return) override;
David Anderson5f805912019-03-07 12:41:15 -080075 binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override;
David Anderson8bdf6252019-06-11 16:43:24 -070076 binder::Status wipeGsiUserdata(int* _aidl_return) override;
David Anderson551ae3a2019-08-01 12:53:06 -070077 binder::Status openImageService(const std::string& prefix,
78 android::sp<IImageService>* _aidl_return) override;
David Andersonc053b3b2019-01-08 18:22:07 -080079
David Andersonb2988ab2019-04-16 17:14:09 -070080 // This is in GsiService, rather than GsiInstaller, since we need to access
81 // it outside of the main lock which protects the unique_ptr.
82 void StartAsyncOperation(const std::string& step, int64_t total_bytes);
83 void UpdateProgress(int status, int64_t bytes_processed);
84
David Andersonb2988ab2019-04-16 17:14:09 -070085 // Helper methods for GsiInstaller.
David Andersonb2988ab2019-04-16 17:14:09 -070086 static bool RemoveGsiFiles(const std::string& install_dir, bool wipeUserdata);
87 bool should_abort() const { return should_abort_; }
David Anderson4c756732019-07-12 14:18:37 -070088 Gsid* parent() const { return parent_.get(); }
David Andersonb2988ab2019-04-16 17:14:09 -070089
David Andersonb3aff182019-01-11 14:37:51 -080090 static void RunStartupTasks();
David Anderson64b53fb2019-07-01 19:05:35 -070091 static std::string GetInstalledImageDir();
Howard Chen3f6d5a62019-08-22 15:26:33 +080092 std::string GetActiveInstalledImageDir();
David Andersonb3aff182019-01-11 14:37:51 -080093
David Andersonc053b3b2019-01-08 18:22:07 -080094 private:
David Anderson4c756732019-07-12 14:18:37 -070095 GsiService(Gsid* parent);
David Andersoneb30ac22019-03-12 15:24:53 -070096 int ValidateInstallParams(GsiInstallParams* params);
David Andersona141ba82019-01-14 19:09:27 -080097 bool DisableGsiInstall();
David Andersonb2988ab2019-04-16 17:14:09 -070098 int ReenableGsi(bool one_shot);
Howard Chen3f6d5a62019-08-22 15:26:33 +080099 static void CleanCorruptedInstallation();
David Andersonb5f44b32019-02-20 18:04:37 -0800100
David Anderson5cc440c2019-04-16 14:34:27 -0700101 enum class AccessLevel { System, SystemOrShell };
David Andersonb5f44b32019-02-20 18:04:37 -0800102 binder::Status CheckUid(AccessLevel level = AccessLevel::System);
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
David Anderson4c756732019-07-12 14:18:37 -0700106 android::sp<Gsid> parent_;
David Andersonb2988ab2019-04-16 17:14:09 -0700107 std::unique_ptr<GsiInstaller> installer_;
David Andersonc86531e2019-02-25 18:50:35 -0800108
109 // These are initialized or set in StartInstall().
Howard Chenecbc0192019-02-25 18:51:26 +0800110 std::atomic<bool> should_abort_ = false;
David Andersonc053b3b2019-01-08 18:22:07 -0800111
David Anderson6a5b8a72019-01-16 16:24:48 -0800112 // Progress bar state.
113 std::mutex progress_lock_;
114 GsiProgress progress_;
David Andersonc053b3b2019-01-08 18:22:07 -0800115};
116
117} // namespace gsi
118} // namespace android