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