xref: /netbsd-src/sys/arch/ews4800mips/stand/common/devopen.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: devopen.c,v 1.1 2005/12/29 15:20:09 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 <machine/sbd.h>
47 
48 #include "local.h"
49 
50 extern uint8_t kernel_binary[];
51 extern int kernel_binary_size;
52 
53 extern struct fs_ops datafs_ops;
54 extern struct fs_ops bfs_ops;
55 struct fs_ops ufs_ops = {
56 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
57 };
58 struct fs_ops nfs_ops = {
59 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
60 };
61 
62 extern struct devsw netdevsw;
63 extern struct devsw dkdevsw;
64 char fname[16];
65 
66 /* Referenced by libsa/open.c */
67 struct fs_ops file_system[1];
68 int nfsys = 1;
69 struct devsw devsw[1];
70 int ndevs = 1;
71 
72 int
73 devopen(struct open_file *f, const char *request, char **file)
74 {
75 	char *p, *filename;
76 	int disk, partition;
77 	void *addr;
78 	size_t size;
79 
80 	strcpy(fname, request);
81 
82 	filename = 0;
83 	for (p = fname; *p; p++) {
84 		if (*p == ':') {
85 			filename = p + 1;
86 			*p = '\0';
87 			break;
88 		}
89 	}
90 
91 	if (filename == 0) {	/* not a loader's request. probably ufs_ls() */
92 		printf("request=%s\n", request);
93 		f->f_dev = &dkdevsw;
94 		file_system[0] = ufs_ops;
95 		devsw[0] = dkdevsw;
96 		*file = "/";
97 		return 0;
98 	}
99 
100 	/* Data section */
101 	if (strcmp(fname, "mem") == 0) {
102 		data_attach(kernel_binary, kernel_binary_size);
103 		*file = "noname";
104 		f->f_flags |= F_NODEV;
105 		file_system[0] = datafs_ops;
106 		printf("data(compiled):noname\n");
107 		return 0;
108 	}
109 
110 	/* NFS boot */
111 	if (strcmp(fname, "nfs") == 0) {
112 		extern int try_bootp;
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