ReactOS  0.4.14-dev-390-g34947ad
image.c File Reference
#include "precomp.h"
#include <ntimage.h>
#include <ndk/ldrfuncs.h>
#include <debug.h>
Include dependency graph for image.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

KSDDKAPI NTSTATUS NTAPI KsLoadResource (IN PVOID ImageBase, IN POOL_TYPE PoolType, IN ULONG_PTR ResourceName, IN ULONG ResourceType, OUT PVOID *Resource, OUT PULONG ResourceSize)
 
NTSTATUS KspQueryRegValue (IN HANDLE KeyHandle, IN LPWSTR KeyName, IN PVOID Buffer, IN OUT PULONG BufferLength, OUT PULONG Type)
 
KSDDKAPI NTSTATUS NTAPI KsGetImageNameAndResourceId (IN HANDLE RegKey, OUT PUNICODE_STRING ImageName, OUT PULONG_PTR ResourceId, OUT PULONG ValueType)
 
KSDDKAPI NTSTATUS NTAPI KsMapModuleName (IN PDEVICE_OBJECT PhysicalDeviceObject, IN PUNICODE_STRING ModuleName, OUT PUNICODE_STRING ImageName, OUT PULONG_PTR ResourceId, OUT PULONG ValueType)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 14 of file image.c.

Function Documentation

◆ KsGetImageNameAndResourceId()

KSDDKAPI NTSTATUS NTAPI KsGetImageNameAndResourceId ( IN HANDLE  RegKey,
OUT PUNICODE_STRING  ImageName,
OUT PULONG_PTR  ResourceId,
OUT PULONG  ValueType 
)

Definition at line 115 of file image.c.

120 {
122  ULONG ImageLength;
123  WCHAR ImagePath[] = {L"\\SystemRoot\\system32\\drivers\\"};
124 
125  /* first clear the provided ImageName */
126  ImageName->Buffer = NULL;
127  ImageName->Length = ImageName->MaximumLength = 0;
128 
129  ImageLength = 0;
130  /* retrieve length of image name */
131  Status = KspQueryRegValue(RegKey, L"Image", NULL, &ImageLength, NULL);
132 
134  {
135  /* key value doesnt exist */
136  return Status;
137  }
138 
139  /* allocate image name buffer */
140  ImageName->MaximumLength = (USHORT)(sizeof(ImagePath) + ImageLength);
141  ImageName->Buffer = AllocateItem(PagedPool, ImageName->MaximumLength);
142 
143  /* check for success */
144  if (!ImageName->Buffer)
145  {
146  /* insufficient memory */
148  }
149 
150  /* copy image name */
151  RtlCopyMemory(ImageName->Buffer, ImagePath, sizeof(ImagePath));
152 
153  /* retrieve image name */
154  Status = KspQueryRegValue(RegKey,
155  L"Image",
156  &ImageName->Buffer[sizeof(ImagePath) / sizeof(WCHAR)],
157  &ImageLength,
158  NULL);
159 
160  if (!NT_SUCCESS(Status))
161  {
162  /* unexpected error */
163  FreeItem(ImageName->Buffer);
164  return Status;
165  }
166 
167  /* now query for resource id length*/
168  ImageLength = 0;
169  Status = KspQueryRegValue(RegKey, L"ResourceId", NULL, &ImageLength, ValueType);
170 
171  /* allocate resource id buffer*/
172  *ResourceId = (ULONG_PTR)AllocateItem(PagedPool, ImageLength);
173 
174  /* check for success */
175  if (!*ResourceId)
176  {
177  /* insufficient memory */
178  FreeItem(ImageName->Buffer);
180  }
181  /* now query for resource id */
182  Status = KspQueryRegValue(RegKey, L"ResourceId", (PVOID)*ResourceId, &ImageLength, ValueType);
183 
184  if (!NT_SUCCESS(Status))
185  {
186  /* unexpected error */
187  FreeItem(ImageName->Buffer);
188  FreeItem((PVOID)*ResourceId);
189  }
190 
191  /* return result */
192  return Status;
193 }
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ PCWSTR _In_z_ PCWSTR _In_ ULONG ValueType
Definition: rtlfuncs.h:4016
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30
LONG NTSTATUS
Definition: precomp.h:26
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
smooth NULL
Definition: ftsmooth.c:416
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static const char * ImageName
Definition: image.c:34
static const WCHAR L[]
Definition: oid.c:1250
Status
Definition: gdiplustypes.h:24
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
unsigned short USHORT
Definition: pedump.c:61
NTSTATUS KspQueryRegValue(IN HANDLE KeyHandle, IN LPWSTR KeyName, IN PVOID Buffer, IN OUT PULONG BufferLength, OUT PULONG Type)
Definition: image.c:98
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101

Referenced by KsMapModuleName().

◆ KsLoadResource()

KSDDKAPI NTSTATUS NTAPI KsLoadResource ( IN PVOID  ImageBase,
IN POOL_TYPE  PoolType,
IN ULONG_PTR  ResourceName,
IN ULONG  ResourceType,
OUT PVOID Resource,
OUT PULONG  ResourceSize 
)

Definition at line 23 of file image.c.

30 {
32  LDR_RESOURCE_INFO ResourceInfo;
33  PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry;
34  PVOID Data;
35  ULONG Size;
36  PVOID _SEH2_VOLATILE Result = NULL;
37 
38  /* set up resource info */
39  ResourceInfo.Type = ResourceType;
40  ResourceInfo.Name = ResourceName;
41  ResourceInfo.Language = 0;
42 
43  _SEH2_TRY
44  {
45  /* find the resource */
46  Status = LdrFindResource_U(ImageBase, &ResourceInfo, RESOURCE_DATA_LEVEL, &ResourceDataEntry);
47  if (NT_SUCCESS(Status))
48  {
49  /* try accessing it */
50  Status = LdrAccessResource(ImageBase, ResourceDataEntry, &Data, &Size);
51  if (NT_SUCCESS(Status))
52  {
53  /* allocate resource buffer */
55  if (Result)
56  {
57  /* copy resource */
59  /* store result */
60  *Resource = Result;
61 
62  if (ResourceSize)
63  {
64  /* resource size is optional */
65  *ResourceSize = Size;
66  }
67  }
68  else
69  {
70  /* not enough memory */
72  }
73  }
74  }
75  }
77  {
78  /* Exception, get the error code */
80  }
81  _SEH2_END;
82 
83  if (!NT_SUCCESS(Status))
84  {
85  /* failed */
86  if (Result)
87  {
88  /* free resource buffer in case of a failure */
90  }
91  }
92  /* done */
93  return Status;
94 }
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30
LONG NTSTATUS
Definition: precomp.h:26
ULONG_PTR Type
Definition: ldrtypes.h:181
#define RtlMoveMemory(Destination, Source, Length)
Definition: typedefs.h:263
_SEH2_TRY
Definition: create.c:4250
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
_Acquires_exclusive_lock_ Resource _Acquires_shared_lock_ Resource _Inout_ PERESOURCE Resource
Definition: cdprocs.h:848
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
ULONG_PTR Name
Definition: ldrtypes.h:182
smooth NULL
Definition: ftsmooth.c:416
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
_In_ LPGUID _In_ PVOID Data
Definition: classpnp.h:778
Definition: pedump.c:457
NTSTATUS NTAPI LdrFindResource_U(_In_ PVOID BaseAddress, _In_ PLDR_RESOURCE_INFO ResourceInfo, _In_ ULONG Level, _Out_ PIMAGE_RESOURCE_DATA_ENTRY *ResourceDataEntry)
NTSTATUS NTAPI LdrAccessResource(_In_ PVOID BaseAddress, _In_ PIMAGE_RESOURCE_DATA_ENTRY ResourceDataEntry, _Out_opt_ PVOID *Resource, _Out_opt_ PULONG Size)
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
#define RESOURCE_DATA_LEVEL
Definition: ldrtypes.h:33
_SEH2_END
Definition: create.c:4424
ULONG_PTR Language
Definition: ldrtypes.h:183
unsigned int ULONG
Definition: retypes.h:1
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
_Must_inspect_result_ _In_ FLT_CONTEXT_TYPE _In_ SIZE_T _In_ POOL_TYPE PoolType
Definition: fltkernel.h:1444

◆ KsMapModuleName()

KSDDKAPI NTSTATUS NTAPI KsMapModuleName ( IN PDEVICE_OBJECT  PhysicalDeviceObject,
IN PUNICODE_STRING  ModuleName,
OUT PUNICODE_STRING  ImageName,
OUT PULONG_PTR  ResourceId,
OUT PULONG  ValueType 
)

Definition at line 201 of file image.c.

207 {
210  UNICODE_STRING Modules = RTL_CONSTANT_STRING(L"Modules\\");
211  HANDLE hKey, hSubKey;
213 
214  /* first open device key */
216 
217  /* check for success */
218  if (!NT_SUCCESS(Status))
219  {
220  /* invalid parameter */
222  }
223 
224  /* initialize subkey buffer */
225  SubKeyName.Length = 0;
226  SubKeyName.MaximumLength = Modules.MaximumLength + ModuleName->MaximumLength;
227  SubKeyName.Buffer = AllocateItem(PagedPool, SubKeyName.MaximumLength);
228 
229  /* check for success */
230  if (!SubKeyName.Buffer)
231  {
232  /* not enough memory */
233  ZwClose(hKey);
234  return STATUS_NO_MEMORY;
235  }
236 
237  /* build subkey string */
240 
241  /* initialize subkey attributes */
243 
244  /* now open the subkey */
245  Status = ZwOpenKey(&hSubKey, GENERIC_READ, &ObjectAttributes);
246 
247  /* check for success */
248  if (NT_SUCCESS(Status))
249  {
250  /* defer work */
251  Status = KsGetImageNameAndResourceId(hSubKey, ImageName, ResourceId, ValueType);
252 
253  /* close subkey */
254  ZwClose(hSubKey);
255  }
256 
257  /* free subkey string */
258  FreeItem(SubKeyName.Buffer);
259 
260  /* close device key */
261  ZwClose(hKey);
262 
263  /* return status */
264  return Status;
265 }
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ PNDIS_STRING SubKeyName
Definition: ndis.h:4723
USHORT MaximumLength
Definition: env_spec_w32.h:370
#define PLUGPLAY_REGKEY_DEVICE
Definition: iofuncs.h:2738
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
_In_ PCWSTR _In_z_ PCWSTR _In_ ULONG ValueType
Definition: rtlfuncs.h:4016
PVOID AllocateItem(IN POOL_TYPE PoolType, IN SIZE_T NumberOfBytes)
Definition: misc.c:30
LONG NTSTATUS
Definition: precomp.h:26
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char UINT32 ACPI_STATUS const char UINT32 const char const char * ModuleName
Definition: acpixf.h:1270
PDEVICE_OBJECT PhysicalDeviceObject
Definition: btrfs_drv.h:1122
VOID FreeItem(IN PVOID Item)
Definition: misc.c:43
smooth NULL
Definition: ftsmooth.c:416
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static const char * ImageName
Definition: image.c:34
static const WCHAR L[]
Definition: oid.c:1250
#define GENERIC_READ
Definition: compat.h:124
Status
Definition: gdiplustypes.h:24
#define STATUS_NO_MEMORY
Definition: ntstatus.h:246
NTSYSAPI NTSTATUS NTAPI RtlAppendUnicodeStringToString(PUNICODE_STRING Destination, PUNICODE_STRING Source)
NTSTATUS NTAPI IoOpenDeviceRegistryKey(IN PDEVICE_OBJECT DeviceObject, IN ULONG DevInstKeyType, IN ACCESS_MASK DesiredAccess, OUT PHANDLE DevInstRegKey)
Definition: pnpmgr.c:4586
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
KSDDKAPI NTSTATUS NTAPI KsGetImageNameAndResourceId(IN HANDLE RegKey, OUT PUNICODE_STRING ImageName, OUT PULONG_PTR ResourceId, OUT PULONG ValueType)
Definition: image.c:115
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14

◆ KspQueryRegValue()

NTSTATUS KspQueryRegValue ( IN HANDLE  KeyHandle,
IN LPWSTR  KeyName,
IN PVOID  Buffer,
IN OUT PULONG  BufferLength,
OUT PULONG  Type 
)

Definition at line 98 of file image.c.

104 {
106  return STATUS_NOT_IMPLEMENTED;
107 }
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:225
#define UNIMPLEMENTED
Definition: debug.h:114

Referenced by KsGetImageNameAndResourceId().