1998-08-21 03:17:42 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-01-04 23:30:47 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
#include <stand.h>
|
2012-05-01 17:16:01 +00:00
|
|
|
|
|
|
|
#include "bootstrap.h"
|
2000-10-16 19:58:32 +00:00
|
|
|
#include "libofw.h"
|
2018-07-08 07:42:58 +00:00
|
|
|
#include "libzfs.h"
|
1998-08-21 03:17:42 +00:00
|
|
|
|
2002-11-10 19:17:36 +00:00
|
|
|
static int ofw_parsedev(struct ofw_devdesc **, const char *, const char **);
|
1998-08-21 03:17:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
int
|
2000-10-16 19:58:32 +00:00
|
|
|
ofw_getdev(void **vdev, const char *devspec, const char **path)
|
1998-08-21 03:17:42 +00:00
|
|
|
{
|
2000-10-16 19:58:32 +00:00
|
|
|
struct ofw_devdesc **dev = (struct ofw_devdesc **)vdev;
|
1998-08-21 03:17:42 +00:00
|
|
|
int rv;
|
2002-11-10 19:17:36 +00:00
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
/*
|
|
|
|
* If it looks like this is just a path and no
|
|
|
|
* device, go with the current device.
|
|
|
|
*/
|
|
|
|
if ((devspec == NULL) ||
|
2003-12-21 12:38:25 +00:00
|
|
|
((strchr(devspec, '@') == NULL) &&
|
|
|
|
(strchr(devspec, ':') == NULL))) {
|
1998-08-21 03:17:42 +00:00
|
|
|
|
2000-10-16 19:58:32 +00:00
|
|
|
if (((rv = ofw_parsedev(dev, getenv("currdev"), NULL)) == 0) &&
|
1998-08-21 03:17:42 +00:00
|
|
|
(path != NULL))
|
|
|
|
*path = devspec;
|
|
|
|
return(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to parse the device name off the beginning of the devspec
|
|
|
|
*/
|
2000-10-16 19:58:32 +00:00
|
|
|
return(ofw_parsedev(dev, devspec, path));
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2000-10-16 19:58:32 +00:00
|
|
|
ofw_parsedev(struct ofw_devdesc **dev, const char *devspec, const char **path)
|
1998-08-21 03:17:42 +00:00
|
|
|
{
|
2002-11-10 19:17:36 +00:00
|
|
|
struct ofw_devdesc *idev;
|
1998-08-21 03:17:42 +00:00
|
|
|
struct devsw *dv;
|
2002-11-10 19:17:36 +00:00
|
|
|
phandle_t handle;
|
|
|
|
const char *p;
|
|
|
|
const char *s;
|
2012-05-01 17:16:01 +00:00
|
|
|
char *ep;
|
2002-11-10 19:17:36 +00:00
|
|
|
char name[256];
|
|
|
|
char type[64];
|
2012-05-12 20:27:33 +00:00
|
|
|
int err;
|
2002-11-10 19:17:36 +00:00
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
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';
|
2012-05-01 17:16:01 +00:00
|
|
|
if ((handle = OF_finddevice(name)) == -1) {
|
|
|
|
bcopy(name, type, len);
|
|
|
|
type[len] = '\0';
|
|
|
|
} else if (OF_getprop(handle, "device_type", type, sizeof(type)) == -1)
|
2002-11-10 19:17:36 +00:00
|
|
|
continue;
|
|
|
|
for (i = 0; (dv = devsw[i]) != NULL; i++) {
|
2002-12-02 01:46:22 +00:00
|
|
|
if (strncmp(dv->dv_name, type, strlen(dv->dv_name)) == 0)
|
2002-11-10 19:17:36 +00:00
|
|
|
goto found;
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-10 19:17:36 +00:00
|
|
|
return(ENOENT);
|
1998-08-21 03:17:42 +00:00
|
|
|
|
2002-11-10 19:17:36 +00:00
|
|
|
found:
|
2012-09-18 15:38:42 +00:00
|
|
|
if (path != NULL)
|
2002-11-10 19:17:36 +00:00
|
|
|
*path = s;
|
2000-10-16 19:58:32 +00:00
|
|
|
idev = malloc(sizeof(struct ofw_devdesc));
|
2005-10-25 12:49:56 +00:00
|
|
|
if (idev == NULL) {
|
|
|
|
printf("ofw_parsedev: malloc failed\n");
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2002-11-10 19:17:36 +00:00
|
|
|
strcpy(idev->d_path, name);
|
2018-03-12 21:39:49 +00:00
|
|
|
idev->dd.d_dev = dv;
|
2018-03-12 21:39:59 +00:00
|
|
|
if (dv->dv_type == DEVT_ZFS) {
|
2012-05-12 20:27:33 +00:00
|
|
|
p = devspec + strlen(dv->dv_name);
|
|
|
|
err = zfs_parsedev((struct zfs_devdesc *)idev, p, path);
|
|
|
|
if (err != 0) {
|
|
|
|
free(idev);
|
|
|
|
return (err);
|
2012-05-01 17:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-22 10:31:01 +00:00
|
|
|
if (dev == NULL) {
|
|
|
|
free(idev);
|
|
|
|
} else {
|
1998-08-21 03:17:42 +00:00
|
|
|
*dev = idev;
|
1998-08-22 10:31:01 +00:00
|
|
|
}
|
1998-08-21 03:17:42 +00:00
|
|
|
return(0);
|
2000-11-10 06:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2003-12-21 12:38:25 +00:00
|
|
|
ofw_setcurrdev(struct env_var *ev, int flags, const void *value)
|
2000-11-10 06:39:58 +00:00
|
|
|
{
|
2021-08-12 05:45:52 +00:00
|
|
|
struct ofw_devdesc *ncurr;
|
|
|
|
int rv;
|
2000-11-10 06:39:58 +00:00
|
|
|
|
2021-08-12 05:45:52 +00:00
|
|
|
if ((rv = ofw_parsedev(&ncurr, value, NULL)) != 0)
|
|
|
|
return (rv);
|
2000-11-10 06:39:58 +00:00
|
|
|
|
2021-08-12 05:45:52 +00:00
|
|
|
free(ncurr);
|
|
|
|
|
|
|
|
return (mount_currdev(ev, flags, value));
|
2000-11-10 06:39:58 +00:00
|
|
|
}
|