123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- #pragma once
- #include <map>
- #include <memory>
- #include <mutex>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <android-base/unique_fd.h>
- #include <android/gsi/BnGsiService.h>
- #include <binder/BinderService.h>
- #include <libfiemap_writer/split_fiemap_writer.h>
- #include <liblp/builder.h>
- #include "libgsi/libgsi.h"
- namespace android {
- namespace gsi {
- class GsiService : public BinderService<GsiService>, public BnGsiService {
- public:
- static void Register();
- GsiService();
- ~GsiService() override;
- binder::Status startGsiInstall(int64_t gsiSize, int64_t userdataSize, bool wipeUserdata,
- int* _aidl_return) override;
- binder::Status beginGsiInstall(const GsiInstallParams& params, int* _aidl_return) override;
- binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream,
- int64_t bytes, bool* _aidl_return) override;
- binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override;
- binder::Status commitGsiChunkFromMemory(const ::std::vector<uint8_t>& bytes,
- bool* _aidl_return) override;
- binder::Status cancelGsiInstall(bool* _aidl_return) override;
- binder::Status setGsiBootable(bool oneShot, int* _aidl_return) override;
- binder::Status isGsiEnabled(bool* _aidl_return) override;
- binder::Status removeGsiInstall(bool* _aidl_return) override;
- binder::Status disableGsiInstall(bool* _aidl_return) override;
- binder::Status isGsiRunning(bool* _aidl_return) override;
- binder::Status isGsiInstalled(bool* _aidl_return) override;
- binder::Status isGsiInstallInProgress(bool* _aidl_return) override;
- binder::Status getUserdataImageSize(int64_t* _aidl_return) override;
- binder::Status getGsiBootStatus(int* _aidl_return) override;
- binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override;
- binder::Status wipeGsiUserdata(int* _aidl_return) override;
- static char const* getServiceName() { return kGsiServiceName; }
- static void RunStartupTasks();
-
-
- class WriteHelper {
- public:
- virtual ~WriteHelper() {};
- virtual bool Write(const void* data, uint64_t bytes) = 0;
- virtual bool Flush() = 0;
- virtual uint64_t Size() = 0;
- WriteHelper() = default;
- WriteHelper(const WriteHelper&) = delete;
- WriteHelper& operator=(const WriteHelper&) = delete;
- WriteHelper& operator=(WriteHelper&&) = delete;
- WriteHelper(WriteHelper&&) = delete;
- };
- private:
- using LpMetadata = android::fs_mgr::LpMetadata;
- using MetadataBuilder = android::fs_mgr::MetadataBuilder;
- using SplitFiemap = android::fiemap_writer::SplitFiemap;
- struct Image {
- std::unique_ptr<SplitFiemap> writer;
- uint64_t actual_size;
- };
- int ValidateInstallParams(GsiInstallParams* params);
- int StartInstall(const GsiInstallParams& params);
- int PerformSanityChecks();
- int PreallocateFiles();
- int PreallocateUserdata();
- int PreallocateSystem();
- int DetermineReadWriteMethod();
- bool FormatUserdata();
- bool CommitGsiChunk(int stream_fd, int64_t bytes);
- bool CommitGsiChunk(const void* data, size_t bytes);
- int SetGsiBootable(bool one_shot);
- int ReenableGsi(bool one_shot);
- int WipeUserdata();
- bool DisableGsiInstall();
- bool AddPartitionFiemap(android::fs_mgr::MetadataBuilder* builder,
- android::fs_mgr::Partition* partition, const Image& image,
- const std::string& block_device);
- std::unique_ptr<LpMetadata> CreateMetadata();
- std::unique_ptr<SplitFiemap> CreateFiemapWriter(const std::string& path, uint64_t size,
- int* error);
- bool CreateInstallStatusFile();
- bool CreateMetadataFile();
- bool SetBootMode(bool one_shot);
- void PostInstallCleanup();
- void StartAsyncOperation(const std::string& step, int64_t total_bytes);
- void UpdateProgress(int status, int64_t bytes_processed);
- int GetExistingImage(const LpMetadata& metadata, const std::string& name, Image* image);
- std::unique_ptr<WriteHelper> OpenPartition(const std::string& name);
- enum class AccessLevel {
- System,
- SystemOrShell
- };
- binder::Status CheckUid(AccessLevel level = AccessLevel::System);
- static bool RemoveGsiFiles(const std::string& install_dir, bool wipeUserdata);
- static std::string GetImagePath(const std::string& image_dir, const std::string& name);
- static std::string GetInstalledImagePath(const std::string& name);
- static std::string GetInstalledImageDir();
- std::mutex main_lock_;
-
-
- bool wipe_userdata_on_failure_;
-
- bool installing_ = false;
- std::atomic<bool> should_abort_ = false;
- std::string install_dir_;
- std::string userdata_gsi_path_;
- std::string system_gsi_path_;
- uint64_t userdata_block_size_;
- uint64_t system_block_size_;
- uint64_t gsi_size_;
- uint64_t userdata_size_;
- bool can_use_devicemapper_;
- bool wipe_userdata_;
-
- uint64_t gsi_bytes_written_;
-
- std::mutex progress_lock_;
- GsiProgress progress_;
- std::unique_ptr<WriteHelper> system_writer_;
-
- std::map<std::string, Image> partitions_;
- std::unique_ptr<LpMetadata> metadata_;
- };
- }
- }
|