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 #include "scope_guard.h"
21
22 namespace Updater {
GetInstance()23 FactoryResetProcess &FactoryResetProcess::GetInstance()
24 {
25 static FactoryResetProcess resetProcessor;
26 return resetProcessor;
27 }
28
FactoryResetProcess()29 FactoryResetProcess::FactoryResetProcess()
30 {
31 RegisterFunc(USER_WIPE_DATA, [this](const std::string &path) { return DoUserReset(path); });
32 RegisterFunc(FACTORY_WIPE_DATA, [this](const std::string &path) { return DoFactoryReset(path); });
33 RegisterFunc(MENU_WIPE_DATA, [this](const std::string &path) { return DoUserReset(path); });
34 }
35
RegisterFunc(FactoryResetMode mode, ResetFunc func)36 void FactoryResetProcess::RegisterFunc(FactoryResetMode mode, ResetFunc func)
37 {
38 if (!resetTab_.emplace(mode, func).second) {
39 LOG(ERROR) << "emplace: " << mode << " fail";
40 }
41 }
42
FactoryResetFunc(FactoryResetMode mode, const std::string &path)43 int FactoryResetProcess::FactoryResetFunc(FactoryResetMode mode, const std::string &path)
44 {
45 auto iter = resetTab_.find(mode);
46 if (iter == resetTab_.end() || iter->second == nullptr) {
47 LOG(ERROR) << "Invalid factory reset tag: " << mode;
48 return 1;
49 }
50 int resetStatus = iter->second(path);
51 ON_SCOPE_EXIT(factoryResetPost) {
52 if (mode == FACTORY_WIPE_DATA &&
53 (FactoryResetPostFunc_ == nullptr || FactoryResetPostFunc_(resetStatus) != 0)) {
54 LOG(ERROR) << "FactoryResetPostFunc_ fail";
55 }
56 };
57 if (resetStatus != 0) {
58 LOG(ERROR) << "Do factory reset failed! tag: " << mode;
59 return 1;
60 }
61 if (CommonResetPostFunc_ == nullptr || CommonResetPostFunc_(mode) != 0) {
62 resetStatus = 1;
63 LOG(ERROR) << "CommonResetPostFunc_ fail";
64 return -1;
65 }
66 return 0;
67 }
68
CommonResetPost(bool flag)69 static int CommonResetPost(bool flag)
70 {
71 LOG(INFO) << "CommonResetPost";
72 return 0;
73 }
74
RegisterCommonResetPostFunc(CommonResetPostFunc ptr)75 void FactoryResetProcess::RegisterCommonResetPostFunc(CommonResetPostFunc ptr)
76 {
77 CommonResetPostFunc_ = std::move(ptr);
78 }
79
FactoryResetPre()80 static int FactoryResetPre()
81 {
82 LOG(INFO) << "FactoryResetPre";
83 return 0;
84 }
85
RegisterFactoryResetPreFunc(FactoryResetPreFunc ptr)86 void FactoryResetProcess::RegisterFactoryResetPreFunc(FactoryResetPreFunc ptr)
87 {
88 FactoryResetPreFunc_ = std::move(ptr);
89 }
90
FactoryResetPost(int status)91 static int FactoryResetPost(int status)
92 {
93 LOG(INFO) << "FactoryResetPost";
94 return 0;
95 }
96
RegisterFactoryResetPostFunc(FactoryResetPostFunc ptr)97 void FactoryResetProcess::RegisterFactoryResetPostFunc(FactoryResetPostFunc ptr)
98 {
99 FactoryResetPostFunc_ = std::move(ptr);
100 }
101
DoUserReset(const std::string &path)102 int FactoryResetProcess::DoUserReset(const std::string &path)
103 {
104 STAGE(UPDATE_STAGE_BEGIN) << "User FactoryReset";
105 LOG(INFO) << "Begin erasing data";
106 if (FormatPartition(path, true) != 0) {
107 LOG(ERROR) << "User level FactoryReset failed";
108 STAGE(UPDATE_STAGE_FAIL) << "User FactoryReset";
109 ERROR_CODE(CODE_FACTORY_RESET_FAIL);
110 return 1;
111 }
112 LOG(INFO) << "User level FactoryReset success";
113 STAGE(UPDATE_STAGE_SUCCESS) << "User FactoryReset";
114
115 return 0;
116 }
117
DoFactoryReset(const std::string &path)118 int FactoryResetProcess::DoFactoryReset(const std::string &path)
119 {
120 int resetStatus = 0;
121 STAGE(UPDATE_STAGE_BEGIN) << "Factory FactoryReset";
122 if (FactoryResetPreFunc_ == nullptr || FactoryResetPreFunc_() != 0) {
123 LOG(ERROR) << "FactoryResetPreFunc_ fail";
124 }
125 LOG(INFO) << "Begin erasing data";
126 if (FormatPartition(path, true) != 0) {
127 STAGE(UPDATE_STAGE_FAIL) << "Factory FactoryReset";
128 ERROR_CODE(CODE_FACTORY_RESET_FAIL);
129 resetStatus = 1;
130 }
131
132 LOG(INFO) << "Factory level FactoryReset status:" << resetStatus;
133 return resetStatus;
134 }
135
RegisterCommonResetPostFunc(void)136 extern "C" __attribute__((constructor)) void RegisterCommonResetPostFunc(void)
137 {
138 FactoryResetProcess::GetInstance().RegisterCommonResetPostFunc(CommonResetPost);
139 }
140
RegisterFactoryResetPreFunc(void)141 extern "C" __attribute__((constructor)) void RegisterFactoryResetPreFunc(void)
142 {
143 FactoryResetProcess::GetInstance().RegisterFactoryResetPreFunc(FactoryResetPre);
144 }
145
RegisterFactoryResetPostFunc(void)146 extern "C" __attribute__((constructor)) void RegisterFactoryResetPostFunc(void)
147 {
148 FactoryResetProcess::GetInstance().RegisterFactoryResetPostFunc(FactoryResetPost);
149 }
150 } // Updater