fs_mgr_slotselect.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2015 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 <stdio.h>
  17. #include <string>
  18. #include "fs_mgr.h"
  19. #include "fs_mgr_priv.h"
  20. // Realistically, this file should be part of the android::fs_mgr namespace;
  21. using namespace android::fs_mgr;
  22. // https://source.android.com/devices/tech/ota/ab/ab_implement#partitions
  23. // All partitions that are A/B-ed should be named as follows (slots are always
  24. // named a, b, etc.): boot_a, boot_b, system_a, system_b, vendor_a, vendor_b.
  25. static std::string other_suffix(const std::string& slot_suffix) {
  26. if (slot_suffix == "_a") {
  27. return "_b";
  28. }
  29. if (slot_suffix == "_b") {
  30. return "_a";
  31. }
  32. return "";
  33. }
  34. // Returns "_b" or "_a", which is *the other* slot of androidboot.slot_suffix
  35. // in kernel cmdline, or an empty string if that parameter does not exist.
  36. std::string fs_mgr_get_other_slot_suffix() {
  37. return other_suffix(fs_mgr_get_slot_suffix());
  38. }
  39. // Returns "_a" or "_b" based on androidboot.slot_suffix in kernel cmdline, or an empty string
  40. // if that parameter does not exist.
  41. std::string fs_mgr_get_slot_suffix() {
  42. std::string ab_suffix;
  43. fs_mgr_get_boot_config("slot_suffix", &ab_suffix);
  44. return ab_suffix;
  45. }
  46. // Updates |fstab| for slot_suffix. Returns true on success, false on error.
  47. bool fs_mgr_update_for_slotselect(Fstab* fstab) {
  48. std::string ab_suffix;
  49. for (auto& entry : *fstab) {
  50. if (!entry.fs_mgr_flags.slot_select && !entry.fs_mgr_flags.slot_select_other) {
  51. continue;
  52. }
  53. if (ab_suffix.empty()) {
  54. ab_suffix = fs_mgr_get_slot_suffix();
  55. // Return false if failed to get ab_suffix when MF_SLOTSELECT is specified.
  56. if (ab_suffix.empty()) return false;
  57. }
  58. const auto& update_suffix =
  59. entry.fs_mgr_flags.slot_select ? ab_suffix : other_suffix(ab_suffix);
  60. entry.blk_device = entry.blk_device + update_suffix;
  61. entry.logical_partition_name = entry.logical_partition_name + update_suffix;
  62. }
  63. return true;
  64. }