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