Home | History | Annotate | Line # | Download | only in altboot
      1 /* $NetBSD: devopen.c,v 1.3 2014/01/05 21:09:33 jakllsch Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 
     34 #include <netinet/in.h>
     35 
     36 #include <lib/libsa/stand.h>
     37 #include <lib/libsa/nfs.h>
     38 #include <lib/libsa/ufs.h>
     39 #include <lib/libsa/tftp.h>
     40 #include <lib/libkern/libkern.h>
     41 
     42 #include "globals.h"
     43 #include "memfs.h"
     44 
     45 struct devsw devnet = { "net", net_strategy, net_open, net_close, noioctl };
     46 struct devsw devdsk = { "dsk", dsk_strategy, dsk_open, dsk_close, noioctl };
     47 
     48 struct fs_ops file_system[1] = { FS_OPS(null) };
     49 int nfsys = 1;
     50 struct fs_ops fs_nfs   = FS_OPS(nfs);
     51 struct fs_ops fs_tftp  = FS_OPS(tftp);
     52 struct fs_ops fs_ffsv2 = FS_OPS(ffsv2);
     53 struct fs_ops fs_ffsv1 = FS_OPS(ffsv1);
     54 struct fs_ops fs_mem   = FS_OPS(mem);
     55 
     56 static void parseunit(const char *, int *, int *, char **);
     57 
     58 int
     59 devopen(struct open_file *of, const char *name, char **file)
     60 {
     61 	int error;
     62 	int unit, part;
     63 	extern char bootfile[]; /* handed by DHCP */
     64 
     65 	if (of->f_flags != F_READ)
     66 		return EPERM;
     67 
     68 	if (strncmp("mem:", name, 4) == 0) {
     69 		of->f_dev = NULL;
     70 		of->f_flags |= F_NODEV;
     71 		file_system[0] = fs_mem;
     72 		*file = (char *)&name[4];
     73 		return 0;		/* MEM */
     74 	}
     75 	if (strncmp("net:", name, 4) == 0 || strncmp("nfs:", name, 4) == 0) {
     76 		of->f_dev = &devnet;
     77 		if ((error = net_open(of, &name[4], "nfs")) != 0)
     78 			return error;
     79 		file_system[0] = fs_nfs;
     80 		*file = bootfile;	/* resolved fname */
     81 		return 0;		/* NFS */
     82 	}
     83 	if (strncmp("tftp:", name, 5) == 0) {
     84 		of->f_dev = &devnet;
     85 		if ((error = net_open(of, &name[5], "tftp")) != 0)
     86 			return error;
     87 		file_system[0] = fs_tftp;
     88 		*file = bootfile;	/* resolved fname */
     89 		return 0;		/* TFTP */
     90 	}
     91 	if (name[0] == 'w' && name[1] == 'd') {
     92 		parseunit(&name[2], &unit, &part, file);
     93 		of->f_dev = &devdsk;
     94 		if (*file == NULL || **file <= ' ')
     95 			*file = "netbsd";
     96 		if ((error = dsk_open(of, unit, part, *file)) != 0)
     97 			return error;
     98 		file_system[0] = *dsk_fsops(of);
     99 		return 0;		/* FFS */
    100 	}
    101 	return ENOENT;
    102 }
    103 
    104 static void
    105 parseunit(const char *name, int *unitp, int *partp, char **pathp)
    106 {
    107 	const char *p = name;
    108 	int unit, part;
    109 
    110 	unit = part = -1;
    111 	while (*p != ':' && *p != '\0') {
    112 		if (unit == -1 && *p >= '0' && *p <= '9')
    113 			unit = *p - '0';
    114 		if (part == -1 && *p >= 'a' && *p < 'a' + 16)
    115 			part = *p - 'a';
    116 		p += 1;
    117 	}
    118 	*unitp = (unit == -1) ? 0 : unit;
    119 	*partp = (part == -1) ? 0 : part;
    120 	*pathp = (*p == ':') ? (char *)p + 1 : NULL;
    121 }
    122 
    123 /* ARGSUSED */
    124 int
    125 noioctl(struct open_file *f, u_long cmd, void *data)
    126 {
    127 
    128 	return EINVAL;
    129 }
    130