-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsg_lib.c
4230 lines (3990 loc) · 145 KB
/
sg_lib.c
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1999-2023 Douglas Gilbert.
* All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the BSD_LICENSE file.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
/* NOTICE:
* On 5th October 2004 (v1.00) this file name was changed from sg_err.c
* to sg_lib.c and the previous GPL was changed to a FreeBSD license.
* The intention is to maintain this file and the related sg_lib.h file
* as open source and encourage their unencumbered use.
*
* CONTRIBUTIONS:
* This file started out as a copy of SCSI opcodes, sense keys and
* additional sense codes (ASC/ASCQ) kept in the Linux SCSI subsystem
* in the kernel source file: drivers/scsi/constant.c . That file
* bore this notice: "Copyright (C) 1993, 1994, 1995 Eric Youngdale"
* and a GPL notice.
*
* Much of the data in this file is derived from SCSI draft standards
* found at https://www.t10.org with the "SCSI Primary Commands-4" (SPC-4)
* being the central point of reference.
*
* Contributions:
* sense key specific field decoding [Trent Piepho 20031116]
*
*/
#define _POSIX_C_SOURCE 200809L /* for posix_memalign() */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "sg_lib.h"
#include "sg_lib_data.h"
#include "sg_unaligned.h"
#include "sg_pr2serr.h"
/* sg_lib_version_str (and datestamp) defined in sg_lib_data.c file */
#define ASCQ_ATA_PT_INFO_AVAILABLE 0x1d /* corresponding ASC is 0 */
typedef unsigned int my_uint; /* convenience to save a few line wraps */
/* Simple ASCII printable (does not use locale), includes space and excludes
* DEL (0x7f). Note all UTF-8 encoding apart from <= 0x7f have top bit set. */
static inline int
my_isprint(int ch)
{
return ((ch >= ' ') && (ch < 0x7f));
}
void sg_rep_invocation(const char * util_name, const char * ver_str,
int argc, char *argv[], FILE * fgp)
{
int k;
FILE * fp = fgp ? fgp : stdout;
/* header line that is visually easy to spot */
fprintf(fp, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"
"vvvvvvvvvv\n");
if (util_name)
fprintf(fp, "utility_given=%s\n", util_name);
else
fprintf(fp, "utility_given=<not_given>\n");
if (ver_str)
fprintf(fp, "version_string=%s\n", ver_str);
else
fprintf(fp, "version_string=<not_given>\n");
if ((argc > 0) && argv) {
fprintf(fp, "invocation_arguments:\n");
for (k = 0; k < argc; ++k)
fprintf(fp, " %s\n", argv[k]);
} else
fprintf(fp, "invocation_arguments:<none>\n");
fprintf(fp, "^^vv^^vv^^vv^^vv^^vv^^vv^^vv^^vv^^vv^^vv^^vv^^vv^^"
"vv^^vv^^vv\n");
}
/* DSENSE is 'descriptor sense' as opposed to the older 'fixed sense'.
* Only (currently) used in SNTL. */
bool
sg_get_initial_dsense(void)
{
int k;
const char * cp;
cp = getenv("SG3_UTILS_DSENSE");
if (cp) {
if (1 == sscanf(cp, "%d", &k))
return k ? true : false;
}
return false;
}
/* Searches 'arr' for match on 'value' then 'peri_type'. If matches
'value' but not 'peri_type' then yields first 'value' match entry.
Last element of 'arr' has NULL 'name'. If no match returns NULL. */
static const struct sg_lib_value_name_t *
get_value_name(const struct sg_lib_value_name_t * arr, int value,
int peri_type)
{
const struct sg_lib_value_name_t * vp = arr;
const struct sg_lib_value_name_t * holdp;
if (peri_type < 0)
peri_type = 0;
for (; vp->name; ++vp) {
if (value == vp->value) {
if (sg_pdt_s_eq(peri_type, vp->peri_dev_type))
return vp;
holdp = vp;
while ((vp + 1)->name && (value == (vp + 1)->value)) {
++vp;
if (sg_pdt_s_eq(peri_type, vp->peri_dev_type))
return vp;
}
return holdp;
}
}
return NULL;
}
/* If this function is not called, sg_warnings_strm will be NULL and all users
* (mainly fprintf() ) need to check and substitute stderr as required */
void
sg_set_warnings_strm(FILE * warnings_strm)
{
sg_warnings_strm = warnings_strm;
}
/* Take care to minimize printf() parsing delays when printing commands */
static char bin2hexascii[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/* Given a SCSI command pointed to by cdbp of sz bytes this function forms
* a SCSI command in ASCII surrounded by square brackets in 'b'. 'b' is at
* least blen bytes long. If cmd_name is true then the command is prefixed
* by its SCSI command name (e.g. "VERIFY(10) [2f ...]". The command is
* shown as spaced separated pairs of hexadecimal digits (i.e. 0-9, a-f).
* Each pair represents byte. The leftmost pair of digits is cdbp[0] . If
* sz <= 0 then this function tries to guess the length of the command. */
char *
sg_get_command_str(const uint8_t * cdbp, int sz, bool cmd_name, int blen,
char * b)
{
int k, j, jj;
if ((b == NULL) || (blen < 1))
return b;
else if ((cdbp == NULL) || (sz < 1)) {
snprintf(b, blen, "<empty>");
return b;
}
if (cmd_name && (blen > 16)) {
sg_get_command_name(cdbp, 0, blen, b);
j = (int)strlen(b);
if (j < (blen - 1))
b[j++] = ' ';
} else
j = 0;
if (j >= blen)
goto fini;
b[j++] = '[';
if (j >= blen)
goto fini;
if (sz <= 0) {
if (SG_VARIABLE_LENGTH_CMD == cdbp[0])
sz = cdbp[7] + 8;
else
sz = sg_get_command_size(cdbp[0]);
}
jj = j;
for (k = 0; (k < sz) && (j < (blen - 3)); ++k, j += 3, ++cdbp) {
b[j] = bin2hexascii[(*cdbp >> 4) & 0xf];
b[j + 1] = bin2hexascii[*cdbp & 0xf];
b[j + 2] = ' ';
}
if (j > jj)
--j; /* don't want trailing space before ']' */
if (j >= blen)
goto fini;
b[j++] = ']';
fini:
if (j >= blen)
b[blen - 1] = '\0'; /* truncated string */
else
b[j] = '\0';
return b;
}
#define CMD_NAME_LEN 128
void
sg_print_command_len(const uint8_t * cdbp, int sz)
{
char buff[CMD_NAME_LEN];
sg_get_command_str(cdbp, sz, true, sizeof(buff), buff);
pr2ws("%s\n", buff);
}
void
sg_print_command(const uint8_t * cdbp)
{
sg_print_command_len(cdbp, 0);
}
bool
sg_scsi_status_is_good(int sstatus)
{
sstatus &= 0xfe;
switch (sstatus) {
case SAM_STAT_GOOD:
case SAM_STAT_CONDITION_MET:
return true;
default:
return false;
}
}
bool
sg_scsi_status_is_bad(int sstatus)
{
sstatus &= 0xfe;
switch (sstatus) {
case SAM_STAT_GOOD:
case SAM_STAT_CONDITION_MET:
return false;
default:
return true;
}
}
void
sg_get_scsi_status_str(int scsi_status, int buff_len, char * buff)
{
const struct sg_lib_simple_value_name_t * sstatus_p;
if ((NULL == buff) || (buff_len < 1))
return;
else if (1 == buff_len) {
buff[0] = '\0';
return;
}
scsi_status &= 0x7e; /* sanitize as much as possible */
for (sstatus_p = sg_lib_sstatus_str_arr; sstatus_p->name; ++sstatus_p) {
if (scsi_status == sstatus_p->value)
break;
}
if (sstatus_p->name)
sg_scnpr(buff, buff_len, "%s", sstatus_p->name);
else
sg_scnpr(buff, buff_len, "Unknown status [0x%x]", scsi_status);
}
static const char * sg_lib_ansi_version_arr[16] = {
"no conformance claimed",
"SCSI-1", /* obsolete, ANSI X3.131-1986 */
"SCSI-2", /* obsolete, ANSI X3.131-1994 */
"SPC", /* withdrawn, ANSI INCITS 301-1997 */
"SPC-2", /* ANSI INCITS 351-2001, ISO/IEC 14776-452 */
"SPC-3", /* ANSI INCITS 408-2005, ISO/IEC 14776-453 */
"SPC-4", /* ANSI INCITS 513-2015 */
"SPC-5", /* ANSI INCITS 502-2020 */
"obsolete, [8h]",
"obsolete, [9h]",
"obsolete, [Ah]",
"obsolete, [Bh]",
"obsolete, [Ch]",
"SPC-6", /* T10/BSR INCITS 566, added in spc6r08 */
"reserved [Eh]",
"reserved [Fh]",
};
char *
sg_get_scsi_ansi_version_str(uint8_t ansi_ver, int blen, char * b)
{
if ((NULL == b) || (blen < 1))
return b;
if (ansi_ver < SG_ARRAY_SIZE(sg_lib_ansi_version_arr))
sg_scnpr(b, blen, "%s", sg_lib_ansi_version_arr[ansi_ver]);
else
sg_scnpr(b, blen, "%s", sg_lib_ansi_version_arr[0]);
return b;
}
void
sg_print_scsi_status(int scsi_status)
{
char buff[128];
sg_get_scsi_status_str(scsi_status, sizeof(buff) - 1, buff);
buff[sizeof(buff) - 1] = '\0';
pr2ws("%s ", buff);
}
/* Get sense key from sense buffer. If successful returns a sense key value
* between 0 and 15. If sense buffer cannot be decode, returns -1 . */
int
sg_get_sense_key(const uint8_t * sbp, int sb_len)
{
if ((NULL == sbp) || (sb_len < 2))
return -1;
switch (sbp[0] & 0x7f) {
case 0x70:
case 0x71:
return (sb_len < 3) ? -1 : (sbp[2] & 0xf);
case 0x72:
case 0x73:
return sbp[1] & 0xf;
default:
return -1;
}
}
/* Yield string associated with sense_key value. Returns 'buff'. */
char *
sg_get_sense_key_str(int sense_key, int buff_len, char * buff)
{
if (1 == buff_len) {
buff[0] = '\0';
return buff;
}
if ((sense_key >= 0) && (sense_key < 16))
sg_scnpr(buff, buff_len, "%s", sg_lib_sense_key_desc[sense_key]);
else
sg_scnpr(buff, buff_len, "invalid value: 0x%x", sense_key);
return buff;
}
/* Yield string associated with ASC/ASCQ values. Returns 'buff'. */
char *
sg_get_additional_sense_str(int asc, int ascq, bool add_sense_leadin,
int buff_len, char * buff)
{
int k, num, rlen;
bool found = false;
if (1 == buff_len) {
buff[0] = '\0';
return buff;
}
for (k = 0; sg_lib_asc_ascq_range[k].text; ++k) {
const struct sg_lib_asc_ascq_range_t * ei2p =
&sg_lib_asc_ascq_range[k];
if ((ei2p->asc == asc) &&
(ascq >= ei2p->ascq_min) &&
(ascq <= ei2p->ascq_max)) {
found = true;
if (add_sense_leadin)
num = sg_scnpr(buff, buff_len, "Additional sense: ");
else
num = 0;
rlen = buff_len - num;
sg_scnpr(buff + num, ((rlen > 0) ? rlen : 0), ei2p->text, ascq);
}
}
if (found)
return buff;
for (k = 0; sg_lib_asc_ascq[k].text; ++k) {
const struct sg_lib_asc_ascq_t * eip = &sg_lib_asc_ascq[k];
if (eip->asc == asc &&
eip->ascq == ascq) {
found = true;
if (add_sense_leadin)
sg_scnpr(buff, buff_len, "Additional sense: %s", eip->text);
else
sg_scnpr(buff, buff_len, "%s", eip->text);
}
}
if (! found) {
if (asc >= 0x80)
sg_scnpr(buff, buff_len, "vendor specific ASC=%02x, ASCQ=%02x "
"(hex)", asc, ascq);
else if (ascq >= 0x80)
sg_scnpr(buff, buff_len, "ASC=%02x, vendor specific qualification "
"ASCQ=%02x (hex)", asc, ascq);
else
sg_scnpr(buff, buff_len, "ASC=%02x, ASCQ=%02x (hex)", asc, ascq);
}
return buff;
}
/* Yield string associated with ASC/ASCQ values. Returns 'buff'. */
char *
sg_get_asc_ascq_str(int asc, int ascq, int buff_len, char * buff)
{
return sg_get_additional_sense_str(asc, ascq, true, buff_len, buff);
}
/* Attempt to find the first SCSI sense data descriptor that matches the
* given 'desc_type'. If found return pointer to start of sense data
* descriptor; otherwise (including fixed format sense data) returns NULL. */
const uint8_t *
sg_scsi_sense_desc_find(const uint8_t * sbp, int sb_len,
int desc_type)
{
int add_sb_len, desc_len, k;
const uint8_t * descp;
if ((sb_len < 8) || (0 == (add_sb_len = sbp[7])))
return NULL;
if ((sbp[0] < 0x72) || (sbp[0] > 0x73))
return NULL;
add_sb_len = (add_sb_len < (sb_len - 8)) ? add_sb_len : (sb_len - 8);
descp = &sbp[8];
for (desc_len = 0, k = 0; k < add_sb_len; k += desc_len) {
int add_d_len;
descp += desc_len;
add_d_len = (k < (add_sb_len - 1)) ? descp[1]: -1;
desc_len = add_d_len + 2;
if (descp[0] == desc_type)
return descp;
if (add_d_len < 0) /* short descriptor ?? */
break;
}
return NULL;
}
/* Returns true if valid bit set, false if valid bit clear. Irrespective the
* information field is written out via 'info_outp' (except when it is
* NULL). Handles both fixed and descriptor sense formats. */
bool
sg_get_sense_info_fld(const uint8_t * sbp, int sb_len,
uint64_t * info_outp)
{
const uint8_t * bp;
if (info_outp)
*info_outp = 0;
if (sb_len < 7)
return false;
switch (sbp[0] & 0x7f) {
case 0x70:
case 0x71:
if (info_outp)
*info_outp = sg_get_unaligned_be32(sbp + 3);
return !!(sbp[0] & 0x80);
case 0x72:
case 0x73:
bp = sg_scsi_sense_desc_find(sbp, sb_len, 0 /* info desc */);
if (bp && (0xa == bp[1])) {
uint64_t ull = sg_get_unaligned_be64(bp + 4);
if (info_outp)
*info_outp = ull;
return !!(bp[2] & 0x80); /* since spc3r23 should be set */
} else
return false;
default:
return false;
}
}
/* Returns true if fixed format or command specific information descriptor
* is found in the descriptor sense; else false. If available the command
* specific information field (4 byte integer in fixed format, 8 byte
* integer in descriptor format) is written out via 'cmd_spec_outp'.
* Handles both fixed and descriptor sense formats. */
bool
sg_get_sense_cmd_spec_fld(const uint8_t * sbp, int sb_len,
uint64_t * cmd_spec_outp)
{
const uint8_t * bp;
if (cmd_spec_outp)
*cmd_spec_outp = 0;
if (sb_len < 7)
return false;
switch (sbp[0] & 0x7f) {
case 0x70:
case 0x71:
if (cmd_spec_outp)
*cmd_spec_outp = sg_get_unaligned_be32(sbp + 8);
return true;
case 0x72:
case 0x73:
bp = sg_scsi_sense_desc_find(sbp, sb_len,
1 /* command specific info desc */);
if (bp && (0xa == bp[1])) {
if (cmd_spec_outp)
*cmd_spec_outp = sg_get_unaligned_be64(bp + 4);
return true;
} else
return false;
default:
return false;
}
}
/* Returns true if any of the 3 bits (i.e. FILEMARK, EOM or ILI) are set.
* In descriptor format if the stream commands descriptor not found
* then returns false. Writes true or false corresponding to these bits to
* the last three arguments if they are non-NULL. */
bool
sg_get_sense_filemark_eom_ili(const uint8_t * sbp, int sb_len,
bool * filemark_p, bool * eom_p, bool * ili_p)
{
const uint8_t * bp;
if (sb_len < 7)
return false;
switch (sbp[0] & 0x7f) {
case 0x70:
case 0x71:
if (sbp[2] & 0xe0) {
if (filemark_p)
*filemark_p = !!(sbp[2] & 0x80);
if (eom_p)
*eom_p = !!(sbp[2] & 0x40);
if (ili_p)
*ili_p = !!(sbp[2] & 0x20);
return true;
} else
return false;
case 0x72:
case 0x73:
/* Look for stream commands sense data descriptor */
bp = sg_scsi_sense_desc_find(sbp, sb_len, 4);
if (bp && (bp[1] >= 2)) {
if (bp[3] & 0xe0) {
if (filemark_p)
*filemark_p = !!(bp[3] & 0x80);
if (eom_p)
*eom_p = !!(bp[3] & 0x40);
if (ili_p)
*ili_p = !!(bp[3] & 0x20);
return true;
}
}
return false;
default:
return false;
}
}
/* Returns true if SKSV is set and sense key is NO_SENSE or NOT_READY. Also
* returns true if progress indication sense data descriptor found. Places
* progress field from sense data where progress_outp points. If progress
* field is not available returns false and *progress_outp is unaltered.
* Handles both fixed and descriptor sense formats.
* Hint: if true is returned *progress_outp may be multiplied by 100 then
* divided by 65536 to get the percentage completion. */
bool
sg_get_sense_progress_fld(const uint8_t * sbp, int sb_len,
int * progress_outp)
{
const uint8_t * bp;
int sk, sk_pr;
if (sb_len < 7)
return false;
switch (sbp[0] & 0x7f) {
case 0x70:
case 0x71:
sk = (sbp[2] & 0xf);
if ((sb_len < 18) ||
((SPC_SK_NO_SENSE != sk) && (SPC_SK_NOT_READY != sk)))
return false;
if (sbp[15] & 0x80) { /* SKSV bit set */
if (progress_outp)
*progress_outp = sg_get_unaligned_be16(sbp + 16);
return true;
} else
return false;
case 0x72:
case 0x73:
/* sense key specific progress (0x2) or progress descriptor (0xa) */
sk = (sbp[1] & 0xf);
sk_pr = (SPC_SK_NO_SENSE == sk) || (SPC_SK_NOT_READY == sk);
if (sk_pr && ((bp = sg_scsi_sense_desc_find(sbp, sb_len, 2))) &&
(0x6 == bp[1]) && (0x80 & bp[4])) {
if (progress_outp)
*progress_outp = sg_get_unaligned_be16(bp + 5);
return true;
} else if (((bp = sg_scsi_sense_desc_find(sbp, sb_len, 0xa))) &&
((0x6 == bp[1]))) {
if (progress_outp)
*progress_outp = sg_get_unaligned_be16(bp + 6);
return true;
} else
return false;
default:
return false;
}
}
char *
sg_get_pdt_str(int pdt, int buff_len, char * buff)
{
if ((pdt < 0) || (pdt > PDT_MAX))
sg_scnpr(buff, buff_len, "bad pdt");
else
sg_scnpr(buff, buff_len, "%s", sg_lib_pdt_strs[pdt]);
return buff;
}
int
sg_get_pdt_from_acronym(const char * acron)
{
int k;
int len = strlen(acron);
const struct sg_aux_info_t * aip;
const char * cc0p;
const char * ccp;
char b[32];
static const int blen = sizeof(b);
if (len >= blen)
len = blen - 1;
for (k = 0; k < len; ++k)
b[k] = tolower(acron[k]);
b[k] = '\0';
if (0 == memcmp("xxx", b, 3))
goto print_pdt_strs;
if (0 == memcmp("spc", b, 3))
return -1;
for (k = 0, aip = sg_lib_pdt_aux_a; k < 0x20; ++k, ++aip) {
if (len < aip->min_match_len)
continue; /* acron too short to match this item */
cc0p = aip->acron;
while ((ccp = strchr(cc0p, ';'))) {
if (0 == memcmp(b, cc0p, aip->min_match_len))
return k;
cc0p = ccp + 1;
}
if (0 == memcmp(b, cc0p, aip->min_match_len))
return k;
}
return -2;
print_pdt_strs:
pr2ws("List of peripheral device type (pdt) acronyms:\n");
for (k = 0, aip = sg_lib_pdt_aux_a; k < 0x20; ++k, ++aip)
pr2ws(" PDT 0x%x: %s [%d]\n", k, aip->acron, aip->min_match_len);
pr2ws("\nMultiple acronyms for a pdt are separated by semi-colons.\n");
pr2ws("The number in square brackets is the minimum match length.\n");
return -3;
}
/* Returns true if left argument is "equal" to the right argument. l_pdt_s
* is a compound PDT (SCSI Peripheral Device Type) or a negative number
* which represents a wildcard (i.e. match anything). r_pdt_s has a similar
* form. PDT values are 5 bits long (0 to 31) and a compound pdt_s is
* formed by shifting the second (upper) PDT by eight bits to the left and
* OR-ing it with the first PDT. The pdt_s values must be defined so
* PDT_DISK (0) is _not_ the upper value in a compound pdt_s. */
bool
sg_pdt_s_eq(int l_pdt_s, int r_pdt_s)
{
bool upper_l = !!(l_pdt_s & PDT_UPPER_MASK);
bool upper_r = !!(r_pdt_s & PDT_UPPER_MASK);
if ((l_pdt_s < 0) || (r_pdt_s < 0))
return true;
if (!upper_l && !upper_r)
return l_pdt_s == r_pdt_s;
else if (upper_l && upper_r)
return (((PDT_UPPER_MASK & l_pdt_s) == (PDT_UPPER_MASK & r_pdt_s)) ||
((PDT_LOWER_MASK & l_pdt_s) == (PDT_LOWER_MASK & r_pdt_s)));
else if (upper_l)
return (((PDT_LOWER_MASK & l_pdt_s) == r_pdt_s) ||
((PDT_UPPER_MASK & l_pdt_s) >> 8) == r_pdt_s);
return (((PDT_LOWER_MASK & r_pdt_s) == l_pdt_s) ||
((PDT_UPPER_MASK & r_pdt_s) >> 8) == l_pdt_s);
}
int
sg_lib_pdt_decay(int pdt)
{
if ((pdt < 0) || (pdt > PDT_MAX))
return 0;
return sg_lib_pdt_decay_arr[pdt];
}
char *
sg_get_trans_proto_str(int tpi, int buff_len, char * buff)
{
if ((tpi < 0) || (tpi > 15))
sg_scnpr(buff, buff_len, "bad tpi");
else
sg_scnpr(buff, buff_len, "%s", sg_lib_transport_proto_strs[tpi]);
return buff;
}
#define TRANSPORT_ID_MIN_LEN 24
char *
sg_decode_transportid_str(const char * lip, uint8_t * bp, int bplen,
bool only_one, int blen, char * b)
{
int num, k, n;
uint64_t ull;
int bump;
if ((NULL == b) || (blen < 1))
return b;
else if (1 == blen) {
b[0] = '\0';
return b;
}
if (NULL == lip)
lip = "";
/* bump = TRANSPORT_ID_MIN_LEN; // some old compilers insisted on this */
for (k = 0, n = 0; bplen > 0; ++k, bp += bump, bplen -= bump) {
int proto_id, normal_len, tpid_format;
if ((k > 0) && only_one)
break;
if ((bplen < 24) || (0 != (bplen % 4)))
n += sg_scn3pr(b, blen, n, "%sTransport Id short or not "
"multiple of 4 [length=%d]:\n", lip, blen);
else
n += sg_scn3pr(b, blen, n, "%sTransport Id of initiator:\n", lip);
tpid_format = ((bp[0] >> 6) & 0x3);
proto_id = (bp[0] & 0xf);
normal_len = (bplen > TRANSPORT_ID_MIN_LEN) ?
TRANSPORT_ID_MIN_LEN : bplen;
switch (proto_id) {
case TPROTO_FCP: /* Fibre channel */
n += sg_scn3pr(b, blen, n, "%s FCP-2 World Wide Name:\n", lip);
if (0 != tpid_format)
n += sg_scn3pr(b, blen, n, "%s [Unexpected TPID format: "
"%d]\n", lip, tpid_format);
n += hex2str(bp + 8, 8, lip, 1, blen - n, b + n);
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_SPI: /* Scsi Parallel Interface, obsolete */
n += sg_scn3pr(b, blen, n, "%s Parallel SCSI initiator SCSI "
"address: 0x%x\n", lip,
sg_get_unaligned_be16(bp + 2));
if (0 != tpid_format)
n += sg_scn3pr(b, blen, n, "%s [Unexpected TPID format: "
"%d]\n", lip, tpid_format);
n += sg_scn3pr(b, blen, n, "%s relative port number (of "
"corresponding target): 0x%x\n", lip,
sg_get_unaligned_be16(bp + 6));
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_SSA:
n += sg_scn3pr(b, blen, n, "%s SSA (transport id not "
"defined):\n", lip);
n += sg_scn3pr(b, blen, n, "%s TPID format: %d\n", lip,
tpid_format);
n += hex2str(bp, normal_len, lip, 1, blen - n, b + n);
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_1394: /* IEEE 1394 */
n += sg_scn3pr(b, blen, n, "%s IEEE 1394 EUI-64 name:\n", lip);
if (0 != tpid_format)
n += sg_scn3pr(b, blen, n, "%s [Unexpected TPID format: "
"%d]\n", lip, tpid_format);
n += hex2str(&bp[8], 8, lip, 1, blen - n, b + n);
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_SRP: /* SCSI over RDMA */
n += sg_scn3pr(b, blen, n, "%s RDMA initiator port "
"identifier:\n", lip);
if (0 != tpid_format)
n += sg_scn3pr(b, blen, n, "%s [Unexpected TPID format: "
"%d]\n", lip, tpid_format);
n += hex2str(bp + 8, 16, lip, 1, blen - n, b + n);
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_ISCSI:
n += sg_scn3pr(b, blen, n, "%s iSCSI ", lip);
num = sg_get_unaligned_be16(bp + 2);
if (0 == tpid_format)
n += sg_scn3pr(b, blen, n, "name: %.*s\n", num, &bp[4]);
else if (1 == tpid_format)
n += sg_scn3pr(b, blen, n, "world wide unique port id: "
"%.*s\n", num, &bp[4]);
else {
n += sg_scn3pr(b, blen, n, " [Unexpected TPID format: "
"%d]\n", tpid_format);
n += hex2str(bp, num + 4, lip, 0, blen - n, b + n);
}
bump = (((num + 4) < TRANSPORT_ID_MIN_LEN) ?
TRANSPORT_ID_MIN_LEN : num + 4);
break;
case TPROTO_SAS:
ull = sg_get_unaligned_be64(bp + 4);
n += sg_scn3pr(b, blen, n, "%s SAS address: 0x%" PRIx64 "\n",
lip, ull);
if (0 != tpid_format)
n += sg_scn3pr(b, blen, n, "%s [Unexpected TPID format: "
"%d]\n", lip, tpid_format);
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_ADT: /* no TransportID defined by T10 yet */
n += sg_scn3pr(b, blen, n, "%s ADT:\n", lip);
n += sg_scn3pr(b, blen, n, "%s TPID format: %d\n", lip,
tpid_format);
n += hex2str(bp, normal_len, lip, 1, blen - n, b + n);
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_ATA: /* no TransportID defined by T10 yet */
n += sg_scn3pr(b, blen, n, "%s ATAPI:\n", lip);
n += sg_scn3pr(b, blen, n, "%s TPID format: %d\n", lip,
tpid_format);
n += hex2str(bp, normal_len, lip, 1, blen - n, b + n);
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_UAS: /* no TransportID defined by T10 yet */
n += sg_scn3pr(b, blen, n, "%s UAS:\n", lip);
n += sg_scn3pr(b, blen, n, "%s TPID format: %d\n", lip,
tpid_format);
n += hex2str(bp, normal_len, lip, 1, blen - n, b + n);
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_SOP:
n += sg_scn3pr(b, blen, n, "%s SOP ", lip);
num = sg_get_unaligned_be16(bp + 2);
if (0 == tpid_format)
n += sg_scn3pr(b, blen, n, "Routing ID: 0x%x\n", num);
else {
n += sg_scn3pr(b, blen, n, " [Unexpected TPID format: "
"%d]\n", tpid_format);
n += hex2str(bp, normal_len, lip, 1, blen - n, b + n);
}
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_PCIE: /* no TransportID defined by T10 yet */
n += sg_scn3pr(b, blen, n, "%s PCIE:\n", lip);
n += sg_scn3pr(b, blen, n, "%s TPID format: %d\n", lip,
tpid_format);
n += hex2str(bp, normal_len, lip, 1, blen - n, b + n);
bump = TRANSPORT_ID_MIN_LEN;
break;
case TPROTO_NONE: /* no TransportID defined by T10 */
n += sg_scn3pr(b, blen, n, "%s No specified protocol\n", lip);
/* n += hex2str(bp, ((bplen > 24) ? 24 : bplen),
* lip, 0, blen - n, b + n); */
bump = TRANSPORT_ID_MIN_LEN;
break;
default:
n += sg_scn3pr(b, blen, n, "%s unknown protocol id=0x%x "
"TPID format=%d\n", lip, proto_id, tpid_format);
n += hex2str(bp, normal_len, lip, 1, blen - n, b + n);
bump = TRANSPORT_ID_MIN_LEN;
break;
}
}
return b;
}
static const char * desig_code_set_str_arr[] =
{
"Reserved [0x0]",
"Binary",
"ASCII",
"UTF-8",
"Reserved [0x4]", "Reserved [0x5]", "Reserved [0x6]", "Reserved [0x7]",
"Reserved [0x8]", "Reserved [0x9]", "Reserved [0xa]", "Reserved [0xb]",
"Reserved [0xc]", "Reserved [0xd]", "Reserved [0xe]", "Reserved [0xf]",
};
const char *
sg_get_desig_code_set_str(int val)
{
if ((val >= 0) && (val < (int)SG_ARRAY_SIZE(desig_code_set_str_arr)))
return desig_code_set_str_arr[val];
else
return NULL;
}
static const char * desig_assoc_str_arr[] =
{
"Addressed logical unit",
"Target port", /* that received request; unless SCSI ports VPD */
"Target device that contains addressed lu",
"Reserved [0x3]",
};
const char *
sg_get_desig_assoc_str(int val)
{
if ((val >= 0) && (val < (int)SG_ARRAY_SIZE(desig_assoc_str_arr)))
return desig_assoc_str_arr[val];
else
return NULL;
}
static const char * desig_type_str_arr[] =
{
"Vendor specific [0x0]",
"T10 vendor identification",
"EUI-64 based",
"NAA",
"Relative target port",
"Target port group", /* spc4r09: _primary_ target port group */
"Logical unit group",
"MD5 logical unit identifier",
"SCSI name string",
"Protocol specific port identifier", /* spc4r36 */
"UUID identifier", /* spc5r08 */
"Reserved [0xb]",
"Reserved [0xc]", "Reserved [0xd]", "Reserved [0xe]", "Reserved [0xf]",
};
const char *
sg_get_desig_type_str(int val)
{
if ((val >= 0) && (val < (int)SG_ARRAY_SIZE(desig_type_str_arr)))
return desig_type_str_arr[val];
else
return NULL;
}
char *
sg_get_zone_type_str(uint8_t zt, int buff_len, char * buff)
{
if ((NULL == buff) || (buff_len < 1))
return NULL;
switch (zt) {
case 1:
sg_scnpr(buff, buff_len, "conventional");
break;
case 2:
sg_scnpr(buff, buff_len, "sequential write required");
break;
case 3: /* obsolete: zbc3r02 */
sg_scnpr(buff, buff_len, "sequential write preferred");
break;
case 4:
sg_scnpr(buff, buff_len, "sequential or before required");
break;
case 5:
sg_scnpr(buff, buff_len, "gap");
break;
default:
sg_scnpr(buff, buff_len, "unknown [0x%x]", zt);
break;
}
return buff;
}
/* Expects a T10 UUID designator (as found in the Device Identification VPD
* page) pointed to by 'dp'. To not produce an error string in 'b', c_set
* should be 1 (binary) and dlen should be 18. Currently T10 only supports
* locally assigned UUIDs. Writes output to string 'b' of no more than blen
* bytes and returns the number of bytes actually written to 'b' but doesn't
* count the trailing null character it always appends (if blen > 0). 'lip'
* is lead-in string (on each line) than may be NULL. skip_prefix avoids
* outputting: ' Locally assigned UUID: ' before the UUID. */
int
sg_t10_uuid_desig2str(const uint8_t *dp, int dlen, int c_set, bool do_long,
bool skip_prefix, const char * lip /* lead-in */,
int blen, char * b)
{
int m;
int n = 0;
if (NULL == lip)
lip = "";
if (1 != c_set) {
n += sg_scn3pr(b, blen, n, "%s << expected binary code_set >>\n",
lip);
n += hex2str(dp, dlen, lip, 0, blen - n, b + n);
return n;
}
if ((1 != ((dp[0] >> 4) & 0xf)) || (18 != dlen)) {
n += sg_scn3pr(b, blen, n, "%s << expected locally assigned "
"UUID, 16 bytes long >>\n", lip);
n += hex2str(dp, dlen, lip, 0, blen - n, b + n);
return n;
}
if (skip_prefix) {
if (strlen(lip) > 0)
n += sg_scn3pr(b, blen, n, "%s", lip);
} else