Debian Codegraph @

src/udev/scsi_id/scsi_serial.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright © IBM Corp. 2003
4 *
5 * Author: Patrick Mansfield<[email protected]>
6 */
7
8 #include <fcntl.h>
9 #include <linux/bsg.h>
10 #include <scsi/scsi.h>
11 #include <scsi/sg.h>
12 #include <stdio.h>
13 #include <sys/ioctl.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16
17 #include "devnum-util.h"
18 #include "fd-util.h"
19 #include "hexdecoct.h"
20 #include "log.h"
21 #include "random-util.h"
22 #include "scsi.h"
23 #include "scsi_id.h"
24 #include "string-util.h"
25 #include "strxcpyx.h"
26 #include "time-util.h"
27
28 /*
29 * A priority based list of id, naa, and binary/ascii for the identifier
30 * descriptor in VPD page 0x83.
31 *
32 * Brute force search for a match starting with the first value in the
33 * following id_search_list. This is not a performance issue, since there
34 * is normally one or some small number of descriptors.
35 */
36 static const struct scsi_id_search_values id_search_list[] = {
37 { SCSI_ID_TGTGROUP, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
38 { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG_EXTENDED, SCSI_ID_BINARY },
39 { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG_EXTENDED, SCSI_ID_ASCII },
40 { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG, SCSI_ID_BINARY },
41 { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG, SCSI_ID_ASCII },
42 /*
43 * Devices already exist using NAA values that are now marked
44 * reserved. These should not conflict with other values, or it is
45 * a bug in the device. As long as we find the IEEE extended one
46 * first, we really don't care what other ones are used. Using
47 * don't care here means that a device that returns multiple
48 * non-IEEE descriptors in a random order will get different
49 * names.
50 */
51 { SCSI_ID_NAA, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
52 { SCSI_ID_NAA, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII },
53 { SCSI_ID_EUI_64, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
54 { SCSI_ID_EUI_64, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII },
55 { SCSI_ID_T10_VENDOR, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
56 { SCSI_ID_T10_VENDOR, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII },
57 { SCSI_ID_VENDOR_SPECIFIC, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY },
58 { SCSI_ID_VENDOR_SPECIFIC, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII },
59 };
60
61 /*
62 * Values returned in the result/status, only the ones used by the code
63 * are used here.
64 */
65
66 #define DID_NO_CONNECT 0x01 /* Unable to connect before timeout */
67 #define DID_BUS_BUSY 0x02 /* Bus remain busy until timeout */
68 #define DID_TIME_OUT 0x03 /* Timed out for some other reason */
69 #define DID_TRANSPORT_DISRUPTED 0x0e /* Transport disrupted and should retry */
70 #define DRIVER_TIMEOUT 0x06
71 #define DRIVER_SENSE 0x08 /* Sense_buffer has been set */
72
73 /* The following "category" function returns one of the following */
74 #define SG_ERR_CAT_CLEAN 0 /* No errors or other information */
75 #define SG_ERR_CAT_MEDIA_CHANGED 1 /* interpreted from sense buffer */
76 #define SG_ERR_CAT_RESET 2 /* interpreted from sense buffer */
77 #define SG_ERR_CAT_TIMEOUT 3
78 #define SG_ERR_CAT_RECOVERED 4 /* Successful command after recovered err */
79 #define SG_ERR_CAT_NOTSUPPORTED 5 /* Illegal / unsupported command */
80 #define SG_ERR_CAT_RETRY 6 /* Command should be retried */
81 #define SG_ERR_CAT_SENSE 98 /* Something else in the sense buffer */
82 #define SG_ERR_CAT_OTHER 99 /* Some other error/warning */
83
84 static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd,
85 char *serial, char *serial_short, size_t max_len);
86
87 static int sg_err_category_new(int scsi_status, int msg_status, int
88 host_status, int driver_status, const
89 unsigned char *sense_buffer, int sb_len) {
90 scsi_status &= 0x7e;
91
92 /*
93 * XXX change to return only two values - failed or OK.
94 */
95
96 if (!scsi_status && !host_status && !driver_status)
97 return SG_ERR_CAT_CLEAN;
98
99 if (IN_SET(scsi_status, SCSI_CHECK_CONDITION, SCSI_COMMAND_TERMINATED) ||
100 (driver_status & 0xf) == DRIVER_SENSE) {
101 if (sense_buffer && (sb_len > 2)) {
102 int sense_key;
103 unsigned char asc;
104
105 if (sense_buffer[0] & 0x2) {
106 sense_key = sense_buffer[1] & 0xf;
107 asc = sense_buffer[2];
108 } else {
109 sense_key = sense_buffer[2] & 0xf;
110 asc = (sb_len > 12) ? sense_buffer[12] : 0;
111 }
112
113 if (sense_key == RECOVERED_ERROR)
114 return SG_ERR_CAT_RECOVERED;
115 else if (sense_key == UNIT_ATTENTION) {
116 if (0x28 == asc)
117 return SG_ERR_CAT_MEDIA_CHANGED;
118 if (0x29 == asc)
119 return SG_ERR_CAT_RESET;
120 } else if (sense_key == ILLEGAL_REQUEST)
121 return SG_ERR_CAT_NOTSUPPORTED;
122 }
123 return SG_ERR_CAT_SENSE;
124 }
125 if (host_status) {
126 if (IN_SET(host_status, DID_NO_CONNECT, DID_BUS_BUSY, DID_TIME_OUT))
127 return SG_ERR_CAT_TIMEOUT;
128 if (host_status == DID_TRANSPORT_DISRUPTED)
129 return SG_ERR_CAT_RETRY;
130 }
131 if (driver_status) {
132 if (driver_status == DRIVER_TIMEOUT)
133 return SG_ERR_CAT_TIMEOUT;
134 }
135 return SG_ERR_CAT_OTHER;
136 }
137
138 static int sg_err_category3(struct sg_io_hdr *hp) {
139 return sg_err_category_new(hp->status, hp->msg_status,
140 hp->host_status, hp->driver_status,
141 hp->sbp, hp->sb_len_wr);
142 }
143
144 static int sg_err_category4(struct sg_io_v4 *hp) {
145 return sg_err_category_new(hp->device_status, 0,
146 hp->transport_status, hp->driver_status,
147 (unsigned char *)(uintptr_t)hp->response,
148 hp->response_len);
149 }
150
151 static int scsi_dump_sense(struct scsi_id_device *dev_scsi,
152 unsigned char *sense_buffer, int sb_len) {
153 int s;
154 unsigned code, sense_class, sense_key, asc, ascq;
155
156 /*
157 * Figure out and print the sense key, asc and ascq.
158 *
159 * If you want to suppress these for a particular drive model, add
160 * a deny list entry in the scsi_id config file.
161 *
162 * XXX We probably need to: lookup the sense/asc/ascq in a retry
163 * table, and if found return 1 (after dumping the sense, asc, and
164 * ascq). So, if/when we get something like a power on/reset,
165 * we'll retry the command.
166 */
167
168 if (sb_len < 1)
169 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
170 "%s: sense buffer empty",
171 dev_scsi->kernel);
172
173 sense_class = (sense_buffer[0] >> 4) & 0x07;
174 code = sense_buffer[0] & 0xf;
175
176 if (sense_class == 7) {
177 /*
178 * extended sense data.
179 */
180 s = sense_buffer[7] + 8;
181 if (sb_len < s)
182 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
183 "%s: sense buffer too small %d bytes, %d bytes too short",
184 dev_scsi->kernel, sb_len,
185 s - sb_len);
186
187 if (IN_SET(code, 0x0, 0x1)) {
188 sense_key = sense_buffer[2] & 0xf;
189 if (s < 14)
190 /*
191 * Possible?
192 */
193 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
194 "%s: sense result too small %d bytes",
195 dev_scsi->kernel, s);
196
197 asc = sense_buffer[12];
198 ascq = sense_buffer[13];
199 } else if (IN_SET(code, 0x2, 0x3)) {
200 sense_key = sense_buffer[1] & 0xf;
201 asc = sense_buffer[2];
202 ascq = sense_buffer[3];
203 } else
204 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
205 "%s: invalid sense code 0x%x",
206 dev_scsi->kernel, code);
207
208 log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x",
209 dev_scsi->kernel, sense_key, asc, ascq);
210 } else {
211 if (sb_len < 4)
212 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
213 "%s: sense buffer too small %d bytes, %d bytes too short",
214 dev_scsi->kernel, sb_len,
215 4 - sb_len);
216
217 if (sense_buffer[0] < 15)
218 log_debug("%s: old sense key: 0x%x", dev_scsi->kernel, sense_buffer[0] & 0x0fu);
219 else
220 log_debug("%s: sense = %2x %2x",
221 dev_scsi->kernel, sense_buffer[0], sense_buffer[2]);
222 log_debug("%s: non-extended sense class %u code 0x%0x",
223 dev_scsi->kernel, sense_class, code);
224
225 }
226
227 return -1;
228 }
229
230 static int scsi_dump(struct scsi_id_device *dev_scsi, struct sg_io_hdr *io) {
231 if (!io->status && !io->host_status && !io->msg_status &&
232 !io->driver_status)
233 /*
234 * Impossible, should not be called.
235 */
236 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
237 "%s: called with no error",
238 __func__);
239
240 log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x",
241 dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status);
242 if (io->status == SCSI_CHECK_CONDITION)
243 return scsi_dump_sense(dev_scsi, io->sbp, io->sb_len_wr);
244 else
245 return -1;
246 }
247
248 static int scsi_dump_v4(struct scsi_id_device *dev_scsi, struct sg_io_v4 *io) {
249 if (!io->device_status && !io->transport_status &&
250 !io->driver_status)
251 /*
252 * Impossible, should not be called.
253 */
254 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
255 "%s: called with no error",
256 __func__);
257
258 log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x",
259 dev_scsi->kernel, io->driver_status, io->transport_status, io->device_status);
260 if (io->device_status == SCSI_CHECK_CONDITION)
261 return scsi_dump_sense(dev_scsi, (unsigned char *)(uintptr_t)io->response,
262 io->response_len);
263 else
264 return -1;
265 }
266
267 static int scsi_inquiry(struct scsi_id_device *dev_scsi, int fd,
268 unsigned char evpd, unsigned char page,
269 unsigned char *buf, unsigned buflen) {
270 unsigned char inq_cmd[INQUIRY_CMDLEN] =
271 { INQUIRY_CMD, evpd, page, 0, buflen, 0 };
272 unsigned char sense[SENSE_BUFF_LEN];
273 void *io_buf;
274 struct sg_io_v4 io_v4;
275 struct sg_io_hdr io_hdr;
276 int retry = 3; /* rather random */
277 int retval;
278
279 if (buflen > SCSI_INQ_BUFF_LEN)
280 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
281 "buflen %u too long", buflen);
282
283 resend:
284 if (dev_scsi->use_sg == 4) {
285 memzero(&io_v4, sizeof(struct sg_io_v4));
286 io_v4.guard = 'Q';
287 io_v4.protocol = BSG_PROTOCOL_SCSI;
288 io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD;
289 io_v4.request_len = sizeof(inq_cmd);
290 io_v4.request = (uintptr_t)inq_cmd;
291 io_v4.max_response_len = sizeof(sense);
292 io_v4.response = (uintptr_t)sense;
293 io_v4.din_xfer_len = buflen;
294 io_v4.din_xferp = (uintptr_t)buf;
295 io_buf = (void *)&io_v4;
296 } else {
297 memzero(&io_hdr, sizeof(struct sg_io_hdr));
298 io_hdr.interface_id = 'S';
299 io_hdr.cmd_len = sizeof(inq_cmd);
300 io_hdr.mx_sb_len = sizeof(sense);
301 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
302 io_hdr.dxfer_len = buflen;
303 io_hdr.dxferp = buf;
304 io_hdr.cmdp = inq_cmd;
305 io_hdr.sbp = sense;
306 io_hdr.timeout = DEF_TIMEOUT;
307 io_buf = (void *)&io_hdr;
308 }
309
310 retval = ioctl(fd, SG_IO, io_buf);
311 if (retval < 0) {
312 if (IN_SET(errno, EINVAL, ENOSYS) && dev_scsi->use_sg == 4) {
313 dev_scsi->use_sg = 3;
314 goto resend;
315 }
316 log_debug_errno(errno, "%s: ioctl failed: %m", dev_scsi->kernel);
317 goto error;
318 }
319
320 if (dev_scsi->use_sg == 4)
321 retval = sg_err_category4(io_buf);
322 else
323 retval = sg_err_category3(io_buf);
324
325 switch (retval) {
326 case SG_ERR_CAT_NOTSUPPORTED:
327 buf[1] = 0;
328 _fallthrough_;
329 case SG_ERR_CAT_CLEAN:
330 case SG_ERR_CAT_RECOVERED:
331 retval = 0;
332 break;
333 case SG_ERR_CAT_RETRY:
334 break;
335
336 default:
337 if (dev_scsi->use_sg == 4)
338 retval = scsi_dump_v4(dev_scsi, io_buf);
339 else
340 retval = scsi_dump(dev_scsi, io_buf);
341 }
342
343 if (!retval) {
344 retval = buflen;
345 } else if (retval > 0) {
346 if (--retry > 0)
347 goto resend;
348 retval = -1;
349 }
350
351 error:
352 if (retval < 0)
353 log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.",
354 dev_scsi->kernel, evpd, page);
355
356 return retval;
357 }
358
359 /* Get list of supported EVPD pages */
360 static int do_scsi_page0_inquiry(struct scsi_id_device *dev_scsi, int fd,
361 unsigned char *buffer, unsigned len) {
362 int retval;
363
364 memzero(buffer, len);
365 retval = scsi_inquiry(dev_scsi, fd, 1, 0x0, buffer, len);
366 if (retval < 0)
367 return 1;
368
369 if (buffer[1] != 0) {
370 log_debug("%s: page 0 not available.", dev_scsi->kernel);
371 return 1;
372 }
373 if (buffer[3] > len) {
374 log_debug("%s: page 0 buffer too long %d", dev_scsi->kernel, buffer[3]);
375 return 1;
376 }
377
378 /*
379 * Following check is based on code once included in the 2.5.x
380 * kernel.
381 *
382 * Some ill behaved devices return the standard inquiry here
383 * rather than the evpd data, snoop the data to verify.
384 */
385 if (buffer[3] > MODEL_LENGTH) {
386 /*
387 * If the vendor id appears in the page assume the page is
388 * invalid.
389 */
390 if (strneq((char*) buffer + VENDOR_LENGTH, dev_scsi->vendor, VENDOR_LENGTH)) {
391 log_debug("%s: invalid page0 data", dev_scsi->kernel);
392 return 1;
393 }
394 }
395 return 0;
396 }
397
398 static int append_vendor_model(
399 const struct scsi_id_device *dev_scsi,
400 char buf[static VENDOR_LENGTH + MODEL_LENGTH]) {
401
402 assert(dev_scsi);
403 assert(buf);
404
405 if (strnlen(dev_scsi->vendor, VENDOR_LENGTH) != VENDOR_LENGTH)
406 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
407 "%s: bad vendor string \"%s\"",
408 dev_scsi->kernel, dev_scsi->vendor);
409 if (strnlen(dev_scsi->model, MODEL_LENGTH) != MODEL_LENGTH)
410 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
411 "%s: bad model string \"%s\"",
412 dev_scsi->kernel, dev_scsi->model);
413 memcpy(buf, dev_scsi->vendor, VENDOR_LENGTH);
414 memcpy(buf + VENDOR_LENGTH, dev_scsi->model, MODEL_LENGTH);
415 return VENDOR_LENGTH + MODEL_LENGTH;
416 }
417
418 /*
419 * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
420 * serial number.
421 */
422 static int check_fill_0x83_id(struct scsi_id_device *dev_scsi,
423 uint8_t *page_83,
424 const struct scsi_id_search_values
425 *id_search, char *serial, char *serial_short,
426 size_t max_len, char *wwn,
427 char *wwn_vendor_extension, char *tgpt_group) {
428 size_t i, j, s, len;
429
430 /*
431 * ASSOCIATION must be with the device (value 0)
432 * or with the target port for SCSI_ID_TGTPORT
433 */
434 if ((page_83[1] & 0x30) == 0x10) {
435 if (id_search->id_type != SCSI_ID_TGTGROUP)
436 return 1;
437 } else if ((page_83[1] & 0x30) != 0)
438 return 1;
439
440 if ((page_83[1] & 0x0f) != id_search->id_type)
441 return 1;
442
443 /*
444 * Possibly check NAA sub-type.
445 */
446 if ((id_search->naa_type != SCSI_ID_NAA_DONT_CARE) &&
447 (id_search->naa_type != (page_83[4] & 0xf0) >> 4))
448 return 1;
449
450 /*
451 * Check for matching code set - ASCII or BINARY.
452 */
453 if ((page_83[0] & 0x0f) != id_search->code_set)
454 return 1;
455
456 /*
457 * page_83[3]: identifier length
458 */
459 len = page_83[3];
460 if ((page_83[0] & 0x0f) != SCSI_ID_ASCII)
461 /*
462 * If not ASCII, use two bytes for each binary value.
463 */
464 len *= 2;
465
466 /*
467 * Add one byte for the NUL termination, and one for the id_type.
468 */
469 len += 2;
470 if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
471 len += VENDOR_LENGTH + MODEL_LENGTH;
472
473 if (max_len < len) {
474 log_debug("%s: length %zu too short - need %zu",
475 dev_scsi->kernel, max_len, len);
476 return 1;
477 }
478
479 if (id_search->id_type == SCSI_ID_TGTGROUP && tgpt_group != NULL) {
480 unsigned group;
481
482 group = ((unsigned)page_83[6] << 8) | page_83[7];
483 sprintf(tgpt_group,"%x", group);
484 return 1;
485 }
486
487 serial[0] = hexchar(id_search->id_type);
488
489 /*
490 * For SCSI_ID_VENDOR_SPECIFIC prepend the vendor and model before
491 * the id since it is not unique across all vendors and models,
492 * this differs from SCSI_ID_T10_VENDOR, where the vendor is
493 * included in the identifier.
494 */
495 if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC) {
496 if (append_vendor_model(dev_scsi, serial + 1) < 0)
497 return 1;
498 /* append_vendor_model() uses memcpy() without null-terminating.
499 * The buffer was zeroed by the caller, but ensure the string is
500 * explicitly terminated for strlen() below. */
501 serial[1 + VENDOR_LENGTH + MODEL_LENGTH] = '\0';
502 }
503
504 i = 4; /* offset to the start of the identifier */
505 s = j = strlen(serial);
506 if ((page_83[0] & 0x0f) == SCSI_ID_ASCII) {
507 /*
508 * ASCII descriptor.
509 */
510 while (i < (4U + page_83[3]))
511 serial[j++] = page_83[i++];
512 } else {
513 /*
514 * Binary descriptor, convert to ASCII, using two bytes of
515 * ASCII for each byte in the page_83.
516 */
517 while (i < (4U + page_83[3])) {
518 serial[j++] = hexchar(page_83[i] >> 4);
519 serial[j++] = hexchar(page_83[i]);
520 i++;
521 }
522 }
523
524 strcpy(serial_short, serial + s);
525
526 if (id_search->id_type == SCSI_ID_NAA && wwn != NULL) {
527 strscpy(wwn, 17, serial + s);
528 if (wwn_vendor_extension)
529 strscpy(wwn_vendor_extension, 17, serial + s + 16);
530 }
531
532 return 0;
533 }
534
535 /* Extract the raw binary from VPD 0x83 pre-SPC devices */
536 static int check_fill_0x83_prespc3(struct scsi_id_device *dev_scsi,
537 uint8_t page_83[static SCSI_INQ_BUFF_LEN],
538 const struct scsi_id_search_values
539 *id_search, char *serial, char *serial_short, size_t max_len) {
540 size_t j;
541
542 assert(max_len > 0);
543
544 serial[0] = hexchar(SCSI_ID_NAA);
545 /* serial has been memset to zero before */
546 j = strlen(serial); /* j = 1; */
547
548 /* Cap reported page length to buffer size in case of malformed responses */
549 size_t page_len = MIN((size_t)page_83[3], SCSI_INQ_BUFF_LEN - 4);
550 for (size_t i = 0; (i < page_len) && (j + 3 < max_len); ++i) {
551 serial[j++] = hexchar(page_83[4+i] >> 4);
552 serial[j++] = hexchar(page_83[4+i]);
553 }
554 serial[max_len-1] = 0;
555 strncpy(serial_short, serial, max_len-1);
556 return 0;
557 }
558
559 /* Get device identification VPD page */
560 static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
561 char *serial, char *serial_short, size_t len,
562 char *unit_serial_number, char *wwn,
563 char *wwn_vendor_extension, char *tgpt_group) {
564 int retval;
565 uint8_t page_83[SCSI_INQ_BUFF_LEN];
566
567 /* also pick up the page 80 serial number */
568 do_scsi_page80_inquiry(dev_scsi, fd, NULL, unit_serial_number, MAX_SERIAL_LEN);
569
570 memzero(page_83, SCSI_INQ_BUFF_LEN);
571 retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83,
572 SCSI_INQ_BUFF_LEN);
573 if (retval < 0)
574 return 1;
575
576 if (page_83[1] != PAGE_83) {
577 log_debug("%s: Invalid page 0x83", dev_scsi->kernel);
578 return 1;
579 }
580
581 /*
582 * XXX Some devices (IBM 3542) return all spaces for an identifier if
583 * the LUN is not actually configured. This leads to identifiers of
584 * the form: "1 ".
585 */
586
587 /*
588 * Model 4, 5, and (some) model 6 EMC Symmetrix devices return
589 * a page 83 reply according to SCSI-2 format instead of SPC-2/3.
590 *
591 * The SCSI-2 page 83 format returns an IEEE WWN in binary
592 * encoded hexi-decimal in the 16 bytes following the initial
593 * 4-byte page 83 reply header.
594 *
595 * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part
596 * of an Identification descriptor. The 3rd byte of the first
597 * Identification descriptor is a reserved (BSZ) byte field.
598 *
599 * Reference the 7th byte of the page 83 reply to determine
600 * whether the reply is compliant with SCSI-2 or SPC-2/3
601 * specifications. A zero value in the 7th byte indicates
602 * an SPC-2/3 conformant reply, (i.e., the reserved field of the
603 * first Identification descriptor). This byte will be non-zero
604 * for a SCSI-2 conformant page 83 reply from these EMC
605 * Symmetrix models since the 7th byte of the reply corresponds
606 * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is,
607 * 0x006048.
608 */
609
610 if (page_83[6] != 0)
611 return check_fill_0x83_prespc3(dev_scsi, page_83, id_search_list,
612 serial, serial_short, len);
613
614 /*
615 * Search for a match in the prioritized id_search_list - since WWN ids
616 * come first we can pick up the WWN in check_fill_0x83_id().
617 */
618
619 /* Cap reported page length to buffer size in case of malformed responses.
620 * Below, j can equal page_end, and at that point page_83[j + 3] (the first descriptor data byte)
621 * must still be readable before the inner bounds check, so page_end + 4 < SCSI_INQ_BUFF_LEN
622 * requires page_end <= SCSI_INQ_BUFF_LEN - 5. */
623 unsigned page_end = MIN(((unsigned)page_83[2] << 8) + (unsigned)page_83[3] + 3U,
624 SCSI_INQ_BUFF_LEN - 5U);
625
626 FOREACH_ELEMENT(search_value, id_search_list) {
627 /*
628 * Examine each descriptor returned. There is normally only
629 * one or a small number of descriptors.
630 */
631 for (unsigned j = 4; j <= page_end; j += page_83[j + 3] + 4) {
632 /* Ensure the full descriptor fits within the buffer, including
633 * fixed-offset accesses up to page_83[7] in the TGTGROUP path
634 * of check_fill_0x83_id(), so require at least 8 bytes from j */
635 if (j + MAX(4U + (unsigned)page_83[j + 3], 8U) > SCSI_INQ_BUFF_LEN)
636 break;
637 retval = check_fill_0x83_id(dev_scsi, page_83 + j,
638 search_value,
639 serial, serial_short, len,
640 wwn, wwn_vendor_extension,
641 tgpt_group);
642 if (!retval)
643 return retval;
644 else if (retval < 0)
645 return retval;
646 }
647 }
648 return 1;
649 }
650
651 /*
652 * Get device identification VPD page for older SCSI-2 device which is not
653 * compliant with either SPC-2 or SPC-3 format.
654 *
655 * Return the hard coded error code value 2 if the page 83 reply is not
656 * conformant to the SCSI-2 format.
657 */
658 static int do_scsi_page83_prespc3_inquiry(struct scsi_id_device *dev_scsi, int fd,
659 char *serial, char *serial_short, size_t len) {
660 int retval;
661 size_t i, j;
662 uint8_t page_83[SCSI_INQ_BUFF_LEN];
663
664 memzero(page_83, SCSI_INQ_BUFF_LEN);
665 retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83, SCSI_INQ_BUFF_LEN);
666 if (retval < 0)
667 return 1;
668
669 if (page_83[1] != PAGE_83) {
670 log_debug("%s: Invalid page 0x83", dev_scsi->kernel);
671 return 1;
672 }
673 /*
674 * Model 4, 5, and (some) model 6 EMC Symmetrix devices return
675 * a page 83 reply according to SCSI-2 format instead of SPC-2/3.
676 *
677 * The SCSI-2 page 83 format returns an IEEE WWN in binary
678 * encoded hexi-decimal in the 16 bytes following the initial
679 * 4-byte page 83 reply header.
680 *
681 * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part
682 * of an Identification descriptor. The 3rd byte of the first
683 * Identification descriptor is a reserved (BSZ) byte field.
684 *
685 * Reference the 7th byte of the page 83 reply to determine
686 * whether the reply is compliant with SCSI-2 or SPC-2/3
687 * specifications. A zero value in the 7th byte indicates
688 * an SPC-2/3 conformant reply, (i.e., the reserved field of the
689 * first Identification descriptor). This byte will be non-zero
690 * for a SCSI-2 conformant page 83 reply from these EMC
691 * Symmetrix models since the 7th byte of the reply corresponds
692 * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is,
693 * 0x006048.
694 */
695 if (page_83[6] == 0)
696 return 2;
697
698 serial[0] = hexchar(SCSI_ID_NAA);
699 /*
700 * The first four bytes contain data, not a descriptor.
701 */
702 i = 4;
703 j = strlen(serial);
704 /*
705 * Binary descriptor, convert to ASCII,
706 * using two bytes of ASCII for each byte
707 * in the page_83.
708 */
709 /* Cap reported page length to buffer size in case of malformed responses */
710 size_t page_len = MIN((size_t)page_83[3] + 4, SCSI_INQ_BUFF_LEN);
711 while (i < page_len && j + 2 < len) {
712 serial[j++] = hexchar(page_83[i] >> 4);
713 serial[j++] = hexchar(page_83[i]);
714 i++;
715 }
716 return 0;
717 }
718
719 /* Get unit serial number VPD page */
720 static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd,
721 char *serial, char *serial_short, size_t max_len) {
722 int retval;
723 int ser_ind;
724 size_t page_len;
725 uint8_t buf[SCSI_INQ_BUFF_LEN];
726
727 memzero(buf, SCSI_INQ_BUFF_LEN);
728 retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_80, buf, SCSI_INQ_BUFF_LEN);
729 if (retval < 0)
730 return retval;
731
732 if (buf[1] != PAGE_80) {
733 log_debug("%s: Invalid page 0x80", dev_scsi->kernel);
734 return 1;
735 }
736
737 page_len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3];
738 if (max_len < page_len) {
739 log_debug("%s: length %zu too short - need %zu",
740 dev_scsi->kernel, max_len, page_len);
741 return 1;
742 }
743 /*
744 * Prepend 'S' to avoid unlikely collision with page 0x83 vendor
745 * specific type where we prepend '0' + vendor + model.
746 */
747 /* Cap reported page length to buffer size in case of malformed responses */
748 page_len = MIN((size_t)buf[3], SCSI_INQ_BUFF_LEN - 4);
749 if (serial) {
750 serial[0] = 'S';
751 ser_ind = append_vendor_model(dev_scsi, serial + 1);
752 if (ser_ind < 0)
753 return 1;
754 ser_ind++; /* for the leading 'S' */
755 for (size_t i = 4; i < page_len + 4; i++, ser_ind++)
756 serial[ser_ind] = buf[i];
757 }
758 if (serial_short) {
759 memcpy(serial_short, buf + 4, page_len);
760 serial_short[page_len] = '\0';
761 }
762 return 0;
763 }
764
765 int scsi_std_inquiry(struct scsi_id_device *dev_scsi, const char *devname) {
766 unsigned char buf[SCSI_INQ_BUFF_LEN] = {};
767 struct stat statbuf;
768 int r;
769
770 _cleanup_close_ int fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC | O_NOCTTY);
771 if (fd < 0)
772 return log_debug_errno(errno, "scsi_id: cannot open %s: %m", devname);
773
774 if (fstat(fd, &statbuf) < 0)
775 return log_debug_errno(errno, "scsi_id: cannot stat %s: %m", devname);
776
777 format_devnum(statbuf.st_rdev, dev_scsi->kernel);
778
779 r = scsi_inquiry(dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN);
780 if (r < 0)
781 return r;
782
783 memcpy(dev_scsi->vendor, buf + 8, 8);
784 dev_scsi->vendor[8] = '\0';
785 memcpy(dev_scsi->model, buf + 16, 16);
786 dev_scsi->model[16] = '\0';
787 memcpy(dev_scsi->revision, buf + 32, 4);
788 dev_scsi->revision[4] = '\0';
789 dev_scsi->type = buf[0] & 0x1f;
790 return 0;
791 }
792
793 int scsi_get_serial(struct scsi_id_device *dev_scsi, const char *devname,
794 int page_code, size_t len) {
795 uint8_t page0[SCSI_INQ_BUFF_LEN];
796 int fd = -EBADF;
797 int cnt;
798 size_t ind;
799 int retval;
800
801 memzero(dev_scsi->serial, len);
802 for (cnt = 20; cnt > 0; cnt--) {
803 fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC | O_NOCTTY);
804 if (fd >= 0 || errno != EBUSY)
805 break;
806
807 usleep_safe(200U*USEC_PER_MSEC + random_u64_range(100U*USEC_PER_MSEC));
808 }
809 if (fd < 0)
810 return 1;
811
812 if (page_code == PAGE_80) {
813 if (do_scsi_page80_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) {
814 retval = 1;
815 goto completed;
816 } else {
817 retval = 0;
818 goto completed;
819 }
820 } else if (page_code == PAGE_83) {
821 if (do_scsi_page83_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) {
822 retval = 1;
823 goto completed;
824 } else {
825 retval = 0;
826 goto completed;
827 }
828 } else if (page_code == PAGE_83_PRE_SPC3) {
829 retval = do_scsi_page83_prespc3_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len);
830 if (retval) {
831 /*
832 * Fallback to servicing a SPC-2/3 compliant page 83
833 * inquiry if the page 83 reply format does not
834 * conform to pre-SPC3 expectations.
835 */
836 if (retval == 2) {
837 if (do_scsi_page83_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) {
838 retval = 1;
839 goto completed;
840 } else {
841 retval = 0;
842 goto completed;
843 }
844 }
845 else {
846 retval = 1;
847 goto completed;
848 }
849 } else {
850 retval = 0;
851 goto completed;
852 }
853 } else if (page_code != 0x00) {
854 log_debug("%s: unsupported page code 0x%d", dev_scsi->kernel, page_code);
855 retval = 1;
856 goto completed;
857 }
858
859 /*
860 * Get page 0, the page of the pages. By default, try from best to
861 * worst of supported pages: 0x83 then 0x80.
862 */
863 if (do_scsi_page0_inquiry(dev_scsi, fd, page0, SCSI_INQ_BUFF_LEN)) {
864 /*
865 * Don't try anything else. Black list if a specific page
866 * should be used for this vendor+model, or maybe have an
867 * optional fall-back to page 0x80 or page 0x83.
868 */
869 retval = 1;
870 goto completed;
871 }
872
873 /* Cap reported page length to buffer size in case of malformed responses */
874 size_t page0_end = MIN((size_t)page0[3] + 3, SCSI_INQ_BUFF_LEN - 1);
875
876 for (ind = 4; ind <= page0_end; ind++)
877 if (page0[ind] == PAGE_83)
878 if (!do_scsi_page83_inquiry(dev_scsi, fd,
879 dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) {
880 /*
881 * Success
882 */
883 retval = 0;
884 goto completed;
885 }
886
887 for (ind = 4; ind <= page0_end; ind++)
888 if (page0[ind] == PAGE_80)
889 if (!do_scsi_page80_inquiry(dev_scsi, fd,
890 dev_scsi->serial, dev_scsi->serial_short, len)) {
891 /*
892 * Success
893 */
894 retval = 0;
895 goto completed;
896 }
897 retval = 1;
898
899 completed:
900 close(fd);
901 return retval;
902 }