2023-10-04 12:00:32 +02:00
|
|
|
#if !defined(OS_WEB)
|
2023-03-21 10:33:00 +01:00
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
|
2021-12-12 00:42:12 +01:00
|
|
|
#include "content/providers/disk_provider.hpp"
|
|
|
|
|
2023-11-21 14:38:01 +01:00
|
|
|
#include <hex/api/localization_manager.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
|
|
|
|
2023-03-12 18:43:05 +01:00
|
|
|
#include <wolv/utils/string.hpp>
|
|
|
|
|
2021-12-12 00:42:12 +01:00
|
|
|
#include <bitset>
|
|
|
|
#include <filesystem>
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <imgui.h>
|
|
|
|
|
2023-11-30 14:40:07 +01:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2022-10-02 20:35:30 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
2023-11-30 10:22:15 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winioctl.h>
|
|
|
|
#include <setupapi.h>
|
|
|
|
#include <cfgmgr32.h>
|
2023-07-16 18:18:41 +02:00
|
|
|
#elif defined(OS_LINUX)
|
2023-11-30 10:22:15 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
2023-07-16 18:18:41 +02:00
|
|
|
#elif defined(OS_MACOS)
|
2023-11-30 10:22:15 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/disk.h>
|
2022-10-02 20:35:30 +02:00
|
|
|
#endif
|
2021-12-12 01:06:24 +01:00
|
|
|
|
2022-10-02 20:35:30 +02:00
|
|
|
#if defined(OS_LINUX)
|
2023-07-16 18:18:41 +02:00
|
|
|
#define lseek lseek64
|
2021-12-12 00:42:12 +01:00
|
|
|
#endif
|
|
|
|
|
2022-11-07 00:04:47 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2021-12-12 00:42:12 +01:00
|
|
|
|
|
|
|
bool DiskProvider::isAvailable() const {
|
2023-07-16 18:18:41 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
return this->m_diskHandle != INVALID_HANDLE_VALUE;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#else
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
return this->m_diskHandle != -1;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#if defined (OS_LINUX)
|
|
|
|
#ifdef BLKSSZGET
|
|
|
|
int blkdev_get_sector_size(int fd, int *sector_size) {
|
|
|
|
if (ioctl(fd, BLKSSZGET, sector_size) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int blkdev_get_sector_size(int fd, int *sector_size) {
|
|
|
|
(void)fd;
|
|
|
|
*sector_size = DEFAULT_SECTOR_SIZE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef BLKGETSIZE64
|
|
|
|
int blkdev_get_size(int fd, u64 *bytes) {
|
|
|
|
if (ioctl(fd, BLKGETSIZE64, bytes) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int blkdev_get_size(int fd, u64 *bytes) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (st.st_size == 0) {
|
2023-10-04 12:00:32 +02:00
|
|
|
// Try BLKGETSIZE
|
2023-07-16 18:18:41 +02:00
|
|
|
unsigned long long bytes64;
|
|
|
|
if (ioctl(fd, BLKGETSIZE, &bytes64) >= 0) {
|
|
|
|
*bytes = bytes64;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*bytes = st.st_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#elif defined(OS_MACOS)
|
|
|
|
int blkdev_get_sector_size(int fd, int *sector_size) {
|
|
|
|
if (ioctl(fd, DKIOCGETBLOCKSIZE, sector_size) >= 0)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int blkdev_get_size(int fd, u64 *bytes) {
|
|
|
|
int sectorSize = 0;
|
|
|
|
if (blkdev_get_sector_size(fd, §orSize) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (ioctl(fd, DKIOCGETBLOCKCOUNT, bytes) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*bytes *= sectorSize;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-12 00:42:12 +01:00
|
|
|
bool DiskProvider::open() {
|
|
|
|
this->m_readable = true;
|
|
|
|
this->m_writable = true;
|
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
const auto &path = this->m_path.native();
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
this->m_diskHandle = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
2022-08-08 21:23:52 +02:00
|
|
|
if (this->m_diskHandle == INVALID_HANDLE_VALUE) {
|
2023-11-10 20:47:08 +01:00
|
|
|
this->m_diskHandle = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
2022-08-08 21:23:52 +02:00
|
|
|
this->m_writable = false;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
if (this->m_diskHandle == INVALID_HANDLE_VALUE)
|
|
|
|
return false;
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
{
|
|
|
|
DISK_GEOMETRY_EX diskGeometry = { };
|
|
|
|
DWORD bytesRead = 0;
|
|
|
|
if (DeviceIoControl(
|
|
|
|
this->m_diskHandle,
|
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
&diskGeometry,
|
|
|
|
sizeof(DISK_GEOMETRY_EX),
|
|
|
|
&bytesRead,
|
|
|
|
nullptr)) {
|
|
|
|
this->m_diskSize = diskGeometry.DiskSize.QuadPart;
|
|
|
|
this->m_sectorSize = diskGeometry.Geometry.BytesPerSector;
|
|
|
|
this->m_sectorBuffer.resize(this->m_sectorSize);
|
|
|
|
}
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
if (this->m_diskHandle == nullptr || this->m_diskHandle == INVALID_HANDLE_VALUE) {
|
|
|
|
this->m_readable = false;
|
|
|
|
this->m_diskHandle = nullptr;
|
|
|
|
CloseHandle(this->m_diskHandle);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#else
|
2022-01-16 02:29:19 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
const auto &path = this->m_path.native();
|
2022-03-27 00:01:28 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
this->m_diskHandle = ::open(path.c_str(), O_RDWR);
|
|
|
|
if (this->m_diskHandle == -1) {
|
|
|
|
this->setErrorMessage(hex::format("hex.builtin.provider.disk.error.read_rw"_lang, path, ::strerror(errno)));
|
|
|
|
log::warn(this->getErrorMessage());
|
|
|
|
this->m_diskHandle = ::open(path.c_str(), O_RDONLY);
|
|
|
|
this->m_writable = false;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
if (this->m_diskHandle == -1) {
|
|
|
|
this->setErrorMessage(hex::format("hex.builtin.provider.disk.error.read_ro"_lang, path, ::strerror(errno)));
|
|
|
|
log::warn(this->getErrorMessage());
|
|
|
|
this->m_readable = false;
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
u64 diskSize = 0;
|
|
|
|
blkdev_get_size(this->m_diskHandle, &diskSize);
|
|
|
|
this->m_diskSize = diskSize;
|
|
|
|
blkdev_get_sector_size(this->m_diskHandle, reinterpret_cast<int *>(&this->m_sectorSize));
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
this->m_sectorBuffer.resize(this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#endif
|
2022-04-17 16:57:30 +02:00
|
|
|
|
2022-09-04 11:16:20 +02:00
|
|
|
return true;
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiskProvider::close() {
|
2023-07-16 18:18:41 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
if (this->m_diskHandle != INVALID_HANDLE_VALUE)
|
2022-08-08 21:23:52 +02:00
|
|
|
::CloseHandle(this->m_diskHandle);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
this->m_diskHandle = INVALID_HANDLE_VALUE;
|
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#else
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
if (this->m_diskHandle != -1)
|
|
|
|
::close(this->m_diskHandle);
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
this->m_diskHandle = -1;
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#endif
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiskProvider::readRaw(u64 offset, void *buffer, size_t size) {
|
2023-07-16 18:18:41 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
DWORD bytesRead = 0;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
u64 startOffset = offset;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
while (size > 0) {
|
|
|
|
LARGE_INTEGER seekPosition;
|
|
|
|
seekPosition.LowPart = (offset & 0xFFFF'FFFF) - (offset % this->m_sectorSize);
|
2023-11-10 20:47:08 +01:00
|
|
|
seekPosition.HighPart = LONG(offset >> 32);
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
if (this->m_sectorBufferAddress != static_cast<u64>(seekPosition.QuadPart)) {
|
|
|
|
::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;
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
std::memcpy(static_cast<u8 *>(buffer) + (offset - startOffset), this->m_sectorBuffer.data() + (offset & (this->m_sectorSize - 1)), std::min(this->m_sectorSize, size));
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
size = std::max<ssize_t>(static_cast<ssize_t>(size) - this->m_sectorSize, 0);
|
|
|
|
offset += this->m_sectorSize;
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#else
|
2021-12-12 00:42:12 +01:00
|
|
|
|
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
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
if (this->m_sectorBufferAddress != seekPosition || this->m_sectorBufferAddress == 0) {
|
2022-01-24 20:53:17 +01:00
|
|
|
::lseek(this->m_diskHandle, seekPosition, SEEK_SET);
|
2023-07-16 18:18:41 +02:00
|
|
|
if (::read(this->m_diskHandle, this->m_sectorBuffer.data(), this->m_sectorBuffer.size()) == -1)
|
2022-03-27 00:01:28 +01:00
|
|
|
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
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
std::memcpy(reinterpret_cast<u8 *>(buffer) + (offset - startOffset),
|
|
|
|
this->m_sectorBuffer.data() + (offset & (this->m_sectorSize - 1)),
|
|
|
|
std::min(this->m_sectorSize, size));
|
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;
|
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#endif
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiskProvider::writeRaw(u64 offset, const void *buffer, size_t size) {
|
2023-07-16 18:18:41 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
DWORD bytesWritten = 0;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
u64 startOffset = offset;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
std::vector<u8> modifiedSectorBuffer;
|
|
|
|
modifiedSectorBuffer.resize(this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
while (size > 0) {
|
|
|
|
u64 sectorBase = offset - (offset % this->m_sectorSize);
|
|
|
|
size_t currSize = std::min(size, this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02: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-08-08 21:23:52 +02:00
|
|
|
LARGE_INTEGER seekPosition;
|
|
|
|
seekPosition.LowPart = (offset & 0xFFFF'FFFF) - (offset % this->m_sectorSize);
|
|
|
|
seekPosition.HighPart = offset >> 32;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02: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-08-08 21:23:52 +02:00
|
|
|
offset += currSize;
|
|
|
|
size -= currSize;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#else
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
u64 startOffset = offset;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
std::vector<u8> modifiedSectorBuffer;
|
|
|
|
modifiedSectorBuffer.resize(this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
while (size > 0) {
|
|
|
|
u64 sectorBase = offset - (offset % this->m_sectorSize);
|
|
|
|
size_t currSize = std::min(size, this->m_sectorSize);
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02: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
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
::lseek(this->m_diskHandle, sectorBase, SEEK_SET);
|
|
|
|
if (::write(this->m_diskHandle, modifiedSectorBuffer.data(), modifiedSectorBuffer.size()) < 0)
|
|
|
|
break;
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
offset += currSize;
|
|
|
|
size -= currSize;
|
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#endif
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t DiskProvider::getActualSize() const {
|
|
|
|
return this->m_diskSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DiskProvider::getName() const {
|
2023-10-17 10:22:56 +02:00
|
|
|
if (this->m_friendlyName.empty())
|
|
|
|
return wolv::util::toUTF8String(this->m_path);
|
|
|
|
else
|
|
|
|
return this->m_friendlyName;
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
2023-06-04 10:42:11 +02:00
|
|
|
std::vector<DiskProvider::Description> DiskProvider::getDataDescription() const {
|
2021-12-12 00:42:12 +01:00
|
|
|
return {
|
2023-10-17 10:22:56 +02:00
|
|
|
{ "hex.builtin.provider.disk.selected_disk"_lang, wolv::util::toUTF8String(this->m_path) },
|
2023-07-16 18:18:41 +02:00
|
|
|
{ "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() {
|
2023-07-16 18:18:41 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
this->m_availableDrives.clear();
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2023-10-16 23:45:46 +02:00
|
|
|
std::array<TCHAR, MAX_DEVICE_ID_LEN> deviceInstanceID = {};
|
|
|
|
std::array<TCHAR, 1024> description = {};
|
|
|
|
|
|
|
|
const GUID hddClass = GUID_DEVINTERFACE_DISK;
|
|
|
|
|
|
|
|
HDEVINFO hDevInfo = SetupDiGetClassDevs(&hddClass, nullptr, nullptr, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
|
|
|
if (hDevInfo == INVALID_HANDLE_VALUE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add all physical drives
|
|
|
|
for (u32 i = 0; ; i++) {
|
|
|
|
SP_DEVINFO_DATA deviceInfoData;
|
|
|
|
deviceInfoData.cbSize = sizeof(deviceInfoData);
|
|
|
|
|
|
|
|
if (SetupDiEnumDeviceInfo(hDevInfo, i, &deviceInfoData) == FALSE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
SP_DEVICE_INTERFACE_DATA interfaceData;
|
|
|
|
interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
|
|
|
|
|
|
|
|
if (!SetupDiEnumInterfaceDevice(hDevInfo, nullptr, &hddClass, i, &interfaceData))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (CM_Get_Device_ID(deviceInfoData.DevInst, deviceInstanceID.data(), MAX_PATH, 0) != CR_SUCCESS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the required size of the device path
|
|
|
|
DWORD requiredSize = 0;
|
|
|
|
SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, nullptr, 0, &requiredSize, nullptr);
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-10-16 23:45:46 +02:00
|
|
|
// Query the device path
|
|
|
|
std::vector<u8> dataBuffer(requiredSize);
|
|
|
|
auto data = reinterpret_cast<SP_INTERFACE_DEVICE_DETAIL_DATA*>(dataBuffer.data());
|
|
|
|
data->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
|
|
|
|
|
|
|
|
if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, data, requiredSize, nullptr, nullptr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto path = data->DevicePath;
|
|
|
|
|
|
|
|
// Query the friendly name of the device
|
|
|
|
DWORD size = 0;
|
|
|
|
DWORD propertyRegDataType = SPDRP_PHYSICAL_DEVICE_OBJECT_NAME;
|
|
|
|
SetupDiGetDeviceRegistryProperty(hDevInfo, &deviceInfoData, SPDRP_FRIENDLYNAME,
|
2023-11-10 20:47:08 +01:00
|
|
|
&propertyRegDataType, reinterpret_cast<BYTE*>(description.data()),
|
2023-10-16 23:45:46 +02:00
|
|
|
sizeof(description),
|
|
|
|
&size);
|
|
|
|
|
|
|
|
auto friendlyName = description.data();
|
|
|
|
|
|
|
|
this->m_availableDrives.insert({ path, friendlyName });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all logical drives
|
|
|
|
std::bitset<32> drives = ::GetLogicalDrives();
|
|
|
|
for (char i = 0; i < 26; i++) {
|
|
|
|
if (drives[i]) {
|
|
|
|
char letter = 'A' + i;
|
|
|
|
this->m_availableDrives.insert({ hex::format(R"(\\.\{:c}:)", letter), hex::format(R"({:c}:/)", letter) });
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
2023-10-16 23:45:46 +02:00
|
|
|
}
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-07-16 18:18:41 +02:00
|
|
|
#endif
|
2021-12-12 11:56:26 +01:00
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-03-16 16:48:15 +01:00
|
|
|
bool DiskProvider::drawLoadInterface() {
|
2023-10-16 23:45:46 +02:00
|
|
|
#if defined(OS_WINDOWS)
|
2022-08-08 21:23:52 +02:00
|
|
|
|
2023-10-16 23:45:46 +02:00
|
|
|
if (this->m_availableDrives.empty())
|
2022-09-11 21:06:15 +02:00
|
|
|
this->reloadDrives();
|
|
|
|
|
2023-10-16 23:45:46 +02:00
|
|
|
ImGui::PushItemWidth(300_scaled);
|
2022-08-08 21:23:52 +02:00
|
|
|
if (ImGui::BeginListBox("hex.builtin.provider.disk.selected_disk"_lang)) {
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-10-16 23:45:46 +02:00
|
|
|
ImGui::PushID(1);
|
|
|
|
for (const auto &[path, friendlyName] : this->m_availableDrives) {
|
2023-10-17 10:22:56 +02:00
|
|
|
if (ImGui::Selectable(friendlyName.c_str(), this->m_path == path)) {
|
2023-10-16 23:45:46 +02:00
|
|
|
this->m_path = path;
|
2023-10-17 10:22:56 +02:00
|
|
|
this->m_friendlyName = friendlyName;
|
|
|
|
}
|
2023-10-16 23:45:46 +02:00
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::InfoTooltip(path.c_str());
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
2023-10-16 23:45:46 +02:00
|
|
|
ImGui::PopID();
|
2022-08-08 21:23:52 +02:00
|
|
|
|
|
|
|
ImGui::EndListBox();
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
2023-10-16 23:45:46 +02:00
|
|
|
ImGui::PopItemWidth();
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
if (ImGui::Button("hex.builtin.provider.disk.reload"_lang)) {
|
|
|
|
this->reloadDrives();
|
2022-01-24 20:53:17 +01:00
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-10-16 23:45:46 +02:00
|
|
|
#else
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
if (ImGui::InputText("hex.builtin.provider.disk.selected_disk"_lang, this->m_pathBuffer.data(), this->m_pathBuffer.size(), ImGuiInputTextFlags_CallbackResize, ImGuiExt::UpdateStringSizeCallback, &this->m_pathBuffer)) {
|
2023-10-16 23:45:46 +02:00
|
|
|
this->m_path = this->m_pathBuffer;
|
2023-10-17 10:22:56 +02:00
|
|
|
this->m_friendlyName = this->m_pathBuffer;
|
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2023-10-16 23:45:46 +02:00
|
|
|
#endif
|
2023-03-16 16:48:15 +01:00
|
|
|
|
|
|
|
return !this->m_path.empty();
|
2022-08-08 21:23:52 +02:00
|
|
|
}
|
2021-12-12 00:42:12 +01:00
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
nlohmann::json DiskProvider::storeSettings(nlohmann::json settings) const {
|
2023-03-12 18:43:05 +01:00
|
|
|
settings["path"] = wolv::util::toUTF8String(this->m_path);
|
2022-01-24 20:53:17 +01:00
|
|
|
|
2023-10-17 10:22:56 +02:00
|
|
|
settings["friendly_name"] = this->m_friendlyName;
|
|
|
|
|
2022-08-08 21:23:52 +02:00
|
|
|
return Provider::storeSettings(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiskProvider::loadSettings(const nlohmann::json &settings) {
|
|
|
|
Provider::loadSettings(settings);
|
|
|
|
|
2023-05-16 11:33:00 +02:00
|
|
|
auto path = settings.at("path").get<std::string>();
|
2023-10-17 10:22:56 +02:00
|
|
|
|
|
|
|
if (settings.contains("friendly_name"))
|
|
|
|
this->m_friendlyName = settings.at("friendly_name").get<std::string>();
|
|
|
|
|
2022-10-04 09:10:58 +02:00
|
|
|
this->setPath(std::u8string(path.begin(), path.end()));
|
2022-08-08 21:23:52 +02:00
|
|
|
this->reloadDrives();
|
2021-12-12 00:42:12 +01:00
|
|
|
}
|
|
|
|
|
2022-08-14 14:45:18 +02:00
|
|
|
std::pair<Region, bool> DiskProvider::getRegionValidity(u64 address) const {
|
2022-08-17 16:28:44 +02:00
|
|
|
address -= this->getBaseAddress();
|
|
|
|
|
2022-08-14 14:45:18 +02:00
|
|
|
if (address < this->getActualSize())
|
2022-08-17 16:28:44 +02:00
|
|
|
return { Region { this->getBaseAddress() + address, this->getActualSize() - address }, true };
|
2022-08-14 14:45:18 +02:00
|
|
|
else
|
|
|
|
return { Region::Invalid(), false };
|
|
|
|
}
|
|
|
|
|
2023-01-24 09:07:11 +01:00
|
|
|
std::variant<std::string, i128> DiskProvider::queryInformation(const std::string &category, const std::string &argument) {
|
|
|
|
if (category == "file_path")
|
2023-03-12 18:43:05 +01:00
|
|
|
return wolv::util::toUTF8String(this->m_path);
|
2023-01-24 09:07:11 +01:00
|
|
|
else if (category == "sector_size")
|
|
|
|
return this->m_sectorSize;
|
2023-10-17 10:22:56 +02:00
|
|
|
else if (category == "friendly_name")
|
|
|
|
return this->m_friendlyName;
|
2023-01-24 09:07:11 +01:00
|
|
|
else
|
|
|
|
return Provider::queryInformation(category, argument);
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
}
|
|
|
|
#endif
|