chiark / gitweb /
4ba60393a07bdc8596393c89aac5be06b9e42f7f
[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         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
128                 char file[PATH_SIZE];
129                 char sysfs_id_buf[256];
130                 uint32_t sysfs_id;
131                 ssize_t size;
132
133                 if (dent->d_name[0] == '.')
134                         continue;
135
136                 snprintf(file, sizeof(file), "/sys/firmware/edd/%s/mbr_signature", dent->d_name);
137                 file[sizeof(file)-1] = '\0';
138
139                 sysfs_fd = open(file, O_RDONLY);
140                 if (sysfs_fd < 0) {
141                         info(udev, "unable to open sysfs '%s'\n", file);
142                         continue;
143                 }
144
145                 size = read(sysfs_fd, sysfs_id_buf, sizeof(sysfs_id_buf)-1);
146                 close(sysfs_fd);
147                 if (size <= 0) {
148                         info(udev, "read sysfs '%s' failed\n", file);
149                         continue;
150                 }
151                 sysfs_id_buf[size] = '\0';
152                 info(udev, "read '%s' from '%s'\n", sysfs_id_buf, file);
153                 sysfs_id = strtoul(sysfs_id_buf, NULL, 16);
154
155                 /* look for matching value, that appears only once */
156                 if (disk_id == sysfs_id) {
157                         if (match[0] == '\0') {
158                                 /* store id */
159                                 util_strlcpy(match, dent->d_name, sizeof(match));
160                         } else {
161                                 /* error, same signature for another device */
162                                 info(udev, "'%s' does not have a unique signature\n", node);
163                                 fprintf(stderr, "'%s' does not have a unique signature\n", node);
164                                 rc = 10;
165                                 goto exit;
166                         }
167                 }
168         }
169
170         if (match[0] != '\0') {
171                 if (export)
172                         printf("ID_EDD=%s\n", match);
173                 else
174                         printf("%s\n", match);
175                 rc = 0;
176         }
177
178 close:
179         close(disk_fd);
180 closedir:
181         closedir(dir);
182 exit:
183         udev_unref(udev);
184         logging_close();
185         return rc;
186 }