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);
123 if (fd < 0 || fstat (fd, &st) < 0)
127 snprintf(path, path_size, "%s/devspec", sysfs_path);
128 fd = open(path, O_RDONLY);
131 if (fd < 0 || fstat (fd, &st) < 0)
135 kill_trailing_dir(sysfs_path);
136 if (!strcmp(sysfs_path, "/sys"))
138 grub_util_info (_("`obppath' not found in parent dirs of `%s',"
139 " no IEEE1275 name discovery"),
148 of_path = xmalloc (size + MAX_DISK_CAT + 1);
149 memset(of_path, 0, size + MAX_DISK_CAT + 1);
150 if (read(fd, of_path, size) < 0)
152 grub_util_info (_("cannot read `%s': %s"), path, strerror (errno));
161 trim_newline(of_path);
169 xrealpath (const char *in)
173 out = xmalloc (PATH_MAX);
174 out = realpath (in, out);
176 out = realpath (in, NULL);
179 grub_util_error (_("failed to get canonical path of `%s'"), in);
184 block_device_get_sysfs_path_and_link(const char *devicenode)
189 size_t tmp_size = strlen (devicenode) + sizeof ("/sys/block/");
190 char *tmp = xmalloc (tmp_size);
192 memcpy (tmp, "/sys/block/", sizeof ("/sys/block/"));
193 strcat (tmp, devicenode);
195 rpath = xrealpath (tmp);
196 rpath2 = xmalloc (strlen (rpath) + sizeof ("/device"));
197 strcpy (rpath2, rpath);
198 strcat (rpath2, "/device");
200 ret = xrealpath (rpath2);
211 return (c >= '0' && c <= '9');
215 trailing_digits (const char *p)
219 end = p + strlen(p) - 1;
222 if (! my_isdigit(*end))
231 __of_path_common(char *sysfs_path,
232 const char *device, int devno)
234 const char *digit_string;
235 char disk[MAX_DISK_CAT];
236 char *of_path = find_obppath(sysfs_path);
241 digit_string = trailing_digits (device);
242 if (*digit_string == '\0')
244 snprintf(disk, sizeof (disk), "/disk@%d", devno);
250 sscanf(digit_string, "%d", &part);
251 snprintf(disk, sizeof (disk), "/disk@%d:%c", devno, 'a' + (part - 1));
253 strcat(of_path, disk);
258 get_basename(char *p)
273 of_path_of_vdisk(const char *sys_devname __attribute__((unused)),
275 const char *devnode __attribute__((unused)),
276 const char *devicenode)
278 char *sysfs_path, *p;
282 sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
283 p = get_basename (sysfs_path);
284 sscanf(p, "vdc-port-%d-%d", &devno, &junk);
285 ret = __of_path_common (sysfs_path, device, devno);
292 of_path_of_ide(const char *sys_devname __attribute__((unused)), const char *device,
293 const char *devnode __attribute__((unused)),
294 const char *devicenode)
296 char *sysfs_path, *p;
300 sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
301 p = get_basename (sysfs_path);
302 sscanf(p, "%d.%d", &chan, &devno);
304 ret = __of_path_common(sysfs_path, device, 2 * chan + devno);
311 vendor_is_ATA(const char *path)
318 path_size = strlen (path) + sizeof ("/vendor");
320 bufname = xmalloc (path_size);
322 snprintf (bufname, path_size, "%s/vendor", path);
323 fd = open (bufname, O_RDONLY);
325 grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
327 memset(bufcont, 0, sizeof (bufcont));
328 err = read(fd, bufcont, sizeof (bufcont));
330 grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
335 return (memcmp(bufcont, "ATA", 3) == 0);
339 check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
341 char *ed = strstr (sysfs_path, "end_device");
350 /* SAS devices are identified using disk@$PHY_ID */
351 p = xstrdup (sysfs_path);
352 ed = strstr(p, "end_device");
357 while (*q && *q != '/')
361 path_size = (strlen (p) + strlen (ed)
362 + sizeof ("%s/sas_device/%s/phy_identifier"));
363 path = xmalloc (path_size);
364 snprintf (path, path_size, "%s/sas_device/%s/phy_identifier", p, ed);
365 fd = open (path, O_RDONLY);
367 grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
369 memset (phy, 0, sizeof (phy));
370 if (read (fd, phy, sizeof (phy) - 1) < 0)
371 grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
375 sscanf (phy, "%d", tgt);
377 snprintf (path, path_size, "%s/sas_device/%s/sas_address", p, ed);
378 fd = open (path, O_RDONLY);
380 grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
382 memset (phy, 0, sizeof (phy));
383 if (read (fd, phy, sizeof (phy) - 1) < 0)
384 grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
385 sscanf (phy, "%lx", sas_address);
393 of_path_of_scsi(const char *sys_devname __attribute__((unused)), const char *device,
394 const char *devnode __attribute__((unused)),
395 const char *devicenode)
397 const char *p, *digit_string, *disk_name;
398 int host, bus, tgt, lun;
399 unsigned long int sas_address;
400 char *sysfs_path, disk[MAX_DISK_CAT - sizeof ("/fp@0,0")];
403 sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
404 p = get_basename (sysfs_path);
405 sscanf(p, "%d:%d:%d:%d", &host, &bus, &tgt, &lun);
406 check_sas (sysfs_path, &tgt, &sas_address);
408 if (vendor_is_ATA(sysfs_path))
410 of_path = __of_path_common(sysfs_path, device, tgt);
415 of_path = find_obppath(sysfs_path);
420 if (strstr (of_path, "qlc"))
421 strcat (of_path, "/fp@0,0");
423 if (strstr (of_path, "sbus"))
428 digit_string = trailing_digits (device);
429 if (strncmp (of_path, "/vdevice/", sizeof ("/vdevice/") - 1) == 0)
431 unsigned long id = 0x8000 | (tgt << 8) | (bus << 5) | lun;
432 if (*digit_string == '\0')
434 snprintf(disk, sizeof (disk), "/%s@%04lx000000000000", disk_name, id);
440 sscanf(digit_string, "%d", &part);
441 snprintf(disk, sizeof (disk),
442 "/%s@%04lx000000000000:%c", disk_name, id, 'a' + (part - 1));
450 sas_id = bus << 16 | tgt << 8 | lun;
452 if (*digit_string == '\0')
454 snprintf(disk, sizeof (disk), "/sas/%s@%x", disk_name, sas_id);
460 sscanf(digit_string, "%d", &part);
461 snprintf(disk, sizeof (disk),
462 "/sas/%s@%x:%c", disk_name, sas_id, 'a' + (part - 1));
470 lunstr = xmalloc (20);
472 lunpart[0] = (lun >> 8) & 0xff;
473 lunpart[1] = lun & 0xff;
474 lunpart[2] = (lun >> 24) & 0xff;
475 lunpart[3] = (lun >> 16) & 0xff;
477 sprintf(lunstr, "%02x%02x%02x%02x00000000", lunpart[0], lunpart[1], lunpart[2], lunpart[3]);
478 long int longlun = atol(lunstr);
480 if (*digit_string == '\0')
482 snprintf(disk, sizeof (disk), "/sas/%s@%lx,%lu", disk_name, sas_address, longlun);
488 sscanf(digit_string, "%d", &part);
489 snprintf(disk, sizeof (disk),
490 "/sas/%s@%lx,%lu:%c", disk_name, sas_address, longlun, 'a' + (part - 1));
495 strcat(of_path, disk);
500 strip_trailing_digits (const char *p)
505 end = new + strlen(new) - 1;
508 if (! my_isdigit(*end))
517 grub_util_devname_to_ofpath (const char *sys_devname)
519 char *name_buf, *device, *devnode, *devicenode, *ofpath;
521 name_buf = xrealpath (sys_devname);
523 device = get_basename (name_buf);
524 devnode = strip_trailing_digits (name_buf);
525 devicenode = strip_trailing_digits (device);
527 if (device[0] == 'h' && device[1] == 'd')
528 ofpath = of_path_of_ide(name_buf, device, devnode, devicenode);
529 else if (device[0] == 's'
530 && (device[1] == 'd' || device[1] == 'r'))
531 ofpath = of_path_of_scsi(name_buf, device, devnode, devicenode);
532 else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i'
533 && device[3] == 's' && device[4] == 'k')
534 ofpath = of_path_of_vdisk(name_buf, device, devnode, devicenode);
535 else if (device[0] == 'f' && device[1] == 'd'
536 && device[2] == '0' && device[3] == '\0')
537 /* All the models I've seen have a devalias "floppy".
538 New models have no floppy at all. */
539 ofpath = xstrdup ("floppy");
542 grub_util_warn (_("unknown device type %s"), device);
553 #ifdef OFPATH_STANDALONE
554 int main(int argc, char **argv)
560 printf(_("Usage: %s DEVICE\n"), argv[0]);
564 of_path = grub_util_devname_to_ofpath (argv[1]);
566 printf("%s\n", of_path);