xref: /reactos/base/setup/lib/utils/devutils.c (revision ad5d9aa2)
1 /*
2  * PROJECT:     ReactOS Setup Library
3  * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE:     Device utility functions
5  * COPYRIGHT:   Copyright 2024 Hermès Bélusca-Maïto <hermes.belusca-maito@reactos.org>
6  */
7 
8 #include "precomp.h"
9 #include "devutils.h"
10 
11 /* FUNCTIONS *****************************************************************/
12 
13 /**
14  * @brief
15  * Open an existing device given by its NT-style path, which is assumed to be
16  * for a disk device or a partition. The open is for synchronous I/O access.
17  *
18  * @param[in]   DevicePath
19  * Supplies the NT-style path to the device to open.
20  *
21  * @param[out]  DeviceHandle
22  * If successful, receives the NT handle of the opened device.
23  * Once the handle is no longer in use, call NtClose() to close it.
24  *
25  * @param[in]   DesiredAccess
26  * An ACCESS_MASK value combination that determines the requested access
27  * to the device. Because the open is for synchronous access, SYNCHRONIZE
28  * is automatically added to the access mask.
29  *
30  * @param[in]   ShareAccess
31  * Specifies the type of share access for the device.
32  *
33  * @return  An NTSTATUS code indicating success or failure.
34  *
35  * @see pOpenDeviceEx()
36  **/
37 NTSTATUS
pOpenDeviceEx_UStr(_In_ PCUNICODE_STRING DevicePath,_Out_ PHANDLE DeviceHandle,_In_ ACCESS_MASK DesiredAccess,_In_ ULONG ShareAccess)38 pOpenDeviceEx_UStr(
39     _In_ PCUNICODE_STRING DevicePath,
40     _Out_ PHANDLE DeviceHandle,
41     _In_ ACCESS_MASK DesiredAccess,
42     _In_ ULONG ShareAccess)
43 {
44     OBJECT_ATTRIBUTES ObjectAttributes;
45     IO_STATUS_BLOCK IoStatusBlock;
46 
47     InitializeObjectAttributes(&ObjectAttributes,
48                                (PUNICODE_STRING)DevicePath,
49                                OBJ_CASE_INSENSITIVE,
50                                NULL,
51                                NULL);
52     return NtOpenFile(DeviceHandle,
53                       DesiredAccess | SYNCHRONIZE,
54                       &ObjectAttributes,
55                       &IoStatusBlock,
56                       ShareAccess,
57                       /* FILE_NON_DIRECTORY_FILE | */
58                       FILE_SYNCHRONOUS_IO_NONALERT);
59 }
60 
61 /**
62  * @brief
63  * Open an existing device given by its NT-style path, which is assumed to be
64  * for a disk device or a partition. The open is share read/write/delete, for
65  * synchronous I/O and read access.
66  *
67  * @param[in]   DevicePath
68  * @param[out]  DeviceHandle
69  * See the DevicePath and DeviceHandle parameters of pOpenDeviceEx_UStr().
70  *
71  * @return  An NTSTATUS code indicating success or failure.
72  *
73  * @see pOpenDevice(), pOpenDeviceEx(), pOpenDeviceEx_UStr()
74  **/
75 NTSTATUS
pOpenDevice_UStr(_In_ PCUNICODE_STRING DevicePath,_Out_ PHANDLE DeviceHandle)76 pOpenDevice_UStr(
77     _In_ PCUNICODE_STRING DevicePath,
78     _Out_ PHANDLE DeviceHandle)
79 {
80     return pOpenDeviceEx_UStr(DevicePath,
81                               DeviceHandle,
82                               FILE_READ_DATA | FILE_READ_ATTRIBUTES,
83                               FILE_SHARE_ALL);
84 }
85 
86 /**
87  * @brief
88  * Open an existing device given by its NT-style path, which is assumed to be
89  * for a disk device or a partition. The open is for synchronous I/O access.
90  *
91  * @param[in]   DevicePath
92  * @param[out]  DeviceHandle
93  * @param[in]   DesiredAccess
94  * @param[in]   ShareAccess
95  * See pOpenDeviceEx_UStr() parameters.
96  *
97  * @return  An NTSTATUS code indicating success or failure.
98  *
99  * @see pOpenDeviceEx_UStr()
100  **/
101 NTSTATUS
pOpenDeviceEx(_In_ PCWSTR DevicePath,_Out_ PHANDLE DeviceHandle,_In_ ACCESS_MASK DesiredAccess,_In_ ULONG ShareAccess)102 pOpenDeviceEx(
103     _In_ PCWSTR DevicePath,
104     _Out_ PHANDLE DeviceHandle,
105     _In_ ACCESS_MASK DesiredAccess,
106     _In_ ULONG ShareAccess)
107 {
108     UNICODE_STRING Name;
109     RtlInitUnicodeString(&Name, DevicePath);
110     return pOpenDeviceEx_UStr(&Name, DeviceHandle, DesiredAccess, ShareAccess);
111 }
112 
113 /**
114  * @brief
115  * Open an existing device given by its NT-style path, which is assumed to be
116  * for a disk device or a partition. The open is share read/write/delete, for
117  * synchronous I/O and read access.
118  *
119  * @param[in]   DevicePath
120  * @param[out]  DeviceHandle
121  * See the DevicePath and DeviceHandle parameters of pOpenDeviceEx_UStr().
122  *
123  * @return  An NTSTATUS code indicating success or failure.
124  *
125  * @see pOpenDeviceEx(), pOpenDevice_UStr(), pOpenDeviceEx_UStr()
126  **/
127 NTSTATUS
pOpenDevice(_In_ PCWSTR DevicePath,_Out_ PHANDLE DeviceHandle)128 pOpenDevice(
129     _In_ PCWSTR DevicePath,
130     _Out_ PHANDLE DeviceHandle)
131 {
132     UNICODE_STRING Name;
133     RtlInitUnicodeString(&Name, DevicePath);
134     return pOpenDevice_UStr(&Name, DeviceHandle);
135 }
136 
137 /* EOF */
138