📄 config.c
字号:
/*++
Copyright (c) 1989-1997 Microsoft Corporation
Module Name:
config.c
Abstract:
This contains all routines necessary for the support of the dynamic
configuration.
This was originally ripped from the DDK TDI source sample and heavily
munged to reduce the amount of NT dependency
Revision History:
--*/
#include "ImSamp.h"
#pragma hdrstop
//
// Local functions used to access the registry.
//
NTSTATUS
ConfigureDriver (
IN PUNICODE_STRING RegistryPath,
IN PCONFIG_DATA ConfigurationInfo
);
STATIC MM_SYSTEMSIZE
ReadMemUsageInformation(
IN NDIS_HANDLE ConfigHandle
);
STATIC ULONG
ReadSingleParameter(
IN NDIS_HANDLE ParametersHandle,
IN PWCHAR ValueName,
IN ULONG DefaultValue,
IN NDIS_PARAMETER_TYPE ParamType
);
STATIC VOID
WriteSingleParameter(
IN NDIS_HANDLE ParametersHandle,
IN PWCHAR ValueName,
IN ULONG ValueData,
IN NDIS_PARAMETER_TYPE ParamType
);
STATIC VOID
SaveConfigInRegistry(
IN NDIS_HANDLE ParametersHandle,
IN PCONFIG_DATA ConfigurationInfo
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, ConfigureDriver)
#pragma alloc_text(INIT, ReadMemUsageInformation)
#pragma alloc_text(INIT, ReadSingleParameter)
#pragma alloc_text(INIT, WriteSingleParameter)
#pragma alloc_text(INIT, SaveConfigInRegistry)
#endif
//
// These strings are used in various places by the registry.
//
#define DECLARE_STRING( _str_ ) STATIC WCHAR Str ## _str_[] = L#_str_
DECLARE_STRING( Large );
DECLARE_STRING( Medium );
DECLARE_STRING( Small );
DECLARE_STRING( PacketPoolSize );
DECLARE_STRING( LookaheadPoolSize );
DECLARE_STRING( ResidualPoolSize );
DECLARE_STRING( MaxMemoryUsage );
DECLARE_STRING( DebugLevel );
DECLARE_STRING( DebugMask );
#define READ_HIDDEN_CONFIG( _Field, ParamType ) \
{ \
ConfigurationInfo->_Field = \
ReadSingleParameter(ConfigHandle, \
Str ## _Field, \
ConfigurationInfo->_Field, \
ParamType); \
}
#define WRITE_HIDDEN_CONFIG( _Field, ParamType ) \
{ \
WriteSingleParameter(ConfigHandle, \
Str ## _Field, \
ConfigurationInfo->_Field, \
ParamType); \
}
NTSTATUS
ConfigureDriver (
IN PUNICODE_STRING RegistryPath,
IN PCONFIG_DATA ConfigurationInfo
)
/*++
Routine Description:
This routine is called by the driver to get information from the configuration
management routines. We read the registry, starting at RegistryPath,
to get the parameters. If they don't exist, we use the defaults in this module.
Arguments:
RegistryPath - The name of the driver's node in the registry.
ConfigurationInfo - A pointer to the configuration information structure.
Return Value:
Status - STATUS_SUCCESS if everything OK, STATUS_INSUFFICIENT_RESOURCES
otherwise.
--*/
{
MM_SYSTEMSIZE DriverMemUsage;
NDIS_HANDLE ConfigHandle;
NDIS_STATUS Status;
NDIS_STRING IMParamsKey = NDIS_STRING_CONST("IMSamp\\Parameters" );
//
// Open the registry. For NT, we can cheat with NdisOpenProtocolConfiguration since
// it uses a different mechanism to access the registry. It also makes less assumptions
// about the string passed to it. On Memphis, if the string contains a backslash, then
// different, bad, things happen. On Memphis, you end looking in
// local machine\system\currentcontrolset\services\vxd\ImSamp
//
#if BINARY_COMPATIBLE
NdisOpenProtocolConfiguration( &Status, &ConfigHandle, RegistryPath );
#else
NdisOpenProtocolConfiguration( &Status, &ConfigHandle, &IMParamsKey );
#endif
if ( NT_SUCCESS( Status )) {
//
// Determine how much resources we should allocate
//
DriverMemUsage = ReadMemUsageInformation ( ConfigHandle );
} else {
DriverMemUsage = MmMediumSystem;
}
switch ( DriverMemUsage ) {
case MmSmallSystem:
ConfigurationInfo->PacketPoolSize = 20;
ConfigurationInfo->LookaheadPoolSize = 20;
ConfigurationInfo->ResidualPoolSize = 10;
ConfigurationInfo->MaxMemoryUsage = 50000;
break;
case MmMediumSystem:
ConfigurationInfo->PacketPoolSize = 50;
ConfigurationInfo->LookaheadPoolSize = 50;
ConfigurationInfo->ResidualPoolSize = 25;
ConfigurationInfo->MaxMemoryUsage = 100000;
break;
case MmLargeSystem:
ConfigurationInfo->PacketPoolSize = 100;
ConfigurationInfo->LookaheadPoolSize = 100;
ConfigurationInfo->ResidualPoolSize = 50;
ConfigurationInfo->MaxMemoryUsage = 0; // no limit
break;
default:
ASSERT( FALSE );
break;
}
//
// set parameters that are not dependent upon the size selected
//
ConfigurationInfo->DebugLevel = 0;
ConfigurationInfo->DebugMask = 0;
if ( NT_SUCCESS( Status )) {
//
// Now read the optional "hidden" parameters; if these do
// not exist then the current values are used. Note that
// the current values will be 0 unless they have been
// explicitly initialized above.
//
// NOTE: These macros expect "ConfigurationInfo" and
// "ParametersHandle" to exist when they are expanded.
//
READ_HIDDEN_CONFIG ( PacketPoolSize, NdisParameterInteger );
READ_HIDDEN_CONFIG ( LookaheadPoolSize, NdisParameterInteger );
READ_HIDDEN_CONFIG ( ResidualPoolSize, NdisParameterInteger );
READ_HIDDEN_CONFIG ( MaxMemoryUsage, NdisParameterInteger );
READ_HIDDEN_CONFIG ( DebugMask, NdisParameterHexInteger );
READ_HIDDEN_CONFIG ( DebugLevel, NdisParameterInteger );
//
// Now that we are completely configured, save the information
// in the registry.
//
SaveConfigInRegistry ( ConfigHandle, ConfigurationInfo );
NdisCloseConfiguration( ConfigHandle );
}
return STATUS_SUCCESS;
} /* ConfigureDriver */
STATIC MM_SYSTEMSIZE
ReadMemUsageInformation(
IN NDIS_HANDLE ConfigHandle
)
/*++
Routine Description:
This routine is called by the driver to read the memory usage information
from the registry.
Arguments:
ConfigHandle - A pointer to the open registry.
Return Value:
0 - not specified, 1
--*/
{
NDIS_STRING MemUsageValueName = NDIS_STRING_CONST( "MemoryUsage" );
PNDIS_CONFIGURATION_PARAMETER ConfigParam;
MM_SYSTEMSIZE ValueToReturn;
NDIS_STATUS Status;
//
// Read the memory usage parameter out of the registry.
//
NdisReadConfiguration(&Status,
&ConfigParam,
ConfigHandle,
&MemUsageValueName,
NdisParameterInteger);
if ( NT_SUCCESS( Status )) {
ValueToReturn = ConfigParam->ParameterData.IntegerData;
if ( ValueToReturn > MmLargeSystem || ValueToReturn < MmSmallSystem ) {
ValueToReturn = MmQuerySystemSize();
}
} else {
ValueToReturn = MmQuerySystemSize();
}
return ValueToReturn;
} /* ReadMemUsageInformation */
STATIC ULONG
ReadSingleParameter(
IN HANDLE ConfigHandle,
IN PWCHAR ValueName,
IN ULONG DefaultValue,
IN NDIS_PARAMETER_TYPE NdisParamType
)
/*++
Routine Description:
This routine is called by the driver to read a single parameter
from the registry. If the parameter is found it is stored
in Data.
Arguments:
ConfigHandle - A pointer to the open registry.
ValueName - The name of the value to search for.
DefaultValue - The default value.
Return Value:
The value to use; will be the default if the value is not
found or is not in the correct range.
--*/
{
UNICODE_STRING ValueKeyName;
ULONG ReturnValue;
NDIS_STATUS Status;
PNDIS_CONFIGURATION_PARAMETER ConfigParam;
IMAssert( NdisParamType == NdisParameterInteger || NdisParamType == NdisParameterHexInteger );
NdisInitUnicodeString( &ValueKeyName, ValueName );
NdisReadConfiguration(&Status,
&ConfigParam,
ConfigHandle,
&ValueKeyName,
NdisParamType);
if ( NT_SUCCESS( Status )) {
ReturnValue = ConfigParam->ParameterData.IntegerData;
} else {
ReturnValue = DefaultValue;
}
return ReturnValue;
} /* ReadSingleParameter */
STATIC VOID
WriteSingleParameter(
IN NDIS_HANDLE ConfigHandle,
IN PWCHAR ValueName,
IN ULONG ValueData,
IN NDIS_PARAMETER_TYPE NdisParamType
)
/*++
Routine Description:
This routine is called by the driver to write a single parameter
from the registry.
Arguments:
ConfigHandle - A pointer to the open registry.
ValueName - The name of the value to store.
ValueData - The data to store at the value.
Return Value:
None.
--*/
{
NDIS_STRING ValueKeyName;
NDIS_STATUS Status;
NDIS_CONFIGURATION_PARAMETER ConfigParam;
IMAssert( NdisParamType == NdisParameterInteger || NdisParamType == NdisParameterHexInteger );
NdisInitUnicodeString ( &ValueKeyName, ValueName );
ConfigParam.ParameterType = NdisParamType;
ConfigParam.ParameterData.IntegerData = ValueData;
NdisWriteConfiguration( &Status, ConfigHandle, &ValueKeyName, &ConfigParam );
if ( !NT_SUCCESS( Status )) {
ImDbgOut(DBG_FAILURE,
DBG_INIT,
("WriteSingleParameter: Could not write dword key: %08X\n", Status ));
}
} /* WriteSingleParameter */
STATIC VOID
SaveConfigInRegistry(
IN HANDLE ConfigHandle,
IN PCONFIG_DATA ConfigurationInfo
)
/*++
Routine Description:
This routine is called by the driver to save its configuration
information in the registry. It saves the information if
the registry structure did not exist before this boot.
Arguments:
ConfigHandle - The handle used to read other parameters.
ConfigurationInfo - Describes the driver's current configuration.
Return Value:
None.
--*/
{
//
// Save the "hidden" parameters, these may not exist in
// the registry.
//
// NOTE: These macros expect "ConfigurationInfo" and
// "ConfigHandle" to exist when they are expanded.
//
//
// Don't write the parameters that are set
// based on Size, since otherwise these will overwrite
// those values since hidden parameters are set up
// after the Size-based configuration is done.
//
#if !BINARY_COMPATIBLE
WRITE_HIDDEN_CONFIG( DebugLevel, NdisParameterInteger );
WRITE_HIDDEN_CONFIG( DebugMask, NdisParameterHexInteger );
#endif
} /* SaveConfigInRegistry */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -