• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 <dlfcn.h>
18 #include <stdint.h>
19 #include <string.h>
20 
21 #include <string>
22 #include <string_view>
23 #include <vector>
24 
25 #include <android-base/endian.h>
26 #include <android-base/logging.h>
27 #include <android-base/strings.h>
28 #include <misc_writer/misc_writer.h>
29 #include <recovery_ui/device.h>
30 #include <recovery_ui/wear_ui.h>
31 
32 namespace android {
33 namespace hardware {
34 namespace google {
35 namespace pixel {
36 
37 namespace {
38 
39 // Provision Silent OTA(SOTA) flag while reason is "enable-sota"
ProvisionSilentOtaFlag(const std::string & reason)40 bool ProvisionSilentOtaFlag(const std::string& reason) {
41     if (android::base::StartsWith(reason, MiscWriter::kSotaFlag)) {
42         MiscWriter misc_writer(MiscWriterActions::kSetSotaFlag);
43         if (!misc_writer.PerformAction()) {
44             LOG(ERROR) << "Failed to set the silent ota flag";
45             return false;
46         }
47         LOG(INFO) << "Silent ota flag set successful";
48     }
49     return true;
50 }
51 
52 /** Call device-specifc WipeEse function, if any. */
WipeEseHook(::RecoveryUI * const ui)53 bool WipeEseHook(::RecoveryUI *const ui) {
54     bool *(*WipeEseFunc)(::RecoveryUI *const);
55     reinterpret_cast<void *&>(WipeEseFunc) = dlsym(RTLD_DEFAULT, "WipeEse");
56     if (WipeEseFunc == nullptr) {
57         LOG(INFO) << "No WipeEse implementation";
58         return true;
59     }
60 
61     return (*WipeEseFunc)(ui);
62 }
63 
64 }  // namespace
65 
66 class PixelWatchDevice : public ::Device {
67   public:
PixelWatchDevice(::WearRecoveryUI * const ui)68     explicit PixelWatchDevice(::WearRecoveryUI* const ui) : ::Device(ui) {}
69 
70     /** Hook to wipe user data not stored in /data */
PostWipeData()71     bool PostWipeData() override {
72         // Try to do everything but report a failure if anything wasn't successful
73         bool totalSuccess = true;
74 
75         // Additional behavior along with wiping data
76         auto reason = GetReason();
77         CHECK(reason.has_value());
78         if (!ProvisionSilentOtaFlag(reason.value())) {
79             totalSuccess = false;
80         }
81 
82         ::RecoveryUI *const ui = GetUI();
83 
84         if (!WipeEseHook(ui)) {
85             totalSuccess = false;
86         }
87 
88         return totalSuccess;
89     }
90 };
91 
92 }  // namespace pixel
93 }  // namespace google
94 }  // namespace hardware
95 }  // namespace android
96 
make_device()97 Device *make_device() {
98     return new ::android::hardware::google::pixel::PixelWatchDevice(new ::WearRecoveryUI);
99 }
100