1*44704f69SBart Van Assche #ifndef SG_LIB_H 2*44704f69SBart Van Assche #define SG_LIB_H 3*44704f69SBart Van Assche 4*44704f69SBart Van Assche /* 5*44704f69SBart Van Assche * Copyright (c) 2004-2022 Douglas Gilbert. 6*44704f69SBart Van Assche * All rights reserved. 7*44704f69SBart Van Assche * Use of this source code is governed by a BSD-style 8*44704f69SBart Van Assche * license that can be found in the BSD_LICENSE file. 9*44704f69SBart Van Assche * 10*44704f69SBart Van Assche * SPDX-License-Identifier: BSD-2-Clause 11*44704f69SBart Van Assche */ 12*44704f69SBart Van Assche 13*44704f69SBart Van Assche /* 14*44704f69SBart Van Assche * 15*44704f69SBart Van Assche * On 5th October 2004 a FreeBSD license was added to this file. 16*44704f69SBart Van Assche * The intention is to keep this file and the related sg_lib.c file 17*44704f69SBart Van Assche * as open source and encourage their unencumbered use. 18*44704f69SBart Van Assche * 19*44704f69SBart Van Assche * Current version number of this library is in the sg_lib_data.c file and 20*44704f69SBart Van Assche * can be accessed with the sg_lib_version() function. 21*44704f69SBart Van Assche */ 22*44704f69SBart Van Assche 23*44704f69SBart Van Assche 24*44704f69SBart Van Assche /* 25*44704f69SBart Van Assche * This header file contains defines and function declarations that may 26*44704f69SBart Van Assche * be useful to applications that communicate with devices that use a 27*44704f69SBart Van Assche * SCSI command set. These command sets have names like SPC-4, SBC-3, 28*44704f69SBart Van Assche * SSC-3, SES-2 and draft standards defining them can be found at 29*44704f69SBart Van Assche * https://www.t10.org . Virtually all devices in the Linux SCSI subsystem 30*44704f69SBart Van Assche * utilize SCSI command sets. Many devices in other Linux device subsystems 31*44704f69SBart Van Assche * utilize SCSI command sets either natively or via emulation (e.g. a 32*44704f69SBart Van Assche * SATA disk in a USB enclosure). 33*44704f69SBart Van Assche */ 34*44704f69SBart Van Assche 35*44704f69SBart Van Assche #include <stdio.h> 36*44704f69SBart Van Assche #include <stdint.h> 37*44704f69SBart Van Assche #include <stdbool.h> 38*44704f69SBart Van Assche 39*44704f69SBart Van Assche #ifdef __cplusplus 40*44704f69SBart Van Assche extern "C" { 41*44704f69SBart Van Assche #endif 42*44704f69SBart Van Assche 43*44704f69SBart Van Assche /* SCSI Peripheral Device Types (PDT) [5 bit field] */ 44*44704f69SBart Van Assche #define PDT_DISK 0x0 /* direct access block device (disk) */ 45*44704f69SBart Van Assche #define PDT_TAPE 0x1 /* sequential access device (magnetic tape) */ 46*44704f69SBart Van Assche #define PDT_PRINTER 0x2 /* printer device (see SSC-1) */ 47*44704f69SBart Van Assche #define PDT_PROCESSOR 0x3 /* processor device (e.g. SAFTE device) */ 48*44704f69SBart Van Assche #define PDT_WO 0x4 /* write once device (some optical disks) */ 49*44704f69SBart Van Assche #define PDT_MMC 0x5 /* CD/DVD/BD (multi-media) */ 50*44704f69SBart Van Assche #define PDT_SCANNER 0x6 /* obsolete */ 51*44704f69SBart Van Assche #define PDT_OPTICAL 0x7 /* optical memory device (some optical disks) */ 52*44704f69SBart Van Assche #define PDT_MCHANGER 0x8 /* media changer device (e.g. tape robot) */ 53*44704f69SBart Van Assche #define PDT_COMMS 0x9 /* communications device (obsolete) */ 54*44704f69SBart Van Assche #define PDT_SAC 0xc /* storage array controller device */ 55*44704f69SBart Van Assche #define PDT_SES 0xd /* SCSI Enclosure Services (SES) device */ 56*44704f69SBart Van Assche #define PDT_RBC 0xe /* Reduced Block Commands (simplified PDT_DISK) */ 57*44704f69SBart Van Assche #define PDT_OCRW 0xf /* optical card read/write device */ 58*44704f69SBart Van Assche #define PDT_BCC 0x10 /* bridge controller commands */ 59*44704f69SBart Van Assche #define PDT_OSD 0x11 /* Object Storage Device (OSD) */ 60*44704f69SBart Van Assche #define PDT_ADC 0x12 /* Automation/drive commands (ADC) */ 61*44704f69SBart Van Assche #define PDT_SMD 0x13 /* Security Manager Device (SMD) */ 62*44704f69SBart Van Assche #define PDT_ZBC 0x14 /* Zoned Block Commands (ZBC) */ 63*44704f69SBart Van Assche #define PDT_WLUN 0x1e /* Well known logical unit (WLUN) */ 64*44704f69SBart Van Assche #define PDT_UNKNOWN 0x1f /* Unknown or no device type */ 65*44704f69SBart Van Assche #define PDT_MASK 0x1f /* For byte 0 of INQUIRY response */ 66*44704f69SBart Van Assche #define PDT_MAX 0x1f 67*44704f69SBart Van Assche 68*44704f69SBart Van Assche #define GRPNUM_MASK 0x3f 69*44704f69SBart Van Assche 70*44704f69SBart Van Assche /* ZBC disks use either PDT_ZBC (if 'host managed') or PDT_DISK . 71*44704f69SBart Van Assche * So squeeze two PDTs into one integer. Use sg_pdt_s_eq() to compare. 72*44704f69SBart Van Assche * N.B. Must not use PDT_DISK as upper */ 73*44704f69SBart Van Assche #define PDT_DISK_ZBC (PDT_DISK | (PDT_ZBC << 8)) 74*44704f69SBart Van Assche #define PDT_ALL (-1) /* for common to all PDTs */ 75*44704f69SBart Van Assche #define PDT_LOWER_MASK 0xff 76*44704f69SBart Van Assche #define PDT_UPPER_MASK (~PDT_LOWER_MASK) 77*44704f69SBart Van Assche 78*44704f69SBart Van Assche #ifndef SAM_STAT_GOOD 79*44704f69SBart Van Assche /* The SCSI status codes as found in SAM-4 at www.t10.org */ 80*44704f69SBart Van Assche #define SAM_STAT_GOOD 0x0 81*44704f69SBart Van Assche #define SAM_STAT_CHECK_CONDITION 0x2 82*44704f69SBart Van Assche #define SAM_STAT_CONDITION_MET 0x4 /* this is not an error */ 83*44704f69SBart Van Assche #define SAM_STAT_BUSY 0x8 84*44704f69SBart Van Assche #define SAM_STAT_INTERMEDIATE 0x10 /* obsolete in SAM-4 */ 85*44704f69SBart Van Assche #define SAM_STAT_INTERMEDIATE_CONDITION_MET 0x14 /* obsolete in SAM-4 */ 86*44704f69SBart Van Assche #define SAM_STAT_RESERVATION_CONFLICT 0x18 87*44704f69SBart Van Assche #define SAM_STAT_COMMAND_TERMINATED 0x22 /* obsolete in SAM-3 */ 88*44704f69SBart Van Assche #define SAM_STAT_TASK_SET_FULL 0x28 89*44704f69SBart Van Assche #define SAM_STAT_ACA_ACTIVE 0x30 90*44704f69SBart Van Assche #define SAM_STAT_TASK_ABORTED 0x40 91*44704f69SBart Van Assche #endif 92*44704f69SBart Van Assche 93*44704f69SBart Van Assche /* The SCSI sense key codes as found in SPC-4 at www.t10.org */ 94*44704f69SBart Van Assche #define SPC_SK_NO_SENSE 0x0 95*44704f69SBart Van Assche #define SPC_SK_RECOVERED_ERROR 0x1 96*44704f69SBart Van Assche #define SPC_SK_NOT_READY 0x2 97*44704f69SBart Van Assche #define SPC_SK_MEDIUM_ERROR 0x3 98*44704f69SBart Van Assche #define SPC_SK_HARDWARE_ERROR 0x4 99*44704f69SBart Van Assche #define SPC_SK_ILLEGAL_REQUEST 0x5 100*44704f69SBart Van Assche #define SPC_SK_UNIT_ATTENTION 0x6 101*44704f69SBart Van Assche #define SPC_SK_DATA_PROTECT 0x7 102*44704f69SBart Van Assche #define SPC_SK_BLANK_CHECK 0x8 103*44704f69SBart Van Assche #define SPC_SK_VENDOR_SPECIFIC 0x9 104*44704f69SBart Van Assche #define SPC_SK_COPY_ABORTED 0xa 105*44704f69SBart Van Assche #define SPC_SK_ABORTED_COMMAND 0xb 106*44704f69SBart Van Assche #define SPC_SK_RESERVED 0xc 107*44704f69SBart Van Assche #define SPC_SK_VOLUME_OVERFLOW 0xd 108*44704f69SBart Van Assche #define SPC_SK_MISCOMPARE 0xe 109*44704f69SBart Van Assche #define SPC_SK_COMPLETED 0xf 110*44704f69SBart Van Assche 111*44704f69SBart Van Assche /* Transport protocol identifiers or just Protocol identifiers */ 112*44704f69SBart Van Assche #define TPROTO_FCP 0 113*44704f69SBart Van Assche #define TPROTO_SPI 1 114*44704f69SBart Van Assche #define TPROTO_SSA 2 115*44704f69SBart Van Assche #define TPROTO_1394 3 116*44704f69SBart Van Assche #define TPROTO_SRP 4 /* SCSI over RDMA */ 117*44704f69SBart Van Assche #define TPROTO_ISCSI 5 118*44704f69SBart Van Assche #define TPROTO_SAS 6 119*44704f69SBart Van Assche #define TPROTO_ADT 7 120*44704f69SBart Van Assche #define TPROTO_ATA 8 121*44704f69SBart Van Assche #define TPROTO_UAS 9 /* USB attached SCSI */ 122*44704f69SBart Van Assche #define TPROTO_SOP 0xa /* SCSI over PCIe */ 123*44704f69SBart Van Assche #define TPROTO_PCIE 0xb /* includes NVMe */ 124*44704f69SBart Van Assche #define TPROTO_NONE 0xf 125*44704f69SBart Van Assche 126*44704f69SBart Van Assche /* SCSI Feature Sets (sfs) */ 127*44704f69SBart Van Assche #define SCSI_FS_SPC_DISCOVERY_2016 0x1 128*44704f69SBart Van Assche #define SCSI_FS_SBC_BASE_2010 0x102 129*44704f69SBart Van Assche #define SCSI_FS_SBC_BASE_2016 0x101 130*44704f69SBart Van Assche #define SCSI_FS_SBC_BASIC_PROV_2016 0x103 131*44704f69SBart Van Assche #define SCSI_FS_SBC_DRIVE_MAINT_2016 0x104 132*44704f69SBart Van Assche #define SCSI_FS_ZBC_HOST_AWARE_2020 0x300 133*44704f69SBart Van Assche #define SCSI_FS_ZBC_HOST_MANAGED_2020 0x301 134*44704f69SBart Van Assche #define SCSI_FS_ZBC_DOMAINS_REALMS_2020 0x302 135*44704f69SBart Van Assche 136*44704f69SBart Van Assche /* Often SCSI responses use the highest integer that can fit in a field 137*44704f69SBart Van Assche * to indicate "unbounded" or limit does not apply. Sometimes represented 138*44704f69SBart Van Assche * in output as "-1" for brevity */ 139*44704f69SBart Van Assche #define SG_LIB_UNBOUNDED_16BIT 0xffff 140*44704f69SBart Van Assche #define SG_LIB_UNBOUNDED_32BIT 0xffffffffU 141*44704f69SBart Van Assche #define SG_LIB_UNBOUNDED_64BIT 0xffffffffffffffffULL 142*44704f69SBart Van Assche 143*44704f69SBart Van Assche #if (__STDC_VERSION__ >= 199901L) /* C99 or later */ 144*44704f69SBart Van Assche typedef uintptr_t sg_uintptr_t; 145*44704f69SBart Van Assche #else 146*44704f69SBart Van Assche typedef unsigned long sg_uintptr_t; 147*44704f69SBart Van Assche #endif 148*44704f69SBart Van Assche 149*44704f69SBart Van Assche /* Borrowed from Linux kernel; no check that 'arr' actually is one */ 150*44704f69SBart Van Assche #define SG_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 151*44704f69SBart Van Assche 152*44704f69SBart Van Assche /* Doesn't seem to be a common C and C++ technique for clearing an 153*44704f69SBart Van Assche * aggregrate (e.g. a struct instance) on the stack. Hence this hack: */ 154*44704f69SBart Van Assche #ifdef __cplusplus 155*44704f69SBart Van Assche #define SG_C_CPP_ZERO_INIT {} 156*44704f69SBart Van Assche #else 157*44704f69SBart Van Assche #define SG_C_CPP_ZERO_INIT ={0} 158*44704f69SBart Van Assche #endif 159*44704f69SBart Van Assche 160*44704f69SBart Van Assche 161*44704f69SBart Van Assche /* The format of the version string is like this: "2.26 20170906" */ 162*44704f69SBart Van Assche const char * sg_lib_version(); 163*44704f69SBart Van Assche 164*44704f69SBart Van Assche /* Returns length of SCSI command given the opcode (first byte). 165*44704f69SBart Van Assche * Yields the wrong answer for variable length commands (opcode=0x7f) 166*44704f69SBart Van Assche * and potentially some vendor specific commands. */ 167*44704f69SBart Van Assche int sg_get_command_size(uint8_t cdb_byte0); 168*44704f69SBart Van Assche 169*44704f69SBart Van Assche /* Command name given pointer to the cdb. Certain command names 170*44704f69SBart Van Assche * depend on peripheral type (give 0 or -1 if unknown). Places command 171*44704f69SBart Van Assche * name into buff and will write no more than buff_len bytes. */ 172*44704f69SBart Van Assche void sg_get_command_name(const uint8_t * cdbp, int peri_type, int buff_len, 173*44704f69SBart Van Assche char * buff); 174*44704f69SBart Van Assche 175*44704f69SBart Van Assche /* Command name given only the first byte (byte 0) of a cdb and 176*44704f69SBart Van Assche * peripheral type (give 0 or -1 if unknown). */ 177*44704f69SBart Van Assche void sg_get_opcode_name(uint8_t cdb_byte0, int peri_type, int buff_len, 178*44704f69SBart Van Assche char * buff); 179*44704f69SBart Van Assche 180*44704f69SBart Van Assche /* Command name given opcode (byte 0), service action and peripheral type. 181*44704f69SBart Van Assche * If no service action give 0, if unknown peripheral type give 0 or -1 . */ 182*44704f69SBart Van Assche void sg_get_opcode_sa_name(uint8_t cdb_byte0, int service_action, 183*44704f69SBart Van Assche int peri_type, int buff_len, char * buff); 184*44704f69SBart Van Assche 185*44704f69SBart Van Assche /* Fetch NVMe command name given first byte (byte offset 0 in 64 byte 186*44704f69SBart Van Assche * command) of command. Gets Admin NVMe command name if 'admin' is true 187*44704f69SBart Van Assche * (e.g. opcode=0x6 -> Identify), otherwise gets NVM command set name 188*44704f69SBart Van Assche * (e.g. opcode=0 -> Flush). Returns 'buff'. */ 189*44704f69SBart Van Assche char * sg_get_nvme_opcode_name(uint8_t cmd_byte0, bool admin, int buff_len, 190*44704f69SBart Van Assche char * buff); 191*44704f69SBart Van Assche 192*44704f69SBart Van Assche /* Fetch scsi status string. */ 193*44704f69SBart Van Assche void sg_get_scsi_status_str(int scsi_status, int buff_len, char * buff); 194*44704f69SBart Van Assche 195*44704f69SBart Van Assche /* This is a slightly stretched SCSI sense "descriptor" format header. 196*44704f69SBart Van Assche * The addition is to allow the 0x70 and 0x71 response codes. The idea 197*44704f69SBart Van Assche * is to place the salient data of both "fixed" and "descriptor" sense 198*44704f69SBart Van Assche * format into one structure to ease application processing. 199*44704f69SBart Van Assche * The original sense buffer should be kept around for those cases 200*44704f69SBart Van Assche * in which more information is required (e.g. the LBA of a MEDIUM ERROR). */ 201*44704f69SBart Van Assche struct sg_scsi_sense_hdr { 202*44704f69SBart Van Assche uint8_t response_code; /* permit: 0x0, 0x70, 0x71, 0x72, 0x73 */ 203*44704f69SBart Van Assche uint8_t sense_key; 204*44704f69SBart Van Assche uint8_t asc; 205*44704f69SBart Van Assche uint8_t ascq; 206*44704f69SBart Van Assche uint8_t byte4; /* descriptor: SDAT_OVFL; fixed: lower three ... */ 207*44704f69SBart Van Assche uint8_t byte5; /* ... bytes of INFO field */ 208*44704f69SBart Van Assche uint8_t byte6; 209*44704f69SBart Van Assche uint8_t additional_length; /* zero for fixed format sense data */ 210*44704f69SBart Van Assche }; 211*44704f69SBart Van Assche 212*44704f69SBart Van Assche /* The '_is_good()' returns true when status is SAM_STAT_GOOD or 213*44704f69SBart Van Assche * SAM_STAT_CONDITION_MET, returns false otherwise. Ignores bit 0. The 214*44704f69SBart Van Assche * '_is_bad() variant is the logical inverse. */ 215*44704f69SBart Van Assche bool sg_scsi_status_is_good(int sstatus); 216*44704f69SBart Van Assche bool sg_scsi_status_is_bad(int sstatus); 217*44704f69SBart Van Assche 218*44704f69SBart Van Assche /* Maps the salient data from a sense buffer which is in either fixed or 219*44704f69SBart Van Assche * descriptor format into a structure mimicking a descriptor format 220*44704f69SBart Van Assche * header (i.e. the first 8 bytes of sense descriptor format). 221*44704f69SBart Van Assche * If zero response code returns false. Otherwise returns true and if 'sshp' 222*44704f69SBart Van Assche * is non-NULL then zero all fields and then set the appropriate fields in 223*44704f69SBart Van Assche * that structure. sshp::additional_length is always 0 for response 224*44704f69SBart Van Assche * codes 0x70 and 0x71 (fixed format). */ 225*44704f69SBart Van Assche bool sg_scsi_normalize_sense(const uint8_t * sensep, int sense_len, 226*44704f69SBart Van Assche struct sg_scsi_sense_hdr * sshp); 227*44704f69SBart Van Assche 228*44704f69SBart Van Assche /* Attempt to find the first SCSI sense data descriptor that matches the 229*44704f69SBart Van Assche * given 'desc_type'. If found return pointer to start of sense data 230*44704f69SBart Van Assche * descriptor; otherwise (including fixed format sense data) returns NULL. */ 231*44704f69SBart Van Assche const uint8_t * sg_scsi_sense_desc_find(const uint8_t * sensep, int sense_len, 232*44704f69SBart Van Assche int desc_type); 233*44704f69SBart Van Assche 234*44704f69SBart Van Assche /* Get sense key from sense buffer. If successful returns a sense key value 235*44704f69SBart Van Assche * between 0 and 15. If sense buffer cannot be decode, returns -1 . */ 236*44704f69SBart Van Assche int sg_get_sense_key(const uint8_t * sensep, int sense_len); 237*44704f69SBart Van Assche 238*44704f69SBart Van Assche /* Yield string associated with sense_key value. Returns 'buff'. */ 239*44704f69SBart Van Assche char * sg_get_sense_key_str(int sense_key, int buff_len, char * buff); 240*44704f69SBart Van Assche 241*44704f69SBart Van Assche /* Yield string associated with ASC/ASCQ values. Returns 'buff'. Prefixes 242*44704f69SBart Van Assche * any valid additional sense found with "Additional sense: ". */ 243*44704f69SBart Van Assche char * sg_get_asc_ascq_str(int asc, int ascq, int buff_len, char * buff); 244*44704f69SBart Van Assche 245*44704f69SBart Van Assche /* Same as sg_get_asc_ascq_str() when add_sense_leadin is true. When it is 246*44704f69SBart Van Assche * false this function does _not_ prefix any valid additional sense found 247*44704f69SBart Van Assche * with "Additional sense: ". */ 248*44704f69SBart Van Assche char * sg_get_additional_sense_str(int asc, int ascq, bool add_sense_leadin, 249*44704f69SBart Van Assche int buff_len, char * buff); 250*44704f69SBart Van Assche 251*44704f69SBart Van Assche /* Returns true if valid bit set, false if valid bit clear. Irrespective the 252*44704f69SBart Van Assche * information field is written out via 'info_outp' (except when it is 253*44704f69SBart Van Assche * NULL). Handles both fixed and descriptor sense formats. */ 254*44704f69SBart Van Assche bool sg_get_sense_info_fld(const uint8_t * sensep, int sb_len, 255*44704f69SBart Van Assche uint64_t * info_outp); 256*44704f69SBart Van Assche 257*44704f69SBart Van Assche /* Returns true if fixed format or command specific information descriptor 258*44704f69SBart Van Assche * is found in the descriptor sense; else false. If available the command 259*44704f69SBart Van Assche * specific information field (4 byte integer in fixed format, 8 byte 260*44704f69SBart Van Assche * integer in descriptor format) is written out via 'cmd_spec_outp'. 261*44704f69SBart Van Assche * Handles both fixed and descriptor sense formats. */ 262*44704f69SBart Van Assche bool sg_get_sense_cmd_spec_fld(const uint8_t * sensep, int sb_len, 263*44704f69SBart Van Assche uint64_t * cmd_spec_outp); 264*44704f69SBart Van Assche 265*44704f69SBart Van Assche /* Returns true if any of the 3 bits (i.e. FILEMARK, EOM or ILI) are set. 266*44704f69SBart Van Assche * In descriptor format if the stream commands descriptor not found 267*44704f69SBart Van Assche * then returns false. Writes true or false corresponding to these bits to 268*44704f69SBart Van Assche * the last three arguments if they are non-NULL. */ 269*44704f69SBart Van Assche bool sg_get_sense_filemark_eom_ili(const uint8_t * sensep, int sb_len, 270*44704f69SBart Van Assche bool * filemark_p, bool * eom_p, 271*44704f69SBart Van Assche bool * ili_p); 272*44704f69SBart Van Assche 273*44704f69SBart Van Assche /* Returns true if SKSV is set and sense key is NO_SENSE or NOT_READY. Also 274*44704f69SBart Van Assche * returns true if progress indication sense data descriptor found. Places 275*44704f69SBart Van Assche * progress field from sense data where progress_outp points. If progress 276*44704f69SBart Van Assche * field is not available returns false. Handles both fixed and descriptor 277*44704f69SBart Van Assche * sense formats. N.B. App should multiply by 100 and divide by 65536 278*44704f69SBart Van Assche * to get percentage completion from given value. */ 279*44704f69SBart Van Assche bool sg_get_sense_progress_fld(const uint8_t * sensep, int sb_len, 280*44704f69SBart Van Assche int * progress_outp); 281*44704f69SBart Van Assche 282*44704f69SBart Van Assche /* Closely related to sg_print_sense(). Puts decoded sense data in 'buff'. 283*44704f69SBart Van Assche * Usually multiline with multiple '\n' including one trailing. If 284*44704f69SBart Van Assche * 'raw_sinfo' set appends sense buffer in hex. 'leadin' is string prepended 285*44704f69SBart Van Assche * to each line written to 'buff', NULL treated as "". Returns the number of 286*44704f69SBart Van Assche * bytes written to 'buff' excluding the trailing '\0'. 287*44704f69SBart Van Assche * N.B. prior to sg3_utils v 1.42 'leadin' was only prepended to the first 288*44704f69SBart Van Assche * line output. Also this function returned type void. */ 289*44704f69SBart Van Assche int sg_get_sense_str(const char * leadin, const uint8_t * sense_buffer, 290*44704f69SBart Van Assche int sb_len, bool raw_sinfo, int buff_len, char * buff); 291*44704f69SBart Van Assche 292*44704f69SBart Van Assche /* Decode descriptor format sense descriptors (assumes sense buffer is 293*44704f69SBart Van Assche * in descriptor format). 'leadin' is string prepended to each line written 294*44704f69SBart Van Assche * to 'b', NULL treated as "". Returns the number of bytes written to 'b' 295*44704f69SBart Van Assche * excluding the trailing '\0'. If problem, returns 0. */ 296*44704f69SBart Van Assche int sg_get_sense_descriptors_str(const char * leadin, 297*44704f69SBart Van Assche const uint8_t * sense_buffer, 298*44704f69SBart Van Assche int sb_len, int blen, char * b); 299*44704f69SBart Van Assche 300*44704f69SBart Van Assche /* Decodes a designation descriptor (e.g. as found in the Device 301*44704f69SBart Van Assche * Identification VPD page (0x83)) into string 'b' whose maximum length is 302*44704f69SBart Van Assche * blen. 'leadin' is string prepended to each line written to 'b', NULL 303*44704f69SBart Van Assche * treated as "". Returns the number of bytes written to 'b' excluding the 304*44704f69SBart Van Assche * trailing '\0'. */ 305*44704f69SBart Van Assche int sg_get_designation_descriptor_str(const char * leadin, 306*44704f69SBart Van Assche const uint8_t * ddp, int dd_len, 307*44704f69SBart Van Assche bool print_assoc, bool do_long, 308*44704f69SBart Van Assche int blen, char * b); 309*44704f69SBart Van Assche 310*44704f69SBart Van Assche /* Expects a T10 UUID designator (as found in the Device Identification VPD 311*44704f69SBart Van Assche * page) pointed to by 'dp'. To not produce an error string in 'b', c_set 312*44704f69SBart Van Assche * should be 1 (binary) and dlen should be 18. Currently T10 only supports 313*44704f69SBart Van Assche * locally assigned UUIDs. Writes output to string 'b' of no more than blen 314*44704f69SBart Van Assche * bytes and returns the number of bytes actually written to 'b' but doesn't 315*44704f69SBart Van Assche * count the trailing null character it always appends (if blen > 0). 'lip' 316*44704f69SBart Van Assche * is lead-in string (on each line) than may be NULL. skip_prefix avoids 317*44704f69SBart Van Assche * outputting: ' Locally assigned UUID: ' before the UUID. */ 318*44704f69SBart Van Assche int sg_t10_uuid_desig2str(const uint8_t * dp, int dlen, int c_set, 319*44704f69SBart Van Assche bool do_long, bool skip_prefix, 320*44704f69SBart Van Assche const char * lip, int blen, char * b); 321*44704f69SBart Van Assche 322*44704f69SBart Van Assche /* Yield string associated with peripheral device type (pdt). Returns 323*44704f69SBart Van Assche * 'buff'. If 'pdt' out of range yields "bad pdt" string. */ 324*44704f69SBart Van Assche char * sg_get_pdt_str(int pdt, int buff_len, char * buff); 325*44704f69SBart Van Assche 326*44704f69SBart Van Assche /* Some lesser used PDTs share a lot in common with a more used PDT. 327*44704f69SBart Van Assche * Examples are PDT_ADC decaying to PDT_TAPE and PDT_ZBC to PDT_DISK. 328*44704f69SBart Van Assche * If such a lesser used 'dev_pdt' is given to this function, then it will 329*44704f69SBart Van Assche * return the more used PDT (i.e. "decays to"); otherwise 'dev_pdt' is 330*44704f69SBart Van Assche * returned. Valid for 'pdt' 0 to 31, for other values returns 0. */ 331*44704f69SBart Van Assche int sg_lib_pdt_decay(int dev_pdt); 332*44704f69SBart Van Assche 333*44704f69SBart Van Assche /* Yield string associated with transport protocol identifier (tpi). Returns 334*44704f69SBart Van Assche * 'buff'. If 'tpi' out of range yields "bad tpi" string. */ 335*44704f69SBart Van Assche char * sg_get_trans_proto_str(int tpi, int buff_len, char * buff); 336*44704f69SBart Van Assche 337*44704f69SBart Van Assche /* Decode TransportID pointed to by 'bp' of length 'bplen'. Place decoded 338*44704f69SBart Van Assche * string output in 'buff' which is also the return value. Each new line 339*44704f69SBart Van Assche * is prefixed by 'leadin'. If leadin NULL treat as "". */ 340*44704f69SBart Van Assche char * sg_decode_transportid_str(const char * leadin, uint8_t * bp, int bplen, 341*44704f69SBart Van Assche bool only_one, int buff_len, char * buff); 342*44704f69SBart Van Assche 343*44704f69SBart Van Assche /* Returns a designator's type string given 'val' (0 to 15 inclusive), 344*44704f69SBart Van Assche * otherwise returns NULL. */ 345*44704f69SBart Van Assche const char * sg_get_desig_type_str(int val); 346*44704f69SBart Van Assche 347*44704f69SBart Van Assche /* Returns a designator's code_set string given 'val' (0 to 15 inclusive), 348*44704f69SBart Van Assche * otherwise returns NULL. */ 349*44704f69SBart Van Assche const char * sg_get_desig_code_set_str(int val); 350*44704f69SBart Van Assche 351*44704f69SBart Van Assche /* Returns a designator's association string given 'val' (0 to 3 inclusive), 352*44704f69SBart Van Assche * otherwise returns NULL. */ 353*44704f69SBart Van Assche const char * sg_get_desig_assoc_str(int val); 354*44704f69SBart Van Assche 355*44704f69SBart Van Assche /* Yield string associated with zone type (see ZBC and ZBC-2) [e.g. REPORT 356*44704f69SBart Van Assche * ZONES command response]. Returns 'buff' unless buff_len < 1 in which 357*44704f69SBart Van Assche * NULL is returned. */ 358*44704f69SBart Van Assche char * sg_get_zone_type_str(uint8_t zt, int buff_len, char * buff); 359*44704f69SBart Van Assche 360*44704f69SBart Van Assche /* Yield SCSI Feature Set (sfs) string. When 'peri_type' is < -1 (or > 31) 361*44704f69SBart Van Assche * returns pointer to string (same as 'buff') associated with 'sfs_code'. 362*44704f69SBart Van Assche * When 'peri_type' is between -1 (for SPC) and 31 (inclusive) then a match 363*44704f69SBart Van Assche * on both 'sfs_code' and 'peri_type' is required. If 'foundp' is not NULL 364*44704f69SBart Van Assche * then where it points is set to true if a match is found else it is set to 365*44704f69SBart Van Assche * false. If 'buff' is not NULL then in the case of a match a descriptive 366*44704f69SBart Van Assche * string is written to 'buff' while if there is not a not then a string 367*44704f69SBart Van Assche * ending in "Reserved" is written (and may be prefixed with SPC, SBC, SSC 368*44704f69SBart Van Assche * or ZBC). Returns 'buff' (i.e. a pointer value) even if it is NULL. 369*44704f69SBart Van Assche * Example: 370*44704f69SBart Van Assche * char b[64]; 371*44704f69SBart Van Assche * ... 372*44704f69SBart Van Assche * printf("%s\n", sg_get_sfs_str(sfs_code, -2, sizeof(b), b, NULL, 0)); 373*44704f69SBart Van Assche */ 374*44704f69SBart Van Assche const char * sg_get_sfs_str(uint16_t sfs_code, int peri_type, int buff_len, 375*44704f69SBart Van Assche char * buff, bool * foundp, int verbose); 376*44704f69SBart Van Assche 377*44704f69SBart Van Assche /* This is a heuristic that takes into account the command bytes and length 378*44704f69SBart Van Assche * to decide whether the presented unstructured sequence of bytes could be 379*44704f69SBart Van Assche * a SCSI command. If so it returns true otherwise false. Vendor specific 380*44704f69SBart Van Assche * SCSI commands (i.e. opcodes from 0xc0 to 0xff), if presented, are assumed 381*44704f69SBart Van Assche * to follow SCSI conventions (i.e. length of 6, 10, 12 or 16 bytes). The 382*44704f69SBart Van Assche * only SCSI commands considered above 16 bytes of length are the Variable 383*44704f69SBart Van Assche * Length Commands (opcode 0x7f) and the XCDB wrapped commands (opcode 0x7e). 384*44704f69SBart Van Assche * Both have an inbuilt length field which can be cross checked with clen. 385*44704f69SBart Van Assche * No NVMe commands (64 bytes long plus some extra added by some OSes) have 386*44704f69SBart Van Assche * opcodes 0x7e or 0x7f yet. ATA is register based but SATA has FIS 387*44704f69SBart Van Assche * structures that are sent across the wire. The 'FIS register' structure is 388*44704f69SBart Van Assche * used to move a command from a SATA host to device, but the ATA 'command' 389*44704f69SBart Van Assche * is not the first byte. So it is harder to say what will happen if a 390*44704f69SBart Van Assche * FIS structure is presented as a SCSI command, hopefully there is a low 391*44704f69SBart Van Assche * probability this function will yield true in that case. */ 392*44704f69SBart Van Assche bool sg_is_scsi_cdb(const uint8_t * cdbp, int clen); 393*44704f69SBart Van Assche 394*44704f69SBart Van Assche /* Yield string associated with NVMe command status value in sct_sc. It 395*44704f69SBart Van Assche * expects to decode DW3 bits 27:17 from the completion queue. Bits 27:25 396*44704f69SBart Van Assche * are the Status Code Type (SCT) and bits 24:17 are the Status Code (SC). 397*44704f69SBart Van Assche * Bit 17 in DW3 should be bit 0 in sct_sc. If no status string is found 398*44704f69SBart Van Assche * a string of the form "Reserved [0x<sct_sc_in_hex>]" is generated. 399*44704f69SBart Van Assche * Returns 'buff'. Does nothing if buff_len<=0 or if buff is NULL.*/ 400*44704f69SBart Van Assche char * sg_get_nvme_cmd_status_str(uint16_t sct_sc, int buff_len, char * buff); 401*44704f69SBart Van Assche 402*44704f69SBart Van Assche /* Attempts to map NVMe status value ((SCT << 8) | SC) n sct_sc to a SCSI 403*44704f69SBart Van Assche * status, sense_key, asc and ascq tuple. If successful returns true and 404*44704f69SBart Van Assche * writes to non-NULL pointer arguments; otherwise returns false. */ 405*44704f69SBart Van Assche bool sg_nvme_status2scsi(uint16_t sct_sc, uint8_t * status_p, uint8_t * sk_p, 406*44704f69SBart Van Assche uint8_t * asc_p, uint8_t * ascq_p); 407*44704f69SBart Van Assche 408*44704f69SBart Van Assche /* Add vendor (sg3_utils) specific sense descriptor for the NVMe Status 409*44704f69SBart Van Assche * field. Assumes descriptor (i.e. not fixed) sense. Assume sbp has room. */ 410*44704f69SBart Van Assche void sg_nvme_desc2sense(uint8_t * sbp, bool dnr, bool more, uint16_t sct_sc); 411*44704f69SBart Van Assche 412*44704f69SBart Van Assche /* Build minimum sense buffer, either descriptor type (desc=true) or fixed 413*44704f69SBart Van Assche * type (desc=false). Assume sbp has enough room (8 or 14 bytes 414*44704f69SBart Van Assche * respectively). sbp should have room for 32 or 18 bytes respectively */ 415*44704f69SBart Van Assche void sg_build_sense_buffer(bool desc, uint8_t *sbp, uint8_t skey, 416*44704f69SBart Van Assche uint8_t asc, uint8_t ascq); 417*44704f69SBart Van Assche 418*44704f69SBart Van Assche /* Returns true if left argument is "equal" to the right argument. l_pdt_s 419*44704f69SBart Van Assche * is a compound PDT (SCSI Peripheral Device Type) or a negative number 420*44704f69SBart Van Assche * which represents a wildcard (i.e. match anything). r_pdt_s has a similar 421*44704f69SBart Van Assche * form. PDT values are 5 bits long (0 to 31) and a compound pdt_s is 422*44704f69SBart Van Assche * formed by shifting the second (upper) PDT by eight bits to the left and 423*44704f69SBart Van Assche * OR-ing it with the first PDT. The pdt_s values must be defined so 424*44704f69SBart Van Assche * PDT_DISK (0) is _not_ the upper value in a compound pdt_s. */ 425*44704f69SBart Van Assche bool sg_pdt_s_eq(int l_pdt_s, int r_pdt_s); 426*44704f69SBart Van Assche 427*44704f69SBart Van Assche extern FILE * sg_warnings_strm; 428*44704f69SBart Van Assche 429*44704f69SBart Van Assche void sg_set_warnings_strm(FILE * warnings_strm); 430*44704f69SBart Van Assche 431*44704f69SBart Van Assche /* Given a SCSI command pointed to by cdbp of sz bytes this function forms a 432*44704f69SBart Van Assche * SCSI command in ASCII hex surrounded by square brackets in 'b'. 'b' is at 433*44704f69SBart Van Assche * least blen bytes long. If cmd_name is true then the command is prefixed 434*44704f69SBart Van Assche * by its SCSI command name (e.g. "VERIFY(10) [2f ...]". The command is 435*44704f69SBart Van Assche * shown as spaced separated pairs of hexadecimal digits (i.e. 0-9, a-f). 436*44704f69SBart Van Assche * Each pair represents byte. The leftmost pair of digits is cdbp[0] . If 437*44704f69SBart Van Assche * sz <= 0 then this function tries to guess the length of the command. */ 438*44704f69SBart Van Assche char * 439*44704f69SBart Van Assche sg_get_command_str(const uint8_t * cdbp, int sz, bool cmd_name, int blen, 440*44704f69SBart Van Assche char * b); 441*44704f69SBart Van Assche 442*44704f69SBart Van Assche /* The following "print" functions send ASCII to 'sg_warnings_strm' file 443*44704f69SBart Van Assche * descriptor (default value is stderr). 'leadin' is string prepended to 444*44704f69SBart Van Assche * each line printed out, NULL treated as "". */ 445*44704f69SBart Van Assche void sg_print_command_len(const uint8_t * command, int len); 446*44704f69SBart Van Assche void sg_print_command(const uint8_t * command); 447*44704f69SBart Van Assche void sg_print_scsi_status(int scsi_status); 448*44704f69SBart Van Assche 449*44704f69SBart Van Assche /* DSENSE is 'descriptor sense' as opposed to the older 'fixed sense'. Reads 450*44704f69SBart Van Assche * environment variable SG3_UTILS_DSENSE. Only (currently) used in SNTL. */ 451*44704f69SBart Van Assche bool sg_get_initial_dsense(void); 452*44704f69SBart Van Assche 453*44704f69SBart Van Assche /* 'leadin' is string prepended to each line printed out, NULL treated as 454*44704f69SBart Van Assche * "". N.B. prior to sg3_utils v 1.42 'leadin' was only prepended to the 455*44704f69SBart Van Assche * first line printed. */ 456*44704f69SBart Van Assche void sg_print_sense(const char * leadin, const uint8_t * sense_buffer, 457*44704f69SBart Van Assche int sb_len, bool raw_info); 458*44704f69SBart Van Assche 459*44704f69SBart Van Assche /* This examines exit_status and if an error message is known it is output 460*44704f69SBart Van Assche * to stdout/stderr and true is returned. If no error message is 461*44704f69SBart Van Assche * available nothing is output and false is returned. If exit_status is 462*44704f69SBart Van Assche * zero (no error) nothing is output and true is returned. If exit_status 463*44704f69SBart Van Assche * is negative then nothing is output and false is returned. If leadin is 464*44704f69SBart Van Assche * non-NULL then it is printed before the error message. All messages are 465*44704f69SBart Van Assche * a single line with a trailing LF. */ 466*44704f69SBart Van Assche bool sg_if_can2stdout(const char * leadin, int exit_status); 467*44704f69SBart Van Assche bool sg_if_can2stderr(const char * leadin, int exit_status); 468*44704f69SBart Van Assche 469*44704f69SBart Van Assche /* This examines exit_status and if an error message is known it is output 470*44704f69SBart Van Assche * as a string to 'b' and true is returned. If 'longer' is true and extra 471*44704f69SBart Van Assche * information is available then it is added to the output. If no error 472*44704f69SBart Van Assche * message is available a null character is output and false is returned. 473*44704f69SBart Van Assche * If exit_status is zero (no error) and 'longer' is true then the string 474*44704f69SBart Van Assche * 'No errors' is output; if 'longer' is false then a null character is 475*44704f69SBart Van Assche * output; in both cases true is returned. If exit_status is negative then 476*44704f69SBart Van Assche * a null character is output and false is returned. All messages are a 477*44704f69SBart Van Assche * single line (less than 80 characters) with no trailing LF. The output 478*44704f69SBart Van Assche * string including the trailing null character is no longer than b_len. */ 479*44704f69SBart Van Assche bool sg_exit2str(int exit_status, bool longer, int b_len, char * b); 480*44704f69SBart Van Assche 481*44704f69SBart Van Assche /* Utilities can use these exit status values for syntax errors and 482*44704f69SBart Van Assche * file (device node) problems (e.g. not found or permissions). */ 483*44704f69SBart Van Assche #define SG_LIB_SYNTAX_ERROR 1 /* command line syntax problem */ 484*44704f69SBart Van Assche 485*44704f69SBart Van Assche /* The sg_err_category_sense() function returns one of the following. 486*44704f69SBart Van Assche * These may be used as exit status values (from a process). Notice that 487*44704f69SBart Van Assche * some of the lower values correspond to SCSI sense key values. */ 488*44704f69SBart Van Assche #define SG_LIB_CAT_CLEAN 0 /* No errors or other information */ 489*44704f69SBart Van Assche #define SG_LIB_OK_TRUE SG_LIB_CAT_CLEAN /* No error, reporting true */ 490*44704f69SBart Van Assche /* Value 1 left unused for utilities to use SG_LIB_SYNTAX_ERROR */ 491*44704f69SBart Van Assche #define SG_LIB_CAT_NOT_READY 2 /* sense key: not ready, see 12 and 13 492*44704f69SBart Van Assche * [sk,asc,ascq: 0x2,<most>,<most>] */ 493*44704f69SBart Van Assche #define SG_LIB_CAT_MEDIUM_HARD 3 /* medium or hardware error, blank check 494*44704f69SBart Van Assche * [sk,asc,ascq: 0x3/0x4/0x8,*,*] */ 495*44704f69SBart Van Assche #define SG_LIB_CAT_ILLEGAL_REQ 5 /* Illegal request (other than invalid 496*44704f69SBart Van Assche * opcode): [sk,asc,ascq: 0x5,*,*] */ 497*44704f69SBart Van Assche #define SG_LIB_CAT_UNIT_ATTENTION 6 /* sense key, device state changed 498*44704f69SBart Van Assche * [sk,asc,ascq: 0x6,*,*] */ 499*44704f69SBart Van Assche /* was SG_LIB_CAT_MEDIA_CHANGED earlier [sk,asc,ascq: 0x6,0x28,*] */ 500*44704f69SBart Van Assche #define SG_LIB_CAT_DATA_PROTECT 7 /* sense key, media write protected? 501*44704f69SBart Van Assche * [sk,asc,ascq: 0x7,*,*] */ 502*44704f69SBart Van Assche #define SG_LIB_CAT_INVALID_OP 9 /* (Illegal request,) Invalid opcode: 503*44704f69SBart Van Assche * [sk,asc,ascq: 0x5,0x20,0x0] */ 504*44704f69SBart Van Assche #define SG_LIB_CAT_COPY_ABORTED 10 /* sense key, some data transferred 505*44704f69SBart Van Assche * [sk,asc,ascq: 0xa,*,*] */ 506*44704f69SBart Van Assche #define SG_LIB_CAT_ABORTED_COMMAND 11 /* interpreted from sense buffer 507*44704f69SBart Van Assche * [sk,asc,ascq: 0xb,! 0x10,*] */ 508*44704f69SBart Van Assche #define SG_LIB_CAT_STANDBY 12 /* sense key: not ready, special case 509*44704f69SBart Van Assche * [sk,asc, ascq: 0x2, 0x4, 0xb] */ 510*44704f69SBart Van Assche #define SG_LIB_CAT_UNAVAILABLE 13 /* sense key: not ready, special case 511*44704f69SBart Van Assche * [sk,asc, ascq: 0x2, 0x4, 0xc] */ 512*44704f69SBart Van Assche #define SG_LIB_CAT_MISCOMPARE 14 /* sense key, probably verify 513*44704f69SBart Van Assche * [sk,asc,ascq: 0xe,*,*] */ 514*44704f69SBart Van Assche #define SG_LIB_FILE_ERROR 15 /* device or other file problem */ 515*44704f69SBart Van Assche /* for 17 and 18, see below */ 516*44704f69SBart Van Assche #define SG_LIB_CAT_NO_SENSE 20 /* sense data with key of "no sense" 517*44704f69SBart Van Assche * [sk,asc,ascq: 0x0,*,*] */ 518*44704f69SBart Van Assche #define SG_LIB_CAT_RECOVERED 21 /* Successful command after recovered err 519*44704f69SBart Van Assche * [sk,asc,ascq: 0x1,*,*] */ 520*44704f69SBart Van Assche #define SG_LIB_LBA_OUT_OF_RANGE 22 /* Illegal request, LBA Out Of Range 521*44704f69SBart Van Assche * [sk,asc,ascq: 0x5,0x21,0x0] */ 522*44704f69SBart Van Assche #define SG_LIB_CAT_RES_CONFLICT SAM_STAT_RESERVATION_CONFLICT 523*44704f69SBart Van Assche /* 24: this is a SCSI status, not sense. 524*44704f69SBart Van Assche * It indicates reservation by another 525*44704f69SBart Van Assche * machine blocks this command */ 526*44704f69SBart Van Assche #define SG_LIB_CAT_CONDITION_MET 25 /* SCSI status, not sense key. 527*44704f69SBart Van Assche * Only from PRE-FETCH (SBC-4) */ 528*44704f69SBart Van Assche #define SG_LIB_CAT_BUSY 26 /* SCSI status, not sense. Invites retry */ 529*44704f69SBart Van Assche #define SG_LIB_CAT_TS_FULL 27 /* SCSI status, not sense. Wait then retry */ 530*44704f69SBart Van Assche #define SG_LIB_CAT_ACA_ACTIVE 28 /* SCSI status; ACA seldom used */ 531*44704f69SBart Van Assche #define SG_LIB_CAT_TASK_ABORTED 29 /* SCSI status, this command aborted by? */ 532*44704f69SBart Van Assche #define SG_LIB_CONTRADICT 31 /* error involving two or more cl options */ 533*44704f69SBart Van Assche #define SG_LIB_LOGIC_ERROR 32 /* unexpected situation in code */ 534*44704f69SBart Van Assche /* for 33 see SG_LIB_CAT_TIMEOUT below */ 535*44704f69SBart Van Assche #define SG_LIB_WINDOWS_ERR 34 /* Windows error number don't fit in 7 bits so 536*44704f69SBart Van Assche * map to a single value for exit statuses */ 537*44704f69SBart Van Assche #define SG_LIB_TRANSPORT_ERROR 35 /* driver or interconnect */ 538*44704f69SBart Van Assche #define SG_LIB_OK_FALSE 36 /* no error, reporting false (cf. no error, 539*44704f69SBart Van Assche * reporting true is SG_LIB_OK_TRUE(0) ) */ 540*44704f69SBart Van Assche #define SG_LIB_CAT_PROTECTION 40 /* subset of aborted command (for PI, DIF) 541*44704f69SBart Van Assche * [sk,asc,ascq: 0xb,0x10,*] */ 542*44704f69SBart Van Assche /* 47: flock error used in ddpt utility */ 543*44704f69SBart Van Assche #define SG_LIB_NVME_STATUS 48 /* NVMe Status Field (SF) other than 0 */ 544*44704f69SBart Van Assche #define SG_LIB_WILD_RESID 49 /* Residual value for data-in transfer of a 545*44704f69SBart Van Assche * SCSI command is nonsensical */ 546*44704f69SBart Van Assche #define SG_LIB_OS_BASE_ERR 50 /* in Linux: values found in: 547*44704f69SBart Van Assche * include/uapi/asm-generic/errno-base.h 548*44704f69SBart Van Assche * Example: ENOMEM reported as 62 (=50+12) 549*44704f69SBart Van Assche * if errno > 46 then use this value */ 550*44704f69SBart Van Assche /* 51-->96 set aside for Unix errno values shifted by SG_LIB_OS_BASE_ERR */ 551*44704f69SBart Van Assche #define SG_LIB_CAT_MALFORMED 97 /* Response to SCSI command malformed */ 552*44704f69SBart Van Assche #define SG_LIB_CAT_SENSE 98 /* Something else is in the sense buffer */ 553*44704f69SBart Van Assche #define SG_LIB_CAT_OTHER 99 /* Some other error/warning has occurred 554*44704f69SBart Van Assche * (e.g. a transport or driver error) */ 555*44704f69SBart Van Assche /* 100 to 120 (inclusive) used by ddpt utility */ 556*44704f69SBart Van Assche #define SG_LIB_UNUSED_ABOVE 120 /* Put extra errors in holes below this */ 557*44704f69SBart Van Assche 558*44704f69SBart Van Assche /* Returns a SG_LIB_CAT_* value. If cannot decode sense_buffer or a less 559*44704f69SBart Van Assche * common sense key then return SG_LIB_CAT_SENSE .*/ 560*44704f69SBart Van Assche int sg_err_category_sense(const uint8_t * sense_buffer, int sb_len); 561*44704f69SBart Van Assche 562*44704f69SBart Van Assche /* Here are some additional sense data categories that are not returned 563*44704f69SBart Van Assche * by sg_err_category_sense() but are returned by some related functions. */ 564*44704f69SBart Van Assche #define SG_LIB_CAT_ILLEGAL_REQ_WITH_INFO 17 /* Illegal request (other than */ 565*44704f69SBart Van Assche /* invalid opcode) plus 'info' field: */ 566*44704f69SBart Van Assche /* [sk,asc,ascq: 0x5,*,*] */ 567*44704f69SBart Van Assche #define SG_LIB_CAT_MEDIUM_HARD_WITH_INFO 18 /* medium or hardware error */ 568*44704f69SBart Van Assche /* sense key plus 'info' field: */ 569*44704f69SBart Van Assche /* [sk,asc,ascq: 0x3/0x4,*,*] */ 570*44704f69SBart Van Assche #define SG_LIB_CAT_TIMEOUT 33 /* SCSI command timeout */ 571*44704f69SBart Van Assche #define SG_LIB_CAT_PROTECTION_WITH_INFO 41 /* aborted command sense key, */ 572*44704f69SBart Van Assche /* protection plus 'info' field: */ 573*44704f69SBart Van Assche /* [sk,asc,ascq: 0xb,0x10,*] */ 574*44704f69SBart Van Assche 575*44704f69SBart Van Assche /* Yield string associated with sense category. Returns 'buff' (or pointer 576*44704f69SBart Van Assche * to "Bad sense category" if 'buff' is NULL). If sense_cat unknown then 577*44704f69SBart Van Assche * yield "Sense category: <sense_cat)val>" string. The original 'sense 578*44704f69SBart Van Assche * category' concept has been expanded to most detected errors and is 579*44704f69SBart Van Assche * returned by these utilities as their exit status value (an (unsigned) 580*44704f69SBart Van Assche * 8 bit value where 0 means good (i.e. no errors)). Uses the 581*44704f69SBart Van Assche * sg_exit2str() function. */ 582*44704f69SBart Van Assche const char * sg_get_category_sense_str(int sense_cat, int buff_len, 583*44704f69SBart Van Assche char * buff, int verbose); 584*44704f69SBart Van Assche 585*44704f69SBart Van Assche 586*44704f69SBart Van Assche /* Iterates to next designation descriptor in the device identification 587*44704f69SBart Van Assche * VPD page. The 'initial_desig_desc' should point to start of first 588*44704f69SBart Van Assche * descriptor with 'page_len' being the number of valid bytes in that 589*44704f69SBart Van Assche * and following descriptors. To start, 'off' should point to a negative 590*44704f69SBart Van Assche * value, thereafter it should point to the value yielded by the previous 591*44704f69SBart Van Assche * call. If 0 returned then 'initial_desig_desc + *off' should be a valid 592*44704f69SBart Van Assche * descriptor; returns -1 if normal end condition and -2 for an abnormal 593*44704f69SBart Van Assche * termination. Matches association, designator_type and/or code_set when 594*44704f69SBart Van Assche * any of those values are greater than or equal to zero. */ 595*44704f69SBart Van Assche int sg_vpd_dev_id_iter(const uint8_t * initial_desig_desc, int page_len, 596*44704f69SBart Van Assche int * off, int m_assoc, int m_desig_type, 597*44704f69SBart Van Assche int m_code_set); 598*44704f69SBart Van Assche 599*44704f69SBart Van Assche 600*44704f69SBart Van Assche /* <<< General purpose (i.e. not SCSI specific) utility functions >>> */ 601*44704f69SBart Van Assche 602*44704f69SBart Van Assche /* Always returns valid string even if errnum is wild (or library problem). 603*44704f69SBart Van Assche * If errnum is negative, flip its sign. */ 604*44704f69SBart Van Assche char * safe_strerror(int errnum); 605*44704f69SBart Van Assche 606*44704f69SBart Van Assche /* Not all platforms support the Unix sleep(seconds) function. */ 607*44704f69SBart Van Assche void sg_sleep_secs(int num_secs); 608*44704f69SBart Van Assche 609*44704f69SBart Van Assche /* There are several SCSI commands that are very destructive for the user 610*44704f69SBart Van Assche * data stored on a device. The FORMAT UNIT command is the prime example 611*44704f69SBart Van Assche * but there are an increasing number of newer SCSI commands that remove or 612*44704f69SBart Van Assche * destroy some or all of the user's data. This function takes 15 seconds, 613*44704f69SBart Van Assche * divided into three parts, saying that 'cmd_name' will be executed on 614*44704f69SBart Van Assche * 'dev_name' and then waits for 5 seconds inviting the user to press 615*44704f69SBart Van Assche * control-C to abort the operation. After three such prompts the function 616*44704f69SBart Van Assche * returns and the utility start to execute the "dangerous" SCSI command, 617*44704f69SBart Van Assche * Utilities that use this function usually have a --quick option to bypass 618*44704f69SBart Van Assche * this call. That may be appropriate if the utility in question is called 619*44704f69SBart Van Assche * from a script or in background processing. If 'stress_all' is true then 620*44704f69SBart Van Assche * state "ALL data" will be lost, if false drop the "ALL". */ 621*44704f69SBart Van Assche void 622*44704f69SBart Van Assche sg_warn_and_wait(const char * cmd_name, const char * dev_name, 623*44704f69SBart Van Assche bool stress_all); 624*44704f69SBart Van Assche 625*44704f69SBart Van Assche 626*44704f69SBart Van Assche /* Print (to stdout) 'str' of bytes in hex, 16 bytes per line optionally 627*44704f69SBart Van Assche * followed at the right hand side of the line with an ASCII interpretation. 628*44704f69SBart Van Assche * Each line is prefixed with an address, starting at 0 for str[0]..str[15]. 629*44704f69SBart Van Assche * All output numbers are in hex. 630*44704f69SBart Van Assche * 'no_ascii' selects on of 3 output format types: 631*44704f69SBart Van Assche * > 0 each line has address then up to 16 ASCII-hex bytes 632*44704f69SBart Van Assche * = 0 in addition, the bytes are listed in ASCII to the right 633*44704f69SBart Van Assche * < 0 only the ASCII-hex bytes are listed (i.e. without address) 634*44704f69SBart Van Assche */ 635*44704f69SBart Van Assche void dStrHex(const char * str, int len, int no_ascii); 636*44704f69SBart Van Assche 637*44704f69SBart Van Assche /* Print (to sg_warnings_strm (stderr)) 'str' of bytes in hex, 16 bytes per 638*44704f69SBart Van Assche * line optionally followed at right by its ASCII interpretation. Same 639*44704f69SBart Van Assche * logic as dStrHex() with different output stream (i.e. stderr). */ 640*44704f69SBart Van Assche void dStrHexErr(const char * str, int len, int no_ascii); 641*44704f69SBart Van Assche 642*44704f69SBart Van Assche /* Read binary starting at 'str' for 'len' bytes and output as ASCII 643*44704f69SBart Van Assche * hexadecimal into file pointer (fp). 16 bytes per line are output with an 644*44704f69SBart Van Assche * additional space between 8th and 9th byte on each line (for readability). 645*44704f69SBart Van Assche * 'no_ascii' selects one of 3 output format types as shown in dStrHex() . */ 646*44704f69SBart Van Assche void dStrHexFp(const char* str, int len, int no_ascii, FILE * fp); 647*44704f69SBart Van Assche 648*44704f69SBart Van Assche /* Read 'len' bytes from 'str' and output as ASCII-Hex bytes (space separated) 649*44704f69SBart Van Assche * to 'b' not to exceed 'b_len' characters. Each line starts with 'leadin' 650*44704f69SBart Van Assche * (NULL for no leadin) and there are 16 bytes per line with an extra space 651*44704f69SBart Van Assche * between the 8th and 9th bytes. 'oformat' is 0 for repeat in printable ASCII 652*44704f69SBart Van Assche * ('.' for non printable chars) to right of each line; 1 don't (so just 653*44704f69SBart Van Assche * output ASCII hex). If 'oformat' is 2 output same as 1 but any LFs are 654*44704f69SBart Van Assche * replaced by space (and trailing spaces are trimmed). Note that an address 655*44704f69SBart Van Assche * is _not_ printed on each line preceding the hex data. Returns number of 656*44704f69SBart Van Assche * bytes written to 'b' excluding the trailing '\0'. The only difference 657*44704f69SBart Van Assche * between dStrHexStr() and hex2str() is the type of the first argument. */ 658*44704f69SBart Van Assche int dStrHexStr(const char * str, int len, const char * leadin, int oformat, 659*44704f69SBart Van Assche int cb_len, char * cbp); 660*44704f69SBart Van Assche int hex2str(const uint8_t * b_str, int len, const char * leadin, int oformat, 661*44704f69SBart Van Assche int cb_len, char * cbp); 662*44704f69SBart Van Assche 663*44704f69SBart Van Assche /* Similar to hex2str() but outputs to file pointed to be fp */ 664*44704f69SBart Van Assche void hex2fp(const uint8_t * b_str, int len, const char * leadin, int oformat, 665*44704f69SBart Van Assche FILE * fp); 666*44704f69SBart Van Assche 667*44704f69SBart Van Assche /* The following 2 functions are equivalent to dStrHex() and dStrHexErr() 668*44704f69SBart Van Assche * respectively. The difference is only the type of the first of argument: 669*44704f69SBart Van Assche * uint8_t instead of char. The name of the argument is changed to b_str to 670*44704f69SBart Van Assche * stress it is a pointer to the start of a binary string. */ 671*44704f69SBart Van Assche void hex2stdout(const uint8_t * b_str, int len, int no_ascii); 672*44704f69SBart Van Assche void hex2stderr(const uint8_t * b_str, int len, int no_ascii); 673*44704f69SBart Van Assche 674*44704f69SBart Van Assche /* Read ASCII hex bytes or binary from fname (a file named '-' taken as 675*44704f69SBart Van Assche * stdin). If reading ASCII hex then there should be either one entry per 676*44704f69SBart Van Assche * line or a comma, space, hyphen or tab separated list of bytes. If no_space 677*44704f69SBart Van Assche * is set then a string of ACSII hex digits is expected, 2 per byte. 678*44704f69SBart Van Assche * Everything from and including a '#' on a line is ignored. Returns 0 if ok, 679*44704f69SBart Van Assche * or an error code. If the error code is SG_LIB_LBA_OUT_OF_RANGE then mp_arr 680*44704f69SBart Van Assche * would be exceeded and both mp_arr and mp_arr_len are written to. 681*44704f69SBart Van Assche * The max_arr_len_and argument may carry extra information: when it is 682*44704f69SBart Van Assche * negative its absolute value is used for the maximum number of bytes to 683*44704f69SBart Van Assche * write to mp_arr _and_ the first hexadecimal value on each line is skipped. 684*44704f69SBart Van Assche * Many hexadecimal output programs place a running address (index) as the 685*44704f69SBart Van Assche * first field on each line. When as_binary and/or no_space are true, the 686*44704f69SBart Van Assche * absolute value of max_arr_len_and is used. */ 687*44704f69SBart Van Assche int sg_f2hex_arr(const char * fname, bool as_binary, bool no_space, 688*44704f69SBart Van Assche uint8_t * mp_arr, int * mp_arr_len, int max_arr_len_and); 689*44704f69SBart Van Assche 690*44704f69SBart Van Assche /* Returns true when executed on big endian machine; else returns false. 691*44704f69SBart Van Assche * Useful for displaying ATA identify words (which need swapping on a 692*44704f69SBart Van Assche * big endian machine). */ 693*44704f69SBart Van Assche bool sg_is_big_endian(); 694*44704f69SBart Van Assche 695*44704f69SBart Van Assche /* Returns true if byte sequence starting at bp with a length of b_len is 696*44704f69SBart Van Assche * all zeros (for sg_all_zeros()) or all 0xff_s (for sg_all_ffs()); 697*44704f69SBart Van Assche * otherwise returns false. If bp is NULL or b_len <= 0 returns false. */ 698*44704f69SBart Van Assche bool sg_all_zeros(const uint8_t * bp, int b_len); 699*44704f69SBart Van Assche bool sg_all_ffs(const uint8_t * bp, int b_len); 700*44704f69SBart Van Assche 701*44704f69SBart Van Assche /* Extract character sequence from ATA words as in the model string 702*44704f69SBart Van Assche * in a IDENTIFY DEVICE response. Returns number of characters 703*44704f69SBart Van Assche * written to 'ochars' before 0 character is found or 'num' words 704*44704f69SBart Van Assche * are processed. */ 705*44704f69SBart Van Assche int sg_ata_get_chars(const uint16_t * word_arr, int start_word, 706*44704f69SBart Van Assche int num_words, bool is_big_endian, char * ochars); 707*44704f69SBart Van Assche 708*44704f69SBart Van Assche /* Print (to stdout) 16 bit 'words' in hex, 8 words per line optionally 709*44704f69SBart Van Assche * followed at the right hand side of the line with an ASCII interpretation 710*44704f69SBart Van Assche * (pairs of ASCII characters in big endian order (upper first)). 711*44704f69SBart Van Assche * Each line is prefixed with an address, starting at 0. 712*44704f69SBart Van Assche * All output numbers are in hex. 'no_ascii' allows for 3 output types: 713*44704f69SBart Van Assche * > 0 each line has address then up to 8 ASCII-hex words 714*44704f69SBart Van Assche * = 0 in addition, the words are listed in ASCII pairs to the right 715*44704f69SBart Van Assche * = -1 only the ASCII-hex words are listed (i.e. without address) 716*44704f69SBart Van Assche * = -2 only the ASCII-hex words, formatted for "hdparm --Istdin" 717*44704f69SBart Van Assche * < -2 same as -1 718*44704f69SBart Van Assche * If 'swapb' is true then bytes in each word swapped. Needs to be set 719*44704f69SBart Van Assche * for ATA IDENTIFY DEVICE response on big-endian machines. 720*44704f69SBart Van Assche */ 721*44704f69SBart Van Assche void dWordHex(const uint16_t * words, int num, int no_ascii, bool swapb); 722*44704f69SBart Van Assche 723*44704f69SBart Van Assche /* If the number in 'buf' can not be decoded or the multiplier is unknown 724*44704f69SBart Van Assche * then -1 is returned. Accepts a hex prefix (0x or 0X) or a decimal 725*44704f69SBart Van Assche * multiplier suffix (as per GNU's dd (since 2002: SI and IEC 60027-2)). 726*44704f69SBart Van Assche * Main (SI) multipliers supported: K, M, G. Ignore leading spaces and 727*44704f69SBart Van Assche * tabs; accept comma, hyphen, space, tab and hash as terminator. 728*44704f69SBart Van Assche * Handles zero and positive values up to 2**31-1 . 729*44704f69SBart Van Assche * Experimental: left argument (must in with hexadecimal digit) added 730*44704f69SBart Van Assche * to, or multiplied, by right argument. No embedded spaces. 731*44704f69SBart Van Assche * Examples: '3+1k' (evaluates to 1027) and '0xf+0x3'. */ 732*44704f69SBart Van Assche int sg_get_num(const char * buf); 733*44704f69SBart Van Assche 734*44704f69SBart Van Assche /* If the number in 'buf' can not be decoded then -1 is returned. Accepts a 735*44704f69SBart Van Assche * hex prefix (0x or 0X) or a 'h' (or 'H') suffix; otherwise decimal is 736*44704f69SBart Van Assche * assumed. Does not accept multipliers. Accept a comma (","), hyphen ("-"), 737*44704f69SBart Van Assche * a whitespace or newline as terminator. Only decimal numbers can represent 738*44704f69SBart Van Assche * negative numbers and '-1' must be treated separately. */ 739*44704f69SBart Van Assche int sg_get_num_nomult(const char * buf); 740*44704f69SBart Van Assche 741*44704f69SBart Van Assche /* If the number in 'buf' can not be decoded or the multiplier is unknown 742*44704f69SBart Van Assche * then -1LL is returned. Accepts a hex prefix (0x or 0X), hex suffix 743*44704f69SBart Van Assche * (h or H), or a decimal multiplier suffix (as per GNU's dd (since 2002: 744*44704f69SBart Van Assche * SI and IEC 60027-2)). Main (SI) multipliers supported: K, M, G, T, P 745*44704f69SBart Van Assche * and E. Ignore leading spaces and tabs; accept comma, hyphen, space, tab 746*44704f69SBart Van Assche * and hash as terminator. Handles zero and positive values up to 2**63-1 . 747*44704f69SBart Van Assche * Experimental: the left argument (must end in with hexadecimal digit) 748*44704f69SBart Van Assche * added to, or multiplied by, the right argument. No embedded spaces. 749*44704f69SBart Van Assche * Examples: '3+1k' (evaluates to 1027) and '0xf+0x3'. */ 750*44704f69SBart Van Assche int64_t sg_get_llnum(const char * buf); 751*44704f69SBart Van Assche 752*44704f69SBart Van Assche /* If the number in 'buf' can not be decoded then -1 is returned. Accepts a 753*44704f69SBart Van Assche * hex prefix (0x or 0X) or a 'h' (or 'H') suffix; otherwise decimal is 754*44704f69SBart Van Assche * assumed. Does not accept multipliers. Accept a comma (","), hyphen ("-"), 755*44704f69SBart Van Assche * a whitespace or newline as terminator. Only decimal numbers can represent 756*44704f69SBart Van Assche * negative numbers and '-1' must be treated separately. */ 757*44704f69SBart Van Assche int64_t sg_get_llnum_nomult(const char * buf); 758*44704f69SBart Van Assche 759*44704f69SBart Van Assche /* Returns pointer to heap (or NULL) that is aligned to a align_to byte 760*44704f69SBart Van Assche * boundary. Sends back *buff_to_free pointer in third argument that may be 761*44704f69SBart Van Assche * different from the return value. If it is different then the *buff_to_free 762*44704f69SBart Van Assche * pointer should be freed (rather than the returned value) when the heap is 763*44704f69SBart Van Assche * no longer needed. If align_to is 0 then aligns to OS's page size. Sets all 764*44704f69SBart Van Assche * returned heap to zeros. If num_bytes is 0 then set to page size. */ 765*44704f69SBart Van Assche uint8_t * sg_memalign(uint32_t num_bytes, uint32_t align_to, 766*44704f69SBart Van Assche uint8_t ** buff_to_free, bool vb); 767*44704f69SBart Van Assche 768*44704f69SBart Van Assche /* Returns OS page size in bytes. If uncertain returns 4096. */ 769*44704f69SBart Van Assche uint32_t sg_get_page_size(void); 770*44704f69SBart Van Assche 771*44704f69SBart Van Assche /* If byte_count is 0 or less then the OS page size is used as denominator. 772*44704f69SBart Van Assche * Returns true if the remainder of ((unsigned)pointer % byte_count) is 0, 773*44704f69SBart Van Assche * else returns false. */ 774*44704f69SBart Van Assche bool sg_is_aligned(const void * pointer, int byte_count); 775*44704f69SBart Van Assche 776*44704f69SBart Van Assche /* Does similar job to sg_get_unaligned_be*() but this function starts at 777*44704f69SBart Van Assche * a given start_bit (i.e. within byte, so 7 is MSbit of byte and 0 is LSbit) 778*44704f69SBart Van Assche * offset. Maximum number of num_bits is 64. For example, these two 779*44704f69SBart Van Assche * invocations are equivalent (and should yield the same result); 780*44704f69SBart Van Assche * sg_get_big_endian(from_bp, 7, 16) 781*44704f69SBart Van Assche * sg_get_unaligned_be16(from_bp) */ 782*44704f69SBart Van Assche uint64_t sg_get_big_endian(const uint8_t * from_bp, 783*44704f69SBart Van Assche int start_bit /* 0 to 7 */, 784*44704f69SBart Van Assche int num_bits /* 1 to 64 */); 785*44704f69SBart Van Assche 786*44704f69SBart Van Assche /* Does similar job to sg_put_unaligned_be*() but this function starts at 787*44704f69SBart Van Assche * a given start_bit offset. Maximum number of num_bits is 64. Preserves 788*44704f69SBart Van Assche * residual bits in partially written bytes. start_bit 7 is MSb. */ 789*44704f69SBart Van Assche void sg_set_big_endian(uint64_t val, uint8_t * to, int start_bit /* 0 to 7 */, 790*44704f69SBart Van Assche int num_bits /* 1 to 64 */); 791*44704f69SBart Van Assche 792*44704f69SBart Van Assche /* If os_err_num is within bounds then the returned value is 'os_err_num + 793*44704f69SBart Van Assche * SG_LIB_OS_BASE_ERR' otherwise SG_LIB_OS_BASE_ERR is returned. If 794*44704f69SBart Van Assche * os_err_num is 0 then 0 is returned. */ 795*44704f69SBart Van Assche int sg_convert_errno(int os_err_num); 796*44704f69SBart Van Assche 797*44704f69SBart Van Assche 798*44704f69SBart Van Assche /* <<< Architectural support functions [is there a better place?] >>> */ 799*44704f69SBart Van Assche 800*44704f69SBart Van Assche /* Non Unix OSes distinguish between text and binary files. 801*44704f69SBart Van Assche * Set text mode on fd. Does nothing in Unix. Returns negative number on 802*44704f69SBart Van Assche * failure. */ 803*44704f69SBart Van Assche int sg_set_text_mode(int fd); 804*44704f69SBart Van Assche 805*44704f69SBart Van Assche /* Set binary mode on fd. Does nothing in Unix. Returns negative number on 806*44704f69SBart Van Assche * failure. */ 807*44704f69SBart Van Assche int sg_set_binary_mode(int fd); 808*44704f69SBart Van Assche 809*44704f69SBart Van Assche #ifdef __cplusplus 810*44704f69SBart Van Assche } 811*44704f69SBart Van Assche #endif 812*44704f69SBart Van Assche 813*44704f69SBart Van Assche #endif /* SG_LIB_H */ 814