1 /* $NetBSD: dev_net.c,v 1.9 2009/03/14 15:36:08 dsl Exp $ */
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
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
39 * At open time, this does:
41 * find interface - netif_open()
42 * BOOTP for IP address - bootp()
45 #include <machine/stdarg.h>
46 #include <sys/param.h>
47 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
52 #include <lib/libkern/libkern.h>
54 #include <lib/libsa/stand.h>
55 #include <lib/libsa/net.h>
56 #include "pxe_netif.h"
59 static int netdev_sock
= -1;
60 static int netdev_opens
;
62 static int net_getparams(int sock
);
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...
70 net_open(struct open_file
*f
, ...)
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");
89 printf("net_open: netif_open() succeeded\n");
91 if (rootip
.s_addr
== 0) {
92 /* Get root IP address, and path, etc. */
93 error
= net_getparams(netdev_sock
);
95 /* getparams makes its own noise */
96 pxe_netif_close(netdev_sock
);
103 f
->f_devdata
= &netdev_sock
;
108 net_close(struct open_file
*f
)
113 printf("net_close: opens=%d\n", netdev_opens
);
116 /* On last close, do netif close, etc. */
118 /* Extra close call? */
119 if (netdev_opens
<= 0)
122 /* Not last close? */
123 if (netdev_opens
> 0)
126 if (netdev_sock
>= 0) {
128 printf("net_close: calling netif_close()\n");
129 pxe_netif_close(netdev_sock
);
136 net_ioctl(struct open_file
*f
, u_long cmd
, void *data
)
142 net_strategy(void *devdata
, int rw
, daddr_t blk
, size_t size
, void *buf
, size_t *rsize
)
149 * Get info for network boot: our IP address, our hostname,
150 * server IP address, and our root path on the server.
157 net_getparams(int sock
)
162 * Try to get boot info using BOOTP. If we succeed, then
163 * the server IP address, gateway, and root path will all
164 * be initialized. If any remain uninitialized, we will
165 * use RARP and RPC/bootparam (the Sun way) to get them.
168 if (myip
.s_addr
!= 0)
171 printf("net_open: BOOTP failed\n");