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