1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "factory_reset.h"
16 #include <string>
17 #include "log/dump.h"
18 #include "log/log.h"
19 #include "fs_manager/mount.h"
20
21 namespace Updater {
GetInstance()22 FactoryResetProcess &FactoryResetProcess::GetInstance()
23 {
24 static FactoryResetProcess resetProcessor;
25 return resetProcessor;
26 }
27
FactoryResetProcess()28 FactoryResetProcess::FactoryResetProcess()
29 {
30 RegisterFunc(USER_WIPE_DATA, [this](const std::string &path) { return DoUserReset(path); });
31 RegisterFunc(FACTORY_WIPE_DATA, [this](const std::string &path) { return DoFactoryReset(path); });
32 }
33
RegisterFunc(FactoryResetMode mode,ResetFunc func)34 void FactoryResetProcess::RegisterFunc(FactoryResetMode mode, ResetFunc func)
35 {
36 if (!resetTab_.emplace(mode, func).second) {
37 LOG(ERROR) << "emplace: " << mode << " fail";
38 }
39 }
40
FactoryResetFunc(FactoryResetMode mode,const std::string & path)41 int FactoryResetProcess::FactoryResetFunc(FactoryResetMode mode, const std::string &path)
42 {
43 auto iter = resetTab_.find(mode);
44 if (iter == resetTab_.end() || iter->second == nullptr) {
45 LOG(ERROR) << "Invalid factory reset tag: " << mode;
46 return 1;
47 }
48 if (CommonResetPreFunc_ == nullptr || CommonResetPreFunc_() != 0) {
49 LOG(ERROR) << "Failed to erase the security status";
50 return -1;
51 }
52 if (iter->second(path) != 0) {
53 LOG(ERROR) << "Do factory reset failed! tag: " << mode;
54 return 1;
55 }
56 return 0;
57 }
58
CommonResetPre()59 static int CommonResetPre()
60 {
61 LOG(INFO) << "CommonResetPre";
62 return 0;
63 }
64
RegisterCommonResetPreFunc(CommonResetPreFunc ptr)65 void FactoryResetProcess::RegisterCommonResetPreFunc(CommonResetPreFunc ptr)
66 {
67 CommonResetPreFunc_ = std::move(ptr);
68 }
69
FactoryResetPre()70 static int FactoryResetPre()
71 {
72 LOG(INFO) << "FactoryResetPre";
73 return 0;
74 }
75
RegisterFactoryResetPreFunc(FactoryResetPreFunc ptr)76 void FactoryResetProcess::RegisterFactoryResetPreFunc(FactoryResetPreFunc ptr)
77 {
78 FactoryResetPreFunc_ = std::move(ptr);
79 }
80
FactoryResetPost(int status)81 static int FactoryResetPost(int status)
82 {
83 LOG(INFO) << "FactoryResetPost";
84 return 0;
85 }
86
RegisterFactoryResetPostFunc(FactoryResetPostFunc ptr)87 void FactoryResetProcess::RegisterFactoryResetPostFunc(FactoryResetPostFunc ptr)
88 {
89 FactoryResetPostFunc_ = std::move(ptr);
90 }
91
DoUserReset(const std::string & path)92 int FactoryResetProcess::DoUserReset(const std::string &path)
93 {
94 STAGE(UPDATE_STAGE_BEGIN) << "User FactoryReset";
95 LOG(INFO) << "Begin erasing data";
96 if (FormatPartition(path, true) != 0) {
97 LOG(ERROR) << "User level FactoryReset failed";
98 STAGE(UPDATE_STAGE_FAIL) << "User FactoryReset";
99 ERROR_CODE(CODE_FACTORY_RESET_FAIL);
100 return 1;
101 }
102 LOG(INFO) << "User level FactoryReset success";
103 STAGE(UPDATE_STAGE_SUCCESS) << "User FactoryReset";
104
105 return 0;
106 }
107
DoFactoryReset(const std::string & path)108 int FactoryResetProcess::DoFactoryReset(const std::string &path)
109 {
110 int resetStatus = 0;
111 STAGE(UPDATE_STAGE_BEGIN) << "Factory FactoryReset";
112 if (FactoryResetPreFunc_ == nullptr || FactoryResetPreFunc_() != 0) {
113 LOG(ERROR) << "FactoryResetPreFunc_ fail";
114 }
115 LOG(INFO) << "Begin erasing data";
116 if (FormatPartition(path, true) != 0) {
117 STAGE(UPDATE_STAGE_FAIL) << "Factory FactoryReset";
118 ERROR_CODE(CODE_FACTORY_RESET_FAIL);
119 resetStatus = 1;
120 }
121
122 LOG(INFO) << "Factory level FactoryReset status:" << resetStatus;
123 if (FactoryResetPostFunc_ == nullptr || FactoryResetPostFunc_(resetStatus) != 0) {
124 LOG(ERROR) << "FactoryResetPostFunc_ fail";
125 }
126 return resetStatus;
127 }
128
RegisterCommonResetPreFunc(void)129 extern "C" __attribute__((constructor)) void RegisterCommonResetPreFunc(void)
130 {
131 FactoryResetProcess::GetInstance().RegisterCommonResetPreFunc(CommonResetPre);
132 }
133
RegisterFactoryResetPreFunc(void)134 extern "C" __attribute__((constructor)) void RegisterFactoryResetPreFunc(void)
135 {
136 FactoryResetProcess::GetInstance().RegisterFactoryResetPreFunc(FactoryResetPre);
137 }
138
RegisterFactoryResetPostFunc(void)139 extern "C" __attribute__((constructor)) void RegisterFactoryResetPostFunc(void)
140 {
141 FactoryResetProcess::GetInstance().RegisterFactoryResetPostFunc(FactoryResetPost);
142 }
143 } // Updater