diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:29:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 16:29:51 +0000 |
commit | 6e7a315eb67cb6c113cf37e1d66c4f11a51a2b3e (patch) | |
tree | 32451fa3cdd9321fb2591fada9891b2cb70a9cd1 /grub-core/osdep/linux | |
parent | Initial commit. (diff) | |
download | grub2-upstream.tar.xz grub2-upstream.zip |
Adding upstream version 2.06.upstream/2.06upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | grub-core/osdep/linux/blocklist.c | 136 | ||||
-rw-r--r-- | grub-core/osdep/linux/emunet.c | 74 | ||||
-rw-r--r-- | grub-core/osdep/linux/getroot.c | 1164 | ||||
-rw-r--r-- | grub-core/osdep/linux/hostdisk.c | 465 | ||||
-rw-r--r-- | grub-core/osdep/linux/ofpath.c | 769 | ||||
-rw-r--r-- | grub-core/osdep/linux/platform.c | 156 |
6 files changed, 2764 insertions, 0 deletions
diff --git a/grub-core/osdep/linux/blocklist.c b/grub-core/osdep/linux/blocklist.c new file mode 100644 index 0000000..c77d608 --- /dev/null +++ b/grub-core/osdep/linux/blocklist.c @@ -0,0 +1,136 @@ +/* grub-setup.c - make GRUB usable */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,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.h> + +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <dirent.h> +#include <assert.h> +#include <sys/ioctl.h> +#include <linux/types.h> +#include <linux/fs.h> +#include <linux/fiemap.h> + +#include <grub/disk.h> +#include <grub/partition.h> +#include <grub/util/misc.h> +#include <grub/util/install.h> +#include <errno.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +void +grub_install_get_blocklist (grub_device_t root_dev, + const char *core_path, + const char *core_img __attribute__ ((unused)), + size_t core_size, + void (*callback) (grub_disk_addr_t sector, + unsigned offset, + unsigned length, + void *data), + void *hook_data) +{ + grub_partition_t container = root_dev->disk->partition; + grub_uint64_t container_start = grub_partition_get_start (container); + struct fiemap fie1; + int fd; + + /* Write the first two sectors of the core image onto the disk. */ + grub_util_info ("opening the core image `%s'", core_path); + fd = open (core_path, O_RDONLY); + if (fd < 0) + grub_util_error (_("cannot open `%s': %s"), core_path, + strerror (errno)); + + grub_memset (&fie1, 0, sizeof (fie1)); + fie1.fm_length = core_size; + fie1.fm_flags = FIEMAP_FLAG_SYNC; + + if (ioctl (fd, FS_IOC_FIEMAP, &fie1) < 0) + { + int nblocks, i; + int bsize; + int mul; + + grub_util_info ("FIEMAP failed. Reverting to FIBMAP"); + + if (ioctl (fd, FIGETBSZ, &bsize) < 0) + grub_util_error (_("can't retrieve blocklists: %s"), + strerror (errno)); + if (bsize & (GRUB_DISK_SECTOR_SIZE - 1)) + grub_util_error ("%s", _("blocksize is not divisible by 512")); + if (!bsize) + grub_util_error ("%s", _("invalid zero blocksize")); + mul = bsize >> GRUB_DISK_SECTOR_BITS; + nblocks = (core_size + bsize - 1) / bsize; + if (mul == 0 || nblocks == 0) + grub_util_error ("%s", _("can't retrieve blocklists")); + for (i = 0; i < nblocks; i++) + { + unsigned blk = i; + int rest; + if (ioctl (fd, FIBMAP, &blk) < 0) + grub_util_error (_("can't retrieve blocklists: %s"), + strerror (errno)); + + rest = core_size - ((i * mul) << GRUB_DISK_SECTOR_BITS); + if (rest <= 0) + break; + if (rest > GRUB_DISK_SECTOR_SIZE * mul) + rest = GRUB_DISK_SECTOR_SIZE * mul; + callback (((grub_uint64_t) blk) * mul + + container_start, + 0, rest, hook_data); + } + } + else + { + struct fiemap *fie2; + int i; + fie2 = xmalloc (sizeof (*fie2) + + fie1.fm_mapped_extents + * sizeof (fie1.fm_extents[1])); + memset (fie2, 0, sizeof (*fie2) + + fie1.fm_mapped_extents * sizeof (fie2->fm_extents[1])); + fie2->fm_length = core_size; + fie2->fm_flags = FIEMAP_FLAG_SYNC; + fie2->fm_extent_count = fie1.fm_mapped_extents; + if (ioctl (fd, FS_IOC_FIEMAP, fie2) < 0) + grub_util_error (_("can't retrieve blocklists: %s"), + strerror (errno)); + for (i = 0; i < fie2->fm_mapped_extents; i++) + { + callback ((fie2->fm_extents[i].fe_physical + >> GRUB_DISK_SECTOR_BITS) + + container_start, + fie2->fm_extents[i].fe_physical + & (GRUB_DISK_SECTOR_SIZE - 1), + fie2->fm_extents[i].fe_length, hook_data); + } + free (fie2); + } + close (fd); +} diff --git a/grub-core/osdep/linux/emunet.c b/grub-core/osdep/linux/emunet.c new file mode 100644 index 0000000..19b188f --- /dev/null +++ b/grub-core/osdep/linux/emunet.c @@ -0,0 +1,74 @@ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 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 <config-util.h> + +#include <sys/socket.h> +#include <sys/types.h> +#include <linux/if.h> +#include <linux/if_tun.h> +#include <unistd.h> +#include <sys/ioctl.h> +#include <fcntl.h> +#include <string.h> + +#include <grub/emu/net.h> + +static int fd; + +grub_ssize_t +grub_emunet_send (const void *packet, grub_size_t sz) +{ + return write (fd, packet, sz); +} + +grub_ssize_t +grub_emunet_receive (void *packet, grub_size_t sz) +{ + return read (fd, packet, sz); +} + +int +grub_emunet_create (grub_size_t *mtu) +{ + struct ifreq ifr; + *mtu = 1500; + fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK); + if (fd < 0) + return -1; + memset (&ifr, 0, sizeof (ifr)); + ifr.ifr_flags = IFF_TAP | IFF_NO_PI; + if (ioctl (fd, TUNSETIFF, &ifr) < 0) + { + close (fd); + fd = -1; + return -1; + } + return 0; +} + +void +grub_emunet_close (void) +{ + if (fd < 0) + return; + + close (fd); + fd = -1; +} diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c new file mode 100644 index 0000000..001b818 --- /dev/null +++ b/grub-core/osdep/linux/getroot.c @@ -0,0 +1,1164 @@ +/* + * 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 + +#if defined(MAJOR_IN_MKDEV) +#include <sys/mkdev.h> +#elif defined(MAJOR_IN_SYSMACROS) +#include <sys/sysmacros.h> +#endif + +#include <grub/types.h> +#include <sys/ioctl.h> /* ioctl */ +#include <sys/mount.h> + +#include <grub/util/misc.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> +#include <grub/emu/exec.h> +#include <grub/btrfs.h> + +#define LVM_DEV_MAPPER_STRING "/dev/mapper/" + +/* 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]; +}; + +struct btrfs_ioctl_ino_lookup_args +{ + grub_uint64_t treeid; + grub_uint64_t objectid; + char name[4080]; +}; + +struct btrfs_ioctl_search_key +{ + grub_uint64_t tree_id; + grub_uint64_t min_objectid; + grub_uint64_t max_objectid; + grub_uint64_t min_offset; + grub_uint64_t max_offset; + grub_uint64_t min_transid; + grub_uint64_t max_transid; + grub_uint32_t min_type; + grub_uint32_t max_type; + grub_uint32_t nr_items; + grub_uint32_t unused[9]; +}; + +struct btrfs_ioctl_search_args { + struct btrfs_ioctl_search_key key; + grub_uint64_t buf[(4096 - sizeof(struct btrfs_ioctl_search_key)) + / sizeof (grub_uint64_t)]; +}; + +#define BTRFS_IOC_TREE_SEARCH _IOWR(0x94, 17, \ + struct btrfs_ioctl_search_args) +#define BTRFS_IOC_INO_LOOKUP _IOWR(0x94, 18, \ + struct btrfs_ioctl_ino_lookup_args) +#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 = xcalloc (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 < 0) + return NULL; + + if (ioctl (fd, BTRFS_IOC_FS_INFO, &fsi) < 0) + { + close (fd); + return NULL; + } + + ret = xcalloc (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; +} + +static char * +get_btrfs_fs_prefix (const char *mount_path) +{ + struct btrfs_ioctl_ino_lookup_args args; + struct stat st; + int fd; + grub_uint64_t tree_id, inode_id; + char *ret = NULL; + + fd = open (mount_path, O_RDONLY); + + if (fd < 0) + return NULL; + memset (&args, 0, sizeof(args)); + args.objectid = GRUB_BTRFS_TREE_ROOT_OBJECTID; + + if (ioctl (fd, BTRFS_IOC_INO_LOOKUP, &args) < 0) + goto fail; + tree_id = args.treeid; + + if (fstat (fd, &st) < 0) + goto fail; + inode_id = st.st_ino; + + while (tree_id != GRUB_BTRFS_ROOT_VOL_OBJECTID + || inode_id != GRUB_BTRFS_TREE_ROOT_OBJECTID) + { + const char *name; + size_t namelen; + struct btrfs_ioctl_search_args sargs; + char *old; + + memset (&sargs, 0, sizeof(sargs)); + + if (inode_id == GRUB_BTRFS_TREE_ROOT_OBJECTID) + { + struct grub_btrfs_root_backref *br; + + sargs.key.tree_id = 1; + sargs.key.min_objectid = tree_id; + sargs.key.max_objectid = tree_id; + + sargs.key.min_offset = 0; + sargs.key.max_offset = ~0ULL; + sargs.key.min_transid = 0; + sargs.key.max_transid = ~0ULL; + sargs.key.min_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF; + sargs.key.max_type = GRUB_BTRFS_ITEM_TYPE_ROOT_BACKREF; + + sargs.key.nr_items = 1; + + if (ioctl (fd, BTRFS_IOC_TREE_SEARCH, &sargs) < 0) + goto fail; + + if (sargs.key.nr_items == 0) + goto fail; + + tree_id = sargs.buf[2]; + br = (struct grub_btrfs_root_backref *) (sargs.buf + 4); + inode_id = grub_le_to_cpu64 (br->inode_id); + name = br->name; + namelen = grub_le_to_cpu16 (br->n); + } + else + { + struct grub_btrfs_inode_ref *ir; + + sargs.key.tree_id = tree_id; + sargs.key.min_objectid = inode_id; + sargs.key.max_objectid = inode_id; + + sargs.key.min_offset = 0; + sargs.key.max_offset = ~0ULL; + sargs.key.min_transid = 0; + sargs.key.max_transid = ~0ULL; + sargs.key.min_type = GRUB_BTRFS_ITEM_TYPE_INODE_REF; + sargs.key.max_type = GRUB_BTRFS_ITEM_TYPE_INODE_REF; + + if (ioctl (fd, BTRFS_IOC_TREE_SEARCH, &sargs) < 0) + goto fail; + + if (sargs.key.nr_items == 0) + goto fail; + + inode_id = sargs.buf[2]; + + ir = (struct grub_btrfs_inode_ref *) (sargs.buf + 4); + name = ir->name; + namelen = grub_le_to_cpu16 (ir->n); + } + old = ret; + ret = xmalloc (namelen + (old ? strlen (old) : 0) + 2); + ret[0] = '/'; + memcpy (ret + 1, name, namelen); + if (old) + { + strcpy (ret + 1 + namelen, old); + free (old); + } + else + ret[1+namelen] = '\0'; + } + if (!ret) + ret = xstrdup ("/"); + close (fd); + return ret; + + fail: + free (ret); + close (fd); + return NULL; +} + + +char ** +grub_find_root_devices_from_mountinfo (const char *dir, char **relroot) +{ + FILE *fp = NULL; + char *buf = NULL; + size_t len = 0; + grub_size_t entry_len, entry_max = 4; + struct mountinfo_entry *entries; + struct mountinfo_entry parent_entry = { 0, 0, 0, "", "", "", "" }; + int i; + int retry = 0; + int dir_fd = -1; + char **ret = NULL; + + if (! *dir) + dir = "/"; + if (relroot) + *relroot = NULL; + + entries = xcalloc (entry_max, sizeof (*entries)); + +again: + fp = grub_util_fopen ("/proc/self/mountinfo", "r"); + if (! fp) + goto out; /* fall through to other methods */ + + entry_len = 0; + + /* 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 *fs_prefix = 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) + fs_prefix = xasprintf ("/@%s", entries[i].enc_root); + else if (strchr (slash + 1, '@')) + fs_prefix = xasprintf ("/%s%s", slash + 1, entries[i].enc_root); + else + fs_prefix = 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); + fs_prefix = get_btrfs_fs_prefix (entries[i].enc_path); + } + else if (!retry && grub_strcmp (entries[i].fstype, "autofs") == 0) + { + /* If the best match is automounted, try to trigger mount. We cannot + simply return here because stat() on automounted directory does not + trigger mount and returns bogus (pseudo)device number instead. + We keep mountpoint open until end of scan to prevent timeout. */ + + int flags = O_RDONLY|O_DIRECTORY; + + fclose (fp); +#ifdef O_LARGEFILE + flags |= O_LARGEFILE; +#endif + dir_fd = open (entries[i].enc_path, flags); + retry = 1; + goto again; + } + if (!ret) + { + ret = xmalloc (2 * sizeof (ret[0])); + ret[0] = strdup (entries[i].device); + ret[1] = 0; + } + if (!fs_prefix) + fs_prefix = entries[i].enc_root; + if (relroot) + { + char *ptr; + grub_size_t enc_root_len = strlen (fs_prefix); + grub_size_t enc_path_len = strlen (entries[i].enc_path); + grub_size_t dir_strlen = strlen (dir); + *relroot = xmalloc (enc_root_len + + 2 + dir_strlen); + ptr = grub_stpcpy (*relroot, fs_prefix); + if (dir_strlen > enc_path_len) + { + while (ptr > *relroot && *(ptr - 1) == '/') + ptr--; + if (dir[enc_path_len] != '/') + *ptr++ = '/'; + ptr = grub_stpcpy (ptr, dir + enc_path_len); + } + *ptr = 0; + } + if (fs_prefix != entries[i].enc_root) + free (fs_prefix); + break; + } + +out: + free (buf); + free (entries); + if (fp) + fclose (fp); + if (dir_fd != -1) + close (dir_fd); + return ret; +} + +static char * +get_mdadm_uuid (const char *os_dev) +{ + const char *argv[5]; + int fd; + pid_t pid; + FILE *mdadm; + char *buf = NULL; + size_t len = 0; + char *name = NULL; + + argv[0] = "mdadm"; + argv[1] = "--detail"; + argv[2] = "--export"; + argv[3] = 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 + { + const 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 */ + + argv[0] = "mdadm"; + argv[1] = "--detail"; + argv[2] = "--export"; + argv[3] = 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; + + if (! S_ISBLK (st->st_mode)) + { + *is_part = 0; + return xstrdup (os_dev); + } + + 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; + } + + /* If this is an rssd device. */ + if ((strncmp ("rssd", p, 4) == 0) && p[4] >= 'a' && p[4] <= 'z') + { + char *pp = p + 4; + while (*pp >= 'a' && *pp <= 'z') + pp++; + if (*pp) + *is_part = 1; + /* /dev/rssd[a-z]+[0-9]* */ + *pp = '\0'; + return path; + } + + /* If this is a loop device */ + if ((strncmp ("loop", p, 4) == 0) && p[4] >= '0' && p[4] <= '9') + { + char *pp = p + 4; + while (*pp >= '0' && *pp <= '9') + pp++; + if (*pp == 'p') + *is_part = 1; + /* /dev/loop[0-9]+p[0-9]* */ + *pp = '\0'; + return path; + } + + /* If this is a NVMe device */ + if ((strncmp ("nvme", p, 4) == 0) && p[4] >= '0' && p[4] <= '9') + { + char *pp = p + 4; + while (*pp >= '0' && *pp <= '9') + pp++; + if (*pp == 'n') + pp++; + while (*pp >= '0' && *pp <= '9') + pp++; + if (*pp == 'p') + *is_part = 1; + /* /dev/nvme[0-9]+n[0-9]+p[0-9]* */ + *pp = '\0'; + return path; + } + } + + return path; +} + +static 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 grub_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; +} + +char * +grub_make_system_path_relative_to_its_root_os (const char *path) +{ + char *bind = NULL; + grub_size_t len; + grub_free (grub_find_root_devices_from_mountinfo (path, &bind)); + if (bind && bind[0]) + { + len = strlen (bind); + while (len > 0 && bind[len - 1] == '/') + { + bind[len - 1] = '\0'; + len--; + } + return bind; + } + grub_free (bind); + return NULL; +} diff --git a/grub-core/osdep/linux/hostdisk.c b/grub-core/osdep/linux/hostdisk.c new file mode 100644 index 0000000..da62f92 --- /dev/null +++ b/grub-core/osdep/linux/hostdisk.c @@ -0,0 +1,465 @@ +/* hostdisk.c - emulate biosdisk */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 1999,2000,2001,2002,2003,2004,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 <grub/disk.h> +#include <grub/partition.h> +#include <grub/msdos_partition.h> +#include <grub/types.h> +#include <grub/err.h> +#include <grub/emu/misc.h> +#include <grub/emu/hostdisk.h> +#include <grub/emu/getroot.h> +#include <grub/emu/exec.h> +#include <grub/misc.h> +#include <grub/i18n.h> +#include <grub/list.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> +#include <assert.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <errno.h> +#include <limits.h> + +# include <sys/ioctl.h> /* ioctl */ +# include <sys/mount.h> +# ifndef BLKFLSBUF +# define BLKFLSBUF _IO (0x12,97) /* flush buffer cache */ +# endif /* ! BLKFLSBUF */ +# include <sys/ioctl.h> /* ioctl */ +# ifndef HDIO_GETGEO +# define HDIO_GETGEO 0x0301 /* get device geometry */ +/* If HDIO_GETGEO is not defined, it is unlikely that hd_geometry is + defined. */ +struct hd_geometry +{ + unsigned char heads; + unsigned char sectors; + unsigned short cylinders; + unsigned long start; +}; +# endif /* ! HDIO_GETGEO */ +# ifndef BLKGETSIZE64 +# define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size */ +# endif /* ! BLKGETSIZE64 */ + + +grub_int64_t +grub_util_get_fd_size_os (grub_util_fd_t fd, const char *name, unsigned *log_secsize) +{ + unsigned long long nr; + unsigned sector_size, log_sector_size; + + if (ioctl (fd, BLKGETSIZE64, &nr)) + return -1; + + if (ioctl (fd, BLKSSZGET, §or_size)) + return -1; + + if (sector_size & (sector_size - 1) || !sector_size) + return -1; + for (log_sector_size = 0; + (1 << log_sector_size) < sector_size; + log_sector_size++); + + if (log_secsize) + *log_secsize = log_sector_size; + + if (nr & ((1 << log_sector_size) - 1)) + grub_util_error ("%s", _("unaligned device size")); + + return nr; +} + +static char * +sysfs_partition_path (const char *dev, const char *entry) +{ + const char *argv[7]; + int fd; + pid_t pid; + FILE *udevadm; + char *buf = NULL; + size_t len = 0; + char *path = NULL; + + argv[0] = "udevadm"; + argv[1] = "info"; + argv[2] = "--query"; + argv[3] = "path"; + argv[4] = "--name"; + argv[5] = dev; + argv[6] = NULL; + + pid = grub_util_exec_pipe (argv, &fd); + + if (!pid) + return NULL; + + /* Parent. Read udevadm's output. */ + udevadm = fdopen (fd, "r"); + if (!udevadm) + { + grub_util_warn (_("Unable to open stream from %s: %s"), + "udevadm", strerror (errno)); + close (fd); + goto out; + } + + if (getline (&buf, &len, udevadm) > 0) + { + char *newline; + + newline = strchr (buf, '\n'); + if (newline) + *newline = '\0'; + path = xasprintf ("/sys%s/%s", buf, entry); + } + +out: + if (udevadm) + fclose (udevadm); + waitpid (pid, NULL, 0); + free (buf); + + return path; +} + +static int +sysfs_partition_start (const char *dev, grub_disk_addr_t *start) +{ + char *path; + FILE *fp; + unsigned long long val; + int ret = 0; + + path = sysfs_partition_path (dev, "start"); + if (!path) + return 0; + + fp = grub_util_fopen (path, "r"); + if (!fp) + goto out; + + if (fscanf (fp, "%llu", &val) == 1) + { + *start = (grub_disk_addr_t) val; + ret = 1; + } + +out: + free (path); + if (fp) + fclose (fp); + + return ret; +} + +grub_disk_addr_t +grub_util_find_partition_start_os (const char *dev) +{ + grub_disk_addr_t start = 0; + grub_util_fd_t fd; + struct hd_geometry hdg; + + if (sysfs_partition_start (dev, &start)) + return start; + + 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, HDIO_GETGEO, &hdg)) + { + grub_error (GRUB_ERR_BAD_DEVICE, + "cannot get disk geometry of `%s'", dev); + close (fd); + return 0; + } + + close (fd); + + return hdg.start; +} + +/* Cache of partition start sectors for each disk. */ +struct linux_partition_cache +{ + struct linux_partition_cache *next; + struct linux_partition_cache **prev; + char *dev; + unsigned long start; + int partno; +}; + +struct linux_partition_cache *linux_partition_cache_list; + +/* Check if we have devfs support. */ +static int +have_devfs (void) +{ + static int dev_devfsd_exists = -1; + + if (dev_devfsd_exists < 0) + { + struct stat st; + + dev_devfsd_exists = stat ("/dev/.devfsd", &st) == 0; + } + + return dev_devfsd_exists; +} + +#pragma GCC diagnostic ignored "-Wformat-nonliteral" + +static int +grub_hostdisk_linux_find_partition (char *dev, grub_disk_addr_t sector) +{ + size_t len = strlen (dev); + const char *format; + char *p; + int i; + char real_dev[PATH_MAX]; + struct linux_partition_cache *cache; + int missing = 0; + + strcpy(real_dev, dev); + + if (have_devfs () && strcmp (real_dev + len - 5, "/disc") == 0) + { + p = real_dev + len - 4; + format = "part%d"; + } + else if (strncmp (real_dev, "/dev/disk/by-id/", + sizeof ("/dev/disk/by-id/") - 1) == 0) + { + p = real_dev + len; + format = "-part%d"; + } + else if (real_dev[len - 1] >= '0' && real_dev[len - 1] <= '9') + { + p = real_dev + len; + format = "p%d"; + } + else + { + p = real_dev + len; + format = "%d"; + } + + for (cache = linux_partition_cache_list; cache; cache = cache->next) + { + if (strcmp (cache->dev, dev) == 0 && cache->start == sector) + { + sprintf (p, format, cache->partno); + strcpy (dev, real_dev); + return 1; + } + } + + for (i = 1; i < 10000; i++) + { + grub_util_fd_t fd; + grub_disk_addr_t start; + struct stat st; + + sprintf (p, format, i); + + fd = open (real_dev, O_RDONLY); + if (fd == -1) + { + if (missing++ < 10) + continue; + else + return 0; + } + missing = 0; + + if (fstat (fd, &st) < 0 + || !grub_util_device_is_mapped_stat (&st) + || !grub_util_get_dm_node_linear_info (st.st_rdev, 0, 0, &start)) + start = grub_util_find_partition_start_os (real_dev); + /* We don't care about errors here. */ + grub_errno = GRUB_ERR_NONE; + + close (fd); + + if (start == sector) + { + struct linux_partition_cache *new_cache_item; + + new_cache_item = xmalloc (sizeof *new_cache_item); + new_cache_item->dev = xstrdup (dev); + new_cache_item->start = start; + new_cache_item->partno = i; + grub_list_push (GRUB_AS_LIST_P (&linux_partition_cache_list), + GRUB_AS_LIST (new_cache_item)); + + strcpy (dev, real_dev); + return 1; + } + } + + return 0; +} + +#pragma GCC diagnostic error "-Wformat-nonliteral" + +void +grub_hostdisk_flush_initial_buffer (const char *os_dev) +{ + grub_util_fd_t fd; + struct stat st; + + fd = open (os_dev, O_RDONLY); + if (fd >= 0 && fstat (fd, &st) >= 0 && S_ISBLK (st.st_mode)) + ioctl (fd, BLKFLSBUF, 0); + if (fd >= 0) + close (fd); +} + +int +grub_util_fd_open_device (const grub_disk_t disk, grub_disk_addr_t sector, int flags, + grub_disk_addr_t *max) +{ + grub_util_fd_t fd; + struct grub_util_hostdisk_data *data = disk->data; + + *max = ~0ULL; + +#ifdef O_LARGEFILE + flags |= O_LARGEFILE; +#endif +#ifdef O_SYNC + flags |= O_SYNC; +#endif +#ifdef O_FSYNC + flags |= O_FSYNC; +#endif +#ifdef O_BINARY + flags |= O_BINARY; +#endif +#ifdef O_CLOEXEC + flags |= O_CLOEXEC; +#endif + + /* Linux has a bug that the disk cache for a whole disk is not consistent + with the one for a partition of the disk. */ + { + int is_partition = 0; + char dev[PATH_MAX]; + grub_disk_addr_t part_start = 0; + + part_start = grub_partition_get_start (disk->partition) + >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS); + + strncpy (dev, grub_util_biosdisk_get_osdev (disk), sizeof (dev) - 1); + dev[sizeof(dev) - 1] = '\0'; + if (disk->partition + && strncmp (dev, "/dev/", 5) == 0) + { + if (sector >= part_start) + is_partition = grub_hostdisk_linux_find_partition (dev, part_start); + else + *max = part_start - sector; + } + + reopen: + + if (data->dev && strcmp (data->dev, dev) == 0 && + data->access_mode == (flags & O_ACCMODE)) + { + grub_dprintf ("hostdisk", "reusing open device `%s'\n", dev); + fd = data->fd; + } + else + { + free (data->dev); + data->dev = 0; + if (data->fd != -1) + { + if (data->access_mode == O_RDWR || data->access_mode == O_WRONLY) + { + fsync (data->fd); + if (data->is_disk) + ioctl (data->fd, BLKFLSBUF, 0); + } + + close (data->fd); + data->fd = -1; + } + + /* Open the partition. */ + grub_dprintf ("hostdisk", "opening the device `%s' in open_device()\n", dev); + fd = open (dev, flags); + if (fd < 0) + { + grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot open `%s': %s"), + dev, strerror (errno)); + return -1; + } + + data->dev = xstrdup (dev); + data->access_mode = (flags & O_ACCMODE); + data->fd = fd; + + if (data->is_disk) + ioctl (data->fd, BLKFLSBUF, 0); + } + + if (is_partition) + { + *max = grub_util_get_fd_size (fd, dev, 0); + *max >>= disk->log_sector_size; + if (sector - part_start >= *max) + { + *max = disk->partition->len - (sector - part_start); + if (*max == 0) + *max = ~0ULL; + is_partition = 0; + strncpy (dev, grub_util_biosdisk_get_osdev (disk), sizeof (dev) - 1); + dev[sizeof(dev) - 1] = '\0'; + goto reopen; + } + sector -= part_start; + *max -= sector; + } + } + + if (grub_util_fd_seek (fd, sector << disk->log_sector_size)) + { + close (fd); + grub_error (GRUB_ERR_BAD_DEVICE, N_("cannot seek `%s': %s"), + grub_util_biosdisk_get_osdev (disk), strerror (errno)); + return -1; + } + + return fd; +} diff --git a/grub-core/osdep/linux/ofpath.c b/grub-core/osdep/linux/ofpath.c new file mode 100644 index 0000000..a6153d3 --- /dev/null +++ b/grub-core/osdep/linux/ofpath.c @@ -0,0 +1,769 @@ +/* ofpath.c - calculate OpenFirmware path names given an OS device */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2009, 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/>. + */ + +#undef OFPATH_STANDALONE + +#ifndef OFPATH_STANDALONE +#include <grub/types.h> +#include <grub/util/misc.h> +#include <grub/util/ofpath.h> +#include <grub/i18n.h> +#endif + +#include <limits.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <ctype.h> + +#ifdef __sparc__ +typedef enum + { + GRUB_OFPATH_SPARC_WWN_ADDR = 1, + GRUB_OFPATH_SPARC_TGT_LUN, + } ofpath_sparc_addressing; + +struct ofpath_sparc_hba +{ + grub_uint32_t device_id; + ofpath_sparc_addressing addressing; +}; + +static struct ofpath_sparc_hba sparc_lsi_hba[] = { + /* Rhea, Jasper 320, LSI53C1020/1030. */ + {0x30, GRUB_OFPATH_SPARC_TGT_LUN}, + /* SAS-1068E. */ + {0x50, GRUB_OFPATH_SPARC_TGT_LUN}, + /* SAS-1064E. */ + {0x56, GRUB_OFPATH_SPARC_TGT_LUN}, + /* Pandora SAS-1068E. */ + {0x58, GRUB_OFPATH_SPARC_TGT_LUN}, + /* Aspen, Invader, LSI SAS-3108. */ + {0x5d, GRUB_OFPATH_SPARC_TGT_LUN}, + /* Niwot, SAS 2108. */ + {0x79, GRUB_OFPATH_SPARC_TGT_LUN}, + /* Erie, Falcon, LSI SAS 2008. */ + {0x72, GRUB_OFPATH_SPARC_WWN_ADDR}, + /* LSI WarpDrive 6203. */ + {0x7e, GRUB_OFPATH_SPARC_WWN_ADDR}, + /* LSI SAS 2308. */ + {0x87, GRUB_OFPATH_SPARC_WWN_ADDR}, + /* LSI SAS 3008. */ + {0x97, GRUB_OFPATH_SPARC_WWN_ADDR}, + {0, 0} +}; + +static const int LSI_VENDOR_ID = 0x1000; +#endif + +#ifdef OFPATH_STANDALONE +#define xmalloc malloc +void +grub_util_error (const char *fmt, ...) +{ + va_list ap; + + fprintf (stderr, "ofpath: error: "); + va_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); + fputc ('\n', stderr); + exit (1); +} + +void +grub_util_info (const char *fmt, ...) +{ + va_list ap; + + fprintf (stderr, "ofpath: info: "); + va_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); + fputc ('\n', stderr); +} + +#define grub_util_warn grub_util_info +#define _(x) x +#define xstrdup strdup +#endif + +static void +kill_trailing_dir(char *path) +{ + char *end = path + strlen(path) - 1; + + while (end >= path) + { + if (*end != '/') + { + end--; + continue; + } + *end = '\0'; + break; + } +} + +static void +trim_newline (char *path) +{ + char *end = path + strlen(path) - 1; + + while (*end == '\n') + *end-- = '\0'; +} + +#define MAX_DISK_CAT 64 + +static char * +find_obppath (const char *sysfs_path_orig) +{ + char *sysfs_path, *path; + size_t path_size = strlen (sysfs_path_orig) + sizeof ("/obppath"); + + sysfs_path = xstrdup (sysfs_path_orig); + path = xmalloc (path_size); + + while (1) + { + int fd; + char *of_path; + struct stat st; + size_t size; + + snprintf(path, path_size, "%s/obppath", sysfs_path); +#if 0 + printf("Trying %s\n", path); +#endif + + fd = open(path, O_RDONLY); + +#ifndef __sparc__ + if (fd < 0 || fstat (fd, &st) < 0) + { + if (fd >= 0) + close (fd); + snprintf(path, path_size, "%s/devspec", sysfs_path); + fd = open(path, O_RDONLY); + } +#endif + + if (fd < 0 || fstat (fd, &st) < 0) + { + if (fd >= 0) + close (fd); + kill_trailing_dir(sysfs_path); + if (!strcmp(sysfs_path, "/sys")) + { + grub_util_info (_("`obppath' not found in parent dirs of `%s'," + " no IEEE1275 name discovery"), + sysfs_path_orig); + free (path); + free (sysfs_path); + return NULL; + } + continue; + } + size = st.st_size; + of_path = xmalloc (size + MAX_DISK_CAT + 1); + memset(of_path, 0, size + MAX_DISK_CAT + 1); + if (read(fd, of_path, size) < 0) + { + grub_util_info (_("cannot read `%s': %s"), path, strerror (errno)); + close(fd); + free (path); + free (of_path); + free (sysfs_path); + return NULL; + } + close(fd); + + trim_newline(of_path); + free (path); + free (sysfs_path); + return of_path; + } +} + +static char * +xrealpath (const char *in) +{ + char *out; +#ifdef PATH_MAX + out = xmalloc (PATH_MAX); + out = realpath (in, out); +#else + out = realpath (in, NULL); +#endif + if (!out) + grub_util_error (_("failed to get canonical path of `%s'"), in); + return out; +} + +static char * +block_device_get_sysfs_path_and_link(const char *devicenode) +{ + char *rpath; + char *rpath2; + char *ret; + size_t tmp_size = strlen (devicenode) + sizeof ("/sys/block/"); + char *tmp = xmalloc (tmp_size); + + memcpy (tmp, "/sys/block/", sizeof ("/sys/block/")); + strcat (tmp, devicenode); + + rpath = xrealpath (tmp); + rpath2 = xmalloc (strlen (rpath) + sizeof ("/device")); + strcpy (rpath2, rpath); + strcat (rpath2, "/device"); + + ret = xrealpath (rpath2); + + free (tmp); + free (rpath); + free (rpath2); + return ret; +} + +static inline int +my_isdigit (int c) +{ + return (c >= '0' && c <= '9'); +} + +static const char * +trailing_digits (const char *p) +{ + const char *end; + + end = p + strlen(p) - 1; + while (end >= p) + { + if (! my_isdigit(*end)) + break; + end--; + } + + return end + 1; +} + +static char * +__of_path_common(char *sysfs_path, + const char *device, int devno) +{ + const char *digit_string; + char disk[MAX_DISK_CAT]; + char *of_path = find_obppath(sysfs_path); + + if (!of_path) + return NULL; + + digit_string = trailing_digits (device); + if (*digit_string == '\0') + { + snprintf(disk, sizeof (disk), "/disk@%d", devno); + } + else + { + int part; + + sscanf(digit_string, "%d", &part); + snprintf(disk, sizeof (disk), "/disk@%d:%c", devno, 'a' + (part - 1)); + } + strcat(of_path, disk); + return of_path; +} + +static char * +get_basename(char *p) +{ + char *ret = p; + + while (*p) + { + if (*p == '/') + ret = p + 1; + p++; + } + + return ret; +} + +static char * +of_path_of_vdisk(const char *sys_devname __attribute__((unused)), + const char *device, + const char *devnode __attribute__((unused)), + const char *devicenode) +{ + char *sysfs_path, *p; + int devno, junk; + char *ret; + + sysfs_path = block_device_get_sysfs_path_and_link(devicenode); + p = get_basename (sysfs_path); + sscanf(p, "vdc-port-%d-%d", &devno, &junk); + ret = __of_path_common (sysfs_path, device, devno); + + free (sysfs_path); + return ret; +} + +static char * +of_path_of_ide(const char *sys_devname __attribute__((unused)), const char *device, + const char *devnode __attribute__((unused)), + const char *devicenode) +{ + char *sysfs_path, *p; + int chan, devno; + char *ret; + + sysfs_path = block_device_get_sysfs_path_and_link(devicenode); + p = get_basename (sysfs_path); + sscanf(p, "%d.%d", &chan, &devno); + + ret = __of_path_common(sysfs_path, device, 2 * chan + devno); + + free (sysfs_path); + return ret; +} + +#ifdef __sparc__ +static char * +of_path_of_nvme(const char *sys_devname __attribute__((unused)), + const char *device, + const char *devnode __attribute__((unused)), + const char *devicenode) +{ + char *sysfs_path, *of_path, disk[MAX_DISK_CAT]; + const char *digit_string, *part_end; + + digit_string = trailing_digits (device); + part_end = devicenode + strlen (devicenode) - 1; + + if ((*digit_string != '\0') && (*part_end == 'p')) + { + /* We have a partition number, strip it off. */ + int part; + char *nvmedev, *end; + + nvmedev = strdup (devicenode); + + if (!nvmedev) + return NULL; + + end = nvmedev + strlen (nvmedev) - 1; + /* Remove the p. */ + *end = '\0'; + sscanf (digit_string, "%d", &part); + snprintf (disk, sizeof (disk), "/disk@1:%c", 'a' + (part - 1)); + sysfs_path = block_device_get_sysfs_path_and_link (nvmedev); + free (nvmedev); + } + else + { + /* We do not have the parition. */ + snprintf (disk, sizeof (disk), "/disk@1"); + sysfs_path = block_device_get_sysfs_path_and_link (device); + } + + of_path = find_obppath (sysfs_path); + + if (of_path) + strcat (of_path, disk); + + free (sysfs_path); + return of_path; +} +#endif + +static int +vendor_is_ATA(const char *path) +{ + int fd, err; + char *bufname; + char bufcont[3]; + size_t path_size; + + path_size = strlen (path) + sizeof ("/vendor"); + + bufname = xmalloc (path_size); + + snprintf (bufname, path_size, "%s/vendor", path); + fd = open (bufname, O_RDONLY); + if (fd < 0) + grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno)); + + memset(bufcont, 0, sizeof (bufcont)); + err = read(fd, bufcont, sizeof (bufcont)); + if (err < 0) + grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno)); + + close(fd); + free (bufname); + + return (memcmp(bufcont, "ATA", 3) == 0); +} + +#ifdef __sparc__ +static void +check_hba_identifiers (const char *sysfs_path, int *vendor, int *device_id) +{ + char *ed = strstr (sysfs_path, "host"); + size_t path_size; + char *p, *path; + char buf[8]; + int fd; + + if (!ed) + return; + + p = xstrdup (sysfs_path); + ed = strstr (p, "host"); + + *ed = '\0'; + + path_size = (strlen (p) + sizeof ("vendor")); + path = xmalloc (path_size); + + if (!path) + goto out; + + snprintf (path, path_size, "%svendor", p); + fd = open (path, O_RDONLY); + + if (fd < 0) + goto out; + + memset (buf, 0, sizeof (buf)); + + if (read (fd, buf, sizeof (buf) - 1) < 0) + goto out; + + close (fd); + sscanf (buf, "%x", vendor); + + snprintf (path, path_size, "%sdevice", p); + fd = open (path, O_RDONLY); + + if (fd < 0) + goto out; + + memset (buf, 0, sizeof (buf)); + + if (read (fd, buf, sizeof (buf) - 1) < 0) + goto out; + + close (fd); + sscanf (buf, "%x", device_id); + + out: + free (path); + free (p); +} +#endif + +static void +check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address) +{ + char *ed = strstr (sysfs_path, "end_device"); + char *p, *q, *path; + char phy[21]; + int fd; + size_t path_size; + + if (!ed) + return; + + /* SAS devices are identified using disk@$PHY_ID */ + p = xstrdup (sysfs_path); + ed = strstr(p, "end_device"); + if (!ed) + return; + + q = ed; + while (*q && *q != '/') + q++; + *q = '\0'; + + path_size = (strlen (p) + strlen (ed) + + sizeof ("%s/sas_device/%s/phy_identifier")); + path = xmalloc (path_size); + snprintf (path, path_size, "%s/sas_device/%s/phy_identifier", p, ed); + fd = open (path, O_RDONLY); + if (fd < 0) + grub_util_error (_("cannot open `%s': %s"), path, strerror (errno)); + + memset (phy, 0, sizeof (phy)); + if (read (fd, phy, sizeof (phy) - 1) < 0) + grub_util_error (_("cannot read `%s': %s"), path, strerror (errno)); + + close (fd); + + sscanf (phy, "%d", tgt); + + snprintf (path, path_size, "%s/sas_device/%s/sas_address", p, ed); + fd = open (path, O_RDONLY); + if (fd < 0) + grub_util_error (_("cannot open `%s': %s"), path, strerror (errno)); + + memset (phy, 0, sizeof (phy)); + if (read (fd, phy, sizeof (phy) - 1) < 0) + grub_util_error (_("cannot read `%s': %s"), path, strerror (errno)); + sscanf (phy, "%lx", sas_address); + + free (path); + free (p); + close (fd); +} + +static char * +of_path_of_scsi(const char *sys_devname __attribute__((unused)), const char *device, + const char *devnode __attribute__((unused)), + const char *devicenode) +{ + const char *p, *digit_string, *disk_name; + int host, bus, tgt, lun; + unsigned long int sas_address = 0; + char *sysfs_path, disk[MAX_DISK_CAT - sizeof ("/fp@0,0")]; + char *of_path; + + sysfs_path = block_device_get_sysfs_path_and_link(devicenode); + p = get_basename (sysfs_path); + sscanf(p, "%d:%d:%d:%d", &host, &bus, &tgt, &lun); + check_sas (sysfs_path, &tgt, &sas_address); + + if (vendor_is_ATA(sysfs_path)) + { + of_path = __of_path_common(sysfs_path, device, tgt); + free (sysfs_path); + return of_path; + } + + of_path = find_obppath(sysfs_path); + if (!of_path) + goto out; + + if (strstr (of_path, "qlc")) + strcat (of_path, "/fp@0,0"); + + if (strstr (of_path, "sbus")) + disk_name = "sd"; + else + disk_name = "disk"; + + digit_string = trailing_digits (device); + if (strncmp (of_path, "/vdevice/", sizeof ("/vdevice/") - 1) == 0) + { + unsigned long id = 0x8000 | (tgt << 8) | (bus << 5) | lun; + if (*digit_string == '\0') + { + snprintf(disk, sizeof (disk), "/%s@%04lx000000000000", disk_name, id); + } + else + { + int part; + + sscanf(digit_string, "%d", &part); + snprintf(disk, sizeof (disk), + "/%s@%04lx000000000000:%c", disk_name, id, 'a' + (part - 1)); + } + } + else + { +#ifdef __sparc__ + ofpath_sparc_addressing addressing = GRUB_OFPATH_SPARC_TGT_LUN; + int vendor = 0, device_id = 0; + char *optr = disk; + + check_hba_identifiers (sysfs_path, &vendor, &device_id); + + if (vendor == LSI_VENDOR_ID) + { + struct ofpath_sparc_hba *lsi_hba; + + /* + * Over time different OF addressing schemes have been supported. + * There is no generic addressing scheme that works across + * every HBA. + */ + for (lsi_hba = sparc_lsi_hba; lsi_hba->device_id; lsi_hba++) + if (lsi_hba->device_id == device_id) + { + addressing = lsi_hba->addressing; + break; + } + } + + if (addressing == GRUB_OFPATH_SPARC_WWN_ADDR) + optr += snprintf (disk, sizeof (disk), "/%s@w%lx,%x", disk_name, + sas_address, lun); + else + optr += snprintf (disk, sizeof (disk), "/%s@%x,%x", disk_name, tgt, + lun); + + if (*digit_string != '\0') + { + int part; + + sscanf (digit_string, "%d", &part); + snprintf (optr, sizeof (disk) - (optr - disk - 1), ":%c", 'a' + + (part - 1)); + } +#else + if (lun == 0) + { + int sas_id = 0; + sas_id = bus << 16 | tgt << 8 | lun; + + if (*digit_string == '\0') + { + snprintf(disk, sizeof (disk), "/sas/%s@%x", disk_name, sas_id); + } + else + { + int part; + + sscanf(digit_string, "%d", &part); + snprintf(disk, sizeof (disk), + "/sas/%s@%x:%c", disk_name, sas_id, 'a' + (part - 1)); + } + } + else + { + char *lunstr; + int lunpart[4]; + + lunstr = xmalloc (20); + + lunpart[0] = (lun >> 8) & 0xff; + lunpart[1] = lun & 0xff; + lunpart[2] = (lun >> 24) & 0xff; + lunpart[3] = (lun >> 16) & 0xff; + + sprintf(lunstr, "%02x%02x%02x%02x00000000", lunpart[0], lunpart[1], lunpart[2], lunpart[3]); + long int longlun = atol(lunstr); + + if (*digit_string == '\0') + { + snprintf(disk, sizeof (disk), "/sas/%s@%lx,%lu", disk_name, sas_address, longlun); + } + else + { + int part; + + sscanf(digit_string, "%d", &part); + snprintf(disk, sizeof (disk), + "/sas/%s@%lx,%lu:%c", disk_name, sas_address, longlun, 'a' + (part - 1)); + } + free (lunstr); + } +#endif + } + strcat(of_path, disk); + + out: + free (sysfs_path); + return of_path; +} + +static char * +strip_trailing_digits (const char *p) +{ + char *new, *end; + + new = strdup (p); + end = new + strlen(new) - 1; + while (end >= new) + { + if (! my_isdigit(*end)) + break; + *end-- = '\0'; + } + + return new; +} + +char * +grub_util_devname_to_ofpath (const char *sys_devname) +{ + char *name_buf, *device, *devnode, *devicenode, *ofpath; + + name_buf = xrealpath (sys_devname); + + device = get_basename (name_buf); + devnode = strip_trailing_digits (name_buf); + devicenode = strip_trailing_digits (device); + + if (device[0] == 'h' && device[1] == 'd') + ofpath = of_path_of_ide(name_buf, device, devnode, devicenode); + else if (device[0] == 's' + && (device[1] == 'd' || device[1] == 'r')) + ofpath = of_path_of_scsi(name_buf, device, devnode, devicenode); + else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i' + && device[3] == 's' && device[4] == 'k') + ofpath = of_path_of_vdisk(name_buf, device, devnode, devicenode); + else if (device[0] == 'f' && device[1] == 'd' + && device[2] == '0' && device[3] == '\0') + /* All the models I've seen have a devalias "floppy". + New models have no floppy at all. */ + ofpath = xstrdup ("floppy"); +#ifdef __sparc__ + else if (device[0] == 'n' && device[1] == 'v' && device[2] == 'm' + && device[3] == 'e') + ofpath = of_path_of_nvme (name_buf, device, devnode, devicenode); +#endif + else + { + grub_util_warn (_("unknown device type %s"), device); + ofpath = NULL; + } + + free (devnode); + free (devicenode); + free (name_buf); + + return ofpath; +} + +#ifdef OFPATH_STANDALONE +int main(int argc, char **argv) +{ + char *of_path; + + if (argc != 2) + { + printf(_("Usage: %s DEVICE\n"), argv[0]); + return 1; + } + + of_path = grub_util_devname_to_ofpath (argv[1]); + if (of_path) + printf("%s\n", of_path); + free (of_path); + + return 0; +} +#endif diff --git a/grub-core/osdep/linux/platform.c b/grub-core/osdep/linux/platform.c new file mode 100644 index 0000000..e28a79d --- /dev/null +++ b/grub-core/osdep/linux/platform.c @@ -0,0 +1,156 @@ +/* + * 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.h> + +#include <grub/util/install.h> +#include <grub/emu/exec.h> +#include <grub/emu/misc.h> +#include <sys/types.h> +#include <dirent.h> +#include <string.h> + +#include <sys/utsname.h> + +static int +is_not_empty_directory (const char *dir) +{ + DIR *d; + struct dirent *de; + + d = opendir (dir); + if (!d) + return 0; + while ((de = readdir (d))) + { + if (strcmp (de->d_name, ".") == 0 + || strcmp (de->d_name, "..") == 0) + continue; + closedir (d); + return 1; + } + + closedir (d); + return 0; +} + +static int +is_64_kernel (void) +{ + struct utsname un; + + if (uname (&un) < 0) + return 0; + + return strcmp (un.machine, "x86_64") == 0; +} + +static int +read_platform_size (void) +{ + FILE *fp; + char *buf = NULL; + size_t len = 0; + int ret = 0; + + /* Newer kernels can tell us directly about the size of the + * underlying firmware - let's see if that interface is there. */ + fp = grub_util_fopen ("/sys/firmware/efi/fw_platform_size", "r"); + if (fp != NULL) + { + if (getline (&buf, &len, fp) >= 3) /* 2 digits plus newline */ + { + if (strncmp (buf, "32", 2) == 0) + ret = 32; + else if (strncmp (buf, "64", 2) == 0) + ret = 64; + } + free (buf); + fclose (fp); + } + + if (ret == 0) + { + /* Unrecognised - fall back to matching the kernel size + * instead */ + if (is_64_kernel ()) + ret = 64; + else + ret = 32; + } + + return ret; +} + +/* Are we running on an EFI-based system? */ +static int +is_efi_system (void) +{ + /* + * Linux uses efivarfs (mounted on /sys/firmware/efi/efivars) to access the + * EFI variable store. Some legacy systems may still use the deprecated + * efivars interface (accessed through /sys/firmware/efi/vars). Where both + * are present, libefivar will use the former in preference, so attempting + * to load efivars will not interfere with later operations. + */ + grub_util_exec_redirect_all ((const char * []){ "modprobe", "efivars", NULL }, + NULL, NULL, "/dev/null"); + + grub_util_info ("Looking for /sys/firmware/efi .."); + if (is_not_empty_directory ("/sys/firmware/efi")) + { + grub_util_info ("...found"); + return 1; + } + else + { + grub_util_info ("... not found"); + return 0; + } +} + +const char * +grub_install_get_default_arm_platform (void) +{ + if (is_efi_system()) + return "arm-efi"; + else + return "arm-uboot"; +} + +const char * +grub_install_get_default_x86_platform (void) +{ + if (is_efi_system()) + { + if (read_platform_size() == 64) + return "x86_64-efi"; + else + return "i386-efi"; + } + + grub_util_info ("Looking for /proc/device-tree .."); + if (is_not_empty_directory ("/proc/device-tree")) + { + grub_util_info ("...found"); + return "i386-ieee1275"; + } + + grub_util_info ("... not found"); + return "i386-pc"; +} |