--- embedaddon/smartmontools/os_win32.cpp 2013/07/22 01:17:35 1.1.1.3 +++ embedaddon/smartmontools/os_win32.cpp 2013/10/14 07:54:03 1.1.1.4 @@ -76,11 +76,18 @@ // CSMI support #include "csmisas.h" +// Silence -Wunused-local-typedefs warning from g++ >= 4.8 +#if __GNUC__ >= 4 +#define ATTR_UNUSED __attribute__((unused)) +#else +#define ATTR_UNUSED /**/ +#endif + // Macro to check constants at compile time using a dummy typedef #define ASSERT_CONST(c, n) \ - typedef char assert_const_##c[((c) == (n)) ? 1 : -1] + typedef char assert_const_##c[((c) == (n)) ? 1 : -1] ATTR_UNUSED #define ASSERT_SIZEOF(t, n) \ - typedef char assert_sizeof_##t[(sizeof(t) == (n)) ? 1 : -1] + typedef char assert_sizeof_##t[(sizeof(t) == (n)) ? 1 : -1] ATTR_UNUSED #ifndef _WIN64 #define SELECT_WIN_32_64(x32, x64) (x32) @@ -88,7 +95,7 @@ #define SELECT_WIN_32_64(x32, x64) (x64) #endif -const char * os_win32_cpp_cvsid = "$Id: os_win32.cpp,v 1.1.1.3 2013/07/22 01:17:35 misho Exp $"; +const char * os_win32_cpp_cvsid = "$Id: os_win32.cpp,v 1.1.1.4 2013/10/14 07:54:03 misho Exp $"; ///////////////////////////////////////////////////////////////////////////// // Windows I/O-controls, some declarations are missing in the include files @@ -597,36 +604,51 @@ std::string win_smart_interface::get_os_version_str() return vstr; } - if (vi.dwPlatformId > 0xff || vi.dwMajorVersion > 0xff || vi.dwMinorVersion > 0xff) - return vstr; + const char * w = 0; + if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT) { - const char * w; - switch (vi.dwPlatformId << 16 | vi.dwMajorVersion << 8 | vi.dwMinorVersion) { - case VER_PLATFORM_WIN32_WINDOWS<<16|0x0400| 0: - w = (vi.szCSDVersion[1] == 'B' || - vi.szCSDVersion[1] == 'C' ? "95-osr2" : "95"); break; - case VER_PLATFORM_WIN32_WINDOWS<<16|0x0400|10: - w = (vi.szCSDVersion[1] == 'A' ? "98se" : "98"); break; - case VER_PLATFORM_WIN32_WINDOWS<<16|0x0400|90: w = "me"; break; - //case VER_PLATFORM_WIN32_NT <<16|0x0300|51: w = "nt3.51"; break; - case VER_PLATFORM_WIN32_NT <<16|0x0400| 0: w = "nt4"; break; - case VER_PLATFORM_WIN32_NT <<16|0x0500| 0: w = "2000"; break; - case VER_PLATFORM_WIN32_NT <<16|0x0500| 1: - w = (!GetSystemMetrics(87/*SM_MEDIACENTER*/) ? "xp" - : "xp-mc"); break; - case VER_PLATFORM_WIN32_NT <<16|0x0500| 2: - w = (!GetSystemMetrics(89/*SM_SERVERR2*/) ? "2003" - : "2003r2"); break; - case VER_PLATFORM_WIN32_NT <<16|0x0600| 0: - w = (vi.wProductType == VER_NT_WORKSTATION ? "vista" - : "2008" ); break; - case VER_PLATFORM_WIN32_NT <<16|0x0600| 1: - w = (vi.wProductType == VER_NT_WORKSTATION ? "win7" - : "2008r2"); break; - case VER_PLATFORM_WIN32_NT <<16|0x0600| 2: - w = (vi.wProductType == VER_NT_WORKSTATION ? "win8" - : "2012"); break; - default: w = 0; break; + if (vi.dwMajorVersion > 6 || (vi.dwMajorVersion == 6 && vi.dwMinorVersion >= 2)) { + // Starting with Windows 8.1 Preview, GetVersionEx() does no longer report the + // actual OS version, see: + // http://msdn.microsoft.com/en-us/library/windows/desktop/dn302074.aspx + + ULONGLONG major_equal = VerSetConditionMask(0, VER_MAJORVERSION, VER_EQUAL); + for (unsigned major = vi.dwMajorVersion; major <= 9; major++) { + OSVERSIONINFOEXA vi2; memset(&vi2, 0, sizeof(vi2)); + vi2.dwOSVersionInfoSize = sizeof(vi2); vi2.dwMajorVersion = major; + if (!VerifyVersionInfo(&vi2, VER_MAJORVERSION, major_equal)) + continue; + if (vi.dwMajorVersion < major) { + vi.dwMajorVersion = major; vi.dwMinorVersion = 0; + } + + ULONGLONG minor_equal = VerSetConditionMask(0, VER_MINORVERSION, VER_EQUAL); + for (unsigned minor = vi.dwMinorVersion; minor <= 9; minor++) { + memset(&vi2, 0, sizeof(vi2)); vi2.dwOSVersionInfoSize = sizeof(vi2); + vi2.dwMinorVersion = minor; + if (!VerifyVersionInfo(&vi2, VER_MINORVERSION, minor_equal)) + continue; + vi.dwMinorVersion = minor; + break; + } + + break; + } + } + + if (vi.dwMajorVersion <= 0xf && vi.dwMinorVersion <= 0xf) { + bool ws = (vi.wProductType <= VER_NT_WORKSTATION); + switch (vi.dwMajorVersion << 4 | vi.dwMinorVersion) { + case 0x50: w = "2000"; break; + case 0x51: w = "xp"; break; + case 0x52: w = (!GetSystemMetrics(89/*SM_SERVERR2*/) + ? "2003" : "2003r2"); break; + case 0x60: w = (ws ? "vista" : "2008" ); break; + case 0x61: w = (ws ? "win7" : "2008r2"); break; + case 0x62: w = (ws ? "win8" : "2012" ); break; + case 0x63: w = (ws ? "win8.1": "2012r2"); break; + } + } } const char * w64 = ""; @@ -636,9 +658,9 @@ std::string win_smart_interface::get_os_version_str() #endif if (!w) - snprintf(vptr, vlen, "-%s%lu.%lu%s", - (vi.dwPlatformId==VER_PLATFORM_WIN32_NT ? "nt" : "9x"), - vi.dwMajorVersion, vi.dwMinorVersion, w64); + snprintf(vptr, vlen, "-%s%u.%u%s", + (vi.dwPlatformId==VER_PLATFORM_WIN32_NT ? "nt" : "??"), + (unsigned)vi.dwMajorVersion, (unsigned)vi.dwMinorVersion, w64); else if (vi.wServicePackMinor) snprintf(vptr, vlen, "-%s%s-sp%u.%u", w, w64, vi.wServicePackMajor, vi.wServicePackMinor); else if (vi.wServicePackMajor) @@ -1101,20 +1123,20 @@ static int smart_get_version(HANDLE hdevice, GETVERSIO if (!DeviceIoControl(hdevice, SMART_GET_VERSION, NULL, 0, &vers, sizeof(vers), &num_out, NULL)) { if (ata_debugmode) - pout(" SMART_GET_VERSION failed, Error=%ld\n", GetLastError()); + pout(" SMART_GET_VERSION failed, Error=%u\n", (unsigned)GetLastError()); errno = ENOSYS; return -1; } assert(num_out == sizeof(GETVERSIONINPARAMS)); if (ata_debugmode > 1) { - pout(" SMART_GET_VERSION suceeded, bytes returned: %lu\n" - " Vers = %d.%d, Caps = 0x%lx, DeviceMap = 0x%02x\n", - num_out, vers.bVersion, vers.bRevision, - vers.fCapabilities, vers.bIDEDeviceMap); + pout(" SMART_GET_VERSION suceeded, bytes returned: %u\n" + " Vers = %d.%d, Caps = 0x%x, DeviceMap = 0x%02x\n", + (unsigned)num_out, vers.bVersion, vers.bRevision, + (unsigned)vers.fCapabilities, vers.bIDEDeviceMap); if (vers_ex.wIdentifier == SMART_VENDOR_3WARE) - pout(" Identifier = %04x(3WARE), ControllerId=%u, DeviceMapEx = 0x%08lx\n", - vers_ex.wIdentifier, vers_ex.wControllerId, vers_ex.dwDeviceMapEx); + pout(" Identifier = %04x(3WARE), ControllerId=%u, DeviceMapEx = 0x%08x\n", + vers_ex.wIdentifier, vers_ex.wControllerId, (unsigned)vers_ex.dwDeviceMapEx); } if (ata_version_ex) @@ -1202,8 +1224,8 @@ static int smart_ioctl(HANDLE hdevice, IDEREGS * regs, } if (ata_debugmode > 1) { - pout(" %s suceeded, bytes returned: %lu (buffer %lu)\n", name, - num_out, outpar->cBufferSize); + pout(" %s suceeded, bytes returned: %u (buffer %u)\n", name, + (unsigned)num_out, (unsigned)outpar->cBufferSize); print_ide_regs_io(regs, (regs->bFeaturesReg == ATA_SMART_STATUS ? (const IDEREGS *)(outpar->bBuffer) : NULL)); } @@ -1279,8 +1301,8 @@ static int ide_pass_through_ioctl(HANDLE hdevice, IDER if ( num_out != size || (buf->DataBuffer[0] == magic && !nonempty(buf->DataBuffer+1, datasize-1))) { if (ata_debugmode) { - pout(" IOCTL_IDE_PASS_THROUGH output data missing (%lu, %lu)\n", - num_out, buf->DataBufferSize); + pout(" IOCTL_IDE_PASS_THROUGH output data missing (%u, %u)\n", + (unsigned)num_out, (unsigned)buf->DataBufferSize); print_ide_regs_io(regs, &buf->IdeReg); } VirtualFree(buf, 0, MEM_RELEASE); @@ -1291,8 +1313,8 @@ static int ide_pass_through_ioctl(HANDLE hdevice, IDER } if (ata_debugmode > 1) { - pout(" IOCTL_IDE_PASS_THROUGH suceeded, bytes returned: %lu (buffer %lu)\n", - num_out, buf->DataBufferSize); + pout(" IOCTL_IDE_PASS_THROUGH suceeded, bytes returned: %u (buffer %u)\n", + (unsigned)num_out, (unsigned)buf->DataBufferSize); print_ide_regs_io(regs, &buf->IdeReg); } *regs = buf->IdeReg; @@ -1398,7 +1420,7 @@ static int ata_pass_through_ioctl(HANDLE hdevice, IDER if ( num_out != size || (ab.ucDataBuf[0] == magic && !nonempty(ab.ucDataBuf+1, datasize-1))) { if (ata_debugmode) { - pout(" IOCTL_ATA_PASS_THROUGH output data missing (%lu)\n", num_out); + pout(" IOCTL_ATA_PASS_THROUGH output data missing (%u)\n", (unsigned)num_out); print_ide_regs_io(regs, ctfregs); } errno = EIO; @@ -1408,7 +1430,7 @@ static int ata_pass_through_ioctl(HANDLE hdevice, IDER } if (ata_debugmode > 1) { - pout(" IOCTL_ATA_PASS_THROUGH suceeded, bytes returned: %lu\n", num_out); + pout(" IOCTL_ATA_PASS_THROUGH suceeded, bytes returned: %u\n", (unsigned)num_out); print_ide_regs_io(regs, ctfregs); } *regs = *ctfregs; @@ -1521,7 +1543,7 @@ static int ata_via_scsi_miniport_smart_ioctl(HANDLE hd // Check result if (sb.srbc.ReturnCode) { if (ata_debugmode) { - pout(" IOCTL_SCSI_MINIPORT_%s failed, ReturnCode=0x%08lx\n", name, sb.srbc.ReturnCode); + pout(" IOCTL_SCSI_MINIPORT_%s failed, ReturnCode=0x%08x\n", name, (unsigned)sb.srbc.ReturnCode); print_ide_regs_io(regs, NULL); } errno = EIO; @@ -1539,8 +1561,8 @@ static int ata_via_scsi_miniport_smart_ioctl(HANDLE hd } if (ata_debugmode > 1) { - pout(" IOCTL_SCSI_MINIPORT_%s suceeded, bytes returned: %lu (buffer %lu)\n", name, - num_out, sb.params.out.cBufferSize); + pout(" IOCTL_SCSI_MINIPORT_%s suceeded, bytes returned: %u (buffer %u)\n", name, + (unsigned)num_out, (unsigned)sb.params.out.cBufferSize); print_ide_regs_io(regs, (code == IOCTL_SCSI_MINIPORT_RETURN_STATUS ? (const IDEREGS *)(sb.params.out.bBuffer) : 0)); } @@ -1595,7 +1617,7 @@ static int ata_via_3ware_miniport_ioctl(HANDLE hdevice if (sb.srbc.ReturnCode) { if (ata_debugmode) { - pout(" ATA via IOCTL_SCSI_MINIPORT failed, ReturnCode=0x%08lx\n", sb.srbc.ReturnCode); + pout(" ATA via IOCTL_SCSI_MINIPORT failed, ReturnCode=0x%08x\n", (unsigned)sb.srbc.ReturnCode); print_ide_regs_io(regs, NULL); } errno = EIO; @@ -1607,7 +1629,7 @@ static int ata_via_3ware_miniport_ioctl(HANDLE hdevice memcpy(data, sb.buffer, datasize); if (ata_debugmode > 1) { - pout(" ATA via IOCTL_SCSI_MINIPORT suceeded, bytes returned: %lu\n", num_out); + pout(" ATA via IOCTL_SCSI_MINIPORT suceeded, bytes returned: %u\n", (unsigned)num_out); print_ide_regs_io(regs, &sb.regs); } *regs = sb.regs; @@ -1643,7 +1665,7 @@ static int update_3ware_devicemap_ioctl(HANDLE hdevice } if (srbc.ReturnCode) { if (ata_debugmode) - pout(" UPDATE DEVICEMAP via IOCTL_SCSI_MINIPORT failed, ReturnCode=0x%08lx\n", srbc.ReturnCode); + pout(" UPDATE DEVICEMAP via IOCTL_SCSI_MINIPORT failed, ReturnCode=0x%08x\n", (unsigned)srbc.ReturnCode); errno = EIO; return -1; } @@ -1932,7 +1954,7 @@ static int storage_query_property_ioctl(HANDLE hdevice if (!DeviceIoControl(hdevice, IOCTL_STORAGE_QUERY_PROPERTY, &query, sizeof(query), data, sizeof(*data), &num_out, NULL)) { if (ata_debugmode > 1 || scsi_debugmode > 1) - pout(" IOCTL_STORAGE_QUERY_PROPERTY failed, Error=%ld\n", GetLastError()); + pout(" IOCTL_STORAGE_QUERY_PROPERTY failed, Error=%u\n", (unsigned)GetLastError()); errno = ENOSYS; return -1; } @@ -1970,16 +1992,16 @@ static int storage_predict_failure_ioctl(HANDLE hdevic if (!DeviceIoControl(hdevice, IOCTL_STORAGE_PREDICT_FAILURE, 0, 0, &pred, sizeof(pred), &num_out, NULL)) { if (ata_debugmode > 1) - pout(" IOCTL_STORAGE_PREDICT_FAILURE failed, Error=%ld\n", GetLastError()); + pout(" IOCTL_STORAGE_PREDICT_FAILURE failed, Error=%u\n", (unsigned)GetLastError()); errno = ENOSYS; return -1; } if (ata_debugmode > 1) { pout(" IOCTL_STORAGE_PREDICT_FAILURE returns:\n" - " PredictFailure: 0x%08lx\n" + " PredictFailure: 0x%08x\n" " VendorSpecific: 0x%02x,0x%02x,0x%02x,...,0x%02x\n", - pred.PredictFailure, + (unsigned)pred.PredictFailure, pred.VendorSpecific[0], pred.VendorSpecific[1], pred.VendorSpecific[2], pred.VendorSpecific[sizeof(pred.VendorSpecific)-1] ); @@ -3151,14 +3173,14 @@ bool win_csmi_device::csmi_ioctl(unsigned code, IOCTL_ // Check result if (csmi_buffer->ReturnCode) { if (scsi_debugmode) { - pout(" IOCTL_SCSI_MINIPORT(CC_CSMI_%u) failed, ReturnCode=%lu\n", - code, csmi_buffer->ReturnCode); + pout(" IOCTL_SCSI_MINIPORT(CC_CSMI_%u) failed, ReturnCode=%u\n", + code, (unsigned)csmi_buffer->ReturnCode); } - return set_err(EIO, "CSMI(%u) failed with ReturnCode=%lu", code, csmi_buffer->ReturnCode); + return set_err(EIO, "CSMI(%u) failed with ReturnCode=%u", code, (unsigned)csmi_buffer->ReturnCode); } if (scsi_debugmode > 1) - pout(" IOCTL_SCSI_MINIPORT(CC_CSMI_%u) succeeded, bytes returned: %lu\n", code, num_out); + pout(" IOCTL_SCSI_MINIPORT(CC_CSMI_%u) succeeded, bytes returned: %u\n", code, (unsigned)num_out); return true; } @@ -3233,7 +3255,7 @@ bool win_scsi_device::open(int pd_num, int ld_num, int FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); if (h == INVALID_HANDLE_VALUE) { - set_err(ENODEV, "%s: Open failed, Error=%ld", b, GetLastError()); + set_err(ENODEV, "%s: Open failed, Error=%u", b, (unsigned)GetLastError()); return false; } set_fh(h);