1 /* $OpenBSD: if_prom.c,v 1.7 2015/07/17 16:13:26 miod Exp $ */ 2 /* $NetBSD: if_prom.c,v 1.9 1997/04/06 08:41:26 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved. 6 * Copyright (c) 1993 Adam Glass. 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 Adam Glass. 19 * 4. The name of the Author 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 Adam Glass ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/types.h> 37 38 #include <netinet/in.h> 39 40 #include <include/rpb.h> 41 #include <include/prom.h> 42 43 #include <lib/libkern/libkern.h> 44 #include <lib/libsa/netif.h> 45 #include <lib/libsa/stand.h> 46 47 #include "stand/bbinfo.h" 48 49 int prom_match(struct netif *, void *); 50 int prom_probe(struct netif *, void *); 51 void prom_init(struct iodesc *, void *); 52 int prom_get(struct iodesc *, void *, size_t, time_t); 53 int prom_put(struct iodesc *, void *, size_t); 54 void prom_end(struct netif *); 55 56 extern struct netif_stats prom_stats[]; 57 58 struct netif_dif prom_ifs[] = { 59 /* dif_unit dif_nsel dif_stats dif_private */ 60 { 0, 1, &prom_stats[0], 0, }, 61 }; 62 63 struct netif_stats prom_stats[nitems(prom_ifs)]; 64 65 struct netbbinfo netbbinfo = { 66 0xfeedbabedeadbeef, /* magic number */ 67 0, /* set */ 68 0, 0, 0, 0, 0, 0, /* ether address */ 69 0, /* force */ 70 { 0, }, /* pad2 */ 71 0, /* cksum */ 72 0xfeedbeefdeadbabe, /* magic number */ 73 }; 74 75 struct netif_driver prom_netif_driver = { 76 "prom", /* netif_bname */ 77 prom_match, /* netif_match */ 78 prom_probe, /* netif_probe */ 79 prom_init, /* netif_init */ 80 prom_get, /* netif_get */ 81 prom_put, /* netif_put */ 82 prom_end, /* netif_end */ 83 prom_ifs, /* netif_ifs */ 84 nitems(prom_ifs) /* netif_nifs */ 85 }; 86 87 int netfd, broken_firmware; 88 89 int 90 prom_match(struct netif *nif, void *machdep_hint) 91 { 92 93 return (1); 94 } 95 96 int 97 prom_probe(struct netif *nif, void *machdep_hint) 98 { 99 100 return 0; 101 } 102 103 int 104 prom_put(struct iodesc *desc, void *pkt, size_t len) 105 { 106 107 prom_write(netfd, len, pkt, 0); 108 109 return len; 110 } 111 112 113 int 114 prom_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout) 115 { 116 prom_return_t ret; 117 time_t t; 118 ssize_t cc; 119 char hate[2000]; 120 121 t = getsecs(); 122 cc = 0; 123 while (((getsecs() - t) < timeout) && !cc) { 124 if (broken_firmware) 125 ret.bits = prom_read(netfd, 0, hate, 0); 126 else 127 ret.bits = prom_read(netfd, sizeof hate, hate, 0); 128 if (ret.u.status == 0) 129 cc = ret.u.retval; 130 } 131 if (broken_firmware) 132 cc = lmin(cc, len); 133 else 134 cc = len; 135 bcopy(hate, pkt, cc); 136 137 return cc; 138 } 139 140 extern char *strchr(); 141 142 void 143 prom_init(struct iodesc *desc, void *machdep_hint) 144 { 145 prom_return_t ret; 146 char devname[64]; 147 int devlen, i, netbbinfovalid; 148 char *enet_addr; 149 u_int64_t *qp, csum; 150 151 broken_firmware = 0; 152 153 csum = 0; 154 for (i = 0, qp = (u_int64_t *)&netbbinfo; 155 i < (sizeof netbbinfo / sizeof (u_int64_t)); i++, qp++) 156 csum += *qp; 157 netbbinfovalid = (csum == 0); 158 if (netbbinfovalid) 159 netbbinfovalid = netbbinfo.set; 160 161 #if 0 162 printf("netbbinfo "); 163 if (!netbbinfovalid) 164 printf("invalid\n"); 165 else 166 printf("valid: force = %d, ea = %s\n", netbbinfo.force, 167 ether_sprintf(netbbinfo.ether_addr)); 168 #endif 169 170 ret.bits = prom_getenv(PROM_E_BOOTED_DEV, devname, sizeof(devname)); 171 devlen = ret.u.retval; 172 173 /* Ethernet address is the 9th component of the booted_dev string. */ 174 enet_addr = devname; 175 for (i = 0; i < 8; i++) { 176 enet_addr = strchr(enet_addr, ' '); 177 if (enet_addr == NULL) { 178 printf("boot: boot device name does not contain ethernet address.\n"); 179 goto punt; 180 } 181 enet_addr++; 182 } 183 if (enet_addr != NULL) { 184 int hv, lv; 185 186 #define dval(c) (((c) >= '0' && (c) <= '9') ? ((c) - '0') : \ 187 (((c) >= 'A' && (c) <= 'F') ? (10 + (c) - 'A') : \ 188 (((c) >= 'a' && (c) <= 'f') ? (10 + (c) - 'a') : -1))) 189 190 for (i = 0; i < 6; i++) { 191 hv = dval(*enet_addr); enet_addr++; 192 lv = dval(*enet_addr); enet_addr++; 193 enet_addr++; 194 195 if (hv == -1 || lv == -1) { 196 printf("boot: boot device name contains bogus ethernet address.\n"); 197 goto punt; 198 } 199 200 desc->myea[i] = (hv << 4) | lv; 201 } 202 #undef dval 203 } 204 205 if (netbbinfovalid && netbbinfo.force) { 206 printf("boot: using hard-coded ethernet address (forced).\n"); 207 bcopy(netbbinfo.ether_addr, desc->myea, sizeof desc->myea); 208 } 209 210 gotit: 211 printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea)); 212 213 ret.bits = prom_open(devname, devlen + 1); 214 if (ret.u.status) { 215 printf("prom_init: open failed: %d\n", ret.u.status); 216 goto reallypunt; 217 } 218 netfd = ret.u.retval; 219 return; 220 221 punt: 222 broken_firmware = 1; 223 if (netbbinfovalid) { 224 printf("boot: using hard-coded ethernet address.\n"); 225 bcopy(netbbinfo.ether_addr, desc->myea, sizeof desc->myea); 226 goto gotit; 227 } 228 229 reallypunt: 230 printf("\n"); 231 printf("Boot device name was: \"%s\"\n", devname); 232 printf("\n"); 233 printf("Your firmware may be too old to network-boot OpenBSD/alpha,\n"); 234 printf("or you might have to hard-code an ethernet address into\n"); 235 printf("your network boot block with setnetbootinfo(8).\n"); 236 halt(); 237 } 238 239 void 240 prom_end(struct netif *nif) 241 { 242 243 prom_close(netfd); 244 } 245