chiark / gitweb /
libudev: call log functions conditionally
[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 filename[UTIL_PATH_SIZE];
49         char match[UTIL_PATH_SIZE];
50
51         udev = udev_new();
52         if (udev == NULL)
53                 goto exit;
54
55         logging_init("edd_id");
56         udev_set_log_fn(udev, log_fn);
57
58         for (i = 1 ; i < argc; i++) {
59                 char *arg = argv[i];
60
61                 if (strcmp(arg, "--export") == 0) {
62                         export = 1;
63                 } else
64                         node = arg;
65         }
66         if (node == NULL) {
67                 err(udev, "no node specified\n");
68                 fprintf(stderr, "no node specified\n");
69                 goto exit;
70         }
71
72         /* check for kernel support */
73         util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), "/firmware/edd", NULL);
74         dir = opendir(filename);
75         if (dir == NULL) {
76                 info(udev, "no kernel EDD support\n");
77                 fprintf(stderr, "no kernel EDD support\n");
78                 rc = 2;
79                 goto exit;
80         }
81
82         disk_fd = open(node, O_RDONLY);
83         if (disk_fd < 0) {
84                 info(udev, "unable to open '%s'\n", node);
85                 fprintf(stderr, "unable to open '%s'\n", node);
86                 rc = 3;
87                 goto closedir;
88         }
89
90         /* check for valid MBR signature */
91         if (lseek(disk_fd, 510, SEEK_SET) < 0) {
92                 info(udev, "seek to MBR validity failed '%s'\n", node);
93                 rc = 4;
94                 goto close;
95         }
96         if (read(disk_fd, &mbr_valid, sizeof(mbr_valid)) != sizeof(mbr_valid)) {
97                 info(udev, "read MBR validity failed '%s'\n", node);
98                 rc = 5;
99                 goto close;
100         }
101         if (mbr_valid != 0xAA55) {
102                 fprintf(stderr, "no valid MBR signature '%s'\n", node);
103                 info(udev, "no valid MBR signature '%s'\n", node);
104                 rc=6;
105                 goto close;
106         }
107
108         /* read EDD signature */
109         if (lseek(disk_fd, 440, SEEK_SET) < 0) {
110                 info(udev, "seek to signature failed '%s'\n", node);
111                 rc = 7;
112                 goto close;
113         }
114         if (read(disk_fd, &disk_id, sizeof(disk_id)) != sizeof(disk_id)) {
115                 info(udev, "read signature failed '%s'\n", node);
116                 rc = 8;
117                 goto close;
118         }
119         /* all zero is invalid */
120         info(udev, "read id 0x%08x from '%s'\n", disk_id, node);
121         if (disk_id == 0) {
122                 fprintf(stderr, "no EDD signature '%s'\n", node);
123                 info(udev, "'%s' signature is zero\n", node);
124                 rc = 9;
125                 goto close;
126         }
127
128         /* lookup signature in sysfs to determine the name */
129         match[0] = '\0';
130         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
131                 char sysfs_id_buf[256];
132                 uint32_t sysfs_id;
133                 ssize_t size;
134
135                 if (dent->d_name[0] == '.')
136                         continue;
137
138                 util_strscpyl(filename, sizeof(filename), dent->d_name, "/mbr_signature", NULL);
139                 sysfs_fd = openat(dirfd(dir), filename, O_RDONLY);
140                 if (sysfs_fd < 0) {
141                         info(udev, "unable to open sysfs '%s'\n", filename);
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", filename);
149                         continue;
150                 }
151                 sysfs_id_buf[size] = '\0';
152                 info(udev, "read '%s' from '%s'\n", sysfs_id_buf, filename);
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_strscpy(match, sizeof(match), dent->d_name);
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 }