David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "BootParameters.h" |
| 18 | |
| 19 | #define LOG_TAG "BootParameters" |
| 20 | |
Mickey Keeley | dfaa9c5 | 2018-04-26 15:05:25 -0700 | [diff] [blame] | 21 | #include <errno.h> |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 24 | #include <android-base/file.h> |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 25 | #include <json/json.h> |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 28 | using android::base::ReadFileToString; |
Mickey Keeley | 1ffcc5e | 2018-05-07 09:42:19 -0700 | [diff] [blame] | 29 | using android::base::RemoveFileIfExists; |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 30 | using android::base::WriteStringToFile; |
| 31 | using Json::ArrayIndex; |
Mickey Keeley | dfaa9c5 | 2018-04-26 15:05:25 -0700 | [diff] [blame] | 32 | using Json::Reader; |
| 33 | using Json::Value; |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | namespace { |
| 38 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 39 | // Keys for deprecated parameters. Devices that OTA from N to O and that used |
| 40 | // the hidden BootParameters API will store these in the JSON blob. To support |
| 41 | // the transition from N to O, these keys are mapped to the new parameters. |
| 42 | constexpr const char *kKeyLegacyVolume = "volume"; |
| 43 | constexpr const char *kKeyLegacyAnimationsDisabled = "boot_animation_disabled"; |
| 44 | constexpr const char *kKeyLegacyParamNames = "param_names"; |
| 45 | constexpr const char *kKeyLegacyParamValues = "param_values"; |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 46 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 47 | constexpr const char *kNextBootFile = "/data/misc/bootanimation/next_boot.proto"; |
| 48 | constexpr const char *kLastBootFile = "/data/misc/bootanimation/last_boot.proto"; |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 49 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 50 | constexpr const char *kLegacyNextBootFile = "/data/misc/bootanimation/next_boot.json"; |
| 51 | constexpr const char *kLegacyLastBootFile = "/data/misc/bootanimation/last_boot.json"; |
| 52 | |
| 53 | void removeLegacyFiles() { |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 54 | std::string err; |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 55 | if (!RemoveFileIfExists(kLegacyLastBootFile, &err)) { |
| 56 | ALOGW("Unable to delete %s: %s", kLegacyLastBootFile, err.c_str()); |
| 57 | } |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 58 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 59 | err.clear(); |
| 60 | if (!RemoveFileIfExists(kLegacyNextBootFile, &err)) { |
| 61 | ALOGW("Unable to delete %s: %s", kLegacyNextBootFile, err.c_str()); |
| 62 | } |
| 63 | } |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 64 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 65 | void createNextBootFile() { |
| 66 | errno = 0; |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 67 | int fd = open(kNextBootFile, O_CREAT, DEFFILEMODE); |
| 68 | if (fd == -1) { |
| 69 | ALOGE("Unable to create next boot file: %s", strerror(errno)); |
| 70 | } else { |
| 71 | // Make next_boot.json writable to everyone so DeviceManagementService |
| 72 | // can save saved_parameters there. |
| 73 | if (fchmod(fd, DEFFILEMODE)) |
| 74 | ALOGE("Unable to set next boot file permissions: %s", strerror(errno)); |
| 75 | close(fd); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | } // namespace |
| 80 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 81 | // Renames the 'next' boot file to the 'last' file and reads its contents. |
| 82 | bool BootParameters::swapAndLoadBootConfigContents(const char *lastBootFile, |
| 83 | const char *nextBootFile, |
| 84 | std::string *contents) { |
| 85 | if (!ReadFileToString(nextBootFile, contents)) { |
| 86 | RemoveFileIfExists(lastBootFile); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | errno = 0; |
| 91 | if (rename(nextBootFile, lastBootFile) && errno != ENOENT) |
| 92 | ALOGE("Unable to swap boot files: %s", strerror(errno)); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 97 | BootParameters::BootParameters() { |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 98 | loadParameters(); |
| 99 | } |
| 100 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 101 | // Saves the boot parameters state to disk so the framework can read it. |
| 102 | void BootParameters::storeParameters() { |
| 103 | errno = 0; |
| 104 | if (!WriteStringToFile(mProto.SerializeAsString(), kLastBootFile)) { |
| 105 | ALOGE("Failed to write boot parameters to %s: %s", kLastBootFile, strerror(errno)); |
| 106 | } |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 107 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 108 | // WriteStringToFile sets the file permissions to 0666, but these are not |
| 109 | // honored by the system. |
| 110 | errno = 0; |
| 111 | if (chmod(kLastBootFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) { |
| 112 | ALOGE("Failed to set permissions for %s: %s", kLastBootFile, strerror(errno)); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Load the boot parameters from disk, try the old location and format if the |
| 117 | // file does not exist. Note: |
| 118 | // - Parse errors result in defaults being used (a normal boot). |
| 119 | // - Legacy boot parameters default to a silent boot. |
| 120 | void BootParameters::loadParameters() { |
| 121 | // Precedence is given to the new file format (.proto). |
| 122 | std::string contents; |
| 123 | if (swapAndLoadBootConfigContents(kLastBootFile, kNextBootFile, &contents)) { |
| 124 | parseBootParameters(contents); |
| 125 | } else if (swapAndLoadBootConfigContents(kLegacyLastBootFile, kLegacyNextBootFile, &contents)) { |
| 126 | parseLegacyBootParameters(contents); |
| 127 | storeParameters(); |
| 128 | removeLegacyFiles(); |
| 129 | } |
| 130 | |
| 131 | createNextBootFile(); |
| 132 | } |
| 133 | |
| 134 | void BootParameters::parseBootParameters(const std::string &contents) { |
| 135 | if (!mProto.ParseFromString(contents)) { |
| 136 | ALOGW("Failed to parse parameters from %s", kLastBootFile); |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 140 | loadStateFromProto(); |
Mickey Keeley | 953f109 | 2018-04-26 11:06:06 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 143 | // Parses the JSON in the proto. |
| 144 | void BootParameters::parseLegacyBootParameters(const std::string &contents) { |
| 145 | Value json; |
| 146 | if (!Reader().parse(contents, json)) { |
| 147 | ALOGW("Failed to parse parameters from %s", kLegacyLastBootFile); |
| 148 | return; |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 149 | } |
Mickey Keeley | 1ffcc5e | 2018-05-07 09:42:19 -0700 | [diff] [blame] | 150 | |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 151 | int volume = 0; |
| 152 | bool bootAnimationDisabled = true; |
| 153 | |
| 154 | Value &jsonValue = json[kKeyLegacyVolume]; |
| 155 | if (jsonValue.isIntegral()) { |
| 156 | volume = jsonValue.asInt(); |
| 157 | } |
| 158 | |
| 159 | jsonValue = json[kKeyLegacyAnimationsDisabled]; |
| 160 | if (jsonValue.isIntegral()) { |
| 161 | bootAnimationDisabled = jsonValue.asInt() == 1; |
| 162 | } |
| 163 | |
| 164 | // Assume a silent boot unless all of the following are true - |
| 165 | // 1. The volume is neither 0 nor -1000 (the legacy default value). |
| 166 | // 2. The boot animations are explicitly enabled. |
| 167 | // Note: brightness was never used. |
| 168 | mProto.set_silent_boot((volume == 0) || (volume == -1000) || bootAnimationDisabled); |
| 169 | |
| 170 | Value &keys = json[kKeyLegacyParamNames]; |
| 171 | Value &values = json[kKeyLegacyParamValues]; |
| 172 | if (keys.isArray() && values.isArray() && (keys.size() == values.size())) { |
| 173 | for (ArrayIndex i = 0; i < keys.size(); ++i) { |
| 174 | auto &key = keys[i]; |
| 175 | auto &value = values[i]; |
| 176 | if (key.isString() && value.isString()) { |
| 177 | auto userParameter = mProto.add_user_parameter(); |
| 178 | userParameter->set_key(key.asString()); |
| 179 | userParameter->set_value(value.asString()); |
Mickey Keeley | 1ffcc5e | 2018-05-07 09:42:19 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
Mickey Keeley | 81121bd | 2018-05-29 17:43:22 -0700 | [diff] [blame^] | 183 | |
| 184 | loadStateFromProto(); |
| 185 | } |
| 186 | |
| 187 | void BootParameters::loadStateFromProto() { |
| 188 | // A missing key returns a safe, default value. |
| 189 | // Ignore invalid or missing parameters. |
| 190 | mIsSilentBoot = mProto.silent_boot(); |
| 191 | |
| 192 | for (const auto ¶m : mProto.user_parameter()) { |
| 193 | mParameters.push_back({.key = param.key().c_str(), .value = param.value().c_str()}); |
| 194 | } |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | } // namespace android |