2 * ata_id - reads product/serial number from ATA drives
4 * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
26 #include <linux/types.h>
27 #include <linux/hdreg.h>
29 #include "../../udev/udev.h"
31 static void log_fn(struct udev *udev, int priority,
32 const char *file, int line, const char *fn,
33 const char *format, va_list args)
35 vsyslog(priority, format, args);
38 static void set_str(char *to, const char *from, size_t count)
42 /* strip trailing whitespace */
43 len = strnlen(from, count);
44 while (len && isspace(from[len-1]))
47 /* strip leading whitespace */
49 while (isspace(from[i]) && (i < len))
54 /* substitute multiple whitespace */
55 if (isspace(from[i])) {
56 while (isspace(from[i]))
70 int main(int argc, char *argv[])
77 const char *node = NULL;
81 static const struct option options[] = {
82 { "export", 0, NULL, 'x' },
83 { "help", 0, NULL, 'h' },
91 logging_init("ata_id");
92 udev_set_log_fn(udev, log_fn);
97 option = getopt_long(argc, argv, "xh", options, NULL);
106 printf("Usage: ata_id [--export] [--help] <device>\n"
107 " --export print values as environemt keys\n"
108 " --help print this help text\n\n");
117 err(udev, "no node specified\n");
122 fd = open(node, O_RDONLY|O_NONBLOCK);
124 err(udev, "unable to open '%s'\n", node);
129 if (ioctl(fd, HDIO_GET_IDENTITY, &id)) {
130 if (errno == ENOTTY) {
131 info(udev, "HDIO_GET_IDENTITY unsupported for '%s'\n", node);
134 err(udev, "HDIO_GET_IDENTITY failed for '%s'\n", node);
140 set_str(model, (char *) id.model, 40);
141 set_str(serial, (char *) id.serial_no, 20);
142 set_str(revision, (char *) id.fw_rev, 8);
145 if ((id.config >> 8) & 0x80) {
146 /* This is an ATAPI device */
147 switch ((id.config >> 8) & 0x1f) {
149 printf("ID_TYPE=cd\n");
152 printf("ID_TYPE=tape\n");
155 printf("ID_TYPE=cd\n");
158 printf("ID_TYPE=optical\n");
161 printf("ID_TYPE=generic\n");
165 printf("ID_TYPE=disk\n");
167 printf("ID_MODEL=%s\n", model);
168 printf("ID_SERIAL=%s\n", serial);
169 printf("ID_REVISION=%s\n", revision);
170 printf("ID_BUS=ata\n");
172 if (serial[0] != '\0')
173 printf("%s_%s\n", model, serial);
175 printf("%s\n", model);