if_prom.c revision 1.5 1 /* $NetBSD: if_prom.c,v 1.5 1997/01/16 01:21:39 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 <lib/libsa/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 ret.bits = prom_read(netfd, sizeof hate, hate, 0);
121 if (ret.u.status == 0)
122 cc += ret.u.retval;
123 }
124 cc = len;
125 bcopy(hate, pkt, cc);
126
127 return cc;
128 }
129
130 extern char *strchr();
131
132 void
133 prom_init(desc, machdep_hint)
134 struct iodesc *desc;
135 void *machdep_hint;
136 {
137 prom_return_t ret;
138 char devname[64];
139 int devlen, i;
140 char *enet_addr;
141
142 ret.bits = prom_getenv(PROM_E_BOOTED_DEV, devname, sizeof(devname));
143 devlen = ret.u.retval;
144
145 /* Ethernet address is the 9th component of the booted_dev string. */
146 enet_addr = devname;
147 for (i = 0; i < 8; i++) {
148 enet_addr = strchr(enet_addr, ' ');
149 if (enet_addr == NULL) {
150 printf("Boot device name does not contain ethernet address.\n");
151 goto punt;
152 }
153 enet_addr++;
154 }
155 if (enet_addr != NULL) {
156 int hv, lv;
157
158 #define dval(c) (((c) >= '0' && (c) <= '9') ? ((c) - '0') : \
159 (((c) >= 'A' && (c) <= 'F') ? (10 + (c) - 'A') : \
160 (((c) >= 'a' && (c) <= 'f') ? (10 + (c) - 'a') : -1)))
161
162 for (i = 0; i < 6; i++) {
163 hv = dval(*enet_addr); enet_addr++;
164 lv = dval(*enet_addr); enet_addr++;
165 enet_addr++;
166
167 if (hv == -1 || lv == -1) {
168 printf("Bogus ethernet address.\n");
169 goto punt;
170 }
171
172 desc->myea[i] = (hv << 4) | lv;
173 }
174 #undef dval
175 }
176
177 printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
178
179 ret.bits = prom_open(devname, devlen + 1);
180 if (ret.u.status) {
181 printf("prom_init: open failed: %d\n", ret.u.status);
182 goto punt;
183 }
184 netfd = ret.u.retval;
185 return;
186
187 punt:
188 printf("Boot device name was: \"%s\"\n", devname);
189 printf("\n");
190 printf("Your firmware may be too old to network-boot NetBSD/Alpha.\n");
191 halt();
192 }
193
194 void
195 prom_end(nif)
196 struct netif *nif;
197 {
198
199 prom_close(netfd);
200 }
201