Move OS-dependent files to grub-core/osdep and document it.
This commit is contained in:
parent
a5b0365ab2
commit
672fa55e81
30 changed files with 39 additions and 30 deletions
102
grub-core/osdep/apple/getroot.c
Normal file
102
grub-core/osdep/apple/getroot.c
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include <grub/types.h>
|
||||
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
# include <sys/disk.h>
|
||||
# include <sys/param.h>
|
||||
# include <sys/sysctl.h>
|
||||
# include <sys/mount.h>
|
||||
|
||||
char *
|
||||
grub_util_part_to_disk (const char *os_dev, struct stat *st,
|
||||
int *is_part)
|
||||
{
|
||||
char *path = xstrdup (os_dev);
|
||||
if (strncmp ("/dev/", path, 5) == 0)
|
||||
{
|
||||
char *p;
|
||||
for (p = path + 5; *p; ++p)
|
||||
if (grub_isdigit(*p))
|
||||
{
|
||||
p = strpbrk (p, "sp");
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
*p = '\0';
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
|
||||
{
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
|
||||
enum grub_dev_abstraction_types ab __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
grub_disk_addr_t
|
||||
grub_util_find_partition_start_os (const char *dev __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
271
grub-core/osdep/aros/getroot.c
Normal file
271
grub-core/osdep/aros/getroot.c
Normal file
|
@ -0,0 +1,271 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <dos/dos.h>
|
||||
#include <dos/filesystem.h>
|
||||
#include <dos/exall.h>
|
||||
#include <proto/dos.h>
|
||||
#include <proto/exec.h>
|
||||
#include <devices/trackdisk.h>
|
||||
|
||||
char *
|
||||
grub_util_part_to_disk (const char *dev,
|
||||
struct stat *st __attribute__ ((unused)),
|
||||
int *is_part)
|
||||
{
|
||||
const char *p;
|
||||
char *dname;
|
||||
char *ret;
|
||||
struct DosList *dl;
|
||||
struct DeviceNode *dn;
|
||||
struct FileSysStartupMsg *fm;
|
||||
struct DosEnvec *envec;
|
||||
|
||||
if (!dev[0])
|
||||
return xstrdup (dev);
|
||||
|
||||
p = dev + strlen (dev) - 1;
|
||||
if (*p != ':')
|
||||
return xstrdup (dev);
|
||||
dname = xmalloc (p - dev + 1);
|
||||
memcpy (dname, dev, p - dev);
|
||||
dname[p - dev] = '\0';
|
||||
dl = LockDosList(LDF_READ);
|
||||
|
||||
if (!dl)
|
||||
{
|
||||
free (dname);
|
||||
return xstrdup (dev);
|
||||
}
|
||||
|
||||
dn = (struct DeviceNode *) FindDosEntry (dl, (unsigned char *) dname,
|
||||
LDF_DEVICES);
|
||||
UnLockDosList (LDF_READ);
|
||||
free (dname);
|
||||
if (!dn)
|
||||
return xstrdup (dev);
|
||||
|
||||
fm = (struct FileSysStartupMsg *) BADDR(dn->dn_Startup);
|
||||
envec = (struct DosEnvec *) fm->fssm_Environ;
|
||||
|
||||
if (envec->de_LowCyl == 0)
|
||||
return xstrdup (dev);
|
||||
|
||||
*is_part = 1;
|
||||
|
||||
ret = xasprintf ("%s/%lx/%lx:", fm->fssm_Device, fm->fssm_Unit, (unsigned long) fm->fssm_Flags);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
|
||||
{
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
|
||||
enum grub_dev_abstraction_types ab __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
grub_disk_addr_t
|
||||
grub_util_find_partition_start_os (const char *dev)
|
||||
{
|
||||
const char *p;
|
||||
char *dname;
|
||||
struct DosList *dl;
|
||||
struct DeviceNode *dn;
|
||||
struct FileSysStartupMsg *fm;
|
||||
struct DosEnvec *envec;
|
||||
|
||||
if (!dev[0])
|
||||
return 0;
|
||||
|
||||
p = dev + strlen (dev) - 1;
|
||||
if (*p != ':')
|
||||
return 0;
|
||||
dname = xmalloc (p - dev + 1);
|
||||
memcpy (dname, dev, p - dev);
|
||||
dname[p - dev] = '\0';
|
||||
dl = LockDosList(LDF_READ);
|
||||
if (!dl)
|
||||
{
|
||||
free (dname);
|
||||
return 0;
|
||||
}
|
||||
dn = (struct DeviceNode *) FindDosEntry (dl, (unsigned char *) dname,
|
||||
LDF_DEVICES);
|
||||
UnLockDosList (LDF_READ);
|
||||
free (dname);
|
||||
if (!dn)
|
||||
return 0;
|
||||
|
||||
fm = (struct FileSysStartupMsg *) BADDR(dn->dn_Startup);
|
||||
envec = (struct DosEnvec *) fm->fssm_Environ;
|
||||
|
||||
return (((grub_uint64_t) envec->de_Surfaces
|
||||
* (grub_uint64_t) envec->de_BlocksPerTrack
|
||||
* (grub_uint64_t) envec->de_LowCyl)
|
||||
* (grub_uint64_t) envec->de_SizeBlock) >> 7;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_make_system_path_relative_to_its_root (const char *path)
|
||||
{
|
||||
char *p;
|
||||
unsigned char *tmp = xmalloc (2048);
|
||||
char *ret;
|
||||
BPTR lck;
|
||||
lck = Lock ((const unsigned char *) path, SHARED_LOCK);
|
||||
if (!lck || !NameFromLock (lck, tmp, 2040))
|
||||
{
|
||||
free (tmp);
|
||||
tmp = (unsigned char *) xstrdup (path);
|
||||
}
|
||||
if (lck)
|
||||
UnLock (lck);
|
||||
p = strchr ((char *) tmp, ':');
|
||||
if (!p)
|
||||
return (char *) tmp;
|
||||
if (p[1] == '/' || p[1] == '\0')
|
||||
{
|
||||
ret = xstrdup (p + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = xmalloc (strlen (p + 1) + 2);
|
||||
ret[0] = '/';
|
||||
strcpy (ret + 1, p + 1);
|
||||
}
|
||||
|
||||
free (tmp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char **
|
||||
grub_guess_root_devices (const char *path)
|
||||
{
|
||||
char **os_dev = NULL;
|
||||
struct InfoData id;
|
||||
BPTR lck;
|
||||
struct DeviceList *dl;
|
||||
struct DosList *dl2;
|
||||
size_t sz;
|
||||
const char *nm;
|
||||
|
||||
lck = Lock ((const unsigned char *) path, SHARED_LOCK);
|
||||
if (!lck)
|
||||
grub_util_info ("Lock(%s) failed", path);
|
||||
if (!lck || !Info (lck, &id))
|
||||
{
|
||||
char *p;
|
||||
if (lck)
|
||||
UnLock (lck);
|
||||
grub_util_info ("Info(%s) failed", path);
|
||||
os_dev = xmalloc (2 * sizeof (os_dev[0]));
|
||||
sz = strlen (path);
|
||||
os_dev[0] = xmalloc (sz + 2);
|
||||
memcpy (os_dev[0], path, sz);
|
||||
os_dev[0][sz] = ':';
|
||||
os_dev[0][sz+1] = '\0';
|
||||
p = strchr (os_dev[0], ':');
|
||||
*(p + 1) = '\0';
|
||||
os_dev[1] = NULL;
|
||||
return os_dev;
|
||||
}
|
||||
dl = BADDR (id.id_VolumeNode);
|
||||
|
||||
if (!dl->dl_Task)
|
||||
grub_util_error ("unsupported device %s", dl->dl_Name);
|
||||
|
||||
grub_util_info ("dl=%p, dl->dl_Name=%s, dl->dl_Task=%p",
|
||||
dl, dl->dl_Name,
|
||||
dl->dl_Task);
|
||||
|
||||
for (dl2 = LockDosList(LDF_READ | LDF_DEVICES);
|
||||
dl2;
|
||||
dl2 = NextDosEntry (dl2, LDF_DEVICES))
|
||||
{
|
||||
if (dl2->dol_Task == dl->dl_Task)
|
||||
break;
|
||||
}
|
||||
|
||||
if (lck)
|
||||
UnLock (lck);
|
||||
|
||||
if (dl2)
|
||||
nm = (char *) dl2->dol_Name;
|
||||
else
|
||||
nm = (char *) dl->dl_Name;
|
||||
|
||||
grub_util_info ("dl2=%p, nm=%s", dl2, nm);
|
||||
|
||||
os_dev = xmalloc (2 * sizeof (os_dev[0]));
|
||||
sz = strlen (nm);
|
||||
|
||||
os_dev[0] = xmalloc (sz + 2);
|
||||
memcpy (os_dev[0], nm, sz);
|
||||
os_dev[0][sz] = ':';
|
||||
os_dev[0][sz+1] = '\0';
|
||||
os_dev[1] = NULL;
|
||||
|
||||
UnLockDosList (LDF_READ | LDF_DEVICES);
|
||||
|
||||
return os_dev;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_biosdisk_is_floppy (grub_disk_t disk)
|
||||
{
|
||||
const char *dname, *p;
|
||||
|
||||
dname = grub_util_biosdisk_get_osdev (disk);
|
||||
|
||||
p = strchr (dname, ':');
|
||||
if (!p || p[1])
|
||||
return 0;
|
||||
|
||||
if (strncmp (dname, TD_NAME, sizeof (TD_NAME) - 1) == 0
|
||||
&& (TD_NAME[sizeof (TD_NAME) - 1] == '/'
|
||||
|| TD_NAME[sizeof (TD_NAME) - 1] == ':'))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
82
grub-core/osdep/basic/getroot.c
Normal file
82
grub-core/osdep/basic/getroot.c
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include <grub/types.h>
|
||||
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
|
||||
|
||||
char *
|
||||
grub_util_part_to_disk (const char *os_dev,
|
||||
struct stat *st __attribute__ ((unused)),
|
||||
int *is_part)
|
||||
{
|
||||
*is_part = 0;
|
||||
return xstrdup (os_dev);
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
|
||||
{
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
|
||||
enum grub_dev_abstraction_types ab __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
grub_disk_addr_t
|
||||
grub_util_find_partition_start_os (const char *dev __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
199
grub-core/osdep/bsd/getroot.c
Normal file
199
grub-core/osdep/bsd/getroot.c
Normal file
|
@ -0,0 +1,199 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include <grub/types.h>
|
||||
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
# include <sys/ioctl.h>
|
||||
# include <sys/disklabel.h> /* struct disklabel */
|
||||
# include <sys/disk.h> /* struct dkwedge_info */
|
||||
# ifdef HAVE_GETRAWPARTITION
|
||||
# include <util.h> /* getrawpartition */
|
||||
# endif /* HAVE_GETRAWPARTITION */
|
||||
#if defined(__NetBSD__)
|
||||
# include <sys/fdio.h>
|
||||
#endif
|
||||
#if defined(__OpenBSD__)
|
||||
# include <sys/dkio.h>
|
||||
#endif
|
||||
|
||||
char *
|
||||
grub_util_part_to_disk (const char *os_dev, struct stat *st,
|
||||
int *is_part)
|
||||
{
|
||||
int rawpart = -1;
|
||||
# ifdef HAVE_GETRAWPARTITION
|
||||
rawpart = getrawpartition();
|
||||
# endif /* HAVE_GETRAWPARTITION */
|
||||
if (rawpart < 0)
|
||||
return xstrdup (os_dev);
|
||||
|
||||
#if defined(__NetBSD__)
|
||||
/* NetBSD disk wedges are of the form "/dev/rdk.*". */
|
||||
if (strncmp ("/dev/rdk", os_dev, sizeof("/dev/rdk") - 1) == 0)
|
||||
{
|
||||
struct dkwedge_info dkw;
|
||||
int fd;
|
||||
|
||||
fd = open (os_dev, O_RDONLY);
|
||||
if (fd == -1)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE,
|
||||
N_("cannot open `%s': %s"), os_dev,
|
||||
strerror (errno));
|
||||
return xstrdup (os_dev);
|
||||
}
|
||||
/* We don't call configure_device_driver since this isn't a floppy device name. */
|
||||
if (ioctl (fd, DIOCGWEDGEINFO, &dkw) == -1)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE,
|
||||
"cannot get disk wedge info of `%s'", os_dev);
|
||||
close (fd);
|
||||
return xstrdup (os_dev);
|
||||
}
|
||||
*is_part = (dkw.dkw_offset != 0);
|
||||
close (fd);
|
||||
return xasprintf ("/dev/r%s%c", dkw.dkw_parent, 'a' + rawpart);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* NetBSD (disk label) partitions are of the form "/dev/r[a-z]+[0-9][a-z]". */
|
||||
if (strncmp ("/dev/r", os_dev, sizeof("/dev/r") - 1) == 0 &&
|
||||
(os_dev[sizeof("/dev/r") - 1] >= 'a' && os_dev[sizeof("/dev/r") - 1] <= 'z') &&
|
||||
strncmp ("fd", os_dev + sizeof("/dev/r") - 1, sizeof("fd") - 1) != 0) /* not a floppy device name */
|
||||
{
|
||||
char *path = xstrdup (os_dev);
|
||||
char *p;
|
||||
for (p = path + sizeof("/dev/r"); *p >= 'a' && *p <= 'z'; p++);
|
||||
if (grub_isdigit(*p))
|
||||
{
|
||||
p++;
|
||||
if ((*p >= 'a' && *p <= 'z') && (*(p+1) == '\0'))
|
||||
{
|
||||
if (*p != 'a' + rawpart)
|
||||
*is_part = 1;
|
||||
/* path matches the required regular expression and
|
||||
p points to its last character. */
|
||||
*p = 'a' + rawpart;
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
return xstrdup (os_dev);
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
|
||||
{
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
|
||||
enum grub_dev_abstraction_types ab __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
grub_disk_addr_t
|
||||
grub_util_find_partition_start_os (const char *dev)
|
||||
{
|
||||
int fd;
|
||||
# if defined(__NetBSD__)
|
||||
struct dkwedge_info dkw;
|
||||
# endif /* defined(__NetBSD__) */
|
||||
struct disklabel label;
|
||||
int p_index;
|
||||
|
||||
fd = open (dev, O_RDONLY);
|
||||
if (fd == -1)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
|
||||
dev, strerror (errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
# if defined(__NetBSD__)
|
||||
configure_device_driver (fd);
|
||||
/* First handle the case of disk wedges. */
|
||||
if (ioctl (fd, DIOCGWEDGEINFO, &dkw) == 0)
|
||||
{
|
||||
close (fd);
|
||||
return (grub_disk_addr_t) dkw.dkw_offset;
|
||||
}
|
||||
# endif /* defined(__NetBSD__) */
|
||||
|
||||
if (ioctl (fd, DIOCGDINFO, &label) == -1)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE,
|
||||
"cannot get disk label of `%s'", dev);
|
||||
close (fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
close (fd);
|
||||
|
||||
if (dev[0])
|
||||
p_index = dev[strlen(dev) - 1] - 'a';
|
||||
else
|
||||
p_index = -1;
|
||||
|
||||
if (p_index >= label.d_npartitions || p_index < 0)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE,
|
||||
"no disk label entry for `%s'", dev);
|
||||
return 0;
|
||||
}
|
||||
return (grub_disk_addr_t) label.d_partitions[p_index].p_offset;
|
||||
}
|
451
grub-core/osdep/devmapper/getroot.c
Normal file
451
grub-core/osdep/devmapper/getroot.c
Normal file
|
@ -0,0 +1,451 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <grub/emu/getroot.h>
|
||||
#include <grub/mm.h>
|
||||
|
||||
#ifdef HAVE_DEVICE_MAPPER
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include <libdevmapper.h>
|
||||
|
||||
#include <grub/types.h>
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
|
||||
static int
|
||||
grub_util_open_dm (const char *os_dev, struct dm_tree **tree,
|
||||
struct dm_tree_node **node)
|
||||
{
|
||||
uint32_t maj, min;
|
||||
struct stat st;
|
||||
|
||||
*node = NULL;
|
||||
*tree = NULL;
|
||||
|
||||
if (stat (os_dev, &st) < 0)
|
||||
return 0;
|
||||
|
||||
maj = major (st.st_rdev);
|
||||
min = minor (st.st_rdev);
|
||||
|
||||
if (!dm_is_dm_major (maj))
|
||||
return 0;
|
||||
|
||||
*tree = dm_tree_create ();
|
||||
if (! *tree)
|
||||
{
|
||||
grub_puts_ (N_("Failed to create `device-mapper' tree"));
|
||||
grub_dprintf ("hostdisk", "dm_tree_create failed\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (! dm_tree_add_dev (*tree, maj, min))
|
||||
{
|
||||
grub_dprintf ("hostdisk", "dm_tree_add_dev failed\n");
|
||||
dm_tree_free (*tree);
|
||||
*tree = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
*node = dm_tree_find_node (*tree, maj, min);
|
||||
if (! *node)
|
||||
{
|
||||
grub_dprintf ("hostdisk", "dm_tree_find_node failed\n");
|
||||
dm_tree_free (*tree);
|
||||
*tree = NULL;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static char *
|
||||
get_dm_uuid (const char *os_dev)
|
||||
{
|
||||
struct dm_tree *tree;
|
||||
struct dm_tree_node *node;
|
||||
const char *node_uuid;
|
||||
char *ret;
|
||||
|
||||
if (!grub_util_open_dm (os_dev, &tree, &node))
|
||||
return NULL;
|
||||
|
||||
node_uuid = dm_tree_node_get_uuid (node);
|
||||
if (! node_uuid)
|
||||
{
|
||||
grub_dprintf ("hostdisk", "%s has no DM uuid\n", os_dev);
|
||||
dm_tree_free (tree);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = grub_strdup (node_uuid);
|
||||
|
||||
dm_tree_free (tree);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dm_abstraction (const char *os_dev)
|
||||
{
|
||||
char *uuid;
|
||||
|
||||
uuid = get_dm_uuid (os_dev);
|
||||
|
||||
if (uuid == NULL)
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
|
||||
if (strncmp (uuid, "LVM-", 4) == 0)
|
||||
{
|
||||
grub_free (uuid);
|
||||
return GRUB_DEV_ABSTRACTION_LVM;
|
||||
}
|
||||
if (strncmp (uuid, "CRYPT-LUKS1-", 4) == 0)
|
||||
{
|
||||
grub_free (uuid);
|
||||
return GRUB_DEV_ABSTRACTION_LUKS;
|
||||
}
|
||||
|
||||
grub_free (uuid);
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
void
|
||||
grub_util_pull_devmapper (const char *os_dev)
|
||||
{
|
||||
struct dm_tree *tree;
|
||||
struct dm_tree_node *node;
|
||||
struct dm_tree_node *child;
|
||||
void *handle = NULL;
|
||||
char *lastsubdev = NULL;
|
||||
char *uuid;
|
||||
|
||||
uuid = get_dm_uuid (os_dev);
|
||||
|
||||
if (!grub_util_open_dm (os_dev, &tree, &node))
|
||||
return;
|
||||
|
||||
while ((child = dm_tree_next_child (&handle, node, 0)))
|
||||
{
|
||||
const struct dm_info *dm = dm_tree_node_get_info (child);
|
||||
char *subdev;
|
||||
if (!dm)
|
||||
continue;
|
||||
subdev = grub_find_device ("/dev", makedev (dm->major, dm->minor));
|
||||
if (subdev)
|
||||
{
|
||||
lastsubdev = subdev;
|
||||
grub_util_pull_device (subdev);
|
||||
}
|
||||
}
|
||||
if (uuid && strncmp (uuid, "CRYPT-LUKS1-", sizeof ("CRYPT-LUKS1-") - 1) == 0
|
||||
&& lastsubdev)
|
||||
{
|
||||
char *grdev = grub_util_get_grub_dev (lastsubdev);
|
||||
dm_tree_free (tree);
|
||||
if (grdev)
|
||||
{
|
||||
grub_err_t err;
|
||||
err = grub_cryptodisk_cheat_mount (grdev, os_dev);
|
||||
if (err)
|
||||
grub_util_error (_("can't mount encrypted volume `%s': %s"),
|
||||
lastsubdev, grub_errmsg);
|
||||
}
|
||||
grub_free (grdev);
|
||||
}
|
||||
else
|
||||
dm_tree_free (tree);
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_devmapper_part_to_disk (struct stat *st,
|
||||
int *is_part, const char *path)
|
||||
{
|
||||
struct dm_tree *tree;
|
||||
uint32_t maj, min;
|
||||
struct dm_tree_node *node = NULL, *child;
|
||||
void *handle;
|
||||
const char *node_uuid, *mapper_name = NULL, *child_uuid, *child_name;
|
||||
|
||||
tree = dm_tree_create ();
|
||||
if (! tree)
|
||||
{
|
||||
grub_util_info ("dm_tree_create failed");
|
||||
goto devmapper_out;
|
||||
}
|
||||
|
||||
maj = major (st->st_rdev);
|
||||
min = minor (st->st_rdev);
|
||||
if (! dm_tree_add_dev (tree, maj, min))
|
||||
{
|
||||
grub_util_info ("dm_tree_add_dev failed");
|
||||
goto devmapper_out;
|
||||
}
|
||||
|
||||
node = dm_tree_find_node (tree, maj, min);
|
||||
if (! node)
|
||||
{
|
||||
grub_util_info ("dm_tree_find_node failed");
|
||||
goto devmapper_out;
|
||||
}
|
||||
reiterate:
|
||||
node_uuid = dm_tree_node_get_uuid (node);
|
||||
if (! node_uuid)
|
||||
{
|
||||
grub_util_info ("%s has no DM uuid", path);
|
||||
goto devmapper_out;
|
||||
}
|
||||
if (strncmp (node_uuid, "LVM-", 4) == 0)
|
||||
{
|
||||
grub_util_info ("%s is an LVM", path);
|
||||
goto devmapper_out;
|
||||
}
|
||||
if (strncmp (node_uuid, "mpath-", 6) == 0)
|
||||
{
|
||||
/* Multipath partitions have partN-mpath-* UUIDs, and are
|
||||
linear mappings so are handled by
|
||||
grub_util_get_dm_node_linear_info. Multipath disks are not
|
||||
linear mappings and must be handled specially. */
|
||||
grub_util_info ("%s is a multipath disk", path);
|
||||
goto devmapper_out;
|
||||
}
|
||||
if (strncmp (node_uuid, "DMRAID-", 7) != 0)
|
||||
{
|
||||
int major, minor;
|
||||
const char *node_name;
|
||||
grub_util_info ("%s is not DM-RAID", path);
|
||||
|
||||
if ((node_name = dm_tree_node_get_name (node))
|
||||
&& grub_util_get_dm_node_linear_info (node_name,
|
||||
&major, &minor, 0))
|
||||
{
|
||||
*is_part = 1;
|
||||
if (tree)
|
||||
dm_tree_free (tree);
|
||||
char *ret = grub_find_device ("/dev",
|
||||
(major << 8) | minor);
|
||||
return ret;
|
||||
}
|
||||
|
||||
goto devmapper_out;
|
||||
}
|
||||
|
||||
handle = NULL;
|
||||
/* Counter-intuitively, device-mapper refers to the disk-like
|
||||
device containing a DM-RAID partition device as a "child" of
|
||||
the partition device. */
|
||||
child = dm_tree_next_child (&handle, node, 0);
|
||||
if (! child)
|
||||
{
|
||||
grub_util_info ("%s has no DM children", path);
|
||||
goto devmapper_out;
|
||||
}
|
||||
child_uuid = dm_tree_node_get_uuid (child);
|
||||
if (! child_uuid)
|
||||
{
|
||||
grub_util_info ("%s child has no DM uuid", path);
|
||||
goto devmapper_out;
|
||||
}
|
||||
else if (strncmp (child_uuid, "DMRAID-", 7) != 0)
|
||||
{
|
||||
grub_util_info ("%s child is not DM-RAID", path);
|
||||
goto devmapper_out;
|
||||
}
|
||||
child_name = dm_tree_node_get_name (child);
|
||||
if (! child_name)
|
||||
{
|
||||
grub_util_info ("%s child has no DM name", path);
|
||||
goto devmapper_out;
|
||||
}
|
||||
mapper_name = child_name;
|
||||
*is_part = 1;
|
||||
node = child;
|
||||
goto reiterate;
|
||||
|
||||
devmapper_out:
|
||||
if (! mapper_name && node)
|
||||
{
|
||||
/* This is a DM-RAID disk, not a partition. */
|
||||
mapper_name = dm_tree_node_get_name (node);
|
||||
if (! mapper_name)
|
||||
grub_util_info ("%s has no DM name", path);
|
||||
}
|
||||
char *ret;
|
||||
if (mapper_name)
|
||||
ret = xasprintf ("/dev/mapper/%s", mapper_name);
|
||||
else
|
||||
ret = NULL;
|
||||
|
||||
if (tree)
|
||||
dm_tree_free (tree);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_device_is_mapped_stat (struct stat *st)
|
||||
{
|
||||
if (!grub_device_mapper_supported ())
|
||||
return 0;
|
||||
|
||||
return dm_is_dm_major (major (st->st_rdev));
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_devmapper_grub_dev (const char *os_dev)
|
||||
{
|
||||
char *uuid, *optr;
|
||||
char *grub_dev;
|
||||
|
||||
uuid = get_dm_uuid (os_dev);
|
||||
if (!uuid)
|
||||
return NULL;
|
||||
|
||||
if (strncmp (uuid, "LVM-", sizeof ("LVM-") - 1) == 0)
|
||||
{
|
||||
unsigned i;
|
||||
int dashes[] = { 0, 6, 10, 14, 18, 22, 26, 32, 38, 42, 46, 50, 54, 58};
|
||||
grub_dev = xmalloc (grub_strlen (uuid) + 40);
|
||||
optr = grub_stpcpy (grub_dev, "lvmid/");
|
||||
for (i = 0; i < ARRAY_SIZE (dashes) - 1; i++)
|
||||
{
|
||||
memcpy (optr, uuid + sizeof ("LVM-") - 1 + dashes[i],
|
||||
dashes[i+1] - dashes[i]);
|
||||
optr += dashes[i+1] - dashes[i];
|
||||
*optr++ = '-';
|
||||
}
|
||||
optr = stpcpy (optr, uuid + sizeof ("LVM-") - 1 + dashes[i]);
|
||||
*optr = '\0';
|
||||
grub_dev[sizeof("lvmid/xxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxxxx") - 1]
|
||||
= '/';
|
||||
free (uuid);
|
||||
return grub_dev;
|
||||
}
|
||||
|
||||
if (strncmp (uuid, "CRYPT-LUKS1-", sizeof ("CRYPT-LUKS1-") - 1) == 0)
|
||||
{
|
||||
char *dash;
|
||||
dash = grub_strchr (uuid + sizeof ("CRYPT-LUKS1-") - 1, '-');
|
||||
if (dash)
|
||||
*dash = 0;
|
||||
grub_dev = grub_xasprintf ("cryptouuid/%s",
|
||||
uuid + sizeof ("CRYPT-LUKS1-") - 1);
|
||||
grub_free (uuid);
|
||||
return grub_dev;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_vg_uuid (const char *os_dev)
|
||||
{
|
||||
char *uuid, *vgid;
|
||||
int dashes[] = { 0, 6, 10, 14, 18, 22, 26, 32};
|
||||
unsigned i;
|
||||
char *optr;
|
||||
|
||||
uuid = get_dm_uuid (os_dev);
|
||||
if (!uuid)
|
||||
return NULL;
|
||||
|
||||
vgid = xmalloc (grub_strlen (uuid));
|
||||
optr = vgid;
|
||||
for (i = 0; i < ARRAY_SIZE (dashes) - 1; i++)
|
||||
{
|
||||
memcpy (optr, uuid + sizeof ("LVM-") - 1 + dashes[i],
|
||||
dashes[i+1] - dashes[i]);
|
||||
optr += dashes[i+1] - dashes[i];
|
||||
*optr++ = '-';
|
||||
}
|
||||
optr--;
|
||||
*optr = '\0';
|
||||
return vgid;
|
||||
}
|
||||
|
||||
void
|
||||
grub_util_devmapper_cleanup (void)
|
||||
{
|
||||
dm_lib_release ();
|
||||
}
|
||||
|
||||
#else
|
||||
void
|
||||
grub_util_pull_devmapper (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_device_is_mapped_stat (struct stat *st __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
grub_util_devmapper_cleanup (void)
|
||||
{
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dm_abstraction (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_vg_uuid (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_devmapper_part_to_disk (struct stat *st __attribute__ ((unused)),
|
||||
int *is_part __attribute__ ((unused)),
|
||||
const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_devmapper_grub_dev (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#endif
|
355
grub-core/osdep/freebsd/getroot.c
Normal file
355
grub-core/osdep/freebsd/getroot.c
Normal file
|
@ -0,0 +1,355 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include <grub/types.h>
|
||||
# include <sys/param.h>
|
||||
# include <sys/mount.h>
|
||||
# include <sys/disk.h> /* DIOCGMEDIASIZE */
|
||||
# include <sys/param.h>
|
||||
# include <sys/sysctl.h>
|
||||
#include <libgeom.h>
|
||||
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
#include <grub/cryptodisk.h>
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <libgeom.h>
|
||||
|
||||
static const char *
|
||||
grub_util_get_geom_abstraction (const char *dev)
|
||||
{
|
||||
char *whole;
|
||||
struct gmesh mesh;
|
||||
struct gclass *class;
|
||||
const char *name;
|
||||
int err;
|
||||
|
||||
if (strncmp (dev, "/dev/", sizeof ("/dev/") - 1) != 0)
|
||||
return 0;
|
||||
name = dev + sizeof ("/dev/") - 1;
|
||||
grub_util_follow_gpart_up (name, NULL, &whole);
|
||||
|
||||
grub_util_info ("following geom '%s'", name);
|
||||
|
||||
err = geom_gettree (&mesh);
|
||||
if (err != 0)
|
||||
/* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
|
||||
Usually left untranslated.
|
||||
*/
|
||||
grub_util_error ("%s", _("couldn't open geom"));
|
||||
|
||||
LIST_FOREACH (class, &mesh.lg_class, lg_class)
|
||||
{
|
||||
struct ggeom *geom;
|
||||
LIST_FOREACH (geom, &class->lg_geom, lg_geom)
|
||||
{
|
||||
struct gprovider *provider;
|
||||
LIST_FOREACH (provider, &geom->lg_provider, lg_provider)
|
||||
if (strcmp (provider->lg_name, name) == 0)
|
||||
return class->lg_name;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dev_abstraction_os (const char *os_dev)
|
||||
{
|
||||
const char *abstrac;
|
||||
abstrac = grub_util_get_geom_abstraction (os_dev);
|
||||
grub_util_info ("abstraction of %s is %s", os_dev, abstrac);
|
||||
if (abstrac && grub_strcasecmp (abstrac, "eli") == 0)
|
||||
return GRUB_DEV_ABSTRACTION_GELI;
|
||||
|
||||
/* Check for LVM. */
|
||||
if (!strncmp (os_dev, LVM_DEV_MAPPER_STRING, sizeof(LVM_DEV_MAPPER_STRING)-1))
|
||||
return GRUB_DEV_ABSTRACTION_LVM;
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_part_to_disk (const char *os_dev, struct stat *st,
|
||||
int *is_part)
|
||||
{
|
||||
char *out, *out2;
|
||||
if (strncmp (os_dev, "/dev/", sizeof ("/dev/") - 1) != 0)
|
||||
return xstrdup (os_dev);
|
||||
grub_util_follow_gpart_up (os_dev + sizeof ("/dev/") - 1, NULL, &out);
|
||||
|
||||
if (grub_strcmp (os_dev + sizeof ("/dev/") - 1, out) != 0)
|
||||
*is_part = 1;
|
||||
out2 = xasprintf ("/dev/%s", out);
|
||||
free (out);
|
||||
|
||||
return out2;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_pull_device_os (const char *os_dev,
|
||||
enum grub_dev_abstraction_types ab)
|
||||
{
|
||||
switch (ab)
|
||||
{
|
||||
case GRUB_DEV_ABSTRACTION_GELI:
|
||||
{
|
||||
char *whole;
|
||||
struct gmesh mesh;
|
||||
struct gclass *class;
|
||||
const char *name;
|
||||
int err;
|
||||
char *lastsubdev = NULL;
|
||||
|
||||
if (strncmp (os_dev, "/dev/", sizeof ("/dev/") - 1) != 0)
|
||||
return 1;
|
||||
name = os_dev + sizeof ("/dev/") - 1;
|
||||
grub_util_follow_gpart_up (name, NULL, &whole);
|
||||
|
||||
grub_util_info ("following geom '%s'", name);
|
||||
|
||||
err = geom_gettree (&mesh);
|
||||
if (err != 0)
|
||||
/* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
|
||||
Usually left untranslated.
|
||||
*/
|
||||
grub_util_error ("%s", _("couldn't open geom"));
|
||||
|
||||
LIST_FOREACH (class, &mesh.lg_class, lg_class)
|
||||
{
|
||||
struct ggeom *geom;
|
||||
LIST_FOREACH (geom, &class->lg_geom, lg_geom)
|
||||
{
|
||||
struct gprovider *provider;
|
||||
LIST_FOREACH (provider, &geom->lg_provider, lg_provider)
|
||||
if (strcmp (provider->lg_name, name) == 0)
|
||||
{
|
||||
struct gconsumer *consumer;
|
||||
char *fname;
|
||||
|
||||
LIST_FOREACH (consumer, &geom->lg_consumer, lg_consumer)
|
||||
break;
|
||||
if (!consumer)
|
||||
grub_util_error ("%s",
|
||||
_("couldn't find geli consumer"));
|
||||
fname = xasprintf ("/dev/%s", consumer->lg_provider->lg_name);
|
||||
grub_util_info ("consumer %s", consumer->lg_provider->lg_name);
|
||||
lastsubdev = consumer->lg_provider->lg_name;
|
||||
grub_util_pull_device (fname);
|
||||
free (fname);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ab == GRUB_DEV_ABSTRACTION_GELI && lastsubdev)
|
||||
{
|
||||
char *fname = xasprintf ("/dev/%s", lastsubdev);
|
||||
char *grdev = grub_util_get_grub_dev (fname);
|
||||
free (fname);
|
||||
|
||||
if (grdev)
|
||||
{
|
||||
grub_err_t gr_err;
|
||||
gr_err = grub_cryptodisk_cheat_mount (grdev, os_dev);
|
||||
if (gr_err)
|
||||
grub_util_error (_("can't mount encrypted volume `%s': %s"),
|
||||
lastsubdev, grub_errmsg);
|
||||
}
|
||||
|
||||
grub_free (grdev);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_grub_dev_os (const char *os_dev)
|
||||
{
|
||||
char *grub_dev = NULL;
|
||||
|
||||
switch (grub_util_get_dev_abstraction (os_dev))
|
||||
{
|
||||
/* Fallback for non-devmapper build. In devmapper-builds LVM is handled
|
||||
in rub_util_get_devmapper_grub_dev and this point isn't reached.
|
||||
*/
|
||||
case GRUB_DEV_ABSTRACTION_LVM:
|
||||
{
|
||||
unsigned short len;
|
||||
grub_size_t offset = sizeof (LVM_DEV_MAPPER_STRING) - 1;
|
||||
|
||||
len = strlen (os_dev) - offset + 1;
|
||||
grub_dev = xmalloc (len + sizeof ("lvm/"));
|
||||
|
||||
grub_memcpy (grub_dev, "lvm/", sizeof ("lvm/") - 1);
|
||||
grub_memcpy (grub_dev + sizeof ("lvm/") - 1, os_dev + offset, len);
|
||||
}
|
||||
break;
|
||||
|
||||
case GRUB_DEV_ABSTRACTION_GELI:
|
||||
{
|
||||
char *whole;
|
||||
struct gmesh mesh;
|
||||
struct gclass *class;
|
||||
const char *name;
|
||||
int err;
|
||||
|
||||
if (strncmp (os_dev, "/dev/", sizeof ("/dev/") - 1) != 0)
|
||||
return 0;
|
||||
name = os_dev + sizeof ("/dev/") - 1;
|
||||
grub_util_follow_gpart_up (name, NULL, &whole);
|
||||
|
||||
grub_util_info ("following geom '%s'", name);
|
||||
|
||||
err = geom_gettree (&mesh);
|
||||
if (err != 0)
|
||||
/* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
|
||||
Usually left untranslated.
|
||||
*/
|
||||
grub_util_error ("%s", _("couldn't open geom"));
|
||||
|
||||
LIST_FOREACH (class, &mesh.lg_class, lg_class)
|
||||
{
|
||||
struct ggeom *geom;
|
||||
LIST_FOREACH (geom, &class->lg_geom, lg_geom)
|
||||
{
|
||||
struct gprovider *provider;
|
||||
LIST_FOREACH (provider, &geom->lg_provider, lg_provider)
|
||||
if (strcmp (provider->lg_name, name) == 0)
|
||||
{
|
||||
struct gconsumer *consumer;
|
||||
char *fname;
|
||||
char *uuid;
|
||||
|
||||
LIST_FOREACH (consumer, &geom->lg_consumer, lg_consumer)
|
||||
break;
|
||||
if (!consumer)
|
||||
grub_util_error ("%s",
|
||||
_("couldn't find geli consumer"));
|
||||
fname = xasprintf ("/dev/%s", consumer->lg_provider->lg_name);
|
||||
uuid = grub_util_get_geli_uuid (fname);
|
||||
if (!uuid)
|
||||
grub_util_error ("%s",
|
||||
_("couldn't retrieve geli UUID"));
|
||||
grub_dev = xasprintf ("cryptouuid/%s", uuid);
|
||||
free (fname);
|
||||
free (uuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return grub_dev;
|
||||
}
|
||||
|
||||
/* FIXME: geom actually gives us the whole container hierarchy.
|
||||
It can be used more efficiently than this. */
|
||||
void
|
||||
grub_util_follow_gpart_up (const char *name, grub_disk_addr_t *off_out, char **name_out)
|
||||
{
|
||||
struct gmesh mesh;
|
||||
struct gclass *class;
|
||||
int err;
|
||||
struct ggeom *geom;
|
||||
|
||||
grub_util_info ("following geom '%s'", name);
|
||||
|
||||
err = geom_gettree (&mesh);
|
||||
if (err != 0)
|
||||
/* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
|
||||
Usually left untranslated.
|
||||
*/
|
||||
grub_util_error ("%s", _("couldn't open geom"));
|
||||
|
||||
LIST_FOREACH (class, &mesh.lg_class, lg_class)
|
||||
if (strcasecmp (class->lg_name, "part") == 0)
|
||||
break;
|
||||
if (!class)
|
||||
/* TRANSLATORS: geom is the name of (k)FreeBSD device framework.
|
||||
Usually left untranslated. "part" is the identifier of one of its
|
||||
classes. */
|
||||
grub_util_error ("%s", _("couldn't find geom `part' class"));
|
||||
|
||||
LIST_FOREACH (geom, &class->lg_geom, lg_geom)
|
||||
{
|
||||
struct gprovider *provider;
|
||||
LIST_FOREACH (provider, &geom->lg_provider, lg_provider)
|
||||
if (strcmp (provider->lg_name, name) == 0)
|
||||
{
|
||||
char *name_tmp = xstrdup (geom->lg_name);
|
||||
grub_disk_addr_t off = 0;
|
||||
struct gconfig *config;
|
||||
grub_util_info ("geom '%s' has parent '%s'", name, geom->lg_name);
|
||||
|
||||
grub_util_follow_gpart_up (name_tmp, &off, name_out);
|
||||
free (name_tmp);
|
||||
LIST_FOREACH (config, &provider->lg_config, lg_config)
|
||||
if (strcasecmp (config->lg_name, "start") == 0)
|
||||
off += strtoull (config->lg_val, 0, 10);
|
||||
if (off_out)
|
||||
*off_out = off;
|
||||
return;
|
||||
}
|
||||
}
|
||||
grub_util_info ("geom '%s' has no parent", name);
|
||||
if (name_out)
|
||||
*name_out = xstrdup (name);
|
||||
if (off_out)
|
||||
*off_out = 0;
|
||||
}
|
||||
|
||||
grub_disk_addr_t
|
||||
grub_util_find_partition_start_os (const char *dev)
|
||||
{
|
||||
grub_disk_addr_t out;
|
||||
if (strncmp (dev, "/dev/", sizeof ("/dev/") - 1) != 0)
|
||||
return 0;
|
||||
grub_util_follow_gpart_up (dev + sizeof ("/dev/") - 1, &out, NULL);
|
||||
|
||||
return out;
|
||||
}
|
20
grub-core/osdep/getroot.c
Normal file
20
grub-core/osdep/getroot.c
Normal file
|
@ -0,0 +1,20 @@
|
|||
#ifdef __linux__
|
||||
#include "linux/getroot.c"
|
||||
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||
#include "freebsd/getroot.c"
|
||||
#elif defined(__NetBSD__) || defined(__OpenBSD__)
|
||||
#include "bsd/getroot.c"
|
||||
#elif defined(__APPLE__)
|
||||
#include "apple/getroot.c"
|
||||
#elif defined(__sun__)
|
||||
#include "sun/getroot.c"
|
||||
#elif defined(__GNU__)
|
||||
#include "hurd/getroot.c"
|
||||
#elif defined(__CYGWIN__) || defined (__MINGW32__)
|
||||
#include "windows/getroot.c"
|
||||
#elif defined(__AROS__)
|
||||
#include "aros/getroot.c"
|
||||
#else
|
||||
# warning "No getroot OS-specific functions is available for your system. Device detection may not work properly."
|
||||
#include "basic/getroot.c"
|
||||
#endif
|
|
@ -1,20 +1,20 @@
|
|||
#ifdef __linux__
|
||||
#include "hostdisk_linux.c"
|
||||
#include "linux/hostdisk.c"
|
||||
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||
#include "hostdisk_freebsd.c"
|
||||
#include "freebsd/hostdisk.c"
|
||||
#elif defined(__NetBSD__) || defined(__OpenBSD__)
|
||||
#include "hostdisk_bsd.c"
|
||||
#include "bsd/hostdisk.c"
|
||||
#elif defined(__APPLE__)
|
||||
#include "hostdisk_apple.c"
|
||||
#include "apple/hostdisk.c"
|
||||
#elif defined(__sun__)
|
||||
#include "hostdisk_sun.c"
|
||||
#include "sun/hostdisk.c"
|
||||
#elif defined(__GNU__)
|
||||
#include "hostdisk_hurd.c"
|
||||
#include "hurd/hostdisk.c"
|
||||
#elif defined(__CYGWIN__) || defined(__MINGW32__)
|
||||
#include "hostdisk_windows.c"
|
||||
#include "windows/hostdisk.c"
|
||||
#elif defined(__AROS__)
|
||||
#include "hostdisk_aros.c"
|
||||
#include "aros/hostdisk.c"
|
||||
#else
|
||||
# warning "No hostdisk OS-specific functions is available for your system. Device detection may not work properly."
|
||||
#include "hostdisk_basic.c"
|
||||
#include "basic/hostdisk.c"
|
||||
#endif
|
241
grub-core/osdep/hurd/getroot.c
Normal file
241
grub-core/osdep/hurd/getroot.c
Normal file
|
@ -0,0 +1,241 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include <grub/types.h>
|
||||
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <hurd.h>
|
||||
#include <hurd/lookup.h>
|
||||
#include <hurd/fs.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
static char *
|
||||
grub_util_find_hurd_root_device (const char *path)
|
||||
{
|
||||
file_t file;
|
||||
error_t err;
|
||||
char *argz = NULL, *name = NULL, *ret;
|
||||
size_t argz_len = 0;
|
||||
int i;
|
||||
|
||||
file = file_name_lookup (path, 0, 0);
|
||||
if (file == MACH_PORT_NULL)
|
||||
/* TRANSLATORS: The first %s is the file being looked at, the second %s is
|
||||
the error message. */
|
||||
grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
|
||||
|
||||
/* This returns catenated 0-terminated strings. */
|
||||
err = file_get_fs_options (file, &argz, &argz_len);
|
||||
if (err)
|
||||
/* TRANSLATORS: On GNU/Hurd, a "translator" is similar to a filesystem
|
||||
mount, but handled by a userland daemon, whose invocation command line
|
||||
is being fetched here. First %s is the file being looked at (for which
|
||||
we are fetching the "translator" command line), second %s is the error
|
||||
message.
|
||||
*/
|
||||
grub_util_error (_("cannot get translator command line "
|
||||
"for path `%s': %s"), path, strerror(err));
|
||||
if (argz_len == 0)
|
||||
grub_util_error (_("translator command line is empty for path `%s'"), path);
|
||||
|
||||
/* Make sure the string is terminated. */
|
||||
argz[argz_len-1] = 0;
|
||||
|
||||
/* Skip first word (translator path) and options. */
|
||||
for (i = strlen (argz) + 1; i < argz_len; i += strlen (argz + i) + 1)
|
||||
{
|
||||
if (argz[i] != '-')
|
||||
{
|
||||
/* Non-option. Only accept one, assumed to be the FS path. */
|
||||
/* XXX: this should be replaced by an RPC to the translator. */
|
||||
if (name)
|
||||
/* TRANSLATORS: we expect to get something like
|
||||
/hurd/foobar --option1 --option2=baz /dev/something
|
||||
*/
|
||||
grub_util_error (_("translator `%s' for path `%s' has several "
|
||||
"non-option words, at least `%s' and `%s'"),
|
||||
argz, path, name, argz + i);
|
||||
name = argz + i;
|
||||
}
|
||||
}
|
||||
|
||||
if (!name)
|
||||
/* TRANSLATORS: we expect to get something like
|
||||
/hurd/foobar --option1 --option2=baz /dev/something
|
||||
*/
|
||||
grub_util_error (_("translator `%s' for path `%s' is given only options, "
|
||||
"cannot find device part"), argz, path);
|
||||
|
||||
if (strncmp (name, "device:", sizeof ("device:") - 1) == 0)
|
||||
{
|
||||
char *dev_name = name + sizeof ("device:") - 1;
|
||||
size_t size = sizeof ("/dev/") - 1 + strlen (dev_name) + 1;
|
||||
char *next;
|
||||
ret = malloc (size);
|
||||
next = stpncpy (ret, "/dev/", size);
|
||||
stpncpy (next, dev_name, size - (next - ret));
|
||||
}
|
||||
else if (!strncmp (name, "file:", sizeof ("file:") - 1))
|
||||
ret = strdup (name + sizeof ("file:") - 1);
|
||||
else
|
||||
ret = strdup (name);
|
||||
|
||||
munmap (argz, argz_len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
is_fulldisk (const char *child, const char *parent)
|
||||
{
|
||||
if (strcmp (parent, child) == 0)
|
||||
return 1;
|
||||
if (strncmp (parent, "/dev/", sizeof ("/dev/") - 1) == 0
|
||||
&& child[0] !=0 && strcmp (parent + sizeof ("/dev/") - 1, child) == 0)
|
||||
return 1;
|
||||
if (strncmp (child, "/dev/", sizeof ("/dev/") - 1) == 0
|
||||
&& parent[0] != 0 && strcmp (child + sizeof ("/dev/") - 1, parent) == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_part_to_disk (const char *os_dev,
|
||||
struct stat *st __attribute__ ((unused)),
|
||||
int *is_part)
|
||||
{
|
||||
char *path;
|
||||
grub_disk_addr_t offset;
|
||||
char *p;
|
||||
|
||||
if (!grub_util_hurd_get_disk_info (os_dev, NULL, &offset, NULL, &path))
|
||||
return xstrdup (os_dev);
|
||||
|
||||
/* Some versions of Hurd use badly glued Linux code to handle partitions
|
||||
resulting in partitions being promoted to disks. */
|
||||
if (path && !(offset == 0 && is_fulldisk (path, os_dev)
|
||||
&& (strncmp ("/dev/sd", os_dev, 7) == 0
|
||||
|| strncmp ("/dev/hd", os_dev, 7) == 0)))
|
||||
{
|
||||
*is_part = !is_fulldisk (path, os_dev);
|
||||
if (path[0] != '/')
|
||||
{
|
||||
char *n = xasprintf ("/dev/%s", path);
|
||||
free (path);
|
||||
path = n;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
free (path);
|
||||
|
||||
path = xstrdup (os_dev);
|
||||
|
||||
p = strchr (path + 7, 's');
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
*p = '\0';
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
|
||||
{
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
|
||||
enum grub_dev_abstraction_types ab __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
grub_disk_addr_t
|
||||
grub_util_find_partition_start_os (const char *dev)
|
||||
{
|
||||
grub_uint32_t secsize;
|
||||
grub_disk_addr_t offset;
|
||||
char *path;
|
||||
if (!grub_util_hurd_get_disk_info (dev, &secsize, &offset, NULL, &path))
|
||||
return 0;
|
||||
if (path && !(offset == 0 && is_fulldisk (path, dev)
|
||||
&& (strncmp ("/dev/sd", dev, 7) == 0
|
||||
|| strncmp ("/dev/hd", dev, 7) == 0)))
|
||||
{
|
||||
free (path);
|
||||
return (secsize / 512) * offset;
|
||||
}
|
||||
free (path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char **
|
||||
grub_guess_root_devices (const char *dir)
|
||||
{
|
||||
char **os_dev = NULL;
|
||||
|
||||
os_dev = xmalloc (2 * sizeof (os_dev[0]));
|
||||
|
||||
/* GNU/Hurd specific function. */
|
||||
os_dev[0] = grub_util_find_hurd_root_device (dir);
|
||||
|
||||
if (!os_dev[0])
|
||||
{
|
||||
free (os_dev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
os_dev[1] = 0;
|
||||
|
||||
return os_dev;
|
||||
}
|
911
grub-core/osdep/linux/getroot.c
Normal file
911
grub-core/osdep/linux/getroot.c
Normal file
|
@ -0,0 +1,911 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include <grub/types.h>
|
||||
#include <sys/ioctl.h> /* ioctl */
|
||||
#include <sys/mount.h>
|
||||
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <linux/types.h>
|
||||
#include <linux/major.h>
|
||||
#include <linux/raid/md_p.h>
|
||||
#include <linux/raid/md_u.h>
|
||||
#include <grub/i18n.h>
|
||||
|
||||
|
||||
/* Defines taken from btrfs/ioctl.h. */
|
||||
|
||||
struct btrfs_ioctl_dev_info_args
|
||||
{
|
||||
grub_uint64_t devid;
|
||||
grub_uint8_t uuid[16];
|
||||
grub_uint64_t bytes_used;
|
||||
grub_uint64_t total_bytes;
|
||||
grub_uint64_t unused[379];
|
||||
grub_uint8_t path[1024];
|
||||
};
|
||||
|
||||
struct btrfs_ioctl_fs_info_args
|
||||
{
|
||||
grub_uint64_t max_id;
|
||||
grub_uint64_t num_devices;
|
||||
grub_uint8_t fsid[16];
|
||||
grub_uint64_t reserved[124];
|
||||
};
|
||||
|
||||
#define BTRFS_IOC_DEV_INFO _IOWR(0x94, 30, \
|
||||
struct btrfs_ioctl_dev_info_args)
|
||||
#define BTRFS_IOC_FS_INFO _IOR(0x94, 31, \
|
||||
struct btrfs_ioctl_fs_info_args)
|
||||
|
||||
static int
|
||||
grub_util_is_imsm (const char *os_dev);
|
||||
|
||||
|
||||
#define ESCAPED_PATH_MAX (4 * PATH_MAX)
|
||||
struct mountinfo_entry
|
||||
{
|
||||
int id;
|
||||
int major, minor;
|
||||
char enc_root[ESCAPED_PATH_MAX + 1], enc_path[ESCAPED_PATH_MAX + 1];
|
||||
char fstype[ESCAPED_PATH_MAX + 1], device[ESCAPED_PATH_MAX + 1];
|
||||
};
|
||||
|
||||
static char **
|
||||
grub_util_raid_getmembers (const char *name, int bootable)
|
||||
{
|
||||
int fd, ret, i, j;
|
||||
char **devicelist;
|
||||
mdu_version_t version;
|
||||
mdu_array_info_t info;
|
||||
mdu_disk_info_t disk;
|
||||
|
||||
fd = open (name, O_RDONLY);
|
||||
|
||||
if (fd == -1)
|
||||
grub_util_error (_("cannot open `%s': %s"), name, strerror (errno));
|
||||
|
||||
ret = ioctl (fd, RAID_VERSION, &version);
|
||||
if (ret != 0)
|
||||
grub_util_error (_("ioctl RAID_VERSION error: %s"), strerror (errno));
|
||||
|
||||
if ((version.major != 0 || version.minor != 90)
|
||||
&& (version.major != 1 || version.minor != 0)
|
||||
&& (version.major != 1 || version.minor != 1)
|
||||
&& (version.major != 1 || version.minor != 2))
|
||||
grub_util_error (_("unsupported RAID version: %d.%d"),
|
||||
version.major, version.minor);
|
||||
|
||||
if (bootable && (version.major != 0 || version.minor != 90))
|
||||
grub_util_error (_("unsupported RAID version: %d.%d"),
|
||||
version.major, version.minor);
|
||||
|
||||
ret = ioctl (fd, GET_ARRAY_INFO, &info);
|
||||
if (ret != 0)
|
||||
grub_util_error (_("ioctl GET_ARRAY_INFO error: %s"), strerror (errno));
|
||||
|
||||
devicelist = xmalloc ((info.nr_disks + 1) * sizeof (char *));
|
||||
|
||||
for (i = 0, j = 0; j < info.nr_disks; i++)
|
||||
{
|
||||
disk.number = i;
|
||||
ret = ioctl (fd, GET_DISK_INFO, &disk);
|
||||
if (ret != 0)
|
||||
grub_util_error (_("ioctl GET_DISK_INFO error: %s"), strerror (errno));
|
||||
|
||||
if (disk.state & (1 << MD_DISK_REMOVED))
|
||||
continue;
|
||||
|
||||
if (disk.state & (1 << MD_DISK_ACTIVE))
|
||||
devicelist[j] = grub_find_device (NULL,
|
||||
makedev (disk.major, disk.minor));
|
||||
else
|
||||
devicelist[j] = NULL;
|
||||
j++;
|
||||
}
|
||||
|
||||
devicelist[j] = NULL;
|
||||
|
||||
close (fd);
|
||||
|
||||
return devicelist;
|
||||
}
|
||||
|
||||
/* Statting something on a btrfs filesystem always returns a virtual device
|
||||
major/minor pair rather than the real underlying device, because btrfs
|
||||
can span multiple underlying devices (and even if it's currently only
|
||||
using a single device it can be dynamically extended onto another). We
|
||||
can't deal with the multiple-device case yet, but in the meantime, we can
|
||||
at least cope with the single-device case by scanning
|
||||
/proc/self/mountinfo. */
|
||||
static void
|
||||
unescape (char *str)
|
||||
{
|
||||
char *optr;
|
||||
const char *iptr;
|
||||
for (iptr = optr = str; *iptr; optr++)
|
||||
{
|
||||
if (iptr[0] == '\\' && iptr[1] >= '0' && iptr[1] < '8'
|
||||
&& iptr[2] >= '0' && iptr[2] < '8'
|
||||
&& iptr[3] >= '0' && iptr[3] < '8')
|
||||
{
|
||||
*optr = (((iptr[1] - '0') << 6) | ((iptr[2] - '0') << 3)
|
||||
| (iptr[3] - '0'));
|
||||
iptr += 4;
|
||||
}
|
||||
else
|
||||
*optr = *iptr++;
|
||||
}
|
||||
*optr = 0;
|
||||
}
|
||||
|
||||
static char **
|
||||
grub_find_root_devices_from_btrfs (const char *dir)
|
||||
{
|
||||
int fd;
|
||||
struct btrfs_ioctl_fs_info_args fsi;
|
||||
int i, j = 0;
|
||||
char **ret;
|
||||
|
||||
fd = open (dir, 0);
|
||||
if (!fd)
|
||||
return NULL;
|
||||
|
||||
if (ioctl (fd, BTRFS_IOC_FS_INFO, &fsi) < 0)
|
||||
{
|
||||
close (fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = xmalloc ((fsi.num_devices + 1) * sizeof (ret[0]));
|
||||
|
||||
for (i = 1; i <= fsi.max_id && j < fsi.num_devices; i++)
|
||||
{
|
||||
struct btrfs_ioctl_dev_info_args devi;
|
||||
memset (&devi, 0, sizeof (devi));
|
||||
devi.devid = i;
|
||||
if (ioctl (fd, BTRFS_IOC_DEV_INFO, &devi) < 0)
|
||||
{
|
||||
close (fd);
|
||||
free (ret);
|
||||
return NULL;
|
||||
}
|
||||
ret[j++] = xstrdup ((char *) devi.path);
|
||||
if (j >= fsi.num_devices)
|
||||
break;
|
||||
}
|
||||
close (fd);
|
||||
ret[j] = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
char **
|
||||
grub_find_root_devices_from_mountinfo (const char *dir, char **relroot)
|
||||
{
|
||||
FILE *fp;
|
||||
char *buf = NULL;
|
||||
size_t len = 0;
|
||||
grub_size_t entry_len = 0, entry_max = 4;
|
||||
struct mountinfo_entry *entries;
|
||||
struct mountinfo_entry parent_entry = { 0, 0, 0, "", "", "", "" };
|
||||
int i;
|
||||
|
||||
if (! *dir)
|
||||
dir = "/";
|
||||
if (relroot)
|
||||
*relroot = NULL;
|
||||
|
||||
fp = fopen ("/proc/self/mountinfo", "r");
|
||||
if (! fp)
|
||||
return NULL; /* fall through to other methods */
|
||||
|
||||
entries = xmalloc (entry_max * sizeof (*entries));
|
||||
|
||||
/* First, build a list of relevant visible mounts. */
|
||||
while (getline (&buf, &len, fp) > 0)
|
||||
{
|
||||
struct mountinfo_entry entry;
|
||||
int count;
|
||||
size_t enc_path_len;
|
||||
const char *sep;
|
||||
|
||||
if (sscanf (buf, "%d %d %u:%u %s %s%n",
|
||||
&entry.id, &parent_entry.id, &entry.major, &entry.minor,
|
||||
entry.enc_root, entry.enc_path, &count) < 6)
|
||||
continue;
|
||||
|
||||
unescape (entry.enc_root);
|
||||
unescape (entry.enc_path);
|
||||
|
||||
enc_path_len = strlen (entry.enc_path);
|
||||
/* Check that enc_path is a prefix of dir. The prefix must either be
|
||||
the entire string, or end with a slash, or be immediately followed
|
||||
by a slash. */
|
||||
if (strncmp (dir, entry.enc_path, enc_path_len) != 0 ||
|
||||
(enc_path_len && dir[enc_path_len - 1] != '/' &&
|
||||
dir[enc_path_len] && dir[enc_path_len] != '/'))
|
||||
continue;
|
||||
|
||||
sep = strstr (buf + count, " - ");
|
||||
if (!sep)
|
||||
continue;
|
||||
|
||||
sep += sizeof (" - ") - 1;
|
||||
if (sscanf (sep, "%s %s", entry.fstype, entry.device) != 2)
|
||||
continue;
|
||||
|
||||
unescape (entry.device);
|
||||
|
||||
/* Using the mount IDs, find out where this fits in the list of
|
||||
visible mount entries we've seen so far. There are three
|
||||
interesting cases. Firstly, it may be inserted at the end: this is
|
||||
the usual case of /foo/bar being mounted after /foo. Secondly, it
|
||||
may be inserted at the start: for example, this can happen for
|
||||
filesystems that are mounted before / and later moved under it.
|
||||
Thirdly, it may occlude part or all of the existing filesystem
|
||||
tree, in which case the end of the list needs to be pruned and this
|
||||
new entry will be inserted at the end. */
|
||||
if (entry_len >= entry_max)
|
||||
{
|
||||
entry_max <<= 1;
|
||||
entries = xrealloc (entries, entry_max * sizeof (*entries));
|
||||
}
|
||||
|
||||
if (!entry_len)
|
||||
{
|
||||
/* Initialise list. */
|
||||
entry_len = 2;
|
||||
entries[0] = parent_entry;
|
||||
entries[1] = entry;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = entry_len - 1; i >= 0; i--)
|
||||
{
|
||||
if (entries[i].id == parent_entry.id)
|
||||
{
|
||||
/* Insert at end, pruning anything previously above this. */
|
||||
entry_len = i + 2;
|
||||
entries[i + 1] = entry;
|
||||
break;
|
||||
}
|
||||
else if (i == 0 && entries[i].id == entry.id)
|
||||
{
|
||||
/* Insert at start. */
|
||||
entry_len++;
|
||||
memmove (entries + 1, entries,
|
||||
(entry_len - 1) * sizeof (*entries));
|
||||
entries[0] = parent_entry;
|
||||
entries[1] = entry;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Now scan visible mounts for the ones we're interested in. */
|
||||
for (i = entry_len - 1; i >= 0; i--)
|
||||
{
|
||||
char **ret = NULL;
|
||||
if (!*entries[i].device)
|
||||
continue;
|
||||
|
||||
if (grub_strcmp (entries[i].fstype, "fuse.zfs") == 0
|
||||
|| grub_strcmp (entries[i].fstype, "zfs") == 0)
|
||||
{
|
||||
char *slash;
|
||||
slash = strchr (entries[i].device, '/');
|
||||
if (slash)
|
||||
*slash = 0;
|
||||
ret = grub_util_find_root_devices_from_poolname (entries[i].device);
|
||||
if (slash)
|
||||
*slash = '/';
|
||||
if (relroot)
|
||||
{
|
||||
if (!slash)
|
||||
*relroot = xasprintf ("/@%s", entries[i].enc_root);
|
||||
else if (strchr (slash + 1, '@'))
|
||||
*relroot = xasprintf ("/%s%s", slash + 1, entries[i].enc_root);
|
||||
else
|
||||
*relroot = xasprintf ("/%s@%s", slash + 1, entries[i].enc_root);
|
||||
}
|
||||
}
|
||||
else if (grub_strcmp (entries[i].fstype, "btrfs") == 0)
|
||||
{
|
||||
ret = grub_find_root_devices_from_btrfs (dir);
|
||||
if (relroot)
|
||||
{
|
||||
char *ptr;
|
||||
*relroot = xmalloc (strlen (entries[i].enc_root) +
|
||||
2 + strlen (dir));
|
||||
ptr = grub_stpcpy (*relroot, entries[i].enc_root);
|
||||
if (strlen (dir) > strlen (entries[i].enc_path))
|
||||
{
|
||||
while (ptr > *relroot && *(ptr - 1) == '/')
|
||||
ptr--;
|
||||
if (dir[strlen (entries[i].enc_path)] != '/')
|
||||
*ptr++ = '/';
|
||||
ptr = grub_stpcpy (ptr, dir + strlen (entries[i].enc_path));
|
||||
}
|
||||
*ptr = 0;
|
||||
}
|
||||
}
|
||||
if (!ret)
|
||||
{
|
||||
ret = xmalloc (2 * sizeof (ret[0]));
|
||||
ret[0] = strdup (entries[i].device);
|
||||
ret[1] = 0;
|
||||
if (relroot)
|
||||
*relroot = strdup (entries[i].enc_root);
|
||||
}
|
||||
free (buf);
|
||||
free (entries);
|
||||
fclose (fp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
free (buf);
|
||||
free (entries);
|
||||
fclose (fp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char *
|
||||
get_mdadm_uuid (const char *os_dev)
|
||||
{
|
||||
char *argv[5];
|
||||
int fd;
|
||||
pid_t pid;
|
||||
FILE *mdadm;
|
||||
char *buf = NULL;
|
||||
size_t len = 0;
|
||||
char *name = NULL;
|
||||
|
||||
/* execvp has inconvenient types, hence the casts. None of these
|
||||
strings will actually be modified. */
|
||||
argv[0] = (char *) "mdadm";
|
||||
argv[1] = (char *) "--detail";
|
||||
argv[2] = (char *) "--export";
|
||||
argv[3] = (char *) os_dev;
|
||||
argv[4] = NULL;
|
||||
|
||||
pid = grub_util_exec_pipe (argv, &fd);
|
||||
|
||||
if (!pid)
|
||||
return NULL;
|
||||
|
||||
/* Parent. Read mdadm's output. */
|
||||
mdadm = fdopen (fd, "r");
|
||||
if (! mdadm)
|
||||
{
|
||||
grub_util_warn (_("Unable to open stream from %s: %s"),
|
||||
"mdadm", strerror (errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
while (getline (&buf, &len, mdadm) > 0)
|
||||
{
|
||||
if (strncmp (buf, "MD_UUID=", sizeof ("MD_UUID=") - 1) == 0)
|
||||
{
|
||||
char *name_start, *ptri, *ptro;
|
||||
|
||||
free (name);
|
||||
name_start = buf + sizeof ("MD_UUID=") - 1;
|
||||
ptro = name = xmalloc (strlen (name_start) + 1);
|
||||
for (ptri = name_start; *ptri && *ptri != '\n' && *ptri != '\r';
|
||||
ptri++)
|
||||
if ((*ptri >= '0' && *ptri <= '9')
|
||||
|| (*ptri >= 'a' && *ptri <= 'f')
|
||||
|| (*ptri >= 'A' && *ptri <= 'F'))
|
||||
*ptro++ = *ptri;
|
||||
*ptro = 0;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
close (fd);
|
||||
waitpid (pid, NULL, 0);
|
||||
free (buf);
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
static int
|
||||
grub_util_is_imsm (const char *os_dev)
|
||||
{
|
||||
int retry;
|
||||
int is_imsm = 0;
|
||||
int container_seen = 0;
|
||||
const char *dev = os_dev;
|
||||
|
||||
do
|
||||
{
|
||||
char *argv[5];
|
||||
int fd;
|
||||
pid_t pid;
|
||||
FILE *mdadm;
|
||||
char *buf = NULL;
|
||||
size_t len = 0;
|
||||
|
||||
retry = 0; /* We'll do one more pass if device is part of container */
|
||||
|
||||
/* execvp has inconvenient types, hence the casts. None of these
|
||||
strings will actually be modified. */
|
||||
argv[0] = (char *) "mdadm";
|
||||
argv[1] = (char *) "--detail";
|
||||
argv[2] = (char *) "--export";
|
||||
argv[3] = (char *) dev;
|
||||
argv[4] = NULL;
|
||||
|
||||
pid = grub_util_exec_pipe (argv, &fd);
|
||||
|
||||
if (!pid)
|
||||
{
|
||||
if (dev != os_dev)
|
||||
free ((void *) dev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Parent. Read mdadm's output. */
|
||||
mdadm = fdopen (fd, "r");
|
||||
if (! mdadm)
|
||||
{
|
||||
grub_util_warn (_("Unable to open stream from %s: %s"),
|
||||
"mdadm", strerror (errno));
|
||||
close (fd);
|
||||
waitpid (pid, NULL, 0);
|
||||
if (dev != os_dev)
|
||||
free ((void *) dev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (getline (&buf, &len, mdadm) > 0)
|
||||
{
|
||||
if (strncmp (buf, "MD_CONTAINER=", sizeof ("MD_CONTAINER=") - 1) == 0
|
||||
&& !container_seen)
|
||||
{
|
||||
char *newdev, *ptr;
|
||||
newdev = xstrdup (buf + sizeof ("MD_CONTAINER=") - 1);
|
||||
ptr = newdev + strlen (newdev) - 1;
|
||||
for (; ptr >= newdev && (*ptr == '\n' || *ptr == '\r'); ptr--);
|
||||
ptr[1] = 0;
|
||||
grub_util_info ("Container of %s is %s", dev, newdev);
|
||||
dev = newdev;
|
||||
container_seen = retry = 1;
|
||||
break;
|
||||
}
|
||||
if (strncmp (buf, "MD_METADATA=imsm",
|
||||
sizeof ("MD_METADATA=imsm") - 1) == 0)
|
||||
{
|
||||
is_imsm = 1;
|
||||
grub_util_info ("%s is imsm", dev);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free (buf);
|
||||
close (fd);
|
||||
waitpid (pid, NULL, 0);
|
||||
}
|
||||
while (retry);
|
||||
|
||||
if (dev != os_dev)
|
||||
free ((void *) dev);
|
||||
return is_imsm;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_part_to_disk (const char *os_dev, struct stat *st,
|
||||
int *is_part)
|
||||
{
|
||||
char *path = xmalloc (PATH_MAX);
|
||||
|
||||
if (! realpath (os_dev, path))
|
||||
return NULL;
|
||||
|
||||
if (strncmp ("/dev/", path, 5) == 0)
|
||||
{
|
||||
char *p = path + 5;
|
||||
|
||||
/* If this is an IDE disk. */
|
||||
if (strncmp ("ide/", p, 4) == 0)
|
||||
{
|
||||
p = strstr (p, "part");
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
strcpy (p, "disc");
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/* If this is a SCSI disk. */
|
||||
if (strncmp ("scsi/", p, 5) == 0)
|
||||
{
|
||||
p = strstr (p, "part");
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
strcpy (p, "disc");
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/* If this is a DAC960 disk. */
|
||||
if (strncmp ("rd/c", p, 4) == 0)
|
||||
{
|
||||
/* /dev/rd/c[0-9]+d[0-9]+(p[0-9]+)? */
|
||||
p = strchr (p, 'p');
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/* If this is a Mylex AcceleRAID Array. */
|
||||
if (strncmp ("rs/c", p, 4) == 0)
|
||||
{
|
||||
/* /dev/rd/c[0-9]+d[0-9]+(p[0-9]+)? */
|
||||
p = strchr (p, 'p');
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
/* If this is a CCISS disk. */
|
||||
if (strncmp ("cciss/c", p, sizeof ("cciss/c") - 1) == 0)
|
||||
{
|
||||
/* /dev/cciss/c[0-9]+d[0-9]+(p[0-9]+)? */
|
||||
p = strchr (p, 'p');
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/* If this is an AOE disk. */
|
||||
if (strncmp ("etherd/e", p, sizeof ("etherd/e") - 1) == 0)
|
||||
{
|
||||
/* /dev/etherd/e[0-9]+\.[0-9]+(p[0-9]+)? */
|
||||
p = strchr (p, 'p');
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/* If this is a Compaq Intelligent Drive Array. */
|
||||
if (strncmp ("ida/c", p, sizeof ("ida/c") - 1) == 0)
|
||||
{
|
||||
/* /dev/ida/c[0-9]+d[0-9]+(p[0-9]+)? */
|
||||
p = strchr (p, 'p');
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/* If this is an I2O disk. */
|
||||
if (strncmp ("i2o/hd", p, sizeof ("i2o/hd") - 1) == 0)
|
||||
{
|
||||
/* /dev/i2o/hd[a-z]([0-9]+)? */
|
||||
if (p[sizeof ("i2o/hda") - 1])
|
||||
*is_part = 1;
|
||||
p[sizeof ("i2o/hda") - 1] = '\0';
|
||||
return path;
|
||||
}
|
||||
|
||||
/* If this is a MultiMediaCard (MMC). */
|
||||
if (strncmp ("mmcblk", p, sizeof ("mmcblk") - 1) == 0)
|
||||
{
|
||||
/* /dev/mmcblk[0-9]+(p[0-9]+)? */
|
||||
p = strchr (p, 'p');
|
||||
if (p)
|
||||
{
|
||||
*is_part = 1;
|
||||
*p = '\0';
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
if (strncmp ("md", p, 2) == 0
|
||||
&& p[2] >= '0' && p[2] <= '9')
|
||||
{
|
||||
char *ptr = p + 2;
|
||||
while (*ptr >= '0' && *ptr <= '9')
|
||||
ptr++;
|
||||
if (*ptr)
|
||||
*is_part = 1;
|
||||
*ptr = 0;
|
||||
return path;
|
||||
}
|
||||
|
||||
if (strncmp ("nbd", p, 3) == 0
|
||||
&& p[3] >= '0' && p[3] <= '9')
|
||||
{
|
||||
char *ptr = p + 3;
|
||||
while (*ptr >= '0' && *ptr <= '9')
|
||||
ptr++;
|
||||
if (*ptr)
|
||||
*is_part = 1;
|
||||
*ptr = 0;
|
||||
return path;
|
||||
}
|
||||
|
||||
/* If this is an IDE, SCSI or Virtio disk. */
|
||||
if (strncmp ("vdisk", p, 5) == 0
|
||||
&& p[5] >= 'a' && p[5] <= 'z')
|
||||
{
|
||||
/* /dev/vdisk[a-z][0-9]* */
|
||||
if (p[6])
|
||||
*is_part = 1;
|
||||
p[6] = '\0';
|
||||
return path;
|
||||
}
|
||||
if ((strncmp ("hd", p, 2) == 0
|
||||
|| strncmp ("vd", p, 2) == 0
|
||||
|| strncmp ("sd", p, 2) == 0)
|
||||
&& p[2] >= 'a' && p[2] <= 'z')
|
||||
{
|
||||
char *pp = p + 2;
|
||||
while (*pp >= 'a' && *pp <= 'z')
|
||||
pp++;
|
||||
if (*pp)
|
||||
*is_part = 1;
|
||||
/* /dev/[hsv]d[a-z]+[0-9]* */
|
||||
*pp = '\0';
|
||||
return path;
|
||||
}
|
||||
|
||||
/* If this is a Xen virtual block device. */
|
||||
if ((strncmp ("xvd", p, 3) == 0) && p[3] >= 'a' && p[3] <= 'z')
|
||||
{
|
||||
char *pp = p + 3;
|
||||
while (*pp >= 'a' && *pp <= 'z')
|
||||
pp++;
|
||||
if (*pp)
|
||||
*is_part = 1;
|
||||
/* /dev/xvd[a-z]+[0-9]* */
|
||||
*pp = '\0';
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_raid_grub_dev (const char *os_dev)
|
||||
{
|
||||
char *grub_dev = NULL;
|
||||
if (os_dev[7] == '_' && os_dev[8] == 'd')
|
||||
{
|
||||
/* This a partitionable RAID device of the form /dev/md_dNNpMM. */
|
||||
|
||||
char *p, *q;
|
||||
|
||||
p = strdup (os_dev + sizeof ("/dev/md_d") - 1);
|
||||
|
||||
q = strchr (p, 'p');
|
||||
if (q)
|
||||
*q = ',';
|
||||
|
||||
grub_dev = xasprintf ("md%s", p);
|
||||
free (p);
|
||||
}
|
||||
else if (os_dev[7] == '/' && os_dev[8] == 'd')
|
||||
{
|
||||
/* This a partitionable RAID device of the form /dev/md/dNNpMM. */
|
||||
|
||||
char *p, *q;
|
||||
|
||||
p = strdup (os_dev + sizeof ("/dev/md/d") - 1);
|
||||
|
||||
q = strchr (p, 'p');
|
||||
if (q)
|
||||
*q = ',';
|
||||
|
||||
grub_dev = xasprintf ("md%s", p);
|
||||
free (p);
|
||||
}
|
||||
else if (os_dev[7] >= '0' && os_dev[7] <= '9')
|
||||
{
|
||||
char *p , *q;
|
||||
|
||||
p = strdup (os_dev + sizeof ("/dev/md") - 1);
|
||||
|
||||
q = strchr (p, 'p');
|
||||
if (q)
|
||||
*q = ',';
|
||||
|
||||
grub_dev = xasprintf ("md%s", p);
|
||||
free (p);
|
||||
}
|
||||
else if (os_dev[7] == '/' && os_dev[8] >= '0' && os_dev[8] <= '9')
|
||||
{
|
||||
char *p , *q;
|
||||
|
||||
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
|
||||
|
||||
q = strchr (p, 'p');
|
||||
if (q)
|
||||
*q = ',';
|
||||
|
||||
grub_dev = xasprintf ("md%s", p);
|
||||
free (p);
|
||||
}
|
||||
else if (os_dev[7] == '/')
|
||||
{
|
||||
/* mdraid 1.x with a free name. */
|
||||
char *p , *q;
|
||||
|
||||
p = strdup (os_dev + sizeof ("/dev/md/") - 1);
|
||||
|
||||
q = strchr (p, 'p');
|
||||
if (q)
|
||||
*q = ',';
|
||||
|
||||
grub_dev = xasprintf ("md/%s", p);
|
||||
free (p);
|
||||
}
|
||||
else
|
||||
grub_util_error (_("unknown kind of RAID device `%s'"), os_dev);
|
||||
|
||||
{
|
||||
char *mdadm_name = get_mdadm_uuid (os_dev);
|
||||
|
||||
if (mdadm_name)
|
||||
{
|
||||
const char *q;
|
||||
|
||||
for (q = os_dev + strlen (os_dev) - 1; q >= os_dev
|
||||
&& grub_isdigit (*q); q--);
|
||||
|
||||
if (q >= os_dev && *q == 'p')
|
||||
{
|
||||
free (grub_dev);
|
||||
grub_dev = xasprintf ("mduuid/%s,%s", mdadm_name, q + 1);
|
||||
goto done;
|
||||
}
|
||||
free (grub_dev);
|
||||
grub_dev = xasprintf ("mduuid/%s", mdadm_name);
|
||||
|
||||
done:
|
||||
free (mdadm_name);
|
||||
}
|
||||
}
|
||||
return grub_dev;
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dev_abstraction_os (const char *os_dev)
|
||||
{
|
||||
#ifndef HAVE_DEVICE_MAPPER
|
||||
if ((strncmp ("/dev/mapper/", os_dev, 12) == 0))
|
||||
return GRUB_DEV_ABSTRACTION_LVM;
|
||||
#endif
|
||||
|
||||
/* Check for RAID. */
|
||||
if (!strncmp (os_dev, "/dev/md", 7) && ! grub_util_device_is_mapped (os_dev)
|
||||
&& !grub_util_is_imsm (os_dev))
|
||||
return GRUB_DEV_ABSTRACTION_RAID;
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_pull_device_os (const char *os_dev,
|
||||
enum grub_dev_abstraction_types ab)
|
||||
{
|
||||
switch (ab)
|
||||
{
|
||||
case GRUB_DEV_ABSTRACTION_RAID:
|
||||
{
|
||||
char **devicelist = grub_util_raid_getmembers (os_dev, 0);
|
||||
int i;
|
||||
for (i = 0; devicelist[i];i++)
|
||||
{
|
||||
grub_util_pull_device (devicelist[i]);
|
||||
free (devicelist[i]);
|
||||
}
|
||||
free (devicelist);
|
||||
}
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_grub_dev_os (const char *os_dev)
|
||||
{
|
||||
char *grub_dev = NULL;
|
||||
|
||||
switch (grub_util_get_dev_abstraction (os_dev))
|
||||
{
|
||||
/* Fallback for non-devmapper build. In devmapper-builds LVM is handled
|
||||
in rub_util_get_devmapper_grub_dev and this point isn't reached.
|
||||
*/
|
||||
case GRUB_DEV_ABSTRACTION_LVM:
|
||||
{
|
||||
unsigned short len;
|
||||
grub_size_t offset = sizeof (LVM_DEV_MAPPER_STRING) - 1;
|
||||
|
||||
len = strlen (os_dev) - offset + 1;
|
||||
grub_dev = xmalloc (len + sizeof ("lvm/"));
|
||||
|
||||
grub_memcpy (grub_dev, "lvm/", sizeof ("lvm/") - 1);
|
||||
grub_memcpy (grub_dev + sizeof ("lvm/") - 1, os_dev + offset, len);
|
||||
}
|
||||
break;
|
||||
|
||||
case GRUB_DEV_ABSTRACTION_RAID:
|
||||
grub_dev = grub_util_get_raid_grub_dev (os_dev);
|
||||
break;
|
||||
|
||||
default: /* GRUB_DEV_ABSTRACTION_NONE */
|
||||
break;
|
||||
}
|
||||
|
||||
return grub_dev;
|
||||
}
|
5
grub-core/osdep/random.c
Normal file
5
grub-core/osdep/random.c
Normal file
|
@ -0,0 +1,5 @@
|
|||
#if defined (_WIN32) || defined (__CYGWIN__)
|
||||
#include "windows/random.c"
|
||||
#else
|
||||
#include "unix/random.c"
|
||||
#endif
|
119
grub-core/osdep/sun/getroot.c
Normal file
119
grub-core/osdep/sun/getroot.c
Normal file
|
@ -0,0 +1,119 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include <grub/types.h>
|
||||
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
|
||||
#include <sys/wait.h>
|
||||
|
||||
# include <sys/types.h>
|
||||
# include <sys/mkdev.h>
|
||||
# include <sys/dkio.h>
|
||||
|
||||
|
||||
char *
|
||||
grub_util_part_to_disk (const char *os_dev,
|
||||
struct stat *st __attribute__ ((unused)),
|
||||
int *is_part)
|
||||
{
|
||||
char *colon = grub_strrchr (os_dev, ':');
|
||||
if (grub_memcmp (os_dev, "/devices", sizeof ("/devices") - 1) == 0
|
||||
&& colon)
|
||||
{
|
||||
char *ret = xmalloc (colon - os_dev + sizeof (":q,raw"));
|
||||
if (grub_strcmp (colon, ":q,raw") != 0)
|
||||
*is_part = 1;
|
||||
grub_memcpy (ret, os_dev, colon - os_dev);
|
||||
grub_memcpy (ret + (colon - os_dev), ":q,raw", sizeof (":q,raw"));
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
return xstrdup (os_dev);
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
|
||||
{
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
|
||||
enum grub_dev_abstraction_types ab __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
grub_disk_addr_t
|
||||
grub_util_find_partition_start_os (const char *dev)
|
||||
{
|
||||
int fd;
|
||||
struct extpart_info pinfo;
|
||||
|
||||
fd = open (dev, O_RDONLY);
|
||||
if (fd == -1)
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"),
|
||||
dev, strerror (errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ioctl (fd, DKIOCEXTPARTINFO, &pinfo))
|
||||
{
|
||||
grub_error (GRUB_ERR_BAD_DEVICE,
|
||||
"cannot get disk geometry of `%s'", dev);
|
||||
close (fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
close (fd);
|
||||
|
||||
return pinfo.p_start;
|
||||
}
|
967
grub-core/osdep/unix/getroot.c
Normal file
967
grub-core/osdep/unix/getroot.c
Normal file
|
@ -0,0 +1,967 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/cryptodisk.h>
|
||||
#include <grub/i18n.h>
|
||||
|
||||
#if !defined (__MINGW32__) && !defined (__CYGWIN__) && !defined (__AROS__)
|
||||
|
||||
#ifdef __linux__
|
||||
#include <sys/ioctl.h> /* ioctl */
|
||||
#include <sys/mount.h>
|
||||
#ifndef MAJOR
|
||||
# ifndef MINORBITS
|
||||
# define MINORBITS 8
|
||||
# endif /* ! MINORBITS */
|
||||
# define MAJOR(dev) ((unsigned) ((dev) >> MINORBITS))
|
||||
#endif /* ! MAJOR */
|
||||
#ifndef FLOPPY_MAJOR
|
||||
# define FLOPPY_MAJOR 2
|
||||
#endif /* ! FLOPPY_MAJOR */
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
|
||||
# include <grub/util/libzfs.h>
|
||||
# include <grub/util/libnvpair.h>
|
||||
#endif
|
||||
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||
# define MAJOR(dev) major(dev)
|
||||
# define FLOPPY_MAJOR 2
|
||||
#endif
|
||||
|
||||
#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
|
||||
#include <sys/mount.h>
|
||||
#endif
|
||||
|
||||
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
||||
# include <sys/ioctl.h>
|
||||
# include <sys/disklabel.h> /* struct disklabel */
|
||||
# include <sys/disk.h> /* struct dkwedge_info */
|
||||
#include <sys/param.h>
|
||||
#include <sys/mount.h>
|
||||
#endif /* defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) */
|
||||
|
||||
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
||||
# define MAJOR(dev) major(dev)
|
||||
# ifdef HAVE_GETRAWPARTITION
|
||||
# include <util.h> /* getrawpartition */
|
||||
# endif /* HAVE_GETRAWPARTITION */
|
||||
#if defined(__NetBSD__)
|
||||
# include <sys/fdio.h>
|
||||
#endif
|
||||
# ifndef FLOPPY_MAJOR
|
||||
# define FLOPPY_MAJOR 2
|
||||
# endif /* ! FLOPPY_MAJOR */
|
||||
# ifndef RAW_FLOPPY_MAJOR
|
||||
# define RAW_FLOPPY_MAJOR 9
|
||||
# endif /* ! RAW_FLOPPY_MAJOR */
|
||||
#endif /* defined(__NetBSD__) */
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static void
|
||||
strip_extra_slashes (char *dir)
|
||||
{
|
||||
char *p = dir;
|
||||
|
||||
while ((p = strchr (p, '/')) != 0)
|
||||
{
|
||||
if (p[1] == '/')
|
||||
{
|
||||
memmove (p, p + 1, strlen (p));
|
||||
continue;
|
||||
}
|
||||
else if (p[1] == '\0')
|
||||
{
|
||||
if (p > dir)
|
||||
p[0] = '\0';
|
||||
break;
|
||||
}
|
||||
|
||||
p++;
|
||||
}
|
||||
}
|
||||
|
||||
static char *
|
||||
xgetcwd (void)
|
||||
{
|
||||
size_t size = 10;
|
||||
char *path;
|
||||
|
||||
path = xmalloc (size);
|
||||
while (! getcwd (path, size))
|
||||
{
|
||||
size <<= 1;
|
||||
path = xrealloc (path, size);
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
pid_t
|
||||
grub_util_exec_pipe (char **argv, int *fd)
|
||||
{
|
||||
int mdadm_pipe[2];
|
||||
pid_t mdadm_pid;
|
||||
|
||||
*fd = 0;
|
||||
|
||||
if (pipe (mdadm_pipe) < 0)
|
||||
{
|
||||
grub_util_warn (_("Unable to create pipe: %s"),
|
||||
strerror (errno));
|
||||
return 0;
|
||||
}
|
||||
mdadm_pid = fork ();
|
||||
if (mdadm_pid < 0)
|
||||
grub_util_error (_("Unable to fork: %s"), strerror (errno));
|
||||
else if (mdadm_pid == 0)
|
||||
{
|
||||
/* Child. */
|
||||
|
||||
/* Close fd's. */
|
||||
grub_util_devmapper_cleanup ();
|
||||
grub_diskfilter_fini ();
|
||||
|
||||
/* Ensure child is not localised. */
|
||||
setenv ("LC_ALL", "C", 1);
|
||||
|
||||
close (mdadm_pipe[0]);
|
||||
dup2 (mdadm_pipe[1], STDOUT_FILENO);
|
||||
close (mdadm_pipe[1]);
|
||||
|
||||
execvp (argv[0], argv);
|
||||
exit (127);
|
||||
}
|
||||
else
|
||||
{
|
||||
close (mdadm_pipe[1]);
|
||||
*fd = mdadm_pipe[0];
|
||||
return mdadm_pid;
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined (__GNU__)
|
||||
char **
|
||||
grub_util_find_root_devices_from_poolname (char *poolname)
|
||||
{
|
||||
char **devices = 0;
|
||||
size_t ndevices = 0;
|
||||
size_t devices_allocated = 0;
|
||||
|
||||
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
|
||||
zpool_handle_t *zpool;
|
||||
libzfs_handle_t *libzfs;
|
||||
nvlist_t *config, *vdev_tree;
|
||||
nvlist_t **children;
|
||||
unsigned int nvlist_count;
|
||||
unsigned int i;
|
||||
char *device = 0;
|
||||
|
||||
libzfs = grub_get_libzfs_handle ();
|
||||
if (! libzfs)
|
||||
return NULL;
|
||||
|
||||
zpool = zpool_open (libzfs, poolname);
|
||||
config = zpool_get_config (zpool, NULL);
|
||||
|
||||
if (nvlist_lookup_nvlist (config, "vdev_tree", &vdev_tree) != 0)
|
||||
error (1, errno, "nvlist_lookup_nvlist (\"vdev_tree\")");
|
||||
|
||||
if (nvlist_lookup_nvlist_array (vdev_tree, "children", &children, &nvlist_count) != 0)
|
||||
error (1, errno, "nvlist_lookup_nvlist_array (\"children\")");
|
||||
assert (nvlist_count > 0);
|
||||
|
||||
while (nvlist_lookup_nvlist_array (children[0], "children",
|
||||
&children, &nvlist_count) == 0)
|
||||
assert (nvlist_count > 0);
|
||||
|
||||
for (i = 0; i < nvlist_count; i++)
|
||||
{
|
||||
if (nvlist_lookup_string (children[i], "path", &device) != 0)
|
||||
error (1, errno, "nvlist_lookup_string (\"path\")");
|
||||
|
||||
struct stat st;
|
||||
if (stat (device, &st) == 0)
|
||||
{
|
||||
#ifdef __sun__
|
||||
if (grub_memcmp (device, "/dev/dsk/", sizeof ("/dev/dsk/") - 1)
|
||||
== 0)
|
||||
device = xasprintf ("/dev/rdsk/%s",
|
||||
device + sizeof ("/dev/dsk/") - 1);
|
||||
else if (grub_memcmp (device, "/devices", sizeof ("/devices") - 1)
|
||||
== 0
|
||||
&& grub_memcmp (device + strlen (device) - 4,
|
||||
",raw", 4) != 0)
|
||||
device = xasprintf ("%s,raw", device);
|
||||
else
|
||||
#endif
|
||||
device = xstrdup (device);
|
||||
if (ndevices >= devices_allocated)
|
||||
{
|
||||
devices_allocated = 2 * (devices_allocated + 8);
|
||||
devices = xrealloc (devices, sizeof (devices[0])
|
||||
* devices_allocated);
|
||||
}
|
||||
devices[ndevices++] = device;
|
||||
}
|
||||
|
||||
device = NULL;
|
||||
}
|
||||
|
||||
zpool_close (zpool);
|
||||
#else
|
||||
FILE *fp;
|
||||
int ret;
|
||||
char *line;
|
||||
size_t len;
|
||||
int st;
|
||||
|
||||
char name[PATH_MAX + 1], state[257], readlen[257], writelen[257];
|
||||
char cksum[257], notes[257];
|
||||
unsigned int dummy;
|
||||
char *argv[4];
|
||||
pid_t pid;
|
||||
int fd;
|
||||
|
||||
/* execvp has inconvenient types, hence the casts. None of these
|
||||
strings will actually be modified. */
|
||||
argv[0] = (char *) "zpool";
|
||||
argv[1] = (char *) "status";
|
||||
argv[2] = (char *) poolname;
|
||||
argv[3] = NULL;
|
||||
|
||||
pid = grub_util_exec_pipe (argv, &fd);
|
||||
if (!pid)
|
||||
return NULL;
|
||||
|
||||
fp = fdopen (fd, "r");
|
||||
if (!fp)
|
||||
{
|
||||
grub_util_warn (_("Unable to open stream from %s: %s"),
|
||||
"zpool", strerror (errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
st = 0;
|
||||
while (1)
|
||||
{
|
||||
line = NULL;
|
||||
ret = getline (&line, &len, fp);
|
||||
if (ret == -1)
|
||||
break;
|
||||
|
||||
if (sscanf (line, " %s %256s %256s %256s %256s %256s",
|
||||
name, state, readlen, writelen, cksum, notes) >= 5)
|
||||
switch (st)
|
||||
{
|
||||
case 0:
|
||||
if (!strcmp (name, "NAME")
|
||||
&& !strcmp (state, "STATE")
|
||||
&& !strcmp (readlen, "READ")
|
||||
&& !strcmp (writelen, "WRITE")
|
||||
&& !strcmp (cksum, "CKSUM"))
|
||||
st++;
|
||||
break;
|
||||
case 1:
|
||||
{
|
||||
char *ptr = line;
|
||||
while (1)
|
||||
{
|
||||
if (strncmp (ptr, poolname, strlen (poolname)) == 0
|
||||
&& grub_isspace(ptr[strlen (poolname)]))
|
||||
st++;
|
||||
if (!grub_isspace (*ptr))
|
||||
break;
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (strcmp (name, "mirror") && !sscanf (name, "mirror-%u", &dummy)
|
||||
&& !sscanf (name, "raidz%u", &dummy)
|
||||
&& !sscanf (name, "raidz1%u", &dummy)
|
||||
&& !sscanf (name, "raidz2%u", &dummy)
|
||||
&& !sscanf (name, "raidz3%u", &dummy)
|
||||
&& !strcmp (state, "ONLINE"))
|
||||
{
|
||||
if (ndevices >= devices_allocated)
|
||||
{
|
||||
devices_allocated = 2 * (devices_allocated + 8);
|
||||
devices = xrealloc (devices, sizeof (devices[0])
|
||||
* devices_allocated);
|
||||
}
|
||||
if (name[0] == '/')
|
||||
devices[ndevices++] = xstrdup (name);
|
||||
else
|
||||
devices[ndevices++] = xasprintf ("/dev/%s", name);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
free (line);
|
||||
}
|
||||
|
||||
out:
|
||||
close (fd);
|
||||
waitpid (pid, NULL, 0);
|
||||
#endif
|
||||
if (devices)
|
||||
{
|
||||
if (ndevices >= devices_allocated)
|
||||
{
|
||||
devices_allocated = 2 * (devices_allocated + 8);
|
||||
devices = xrealloc (devices, sizeof (devices[0])
|
||||
* devices_allocated);
|
||||
}
|
||||
devices[ndevices++] = 0;
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
|
||||
static char **
|
||||
find_root_devices_from_libzfs (const char *dir)
|
||||
{
|
||||
char **devices = NULL;
|
||||
char *poolname;
|
||||
char *poolfs;
|
||||
|
||||
grub_find_zpool_from_dir (dir, &poolname, &poolfs);
|
||||
if (! poolname)
|
||||
return NULL;
|
||||
|
||||
devices = grub_util_find_root_devices_from_poolname (poolname);
|
||||
|
||||
free (poolname);
|
||||
if (poolfs)
|
||||
free (poolfs);
|
||||
|
||||
return devices;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_find_device (const char *dir, dev_t dev)
|
||||
{
|
||||
DIR *dp;
|
||||
char *saved_cwd;
|
||||
struct dirent *ent;
|
||||
|
||||
if (! dir)
|
||||
dir = "/dev";
|
||||
|
||||
dp = opendir (dir);
|
||||
if (! dp)
|
||||
return 0;
|
||||
|
||||
saved_cwd = xgetcwd ();
|
||||
|
||||
grub_util_info ("changing current directory to %s", dir);
|
||||
if (chdir (dir) < 0)
|
||||
{
|
||||
free (saved_cwd);
|
||||
closedir (dp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((ent = readdir (dp)) != 0)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
/* Avoid:
|
||||
- dotfiles (like "/dev/.tmp.md0") since they could be duplicates.
|
||||
- dotdirs (like "/dev/.static") since they could contain duplicates. */
|
||||
if (ent->d_name[0] == '.')
|
||||
continue;
|
||||
|
||||
if (lstat (ent->d_name, &st) < 0)
|
||||
/* Ignore any error. */
|
||||
continue;
|
||||
|
||||
if (S_ISLNK (st.st_mode)) {
|
||||
#ifdef __linux__
|
||||
if (strcmp (dir, "mapper") == 0 || strcmp (dir, "/dev/mapper") == 0) {
|
||||
/* Follow symbolic links under /dev/mapper/; the canonical name
|
||||
may be something like /dev/dm-0, but the names under
|
||||
/dev/mapper/ are more human-readable and so we prefer them if
|
||||
we can get them. */
|
||||
if (stat (ent->d_name, &st) < 0)
|
||||
continue;
|
||||
} else
|
||||
#endif /* __linux__ */
|
||||
/* Don't follow other symbolic links. */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (S_ISDIR (st.st_mode))
|
||||
{
|
||||
/* Find it recursively. */
|
||||
char *res;
|
||||
|
||||
res = grub_find_device (ent->d_name, dev);
|
||||
|
||||
if (res)
|
||||
{
|
||||
if (chdir (saved_cwd) < 0)
|
||||
grub_util_error ("%s",
|
||||
_("cannot restore the original directory"));
|
||||
|
||||
free (saved_cwd);
|
||||
closedir (dp);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__)
|
||||
if (S_ISCHR (st.st_mode) && st.st_rdev == dev)
|
||||
#else
|
||||
if (S_ISBLK (st.st_mode) && st.st_rdev == dev)
|
||||
#endif
|
||||
{
|
||||
#ifdef __linux__
|
||||
/* Skip device names like /dev/dm-0, which are short-hand aliases
|
||||
to more descriptive device names, e.g. those under /dev/mapper */
|
||||
if (ent->d_name[0] == 'd' &&
|
||||
ent->d_name[1] == 'm' &&
|
||||
ent->d_name[2] == '-' &&
|
||||
ent->d_name[3] >= '0' &&
|
||||
ent->d_name[3] <= '9')
|
||||
continue;
|
||||
#endif
|
||||
|
||||
/* Found! */
|
||||
char *res;
|
||||
char *cwd;
|
||||
#if defined(__NetBSD__) || defined(__OpenBSD__)
|
||||
/* Convert this block device to its character (raw) device. */
|
||||
const char *template = "%s/r%s";
|
||||
#else
|
||||
/* Keep the device name as it is. */
|
||||
const char *template = "%s/%s";
|
||||
#endif
|
||||
|
||||
cwd = xgetcwd ();
|
||||
res = xmalloc (strlen (cwd) + strlen (ent->d_name) + 3);
|
||||
sprintf (res, template, cwd, ent->d_name);
|
||||
strip_extra_slashes (res);
|
||||
free (cwd);
|
||||
|
||||
/* /dev/root is not a real block device keep looking, takes care
|
||||
of situation where root filesystem is on the same partition as
|
||||
grub files */
|
||||
|
||||
if (strcmp(res, "/dev/root") == 0)
|
||||
{
|
||||
free (res);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (chdir (saved_cwd) < 0)
|
||||
grub_util_error ("%s", _("cannot restore the original directory"));
|
||||
|
||||
free (saved_cwd);
|
||||
closedir (dp);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
if (chdir (saved_cwd) < 0)
|
||||
grub_util_error ("%s", _("cannot restore the original directory"));
|
||||
|
||||
free (saved_cwd);
|
||||
closedir (dp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char **
|
||||
grub_guess_root_devices (const char *dir)
|
||||
{
|
||||
char **os_dev = NULL;
|
||||
struct stat st;
|
||||
dev_t dev;
|
||||
|
||||
#ifdef __linux__
|
||||
if (!os_dev)
|
||||
os_dev = grub_find_root_devices_from_mountinfo (dir, NULL);
|
||||
#endif /* __linux__ */
|
||||
|
||||
if (!os_dev)
|
||||
os_dev = find_root_devices_from_libzfs (dir);
|
||||
|
||||
if (os_dev)
|
||||
{
|
||||
char **cur;
|
||||
for (cur = os_dev; *cur; cur++)
|
||||
{
|
||||
char *tmp = *cur;
|
||||
int root, dm;
|
||||
if (strcmp (*cur, "/dev/root") == 0
|
||||
|| strncmp (*cur, "/dev/dm-", sizeof ("/dev/dm-") - 1) == 0)
|
||||
*cur = tmp;
|
||||
else
|
||||
{
|
||||
*cur = canonicalize_file_name (tmp);
|
||||
if (*cur == NULL)
|
||||
grub_util_error (_("failed to get canonical path of `%s'"), tmp);
|
||||
free (tmp);
|
||||
}
|
||||
root = (strcmp (*cur, "/dev/root") == 0);
|
||||
dm = (strncmp (*cur, "/dev/dm-", sizeof ("/dev/dm-") - 1) == 0);
|
||||
if (!dm && !root)
|
||||
continue;
|
||||
if (stat (*cur, &st) < 0)
|
||||
break;
|
||||
free (*cur);
|
||||
dev = st.st_rdev;
|
||||
*cur = grub_find_device (dm ? "/dev/mapper" : "/dev", dev);
|
||||
}
|
||||
if (!*cur)
|
||||
return os_dev;
|
||||
for (cur = os_dev; *cur; cur++)
|
||||
free (*cur);
|
||||
free (os_dev);
|
||||
os_dev = 0;
|
||||
}
|
||||
|
||||
if (stat (dir, &st) < 0)
|
||||
grub_util_error (_("cannot stat `%s': %s"), dir, strerror (errno));
|
||||
|
||||
dev = st.st_dev;
|
||||
|
||||
os_dev = xmalloc (2 * sizeof (os_dev[0]));
|
||||
|
||||
/* This might be truly slow, but is there any better way? */
|
||||
os_dev[0] = grub_find_device ("/dev", dev);
|
||||
|
||||
if (!os_dev[0])
|
||||
{
|
||||
free (os_dev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
os_dev[1] = 0;
|
||||
|
||||
return os_dev;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void
|
||||
grub_util_pull_lvm_by_command (const char *os_dev)
|
||||
{
|
||||
char *argv[8];
|
||||
int fd;
|
||||
pid_t pid;
|
||||
FILE *mdadm;
|
||||
char *buf = NULL;
|
||||
size_t len = 0;
|
||||
char *vgname = NULL;
|
||||
const char *iptr;
|
||||
char *optr;
|
||||
char *vgid = NULL;
|
||||
grub_size_t vgidlen = 0;
|
||||
|
||||
vgid = grub_util_get_vg_uuid (os_dev);
|
||||
if (vgid)
|
||||
vgidlen = grub_strlen (vgid);
|
||||
|
||||
if (!vgid)
|
||||
{
|
||||
if (strncmp (os_dev, LVM_DEV_MAPPER_STRING,
|
||||
sizeof (LVM_DEV_MAPPER_STRING) - 1)
|
||||
!= 0)
|
||||
return;
|
||||
|
||||
vgname = xmalloc (strlen (os_dev + sizeof (LVM_DEV_MAPPER_STRING) - 1) + 1);
|
||||
for (iptr = os_dev + sizeof (LVM_DEV_MAPPER_STRING) - 1, optr = vgname; *iptr; )
|
||||
if (*iptr != '-')
|
||||
*optr++ = *iptr++;
|
||||
else if (iptr[0] == '-' && iptr[1] == '-')
|
||||
{
|
||||
iptr += 2;
|
||||
*optr++ = '-';
|
||||
}
|
||||
else
|
||||
break;
|
||||
*optr = '\0';
|
||||
}
|
||||
|
||||
/* execvp has inconvenient types, hence the casts. None of these
|
||||
strings will actually be modified. */
|
||||
/* by default PV name is left aligned in 10 character field, meaning that
|
||||
we do not know where name ends. Using dummy --separator disables
|
||||
alignment. We have a single field, so separator itself is not output */
|
||||
argv[0] = (char *) "vgs";
|
||||
argv[1] = (char *) "--options";
|
||||
if (vgid)
|
||||
argv[2] = (char *) "vg_uuid,pv_name";
|
||||
else
|
||||
argv[2] = (char *) "pv_name";
|
||||
argv[3] = (char *) "--noheadings";
|
||||
argv[4] = (char *) "--separator";
|
||||
argv[5] = (char *) ":";
|
||||
argv[6] = vgname;
|
||||
argv[7] = NULL;
|
||||
|
||||
pid = grub_util_exec_pipe (argv, &fd);
|
||||
free (vgname);
|
||||
|
||||
if (!pid)
|
||||
return;
|
||||
|
||||
/* Parent. Read mdadm's output. */
|
||||
mdadm = fdopen (fd, "r");
|
||||
if (! mdadm)
|
||||
{
|
||||
grub_util_warn (_("Unable to open stream from %s: %s"),
|
||||
"vgs", strerror (errno));
|
||||
goto out;
|
||||
}
|
||||
|
||||
while (getline (&buf, &len, mdadm) > 0)
|
||||
{
|
||||
char *ptr;
|
||||
/* LVM adds two spaces as standard prefix */
|
||||
for (ptr = buf; ptr < buf + 2 && *ptr == ' '; ptr++);
|
||||
|
||||
if (vgid && (grub_strncmp (vgid, ptr, vgidlen) != 0
|
||||
|| ptr[vgidlen] != ':'))
|
||||
continue;
|
||||
if (vgid)
|
||||
ptr += vgidlen + 1;
|
||||
if (*ptr == '\0')
|
||||
continue;
|
||||
*(ptr + strlen (ptr) - 1) = '\0';
|
||||
grub_util_pull_device (ptr);
|
||||
}
|
||||
|
||||
out:
|
||||
close (fd);
|
||||
waitpid (pid, NULL, 0);
|
||||
free (buf);
|
||||
}
|
||||
|
||||
/* ZFS has similar problems to those of btrfs (see above). */
|
||||
void
|
||||
grub_find_zpool_from_dir (const char *dir, char **poolname, char **poolfs)
|
||||
{
|
||||
char *slash;
|
||||
|
||||
*poolname = *poolfs = NULL;
|
||||
|
||||
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && defined(HAVE_STRUCT_STATFS_F_MNTFROMNAME)
|
||||
/* FreeBSD and GNU/kFreeBSD. */
|
||||
{
|
||||
struct statfs mnt;
|
||||
|
||||
if (statfs (dir, &mnt) != 0)
|
||||
return;
|
||||
|
||||
if (strcmp (mnt.f_fstypename, "zfs") != 0)
|
||||
return;
|
||||
|
||||
*poolname = xstrdup (mnt.f_mntfromname);
|
||||
}
|
||||
#elif defined(HAVE_GETEXTMNTENT)
|
||||
/* Solaris. */
|
||||
{
|
||||
struct stat st;
|
||||
struct extmnttab mnt;
|
||||
|
||||
if (stat (dir, &st) != 0)
|
||||
return;
|
||||
|
||||
FILE *mnttab = fopen ("/etc/mnttab", "r");
|
||||
if (! mnttab)
|
||||
return;
|
||||
|
||||
while (getextmntent (mnttab, &mnt, sizeof (mnt)) == 0)
|
||||
{
|
||||
if (makedev (mnt.mnt_major, mnt.mnt_minor) == st.st_dev
|
||||
&& !strcmp (mnt.mnt_fstype, "zfs"))
|
||||
{
|
||||
*poolname = xstrdup (mnt.mnt_special);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fclose (mnttab);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (! *poolname)
|
||||
return;
|
||||
|
||||
slash = strchr (*poolname, '/');
|
||||
if (slash)
|
||||
{
|
||||
*slash = '\0';
|
||||
*poolfs = xstrdup (slash + 1);
|
||||
}
|
||||
else
|
||||
*poolfs = xstrdup ("");
|
||||
}
|
||||
|
||||
/* This function never prints trailing slashes (so that its output
|
||||
can be appended a slash unconditionally). */
|
||||
char *
|
||||
grub_make_system_path_relative_to_its_root (const char *path)
|
||||
{
|
||||
struct stat st;
|
||||
char *p, *buf, *buf2, *buf3, *ret;
|
||||
uintptr_t offset = 0;
|
||||
dev_t num;
|
||||
size_t len;
|
||||
char *poolfs = NULL;
|
||||
|
||||
/* canonicalize. */
|
||||
p = canonicalize_file_name (path);
|
||||
if (p == NULL)
|
||||
grub_util_error (_("failed to get canonical path of `%s'"), path);
|
||||
|
||||
/* For ZFS sub-pool filesystems, could be extended to others (btrfs?). */
|
||||
{
|
||||
char *dummy;
|
||||
grub_find_zpool_from_dir (p, &dummy, &poolfs);
|
||||
}
|
||||
|
||||
len = strlen (p) + 1;
|
||||
buf = xstrdup (p);
|
||||
free (p);
|
||||
|
||||
if (stat (buf, &st) < 0)
|
||||
grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno));
|
||||
|
||||
buf2 = xstrdup (buf);
|
||||
num = st.st_dev;
|
||||
|
||||
/* This loop sets offset to the number of chars of the root
|
||||
directory we're inspecting. */
|
||||
while (1)
|
||||
{
|
||||
p = strrchr (buf, '/');
|
||||
if (p == NULL)
|
||||
/* This should never happen. */
|
||||
grub_util_error ("%s",
|
||||
/* TRANSLATORS: canonical pathname is the
|
||||
complete one e.g. /etc/fstab. It has
|
||||
to contain `/' normally, if it doesn't
|
||||
we're in trouble and throw this error. */
|
||||
_("no `/' in canonical filename"));
|
||||
if (p != buf)
|
||||
*p = 0;
|
||||
else
|
||||
*++p = 0;
|
||||
|
||||
if (stat (buf, &st) < 0)
|
||||
grub_util_error (_("cannot stat `%s': %s"), buf, strerror (errno));
|
||||
|
||||
/* buf is another filesystem; we found it. */
|
||||
if (st.st_dev != num)
|
||||
{
|
||||
/* offset == 0 means path given is the mount point.
|
||||
This works around special-casing of "/" in Un*x. This function never
|
||||
prints trailing slashes (so that its output can be appended a slash
|
||||
unconditionally). Each slash in is considered a preceding slash, and
|
||||
therefore the root directory is an empty string. */
|
||||
if (offset == 0)
|
||||
{
|
||||
free (buf);
|
||||
#ifdef __linux__
|
||||
{
|
||||
char *bind;
|
||||
grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind));
|
||||
if (bind && bind[0] && bind[1])
|
||||
{
|
||||
buf3 = bind;
|
||||
goto parsedir;
|
||||
}
|
||||
grub_free (bind);
|
||||
}
|
||||
#endif
|
||||
free (buf2);
|
||||
if (poolfs)
|
||||
return xasprintf ("/%s/@", poolfs);
|
||||
return xstrdup ("");
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
offset = p - buf;
|
||||
/* offset == 1 means root directory. */
|
||||
if (offset == 1)
|
||||
{
|
||||
/* Include leading slash. */
|
||||
offset = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free (buf);
|
||||
buf3 = xstrdup (buf2 + offset);
|
||||
buf2[offset] = 0;
|
||||
#ifdef __linux__
|
||||
{
|
||||
char *bind;
|
||||
grub_free (grub_find_root_devices_from_mountinfo (buf2, &bind));
|
||||
if (bind && bind[0] && bind[1])
|
||||
{
|
||||
char *temp = buf3;
|
||||
buf3 = grub_xasprintf ("%s%s%s", bind, buf3[0] == '/' ?"":"/", buf3);
|
||||
grub_free (temp);
|
||||
}
|
||||
grub_free (bind);
|
||||
}
|
||||
#endif
|
||||
|
||||
free (buf2);
|
||||
|
||||
#ifdef __linux__
|
||||
parsedir:
|
||||
#endif
|
||||
/* Remove trailing slashes, return empty string if root directory. */
|
||||
len = strlen (buf3);
|
||||
while (len > 0 && buf3[len - 1] == '/')
|
||||
{
|
||||
buf3[len - 1] = '\0';
|
||||
len--;
|
||||
}
|
||||
|
||||
if (poolfs)
|
||||
{
|
||||
ret = xasprintf ("/%s/@%s", poolfs, buf3);
|
||||
free (buf3);
|
||||
}
|
||||
else
|
||||
ret = buf3;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_biosdisk_is_floppy (grub_disk_t disk)
|
||||
{
|
||||
struct stat st;
|
||||
int fd;
|
||||
const char *dname;
|
||||
|
||||
dname = grub_util_biosdisk_get_osdev (disk);
|
||||
|
||||
if (!dname)
|
||||
return 0;
|
||||
|
||||
fd = open (dname, O_RDONLY);
|
||||
/* Shouldn't happen. */
|
||||
if (fd == -1)
|
||||
return 0;
|
||||
|
||||
/* Shouldn't happen either. */
|
||||
if (fstat (fd, &st) < 0)
|
||||
{
|
||||
close (fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
close (fd);
|
||||
|
||||
#if defined(__NetBSD__)
|
||||
if (major(st.st_rdev) == RAW_FLOPPY_MAJOR)
|
||||
return 1;
|
||||
#endif
|
||||
|
||||
#if defined(FLOPPY_MAJOR)
|
||||
if (major(st.st_rdev) == FLOPPY_MAJOR)
|
||||
#else
|
||||
/* Some kernels (e.g. kFreeBSD) don't have a static major number
|
||||
for floppies, but they still use a "fd[0-9]" pathname. */
|
||||
if (dname[5] == 'f'
|
||||
&& dname[6] == 'd'
|
||||
&& dname[7] >= '0'
|
||||
&& dname[7] <= '9')
|
||||
#endif
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *
|
||||
grub_util_check_block_device (const char *blk_dev)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if (stat (blk_dev, &st) < 0)
|
||||
grub_util_error (_("cannot stat `%s': %s"), blk_dev,
|
||||
strerror (errno));
|
||||
|
||||
if (S_ISBLK (st.st_mode))
|
||||
return (blk_dev);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *
|
||||
grub_util_check_char_device (const char *blk_dev)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if (stat (blk_dev, &st) < 0)
|
||||
grub_util_error (_("cannot stat `%s': %s"), blk_dev, strerror (errno));
|
||||
|
||||
if (S_ISCHR (st.st_mode))
|
||||
return (blk_dev);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void
|
||||
grub_util_pull_lvm_by_command (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
53
grub-core/osdep/unix/random.c
Normal file
53
grub-core/osdep/unix/random.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1992-1999,2001,2003,2004,2005,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <grub/types.h>
|
||||
#include <grub/crypto.h>
|
||||
#include <grub/auth.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/i18n.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int
|
||||
grub_get_random (void *out, grub_size_t len)
|
||||
{
|
||||
#if ! defined (__linux__) && ! defined (__FreeBSD__) && ! defined (__FreeBSD_kernel__) && ! defined (__OpenBSD__) && !defined (__GNU__) && ! defined (_WIN32) && !defined(__CYGWIN__) && !defined (__NetBSD__) && !defined (__APPLE__) && !defined(__sun__)
|
||||
/* TRANSLATORS: The generator might still be secure just GRUB isn't sure about it. */
|
||||
printf ("%s", _("WARNING: your random generator isn't known to be secure\n"));
|
||||
#warning "your random generator isn't known to be secure"
|
||||
#endif
|
||||
FILE *f;
|
||||
size_t rd;
|
||||
|
||||
f = fopen ("/dev/urandom", "rb");
|
||||
if (!f)
|
||||
return 1;
|
||||
rd = fread (out, 1, len, f);
|
||||
fclose (f);
|
||||
|
||||
if (rd != len)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
346
grub-core/osdep/windows/getroot.c
Normal file
346
grub-core/osdep/windows/getroot.c
Normal file
|
@ -0,0 +1,346 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1999,2000,2001,2002,2003,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config-util.h>
|
||||
#include <config.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#ifdef HAVE_LIMITS_H
|
||||
#include <limits.h>
|
||||
#endif
|
||||
|
||||
#include <grub/types.h>
|
||||
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/util/lvm.h>
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/emu/hostdisk.h>
|
||||
#include <grub/emu/getroot.h>
|
||||
#include <grub/charset.h>
|
||||
#include <grub/util/windows.h>
|
||||
#include <windows.h>
|
||||
#include <winioctl.h>
|
||||
|
||||
char *
|
||||
grub_util_tchar_to_utf8 (LPCTSTR in)
|
||||
{
|
||||
#if SIZEOF_TCHAR == 1
|
||||
return xstrdup (path);
|
||||
#elif SIZEOF_TCHAR == 2
|
||||
size_t ssz;
|
||||
for (ssz = 0; in[ssz]; ssz++);
|
||||
|
||||
size_t tsz = GRUB_MAX_UTF8_PER_UTF16 * ssz + 1;
|
||||
grub_uint8_t *ret = xmalloc (tsz);
|
||||
*grub_utf16_to_utf8 (ret, in, ssz) = '\0';
|
||||
return (char *) ret;
|
||||
#else
|
||||
#error "Unsupported TCHAR size"
|
||||
#endif
|
||||
}
|
||||
|
||||
#if SIZEOF_TCHAR == 1
|
||||
#define tcsnicmp strncasecmp
|
||||
#define tclen strlen
|
||||
#elif SIZEOF_TCHAR == 2
|
||||
#define tcsnicmp wcsnicmp
|
||||
#define tclen wcslen
|
||||
#endif
|
||||
|
||||
char **
|
||||
grub_guess_root_devices (const char *dir)
|
||||
{
|
||||
char **os_dev = NULL;
|
||||
TCHAR *dirwindows, *mntpointwindows;
|
||||
TCHAR *ptr;
|
||||
TCHAR volumename[100];
|
||||
size_t mntpointwindows_sz;
|
||||
|
||||
dirwindows = grub_util_get_windows_path (dir);
|
||||
if (!dirwindows)
|
||||
return 0;
|
||||
|
||||
mntpointwindows_sz = strlen (dir) * 2 + 1;
|
||||
mntpointwindows = xmalloc ((mntpointwindows_sz + 1) * sizeof (mntpointwindows[0]));
|
||||
|
||||
if (!GetVolumePathName (dirwindows,
|
||||
mntpointwindows,
|
||||
mntpointwindows_sz))
|
||||
{
|
||||
free (dirwindows);
|
||||
free (mntpointwindows);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!mntpointwindows[0])
|
||||
{
|
||||
free (dirwindows);
|
||||
free (mntpointwindows);
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (ptr = mntpointwindows; *ptr; ptr++);
|
||||
if (*(ptr - 1) != '\\')
|
||||
{
|
||||
*ptr = '\\';
|
||||
*(ptr + 1) = '\0';
|
||||
}
|
||||
|
||||
if (!GetVolumeNameForVolumeMountPoint (mntpointwindows,
|
||||
volumename,
|
||||
ARRAY_SIZE (volumename)))
|
||||
{
|
||||
free (dirwindows);
|
||||
free (mntpointwindows);
|
||||
return 0;
|
||||
}
|
||||
os_dev = xmalloc (2 * sizeof (os_dev[0]));
|
||||
|
||||
for (ptr = volumename; *ptr; ptr++);
|
||||
while (ptr > volumename && *(ptr - 1) == '\\')
|
||||
*--ptr = '\0';
|
||||
|
||||
os_dev[0] = grub_util_tchar_to_utf8 (volumename);
|
||||
free (dirwindows);
|
||||
free (mntpointwindows);
|
||||
|
||||
if (!os_dev[0])
|
||||
{
|
||||
free (os_dev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
os_dev[1] = 0;
|
||||
|
||||
return os_dev;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_part_to_disk (const char *os_dev,
|
||||
struct stat *st __attribute__ ((unused)),
|
||||
int *is_part)
|
||||
{
|
||||
HANDLE hd;
|
||||
LPTSTR name = grub_util_get_windows_path (os_dev);
|
||||
VOLUME_DISK_EXTENTS exts;
|
||||
DWORD extsbytes;
|
||||
char *ret;
|
||||
|
||||
if (((name[0] == '/') || (name[0] == '\\')) &&
|
||||
((name[1] == '/') || (name[1] == '\\')) &&
|
||||
((name[2] == '.') || (name[2] == '?')) &&
|
||||
((name[3] == '/') || (name[3] == '\\'))
|
||||
&& (tcsnicmp (name + 4, TEXT("PhysicalDrive"), sizeof ("PhysicalDrive") - 1) == 0
|
||||
|| tcsnicmp (name + 4, TEXT("Harddisk"), sizeof ("Harddisk") - 1) == 0
|
||||
|| ((name[4] == 'A' || name[4] == 'a' || name[4] == 'B' || name[4] == 'b')
|
||||
&& name[5] == ':' && name[6] == '\0')))
|
||||
{
|
||||
grub_util_info ("Matches full disk pattern");
|
||||
ret = grub_util_tchar_to_utf8 (name);
|
||||
free (name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
0, OPEN_EXISTING, 0, 0);
|
||||
if (hd == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
grub_util_info ("CreateFile failed");
|
||||
ret = grub_util_tchar_to_utf8 (name);
|
||||
free (name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!DeviceIoControl(hd, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
|
||||
NULL, 0, &exts, sizeof (exts), &extsbytes, NULL))
|
||||
{
|
||||
grub_util_info ("IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS failed");
|
||||
ret = grub_util_tchar_to_utf8 (name);
|
||||
CloseHandle (hd);
|
||||
free (name);
|
||||
return ret;
|
||||
}
|
||||
|
||||
CloseHandle (hd);
|
||||
|
||||
*is_part = 1;
|
||||
free (name);
|
||||
return xasprintf ("\\\\?\\PhysicalDrive%lu", (unsigned long) exts.Extents[0].DiskNumber);
|
||||
}
|
||||
|
||||
enum grub_dev_abstraction_types
|
||||
grub_util_get_dev_abstraction_os (const char *os_dev __attribute__((unused)))
|
||||
{
|
||||
return GRUB_DEV_ABSTRACTION_NONE;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_pull_device_os (const char *os_dev __attribute__ ((unused)),
|
||||
enum grub_dev_abstraction_types ab __attribute__ ((unused)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_util_get_grub_dev_os (const char *os_dev __attribute__ ((unused)))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
grub_disk_addr_t
|
||||
grub_util_find_partition_start_os (const char *os_dev)
|
||||
{
|
||||
HANDLE hd;
|
||||
LPTSTR name = grub_util_get_windows_path (os_dev);
|
||||
VOLUME_DISK_EXTENTS exts;
|
||||
DWORD extsbytes;
|
||||
char *ret;
|
||||
|
||||
if (((name[0] == '/') || (name[0] == '\\')) &&
|
||||
((name[1] == '/') || (name[1] == '\\')) &&
|
||||
((name[2] == '.') || (name[2] == '?')) &&
|
||||
((name[3] == '/') || (name[3] == '\\'))
|
||||
&& (tcsnicmp (name + 4, TEXT("PhysicalDrive"), sizeof ("PhysicalDrive") - 1) == 0
|
||||
|| tcsnicmp (name + 4, TEXT("Harddisk"), sizeof ("Harddisk") - 1) == 0
|
||||
|| ((name[4] == 'A' || name[4] == 'a' || name[4] == 'B' || name[4] == 'b')
|
||||
&& name[5] == ':' && name[6] == '\0')))
|
||||
{
|
||||
ret = grub_util_tchar_to_utf8 (name);
|
||||
free (name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
hd = CreateFile (name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
0, OPEN_EXISTING, 0, 0);
|
||||
if (hd == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
ret = grub_util_tchar_to_utf8 (name);
|
||||
free (name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!DeviceIoControl(hd, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
|
||||
NULL, 0, &exts, sizeof (exts), &extsbytes, NULL))
|
||||
{
|
||||
ret = grub_util_tchar_to_utf8 (name);
|
||||
CloseHandle (hd);
|
||||
free (name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
CloseHandle (hd);
|
||||
free (name);
|
||||
return exts.Extents[0].StartingOffset.QuadPart / 512;
|
||||
}
|
||||
|
||||
char *
|
||||
grub_make_system_path_relative_to_its_root (const char *path)
|
||||
{
|
||||
TCHAR *dirwindows, *mntpointwindows;
|
||||
TCHAR *ptr;
|
||||
TCHAR volumename[100];
|
||||
size_t mntpointwindows_sz;
|
||||
size_t offset, flen;
|
||||
TCHAR *ret;
|
||||
char *cret;
|
||||
|
||||
dirwindows = grub_util_get_windows_path (path);
|
||||
if (!dirwindows)
|
||||
return xstrdup (path);
|
||||
|
||||
mntpointwindows_sz = strlen (path) * 2 + 1;
|
||||
mntpointwindows = xmalloc ((mntpointwindows_sz + 1) * sizeof (mntpointwindows[0]));
|
||||
|
||||
if (!GetVolumePathName (dirwindows,
|
||||
mntpointwindows,
|
||||
mntpointwindows_sz))
|
||||
{
|
||||
offset = 0;
|
||||
if (dirwindows[0] && dirwindows[1] == ':')
|
||||
offset = 2;
|
||||
}
|
||||
offset = tclen (mntpointwindows);
|
||||
free (mntpointwindows);
|
||||
flen = tclen (dirwindows);
|
||||
if (offset > flen)
|
||||
{
|
||||
offset = 0;
|
||||
if (dirwindows[0] && dirwindows[1] == ':')
|
||||
offset = 2;
|
||||
}
|
||||
ret = xmalloc (sizeof (ret[0]) * (flen - offset + 2));
|
||||
if (dirwindows[offset] != '\\'
|
||||
&& dirwindows[offset] != '/'
|
||||
&& dirwindows[offset])
|
||||
{
|
||||
ret[0] = '\\';
|
||||
memcpy (ret + 1, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0]));
|
||||
}
|
||||
else
|
||||
memcpy (ret, dirwindows + offset, (flen - offset + 1) * sizeof (ret[0]));
|
||||
|
||||
free (dirwindows);
|
||||
|
||||
for (ptr = ret; *ptr; ptr++)
|
||||
if (*ptr == '\\')
|
||||
*ptr = '/';
|
||||
|
||||
cret = grub_util_tchar_to_utf8 (ret);
|
||||
free (ret);
|
||||
|
||||
return cret;
|
||||
}
|
||||
|
||||
int
|
||||
grub_util_biosdisk_is_floppy (grub_disk_t disk)
|
||||
{
|
||||
int ret;
|
||||
const char *dname;
|
||||
LPTSTR name;
|
||||
|
||||
dname = grub_util_biosdisk_get_osdev (disk);
|
||||
|
||||
if (!dname)
|
||||
return 0;
|
||||
|
||||
name = grub_util_get_windows_path (dname);
|
||||
|
||||
ret = (((name[0] == '/') || (name[0] == '\\')) &&
|
||||
((name[1] == '/') || (name[1] == '\\')) &&
|
||||
((name[2] == '.') || (name[2] == '?')) &&
|
||||
((name[3] == '/') || (name[3] == '\\'))
|
||||
&& (name[4] == 'A' || name[4] == 'a' || name[4] == 'B' || name[4] == 'b')
|
||||
&& name[5] == ':' && name[6] == '\0');
|
||||
free (name);
|
||||
|
||||
return ret;
|
||||
}
|
55
grub-core/osdep/windows/random.c
Normal file
55
grub-core/osdep/windows/random.c
Normal file
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 1992-1999,2001,2003,2004,2005,2009,2010,2011,2012,2013 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <grub/types.h>
|
||||
#include <grub/crypto.h>
|
||||
#include <grub/auth.h>
|
||||
#include <grub/emu/misc.h>
|
||||
#include <grub/util/misc.h>
|
||||
#include <grub/i18n.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <windows.h>
|
||||
#include <wincrypt.h>
|
||||
|
||||
int
|
||||
grub_get_random (void *out, grub_size_t len)
|
||||
{
|
||||
HCRYPTPROV hCryptProv;
|
||||
if (!CryptAcquireContext (&hCryptProv,
|
||||
NULL,
|
||||
MS_DEF_PROV,
|
||||
PROV_RSA_FULL,
|
||||
CRYPT_VERIFYCONTEXT))
|
||||
return 1;
|
||||
if (!CryptGenRandom (hCryptProv, len, out))
|
||||
{
|
||||
CryptReleaseContext (hCryptProv, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
CryptReleaseContext (hCryptProv, 0);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue