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
21 #include <errno.h>
22 #include <fcntl.h>
23
24 #include <android-base/file.h>
25 #include <json/json.h>
26 #include <utils/Log.h>
27
28 using android::base::ReadFileToString;
29 using android::base::RemoveFileIfExists;
30 using android::base::WriteStringToFile;
31 using Json::ArrayIndex;
32 using Json::Reader;
33 using Json::Value;
34
35 namespace android {
36
37 namespace {
38
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";
46
47 constexpr const char *kNextBootFile = "/data/misc/bootanimation/next_boot.proto";
48 constexpr const char *kLastBootFile = "/data/misc/bootanimation/last_boot.proto";
49
50 constexpr const char *kLegacyNextBootFile = "/data/misc/bootanimation/next_boot.json";
51 constexpr const char *kLegacyLastBootFile = "/data/misc/bootanimation/last_boot.json";
52
removeLegacyFiles()53 void removeLegacyFiles() {
54 std::string err;
55 if (!RemoveFileIfExists(kLegacyLastBootFile, &err)) {
56 ALOGW("Unable to delete %s: %s", kLegacyLastBootFile, err.c_str());
57 }
58
59 err.clear();
60 if (!RemoveFileIfExists(kLegacyNextBootFile, &err)) {
61 ALOGW("Unable to delete %s: %s", kLegacyNextBootFile, err.c_str());
62 }
63 }
64
createNextBootFile()65 void createNextBootFile() {
66 errno = 0;
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
81 // Renames the 'next' boot file to the 'last' file and reads its contents.
swapAndLoadBootConfigContents(const char * lastBootFile,const char * nextBootFile,std::string * 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
BootParameters()97 BootParameters::BootParameters() {
98 loadParameters();
99 }
100
101 // Saves the boot parameters state to disk so the framework can read it.
storeParameters()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 }
107
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.
loadParameters()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
parseBootParameters(const std::string & contents)134 void BootParameters::parseBootParameters(const std::string &contents) {
135 if (!mProto.ParseFromString(contents)) {
136 ALOGW("Failed to parse parameters from %s", kLastBootFile);
137 return;
138 }
139
140 loadStateFromProto();
141 }
142
143 // Parses the JSON in the proto.
parseLegacyBootParameters(const std::string & contents)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;
149 }
150
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());
180 }
181 }
182 }
183
184 loadStateFromProto();
185 }
186
loadStateFromProto()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 }
195 }
196
197 } // namespace android
198