Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / boot / ia64 / libski / 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/ia64/libski/devicename.c,v 1.2 2003/09/08 09:11:32 obrien Exp $
27  * $DragonFly: src/sys/boot/ia64/libski/devicename.c,v 1.1 2003/11/10 06:08:37 dillon Exp $
28  */
29
30 #include <stand.h>
31 #include <string.h>
32 #include <sys/disklabel.h>
33 #include "bootstrap.h"
34 #include "libski.h"
35
36 static int      ski_parsedev(struct ski_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 ski_getdev(void **vdev, const char *devspec, const char **path)
45 {
46         struct ski_devdesc **dev = (struct ski_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 = ski_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(ski_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 ski_parsedev(struct ski_devdesc **dev, const char *devspec, const char **path)
85 {
86         struct ski_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 ski_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.skidisk.unit = unit;
147                 idev->d_kind.skidisk.slice = slice;
148                 idev->d_kind.skidisk.partition = partition;
149
150                 if (path != NULL)
151                         *path = (*cp == 0) ? cp : cp + 1;
152                 break;
153         
154         case DEVT_NET:
155                 unit = 0;
156         
157                 if (*np && (*np != ':')) {
158                         unit = strtol(np, &cp, 0);      /* get unit number if present */
159                         if (cp == np) {
160                                 err = EUNIT;
161                                 goto fail;
162                         }
163                 }
164                 if (*cp && (*cp != ':')) {
165                         err = EINVAL;
166                         goto fail;
167                 }
168         
169                 idev->d_kind.netif.unit = unit;
170                 if (path != NULL)
171                         *path = (*cp == 0) ? cp : cp + 1;
172                 break;
173
174         default:
175                 err = EINVAL;
176                 goto fail;
177         }
178         idev->d_dev = dv;
179         idev->d_type = dv->dv_type;
180         if (dev == NULL) {
181                 free(idev);
182         } else {
183                 *dev = idev;
184         }
185         return(0);
186
187  fail:
188         free(idev);
189         return(err);
190 }
191
192
193 char *
194 ski_fmtdev(void *vdev)
195 {
196         struct ski_devdesc *dev = (struct ski_devdesc *)vdev;
197         static char     buf[128];       /* XXX device length constant? */
198         char            *cp;
199     
200         switch(dev->d_type) {
201         case DEVT_NONE:
202                 strcpy(buf, "(no device)");
203                 break;
204
205         case DEVT_DISK:
206                 cp = buf;
207                 cp += sprintf(cp, "%s%d", dev->d_dev->dv_name, dev->d_kind.skidisk.unit);
208                 if (dev->d_kind.skidisk.slice > 0)
209                         cp += sprintf(cp, "s%d", dev->d_kind.skidisk.slice);
210                 if (dev->d_kind.skidisk.partition >= 0)
211                         cp += sprintf(cp, "%c", dev->d_kind.skidisk.partition + 'a');
212                 strcat(cp, ":");
213                 break;
214
215         case DEVT_NET:
216                 sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_kind.netif.unit);
217                 break;
218         }
219         return(buf);
220 }
221
222
223 /*
224  * Set currdev to suit the value being supplied in (value)
225  */
226 int
227 ski_setcurrdev(struct env_var *ev, int flags, void *value)
228 {
229         struct ski_devdesc *ncurr;
230         int             rv;
231     
232         if ((rv = ski_parsedev(&ncurr, value, NULL)) != 0)
233                 return(rv);
234         free(ncurr);
235         env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
236         return(0);
237 }
238