libgsi.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "libgsi/libgsi.h"
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <string>
  20. #include <android-base/file.h>
  21. #include <android-base/parseint.h>
  22. #include <android-base/unique_fd.h>
  23. #include "file_paths.h"
  24. #include "libgsi_private.h"
  25. namespace android {
  26. namespace gsi {
  27. using namespace std::literals;
  28. using android::base::unique_fd;
  29. bool IsGsiRunning() {
  30. return !access(kGsiBootedIndicatorFile, F_OK);
  31. }
  32. bool IsGsiInstalled() {
  33. return !access(kGsiInstallStatusFile, F_OK);
  34. }
  35. static bool WriteAndSyncFile(const std::string& data, const std::string& file) {
  36. unique_fd fd(open(file.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
  37. if (fd < 0) {
  38. return false;
  39. }
  40. if (!android::base::WriteFully(fd, data.c_str(), data.size())) {
  41. return false;
  42. }
  43. return fsync(fd) == 0;
  44. }
  45. static bool CanBootIntoGsi(std::string* error) {
  46. if (!IsGsiInstalled()) {
  47. *error = "not detected";
  48. return false;
  49. }
  50. std::string boot_key;
  51. if (!GetInstallStatus(&boot_key)) {
  52. *error = "error ("s + strerror(errno) + ")";
  53. return false;
  54. }
  55. // Give up if we've failed to boot kMaxBootAttempts times.
  56. int attempts;
  57. if (GetBootAttempts(boot_key, &attempts)) {
  58. if (attempts + 1 > kMaxBootAttempts) {
  59. *error = "exceeded max boot attempts";
  60. return false;
  61. }
  62. std::string new_key;
  63. if (!access(kGsiOneShotBootFile, F_OK)) {
  64. // Mark the GSI as disabled. This only affects the next boot, not
  65. // the current boot. Note that we leave the one_shot status behind.
  66. // This is so IGsiService can still return GSI_STATE_SINGLE_BOOT
  67. // while the GSI is running.
  68. new_key = kInstallStatusDisabled;
  69. } else {
  70. new_key = std::to_string(attempts + 1);
  71. }
  72. if (!WriteAndSyncFile(new_key, kGsiInstallStatusFile)) {
  73. *error = "error ("s + strerror(errno) + ")";
  74. return false;
  75. }
  76. return true;
  77. }
  78. if (boot_key != kInstallStatusOk) {
  79. *error = "not enabled";
  80. return false;
  81. }
  82. return true;
  83. }
  84. bool CanBootIntoGsi(std::string* metadata_file, std::string* error) {
  85. // Always delete this as a safety precaution, so we can return to the
  86. // original system image. If we're confident GSI will boot, this will
  87. // get re-created by MarkSystemAsGsi.
  88. android::base::RemoveFileIfExists(kGsiBootedIndicatorFile);
  89. if (!CanBootIntoGsi(error)) {
  90. return false;
  91. }
  92. *metadata_file = kGsiLpMetadataFile;
  93. return true;
  94. }
  95. bool UninstallGsi() {
  96. return android::base::WriteStringToFile(kInstallStatusWipe, kGsiInstallStatusFile);
  97. }
  98. bool DisableGsi() {
  99. return android::base::WriteStringToFile(kInstallStatusDisabled, kGsiInstallStatusFile);
  100. }
  101. bool MarkSystemAsGsi() {
  102. return android::base::WriteStringToFile("1", kGsiBootedIndicatorFile);
  103. }
  104. bool GetInstallStatus(std::string* status) {
  105. return android::base::ReadFileToString(kGsiInstallStatusFile, status);
  106. }
  107. bool GetBootAttempts(const std::string& boot_key, int* attempts) {
  108. return android::base::ParseInt(boot_key, attempts);
  109. }
  110. } // namespace gsi
  111. } // namespace android