1 /*- 2 * Copyright (c) 2000-2001 Benno Rice 3 * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 30 #include <sys/param.h> 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 34 #include <net/if.h> 35 #include <netinet/in.h> 36 #include <netinet/in_systm.h> 37 #include <netinet/if_ether.h> 38 #include <netinet/ip.h> 39 40 #include <stand.h> 41 #include <net.h> 42 #include <netif.h> 43 44 #include "api_public.h" 45 #include "glue.h" 46 #include "libuboot.h" 47 #include "dev_net.h" 48 49 static int net_probe(struct netif *, void *); 50 static int net_match(struct netif *, void *); 51 static void net_init(struct iodesc *, void *); 52 static ssize_t net_get(struct iodesc *, void **, time_t); 53 static ssize_t net_put(struct iodesc *, void *, size_t); 54 static void net_end(struct netif *); 55 56 extern struct netif_stats net_stats[]; 57 58 struct netif_dif net_ifs[] = { 59 /* dif_unit dif_nsel dif_stats dif_private */ 60 { 0, 1, &net_stats[0], 0, }, 61 }; 62 63 struct netif_stats net_stats[nitems(net_ifs)]; 64 65 struct netif_driver uboot_net = { 66 "uboot_eth", /* netif_bname */ 67 net_match, /* netif_match */ 68 net_probe, /* netif_probe */ 69 net_init, /* netif_init */ 70 net_get, /* netif_get */ 71 net_put, /* netif_put */ 72 net_end, /* netif_end */ 73 net_ifs, /* netif_ifs */ 74 nitems(net_ifs) /* netif_nifs */ 75 }; 76 77 struct uboot_softc { 78 uint32_t sc_pad; 79 uint8_t sc_rxbuf[ETHER_MAX_LEN]; 80 uint8_t sc_txbuf[ETHER_MAX_LEN + PKTALIGN]; 81 uint8_t *sc_txbufp; 82 int sc_handle; /* device handle for ub_dev_xxx */ 83 }; 84 85 static struct uboot_softc uboot_softc; 86 87 /* 88 * get_env_net_params() 89 * 90 * Attempt to obtain all the parms we need for netbooting from the U-Boot 91 * environment. If we fail to obtain the values it may still be possible to 92 * netboot; the net_dev code will attempt to get the values from bootp, rarp, 93 * and other such sources. 94 * 95 * If rootip.s_addr is non-zero net_dev assumes the required global variables 96 * are set and skips the bootp inquiry. For that reason, we don't set rootip 97 * until we've verified that we have at least the minimum required info. 98 * 99 * This is called from netif_init() which can result in it getting called 100 * multiple times, by design. The network code at higher layers zeroes out 101 * rootip when it closes a network interface, so if it gets opened again we have 102 * to obtain all this info again. 103 */ 104 static void 105 get_env_net_params() 106 { 107 char *envstr; 108 in_addr_t rootaddr, serveraddr; 109 110 /* 111 * Silently get out right away if we don't have rootpath, because none 112 * of the other info we obtain below is sufficient to boot without it. 113 * 114 * If we do have rootpath, copy it into the global var and also set 115 * dhcp.root-path in the env. If we don't get all the other info from 116 * the u-boot env below, we will still try dhcp/bootp, but the server- 117 * provided path will not replace the user-provided value we set here. 118 */ 119 if ((envstr = ub_env_get("rootpath")) == NULL) 120 return; 121 strlcpy(rootpath, envstr, sizeof(rootpath)); 122 setenv("dhcp.root-path", rootpath, 0); 123 124 /* 125 * Our own IP address must be valid. Silently get out if it's not set, 126 * but whine if it's there and we can't parse it. 127 */ 128 if ((envstr = ub_env_get("ipaddr")) == NULL) 129 return; 130 if ((myip.s_addr = inet_addr(envstr)) == INADDR_NONE) { 131 printf("Could not parse ipaddr '%s'\n", envstr); 132 return; 133 } 134 135 /* 136 * Netmask is optional, default to the "natural" netmask for our IP, but 137 * whine if it was provided and we couldn't parse it. 138 */ 139 if ((envstr = ub_env_get("netmask")) != NULL && 140 (netmask = inet_addr(envstr)) == INADDR_NONE) { 141 printf("Could not parse netmask '%s'\n", envstr); 142 } 143 if (netmask == INADDR_NONE) { 144 if (IN_CLASSA(myip.s_addr)) 145 netmask = IN_CLASSA_NET; 146 else if (IN_CLASSB(myip.s_addr)) 147 netmask = IN_CLASSB_NET; 148 else 149 netmask = IN_CLASSC_NET; 150 } 151 152 /* 153 * Get optional serverip before rootpath; the latter can override it. 154 * Whine only if it's present but can't be parsed. 155 */ 156 serveraddr = INADDR_NONE; 157 if ((envstr = ub_env_get("serverip")) != NULL) { 158 if ((serveraddr = inet_addr(envstr)) == INADDR_NONE) 159 printf("Could not parse serverip '%s'\n", envstr); 160 } 161 162 /* 163 * There must be a rootpath. It may be ip:/path or it may be just the 164 * path in which case the ip needs to be in serverip. 165 */ 166 rootaddr = net_parse_rootpath(); 167 if (rootaddr == INADDR_NONE) 168 rootaddr = serveraddr; 169 if (rootaddr == INADDR_NONE) { 170 printf("No server address for rootpath '%s'\n", envstr); 171 return; 172 } 173 rootip.s_addr = rootaddr; 174 175 /* 176 * Gateway IP is optional unless rootip is on a different net in which 177 * case whine if it's missing or we can't parse it, and set rootip addr 178 * to zero, which signals to other network code that network params 179 * aren't set (so it will try dhcp, bootp, etc). 180 */ 181 envstr = ub_env_get("gatewayip"); 182 if (!SAMENET(myip, rootip, netmask)) { 183 if (envstr == NULL) { 184 printf("Need gatewayip for a root server on a " 185 "different network.\n"); 186 rootip.s_addr = 0; 187 return; 188 } 189 if ((gateip.s_addr = inet_addr(envstr) == INADDR_NONE)) { 190 printf("Could not parse gatewayip '%s'\n", envstr); 191 rootip.s_addr = 0; 192 return; 193 } 194 } 195 } 196 197 static int 198 net_match(struct netif *nif, void *machdep_hint) 199 { 200 char **a = (char **)machdep_hint; 201 202 if (memcmp("net", *a, 3) == 0) 203 return (1); 204 205 printf("net_match: could not match network device\n"); 206 return (0); 207 } 208 209 static int 210 net_probe(struct netif *nif, void *machdep_hint) 211 { 212 struct device_info *di; 213 int i; 214 215 for (i = 0; i < devs_no; i++) 216 if ((di = ub_dev_get(i)) != NULL) 217 if (di->type == DEV_TYP_NET) 218 break; 219 220 if (i == devs_no) { 221 printf("net_probe: no network devices found, maybe not" 222 " enumerated yet..?\n"); 223 return (-1); 224 } 225 226 #if defined(NETIF_DEBUG) 227 printf("net_probe: network device found: %d\n", i); 228 #endif 229 uboot_softc.sc_handle = i; 230 231 return (0); 232 } 233 234 static ssize_t 235 net_put(struct iodesc *desc, void *pkt, size_t len) 236 { 237 struct netif *nif = desc->io_netif; 238 struct uboot_softc *sc = nif->nif_devdata; 239 size_t sendlen; 240 ssize_t rv; 241 242 #if defined(NETIF_DEBUG) 243 struct ether_header *eh; 244 245 printf("net_put: desc %p, pkt %p, len %d\n", desc, pkt, len); 246 eh = pkt; 247 printf("dst: %s ", ether_sprintf(eh->ether_dhost)); 248 printf("src: %s ", ether_sprintf(eh->ether_shost)); 249 printf("type: 0x%x\n", eh->ether_type & 0xffff); 250 #endif 251 252 if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) { 253 sendlen = ETHER_MIN_LEN - ETHER_CRC_LEN; 254 bzero(sc->sc_txbufp, sendlen); 255 } else 256 sendlen = len; 257 258 memcpy(sc->sc_txbufp, pkt, len); 259 260 rv = ub_dev_send(sc->sc_handle, sc->sc_txbufp, sendlen); 261 262 #if defined(NETIF_DEBUG) 263 printf("net_put: ub_send returned %d\n", rv); 264 #endif 265 if (rv == 0) 266 rv = len; 267 else 268 rv = -1; 269 270 return (rv); 271 } 272 273 static ssize_t 274 net_get(struct iodesc *desc, void **pkt, time_t timeout) 275 { 276 struct netif *nif = desc->io_netif; 277 struct uboot_softc *sc = nif->nif_devdata; 278 time_t t; 279 int err, rlen; 280 size_t len; 281 char *buf; 282 283 #if defined(NETIF_DEBUG) 284 printf("net_get: pkt %p, timeout %d\n", pkt, timeout); 285 #endif 286 t = getsecs(); 287 len = sizeof (sc->sc_rxbuf); 288 do { 289 err = ub_dev_recv(sc->sc_handle, sc->sc_rxbuf, len, &rlen); 290 291 if (err != 0) { 292 printf("net_get: ub_dev_recv() failed, error=%d\n", 293 err); 294 rlen = 0; 295 break; 296 } 297 } while ((rlen == -1 || rlen == 0) && (getsecs() - t < timeout)); 298 299 #if defined(NETIF_DEBUG) 300 printf("net_get: received len %d (%x)\n", rlen, rlen); 301 #endif 302 303 if (rlen > 0) { 304 buf = malloc(rlen + ETHER_ALIGN); 305 if (buf == NULL) 306 return (-1); 307 memcpy(buf + ETHER_ALIGN, sc->sc_rxbuf, rlen); 308 *pkt = buf; 309 return ((ssize_t)rlen); 310 } 311 312 return (-1); 313 } 314 315 static void 316 net_init(struct iodesc *desc, void *machdep_hint) 317 { 318 struct netif *nif = desc->io_netif; 319 struct uboot_softc *sc; 320 struct device_info *di; 321 int err; 322 323 sc = nif->nif_devdata = &uboot_softc; 324 325 if ((err = ub_dev_open(sc->sc_handle)) != 0) 326 panic("%s%d: initialisation failed with error %d\n", 327 nif->nif_driver->netif_bname, nif->nif_unit, err); 328 329 /* Get MAC address */ 330 di = ub_dev_get(sc->sc_handle); 331 memcpy(desc->myea, di->di_net.hwaddr, 6); 332 if (memcmp (desc->myea, "\0\0\0\0\0\0", 6) == 0) { 333 panic("%s%d: empty ethernet address!", 334 nif->nif_driver->netif_bname, nif->nif_unit); 335 } 336 337 /* Attempt to get netboot params from the u-boot env. */ 338 get_env_net_params(); 339 if (myip.s_addr != 0) 340 desc->myip = myip; 341 342 #if defined(NETIF_DEBUG) 343 printf("network: %s%d attached to %s\n", nif->nif_driver->netif_bname, 344 nif->nif_unit, ether_sprintf(desc->myea)); 345 #endif 346 347 /* Set correct alignment for TX packets */ 348 sc->sc_txbufp = sc->sc_txbuf; 349 if ((unsigned long)sc->sc_txbufp % PKTALIGN) 350 sc->sc_txbufp += PKTALIGN - 351 (unsigned long)sc->sc_txbufp % PKTALIGN; 352 } 353 354 static void 355 net_end(struct netif *nif) 356 { 357 struct uboot_softc *sc = nif->nif_devdata; 358 int err; 359 360 if ((err = ub_dev_close(sc->sc_handle)) != 0) 361 panic("%s%d: net_end failed with error %d\n", 362 nif->nif_driver->netif_bname, nif->nif_unit, err); 363 } 364