summaryrefslogtreecommitdiff
path: root/libgsi.cpp
diff options
context:
space:
mode:
authorDavid Anderson <dvander@google.com>2019-01-11 14:37:51 -0800
committerDavid Anderson <dvander@google.com>2019-01-14 19:10:28 -0800
commitb3aff184cac0cbea6a587c4bf290fe2ae83b85b1 (patch)
treeceff0baab37474577965a450a1df7f2b99373cef /libgsi.cpp
parentdeb2e96a68209018d82e7c2187055c9e8a17dbda (diff)
downloadgsid-b3aff184cac0cbea6a587c4bf290fe2ae83b85b1.tar.gz
Implement a boot attempt counter.
Bug: 122556707 Test: manual test Change-Id: I75457b95aef9d74bbf4121850f2f29fdc887386c
Diffstat (limited to 'libgsi.cpp')
-rw-r--r--libgsi.cpp45
1 files changed, 40 insertions, 5 deletions
diff --git a/libgsi.cpp b/libgsi.cpp
index 135fe04..7be4251 100644
--- a/libgsi.cpp
+++ b/libgsi.cpp
@@ -16,21 +16,28 @@
#include "libgsi/libgsi.h"
+#include <string.h>
#include <unistd.h>
+#include <string>
+
#include <android-base/file.h>
+#include <android-base/parseint.h>
#include "file_paths.h"
+#include "libgsi_private.h"
namespace android {
namespace gsi {
+using namespace std::literals;
+
bool IsGsiRunning() {
return !access(kGsiBootedIndicatorFile, F_OK);
}
bool IsGsiInstalled() {
- return !access(kGsiBootableFile, F_OK);
+ return !access(kGsiInstallStatusFile, F_OK);
}
static bool CanBootIntoGsi(std::string* error) {
@@ -38,8 +45,29 @@ static bool CanBootIntoGsi(std::string* error) {
*error = "not detected";
return false;
}
- // :TODO: boot attempts
- return true;
+
+ std::string boot_key;
+ if (!GetInstallStatus(&boot_key)) {
+ *error = "error ("s + strerror(errno) + ")";
+ return false;
+ }
+
+ // Give up if we've failed to boot kMaxBootAttempts times.
+ int attempts;
+ if (GetBootAttempts(boot_key, &attempts)) {
+ if (attempts + 1 >= kMaxBootAttempts) {
+ *error = "exceeded max boot attempts";
+ return false;
+ }
+ std::string new_key = std::to_string(attempts + 1);
+ if (!android::base::WriteStringToFile(new_key, kGsiInstallStatusFile)) {
+ *error = "error ("s + strerror(errno) + ")";
+ return false;
+ }
+ return true;
+ }
+
+ return boot_key == kInstallStatusOk;
}
bool CanBootIntoGsi(std::string* metadata_file, std::string* error) {
@@ -49,7 +77,6 @@ bool CanBootIntoGsi(std::string* metadata_file, std::string* error) {
android::base::RemoveFileIfExists(kGsiBootedIndicatorFile);
if (!CanBootIntoGsi(error)) {
- android::base::RemoveFileIfExists(kGsiBootableFile);
return false;
}
@@ -58,7 +85,7 @@ bool CanBootIntoGsi(std::string* metadata_file, std::string* error) {
}
bool UninstallGsi() {
- if (!android::base::RemoveFileIfExists(kGsiBootableFile)) {
+ if (!android::base::WriteStringToFile(kInstallStatusWipe, kGsiInstallStatusFile)) {
return false;
}
return true;
@@ -68,5 +95,13 @@ bool MarkSystemAsGsi() {
return android::base::WriteStringToFile("1", kGsiBootedIndicatorFile);
}
+bool GetInstallStatus(std::string* status) {
+ return android::base::ReadFileToString(kGsiInstallStatusFile, status);
+}
+
+bool GetBootAttempts(const std::string& boot_key, int* attempts) {
+ return android::base::ParseInt(boot_key, attempts);
+}
+
} // namespace gsi
} // namespace android