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

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