2 * edd_id - naming of BIOS disk devices via EDD
4 * Copyright (C) 2005 John Hull <John_Hull@Dell.com>
5 * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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/>.
36 #include "libudev-private.h"
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)
42 vsyslog(priority, format, args);
45 int main(int argc, char *argv[])
48 const char *node = NULL;
58 char filename[UTIL_PATH_SIZE];
59 char match[UTIL_PATH_SIZE];
65 udev_log_init("edd_id");
66 udev_set_log_fn(udev, log_fn);
68 for (i = 1 ; i < argc; i++) {
71 if (strcmp(arg, "--export") == 0) {
77 err(udev, "no node specified\n");
78 fprintf(stderr, "no node specified\n");
82 /* check for kernel support */
83 util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), "/firmware/edd", NULL);
84 dir = opendir(filename);
86 info(udev, "no kernel EDD support\n");
87 fprintf(stderr, "no kernel EDD support\n");
92 disk_fd = open(node, O_RDONLY);
94 info(udev, "unable to open '%s'\n", node);
95 fprintf(stderr, "unable to open '%s'\n", node);
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);
106 if (read(disk_fd, &mbr_valid, sizeof(mbr_valid)) != sizeof(mbr_valid)) {
107 info(udev, "read MBR validity failed '%s'\n", node);
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);
118 /* read EDD signature */
119 if (lseek(disk_fd, 440, SEEK_SET) < 0) {
120 info(udev, "seek to signature failed '%s'\n", node);
124 if (read(disk_fd, &disk_id, sizeof(disk_id)) != sizeof(disk_id)) {
125 info(udev, "read signature failed '%s'\n", node);
129 /* all zero is invalid */
130 info(udev, "read id 0x%08x from '%s'\n", disk_id, node);
132 fprintf(stderr, "no EDD signature '%s'\n", node);
133 info(udev, "'%s' signature is zero\n", node);
138 /* lookup signature in sysfs to determine the name */
140 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
141 char sysfs_id_buf[256];
145 if (dent->d_name[0] == '.')
148 util_strscpyl(filename, sizeof(filename), dent->d_name, "/mbr_signature", NULL);
149 sysfs_fd = openat(dirfd(dir), filename, O_RDONLY);
151 info(udev, "unable to open sysfs '%s'\n", filename);
155 size = read(sysfs_fd, sysfs_id_buf, sizeof(sysfs_id_buf)-1);
158 info(udev, "read sysfs '%s' failed\n", filename);
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);
165 /* look for matching value, that appears only once */
166 if (disk_id == sysfs_id) {
167 if (match[0] == '\0') {
169 util_strscpy(match, sizeof(match), dent->d_name);
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);
180 if (match[0] != '\0') {
182 printf("ID_EDD=%s\n", match);
184 printf("%s\n", match);