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.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
24 #include <linux/types.h>
25 #include <linux/hdreg.h>
27 #include "../../logging.h"
28 #include "../../udev_utils.h"
31 void log_message(int priority, const char *format, ...)
34 static int udev_log = -1;
39 value = getenv("UDEV_LOG");
41 udev_log = log_priority(value);
46 if (priority > udev_log)
49 va_start(args, format);
50 vsyslog(priority, format, args);
55 static void set_str(char *to, const char *from, size_t count)
59 /* strip trailing whitespace */
60 len = strnlen(from, count);
61 while (len && isspace(from[len-1]))
64 /* strip leading whitespace */
66 while (isspace(from[i]) && (i < len))
71 /* substitute multiple whitespace */
72 if (isspace(from[i])) {
73 while (isspace(from[i]))
87 int main(int argc, char *argv[])
93 const char *node = NULL;
99 logging_init("ata_id");
101 for (i = 1 ; i < argc; i++) {
104 if (strcmp(arg, "--export") == 0) {
110 err("no node specified");
115 fd = open(node, O_RDONLY|O_NONBLOCK);
117 err("unable to open '%s'", node);
122 if (ioctl(fd, HDIO_GET_IDENTITY, &id)) {
123 err("HDIO_GET_IDENTITY failed for '%s'", node);
128 set_str(model, (char *) id.model, 40);
129 set_str(serial, (char *) id.serial_no, 20);
130 set_str(revision, (char *) id.fw_rev, 8);
133 if ((id.config >> 8) & 0x80) {
134 /* This is an ATAPI device */
135 switch ((id.config >> 8) & 0x1f) {
137 printf("ID_TYPE=cd\n");
140 printf("ID_TYPE=tape\n");
143 printf("ID_TYPE=cd\n");
146 printf("ID_TYPE=optical\n");
149 printf("ID_TYPE=generic\n");
153 printf("ID_TYPE=disk\n");
155 printf("ID_MODEL=%s\n", model);
156 printf("ID_SERIAL=%s\n", serial);
157 printf("ID_REVISION=%s\n", revision);
158 printf("ID_BUS=ata\n");
160 if (serial[0] != '\0')
161 printf("%s_%s\n", model, serial);
163 printf("%s\n", model);