devopen.c revision 1.9 1 1.9 cegger /* $NetBSD: devopen.c,v 1.9 2009/10/26 19:16:56 cegger Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.2 thorpej * Copyright 2001, 2002 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.1 thorpej #include <sys/param.h>
39 1.6 junyoung #include <lib/libsa/stand.h>
40 1.3 drochner #include <netinet/in.h>
41 1.3 drochner #include <netinet/in_systm.h>
42 1.3 drochner #include <net.h>
43 1.1 thorpej
44 1.1 thorpej #include <libi386.h>
45 1.1 thorpej #ifdef _STANDALONE
46 1.1 thorpej #include <lib/libkern/libkern.h>
47 1.1 thorpej #include <bootinfo.h>
48 1.1 thorpej #else
49 1.1 thorpej #include <string.h>
50 1.1 thorpej #endif
51 1.1 thorpej
52 1.1 thorpej #include "pxeboot.h"
53 1.1 thorpej
54 1.1 thorpej #ifdef _STANDALONE
55 1.1 thorpej struct btinfo_bootpath bibp;
56 1.1 thorpej #endif
57 1.1 thorpej
58 1.1 thorpej /*
59 1.1 thorpej * Open the "device" named by the combined file system/file name
60 1.1 thorpej * given as the fname arg. Format is:
61 1.1 thorpej *
62 1.1 thorpej * nfs:netbsd
63 1.1 thorpej * tftp:netbsd
64 1.1 thorpej * netbsd
65 1.1 thorpej *
66 1.1 thorpej * If no file system is specified, we default to the first in the
67 1.1 thorpej * file system table (which ought to be NFS).
68 1.1 thorpej *
69 1.1 thorpej * We always open just one device (the PXE netif).
70 1.1 thorpej */
71 1.1 thorpej int
72 1.1 thorpej devopen(struct open_file *f, const char *fname, char **file)
73 1.1 thorpej {
74 1.1 thorpej struct devsw *dp;
75 1.1 thorpej char *filename;
76 1.1 thorpej size_t fsnamelen;
77 1.2 thorpej int i, error;
78 1.1 thorpej
79 1.2 thorpej dp = &devsw[0];
80 1.2 thorpej
81 1.2 thorpej /* Set the default boot file system. */
82 1.9 cegger memcpy(file_system, pxeboot_fstab[0].fst_ops, sizeof(struct fs_ops));
83 1.3 drochner
84 1.3 drochner /* if we got passed a filename, pass it to the BOOTP server */
85 1.3 drochner if (fname)
86 1.3 drochner strncpy(bootfile, fname, FNAME_SIZE);
87 1.1 thorpej
88 1.2 thorpej /* Open the device; this might give us a boot file name. */
89 1.2 thorpej error = (*dp->dv_open)(f, NULL);
90 1.2 thorpej if (error)
91 1.2 thorpej return (error);
92 1.2 thorpej
93 1.2 thorpej f->f_dev = dp;
94 1.2 thorpej
95 1.5 drochner /*
96 1.5 drochner * If the DHCP server provided a file name:
97 1.5 drochner * - If it contains a ":", assume it points to a NetBSD kernel.
98 1.5 drochner * - If not, assume that the DHCP server was not able to pass
99 1.5 drochner * a separate filename for the kernel. (The name probably
100 1.5 drochner * was the same as used to load "pxeboot".) Ignore it and
101 1.5 drochner * use the default in this case.
102 1.5 drochner * So we cater to simple DHCP servers while being able to
103 1.5 drochner * use the power of conditional behaviour in modern ones.
104 1.5 drochner */
105 1.5 drochner if (strchr(bootfile, ':'))
106 1.2 thorpej fname = bootfile;
107 1.2 thorpej
108 1.5 drochner filename = (fname ? strchr(fname, ':') : NULL);
109 1.1 thorpej if (filename != NULL) {
110 1.1 thorpej fsnamelen = (size_t)((const char *)filename - fname);
111 1.1 thorpej for (i = 0; i < npxeboot_fstab; i++) {
112 1.1 thorpej if (strncmp(fname, pxeboot_fstab[i].fst_name,
113 1.1 thorpej fsnamelen) == 0) {
114 1.9 cegger memcpy(file_system, pxeboot_fstab[i].fst_ops,
115 1.1 thorpej sizeof(struct fs_ops));
116 1.1 thorpej break;
117 1.1 thorpej }
118 1.1 thorpej }
119 1.1 thorpej if (i == npxeboot_fstab) {
120 1.1 thorpej printf("Invalid file system type specified in %s\n",
121 1.1 thorpej fname);
122 1.2 thorpej error = EINVAL;
123 1.2 thorpej goto bad;
124 1.1 thorpej }
125 1.1 thorpej filename++;
126 1.1 thorpej if (filename[0] == '\0') {
127 1.1 thorpej printf("No file specified in %s\n", fname);
128 1.2 thorpej error = EINVAL;
129 1.2 thorpej goto bad;
130 1.1 thorpej }
131 1.1 thorpej } else
132 1.1 thorpej filename = (char *)fname;
133 1.1 thorpej
134 1.1 thorpej *file = filename;
135 1.1 thorpej
136 1.1 thorpej #ifdef _STANDALONE
137 1.1 thorpej strncpy(bibp.bootpath, filename, sizeof(bibp.bootpath));
138 1.1 thorpej BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
139 1.1 thorpej #endif
140 1.1 thorpej
141 1.2 thorpej return (0);
142 1.2 thorpej bad:
143 1.2 thorpej (*dp->dv_close)(f);
144 1.2 thorpej f->f_dev = NULL;
145 1.2 thorpej return (error);
146 1.1 thorpej }
147