1*8352e4cbSmiod /* $OpenBSD: devopen.c,v 1.4 2023/01/10 17:10:57 miod Exp $ */
296f141a8Smiod /* $NetBSD: devopen.c,v 1.3 2013/01/16 15:46:20 tsutsui Exp $ */
396f141a8Smiod
496f141a8Smiod /*
596f141a8Smiod * Copyright (c) 1992 OMRON Corporation.
696f141a8Smiod *
796f141a8Smiod * This code is derived from software contributed to Berkeley by
896f141a8Smiod * OMRON Corporation.
996f141a8Smiod *
1096f141a8Smiod * Redistribution and use in source and binary forms, with or without
1196f141a8Smiod * modification, are permitted provided that the following conditions
1296f141a8Smiod * are met:
1396f141a8Smiod * 1. Redistributions of source code must retain the above copyright
1496f141a8Smiod * notice, this list of conditions and the following disclaimer.
1596f141a8Smiod * 2. Redistributions in binary form must reproduce the above copyright
1696f141a8Smiod * notice, this list of conditions and the following disclaimer in the
1796f141a8Smiod * documentation and/or other materials provided with the distribution.
1896f141a8Smiod * 3. All advertising materials mentioning features or use of this software
1996f141a8Smiod * must display the following acknowledgement:
2096f141a8Smiod * This product includes software developed by the University of
2196f141a8Smiod * California, Berkeley and its contributors.
2296f141a8Smiod * 4. Neither the name of the University nor the names of its contributors
2396f141a8Smiod * may be used to endorse or promote products derived from this software
2496f141a8Smiod * without specific prior written permission.
2596f141a8Smiod *
2696f141a8Smiod * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2796f141a8Smiod * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2896f141a8Smiod * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2996f141a8Smiod * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3096f141a8Smiod * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3196f141a8Smiod * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3296f141a8Smiod * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3396f141a8Smiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3496f141a8Smiod * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3596f141a8Smiod * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3696f141a8Smiod * SUCH DAMAGE.
3796f141a8Smiod *
3896f141a8Smiod * @(#)conf.c 8.1 (Berkeley) 6/10/93
3996f141a8Smiod */
4096f141a8Smiod /*
4196f141a8Smiod * Copyright (c) 1992, 1993
4296f141a8Smiod * The Regents of the University of California. All rights reserved.
4396f141a8Smiod *
4496f141a8Smiod * This code is derived from software contributed to Berkeley by
4596f141a8Smiod * OMRON Corporation.
4696f141a8Smiod *
4796f141a8Smiod * Redistribution and use in source and binary forms, with or without
4896f141a8Smiod * modification, are permitted provided that the following conditions
4996f141a8Smiod * are met:
5096f141a8Smiod * 1. Redistributions of source code must retain the above copyright
5196f141a8Smiod * notice, this list of conditions and the following disclaimer.
5296f141a8Smiod * 2. Redistributions in binary form must reproduce the above copyright
5396f141a8Smiod * notice, this list of conditions and the following disclaimer in the
5496f141a8Smiod * documentation and/or other materials provided with the distribution.
5596f141a8Smiod * 3. Neither the name of the University nor the names of its contributors
5696f141a8Smiod * may be used to endorse or promote products derived from this software
5796f141a8Smiod * without specific prior written permission.
5896f141a8Smiod *
5996f141a8Smiod * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6096f141a8Smiod * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6196f141a8Smiod * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6296f141a8Smiod * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6396f141a8Smiod * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6496f141a8Smiod * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6596f141a8Smiod * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6696f141a8Smiod * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6796f141a8Smiod * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6896f141a8Smiod * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6996f141a8Smiod * SUCH DAMAGE.
7096f141a8Smiod *
7196f141a8Smiod * @(#)conf.c 8.1 (Berkeley) 6/10/93
7296f141a8Smiod */
7396f141a8Smiod
742a8992e0Saoyama #include <sys/reboot.h>
7596f141a8Smiod #include <lib/libkern/libkern.h>
7696f141a8Smiod #include <luna88k/stand/boot/samachdep.h>
7796f141a8Smiod #include <machine/disklabel.h>
7896f141a8Smiod
79d2f66e2eSmiod #define MAXDEVNAME 16
80d2f66e2eSmiod
8196f141a8Smiod static int make_device(const char *, int *, int *, int *, char **);
8296f141a8Smiod
8396f141a8Smiod int
devopen(struct open_file * f,const char * fname,char ** file)8496f141a8Smiod devopen(struct open_file *f, const char *fname, char **file)
8596f141a8Smiod {
8696f141a8Smiod int dev, unit, part;
8796f141a8Smiod int error;
8896f141a8Smiod struct devsw *dp;
8996f141a8Smiod int i;
9096f141a8Smiod
9196f141a8Smiod if (make_device(fname, &dev, &unit, &part, file) != 0)
9296f141a8Smiod return ENXIO;
9396f141a8Smiod
9496f141a8Smiod #ifdef DEBUG
95*8352e4cbSmiod printf("%s: %s(%d,%d):%s\n", __func__,
9696f141a8Smiod devsw[dev].dv_name, unit, part, *file);
9796f141a8Smiod #endif
9896f141a8Smiod dp = &devsw[dev];
9996f141a8Smiod error = (*dp->dv_open)(f, unit, part);
10096f141a8Smiod if (error != 0) {
10196f141a8Smiod #ifdef DEBUG
102*8352e4cbSmiod printf("%s: open %s(%d,%d):%s failed (%s)\n", __func__,
10396f141a8Smiod devsw[dev].dv_name, unit, part, *file, strerror(error));
10496f141a8Smiod #endif
10596f141a8Smiod return error;
10696f141a8Smiod }
10796f141a8Smiod
10896f141a8Smiod for (i = 0; i < nfsys_disk; i++)
10996f141a8Smiod file_system[i] = file_system_disk[i];
11096f141a8Smiod nfsys = nfsys_disk;
11196f141a8Smiod
11296f141a8Smiod #ifdef SUPPORT_ETHERNET
11396f141a8Smiod if (strcmp(dp->dv_name, "le") == 0) {
11496f141a8Smiod /* XXX mixing local fs_ops on netboot could be troublesome */
11596f141a8Smiod file_system[0] = file_system_nfs[0];
11696f141a8Smiod nfsys = 1;
11796f141a8Smiod }
11896f141a8Smiod #endif
11996f141a8Smiod
12096f141a8Smiod f->f_dev = dp;
12196f141a8Smiod
1222a8992e0Saoyama /* Save boot device information to pass to the kernel */
123*8352e4cbSmiod bootdev = MAKEBOOTDEV(dev, 0, unit / 10, 6 - unit % 10, part);
1242a8992e0Saoyama
12596f141a8Smiod return 0;
12696f141a8Smiod }
12796f141a8Smiod
12896f141a8Smiod int
make_device(const char * str,int * devp,int * unitp,int * partp,char ** fname)12996f141a8Smiod make_device(const char *str, int *devp, int *unitp, int *partp, char **fname)
13096f141a8Smiod {
13196f141a8Smiod const char *cp;
13296f141a8Smiod struct devsw *dp;
133d2f66e2eSmiod int major, unit = 0, part = 0;
13496f141a8Smiod int i;
13596f141a8Smiod char devname[MAXDEVNAME + 1];
13696f141a8Smiod
13796f141a8Smiod /*
13896f141a8Smiod * parse path strings
13996f141a8Smiod */
14096f141a8Smiod /* find end of dev type name */
14196f141a8Smiod for (cp = str, i = 0; *cp != '\0' && *cp != '(' && i < MAXDEVNAME; i++)
14296f141a8Smiod devname[i] = *cp++;
14396f141a8Smiod if (*cp != '(') {
14496f141a8Smiod return (-1);
14596f141a8Smiod }
14696f141a8Smiod devname[i] = '\0';
14796f141a8Smiod /* compare dev type name */
14896f141a8Smiod for (dp = devsw; dp->dv_name; dp++)
14996f141a8Smiod if (!strcmp(devname, dp->dv_name))
15096f141a8Smiod break;
15196f141a8Smiod cp++;
15296f141a8Smiod if (dp->dv_name == NULL) {
15396f141a8Smiod return (-1);
15496f141a8Smiod }
15596f141a8Smiod major = dp - devsw;
156d2f66e2eSmiod /* get mixed controller and unit number */
157d2f66e2eSmiod for (; *cp != ',' && *cp != ')'; cp++) {
158d2f66e2eSmiod if (*cp == '\0')
159d2f66e2eSmiod return -1;
16096f141a8Smiod if (*cp >= '0' && *cp <= '9')
161d2f66e2eSmiod unit = unit * 10 + *cp - '0';
162d2f66e2eSmiod }
163d2f66e2eSmiod if (unit < 0 || unit >= 20 || (unit % 10) > 7) {
16496f141a8Smiod #ifdef DEBUG
16596f141a8Smiod printf("%s: invalid unit number (%d)\n", __func__, unit);
16696f141a8Smiod #endif
16796f141a8Smiod return (-1);
16896f141a8Smiod }
169d2f66e2eSmiod /* get optional partition number */
170d2f66e2eSmiod if (*cp == ',')
171d2f66e2eSmiod cp++;
172d2f66e2eSmiod
173d2f66e2eSmiod for (; /* *cp != ',' && */ *cp != ')'; cp++) {
174d2f66e2eSmiod if (*cp == '\0')
175d2f66e2eSmiod return -1;
176d2f66e2eSmiod if (*cp >= '0' && *cp <= '9')
177d2f66e2eSmiod part = part * 10 + *cp - '0';
17896f141a8Smiod }
179d2f66e2eSmiod if (part < 0 || part >= MAXPARTITIONS) {
18096f141a8Smiod #ifdef DEBUG
18196f141a8Smiod printf("%s: invalid partition number (%d)\n", __func__, part);
18296f141a8Smiod #endif
18396f141a8Smiod return (-1);
18496f141a8Smiod }
185d2f66e2eSmiod /* check out end of dev spec */
18696f141a8Smiod *devp = major;
18796f141a8Smiod *unitp = unit;
18896f141a8Smiod *partp = part;
189d2f66e2eSmiod cp++;
190*8352e4cbSmiod if (*cp == ':')
191*8352e4cbSmiod cp++;
192d2f66e2eSmiod if (*cp == '\0')
193d2f66e2eSmiod *fname = "bsd";
194d2f66e2eSmiod else
195d2f66e2eSmiod *fname = (char *)cp; /* XXX */
19696f141a8Smiod #ifdef DEBUG
197*8352e4cbSmiod printf("%s(%s): major = %d, unit = %d, part = %d, fname = %s\n",
198*8352e4cbSmiod __func__, str, major, unit, part, *fname);
19996f141a8Smiod #endif
20096f141a8Smiod
20196f141a8Smiod return 0;
20296f141a8Smiod }
203