1 /* $NetBSD: devopen.c,v 1.1 2011/01/23 01:05:30 nisimura Exp $ */
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Tohru Nishimura.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 
34 #include <netinet/in.h>
35 
36 #include <lib/libsa/stand.h>
37 #include <lib/libsa/nfs.h>
38 #include <lib/libsa/ufs.h>
39 #include <lib/libsa/tftp.h>
40 #include <lib/libkern/libkern.h>
41 
42 #include "globals.h"
43 
44 struct devsw devnet = { "net", net_strategy, net_open, net_close, noioctl };
45 struct devsw devdsk = { "dsk", dsk_strategy, dsk_open, dsk_close, noioctl };
46 
47 struct fs_ops file_system[1] = { FS_OPS(null) };
48 int nfsys = 1;
49 struct fs_ops fs_nfs   = FS_OPS(nfs);
50 struct fs_ops fs_tftp  = FS_OPS(tftp);
51 struct fs_ops fs_ffsv2 = FS_OPS(ffsv2);
52 struct fs_ops fs_ffsv1 = FS_OPS(ffsv1);
53 extern char *fsmod;
54 
55 static void parseunit(const char *, int *, int *, char **);
56 
57 int
58 devopen(struct open_file *of, const char *name, char **file)
59 {
60 	int error;
61 	int unit, part;
62 	extern char bootfile[]; /* handed by DHCP */
63 
64 	if (of->f_flags != F_READ)
65 		return EPERM;
66 
67 	if (strncmp("net:", name, 4) == 0 || strncmp("nfs:", name, 4) == 0) {
68 		of->f_dev = &devnet;
69 		if ((error = net_open(of, &name[4], "nfs")) != 0)
70 			return error;
71 		file_system[0] = fs_nfs;
72 		*file = bootfile;	/* resolved fname */
73 		return 0;		/* NFS */
74 	}
75 	if (strncmp("tftp:", name, 5) == 0) {
76 		of->f_dev = &devnet;
77 		if ((error = net_open(of, &name[5], "tftp")) != 0)
78 			return error;
79 		file_system[0] = fs_tftp;
80 		*file = bootfile;	/* resolved fname */
81 		return 0;		/* TFTP */
82 	}
83 	if (name[0] == 'w' && name[1] == 'd') {
84 		parseunit(&name[2], &unit, &part, file);
85 		of->f_dev = &devdsk;
86 		if (*file == NULL || **file <= ' ')
87 			*file = "netbsd";
88 		if ((error = dsk_open(of, unit, part, *file)) != 0)
89 			return error;
90 		file_system[0] = *dsk_fsops(of);
91 		return 0;		/* FFS */
92 	}
93 	return ENOENT;
94 }
95 
96 static void
97 parseunit(const char *name, int *unitp, int *partp, char **pathp)
98 {
99 	const char *p = name;
100 	int unit, part;
101 
102 	unit = part = -1;
103 	while (*p != ':' && *p != '\0') {
104 		if (unit == -1 && *p >= '0' && *p <= '9')
105 			unit = *p - '0';
106 		if (part == -1 && *p >= 'a' && *p < 'a' + 16)
107 			part = *p - 'a';
108 		p += 1;
109 	}
110 	*unitp = (unit == -1) ? 0 : unit;
111 	*partp = (part == -1) ? 0 : part;
112 	*pathp = (*p == ':') ? (char *)p + 1 : NULL;
113 }
114 
115 /* ARGSUSED */
116 int
117 noioctl(struct open_file *f, u_long cmd, void *data)
118 {
119 
120 	return EINVAL;
121 }
122