• 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_0 {
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 
PostWipeData(const std::vector<std::string> & args)102 Result PostWipeData(const std::vector<std::string>& args) {
103     if (!args.size()||((args[0] != "userdata")&&(args[0] != "support"))) {
104         return { Status::INVALID_ARGUMENT, "Invalid oem command" };
105     }
106 
107     if (args[0] == "support") {
108         return { Status::SUCCESS, "" };
109     }
110 
111     // Connect to Titan M
112     ::nos::NuggetClient client;
113     client.Open();
114     if (!client.IsOpen()) {
115         return { Status::FAILURE_UNKNOWN, "open Titan M fail" };
116     }
117 
118     // Tell Titan M to wipe user data
119     const uint32_t magicValue = htole32(ERASE_CONFIRMATION);
120     std::vector<uint8_t> magic(sizeof(magicValue));
121     memcpy(magic.data(), &magicValue, sizeof(magicValue));
122     const uint8_t retry_count = 5;
123     for(uint8_t i = 0; i < retry_count; i++) {
124         const uint32_t status
125             = client.CallApp(APP_ID_NUGGET, NUGGET_PARAM_NUKE_FROM_ORBIT, magic, nullptr);
126         if (status == APP_SUCCESS) {
127             return { Status::SUCCESS, "" };
128         }
129     }
130 
131     return { Status::FAILURE_UNKNOWN, "Titan M user data wipe failed" };
132 }
133 
doOemCommand(const::android::hardware::hidl_string & oemCmdArgs,doOemCommand_cb _hidl_cb)134 Return<void> Fastboot::doOemCommand(const ::android::hardware::hidl_string& oemCmdArgs,
135                           doOemCommand_cb _hidl_cb) {
136     const std::unordered_map<std::string, OEMCommandHandler> kOEMCmdMap = {
137         {FB_OEM_SET_BRIGHTNESS, SetBrightnessLevel},
138         {FB_OEM_POST_WIPEDATA, PostWipeData},
139     };
140 
141     auto args = android::base::Split(oemCmdArgs, " ");
142     if (args.size() < 2) {
143         _hidl_cb({ Status::INVALID_ARGUMENT, "Invalid OEM command" });
144         return Void();
145     }
146 
147     // args[0] will be "oem", args[1] will be the command name
148     auto cmd_handler = kOEMCmdMap.find(args[1]);
149     if (cmd_handler != kOEMCmdMap.end()) {
150         _hidl_cb(cmd_handler->second(std::vector<std::string>(args.begin() + 2, args.end())));
151     } else {
152         _hidl_cb({ Status::FAILURE_UNKNOWN, "Unknown OEM command" });
153     }
154 
155     return Void();
156 }
157 
Fastboot()158 Fastboot::Fastboot() {}
159 
160 // Methods from ::android::hidl::base::V1_0::IBase follow.
161 
HIDL_FETCH_IFastboot(const char *)162 extern "C" IFastboot* HIDL_FETCH_IFastboot(const char* /* name */) {
163     return new Fastboot();
164 }
165 
166 }  // namespace implementation
167 }  // namespace V1_0
168 }  // namespace fastboot
169 }  // namespace hardware
170 }  // namespace android
171