/* (C) 2003 XDA Developers itsme@xs4all.nl * * $Header$ */ #include #include // HT_FIND, HT_FILE, HANDLE_SHIFT, APICALL_SCALE, FIRST_METHOD #include // PSLFindFirstPartition, PSLFindNextPartition, PSLFindClosePartition, PSLFindNextStore, PSLFindCloseStore #include "debug.h" #include #include "stringutils.h" std::string SI_DeviceClassString(DWORD dw) { switch(dw) { case STORAGE_DEVICE_CLASS_BLOCK : return "BLOCK"; case STORAGE_DEVICE_CLASS_MULTIMEDIA: return "MULTIMEDIA"; default: return stringformat("UNKNOWNDEVCLASS_%08lx", dw); } } std::string SI_DeviceTypeString(DWORD dw) { if (dw==0) return "(none)"; StringList list; if (dw&STORAGE_DEVICE_TYPE_PCIIDE ) list.push_back("PCIIDE"); if (dw&STORAGE_DEVICE_TYPE_FLASH ) list.push_back("FLASH"); if (dw&STORAGE_DEVICE_TYPE_ATA ) list.push_back("ATA"); if (dw&STORAGE_DEVICE_TYPE_ATAPI ) list.push_back("ATAPI"); if (dw&STORAGE_DEVICE_TYPE_PCCARD ) list.push_back("PCCARD"); if (dw&STORAGE_DEVICE_TYPE_CFCARD ) list.push_back("CFCARD"); if (dw&STORAGE_DEVICE_TYPE_SRAM ) list.push_back("SRAM"); if (dw&STORAGE_DEVICE_TYPE_DVD ) list.push_back("DVD"); if (dw&STORAGE_DEVICE_TYPE_CDROM ) list.push_back("CDROM"); if (dw&STORAGE_DEVICE_TYPE_USB ) list.push_back("USB"); if (dw&STORAGE_DEVICE_TYPE_1394 ) list.push_back("1394"); if (dw&STORAGE_DEVICE_TYPE_DOC ) list.push_back("DOC"); if (dw&STORAGE_DEVICE_TYPE_UNKNOWN ) list.push_back("UNKNOWN"); if (dw&STORAGE_DEVICE_TYPE_REMOVABLE_DRIVE) list.push_back("REMOVABLE_DRIVE"); if (dw&STORAGE_DEVICE_TYPE_REMOVABLE_MEDIA) list.push_back("REMOVABLE_MEDIA"); const DWORD knownflags= STORAGE_DEVICE_TYPE_PCIIDE|STORAGE_DEVICE_TYPE_FLASH|STORAGE_DEVICE_TYPE_ATA|STORAGE_DEVICE_TYPE_ATAPI|STORAGE_DEVICE_TYPE_PCCARD|STORAGE_DEVICE_TYPE_CFCARD|STORAGE_DEVICE_TYPE_SRAM|STORAGE_DEVICE_TYPE_DVD|STORAGE_DEVICE_TYPE_CDROM|STORAGE_DEVICE_TYPE_USB|STORAGE_DEVICE_TYPE_1394|STORAGE_DEVICE_TYPE_DOC|STORAGE_DEVICE_TYPE_UNKNOWN|STORAGE_DEVICE_TYPE_REMOVABLE_DRIVE|STORAGE_DEVICE_TYPE_REMOVABLE_MEDIA; if (dw&~knownflags) list.push_back(stringformat("UNKNOWNDEVTYPE_%08lx", dw&~knownflags)); return JoinStringList(list, ","); } std::string SI_DeviceFlagsString(DWORD dw) { if (dw==0) return "(none)"; StringList list; if (dw&STORAGE_DEVICE_FLAG_READWRITE ) list.push_back("READWRITE"); if (dw&STORAGE_DEVICE_FLAG_READONLY ) list.push_back("READONLY"); if (dw&STORAGE_DEVICE_FLAG_TRANSACTED) list.push_back("TRANSACTED"); if (dw&STORAGE_DEVICE_FLAG_MEDIASENSE) list.push_back("MEDIASENSE"); const DWORD knownflags= STORAGE_DEVICE_FLAG_READWRITE|STORAGE_DEVICE_FLAG_READONLY|STORAGE_DEVICE_FLAG_TRANSACTED|STORAGE_DEVICE_FLAG_MEDIASENSE; if (dw&~knownflags) list.push_back(stringformat("UNKNOWNDEVFLAGS_%08lx", dw&~knownflags)); return JoinStringList(list, ","); } std::string SI_DeviceInfoString(const STORAGEDEVICEINFO& sdi) { if (sdi.cbSize!=sizeof(STORAGEDEVICEINFO)) debug("WARNING: sdi.cbSize=%d (!= %d)\n", sdi.cbSize, sizeof(STORAGEDEVICEINFO)); return stringformat("profile='%ls' class=%s type=%s flags=%s", sdi.szProfile, SI_DeviceClassString(sdi.dwDeviceClass).c_str(), SI_DeviceTypeString(sdi.dwDeviceType).c_str(), SI_DeviceFlagsString(sdi.dwDeviceFlags).c_str()); } std::string SectorNumString(const SECTORNUM& sn) { return stringformat("%Lx", sn); } std::string FileTimeString(const FILETIME& ft) { SYSTEMTIME systime; FILETIME lfiletime; FileTimeToLocalFileTime(&ft, &lfiletime); FileTimeToSystemTime(&lfiletime, &systime); return stringformat("%04d-%02d-%02d %02d:%02d:%02d.%03d", systime.wYear, systime.wMonth, systime.wDay, systime.wHour, systime.wMinute, systime.wSecond,systime.wMilliseconds); } std::string SI_AttributesString(DWORD dw) { if (dw==0) return "(none)"; StringList list; if (dw&STORE_ATTRIBUTE_READONLY ) list.push_back("READONLY"); if (dw&STORE_ATTRIBUTE_REMOVABLE ) list.push_back("REMOVABLE"); if (dw&STORE_ATTRIBUTE_UNFORMATTED) list.push_back("UNFORMATTED"); if (dw&STORE_ATTRIBUTE_AUTOFORMAT ) list.push_back("AUTOFORMAT"); if (dw&STORE_ATTRIBUTE_AUTOPART ) list.push_back("AUTOPART"); if (dw&STORE_ATTRIBUTE_AUTOMOUNT ) list.push_back("AUTOMOUNT"); const DWORD knownflags= STORE_ATTRIBUTE_READONLY|STORE_ATTRIBUTE_REMOVABLE|STORE_ATTRIBUTE_UNFORMATTED|STORE_ATTRIBUTE_AUTOFORMAT|STORE_ATTRIBUTE_AUTOPART|STORE_ATTRIBUTE_AUTOMOUNT; if (dw&~knownflags) list.push_back(stringformat("UNKNOWNSATTRS_%08lx", dw&~knownflags)); return JoinStringList(list, ","); } std::string PI_AttributesString(DWORD dw) { if (dw==0) return "(none)"; StringList list; if (dw&PARTITION_ATTRIBUTE_EXPENDABLE) list.push_back("EXPENDABLE"); if (dw&PARTITION_ATTRIBUTE_READONLY ) list.push_back("READONLY"); if (dw&PARTITION_ATTRIBUTE_AUTOFORMAT) list.push_back("AUTOFORMAT"); if (dw&PARTITION_ATTRIBUTE_ACTIVE ) list.push_back("ACTIVE"); if (dw&PARTITION_ATTRIBUTE_BOOT ) list.push_back("BOOT"); if (dw&PARTITION_ATTRIBUTE_MOUNTED ) list.push_back("MOUNTED"); const DWORD knownflags= PARTITION_ATTRIBUTE_EXPENDABLE|PARTITION_ATTRIBUTE_READONLY|PARTITION_ATTRIBUTE_AUTOFORMAT|PARTITION_ATTRIBUTE_ACTIVE|PARTITION_ATTRIBUTE_BOOT|PARTITION_ATTRIBUTE_MOUNTED; if (dw&~knownflags) list.push_back(stringformat("UNKNOWNPATTRS_%08lx", dw&~knownflags)); return JoinStringList(list, ","); } void DumpStoreInfo(const STOREINFO& si) { if (si.cbSize!=sizeof(STOREINFO)) debug("WARNING: si.cbSize=%d (!= %d)\n", si.cbSize, sizeof(STOREINFO)); debug(" si=%08lx -> class=%08lx type=%08lx\n", &si, si.dwDeviceClass, si.dwDeviceType); debug("dev='%ls' store='%ls'\n", si.szDeviceName, si.szStoreName); debug(" class=%s type=%s flags=%s\n", SI_DeviceClassString(si.dwDeviceClass).c_str(), SI_DeviceTypeString(si.dwDeviceType).c_str(), SI_DeviceFlagsString(si.dwDeviceFlags).c_str()); debug(" sdi: %s\n", SI_DeviceInfoString(si.sdi).c_str()); debug(" nsect=%s bpsect=%x free=%s maxpartsize=%s\n", SectorNumString(si.snNumSectors).c_str(), si.dwBytesPerSector, SectorNumString(si.snFreeSectors).c_str(), SectorNumString(si.snBiggestPartCreatable).c_str()); debug(" created=%s modified=%s\n", FileTimeString(si.ftCreated).c_str(), FileTimeString(si.ftLastModified).c_str()); debug(" attribs=%s partitions=%d mounts=%d\n", SI_AttributesString(si.dwAttributes).c_str(), si.dwPartitionCount, si.dwMountCount); } void DumpPartitionInfo(const PARTINFO& pi) { if (pi.cbSize!=sizeof(PARTINFO)) debug("WARNING: pi.cbSize=%d (!= %d)\n", pi.cbSize, sizeof(PARTINFO)); debug("name='%ls' filesys='%ls' volname='%ls'", pi.szPartitionName, pi.szFileSys, pi.szVolumeName); debug(" end=%s created=%s modified=%s", SectorNumString(pi.snNumSectors).c_str(), FileTimeString(pi.ftCreated).c_str(), FileTimeString(pi.ftLastModified).c_str()); debug(" attr=%s type=%02x\n", PI_AttributesString(pi.dwAttributes).c_str(), pi.bPartType); } HANDLE FindFirstStore(STOREINFO *psi) { return (FindFirstFileW( L"\\StoreMgr", (WIN32_FIND_DATA *)psi)); } // Storage Management API's HANDLE WINAPI OpenStore(LPCTSTR szDeviceName) { TCHAR szFileName[MAX_PATH]; memset( szFileName, 0, sizeof(szFileName)); wcscpy( szFileName, L"\\StoreMgr\\"); __try { wcsncat( szFileName, szDeviceName, MAX_PATH-15); } __except(EXCEPTION_EXECUTE_HANDLER) { SetLastError(ERROR_BAD_ARGUMENTS); } return CreateFile( szFileName, 0, 0, NULL, 0, 0, NULL); } void ListPartitions(const TCHAR *szDevname) { HANDLE hs= OpenStore(szDevname); PARTINFO pi; pi.cbSize= sizeof(PARTINFO); HANDLE hfp= PSLFindFirstPartition(hs, &pi); if (hfp==INVALID_HANDLE_VALUE || hfp==NULL) { error("FindFirstPartition"); return; } do { DumpPartitionInfo(pi); } while (PSLFindNextPartition(hfp, &pi)); if (GetLastError()!=ERROR_NO_MORE_ITEMS) error("FindNextPartition"); PSLFindClosePartition(hfp); } void ListStores() { STOREINFO si; si.cbSize= sizeof(STOREINFO); HANDLE hfs= FindFirstStore(&si); if (hfs==INVALID_HANDLE_VALUE || hfs==NULL) { error("FindFirstStore"); return; } do { DumpStoreInfo(si); ListPartitions(si.szDeviceName); } while (PSLFindNextStore(hfs, &si)); if (GetLastError()!=ERROR_NO_MORE_ITEMS) error("FindNextStore"); PSLFindCloseStore(hfs); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { DebugSetLogfile("tststores.log"); ListStores(); return 0; }