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

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