2009-04-22 09:57:39 +00:00
|
|
|
/* ofpath.c - calculate OpenFirmware path names given an OS device */
|
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2013-10-08 16:25:49 +00:00
|
|
|
* Copyright (C) 2009, 2011,2012, 2013 Free Software Foundation, Inc.
|
2009-04-22 09:57:39 +00:00
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#undef OFPATH_STANDALONE
|
|
|
|
|
|
|
|
#ifndef OFPATH_STANDALONE
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/util/misc.h>
|
|
|
|
#include <grub/util/ofpath.h>
|
2011-11-11 23:34:14 +00:00
|
|
|
#include <grub/i18n.h>
|
2009-04-22 09:57:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#ifdef OFPATH_STANDALONE
|
|
|
|
#define xmalloc malloc
|
|
|
|
void
|
|
|
|
grub_util_error (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fprintf (stderr, "ofpath: error: ");
|
|
|
|
va_start (ap, fmt);
|
|
|
|
vfprintf (stderr, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
void
|
|
|
|
grub_util_info (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
fprintf (stderr, "ofpath: info: ");
|
|
|
|
va_start (ap, fmt);
|
|
|
|
vfprintf (stderr, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
}
|
|
|
|
|
2012-06-04 20:37:39 +00:00
|
|
|
#define grub_util_warn grub_util_info
|
2011-11-11 23:34:14 +00:00
|
|
|
#define _(x) x
|
2012-06-04 20:37:39 +00:00
|
|
|
#define xstrdup strdup
|
2009-04-22 09:57:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
kill_trailing_dir(char *path)
|
|
|
|
{
|
|
|
|
char *end = path + strlen(path) - 1;
|
|
|
|
|
|
|
|
while (end >= path)
|
|
|
|
{
|
|
|
|
if (*end != '/')
|
|
|
|
{
|
|
|
|
end--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*end = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
trim_newline (char *path)
|
|
|
|
{
|
|
|
|
char *end = path + strlen(path) - 1;
|
|
|
|
|
|
|
|
while (*end == '\n')
|
|
|
|
*end-- = '\0';
|
|
|
|
}
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
#define MAX_DISK_CAT 64
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
static char *
|
|
|
|
find_obppath (const char *sysfs_path_orig)
|
2009-04-22 09:57:39 +00:00
|
|
|
{
|
|
|
|
char *sysfs_path, *path;
|
2012-01-29 17:51:21 +00:00
|
|
|
size_t path_size = strlen (sysfs_path_orig) + sizeof ("/obppath");
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
sysfs_path = xstrdup (sysfs_path_orig);
|
|
|
|
path = xmalloc (path_size);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int fd;
|
2012-01-29 17:51:21 +00:00
|
|
|
char *of_path;
|
|
|
|
struct stat st;
|
|
|
|
size_t size;
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
snprintf(path, path_size, "%s/obppath", sysfs_path);
|
2009-04-22 09:57:39 +00:00
|
|
|
#if 0
|
|
|
|
printf("Trying %s\n", path);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
2012-06-04 20:37:39 +00:00
|
|
|
if (fd < 0 || fstat (fd, &st) < 0)
|
|
|
|
{
|
2015-01-27 17:29:00 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
close (fd);
|
2012-06-04 20:37:39 +00:00
|
|
|
snprintf(path, path_size, "%s/devspec", sysfs_path);
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
}
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
if (fd < 0 || fstat (fd, &st) < 0)
|
2009-04-22 09:57:39 +00:00
|
|
|
{
|
2015-01-26 18:41:29 +00:00
|
|
|
if (fd >= 0)
|
|
|
|
close (fd);
|
2009-04-22 09:57:39 +00:00
|
|
|
kill_trailing_dir(sysfs_path);
|
|
|
|
if (!strcmp(sysfs_path, "/sys"))
|
2011-12-23 18:25:24 +00:00
|
|
|
{
|
2013-01-13 21:45:16 +00:00
|
|
|
grub_util_info (_("`obppath' not found in parent dirs of `%s',"
|
2011-12-23 18:25:24 +00:00
|
|
|
" no IEEE1275 name discovery"),
|
|
|
|
sysfs_path_orig);
|
|
|
|
free (path);
|
|
|
|
free (sysfs_path);
|
2012-01-29 17:51:21 +00:00
|
|
|
return NULL;
|
2011-12-23 18:25:24 +00:00
|
|
|
}
|
2009-04-22 09:57:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-01-29 17:51:21 +00:00
|
|
|
size = st.st_size;
|
|
|
|
of_path = xmalloc (size + MAX_DISK_CAT + 1);
|
|
|
|
memset(of_path, 0, size + MAX_DISK_CAT + 1);
|
2013-11-29 04:46:19 +00:00
|
|
|
if (read(fd, of_path, size) < 0)
|
|
|
|
{
|
|
|
|
grub_util_info (_("cannot read `%s': %s"), path, strerror (errno));
|
|
|
|
close(fd);
|
2015-01-26 18:41:29 +00:00
|
|
|
free (path);
|
|
|
|
free (of_path);
|
|
|
|
free (sysfs_path);
|
2013-11-29 04:46:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-04-22 09:57:39 +00:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
trim_newline(of_path);
|
2012-01-29 17:51:21 +00:00
|
|
|
free (path);
|
|
|
|
free (sysfs_path);
|
|
|
|
return of_path;
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
static char *
|
|
|
|
xrealpath (const char *in)
|
2009-04-22 09:57:39 +00:00
|
|
|
{
|
2012-01-29 17:51:21 +00:00
|
|
|
char *out;
|
|
|
|
#ifdef PATH_MAX
|
|
|
|
out = xmalloc (PATH_MAX);
|
|
|
|
out = realpath (in, out);
|
|
|
|
#else
|
|
|
|
out = realpath (in, NULL);
|
|
|
|
#endif
|
|
|
|
if (!out)
|
2013-01-13 21:45:16 +00:00
|
|
|
grub_util_error (_("failed to get canonical path of `%s'"), in);
|
2012-01-29 17:51:21 +00:00
|
|
|
return out;
|
|
|
|
}
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
static char *
|
|
|
|
block_device_get_sysfs_path_and_link(const char *devicenode)
|
|
|
|
{
|
|
|
|
char *rpath;
|
|
|
|
char *rpath2;
|
|
|
|
char *ret;
|
|
|
|
size_t tmp_size = strlen (devicenode) + sizeof ("/sys/block/");
|
|
|
|
char *tmp = xmalloc (tmp_size);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
memcpy (tmp, "/sys/block/", sizeof ("/sys/block/"));
|
|
|
|
strcat (tmp, devicenode);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
rpath = xrealpath (tmp);
|
|
|
|
rpath2 = xmalloc (strlen (rpath) + sizeof ("/device"));
|
|
|
|
strcpy (rpath2, rpath);
|
|
|
|
strcat (rpath2, "/device");
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
ret = xrealpath (rpath2);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
free (tmp);
|
2009-04-22 09:57:39 +00:00
|
|
|
free (rpath);
|
2012-01-29 17:51:21 +00:00
|
|
|
free (rpath2);
|
|
|
|
return ret;
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
|
2012-03-02 10:21:01 +00:00
|
|
|
static inline int
|
|
|
|
my_isdigit (int c)
|
|
|
|
{
|
|
|
|
return (c >= '0' && c <= '9');
|
|
|
|
}
|
|
|
|
|
2009-04-22 09:57:39 +00:00
|
|
|
static const char *
|
|
|
|
trailing_digits (const char *p)
|
|
|
|
{
|
|
|
|
const char *end;
|
|
|
|
|
|
|
|
end = p + strlen(p) - 1;
|
|
|
|
while (end >= p)
|
|
|
|
{
|
2012-03-02 10:21:01 +00:00
|
|
|
if (! my_isdigit(*end))
|
2009-04-22 09:57:39 +00:00
|
|
|
break;
|
|
|
|
end--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return end + 1;
|
|
|
|
}
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
static char *
|
|
|
|
__of_path_common(char *sysfs_path,
|
2009-04-22 09:57:39 +00:00
|
|
|
const char *device, int devno)
|
|
|
|
{
|
|
|
|
const char *digit_string;
|
2012-01-29 17:51:21 +00:00
|
|
|
char disk[MAX_DISK_CAT];
|
|
|
|
char *of_path = find_obppath(sysfs_path);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
if (!of_path)
|
|
|
|
return NULL;
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
digit_string = trailing_digits (device);
|
|
|
|
if (*digit_string == '\0')
|
|
|
|
{
|
2012-01-29 17:51:21 +00:00
|
|
|
snprintf(disk, sizeof (disk), "/disk@%d", devno);
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int part;
|
|
|
|
|
|
|
|
sscanf(digit_string, "%d", &part);
|
2012-01-29 17:51:21 +00:00
|
|
|
snprintf(disk, sizeof (disk), "/disk@%d:%c", devno, 'a' + (part - 1));
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
strcat(of_path, disk);
|
2012-01-29 17:51:21 +00:00
|
|
|
return of_path;
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_basename(char *p)
|
|
|
|
{
|
|
|
|
char *ret = p;
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
if (*p == '/')
|
|
|
|
ret = p + 1;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
static char *
|
2012-02-29 13:10:04 +00:00
|
|
|
of_path_of_vdisk(const char *sys_devname __attribute__((unused)),
|
2009-12-20 13:09:16 +00:00
|
|
|
const char *device,
|
|
|
|
const char *devnode __attribute__((unused)),
|
|
|
|
const char *devicenode)
|
2009-04-22 09:57:39 +00:00
|
|
|
{
|
|
|
|
char *sysfs_path, *p;
|
|
|
|
int devno, junk;
|
2012-01-29 17:51:21 +00:00
|
|
|
char *ret;
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
|
2009-04-22 09:57:39 +00:00
|
|
|
p = get_basename (sysfs_path);
|
|
|
|
sscanf(p, "vdc-port-%d-%d", &devno, &junk);
|
2012-01-29 17:51:21 +00:00
|
|
|
ret = __of_path_common (sysfs_path, device, devno);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
free (sysfs_path);
|
2011-12-23 18:25:24 +00:00
|
|
|
return ret;
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
static char *
|
2012-02-29 13:10:04 +00:00
|
|
|
of_path_of_ide(const char *sys_devname __attribute__((unused)), const char *device,
|
2009-12-20 13:09:16 +00:00
|
|
|
const char *devnode __attribute__((unused)),
|
|
|
|
const char *devicenode)
|
2009-04-22 09:57:39 +00:00
|
|
|
{
|
|
|
|
char *sysfs_path, *p;
|
|
|
|
int chan, devno;
|
2012-01-29 17:51:21 +00:00
|
|
|
char *ret;
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
|
2009-04-22 09:57:39 +00:00
|
|
|
p = get_basename (sysfs_path);
|
|
|
|
sscanf(p, "%d.%d", &chan, &devno);
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
ret = __of_path_common(sysfs_path, device, 2 * chan + devno);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
free (sysfs_path);
|
2011-12-23 18:25:24 +00:00
|
|
|
return ret;
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
vendor_is_ATA(const char *path)
|
|
|
|
{
|
|
|
|
int fd, err;
|
2012-01-29 17:51:21 +00:00
|
|
|
char *bufname;
|
|
|
|
char bufcont[3];
|
|
|
|
size_t path_size;
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
path_size = strlen (path) + sizeof ("/vendor");
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
bufname = xmalloc (path_size);
|
|
|
|
|
2012-02-05 10:07:33 +00:00
|
|
|
snprintf (bufname, path_size, "%s/vendor", path);
|
|
|
|
fd = open (bufname, O_RDONLY);
|
2009-04-22 09:57:39 +00:00
|
|
|
if (fd < 0)
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
memset(bufcont, 0, sizeof (bufcont));
|
|
|
|
err = read(fd, bufcont, sizeof (bufcont));
|
2009-04-22 09:57:39 +00:00
|
|
|
if (err < 0)
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
close(fd);
|
2012-01-29 17:51:21 +00:00
|
|
|
free (bufname);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
return (memcmp(bufcont, "ATA", 3) == 0);
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-01-27 15:49:49 +00:00
|
|
|
check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
|
2009-04-22 09:57:39 +00:00
|
|
|
{
|
|
|
|
char *ed = strstr (sysfs_path, "end_device");
|
|
|
|
char *p, *q, *path;
|
2013-04-27 20:03:13 +00:00
|
|
|
char phy[21];
|
2009-04-22 09:57:39 +00:00
|
|
|
int fd;
|
2012-01-29 17:51:21 +00:00
|
|
|
size_t path_size;
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
if (!ed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* SAS devices are identified using disk@$PHY_ID */
|
2015-01-27 15:49:49 +00:00
|
|
|
p = xstrdup (sysfs_path);
|
2009-04-22 09:57:39 +00:00
|
|
|
ed = strstr(p, "end_device");
|
2015-01-27 15:49:49 +00:00
|
|
|
if (!ed)
|
|
|
|
return;
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
q = ed;
|
|
|
|
while (*q && *q != '/')
|
|
|
|
q++;
|
|
|
|
*q = '\0';
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
path_size = (strlen (p) + strlen (ed)
|
2012-06-02 12:53:23 +00:00
|
|
|
+ sizeof ("%s/sas_device/%s/phy_identifier"));
|
2012-01-29 17:51:21 +00:00
|
|
|
path = xmalloc (path_size);
|
2012-06-02 12:53:23 +00:00
|
|
|
snprintf (path, path_size, "%s/sas_device/%s/phy_identifier", p, ed);
|
2012-02-05 10:07:33 +00:00
|
|
|
fd = open (path, O_RDONLY);
|
2009-04-22 09:57:39 +00:00
|
|
|
if (fd < 0)
|
2012-02-05 10:07:33 +00:00
|
|
|
grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
memset (phy, 0, sizeof (phy));
|
2013-11-29 04:46:19 +00:00
|
|
|
if (read (fd, phy, sizeof (phy) - 1) < 0)
|
|
|
|
grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
|
|
|
|
|
2013-04-27 20:03:13 +00:00
|
|
|
close (fd);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
sscanf (phy, "%d", tgt);
|
|
|
|
|
2013-04-27 20:03:13 +00:00
|
|
|
snprintf (path, path_size, "%s/sas_device/%s/sas_address", p, ed);
|
|
|
|
fd = open (path, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2013-11-29 04:46:19 +00:00
|
|
|
grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
|
2013-04-27 20:03:13 +00:00
|
|
|
|
|
|
|
memset (phy, 0, sizeof (phy));
|
2013-11-29 04:46:19 +00:00
|
|
|
if (read (fd, phy, sizeof (phy) - 1) < 0)
|
|
|
|
grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
|
2013-04-27 20:03:13 +00:00
|
|
|
sscanf (phy, "%lx", sas_address);
|
|
|
|
|
2009-04-22 09:57:39 +00:00
|
|
|
free (path);
|
|
|
|
free (p);
|
2011-06-23 20:18:31 +00:00
|
|
|
close (fd);
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
static char *
|
2012-02-29 13:10:04 +00:00
|
|
|
of_path_of_scsi(const char *sys_devname __attribute__((unused)), const char *device,
|
2009-12-20 13:09:16 +00:00
|
|
|
const char *devnode __attribute__((unused)),
|
|
|
|
const char *devicenode)
|
2009-04-22 09:57:39 +00:00
|
|
|
{
|
|
|
|
const char *p, *digit_string, *disk_name;
|
|
|
|
int host, bus, tgt, lun;
|
2013-04-27 20:03:13 +00:00
|
|
|
unsigned long int sas_address;
|
2012-01-29 17:51:21 +00:00
|
|
|
char *sysfs_path, disk[MAX_DISK_CAT - sizeof ("/fp@0,0")];
|
|
|
|
char *of_path;
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
|
2009-04-22 09:57:39 +00:00
|
|
|
p = get_basename (sysfs_path);
|
|
|
|
sscanf(p, "%d:%d:%d:%d", &host, &bus, &tgt, &lun);
|
2013-04-27 20:03:13 +00:00
|
|
|
check_sas (sysfs_path, &tgt, &sas_address);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
if (vendor_is_ATA(sysfs_path))
|
|
|
|
{
|
2012-01-29 17:51:21 +00:00
|
|
|
of_path = __of_path_common(sysfs_path, device, tgt);
|
2009-04-22 09:57:39 +00:00
|
|
|
free (sysfs_path);
|
2012-01-29 17:51:21 +00:00
|
|
|
return of_path;
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 17:51:21 +00:00
|
|
|
of_path = find_obppath(sysfs_path);
|
2009-04-22 09:57:39 +00:00
|
|
|
free (sysfs_path);
|
2012-01-29 17:51:21 +00:00
|
|
|
if (!of_path)
|
|
|
|
return NULL;
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
if (strstr (of_path, "qlc"))
|
|
|
|
strcat (of_path, "/fp@0,0");
|
|
|
|
|
|
|
|
if (strstr (of_path, "sbus"))
|
|
|
|
disk_name = "sd";
|
|
|
|
else
|
|
|
|
disk_name = "disk";
|
|
|
|
|
|
|
|
digit_string = trailing_digits (device);
|
2012-06-04 20:37:39 +00:00
|
|
|
if (strncmp (of_path, "/vdevice/", sizeof ("/vdevice/") - 1) == 0)
|
2009-04-22 09:57:39 +00:00
|
|
|
{
|
2012-06-04 20:37:39 +00:00
|
|
|
unsigned long id = 0x8000 | (tgt << 8) | (bus << 5) | lun;
|
|
|
|
if (*digit_string == '\0')
|
|
|
|
{
|
2012-06-04 21:15:54 +00:00
|
|
|
snprintf(disk, sizeof (disk), "/%s@%04lx000000000000", disk_name, id);
|
2012-06-04 20:37:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int part;
|
|
|
|
|
|
|
|
sscanf(digit_string, "%d", &part);
|
|
|
|
snprintf(disk, sizeof (disk),
|
2012-06-04 21:15:54 +00:00
|
|
|
"/%s@%04lx000000000000:%c", disk_name, id, 'a' + (part - 1));
|
2012-06-04 20:37:39 +00:00
|
|
|
}
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-27 20:03:13 +00:00
|
|
|
if (lun == 0)
|
|
|
|
{
|
|
|
|
int sas_id = 0;
|
|
|
|
sas_id = bus << 16 | tgt << 8 | lun;
|
|
|
|
|
|
|
|
if (*digit_string == '\0')
|
|
|
|
{
|
|
|
|
snprintf(disk, sizeof (disk), "/sas/%s@%x", disk_name, sas_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int part;
|
|
|
|
|
|
|
|
sscanf(digit_string, "%d", &part);
|
|
|
|
snprintf(disk, sizeof (disk),
|
|
|
|
"/sas/%s@%x:%c", disk_name, sas_id, 'a' + (part - 1));
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 20:37:39 +00:00
|
|
|
else
|
2013-04-27 20:03:13 +00:00
|
|
|
{
|
|
|
|
char *lunstr;
|
|
|
|
int lunpart[4];
|
|
|
|
|
|
|
|
lunstr = xmalloc (20);
|
|
|
|
|
|
|
|
lunpart[0] = (lun >> 8) & 0xff;
|
|
|
|
lunpart[1] = lun & 0xff;
|
|
|
|
lunpart[2] = (lun >> 24) & 0xff;
|
|
|
|
lunpart[3] = (lun >> 16) & 0xff;
|
|
|
|
|
|
|
|
sprintf(lunstr, "%02x%02x%02x%02x00000000", lunpart[0], lunpart[1], lunpart[2], lunpart[3]);
|
|
|
|
long int longlun = atol(lunstr);
|
|
|
|
|
|
|
|
if (*digit_string == '\0')
|
|
|
|
{
|
|
|
|
snprintf(disk, sizeof (disk), "/sas/%s@%lx,%lu", disk_name, sas_address, longlun);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int part;
|
|
|
|
|
|
|
|
sscanf(digit_string, "%d", &part);
|
|
|
|
snprintf(disk, sizeof (disk),
|
|
|
|
"/sas/%s@%lx,%lu:%c", disk_name, sas_address, longlun, 'a' + (part - 1));
|
|
|
|
}
|
2015-01-26 18:41:29 +00:00
|
|
|
free (lunstr);
|
2013-04-27 20:03:13 +00:00
|
|
|
}
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
strcat(of_path, disk);
|
2012-01-29 17:51:21 +00:00
|
|
|
return of_path;
|
2009-04-22 09:57:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
strip_trailing_digits (const char *p)
|
|
|
|
{
|
|
|
|
char *new, *end;
|
|
|
|
|
|
|
|
new = strdup (p);
|
|
|
|
end = new + strlen(new) - 1;
|
|
|
|
while (end >= new)
|
|
|
|
{
|
2012-03-02 10:21:01 +00:00
|
|
|
if (! my_isdigit(*end))
|
2009-04-22 09:57:39 +00:00
|
|
|
break;
|
|
|
|
*end-- = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2012-02-29 13:10:04 +00:00
|
|
|
grub_util_devname_to_ofpath (const char *sys_devname)
|
2009-04-22 09:57:39 +00:00
|
|
|
{
|
|
|
|
char *name_buf, *device, *devnode, *devicenode, *ofpath;
|
|
|
|
|
2012-02-29 13:10:04 +00:00
|
|
|
name_buf = xrealpath (sys_devname);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
2011-01-12 18:58:32 +00:00
|
|
|
device = get_basename (name_buf);
|
|
|
|
devnode = strip_trailing_digits (name_buf);
|
2009-04-22 09:57:39 +00:00
|
|
|
devicenode = strip_trailing_digits (device);
|
|
|
|
|
|
|
|
if (device[0] == 'h' && device[1] == 'd')
|
2012-01-29 17:51:21 +00:00
|
|
|
ofpath = of_path_of_ide(name_buf, device, devnode, devicenode);
|
2009-04-22 09:57:39 +00:00
|
|
|
else if (device[0] == 's'
|
|
|
|
&& (device[1] == 'd' || device[1] == 'r'))
|
2012-01-29 17:51:21 +00:00
|
|
|
ofpath = of_path_of_scsi(name_buf, device, devnode, devicenode);
|
2009-04-22 09:57:39 +00:00
|
|
|
else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i'
|
|
|
|
&& device[3] == 's' && device[4] == 'k')
|
2012-01-29 17:51:21 +00:00
|
|
|
ofpath = of_path_of_vdisk(name_buf, device, devnode, devicenode);
|
2011-01-12 18:58:32 +00:00
|
|
|
else if (device[0] == 'f' && device[1] == 'd'
|
|
|
|
&& device[2] == '0' && device[3] == '\0')
|
|
|
|
/* All the models I've seen have a devalias "floppy".
|
|
|
|
New models have no floppy at all. */
|
2012-01-29 17:51:21 +00:00
|
|
|
ofpath = xstrdup ("floppy");
|
2011-01-12 18:58:32 +00:00
|
|
|
else
|
2011-12-23 18:25:24 +00:00
|
|
|
{
|
2015-05-13 06:47:17 +00:00
|
|
|
grub_util_warn (_("unknown device type %s"), device);
|
2015-01-26 18:41:29 +00:00
|
|
|
ofpath = NULL;
|
2011-12-23 18:25:24 +00:00
|
|
|
}
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
free (devnode);
|
|
|
|
free (devicenode);
|
|
|
|
free (name_buf);
|
|
|
|
|
|
|
|
return ofpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef OFPATH_STANDALONE
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *of_path;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
2011-11-11 23:34:14 +00:00
|
|
|
printf(_("Usage: %s DEVICE\n"), argv[0]);
|
2009-04-22 09:57:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
of_path = grub_util_devname_to_ofpath (argv[1]);
|
2012-01-29 17:51:21 +00:00
|
|
|
if (of_path)
|
|
|
|
printf("%s\n", of_path);
|
2011-06-23 20:18:31 +00:00
|
|
|
free (of_path);
|
2009-04-22 09:57:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|