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;
48 char filename[UTIL_PATH_SIZE];
49 char match[UTIL_PATH_SIZE];
55 logging_init("edd_id");
56 udev_set_log_fn(udev, log_fn);
58 for (i = 1 ; i < argc; i++) {
61 if (strcmp(arg, "--export") == 0) {
67 err(udev, "no node specified\n");
68 fprintf(stderr, "no node specified\n");
72 /* check for kernel support */
73 util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), "/firmware/edd", NULL);
74 dir = opendir(filename);
76 info(udev, "no kernel EDD support\n");
77 fprintf(stderr, "no kernel EDD support\n");
82 disk_fd = open(node, O_RDONLY);
84 info(udev, "unable to open '%s'\n", node);
85 fprintf(stderr, "unable to open '%s'\n", node);
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);
96 if (read(disk_fd, &mbr_valid, sizeof(mbr_valid)) != sizeof(mbr_valid)) {
97 info(udev, "read MBR validity failed '%s'\n", node);
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);
108 /* read EDD signature */
109 if (lseek(disk_fd, 440, SEEK_SET) < 0) {
110 info(udev, "seek to signature failed '%s'\n", node);
114 if (read(disk_fd, &disk_id, sizeof(disk_id)) != sizeof(disk_id)) {
115 info(udev, "read signature failed '%s'\n", node);
119 /* all zero is invalid */
120 info(udev, "read id 0x%08x from '%s'\n", disk_id, node);
122 fprintf(stderr, "no EDD signature '%s'\n", node);
123 info(udev, "'%s' signature is zero\n", node);
128 /* lookup signature in sysfs to determine the name */
130 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
131 char sysfs_id_buf[256];
135 if (dent->d_name[0] == '.')
138 util_strscpyl(filename, sizeof(filename), dent->d_name, "/mbr_signature", NULL);
139 sysfs_fd = openat(dirfd(dir), filename, O_RDONLY);
141 info(udev, "unable to open sysfs '%s'\n", filename);
145 size = read(sysfs_fd, sysfs_id_buf, sizeof(sysfs_id_buf)-1);
148 info(udev, "read sysfs '%s' failed\n", filename);
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);
155 /* look for matching value, that appears only once */
156 if (disk_id == sysfs_id) {
157 if (match[0] == '\0') {
159 util_strscpy(match, sizeof(match), dent->d_name);
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);
170 if (match[0] != '\0') {
172 printf("ID_EDD=%s\n", match);
174 printf("%s\n", match);