2 * libefiboot - library for the manipulation of EFI boot variables
3 * Copyright 2012-2015 Red Hat, Inc.
4 * Copyright (C) 2001 Dell Computer Corporation <Matt_Domsch@dell.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see
18 * <http://www.gnu.org/licenses/>.
22 #include "fix_coverity.h"
29 #include <linux/ethtool.h>
30 #include <linux/version.h>
31 #include <linux/sockios.h>
33 #include <scsi/scsi.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/sysmacros.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
47 find_parent_devpath(const char * const child, char **parent)
53 /* strip leading /dev/ */
54 node = strrchr(child, '/');
59 /* look up full path symlink */
60 ret = sysfs_readlink(&linkbuf, "class/block/%s", node);
61 if (ret < 0 || !linkbuf)
65 node = strrchr(linkbuf, '/');
71 node = strrchr(linkbuf, '/');
77 /* write out new path */
78 ret = asprintf(parent, "/dev/%s", node);
86 set_part_name(struct device *dev, const char * const fmt, ...)
96 rc = vasprintf(&dev->part_name, fmt, ap);
101 efi_error("could not allocate memory");
107 reset_part_name(struct device *dev)
112 if (dev->part_name) {
113 free(dev->part_name);
114 dev->part_name = NULL;
120 if (dev->probes[dev->n_probes]->make_part_name) {
121 part = dev->probes[dev->n_probes]->make_part_name(dev);
122 dev->part_name = part;
125 rc = asprintf(&dev->part_name, "%s%d",
126 dev->disk_name, dev->part);
128 efi_error("could not allocate memory");
134 set_part(struct device *dev, int value)
138 if (dev->part == value)
142 rc = reset_part_name(dev);
144 efi_error("reset_part_name() failed");
150 set_disk_name(struct device *dev, const char * const fmt, ...)
157 rc = vasprintf(&dev->disk_name, fmt, ap);
162 efi_error("could not allocate memory");
168 set_disk_and_part_name(struct device *dev)
171 * results are like such:
172 * maj:min -> ../../devices/pci$PCI_STUFF/$BLOCKDEV_STUFF/block/$DISK/$PART
175 char *ultimate = pathseg(dev->link, -1);
176 char *penultimate = pathseg(dev->link, -2);
177 char *approximate = pathseg(dev->link, -3);
178 char *proximate = pathseg(dev->link, -4);
181 debug(DEBUG, "dev->disk_name:%p dev->part_name:%p", dev->disk_name, dev->part_name);
182 debug(DEBUG, "dev->part:%d", dev->part);
183 debug(DEBUG, "ultimate:%s", ultimate);
184 debug(DEBUG, "penultimate:%s", penultimate);
185 debug(DEBUG, "approximate:%s", approximate);
186 debug(DEBUG, "proximate:%s", proximate);
188 if (!strcmp(proximate, "nvme") || !strcmp(approximate, "block")) {
190 * 259:1 -> ../../devices/pci0000:00/0000:00:1d.0/0000:05:00.0/nvme/nvme0/nvme0n1/nvme0n1p1
191 * 8:1 -> ../../devices/pci0000:00/0000:00:17.0/ata2/host1/target1:0:0/1:0:0:0/block/sda/sda1
192 * 8:33 -> ../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/host4/port-4:0/end_device-4:0/target4:0:0/4:0:0:0/block/sdc/sdc1
193 * 252:1 -> ../../devices/pci0000:00/0000:00:07.0/virtio2/block/vda/vda1
194 * 259:3 -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0012:00/ndbus0/region11/btt11.0/block/pmem11s/pmem11s1
196 set_disk_name(dev, "%s", penultimate);
197 set_part_name(dev, "%s", ultimate);
198 debug(DEBUG, "disk:%s part:%s", penultimate, ultimate);
199 } else if (!strcmp(approximate, "nvme")) {
201 * 259:0 -> ../../devices/pci0000:00/0000:00:1d.0/0000:05:00.0/nvme/nvme0/nvme0n1
203 set_disk_name(dev, "%s", ultimate);
204 set_part_name(dev, "%sp%d", ultimate, dev->part);
205 debug(DEBUG, "disk:%s part:%sp%d", ultimate, ultimate, dev->part);
206 } else if (!strcmp(penultimate, "block")) {
208 * 253:0 -> ../../devices/virtual/block/dm-0 (... I guess)
209 * 8:0 -> ../../devices/pci0000:00/0000:00:17.0/ata2/host1/target1:0:0/1:0:0:0/block/sda
210 * 11:0 -> ../../devices/pci0000:00/0000:00:11.5/ata3/host2/target2:0:0/2:0:0:0/block/sr0
211 * 8:32 -> ../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/host4/port-4:0/end_device-4:0/target4:0:0/4:0:0:0/block/sdc
212 * 252:0 -> ../../devices/pci0000:00/0000:00:07.0/virtio2/block/vda
213 * 259:0 -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0012:00/ndbus0/region9/btt9.0/block/pmem9s
214 * 259:1 -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0012:00/ndbus0/region11/btt11.0/block/pmem11s
216 set_disk_name(dev, "%s", ultimate);
217 set_part_name(dev, "%s%d", ultimate, dev->part);
218 debug(DEBUG, "disk:%s part:%s%d", ultimate, ultimate, dev->part);
219 } else if (!strcmp(approximate, "mtd")) {
221 * 31:0 -> ../../devices/platform/1e000000.palmbus/1e000b00.spi/spi_master/spi32766/spi32766.0/mtd/mtd0/mtdblock0
223 set_disk_name(dev, "%s", ultimate);
224 debug(DEBUG, "disk:%s", ultimate);
230 static struct dev_probe *dev_probes[] = {
232 * pmem needs to be before PCI, so if it provides root it'll
248 supports_iface(struct dev_probe *probe, enum interface_type iftype)
250 for (unsigned int i = 0; probe->iftypes[i] != unknown; i++)
251 if (probe->iftypes[i] == iftype)
257 device_free(struct device *dev)
273 if (dev->interface_type == network) {
278 free(dev->disk_name);
280 free(dev->part_name);
286 memset(dev, 0, sizeof(*dev));
291 *device_get(int fd, int partition)
294 char *linkbuf = NULL, *tmpbuf = NULL;
295 unsigned int i, n = 0;
298 size_t nmemb = (sizeof(dev_probes)
299 / sizeof(dev_probes[0])) + 1;
301 dev = calloc(1, sizeof(*dev));
303 efi_error("could not allocate %zd bytes", sizeof(*dev));
307 dev->part = partition;
308 debug(DEBUG, "partition:%d dev->part:%d", partition, dev->part);
309 dev->probes = calloc(nmemb, sizeof(struct dev_probe *));
311 efi_error("could not allocate %zd bytes",
312 nmemb * sizeof(struct dev_probe *));
316 rc = fstat(fd, &dev->stat);
318 efi_error("fstat(%d) failed", fd);
322 dev->pci_root.pci_root_domain = 0xffff;
323 dev->pci_root.pci_root_bus = 0xff;
325 if (S_ISBLK(dev->stat.st_mode)) {
326 dev->major = major(dev->stat.st_rdev);
327 dev->minor = minor(dev->stat.st_rdev);
328 } else if (S_ISREG(dev->stat.st_mode)) {
329 dev->major = major(dev->stat.st_dev);
330 dev->minor = minor(dev->stat.st_dev);
332 efi_error("device is not a block device or regular file");
336 rc = sysfs_readlink(&linkbuf, "dev/block/%"PRIu64":%"PRIu32,
337 dev->major, dev->minor);
338 if (rc < 0 || !linkbuf) {
339 efi_error("readlink of /sys/dev/block/%"PRIu64":%"PRIu32" failed",
340 dev->major, dev->minor);
344 dev->link = strdup(linkbuf);
346 efi_error("strdup(\"%s\") failed", linkbuf);
349 debug(DEBUG, "dev->link: %s\n", dev->link);
351 if (dev->part == -1) {
352 rc = read_sysfs_file(&tmpbuf, "dev/block/%s/partition", dev->link);
354 efi_error("device has no /partition node; not a partition");
356 rc = sscanf((char *)tmpbuf, "%d\n", &dev->part);
358 efi_error("couldn't parse partition number for %s", tmpbuf);
362 rc = set_disk_and_part_name(dev);
364 efi_error("could not set disk and partition names");
367 debug(DEBUG, "dev->disk_name: %s", dev->disk_name);
368 debug(DEBUG, "dev->part_name: %s", dev->part_name);
370 rc = sysfs_readlink(&tmpbuf, "block/%s/device", dev->disk_name);
371 if (rc < 0 || !tmpbuf) {
372 efi_error("readlink of /sys/block/%s/device failed",
377 dev->device = strdup(tmpbuf);
379 efi_error("strdup(\"%s\") failed", tmpbuf);
383 rc = sysfs_readlink(&tmpbuf, "block/%s/device/driver", dev->disk_name);
384 if (rc < 0 || !tmpbuf) {
385 efi_error("readlink of /sys/block/%s/device/driver failed",
390 linkbuf = pathseg(tmpbuf, -1);
392 efi_error("could not get segment -1 of \"%s\"", tmpbuf);
396 dev->driver = strdup(linkbuf);
398 efi_error("strdup(\"%s\") failed", linkbuf);
402 const char *current = dev->link;
403 bool needs_root = true;
405 debug(DEBUG, "searching for device nodes in %s", dev->link);
406 for (i = 0; dev_probes[i] && dev_probes[i]->parse; i++) {
407 struct dev_probe *probe = dev_probes[i];
410 if (!needs_root && (probe->flags & DEV_PROVIDES_ROOT)) {
411 debug(DEBUG, "not testing %s because flags is 0x%x", probe->name, probe->flags);
415 debug(DEBUG, "trying %s", probe->name);
416 pos = probe->parse(dev, current);
418 efi_error("parsing %s failed", probe->name);
420 } else if (pos == 0) {
423 debug(DEBUG, "%s matched %s", probe->name, current);
425 if (probe->flags & DEV_PROVIDES_HD)
427 dev->probes[n++] = dev_probes[i];
429 debug(DEBUG, "current:%s", current);
431 if (!*current || !strncmp(current, "block/", 6))
435 if (dev->interface_type == unknown) {
436 efi_error("unknown storage interface");
448 make_blockdev_path(uint8_t *buf, ssize_t size, struct device *dev)
452 debug(DEBUG, "entry buf:%p size:%zd", buf, size);
454 for (unsigned int i = 0; dev->probes[i] &&
455 dev->probes[i]->parse; i++) {
456 struct dev_probe *probe = dev->probes[i];
462 sz = probe->create(dev, buf + off, size ? size - off : 0, 0);
464 efi_error("could not create %s device path",
471 debug(DEBUG, "= %zd", off);
477 make_mac_path(uint8_t *buf, ssize_t size, const char * const ifname)
480 struct ethtool_drvinfo drvinfo = { 0, };
482 ssize_t ret = -1, sz, off = 0;
483 char busname[PATH_MAX+1] = "";
486 memset(&dev, 0, sizeof (dev));
487 dev.interface_type = network;
488 dev.ifname = strdup(ifname);
493 * find the device link, which looks like:
494 * ../../devices/$PCI_STUFF/net/$IFACE
496 rc = sysfs_readlink(&dev.link, "class/net/%s", ifname);
497 if (rc < 0 || !dev.link)
500 memset(&ifr, 0, sizeof (ifr));
501 strncpy(ifr.ifr_name, ifname, IF_NAMESIZE);
502 ifr.ifr_name[IF_NAMESIZE-1] = '\0';
503 drvinfo.cmd = ETHTOOL_GDRVINFO;
504 ifr.ifr_data = (caddr_t)&drvinfo;
506 fd = socket(AF_INET, SOCK_DGRAM, 0);
510 rc = ioctl(fd, SIOCETHTOOL, &ifr);
514 strncpy(busname, drvinfo.bus_info, PATH_MAX);
516 rc = ioctl(fd, SIOCGIFHWADDR, &ifr);
520 sz = pci_parser.create(&dev, buf, size, off);
525 sz = efidp_make_mac_addr(buf+off, size?size-off:0,
526 ifr.ifr_ifru.ifru_hwaddr.sa_family,
527 (uint8_t *)ifr.ifr_ifru.ifru_hwaddr.sa_data,
528 sizeof(ifr.ifr_ifru.ifru_hwaddr.sa_data));
540 /************************************************************
543 * - filedes is an open file descriptor, suitable for reading
546 * sector size, or 512.
547 ************************************************************/
549 get_sector_size(int filedes)
551 int rc, sector_size = 512;
553 rc = ioctl(filedes, BLKSSZGET, §or_size);