1/*- 2 * Copyright (c) 2000 Alfred Perlstein <alfred@freebsd.org> 3 * Copyright (c) 2000 Paul Saab <ps@freebsd.org> 4 * Copyright (c) 2000 John Baldwin <jhb@freebsd.org> 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD$"); 31 32#include <stand.h> 33#include <string.h> 34#include <stdarg.h> 35 36#include <netinet/in_systm.h> 37#include <netinet/in.h> 38#include <netinet/udp.h> 39 40#include <net.h> 41#include <netif.h> 42#include <nfsv2.h> 43#include <iodesc.h> 44 45#include <bootp.h> 46#include <bootstrap.h> 47#include "btxv86.h" 48#include "pxe.h" 49 50/* 51 * Allocate the PXE buffers statically instead of sticking grimy fingers into 52 * BTX's private data area. The scratch buffer is used to send information to 53 * the PXE BIOS, and the data buffer is used to receive data from the PXE BIOS. 54 */ 55#define PXE_BUFFER_SIZE 0x2000 56#define PXE_TFTP_BUFFER_SIZE 512 57static char scratch_buffer[PXE_BUFFER_SIZE]; 58static char data_buffer[PXE_BUFFER_SIZE]; 59 60static pxenv_t *pxenv_p = NULL; /* PXENV+ */ 61static pxe_t *pxe_p = NULL; /* !PXE */ 62static BOOTPLAYER bootplayer; /* PXE Cached information. */ 63 64static int pxe_debug = 0; 65static int pxe_sock = -1; 66static int pxe_opens = 0; 67 68void pxe_enable(void *pxeinfo); 69static void (*pxe_call)(int func); 70static void pxenv_call(int func); 71static void bangpxe_call(int func); 72 73static int pxe_init(void); 74static int pxe_strategy(void *devdata, int flag, daddr_t dblk, 75 size_t size, char *buf, size_t *rsize); 76static int pxe_open(struct open_file *f, ...); 77static int pxe_close(struct open_file *f); 78static void pxe_print(int verbose); 79static void pxe_cleanup(void); 80static void pxe_setnfshandle(char *rootpath); 81 82static void pxe_perror(int error); 83static int pxe_netif_match(struct netif *nif, void *machdep_hint); 84static int pxe_netif_probe(struct netif *nif, void *machdep_hint); 85static void pxe_netif_init(struct iodesc *desc, void *machdep_hint); 86static int pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, 87 time_t timeout); 88static int pxe_netif_put(struct iodesc *desc, void *pkt, size_t len); 89static void pxe_netif_end(struct netif *nif); 90 91#ifdef OLD_NFSV2 92int nfs_getrootfh(struct iodesc*, char*, u_char*); 93#else 94int nfs_getrootfh(struct iodesc*, char*, uint32_t*, u_char*); 95#endif 96 97extern struct netif_stats pxe_st[]; 98extern u_int16_t __bangpxeseg; 99extern u_int16_t __bangpxeoff; 100extern void __bangpxeentry(void); 101extern u_int16_t __pxenvseg; 102extern u_int16_t __pxenvoff; 103extern void __pxenventry(void); 104 105struct netif_dif pxe_ifs[] = { 106/* dif_unit dif_nsel dif_stats dif_private */ 107 {0, 1, &pxe_st[0], 0} 108}; 109 110struct netif_stats pxe_st[NENTS(pxe_ifs)]; 111 112struct netif_driver pxenetif = { 113 "pxenet", 114 pxe_netif_match, 115 pxe_netif_probe, 116 pxe_netif_init, 117 pxe_netif_get, 118 pxe_netif_put, 119 pxe_netif_end, 120 pxe_ifs, 121 NENTS(pxe_ifs) 122}; 123 124struct netif_driver *netif_drivers[] = { 125 &pxenetif, 126 NULL 127}; 128 129struct devsw pxedisk = { 130 "pxe", 131 DEVT_NET, 132 pxe_init, 133 pxe_strategy, 134 pxe_open, 135 pxe_close, 136 noioctl, 137 pxe_print, 138 pxe_cleanup 139}; 140 141/* 142 * This function is called by the loader to enable PXE support if we 143 * are booted by PXE. The passed in pointer is a pointer to the 144 * PXENV+ structure. 145 */ 146void 147pxe_enable(void *pxeinfo) 148{ 149 pxenv_p = (pxenv_t *)pxeinfo; 150 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 + 151 pxenv_p->PXEPtr.offset); 152 pxe_call = NULL; 153} 154 155/* 156 * return true if pxe structures are found/initialized, 157 * also figures out our IP information via the pxe cached info struct 158 */ 159static int 160pxe_init(void) 161{ 162 t_PXENV_GET_CACHED_INFO *gci_p; 163 int counter; 164 uint8_t checksum; 165 uint8_t *checkptr; 166 167 if(pxenv_p == NULL) 168 return (0); 169 170 /* look for "PXENV+" */ 171 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) { 172 pxenv_p = NULL; 173 return (0); 174 } 175 176 /* make sure the size is something we can handle */ 177 if (pxenv_p->Length > sizeof(*pxenv_p)) { 178 printf("PXENV+ structure too large, ignoring\n"); 179 pxenv_p = NULL; 180 return (0); 181 } 182 183 /* 184 * do byte checksum: 185 * add up each byte in the structure, the total should be 0 186 */ 187 checksum = 0; 188 checkptr = (uint8_t *) pxenv_p; 189 for (counter = 0; counter < pxenv_p->Length; counter++) 190 checksum += *checkptr++; 191 if (checksum != 0) { 192 printf("PXENV+ structure failed checksum, ignoring\n"); 193 pxenv_p = NULL; 194 return (0); 195 } 196 197 198 /* 199 * PXENV+ passed, so use that if !PXE is not available or 200 * the checksum fails. 201 */ 202 pxe_call = pxenv_call; 203 if (pxenv_p->Version >= 0x0200) { 204 for (;;) { 205 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) { 206 pxe_p = NULL; 207 break; 208 } 209 checksum = 0; 210 checkptr = (uint8_t *)pxe_p; 211 for (counter = 0; counter < pxe_p->StructLength; 212 counter++) 213 checksum += *checkptr++; 214 if (checksum != 0) { 215 pxe_p = NULL; 216 break; 217 } 218 pxe_call = bangpxe_call; 219 break; 220 } 221 } 222 223 printf("\nPXE version %d.%d, real mode entry point ", 224 (uint8_t) (pxenv_p->Version >> 8), 225 (uint8_t) (pxenv_p->Version & 0xFF)); 226 if (pxe_call == bangpxe_call) 227 printf("@%04x:%04x\n", 228 pxe_p->EntryPointSP.segment, 229 pxe_p->EntryPointSP.offset); 230 else 231 printf("@%04x:%04x\n", 232 pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset); 233 234 gci_p = (t_PXENV_GET_CACHED_INFO *) scratch_buffer; 235 bzero(gci_p, sizeof(*gci_p)); 236 gci_p->PacketType = PXENV_PACKET_TYPE_BINL_REPLY; 237 pxe_call(PXENV_GET_CACHED_INFO); 238 if (gci_p->Status != 0) { 239 pxe_perror(gci_p->Status); 240 pxe_p = NULL; 241 return (0); 242 } 243 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset), 244 &bootplayer, gci_p->BufferSize); 245 return (1); 246} 247 248 249static int 250pxe_strategy(void *devdata, int flag, daddr_t dblk, size_t size, 251 char *buf, size_t *rsize) 252{ 253 return (EIO); 254} 255 256static int 257pxe_open(struct open_file *f, ...) 258{ 259 va_list args; 260 char *devname; /* Device part of file name (or NULL). */ 261 char temp[FNAME_SIZE]; 262 int error = 0; 263 int i; 264 265 va_start(args, f); 266 devname = va_arg(args, char*); 267 va_end(args); 268 269 /* On first open, do netif open, mount, etc. */ 270 if (pxe_opens == 0) { 271 /* Find network interface. */ 272 if (pxe_sock < 0) { 273 pxe_sock = netif_open(devname); 274 if (pxe_sock < 0) { 275 printf("pxe_open: netif_open() failed\n"); 276 return (ENXIO); 277 } 278 if (pxe_debug) 279 printf("pxe_open: netif_open() succeeded\n"); 280 } 281 if (rootip.s_addr == 0) { 282 /* 283 * Do a bootp/dhcp request to find out where our 284 * NFS/TFTP server is. Even if we dont get back 285 * the proper information, fall back to the server 286 * which brought us to life and a default rootpath. 287 */ 288 bootp(pxe_sock, BOOTP_PXE); 289 if (rootip.s_addr == 0) 290 rootip.s_addr = bootplayer.sip; 291 if (!rootpath[0]) 292 strcpy(rootpath, PXENFSROOTPATH); 293 294 for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++) 295 if (rootpath[i] == ':') 296 break; 297 if (i && i != FNAME_SIZE && rootpath[i] == ':') { 298 rootpath[i++] = '\0'; 299 if (inet_addr(&rootpath[0]) != INADDR_NONE) 300 rootip.s_addr = inet_addr(&rootpath[0]); 301 bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1); 302 bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1); 303 } 304 printf("pxe_open: server addr: %s\n", inet_ntoa(rootip)); 305 printf("pxe_open: server path: %s\n", rootpath); 306 printf("pxe_open: gateway ip: %s\n", inet_ntoa(gateip)); 307 308 setenv("boot.netif.ip", inet_ntoa(myip), 1); 309 setenv("boot.netif.netmask", intoa(netmask), 1); 310 setenv("boot.netif.gateway", inet_ntoa(gateip), 1); 311 if (bootplayer.Hardware == ETHER_TYPE) { 312 sprintf(temp, "%6D", bootplayer.CAddr, ":"); 313 setenv("boot.netif.hwaddr", temp, 1); 314 } 315 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1); 316 setenv("boot.nfsroot.path", rootpath, 1); 317 setenv("dhcp.host-name", hostname, 1); 318 } 319 } 320 pxe_opens++; 321 f->f_devdata = &pxe_sock; 322 return (error); 323} 324 325static int 326pxe_close(struct open_file *f) 327{ 328 329#ifdef PXE_DEBUG 330 if (pxe_debug) 331 printf("pxe_close: opens=%d\n", pxe_opens); 332#endif 333 334 /* On last close, do netif close, etc. */ 335 f->f_devdata = NULL; 336 /* Extra close call? */ 337 if (pxe_opens <= 0) 338 return (0); 339 pxe_opens--; 340 /* Not last close? */ 341 if (pxe_opens > 0) 342 return(0); 343 344#ifdef LOADER_NFS_SUPPORT 345 /* get an NFS filehandle for our root filesystem */ 346 pxe_setnfshandle(rootpath); 347#endif 348 349 if (pxe_sock >= 0) { 350 351#ifdef PXE_DEBUG 352 if (pxe_debug) 353 printf("pxe_close: calling netif_close()\n"); 354#endif 355 netif_close(pxe_sock); 356 pxe_sock = -1; 357 } 358 return (0); 359} 360 361static void 362pxe_print(int verbose) 363{ 364 365 if (pxe_call == NULL) 366 return; 367 368 printf(" pxe0: %s:%s\n", inet_ntoa(rootip), rootpath); 369} 370 371static void 372pxe_cleanup(void) 373{ 374#ifdef PXE_DEBUG 375 t_PXENV_UNLOAD_STACK *unload_stack_p = 376 (t_PXENV_UNLOAD_STACK *)scratch_buffer; 377 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p = 378 (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer; 379#endif 380 381 if (pxe_call == NULL) 382 return; 383 384 pxe_call(PXENV_UNDI_SHUTDOWN); 385 386#ifdef PXE_DEBUG 387 if (pxe_debug && undi_shutdown_p->Status != 0) 388 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n", 389 undi_shutdown_p->Status); 390#endif 391 392 pxe_call(PXENV_UNLOAD_STACK); 393 394#ifdef PXE_DEBUG 395 if (pxe_debug && unload_stack_p->Status != 0) 396 printf("pxe_cleanup: UNLOAD_STACK failed %x\n", 397 unload_stack_p->Status); 398#endif 399} 400 401void 402pxe_perror(int err) 403{ 404 return; 405} 406 407#ifdef LOADER_NFS_SUPPORT 408/* 409 * Reach inside the libstand NFS code and dig out an NFS handle 410 * for the root filesystem. 411 */ 412#ifdef OLD_NFSV2 413struct nfs_iodesc { 414 struct iodesc *iodesc; 415 off_t off; 416 u_char fh[NFS_FHSIZE]; 417 /* structure truncated here */ 418}; 419extern struct nfs_iodesc nfs_root_node; 420extern int rpc_port; 421 422static void 423pxe_rpcmountcall() 424{ 425 struct iodesc *d; 426 int error; 427 428 if (!(d = socktodesc(pxe_sock))) 429 return; 430 d->myport = htons(--rpc_port); 431 d->destip = rootip; 432 if ((error = nfs_getrootfh(d, rootpath, nfs_root_node.fh)) != 0) 433 printf("NFS MOUNT RPC error: %d\n", error); 434 nfs_root_node.iodesc = d; 435} 436 437static void 438pxe_setnfshandle(char *rootpath) 439{ 440 int i; 441 u_char *fh; 442 char buf[2 * NFS_FHSIZE + 3], *cp; 443 444 /* 445 * If NFS files were never opened, we need to do mount call 446 * ourselves. Use nfs_root_node.iodesc as flag indicating 447 * previous NFS usage. 448 */ 449 if (nfs_root_node.iodesc == NULL) 450 pxe_rpcmountcall(); 451 452 fh = &nfs_root_node.fh[0]; 453 buf[0] = 'X'; 454 cp = &buf[1]; 455 for (i = 0; i < NFS_FHSIZE; i++, cp += 2) 456 sprintf(cp, "%02x", fh[i]); 457 sprintf(cp, "X"); 458 setenv("boot.nfsroot.nfshandle", buf, 1); 459} 460#else /* !OLD_NFSV2 */ 461 462#define NFS_V3MAXFHSIZE 64 463 464struct nfs_iodesc { 465 struct iodesc *iodesc; 466 off_t off; 467 uint32_t fhsize; 468 u_char fh[NFS_V3MAXFHSIZE]; 469 /* structure truncated */ 470}; 471extern struct nfs_iodesc nfs_root_node; 472extern int rpc_port; 473 474static void 475pxe_rpcmountcall() 476{ 477 struct iodesc *d; 478 int error; 479 480 if (!(d = socktodesc(pxe_sock))) 481 return; 482 d->myport = htons(--rpc_port); 483 d->destip = rootip; 484 if ((error = nfs_getrootfh(d, rootpath, &nfs_root_node.fhsize, 485 nfs_root_node.fh)) != 0) { 486 printf("NFS MOUNT RPC error: %d\n", error); 487 nfs_root_node.fhsize = 0; 488 } 489 nfs_root_node.iodesc = d; 490} 491 492static void 493pxe_setnfshandle(char *rootpath) 494{ 495 int i; 496 u_char *fh; 497 char buf[2 * NFS_V3MAXFHSIZE + 3], *cp; 498 499 /* 500 * If NFS files were never opened, we need to do mount call 501 * ourselves. Use nfs_root_node.iodesc as flag indicating 502 * previous NFS usage. 503 */ 504 if (nfs_root_node.iodesc == NULL) 505 pxe_rpcmountcall(); 506 507 fh = &nfs_root_node.fh[0]; 508 buf[0] = 'X'; 509 cp = &buf[1]; 510 for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2) 511 sprintf(cp, "%02x", fh[i]); 512 sprintf(cp, "X"); 513 setenv("boot.nfsroot.nfshandle", buf, 1); 514 sprintf(buf, "%d", nfs_root_node.fhsize); 515 setenv("boot.nfsroot.nfshandlelen", buf, 1); 516} 517#endif /* OLD_NFSV2 */ 518#endif /* LOADER_NFS_SUPPORT */ 519 520void 521pxenv_call(int func) 522{ 523#ifdef PXE_DEBUG 524 if (pxe_debug) 525 printf("pxenv_call %x\n", func); 526#endif 527 528 bzero(&v86, sizeof(v86)); 529 bzero(data_buffer, sizeof(data_buffer)); 530 531 __pxenvseg = pxenv_p->RMEntry.segment; 532 __pxenvoff = pxenv_p->RMEntry.offset; 533 534 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS; 535 v86.es = VTOPSEG(scratch_buffer); 536 v86.edi = VTOPOFF(scratch_buffer); 537 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry); 538 v86.ebx = func; 539 v86int(); 540 v86.ctl = V86_FLAGS; 541} 542 543void 544bangpxe_call(int func) 545{ 546#ifdef PXE_DEBUG 547 if (pxe_debug) 548 printf("bangpxe_call %x\n", func); 549#endif 550 551 bzero(&v86, sizeof(v86)); 552 bzero(data_buffer, sizeof(data_buffer)); 553 554 __bangpxeseg = pxe_p->EntryPointSP.segment; 555 __bangpxeoff = pxe_p->EntryPointSP.offset; 556 557 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS; 558 v86.edx = VTOPSEG(scratch_buffer); 559 v86.eax = VTOPOFF(scratch_buffer); 560 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry); 561 v86.ebx = func; 562 v86int(); 563 v86.ctl = V86_FLAGS; 564} 565 566 567time_t 568getsecs() 569{ 570 time_t n = 0; 571 time(&n); 572 return n; 573} 574 575static int 576pxe_netif_match(struct netif *nif, void *machdep_hint) 577{ 578 return 1; 579} 580 581 582static int 583pxe_netif_probe(struct netif *nif, void *machdep_hint) 584{ 585 t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer; 586 587 if (pxe_call == NULL) 588 return -1; 589 590 bzero(udpopen_p, sizeof(*udpopen_p)); 591 udpopen_p->src_ip = bootplayer.yip; 592 pxe_call(PXENV_UDP_OPEN); 593 594 if (udpopen_p->status != 0) { 595 printf("pxe_netif_probe: failed %x\n", udpopen_p->status); 596 return -1; 597 } 598 return 0; 599} 600 601static void 602pxe_netif_end(struct netif *nif) 603{ 604 t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer; 605 bzero(udpclose_p, sizeof(*udpclose_p)); 606 607 pxe_call(PXENV_UDP_CLOSE); 608 if (udpclose_p->status != 0) 609 printf("pxe_end failed %x\n", udpclose_p->status); 610} 611 612static void 613pxe_netif_init(struct iodesc *desc, void *machdep_hint) 614{ 615 int i; 616 for (i = 0; i < 6; ++i) 617 desc->myea[i] = bootplayer.CAddr[i]; 618 desc->xid = bootplayer.ident; 619} 620 621static int 622pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout) 623{ 624 return len; 625} 626 627static int 628pxe_netif_put(struct iodesc *desc, void *pkt, size_t len) 629{ 630 return len; 631} 632 633ssize_t 634sendudp(struct iodesc *h, void *pkt, size_t len) 635{ 636 t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer; 637 bzero(udpwrite_p, sizeof(*udpwrite_p)); 638 639 udpwrite_p->ip = h->destip.s_addr; 640 udpwrite_p->dst_port = h->destport; 641 udpwrite_p->src_port = h->myport; 642 udpwrite_p->buffer_size = len; 643 udpwrite_p->buffer.segment = VTOPSEG(pkt); 644 udpwrite_p->buffer.offset = VTOPOFF(pkt); 645 646 if (netmask == 0 || SAMENET(myip, h->destip, netmask)) 647 udpwrite_p->gw = 0; 648 else 649 udpwrite_p->gw = gateip.s_addr; 650 651 pxe_call(PXENV_UDP_WRITE); 652 653#if 0 654 /* XXX - I dont know why we need this. */ 655 delay(1000); 656#endif 657 if (udpwrite_p->status != 0) { 658 /* XXX: This happens a lot. It shouldn't. */ 659 if (udpwrite_p->status != 1) 660 printf("sendudp failed %x\n", udpwrite_p->status); 661 return -1; 662 } 663 return len; 664} 665 666ssize_t 667readudp(struct iodesc *h, void *pkt, size_t len, time_t timeout) 668{ 669 t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer; 670 struct udphdr *uh = NULL; 671 672 uh = (struct udphdr *) pkt - 1; 673 bzero(udpread_p, sizeof(*udpread_p)); 674 675 udpread_p->dest_ip = h->myip.s_addr; 676 udpread_p->d_port = h->myport; 677 udpread_p->buffer_size = len; 678 udpread_p->buffer.segment = VTOPSEG(data_buffer); 679 udpread_p->buffer.offset = VTOPOFF(data_buffer); 680 681 pxe_call(PXENV_UDP_READ); 682 683#if 0 684 /* XXX - I dont know why we need this. */ 685 delay(1000); 686#endif 687 if (udpread_p->status != 0) { 688 /* XXX: This happens a lot. It shouldn't. */ 689 if (udpread_p->status != 1) 690 printf("readudp failed %x\n", udpread_p->status); 691 return -1; 692 } 693 bcopy(data_buffer, pkt, udpread_p->buffer_size); 694 uh->uh_sport = udpread_p->s_port; 695 return udpread_p->buffer_size; 696} 697