blob: 30a9b2895c441de340d602f67cb767837462b7d0 [file] [log] [blame]
David Pursell54a8fe42017-09-29 16:05:26 -07001/*
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 Keeleydfaa9c52018-04-26 15:05:25 -070021#include <errno.h>
David Pursell54a8fe42017-09-29 16:05:26 -070022#include <fcntl.h>
23
David Pursell54a8fe42017-09-29 16:05:26 -070024#include <android-base/file.h>
Mickey Keeley81121bd2018-05-29 17:43:22 -070025#include <json/json.h>
David Pursell54a8fe42017-09-29 16:05:26 -070026#include <utils/Log.h>
27
David Pursell54a8fe42017-09-29 16:05:26 -070028using android::base::ReadFileToString;
Mickey Keeley1ffcc5e2018-05-07 09:42:19 -070029using android::base::RemoveFileIfExists;
Mickey Keeley81121bd2018-05-29 17:43:22 -070030using android::base::WriteStringToFile;
31using Json::ArrayIndex;
Mickey Keeleydfaa9c52018-04-26 15:05:25 -070032using Json::Reader;
33using Json::Value;
David Pursell54a8fe42017-09-29 16:05:26 -070034
35namespace android {
36
37namespace {
38
Mickey Keeley81121bd2018-05-29 17:43:22 -070039// 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.
42constexpr const char *kKeyLegacyVolume = "volume";
43constexpr const char *kKeyLegacyAnimationsDisabled = "boot_animation_disabled";
44constexpr const char *kKeyLegacyParamNames = "param_names";
45constexpr const char *kKeyLegacyParamValues = "param_values";
David Pursell54a8fe42017-09-29 16:05:26 -070046
Mickey Keeley81121bd2018-05-29 17:43:22 -070047constexpr const char *kNextBootFile = "/data/misc/bootanimation/next_boot.proto";
48constexpr const char *kLastBootFile = "/data/misc/bootanimation/last_boot.proto";
David Pursell54a8fe42017-09-29 16:05:26 -070049
Mickey Keeley81121bd2018-05-29 17:43:22 -070050constexpr const char *kLegacyNextBootFile = "/data/misc/bootanimation/next_boot.json";
51constexpr const char *kLegacyLastBootFile = "/data/misc/bootanimation/last_boot.json";
52
53void removeLegacyFiles() {
David Pursell54a8fe42017-09-29 16:05:26 -070054 std::string err;
Mickey Keeley81121bd2018-05-29 17:43:22 -070055 if (!RemoveFileIfExists(kLegacyLastBootFile, &err)) {
56 ALOGW("Unable to delete %s: %s", kLegacyLastBootFile, err.c_str());
57 }
David Pursell54a8fe42017-09-29 16:05:26 -070058
Mickey Keeley81121bd2018-05-29 17:43:22 -070059 err.clear();
60 if (!RemoveFileIfExists(kLegacyNextBootFile, &err)) {
61 ALOGW("Unable to delete %s: %s", kLegacyNextBootFile, err.c_str());
62 }
63}
David Pursell54a8fe42017-09-29 16:05:26 -070064
Mickey Keeley81121bd2018-05-29 17:43:22 -070065void createNextBootFile() {
66 errno = 0;
David Pursell54a8fe42017-09-29 16:05:26 -070067 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 Keeley81121bd2018-05-29 17:43:22 -070081// Renames the 'next' boot file to the 'last' file and reads its contents.
82bool 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 Pursell54a8fe42017-09-29 16:05:26 -070097BootParameters::BootParameters() {
David Pursell54a8fe42017-09-29 16:05:26 -070098 loadParameters();
99}
100
Mickey Keeley81121bd2018-05-29 17:43:22 -0700101// Saves the boot parameters state to disk so the framework can read it.
102void 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 Pursell54a8fe42017-09-29 16:05:26 -0700107
Mickey Keeley81121bd2018-05-29 17:43:22 -0700108 // 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.
120void 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
134void BootParameters::parseBootParameters(const std::string &contents) {
135 if (!mProto.ParseFromString(contents)) {
136 ALOGW("Failed to parse parameters from %s", kLastBootFile);
David Pursell54a8fe42017-09-29 16:05:26 -0700137 return;
138 }
139
Mickey Keeley81121bd2018-05-29 17:43:22 -0700140 loadStateFromProto();
Mickey Keeley953f1092018-04-26 11:06:06 -0700141}
142
Mickey Keeley81121bd2018-05-29 17:43:22 -0700143// Parses the JSON in the proto.
144void 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 Pursell54a8fe42017-09-29 16:05:26 -0700149 }
Mickey Keeley1ffcc5e2018-05-07 09:42:19 -0700150
Mickey Keeley81121bd2018-05-29 17:43:22 -0700151 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 Keeley1ffcc5e2018-05-07 09:42:19 -0700180 }
181 }
182 }
Mickey Keeley81121bd2018-05-29 17:43:22 -0700183
184 loadStateFromProto();
185}
186
187void 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 &param : mProto.user_parameter()) {
193 mParameters.push_back({.key = param.key().c_str(), .value = param.value().c_str()});
194 }
David Pursell54a8fe42017-09-29 16:05:26 -0700195}
196
197} // namespace android