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> |
David Anderson | 15d8b4c | 2019-02-01 16:30:31 -0800 | [diff] [blame^] | 26 | #include <android-base/unique_fd.h> |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 27 | |
| 28 | #include "file_paths.h" |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 29 | #include "libgsi_private.h" |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace gsi { |
| 33 | |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 34 | using namespace std::literals; |
David Anderson | 15d8b4c | 2019-02-01 16:30:31 -0800 | [diff] [blame^] | 35 | using android::base::unique_fd; |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 36 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 37 | bool IsGsiRunning() { |
| 38 | return !access(kGsiBootedIndicatorFile, F_OK); |
| 39 | } |
| 40 | |
| 41 | bool IsGsiInstalled() { |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 42 | return !access(kGsiInstallStatusFile, F_OK); |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 43 | } |
| 44 | |
David Anderson | 782b323 | 2019-01-17 14:18:20 -0800 | [diff] [blame] | 45 | // If true, we only ever allow a single boot into GSI. Rebooting the device |
| 46 | // will bring the device back to its normal system image. |
| 47 | static constexpr bool kOnlyAllowSingleBoot = true; |
| 48 | |
David Anderson | 15d8b4c | 2019-02-01 16:30:31 -0800 | [diff] [blame^] | 49 | static bool WriteAndSyncFile(const std::string& data, const std::string& file) { |
| 50 | unique_fd fd(open(file.c_str(), O_WRONLY | O_NOFOLLOW | O_CLOEXEC)); |
| 51 | if (fd < 0) { |
| 52 | return false; |
| 53 | } |
| 54 | if (!android::base::WriteFully(fd, data.c_str(), data.size())) { |
| 55 | return false; |
| 56 | } |
| 57 | return fsync(fd) == 0; |
| 58 | } |
| 59 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 60 | static bool CanBootIntoGsi(std::string* error) { |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 61 | if (!IsGsiInstalled()) { |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 62 | *error = "not detected"; |
| 63 | return false; |
| 64 | } |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 65 | |
| 66 | std::string boot_key; |
| 67 | if (!GetInstallStatus(&boot_key)) { |
| 68 | *error = "error ("s + strerror(errno) + ")"; |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | // Give up if we've failed to boot kMaxBootAttempts times. |
| 73 | int attempts; |
| 74 | if (GetBootAttempts(boot_key, &attempts)) { |
David Anderson | 782b323 | 2019-01-17 14:18:20 -0800 | [diff] [blame] | 75 | if (attempts + 1 > kMaxBootAttempts) { |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 76 | *error = "exceeded max boot attempts"; |
| 77 | return false; |
| 78 | } |
David Anderson | 782b323 | 2019-01-17 14:18:20 -0800 | [diff] [blame] | 79 | |
| 80 | std::string new_key; |
| 81 | if (kOnlyAllowSingleBoot) { |
| 82 | // Mark the GSI as disabled. This only affects the next boot, not |
| 83 | // the current boot. |
| 84 | new_key = kInstallStatusDisabled; |
| 85 | } else { |
| 86 | new_key = std::to_string(attempts + 1); |
| 87 | } |
David Anderson | 15d8b4c | 2019-02-01 16:30:31 -0800 | [diff] [blame^] | 88 | if (!WriteAndSyncFile(new_key, kGsiInstallStatusFile)) { |
David Anderson | b3aff18 | 2019-01-11 14:37:51 -0800 | [diff] [blame] | 89 | *error = "error ("s + strerror(errno) + ")"; |
| 90 | return false; |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | return boot_key == kInstallStatusOk; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | bool CanBootIntoGsi(std::string* metadata_file, std::string* error) { |
David Anderson | c053b3b | 2019-01-08 18:22:07 -0800 | [diff] [blame] | 99 | // Always delete this as a safety precaution, so we can return to the |
| 100 | // original system image. If we're confident GSI will boot, this will |
| 101 | // get re-created by MarkSystemAsGsi. |
| 102 | android::base::RemoveFileIfExists(kGsiBootedIndicatorFile); |
| 103 | |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 104 | if (!CanBootIntoGsi(error)) { |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 108 | *metadata_file = kGsiLpMetadataFile; |
David Anderson | ee84d74 | 2019-01-07 18:10:29 -0800 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
| 112 | bool UninstallGsi() { |
David Anderson | a141ba8 | 2019-01-14 19:09:27 -0800 | [diff] [blame] | 113 | return android::base::WriteStringToFile(kInstallStatusWipe, kGsiInstallStatusFile); |
| 114 | } |
| 115 | |
| 116 | bool DisableGsi() { |
| 117 | return android::base::WriteStringToFile(kInstallStatusDisabled, kGsiInstallStatusFile); |
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) { |
| 125 | return android::base::ReadFileToString(kGsiInstallStatusFile, status); |
| 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 |