1 /* $NetBSD: dev_net.c,v 1.1 2011/01/23 01:05:30 nisimura 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 #include <netinet/in_systm.h> 36 37 #include <lib/libsa/stand.h> 38 #include <lib/libsa/net.h> 39 #include <lib/libsa/bootp.h> 40 #include <lib/libsa/nfs.h> 41 #include <lib/libkern/libkern.h> 42 43 #include <machine/bootinfo.h> 44 #include <machine/stdarg.h> 45 46 #include "globals.h" 47 48 static int netdev_sock = -1; 49 static int netdev_opens; 50 51 int 52 net_open(struct open_file *f, ...) 53 54 { 55 va_list ap; 56 char *file, *proto; 57 int error; 58 extern struct btinfo_bootpath bi_path; 59 60 va_start(ap, f); 61 file = va_arg(ap, char *); 62 proto = va_arg(ap, char *); 63 va_end(ap); 64 65 if (netdev_opens > 0) 66 return 0; 67 68 if ((netdev_sock = netif_open(NULL)) < 0) 69 return ENXIO; /* never fails indeed */ 70 71 error = 0; 72 bootp(netdev_sock); /* send DHCP request */ 73 if (myip.s_addr == 0) { 74 error = ENOENT; /* IP address was not found */ 75 goto bad; 76 } 77 78 if (file[0] != '\0') 79 snprintf(bootfile, sizeof(bootfile), file); 80 else if (bootfile[0] == '\0') 81 snprintf(bootfile, sizeof(bootfile), "netbsd"); 82 83 if (strcmp(proto, "nfs") == 0 84 && (error = nfs_mount(netdev_sock, rootip, rootpath)) != 0) 85 goto bad; 86 87 snprintf(bi_path.bootpath, sizeof(bi_path.bootpath), bootfile); 88 f->f_devdata = &netdev_sock; 89 netdev_opens++; 90 return 0; 91 bad: 92 netif_close(netdev_sock); 93 netdev_sock = -1; 94 return error; 95 } 96 97 int 98 net_close(struct open_file *f) 99 100 { 101 f->f_devdata = NULL; 102 if (--netdev_opens > 0) 103 return 0; 104 netif_close(netdev_sock); 105 netdev_sock = -1; 106 return 0; 107 } 108 109 int 110 net_strategy(void *devdata, int rw, daddr_t dblk, size_t size, 111 void *p, size_t *rsize) 112 { 113 114 return EIO; 115 } 116