git: 6cb83949f7dd - stable/13 - stand/ofw: Use devparse

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 24 Jan 2023 22:13:31 UTC
The branch stable/13 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=6cb83949f7dd65d12e0baa5ee657ec8ebfc3233f

commit 6cb83949f7dd65d12e0baa5ee657ec8ebfc3233f
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-11-30 22:10:18 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:40 +0000

    stand/ofw: Use devparse
    
    Retire the custom parsedev routine and use the standard devparse.
    
    Sponsored by:           Netflix
    Differential Revision:  https://reviews.freebsd.org/D37559
    
    (cherry picked from commit 854001759e59172a2760d3a77f37a287f92053b3)
---
 stand/libofw/devicename.c | 93 ++++++-----------------------------------------
 1 file changed, 12 insertions(+), 81 deletions(-)

diff --git a/stand/libofw/devicename.c b/stand/libofw/devicename.c
index 6ed2343ee7bd..7d488608c3f8 100644
--- a/stand/libofw/devicename.c
+++ b/stand/libofw/devicename.c
@@ -32,9 +32,7 @@ __FBSDID("$FreeBSD$");
 #include "bootstrap.h"
 #include "libofw.h"
 
-static int ofw_parsedev(struct ofw_devdesc **, const char *, const char **);
-
-/* 
+/*
  * Point (dev) at an allocated device specifier for the device matching the
  * path in (devspec). If it contains an explicit device specification,
  * use that.  If not, use the default device.
@@ -42,24 +40,24 @@ static int ofw_parsedev(struct ofw_devdesc **, const char *, const char **);
 int
 ofw_getdev(void **vdev, const char *devspec, const char **path)
 {
-	struct ofw_devdesc **dev = (struct ofw_devdesc **)vdev;
+	struct devdesc **dev = (struct devdesc **)vdev;
 	int rv;
 
 	/*
-	 * If it looks like this is just a path and no device, go with the current
-	 * device.
+	 * If it looks like this is just a path and no device, go with the
+	 * current device.
 	 */
 	if (devspec == NULL || strpbrk(devspec, ":@") == NULL) {
-		if (((rv = ofw_parsedev(dev, getenv("currdev"), NULL)) == 0) &&
-		    (path != NULL))
+		rv = devparse(dev, getenv("currdev"), NULL);
+		if (rv == 0  && path != NULL)
 			*path = devspec;
-		return(rv);
+		return (rv);
 	}
-    
+
 	/*
 	 * Try to parse the device name off the beginning of the devspec
 	 */
-	return(ofw_parsedev(dev, devspec, path));
+	return (devparse(dev, devspec, path));
 }
 
 /*
@@ -94,80 +92,13 @@ ofw_path_to_handle(const char *ofwpath, const char *want_type, const char **path
 	return ((phandle_t)-1);
 }
 
-/*
- * Point (dev) at an allocated device specifier matching the string version
- * at the beginning of (devspec).  Return a pointer to the remaining
- * text in (path).
- */
-static int
-ofw_parsedev(struct ofw_devdesc **dev, const char *devspec, const char **path)
-{
-    struct ofw_devdesc	*idev;
-    struct devsw	*dv;
-    phandle_t		handle;
-    const char		*p;
-    const char		*s;
-    char		*ep;
-    char		name[256];
-    char		type[64];
-    int			err;
-    int			len;
-    int			i;
-
-    /* XXX next step: use devparse -- don't forget to hack ofw_disk like you did ofw_net */
-    for (p = s = devspec; *s != '\0'; p = s) {
-	if ((s = strchr(p + 1, '/')) == NULL)
-	    s = strchr(p, '\0');
-	len = s - devspec;
-	bcopy(devspec, name, len);
-	name[len] = '\0';
-	if ((handle = OF_finddevice(name)) == -1) {
-	    bcopy(name, type, len);
-	    type[len] = '\0';
-	} else if (OF_getprop(handle, "device_type", type, sizeof(type)) == -1)
-	    continue;
-	for (i = 0; (dv = devsw[i]) != NULL; i++) {
-	    if (strncmp(dv->dv_name, type, strlen(dv->dv_name)) == 0)
-		goto found;
-	}
-    }
-    return(ENOENT);
-
-found:
-    if (path != NULL)
-	*path = s;
-    idev = malloc(sizeof(struct ofw_devdesc));
-    if (idev == NULL) {
-	printf("ofw_parsedev: malloc failed\n");
-	return ENOMEM;
-    }
-    idev->dd.d_dev = dv;
-    if (dv->dv_parsedev != NULL) {
-	p = devspec + strlen(dv->dv_name);
-	free(idev);
-	err = dv->dv_parsedev((struct devdesc **)&idev, devspec, path);
-	if (err != 0) {
-	    return (err);
-	}
-    } else {
-	strcpy(idev->d_path, name);
-    }
-
-    if (dev == NULL) {
-	free(idev);
-    } else {
-	*dev = idev;
-    }
-    return(0);
-}
-
 int
 ofw_setcurrdev(struct env_var *ev, int flags, const void *value)
 {
-	struct ofw_devdesc	*ncurr;
-	int			rv;
+	struct devdesc	*ncurr;
+	int		rv;
 
-	if ((rv = ofw_parsedev(&ncurr, value, NULL)) != 0)
+	if ((rv = devparse(&ncurr, value, NULL)) != 0)
 		return (rv);
 
 	free(ncurr);