Blob Blame History Raw
From fa9336914ef43d1a1dbc3384c8051362eea12cd7 Mon Sep 17 00:00:00 2001
From: Peter Jones <pjones@redhat.com>
Date: Tue, 15 Oct 2019 16:53:27 -0400
Subject: [PATCH 58/63] sysfs parsers: make all the /sys/block link parsers
 work the same way

Apparently I wrote some of these one way and some the other, and the one
special case where everything was "current+sz" instead of some form of
"current += pos; sz += pos; ...; return sz;".

Make them all the same, where possible.

Signed-off-by: Peter Jones <pjones@redhat.com>
---
 src/linux-acpi-root.c    |  65 +++++-----
 src/linux-pci-root.c     |   9 +-
 src/linux-pci.c          |  21 ++--
 src/linux-sata.c         |  56 +++++----
 src/linux-scsi.c         | 248 ++++++++++++++++++++-------------------
 src/linux-soc-root.c     |  19 +--
 src/linux-virtblk.c      |  15 ++-
 src/linux-virtual-root.c |   2 +-
 8 files changed, 227 insertions(+), 208 deletions(-)

diff --git a/src/linux-acpi-root.c b/src/linux-acpi-root.c
index 57a648981b3..6bfc1ad9a2b 100644
--- a/src/linux-acpi-root.c
+++ b/src/linux-acpi-root.c
@@ -44,14 +44,13 @@ static ssize_t
 parse_acpi_root(struct device *dev, const char *current, const char *root UNUSED)
 {
 	int rc;
-	int pos = -1;
+	int pos0 = -1, pos1 = -1, pos2 = -1;
+	ssize_t sz = 0;
 	uint16_t pad0;
 	uint8_t pad1;
 	char *acpi_header = NULL;
 	char *colon;
 
-	const char *devpart = current;
-
 	debug("entry");
 
 	/*
@@ -61,23 +60,26 @@ parse_acpi_root(struct device *dev, const char *current, const char *root UNUSED
 	 * This is annoying because "/%04ms%h:%hhx/" won't bind from the right
 	 * side in sscanf.
 	 */
-	rc = sscanf(devpart, "../../devices/platform/%n", &pos);
-	debug("current:'%s' rc:%d pos:%d", devpart, rc, pos);
-	dbgmk("         ", pos);
-	if (rc != 0 || pos < 1)
+	rc = sscanf(current, "../../devices/%nplatform/%n", &pos0, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
+	if (rc != 0 || pos0 == -1 || pos1 == -1)
 		return 0;
-	devpart += pos;
+	current += pos1;
+	sz += pos1;
 
+	debug("searching for an ACPI string like A0000:00 or ACPI0000:00");
+	pos0 = 0;
 	/*
 	 * If it's too short to be A0000:00, it's not an ACPI string
 	 */
-	if (strlen(devpart) < 8)
+	if (strlen(current) < 8)
 		return 0;
 
-	colon = strchr(devpart, ':');
+	colon = strchr(current, ':');
 	if (!colon)
 		return 0;
-	pos = colon - devpart;
+	pos1 = colon - current;
 
 	/*
 	 * If colon doesn't point at something between one of these:
@@ -85,40 +87,44 @@ parse_acpi_root(struct device *dev, const char *current, const char *root UNUSED
 	 *	^ 5	    ^ 8
 	 * Then it's not an ACPI string.
 	 */
-	if (pos < 5 || pos > 8)
+	if (pos1 < 5 || pos1 > 8)
 		return 0;
 
-	dev->acpi_root.acpi_hid_str = strndup(devpart, pos + 1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
+
+	dev->acpi_root.acpi_hid_str = strndup(current, pos1 + 1);
 	if (!dev->acpi_root.acpi_hid_str) {
 		efi_error("Could not allocate memory");
 		return -1;
 	}
-	dev->acpi_root.acpi_hid_str[pos] = 0;
+	dev->acpi_root.acpi_hid_str[pos1] = 0;
 	debug("acpi_hid_str:'%s'", dev->acpi_root.acpi_hid_str);
 
-	pos -= 4;
-	debug("current:'%s' rc:%d pos:%d", devpart, rc, pos);
-	dbgmk("         ", pos);
-	acpi_header = strndupa(devpart, pos);
+	pos2 = pos1 - 4;
+	debug("current:'%s' rc:%d pos0:%d pos1:%d", current, rc, pos0, pos2);
+	dbgmk("         ", pos0, pos2);
+	acpi_header = strndupa(current, pos2);
 	if (!acpi_header)
 		return 0;
-	acpi_header[pos] = 0;
-	debug("current:'%s' acpi_header:'%s'", devpart, acpi_header);
-	devpart += pos;
+	acpi_header[pos2] = 0;
+	debug("current:'%s' acpi_header:'%s'", current, acpi_header);
+	current += pos1;
+	sz += pos1;
 
 	/*
 	 * If we can't find these numbers, it's not an ACPI string
 	 */
-	rc = sscanf(devpart, "%hx:%hhx/%n", &pad0, &pad1, &pos);
+	rc = sscanf(current, "%n%hx:%hhx/%n", &pos0, &pad0, &pad1, &pos1);
 	if (rc != 2) {
-		efi_error("Could not parse ACPI path \"%s\"", devpart);
+		efi_error("Could not parse ACPI path \"%s\"", current);
 		return 0;
 	}
-	debug("current:'%s' parsed:%04hx:%02hhx pos:%d rc:%d",
-	      devpart, pad0, pad1, pos, rc);
-	dbgmk("         ", pos);
-
-	devpart += pos;
+	debug("current:'%s' parsed:%04hx:%02hhx rc:%d pos0:%d pos1:%d",
+	      current, pad0, pad1, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
+	current += pos1;
+	sz += pos1;
 
 	rc = parse_acpi_hid_uid(dev, "devices/platform/%s%04hX:%02hhX",
 				acpi_header, pad0, pad1);
@@ -139,8 +145,7 @@ parse_acpi_root(struct device *dev, const char *current, const char *root UNUSED
 	      dev->acpi_root.acpi_uid_str,
 	      dev->acpi_root.acpi_cid_str);
 
-	size_t sz = devpart - current;
-	debug("current:'%s' sz:%zd", devpart, sz);
+	debug("current:'%s' sz:%zd", current, sz);
 	return sz;
 }
 
diff --git a/src/linux-pci-root.c b/src/linux-pci-root.c
index c7e279118f5..8f7cfe293bf 100644
--- a/src/linux-pci-root.c
+++ b/src/linux-pci-root.c
@@ -47,7 +47,6 @@ parse_pci_root(struct device *dev, const char *current, const char *root UNUSED)
 	int pos0 = -1, pos1 = -1;
 	uint16_t root_domain;
 	uint8_t root_bus;
-	const char *devpart = current;
 
 	debug("entry");
 
@@ -56,8 +55,8 @@ parse_pci_root(struct device *dev, const char *current, const char *root UNUSED)
 	 * pci0000:00/
 	 *    ^d   ^p
 	 */
-	rc = sscanf(devpart, "%n../../devices/pci%hx:%hhx/%n", &pos0, &root_domain, &root_bus, &pos1);
-	debug("current:'%s' rc:%d pos0:%d pos1:%d", devpart, rc, pos0, pos1);
+	rc = sscanf(current, "%n../../devices/pci%hx:%hhx/%n", &pos0, &root_domain, &root_bus, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d", current, rc, pos0, pos1);
 	dbgmk("         ", pos0, pos1);
 
 	/*
@@ -65,7 +64,7 @@ parse_pci_root(struct device *dev, const char *current, const char *root UNUSED)
 	 */
 	if (rc != 2)
 	        return 0;
-	devpart += pos1;
+	current += pos1;
 
 	dev->pci_root.pci_domain = root_domain;
 	dev->pci_root.pci_bus = root_bus;
@@ -76,7 +75,7 @@ parse_pci_root(struct device *dev, const char *current, const char *root UNUSED)
 	        return -1;
 
 	errno = 0;
-	debug("current:'%s' sz:%d\n", devpart, pos1);
+	debug("current:'%s' sz:%d\n", current, pos1);
 	return pos1;
 }
 
diff --git a/src/linux-pci.c b/src/linux-pci.c
index a3a0dc28afd..ca6f693cd8a 100644
--- a/src/linux-pci.c
+++ b/src/linux-pci.c
@@ -46,8 +46,7 @@ static ssize_t
 parse_pci(struct device *dev, const char *current, const char *root)
 {
 	int rc;
-	int pos0 = -1, pos1 = -1;
-	const char *devpart = current;
+	ssize_t sz = 0;
 
 	debug("entry");
 
@@ -55,22 +54,24 @@ parse_pci(struct device *dev, const char *current, const char *root)
 	 * 0000:00:01.0/0000:01:00.0/
 	 *              ^d   ^b ^d ^f (of the last one in the series)
 	 */
-	while (*devpart) {
+	while (*current) {
 	        uint16_t domain;
 	        uint8_t bus, device, function;
 	        struct pci_dev_info *pci_dev;
 	        unsigned int i = dev->n_pci_devs;
 	        struct stat statbuf;
+		int pos0 = -1, pos1 = -1;
 
 	        pos0 = pos1 = -1;
 	        debug("searching for 0000:00:00.0/");
-	        rc = sscanf(devpart, "%n%hx:%hhx:%hhx.%hhx/%n",
+	        rc = sscanf(current, "%n%hx:%hhx:%hhx.%hhx/%n",
 	                    &pos0, &domain, &bus, &device, &function, &pos1);
-	        debug("current:'%s' rc:%d pos0:%d pos1:%d", devpart, rc, pos0, pos1);
+	        debug("current:'%s' rc:%d pos0:%d pos1:%d", current, rc, pos0, pos1);
 		dbgmk("         ", pos0, pos1);
 	        if (rc != 4)
 	                break;
-	        devpart += pos1;
+	        current += pos1;
+		sz += pos1;
 
 	        debug("found pci domain %04hx:%02hhx:%02hhx.%02hhx",
 	              domain, bus, device, function);
@@ -87,13 +88,13 @@ parse_pci(struct device *dev, const char *current, const char *root)
 	        dev->pci_dev[i].pci_bus = bus;
 	        dev->pci_dev[i].pci_device = device;
 	        dev->pci_dev[i].pci_function = function;
-	        char *tmp = strndup(root, devpart-root+1);
+	        char *tmp = strndup(root, current-root+1);
 	        char *linkbuf = NULL;
 	        if (!tmp) {
 	                efi_error("could not allocate memory");
 	                return -1;
 	        }
-	        tmp[devpart - root] = '\0';
+	        tmp[current - root] = '\0';
 	        rc = sysfs_stat(&statbuf, "class/block/%s/driver", tmp);
 	        if (rc < 0 && errno == ENOENT) {
 	                debug("No driver link for /sys/class/block/%s", tmp);
@@ -114,8 +115,8 @@ parse_pci(struct device *dev, const char *current, const char *root)
 	        dev->n_pci_devs += 1;
 	}
 
-	debug("current:'%s' sz:%zd\n", devpart, devpart - current);
-	return devpart - current;
+	debug("current:'%s' sz:%zd\n", current, sz);
+	return sz;
 }
 
 static ssize_t
diff --git a/src/linux-sata.c b/src/linux-sata.c
index 4f4e983568e..b49cf99dcb7 100644
--- a/src/linux-sata.c
+++ b/src/linux-sata.c
@@ -138,15 +138,15 @@ sysfs_sata_get_port_info(uint32_t print_id, struct device *dev)
 }
 
 static ssize_t
-parse_sata(struct device *dev, const char *devlink, const char *root UNUSED)
+parse_sata(struct device *dev, const char *current, const char *root UNUSED)
 {
-	const char *current = devlink;
 	uint32_t print_id;
 	uint32_t scsi_bus, tosser0;
 	uint32_t scsi_device, tosser1;
 	uint32_t scsi_target, tosser2;
 	uint64_t scsi_lun, tosser3;
-	int pos = 0;
+	int pos0 = -1, pos1 = -1;
+	size_t sz = 0;
 	int rc;
 
 	debug("entry");
@@ -160,9 +160,9 @@ parse_sata(struct device *dev, const char *devlink, const char *root UNUSED)
 	 *    ^dev  ^host   x y z
 	 */
 	debug("searching for ata1/");
-	rc = sscanf(current, "ata%"PRIu32"/%n", &print_id, &pos);
-	debug("current:'%s' rc:%d pos:%d\n", current, rc, pos);
-	dbgmk("         ", pos);
+	rc = sscanf(current, "%nata%"PRIu32"/%n", &pos0, &print_id, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
 	/*
 	 * If we don't find this one, it isn't an ata device, so return 0 not
 	 * error.  Later errors mean it is an ata device, but we can't parse
@@ -170,36 +170,40 @@ parse_sata(struct device *dev, const char *devlink, const char *root UNUSED)
 	 */
 	if (rc != 1)
 	        return 0;
-	current += pos;
-	pos = 0;
+	current += pos1;
+	sz += pos1;
+	pos0 = pos1 = -1;
 
 	debug("searching for host0/");
-	rc = sscanf(current, "host%"PRIu32"/%n", &scsi_bus, &pos);
-	debug("current:'%s' rc:%d pos:%d\n", current, rc, pos);
-	dbgmk("         ", pos);
+	rc = sscanf(current, "%nhost%"PRIu32"/%n", &pos0, &scsi_bus, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
 	if (rc != 1)
 	        return -1;
-	current += pos;
-	pos = 0;
+	current += pos1;
+	sz += pos1;
+	pos0 = pos1 = -1;
 
 	debug("searching for target0:0:0:0/");
-	rc = sscanf(current, "target%"PRIu32":%"PRIu32":%"PRIu64"/%n",
-	            &scsi_device, &scsi_target, &scsi_lun, &pos);
-	debug("current:'%s' rc:%d pos:%d\n", current, rc, pos);
-	dbgmk("         ", pos);
+	rc = sscanf(current, "%ntarget%"PRIu32":%"PRIu32":%"PRIu64"/%n",
+	            &pos0, &scsi_device, &scsi_target, &scsi_lun, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
 	if (rc != 3)
 	        return -1;
-	current += pos;
-	pos = 0;
+	current += pos1;
+	sz += pos1;
+	pos0 = pos1 = -1;
 
 	debug("searching for 0:0:0:0/");
-	rc = sscanf(current, "%"PRIu32":%"PRIu32":%"PRIu32":%"PRIu64"/%n",
-	            &tosser0, &tosser1, &tosser2, &tosser3, &pos);
-	debug("current:'%s' rc:%d pos:%d\n", current, rc, pos);
-	dbgmk("         ", pos);
+	rc = sscanf(current, "%n%"PRIu32":%"PRIu32":%"PRIu32":%"PRIu64"/%n",
+	            &pos0, &tosser0, &tosser1, &tosser2, &tosser3, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
 	if (rc != 4)
 	        return -1;
-	current += pos;
+	current += pos1;
+	sz += pos1;
 
 	rc = sysfs_sata_get_port_info(print_id, dev);
 	if (rc < 0)
@@ -213,8 +217,8 @@ parse_sata(struct device *dev, const char *devlink, const char *root UNUSED)
 	if (dev->interface_type == unknown)
 	        dev->interface_type = sata;
 
-	debug("current:'%s' sz:%zd\n", current, current - devlink);
-	return current - devlink;
+	debug("current:'%s' sz:%zd\n", current, sz);
+	return sz;
 }
 
 static ssize_t
diff --git a/src/linux-scsi.c b/src/linux-scsi.c
index 6b92908d6bc..73db53b3ed6 100644
--- a/src/linux-scsi.c
+++ b/src/linux-scsi.c
@@ -24,6 +24,7 @@
 #include <fcntl.h>
 #include <inttypes.h>
 #include <stdint.h>
+#include <sys/param.h>
 #include <unistd.h>
 
 #include "efiboot.h"
@@ -44,7 +45,7 @@ parse_scsi_link(const char *current, uint32_t *scsi_host,
 {
 	int rc;
 	ssize_t sz = 0;
-	int pos0 = 0, pos1 = 0;
+	int pos0 = -1, pos1 = -1, pos2 = -1;
 
 	debug("entry");
 	/*
@@ -99,14 +100,14 @@ parse_scsi_link(const char *current, uint32_t *scsi_host,
 	 * or host4/port-4:0:0
 	 */
 	debug("searching for host4/");
-	rc = sscanf(current, "host%d/%n", scsi_host, &pos0);
-	debug("current:'%s' rc:%d pos0:%d\n", current, rc, pos0);
-	dbgmk("         ", pos0);
+	rc = sscanf(current, "%nhost%d/%n", scsi_host, &pos0, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
 	if (rc != 1)
 	        return -1;
-	current += pos0;
-	sz += pos0;
-	pos0 = 0;
+	current += pos1;
+	sz += pos1;
+	pos0 = pos1 = -1;
 
 	/*
 	 * We might have this next:
@@ -117,96 +118,100 @@ parse_scsi_link(const char *current, uint32_t *scsi_host,
 	 * port-2:0:2/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda/sda1
 	 */
 	debug("searching for port-4:0 or port-4:0:0");
-	rc = sscanf(current, "port-%d:%d%n:%d%n", &tosser0,
-	            &tosser1, &pos0, &tosser2, &pos1);
-	debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
-	dbgmk("         ", pos0, pos1);
-	if (rc == 2 || rc == 3) {
-		current += pos0;
-	        sz += pos0;
-	        pos0 = 0;
-	        if (local_port_id && rc == 2)
-	                *local_port_id = tosser1;
-	        if (remote_port_id && rc == 3)
-	                *remote_port_id = tosser2;
-
-	        if (current[0] == '/') {
-			current += 1;
-	                sz += 1;
-		}
-
-	        /*
-	         * We might have this next:
-	         * expander-2:0/port-2:0:2/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda/sda1
-	         *                       ^ port id
-	         *                     ^ scsi target id
-	         *                   ^ host number
-	         *          ^ host number
-	         * We don't actually care about either number in expander-.../,
-	         * because they're replicated in all the other places.  We just need
-	         * to get past it.
-	         */
-	        debug("searching for expander-4:0/");
-	        rc = sscanf(current, "expander-%d:%d/%n", &tosser0, &tosser1, &pos0);
-	        debug("current:'%s' rc:%d pos0:%d\n", current, rc, pos0);
-		dbgmk("         ", pos0);
-	        if (rc == 2) {
-	                if (!remote_target_id) {
-	                        efi_error("Device is PHY is a remote target, but remote_target_id is NULL");
-	                        return -1;
-	                }
-	                *remote_target_id = tosser1;
-			current += pos0;
-	                sz += pos0;
-	                pos0 = 0;
-
-	                /*
-	                 * if we have that, we should have a 3-part port next
-	                 */
-	                debug("searching for port-2:0:2/");
-	                rc = sscanf(current, "port-%d:%d:%d/%n", &tosser0, &tosser1, &tosser2, &pos0);
-	                debug("current:'%s' rc:%d pos0:%d\n", current, rc, pos0);
-			dbgmk("         ", pos0);
-	                if (rc != 3) {
-	                        efi_error("Couldn't parse port expander port string");
-	                        return -1;
-	                }
-			current += pos0;
-	                sz += pos0;
-	        }
-	        pos0 = 0;
-
-	        /* next:
-	         *    /end_device-4:0
-	         * or /end_device-4:0:0
-	         * awesomely these are the exact same fields that go into port-blah,
-	         * but we don't care for now about any of them anyway.
-	         */
-	        debug("searching for end_device-4:0/ or end_device-4:0:0/");
-	        rc = sscanf(current, "end_device-%d:%d%n", &tosser0, &tosser1, &pos0);
-	        debug("current:'%s' rc:%d pos0:%d\n", current, rc, pos0);
-	        if (rc != 2)
-	                return -1;
+	rc = sscanf(current, "%nport-%d:%d%n:%d%n",
+		    &pos0, &tosser0, &tosser1, &pos1, &tosser2, &pos2);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d pos2:%d\n", current, rc, pos0, pos1, pos2);
+	dbgmk("         ", pos0, MAX(pos1, pos2));
+	if (rc == 3) {
+		if (remote_port_id)
+			*remote_port_id = tosser2;
+		pos1 = pos2;
+	} else if (rc == 2) {
+		if (local_port_id)
+			*local_port_id = tosser1;
+	} else if (rc != 0) {
+		return -1;
+	} else {
+		pos1 = 0;
+	}
+	current += pos1;
+	sz += pos1;
 
-	        pos1 = 0;
-	        rc = sscanf(current + pos0, ":%d%n", &tosser2, &pos1);
-	        if (rc != 0 && rc != 1)
-	                return -1;
-		dbgmk("         ", pos0, pos0+pos1);
-	        if (remote_port_id && rc == 1)
-	                *remote_port_id = tosser2;
-	        if (local_port_id && rc == 0)
-	                *local_port_id = tosser1;
-		current += pos0 + pos1;
-	        sz += pos0 + pos1;
-	        pos0 = pos1 = 0;
+	if (current[0] == '/') {
+		current += 1;
+		sz += 1;
+	}
+	pos0 = pos1 = pos2 = -1;
+
+        /*
+         * We might have this next:
+         * expander-2:0/port-2:0:2/end_device-2:0:2/target2:0:0/2:0:0:0/block/sda/sda1
+         *                       ^ port id
+         *                     ^ scsi target id
+         *                   ^ host number
+         *          ^ host number
+         * We don't actually care about either number in expander-.../,
+         * because they're replicated in all the other places.  We just need
+         * to get past it.
+         */
+        debug("searching for expander-4:0/");
+        rc = sscanf(current, "%nexpander-%d:%d/%n", &pos0, &tosser0, &tosser1, &pos1);
+        debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
+        if (rc == 2) {
+                if (!remote_target_id) {
+                        efi_error("Device is PHY is a remote target, but remote_target_id is NULL");
+                        return -1;
+                }
+                *remote_target_id = tosser1;
+		current += pos1;
+                sz += pos1;
+		pos0 = pos1 = -1;
+
+                /*
+                 * if we have that, we should have a 3-part port next
+                 */
+                debug("searching for port-2:0:2/");
+                rc = sscanf(current, "%nport-%d:%d:%d/%n", &pos0, &tosser0, &tosser1, &tosser2, &pos1);
+                debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+		dbgmk("         ", pos0, pos1);
+                if (rc != 3) {
+                        efi_error("Couldn't parse port expander port string");
+                        return -1;
+                }
+		current += pos1;
+                sz += pos1;
+        }
+	pos0 = pos1 = -1;
+
+        /* next:
+         *    /end_device-4:0
+         * or /end_device-4:0:0
+         * awesomely these are the exact same fields that go into port-blah,
+         * but we don't care for now about any of them anyway.
+         */
+        debug("searching for end_device-4:0/ or end_device-4:0:0/");
+        rc = sscanf(current, "%nend_device-%d:%d%n:%d%n",
+		    &pos0, &tosser0, &tosser1, &pos1, &tosser2, &pos2);
+        debug("current:'%s' rc:%d pos0:%d\n", current, rc, pos0);
+	dbgmk("         ", pos0, MAX(pos1, pos2));
+	if (rc == 3) {
+		if (remote_port_id)
+			*remote_port_id = tosser2;
+		pos1 = pos2;
+	} else if (rc == 2) {
+		if (local_port_id)
+			*local_port_id = tosser1;
+	} else {
+		pos1 = 0;
+	}
+	current += pos1;
+	sz += pos1;
+	pos0 = pos1 = pos2 = -1;
 
-	        if (current[0] == '/') {
-			current += sz;
-	                sz += 1;
-		}
-	} else if (rc != 0) {
-	        return -1;
+        if (current[0] == '/') {
+		current += sz;
+                sz += 1;
 	}
 
 	/* now:
@@ -214,28 +219,28 @@ parse_scsi_link(const char *current, uint32_t *scsi_host,
 	 */
 	uint64_t tosser3;
 	debug("searching for target4:0:0/");
-	rc = sscanf(current, "target%d:%d:%"PRIu64"/%n", &tosser0, &tosser1,
-	            &tosser3, &pos0);
-	debug("current:'%s' rc:%d pos0:%d\n", current, rc, pos0);
-	dbgmk("         ", pos0);
+	rc = sscanf(current, "%ntarget%d:%d:%"PRIu64"/%n",
+		    &pos0, &tosser0, &tosser1, &tosser3, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
 	if (rc != 3)
 	        return -1;
-	current += pos0;
-	sz += pos0;
-	pos0 = 0;
+	current += pos1;
+	sz += pos1;
+	pos0 = pos1 = -1;
 
 	/* now:
 	 * %d:%d:%d:%llu/
 	 */
 	debug("searching for 4:0:0:0/");
-	rc = sscanf(current, "%d:%d:%d:%"PRIu64"/%n",
-	            scsi_bus, scsi_device, scsi_target, scsi_lun, &pos0);
-	debug("current:'%s' rc:%d pos0:%d\n", current, rc, pos0);
-	dbgmk("         ", pos0);
+	rc = sscanf(current, "%n%d:%d:%d:%"PRIu64"/%n",
+	            &pos0, scsi_bus, scsi_device, scsi_target, scsi_lun, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
 	if (rc != 4)
 	        return -1;
-	current += pos0;
-	sz += pos0;
+	current += pos1;
+	sz += pos1;
 
 	debug("current:'%s' sz:%zd\n", current, sz);
 	return sz;
@@ -246,31 +251,32 @@ parse_scsi(struct device *dev, const char *current, const char *root UNUSED)
 {
 	uint32_t scsi_host, scsi_bus, scsi_device, scsi_target;
 	uint64_t scsi_lun;
-	ssize_t sz;
-	int pos;
+	ssize_t sz = 0;
+	int pos0, pos1;
 	int rc;
 
 	debug("entry");
 
-	debug("searching for ../../../0:0:0:0");
-	rc = sscanf(dev->device, "../../../%d:%d:%d:%"PRIu64"%n",
+	debug("searching device for ../../../0:0:0:0");
+	pos0 = pos1 = -1;
+	rc = sscanf(dev->device, "../../../%n%d:%d:%d:%"PRIu64"%n",
+		    &pos0,
 	            &dev->scsi_info.scsi_bus,
 	            &dev->scsi_info.scsi_device,
 	            &dev->scsi_info.scsi_target,
 	            &dev->scsi_info.scsi_lun,
-	            &pos);
-	debug("current:'%s' rc:%d pos:%d\n", dev->device, rc, pos);
-	dbgmk("         ", pos);
+	            &pos1);
+	debug("device:'%s' rc:%d pos0:%d pos1:%d\n", dev->device, rc, pos0, pos1);
+	dbgmk("        ", pos0, pos1);
 	if (rc != 4)
 	        return 0;
 
-	sz = parse_scsi_link(current, &scsi_host,
-	                      &scsi_bus, &scsi_device,
-	                      &scsi_target, &scsi_lun,
-	                      NULL, NULL, NULL);
-	if (sz < 0)
+	pos0 = parse_scsi_link(current, &scsi_host, &scsi_bus, &scsi_device,
+			       &scsi_target, &scsi_lun, NULL, NULL, NULL);
+	if (pos0 < 0)
 	        return 0;
-	current += sz;
+	current += pos0;
+	sz += pos0;
 
 	/*
 	 * SCSI disks can have up to 16 partitions, or 4 bits worth
diff --git a/src/linux-soc-root.c b/src/linux-soc-root.c
index 9c9e9573dcd..bad37c9f874 100644
--- a/src/linux-soc-root.c
+++ b/src/linux-soc-root.c
@@ -41,20 +41,21 @@ static ssize_t
 parse_soc_root(struct device *dev UNUSED, const char *current, const char *root UNUSED)
 {
 	int rc;
-	int pos = 0;
-	const char *devpart = current;
+	int pos0 = -1, pos1 = -1;
+	ssize_t sz = 0;
 
 	debug("entry");
 
-	rc = sscanf(devpart, "../../devices/platform/soc/%*[^/]/%n", &pos);
-	if (rc != 0)
+	rc = sscanf(current, "../../devices/%nplatform/soc/%*[^/]/%n", &pos0, &pos1);
+	if (rc != 0 || pos0 == -1 || pos1 == -1)
 	        return 0;
-	debug("current:'%s' rc:%d pos:%d", current, rc, pos);
-	dbgmk("         ", pos);
-	devpart += pos;
+	debug("current:'%s' rc:%d pos0:%d pos1:%d", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
+	current += pos1;
+	sz += pos1;
 
-	debug("current:'%s' sz:%d\n", devpart, pos);
-	return pos;
+	debug("current:'%s' sz:%zd\n", current, sz);
+	return sz;
 }
 
 enum interface_type soc_root_iftypes[] = { soc_root, unknown };
diff --git a/src/linux-virtblk.c b/src/linux-virtblk.c
index df14673dac8..a3366c9c677 100644
--- a/src/linux-virtblk.c
+++ b/src/linux-virtblk.c
@@ -48,15 +48,16 @@ static ssize_t
 parse_virtblk(struct device *dev, const char *current, const char *root UNUSED)
 {
 	uint32_t tosser;
-	int pos = -1;
+	int pos0 = -1, pos1 = -1;
+	ssize_t sz = 0;
 	int rc;
 
 	debug("entry");
 
 	debug("searching for virtio0/");
-	rc = sscanf(current, "virtio%x/%n", &tosser, &pos);
-	debug("current:'%s' rc:%d pos:%d\n", current, rc, pos);
-	dbgmk("         ", pos);
+	rc = sscanf(current, "%nvirtio%x/%n", &pos0, &tosser, &pos1);
+	debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
+	dbgmk("         ", pos0, pos1);
 	/*
 	 * If we couldn't find virtioX/ then it isn't a virtio device.
 	 */
@@ -64,9 +65,11 @@ parse_virtblk(struct device *dev, const char *current, const char *root UNUSED)
 	        return 0;
 
 	dev->interface_type = virtblk;
+	current += pos1;
+	sz += pos1;
 
-	debug("current:'%s' sz:%d\n", current, pos);
-	return pos;
+	debug("current:'%s' sz:%zd\n", current, sz);
+	return sz;
 }
 
 enum interface_type virtblk_iftypes[] = { virtblk, unknown };
diff --git a/src/linux-virtual-root.c b/src/linux-virtual-root.c
index b2d36b4095f..75fbbfc1de6 100644
--- a/src/linux-virtual-root.c
+++ b/src/linux-virtual-root.c
@@ -61,7 +61,7 @@ parse_virtual_root(struct device *dev UNUSED, const char *current, const char *r
 		pos0 = pos1 = -1;
 		rc = sscanf(current, subdirs[i].fmt, &pos0, &pos1);
 		debug("current:'%s' rc:%d pos0:%d pos1:%d\n", current, rc, pos0, pos1);
-		dbgmk("        ", pos0, pos1);
+		dbgmk("         ", pos0, pos1);
 		if (rc == 1) {
 			sz += pos1;
 			current += pos1;
-- 
2.26.2