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_PARTITION_H 17 #define FLASHING_PARTITION_H 18 #include <cerrno> 19 #include <cstdlib> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 #include "blockdevice.h" 24 #include "flash_utils.h" 25 26 namespace flashd { 27 enum class PartitionType { 28 NORMAL, 29 LOGICAL, 30 EXTENDED, 31 }; 32 33 class Partition { 34 public: Partition(const std::string & name,const std::string & path,BlockDevicePtr device,FlashingPtr flash)35 Partition(const std::string &name, const std::string &path, BlockDevicePtr device, FlashingPtr flash) 36 : partPath_(path), devName_(name), device_(device), flash_(flash) {} 37 ~Partition(); 38 39 int Load(); 40 int DoFlash(const std::string &fileName); 41 int DoFormat(const std::string &fsType); 42 int DoErase(); 43 int DoResize(uint32_t blocks); 44 45 bool IsOnlyErase() const; 46 const std::string GetPartitionName() const; GetPartitionType()47 PartitionType GetPartitionType() const 48 { 49 return type_; 50 } GetPartitionPath()51 const std::string GetPartitionPath() const 52 { 53 return partPath_; 54 } 55 private: 56 std::string ReadPartitionSysInfo(const std::string &partition, 57 const std::string &type, std::vector<std::string> &table); 58 int Open(); 59 int WriteRowData(int inputFd, size_t fileSize, std::vector<uint8_t> &buffer, size_t dataSize); 60 int IsBlockDevice(int fd) const; 61 int GetMountInfo(); 62 int DoUmount(); 63 int DoMount(); 64 int BuildCommandParam(const std::string &fsType, std::vector<std::string> &formatCmds) const; 65 uint64_t GetBlockDeviceSize(int fd) const; 66 uint32_t GetMountFlags(const std::string &mountFlagsStr, std::string &data) const; 67 68 int fd_ = -1; 69 int partNumber_ { 0 }; // Partition number. 70 std::string partPath_ {}; 71 std::string devName_ {}; // partition DEVNAME 72 std::string partName_ {}; // partition PARTN 73 PartitionType type_ { PartitionType::NORMAL }; 74 std::string fsType_ {}; // File system type, ext4, f2fs etc. 75 std::string mountFlags_ {}; // flags:rw,relatime,fmask=0077,dmask=0077 76 std::string mountPoint_ {}; 77 BlockDevicePtr device_ = nullptr; 78 FlashingPtr flash_ = nullptr; 79 }; 80 using PartitionPtr = Partition *; 81 } 82 #endif // FLASHING_PARTITION_H