Files
aaruremote/win32/device.c

122 lines
3.4 KiB
C
Raw Normal View History

/*
2020-03-01 05:44:49 +00:00
* This file is part of the Aaru Remote Server.
2020-01-03 17:42:13 +00:00
* Copyright (c) 2019-2020 Natalia Portillo.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2020-03-01 05:46:10 +00:00
#include "../aaruremote.h"
#include "win32.h"
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
2020-03-01 19:47:15 +00:00
void* DeviceOpen(const char* device_path)
{
2020-03-01 19:47:15 +00:00
DeviceContext* ctx;
2020-03-01 19:47:15 +00:00
ctx = malloc(sizeof(DeviceContext));
if(!ctx) return NULL;
2020-03-01 19:47:15 +00:00
memset(ctx, 0, sizeof(DeviceContext));
ctx->handle = CreateFile(device_path,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(ctx->handle == INVALID_HANDLE_VALUE)
{
free(ctx);
return NULL;
}
strncpy(ctx->device_path, device_path, 4096);
return ctx;
}
2020-03-01 19:47:15 +00:00
void DeviceClose(void* device_ctx)
{
2020-03-01 19:47:15 +00:00
DeviceContext* ctx = device_ctx;
if(!ctx) return;
CloseHandle(ctx->handle);
free(ctx);
}
2020-03-01 19:47:15 +00:00
int32_t GetDeviceType(void* device_ctx)
{
2020-03-01 19:47:15 +00:00
DeviceContext* ctx = device_ctx;
2019-10-27 15:08:18 +00:00
STORAGE_PROPERTY_QUERY query;
DWORD error = 0;
BOOL ret;
DWORD returned;
PSTORAGE_DEVICE_DESCRIPTOR descriptor;
char* buf;
if(!ctx) return -1;
2019-10-27 15:08:18 +00:00
buf = malloc(1000);
2020-03-01 05:50:46 +00:00
if(!buf) return AARUREMOTE_DEVICE_TYPE_UNKNOWN;
2019-10-27 15:08:18 +00:00
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
memset(buf, 0, 1000);
ret = DeviceIoControl(
ctx->handle, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(STORAGE_PROPERTY_QUERY), buf, 1000, &returned, NULL);
if(!ret) error = GetLastError();
if(!ret && error != 0)
{
free(buf);
2020-03-01 05:50:46 +00:00
return AARUREMOTE_DEVICE_TYPE_UNKNOWN;
2019-10-27 15:08:18 +00:00
}
descriptor = (PSTORAGE_DEVICE_DESCRIPTOR)buf;
switch(descriptor->BusType)
{
2020-03-01 05:50:46 +00:00
case 1: returned = AARUREMOTE_DEVICE_TYPE_SCSI; break;
case 2: returned = AARUREMOTE_DEVICE_TYPE_ATAPI; break;
case 3: returned = AARUREMOTE_DEVICE_TYPE_ATA; break;
case 4: returned = AARUREMOTE_DEVICE_TYPE_SCSI; break;
case 5: returned = AARUREMOTE_DEVICE_TYPE_SCSI; break;
case 6: returned = AARUREMOTE_DEVICE_TYPE_SCSI; break;
case 7: returned = AARUREMOTE_DEVICE_TYPE_SCSI; break;
case 9: returned = AARUREMOTE_DEVICE_TYPE_SCSI; break;
case 0xA: returned = AARUREMOTE_DEVICE_TYPE_SCSI; break;
case 0xB: returned = AARUREMOTE_DEVICE_TYPE_ATA; break;
case 0xC: returned = AARUREMOTE_DEVICE_TYPE_SECURE_DIGITAL; break;
case 0xD: returned = AARUREMOTE_DEVICE_TYPE_MMC; break;
case 0x11: returned = AARUREMOTE_DEVICE_TYPE_NVME; break;
default: returned = AARUREMOTE_DEVICE_TYPE_UNKNOWN; break;
2019-10-27 15:08:18 +00:00
}
free(buf);
return returned;
}