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