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
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
35 #include <linux/types.h>
36 #include <linux/hdreg.h>
38 #include "../../udev/udev.h"
40 static void log_fn(struct udev *udev, int priority,
41 const char *file, int line, const char *fn,
42 const char *format, va_list args)
44 vsyslog(priority, format, args);
47 static void set_str(char *to, const char *from, size_t count)
51 /* strip trailing whitespace */
52 len = strnlen(from, count);
53 while (len && isspace(from[len-1]))
56 /* strip leading whitespace */
58 while (isspace(from[i]) && (i < len))
63 /* substitute multiple whitespace */
64 if (isspace(from[i])) {
65 while (isspace(from[i]))
79 int main(int argc, char *argv[])
86 const char *node = NULL;
90 static const struct option options[] = {
91 { "export", no_argument, NULL, 'x' },
92 { "help", no_argument, NULL, 'h' },
100 logging_init("ata_id");
101 udev_set_log_fn(udev, log_fn);
106 option = getopt_long(argc, argv, "xh", options, NULL);
115 printf("Usage: ata_id [--export] [--help] <device>\n"
116 " --export print values as environemt keys\n"
117 " --help print this help text\n\n");
126 err(udev, "no node specified\n");
131 fd = open(node, O_RDONLY|O_NONBLOCK);
133 err(udev, "unable to open '%s'\n", node);
138 if (ioctl(fd, HDIO_GET_IDENTITY, &id)) {
139 if (errno == ENOTTY) {
140 info(udev, "HDIO_GET_IDENTITY unsupported for '%s'\n", node);
143 err(udev, "HDIO_GET_IDENTITY failed for '%s'\n", node);
149 set_str(model, (char *) id.model, 40);
150 set_str(serial, (char *) id.serial_no, 20);
151 set_str(revision, (char *) id.fw_rev, 8);
154 if ((id.config >> 8) & 0x80) {
155 /* This is an ATAPI device */
156 switch ((id.config >> 8) & 0x1f) {
158 printf("ID_TYPE=cd\n");
161 printf("ID_TYPE=tape\n");
164 printf("ID_TYPE=cd\n");
167 printf("ID_TYPE=optical\n");
170 printf("ID_TYPE=generic\n");
174 printf("ID_TYPE=disk\n");
176 printf("ID_MODEL=%s\n", model);
177 printf("ID_SERIAL=%s\n", serial);
178 printf("ID_REVISION=%s\n", revision);
179 printf("ID_BUS=ata\n");
181 if (serial[0] != '\0')
182 printf("%s_%s\n", model, serial);
184 printf("%s\n", model);