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