Home | History | Annotate | Line # | Download | only in pxeboot
dev_net.c revision 1.7
      1 /*	$NetBSD: dev_net.c,v 1.7 2008/05/11 11:42:02 chris Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gordon W. Ross.
      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 /*
     33  * This module implements a "raw device" interface suitable for
     34  * use by the stand-alone I/O library NFS and TFTP code.  This interface
     35  * does not support any "block" access, and exists only for the
     36  * purpose of initializing the network interface and getting boot
     37  * parameters.
     38  *
     39  * At open time, this does:
     40  *
     41  * find interface       - netif_open()
     42  * BOOTP for IP address - bootp()
     43  */
     44 
     45 #include <machine/stdarg.h>
     46 #include <sys/param.h>
     47 #include <sys/socket.h>
     48 #include <net/if.h>
     49 #include <netinet/in.h>
     50 #include <netinet/in_systm.h>
     51 
     52 #include <lib/libkern/libkern.h>
     53 
     54 #include <lib/libsa/stand.h>
     55 #include <lib/libsa/net.h>
     56 #include "pxe_netif.h"
     57 #include "dev_net.h"
     58 
     59 static int netdev_sock = -1;
     60 static int netdev_opens;
     61 
     62 static int net_getparams __P((int sock));
     63 
     64 /*
     65  * Called by devopen after it sets f->f_dev to our devsw entry.
     66  * This opens the low-level device and sets f->f_devdata.
     67  * This is declared with variable arguments...
     68  */
     69 int
     70 net_open(struct open_file *f, ...)
     71 {
     72 	int error = 0;
     73 
     74 #ifdef	NETIF_DEBUG
     75 	if (debug)
     76 		printf("net_open\n");
     77 #endif
     78 
     79 	/* On first open, do netif open, mount, etc. */
     80 	if (netdev_opens == 0) {
     81 		/* Find network interface. */
     82 		if (netdev_sock < 0) {
     83 			netdev_sock = pxe_netif_open();
     84 			if (netdev_sock < 0) {
     85 				printf("net_open: netif_open() failed\n");
     86 				return (ENXIO);
     87 			}
     88 			if (debug)
     89 				printf("net_open: netif_open() succeeded\n");
     90 		}
     91 		if (rootip.s_addr == 0) {
     92 			/* Get root IP address, and path, etc. */
     93 			error = net_getparams(netdev_sock);
     94 			if (error) {
     95 				/* getparams makes its own noise */
     96 				pxe_netif_close(netdev_sock);
     97 				netdev_sock = -1;
     98 				return (error);
     99 			}
    100 		}
    101 	}
    102 	netdev_opens++;
    103 	f->f_devdata = &netdev_sock;
    104 	return (error);
    105 }
    106 
    107 int
    108 net_close(f)
    109 	struct open_file *f;
    110 {
    111 
    112 #ifdef	NETIF_DEBUG
    113 	if (debug)
    114 		printf("net_close: opens=%d\n", netdev_opens);
    115 #endif
    116 
    117 	/* On last close, do netif close, etc. */
    118 	f->f_devdata = NULL;
    119 	/* Extra close call? */
    120 	if (netdev_opens <= 0)
    121 		return (0);
    122 	netdev_opens--;
    123 	/* Not last close? */
    124 	if (netdev_opens > 0)
    125 		return(0);
    126 	rootip.s_addr = 0;
    127 	if (netdev_sock >= 0) {
    128 		if (debug)
    129 			printf("net_close: calling netif_close()\n");
    130 		pxe_netif_close(netdev_sock);
    131 		//pxe_netif_shutdown(); /* XXX shouldn't be done here */
    132 		netdev_sock = -1;
    133 	}
    134 	return (0);
    135 }
    136 
    137 int
    138 net_ioctl(f, cmd, data)
    139 	struct open_file *f;
    140 	u_long cmd;
    141 	void *data;
    142 {
    143 	return EIO;
    144 }
    145 
    146 int
    147 net_strategy(devdata, rw, blk, size, buf, rsize)
    148 	void *devdata;
    149 	int rw;
    150 	daddr_t blk;
    151 	size_t size;
    152 	void *buf;
    153 	size_t *rsize;
    154 {
    155 	return EIO;
    156 }
    157 
    158 
    159 /*
    160  * Get info for network boot: our IP address, our hostname,
    161  * server IP address, and our root path on the server.
    162  */
    163 #ifdef	SUPPORT_BOOTP
    164 int bootp __P((int sock));
    165 #endif
    166 
    167 static int
    168 net_getparams(sock)
    169 	int sock;
    170 {
    171 
    172 #ifdef	SUPPORT_BOOTP
    173 	/*
    174 	 * Try to get boot info using BOOTP.  If we succeed, then
    175 	 * the server IP address, gateway, and root path will all
    176 	 * be initialized.  If any remain uninitialized, we will
    177 	 * use RARP and RPC/bootparam (the Sun way) to get them.
    178 	 */
    179 	bootp(sock);
    180 	if (myip.s_addr != 0)
    181 		return (0);
    182 	if (debug)
    183 		printf("net_open: BOOTP failed\n");
    184 #endif
    185 
    186 	return (EIO);
    187 }
    188