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 "format_commander.h" 17 18#include "flashd_define.h" 19#include "flashd_utils.h" 20#include "fs_manager/mount.h" 21#include "updater/updater.h" 22 23namespace Flashd { 24namespace { 25const std::vector<std::string> ERASE_ONLY_LIST = { "boot", "fastboot", "kernel", "misc", "system" }; 26constexpr size_t CMD_PARAM_COUNT_MIN = 2; 27} 28 29void FormatCommander::DoCommand([[maybe_unused]] const std::string &cmmParam, [[maybe_unused]] size_t fileSize) 30{ 31 FLASHD_LOGI("unsupport"); 32} 33 34void FormatCommander::DoCommand(const uint8_t *payload, int payloadSize) 35{ 36 FLASHD_LOGI("start to format"); 37 if (payload == nullptr || payloadSize <= 0) { 38 NotifyFail(CmdType::FORMAT); 39 FLASHD_LOGE("payload is null or payloadSize is invaild"); 40 return; 41 } 42 43 std::string cmdParam(reinterpret_cast<const char *>(payload), payloadSize); 44 auto params = Split(cmdParam, { "-f" }); 45 if (params.size() < CMD_PARAM_COUNT_MIN) { 46 NotifyFail(CmdType::FORMAT); 47 FLASHD_LOGE("param is invaild"); 48 return; 49 } 50 51 if (auto ret = DoFormat(params[1]); ret != 0) { 52 NotifyFail(CmdType::FORMAT); 53 FLASHD_LOGE("DoFormat fail, ret = %d", ret); 54 return; 55 } 56 57 NotifySuccess(CmdType::FORMAT); 58 FLASHD_LOGI("format success"); 59} 60 61bool FormatCommander::IsOnlySupportErase(const std::string &partitionName) const 62{ 63 auto iter = std::find(ERASE_ONLY_LIST.begin(), ERASE_ONLY_LIST.end(), partitionName); 64 return iter != ERASE_ONLY_LIST.end(); 65} 66 67int FormatCommander::DoFormat(const std::string &partitionName) const 68{ 69 FLASHD_LOGI("start to format %s", partitionName.c_str()); 70 Partition part(partitionName); 71 if (IsOnlySupportErase(partitionName)) { 72 return part.DoErase(); 73 } 74 return part.DoFormat(); 75} 76 77void FormatCommander::PostCommand() 78{ 79 Updater::PostUpdater(false); 80} 81} // namespace Flashd