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