chiark / gitweb /
scsi_id: make sure, we do not have slashes in values
[elogind.git] / extras / edd_id / edd_id.c
1 /*
2  * edd_id - naming of BIOS disk devices via EDD
3  *
4  * Copyright (C) 2005 John Hull <John_Hull@Dell.com>
5  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  */
11
12 #ifndef _GNU_SOURCE
13 #define _GNU_SOURCE 1
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <dirent.h>
24 #include <stdint.h>
25
26 #include "../../udev/udev.h"
27
28 static void log_fn(struct udev *udev, int priority,
29                    const char *file, int line, const char *fn,
30                    const char *format, va_list args)
31 {
32         vsyslog(priority, format, args);
33 }
34
35 int main(int argc, char *argv[])
36 {
37         struct udev *udev;
38         const char *node = NULL;
39         int i;
40         int export = 0;
41         uint32_t disk_id;
42         uint16_t mbr_valid;
43         struct dirent *dent;
44         int disk_fd;
45         int sysfs_fd;
46         DIR *dir = NULL;
47         int rc = 1;
48         char match[NAME_MAX];
49
50         udev = udev_new();
51         if (udev == NULL)
52                 goto exit;
53
54         logging_init("edd_id");
55         udev_set_log_fn(udev, log_fn);
56
57         for (i = 1 ; i < argc; i++) {
58                 char *arg = argv[i];
59
60                 if (strcmp(arg, "--export") == 0) {
61                         export = 1;
62                 } else
63                         node = arg;
64         }
65         if (node == NULL) {
66                 err(udev, "no node specified\n");
67                 fprintf(stderr, "no node specified\n");
68                 goto exit;
69         }
70
71         /* check for kernel support */
72         dir = opendir("/sys/firmware/edd");
73         if (dir == NULL) {
74                 info(udev, "no kernel EDD support\n");
75                 fprintf(stderr, "no kernel EDD support\n");
76                 rc = 2;
77                 goto exit;
78         }
79
80         disk_fd = open(node, O_RDONLY);
81         if (disk_fd < 0) {
82                 info(udev, "unable to open '%s'\n", node);
83                 fprintf(stderr, "unable to open '%s'\n", node);
84                 rc = 3;
85                 goto closedir;
86         }
87
88         /* check for valid MBR signature */
89         if (lseek(disk_fd, 510, SEEK_SET) < 0) {
90                 info(udev, "seek to MBR validity failed '%s'\n", node);
91                 rc = 4;
92                 goto close;
93         }
94         if (read(disk_fd, &mbr_valid, sizeof(mbr_valid)) != sizeof(mbr_valid)) {
95                 info(udev, "read MBR validity failed '%s'\n", node);
96                 rc = 5;
97                 goto close;
98         }
99         if (mbr_valid != 0xAA55) {
100                 fprintf(stderr, "no valid MBR signature '%s'\n", node);
101                 info(udev, "no valid MBR signature '%s'\n", node);
102                 rc=6;
103                 goto close;
104         }
105
106         /* read EDD signature */
107         if (lseek(disk_fd, 440, SEEK_SET) < 0) {
108                 info(udev, "seek to signature failed '%s'\n", node);
109                 rc = 7;
110                 goto close;
111         }
112         if (read(disk_fd, &disk_id, sizeof(disk_id)) != sizeof(disk_id)) {
113                 info(udev, "read signature failed '%s'\n", node);
114                 rc = 8;
115                 goto close;
116         }
117         /* all zero is invalid */
118         info(udev, "read id 0x%08x from '%s'\n", disk_id, node);
119         if (disk_id == 0) {
120                 fprintf(stderr, "no EDD signature '%s'\n", node);
121                 info(udev, "'%s' signature is zero\n", node);
122                 rc = 9;
123                 goto close;
124         }
125
126         /* lookup signature in sysfs to determine the name */
127         match[0] = '\0';
128         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
129                 char file[UTIL_PATH_SIZE];
130                 char sysfs_id_buf[256];
131                 uint32_t sysfs_id;
132                 ssize_t size;
133
134                 if (dent->d_name[0] == '.')
135                         continue;
136
137                 snprintf(file, sizeof(file), "/sys/firmware/edd/%s/mbr_signature", dent->d_name);
138                 file[sizeof(file)-1] = '\0';
139
140                 sysfs_fd = open(file, O_RDONLY);
141                 if (sysfs_fd < 0) {
142                         info(udev, "unable to open sysfs '%s'\n", file);
143                         continue;
144                 }
145
146                 size = read(sysfs_fd, sysfs_id_buf, sizeof(sysfs_id_buf)-1);
147                 close(sysfs_fd);
148                 if (size <= 0) {
149                         info(udev, "read sysfs '%s' failed\n", file);
150                         continue;
151                 }
152                 sysfs_id_buf[size] = '\0';
153                 info(udev, "read '%s' from '%s'\n", sysfs_id_buf, file);
154                 sysfs_id = strtoul(sysfs_id_buf, NULL, 16);
155
156                 /* look for matching value, that appears only once */
157                 if (disk_id == sysfs_id) {
158                         if (match[0] == '\0') {
159                                 /* store id */
160                                 util_strlcpy(match, dent->d_name, sizeof(match));
161                         } else {
162                                 /* error, same signature for another device */
163                                 info(udev, "'%s' does not have a unique signature\n", node);
164                                 fprintf(stderr, "'%s' does not have a unique signature\n", node);
165                                 rc = 10;
166                                 goto exit;
167                         }
168                 }
169         }
170
171         if (match[0] != '\0') {
172                 if (export)
173                         printf("ID_EDD=%s\n", match);
174                 else
175                         printf("%s\n", match);
176                 rc = 0;
177         }
178
179 close:
180         close(disk_fd);
181 closedir:
182         closedir(dir);
183 exit:
184         udev_unref(udev);
185         logging_close();
186         return rc;
187 }