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