2021-12-12 00:42:12 +01:00
|
|
|
#include "content/providers/disk_provider.hpp"
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/localization.hpp>
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-02-27 23:25:39 +01:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2022-03-27 00:01:28 +01:00
|
|
|
#include <hex/ui/imgui_imhex_extensions.h>
|
2022-02-27 23:25:39 +01:00
|
|
|
|
2021-12-12 00:42:12 +01:00
|
|
|
#include <bitset>
|
|
|
|
#include <filesystem>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <imgui.h>
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_LINUX)
|
2021-12-12 13:42:01 +01:00
|
|
|
#include <fcntl.h>
|
2021-12-12 01:06:24 +01:00
|
|
|
#include <unistd.h>
|
2021-12-12 13:42:01 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2021-12-12 01:06:24 +01:00
|
|
|
|
|
|
|
#define lseek lseek64
|
2022-01-24 20:53:17 +01:00
|
|
|
#elif defined(OS_MACOS)
|
2021-12-12 13:42:01 +01:00
|
|
|
#include <fcntl.h>
|
2021-12-12 00:42:12 +01:00
|
|
|
#include <unistd.h>
|
2021-12-12 13:42:01 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2021-12-12 00:42:12 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace hex::plugin::builtin::prv {
|
|
|
|
|
|
|
|
DiskProvider::DiskProvider() : Provider() {
|
2021-12-12 11:56:26 +01:00
|
|
|
this->reloadDrives();
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DiskProvider::~DiskProvider() {
|
|
|
|
this->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskProvider::isAvailable() const {
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
return this->m_diskHandle != INVALID_HANDLE_VALUE;
|
|
|
|
#else
|
|
|
|
return this->m_diskHandle != -1;
|
|
|
|
#endif
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskProvider::isReadable() const {
|
|
|
|
return this->m_readable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskProvider::isWritable() const {
|
|
|
|
return this->m_writable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskProvider::isResizable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskProvider::isSavable() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-04 11:36:37 +01:00
|
|
|
void DiskProvider::setPath(const std::fs::path &path) {
|
2021-12-12 00:42:12 +01:00
|
|
|
this->m_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskProvider::open() {
|
|
|
|
this->m_readable = true;
|
|
|
|
this->m_writable = true;
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const auto &path = this->m_path.native();
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_diskHandle = reinterpret_cast<HANDLE>(CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
|
|
|
|
if (this->m_diskHandle == INVALID_HANDLE_VALUE) {
|
|
|
|
this->m_diskHandle = reinterpret_cast<HANDLE>(CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
|
2022-02-01 22:09:44 +01:00
|
|
|
this->m_writable = false;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (this->m_diskHandle == INVALID_HANDLE_VALUE)
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
{
|
2022-03-27 00:01:28 +01:00
|
|
|
DISK_GEOMETRY_EX diskGeometry = { };
|
2022-02-01 22:09:44 +01:00
|
|
|
DWORD bytesRead = 0;
|
2022-01-24 20:53:17 +01:00
|
|
|
if (DeviceIoControl(
|
|
|
|
this->m_diskHandle,
|
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
&diskGeometry,
|
|
|
|
sizeof(DISK_GEOMETRY_EX),
|
|
|
|
&bytesRead,
|
|
|
|
nullptr)) {
|
2022-02-01 22:09:44 +01:00
|
|
|
this->m_diskSize = diskGeometry.DiskSize.QuadPart;
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_sectorSize = diskGeometry.Geometry.BytesPerSector;
|
|
|
|
this->m_sectorBuffer.resize(this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (this->m_diskHandle == nullptr || this->m_diskHandle == INVALID_HANDLE_VALUE) {
|
2022-02-01 22:09:44 +01:00
|
|
|
this->m_readable = false;
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_diskHandle = nullptr;
|
|
|
|
CloseHandle(this->m_diskHandle);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#else
|
|
|
|
const auto &path = this->m_path.native();
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
struct stat driveStat;
|
2022-01-16 02:29:19 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
if (::stat(path.c_str(), &driveStat) == 0)
|
|
|
|
this->m_diskSize = driveStat.st_size;
|
|
|
|
else
|
|
|
|
this->m_diskSize = 0;
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_sectorSize = 0;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_diskHandle = ::open(path.c_str(), O_RDWR);
|
|
|
|
if (this->m_diskHandle == -1) {
|
|
|
|
this->m_diskHandle = ::open(path.c_str(), O_RDONLY);
|
2022-02-01 22:09:44 +01:00
|
|
|
this->m_writable = false;
|
2022-01-24 20:53:17 +01:00
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (this->m_diskHandle == -1) {
|
|
|
|
this->m_readable = false;
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#endif
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-04-17 16:57:30 +02:00
|
|
|
Provider::resize(this->getActualSize());
|
|
|
|
|
2021-12-12 00:42:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiskProvider::close() {
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
if (this->m_diskHandle != INVALID_HANDLE_VALUE)
|
|
|
|
::CloseHandle(this->m_diskHandle);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_diskHandle = INVALID_HANDLE_VALUE;
|
|
|
|
#else
|
|
|
|
if (this->m_diskHandle != -1)
|
|
|
|
::close(this->m_diskHandle);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_diskHandle = -1;
|
|
|
|
#endif
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiskProvider::readRaw(u64 offset, void *buffer, size_t size) {
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
DWORD bytesRead = 0;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
u64 startOffset = offset;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
while (size > 0) {
|
|
|
|
LARGE_INTEGER seekPosition;
|
2022-02-01 22:09:44 +01:00
|
|
|
seekPosition.LowPart = (offset & 0xFFFF'FFFF) - (offset % this->m_sectorSize);
|
2022-01-24 20:53:17 +01:00
|
|
|
seekPosition.HighPart = offset >> 32;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
if (this->m_sectorBufferAddress != static_cast<u64>(seekPosition.QuadPart)) {
|
2022-01-24 20:53:17 +01:00
|
|
|
::SetFilePointer(this->m_diskHandle, seekPosition.LowPart, &seekPosition.HighPart, FILE_BEGIN);
|
|
|
|
::ReadFile(this->m_diskHandle, this->m_sectorBuffer.data(), this->m_sectorBuffer.size(), &bytesRead, nullptr);
|
|
|
|
this->m_sectorBufferAddress = seekPosition.QuadPart;
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::memcpy(reinterpret_cast<u8 *>(buffer) + (offset - startOffset), this->m_sectorBuffer.data() + (offset & (this->m_sectorSize - 1)), std::min(this->m_sectorSize, size));
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
size = std::max<ssize_t>(static_cast<ssize_t>(size) - this->m_sectorSize, 0);
|
|
|
|
offset += this->m_sectorSize;
|
|
|
|
}
|
|
|
|
#else
|
2022-02-01 22:09:44 +01:00
|
|
|
u64 startOffset = offset;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
while (size > 0) {
|
|
|
|
u64 seekPosition = offset - (offset % this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (this->m_sectorBufferAddress != seekPosition) {
|
|
|
|
::lseek(this->m_diskHandle, seekPosition, SEEK_SET);
|
2022-03-27 00:01:28 +01:00
|
|
|
if (::read(this->m_diskHandle, buffer, size) < 0)
|
|
|
|
break;
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_sectorBufferAddress = seekPosition;
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
|
|
|
|
std::memcpy(reinterpret_cast<u8 *>(buffer) + (offset - startOffset), this->m_sectorBuffer.data() + (offset & (this->m_sectorSize - 1)), std::min(this->m_sectorSize, size));
|
|
|
|
|
|
|
|
size = std::max<ssize_t>(static_cast<ssize_t>(size) - this->m_sectorSize, 0);
|
|
|
|
offset += this->m_sectorSize;
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiskProvider::writeRaw(u64 offset, const void *buffer, size_t size) {
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
DWORD bytesWritten = 0;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
u64 startOffset = offset;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::vector<u8> modifiedSectorBuffer;
|
|
|
|
modifiedSectorBuffer.resize(this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
while (size > 0) {
|
2022-02-01 22:09:44 +01:00
|
|
|
u64 sectorBase = offset - (offset % this->m_sectorSize);
|
2022-01-24 20:53:17 +01:00
|
|
|
size_t currSize = std::min(size, this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
this->readRaw(sectorBase, modifiedSectorBuffer.data(), modifiedSectorBuffer.size());
|
|
|
|
std::memcpy(modifiedSectorBuffer.data() + ((offset - sectorBase) % this->m_sectorSize), reinterpret_cast<const u8 *>(buffer) + (startOffset - offset), currSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
LARGE_INTEGER seekPosition;
|
2022-02-01 22:09:44 +01:00
|
|
|
seekPosition.LowPart = (offset & 0xFFFF'FFFF) - (offset % this->m_sectorSize);
|
2022-01-24 20:53:17 +01:00
|
|
|
seekPosition.HighPart = offset >> 32;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
::SetFilePointer(this->m_diskHandle, seekPosition.LowPart, &seekPosition.HighPart, FILE_BEGIN);
|
|
|
|
::WriteFile(this->m_diskHandle, modifiedSectorBuffer.data(), modifiedSectorBuffer.size(), &bytesWritten, nullptr);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
offset += currSize;
|
|
|
|
size -= currSize;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#else
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
u64 startOffset = offset;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::vector<u8> modifiedSectorBuffer;
|
|
|
|
modifiedSectorBuffer.resize(this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
while (size > 0) {
|
2022-02-01 22:09:44 +01:00
|
|
|
u64 sectorBase = offset - (offset % this->m_sectorSize);
|
2022-01-24 20:53:17 +01:00
|
|
|
size_t currSize = std::min(size, this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
this->readRaw(sectorBase, modifiedSectorBuffer.data(), modifiedSectorBuffer.size());
|
|
|
|
std::memcpy(modifiedSectorBuffer.data() + ((offset - sectorBase) % this->m_sectorSize), reinterpret_cast<const u8 *>(buffer) + (startOffset - offset), currSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
::lseek(this->m_diskHandle, sectorBase, SEEK_SET);
|
2022-03-27 00:01:28 +01:00
|
|
|
if (::write(this->m_diskHandle, modifiedSectorBuffer.data(), modifiedSectorBuffer.size()) < 0)
|
|
|
|
break;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
offset += currSize;
|
|
|
|
size -= currSize;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#endif
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t DiskProvider::getActualSize() const {
|
|
|
|
return this->m_diskSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DiskProvider::getName() const {
|
2022-01-16 01:51:31 +01:00
|
|
|
return this->m_path.string();
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::string>> DiskProvider::getDataInformation() const {
|
|
|
|
return {
|
2022-01-24 20:53:17 +01:00
|
|
|
{"hex.builtin.provider.disk.selected_disk"_lang, this->m_path.string() },
|
|
|
|
{ "hex.builtin.provider.disk.disk_size"_lang, hex::toByteString(this->m_diskSize) },
|
|
|
|
{ "hex.builtin.provider.disk.sector_size"_lang, hex::toByteString(this->m_sectorSize)}
|
2021-12-12 00:42:12 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-12 11:56:26 +01:00
|
|
|
void DiskProvider::reloadDrives() {
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
this->m_availableDrives.clear();
|
|
|
|
std::bitset<32> drives = ::GetLogicalDrives();
|
|
|
|
for (char i = 0; i < 26; i++) {
|
|
|
|
if (drives[i])
|
|
|
|
this->m_availableDrives.insert(hex::format(R"(\\.\{:c}:)", 'A' + i));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto logicalDrives = this->m_availableDrives;
|
|
|
|
for (const auto &drive : logicalDrives) {
|
|
|
|
auto handle = reinterpret_cast<HANDLE>(::CreateFile(drive.data(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr));
|
2021-12-12 11:56:26 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (handle == INVALID_HANDLE_VALUE) continue;
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
VOLUME_DISK_EXTENTS diskExtents = { };
|
2022-02-01 22:09:44 +01:00
|
|
|
DWORD bytesRead = 0;
|
|
|
|
auto result = ::DeviceIoControl(
|
2022-01-24 20:53:17 +01:00
|
|
|
handle,
|
|
|
|
IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
&diskExtents,
|
|
|
|
sizeof(VOLUME_DISK_EXTENTS),
|
|
|
|
&bytesRead,
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
auto diskPath = hex::format(R"(\\.\PhysicalDrive{})", diskExtents.Extents[0].DiskNumber);
|
|
|
|
this->m_availableDrives.insert(diskPath);
|
2021-12-12 11:56:26 +01:00
|
|
|
}
|
2022-01-24 20:53:17 +01:00
|
|
|
|
|
|
|
::CloseHandle(handle);
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-12 11:56:26 +01:00
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
|
|
|
void DiskProvider::drawLoadInterface() {
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
if (ImGui::BeginListBox("hex.builtin.provider.disk.selected_disk"_lang)) {
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
for (const auto &drive : this->m_availableDrives) {
|
|
|
|
if (ImGui::Selectable(drive.c_str(), this->m_path == drive))
|
|
|
|
this->m_path = drive;
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
ImGui::EndListBox();
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
ImGui::SameLine();
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
if (ImGui::Button("hex.builtin.provider.disk.reload"_lang)) {
|
|
|
|
this->reloadDrives();
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#else
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
if (ImGui::InputText("hex.builtin.provider.disk.selected_disk"_lang, this->m_pathBuffer.data(), this->m_pathBuffer.size(), ImGuiInputTextFlags_CallbackResize, ImGui::UpdateStringSizeCallback, &this->m_pathBuffer))
|
2022-01-24 20:53:17 +01:00
|
|
|
this->m_path = this->m_pathBuffer;
|
|
|
|
|
|
|
|
#endif
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|