Skip to content

Commit

Permalink
[misc] add S Mode detection to Windows version reporting
Browse files Browse the repository at this point in the history
* With thanks to @Wack0
  • Loading branch information
pbatard committed Aug 18, 2023
1 parent c5ad16f commit 5b6574d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/drive.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Rufus: The Reliable USB Formatting Utility
* Drive access function calls
* Copyright © 2011-2022 Pete Batard <[email protected]>
* Copyright © 2011-2023 Pete Batard <[email protected]>
*
* 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
Expand Down Expand Up @@ -57,7 +57,7 @@
#define VDS_RESCAN_REFRESH 0x00000001
#define VDS_RESCAN_REENUMERATE 0x00000002

#define VDS_SET_ERROR(hr) do { if (hr != S_OK) { SetLastError(hr); FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_GEN_FAILURE; } } while(0)
#define VDS_SET_ERROR(hr) do { if (hr != S_OK) { SetLastError((DWORD)hr); FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|ERROR_GEN_FAILURE; } } while(0)

#if !defined(__MINGW32__)
typedef enum _FSINFOCLASS {
Expand Down
10 changes: 5 additions & 5 deletions src/rufus.rc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 232, 326
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 4.3.2075"
CAPTION "Rufus 4.3.2076"
FONT 9, "Segoe UI Symbol", 400, 0, 0x0
BEGIN
LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP
Expand Down Expand Up @@ -392,8 +392,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 4,3,2075,0
PRODUCTVERSION 4,3,2075,0
FILEVERSION 4,3,2076,0
PRODUCTVERSION 4,3,2076,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -411,13 +411,13 @@ BEGIN
VALUE "Comments", "https://rufus.ie"
VALUE "CompanyName", "Akeo Consulting"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "4.3.2075"
VALUE "FileVersion", "4.3.2076"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "� 2011-2023 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html"
VALUE "OriginalFilename", "rufus-4.3.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "4.3.2075"
VALUE "ProductVersion", "4.3.2076"
END
END
BLOCK "VarFileInfo"
Expand Down
32 changes: 32 additions & 0 deletions src/stdfn.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@

#include "settings.h"

// MinGW doesn't yet know these (from wldp.h)
typedef enum WLDP_WINDOWS_LOCKDOWN_MODE
{
WLDP_WINDOWS_LOCKDOWN_MODE_UNLOCKED = 0,
WLDP_WINDOWS_LOCKDOWN_MODE_TRIAL,
WLDP_WINDOWS_LOCKDOWN_MODE_LOCKED,
WLDP_WINDOWS_LOCKDOWN_MODE_MAX,
} WLDP_WINDOWS_LOCKDOWN_MODE, * PWLDP_WINDOWS_LOCKDOWN_MODE;

windows_version_t WindowsVersion = { 0 };

/*
Expand Down Expand Up @@ -304,6 +313,25 @@ static const char* GetEdition(DWORD ProductType)
}
}

PF_TYPE_DECL(WINAPI, HRESULT, WldpQueryWindowsLockdownMode, (PWLDP_WINDOWS_LOCKDOWN_MODE));
BOOL isSMode(void)
{
BOOL r = FALSE;
WLDP_WINDOWS_LOCKDOWN_MODE mode;
PF_INIT_OR_OUT(WldpQueryWindowsLockdownMode, Wldp);

HRESULT hr = pfWldpQueryWindowsLockdownMode(&mode);
if (hr != S_OK) {
SetLastError((DWORD)hr);

This comment has been minimized.

Copy link
@Wack0

Wack0 Aug 18, 2023

I personally dislike the use of SetLastError with an HRESULT here, especially because the function can only return S_OK or HRESULT_FROM_NT(failing_ntstatus), but that would require heavy refactor (probably to take an HRESULT, and where it's used after failing Win32 function, to pass HRESULT_FROM_WIN32(GetLastError()))

also about WindowsErrorString, FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM) should be able to take an HRESULT directly, MS themselves do it, and in their CRT too (in _com_error::ErrorMessage which is one documented way to get error message from HRESULT)

This comment has been minimized.

Copy link
@pbatard

pbatard Aug 18, 2023

Author Owner

Well, I pretty much picked it up from https://devblogs.microsoft.com/oldnewthing/20061103-07/?p=29133 where they pretty much just cast to DWORD. Also, this uprintf will only ever be seen in the Debug facility (not the Rufus log) because it happens before we instantiate the log Window, so I can't say I care that much about an error that will have next to no impact on what we're trying to accomplish (if the call fails, then there's no fallback and we may possibly misreport the user's environment). In other words, I added this line mostly for development to see if the function would barf when I introduced the call, and it does not output anything that a regular Rufus user will ever see.

This comment has been minimized.

Copy link
@Wack0

Wack0 Aug 18, 2023

about the VDS stuff, that CAN be done by FormatMessage too, you just need to pass FORMAT_MESSAGE_FROM_HMODULE and the base address of vdsutil.dll as lpSource.

Same for wim, except using wimgapi.dll.

This comment has been minimized.

Copy link
@pbatard

pbatard Aug 18, 2023

Author Owner

I appreciate the pointers. I suppose the same is true for wininet.dll as well, because I also went out of my way to duplicate an error message table there, which I wouldn't mind dropping.

I sure wish FormatMessage() (or a new FormatMessageEx() call) was smart enough to look at the facility to open and use the corresponding HMODULE automatically, because it looks to me like resolving some of the facilities where FORMAT_MESSAGE_FROM_SYSTEM alone will yield nothing, to the DLL that contains those messages, should be a fairly straightforward matter...

This comment has been minimized.

Copy link
@Wack0

Wack0 Aug 18, 2023

yes, the same is true for wininet, and in fact it's actually documented there: https://learn.microsoft.com/en-gb/windows/win32/wininet/appendix-c-handling-errors

uprintf("Could not detect S Mode: %s", WindowsErrorString());
} else {
r = (mode != WLDP_WINDOWS_LOCKDOWN_MODE_UNLOCKED);
}

out:
return r;
}

/*
* Modified from smartmontools' os_win32.cpp
*/
Expand Down Expand Up @@ -451,6 +479,10 @@ void GetWindowsVersion(windows_version_t* windows_version)
safe_sprintf(vptr, vlen, " (Build %lu.%lu)", windows_version->BuildNumber, windows_version->Ubr);
else
safe_sprintf(vptr, vlen, " (Build %lu)", windows_version->BuildNumber);
vptr = &windows_version->VersionStr[safe_strlen(windows_version->VersionStr)];
vlen = sizeof(windows_version->VersionStr) - safe_strlen(windows_version->VersionStr) - 1;
if (isSMode())
safe_sprintf(vptr, vlen, " in S Mode");
}

/*
Expand Down

0 comments on commit 5b6574d

Please sign in to comment.