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.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-08-21 03:17:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stand.h>
|
2000-10-16 19:58:32 +00:00
|
|
|
#include "libofw.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;
|
|
|
|
char name[256];
|
|
|
|
char type[64];
|
|
|
|
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';
|
|
|
|
if ((handle = OF_finddevice(name)) == -1)
|
1998-08-21 03:17:42 +00:00
|
|
|
break;
|
2002-11-10 19:17:36 +00:00
|
|
|
if (OF_getprop(handle, "device_type", type, sizeof(type)) == -1)
|
|
|
|
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:
|
|
|
|
if (*s != '\0')
|
|
|
|
*path = s;
|
2000-10-16 19:58:32 +00:00
|
|
|
idev = malloc(sizeof(struct ofw_devdesc));
|
2002-11-10 19:17:36 +00:00
|
|
|
strcpy(idev->d_path, name);
|
1998-08-21 03:17:42 +00:00
|
|
|
idev->d_dev = dv;
|
|
|
|
idev->d_type = dv->dv_type;
|
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
|
|
|
{
|
2002-11-10 19:17:36 +00:00
|
|
|
struct ofw_devdesc *ncurr;
|
|
|
|
int rv;
|
2000-11-10 06:39:58 +00:00
|
|
|
|
2002-11-10 19:17:36 +00:00
|
|
|
if ((rv = ofw_parsedev(&ncurr, value, NULL)) != 0)
|
|
|
|
return rv;
|
2000-11-10 06:39:58 +00:00
|
|
|
|
2002-11-10 19:17:36 +00:00
|
|
|
free(ncurr);
|
|
|
|
env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
|
|
|
|
return 0;
|
2000-11-10 06:39:58 +00:00
|
|
|
}
|