gsi_service.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  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. #include <map>
  18. #include <memory>
  19. #include <mutex>
  20. #include <sstream>
  21. #include <string>
  22. #include <vector>
  23. #include <android-base/unique_fd.h>
  24. #include <android/gsi/BnGsiService.h>
  25. #include <binder/BinderService.h>
  26. #include <libfiemap_writer/split_fiemap_writer.h>
  27. #include <liblp/builder.h>
  28. #include "libgsi/libgsi.h"
  29. namespace android {
  30. namespace gsi {
  31. class GsiService : public BinderService<GsiService>, public BnGsiService {
  32. public:
  33. static void Register();
  34. GsiService();
  35. ~GsiService() override;
  36. binder::Status startGsiInstall(int64_t gsiSize, int64_t userdataSize, bool wipeUserdata,
  37. int* _aidl_return) override;
  38. binder::Status beginGsiInstall(const GsiInstallParams& params, int* _aidl_return) override;
  39. binder::Status commitGsiChunkFromStream(const ::android::os::ParcelFileDescriptor& stream,
  40. int64_t bytes, bool* _aidl_return) override;
  41. binder::Status getInstallProgress(::android::gsi::GsiProgress* _aidl_return) override;
  42. binder::Status commitGsiChunkFromMemory(const ::std::vector<uint8_t>& bytes,
  43. bool* _aidl_return) override;
  44. binder::Status cancelGsiInstall(bool* _aidl_return) override;
  45. binder::Status setGsiBootable(bool oneShot, int* _aidl_return) override;
  46. binder::Status isGsiEnabled(bool* _aidl_return) override;
  47. binder::Status removeGsiInstall(bool* _aidl_return) override;
  48. binder::Status disableGsiInstall(bool* _aidl_return) override;
  49. binder::Status isGsiRunning(bool* _aidl_return) override;
  50. binder::Status isGsiInstalled(bool* _aidl_return) override;
  51. binder::Status isGsiInstallInProgress(bool* _aidl_return) override;
  52. binder::Status getUserdataImageSize(int64_t* _aidl_return) override;
  53. binder::Status getGsiBootStatus(int* _aidl_return) override;
  54. binder::Status getInstalledGsiImageDir(std::string* _aidl_return) override;
  55. binder::Status wipeGsiUserdata(int* _aidl_return) override;
  56. static char const* getServiceName() { return kGsiServiceName; }
  57. static void RunStartupTasks();
  58. // This helper class will redirect writes to either a SplitFiemap or
  59. // device-mapper.
  60. class WriteHelper {
  61. public:
  62. virtual ~WriteHelper() {};
  63. virtual bool Write(const void* data, uint64_t bytes) = 0;
  64. virtual bool Flush() = 0;
  65. virtual uint64_t Size() = 0;
  66. WriteHelper() = default;
  67. WriteHelper(const WriteHelper&) = delete;
  68. WriteHelper& operator=(const WriteHelper&) = delete;
  69. WriteHelper& operator=(WriteHelper&&) = delete;
  70. WriteHelper(WriteHelper&&) = delete;
  71. };
  72. private:
  73. using LpMetadata = android::fs_mgr::LpMetadata;
  74. using MetadataBuilder = android::fs_mgr::MetadataBuilder;
  75. using SplitFiemap = android::fiemap_writer::SplitFiemap;
  76. struct Image {
  77. std::unique_ptr<SplitFiemap> writer;
  78. uint64_t actual_size;
  79. };
  80. int ValidateInstallParams(GsiInstallParams* params);
  81. int StartInstall(const GsiInstallParams& params);
  82. int PerformSanityChecks();
  83. int PreallocateFiles();
  84. int PreallocateUserdata();
  85. int PreallocateSystem();
  86. int DetermineReadWriteMethod();
  87. bool FormatUserdata();
  88. bool CommitGsiChunk(int stream_fd, int64_t bytes);
  89. bool CommitGsiChunk(const void* data, size_t bytes);
  90. int SetGsiBootable(bool one_shot);
  91. int ReenableGsi(bool one_shot);
  92. int WipeUserdata();
  93. bool DisableGsiInstall();
  94. bool AddPartitionFiemap(android::fs_mgr::MetadataBuilder* builder,
  95. android::fs_mgr::Partition* partition, const Image& image,
  96. const std::string& block_device);
  97. std::unique_ptr<LpMetadata> CreateMetadata();
  98. std::unique_ptr<SplitFiemap> CreateFiemapWriter(const std::string& path, uint64_t size,
  99. int* error);
  100. bool CreateInstallStatusFile();
  101. bool CreateMetadataFile();
  102. bool SetBootMode(bool one_shot);
  103. void PostInstallCleanup();
  104. void StartAsyncOperation(const std::string& step, int64_t total_bytes);
  105. void UpdateProgress(int status, int64_t bytes_processed);
  106. int GetExistingImage(const LpMetadata& metadata, const std::string& name, Image* image);
  107. std::unique_ptr<WriteHelper> OpenPartition(const std::string& name);
  108. enum class AccessLevel {
  109. System,
  110. SystemOrShell
  111. };
  112. binder::Status CheckUid(AccessLevel level = AccessLevel::System);
  113. static bool RemoveGsiFiles(const std::string& install_dir, bool wipeUserdata);
  114. static std::string GetImagePath(const std::string& image_dir, const std::string& name);
  115. static std::string GetInstalledImagePath(const std::string& name);
  116. static std::string GetInstalledImageDir();
  117. std::mutex main_lock_;
  118. // Set before installation starts, to determine whether or not to delete
  119. // the userdata image if installation fails.
  120. bool wipe_userdata_on_failure_;
  121. // These are initialized or set in StartInstall().
  122. bool installing_ = false;
  123. std::atomic<bool> should_abort_ = false;
  124. std::string install_dir_;
  125. std::string userdata_gsi_path_;
  126. std::string system_gsi_path_;
  127. uint64_t userdata_block_size_;
  128. uint64_t system_block_size_;
  129. uint64_t gsi_size_;
  130. uint64_t userdata_size_;
  131. bool can_use_devicemapper_;
  132. bool wipe_userdata_;
  133. // Remaining data we're waiting to receive for the GSI image.
  134. uint64_t gsi_bytes_written_;
  135. // Progress bar state.
  136. std::mutex progress_lock_;
  137. GsiProgress progress_;
  138. std::unique_ptr<WriteHelper> system_writer_;
  139. // This is used to track which GSI partitions have been created.
  140. std::map<std::string, Image> partitions_;
  141. std::unique_ptr<LpMetadata> metadata_;
  142. };
  143. } // namespace gsi
  144. } // namespace android