• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 "fastboot/Fastboot.h"
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/strings.h>
26 #include <android-base/unique_fd.h>
27 #include <app_nugget.h>
28 #include <nos/NuggetClient.h>
29 #include <nos/debug.h>
30 
31 namespace android {
32 namespace hardware {
33 namespace fastboot {
34 namespace V1_1 {
35 namespace implementation {
36 
37 constexpr const char* BRIGHTNESS_FILE = "/sys/class/backlight/panel0-backlight/brightness";
38 constexpr int DISPLAY_BRIGHTNESS_DIM_THRESHOLD = 20;
39 
40 using  OEMCommandHandler = std::function<Result(const std::vector<std::string>&)>;
41 
getPartitionType(const::android::hardware::hidl_string &,getPartitionType_cb _hidl_cb)42 Return<void> Fastboot::getPartitionType(const ::android::hardware::hidl_string& /* partitionName */,
43                                   getPartitionType_cb _hidl_cb) {
44     // For bluecross devices, all partitions need to return raw.
45     _hidl_cb(FileSystemType::RAW, { Status::SUCCESS, "" });
46     return Void();
47 }
48 
getVariant(getVariant_cb _hidl_cb)49 Return<void> Fastboot::getVariant(getVariant_cb _hidl_cb) {
50     _hidl_cb("MSM USF", {Status::SUCCESS, "" });
51     return Void();
52 }
53 
getOffModeChargeState(getOffModeChargeState_cb _hidl_cb)54 Return<void> Fastboot::getOffModeChargeState(getOffModeChargeState_cb _hidl_cb) {
55     constexpr const char* kDevinfoPath = "/dev/block/by-name/devinfo";
56     constexpr int kDevInfoOffModeChargeOffset = 15;
57 
58     uint8_t off_mode_charge_status = 0;
59     android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(kDevinfoPath,
60                                                         O_RDONLY | O_BINARY)));
61     if (!android::base::ReadFullyAtOffset(fd, &off_mode_charge_status, 1 /* byte count */,
62                                           kDevInfoOffModeChargeOffset)) {
63         _hidl_cb(false,
64                  { Status::FAILURE_UNKNOWN, "Unable to read off-mode-charge state" });
65     } else {
66         _hidl_cb(off_mode_charge_status != 0, { Status::SUCCESS, "" });
67     }
68 
69     return Void();
70 }
71 
getBatteryVoltageFlashingThreshold(getBatteryVoltageFlashingThreshold_cb _hidl_cb)72 Return<void> Fastboot::getBatteryVoltageFlashingThreshold(
73         getBatteryVoltageFlashingThreshold_cb _hidl_cb) {
74     constexpr int kMinVoltageForFlashing = 3500;
75     _hidl_cb(kMinVoltageForFlashing, { Status::SUCCESS, "" });
76     return Void();
77 }
78 
SetBrightnessLevel(const std::vector<std::string> & args)79 Result SetBrightnessLevel(const std::vector<std::string>& args) {
80     if (!args.size()) {
81         return { Status::INVALID_ARGUMENT, "Brightness level unspecified" };
82     }
83 
84     auto level = std::stoi(args[0]);
85 
86     if (level < 0 || level > 100)  {
87         return { Status::INVALID_ARGUMENT, "Brighness level must be between 0 and 100" };
88     }
89 
90     // Avoid screen being dimmed too much.
91     if (level < DISPLAY_BRIGHTNESS_DIM_THRESHOLD) {
92         level = DISPLAY_BRIGHTNESS_DIM_THRESHOLD;
93     }
94 
95     if (android::base::WriteStringToFile(std::to_string(level), BRIGHTNESS_FILE)) {
96             return { Status::SUCCESS, "" };
97     }
98 
99     return { Status::FAILURE_UNKNOWN, "Unable to set display brightness" };
100 }
101 
doOemCommand(const::android::hardware::hidl_string & oemCmdArgs,doOemCommand_cb _hidl_cb)102 Return<void> Fastboot::doOemCommand(const ::android::hardware::hidl_string& oemCmdArgs,
103                           doOemCommand_cb _hidl_cb) {
104     const std::unordered_map<std::string, OEMCommandHandler> kOEMCmdMap = {
105         {FB_OEM_SET_BRIGHTNESS, SetBrightnessLevel},
106     };
107 
108     auto args = android::base::Split(oemCmdArgs, " ");
109     if (args.size() < 2) {
110         _hidl_cb({ Status::INVALID_ARGUMENT, "Invalid OEM command" });
111         return Void();
112     }
113 
114     // args[0] will be "oem", args[1] will be the command name
115     auto cmd_handler = kOEMCmdMap.find(args[1]);
116     if (cmd_handler != kOEMCmdMap.end()) {
117         _hidl_cb(cmd_handler->second(std::vector<std::string>(args.begin() + 2, args.end())));
118     } else {
119         _hidl_cb({ Status::FAILURE_UNKNOWN, "Unknown OEM command" });
120     }
121 
122     return Void();
123 }
124 
doOemSpecificErase(V1_1::IFastboot::doOemSpecificErase_cb _hidl_cb)125 Return<void> Fastboot::doOemSpecificErase(V1_1::IFastboot::doOemSpecificErase_cb _hidl_cb) {
126     // Connect to Titan M
127     ::nos::NuggetClient client;
128     client.Open();
129     if (!client.IsOpen()) {
130         _hidl_cb({ Status::FAILURE_UNKNOWN, "open Titan M fail" });
131         return Void();
132     }
133 
134     // Tell Titan M to wipe user data
135     const uint32_t magicValue = htole32(ERASE_CONFIRMATION);
136     std::vector<uint8_t> magic(sizeof(magicValue));
137     memcpy(magic.data(), &magicValue, sizeof(magicValue));
138     const uint8_t retry_count = 5;
139     for(uint8_t i = 0; i < retry_count; i++) {
140         const uint32_t status
141             = client.CallApp(APP_ID_NUGGET, NUGGET_PARAM_NUKE_FROM_ORBIT, magic, nullptr);
142         if (status == APP_SUCCESS) {
143             _hidl_cb({ Status::SUCCESS, "" });
144             return Void();
145         }
146     }
147 
148     _hidl_cb({ Status::FAILURE_UNKNOWN, "Titan M user data wipe failed" });
149     return Void();
150 }
151 
Fastboot()152 Fastboot::Fastboot() {}
153 
154 // Methods from ::android::hidl::base::V1_0::IBase follow.
155 
HIDL_FETCH_IFastboot(const char *)156 extern "C" IFastboot* HIDL_FETCH_IFastboot(const char* /* name */) {
157     return new Fastboot();
158 }
159 
160 }  // namespace implementation
161 }  // namespace V1_1
162 }  // namespace fastboot
163 }  // namespace hardware
164 }  // namespace android
165