/*
	Copyright (c) Microsoft Corporation.  All rights reserved.
*/

#include "sdv_prefixer.h"

#include "sdv_prefixer_ndis.h"

#include "sdv_prefixer_wdf.h"

#include "sdv_prefixer_netioapi.h"

#include "slic_base_ndis.h"

#include "slic_base_wdf.h"

#include "slic_base_netioapi.h"

#define sdv_irql_effective 0


#define FILE_AUTOGENERATED_DEVICE_NAME	128
#define FILE_DEVICE_DISK_FILE_SYSTEM	8
#define FILE_DEVICE_NETWORK_FILE_SYSTEM 20
#define FILE_DEVICE_SECURE_OPEN			256
#define FILE_DEVICE_TAPE_FILE_SYSTEM	32


#define IRP_MJ_CREATE 0
#define IRP_MJ_CREATE_NAMED_PIPE 1
#define IRP_MJ_CLOSE 2
#define IRP_MJ_READ 3
#define IRP_MJ_WRITE 4
#define IRP_MJ_DEVICE_CONTROL 14
#define IRP_MJ_INTERNAL_DEVICE_CONTROL 15
#define IRP_MJ_POWER 22
#define IRP_MJ_SHUTDOWN					16
#define IRP_MJ_CLEANUP					18
#define IRP_MJ_SYSTEM_CONTROL			23
#define IRP_MJ_PNP						27

#define IRP_MN_REMOVE_DEVICE 2
#define IRP_MN_START_DEVICE 0
#define IRP_MN_SURPRISE_REMOVAL 23

#define IRP_MN_CANCEL_STOP_DEVICE 6
#define IRP_MN_CANCEL_REMOVE_DEVICE 3

#define IRP_MN_WAIT_WAKE		 0
#define IRP_MN_QUERY_STOP_DEVICE 5
#define IRP_MN_QUERY_REMOVE_DEVICE 1
#define IRP_MN_QUERY_DEVICE_RELATIONS 7
#define IRP_MN_QUERY_INTERFACE 8
#define IRP_MN_QUERY_CAPABILITIES 9
#define IRP_MN_QUERY_RESOURCES 10
#define IRP_MN_QUERY_RESOURCE_REQUIREMENTS 11
#define IRP_MN_QUERY_DEVICE_TEXT 12
#define IRP_MN_QUERY_ID 19
#define IRP_MN_QUERY_PNP_DEVICE_STATE 20
#define IRP_MN_QUERY_BUS_INFORMATION 21
#define IRP_MN_QUERY_LEGACY_BUS_INFORMATION 24

#define IRP_MN_POWER_SEQUENCE				0
#define IRP_MN_SET_POWER					2
#define IRP_MN_QUERY_POWER					3

#define IO_TYPE_ADAPTER					1
#define IO_TYPE_CONTROLLER				2
#define IO_TYPE_DEVICE					3
#define IO_TYPE_DRIVER					4
#define IO_TYPE_FILE					5
#define IO_TYPE_IRP						6
#define IO_TYPE_MASTER_ADAPTER			7
#define IO_TYPE_OPEN_PACKET				8
#define IO_TYPE_TIMER					9
#define IO_TYPE_VPB						10
#define IO_TYPE_ERROR_LOG				11
#define IO_TYPE_ERROR_MESSAGE			12
#define IO_TYPE_DEVICE_OBJECT_EXTENSION 13

#define NT_SUCCESS(Status) (Status >= 0)
#define STATUS_INSUFFICIENT_RESOURCES 3221225626
#define STATUS_UNSUCCESSFUL 3221225473
#define NULL 0

#define PowerDeviceD3 4
#define PowerDeviceD2 3
#define PowerDeviceD1 2
#define PowerDeviceD0 1

#define STATUS_NOT_SUPPORTED 3221225659
#define STATUS_DELETE_PENDING 3221225558
#define STATUS_NO_SUCH_DEVICE 3221225486
#define STATUS_PENDING 259
#define STATUS_SUCCESS 0
#define STATUS_CONTINUE_COMPLETION STATUS_SUCCESS
#define STATUS_MORE_PROCESSING_REQUIRED 3221225494
#define STATUS_INVALID_DEVICE_STATE 3221225860
#define STATUS_IO_TIMEOUT 3221225653
#define STATUS_CANCELLED				 3221225760
#define STATUS_INVALID_PARAMETER		3221225485

#define IRP_MJ_PNP 27

#define WMIREG_ACTION_REGISTER		1
#define WMIREG_ACTION_DEREGISTER	2

#define KernelMode 0
#define UserMode 1
#define MaximumMode 2

#define BusRelations 0
#define EjectionRelations 1
#define PowerRelations 2
#define RemovalRelations 3
#define TargetDeviceRelation 4
#define SingleBusRelations 5


#define POOL_TYPE_MASK 7


#define OBJ_KERNEL_HANDLE 512


/*
  IRQL

  The SDV Rules specifies IRQL requirements for various DDIs.

  The IRQLs are positive ordered numbers starting from 0.  The actual
  numbers are processer specific, on X86 ranging from 0 to 31 and on
  AMD64 and IA64 ranging from 0 to 15.	For all three platforms
  PASSIVE_LEVEL, APC_LEVEL and DISPATCH_LEVEL are respectively 0, 1
  and 2.

  SDV rules only distinguish between the concrete values
  PASSIVE_LEVEL, APC_LEVEL, DISPATCH_LEVEL and the predicate
  SDV_IRQL_ELEVATED_LEVEL for 'everything else' (3-31 on X86, 3-15 on
  AMD64 or IA64).  The SDV rules are unable to distinguish between
  DIRQL, CMC_LEVEL, HIGH_LEVEL, etc.  The OS header files provide the
  equivalent macro definitions except for the SDV_IRQL_ELEVATED_LEVEL
  predicate.

  The SDV OS Model declares the SDV_DIRQL macro for elevating to an
  arbitrary Device IRQL.  The definition of SDV_DIRQL and the
  definition of SDV_IRQL_ELEVATED_LEVEL implies that
  SDV_IRQL_ELEVATED_LEVEL(SDV_DIRQL)==TRUE.

  See the article "Scheduling, Thread Context, and IRQL" for more
  details.
*/

#define HIGH_LEVEL 31
#define POWER_LEVEL 30
#define IPI_LEVEL 29
#define PROFILE_LEVEL 27

#define PASSIVE_LEVEL 0 
#define APC_LEVEL 1 
#define DISPATCH_LEVEL 2 

#define SDV_IRQL_ELEVATED_LEVEL(irql) ((irql)!=PASSIVE_LEVEL && (irql)!=APC_LEVEL && (irql)!=DISPATCH_LEVEL)

#define SDV_IRQL_PREVIOUS_EQUAL(irql) (irql == sdv_irql_previous)

#define LTDIRQL(irql)					(!SDV_IRQL_ELEVATED_LEVEL(irql))
#define LTDISPATCH(irql)				((irql)==PASSIVE_LEVEL || (irql)==APC_LEVEL)



#define STACKL Tail.Overlay.CurrentStackLocation


#define FILE_DEVICE_8042_PORT	39
#define FILE_DEVICE_ACPI	50
#define FILE_DEVICE_BATTERY 41
#define FILE_DEVICE_BEEP	1
#define FILE_DEVICE_BUS_EXTENDER	42
#define FILE_DEVICE_CD_ROM	2
#define FILE_DEVICE_CD_ROM_FILE_SYSTEM	3
#define FILE_DEVICE_CHANGER 48
#define FILE_DEVICE_CONTROLLER	4
#define FILE_DEVICE_DATALINK	5
#define FILE_DEVICE_DFS 6
#define FILE_DEVICE_DFS_FILE_SYSTEM 53
#define FILE_DEVICE_DFS_VOLUME	54
#define FILE_DEVICE_DISK	7
#define FILE_DEVICE_DISK_FILE_SYSTEM	8
#define FILE_DEVICE_DVD 51
#define FILE_DEVICE_FILE_SYSTEM 9
#define FILE_DEVICE_FIPS	58
#define FILE_DEVICE_FULLSCREEN_VIDEO	52
#define FILE_DEVICE_INPORT_PORT 10
#define FILE_DEVICE_KEYBOARD	11
#define FILE_DEVICE_KS	47
#define FILE_DEVICE_KSEC	57
#define FILE_DEVICE_MAILSLOT	12
#define FILE_DEVICE_MASS_STORAGE	45
#define FILE_DEVICE_MIDI_IN 13
#define FILE_DEVICE_MIDI_OUT	14
#define FILE_DEVICE_MODEM	43
#define FILE_DEVICE_MOUSE	15
#define FILE_DEVICE_MULTI_UNC_PROVIDER	16
#define FILE_DEVICE_NAMED_PIPE	17
#define FILE_DEVICE_NETWORK 18
#define FILE_DEVICE_NETWORK_BROWSER 19
#define FILE_DEVICE_NETWORK_FILE_SYSTEM 20
#define FILE_DEVICE_NETWORK_REDIRECTOR	40
#define FILE_DEVICE_NULL	21
#define FILE_DEVICE_PARALLEL_PORT	22
#define FILE_DEVICE_PHYSICAL_NETCARD	23
#define FILE_DEVICE_PRINTER 24
#define FILE_DEVICE_SCANNER 25
#define FILE_DEVICE_SCREEN	28
#define FILE_DEVICE_SERENUM 55
#define FILE_DEVICE_SERIAL_MOUSE_PORT	26
#define FILE_DEVICE_SERIAL_PORT 27
#define FILE_DEVICE_SMARTCARD	49
#define FILE_DEVICE_SMB 46
#define FILE_DEVICE_SOUND	29
#define FILE_DEVICE_STREAMS 30
#define FILE_DEVICE_TAPE	31
#define FILE_DEVICE_TAPE_FILE_SYSTEM	32
#define FILE_DEVICE_TERMSRV 57
#define FILE_DEVICE_TRANSPORT	33
#define FILE_DEVICE_UNKNOWN 34
#define FILE_DEVICE_VDM 44
#define FILE_DEVICE_VIDEO	35
#define FILE_DEVICE_VIRTUAL_DISK	36
#define FILE_DEVICE_WAVE_IN 37
#define FILE_DEVICE_WAVE_OUT	38


#define IrpProcessed 0
#define IrpNotCompleted 1
#define IrpNotWmi 2
#define IrpForward 3

#define DO_VERIFY_VOLUME					2	   
#define DO_BUFFERED_IO						4	   
#define DO_EXCLUSIVE						8	   
#define DO_DIRECT_IO						16		
#define DO_MAP_IO_BUFFER					32		
#define DO_DEVICE_INITIALIZING				128		 
#define DO_SHUTDOWN_REGISTERED				2048	  
#define DO_BUS_ENUMERATED_DEVICE			4096	  
#define DO_POWER_PAGABLE					8192	  
#define DO_POWER_INRUSH						16384

/*

#define PowerSystemUnspecified	  0
#define PowerSystemWorking		  1
#define PowerSystemSleeping1	  2
#define PowerSystemSleeping2	  3
#define PowerSystemSleeping3	  4
#define PowerSystemHibernate	  5
#define PowerSystemShutdown		  6
#define PowerSystemMaximum		  7 


#define	 PowerDeviceUnspecified	 0
#define	 PowerDeviceD0 1
#define	 PowerDeviceD1 2
#define	 PowerDeviceD2 3
#define	 PowerDeviceD3 4
#define	 PowerDeviceMaximum 5*/


#define SDV_RunDispatchFunction \
 fun_IRP_MJ_CREATE,\
 fun_IRP_MJ_CREATE_NAMED_PIPE,\
 fun_IRP_MJ_CLOSE,\
 fun_IRP_MJ_READ,\
 fun_IRP_MJ_WRITE,\
 fun_IRP_MJ_QUERY_INFORMATION,\
 fun_IRP_MJ_SET_INFORMATION,\
 fun_IRP_MJ_QUERY_EA,\
 fun_IRP_MJ_SET_EA,\
 fun_IRP_MJ_FLUSH_BUFFERS,\
 fun_IRP_MJ_QUERY_VOLUME_INFORMATION,\
 fun_IRP_MJ_SET_VOLUME_INFORMATION,\
 fun_IRP_MJ_DIRECTORY_CONTROL,\
 fun_IRP_MJ_FILE_SYSTEM_CONTROL,\
 fun_IRP_MJ_DEVICE_CONTROL,\
 fun_IRP_MJ_INTERNAL_DEVICE_CONTROL,\
 fun_IRP_MJ_SHUTDOWN,\
 fun_IRP_MJ_LOCK_CONTROL,\
 fun_IRP_MJ_CLEANUP,\
 fun_IRP_MJ_CREATE_MAILSLOT,\
 fun_IRP_MJ_QUERY_SECURITY,\
 fun_IRP_MJ_SET_SECURITY,\
 fun_IRP_MJ_POWER,\
 fun_IRP_MJ_SYSTEM_CONTROL,\
 fun_IRP_MJ_DEVICE_CHANGE,\
 fun_IRP_MJ_QUERY_QUOTA,\
 fun_IRP_MJ_SET_QUOTA,\
 fun_IRP_MJ_PNP

#define SDV_RunRemoveDevice fun_IRP_MJ_PNP
#define SDV_RunStartDevice	fun_IRP_MJ_PNP

#define SDV_DRIVER_CANCEL \
 fun_DRIVER_CANCEL_1,\
 fun_DRIVER_CANCEL_2,\
 fun_DRIVER_CANCEL_3,\
 fun_DRIVER_CANCEL_4,\
 fun_DRIVER_CANCEL_5,\
 fun_DRIVER_CANCEL_6,\
 fun_DRIVER_CANCEL_7

#define SDV_KDEFERRED_ROUTINE \
 fun_KDEFERRED_ROUTINE_1,\
 fun_KDEFERRED_ROUTINE_2,\
 fun_KDEFERRED_ROUTINE_3,\
 fun_KDEFERRED_ROUTINE_4,\
 fun_KDEFERRED_ROUTINE_5,\
 fun_KDEFERRED_ROUTINE_6,\
 fun_KDEFERRED_ROUTINE_7,\
 fun_KDEFERRED_ROUTINE_8,\
 fun_KDEFERRED_ROUTINE_9

#define SDV_KSERVICE_ROUTINE \
fun_KSERVICE_ROUTINE_1,\
fun_KSERVICE_ROUTINE_2,\
fun_KSERVICE_ROUTINE_3,\
fun_KSERVICE_ROUTINE_4,\
fun_KSERVICE_ROUTINE_5,\
fun_KSERVICE_ROUTINE_6

#define SDV_WORKER_THREAD_ROUTINE \
 fun_WORKER_THREAD_ROUTINE_1,\
 fun_WORKER_THREAD_ROUTINE_2,\
 fun_WORKER_THREAD_ROUTINE_3,\
 fun_WORKER_THREAD_ROUTINE_4,\
 fun_WORKER_THREAD_ROUTINE_5,\
 fun_WORKER_THREAD_ROUTINE_6


#define SDV_IO_COMPLETION_ROUTINE \
 fun_IO_COMPLETION_ROUTINE_1,\
 fun_IO_COMPLETION_ROUTINE_2,\
 fun_IO_COMPLETION_ROUTINE_3,\
 fun_IO_COMPLETION_ROUTINE_4,\
 fun_IO_COMPLETION_ROUTINE_5,\
 fun_IO_COMPLETION_ROUTINE_6,\
 fun_IO_COMPLETION_ROUTINE_7,\
 fun_IO_COMPLETION_ROUTINE_8,\
 fun_IO_COMPLETION_ROUTINE_9,\
 fun_IO_COMPLETION_ROUTINE_10,\
 fun_IO_COMPLETION_ROUTINE_11,\
 fun_IO_COMPLETION_ROUTINE_12
 
#define SDV_IO_DPC_ROUTINE \
 fun_IO_DPC_ROUTINE_1,\
 fun_IO_DPC_ROUTINE_2,\
 fun_IO_DPC_ROUTINE_3,\
 fun_IO_DPC_ROUTINE_4,\
 fun_IO_DPC_ROUTINE_5,\
 fun_IO_DPC_ROUTINE_6

#define SDV_IO_WORKITEM_ROUTINE \
 fun_IO_WORKITEM_ROUTINE_1,\
 fun_IO_WORKITEM_ROUTINE_2,\
 fun_IO_WORKITEM_ROUTINE_3,\
 fun_IO_WORKITEM_ROUTINE_4,\
 fun_IO_WORKITEM_ROUTINE_5,\
 fun_IO_WORKITEM_ROUTINE_6,\
 fun_IO_WORKITEM_ROUTINE_7,\
 fun_IO_WORKITEM_ROUTINE_8,\
 fun_IO_WORKITEM_ROUTINE_9,\
 fun_IO_WORKITEM_ROUTINE_10

#define SDV_IO_WORKITEM_ROUTINE_EX \
 fun_IO_WORKITEM_ROUTINE_EX_1,\
 fun_IO_WORKITEM_ROUTINE_EX_2,\
 fun_IO_WORKITEM_ROUTINE_EX_3,\
 fun_IO_WORKITEM_ROUTINE_EX_4,\
 fun_IO_WORKITEM_ROUTINE_EX_5,\
 fun_IO_WORKITEM_ROUTINE_EX_6,\
 fun_IO_WORKITEM_ROUTINE_EX_7,\
 fun_IO_WORKITEM_ROUTINE_EX_8,\
 fun_IO_WORKITEM_ROUTINE_EX_9,\
 fun_IO_WORKITEM_ROUTINE_EX_10

#define SDV_REQUEST_POWER_COMPLETE \
 fun_REQUEST_POWER_COMPLETE_1,\
 fun_REQUEST_POWER_COMPLETE_2,\
 fun_REQUEST_POWER_COMPLETE_3,\
 fun_REQUEST_POWER_COMPLETE_4,\
 fun_REQUEST_POWER_COMPLETE_5,\
 fun_REQUEST_POWER_COMPLETE_6,\
 fun_REQUEST_POWER_COMPLETE_7,\
 fun_REQUEST_POWER_COMPLETE_8,\
 fun_REQUEST_POWER_COMPLETE_9,\
 fun_REQUEST_POWER_COMPLETE_10
 
#define SDV_PO_RUNTIME_CALLBACKS \
 fun_PO_FX_COMPONENT_IDLE_STATE_CALLBACK,\
 fun_PO_FX_COMPONENT_ACTIVE_CONDITION_CALLBACK,\
 fun_PO_FX_DEVICE_POWER_REQUIRED_CALLBACK,\
 fun_PO_FX_POWER_CONTROL_CALLBACK,\
 fun_PO_FX_COMPONENT_IDLE_CONDITION_CALLBACK,\
 fun_PO_FX_DEVICE_POWER_NOT_REQUIRED_CALLBACK,\
 fun_PO_FX_COMPONENT_CRITICAL_TRANSITION_CALLBACK 
 
#define SDV_STANDARD_DRIVER_ROUTINES \
 SDV_RunDispatchFunction,\
 fun_DriverEntry,\
 fun_AddDevice,\
 fun_DriverUnload,\
 fun_DriverStartIo,\
 SDV_IO_COMPLETION_ROUTINE

#define SDV_SPECIAL_DRIVER_ROUTINES	 \
 SDV_KDEFERRED_ROUTINE,\
 SDV_KSERVICE_ROUTINE,\
 SDV_DRIVER_CANCEL,\
 SDV_WORKER_THREAD_ROUTINE,\
 SDV_IO_DPC_ROUTINE,\
 SDV_IO_WORKITEM_ROUTINE,\
 SDV_IO_WORKITEM_ROUTINE_EX,\
 SDV_REQUEST_POWER_COMPLETE


#define EFLAGS_IF_MASK 512

 
#define PO_FX_FLAG_BLOCKING	  1
#define PO_FX_FLAG_ASYNC_ONLY 2

#define NonPagedPool	0
#define NonPagedPoolSession 32
#define PagedPool	1
#define NonPagedPoolCacheAlignedMustS	6
#define NonPagedPoolSessionNx	544
#define NonPagedPoolMustSucceed 2
#define DontUseThisType 3
#define PagedPoolCacheAlignedSession	37
#define MaxPoolType 7
#define NonPagedPoolCacheAligned	4
#define NonPagedPoolNx	512
#define PagedPoolSession	33
#define NonPagedPoolMustSucceedSession	34
#define PagedPoolCacheAligned	5
#define NonPagedPoolCacheAlignedSession 36
#define DontUseThisTypeSession	35
#define NonPagedPoolCacheAlignedMustSSession	38
#define NonPagedPoolNxCacheAligned	516
