Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / i386 / stand / netboot / dev_net.c
blob32d52215df3b42db608a1bfcdb961672fecf2ef0
1 /* $NetBSD: dev_net.c,v 1.14 2009/03/14 21:04:10 dsl Exp $ */
3 /*
4 * Copyright (c) 1995 Gordon W. Ross
5 * All rights reserved.
6 * Copyright (c) 1996
7 * Matthias Drochner. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * network device for libsa
32 * supports BOOTP, RARP and BOOTPARAM
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <net/if.h>
38 #include <netinet/in.h>
39 #include <netinet/in_systm.h>
41 #include <lib/libsa/stand.h>
42 #include <lib/libsa/net.h>
43 #include <lib/libsa/bootparam.h>
44 #include <machine/stdarg.h>
46 #include <netif/netif_small.h>
48 #include <lib/libkern/libkern.h>
50 #include "dev_net.h"
52 #ifdef SUPPORT_BOOTP
53 void bootp(int);
54 #endif
56 static int netdev_sock = -1;
59 * Called by devopen after it sets f->f_dev to our devsw entry.
60 * This opens the low-level device and sets f->f_devdata.
62 int
63 net_open(struct open_file *f, ...)
65 int error = 0;
67 #ifdef NET_DEBUG
68 if (netdev_sock != -1)
69 panic("net_open");
70 #endif
72 /* Find network interface. */
73 if ((netdev_sock = netif_open()) < 0)
74 return (ENXIO);
76 #ifdef SUPPORT_BOOTP
77 printf("configure network...trying bootp\n");
78 /* Get boot info using BOOTP way. (RFC951, RFC1048) */
79 bootp(netdev_sock);
80 #endif
82 if (myip.s_addr != INADDR_ANY) { /* got bootp reply or
83 * manually set */
85 #ifdef TFTP_HACK
86 int num, i;
87 /* XXX (some) tftp servers don't like leading "/" */
88 for (num = 0; bootfile[num] == '/'; num++);
89 for (i = 0; (bootfile[i] = bootfile[i + num]) != 0; i++);
90 #endif
92 printf("boot: client IP address: %s\n",
93 inet_ntoa(myip));
94 printf("boot: client name: %s\n", hostname);
95 } else {
97 #ifdef SUPPORT_RARP
99 * no answer, Get boot info using RARP and Sun
100 * bootparams.
102 printf("configure network...trying rarp\n");
104 /* Get our IP address. (rarp.c) */
105 if (rarp_getipaddress(netdev_sock)) {
106 error = EIO;
107 goto bad;
109 printf("boot: client IP address: %s\n",
110 inet_ntoa(myip));
112 #ifdef SUPPORT_BOOTPARAM
113 /* Get our hostname, server IP address. */
114 if (!bp_whoami(netdev_sock)) {
115 printf("boot: client name: %s\n", hostname);
117 /* Get the root pathname. */
118 bp_getfile(netdev_sock, "root", &rootip,
119 rootpath);
121 #else
123 * else fallback: use rarp server address
125 #endif
127 #else /* no SUPPORT_RARP */
128 error = EIO;
129 goto bad;
130 #endif
133 printf("boot: server: %s, rootpath: %s, bootfile: %s\n",
134 inet_ntoa(rootip), rootpath, bootfile);
136 f->f_devdata = &netdev_sock;
137 return (error);
139 bad:
140 printf("net_open failed\n");
141 netif_close(netdev_sock);
142 return (error);
146 net_close(struct open_file *f)
148 #ifdef NET_DEBUG
149 if (netdev_sock == -1)
150 panic("net_close");
151 #endif
153 netif_close(netdev_sock);
154 netdev_sock = -1;
156 f->f_devdata = NULL;
158 return (0);
162 net_ioctl(struct open_file *f, u_long c, void *d)
164 return EIO;
168 net_strategy(void *d, int f, daddr_t b, size_t s, void *buf, size_t *r)
170 return EIO;