This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskutil_openraw_windows.go
86 lines (78 loc) · 2.2 KB
/
diskutil_openraw_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package diskutil
import (
"errors"
"syscall"
"unsafe"
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procCreateFileA = kernel32.NewProc("CreateFileA")
procDeviceIoControl = kernel32.NewProc("DeviceIoControl")
procWriteFile = kernel32.NewProc("WriteFile")
)
// OpenDiskRaw acquires a file handle to a physicaldisk path using windows API calls.
func OpenDiskRaw(diskPath string) (syscall.Handle, error) {
// move diskpath to a buffer
diskPathBin := []byte(diskPath)
diskPathBuf := make([]byte, len(diskPathBin)+1)
copy(diskPathBuf, diskPathBin)
diskPathBuf[len(diskPathBuf)-1] = 0
/* CreateFileA(
diskPath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
) */
ret, _, lastErr := syscall.Syscall9(
procCreateFileA.Addr(),
7,
uintptr(unsafe.Pointer(&diskPathBuf[0])),
uintptr(syscall.GENERIC_READ|syscall.GENERIC_WRITE),
// uintptr(syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE),
uintptr(0),
uintptr(0),
uintptr(syscall.OPEN_EXISTING),
uintptr(syscall.FILE_ATTRIBUTE_NORMAL),
uintptr(0),
uintptr(0), uintptr(0), // unused args
)
retHandle := syscall.Handle(ret)
if retHandle == syscall.InvalidHandle {
if lastErr != 0 {
return syscall.InvalidHandle, lastErr
} else {
return syscall.InvalidHandle, errors.New("unknown error opening disk")
}
}
var dismountStatus uint32
/* DeviceIoControl(retHandle, FSCTL_DISMOUNT_VOLUME, 0, 0, 0, 0, &status, 0) */
ret, _, lastErr = syscall.Syscall9(
procDeviceIoControl.Addr(),
8,
uintptr(retHandle),
uintptr(0x00090020), // FSCTL_DISMOUNT_VOLUME
uintptr(0), uintptr(0), uintptr(0), uintptr(0),
uintptr(unsafe.Pointer(&dismountStatus)),
uintptr(0),
uintptr(0), // unused arg
)
// ignore the result
_ = ret
/* DeviceIoControl(retHandle, FSCTL_LOCK_VOLUME, 0, 0, 0, 0, &status, 0) */
ret, _, lastErr = syscall.Syscall9(
procDeviceIoControl.Addr(),
8,
uintptr(retHandle),
uintptr(0x00090018), // FSCTL_LOCK_VOLUME
uintptr(0), uintptr(0), uintptr(0), uintptr(0),
uintptr(unsafe.Pointer(&dismountStatus)),
uintptr(0),
uintptr(0), // unused argument
)
// ignore the result
_ = ret
// DefineDosDevice
return retHandle, nil
}