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 it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
26 #include "../../udev/udev.h"
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)
32 vsyslog(priority, format, args);
35 int main(int argc, char *argv[])
38 const char *node = NULL;
54 logging_init("edd_id");
55 udev_set_log_fn(udev, log_fn);
57 for (i = 1 ; i < argc; i++) {
60 if (strcmp(arg, "--export") == 0) {
66 err(udev, "no node specified\n");
67 fprintf(stderr, "no node specified\n");
71 /* check for kernel support */
72 dir = opendir("/sys/firmware/edd");
74 info(udev, "no kernel EDD support\n");
75 fprintf(stderr, "no kernel EDD support\n");
80 disk_fd = open(node, O_RDONLY);
82 info(udev, "unable to open '%s'\n", node);
83 fprintf(stderr, "unable to open '%s'\n", node);
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);
94 if (read(disk_fd, &mbr_valid, sizeof(mbr_valid)) != sizeof(mbr_valid)) {
95 info(udev, "read MBR validity failed '%s'\n", node);
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);
106 /* read EDD signature */
107 if (lseek(disk_fd, 440, SEEK_SET) < 0) {
108 info(udev, "seek to signature failed '%s'\n", node);
112 if (read(disk_fd, &disk_id, sizeof(disk_id)) != sizeof(disk_id)) {
113 info(udev, "read signature failed '%s'\n", node);
117 /* all zero is invalid */
118 info(udev, "read id 0x%08x from '%s'\n", disk_id, node);
120 fprintf(stderr, "no EDD signature '%s'\n", node);
121 info(udev, "'%s' signature is zero\n", node);
126 /* lookup signature in sysfs to determine the name */
128 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
129 char file[UTIL_PATH_SIZE];
130 char sysfs_id_buf[256];
134 if (dent->d_name[0] == '.')
137 snprintf(file, sizeof(file), "/sys/firmware/edd/%s/mbr_signature", dent->d_name);
138 file[sizeof(file)-1] = '\0';
140 sysfs_fd = open(file, O_RDONLY);
142 info(udev, "unable to open sysfs '%s'\n", file);
146 size = read(sysfs_fd, sysfs_id_buf, sizeof(sysfs_id_buf)-1);
149 info(udev, "read sysfs '%s' failed\n", file);
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);
156 /* look for matching value, that appears only once */
157 if (disk_id == sysfs_id) {
158 if (match[0] == '\0') {
160 util_strlcpy(match, dent->d_name, sizeof(match));
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);
171 if (match[0] != '\0') {
173 printf("ID_EDD=%s\n", match);
175 printf("%s\n", match);