2 * ata_id - reads product/serial number from ATA drives
4 * Copyright (C) 2005 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.h"
32 void log_message(int priority, const char *format, ...)
35 static int udev_log = -1;
40 value = getenv("UDEV_LOG");
42 udev_log = log_priority(value);
47 if (priority > udev_log)
50 va_start(args, format);
51 vsyslog(priority, format, args);
56 static void set_str(char *to, const char *from, size_t count)
60 /* strip trailing whitespace */
61 len = strnlen(from, count);
62 while (len && isspace(from[len-1]))
65 /* strip leading whitespace */
67 while (isspace(from[i]) && (i < len))
72 /* substitute multiple whitespace */
73 if (isspace(from[i])) {
74 while (isspace(from[i]))
88 int main(int argc, char *argv[])
94 const char *node = NULL;
98 static const struct option options[] = {
99 { "export", 0, NULL, 'x' },
100 { "help", 0, NULL, 'h' },
104 logging_init("ata_id");
109 option = getopt_long(argc, argv, "xh", options, NULL);
118 printf("Usage: ata_id [--export] [--help] <device>\n"
119 " --export print values as environemt keys\n"
120 " --help print this help text\n\n");
129 err("no node specified");
134 fd = open(node, O_RDONLY|O_NONBLOCK);
136 err("unable to open '%s'", node);
141 if (ioctl(fd, HDIO_GET_IDENTITY, &id)) {
142 if (errno == ENOTTY) {
143 info("HDIO_GET_IDENTITY unsupported for '%s'", node);
146 err("HDIO_GET_IDENTITY failed for '%s'", node);
152 set_str(model, (char *) id.model, 40);
153 set_str(serial, (char *) id.serial_no, 20);
154 set_str(revision, (char *) id.fw_rev, 8);
157 if ((id.config >> 8) & 0x80) {
158 /* This is an ATAPI device */
159 switch ((id.config >> 8) & 0x1f) {
161 printf("ID_TYPE=cd\n");
164 printf("ID_TYPE=tape\n");
167 printf("ID_TYPE=cd\n");
170 printf("ID_TYPE=optical\n");
173 printf("ID_TYPE=generic\n");
177 printf("ID_TYPE=disk\n");
179 printf("ID_MODEL=%s\n", model);
180 printf("ID_SERIAL=%s\n", serial);
181 printf("ID_REVISION=%s\n", revision);
182 printf("ID_BUS=ata\n");
184 if (serial[0] != '\0')
185 printf("%s_%s\n", model, serial);
187 printf("%s\n", model);