xref: /aosp_15_r20/system/gsid/libgsi.cpp (revision 4e2b41f188908a2ae9d9a2089f1f10779d080021)
1*4e2b41f1SAndroid Build Coastguard Worker //
2*4e2b41f1SAndroid Build Coastguard Worker // Copyright (C) 2019 The Android Open Source Project
3*4e2b41f1SAndroid Build Coastguard Worker //
4*4e2b41f1SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
5*4e2b41f1SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
6*4e2b41f1SAndroid Build Coastguard Worker // You may obtain a copy of the License at
7*4e2b41f1SAndroid Build Coastguard Worker //
8*4e2b41f1SAndroid Build Coastguard Worker //      http://www.apache.org/licenses/LICENSE-2.0
9*4e2b41f1SAndroid Build Coastguard Worker //
10*4e2b41f1SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
11*4e2b41f1SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
12*4e2b41f1SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4e2b41f1SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
14*4e2b41f1SAndroid Build Coastguard Worker // limitations under the License.
15*4e2b41f1SAndroid Build Coastguard Worker //
16*4e2b41f1SAndroid Build Coastguard Worker 
17*4e2b41f1SAndroid Build Coastguard Worker #include "libgsi/libgsi.h"
18*4e2b41f1SAndroid Build Coastguard Worker 
19*4e2b41f1SAndroid Build Coastguard Worker #include <string.h>
20*4e2b41f1SAndroid Build Coastguard Worker #include <unistd.h>
21*4e2b41f1SAndroid Build Coastguard Worker 
22*4e2b41f1SAndroid Build Coastguard Worker #include <string>
23*4e2b41f1SAndroid Build Coastguard Worker 
24*4e2b41f1SAndroid Build Coastguard Worker #include <android-base/file.h>
25*4e2b41f1SAndroid Build Coastguard Worker #include <android-base/parseint.h>
26*4e2b41f1SAndroid Build Coastguard Worker #include <android-base/strings.h>
27*4e2b41f1SAndroid Build Coastguard Worker #include <android-base/unique_fd.h>
28*4e2b41f1SAndroid Build Coastguard Worker 
29*4e2b41f1SAndroid Build Coastguard Worker #include "file_paths.h"
30*4e2b41f1SAndroid Build Coastguard Worker #include "libgsi_private.h"
31*4e2b41f1SAndroid Build Coastguard Worker 
32*4e2b41f1SAndroid Build Coastguard Worker namespace android {
33*4e2b41f1SAndroid Build Coastguard Worker namespace gsi {
34*4e2b41f1SAndroid Build Coastguard Worker 
35*4e2b41f1SAndroid Build Coastguard Worker using namespace std::literals;
36*4e2b41f1SAndroid Build Coastguard Worker using android::base::ReadFileToString;
37*4e2b41f1SAndroid Build Coastguard Worker using android::base::Split;
38*4e2b41f1SAndroid Build Coastguard Worker using android::base::unique_fd;
39*4e2b41f1SAndroid Build Coastguard Worker 
IsGsiImage()40*4e2b41f1SAndroid Build Coastguard Worker bool IsGsiImage() {
41*4e2b41f1SAndroid Build Coastguard Worker     return !access(kGsiSpecificInitRcFile, F_OK);
42*4e2b41f1SAndroid Build Coastguard Worker }
43*4e2b41f1SAndroid Build Coastguard Worker 
IsGsiRunning()44*4e2b41f1SAndroid Build Coastguard Worker bool IsGsiRunning() {
45*4e2b41f1SAndroid Build Coastguard Worker     return !access(kGsiBootedIndicatorFile, F_OK);
46*4e2b41f1SAndroid Build Coastguard Worker }
47*4e2b41f1SAndroid Build Coastguard Worker 
IsGsiInstalled()48*4e2b41f1SAndroid Build Coastguard Worker bool IsGsiInstalled() {
49*4e2b41f1SAndroid Build Coastguard Worker     return !access(kDsuInstallStatusFile, F_OK);
50*4e2b41f1SAndroid Build Coastguard Worker }
51*4e2b41f1SAndroid Build Coastguard Worker 
WriteAndSyncFile(const std::string & data,const std::string & file)52*4e2b41f1SAndroid Build Coastguard Worker static bool WriteAndSyncFile(const std::string& data, const std::string& file) {
53*4e2b41f1SAndroid Build Coastguard Worker     unique_fd fd(open(file.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC));
54*4e2b41f1SAndroid Build Coastguard Worker     if (fd < 0) {
55*4e2b41f1SAndroid Build Coastguard Worker         return false;
56*4e2b41f1SAndroid Build Coastguard Worker     }
57*4e2b41f1SAndroid Build Coastguard Worker     if (!android::base::WriteFully(fd, data.c_str(), data.size())) {
58*4e2b41f1SAndroid Build Coastguard Worker         return false;
59*4e2b41f1SAndroid Build Coastguard Worker     }
60*4e2b41f1SAndroid Build Coastguard Worker     return fsync(fd) == 0;
61*4e2b41f1SAndroid Build Coastguard Worker }
62*4e2b41f1SAndroid Build Coastguard Worker 
GetDsuSlot(const std::string & install_dir)63*4e2b41f1SAndroid Build Coastguard Worker std::string GetDsuSlot(const std::string& install_dir) {
64*4e2b41f1SAndroid Build Coastguard Worker     return android::base::Basename(install_dir);
65*4e2b41f1SAndroid Build Coastguard Worker }
66*4e2b41f1SAndroid Build Coastguard Worker 
CanBootIntoGsi(std::string * error)67*4e2b41f1SAndroid Build Coastguard Worker bool CanBootIntoGsi(std::string* error) {
68*4e2b41f1SAndroid Build Coastguard Worker     // Always delete this as a safety precaution, so we can return to the
69*4e2b41f1SAndroid Build Coastguard Worker     // original system image. If we're confident GSI will boot, this will
70*4e2b41f1SAndroid Build Coastguard Worker     // get re-created by MarkSystemAsGsi.
71*4e2b41f1SAndroid Build Coastguard Worker     android::base::RemoveFileIfExists(kGsiBootedIndicatorFile);
72*4e2b41f1SAndroid Build Coastguard Worker 
73*4e2b41f1SAndroid Build Coastguard Worker     if (!IsGsiInstalled()) {
74*4e2b41f1SAndroid Build Coastguard Worker         *error = "not detected";
75*4e2b41f1SAndroid Build Coastguard Worker         return false;
76*4e2b41f1SAndroid Build Coastguard Worker     }
77*4e2b41f1SAndroid Build Coastguard Worker 
78*4e2b41f1SAndroid Build Coastguard Worker     std::string boot_key;
79*4e2b41f1SAndroid Build Coastguard Worker     if (!GetInstallStatus(&boot_key)) {
80*4e2b41f1SAndroid Build Coastguard Worker         *error = "error ("s + strerror(errno) + ")";
81*4e2b41f1SAndroid Build Coastguard Worker         return false;
82*4e2b41f1SAndroid Build Coastguard Worker     }
83*4e2b41f1SAndroid Build Coastguard Worker 
84*4e2b41f1SAndroid Build Coastguard Worker     // Give up if we've failed to boot kMaxBootAttempts times.
85*4e2b41f1SAndroid Build Coastguard Worker     int attempts;
86*4e2b41f1SAndroid Build Coastguard Worker     if (GetBootAttempts(boot_key, &attempts)) {
87*4e2b41f1SAndroid Build Coastguard Worker         if (attempts + 1 > kMaxBootAttempts) {
88*4e2b41f1SAndroid Build Coastguard Worker             *error = "exceeded max boot attempts";
89*4e2b41f1SAndroid Build Coastguard Worker             return false;
90*4e2b41f1SAndroid Build Coastguard Worker         }
91*4e2b41f1SAndroid Build Coastguard Worker 
92*4e2b41f1SAndroid Build Coastguard Worker         std::string new_key;
93*4e2b41f1SAndroid Build Coastguard Worker         if (!access(kDsuOneShotBootFile, F_OK)) {
94*4e2b41f1SAndroid Build Coastguard Worker             // Mark the GSI as disabled. This only affects the next boot, not
95*4e2b41f1SAndroid Build Coastguard Worker             // the current boot. Note that we leave the one_shot status behind.
96*4e2b41f1SAndroid Build Coastguard Worker             // This is so IGsiService can still return GSI_STATE_SINGLE_BOOT
97*4e2b41f1SAndroid Build Coastguard Worker             // while the GSI is running.
98*4e2b41f1SAndroid Build Coastguard Worker             new_key = kInstallStatusDisabled;
99*4e2b41f1SAndroid Build Coastguard Worker         } else {
100*4e2b41f1SAndroid Build Coastguard Worker             new_key = std::to_string(attempts + 1);
101*4e2b41f1SAndroid Build Coastguard Worker         }
102*4e2b41f1SAndroid Build Coastguard Worker         if (!WriteAndSyncFile(new_key, kDsuInstallStatusFile)) {
103*4e2b41f1SAndroid Build Coastguard Worker             *error = "error ("s + strerror(errno) + ")";
104*4e2b41f1SAndroid Build Coastguard Worker             return false;
105*4e2b41f1SAndroid Build Coastguard Worker         }
106*4e2b41f1SAndroid Build Coastguard Worker         return true;
107*4e2b41f1SAndroid Build Coastguard Worker     }
108*4e2b41f1SAndroid Build Coastguard Worker 
109*4e2b41f1SAndroid Build Coastguard Worker     if (boot_key != kInstallStatusOk) {
110*4e2b41f1SAndroid Build Coastguard Worker         *error = "not enabled";
111*4e2b41f1SAndroid Build Coastguard Worker         return false;
112*4e2b41f1SAndroid Build Coastguard Worker     }
113*4e2b41f1SAndroid Build Coastguard Worker     return true;
114*4e2b41f1SAndroid Build Coastguard Worker }
115*4e2b41f1SAndroid Build Coastguard Worker 
UninstallGsi()116*4e2b41f1SAndroid Build Coastguard Worker bool UninstallGsi() {
117*4e2b41f1SAndroid Build Coastguard Worker     return android::base::WriteStringToFile(kInstallStatusWipe, kDsuInstallStatusFile);
118*4e2b41f1SAndroid Build Coastguard Worker }
119*4e2b41f1SAndroid Build Coastguard Worker 
DisableGsi()120*4e2b41f1SAndroid Build Coastguard Worker bool DisableGsi() {
121*4e2b41f1SAndroid Build Coastguard Worker     return android::base::WriteStringToFile(kInstallStatusDisabled, kDsuInstallStatusFile);
122*4e2b41f1SAndroid Build Coastguard Worker }
123*4e2b41f1SAndroid Build Coastguard Worker 
MarkSystemAsGsi()124*4e2b41f1SAndroid Build Coastguard Worker bool MarkSystemAsGsi() {
125*4e2b41f1SAndroid Build Coastguard Worker     return android::base::WriteStringToFile("1", kGsiBootedIndicatorFile);
126*4e2b41f1SAndroid Build Coastguard Worker }
127*4e2b41f1SAndroid Build Coastguard Worker 
GetInstallStatus(std::string * status)128*4e2b41f1SAndroid Build Coastguard Worker bool GetInstallStatus(std::string* status) {
129*4e2b41f1SAndroid Build Coastguard Worker     return android::base::ReadFileToString(kDsuInstallStatusFile, status);
130*4e2b41f1SAndroid Build Coastguard Worker }
131*4e2b41f1SAndroid Build Coastguard Worker 
GetBootAttempts(const std::string & boot_key,int * attempts)132*4e2b41f1SAndroid Build Coastguard Worker bool GetBootAttempts(const std::string& boot_key, int* attempts) {
133*4e2b41f1SAndroid Build Coastguard Worker     return android::base::ParseInt(boot_key, attempts);
134*4e2b41f1SAndroid Build Coastguard Worker }
135*4e2b41f1SAndroid Build Coastguard Worker 
136*4e2b41f1SAndroid Build Coastguard Worker }  // namespace gsi
137*4e2b41f1SAndroid Build Coastguard Worker }  // namespace android
138