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/>.
26 #include <linux/ethtool.h>
27 #include <linux/version.h>
28 #include <linux/sockios.h>
30 #include <scsi/scsi.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
40 #include <sys/sysmacros.h>
51 __attribute__((__visibility__ ("hidden")))
52 set_disk_and_part_name(struct disk_info *info)
57 rc = sysfs_readlink(&linkbuf, "/sys/dev/block/%"PRIu64":%hhu",
58 info->major, info->minor);
63 ultimate = strrchr(linkbuf, '/');
72 penultimate = strrchr(linkbuf, '/');
80 * If there's a better way to figure this out, it'd be good, because
81 * I don't want to have to change this for every new disk type...
83 if (!strcmp(penultimate, "block")) {
84 if (!info->disk_name) {
85 info->disk_name = strdup(ultimate);
89 if (!info->part_name) {
90 rc = asprintf(&info->part_name, "%s%d", info->disk_name,
95 } else if (!strncmp(penultimate, "nvme", 4)) {
96 if (!info->disk_name) {
97 info->disk_name = strdup(ultimate);
101 if (!info->part_name) {
102 rc = asprintf(&info->part_name, "%sp%d",
103 info->disk_name, info->part);
108 if (!info->disk_name) {
109 info->disk_name = strdup(penultimate);
110 if (!info->disk_name)
113 if (!info->part_name) {
114 info->part_name = strdup(ultimate);
115 if (!info->part_name)
124 __attribute__((__visibility__ ("hidden")))
125 get_partition_number(const char *devpath)
127 struct stat statbuf = { 0, };
129 unsigned int maj, min;
134 rc = stat(devpath, &statbuf);
138 if (!S_ISBLK(statbuf.st_mode)) {
143 maj = major(statbuf.st_rdev);
144 min = minor(statbuf.st_rdev);
146 rc = sysfs_readlink(&linkbuf, "/sys/dev/block/%u:%u", maj, min);
150 rc = read_sysfs_file(&partbuf, "/sys/dev/block/%s/partition", linkbuf);
154 rc = sscanf((char *)partbuf, "%d\n", &ret);
161 __attribute__((__visibility__ ("hidden")))
162 find_parent_devpath(const char * const child, char **parent)
168 /* strip leading /dev/ */
169 node = strrchr(child, '/');
174 /* look up full path symlink */
175 ret = sysfs_readlink(&linkbuf, "/sys/class/block/%s", node);
180 node = strrchr(linkbuf, '/');
186 node = strrchr(linkbuf, '/');
192 /* write out new path */
193 ret = asprintf(parent, "/dev/%s", node);
201 sysfs_test_nvme(const char *buf, ssize_t size)
203 if (!strncmp(buf, "nvme/", MIN(size, 5)))
209 sysfs_test_sata(const char *buf, ssize_t size)
211 if (!strncmp(buf, "ata", MIN(size, 3)))
217 sysfs_test_sas(const char *buf, ssize_t size)
221 struct stat statbuf = { 0, };
227 newbuf = strndupa(buf, size+1);
233 rc = sscanf(newbuf, "host%d/%n", &host, &sz);
235 return (errno == 0) ? 0 : -1;
237 rc = asprintfa(&path, "/sys/class/scsi_host/host%d/host_sas_address",
242 rc = stat(path, &statbuf);
249 sysfs_sata_get_port_info(uint32_t print_id, struct disk_info *info)
256 d = opendir("/sys/class/ata_device/");
260 while ((de = readdir(d)) != NULL) {
261 uint32_t found_print_id;
263 uint32_t found_devno = 0;
265 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
269 rc = sscanf(de->d_name, "dev%d.%d.%d", &found_print_id,
270 &found_pmp, &found_devno);
271 if (rc < 2 || rc > 3) {
275 } else if (found_print_id != print_id) {
277 } else if (rc == 3) {
279 * the kernel doesn't't ever tell us the SATA PMPN
280 * sentinal value, it'll give us devM.N instead of
281 * devM.N.O in that case instead.
283 if (found_pmp > 0x7fff) {
288 info->sata_info.ata_devno = 0;
289 info->sata_info.ata_pmp = found_pmp;
291 } else if (rc == 2) {
292 info->sata_info.ata_devno = 0;
293 info->sata_info.ata_pmp = 0xffff;
299 rc = read_sysfs_file(&buf, "/sys/class/ata_port/ata%d/port_no",
304 rc = sscanf((char *)buf, "%d", &info->sata_info.ata_port);
309 * ata_port numbers are 1-indexed from libata in the kernel, but
310 * they're 0-indexed in the spec. For maximal confusion.
312 if (info->sata_info.ata_port == 0) {
316 info->sata_info.ata_port -= 1;
323 sysfs_parse_nvme(uint8_t *buf, ssize_t size, ssize_t *off,
324 const char *pbuf, ssize_t psize, ssize_t *poff,
325 struct disk_info *info)
329 uint8_t *filebuf = NULL;
336 newpbuf = strndupa(pbuf, psize+1);
339 newpbuf[psize] = '\0';
348 rc = sscanf(newpbuf, "nvme/nvme%d/nvme%dn%d%n", &tosser0,
349 &ctrl_id, &ns_id, &psz);
354 info->nvme_info.ctrl_id = ctrl_id;
355 info->nvme_info.ns_id = ns_id;
356 info->nvme_info.has_eui = 0;
357 info->interface_type = nvme;
360 * now fish the eui out of sysfs is there is one...
362 rc = read_sysfs_file(&filebuf,
363 "/sys/class/block/nvme%dn%d/device/eui",
371 rc = sscanf((char *)filebuf,
372 "%02hhx-%02hhx-%02hhx-%02hhx-"
373 "%02hhx-%02hhx-%02hhx-%02hhx",
374 &eui[0], &eui[1], &eui[2], &eui[3],
375 &eui[4], &eui[5], &eui[6], &eui[7]);
380 info->nvme_info.has_eui = 1;
381 memcpy(info->nvme_info.eui, eui, sizeof(eui));
384 *off = efidp_make_nvme(buf, size,
385 info->nvme_info.ns_id,
386 info->nvme_info.has_eui ? info->nvme_info.eui
393 sysfs_parse_sata(uint8_t *buf, ssize_t size, ssize_t *off,
394 const char *pbuf, ssize_t psize, ssize_t *poff,
395 struct disk_info *info)
406 uint32_t scsi_device;
407 uint32_t scsi_target;
412 newpbuf = strndupa(pbuf, psize+1);
415 newpbuf[psize] = '\0';
417 /* find the ata info:
418 * ata1/host0/target0:0:0/
421 rc = sscanf(newpbuf, "ata%d/host%d/target%d:%d:%d/%n",
422 &print_id, &scsi_bus, &scsi_device, &scsi_target, &scsi_lun,
428 /* find the emulated scsi bits (and ignore them)
431 uint32_t dummy0, dummy1, dummy2;
433 rc = sscanf(newpbuf+*poff, "%d:%d:%d:%"PRIu64"/%n", &dummy0, &dummy1,
434 &dummy2, &dummy3, &psz);
442 char *disk_name = NULL;
443 char *part_name = NULL;
445 rc = sscanf(newpbuf+*poff, "block/%m[^/]%n/%m[^/]%n", &disk_name, &psz,
448 rc = asprintf(&part_name, "%s%d", disk_name, info->part);
455 } else if (rc != 2) {
462 info->sata_info.scsi_bus = scsi_bus;
463 info->sata_info.scsi_device = scsi_device;
464 info->sata_info.scsi_target = scsi_target;
465 info->sata_info.scsi_lun = scsi_lun;
467 rc = sysfs_sata_get_port_info(print_id, info);
474 /* check the original of this; it's guaranteed in our copy */
475 if (pbuf[*poff] != '\0') {
482 info->disk_name = disk_name;
483 info->part_name = part_name;
484 if (info->interface_type == interface_type_unknown)
485 info->interface_type = sata;
487 if (info->interface_type == ata) {
488 *off = efidp_make_atapi(buf, size, info->sata_info.ata_port,
489 info->sata_info.ata_pmp,
490 info->sata_info.ata_devno);
492 *off = efidp_make_sata(buf, size, info->sata_info.ata_port,
493 info->sata_info.ata_pmp,
494 info->sata_info.ata_devno);
500 sysfs_parse_sas(uint8_t *buf, ssize_t size, ssize_t *off,
501 const char *pbuf, ssize_t psize, ssize_t *poff,
502 struct disk_info *info)
506 uint8_t *filebuf = NULL;
507 uint64_t sas_address;
511 newpbuf = strndupa(pbuf, psize+1);
514 newpbuf[psize] = '\0';
520 * host4/port-4:0/end_device-4:0/target4:0:0/4:0:0:0/block/sdc/sdc1
522 uint32_t tosser0, tosser1, tosser2;
524 /* ignore a bunch of stuff
526 * or host4/port-4:0:0
528 rc = sscanf(newpbuf+*poff, "host%d/port-%d:%d%n", &tosser0, &tosser1,
535 rc = sscanf(pbuf+*poff, ":%d%n", &tosser0, &psz);
536 if (rc != 0 && rc != 1)
542 * or /end_device-4:0:0
543 * awesomely these are the exact same fields that go into port-blah,
544 * but we don't care for now about any of them anyway.
546 rc = sscanf(newpbuf+*poff, "/end_device-%d:%d%n", &tosser0, &tosser1,
553 rc = sscanf(newpbuf+*poff, ":%d%n", &tosser0, &psz);
554 if (rc != 0 && rc != 1)
562 rc = sscanf(newpbuf+*poff, "/target%d:%d:%"PRIu64"/%n", &tosser0,
563 &tosser1, &tosser3, &psz);
571 rc = sscanf(newpbuf+*poff, "%d:%d:%d:%"PRIu64"/%n",
572 &info->sas_info.scsi_bus,
573 &info->sas_info.scsi_device,
574 &info->sas_info.scsi_target,
575 &info->sas_info.scsi_lun, &psz);
583 char *disk_name = NULL;
584 char *part_name = NULL;
585 rc = sscanf(newpbuf+*poff, "block/%m[^/]/%m[^/]%n", &disk_name,
591 /* check the original of this; it's guaranteed in our copy */
592 if (pbuf[*poff] != '\0') {
600 * we also need to get the actual sas_address from someplace...
602 rc = read_sysfs_file(&filebuf,
603 "/sys/class/block/%s/device/sas_address",
608 rc = sscanf((char *)filebuf, "%"PRIx64, &sas_address);
612 info->sas_info.sas_address = sas_address;
613 info->disk_name = disk_name;
614 info->part_name = part_name;
615 info->interface_type = sas;
617 *off = efidp_make_sas(buf, size, sas_address);
622 make_pci_path(uint8_t *buf, ssize_t size, char *pathstr, ssize_t *pathoff)
629 if (pathstr == NULL || pathoff == NULL || pathstr[0] == '\0') {
636 uint16_t root_domain;
638 uint32_t acpi_hid = 0;
639 uint64_t acpi_uid_int = 0;
641 * find the pci root domain and port; they basically look like:
645 rc = sscanf(pathstr+poff, "pci%hx:%hhx/%n", &root_domain,
651 uint8_t *fbuf = NULL;
652 rc = read_sysfs_file(&fbuf,
653 "/sys/devices/pci%04x:%02x/firmware_node/hid",
654 root_domain, root_bus);
659 rc = sscanf((char *)fbuf, "PNP%hx", &tmp16);
662 acpi_hid = EFIDP_EFI_PNP_ID(tmp16);
664 /* Apparently basically nothing can look up a PcieRoot() node,
665 * because they just check _CID. So since _CID for the root pretty
666 * much always has to be PNP0A03 anyway, just use that no matter
669 if (acpi_hid == EFIDP_ACPI_PCIE_ROOT_HID)
670 acpi_hid = EFIDP_ACPI_PCI_ROOT_HID;
675 rc = read_sysfs_file(&fbuf,
676 "/sys/devices/pci%4x:%02x/firmware_node/uid",
677 root_domain, root_bus);
678 if (rc <= 0 && errno != ENOENT)
681 rc = sscanf((char *)fbuf, "%"PRIu64"\n", &acpi_uid_int);
683 /* kernel uses "%s\n" to print it, so there
684 * should always be some value and a newline... */
685 int l = strlen((char *)buf);
695 sz = efidp_make_acpi_hid_ex(buf+off, size?size-off:0,
696 acpi_hid, 0, 0, "", (char *)fbuf,
699 sz = efidp_make_acpi_hid(buf+off, size?size-off:0,
700 acpi_hid, acpi_uid_int);
706 /* find the pci domain/bus/device/function:
707 * 0000:00:01.0/0000:01:00.0/
708 * ^d ^b ^d ^f (of the last one in the series)
713 uint8_t bus, device, function;
714 rc = sscanf(pathstr+poff, "%hx:%hhx:%hhx.%hhx/%n",
715 &domain, &bus, &device, &function, &psz);
721 sz = efidp_make_pci(buf+off, size?size-off:0, device, function);
734 __attribute__((__visibility__ ("hidden")))
735 make_blockdev_path(uint8_t *buf, ssize_t size, struct disk_info *info)
737 char *linkbuf = NULL;
738 char *driverbuf = NULL;
739 ssize_t off=0, sz=0, loff=0;
744 rc = sysfs_readlink(&linkbuf, "/sys/dev/block/%"PRIu64":%u",
745 info->major, info->minor);
750 * the sysfs path basically looks like:
751 * ../../devices/pci$PCI_STUFF/$BLOCKDEV_STUFF/block/$DISK/$PART
753 rc = sscanf(linkbuf+loff, "../../devices/%n", &lsz);
759 sz = make_pci_path(buf, size, linkbuf+loff, &tmplsz);
765 char *tmppath = strdupa(linkbuf);
768 tmppath[loff] = '\0';
769 rc = sysfs_readlink(&driverbuf, "/sys/dev/block/%s/driver", tmppath);
773 char *driver = strrchr(driverbuf, '/');
774 if (!driver || !*driver)
778 if (!strncmp(driver, "pata_", 5) || !(strcmp(driver, "ata_piix")))
779 info->interface_type = ata;
781 if (info->interface_type == interface_type_unknown ||
782 info->interface_type == atapi ||
783 info->interface_type == usb ||
784 info->interface_type == i1394 ||
785 info->interface_type == fibre ||
786 info->interface_type == i2o ||
787 info->interface_type == md) {
791 rc = sscanf(linkbuf+loff, "virtio%x/%n", &tosser, &tmpoff);
794 } else if (rc == 1) {
795 info->interface_type = virtblk;
801 /* /dev/nvme0n1 looks like:
802 * /sys/dev/block/259:0 -> ../../devices/pci0000:00/0000:00:1d.0/0000:05:00.0/nvme/nvme0/nvme0n1
805 rc = sysfs_test_nvme(linkbuf+loff, PATH_MAX-off);
810 rc = sysfs_parse_nvme(buf+off, size?size-off:0, &sz,
811 linkbuf+loff, PATH_MAX-off,
821 /* /dev/sda as SATA looks like:
822 * /sys/dev/block/8:0 -> ../../devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda
825 rc = sysfs_test_sata(linkbuf+loff, PATH_MAX-off);
828 if (!found && rc > 0) {
830 rc = sysfs_parse_sata(buf+off, size?size-off:0, &sz,
831 linkbuf+loff, PATH_MAX-off,
841 /* /dev/sdc as SAS looks like:
842 * /sys/dev/block/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
845 rc = sysfs_test_sas(linkbuf+loff, PATH_MAX-off);
850 rc = sysfs_parse_sas(buf+off, size?size-off:0, &sz,
851 linkbuf+loff, PATH_MAX-off,
861 if (!found && info->interface_type == scsi) {
864 rc = sysfs_readlink(&linkbuf, "/sys/class/block/%s/device",
869 rc = sscanf(linkbuf, "../../../%d:%d:%d:%"PRIu64,
870 &info->scsi_info.scsi_bus,
871 &info->scsi_info.scsi_device,
872 &info->scsi_info.scsi_target,
873 &info->scsi_info.scsi_lun);
877 sz = efidp_make_scsi(buf+off, size?size-off:0,
878 info->scsi_info.scsi_target,
879 info->scsi_info.scsi_lun);
895 __attribute__((__visibility__ ("hidden")))
896 eb_disk_info_from_fd(int fd, struct disk_info *info)
901 memset(info, 0, sizeof *info);
903 info->pci_root.root_pci_domain = 0xffff;
904 info->pci_root.root_pci_bus = 0xff;
906 memset(&buf, 0, sizeof(struct stat));
907 rc = fstat(fd, &buf);
912 if (S_ISBLK(buf.st_mode)) {
913 info->major = buf.st_rdev >> 8;
914 info->minor = buf.st_rdev & 0xFF;
915 } else if (S_ISREG(buf.st_mode)) {
916 info->major = buf.st_dev >> 8;
917 info->minor = buf.st_dev & 0xFF;
919 printf("Cannot stat non-block or non-regular file\n");
923 /* IDE disks can have up to 64 partitions, or 6 bits worth,
924 * and have one bit for the disk number.
925 * This leaves an extra bit at the top.
927 if (info->major == 3) {
928 info->disknum = (info->minor >> 6) & 1;
929 info->controllernum = (info->major - 3 + 0) + info->disknum;
930 info->interface_type = ata;
931 info->part = info->minor & 0x3F;
933 } else if (info->major == 22) {
934 info->disknum = (info->minor >> 6) & 1;
935 info->controllernum = (info->major - 22 + 2) + info->disknum;
936 info->interface_type = ata;
937 info->part = info->minor & 0x3F;
939 } else if (info->major >= 33 && info->major <= 34) {
940 info->disknum = (info->minor >> 6) & 1;
941 info->controllernum = (info->major - 33 + 4) + info->disknum;
942 info->interface_type = ata;
943 info->part = info->minor & 0x3F;
945 } else if (info->major >= 56 && info->major <= 57) {
946 info->disknum = (info->minor >> 6) & 1;
947 info->controllernum = (info->major - 56 + 8) + info->disknum;
948 info->interface_type = ata;
949 info->part = info->minor & 0x3F;
951 } else if (info->major >= 88 && info->major <= 91) {
952 info->disknum = (info->minor >> 6) & 1;
953 info->controllernum = (info->major - 88 + 12) + info->disknum;
954 info->interface_type = ata;
955 info->part = info->minor & 0x3F;
959 /* I2O disks can have up to 16 partitions, or 4 bits worth. */
960 if (info->major >= 80 && info->major <= 87) {
961 info->interface_type = i2o;
962 info->disknum = 16*(info->major-80) + (info->minor >> 4);
963 info->part = (info->minor & 0xF);
967 /* SCSI disks can have up to 16 partitions, or 4 bits worth
968 * and have one bit for the disk number.
970 if (info->major == 8) {
971 info->interface_type = scsi;
972 info->disknum = (info->minor >> 4);
973 info->part = (info->minor & 0xF);
975 } else if (info->major >= 65 && info->major <= 71) {
976 info->interface_type = scsi;
977 info->disknum = 16*(info->major-64) + (info->minor >> 4);
978 info->part = (info->minor & 0xF);
980 } else if (info->major >= 128 && info->major <= 135) {
981 info->interface_type = scsi;
982 info->disknum = 16*(info->major-128) + (info->minor >> 4);
983 info->part = (info->minor & 0xF);
992 make_net_pci_path(uint8_t *buf, ssize_t size, const char * const ifname)
994 char *linkbuf = NULL;
995 ssize_t off=0, sz=0, loff=0;
999 rc = sysfs_readlink(&linkbuf, "/sys/class/net/%s", ifname);
1004 * the sysfs path basically looks like:
1005 * ../../devices/$PCI_STUFF/net/$IFACE
1007 rc = sscanf(linkbuf+loff, "../../devices/%n", &lsz);
1013 sz = make_pci_path(buf, size, linkbuf+loff, &tmplsz);
1023 __attribute__((__visibility__ ("hidden")))
1024 make_mac_path(uint8_t *buf, ssize_t size, const char * const ifname)
1027 struct ethtool_drvinfo drvinfo = { 0, };
1029 ssize_t ret = -1, sz, off=0;
1030 char busname[PATH_MAX+1] = "";
1032 memset(&ifr, 0, sizeof (ifr));
1033 strncpy(ifr.ifr_name, ifname, IF_NAMESIZE);
1034 drvinfo.cmd = ETHTOOL_GDRVINFO;
1035 ifr.ifr_data = (caddr_t)&drvinfo;
1037 fd = socket(AF_INET, SOCK_DGRAM, 0);
1041 rc = ioctl(fd, SIOCETHTOOL, &ifr);
1045 strncpy(busname, drvinfo.bus_info, PATH_MAX);
1047 rc = ioctl(fd, SIOCGIFHWADDR, &ifr);
1051 sz = make_net_pci_path(buf, size, ifname);
1056 sz = efidp_make_mac_addr(buf+off, size?size-off:0,
1057 ifr.ifr_ifru.ifru_hwaddr.sa_family,
1058 (uint8_t *)ifr.ifr_ifru.ifru_hwaddr.sa_data,
1059 sizeof(ifr.ifr_ifru.ifru_hwaddr.sa_data));