-
Notifications
You must be signed in to change notification settings - Fork 4
/
tapedrive.go
490 lines (434 loc) · 13.5 KB
/
tapedrive.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package main
// vim: ts=4:sts=4:
import (
"bytes"
//"encoding/json"
"errors"
"fmt"
"github.com/HewlettPackard/structex"
"github.com/benmcclelland/mtio"
"github.com/benmcclelland/sgio"
"os"
"strings"
"syscall"
"unsafe"
)
/*
type TapeDriveInterface interface {
Open() error
SetUserLabel(string) error
GetAttribute(*CmAttr) error
}
*/
type InquiryInfoType struct {
Vendor string
Model string
Firmware string
}
type TapeDrive struct {
DeviceName string
Dev *os.File
CmList *Cm
InquiryInfo InquiryInfoType
dumpFd *os.File
}
func TapeDriveNewDefault() (*TapeDrive, error) {
return TapeDriveNew("")
}
func TapeDriveNewFake() (*TapeDrive, error) {
return TapeDriveNew("FAKE")
}
func (drive TapeDrive) IsFake() bool {
return drive.DeviceName == "FAKE"
}
// copy of sg.OpenScsiDevice() but with RDONLY instead of O_RDWR
func OpenScsiDeviceRO(fname string) (*os.File, error) {
f, err := os.OpenFile(fname, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
var version uint32
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(f.Fd()),
uintptr(sgio.SG_GET_VERSION_NUM),
uintptr(unsafe.Pointer(&version)),
)
if errno != 0 {
return nil, fmt.Errorf("failed to get version info from sg device (errno=%d)", errno)
}
if version < 30000 {
return nil, fmt.Errorf("device does not appear to be an sg device")
}
return f, nil
}
func TapeDriveNew(devicename string) (*TapeDrive, error) {
if devicename == "" {
if os.Getenv("TAPE") != "" {
devicename = os.Getenv("TAPE")
} else {
devicename = "/dev/nst0"
}
}
drive := &TapeDrive{DeviceName: devicename, CmList: CmNew()}
if drive.IsFake() {
fmt.Println("Will use a fake tape drive")
return drive, nil
}
fmt.Printf("Opening device %s\n", devicename)
dev, err := OpenScsiDeviceRO(devicename)
if err != nil {
fmt.Println("Failed to open:", err)
return nil, err
}
fmt.Println("Checking whether device is ready")
err = sgio.TestUnitReady(dev)
if err != nil {
fmt.Println("Unit is not ready:", err)
return nil, err
}
fmt.Println("Unit is ready")
drive.Dev = dev
return drive, nil
}
func (drive *TapeDrive) GetStatus() error {
// http://manpages.ubuntu.com/manpages/focal/man4/st.4.html
mtget, _ := mtio.GetStatus(drive.Dev)
fmt.Println(mtget)
//blocksz := uint32(mtget.DsReg) & 0x00FFFFFF
//density := (uint32(mtget.DsReg) & 0xFF000000) >> 24
//fmt.Printf("blocksz=%d density=%x\n", blocksz, density)
//fmt.Println(mtio.GetPos(drive.Dev))
return nil
}
func (drive *TapeDrive) SetDumpFile(file string) error {
if drive.dumpFd != nil {
drive.dumpFd.Close()
}
fo, err := os.Create(file)
if err != nil {
panic(err)
}
drive.dumpFd = fo
return nil
}
func (drive *TapeDrive) SetUserLabel(str string) error {
senseBuf := make([]byte, sgio.SENSE_BUF_LEN)
inqCmdBlk := []uint8{0x8D, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 169, 0, 0}
wrAtt := make([]byte, 169)
wrAtt[0] = 0
wrAtt[1] = 0
wrAtt[2] = 0
wrAtt[3] = 165
wrAtt[4] = 0x08
wrAtt[5] = 0x03
wrAtt[6] = 2
wrAtt[7] = 0
wrAtt[8] = 160
for i := 0; i < 160; i++ {
if i < len(str) {
wrAtt[9+i] = str[i]
} else {
wrAtt[9+i] = 0
}
}
ioHdr := &sgio.SgIoHdr{
InterfaceID: int32('S'),
CmdLen: uint8(len(inqCmdBlk)),
MxSbLen: sgio.SENSE_BUF_LEN,
DxferDirection: sgio.SG_DXFER_TO_DEV,
DxferLen: uint32(len(wrAtt)),
Dxferp: &wrAtt[0],
Cmdp: &inqCmdBlk[0],
Sbp: &senseBuf[0],
Timeout: sgio.TIMEOUT_20_SECS,
}
err := sgio.SgioSyscall(drive.Dev, ioHdr)
if err != nil {
return err
}
err = sgio.CheckSense(ioHdr, &senseBuf)
if err != nil {
return err
}
return nil
}
func (drive *TapeDrive) GetAttribute(attr *CmAttr) error {
if drive == nil {
return errors.New("drive is nil")
}
senseBuf := make([]byte, sgio.SENSE_BUF_LEN)
replyBuf := make([]byte, READ_ATT_REPLY_LEN)
/* READ ATTRIBUTE (8Ch)
bits: 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
byte0: --- OPERATION CODE (8Ch) ----
byte1: reserved | SERVICE ACTION
byte2: obsolete
byte3: obsolete
byte4: obsolete
byte5: LOGICAL VOLUME NUMBER
byte6: reserved
byte7: PARTITION NUMBER
byte8: (MSB) <-- FIRST ATTRIBUTE
byte9: IDENTIFIER --> (LSB)
byte10: (MSB) <-- ALLOCATION
byte11:
byte12:
byte13: LENGTH --> (LSB)
byte14: reserved | CACHE
byte15: CONTROL BYTE (00h)
*/
inqCmdBlk := []uint8{0x8C, 0, 0, 0, 0, 0, 0, 0, 0x04, 0x00, 0, 0, 159, 0, 0, 0}
inqCmdBlk[8] = uint8(0xff & (attr.Command >> 8))
inqCmdBlk[9] = uint8(0xff & attr.Command)
inqCmdBlk[12] = uint8(0xff & attr.Len)
ioHdr := &sgio.SgIoHdr{
InterfaceID: int32('S'),
CmdLen: uint8(len(inqCmdBlk)),
MxSbLen: sgio.SENSE_BUF_LEN,
DxferDirection: sgio.SG_DXFER_FROM_DEV,
DxferLen: READ_ATT_REPLY_LEN,
Dxferp: &replyBuf[0],
Cmdp: &inqCmdBlk[0],
Sbp: &senseBuf[0],
Timeout: sgio.TIMEOUT_20_SECS,
}
if drive.IsFake() {
if attr.MockStr == "err" {
attr.IsValid = false
return errors.New("mocked error")
}
if attr.DataType == TYPE_BINARY {
attr.DataInt = attr.MockInt
attr.IsValid = true
return nil
}
if attr.DataType == TYPE_ASCII {
attr.DataStr = attr.MockStr
attr.IsValid = true
return nil
}
return errors.New("Invalid type")
}
attr.IsValid = false
err := sgio.SgioSyscall(drive.Dev, ioHdr)
if drive.dumpFd != nil {
senserr := sgio.CheckSense(ioHdr, &senseBuf)
senstr := "<nil>"
if senserr != nil {
senstr = strings.Replace(senserr.Error(), "\n", " ", -1)
}
drive.dumpFd.Write([]byte(fmt.Sprintf("GetAttribute[%s]:\nsyscallerr: %v\nsenserr: %v\ncommand: 0x%04x\ninqCmdBlk: %v\nsenseBuf: %v\nreplyBuf: %v\n\n", attr.Name, err, senstr, attr.Command, inqCmdBlk, senseBuf, replyBuf)))
}
if err != nil {
return err
}
err = sgio.CheckSense(ioHdr, &senseBuf)
if err != nil {
return err
}
if attr.DataType == TYPE_BINARY {
attr.DataInt = 0
for i := 0; i < attr.Len; i++ {
attr.DataInt *= 256
attr.DataInt += uint64(replyBuf[9+i])
}
attr.IsValid = true
return nil
}
if attr.DataType == TYPE_ASCII {
attr.DataStr = string(replyBuf[9:(9 + attr.Len)])
if !attr.NoTrim {
attr.DataStr = strings.TrimRight(attr.DataStr, " ")
}
attr.IsValid = true
return nil
}
return errors.New("Invalid type")
}
type SCSI_Inquiry_Cmd struct {
OpCode uint8
EVPD uint8 `bitfield:"1"`
reserved0 uint8 `bitfield:"4,reserved"`
obsolete0 uint8 `bitfield:"3,reserved"`
PageCode uint8
AllocationLength uint16
ControlByte uint8
}
type SCSI_Drive_Serial_Numbers_Return struct {
PeripheralDeviceType uint8 `bitfield:"5"` // Byte 0
PeripheralQualifier uint8 `bitfield:"3"`
PageCode uint8 // Byte 1
reserved0 uint8 `bitfield:"8,reserved"` // Byte 2
PageLength uint8
ManufSN [12]byte
ReportedSN [12]byte
}
type SCSI_Inquiry_Return struct {
PeripheralDeviceType uint8 `bitfield:"5"` // Byte 0
PeripheralQualifier uint8 `bitfield:"3"`
Reserved0 uint8
Version uint8
ReponseDataFormat uint8 `bitfield:"4"`
HiSup uint8 `bitfield:"1"`
NACA uint8 `bitfield:"1"`
Obsolete0 uint8 `bitfield:"1"`
Obsolete1 uint8 `bitfield:"1"`
AdditionalLen uint8
Protect uint8 `bitfield:"1"`
Reserved1 uint8 `bitfield:"2"`
ThreePC uint8 `bitfield:"1"`
TPGS uint8 `bitfield:"2"`
ACC uint8 `bitfield:"1"`
SCCS uint8 `bitfield:"1"`
Osef0 uint8
Osef1 uint8
VendorID [8]byte
ProductID [16]byte
ProductRevision [4]byte // YMDV(F63D), Y=15 M=6 D=3 V=D
Reserved2 uint8
Obsolete2 uint8
MaxSpeed uint8 `bitfield:"4"`
ProtocolID uint8 `bitfield:"4"`
FIPS uint8 `bitfield:"2"`
Reserved3 uint8 `bitfield:"5"`
Restricted uint8 `bitfield:"1"`
Reserved4 uint8
OEMSpecific uint8
OEMSpecificSubfield uint8
Reserved5 uint8
Reserved6 uint32
PartNumber [8]byte
Reserved7 uint8
Reserved8 uint8
Truc1 uint16
Truc2 uint16
Truc3 uint16
Truc4 uint16
Truc5 uint16
Truc6 uint16
}
func (drive *TapeDrive) ScsiInquiry() error {
senseBuf := make([]byte, sgio.SENSE_BUF_LEN)
replyBuf := make([]byte, 0xFF)
inqCmdBlk := []uint8{0x12, 0, 0, 0, 0xFF, 0}
ioHdr := &sgio.SgIoHdr{
InterfaceID: int32('S'),
CmdLen: uint8(len(inqCmdBlk)),
MxSbLen: sgio.SENSE_BUF_LEN,
DxferDirection: sgio.SG_DXFER_FROM_DEV,
DxferLen: 0xFF,
Dxferp: &replyBuf[0],
Cmdp: &inqCmdBlk[0],
Sbp: &senseBuf[0],
Timeout: sgio.TIMEOUT_20_SECS,
}
if !drive.IsFake() {
err := sgio.SgioSyscall(drive.Dev, ioHdr)
if drive.dumpFd != nil {
senserr := sgio.CheckSense(ioHdr, &senseBuf)
senstr := "<nil>"
if senserr != nil {
senstr = strings.Replace(senserr.Error(), "\n", " ", -1)
}
drive.dumpFd.Write([]byte(fmt.Sprintf("ScsiInquiry:\nsyscallerr: %v\nsenserr: %v\ninqCmdBlk: %v\nsenseBuf: %v\nreplyBuf: %v\n\n", err, senstr, inqCmdBlk, senseBuf, replyBuf)))
}
if err != nil {
return err
}
err = sgio.CheckSense(ioHdr, &senseBuf)
if err != nil {
return err
}
} else {
replyBuf = []byte{1, 128, 3, 2, 91, 0, 1, 48, 72, 80, 32, 32, 32, 32, 32, 32, 85, 108, 116, 114, 105, 117, 109, 32, 50, 45, 83, 67, 83, 73, 32, 32, 70, 54, 51, 68, 0, 0, 0, 0, 0, 12, 0, 36, 68, 82, 45, 49, 48, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 84, 11, 28, 2, 119, 2, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
}
//fmt.Println(replyBuf)
var parsed = new(SCSI_Inquiry_Return)
if err := structex.Decode(bytes.NewReader(replyBuf), parsed); err != nil {
fmt.Println("structex failed:", err)
}
drive.InquiryInfo.Vendor = strings.Trim(string(parsed.VendorID[:]), " \u0000")
drive.InquiryInfo.Model = strings.Trim(string(parsed.ProductID[:]), " \u0000")
drive.InquiryInfo.Firmware = strings.Trim(string(parsed.ProductRevision[:]), " \u0000")
//fmt.Printf("MaxSpeed=%d ProtoID=%d OEMSpec=%d OEMSpecSub=%d PartNu=<%s>\n", parsed.MaxSpeed, parsed.ProtocolID, parsed.OEMSpecific, parsed.OEMSpecificSubfield, parsed.PartNumber)
//fmt.Printf("Truc1=%04x Truc2=%04x Truc3=%04x Truc4=%04x Truc5=%04x Truc6=%04x\n", parsed.Truc1, parsed.Truc2, parsed.Truc3, parsed.Truc4, parsed.Truc5, parsed.Truc6)
return nil
}
type LogSenseType struct {
PageCode uint8
SubPageCode uint8
}
func (drive *TapeDrive) scsiLogSense(ls *LogSenseType) error {
senseBuf := make([]byte, sgio.SENSE_BUF_LEN)
replyBuf := make([]byte, READ_ATT_REPLY_LEN)
/* LOG SENSE (4Dh)
bits: 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
byte0: --- OPERATION CODE (4Dh) ----
byte1: reserved |PPC|SP
byte2: PC | PAGE CODE
byte3: SUBPAGE CODE
byte4: reserved
byte5: <-- (MSB) PARAMETER..........
byte6: ............POINTER (LSB) -->
byte7: <-- (MSB) ALLOCATION.........
byte8: .............LENGTH (LSB) -->
byte9: CONTROL BYTE (00h)
The log values returned are controlled by the Page Control ( PC ) field value as follows:
Value Description
00b the maximum value for each log entry is returned.
01b the current values are returned.
10b the maximum value for each log entry is returned.
11b the power-on values are returned.
NOTE 10 - For page 2Eh (TapeAlert) only, the PC field is ignored. Current values are always returned.
The Parameter Pointer Control ( PPC ) must be set to 0. Returning changed parameters is not supported. The
Save Page ( SP ) field must be set to 0. Saved pages are not supported. The Parameter Pointer will be 0.
*/
var opcode uint8 = 0x4D
var ppc uint8 = 0
var sp uint8 = 0
var pc uint8 = 0b01
var parameterpointer uint16 = 0
var alloclen uint16 = 0
var controlbyte uint8 = 0
inqCmdBlk := []uint8{
opcode,
((ppc & 0b1) << 1) | (sp & 0b1),
((pc & 0b11) << 6) | (ls.PageCode & 0b111111),
ls.SubPageCode,
0,
uint8((parameterpointer & 0xFF00) >> 8),
uint8((parameterpointer & 0x00FF) >> 0),
uint8((alloclen & 0xFF00) >> 8),
uint8((alloclen & 0x00FF) >> 0),
controlbyte}
ioHdr := &sgio.SgIoHdr{
InterfaceID: int32('S'),
CmdLen: uint8(len(inqCmdBlk)),
MxSbLen: sgio.SENSE_BUF_LEN,
DxferDirection: sgio.SG_DXFER_FROM_DEV,
DxferLen: READ_ATT_REPLY_LEN,
Dxferp: &replyBuf[0],
Cmdp: &inqCmdBlk[0],
Sbp: &senseBuf[0],
Timeout: sgio.TIMEOUT_20_SECS,
}
err := sgio.SgioSyscall(drive.Dev, ioHdr)
if err != nil {
return err
}
err = sgio.CheckSense(ioHdr, &senseBuf)
if err != nil {
return err
}
fmt.Println(replyBuf)
return nil
}
func (drive *TapeDrive) String() string {
s := fmt.Sprintf("Drive information:\n")
s += fmt.Sprintf(" Vendor : %s\n", drive.InquiryInfo.Vendor)
s += fmt.Sprintf(" Model : %s\n", drive.InquiryInfo.Model)
s += fmt.Sprintf(" Firmware: %s\n", drive.InquiryInfo.Firmware)
s += drive.CmList.String()
return s
}