xref: /openbsd-src/sys/arch/macppc/stand/net.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: net.c,v 1.4 2003/10/16 04:30:09 drahn Exp $	*/
2 /*	$NetBSD: net.c,v 1.1 1997/04/16 20:29:18 thorpej Exp $	*/
3 
4 /*
5  * Copyright (C) 1995 Wolfgang Solfrank.
6  * Copyright (C) 1995 TooLs GmbH.
7  * All rights reserved.
8  *
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.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * This module implements a "raw device" interface suitable for
37  * use by the stand-alone I/O library NFS code.  This interface
38  * does not support any "block" access, and exists only for the
39  * purpose of initializing the network interface, getting boot
40  * parameters, and performing the NFS mount.
41  *
42  * At open time, this does:
43  *
44  * find interface	- netif_open()
45  * BOOTP		- bootp()
46  * RPC/mountd		- nfs_mount()
47  *
48  * The root file handle from mountd is saved in a global
49  * for use by the NFS open code (NFS/lookup).
50  *
51  * Note: this is based in part on sys/arch/sparc/stand/net.c
52  */
53 
54 #include <sys/param.h>
55 #include <sys/socket.h>
56 #include <net/if.h>
57 #include <netinet/in.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/in_systm.h>
60 
61 #include <lib/libsa/stand.h>
62 #include <lib/libsa/net.h>
63 #include <lib/libsa/netif.h>
64 
65 char	rootpath[FNAME_SIZE];
66 
67 static	int netdev_sock = -1;
68 static	int open_count;
69 
70 /*
71  * Called by devopen after it sets f->f_dev to our devsw entry.
72  * This opens the low-level device and sets f->f_devdata.
73  */
74 int
75 net_open(struct of_dev *op)
76 {
77 	int error = 0;
78 
79 	/*
80 	 * On first open, do netif open, mount, etc.
81 	 */
82 	if (open_count == 0) {
83 		/* Find network interface. */
84 		if ((netdev_sock = netif_open(op)) < 0) {
85 			error = errno;
86 			goto bad;
87 		}
88 		if ((error = net_mountroot()) != 0)
89 			goto bad;
90 	}
91 	open_count++;
92 bad:
93 	if (netdev_sock >= 0 && open_count == 0) {
94 		netif_close(netdev_sock);
95 		netdev_sock = -1;
96 	}
97 	return error;
98 }
99 
100 int
101 net_close(struct of_dev *op)
102 {
103 	/*
104 	 * On last close, do netif close, etc.
105 	 */
106 	if (open_count > 0)
107 		if (--open_count == 0) {
108 			netif_close(netdev_sock);
109 			netdev_sock = -1;
110 		}
111 }
112 
113 int
114 net_mountroot()
115 {
116 
117 #ifdef	DEBUG
118 	printf("net_mountroot\n");
119 #endif
120 
121 	/*
122 	 * Get info for NFS boot: our IP address, out hostname,
123 	 * server IP address, and our root path on the server.
124 	 * We use BOOTP (RFC951, RFC1532) exclusively as mandated
125 	 * by PowerPC Reference Platform Specification I.4.2
126 	 */
127 
128 	bootp(netdev_sock);
129 
130 	if (myip.s_addr == 0)
131 		return ETIMEDOUT;
132 
133 	printf("Using IP address: %s\n", inet_ntoa(myip));
134 
135 #ifdef	DEBUG
136 	printf("myip: %s (%s)", hostname, inet_ntoa(myip));
137 	if (gateip.s_addr)
138 		printf(", gateip: %s", inet_ntoa(gateip));
139 	if (netmask)
140 		printf(", netmask: %s", intoa(netmask));
141 	printf("\n");
142 #endif
143 	printf("root addr=%s path=%s\n", inet_ntoa(rootip), rootpath);
144 
145 	/*
146 	 * Get the NFS file handle (mount).
147 	 */
148 	if (nfs_mount(netdev_sock, rootip, rootpath) < 0)
149 		return errno;
150 	return 0;
151 }
152