Skip to content

Commit e29f478

Browse files
authored
Merge pull request #12 from 1a2m3/1a2m3-patch-20220123
20220202 update
2 parents 1c24cbc + 5cca8cb commit e29f478

34 files changed

+6830
-31
lines changed

src/SpdReaderWriterDll/Eeprom.cs

Lines changed: 263 additions & 19 deletions
Large diffs are not rendered by default.

src/SpdReaderWriterDll/OpenLibSys.cs

Lines changed: 414 additions & 0 deletions
Large diffs are not rendered by default.

src/SpdReaderWriterDll/PciDevice.cs

Lines changed: 443 additions & 0 deletions
Large diffs are not rendered by default.

src/SpdReaderWriterDll/Response.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public struct Response {
3232
/// </summary>
3333
public const byte OFF = 0;
3434
/// <summary>
35-
/// A response expected from the device after executing Command.TESTCOMM command to identify the correct device
35+
/// A response expected from the device after executing SerialDeviceCommand.TESTCOMM command to identify the correct device
3636
/// </summary>
3737
public const char WELCOME = '!';
3838
/// <summary>
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.IO.Ports;
66
using System.Linq;
77
using System.Text;
8-
using static SpdReaderWriterDll.Command;
8+
using static SpdReaderWriterDll.SerialDeviceCommand;
99
using static SpdReaderWriterDll.Pin;
1010
using static SpdReaderWriterDll.Pin.Name;
1111
using static SpdReaderWriterDll.Spd;
@@ -15,12 +15,12 @@ namespace SpdReaderWriterDll {
1515
/// <summary>
1616
/// Defines Device class, properties, and methods to handle the communication with the device
1717
/// </summary>
18-
public class Device {
18+
public class SerialDevice {
1919
/// <summary>
2020
/// Initializes the SPD reader/writer device
2121
/// </summary>
2222
/// <param name="portSettings">Serial port settings</param>
23-
public Device(SerialPortSettings portSettings) {
23+
public SerialDevice(SerialPortSettings portSettings) {
2424
PortSettings = portSettings;
2525
}
2626

@@ -29,7 +29,7 @@ public Device(SerialPortSettings portSettings) {
2929
/// </summary>
3030
/// <param name="portSettings">Serial port settings</param>
3131
/// <param name="portName">Serial port name</param>
32-
public Device(SerialPortSettings portSettings, string portName) {
32+
public SerialDevice(SerialPortSettings portSettings, string portName) {
3333
PortSettings = portSettings;
3434
PortName = portName;
3535
}
@@ -40,7 +40,7 @@ public Device(SerialPortSettings portSettings, string portName) {
4040
/// <param name="portSettings">Serial port settings</param>
4141
/// <param name="portName" >Serial port name</param>
4242
/// <param name="i2cAddress">EEPROM address on the device's i2c bus</param>
43-
public Device(SerialPortSettings portSettings, string portName, UInt8 i2cAddress) {
43+
public SerialDevice(SerialPortSettings portSettings, string portName, UInt8 i2cAddress) {
4444
PortSettings = portSettings;
4545
PortName = portName;
4646
I2CAddress = i2cAddress;
@@ -53,7 +53,7 @@ public Device(SerialPortSettings portSettings, string portName, UInt8 i2cAddress
5353
/// <param name="portName">Serial port name</param>
5454
/// <param name="i2cAddress">EEPROM address on the device's i2c bus</param>
5555
/// <param name="spdSize">Total EEPROM size</param>
56-
public Device(SerialPortSettings portSettings, string portName, UInt8 i2cAddress, Ram.SpdSize spdSize) {
56+
public SerialDevice(SerialPortSettings portSettings, string portName, UInt8 i2cAddress, Ram.SpdSize spdSize) {
5757
PortSettings = portSettings;
5858
PortName = portName;
5959
I2CAddress = i2cAddress;
@@ -79,7 +79,7 @@ public override string ToString() {
7979
/// <summary>
8080
/// Device class destructor
8181
/// </summary>
82-
~Device() {
82+
~SerialDevice() {
8383
DisposePrivate();
8484
}
8585

@@ -1038,7 +1038,7 @@ private string[] FindPrivate() {
10381038
lock (FindLock) {
10391039
foreach (string _portName in SerialPort.GetPortNames().Distinct().ToArray()) {
10401040

1041-
Device _device = new Device(PortSettings, _portName);
1041+
SerialDevice _device = new SerialDevice(PortSettings, _portName);
10421042
try {
10431043
lock (_device.PortLock) {
10441044
if (_device.ConnectPrivate()) {

src/SpdReaderWriterDll/Command.cs renamed to src/SpdReaderWriterDll/SerialDeviceCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace SpdReaderWriterDll {
22
/// <summary>
33
/// Device commands
44
/// </summary>
5-
public struct Command {
5+
public struct SerialDeviceCommand {
66
/// <summary>
77
/// Read byte
88
/// </summary>

src/SpdReaderWriterDll/Spd.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Spd {
1515
/// </summary>
1616
/// <param name="device">Device instance</param>
1717
/// <returns>RAM Type</returns>
18-
public static Ram.Type GetRamType(Device device) {
18+
public static Ram.Type GetRamType(SerialDevice device) {
1919

2020
if (device == null) {
2121
throw new NullReferenceException($"Invalid device");
@@ -64,7 +64,7 @@ public static Ram.Type GetRamType(byte[] input) {
6464
/// </summary>
6565
/// <param name="device">Device instance</param>
6666
/// <returns>SPD size</returns>
67-
public static SpdSize GetSpdSize(Device device) {
67+
public static SpdSize GetSpdSize(SerialDevice device) {
6868

6969
if (device == null) {
7070
throw new NullReferenceException($"Invalid device");

src/WinRing0Dll/Driver.cpp

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
//-----------------------------------------------------------------------------
2+
// Author : hiyohiyo
3+
// Mail : hiyohiyo@crystalmark.info
4+
// Web : http://openlibsys.org/
5+
// License : The modified BSD license
6+
//
7+
// Copyright 2007 OpenLibSys.org. All rights reserved.
8+
//-----------------------------------------------------------------------------
9+
10+
#include "stdafx.h"
11+
#include "Driver.h"
12+
#include <tchar.h>
13+
#include "OlsDll.h"
14+
15+
extern HANDLE gHandle;
16+
17+
static BOOL InstallDriver(SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath);
18+
static BOOL RemoveDriver(SC_HANDLE hSCManager, LPCTSTR DriverId);
19+
static BOOL StartDriver(SC_HANDLE hSCManager, LPCTSTR DriverId);
20+
static BOOL StopDriver(SC_HANDLE hSCManager, LPCTSTR DriverId);
21+
static BOOL SystemInstallDriver(SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath);
22+
static BOOL IsSystemInstallDriver(SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath);
23+
24+
BOOL ManageDriver(LPCTSTR DriverId, LPCTSTR DriverPath, USHORT Function)
25+
{
26+
SC_HANDLE hSCManager = NULL;
27+
BOOL rCode = FALSE;
28+
DWORD error = NO_ERROR;
29+
30+
if(DriverId == NULL || DriverPath == NULL)
31+
{
32+
return FALSE;
33+
}
34+
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
35+
36+
if(hSCManager == NULL)
37+
{
38+
return FALSE;
39+
}
40+
41+
switch(Function)
42+
{
43+
case OLS_DRIVER_INSTALL:
44+
if(InstallDriver(hSCManager, DriverId, DriverPath))
45+
{
46+
rCode = StartDriver(hSCManager, DriverId);
47+
}
48+
break;
49+
case OLS_DRIVER_REMOVE:
50+
if(! IsSystemInstallDriver(hSCManager, DriverId, DriverPath))
51+
{
52+
StopDriver(hSCManager, DriverId);
53+
rCode = RemoveDriver(hSCManager, DriverId);
54+
}
55+
break;
56+
case OLS_DRIVER_SYSTEM_INSTALL:
57+
if(IsSystemInstallDriver(hSCManager, DriverId, DriverPath))
58+
{
59+
rCode = TRUE;
60+
}
61+
else
62+
{
63+
if(! OpenDriver())
64+
{
65+
StopDriver(hSCManager, DriverId);
66+
RemoveDriver(hSCManager, DriverId);
67+
if(InstallDriver(hSCManager, DriverId, DriverPath))
68+
{
69+
StartDriver(hSCManager, DriverId);
70+
}
71+
OpenDriver();
72+
}
73+
rCode = SystemInstallDriver(hSCManager, DriverId, DriverPath);
74+
}
75+
break;
76+
case OLS_DRIVER_SYSTEM_UNINSTALL:
77+
if(! IsSystemInstallDriver(hSCManager, DriverId, DriverPath))
78+
{
79+
rCode = TRUE;
80+
}
81+
else
82+
{
83+
if(gHandle != INVALID_HANDLE_VALUE)
84+
{
85+
CloseHandle(gHandle);
86+
gHandle = INVALID_HANDLE_VALUE;
87+
}
88+
89+
if(StopDriver(hSCManager, DriverId))
90+
{
91+
rCode = RemoveDriver(hSCManager, DriverId);
92+
}
93+
}
94+
break;
95+
default:
96+
rCode = FALSE;
97+
break;
98+
}
99+
100+
if(hSCManager != NULL)
101+
{
102+
CloseServiceHandle(hSCManager);
103+
}
104+
105+
return rCode;
106+
}
107+
108+
BOOL InstallDriver(SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath)
109+
{
110+
SC_HANDLE hService = NULL;
111+
BOOL rCode = FALSE;
112+
DWORD error = NO_ERROR;
113+
114+
hService = CreateService(hSCManager,
115+
DriverId,
116+
DriverId,
117+
SERVICE_ALL_ACCESS,
118+
SERVICE_KERNEL_DRIVER,
119+
SERVICE_DEMAND_START,
120+
SERVICE_ERROR_NORMAL,
121+
DriverPath,
122+
NULL,
123+
NULL,
124+
NULL,
125+
NULL,
126+
NULL
127+
);
128+
129+
if(hService == NULL)
130+
{
131+
error = GetLastError();
132+
if(error == ERROR_SERVICE_EXISTS)
133+
{
134+
rCode = TRUE;
135+
}
136+
}
137+
else
138+
{
139+
rCode = TRUE;
140+
CloseServiceHandle(hService);
141+
}
142+
143+
return rCode;
144+
}
145+
146+
BOOL SystemInstallDriver(SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath)
147+
{
148+
SC_HANDLE hService = NULL;
149+
BOOL rCode = FALSE;
150+
151+
hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
152+
153+
if(hService != NULL)
154+
{
155+
rCode = ChangeServiceConfig(hService,
156+
SERVICE_KERNEL_DRIVER,
157+
SERVICE_AUTO_START,
158+
SERVICE_ERROR_NORMAL,
159+
DriverPath,
160+
NULL,
161+
NULL,
162+
NULL,
163+
NULL,
164+
NULL,
165+
NULL
166+
);
167+
CloseServiceHandle(hService);
168+
}
169+
170+
return rCode;
171+
}
172+
173+
BOOL RemoveDriver(SC_HANDLE hSCManager, LPCTSTR DriverId)
174+
{
175+
SC_HANDLE hService = NULL;
176+
BOOL rCode = FALSE;
177+
178+
hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
179+
if(hService == NULL)
180+
{
181+
rCode = TRUE;
182+
}
183+
else
184+
{
185+
rCode = DeleteService(hService);
186+
CloseServiceHandle(hService);
187+
}
188+
189+
return rCode;
190+
}
191+
192+
BOOL StartDriver(SC_HANDLE hSCManager, LPCTSTR DriverId)
193+
{
194+
SC_HANDLE hService = NULL;
195+
BOOL rCode = FALSE;
196+
DWORD error = NO_ERROR;
197+
198+
hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
199+
200+
if(hService != NULL)
201+
{
202+
if(! StartService(hService, 0, NULL))
203+
{
204+
error = GetLastError();
205+
if(error == ERROR_SERVICE_ALREADY_RUNNING)
206+
{
207+
rCode = TRUE;
208+
}
209+
}
210+
else
211+
{
212+
rCode = TRUE;
213+
}
214+
CloseServiceHandle(hService);
215+
}
216+
217+
return rCode;
218+
}
219+
220+
BOOL StopDriver(SC_HANDLE hSCManager, LPCTSTR DriverId)
221+
{
222+
SC_HANDLE hService = NULL;
223+
BOOL rCode = FALSE;
224+
SERVICE_STATUS serviceStatus;
225+
DWORD error = NO_ERROR;
226+
227+
hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
228+
229+
if(hService != NULL)
230+
{
231+
rCode = ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus);
232+
error = GetLastError();
233+
CloseServiceHandle(hService);
234+
}
235+
236+
return rCode;
237+
}
238+
239+
BOOL IsSystemInstallDriver(SC_HANDLE hSCManager, LPCTSTR DriverId, LPCTSTR DriverPath)
240+
{
241+
SC_HANDLE hService = OpenService(hSCManager, DriverId, SERVICE_ALL_ACCESS);
242+
if (hService != NULL)
243+
{
244+
DWORD dwSize;
245+
if (!QueryServiceConfig(hService, NULL, 0, &dwSize))
246+
{
247+
LPQUERY_SERVICE_CONFIG lpqsc = reinterpret_cast<LPQUERY_SERVICE_CONFIG>(new BYTE[dwSize]);
248+
if (!lpqsc)
249+
{
250+
return FALSE;
251+
}
252+
253+
if (!QueryServiceConfig(hService, lpqsc, dwSize, &dwSize))
254+
{
255+
if (lpqsc->dwStartType == SERVICE_AUTO_START)
256+
{
257+
CloseServiceHandle(hService);
258+
delete[] lpqsc;
259+
return TRUE;
260+
}
261+
}
262+
}
263+
}
264+
265+
return FALSE;
266+
}

src/WinRing0Dll/Driver.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//-----------------------------------------------------------------------------
2+
// Author : hiyohiyo
3+
// Mail : hiyohiyo@crystalmark.info
4+
// Web : http://openlibsys.org/
5+
// License : The modified BSD license
6+
//
7+
// Copyright 2007 OpenLibSys.org. All rights reserved.
8+
//-----------------------------------------------------------------------------
9+
10+
#pragma once
11+
12+
#define OLS_DRIVER_INSTALL 1
13+
#define OLS_DRIVER_REMOVE 2
14+
#define OLS_DRIVER_SYSTEM_INSTALL 3
15+
#define OLS_DRIVER_SYSTEM_UNINSTALL 4
16+
17+
BOOL ManageDriver(LPCTSTR DriverId, LPCTSTR DriverPath, USHORT Function);

0 commit comments

Comments
 (0)