Blame SOURCES/0014-Make-a-platform-ACPI-root-parser-separate-from-PCI-r.patch

80b73c
From 6b62aa40cfa1feb924609a065098da98c99e925c Mon Sep 17 00:00:00 2001
80b73c
From: Peter Jones <pjones@redhat.com>
80b73c
Date: Wed, 20 Jun 2018 14:45:14 -0400
80b73c
Subject: [PATCH 14/17] Make a platform ACPI root parser separate from PCI
80b73c
 roots.
80b73c
80b73c
Because apparently PNP0A03 and PNP0A0C weren't good enough.
80b73c
80b73c
Signed-off-by: Peter Jones <pjones@redhat.com>
80b73c
---
80b73c
 src/linux-acpi-root.c | 199 ++++++++++++++++++++++++++++++++++++++++++
80b73c
 src/linux-pci-root.c  | 136 +++++++++++++++++++++++++++++
80b73c
 src/linux-pci.c       |   1 -
80b73c
 src/linux.c           |   4 +-
80b73c
 src/linux.h           |   4 +-
80b73c
 5 files changed, 341 insertions(+), 3 deletions(-)
80b73c
 create mode 100644 src/linux-acpi-root.c
80b73c
 create mode 100644 src/linux-pci-root.c
80b73c
80b73c
diff --git a/src/linux-acpi-root.c b/src/linux-acpi-root.c
80b73c
new file mode 100644
80b73c
index 00000000000..c7d8276a642
80b73c
--- /dev/null
80b73c
+++ b/src/linux-acpi-root.c
80b73c
@@ -0,0 +1,199 @@
80b73c
+/*
80b73c
+ * libefiboot - library for the manipulation of EFI boot variables
80b73c
+ * Copyright 2012-2018 Red Hat, Inc.
80b73c
+ *
80b73c
+ * This library is free software; you can redistribute it and/or
80b73c
+ * modify it under the terms of the GNU Lesser General Public License as
80b73c
+ * published by the Free Software Foundation; either version 2.1 of the
80b73c
+ * License, or (at your option) any later version.
80b73c
+ *
80b73c
+ * This library is distributed in the hope that it will be useful,
80b73c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
80b73c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
80b73c
+ * Lesser General Public License for more details.
80b73c
+ *
80b73c
+ * You should have received a copy of the GNU Lesser General Public
80b73c
+ * License along with this library; if not, see
80b73c
+ * <http://www.gnu.org/licenses/>.
80b73c
+ *
80b73c
+ */
80b73c
+
80b73c
+#include "fix_coverity.h"
80b73c
+
80b73c
+#include <errno.h>
80b73c
+#include <fcntl.h>
80b73c
+#include <inttypes.h>
80b73c
+#include <stdint.h>
80b73c
+#include <unistd.h>
80b73c
+
80b73c
+#include "efiboot.h"
80b73c
+
80b73c
+/*
80b73c
+ * support for ACPI-like platform root hub and devices
80b73c
+ *
80b73c
+ * various devices /sys/dev/block/$major:$minor start with:
80b73c
+ * maj:min -> ../../devices/ACPI0000:00/$PCI_DEVICES/$BLOCKDEV_STUFF/block/$DISK/$PART
80b73c
+ * i.e.:                    APMC0D0D:00/ata1/host0/target0:0:0/0:0:0:0/block/sda
80b73c
+ *                          ^ root hub ^blockdev stuff
80b73c
+ * or:
80b73c
+ * maj:min -> ../../devices/ACPI0000:00/$PCI_DEVICES/$BLOCKDEV_STUFF/block/$DISK/$PART
80b73c
+ * i.e.:                    APMC0D0D:00/0000:00:1d.0/0000:05:00.0/ata1/host0/target0:0:0/0:0:0:0/block/sda
80b73c
+ *                          ^ root hub ^pci dev      ^pci dev     ^ blockdev stuff
80b73c
+ */
80b73c
+static ssize_t
80b73c
+parse_acpi_root(struct device *dev, const char *current, const char *root UNUSED)
80b73c
+{
80b73c
+        int rc;
80b73c
+        int pos;
80b73c
+        uint16_t pad0;
80b73c
+        uint8_t pad1;
80b73c
+        char *acpi_header = NULL;
80b73c
+        char *colon;
80b73c
+
80b73c
+        const char *devpart = current;
80b73c
+        char *spaces;
80b73c
+
80b73c
+        pos = strlen(current);
80b73c
+        spaces = alloca(pos+1);
80b73c
+        memset(spaces, ' ', pos+1);
80b73c
+        spaces[pos] = '\0';
80b73c
+        pos = 0;
80b73c
+
80b73c
+        debug(DEBUG, "entry");
80b73c
+
80b73c
+        /*
80b73c
+         * find the ACPI root dunno0 and dunno1; they basically look like:
80b73c
+         * ABCD0000:00/
80b73c
+         *     ^d0  ^d1
80b73c
+         * This is annoying because "/%04ms%h:%hhx/" won't bind from the right
80b73c
+         * side in sscanf.
80b73c
+         */
80b73c
+        rc = sscanf(devpart, "../../devices/platform/%n", &pos;;
80b73c
+        debug(DEBUG, "devpart:\"%s\" rc:%d pos:%d", devpart, rc, pos);
80b73c
+        if (rc != 0 || pos < 1)
80b73c
+                return 0;
80b73c
+        devpart += pos;
80b73c
+
80b73c
+        /*
80b73c
+         * If it's too short to be A0000:00, it's not an ACPI string
80b73c
+         */
80b73c
+        if (strlen(devpart) < 8)
80b73c
+                return 0;
80b73c
+
80b73c
+        colon = strchr(devpart, ':');
80b73c
+        if (!colon)
80b73c
+                return 0;
80b73c
+        pos = colon - devpart;
80b73c
+
80b73c
+        /*
80b73c
+         * If colon doesn't point at something between one of these:
80b73c
+         * A0000:00 ACPI0000:00
80b73c
+         *      ^ 5         ^ 8
80b73c
+         * Then it's not an ACPI string.
80b73c
+         */
80b73c
+        if (pos < 5 || pos > 8)
80b73c
+                return 0;
80b73c
+
80b73c
+        dev->acpi_root.acpi_hid_str = strndup(devpart, pos + 1);
80b73c
+        if (!dev->acpi_root.acpi_hid_str) {
80b73c
+                efi_error("Could not allocate memory");
80b73c
+                return -1;
80b73c
+        }
80b73c
+        dev->acpi_root.acpi_hid_str[pos] = 0;
80b73c
+        debug(DEBUG, "acpi_hid_str:\"%s\"", dev->acpi_root.acpi_hid_str);
80b73c
+
80b73c
+        pos -= 4;
80b73c
+        debug(DEBUG, "devpart:\"%s\" rc:%d pos:%d", devpart, rc, pos);
80b73c
+        acpi_header = strndupa(devpart, pos);
80b73c
+        if (!acpi_header)
80b73c
+                return 0;
80b73c
+        acpi_header[pos] = 0;
80b73c
+        debug(DEBUG, "devpart:\"%s\" acpi_header:\"%s\"", devpart, acpi_header);
80b73c
+        devpart += pos;
80b73c
+
80b73c
+        /*
80b73c
+         * If we can't find these numbers, it's not an ACPI string
80b73c
+         */
80b73c
+        rc = sscanf(devpart, "%hx:%hhx/%n", &pad0, &pad1, &pos;;
80b73c
+        if (rc != 2) {
80b73c
+                efi_error("Could not parse ACPI path \"%s\"", devpart);
80b73c
+                return 0;
80b73c
+        }
80b73c
+        debug(DEBUG, "devpart:\"%s\" parsed:%04hx:%02hhx pos:%d rc:%d",
80b73c
+              devpart, pad0, pad1, pos, rc);
80b73c
+
80b73c
+        devpart += pos;
80b73c
+
80b73c
+        rc = parse_acpi_hid_uid(dev, "devices/platform/%s%04hX:%02hhX",
80b73c
+                                acpi_header, pad0, pad1);
80b73c
+        debug(DEBUG, "rc:%d acpi_header:%s pad0:%04hX pad1:%02hhX",
80b73c
+              rc, acpi_header, pad0, pad1);
80b73c
+        if (rc < 0 && errno == ENOENT) {
80b73c
+                rc = parse_acpi_hid_uid(dev, "devices/platform/%s%04hx:%02hhx",
80b73c
+                                acpi_header, pad0, pad1);
80b73c
+                debug(DEBUG, "rc:%d acpi_header:%s pad0:%04hx pad1:%02hhx",
80b73c
+                      rc, acpi_header, pad0, pad1);
80b73c
+        }
80b73c
+        if (rc < 0) {
80b73c
+                efi_error("Could not parse hid/uid");
80b73c
+                return rc;
80b73c
+        }
80b73c
+        debug(DEBUG, "Parsed HID:0x%08x UID:0x%"PRIx64" uidstr:\"%s\" path:\"%s\"",
80b73c
+              dev->acpi_root.acpi_hid, dev->acpi_root.acpi_uid,
80b73c
+              dev->acpi_root.acpi_uid_str,
80b73c
+              dev->acpi_root.acpi_cid_str);
80b73c
+
80b73c
+        return devpart - current;
80b73c
+}
80b73c
+
80b73c
+static ssize_t
80b73c
+dp_create_acpi_root(struct device *dev,
80b73c
+                    uint8_t *buf, ssize_t size, ssize_t off)
80b73c
+{
80b73c
+        ssize_t sz = 0, new = 0;
80b73c
+
80b73c
+        debug(DEBUG, "entry buf:%p size:%zd off:%zd", buf, size, off);
80b73c
+
80b73c
+        if (dev->acpi_root.acpi_uid_str || dev->acpi_root.acpi_cid_str) {
80b73c
+                debug(DEBUG, "creating acpi_hid_ex dp hid:0x%08x uid:0x%"PRIx64" uidstr:\"%s\" cidstr:\"%s\"",
80b73c
+                      dev->acpi_root.acpi_hid, dev->acpi_root.acpi_uid,
80b73c
+                      dev->acpi_root.acpi_uid_str, dev->acpi_root.acpi_cid_str);
80b73c
+                new = efidp_make_acpi_hid_ex(buf + off, size ? size - off : 0,
80b73c
+                                            dev->acpi_root.acpi_hid,
80b73c
+                                            dev->acpi_root.acpi_uid,
80b73c
+                                            dev->acpi_root.acpi_cid,
80b73c
+                                            dev->acpi_root.acpi_hid_str,
80b73c
+                                            dev->acpi_root.acpi_uid_str,
80b73c
+                                            dev->acpi_root.acpi_cid_str);
80b73c
+                if (new < 0) {
80b73c
+                        efi_error("efidp_make_acpi_hid_ex() failed");
80b73c
+                        return new;
80b73c
+                }
80b73c
+        } else {
80b73c
+                debug(DEBUG, "creating acpi_hid dp hid:0x%08x uid:0x%0"PRIx64,
80b73c
+                      dev->acpi_root.acpi_hid,
80b73c
+                      dev->acpi_root.acpi_uid);
80b73c
+                new = efidp_make_acpi_hid(buf + off, size ? size - off : 0,
80b73c
+                                         dev->acpi_root.acpi_hid,
80b73c
+                                         dev->acpi_root.acpi_uid);
80b73c
+                if (new < 0) {
80b73c
+                        efi_error("efidp_make_acpi_hid() failed");
80b73c
+                        return new;
80b73c
+                }
80b73c
+        }
80b73c
+        off += new;
80b73c
+        sz += new;
80b73c
+
80b73c
+        debug(DEBUG, "returning %zd", sz);
80b73c
+        return sz;
80b73c
+}
80b73c
+
80b73c
+enum interface_type acpi_root_iftypes[] = { acpi_root, unknown };
80b73c
+
80b73c
+struct dev_probe HIDDEN acpi_root_parser = {
80b73c
+        .name = "acpi_root",
80b73c
+        .iftypes = acpi_root_iftypes,
80b73c
+        .flags = DEV_PROVIDES_ROOT,
80b73c
+        .parse = parse_acpi_root,
80b73c
+        .create = dp_create_acpi_root,
80b73c
+};
80b73c
diff --git a/src/linux-pci-root.c b/src/linux-pci-root.c
80b73c
new file mode 100644
80b73c
index 00000000000..8f556a066f3
80b73c
--- /dev/null
80b73c
+++ b/src/linux-pci-root.c
80b73c
@@ -0,0 +1,136 @@
80b73c
+/*
80b73c
+ * libefiboot - library for the manipulation of EFI boot variables
80b73c
+ * Copyright 2012-2018 Red Hat, Inc.
80b73c
+ *
80b73c
+ * This library is free software; you can redistribute it and/or
80b73c
+ * modify it under the terms of the GNU Lesser General Public License as
80b73c
+ * published by the Free Software Foundation; either version 2.1 of the
80b73c
+ * License, or (at your option) any later version.
80b73c
+ *
80b73c
+ * This library is distributed in the hope that it will be useful,
80b73c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
80b73c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
80b73c
+ * Lesser General Public License for more details.
80b73c
+ *
80b73c
+ * You should have received a copy of the GNU Lesser General Public
80b73c
+ * License along with this library; if not, see
80b73c
+ * <http://www.gnu.org/licenses/>.
80b73c
+ *
80b73c
+ */
80b73c
+
80b73c
+#include "fix_coverity.h"
80b73c
+
80b73c
+#include <errno.h>
80b73c
+#include <fcntl.h>
80b73c
+#include <inttypes.h>
80b73c
+#include <stdint.h>
80b73c
+#include <unistd.h>
80b73c
+
80b73c
+#include "efiboot.h"
80b73c
+
80b73c
+/*
80b73c
+ * support for PCI root hub and devices
80b73c
+ *
80b73c
+ * various devices /sys/dev/block/$major:$minor start with:
80b73c
+ * maj:min -> ../../devices/pci$PCIROOT/$PCI_DEVICES/$BLOCKDEV_STUFF/block/$DISK/$PART
80b73c
+ * i.e.:                    pci0000:00/0000:00:1d.0/0000:05:00.0/
80b73c
+ *                          ^ root hub ^device      ^device
80b73c
+ *
80b73c
+ * for network devices, we also get:
80b73c
+ * /sys/class/net/$IFACE -> ../../devices/$PCI_STUFF/net/$IFACE
80b73c
+ *
80b73c
+ */
80b73c
+static ssize_t
80b73c
+parse_pci_root(struct device *dev, const char *current, const char *root UNUSED)
80b73c
+{
80b73c
+        int rc;
80b73c
+        int pos;
80b73c
+        uint16_t root_domain;
80b73c
+        uint8_t root_bus;
80b73c
+        const char *devpart = current;
80b73c
+        char *spaces;
80b73c
+
80b73c
+        pos = strlen(current);
80b73c
+        spaces = alloca(pos+1);
80b73c
+        memset(spaces, ' ', pos+1);
80b73c
+        spaces[pos] = '\0';
80b73c
+        pos = 0;
80b73c
+
80b73c
+        debug(DEBUG, "entry");
80b73c
+
80b73c
+        /*
80b73c
+         * find the pci root domain and port; they basically look like:
80b73c
+         * pci0000:00/
80b73c
+         *    ^d   ^p
80b73c
+         */
80b73c
+        rc = sscanf(devpart, "../../devices/pci%hx:%hhx/%n", &root_domain, &root_bus, &pos;;
80b73c
+        /*
80b73c
+         * If we can't find that, it's not a PCI device.
80b73c
+         */
80b73c
+        if (rc != 2)
80b73c
+                return 0;
80b73c
+        devpart += pos;
80b73c
+
80b73c
+        dev->pci_root.pci_domain = root_domain;
80b73c
+        dev->pci_root.pci_bus = root_bus;
80b73c
+
80b73c
+        rc = parse_acpi_hid_uid(dev, "devices/pci%04hx:%02hhx",
80b73c
+                                root_domain, root_bus);
80b73c
+        if (rc < 0)
80b73c
+                return -1;
80b73c
+
80b73c
+        errno = 0;
80b73c
+        return devpart - current;
80b73c
+}
80b73c
+
80b73c
+static ssize_t
80b73c
+dp_create_pci_root(struct device *dev UNUSED,
80b73c
+                   uint8_t *buf, ssize_t size, ssize_t off)
80b73c
+{
80b73c
+        debug(DEBUG, "entry buf:%p size:%zd off:%zd", buf, size, off);
80b73c
+        debug(DEBUG, "returning 0");
80b73c
+#if 0
80b73c
+        if (dev->acpi_root.acpi_uid_str) {
80b73c
+                debug(DEBUG, "creating acpi_hid_ex dp hid:0x%08x uid:\"%s\"",
80b73c
+                      dev->acpi_root.acpi_hid,
80b73c
+                      dev->acpi_root.acpi_uid_str);
80b73c
+                new = efidp_make_acpi_hid_ex(buf + off, size ? size - off : 0,
80b73c
+                                            dev->acpi_root.acpi_hid,
80b73c
+                                            0, 0, "",
80b73c
+                                            dev->acpi_root.acpi_uid_str,
80b73c
+                                            "");
80b73c
+                if (new < 0) {
80b73c
+                        efi_error("efidp_make_acpi_hid_ex() failed");
80b73c
+                        return new;
80b73c
+                }
80b73c
+        } else {
80b73c
+                debug(DEBUG, "creating acpi_hid dp hid:0x%08x uid:0x%0"PRIx64,
80b73c
+                      dev->acpi_root.acpi_hid,
80b73c
+                      dev->acpi_root.acpi_uid);
80b73c
+                new = efidp_make_acpi_hid(buf + off, size ? size - off : 0,
80b73c
+                                         dev->acpi_root.acpi_hid,
80b73c
+                                         dev->acpi_root.acpi_uid);
80b73c
+                if (new < 0) {
80b73c
+                        efi_error("efidp_make_acpi_hid() failed");
80b73c
+                        return new;
80b73c
+                }
80b73c
+        }
80b73c
+        off += new;
80b73c
+        sz += new;
80b73c
+
80b73c
+        debug(DEBUG, "returning %zd", sz);
80b73c
+        return sz;
80b73c
+#else
80b73c
+        return 0;
80b73c
+#endif
80b73c
+}
80b73c
+
80b73c
+enum interface_type pci_root_iftypes[] = { pci_root, unknown };
80b73c
+
80b73c
+struct dev_probe HIDDEN pci_root_parser = {
80b73c
+        .name = "pci_root",
80b73c
+        .iftypes = pci_root_iftypes,
80b73c
+        .flags = DEV_PROVIDES_ROOT,
80b73c
+        .parse = parse_pci_root,
80b73c
+        .create = dp_create_pci_root,
80b73c
+};
80b73c
diff --git a/src/linux-pci.c b/src/linux-pci.c
80b73c
index aa3e40c0f7c..0f59d3e840d 100644
80b73c
--- a/src/linux-pci.c
80b73c
+++ b/src/linux-pci.c
80b73c
@@ -147,7 +147,6 @@ enum interface_type pci_iftypes[] = { pci, unknown };
80b73c
 struct dev_probe HIDDEN pci_parser = {
80b73c
         .name = "pci",
80b73c
         .iftypes = pci_iftypes,
80b73c
-        .flags = DEV_PROVIDES_ROOT,
80b73c
         .parse = parse_pci,
80b73c
         .create = dp_create_pci,
80b73c
 };
80b73c
diff --git a/src/linux.c b/src/linux.c
80b73c
index 9f3a22f7025..436fb882a98 100644
80b73c
--- a/src/linux.c
80b73c
+++ b/src/linux.c
80b73c
@@ -235,6 +235,8 @@ static struct dev_probe *dev_probes[] = {
80b73c
          * be found first.
80b73c
          */
80b73c
         &pmem_parser,
80b73c
+        &acpi_root_parser,
80b73c
+        &pci_root_parser,
80b73c
         &pci_parser,
80b73c
         &virtblk_parser,
80b73c
         &sas_parser,
80b73c
@@ -447,7 +449,7 @@ struct device HIDDEN
80b73c
                 }
80b73c
                 debug(DEBUG, "%s matched %s", probe->name, current);
80b73c
 
80b73c
-                if (probe->flags & DEV_PROVIDES_HD)
80b73c
+                if (probe->flags & DEV_PROVIDES_HD || probe->flags & DEV_PROVIDES_ROOT)
80b73c
                         needs_root = false;
80b73c
                 dev->probes[n++] = dev_probes[i];
80b73c
                 current += pos;
80b73c
diff --git a/src/linux.h b/src/linux.h
80b73c
index aa9e3d14a83..7b18bda31c6 100644
80b73c
--- a/src/linux.h
80b73c
+++ b/src/linux.h
80b73c
@@ -95,7 +95,7 @@ struct nvdimm_info {
80b73c
 
80b73c
 enum interface_type {
80b73c
         unknown,
80b73c
-        isa, pci, network,
80b73c
+        isa, acpi_root, pci_root, pci, network,
80b73c
         ata, atapi, scsi, sata, sas,
80b73c
         usb, i1394, fibre, i2o,
80b73c
         md, virtblk,
80b73c
@@ -264,6 +264,8 @@ extern ssize_t parse_scsi_link(const char *current, uint32_t *host,
80b73c
 
80b73c
 /* device support implementations */
80b73c
 extern struct dev_probe pmem_parser;
80b73c
+extern struct dev_probe pci_root_parser;
80b73c
+extern struct dev_probe acpi_root_parser;
80b73c
 extern struct dev_probe pci_parser;
80b73c
 extern struct dev_probe sas_parser;
80b73c
 extern struct dev_probe sata_parser;
80b73c
-- 
80b73c
2.17.1
80b73c