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