diff --git a/services/flashd/partition.cpp b/services/flashd/partition.cpp index 7519f001007858acc004bfa2bdb93de0ea7c79e6..63da3f5a07b13ea1e6999c350615c3a5df2ab355 100644 --- a/services/flashd/partition.cpp +++ b/services/flashd/partition.cpp @@ -23,6 +23,8 @@ #include "flashd_utils.h" #include "fs_manager/mount.h" #include "utils.h" +#include "ptable_manager.h" +#include "applypatch/data_writer.h" namespace Flashd { int Partition::DoFlash(const uint8_t *buffer, int bufferSize) const @@ -40,6 +42,50 @@ int Partition::DoFlash(const uint8_t *buffer, int bufferSize) const } int Partition::DoErase() const +{ + std::string writeMode = "WRITE_RAW"; + uint64_t partitionSize = 0; +#ifdef UPDATER_USE_PTABLE + DevicePtable::GetInstance().LoadPartitionInfo(); + DevicePtable &devicePtb = DevicePtable::GetInstance(); + Ptable::PtnInfo ptnInfo; + if (!devicePtb.GetPartionInfoByName(devName_, ptnInfo)) { + FLASHD_LOGE("DoErase: cannot find the lun index of partition: %s", devName.c_str()); + return -1; + } + writeMode = ptnInfo.writeMode; + partitionSize = ptnInfo.partitionSize; + std::unique_ptr dataWriter = DataWriter::CreateDataWriter(writeMode, + ptnInfo.writePath, devName_, ptnInfo.startAddr); +#else + std::unique_ptr dataWriter = DataWriter::CreateDataWriter(WRITE_RAW, + GetBlockDeviceByMountPoint(partition)); +#endif +<<<<<<< HEAD +======= + if (!dataWriter) { + FLASHD_LOGE("DataWriter creation failed"); + return -1; + } +>>>>>>> 377e7c59 (flashd erase norflash partition) + if (writeMode != "WRITE_RAW") { + FLASHD_LOGI("erase norflash partition"); + std::vector zeroBuffer(partitionSize, 0); + if (!dataWriter->Write(zeroBuffer.data(), partitionSize, nullptr)) { + FLASHD_LOGE("erase %s failed", devName_.c_str()); + return -1; + } + } else { + FLASHD_LOGI("erase raw partition"); + if (auto ret = EraseRawImage(); ret != 0) { + FLASHD_LOGE("erase %s failed", devName_.c_str()); + return -1; + } + } + return 0; +} + +int Partition::EraseRawImage() const { auto fd = open(Updater::Utils::GetPartitionRealPath(devName_).c_str(), O_RDWR); if (fd < 0) { diff --git a/services/flashd/partition.h b/services/flashd/partition.h index 1bd3846d086fce9b24d29ad5e82df086f2980d5a..b9f9bec7d92c057476f102796d9673c7437a5da9 100644 --- a/services/flashd/partition.h +++ b/services/flashd/partition.h @@ -30,6 +30,7 @@ public: int DoFlash(const uint8_t *buffer, int bufferSize) const; int DoFormat() const; int DoErase() const; + int EraseRawImage() const; private: uint64_t GetBlockDeviceSize(int fd) const;