1422eb17fSToomas Soome /*
2199767f8SToomas Soome  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3199767f8SToomas Soome  * All rights reserved.
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome  * modification, are permitted provided that the following conditions
7199767f8SToomas Soome  * are met:
8199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome  *
14199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome  * SUCH DAMAGE.
25199767f8SToomas Soome  */
26199767f8SToomas Soome 
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome 
29199767f8SToomas Soome #include <stand.h>
30199767f8SToomas Soome #include <string.h>
31422eb17fSToomas Soome #include <sys/param.h>
32199767f8SToomas Soome #include "bootstrap.h"
33199767f8SToomas Soome #include "disk.h"
34199767f8SToomas Soome #include "libi386.h"
353d4c0714SToomas Soome #include "libzfs.h"
36199767f8SToomas Soome 
37422eb17fSToomas Soome static int i386_parsedev(struct i386_devdesc **, const char *, const char **);
38199767f8SToomas Soome 
39422eb17fSToomas Soome /*
40422eb17fSToomas Soome  * Point dev at an allocated device specifier for the device matching the
41422eb17fSToomas Soome  * path in devspec. If it contains an explicit device specification,
42199767f8SToomas Soome  * use that.  If not, use the default device.
43199767f8SToomas Soome  */
44199767f8SToomas Soome int
i386_getdev(void ** vdev,const char * devspec,const char ** path)45199767f8SToomas Soome i386_getdev(void **vdev, const char *devspec, const char **path)
46199767f8SToomas Soome {
47422eb17fSToomas Soome 	struct i386_devdesc **dev = (struct i386_devdesc **)vdev;
48422eb17fSToomas Soome 	int rv;
49422eb17fSToomas Soome 
50422eb17fSToomas Soome 	/*
51422eb17fSToomas Soome 	 * If it looks like this is just a path and no
52422eb17fSToomas Soome 	 * device, go with the current device.
53422eb17fSToomas Soome 	 */
54422eb17fSToomas Soome 	if ((devspec == NULL) ||
55422eb17fSToomas Soome 	    (devspec[0] == '/') || (strchr(devspec, ':') == NULL)) {
56422eb17fSToomas Soome 
57422eb17fSToomas Soome 		rv = i386_parsedev(dev, getenv("currdev"), NULL);
58422eb17fSToomas Soome 		if (rv == 0 && path != NULL)
59422eb17fSToomas Soome 			*path = devspec;
60422eb17fSToomas Soome 		return (rv);
61422eb17fSToomas Soome 	}
62422eb17fSToomas Soome 
63422eb17fSToomas Soome 	/*
64422eb17fSToomas Soome 	 * Try to parse the device name off the beginning of the devspec
65422eb17fSToomas Soome 	 */
66422eb17fSToomas Soome 	return (i386_parsedev(dev, devspec, path));
67199767f8SToomas Soome }
68199767f8SToomas Soome 
69199767f8SToomas Soome /*
70199767f8SToomas Soome  * Point (dev) at an allocated device specifier matching the string version
71199767f8SToomas Soome  * at the beginning of (devspec).  Return a pointer to the remaining
72199767f8SToomas Soome  * text in (path).
73199767f8SToomas Soome  *
74199767f8SToomas Soome  * In all cases, the beginning of (devspec) is compared to the names
75199767f8SToomas Soome  * of known devices in the device switch, and then any following text
76199767f8SToomas Soome  * is parsed according to the rules applied to the device type.
77199767f8SToomas Soome  *
78199767f8SToomas Soome  * For disk-type devices, the syntax is:
79199767f8SToomas Soome  *
80199767f8SToomas Soome  * disk<unit>[s<slice>][<partition>]:
81422eb17fSToomas Soome  *
82199767f8SToomas Soome  */
83199767f8SToomas Soome static int
i386_parsedev(struct i386_devdesc ** dev,const char * devspec,const char ** path)84199767f8SToomas Soome i386_parsedev(struct i386_devdesc **dev, const char *devspec, const char **path)
85199767f8SToomas Soome {
86422eb17fSToomas Soome 	struct i386_devdesc *idev;
87422eb17fSToomas Soome 	struct devsw *dv;
88422eb17fSToomas Soome 	int i, unit, err;
89422eb17fSToomas Soome 	char *cp;
90422eb17fSToomas Soome 	const char *np;
91422eb17fSToomas Soome 
92422eb17fSToomas Soome 	/* minimum length check */
93422eb17fSToomas Soome 	if (strlen(devspec) < 2)
94422eb17fSToomas Soome 		return (EINVAL);
95422eb17fSToomas Soome 
96422eb17fSToomas Soome 	/* look for a device that matches */
97422eb17fSToomas Soome 	for (i = 0, dv = NULL; devsw[i] != NULL; i++) {
98422eb17fSToomas Soome 		dv = devsw[i];
99422eb17fSToomas Soome 		if (strncmp(devspec, dv->dv_name, strlen(dv->dv_name)) == 0)
100422eb17fSToomas Soome 			break;
101199767f8SToomas Soome 	}
102422eb17fSToomas Soome 	if (devsw[i] == NULL)
103422eb17fSToomas Soome 		return (ENOENT);
104422eb17fSToomas Soome 
105422eb17fSToomas Soome 	np = devspec + strlen(dv->dv_name);
106422eb17fSToomas Soome 	idev = NULL;
107422eb17fSToomas Soome 	err = 0;
108422eb17fSToomas Soome 
109422eb17fSToomas Soome 	switch (dv->dv_type) {
110422eb17fSToomas Soome 	case DEVT_NONE:
111422eb17fSToomas Soome 		break;
112422eb17fSToomas Soome 
113422eb17fSToomas Soome 	case DEVT_DISK:
114422eb17fSToomas Soome 		idev = malloc(sizeof (struct i386_devdesc));
115422eb17fSToomas Soome 		if (idev == NULL)
116422eb17fSToomas Soome 			return (ENOMEM);
117422eb17fSToomas Soome 
118422eb17fSToomas Soome 		err = disk_parsedev((struct disk_devdesc *)idev, np, path);
119422eb17fSToomas Soome 		if (err != 0)
120422eb17fSToomas Soome 			goto fail;
121422eb17fSToomas Soome 		break;
122422eb17fSToomas Soome 
123422eb17fSToomas Soome 	case DEVT_ZFS:
124422eb17fSToomas Soome 		idev = malloc(sizeof (struct zfs_devdesc));
125422eb17fSToomas Soome 		if (idev == NULL)
126422eb17fSToomas Soome 			return (ENOMEM);
127422eb17fSToomas Soome 
128422eb17fSToomas Soome 		err = zfs_parsedev((struct zfs_devdesc *)idev, np, path);
129422eb17fSToomas Soome 		if (err != 0)
130422eb17fSToomas Soome 			goto fail;
131422eb17fSToomas Soome 		break;
132422eb17fSToomas Soome 
133422eb17fSToomas Soome 	default:
134422eb17fSToomas Soome 		idev = malloc(sizeof (struct devdesc));
135422eb17fSToomas Soome 		if (idev == NULL)
136422eb17fSToomas Soome 			return (ENOMEM);
137422eb17fSToomas Soome 
138422eb17fSToomas Soome 		unit = 0;
139199767f8SToomas Soome 		cp = (char *)np;
140422eb17fSToomas Soome 
141422eb17fSToomas Soome 		if (*np && (*np != ':')) {
142422eb17fSToomas Soome 			/* get unit number if present */
143422eb17fSToomas Soome 			unit = strtol(np, &cp, 0);
144422eb17fSToomas Soome 			if (cp == np) {
145422eb17fSToomas Soome 				err = EUNIT;
146422eb17fSToomas Soome 				goto fail;
147422eb17fSToomas Soome 			}
148422eb17fSToomas Soome 		}
149422eb17fSToomas Soome 		if (*cp && (*cp != ':')) {
150422eb17fSToomas Soome 			err = EINVAL;
151422eb17fSToomas Soome 			goto fail;
152422eb17fSToomas Soome 		}
153422eb17fSToomas Soome 
15476b35943SToomas Soome 		idev->dd.d_unit = unit;
155422eb17fSToomas Soome 		if (path != NULL)
156422eb17fSToomas Soome 			*path = (*cp == '\0') ? cp : cp + 1;
157422eb17fSToomas Soome 		break;
158199767f8SToomas Soome 	}
159199767f8SToomas Soome 
16076b35943SToomas Soome 	idev->dd.d_dev = dv;
161422eb17fSToomas Soome 
162422eb17fSToomas Soome 	if (dev != NULL)
163422eb17fSToomas Soome 		*dev = idev;
164422eb17fSToomas Soome 	else
165422eb17fSToomas Soome 		free(idev);
166422eb17fSToomas Soome 	return (0);
167422eb17fSToomas Soome 
168422eb17fSToomas Soome fail:
169199767f8SToomas Soome 	free(idev);
170422eb17fSToomas Soome 	return (err);
171199767f8SToomas Soome }
172199767f8SToomas Soome 
173199767f8SToomas Soome 
174199767f8SToomas Soome char *
i386_fmtdev(void * vdev)175199767f8SToomas Soome i386_fmtdev(void *vdev)
176199767f8SToomas Soome {
177422eb17fSToomas Soome 	struct i386_devdesc *dev = (struct i386_devdesc *)vdev;
178422eb17fSToomas Soome 	static char buf[SPECNAMELEN + 1];
179422eb17fSToomas Soome 
180c142ce19SToomas Soome 	switch (dev->dd.d_dev->dv_type) {
181422eb17fSToomas Soome 	case DEVT_NONE:
182422eb17fSToomas Soome 		strlcpy(buf, "(no device)", sizeof (buf));
183422eb17fSToomas Soome 		break;
184422eb17fSToomas Soome 
185422eb17fSToomas Soome 	case DEVT_DISK:
186422eb17fSToomas Soome 		return (disk_fmtdev(vdev));
187422eb17fSToomas Soome 
188422eb17fSToomas Soome 	case DEVT_ZFS:
189422eb17fSToomas Soome 		return (zfs_fmtdev(vdev));
190422eb17fSToomas Soome 
191422eb17fSToomas Soome 	default:
19276b35943SToomas Soome 		snprintf(buf, sizeof (buf), "%s%d:", dev->dd.d_dev->dv_name,
19376b35943SToomas Soome 		    dev->dd.d_unit);
194422eb17fSToomas Soome 		break;
195422eb17fSToomas Soome 	}
196422eb17fSToomas Soome 	return (buf);
197199767f8SToomas Soome }
198199767f8SToomas Soome 
199199767f8SToomas Soome 
200199767f8SToomas Soome /*
201199767f8SToomas Soome  * Set currdev to suit the value being supplied in (value)
202199767f8SToomas Soome  */
203199767f8SToomas Soome int
i386_setcurrdev(struct env_var * ev,int flags,const void * value)204199767f8SToomas Soome i386_setcurrdev(struct env_var *ev, int flags, const void *value)
205199767f8SToomas Soome {
206422eb17fSToomas Soome 	struct i386_devdesc *ncurr;
207422eb17fSToomas Soome 	int rv;
208422eb17fSToomas Soome 
209422eb17fSToomas Soome 	if ((rv = i386_parsedev(&ncurr, value, NULL)) != 0)
210422eb17fSToomas Soome 		return (rv);
211422eb17fSToomas Soome 
212422eb17fSToomas Soome 	free(ncurr);
213422eb17fSToomas Soome 	env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
214422eb17fSToomas Soome 	return (0);
215199767f8SToomas Soome }
216