1 /*
2 * Copyright (C) 2016 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 <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <errno.h>
22
23 #include <string>
24
25 #include <android-base/logging.h>
26 #include <bootloader_message/bootloader_message.h>
27 #include <recovery_ui/device.h>
28 #include <recovery_ui/screen_ui.h>
29
30 // Wipes the dark theme flag as part of data wipe.
WipeDarkThemeFlag()31 static bool WipeDarkThemeFlag() {
32 // Must be consistent with the one in init.hardware.rc (10-byte `theme-dark`). The magic is at
33 // 10814 in vendor space, or (2048 + 10814) since the start of /misc.
34 const std::string wipe_str(10, '\x00');
35 constexpr size_t kDarkThemeFlagOffsetInVendorSpace = 10814;
36 if (std::string err; !WriteMiscPartitionVendorSpace(
37 wipe_str.data(), wipe_str.size(), kDarkThemeFlagOffsetInVendorSpace, &err)) {
38 LOG(ERROR) << "Failed to write wipe string: " << err;
39 return false;
40 }
41 LOG(INFO) << "Dark theme flag wiped successfully";
42 return true;
43 }
44
45 class Nanohub_Device : public Device {
46 public:
Nanohub_Device(ScreenRecoveryUI * ui)47 Nanohub_Device(ScreenRecoveryUI* ui) : Device(ui) {}
48 bool PostWipeData();
49 };
50
PostWipeData()51 bool Nanohub_Device::PostWipeData() {
52 int fd = open("/sys/class/nanohub/nanohub/erase_shared", O_WRONLY);
53 if (fd < 0) {
54 PLOG(ERROR) << "open erase_shared failed";
55 } else {
56 if (write(fd, "1\n", 2) != 2) {
57 PLOG(ERROR) << "write to erase_shared failed";
58 } else {
59 LOG(INFO) << "Successfully erased nanoapps";
60 }
61 close(fd);
62 }
63
64 WipeDarkThemeFlag();
65
66 // open/write failure caused by permissions issues would persist across
67 // reboots, so always return true to prevent a factory reset failure loop.
68 return true;
69 }
70
make_device()71 Device* make_device() {
72 return new Nanohub_Device(new ScreenRecoveryUI);
73 }
74