1 /* ofpath.c - calculate OpenFirmware path names given an OS device */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009, 2011,2012, 2013 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #undef OFPATH_STANDALONE
22 #ifndef OFPATH_STANDALONE
23 #include <grub/types.h>
24 #include <grub/util/misc.h>
25 #include <grub/util/ofpath.h>
26 #include <grub/i18n.h>
35 #include <sys/types.h>
41 #ifdef OFPATH_STANDALONE
42 #define xmalloc malloc
44 grub_util_error (const char *fmt, ...)
48 fprintf (stderr, "ofpath: error: ");
50 vfprintf (stderr, fmt, ap);
57 grub_util_info (const char *fmt, ...)
61 fprintf (stderr, "ofpath: info: ");
63 vfprintf (stderr, fmt, ap);
68 #define grub_util_warn grub_util_info
70 #define xstrdup strdup
74 kill_trailing_dir(char *path)
76 char *end = path + strlen(path) - 1;
91 trim_newline (char *path)
93 char *end = path + strlen(path) - 1;
99 #define MAX_DISK_CAT 64
102 find_obppath (const char *sysfs_path_orig)
104 char *sysfs_path, *path;
105 size_t path_size = strlen (sysfs_path_orig) + sizeof ("/obppath");
107 sysfs_path = xstrdup (sysfs_path_orig);
108 path = xmalloc (path_size);
117 snprintf(path, path_size, "%s/obppath", sysfs_path);
119 printf("Trying %s\n", path);
122 fd = open(path, O_RDONLY);
125 if (fd < 0 || fstat (fd, &st) < 0)
129 snprintf(path, path_size, "%s/devspec", sysfs_path);
130 fd = open(path, O_RDONLY);
134 if (fd < 0 || fstat (fd, &st) < 0)
138 kill_trailing_dir(sysfs_path);
139 if (!strcmp(sysfs_path, "/sys"))
141 grub_util_info (_("`obppath' not found in parent dirs of `%s',"
142 " no IEEE1275 name discovery"),
151 of_path = xmalloc (size + MAX_DISK_CAT + 1);
152 memset(of_path, 0, size + MAX_DISK_CAT + 1);
153 if (read(fd, of_path, size) < 0)
155 grub_util_info (_("cannot read `%s': %s"), path, strerror (errno));
164 trim_newline(of_path);
172 xrealpath (const char *in)
176 out = xmalloc (PATH_MAX);
177 out = realpath (in, out);
179 out = realpath (in, NULL);
182 grub_util_error (_("failed to get canonical path of `%s'"), in);
187 block_device_get_sysfs_path_and_link(const char *devicenode)
192 size_t tmp_size = strlen (devicenode) + sizeof ("/sys/block/");
193 char *tmp = xmalloc (tmp_size);
195 memcpy (tmp, "/sys/block/", sizeof ("/sys/block/"));
196 strcat (tmp, devicenode);
198 rpath = xrealpath (tmp);
199 rpath2 = xmalloc (strlen (rpath) + sizeof ("/device"));
200 strcpy (rpath2, rpath);
201 strcat (rpath2, "/device");
203 ret = xrealpath (rpath2);
214 return (c >= '0' && c <= '9');
218 trailing_digits (const char *p)
222 end = p + strlen(p) - 1;
225 if (! my_isdigit(*end))
234 __of_path_common(char *sysfs_path,
235 const char *device, int devno)
237 const char *digit_string;
238 char disk[MAX_DISK_CAT];
239 char *of_path = find_obppath(sysfs_path);
244 digit_string = trailing_digits (device);
245 if (*digit_string == '\0')
247 snprintf(disk, sizeof (disk), "/disk@%d", devno);
253 sscanf(digit_string, "%d", &part);
254 snprintf(disk, sizeof (disk), "/disk@%d:%c", devno, 'a' + (part - 1));
256 strcat(of_path, disk);
261 get_basename(char *p)
276 of_path_of_vdisk(const char *sys_devname __attribute__((unused)),
278 const char *devnode __attribute__((unused)),
279 const char *devicenode)
281 char *sysfs_path, *p;
285 sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
286 p = get_basename (sysfs_path);
287 sscanf(p, "vdc-port-%d-%d", &devno, &junk);
288 ret = __of_path_common (sysfs_path, device, devno);
295 of_path_of_ide(const char *sys_devname __attribute__((unused)), const char *device,
296 const char *devnode __attribute__((unused)),
297 const char *devicenode)
299 char *sysfs_path, *p;
303 sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
304 p = get_basename (sysfs_path);
305 sscanf(p, "%d.%d", &chan, &devno);
307 ret = __of_path_common(sysfs_path, device, 2 * chan + devno);
314 vendor_is_ATA(const char *path)
321 path_size = strlen (path) + sizeof ("/vendor");
323 bufname = xmalloc (path_size);
325 snprintf (bufname, path_size, "%s/vendor", path);
326 fd = open (bufname, O_RDONLY);
328 grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
330 memset(bufcont, 0, sizeof (bufcont));
331 err = read(fd, bufcont, sizeof (bufcont));
333 grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
338 return (memcmp(bufcont, "ATA", 3) == 0);
342 check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
344 char *ed = strstr (sysfs_path, "end_device");
353 /* SAS devices are identified using disk@$PHY_ID */
354 p = xstrdup (sysfs_path);
355 ed = strstr(p, "end_device");
360 while (*q && *q != '/')
364 path_size = (strlen (p) + strlen (ed)
365 + sizeof ("%s/sas_device/%s/phy_identifier"));
366 path = xmalloc (path_size);
367 snprintf (path, path_size, "%s/sas_device/%s/phy_identifier", p, ed);
368 fd = open (path, O_RDONLY);
370 grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
372 memset (phy, 0, sizeof (phy));
373 if (read (fd, phy, sizeof (phy) - 1) < 0)
374 grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
378 sscanf (phy, "%d", tgt);
380 snprintf (path, path_size, "%s/sas_device/%s/sas_address", p, ed);
381 fd = open (path, O_RDONLY);
383 grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
385 memset (phy, 0, sizeof (phy));
386 if (read (fd, phy, sizeof (phy) - 1) < 0)
387 grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
388 sscanf (phy, "%lx", sas_address);
396 of_path_of_scsi(const char *sys_devname __attribute__((unused)), const char *device,
397 const char *devnode __attribute__((unused)),
398 const char *devicenode)
400 const char *p, *digit_string, *disk_name;
401 int host, bus, tgt, lun;
402 unsigned long int sas_address;
403 char *sysfs_path, disk[MAX_DISK_CAT - sizeof ("/fp@0,0")];
406 sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
407 p = get_basename (sysfs_path);
408 sscanf(p, "%d:%d:%d:%d", &host, &bus, &tgt, &lun);
409 check_sas (sysfs_path, &tgt, &sas_address);
411 if (vendor_is_ATA(sysfs_path))
413 of_path = __of_path_common(sysfs_path, device, tgt);
418 of_path = find_obppath(sysfs_path);
423 if (strstr (of_path, "qlc"))
424 strcat (of_path, "/fp@0,0");
426 if (strstr (of_path, "sbus"))
431 digit_string = trailing_digits (device);
432 if (strncmp (of_path, "/vdevice/", sizeof ("/vdevice/") - 1) == 0)
434 unsigned long id = 0x8000 | (tgt << 8) | (bus << 5) | lun;
435 if (*digit_string == '\0')
437 snprintf(disk, sizeof (disk), "/%s@%04lx000000000000", disk_name, id);
443 sscanf(digit_string, "%d", &part);
444 snprintf(disk, sizeof (disk),
445 "/%s@%04lx000000000000:%c", disk_name, id, 'a' + (part - 1));
453 sas_id = bus << 16 | tgt << 8 | lun;
455 if (*digit_string == '\0')
457 snprintf(disk, sizeof (disk), "/sas/%s@%x", disk_name, sas_id);
463 sscanf(digit_string, "%d", &part);
464 snprintf(disk, sizeof (disk),
465 "/sas/%s@%x:%c", disk_name, sas_id, 'a' + (part - 1));
473 lunstr = xmalloc (20);
475 lunpart[0] = (lun >> 8) & 0xff;
476 lunpart[1] = lun & 0xff;
477 lunpart[2] = (lun >> 24) & 0xff;
478 lunpart[3] = (lun >> 16) & 0xff;
480 sprintf(lunstr, "%02x%02x%02x%02x00000000", lunpart[0], lunpart[1], lunpart[2], lunpart[3]);
481 long int longlun = atol(lunstr);
483 if (*digit_string == '\0')
485 snprintf(disk, sizeof (disk), "/sas/%s@%lx,%lu", disk_name, sas_address, longlun);
491 sscanf(digit_string, "%d", &part);
492 snprintf(disk, sizeof (disk),
493 "/sas/%s@%lx,%lu:%c", disk_name, sas_address, longlun, 'a' + (part - 1));
498 strcat(of_path, disk);
503 strip_trailing_digits (const char *p)
508 end = new + strlen(new) - 1;
511 if (! my_isdigit(*end))
520 grub_util_devname_to_ofpath (const char *sys_devname)
522 char *name_buf, *device, *devnode, *devicenode, *ofpath;
524 name_buf = xrealpath (sys_devname);
526 device = get_basename (name_buf);
527 devnode = strip_trailing_digits (name_buf);
528 devicenode = strip_trailing_digits (device);
530 if (device[0] == 'h' && device[1] == 'd')
531 ofpath = of_path_of_ide(name_buf, device, devnode, devicenode);
532 else if (device[0] == 's'
533 && (device[1] == 'd' || device[1] == 'r'))
534 ofpath = of_path_of_scsi(name_buf, device, devnode, devicenode);
535 else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i'
536 && device[3] == 's' && device[4] == 'k')
537 ofpath = of_path_of_vdisk(name_buf, device, devnode, devicenode);
538 else if (device[0] == 'f' && device[1] == 'd'
539 && device[2] == '0' && device[3] == '\0')
540 /* All the models I've seen have a devalias "floppy".
541 New models have no floppy at all. */
542 ofpath = xstrdup ("floppy");
545 grub_util_warn (_("unknown device type %s"), device);
556 #ifdef OFPATH_STANDALONE
557 int main(int argc, char **argv)
563 printf(_("Usage: %s DEVICE\n"), argv[0]);
567 of_path = grub_util_devname_to_ofpath (argv[1]);
569 printf("%s\n", of_path);