/*-1* Copyright (c) 1998 Michael Smith <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <stand.h>2728#include "bootstrap.h"29#include "libofw.h"3031/*32* Point (dev) at an allocated device specifier for the device matching the33* path in (devspec). If it contains an explicit device specification,34* use that. If not, use the default device.35*/36int37ofw_getdev(void **vdev, const char *devspec, const char **path)38{39struct devdesc **dev = (struct devdesc **)vdev;40int rv;4142/*43* If it looks like this is just a path and no device, go with the44* current device.45*/46if (devspec == NULL || strpbrk(devspec, ":@") == NULL) {47rv = devparse(dev, getenv("currdev"), NULL);48if (rv == 0 && path != NULL)49*path = devspec;50return (rv);51}5253/*54* Try to parse the device name off the beginning of the devspec55*/56return (devparse(dev, devspec, path));57}5859/*60* Search the OFW (path) for a node that's of (want_type).61*/62phandle_t63ofw_path_to_handle(const char *ofwpath, const char *want_type, const char **path)64{65const char *p, *s;66char name[256];67char type[64];68phandle_t handle;69int len;7071for (p = s = ofwpath; *s != '\0'; p = s) {72if ((s = strchr(p + 1, '/')) == NULL)73s = strchr(p, '\0');74len = s - ofwpath;75if (len >= sizeof(name))76return ((phandle_t)-1);77bcopy(ofwpath, name, len);78name[len] = '\0';79if ((handle = OF_finddevice(name)) == -1)80continue;81if (OF_getprop(handle, "device_type", type, sizeof(type)) == -1)82continue;83if (strcmp(want_type, type) == 0) {84*path = s;85return (handle);86}87}88return ((phandle_t)-1);89}9091int92ofw_common_parsedev(struct devdesc **dev, const char *devspec, const char **path,93const char *ofwtype)94{95const char *rem_path;96struct ofw_devdesc *idev;9798if (ofw_path_to_handle(devspec, ofwtype, &rem_path) == -1)99return (ENOENT);100idev = malloc(sizeof(struct ofw_devdesc));101if (idev == NULL) {102printf("ofw_parsedev: malloc failed\n");103return (ENOMEM);104};105strlcpy(idev->d_path, devspec, min(rem_path - devspec + 1,106sizeof(idev->d_path)));107*dev = &idev->dd;108if (path != NULL)109*path = rem_path;110return (0);111}112113114