xref: /netbsd-src/sys/arch/ews4800mips/stand/common/devopen.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: devopen.c,v 1.3 2007/10/27 12:26:20 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <lib/libsa/stand.h>
40 #include <lib/libkern/libkern.h>
41 
42 #include <netinet/in.h>
43 #include <lib/libsa/dev_net.h>
44 #include <lib/libsa/ufs.h>
45 #include <lib/libsa/nfs.h>
46 #include <lib/libsa/dev_net.h>
47 #include <machine/sbd.h>
48 
49 #include "local.h"
50 
51 extern uint8_t kernel_binary[];
52 extern int kernel_binary_size;
53 
54 extern struct fs_ops datafs_ops;
55 extern struct fs_ops bfs_ops;
56 struct fs_ops ufs_ops = {
57 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
58 };
59 struct fs_ops nfs_ops = {
60 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
61 };
62 
63 extern struct devsw netdevsw;
64 extern struct devsw dkdevsw;
65 char fname[16];
66 
67 /* Referenced by libsa/open.c */
68 struct fs_ops file_system[1];
69 int nfsys = 1;
70 struct devsw devsw[1];
71 int ndevs = 1;
72 
73 int
74 devopen(struct open_file *f, const char *request, char **file)
75 {
76 	char *p, *filename;
77 	int disk, partition;
78 	void *addr;
79 	size_t size;
80 
81 	strcpy(fname, request);
82 
83 	filename = 0;
84 	for (p = fname; *p; p++) {
85 		if (*p == ':') {
86 			filename = p + 1;
87 			*p = '\0';
88 			break;
89 		}
90 	}
91 
92 	if (filename == 0) {	/* not a loader's request. probably ufs_ls() */
93 		printf("request=%s\n", request);
94 		f->f_dev = &dkdevsw;
95 		file_system[0] = ufs_ops;
96 		devsw[0] = dkdevsw;
97 		*file = "/";
98 		return 0;
99 	}
100 
101 	/* Data section */
102 	if (strcmp(fname, "mem") == 0) {
103 		data_attach(kernel_binary, kernel_binary_size);
104 		*file = "noname";
105 		f->f_flags |= F_NODEV;
106 		file_system[0] = datafs_ops;
107 		printf("data(compiled):noname\n");
108 		return 0;
109 	}
110 
111 	/* NFS boot */
112 	if (strcmp(fname, "nfs") == 0) {
113 		if (!DEVICE_CAPABILITY.network_enabled) {
114 			printf("Network disabled.\n");
115 			return -1;
116 		}
117 		try_bootp = true;
118 		file_system[0] = nfs_ops;
119 		f->f_dev = &netdevsw;
120 		if (*filename == '\0') {
121 			printf("set kernel filename. ex.) nfs:netbsd\n");
122 			return 1;
123 		}
124 		*--filename = '/';
125 		*file = filename;
126 		printf("nfs:/%s\n", filename);
127 		net_open(f);
128 		return 0;
129 	}
130 
131 	/* FD boot */
132 	if (strcmp(fname, "fd") == 0) {
133 		printf("floppy(boot):/%s (ustarfs)\n", filename);
134 		f->f_dev = &dkdevsw;
135 		file_system[0] = datafs_ops;
136 		devsw[0] = dkdevsw;
137 		device_attach(NVSRAM_BOOTDEV_FLOPPYDISK, -1, -1);
138 		if (!ustarfs_load(filename, &addr, &size))
139 			return -1;
140 		data_attach(addr, size);
141 		*file = filename;
142 		return 0;
143 	}
144 
145 	/* Disk boot */
146 	if (strncmp(fname, "sd", 2) == 0) {
147 		enum fstype fs;
148 		if (!DEVICE_CAPABILITY.disk_enabled) {
149 			printf("Disk disabled.\n");
150 			return -1;
151 		}
152 
153 		disk = fname[2] - '0';
154 		partition = fname[3] - 'a';
155 		if (disk < 0 || disk > 9 || partition < 0 || partition > 15) {
156 			fs = FSTYPE_USTARFS;
157 			printf("disk(boot):%s ", filename);
158 			device_attach(NVSRAM_BOOTDEV_HARDDISK, -1, -1);
159 		} else {
160 			fs = fstype(partition);
161 			printf("disk(%d,%d):/%s ",
162 			    disk, partition, filename);
163 			device_attach(NVSRAM_BOOTDEV_HARDDISK, disk, partition);
164 		}
165 
166 		switch (fs) {
167 		case FSTYPE_UFS:
168 			printf(" (ufs)\n");
169 			f->f_dev = &dkdevsw;
170 			file_system[0] = ufs_ops;
171 			devsw[0] = dkdevsw;
172 			break;
173 		case FSTYPE_BFS:
174 			printf(" (bfs)\n");
175 			f->f_flags |= F_NODEV;
176 			file_system[0] = bfs_ops;
177 			break;
178 		case FSTYPE_USTARFS:
179 			printf(" (ustarfs)\n");
180 			f->f_dev = &dkdevsw;
181 			file_system[0] = datafs_ops;
182 			devsw[0] = dkdevsw;
183 			if (!ustarfs_load(filename, &addr, &size))
184 				return -1;
185 			data_attach(addr, size);
186 			break;
187 		}
188 		*file = filename;
189 		return 0;
190 	}
191 
192 	printf("%s invalid.\n", fname);
193 
194 	return -1;
195 }
196