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