chiark / gitweb /
autogen.sh: enable git pre-commit
[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
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <dirent.h>
33 #include <stdint.h>
34
35 #include "libudev.h"
36 #include "libudev-private.h"
37
38 static void log_fn(struct udev *udev, int priority,
39                    const char *file, int line, const char *fn,
40                    const char *format, va_list args)
41 {
42         vsyslog(priority, format, args);
43 }
44
45 int main(int argc, char *argv[])
46 {
47         struct udev *udev;
48         const char *node = NULL;
49         int i;
50         int export = 0;
51         uint32_t disk_id;
52         uint16_t mbr_valid;
53         struct dirent *dent;
54         int disk_fd;
55         int sysfs_fd;
56         DIR *dir = NULL;
57         int rc = 1;
58         char filename[UTIL_PATH_SIZE];
59         char match[UTIL_PATH_SIZE];
60
61         udev = udev_new();
62         if (udev == NULL)
63                 goto exit;
64
65         udev_log_init("edd_id");
66         udev_set_log_fn(udev, log_fn);
67
68         for (i = 1 ; i < argc; i++) {
69                 char *arg = argv[i];
70
71                 if (strcmp(arg, "--export") == 0) {
72                         export = 1;
73                 } else
74                         node = arg;
75         }
76         if (node == NULL) {
77                 err(udev, "no node specified\n");
78                 fprintf(stderr, "no node specified\n");
79                 goto exit;
80         }
81
82         /* check for kernel support */
83         util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), "/firmware/edd", NULL);
84         dir = opendir(filename);
85         if (dir == NULL) {
86                 info(udev, "no kernel EDD support\n");
87                 fprintf(stderr, "no kernel EDD support\n");
88                 rc = 2;
89                 goto exit;
90         }
91
92         disk_fd = open(node, O_RDONLY);
93         if (disk_fd < 0) {
94                 info(udev, "unable to open '%s'\n", node);
95                 fprintf(stderr, "unable to open '%s'\n", node);
96                 rc = 3;
97                 goto closedir;
98         }
99
100         /* check for valid MBR signature */
101         if (lseek(disk_fd, 510, SEEK_SET) < 0) {
102                 info(udev, "seek to MBR validity failed '%s'\n", node);
103                 rc = 4;
104                 goto close;
105         }
106         if (read(disk_fd, &mbr_valid, sizeof(mbr_valid)) != sizeof(mbr_valid)) {
107                 info(udev, "read MBR validity failed '%s'\n", node);
108                 rc = 5;
109                 goto close;
110         }
111         if (mbr_valid != 0xAA55) {
112                 fprintf(stderr, "no valid MBR signature '%s'\n", node);
113                 info(udev, "no valid MBR signature '%s'\n", node);
114                 rc=6;
115                 goto close;
116         }
117
118         /* read EDD signature */
119         if (lseek(disk_fd, 440, SEEK_SET) < 0) {
120                 info(udev, "seek to signature failed '%s'\n", node);
121                 rc = 7;
122                 goto close;
123         }
124         if (read(disk_fd, &disk_id, sizeof(disk_id)) != sizeof(disk_id)) {
125                 info(udev, "read signature failed '%s'\n", node);
126                 rc = 8;
127                 goto close;
128         }
129         /* all zero is invalid */
130         info(udev, "read id 0x%08x from '%s'\n", disk_id, node);
131         if (disk_id == 0) {
132                 fprintf(stderr, "no EDD signature '%s'\n", node);
133                 info(udev, "'%s' signature is zero\n", node);
134                 rc = 9;
135                 goto close;
136         }
137
138         /* lookup signature in sysfs to determine the name */
139         match[0] = '\0';
140         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
141                 char sysfs_id_buf[256];
142                 uint32_t sysfs_id;
143                 ssize_t size;
144
145                 if (dent->d_name[0] == '.')
146                         continue;
147
148                 util_strscpyl(filename, sizeof(filename), dent->d_name, "/mbr_signature", NULL);
149                 sysfs_fd = openat(dirfd(dir), filename, O_RDONLY);
150                 if (sysfs_fd < 0) {
151                         info(udev, "unable to open sysfs '%s'\n", filename);
152                         continue;
153                 }
154
155                 size = read(sysfs_fd, sysfs_id_buf, sizeof(sysfs_id_buf)-1);
156                 close(sysfs_fd);
157                 if (size <= 0) {
158                         info(udev, "read sysfs '%s' failed\n", filename);
159                         continue;
160                 }
161                 sysfs_id_buf[size] = '\0';
162                 info(udev, "read '%s' from '%s'\n", sysfs_id_buf, filename);
163                 sysfs_id = strtoul(sysfs_id_buf, NULL, 16);
164
165                 /* look for matching value, that appears only once */
166                 if (disk_id == sysfs_id) {
167                         if (match[0] == '\0') {
168                                 /* store id */
169                                 util_strscpy(match, sizeof(match), dent->d_name);
170                         } else {
171                                 /* error, same signature for another device */
172                                 info(udev, "'%s' does not have a unique signature\n", node);
173                                 fprintf(stderr, "'%s' does not have a unique signature\n", node);
174                                 rc = 10;
175                                 goto exit;
176                         }
177                 }
178         }
179
180         if (match[0] != '\0') {
181                 if (export)
182                         printf("ID_EDD=%s\n", match);
183                 else
184                         printf("%s\n", match);
185                 rc = 0;
186         }
187
188 close:
189         close(disk_fd);
190 closedir:
191         closedir(dir);
192 exit:
193         udev_unref(udev);
194         udev_log_close();
195         return rc;
196 }