xref: /reactos/base/setup/lib/utils/devutils.c (revision 201f00ab)
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 NTSTATUS
36 pOpenDeviceEx(
37     _In_ PCWSTR DevicePath,
38     _Out_ PHANDLE DeviceHandle,
39     _In_ ACCESS_MASK DesiredAccess,
40     _In_ ULONG ShareAccess)
41 {
42     UNICODE_STRING Name;
43     OBJECT_ATTRIBUTES ObjectAttributes;
44     IO_STATUS_BLOCK IoStatusBlock;
45 
46     RtlInitUnicodeString(&Name, DevicePath);
47     InitializeObjectAttributes(&ObjectAttributes,
48                                &Name,
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().
70  *
71  * @return  An NTSTATUS code indicating success or failure.
72  *
73  * @see pOpenDeviceEx()
74  **/
75 NTSTATUS
76 pOpenDevice(
77     _In_ PCWSTR DevicePath,
78     _Out_ PHANDLE DeviceHandle)
79 {
80     return pOpenDeviceEx(DevicePath,
81                          DeviceHandle,
82                          FILE_READ_DATA | FILE_READ_ATTRIBUTES,
83                          FILE_SHARE_VALID_FLAGS // FILE_SHARE_READ,WRITE,DELETE
84                          );
85 }
86 
87 /* EOF */
88