David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 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 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 19 | #include <string.h> |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 22 | #include <string> |
| 23 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 24 | #include <android-base/file.h> |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 25 | #include <android-base/parseint.h> |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 26 | #include <android-base/strings.h> |
David Anderson | 15d8b4c | 2019-02-01 16:30:31 -0800 | [diff] [blame] | 27 | #include <android-base/unique_fd.h> |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 28 | |
| 29 | #include "file_paths.h" |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 30 | #include "libgsi_private.h" |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | namespace gsi { |
| 34 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 35 | using namespace std::literals; |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 36 | using android::base::ReadFileToString; |
| 37 | using android::base::Split; |
David Anderson | 15d8b4c | 2019-02-01 16:30:31 -0800 | [diff] [blame] | 38 | using android::base::unique_fd; |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 39 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 40 | bool IsGsiRunning() { |
| 41 | return !access(kGsiBootedIndicatorFile, F_OK); |
| 42 | } |
| 43 | |
| 44 | bool IsGsiInstalled() { |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 45 | return !access(kDsuInstallStatusFile, F_OK); |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 46 | } |
| 47 | |
David Anderson | 15d8b4c | 2019-02-01 16:30:31 -0800 | [diff] [blame] | 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 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 59 | std::string GetDsuSlot(const std::string& install_dir) { |
| 60 | return android::base::Basename(install_dir); |
| 61 | } |
| 62 | |
Howard Chen | d5229bf | 2019-09-15 23:16:06 +0800 | [diff] [blame] | 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 | |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 69 | if (!IsGsiInstalled()) { |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 70 | *error = "not detected"; |
| 71 | return false; |
| 72 | } |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 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)) { |
David Anderson | 782b323 | 2019-01-17 14:18:20 -0800 | [diff] [blame] | 83 | if (attempts + 1 > kMaxBootAttempts) { |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 84 | *error = "exceeded max boot attempts"; |
| 85 | return false; |
| 86 | } |
David Anderson | 782b323 | 2019-01-17 14:18:20 -0800 | [diff] [blame] | 87 | |
| 88 | std::string new_key; |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 89 | if (!access(kDsuOneShotBootFile, F_OK)) { |
David Anderson | 782b323 | 2019-01-17 14:18:20 -0800 | [diff] [blame] | 90 | // Mark the GSI as disabled. This only affects the next boot, not |
David Anderson | 564a04c | 2019-02-27 13:33:44 -0800 | [diff] [blame] | 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. |
David Anderson | 782b323 | 2019-01-17 14:18:20 -0800 | [diff] [blame] | 94 | new_key = kInstallStatusDisabled; |
| 95 | } else { |
| 96 | new_key = std::to_string(attempts + 1); |
| 97 | } |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 98 | if (!WriteAndSyncFile(new_key, kDsuInstallStatusFile)) { |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 99 | *error = "error ("s + strerror(errno) + ")"; |
| 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
David Anderson | e7a8ade | 2019-03-13 19:21:22 -0700 | [diff] [blame] | 105 | if (boot_key != kInstallStatusOk) { |
| 106 | *error = "not enabled"; |
| 107 | return false; |
| 108 | } |
| 109 | return true; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 110 | } |
| 111 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 112 | bool UninstallGsi() { |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 113 | return android::base::WriteStringToFile(kInstallStatusWipe, kDsuInstallStatusFile); |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool DisableGsi() { |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 117 | return android::base::WriteStringToFile(kInstallStatusDisabled, kDsuInstallStatusFile); |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | bool MarkSystemAsGsi() { |
| 121 | return android::base::WriteStringToFile("1", kGsiBootedIndicatorFile); |
| 122 | } |
| 123 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 124 | bool GetInstallStatus(std::string* status) { |
David Anderson | 9bdf863 | 2019-07-01 14:49:11 -0700 | [diff] [blame] | 125 | return android::base::ReadFileToString(kDsuInstallStatusFile, status); |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | bool GetBootAttempts(const std::string& boot_key, int* attempts) { |
| 129 | return android::base::ParseInt(boot_key, attempts); |
| 130 | } |
| 131 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 132 | } // namespace gsi |
| 133 | } // namespace android |