1 /* 2 * Copyright (c) 2021 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 #ifndef FLASHING_H 17 #define FLASHING_H 18 #include <cstdlib> 19 #include <memory> 20 #include <fcntl.h> 21 #include <vector> 22 #include "blockdevice.h" 23 #include "partition.h" 24 #include "updater/updater.h" 25 26 // Just update-mode use 27 namespace flashd { 28 class FlashService { 29 public: FlashService(std::string & errorMsg,ProgressFunction progressor)30 FlashService(std::string &errorMsg, ProgressFunction progressor) 31 : errorMsg_(errorMsg), progressor_(progressor) {} FlashService(std::string & errorMsg)32 explicit FlashService(std::string &errorMsg) : errorMsg_(errorMsg), progressor_(nullptr) {} 33 ~FlashService(); 34 35 int DoUpdate(const std::string &packageName); 36 int DoFlashPartition(const std::string &fileName, const std::string &partition); 37 int DoErasePartition(const std::string &partition); 38 int DoFormatPartition(const std::string &partition, const std::string &fsType); 39 int GetPartitionPath(const std::string &partition, std::string ¶titionPath); 40 int DoResizeParatiton(const std::string &partition, uint32_t blocks); 41 42 int LoadSysDevice(); 43 PartitionPtr GetPartition(const std::string &partition) const; 44 45 void PostProgress(uint32_t type, size_t dataLen, const void *context) const; 46 void RecordMsg(uint8_t level, const char *msg, ...); GetErrorLevel()47 uint8_t GetErrorLevel() const 48 { 49 return errorLevel_; 50 } 51 52 static std::string ReadSysInfo(const std::string &path, const std::string &type, std::vector<std::string> &table); 53 static std::string GetParamFromTable(const std::vector<std::string> &table, const std::string ¶m); 54 static int ExecCommand(const std::vector<std::string> &cmds); 55 56 static const std::string GetBaseName(const std::string &path); 57 static const std::string GetPathRoot(const std::string &path); 58 static const std::string GetRealPath(const std::string &path); 59 static const std::string GetPartNameByAlias(const std::string &alias); 60 static bool CheckFreeSpace(const std::string &root, uint32_t blocks); 61 private: 62 int LoadBlockDevice(const std::string &fileDir); 63 int LoadPartition(std::vector<std::string> &partitionsNames); 64 65 int AddNewBlockDevice(DeviceType type, const std::string &devPath); 66 int AddNewPartition(const std::string &path, BlockDevicePtr device); 67 68 int CheckOperationPermission(int type, const std::string &partition) const; 69 std::string &errorMsg_; 70 ProgressFunction progressor_; 71 uint8_t errorLevel_ = 0; 72 bool loadSysDevice_ = false; 73 std::vector<BlockDevicePtr> blockDevices_; 74 std::vector<PartitionPtr> partitions_; 75 }; 76 } 77 #endif // FLASHING_H