From db11ba13c878625cde8bb27fed2d9394f45196df Mon Sep 17 00:00:00 2001 From: Christophe Grenier Date: Mon, 10 Mar 2008 07:14:03 +0100 Subject: [PATCH] Get disk model under Windows --- src/Makefile.am | 4 +- src/hdaccess.c | 52 ++++++++++++++++----- src/win32.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ src/win32.h | 4 ++ 4 files changed, 165 insertions(+), 13 deletions(-) create mode 100644 src/win32.c create mode 100644 src/win32.h diff --git a/src/Makefile.am b/src/Makefile.am index bc8a3604..2d04d4fc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,8 +10,8 @@ endif sbin_PROGRAMS = testdisk photorec #diskcp -base_C = common.c crc.c ewf.c fnctdsk.c hdaccess.c hdcache.c intrf.c log.c parti386.c partgpt.c partmac.c partsun.c partnone.c partxbox.c chgtype.c io_redir.c ntfs_io.c ntfs_utl.c partauto.c sudo.c unicode.c -base_H = common.h crc.h ewf.h fnctdsk.h hdaccess.h guid_cmp.h guid_cpy.h hdcache.h intrf.h intrfn.h lang.h log.h types.h chgtype.h io_redir.h ntfs_utl.h partgpt.h partmac.h partnone.h partxbox.h partauto.h sudo.h unicode.h +base_C = common.c crc.c ewf.c fnctdsk.c hdaccess.c hdcache.c intrf.c log.c parti386.c partgpt.c partmac.c partsun.c partnone.c partxbox.c chgtype.c io_redir.c ntfs_io.c ntfs_utl.c partauto.c sudo.c unicode.c win32.c +base_H = common.h crc.h ewf.h fnctdsk.h hdaccess.h guid_cmp.h guid_cpy.h hdcache.h intrf.h intrfn.h lang.h log.h types.h chgtype.h io_redir.h ntfs_utl.h partgpt.h partmac.h partnone.h partxbox.h partauto.h sudo.h unicode.h win32.h fs_C = analyse.c bfs.c bsd.c cramfs.c fat.c fatx.c ext2.c jfs.c hfs.c hfsp.c luks.c lvm.c md.c netware.c ntfs.c rfs.c savehdr.c sun.c swap.c sysv.c ufs.c xfs.c fs_H = analyse.h bfs.h bsd.h cramfs.h fat.h fatx.h ext2.h jfs_superblock.h jfs.h hfs.h hfsp.h luks.h lvm.h md.h netware.h ntfs.h rfs.h savehdr.h sun.h swap.h sysv.h ufs.h xfs.h diff --git a/src/hdaccess.c b/src/hdaccess.c index 7a151379..e769fa00 100644 --- a/src/hdaccess.c +++ b/src/hdaccess.c @@ -2,7 +2,7 @@ File: hdaccess.c - Copyright (C) 1998-2007 Christophe GRENIER + Copyright (C) 1998-2008 Christophe GRENIER This software is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -99,6 +99,9 @@ #include #include #endif +#if defined(__CYGWIN__) || defined(__MINGW32__) +#include "win32.h" +#endif #include "fnctdsk.h" #include "ewf.h" #include "log.h" @@ -794,7 +797,7 @@ list_disk_t *hd_parse(list_disk_t *list_disk, const int verbose, const arch_fnct #elif defined(__CYGWIN__) || defined(__MINGW32__) { int do_insert=0; - char device_hd[]="\\\\.\\PhysicalDrive0"; + char device_hd[]="\\\\.\\PhysicalDrive00"; char device_cdrom[]="\\\\.\\C:"; #if defined(__CYGWIN__) char device_scsi[]="/dev/sda"; @@ -812,7 +815,7 @@ list_disk_t *hd_parse(list_disk_t *list_disk, const int verbose, const arch_fnct for(i=0;i<16;i++) { disk_t *disk_car; - device_hd[strlen(device_hd)-1]='0'+i; + sprintf(device_hd,"\\\\.\\PhysicalDrive%u", i); disk_car=file_test_availability_win32(device_hd,verbose,arch,testdisk_mode); if(do_insert>0 || (testdisk_mode&TESTDISK_O_ALL)==TESTDISK_O_ALL) list_disk=insert_new_disk(list_disk,disk_car); @@ -1530,6 +1533,12 @@ static void disk_get_info(const int hd_h, disk_t *dev, const int verbose) free(product); } #endif +#if defined(__CYGWIN__) || defined(__MINGW32__) + { + HANDLE handle=(HANDLE)get_osfhandle(hd_h); + file_win32_disk_get_info(handle, dev, verbose); + } +#endif } #endif @@ -2120,8 +2129,8 @@ static int file_win32_read(disk_t *disk_car, const unsigned int count, void *buf static int file_win32_write(disk_t *disk_car,const unsigned int count, const void *buf, const uint64_t offset); static int file_win32_nowrite(disk_t *disk_car, const unsigned int count, const void *buf, const uint64_t offset); static int file_win32_sync(disk_t *disk_car); -uint64_t filewin32_getfilesize(HANDLE handle, const char *device); -uint64_t filewin32_setfilepointer(HANDLE handle, const char *device); +static uint64_t filewin32_getfilesize(HANDLE handle, const char *device); +static uint64_t filewin32_setfilepointer(HANDLE handle, const char *device); struct info_file_win32_struct { @@ -2130,7 +2139,7 @@ struct info_file_win32_struct int mode; }; -uint64_t filewin32_getfilesize(HANDLE handle, const char *device) +static uint64_t filewin32_getfilesize(HANDLE handle, const char *device) { DWORD lpFileSizeLow; DWORD lpFileSizeHigh; @@ -2155,7 +2164,7 @@ uint64_t filewin32_getfilesize(HANDLE handle, const char *device) return lpFileSizeLow+((uint64_t)lpFileSizeHigh>>32); } -uint64_t filewin32_setfilepointer(HANDLE handle, const char *device) +static uint64_t filewin32_setfilepointer(HANDLE handle, const char *device) { LARGE_INTEGER li; li.QuadPart = 0; @@ -2307,6 +2316,7 @@ disk_t *file_test_availability_win32(const char *device, const int verbose, cons disk_car->clean=file_win32_clean; disk_car->data=data; disk_car->offset=0; + file_win32_disk_get_info(handle, disk_car, verbose); if(disk_car->disk_size==0) { compute_device_size(disk_car); @@ -2354,13 +2364,33 @@ static const char *file_win32_description_short(disk_t *disk_car) struct info_file_win32_struct *data=disk_car->data; char buffer_disk_size[100]; if(data->file_name[0]=='\\' && data->file_name[1]=='\\' && data->file_name[2]=='.' && data->file_name[3]=='\\' && data->file_name[5]==':') - snprintf(disk_car->description_short_txt, sizeof(disk_car->description_txt),"Drive %c: - %s%s", - data->file_name[4], size_to_unit(disk_car->disk_size,buffer_disk_size), - ((data->mode&FILE_WRITE_DATA)==FILE_WRITE_DATA?"":" (RO)")); + { + if(disk_car->model==NULL) + snprintf(disk_car->description_short_txt, + sizeof(disk_car->description_txt), "Drive %c: - %s%s", + data->file_name[4], size_to_unit(disk_car->disk_size,buffer_disk_size), + ((data->mode&FILE_WRITE_DATA)==FILE_WRITE_DATA?"":" (RO)")); + else + snprintf(disk_car->description_short_txt, + sizeof(disk_car->description_txt), "Drive %c: - %s%s - %s", + data->file_name[4], size_to_unit(disk_car->disk_size,buffer_disk_size), + ((data->mode&FILE_WRITE_DATA)==FILE_WRITE_DATA?"":" (RO)"), + disk_car->model); + } else - snprintf(disk_car->description_short_txt, sizeof(disk_car->description_txt),"Disk %s - %s%s", + { + if(disk_car->model==NULL) + snprintf(disk_car->description_short_txt, + sizeof(disk_car->description_txt), "Disk %s - %s%s", data->file_name, size_to_unit(disk_car->disk_size,buffer_disk_size), ((data->mode&FILE_WRITE_DATA)==FILE_WRITE_DATA?"":" (RO)")); + else + snprintf(disk_car->description_short_txt, + sizeof(disk_car->description_txt), "Disk %s - %s%s - %s", + data->file_name, size_to_unit(disk_car->disk_size,buffer_disk_size), + ((data->mode&FILE_WRITE_DATA)==FILE_WRITE_DATA?"":" (RO)"), + disk_car->model); + } return disk_car->description_short_txt; } diff --git a/src/win32.c b/src/win32.c new file mode 100644 index 00000000..6255e63f --- /dev/null +++ b/src/win32.c @@ -0,0 +1,118 @@ +/* + + File: win32.c + + Copyright (C) 2008 Christophe GRENIER + + This software 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; either version 2 of the License, or + (at your option) any later version. + + 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, write the Free Software Foundation, Inc., 51 + Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + */ +#ifdef HAVE_CONFIG_H +#include +#endif + +#if defined(__CYGWIN__) || defined(__MINGW32__) +#include "types.h" +#include "common.h" +#ifdef HAVE_STDLIB_H +#include /* free */ +#endif +#ifdef HAVE_WINDEF_H +#include +#endif +#ifdef HAVE_WINBASE_H +#include +#include +#endif +#include /* isspace */ +#ifdef HAVE_W32API_DDK_NTDDDISK_H +#include +#endif +#include "fnctdsk.h" +#include "log.h" +#include "win32.h" + +void file_win32_disk_get_info(HANDLE handle, disk_t *dev, const int verbose) +{ +#ifdef IOCTL_STORAGE_QUERY_PROPERTY + DWORD cbBytesReturned = 0; + STORAGE_PROPERTY_QUERY query; + char buffer [10240]; + memset((void *) & query, 0, sizeof (query)); + query.PropertyId = StorageDeviceProperty; + query.QueryType = PropertyStandardQuery; + memset(&buffer, 0, sizeof (buffer)); + + if ( DeviceIoControl(handle, IOCTL_STORAGE_QUERY_PROPERTY, + &query, + sizeof (query), + &buffer, + sizeof (buffer), + &cbBytesReturned, NULL) ) + { + const STORAGE_DEVICE_DESCRIPTOR * descrip = (const STORAGE_DEVICE_DESCRIPTOR *) & buffer; + const unsigned int offsetVendor=descrip->VendorIdOffset; + const unsigned int offsetProduct=descrip->ProductIdOffset; + unsigned int lenVendor=0; + unsigned int lenProduct=0; + if(verbose>0) + { + log_info("IOCTL_STORAGE_QUERY_PROPERTY:\n"); + dump_log(&buffer, cbBytesReturned); + } + if(offsetVendor>0) + { + for(lenVendor=0; offsetVendor+lenVendor < sizeof(buffer) && + offsetVendor+lenVendor < cbBytesReturned && + buffer[offsetVendor+lenVendor] != '\0'; + lenVendor++); + } + if(offsetProduct>0) + { + for(lenProduct=0; offsetProduct+lenProduct < sizeof(buffer) && + offsetProduct+lenProduct < cbBytesReturned && + buffer[offsetProduct+lenProduct] != '\0'; + lenProduct++); + } + + if(lenVendor+lenProduct>0) + { + int i; + dev->model = (char*) MALLOC(lenVendor+1+lenProduct+1); + dev->model[0]='\0'; + if(lenVendor>0) + { + memcpy(dev->model, &buffer[offsetVendor], lenVendor); + dev->model[lenVendor]='\0'; + for(i=lenVendor-1;i>=0 && dev->model[i]==' ';i--); + if(i>=0) + dev->model[++i]=' '; + dev->model[++i]='\0'; + } + if(lenProduct>0) + { + strncat(dev->model, &buffer[offsetProduct],lenProduct); + for(i=strlen(dev->model)-1;i>=0 && dev->model[i]==' ';i--); + dev->model[++i]='\0'; + } + if(strlen(dev->model)>0) + return ; + free(dev->model); + dev->model=NULL; + } + } +#endif +} +#endif diff --git a/src/win32.h b/src/win32.h new file mode 100644 index 00000000..5ca89b12 --- /dev/null +++ b/src/win32.h @@ -0,0 +1,4 @@ +#if defined(__CYGWIN__) || defined(__MINGW32__) +void file_win32_disk_get_info(HANDLE handle, disk_t *dev, const int verbose); +#endif +