devicename.c revision 67228
190926Snectar/*-
255682Smarkm * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
355682Smarkm * All rights reserved.
455682Smarkm *
555682Smarkm * Redistribution and use in source and binary forms, with or without
655682Smarkm * modification, are permitted provided that the following conditions
755682Smarkm * are met:
855682Smarkm * 1. Redistributions of source code must retain the above copyright
955682Smarkm *    notice, this list of conditions and the following disclaimer.
1055682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer in the
1255682Smarkm *    documentation and/or other materials provided with the distribution.
1355682Smarkm *
1455682Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1555682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1690926Snectar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1790926Snectar * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1890926Snectar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1990926Snectar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2090926Snectar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2190926Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2290926Snectar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2355682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2455682Smarkm * SUCH DAMAGE.
2555682Smarkm *
2655682Smarkm * $FreeBSD: head/sys/boot/ofw/libofw/devicename.c 67227 2000-10-16 19:58:32Z obrien $
2755682Smarkm */
2855682Smarkm
2955682Smarkm#include <stand.h>
3055682Smarkm#include <sys/disklabel.h>
3155682Smarkm#include "libofw.h"
3255682Smarkm
3372445Sassarstatic int	ofw_parsedev(struct ofw_devdesc **, const char *, const char **);
3472445Sassar
3572445Sassar/*
3672445Sassar * Point (dev) at an allocated device specifier for the device matching the
3755682Smarkm * path in (devspec). If it contains an explicit device specification,
3855682Smarkm * use that.  If not, use the default device.
3955682Smarkm */
4055682Smarkmint
4155682Smarkmofw_getdev(void **vdev, const char *devspec, const char **path)
4255682Smarkm{
4355682Smarkm    struct ofw_devdesc **dev = (struct ofw_devdesc **)vdev;
4455682Smarkm    int				rv;
4555682Smarkm
4655682Smarkm    /*
4755682Smarkm     * If it looks like this is just a path and no
4855682Smarkm     * device, go with the current device.
4955682Smarkm     */
5055682Smarkm    if ((devspec == NULL) ||
5155682Smarkm	(devspec[0] == '/') ||
5255682Smarkm	(strchr(devspec, ':') == NULL)) {
5355682Smarkm
5455682Smarkm	if (((rv = ofw_parsedev(dev, getenv("currdev"), NULL)) == 0) &&
5555682Smarkm	    (path != NULL))
5655682Smarkm		*path = devspec;
5755682Smarkm	return(rv);
5855682Smarkm    }
5955682Smarkm
6055682Smarkm    /*
6155682Smarkm     * Try to parse the device name off the beginning of the devspec
6255682Smarkm     */
6355682Smarkm    return(ofw_parsedev(dev, devspec, path));
64}
65
66/*
67 * Point (dev) at an allocated device specifier matching the string version
68 * at the beginning of (devspec).  Return a pointer to the remaining
69 * text in (path).
70 *
71 * In all cases, the beginning of (devspec) is compared to the names
72 * of known devices in the device switch, and then any following text
73 * is parsed according to the rules applied to the device type.
74 *
75 * For disk-type devices, the syntax is:
76 *
77 * disk<unit>[s<slice>][<partition>]:
78 *
79 */
80static int
81ofw_parsedev(struct ofw_devdesc **dev, const char *devspec, const char **path)
82{
83    struct ofw_devdesc *idev;
84    struct devsw	*dv;
85    int			i, unit, slice, partition, err;
86    char		*cp;
87    const char		*np;
88
89    /* minimum length check */
90    if (strlen(devspec) < 2)
91	return(EINVAL);
92
93    /* look for a device that matches */
94    for (i = 0, dv = NULL; devsw[i] != NULL; i++) {
95	if (!strncmp(devspec, devsw[i]->dv_name, strlen(devsw[i]->dv_name))) {
96	    dv = devsw[i];
97	    break;
98	}
99    }
100
101    if (dv == NULL)
102	return(ENOENT);
103    idev = malloc(sizeof(struct ofw_devdesc));
104    err = 0;
105    np = (devspec + strlen(dv->dv_name));
106
107    switch(dv->dv_type) {
108    case DEVT_NONE:			/* XXX what to do here?  Do we care? */
109	break;
110
111    case DEVT_DISK:
112	unit = -1;
113	slice = -1;
114	partition = -1;
115	if (*np && (*np != ':')) {
116	    unit = strtol(np, &cp, 10);	/* next comes the unit number */
117	    if (cp == np) {
118		err = EUNIT;
119		goto fail;
120	    }
121	    if (*cp == 's') {		/* got a slice number */
122		np = cp + 1;
123		slice = strtol(np, &cp, 10);
124		if (cp == np) {
125		    err = ESLICE;
126		    goto fail;
127		}
128	    }
129	    if (*cp && (*cp != ':')) {
130		partition = *cp - 'a';		/* get a partition number */
131		if ((partition < 0) || (partition >= MAXPARTITIONS)) {
132		    err = EPART;
133		    goto fail;
134		}
135		cp++;
136	    }
137	}
138	if (*cp && (*cp != ':')) {
139	    err = EINVAL;
140	    goto fail;
141	}
142
143	idev->d_kind.ofwdisk.unit = unit;
144	idev->d_kind.ofwdisk.slice = slice;
145	idev->d_kind.ofwdisk.partition = partition;
146	if (path != NULL)
147	    *path = (*cp == 0) ? cp : cp + 1;
148	break;
149
150    case DEVT_NET:
151	unit = 0;
152
153	if (*np && (*np != ':')) {
154	    unit = strtol(np, &cp, 0);	/* get unit number if present */
155	    if (cp == np) {
156		err = EUNIT;
157		goto fail;
158	    }
159	}
160	if (*cp && (*cp != ':')) {
161	    err = EINVAL;
162	    goto fail;
163	}
164
165	idev->d_kind.netif.unit = unit;
166	if (path != NULL)
167	    *path = (*cp == 0) ? cp : cp + 1;
168	break;
169
170    default:
171	err = EINVAL;
172	goto fail;
173    }
174    idev->d_dev = dv;
175    idev->d_type = dv->dv_type;
176    if (dev == NULL) {
177	free(idev);
178    } else {
179	*dev = idev;
180    }
181    return(0);
182
183 fail:
184    free(idev);
185    return(err);
186}
187