1*11c03005Smaxv /* $NetBSD: devicename.c,v 1.9 2017/06/25 12:04:37 maxv Exp $ */
2ba7cbe76Scherry
3ba7cbe76Scherry /*-
4ba7cbe76Scherry * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5ba7cbe76Scherry * All rights reserved.
6ba7cbe76Scherry *
7ba7cbe76Scherry * Redistribution and use in source and binary forms, with or without
8ba7cbe76Scherry * modification, are permitted provided that the following conditions
9ba7cbe76Scherry * are met:
10ba7cbe76Scherry * 1. Redistributions of source code must retain the above copyright
11ba7cbe76Scherry * notice, this list of conditions and the following disclaimer.
12ba7cbe76Scherry * 2. Redistributions in binary form must reproduce the above copyright
13ba7cbe76Scherry * notice, this list of conditions and the following disclaimer in the
14ba7cbe76Scherry * documentation and/or other materials provided with the distribution.
15ba7cbe76Scherry *
16ba7cbe76Scherry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ba7cbe76Scherry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ba7cbe76Scherry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ba7cbe76Scherry * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ba7cbe76Scherry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ba7cbe76Scherry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ba7cbe76Scherry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ba7cbe76Scherry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ba7cbe76Scherry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ba7cbe76Scherry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ba7cbe76Scherry * SUCH DAMAGE.
27ba7cbe76Scherry */
28ba7cbe76Scherry
29ba7cbe76Scherry #include <sys/cdefs.h>
3040d4f67eScherry /* __FBSDID("$FreeBSD: src/sys/boot/ia64/libski/devicename.c,v 1.2 2003/09/08 09:11:32 obrien Exp $"); */
31ba7cbe76Scherry
32ba7cbe76Scherry #include <lib/libsa/stand.h>
3393b81f4cSkiyohara #include <lib/libsa/loadfile.h>
34ff478f58Smartin #include <lib/libkern/libkern.h>
35ba7cbe76Scherry #include <sys/disklabel.h>
36ba7cbe76Scherry
37ba7cbe76Scherry #include <bootstrap.h>
38ba7cbe76Scherry #include "libski.h"
39ba7cbe76Scherry
40ba7cbe76Scherry static int ski_parsedev(struct ski_devdesc **dev, const char *devspec, const char **path);
41ba7cbe76Scherry
42ba7cbe76Scherry /*
43ba7cbe76Scherry * Point (dev) at an allocated device specifier for the device matching the
44ba7cbe76Scherry * path in (devspec). If it contains an explicit device specification,
45ba7cbe76Scherry * use that. If not, use the default device.
46ba7cbe76Scherry */
47ba7cbe76Scherry int
ski_getdev(void ** vdev,const char * devspec,const char ** path)48ba7cbe76Scherry ski_getdev(void **vdev, const char *devspec, const char **path)
49ba7cbe76Scherry {
50ba7cbe76Scherry struct ski_devdesc **dev = (struct ski_devdesc **)vdev;
51ba7cbe76Scherry int rv;
52ba7cbe76Scherry
53ba7cbe76Scherry /*
54ba7cbe76Scherry * If it looks like this is just a path and no
55ba7cbe76Scherry * device, go with the current device.
56ba7cbe76Scherry */
57ba7cbe76Scherry if ((devspec == NULL) ||
58ba7cbe76Scherry (devspec[0] == '/') ||
59ba7cbe76Scherry (strchr(devspec, ':') == NULL)) {
60ba7cbe76Scherry
61ba7cbe76Scherry if (((rv = ski_parsedev(dev, getenv("currdev"), NULL)) == 0) &&
62ba7cbe76Scherry (path != NULL))
63ba7cbe76Scherry *path = devspec;
64ba7cbe76Scherry return(rv);
65ba7cbe76Scherry }
66ba7cbe76Scherry
67ba7cbe76Scherry /*
68ba7cbe76Scherry * Try to parse the device name off the beginning of the devspec
69ba7cbe76Scherry */
70ba7cbe76Scherry return(ski_parsedev(dev, devspec, path));
71ba7cbe76Scherry }
72ba7cbe76Scherry
73ba7cbe76Scherry /*
74ba7cbe76Scherry * Point (dev) at an allocated device specifier matching the string version
75ba7cbe76Scherry * at the beginning of (devspec). Return a pointer to the remaining
76ba7cbe76Scherry * text in (path).
77ba7cbe76Scherry *
78ba7cbe76Scherry * In all cases, the beginning of (devspec) is compared to the names
79ba7cbe76Scherry * of known devices in the device switch, and then any following text
80ba7cbe76Scherry * is parsed according to the rules applied to the device type.
81ba7cbe76Scherry *
82ba7cbe76Scherry * For disk-type devices, the syntax is:
83ba7cbe76Scherry *
84ba7cbe76Scherry * disk<unit>[s<slice>][<partition>]:
85ba7cbe76Scherry *
86ba7cbe76Scherry */
87ba7cbe76Scherry static int
ski_parsedev(struct ski_devdesc ** dev,const char * devspec,const char ** path)88ba7cbe76Scherry ski_parsedev(struct ski_devdesc **dev, const char *devspec, const char **path)
89ba7cbe76Scherry {
90ba7cbe76Scherry struct ski_devdesc *idev;
91ba7cbe76Scherry struct devsw *dv;
92ba7cbe76Scherry int dv_type;
93ba7cbe76Scherry int i, unit, slice, partition, err;
94eedf4fb2Smaxv char *cp = NULL;
95ba7cbe76Scherry const char *np;
96ba7cbe76Scherry
97ba7cbe76Scherry /* minimum length check */
98ba7cbe76Scherry if (strlen(devspec) < 2)
99ba7cbe76Scherry return(EINVAL);
100ba7cbe76Scherry
101ba7cbe76Scherry /* look for a device that matches */
102ba7cbe76Scherry for (i = 0, dv = NULL; i < ndevs; i++) {
103ba7cbe76Scherry if (!strncmp(devspec, devsw[i].dv_name, strlen(devsw[i].dv_name))) {
104ba7cbe76Scherry dv = &devsw[i];
105ba7cbe76Scherry break;
106ba7cbe76Scherry }
107ba7cbe76Scherry }
108ba7cbe76Scherry
109ba7cbe76Scherry if (dv == NULL)
110ba7cbe76Scherry return(ENOENT);
111ba7cbe76Scherry idev = alloc(sizeof(struct ski_devdesc));
112ba7cbe76Scherry err = 0;
113ba7cbe76Scherry np = (devspec + strlen(dv->dv_name));
114ba7cbe76Scherry
115ba7cbe76Scherry /* XXX: Fixme */
116ba7cbe76Scherry dv_type = DEVT_DISK;
117ba7cbe76Scherry
118ba7cbe76Scherry switch(dv_type) {
119ba7cbe76Scherry case DEVT_NONE: /* XXX what to do here? Do we care? */
120ba7cbe76Scherry break;
121ba7cbe76Scherry
122ba7cbe76Scherry case DEVT_DISK: /* XXX: Need to fix DEVT_DISK for NetBSD disk conventions. */
123ba7cbe76Scherry unit = -1;
124ba7cbe76Scherry slice = -1;
125ba7cbe76Scherry partition = -1;
126ba7cbe76Scherry if (*np && (*np != ':')) {
127ba7cbe76Scherry unit = strtol(np, &cp, 10); /* next comes the unit number */
128ba7cbe76Scherry if (cp == np) {
129ba7cbe76Scherry err = EUNIT;
130ba7cbe76Scherry goto fail;
131ba7cbe76Scherry }
132ba7cbe76Scherry if (*cp == 's') { /* got a slice number */
133ba7cbe76Scherry np = cp + 1;
134ba7cbe76Scherry slice = strtol(np, &cp, 10);
135ba7cbe76Scherry if (cp == np) {
136ba7cbe76Scherry err = EPART;
137ba7cbe76Scherry goto fail;
138ba7cbe76Scherry }
139ba7cbe76Scherry }
140ba7cbe76Scherry if (*cp && (*cp != ':')) {
141ba7cbe76Scherry partition = *cp - 'a'; /* get a partition number */
142ba7cbe76Scherry if ((partition < 0) || (partition >= MAXPARTITIONS)) {
143ba7cbe76Scherry err = EPART;
144ba7cbe76Scherry goto fail;
145ba7cbe76Scherry }
146ba7cbe76Scherry cp++;
147ba7cbe76Scherry }
148ba7cbe76Scherry }
149eedf4fb2Smaxv if (cp == NULL) {
150eedf4fb2Smaxv err = EINVAL;
151eedf4fb2Smaxv goto fail;
152eedf4fb2Smaxv }
153ba7cbe76Scherry if (*cp && (*cp != ':')) {
154ba7cbe76Scherry err = EINVAL;
155ba7cbe76Scherry goto fail;
156ba7cbe76Scherry }
157ba7cbe76Scherry
158ba7cbe76Scherry idev->d_kind.skidisk.unit = unit;
159ba7cbe76Scherry idev->d_kind.skidisk.slice = slice;
160ba7cbe76Scherry idev->d_kind.skidisk.partition = partition;
161ba7cbe76Scherry
162ba7cbe76Scherry if (path != NULL)
163ba7cbe76Scherry *path = (*cp == 0) ? cp : cp + 1;
164ba7cbe76Scherry break;
165ba7cbe76Scherry
166ba7cbe76Scherry case DEVT_NET:
167ba7cbe76Scherry unit = 0;
168ba7cbe76Scherry
169ba7cbe76Scherry if (*np && (*np != ':')) {
170ba7cbe76Scherry unit = strtol(np, &cp, 0); /* get unit number if present */
171ba7cbe76Scherry if (cp == np) {
172ba7cbe76Scherry err = EUNIT;
173ba7cbe76Scherry goto fail;
174ba7cbe76Scherry }
175ba7cbe76Scherry }
176eedf4fb2Smaxv if (cp == NULL) {
177eedf4fb2Smaxv err = EINVAL;
178eedf4fb2Smaxv goto fail;
179eedf4fb2Smaxv }
180ba7cbe76Scherry if (*cp && (*cp != ':')) {
181ba7cbe76Scherry err = EINVAL;
182ba7cbe76Scherry goto fail;
183ba7cbe76Scherry }
184ba7cbe76Scherry
185ba7cbe76Scherry idev->d_kind.netif.unit = unit;
186ba7cbe76Scherry if (path != NULL)
187ba7cbe76Scherry *path = (*cp == 0) ? cp : cp + 1;
188ba7cbe76Scherry break;
189ba7cbe76Scherry
190ba7cbe76Scherry default:
191ba7cbe76Scherry err = EINVAL;
192ba7cbe76Scherry goto fail;
193ba7cbe76Scherry }
194ba7cbe76Scherry idev->d_dev = dv;
195ba7cbe76Scherry idev->d_type = dv_type;
196ba7cbe76Scherry if (dev == NULL) {
197ba7cbe76Scherry free(idev);
198ba7cbe76Scherry } else {
199ba7cbe76Scherry *dev = idev;
200ba7cbe76Scherry }
201ba7cbe76Scherry return(0);
202ba7cbe76Scherry
203ba7cbe76Scherry fail:
204ba7cbe76Scherry free(idev);
205ba7cbe76Scherry return(err);
206ba7cbe76Scherry }
207ba7cbe76Scherry
208ba7cbe76Scherry
209ba7cbe76Scherry char *
ski_fmtdev(void * vdev)210ba7cbe76Scherry ski_fmtdev(void *vdev)
211ba7cbe76Scherry {
212ba7cbe76Scherry struct ski_devdesc *dev = (struct ski_devdesc *)vdev;
213ba7cbe76Scherry static char buf[128]; /* XXX device length constant? */
21419d51526Schristos size_t len, buflen = sizeof(buf);
215ba7cbe76Scherry
216ba7cbe76Scherry switch(dev->d_type) {
217ba7cbe76Scherry case DEVT_NONE:
21819d51526Schristos strlcpy(buf, "(no device)", buflen);
219ba7cbe76Scherry break;
220ba7cbe76Scherry
221ba7cbe76Scherry case DEVT_DISK:
22219d51526Schristos len = snprintf(buf, buflen, "%s%d", dev->d_dev->dv_name, dev->d_kind.skidisk.unit);
22319d51526Schristos if (len > buflen)
22419d51526Schristos len = buflen;
22519d51526Schristos if (dev->d_kind.skidisk.slice > 0) {
22619d51526Schristos len += snprintf(buf + len, buflen - len, "s%d", dev->d_kind.skidisk.slice);
22719d51526Schristos if (len > buflen)
22819d51526Schristos len = buflen;
22919d51526Schristos }
23019d51526Schristos if (dev->d_kind.skidisk.partition >= 0) {
23119d51526Schristos len += snprintf(buf + len, buflen - len, "%c", dev->d_kind.skidisk.partition + 'a');
23219d51526Schristos if (len > buflen)
23319d51526Schristos len = buflen;
23419d51526Schristos }
235fb494b14Smartin strlcat(buf, ":", buflen - len);
236ba7cbe76Scherry break;
237ba7cbe76Scherry
238ba7cbe76Scherry case DEVT_NET:
239*11c03005Smaxv snprintf(buf, buflen, "%s%d:", dev->d_dev->dv_name, dev->d_kind.netif.unit);
240ba7cbe76Scherry break;
241ba7cbe76Scherry }
242ba7cbe76Scherry return(buf);
243ba7cbe76Scherry }
244ba7cbe76Scherry
245ba7cbe76Scherry
246ba7cbe76Scherry /*
247ba7cbe76Scherry * Set currdev to suit the value being supplied in (value)
248ba7cbe76Scherry */
249ba7cbe76Scherry int
ski_setcurrdev(struct env_var * ev,int flags,void * value)250ba7cbe76Scherry ski_setcurrdev(struct env_var *ev, int flags, void *value)
251ba7cbe76Scherry {
252ba7cbe76Scherry struct ski_devdesc *ncurr;
253ba7cbe76Scherry int rv;
254ba7cbe76Scherry
255ba7cbe76Scherry if ((rv = ski_parsedev(&ncurr, value, NULL)) != 0)
256ba7cbe76Scherry return(rv);
257ba7cbe76Scherry free(ncurr);
258ba7cbe76Scherry env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
259ba7cbe76Scherry return(0);
260ba7cbe76Scherry }
261ba7cbe76Scherry
262