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> |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
| 27 | using android::base::RemoveFileIfExists; |
| 28 | using android::base::ReadFileToString; |
Mickey Keeley | dfaa9c5 | 2018-04-26 15:05:25 -0700 | [diff] [blame] | 29 | using Json::Reader; |
| 30 | using Json::Value; |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | namespace { |
| 35 | |
Mickey Keeley | dfaa9c5 | 2018-04-26 15:05:25 -0700 | [diff] [blame] | 36 | // Keys for volume, brightness, and user-defined parameters. |
| 37 | constexpr const char* kKeyVolume = "volume"; |
| 38 | constexpr const char* kKeyBrightness = "brightness"; |
| 39 | constexpr const char* kKeyParams = "params"; |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 40 | |
| 41 | constexpr const char* kNextBootFile = "/data/misc/bootanimation/next_boot.json"; |
| 42 | constexpr const char* kLastBootFile = "/data/misc/bootanimation/last_boot.json"; |
| 43 | |
| 44 | void swapBootConfigs() { |
| 45 | // rename() will fail if next_boot.json doesn't exist, so delete |
| 46 | // last_boot.json manually first. |
| 47 | std::string err; |
| 48 | if (!RemoveFileIfExists(kLastBootFile, &err)) |
| 49 | ALOGE("Unable to delete last boot file: %s", err.c_str()); |
| 50 | |
| 51 | if (rename(kNextBootFile, kLastBootFile) && errno != ENOENT) |
| 52 | ALOGE("Unable to swap boot files: %s", strerror(errno)); |
| 53 | |
| 54 | int fd = open(kNextBootFile, O_CREAT, DEFFILEMODE); |
| 55 | if (fd == -1) { |
| 56 | ALOGE("Unable to create next boot file: %s", strerror(errno)); |
| 57 | } else { |
| 58 | // Make next_boot.json writable to everyone so DeviceManagementService |
| 59 | // can save saved_parameters there. |
| 60 | if (fchmod(fd, DEFFILEMODE)) |
| 61 | ALOGE("Unable to set next boot file permissions: %s", strerror(errno)); |
| 62 | close(fd); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } // namespace |
| 67 | |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 68 | BootParameters::BootParameters() { |
| 69 | swapBootConfigs(); |
| 70 | loadParameters(); |
| 71 | } |
| 72 | |
| 73 | void BootParameters::loadParameters() { |
| 74 | std::string contents; |
| 75 | if (!ReadFileToString(kLastBootFile, &contents)) { |
| 76 | if (errno != ENOENT) |
| 77 | ALOGE("Unable to read from %s: %s", kLastBootFile, strerror(errno)); |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | |
Mickey Keeley | 953f109 | 2018-04-26 11:06:06 -0700 | [diff] [blame] | 82 | loadParameters(contents); |
| 83 | } |
| 84 | |
| 85 | void BootParameters::loadParameters(const std::string& raw_json) { |
Mickey Keeley | dfaa9c5 | 2018-04-26 15:05:25 -0700 | [diff] [blame] | 86 | if (!Reader().parse(raw_json, mJson)) { |
| 87 | return; |
| 88 | } |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 89 | |
Mickey Keeley | dfaa9c5 | 2018-04-26 15:05:25 -0700 | [diff] [blame] | 90 | // A missing key returns a safe, missing value. |
| 91 | // Ignore invalid or missing JSON parameters. |
| 92 | Value& jsonValue = mJson[kKeyVolume]; |
| 93 | if (jsonValue.isDouble()) { |
| 94 | mVolume = jsonValue.asFloat(); |
| 95 | } |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 96 | |
Mickey Keeley | dfaa9c5 | 2018-04-26 15:05:25 -0700 | [diff] [blame] | 97 | jsonValue = mJson[kKeyBrightness]; |
| 98 | if (jsonValue.isDouble()) { |
| 99 | mBrightness = jsonValue.asFloat(); |
| 100 | } |
| 101 | |
| 102 | jsonValue = mJson[kKeyParams]; |
| 103 | if (jsonValue.isObject()) { |
| 104 | for (auto &key : jsonValue.getMemberNames()) { |
| 105 | Value& value = jsonValue[key]; |
| 106 | if (value.isString()) { |
| 107 | mParameters.push_back({ |
| 108 | .key = key.c_str(), |
| 109 | .value = value.asCString() |
| 110 | }); |
| 111 | } |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 112 | } |
Mickey Keeley | dfaa9c5 | 2018-04-26 15:05:25 -0700 | [diff] [blame] | 113 | } |
David Pursell | 54a8fe4 | 2017-09-29 16:05:26 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } // namespace android |