if_prom.c revision 1.3 1 /* $NetBSD: if_prom.c,v 1.3 1996/10/02 21:06:56 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1993 Adam Glass
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Adam Glass.
18 * 4. The name of the Author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/types.h>
36
37 #include <netinet/in.h>
38 #include <netinet/in_systm.h>
39
40 #include "netif.h"
41 #include "include/prom.h"
42 #include "lib/libkern/libkern.h"
43
44 int prom_probe();
45 int prom_match();
46 void prom_init();
47 int prom_get();
48 int prom_put();
49 void prom_end();
50
51 extern struct netif_stats prom_stats[];
52
53 struct netif_dif prom_ifs[] = {
54 /* dif_unit dif_nsel dif_stats dif_private */
55 { 0, 1, &prom_stats[0], 0, },
56 };
57
58 struct netif_stats prom_stats[NENTS(prom_ifs)];
59
60 struct netif_driver prom_netif_driver = {
61 "prom", /* netif_bname */
62 prom_match, /* netif_match */
63 prom_probe, /* netif_probe */
64 prom_init, /* netif_init */
65 prom_get, /* netif_get */
66 prom_put, /* netif_put */
67 prom_end, /* netif_end */
68 prom_ifs, /* netif_ifs */
69 NENTS(prom_ifs) /* netif_nifs */
70 };
71
72 int netfd;
73
74 int
75 prom_match(nif, machdep_hint)
76 struct netif *nif;
77 void *machdep_hint;
78 {
79
80 return (1);
81 }
82
83 int
84 prom_probe(nif, machdep_hint)
85 struct netif *nif;
86 void *machdep_hint;
87 {
88
89 return 0;
90 }
91
92 int
93 prom_put(desc, pkt, len)
94 struct iodesc *desc;
95 void *pkt;
96 int len;
97 {
98
99 prom_write(netfd, len, pkt, 0);
100
101 return len;
102 }
103
104
105 int
106 prom_get(desc, pkt, len, timeout)
107 struct iodesc *desc;
108 void *pkt;
109 int len;
110 time_t timeout;
111 {
112 prom_return_t ret;
113 time_t t;
114 int cc;
115 char hate[2000];
116
117 t = getsecs();
118 cc = 0;
119 while (((getsecs() - t) < timeout) && !cc) {
120 #if 0 /* TC machines' firmware */
121 ret.bits = prom_read(netfd, 0, hate, 0);
122 #else
123 ret.bits = prom_read(netfd, sizeof hate, hate, 0);
124 #endif
125 if (ret.u.status == 0)
126 cc += ret.u.retval;
127 }
128
129 #if 0 /* TC machines' firmware */
130 cc = min(cc, len);
131 #else
132 cc = len;
133 #endif
134 bcopy(hate, pkt, cc);
135
136 return cc;
137 }
138
139 extern char *strchr();
140
141 void
142 prom_init(desc, machdep_hint)
143 struct iodesc *desc;
144 void *machdep_hint;
145 {
146
147 prom_return_t ret;
148 char devname[64];
149 int devlen, i;
150 char *enet_addr;
151
152 ret.bits = prom_getenv(PROM_E_BOOTED_DEV, devname, sizeof(devname));
153 devlen = ret.u.retval;
154
155 /* Ethernet address is the 9th component of the booted_dev string. */
156 enet_addr = devname;
157 for (i = 0; i < 8; i++) {
158 enet_addr = strchr(enet_addr, ' ');
159 if (enet_addr == NULL) {
160 printf("Boot device name does not contain ethernet address.\n");
161 goto punt;
162 }
163 enet_addr++;
164 }
165 if (enet_addr != NULL) {
166 int hv, lv;
167
168 #define dval(c) (((c) >= '0' && (c) <= '9') ? ((c) - '0') : \
169 (((c) >= 'A' && (c) <= 'F') ? (10 + (c) - 'A') : \
170 (((c) >= 'a' && (c) <= 'f') ? (10 + (c) - 'a') : -1)))
171
172 for (i = 0; i < 6; i++) {
173 hv = dval(*enet_addr); enet_addr++;
174 lv = dval(*enet_addr); enet_addr++;
175 enet_addr++;
176
177 if (hv == -1 || lv == -1) {
178 printf("Bogus ethernet address.\n");
179 goto punt;
180 }
181
182 desc->myea[i] = (hv << 4) | lv;
183 }
184 #undef dval
185 }
186
187 printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
188
189 ret.bits = prom_open(devname, devlen + 1);
190 if (ret.u.status) {
191 printf("prom_init: open failed: %d\n", ret.u.status);
192 return;
193 }
194 netfd = ret.u.retval;
195 return;
196
197 punt:
198 printf("Boot device name was: \"%s\"\n", devname);
199 printf("\n");
200 printf("Your firmware may be too old to network-boot NetBSD/Alpha.\n");
201 halt();
202 }
203
204 void
205 prom_end(nif)
206 struct netif *nif;
207 {
208
209 prom_close(netfd);
210 }
211