1*95f49f93Schristos /* $NetBSD: devopen.c,v 1.2 2018/03/06 22:13:14 christos Exp $ */
25f7e80a8Spooka
35f7e80a8Spooka /*-
45f7e80a8Spooka * Copyright (c) 1992, 1993
55f7e80a8Spooka * The Regents of the University of California. All rights reserved.
65f7e80a8Spooka *
75f7e80a8Spooka * This code is derived from software contributed to Berkeley by
85f7e80a8Spooka * Ralph Campbell.
95f7e80a8Spooka *
105f7e80a8Spooka * Redistribution and use in source and binary forms, with or without
115f7e80a8Spooka * modification, are permitted provided that the following conditions
125f7e80a8Spooka * are met:
135f7e80a8Spooka * 1. Redistributions of source code must retain the above copyright
145f7e80a8Spooka * notice, this list of conditions and the following disclaimer.
155f7e80a8Spooka * 2. Redistributions in binary form must reproduce the above copyright
165f7e80a8Spooka * notice, this list of conditions and the following disclaimer in the
175f7e80a8Spooka * documentation and/or other materials provided with the distribution.
185f7e80a8Spooka * 3. Neither the name of the University nor the names of its contributors
195f7e80a8Spooka * may be used to endorse or promote products derived from this software
205f7e80a8Spooka * without specific prior written permission.
215f7e80a8Spooka *
225f7e80a8Spooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
235f7e80a8Spooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
245f7e80a8Spooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
255f7e80a8Spooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
265f7e80a8Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
275f7e80a8Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
285f7e80a8Spooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
295f7e80a8Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
305f7e80a8Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
315f7e80a8Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
325f7e80a8Spooka * SUCH DAMAGE.
335f7e80a8Spooka *
345f7e80a8Spooka * @(#)devopen.c 8.1 (Berkeley) 6/10/93
355f7e80a8Spooka */
365f7e80a8Spooka
375f7e80a8Spooka #include <lib/libsa/stand.h>
385f7e80a8Spooka #include <lib/libkern/libkern.h>
395f7e80a8Spooka
405f7e80a8Spooka /*
415f7e80a8Spooka * Decode the string 'fname', open the device and return the remaining
425f7e80a8Spooka * file name if any.
435f7e80a8Spooka */
445f7e80a8Spooka int
devopen(struct open_file * f,const char * fname,char ** file)455f7e80a8Spooka devopen(struct open_file *f,
465f7e80a8Spooka const char *fname,
475f7e80a8Spooka char **file) /* out */
485f7e80a8Spooka {
495f7e80a8Spooka int ctlr = 0, unit = 0, part = 0;
505f7e80a8Spooka int c, rc;
515f7e80a8Spooka char device_name[20];
525f7e80a8Spooka const char *cp;
535f7e80a8Spooka char *ncp;
545f7e80a8Spooka #if !defined(LIBSA_SINGLE_DEVICE)
555f7e80a8Spooka int i;
565f7e80a8Spooka struct devsw *dp;
575f7e80a8Spooka #endif
585f7e80a8Spooka
595f7e80a8Spooka cp = fname;
605f7e80a8Spooka ncp = device_name;
615f7e80a8Spooka
62*95f49f93Schristos /*
63*95f49f93Schristos * Require the form CTRL/DEVNAME(UNIT,PART)/FILENAME
64*95f49f93Schristos * e.g. '0/ace(0,0)/netbsd' '0/tftp(0,0)/netbsd'
655f7e80a8Spooka */
665f7e80a8Spooka
675f7e80a8Spooka /* get controller number */
68*95f49f93Schristos c = *cp++;
69*95f49f93Schristos if (c < '0' || c > '9')
70*95f49f93Schristos return ENXIO;
715f7e80a8Spooka ctlr = c - '0';
725f7e80a8Spooka
73*95f49f93Schristos c = *cp++;
745f7e80a8Spooka if (c != '/')
75*95f49f93Schristos return ENXIO;
765f7e80a8Spooka
775f7e80a8Spooka /* get device name */
785f7e80a8Spooka while ((c = *cp) != '\0') {
795f7e80a8Spooka if ((c == '(') || (c == '/')) {
805f7e80a8Spooka cp++;
815f7e80a8Spooka break;
825f7e80a8Spooka }
835f7e80a8Spooka if (ncp < device_name + sizeof(device_name) - 1)
845f7e80a8Spooka *ncp++ = c;
855f7e80a8Spooka cp++;
865f7e80a8Spooka }
875f7e80a8Spooka if (ncp == device_name)
88*95f49f93Schristos return ENXIO;
895f7e80a8Spooka
905f7e80a8Spooka /* get device number */
91*95f49f93Schristos c = *cp++;
92*95f49f93Schristos if (c < '0' || c > '9')
93*95f49f93Schristos return ENXIO;
945f7e80a8Spooka unit = c - '0';
95*95f49f93Schristos
96*95f49f93Schristos c = *cp++;
97*95f49f93Schristos if (c != ',')
985f7e80a8Spooka return (ENXIO);
995f7e80a8Spooka
1005f7e80a8Spooka /* get partition number */
101*95f49f93Schristos c = *cp++;
102*95f49f93Schristos if (c < '0' || c > '9')
103*95f49f93Schristos return ENXIO;
1045f7e80a8Spooka part = c - '0';
1055f7e80a8Spooka
106*95f49f93Schristos c = *cp++;
107*95f49f93Schristos if (c != ')')
108*95f49f93Schristos return ENXIO;
1095f7e80a8Spooka
1105f7e80a8Spooka *ncp = '\0';
1115f7e80a8Spooka
1125f7e80a8Spooka #ifdef LIBSA_SINGLE_DEVICE
1135f7e80a8Spooka rc = DEV_OPEN(dp)(f, ctlr, unit, part);
1145f7e80a8Spooka #else /* !LIBSA_SINGLE_DEVICE */
1155f7e80a8Spooka for (dp = devsw, i = 0; i < ndevs; dp++, i++)
1165f7e80a8Spooka if (dp->dv_name && strcmp(device_name, dp->dv_name) == 0)
1175f7e80a8Spooka goto fnd;
1185f7e80a8Spooka printf("Unknown device '%s'\nKnown devices are:", device_name);
1195f7e80a8Spooka for (dp = devsw, i = 0; i < ndevs; dp++, i++)
1205f7e80a8Spooka if (dp->dv_name)
1215f7e80a8Spooka printf(" %s", dp->dv_name);
1225f7e80a8Spooka printf("\n");
123*95f49f93Schristos return ENXIO;
1245f7e80a8Spooka
1255f7e80a8Spooka fnd:
1265f7e80a8Spooka #ifdef BOOTNET
1275f7e80a8Spooka if (strcmp(device_name, "tftp") == 0)
1285f7e80a8Spooka rc = (dp->dv_open)(f, device_name);
1295f7e80a8Spooka else
1305f7e80a8Spooka #endif /* BOOTNET */
1315f7e80a8Spooka rc = (dp->dv_open)(f, ctlr, unit, part, cp);
1325f7e80a8Spooka #endif /* !LIBSA_SINGLE_DEVICE */
1335f7e80a8Spooka if (rc)
134*95f49f93Schristos return rc;
1355f7e80a8Spooka
1365f7e80a8Spooka #ifndef LIBSA_SINGLE_DEVICE
1375f7e80a8Spooka f->f_dev = dp;
1385f7e80a8Spooka #endif
1395f7e80a8Spooka if (file && *cp != '\0')
1405f7e80a8Spooka *file = (char *)cp; /* XXX */
141*95f49f93Schristos return 0;
1425f7e80a8Spooka }
143