summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWei Mao <[email protected]>2024-09-03 16:23:42 -0700
committerWei Mao <[email protected]>2024-09-03 16:23:42 -0700
commit3692e25f8cb37f62f8e89a659d0f64c1a34ab3e3 (patch)
treee0751ab486d32a1f9e036a091be01add76e9e97a
parentc4a093eed801a0b848e331c7ee64a91a1b114fdf (diff)
Copy general\ioctl\wdm\exe to general\ioctl\kmdf\exe
-rw-r--r--general/ioctl/kmdf/exe/install.c432
-rw-r--r--general/ioctl/kmdf/exe/nonpnp.inf8
-rw-r--r--general/ioctl/kmdf/exe/testapp.c120
3 files changed, 86 insertions, 474 deletions
diff --git a/general/ioctl/kmdf/exe/install.c b/general/ioctl/kmdf/exe/install.c
index 7060243a..ed2aa6e8 100644
--- a/general/ioctl/kmdf/exe/install.c
+++ b/general/ioctl/kmdf/exe/install.c
@@ -22,101 +22,44 @@ Environment:
--*/
-#include <DriverSpecs.h>
-_Analysis_mode_(_Analysis_code_type_user_code_)
-
#include <windows.h>
-#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <strsafe.h>
#include "public.h"
-#include <wdfinstaller.h>
-
-#define ARRAY_SIZE(x) (sizeof(x) /sizeof(x[0]))
-
-extern
-PCHAR
-GetCoinstallerVersion(
- VOID
- ) ;
-
BOOLEAN
InstallDriver(
- IN SC_HANDLE SchSCManager,
- IN LPCTSTR DriverName,
- IN LPCTSTR ServiceExe
+ _In_ SC_HANDLE SchSCManager,
+ _In_ LPCTSTR DriverName,
+ _In_ LPCTSTR ServiceExe
);
BOOLEAN
RemoveDriver(
- IN SC_HANDLE SchSCManager,
- IN LPCTSTR DriverName
+ _In_ SC_HANDLE SchSCManager,
+ _In_ LPCTSTR DriverName
);
BOOLEAN
StartDriver(
- IN SC_HANDLE SchSCManager,
- IN LPCTSTR DriverName
+ _In_ SC_HANDLE SchSCManager,
+ _In_ LPCTSTR DriverName
);
BOOLEAN
StopDriver(
- IN SC_HANDLE SchSCManager,
- IN LPCTSTR DriverName
+ _In_ SC_HANDLE SchSCManager,
+ _In_ LPCTSTR DriverName
);
-#define SYSTEM32_DRIVERS "\\System32\\Drivers\\"
-#define NONPNP_INF_FILENAME L"\\nonpnp.inf"
-#define WDF_SECTION_NAME L"nonpnp.NT.Wdf"
-
-//----------------------------------------------------------------------------
-//
-//----------------------------------------------------------------------------
-PFN_WDFPREDEVICEINSTALLEX pfnWdfPreDeviceInstallEx;
-PFN_WDFPOSTDEVICEINSTALL pfnWdfPostDeviceInstall;
-PFN_WDFPREDEVICEREMOVE pfnWdfPreDeviceRemove;
-PFN_WDFPOSTDEVICEREMOVE pfnWdfPostDeviceRemove;
-
-//-----------------------------------------------------------------------------
-// 4127 -- Conditional Expression is Constant warning
-//-----------------------------------------------------------------------------
-#define WHILE(a) \
-__pragma(warning(suppress:4127)) while(a)
-
-LONG
-GetPathToInf(
- _Out_writes_(InfFilePathSize) PWCHAR InfFilePath,
- IN ULONG InfFilePathSize
- )
-{
- LONG error = ERROR_SUCCESS;
-
- if (GetCurrentDirectoryW(InfFilePathSize, InfFilePath) == 0) {
- error = GetLastError();
- printf("InstallDriver failed! Error = %d \n", error);
- return error;
- }
- if (FAILED( StringCchCatW(InfFilePath,
- InfFilePathSize,
- NONPNP_INF_FILENAME) )) {
- error = ERROR_BUFFER_OVERFLOW;
- return error;
- }
- return error;
-
-}
-
-//----------------------------------------------------------------------------
-//
-//----------------------------------------------------------------------------
BOOLEAN
InstallDriver(
- IN SC_HANDLE SchSCManager,
- IN LPCTSTR DriverName,
- IN LPCTSTR ServiceExe
+ _In_ SC_HANDLE SchSCManager,
+ _In_ LPCTSTR DriverName,
+ _In_ LPCTSTR ServiceExe
)
/*++
@@ -130,10 +73,6 @@ Return Value:
{
SC_HANDLE schService;
DWORD err;
- WCHAR infPath[MAX_PATH];
- WDF_COINSTALLER_INSTALL_OPTIONS clientOptions;
-
- WDF_COINSTALLER_INSTALL_OPTIONS_INIT(&clientOptions);
//
// NOTE: This creates an entry for a standalone driver. If this
@@ -142,22 +81,6 @@ Return Value:
// query the registry for existing driver information
// (in order to determine a unique Tag, etc.).
//
- //
- // PRE-INSTALL for WDF support
- //
- err = GetPathToInf(infPath, ARRAY_SIZE(infPath) );
- if (err != ERROR_SUCCESS) {
- return FALSE;
- }
- err = pfnWdfPreDeviceInstallEx(infPath, WDF_SECTION_NAME, &clientOptions);
-
- if (err != ERROR_SUCCESS) {
- if (err == ERROR_SUCCESS_REBOOT_REQUIRED) {
- printf("System needs to be rebooted, before the driver installation can proceed.\n");
- }
-
- return FALSE;
- }
//
// Create a new a service object.
@@ -205,15 +128,10 @@ Return Value:
//
// Close the service object.
//
- CloseServiceHandle(schService);
- //
- // POST-INSTALL for WDF support
- //
- err = pfnWdfPostDeviceInstall( infPath, WDF_SECTION_NAME );
+ if (schService) {
- if (err != ERROR_SUCCESS) {
- return FALSE;
+ CloseServiceHandle(schService);
}
//
@@ -226,9 +144,9 @@ Return Value:
BOOLEAN
ManageDriver(
- IN LPCTSTR DriverName,
- IN LPCTSTR ServiceName,
- IN USHORT Function
+ _In_ LPCTSTR DriverName,
+ _In_ LPCTSTR ServiceName,
+ _In_ USHORT Function
)
{
@@ -337,7 +255,11 @@ ManageDriver(
//
// Close handle to service control manager.
//
- CloseServiceHandle(schSCManager);
+
+ if (schSCManager) {
+
+ CloseServiceHandle(schSCManager);
+ }
return rCode;
@@ -346,28 +268,12 @@ ManageDriver(
BOOLEAN
RemoveDriver(
- IN SC_HANDLE SchSCManager,
- IN LPCTSTR DriverName
+ _In_ SC_HANDLE SchSCManager,
+ _In_ LPCTSTR DriverName
)
{
SC_HANDLE schService;
BOOLEAN rCode;
- DWORD err;
- WCHAR infPath[MAX_PATH];
-
- err = GetPathToInf(infPath, ARRAY_SIZE(infPath) );
- if (err != ERROR_SUCCESS) {
- return FALSE;
- }
-
- //
- // PRE-REMOVE of WDF support
- //
- err = pfnWdfPreDeviceRemove( infPath, WDF_SECTION_NAME );
-
- if (err != ERROR_SUCCESS) {
- return FALSE;
- }
//
// Open the handle to the existing service.
@@ -415,15 +321,10 @@ RemoveDriver(
//
// Close the service object.
//
- CloseServiceHandle(schService);
- //
- // POST-REMOVE of WDF support
- //
- err = pfnWdfPostDeviceRemove(infPath, WDF_SECTION_NAME );
+ if (schService) {
- if (err != ERROR_SUCCESS) {
- rCode = FALSE;
+ CloseServiceHandle(schService);
}
return rCode;
@@ -434,59 +335,73 @@ RemoveDriver(
BOOLEAN
StartDriver(
- IN SC_HANDLE SchSCManager,
- IN LPCTSTR DriverName
+ _In_ SC_HANDLE SchSCManager,
+ _In_ LPCTSTR DriverName
)
{
SC_HANDLE schService;
DWORD err;
- BOOL ok;
//
// Open the handle to the existing service.
//
+
schService = OpenService(SchSCManager,
DriverName,
SERVICE_ALL_ACCESS
);
if (schService == NULL) {
+
+ printf("OpenService failed! Error = %d \n", GetLastError());
+
//
// Indicate failure.
//
- printf("OpenService failed! Error = %d\n", GetLastError());
+
return FALSE;
}
//
// Start the execution of the service (i.e. start the driver).
//
- ok = StartService( schService, 0, NULL );
- if (!ok) {
+ if (!StartService(schService, // service identifier
+ 0, // number of arguments
+ NULL // pointer to arguments
+ )) {
err = GetLastError();
if (err == ERROR_SERVICE_ALREADY_RUNNING) {
+
//
// Ignore this error.
//
+
return TRUE;
} else {
+
+ printf("StartService failure! Error = %d \n", err );
+
//
- // Indicate failure.
- // Fall through to properly close the service handle.
+ // Indicate failure. Fall through to properly close the service handle.
//
- printf("StartService failure! Error = %d\n", err );
+
return FALSE;
}
+
}
//
// Close the service object.
//
- CloseServiceHandle(schService);
+
+ if (schService) {
+
+ CloseServiceHandle(schService);
+ }
return TRUE;
@@ -496,8 +411,8 @@ StartDriver(
BOOLEAN
StopDriver(
- IN SC_HANDLE SchSCManager,
- IN LPCTSTR DriverName
+ _In_ SC_HANDLE SchSCManager,
+ _In_ LPCTSTR DriverName
)
{
BOOLEAN rCode = TRUE;
@@ -549,264 +464,87 @@ StopDriver(
//
// Close the service object.
//
- CloseServiceHandle (schService);
-
- return rCode;
-
-} // StopDriver
-
-
-//
-// Caller must free returned pathname string.
-//
-PCHAR
-BuildDriversDirPath(
- _In_ PSTR DriverName
- )
-{
- size_t remain;
- size_t len;
- PCHAR dir;
-
- if (!DriverName || strlen(DriverName) == 0) {
- return NULL;
- }
-
- remain = MAX_PATH;
-
- //
- // Allocate string space
- //
- dir = (PCHAR) malloc( remain + 1 );
-
- if (!dir) {
- return NULL;
- }
-
- //
- // Get the base windows directory path.
- //
- len = GetWindowsDirectory( dir, (UINT) remain );
-
- if (len == 0 ||
- (remain - len) < sizeof(SYSTEM32_DRIVERS)) {
- free(dir);
- return NULL;
- }
- remain -= len;
- //
- // Build dir to have "%windir%\System32\Drivers\<DriverName>".
- //
- if (FAILED( StringCchCat(dir, remain, SYSTEM32_DRIVERS) )) {
- free(dir);
- return NULL;
- }
-
- remain -= sizeof(SYSTEM32_DRIVERS);
- len += sizeof(SYSTEM32_DRIVERS);
- len += strlen(DriverName);
-
- if (remain < len) {
- free(dir);
- return NULL;
- }
+ if (schService) {
- if (FAILED( StringCchCat(dir, remain, DriverName) )) {
- free(dir);
- return NULL;
+ CloseServiceHandle (schService);
}
- dir[len] = '\0'; // keeps prefast happy
-
- return dir;
-}
+ return rCode;
+} // StopDriver
BOOLEAN
SetupDriverName(
- _Inout_updates_all_(BufferLength) PCHAR DriverLocation,
+ _Inout_updates_bytes_all_(BufferLength) PCHAR DriverLocation,
_In_ ULONG BufferLength
)
{
HANDLE fileHandle;
- DWORD driverLocLen = 0;
- BOOL ok;
- PCHAR driversDir;
+ DWORD driverLocLen = 0;
//
- // Setup path name to driver file.
+ // Get the current directory.
//
- driverLocLen =
- GetCurrentDirectory(BufferLength, DriverLocation);
- if (!driverLocLen) {
+ driverLocLen = GetCurrentDirectory(BufferLength,
+ DriverLocation
+ );
- printf("GetCurrentDirectory failed! Error = %d \n",
- GetLastError());
+ if (driverLocLen == 0) {
- return FALSE;
- }
+ printf("GetCurrentDirectory failed! Error = %d \n", GetLastError());
- if (FAILED( StringCchCat(DriverLocation, BufferLength, "\\" DRIVER_NAME ".sys") )) {
return FALSE;
}
//
- // Insure driver file is in the specified directory.
+ // Setup path name to driver file.
//
- fileHandle = CreateFile( DriverLocation,
- GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL );
-
- if (fileHandle == INVALID_HANDLE_VALUE) {
- //
- // Indicate failure.
- //
- printf("Driver: %s.SYS is not in the %s directory. \n",
- DRIVER_NAME, DriverLocation );
+ if (FAILED( StringCbCat(DriverLocation, BufferLength, "\\"DRIVER_NAME".sys") )) {
return FALSE;
}
//
- // Build %windir%\System32\Drivers\<DRIVER_NAME> path.
- // Copy the driver to %windir%\system32\drivers
+ // Insure driver file is in the specified directory.
//
- driversDir = BuildDriversDirPath( DRIVER_NAME ".sys" );
- if (!driversDir) {
- printf("BuildDriversDirPath failed!\n");
- return FALSE;
- }
+ if ((fileHandle = CreateFile(DriverLocation,
+ GENERIC_READ,
+ 0,
+ NULL,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL
+ )) == INVALID_HANDLE_VALUE) {
- ok = CopyFile( DriverLocation, driversDir, FALSE );
- if(!ok) {
- printf("CopyFile failed: error(%d) - \"%s\"\n",
- GetLastError(), driversDir );
- free(driversDir);
- return FALSE;
- }
+ printf("%s.sys is not loaded.\n", DRIVER_NAME);
+
+ //
+ // Indicate failure.
+ //
- if (FAILED( StringCchCopy(DriverLocation, BufferLength, driversDir) )) {
- free(driversDir);
return FALSE;
}
- free(driversDir);
-
//
// Close open file handle.
//
+
if (fileHandle) {
+
CloseHandle(fileHandle);
}
//
// Indicate success.
//
- return TRUE;
-
-} // SetupDriverName
+ return TRUE;
-HMODULE
-LoadWdfCoInstaller(
- VOID
- )
-{
- HMODULE library = NULL;
- DWORD error = ERROR_SUCCESS;
- CHAR szCurDir[MAX_PATH];
- CHAR tempCoinstallerName[MAX_PATH];
- PCHAR coinstallerVersion;
-
- do {
-
- if (GetCurrentDirectory(MAX_PATH, szCurDir) == 0) {
-
- printf("GetCurrentDirectory failed! Error = %d \n", GetLastError());
- break;
- }
- coinstallerVersion = GetCoinstallerVersion();
- if (FAILED( StringCchPrintf(tempCoinstallerName,
- MAX_PATH,
- "\\WdfCoInstaller%s.dll",
- coinstallerVersion) )) {
- break;
- }
- if (FAILED( StringCchCat(szCurDir, MAX_PATH, tempCoinstallerName) )) {
- break;
- }
-
- library = LoadLibrary(szCurDir);
-
- if (library == NULL) {
- error = GetLastError();
- printf("LoadLibrary(%s) failed: %d\n", szCurDir, error);
- break;
- }
-
- pfnWdfPreDeviceInstallEx =
- (PFN_WDFPREDEVICEINSTALLEX) GetProcAddress( library, "WdfPreDeviceInstallEx" );
-
- if (pfnWdfPreDeviceInstallEx == NULL) {
- error = GetLastError();
- printf("GetProcAddress(\"WdfPreDeviceInstallEx\") failed: %d\n", error);
- return NULL;
- }
-
- pfnWdfPostDeviceInstall =
- (PFN_WDFPOSTDEVICEINSTALL) GetProcAddress( library, "WdfPostDeviceInstall" );
-
- if (pfnWdfPostDeviceInstall == NULL) {
- error = GetLastError();
- printf("GetProcAddress(\"WdfPostDeviceInstall\") failed: %d\n", error);
- return NULL;
- }
-
- pfnWdfPreDeviceRemove =
- (PFN_WDFPREDEVICEREMOVE) GetProcAddress( library, "WdfPreDeviceRemove" );
-
- if (pfnWdfPreDeviceRemove == NULL) {
- error = GetLastError();
- printf("GetProcAddress(\"WdfPreDeviceRemove\") failed: %d\n", error);
- return NULL;
- }
-
- pfnWdfPostDeviceRemove =
- (PFN_WDFPREDEVICEREMOVE) GetProcAddress( library, "WdfPostDeviceRemove" );
-
- if (pfnWdfPostDeviceRemove == NULL) {
- error = GetLastError();
- printf("GetProcAddress(\"WdfPostDeviceRemove\") failed: %d\n", error);
- return NULL;
- }
-
- } WHILE (0);
-
- if (error != ERROR_SUCCESS) {
- if (library) {
- FreeLibrary( library );
- }
- library = NULL;
- }
- return library;
-}
+} // SetupDriverName
-VOID
-UnloadWdfCoInstaller(
- HMODULE Library
- )
-{
- if (Library) {
- FreeLibrary( Library );
- }
-}
diff --git a/general/ioctl/kmdf/exe/nonpnp.inf b/general/ioctl/kmdf/exe/nonpnp.inf
deleted file mode 100644
index b20a58b4..00000000
--- a/general/ioctl/kmdf/exe/nonpnp.inf
+++ /dev/null
@@ -1,8 +0,0 @@
-[Version]
-Signature="$WINDOWS NT$"
-
-[nonpnp.NT.Wdf]
-KmdfService = nonpnp, nonpnp_Service_kmdfInst
-
-[nonpnp_Service_kmdfInst]
-KmdfLibraryVersion = 1.11
diff --git a/general/ioctl/kmdf/exe/testapp.c b/general/ioctl/kmdf/exe/testapp.c
index 6586401e..492c6355 100644
--- a/general/ioctl/kmdf/exe/testapp.c
+++ b/general/ioctl/kmdf/exe/testapp.c
@@ -54,16 +54,6 @@ ManageDriver(
IN USHORT Function
);
-HMODULE
-LoadWdfCoInstaller(
- VOID
- );
-
-VOID
-UnloadWdfCoInstaller(
- HMODULE Library
- );
-
BOOLEAN
SetupDriverName(
_Inout_updates_all_(BufferLength) PCHAR DriverLocation,
@@ -80,14 +70,7 @@ DoIoctls(
HANDLE hDevice
);
-// for example, WDF 1.9 is "01009". the size 6 includes the ending NULL marker
-//
-#define MAX_VERSION_SIZE 6
-
-CHAR G_coInstallerVersion[MAX_VERSION_SIZE] = {0};
BOOLEAN G_fLoop = FALSE;
-BOOL G_versionSpecified = FALSE;
-
//-----------------------------------------------------------------------------
@@ -98,30 +81,9 @@ __pragma(warning(disable: 4127)) while(constant); __pragma(warning(default: 4127
#define USAGE \
-"Usage: nonpnpapp <-V version> <-l> \n" \
- " -V version {if no version is specified the version specified in the build environment will be used.}\n" \
- " The version is the version of the KMDF coinstaller to use \n" \
- " The format of version is MMmmm where MM -- major #, mmm - serial# \n" \
+"Usage: nonpnpapp <-l> \n" \
" -l { option to continuously read & write to the file} \n"
-BOOL
-ValidateCoinstallerVersion(
- _In_ PSTR Version
- )
-{ BOOL ok = FALSE;
- INT i;
-
- for(i= 0; i<MAX_VERSION_SIZE ;i++){
- if( ! IsCharAlphaNumericA(Version[i])) {
- break;
- }
- }
- if (i == (MAX_VERSION_SIZE -sizeof(CHAR))) {
- ok = TRUE;
- }
- return ok;
-}
-
LONG
Parse(
_In_ int argc,
@@ -143,42 +105,12 @@ Return Value:
--*/
{
int i;
- BOOL ok;
LONG error = ERROR_SUCCESS;
for (i=0; i<argc; i++) {
if (argv[i][0] == '-' ||
argv[i][0] == '/') {
switch(argv[i][1]) {
- case 'V':
- case 'v':
- if (( (i+1 < argc ) &&
- ( argv[i+1][0] != '-' && argv[i+1][0] != '/'))) {
- //
- // use version in commandline
- //
- i++;
- ok = ValidateCoinstallerVersion(argv[i]);
- if (!ok) {
- printf("Not a valid format for coinstaller version\n"
- "It should be characters between A-Z, a-z , 0-9\n"
- "The version format is MMmmm where MM -- major #, mmm - serial#");
- error = ERROR_INVALID_PARAMETER;
- break;
- }
- if (FAILED( StringCchCopy(G_coInstallerVersion,
- MAX_VERSION_SIZE,
- argv[i]) )) {
- break;
- }
- G_versionSpecified = TRUE;
-
- }
- else{
- printf(USAGE);
- error = ERROR_INVALID_PARAMETER;
- }
- break;
case 'l':
case 'L':
G_fLoop = TRUE;
@@ -193,24 +125,6 @@ Return Value:
return error;
}
-PCHAR
-GetCoinstallerVersion(
- VOID
- )
-{
- if (!G_versionSpecified &&
- FAILED( StringCchPrintf(G_coInstallerVersion,
- MAX_VERSION_SIZE,
- "%02d%03d", // for example, "01009"
- KMDF_VERSION_MAJOR,
- KMDF_VERSION_MINOR)))
- {
- printf("StringCchCopy failed with error \n");
- }
-
- return (PCHAR)&G_coInstallerVersion;
-}
-
VOID __cdecl
main(
_In_ ULONG argc,
@@ -221,9 +135,7 @@ main(
DWORD errNum = 0;
CHAR driverLocation [MAX_PATH];
BOOL ok;
- HMODULE library = NULL;
LONG error;
- PCHAR coinstallerVersion;
//
// Parse command line args
@@ -236,19 +148,6 @@ main(
}
}
- if (!G_versionSpecified ) {
- coinstallerVersion = GetCoinstallerVersion();
-
- //
- // if no version is specified or an invalid one is specified use default version
- //
- printf("No version specified. Using default version:%s\n",
- coinstallerVersion);
-
- } else {
- coinstallerVersion = (PCHAR)&G_coInstallerVersion;
- }
-
//
// open the device
//
@@ -273,17 +172,6 @@ main(
}
//
- // Load WdfCoInstaller.dll.
- //
- library = LoadWdfCoInstaller();
-
- if (library == NULL) {
- printf("The WdfCoInstaller%s.dll library needs to be "
- "in same directory as nonpnpapp.exe\n", coinstallerVersion);
- return;
- }
-
- //
// The driver is not started yet so let us install the driver.
// First setup full path to driver name.
//
@@ -351,12 +239,6 @@ main(
driverLocation,
DRIVER_FUNC_REMOVE );
- //
- // Unload WdfCoInstaller.dll
- //
- if ( library ) {
- UnloadWdfCoInstaller( library );
- }
return;
}