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 <stdint.h>
18 #include <string.h>
19
20 #include <string>
21 #include <string_view>
22 #include <vector>
23
24 #include <android-base/endian.h>
25 #include <android-base/logging.h>
26
27 #include <app_nugget.h>
28 #include <bootloader_message/bootloader_message.h>
29 #include <nos/NuggetClient.h>
30 #include <nos/debug.h>
31 #include <recovery_ui/device.h>
32 #include <recovery_ui/screen_ui.h>
33
34 namespace android {
35 namespace device {
36 namespace google {
37 namespace crosshatch {
38
39 namespace {
40
41 /** Wipe user data from Titan M. */
WipeTitanM()42 bool WipeTitanM() {
43 // Connect to Titan M
44 ::nos::NuggetClient client;
45 client.Open();
46 if (!client.IsOpen()) {
47 LOG(ERROR) << "Failed to connect to Titan M";
48 return false;
49 }
50
51 // Tell it to wipe user data
52 const uint32_t magicValue = htole32(ERASE_CONFIRMATION);
53 std::vector<uint8_t> magic(sizeof(magicValue));
54 memcpy(magic.data(), &magicValue, sizeof(magicValue));
55 const uint32_t status
56 = client.CallApp(APP_ID_NUGGET, NUGGET_PARAM_NUKE_FROM_ORBIT, magic, nullptr);
57 if (status != APP_SUCCESS) {
58 LOG(ERROR) << "Titan M user data wipe failed: " << ::nos::StatusCodeString(status)
59 << " (" << status << ")";
60 return false;
61 }
62
63 LOG(INFO) << "Titan M wipe successful";
64 return true;
65 }
66
67 // Wipes the provisioned flag as part of data wipe.
WipeProvisionedFlag()68 bool WipeProvisionedFlag() {
69 // Must be consistent with the one in init.hardware.rc (10-byte `theme-dark`).
70 const std::string wipe_str(10, '\x00');
71 constexpr size_t kProvisionedFlagOffsetInVendorSpace = 0;
72 if (std::string err; !WriteMiscPartitionVendorSpace(
73 wipe_str.data(), wipe_str.size(), kProvisionedFlagOffsetInVendorSpace, &err)) {
74 LOG(ERROR) << "Failed to write wipe string: " << err;
75 return false;
76 }
77 LOG(INFO) << "Provisioned flag wiped successful";
78 return true;
79 }
80
81 } // namespace
82
83 class CrosshatchDevice : public ::Device
84 {
85 public:
CrosshatchDevice(::ScreenRecoveryUI * const ui)86 CrosshatchDevice(::ScreenRecoveryUI* const ui) : ::Device(ui) {}
87
88 /** Hook to wipe user data not stored in /data */
PostWipeData()89 bool PostWipeData() override {
90 // Try to do everything but report a failure if anything wasn't successful
91 bool totalSuccess = true;
92 ::RecoveryUI* const ui = GetUI();
93
94 ui->Print("Wiping Titan M...\n");
95 if (!WipeTitanM()) {
96 totalSuccess = false;
97 }
98
99 if (!WipeProvisionedFlag()) {
100 totalSuccess = false;
101 }
102
103 // Extendable to wipe other components
104
105 return totalSuccess;
106 }
107 };
108
109 } // namespace crosshatch
110 } // namespace google
111 } // namespace device
112 } // namespace android
113
make_device()114 Device *make_device()
115 {
116 return new ::android::device::google::crosshatch::CrosshatchDevice(new ::ScreenRecoveryUI);
117 }
118