1 /*
2  * Copyright (c) 2022 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 
16 #include "update_service_restorer.h"
17 
18 #ifndef UPDATER_FUZZ
19 #include "fs_manager/mount.h"
20 #endif
21 #include "updaterkits/updaterkits.h"
22 
23 #include "update_define.h"
24 #include "update_log.h"
25 #include "update_system_event.h"
26 
27 namespace OHOS {
28 namespace UpdateEngine {
29 const std::string MISC_PATH = "/misc";
30 const std::string MISC_FILE = "/dev/block/by-name/misc";
31 const std::string CMD_WIPE_DATA = "--user_wipe_data";
32 
FactoryReset(BusinessError & businessError)33 int32_t UpdateServiceRestorer::FactoryReset(BusinessError &businessError)
34 {
35 #ifndef UPDATER_UT
36     businessError.errorNum = CallResult::SUCCESS;
37     auto miscBlockDev = Updater::GetBlockDeviceByMountPoint(MISC_PATH);
38     ENGINE_LOGI("FactoryReset::misc path : %{public}s", miscBlockDev.c_str());
39     ENGINE_CHECK(!miscBlockDev.empty(), miscBlockDev = MISC_FILE, "cannot get block device of partition");
40     int32_t ret = RebootAndCleanUserData(miscBlockDev, CMD_WIPE_DATA) ? INT_CALL_SUCCESS : INT_CALL_FAIL;
41     ENGINE_LOGI("FactoryReset result : %{public}d", ret);
42     SYS_EVENT_SYSTEM_RESET(
43         0, ret == INT_CALL_SUCCESS ? UpdateSystemEvent::EVENT_SUCCESS_RESULT : UpdateSystemEvent::EVENT_FAILED_RESULT);
44     return ret;
45 #else
46     return INT_CALL_SUCCESS;
47 #endif
48 }
49 } // namespace UpdateEngine
50 } // namespace OHOS
51