1*62cf489dScegger /* $NetBSD: devopen.c,v 1.9 2009/10/26 19:16:56 cegger Exp $ */
28bb2be29Sthorpej
38bb2be29Sthorpej /*
477fa5a79Sthorpej * Copyright 2001, 2002 Wasabi Systems, Inc.
58bb2be29Sthorpej * All rights reserved.
68bb2be29Sthorpej *
78bb2be29Sthorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
88bb2be29Sthorpej *
98bb2be29Sthorpej * Redistribution and use in source and binary forms, with or without
108bb2be29Sthorpej * modification, are permitted provided that the following conditions
118bb2be29Sthorpej * are met:
128bb2be29Sthorpej * 1. Redistributions of source code must retain the above copyright
138bb2be29Sthorpej * notice, this list of conditions and the following disclaimer.
148bb2be29Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
158bb2be29Sthorpej * notice, this list of conditions and the following disclaimer in the
168bb2be29Sthorpej * documentation and/or other materials provided with the distribution.
178bb2be29Sthorpej * 3. All advertising materials mentioning features or use of this software
188bb2be29Sthorpej * must display the following acknowledgement:
198bb2be29Sthorpej * This product includes software developed for the NetBSD Project by
208bb2be29Sthorpej * Wasabi Systems, Inc.
218bb2be29Sthorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
228bb2be29Sthorpej * or promote products derived from this software without specific prior
238bb2be29Sthorpej * written permission.
248bb2be29Sthorpej *
258bb2be29Sthorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
268bb2be29Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
278bb2be29Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
288bb2be29Sthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
298bb2be29Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
308bb2be29Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
318bb2be29Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
328bb2be29Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
338bb2be29Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
348bb2be29Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
358bb2be29Sthorpej * POSSIBILITY OF SUCH DAMAGE.
368bb2be29Sthorpej */
378bb2be29Sthorpej
388bb2be29Sthorpej #include <sys/param.h>
39e63501d2Sjunyoung #include <lib/libsa/stand.h>
40a82c138bSdrochner #include <netinet/in.h>
41a82c138bSdrochner #include <netinet/in_systm.h>
42a82c138bSdrochner #include <net.h>
438bb2be29Sthorpej
448bb2be29Sthorpej #include <libi386.h>
458bb2be29Sthorpej #ifdef _STANDALONE
468bb2be29Sthorpej #include <lib/libkern/libkern.h>
478bb2be29Sthorpej #include <bootinfo.h>
488bb2be29Sthorpej #else
498bb2be29Sthorpej #include <string.h>
508bb2be29Sthorpej #endif
518bb2be29Sthorpej
528bb2be29Sthorpej #include "pxeboot.h"
538bb2be29Sthorpej
548bb2be29Sthorpej #ifdef _STANDALONE
558bb2be29Sthorpej struct btinfo_bootpath bibp;
568bb2be29Sthorpej #endif
578bb2be29Sthorpej
588bb2be29Sthorpej /*
598bb2be29Sthorpej * Open the "device" named by the combined file system/file name
608bb2be29Sthorpej * given as the fname arg. Format is:
618bb2be29Sthorpej *
628bb2be29Sthorpej * nfs:netbsd
638bb2be29Sthorpej * tftp:netbsd
648bb2be29Sthorpej * netbsd
658bb2be29Sthorpej *
668bb2be29Sthorpej * If no file system is specified, we default to the first in the
678bb2be29Sthorpej * file system table (which ought to be NFS).
688bb2be29Sthorpej *
698bb2be29Sthorpej * We always open just one device (the PXE netif).
708bb2be29Sthorpej */
718bb2be29Sthorpej int
devopen(struct open_file * f,const char * fname,char ** file)728bb2be29Sthorpej devopen(struct open_file *f, const char *fname, char **file)
738bb2be29Sthorpej {
748bb2be29Sthorpej struct devsw *dp;
758bb2be29Sthorpej char *filename;
768bb2be29Sthorpej size_t fsnamelen;
7777fa5a79Sthorpej int i, error;
788bb2be29Sthorpej
7977fa5a79Sthorpej dp = &devsw[0];
8077fa5a79Sthorpej
8177fa5a79Sthorpej /* Set the default boot file system. */
82e2cb8590Scegger memcpy(file_system, pxeboot_fstab[0].fst_ops, sizeof(struct fs_ops));
838bb2be29Sthorpej
84a82c138bSdrochner /* if we got passed a filename, pass it to the BOOTP server */
85a82c138bSdrochner if (fname)
86a82c138bSdrochner strncpy(bootfile, fname, FNAME_SIZE);
87a82c138bSdrochner
8877fa5a79Sthorpej /* Open the device; this might give us a boot file name. */
8977fa5a79Sthorpej error = (*dp->dv_open)(f, NULL);
9077fa5a79Sthorpej if (error)
9177fa5a79Sthorpej return (error);
9277fa5a79Sthorpej
9377fa5a79Sthorpej f->f_dev = dp;
9477fa5a79Sthorpej
95038b8b65Sdrochner /*
96038b8b65Sdrochner * If the DHCP server provided a file name:
97038b8b65Sdrochner * - If it contains a ":", assume it points to a NetBSD kernel.
98038b8b65Sdrochner * - If not, assume that the DHCP server was not able to pass
99038b8b65Sdrochner * a separate filename for the kernel. (The name probably
100038b8b65Sdrochner * was the same as used to load "pxeboot".) Ignore it and
101038b8b65Sdrochner * use the default in this case.
102038b8b65Sdrochner * So we cater to simple DHCP servers while being able to
103038b8b65Sdrochner * use the power of conditional behaviour in modern ones.
104038b8b65Sdrochner */
105038b8b65Sdrochner if (strchr(bootfile, ':'))
10677fa5a79Sthorpej fname = bootfile;
10777fa5a79Sthorpej
108038b8b65Sdrochner filename = (fname ? strchr(fname, ':') : NULL);
1098bb2be29Sthorpej if (filename != NULL) {
1108bb2be29Sthorpej fsnamelen = (size_t)((const char *)filename - fname);
1118bb2be29Sthorpej for (i = 0; i < npxeboot_fstab; i++) {
1128bb2be29Sthorpej if (strncmp(fname, pxeboot_fstab[i].fst_name,
1138bb2be29Sthorpej fsnamelen) == 0) {
114e2cb8590Scegger memcpy(file_system, pxeboot_fstab[i].fst_ops,
1158bb2be29Sthorpej sizeof(struct fs_ops));
1168bb2be29Sthorpej break;
1178bb2be29Sthorpej }
1188bb2be29Sthorpej }
1198bb2be29Sthorpej if (i == npxeboot_fstab) {
1208bb2be29Sthorpej printf("Invalid file system type specified in %s\n",
1218bb2be29Sthorpej fname);
12277fa5a79Sthorpej error = EINVAL;
12377fa5a79Sthorpej goto bad;
1248bb2be29Sthorpej }
1258bb2be29Sthorpej filename++;
1268bb2be29Sthorpej if (filename[0] == '\0') {
1278bb2be29Sthorpej printf("No file specified in %s\n", fname);
12877fa5a79Sthorpej error = EINVAL;
12977fa5a79Sthorpej goto bad;
1308bb2be29Sthorpej }
1318bb2be29Sthorpej } else
1328bb2be29Sthorpej filename = (char *)fname;
1338bb2be29Sthorpej
1348bb2be29Sthorpej *file = filename;
1358bb2be29Sthorpej
1368bb2be29Sthorpej #ifdef _STANDALONE
1378bb2be29Sthorpej strncpy(bibp.bootpath, filename, sizeof(bibp.bootpath));
1388bb2be29Sthorpej BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
1398bb2be29Sthorpej #endif
1408bb2be29Sthorpej
14177fa5a79Sthorpej return (0);
14277fa5a79Sthorpej bad:
14377fa5a79Sthorpej (*dp->dv_close)(f);
14477fa5a79Sthorpej f->f_dev = NULL;
14577fa5a79Sthorpej return (error);
1468bb2be29Sthorpej }
147