1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (C) 2022 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
16fb299fa2Sopenharmony_ci#include "erase_commander.h"
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include "flashd_define.h"
19fb299fa2Sopenharmony_ci#include "flashd_utils.h"
20fb299fa2Sopenharmony_ci#include "updater/updater.h"
21fb299fa2Sopenharmony_ci
22fb299fa2Sopenharmony_ci
23fb299fa2Sopenharmony_cinamespace Flashd {
24fb299fa2Sopenharmony_cinamespace {
25fb299fa2Sopenharmony_ciconstexpr size_t CMD_PARAM_COUNT_MIN = 2;
26fb299fa2Sopenharmony_ci}
27fb299fa2Sopenharmony_ci
28fb299fa2Sopenharmony_civoid EraseCommander::DoCommand([[maybe_unused]] const std::string &cmdParam, [[maybe_unused]] size_t fileSize)
29fb299fa2Sopenharmony_ci{
30fb299fa2Sopenharmony_ci    FLASHD_LOGI("unsupport");
31fb299fa2Sopenharmony_ci}
32fb299fa2Sopenharmony_ci
33fb299fa2Sopenharmony_civoid EraseCommander::DoCommand(const uint8_t *payload, int payloadSize)
34fb299fa2Sopenharmony_ci{
35fb299fa2Sopenharmony_ci    FLASHD_LOGI("start to erase");
36fb299fa2Sopenharmony_ci    if (payload == nullptr || payloadSize <= 0) {
37fb299fa2Sopenharmony_ci        NotifyFail(CmdType::ERASE);
38fb299fa2Sopenharmony_ci        FLASHD_LOGI("payload is null or payloadSize is invaild");
39fb299fa2Sopenharmony_ci        return;
40fb299fa2Sopenharmony_ci    }
41fb299fa2Sopenharmony_ci
42fb299fa2Sopenharmony_ci    std::string cmdParam(reinterpret_cast<const char *>(payload), payloadSize);
43fb299fa2Sopenharmony_ci    auto params = Split(cmdParam, { "-f" });
44fb299fa2Sopenharmony_ci    if (params.size() < CMD_PARAM_COUNT_MIN) {
45fb299fa2Sopenharmony_ci        NotifyFail(CmdType::ERASE);
46fb299fa2Sopenharmony_ci        FLASHD_LOGE("param is invaild");
47fb299fa2Sopenharmony_ci        return;
48fb299fa2Sopenharmony_ci    }
49fb299fa2Sopenharmony_ci    if (!DoErase(params[1])) {
50fb299fa2Sopenharmony_ci        NotifyFail(CmdType::ERASE);
51fb299fa2Sopenharmony_ci        return;
52fb299fa2Sopenharmony_ci    }
53fb299fa2Sopenharmony_ci    NotifySuccess(CmdType::ERASE);
54fb299fa2Sopenharmony_ci}
55fb299fa2Sopenharmony_ci
56fb299fa2Sopenharmony_cibool EraseCommander::DoErase(const std::string &partitionName) const
57fb299fa2Sopenharmony_ci{
58fb299fa2Sopenharmony_ci    FLASHD_LOGI("start to erase %s", partitionName.c_str());
59fb299fa2Sopenharmony_ci    Partition part(partitionName);
60fb299fa2Sopenharmony_ci    if (auto ret = part.DoErase(); ret != 0) {
61fb299fa2Sopenharmony_ci        FLASHD_LOGE("DoErase fail, ret = %d", ret);
62fb299fa2Sopenharmony_ci        return false;
63fb299fa2Sopenharmony_ci    }
64fb299fa2Sopenharmony_ci    return true;
65fb299fa2Sopenharmony_ci}
66fb299fa2Sopenharmony_ci
67fb299fa2Sopenharmony_civoid EraseCommander::PostCommand()
68fb299fa2Sopenharmony_ci{
69fb299fa2Sopenharmony_ci    Updater::PostUpdater(false);
70fb299fa2Sopenharmony_ci}
71fb299fa2Sopenharmony_ci} // namespace Flashd