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>
44 GRUB_OFPATH_SPARC_WWN_ADDR = 1,
45 GRUB_OFPATH_SPARC_TGT_LUN,
46 } ofpath_sparc_addressing;
48 struct ofpath_sparc_hba
50 grub_uint32_t device_id;
51 ofpath_sparc_addressing addressing;
54 static struct ofpath_sparc_hba sparc_lsi_hba[] = {
55 /* Rhea, Jasper 320, LSI53C1020/1030. */
56 {0x30, GRUB_OFPATH_SPARC_TGT_LUN},
58 {0x50, GRUB_OFPATH_SPARC_TGT_LUN},
60 {0x56, GRUB_OFPATH_SPARC_TGT_LUN},
61 /* Pandora SAS-1068E. */
62 {0x58, GRUB_OFPATH_SPARC_TGT_LUN},
63 /* Aspen, Invader, LSI SAS-3108. */
64 {0x5d, GRUB_OFPATH_SPARC_TGT_LUN},
65 /* Niwot, SAS 2108. */
66 {0x79, GRUB_OFPATH_SPARC_TGT_LUN},
67 /* Erie, Falcon, LSI SAS 2008. */
68 {0x72, GRUB_OFPATH_SPARC_WWN_ADDR},
69 /* LSI WarpDrive 6203. */
70 {0x7e, GRUB_OFPATH_SPARC_WWN_ADDR},
72 {0x87, GRUB_OFPATH_SPARC_WWN_ADDR},
74 {0x97, GRUB_OFPATH_SPARC_WWN_ADDR},
78 static const int LSI_VENDOR_ID = 0x1000;
81 #ifdef OFPATH_STANDALONE
82 #define xmalloc malloc
84 grub_util_error (const char *fmt, ...)
88 fprintf (stderr, "ofpath: error: ");
90 vfprintf (stderr, fmt, ap);
97 grub_util_info (const char *fmt, ...)
101 fprintf (stderr, "ofpath: info: ");
103 vfprintf (stderr, fmt, ap);
105 fputc ('\n', stderr);
108 #define grub_util_warn grub_util_info
110 #define xstrdup strdup
114 kill_trailing_dir(char *path)
116 char *end = path + strlen(path) - 1;
131 trim_newline (char *path)
133 char *end = path + strlen(path) - 1;
139 #define MAX_DISK_CAT 64
142 find_obppath (const char *sysfs_path_orig)
144 char *sysfs_path, *path;
145 size_t path_size = strlen (sysfs_path_orig) + sizeof ("/obppath");
147 sysfs_path = xstrdup (sysfs_path_orig);
148 path = xmalloc (path_size);
157 snprintf(path, path_size, "%s/obppath", sysfs_path);
159 printf("Trying %s\n", path);
162 fd = open(path, O_RDONLY);
165 if (fd < 0 || fstat (fd, &st) < 0)
169 snprintf(path, path_size, "%s/devspec", sysfs_path);
170 fd = open(path, O_RDONLY);
174 if (fd < 0 || fstat (fd, &st) < 0)
178 kill_trailing_dir(sysfs_path);
179 if (!strcmp(sysfs_path, "/sys"))
181 grub_util_info (_("`obppath' not found in parent dirs of `%s',"
182 " no IEEE1275 name discovery"),
191 of_path = xmalloc (size + MAX_DISK_CAT + 1);
192 memset(of_path, 0, size + MAX_DISK_CAT + 1);
193 if (read(fd, of_path, size) < 0)
195 grub_util_info (_("cannot read `%s': %s"), path, strerror (errno));
204 trim_newline(of_path);
212 xrealpath (const char *in)
216 out = xmalloc (PATH_MAX);
217 out = realpath (in, out);
219 out = realpath (in, NULL);
222 grub_util_error (_("failed to get canonical path of `%s'"), in);
227 block_device_get_sysfs_path_and_link(const char *devicenode)
232 size_t tmp_size = strlen (devicenode) + sizeof ("/sys/block/");
233 char *tmp = xmalloc (tmp_size);
235 memcpy (tmp, "/sys/block/", sizeof ("/sys/block/"));
236 strcat (tmp, devicenode);
238 rpath = xrealpath (tmp);
239 rpath2 = xmalloc (strlen (rpath) + sizeof ("/device"));
240 strcpy (rpath2, rpath);
241 strcat (rpath2, "/device");
243 ret = xrealpath (rpath2);
254 return (c >= '0' && c <= '9');
258 trailing_digits (const char *p)
262 end = p + strlen(p) - 1;
265 if (! my_isdigit(*end))
274 __of_path_common(char *sysfs_path,
275 const char *device, int devno)
277 const char *digit_string;
278 char disk[MAX_DISK_CAT];
279 char *of_path = find_obppath(sysfs_path);
284 digit_string = trailing_digits (device);
285 if (*digit_string == '\0')
287 snprintf(disk, sizeof (disk), "/disk@%d", devno);
293 sscanf(digit_string, "%d", &part);
294 snprintf(disk, sizeof (disk), "/disk@%d:%c", devno, 'a' + (part - 1));
296 strcat(of_path, disk);
301 get_basename(char *p)
316 of_path_of_vdisk(const char *sys_devname __attribute__((unused)),
318 const char *devnode __attribute__((unused)),
319 const char *devicenode)
321 char *sysfs_path, *p;
325 sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
326 p = get_basename (sysfs_path);
327 sscanf(p, "vdc-port-%d-%d", &devno, &junk);
328 ret = __of_path_common (sysfs_path, device, devno);
335 of_path_of_ide(const char *sys_devname __attribute__((unused)), const char *device,
336 const char *devnode __attribute__((unused)),
337 const char *devicenode)
339 char *sysfs_path, *p;
343 sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
344 p = get_basename (sysfs_path);
345 sscanf(p, "%d.%d", &chan, &devno);
347 ret = __of_path_common(sysfs_path, device, 2 * chan + devno);
354 of_path_of_nvme(const char *sys_devname __attribute__((unused)),
356 const char *devnode __attribute__((unused)),
357 const char *devicenode)
359 char *sysfs_path, *of_path, disk[MAX_DISK_CAT];
360 const char *digit_string, *part_end;
362 digit_string = trailing_digits (device);
363 part_end = devicenode + strlen (devicenode) - 1;
365 if ((*digit_string != '\0') && (*part_end == 'p'))
367 /* We have a partition number, strip it off. */
371 nvmedev = strdup (devicenode);
376 end = nvmedev + strlen (nvmedev) - 1;
379 sscanf (digit_string, "%d", &part);
380 snprintf (disk, sizeof (disk), "/disk@1:%c", 'a' + (part - 1));
381 sysfs_path = block_device_get_sysfs_path_and_link (nvmedev);
386 /* We do not have the parition. */
387 snprintf (disk, sizeof (disk), "/disk@1");
388 sysfs_path = block_device_get_sysfs_path_and_link (device);
391 of_path = find_obppath (sysfs_path);
394 strcat (of_path, disk);
401 vendor_is_ATA(const char *path)
408 path_size = strlen (path) + sizeof ("/vendor");
410 bufname = xmalloc (path_size);
412 snprintf (bufname, path_size, "%s/vendor", path);
413 fd = open (bufname, O_RDONLY);
415 grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
417 memset(bufcont, 0, sizeof (bufcont));
418 err = read(fd, bufcont, sizeof (bufcont));
420 grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
425 return (memcmp(bufcont, "ATA", 3) == 0);
430 check_hba_identifiers (const char *sysfs_path, int *vendor, int *device_id)
432 char *ed = strstr (sysfs_path, "host");
441 p = xstrdup (sysfs_path);
442 ed = strstr (p, "host");
446 path_size = (strlen (p) + sizeof ("vendor"));
447 path = xmalloc (path_size);
452 snprintf (path, path_size, "%svendor", p);
453 fd = open (path, O_RDONLY);
458 memset (buf, 0, sizeof (buf));
460 if (read (fd, buf, sizeof (buf) - 1) < 0)
464 sscanf (buf, "%x", vendor);
466 snprintf (path, path_size, "%sdevice", p);
467 fd = open (path, O_RDONLY);
472 memset (buf, 0, sizeof (buf));
474 if (read (fd, buf, sizeof (buf) - 1) < 0)
478 sscanf (buf, "%x", device_id);
487 check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
489 char *ed = strstr (sysfs_path, "end_device");
498 /* SAS devices are identified using disk@$PHY_ID */
499 p = xstrdup (sysfs_path);
500 ed = strstr(p, "end_device");
505 while (*q && *q != '/')
509 path_size = (strlen (p) + strlen (ed)
510 + sizeof ("%s/sas_device/%s/phy_identifier"));
511 path = xmalloc (path_size);
512 snprintf (path, path_size, "%s/sas_device/%s/phy_identifier", p, ed);
513 fd = open (path, O_RDONLY);
515 grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
517 memset (phy, 0, sizeof (phy));
518 if (read (fd, phy, sizeof (phy) - 1) < 0)
519 grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
523 sscanf (phy, "%d", tgt);
525 snprintf (path, path_size, "%s/sas_device/%s/sas_address", p, ed);
526 fd = open (path, O_RDONLY);
528 grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
530 memset (phy, 0, sizeof (phy));
531 if (read (fd, phy, sizeof (phy) - 1) < 0)
532 grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
533 sscanf (phy, "%lx", sas_address);
541 of_path_of_scsi(const char *sys_devname __attribute__((unused)), const char *device,
542 const char *devnode __attribute__((unused)),
543 const char *devicenode)
545 const char *p, *digit_string, *disk_name;
546 int host, bus, tgt, lun;
547 unsigned long int sas_address = 0;
548 char *sysfs_path, disk[MAX_DISK_CAT - sizeof ("/fp@0,0")];
551 sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
552 p = get_basename (sysfs_path);
553 sscanf(p, "%d:%d:%d:%d", &host, &bus, &tgt, &lun);
554 check_sas (sysfs_path, &tgt, &sas_address);
556 if (vendor_is_ATA(sysfs_path))
558 of_path = __of_path_common(sysfs_path, device, tgt);
563 of_path = find_obppath(sysfs_path);
567 if (strstr (of_path, "qlc"))
568 strcat (of_path, "/fp@0,0");
570 if (strstr (of_path, "sbus"))
575 digit_string = trailing_digits (device);
576 if (strncmp (of_path, "/vdevice/", sizeof ("/vdevice/") - 1) == 0)
578 unsigned long id = 0x8000 | (tgt << 8) | (bus << 5) | lun;
579 if (*digit_string == '\0')
581 snprintf(disk, sizeof (disk), "/%s@%04lx000000000000", disk_name, id);
587 sscanf(digit_string, "%d", &part);
588 snprintf(disk, sizeof (disk),
589 "/%s@%04lx000000000000:%c", disk_name, id, 'a' + (part - 1));
595 ofpath_sparc_addressing addressing = GRUB_OFPATH_SPARC_TGT_LUN;
596 int vendor = 0, device_id = 0;
599 check_hba_identifiers (sysfs_path, &vendor, &device_id);
601 if (vendor == LSI_VENDOR_ID)
603 struct ofpath_sparc_hba *lsi_hba;
606 * Over time different OF addressing schemes have been supported.
607 * There is no generic addressing scheme that works across
610 for (lsi_hba = sparc_lsi_hba; lsi_hba->device_id; lsi_hba++)
611 if (lsi_hba->device_id == device_id)
613 addressing = lsi_hba->addressing;
618 if (addressing == GRUB_OFPATH_SPARC_WWN_ADDR)
619 optr += snprintf (disk, sizeof (disk), "/%s@w%lx,%x", disk_name,
622 optr += snprintf (disk, sizeof (disk), "/%s@%x,%x", disk_name, tgt,
625 if (*digit_string != '\0')
629 sscanf (digit_string, "%d", &part);
630 snprintf (optr, sizeof (disk) - (optr - disk - 1), ":%c", 'a'
637 sas_id = bus << 16 | tgt << 8 | lun;
639 if (*digit_string == '\0')
641 snprintf(disk, sizeof (disk), "/sas/%s@%x", disk_name, sas_id);
647 sscanf(digit_string, "%d", &part);
648 snprintf(disk, sizeof (disk),
649 "/sas/%s@%x:%c", disk_name, sas_id, 'a' + (part - 1));
657 lunstr = xmalloc (20);
659 lunpart[0] = (lun >> 8) & 0xff;
660 lunpart[1] = lun & 0xff;
661 lunpart[2] = (lun >> 24) & 0xff;
662 lunpart[3] = (lun >> 16) & 0xff;
664 sprintf(lunstr, "%02x%02x%02x%02x00000000", lunpart[0], lunpart[1], lunpart[2], lunpart[3]);
665 long int longlun = atol(lunstr);
667 if (*digit_string == '\0')
669 snprintf(disk, sizeof (disk), "/sas/%s@%lx,%lu", disk_name, sas_address, longlun);
675 sscanf(digit_string, "%d", &part);
676 snprintf(disk, sizeof (disk),
677 "/sas/%s@%lx,%lu:%c", disk_name, sas_address, longlun, 'a' + (part - 1));
683 strcat(of_path, disk);
691 strip_trailing_digits (const char *p)
696 end = new + strlen(new) - 1;
699 if (! my_isdigit(*end))
708 grub_util_devname_to_ofpath (const char *sys_devname)
710 char *name_buf, *device, *devnode, *devicenode, *ofpath;
712 name_buf = xrealpath (sys_devname);
714 device = get_basename (name_buf);
715 devnode = strip_trailing_digits (name_buf);
716 devicenode = strip_trailing_digits (device);
718 if (device[0] == 'h' && device[1] == 'd')
719 ofpath = of_path_of_ide(name_buf, device, devnode, devicenode);
720 else if (device[0] == 's'
721 && (device[1] == 'd' || device[1] == 'r'))
722 ofpath = of_path_of_scsi(name_buf, device, devnode, devicenode);
723 else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i'
724 && device[3] == 's' && device[4] == 'k')
725 ofpath = of_path_of_vdisk(name_buf, device, devnode, devicenode);
726 else if (device[0] == 'f' && device[1] == 'd'
727 && device[2] == '0' && device[3] == '\0')
728 /* All the models I've seen have a devalias "floppy".
729 New models have no floppy at all. */
730 ofpath = xstrdup ("floppy");
731 else if (device[0] == 'n' && device[1] == 'v' && device[2] == 'm'
733 ofpath = of_path_of_nvme (name_buf, device, devnode, devicenode);
736 grub_util_warn (_("unknown device type %s"), device);
747 #ifdef OFPATH_STANDALONE
748 int main(int argc, char **argv)
754 printf(_("Usage: %s DEVICE\n"), argv[0]);
758 of_path = grub_util_devname_to_ofpath (argv[1]);
760 printf("%s\n", of_path);