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
CommonResetPost(bool flag)31 static int CommonResetPost(bool flag)
32 {
33 LOG(INFO) << "CommonResetPost";
34 return 0;
35 }
36
RegisterCommonResetPostFunc(CommonResetPostFunc ptr)37 void FactoryResetProcess::RegisterCommonResetPostFunc(CommonResetPostFunc ptr)
38 {
39 CommonResetPostFunc_ = std::move(ptr);
40 }
41
FactoryResetPre(FactoryResetMode mode)42 static int FactoryResetPre(FactoryResetMode mode)
43 {
44 LOG(INFO) << "FactoryResetPre";
45 return 0;
46 }
47
RegisterFactoryResetPreFunc(FactoryResetPreFunc ptr)48 void FactoryResetProcess::RegisterFactoryResetPreFunc(FactoryResetPreFunc ptr)
49 {
50 FactoryResetPreFunc_ = std::move(ptr);
51 }
52
FactoryResetPost(FactoryResetMode mode,int status)53 static int FactoryResetPost(FactoryResetMode mode, int status)
54 {
55 LOG(INFO) << "FactoryResetPost";
56 return 0;
57 }
58
RegisterFactoryResetPostFunc(FactoryResetPostFunc ptr)59 void FactoryResetProcess::RegisterFactoryResetPostFunc(FactoryResetPostFunc ptr)
60 {
61 FactoryResetPostFunc_ = std::move(ptr);
62 }
63
DoFactoryReset(FactoryResetMode mode,const std::string & path)64 int FactoryResetProcess::DoFactoryReset(FactoryResetMode mode, const std::string &path)
65 {
66 int resetStatus = 0;
67 STAGE(UPDATE_STAGE_BEGIN) << "Factory FactoryReset";
68 if (FactoryResetPreFunc_ == nullptr || FactoryResetPreFunc_(mode) != 0) {
69 LOG(ERROR) << "FactoryResetPreFunc_ fail";
70 return -1;
71 }
72 LOG(INFO) << "Begin erasing data";
73 if (FormatPartition(path, true) != 0) {
74 STAGE(UPDATE_STAGE_FAIL) << "Factory FactoryReset";
75 ERROR_CODE(CODE_FACTORY_RESET_FAIL);
76 resetStatus = 1;
77 }
78 if (resetStatus == 0 && (CommonResetPostFunc_ == nullptr || CommonResetPostFunc_(mode) != 0)) {
79 LOG(ERROR) << "CommonResetPostFunc_ fail";
80 resetStatus = -1;
81 }
82 if (FactoryResetPostFunc_ == nullptr || FactoryResetPostFunc_(mode, resetStatus) != 0) {
83 LOG(ERROR) << "FactoryResetPostFunc_ fail";
84 return -1;
85 }
86
87 LOG(INFO) << "Factory level FactoryReset status:" << resetStatus;
88 return resetStatus;
89 }
90
RegisterCommonResetPostFunc(void)91 extern "C" __attribute__((constructor)) void RegisterCommonResetPostFunc(void)
92 {
93 FactoryResetProcess::GetInstance().RegisterCommonResetPostFunc(CommonResetPost);
94 }
95
RegisterFactoryResetPreFunc(void)96 extern "C" __attribute__((constructor)) void RegisterFactoryResetPreFunc(void)
97 {
98 FactoryResetProcess::GetInstance().RegisterFactoryResetPreFunc(FactoryResetPre);
99 }
100
RegisterFactoryResetPostFunc(void)101 extern "C" __attribute__((constructor)) void RegisterFactoryResetPostFunc(void)
102 {
103 FactoryResetProcess::GetInstance().RegisterFactoryResetPostFunc(FactoryResetPost);
104 }
105 } // Updater