Sun May 15 14:48:37 2022 UTC ()
disks: Get disk identify data from drvctl

When /dev/drvctl exists, attempt to use the disk-info/type property as
a disk's description string. Fallback to ATA / SCSI probing when the
identify data is not available through this interface.

This has the side-effect of adding descriptions for things like NVMe and
SD/eMMC devices.


(jmcneill)
diff -r1.77 -r1.78 src/usr.sbin/sysinst/disks.c

cvs diff -r1.77 -r1.78 src/usr.sbin/sysinst/disks.c (expand / switch to unified diff)

--- src/usr.sbin/sysinst/disks.c 2022/05/15 12:48:25 1.77
+++ src/usr.sbin/sysinst/disks.c 2022/05/15 14:48:37 1.78
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: disks.c,v 1.77 2022/05/15 12:48:25 jmcneill Exp $ */ 1/* $NetBSD: disks.c,v 1.78 2022/05/15 14:48:37 jmcneill Exp $ */
2 2
3/* 3/*
4 * Copyright 1997 Piermont Information Systems Inc. 4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Written by Philip A. Nelson for Piermont Information Systems Inc. 7 * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -51,26 +51,28 @@ @@ -51,26 +51,28 @@
51#include <sys/param.h> 51#include <sys/param.h>
52#include <sys/sysctl.h> 52#include <sys/sysctl.h>
53#include <sys/swap.h> 53#include <sys/swap.h>
54#include <sys/disklabel_gpt.h> 54#include <sys/disklabel_gpt.h>
55#include <ufs/ufs/dinode.h> 55#include <ufs/ufs/dinode.h>
56#include <ufs/ffs/fs.h> 56#include <ufs/ffs/fs.h>
57 57
58#include <dev/scsipi/scsipi_all.h> 58#include <dev/scsipi/scsipi_all.h>
59#include <sys/scsiio.h> 59#include <sys/scsiio.h>
60 60
61#include <dev/ata/atareg.h> 61#include <dev/ata/atareg.h>
62#include <sys/ataio.h> 62#include <sys/ataio.h>
63 63
 64#include <sys/drvctlio.h>
 65
64#include "defs.h" 66#include "defs.h"
65#include "md.h" 67#include "md.h"
66#include "msg_defs.h" 68#include "msg_defs.h"
67#include "menu_defs.h" 69#include "menu_defs.h"
68#include "txtwalk.h" 70#include "txtwalk.h"
69 71
70/* #define DEBUG_VERBOSE 1 */ 72/* #define DEBUG_VERBOSE 1 */
71 73
72/* Disk descriptions */ 74/* Disk descriptions */
73struct disk_desc { 75struct disk_desc {
74 char dd_name[SSTRSIZE]; 76 char dd_name[SSTRSIZE];
75 char dd_descr[256]; 77 char dd_descr[256];
76 bool dd_no_mbr, dd_no_part; 78 bool dd_no_mbr, dd_no_part;
@@ -311,41 +313,107 @@ get_descr_ata(struct disk_desc *dd) @@ -311,41 +313,107 @@ get_descr_ata(struct disk_desc *dd)
311 313
312 ata_extract_string(model, sizeof(model), 314 ata_extract_string(model, sizeof(model),
313 inqbuf->atap_model, sizeof(inqbuf->atap_model), needswap); 315 inqbuf->atap_model, sizeof(inqbuf->atap_model), needswap);
314 humanize_number(size, sizeof(size), 316 humanize_number(size, sizeof(size),
315 (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec, 317 (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
316 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 318 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
317 319
318 snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)", 320 snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
319 dd->dd_name, size, model); 321 dd->dd_name, size, model);
320 322
321 return 1; 323 return 1;
322} 324}
323 325
 326static int
 327get_descr_drvctl(struct disk_desc *dd)
 328{
 329 prop_dictionary_t command_dict;
 330 prop_dictionary_t args_dict;
 331 prop_dictionary_t results_dict;
 332 prop_dictionary_t props;
 333 int8_t perr;
 334 int error, fd;
 335 bool rv;
 336 char size[5];
 337 const char *model;
 338
 339 fd = open("/dev/drvctl", O_RDONLY);
 340 if (fd == -1)
 341 return 0;
 342
 343 command_dict = prop_dictionary_create();
 344 args_dict = prop_dictionary_create();
 345
 346 prop_dictionary_set_cstring_nocopy(command_dict, "drvctl-command",
 347 "get-properties");
 348 prop_dictionary_set_cstring_nocopy(args_dict, "device-name",
 349 dd->dd_name);
 350 prop_dictionary_set(command_dict, "drvctl-arguments", args_dict);
 351 prop_object_release(args_dict);
 352
 353 error = prop_dictionary_sendrecv_ioctl(command_dict, fd,
 354 DRVCTLCOMMAND, &results_dict);
 355 prop_object_release(command_dict);
 356 close(fd);
 357 if (error)
 358 return 0;
 359
 360 rv = prop_dictionary_get_int8(results_dict, "drvctl-error", &perr);
 361 if (rv == false || perr != 0) {
 362 prop_object_release(results_dict);
 363 return 0;
 364 }
 365
 366 props = prop_dictionary_get(results_dict,
 367 "drvctl-result-data");
 368 if (props == NULL) {
 369 prop_object_release(results_dict);
 370 return 0;
 371 }
 372 props = prop_dictionary_get(props, "disk-info");
 373 if (props == NULL ||
 374 !prop_dictionary_get_string(props, "type", &model)) {
 375 prop_object_release(results_dict);
 376 return 0;
 377 }
 378
 379 humanize_number(size, sizeof(size),
 380 (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
 381 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
 382
 383 snprintf(dd->dd_descr, sizeof(dd->dd_descr), "%s (%s, %s)",
 384 dd->dd_name, size, model);
 385
 386 prop_object_release(results_dict);
 387
 388 return 1;
 389}
 390
324static void 391static void
325get_descr(struct disk_desc *dd) 392get_descr(struct disk_desc *dd)
326{ 393{
327 char size[5]; 394 char size[5];
328 dd->dd_descr[0] = '\0'; 395 dd->dd_descr[0] = '\0';
329 396
 397 /* try drvctl first, fallback to direct probing */
 398 if (get_descr_drvctl(dd))
 399 return;
330 /* try ATA */ 400 /* try ATA */
331 if (get_descr_ata(dd)) 401 if (get_descr_ata(dd))
332 return; 402 return;
333 /* try SCSI */ 403 /* try SCSI */
334 if (get_descr_scsi(dd)) 404 if (get_descr_scsi(dd))
335 return; 405 return;
336 406
337 /* XXX: identify for ld @ NVME or microSD */ 
338 
339 /* XXX: get description from raid, cgd, vnd... */ 407 /* XXX: get description from raid, cgd, vnd... */
340 408
341 /* punt, just give some generic info */ 409 /* punt, just give some generic info */
342 humanize_number(size, sizeof(size), 410 humanize_number(size, sizeof(size),
343 (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec, 411 (uint64_t)dd->dd_secsize * (uint64_t)dd->dd_totsec,
344 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 412 "", HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
345 413
346 snprintf(dd->dd_descr, sizeof(dd->dd_descr), 414 snprintf(dd->dd_descr, sizeof(dd->dd_descr),
347 "%s (%s)", dd->dd_name, size); 415 "%s (%s)", dd->dd_name, size);
348} 416}
349 417
350/* 418/*
351 * State for helper callback for get_default_cdrom 419 * State for helper callback for get_default_cdrom