dynamic_partition_control_android.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // Copyright (C) 2018 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. #include "update_engine/dynamic_partition_control_android.h"
  17. #include <memory>
  18. #include <set>
  19. #include <string>
  20. #include <android-base/properties.h>
  21. #include <android-base/strings.h>
  22. #include <base/files/file_util.h>
  23. #include <base/logging.h>
  24. #include <bootloader_message/bootloader_message.h>
  25. #include <fs_mgr_dm_linear.h>
  26. #include "update_engine/common/boot_control_interface.h"
  27. #include "update_engine/common/utils.h"
  28. using android::base::GetBoolProperty;
  29. using android::base::Join;
  30. using android::dm::DeviceMapper;
  31. using android::dm::DmDeviceState;
  32. using android::fs_mgr::CreateLogicalPartition;
  33. using android::fs_mgr::DestroyLogicalPartition;
  34. using android::fs_mgr::MetadataBuilder;
  35. using android::fs_mgr::PartitionOpener;
  36. namespace chromeos_update_engine {
  37. constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
  38. constexpr char kRetrfoitDynamicPartitions[] =
  39. "ro.boot.dynamic_partitions_retrofit";
  40. constexpr uint64_t kMapTimeoutMillis = 1000;
  41. DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
  42. CleanupInternal(false /* wait */);
  43. }
  44. bool DynamicPartitionControlAndroid::IsDynamicPartitionsEnabled() {
  45. return GetBoolProperty(kUseDynamicPartitions, false);
  46. }
  47. bool DynamicPartitionControlAndroid::IsDynamicPartitionsRetrofit() {
  48. return GetBoolProperty(kRetrfoitDynamicPartitions, false);
  49. }
  50. bool DynamicPartitionControlAndroid::MapPartitionInternal(
  51. const std::string& super_device,
  52. const std::string& target_partition_name,
  53. uint32_t slot,
  54. bool force_writable,
  55. std::string* path) {
  56. if (!CreateLogicalPartition(super_device.c_str(),
  57. slot,
  58. target_partition_name,
  59. force_writable,
  60. std::chrono::milliseconds(kMapTimeoutMillis),
  61. path)) {
  62. LOG(ERROR) << "Cannot map " << target_partition_name << " in "
  63. << super_device << " on device mapper.";
  64. return false;
  65. }
  66. LOG(INFO) << "Succesfully mapped " << target_partition_name
  67. << " to device mapper (force_writable = " << force_writable
  68. << "); device path at " << *path;
  69. mapped_devices_.insert(target_partition_name);
  70. return true;
  71. }
  72. bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
  73. const std::string& super_device,
  74. const std::string& target_partition_name,
  75. uint32_t slot,
  76. bool force_writable,
  77. std::string* path) {
  78. DmDeviceState state = GetState(target_partition_name);
  79. if (state == DmDeviceState::ACTIVE) {
  80. if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
  81. if (GetDmDevicePathByName(target_partition_name, path)) {
  82. LOG(INFO) << target_partition_name
  83. << " is mapped on device mapper: " << *path;
  84. return true;
  85. }
  86. LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
  87. return false;
  88. }
  89. // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
  90. // the device might be mapped incorrectly before. Attempt to unmap it.
  91. // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
  92. // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
  93. // should directly call GetDmDevicePathByName.
  94. if (!UnmapPartitionOnDeviceMapper(target_partition_name, true /* wait */)) {
  95. LOG(ERROR) << target_partition_name
  96. << " is mapped before the update, and it cannot be unmapped.";
  97. return false;
  98. }
  99. state = GetState(target_partition_name);
  100. if (state != DmDeviceState::INVALID) {
  101. LOG(ERROR) << target_partition_name << " is unmapped but state is "
  102. << static_cast<std::underlying_type_t<DmDeviceState>>(state);
  103. return false;
  104. }
  105. }
  106. if (state == DmDeviceState::INVALID) {
  107. return MapPartitionInternal(
  108. super_device, target_partition_name, slot, force_writable, path);
  109. }
  110. LOG(ERROR) << target_partition_name
  111. << " is mapped on device mapper but state is unknown: "
  112. << static_cast<std::underlying_type_t<DmDeviceState>>(state);
  113. return false;
  114. }
  115. bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
  116. const std::string& target_partition_name, bool wait) {
  117. if (DeviceMapper::Instance().GetState(target_partition_name) !=
  118. DmDeviceState::INVALID) {
  119. if (!DestroyLogicalPartition(
  120. target_partition_name,
  121. std::chrono::milliseconds(wait ? kMapTimeoutMillis : 0))) {
  122. LOG(ERROR) << "Cannot unmap " << target_partition_name
  123. << " from device mapper.";
  124. return false;
  125. }
  126. LOG(INFO) << "Successfully unmapped " << target_partition_name
  127. << " from device mapper.";
  128. }
  129. mapped_devices_.erase(target_partition_name);
  130. return true;
  131. }
  132. void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
  133. // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
  134. // a copy is needed for the loop.
  135. std::set<std::string> mapped = mapped_devices_;
  136. LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
  137. for (const auto& partition_name : mapped) {
  138. ignore_result(UnmapPartitionOnDeviceMapper(partition_name, wait));
  139. }
  140. }
  141. void DynamicPartitionControlAndroid::Cleanup() {
  142. CleanupInternal(true /* wait */);
  143. }
  144. bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
  145. return base::PathExists(base::FilePath(path));
  146. }
  147. android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
  148. const std::string& name) {
  149. return DeviceMapper::Instance().GetState(name);
  150. }
  151. bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
  152. const std::string& name, std::string* path) {
  153. return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
  154. }
  155. std::unique_ptr<MetadataBuilder>
  156. DynamicPartitionControlAndroid::LoadMetadataBuilder(
  157. const std::string& super_device,
  158. uint32_t source_slot,
  159. uint32_t target_slot) {
  160. std::unique_ptr<MetadataBuilder> builder;
  161. if (target_slot != BootControlInterface::kInvalidSlot &&
  162. IsDynamicPartitionsRetrofit()) {
  163. builder = MetadataBuilder::NewForUpdate(
  164. PartitionOpener(), super_device, source_slot, target_slot);
  165. } else {
  166. builder =
  167. MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
  168. }
  169. if (builder == nullptr) {
  170. LOG(WARNING) << "No metadata slot "
  171. << BootControlInterface::SlotName(source_slot) << " in "
  172. << super_device;
  173. return nullptr;
  174. }
  175. LOG(INFO) << "Loaded metadata from slot "
  176. << BootControlInterface::SlotName(source_slot) << " in "
  177. << super_device;
  178. return builder;
  179. }
  180. bool DynamicPartitionControlAndroid::StoreMetadata(
  181. const std::string& super_device,
  182. MetadataBuilder* builder,
  183. uint32_t target_slot) {
  184. auto metadata = builder->Export();
  185. if (metadata == nullptr) {
  186. LOG(ERROR) << "Cannot export metadata to slot "
  187. << BootControlInterface::SlotName(target_slot) << " in "
  188. << super_device;
  189. return false;
  190. }
  191. if (IsDynamicPartitionsRetrofit()) {
  192. if (!FlashPartitionTable(super_device, *metadata)) {
  193. LOG(ERROR) << "Cannot write metadata to " << super_device;
  194. return false;
  195. }
  196. LOG(INFO) << "Written metadata to " << super_device;
  197. } else {
  198. if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
  199. LOG(ERROR) << "Cannot write metadata to slot "
  200. << BootControlInterface::SlotName(target_slot) << " in "
  201. << super_device;
  202. return false;
  203. }
  204. LOG(INFO) << "Copied metadata to slot "
  205. << BootControlInterface::SlotName(target_slot) << " in "
  206. << super_device;
  207. }
  208. return true;
  209. }
  210. bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
  211. // We can't use fs_mgr to look up |partition_name| because fstab
  212. // doesn't list every slot partition (it uses the slotselect option
  213. // to mask the suffix).
  214. //
  215. // We can however assume that there's an entry for the /misc mount
  216. // point and use that to get the device file for the misc
  217. // partition. This helps us locate the disk that |partition_name|
  218. // resides on. From there we'll assume that a by-name scheme is used
  219. // so we can just replace the trailing "misc" by the given
  220. // |partition_name| and suffix corresponding to |slot|, e.g.
  221. //
  222. // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
  223. // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
  224. //
  225. // If needed, it's possible to relax the by-name assumption in the
  226. // future by trawling /sys/block looking for the appropriate sibling
  227. // of misc and then finding an entry in /dev matching the sysfs
  228. // entry.
  229. std::string err, misc_device = get_bootloader_message_blk_device(&err);
  230. if (misc_device.empty()) {
  231. LOG(ERROR) << "Unable to get misc block device: " << err;
  232. return false;
  233. }
  234. if (!utils::IsSymlink(misc_device.c_str())) {
  235. LOG(ERROR) << "Device file " << misc_device << " for /misc "
  236. << "is not a symlink.";
  237. return false;
  238. }
  239. *out = base::FilePath(misc_device).DirName().value();
  240. return true;
  241. }
  242. } // namespace chromeos_update_engine