1
0
mirror of https://github.com/systemd/systemd.git synced 2025-08-04 12:22:23 +03:00

ata_id: modernize

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2014-12-25 10:42:23 -05:00
parent f2c0b4f1b1
commit 11c6f693e9

View File

@ -43,6 +43,7 @@
#include "libudev.h" #include "libudev.h"
#include "libudev-private.h" #include "libudev-private.h"
#include "udev-util.h"
#include "log.h" #include "log.h"
#define COMMAND_TIMEOUT_MSEC (30 * 1000) #define COMMAND_TIMEOUT_MSEC (30 * 1000)
@ -407,7 +408,7 @@ out:
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct udev *udev; _cleanup_udev_unref_ struct udev *udev = NULL;
struct hd_driveid id; struct hd_driveid id;
union { union {
uint8_t byte[512]; uint8_t byte[512];
@ -421,9 +422,8 @@ int main(int argc, char *argv[])
char revision[9]; char revision[9];
const char *node = NULL; const char *node = NULL;
int export = 0; int export = 0;
int fd; _cleanup_close_ int fd = -1;
uint16_t word; uint16_t word;
int rc = 0;
int is_packet_device = 0; int is_packet_device = 0;
static const struct option options[] = { static const struct option options[] = {
{ "export", no_argument, NULL, 'x' }, { "export", no_argument, NULL, 'x' },
@ -436,7 +436,7 @@ int main(int argc, char *argv[])
udev = udev_new(); udev = udev_new();
if (udev == NULL) if (udev == NULL)
goto exit; return 0;
while (1) { while (1) {
int option; int option;
@ -453,22 +453,20 @@ int main(int argc, char *argv[])
printf("Usage: ata_id [--export] [--help] <device>\n" printf("Usage: ata_id [--export] [--help] <device>\n"
" --export print values as environment keys\n" " --export print values as environment keys\n"
" --help print this help text\n\n"); " --help print this help text\n\n");
goto exit; return 0;
} }
} }
node = argv[optind]; node = argv[optind];
if (node == NULL) { if (node == NULL) {
log_error("no node specified"); log_error("no node specified");
rc = 1; return 1;
goto exit;
} }
fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC); fd = open(node, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
if (fd < 0) { if (fd < 0) {
log_error("unable to open '%s'", node); log_error("unable to open '%s'", node);
rc = 1; return 1;
goto exit;
} }
if (disk_identify(udev, fd, identify.byte, &is_packet_device) == 0) { if (disk_identify(udev, fd, identify.byte, &is_packet_device) == 0) {
@ -499,8 +497,7 @@ int main(int argc, char *argv[])
/* If this fails, then try HDIO_GET_IDENTITY */ /* If this fails, then try HDIO_GET_IDENTITY */
if (ioctl(fd, HDIO_GET_IDENTITY, &id) != 0) { if (ioctl(fd, HDIO_GET_IDENTITY, &id) != 0) {
log_debug_errno(errno, "HDIO_GET_IDENTITY failed for '%s': %m", node); log_debug_errno(errno, "HDIO_GET_IDENTITY failed for '%s': %m", node);
rc = 2; return 2;
goto close;
} }
} }
identify_words = &identify.wyde; identify_words = &identify.wyde;
@ -553,8 +550,8 @@ int main(int argc, char *argv[])
} }
if (id.command_set_1 & (1<<5)) { if (id.command_set_1 & (1<<5)) {
printf ("ID_ATA_WRITE_CACHE=1\n"); printf("ID_ATA_WRITE_CACHE=1\n");
printf ("ID_ATA_WRITE_CACHE_ENABLED=%d\n", (id.cfs_enable_1 & (1<<5)) ? 1 : 0); printf("ID_ATA_WRITE_CACHE_ENABLED=%d\n", (id.cfs_enable_1 & (1<<5)) ? 1 : 0);
} }
if (id.command_set_1 & (1<<10)) { if (id.command_set_1 & (1<<10)) {
printf("ID_ATA_FEATURE_SET_HPA=1\n"); printf("ID_ATA_FEATURE_SET_HPA=1\n");
@ -635,13 +632,10 @@ int main(int argc, char *argv[])
/* Word 217 indicates the nominal media rotation rate of the device */ /* Word 217 indicates the nominal media rotation rate of the device */
word = identify_words[217]; word = identify_words[217];
if (word != 0x0000) { if (word == 0x0001)
if (word == 0x0001) { printf ("ID_ATA_ROTATION_RATE_RPM=0\n"); /* non-rotating e.g. SSD */
printf ("ID_ATA_ROTATION_RATE_RPM=0\n"); /* non-rotating e.g. SSD */ else if (word >= 0x0401 && word <= 0xfffe)
} else if (word >= 0x0401 && word <= 0xfffe) { printf ("ID_ATA_ROTATION_RATE_RPM=%d\n", word);
printf ("ID_ATA_ROTATION_RATE_RPM=%d\n", word);
}
}
/* /*
* Words 108-111 contain a mandatory World Wide Name (WWN) in the NAA IEEE Registered identifier * Words 108-111 contain a mandatory World Wide Name (WWN) in the NAA IEEE Registered identifier
@ -649,33 +643,22 @@ int main(int argc, char *argv[])
* All other values are reserved. * All other values are reserved.
*/ */
word = identify_words[108]; word = identify_words[108];
if ((word & 0xf000) == 0x5000) { if ((word & 0xf000) == 0x5000)
uint64_t wwwn; printf("ID_WWN=0x%1$"PRIu64"x\n"
"ID_WWN_WITH_EXTENSION=0x%1$"PRIu64"x\n",
wwwn = identify.octa[108/4]; identify.octa[108/4]);
printf("ID_WWN=0x%llx\n", (unsigned long long int) wwwn);
/* ATA devices have no vendor extension */
printf("ID_WWN_WITH_EXTENSION=0x%llx\n", (unsigned long long int) wwwn);
}
/* from Linux's include/linux/ata.h */ /* from Linux's include/linux/ata.h */
if (identify_words[0] == 0x848a || identify_words[0] == 0x844a) { if (identify_words[0] == 0x848a ||
identify_words[0] == 0x844a ||
(identify_words[83] & 0xc004) == 0x4004)
printf("ID_ATA_CFA=1\n"); printf("ID_ATA_CFA=1\n");
} else {
if ((identify_words[83] & 0xc004) == 0x4004) {
printf("ID_ATA_CFA=1\n");
}
}
} else { } else {
if (serial[0] != '\0') if (serial[0] != '\0')
printf("%s_%s\n", model, serial); printf("%s_%s\n", model, serial);
else else
printf("%s\n", model); printf("%s\n", model);
} }
close:
close(fd); return 0;
exit:
udev_unref(udev);
log_close();
return rc;
} }