199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2000 Alfred Perlstein <alfred@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2000 Paul Saab <ps@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2000 John Baldwin <jhb@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdarg.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in_systm.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/udp.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <net.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netif.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <nfsv2.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <iodesc.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <bootp.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <bootstrap.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "btxv86.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "pxe.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Allocate the PXE buffers statically instead of sticking grimy fingers into
199767f8919635c4928607450d9e0abb932109ceToomas Soome * BTX's private data area. The scratch buffer is used to send information to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the PXE BIOS, and the data buffer is used to receive data from the PXE BIOS.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PXE_BUFFER_SIZE 0x2000
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PXE_TFTP_BUFFER_SIZE 512
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char scratch_buffer[PXE_BUFFER_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char data_buffer[PXE_BUFFER_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic pxenv_t *pxenv_p = NULL; /* PXENV+ */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic pxe_t *pxe_p = NULL; /* !PXE */
199767f8919635c4928607450d9e0abb932109ceToomas SoomeBOOTPLAYER bootplayer = {0}; /* PXE Cached information. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_debug = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_sock = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_opens = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid pxe_enable(void *pxeinfo);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void (*pxe_call)(int func);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void pxenv_call(int func);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void bangpxe_call(int func);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_init(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_strategy(void *devdata, int flag, daddr_t dblk,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t offset, size_t size, char *buf, size_t *rsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_open(struct open_file *f, ...);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_close(struct open_file *f);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_print(int verbose);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void pxe_cleanup(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void pxe_setnfshandle(char *rootpath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void pxe_perror(int error);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_netif_match(struct netif *nif, void *machdep_hint);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_netif_probe(struct netif *nif, void *machdep_hint);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void pxe_netif_init(struct iodesc *desc, void *machdep_hint);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_netif_get(struct iodesc *desc, void *pkt, size_t len,
199767f8919635c4928607450d9e0abb932109ceToomas Soome time_t timeout);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void pxe_netif_end(struct netif *nif);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef OLD_NFSV2
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint nfs_getrootfh(struct iodesc*, char*, u_char*);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint nfs_getrootfh(struct iodesc*, char*, uint32_t*, u_char*);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern struct netif_stats pxe_st[];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern u_int16_t __bangpxeseg;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern u_int16_t __bangpxeoff;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern void __bangpxeentry(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern u_int16_t __pxenvseg;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern u_int16_t __pxenvoff;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern void __pxenventry(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct netif_dif pxe_ifs[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* dif_unit dif_nsel dif_stats dif_private */
199767f8919635c4928607450d9e0abb932109ceToomas Soome {0, 1, &pxe_st[0], 0}
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct netif_stats pxe_st[NENTS(pxe_ifs)];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct netif_driver pxenetif = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "pxenet",
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_netif_match,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_netif_probe,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_netif_init,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_netif_get,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_netif_put,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_netif_end,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_ifs,
199767f8919635c4928607450d9e0abb932109ceToomas Soome NENTS(pxe_ifs)
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct netif_driver *netif_drivers[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome &pxenetif,
199767f8919635c4928607450d9e0abb932109ceToomas Soome NULL
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct devsw pxedisk = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "pxe",
199767f8919635c4928607450d9e0abb932109ceToomas Soome DEVT_NET,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_init,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_strategy,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_open,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_close,
199767f8919635c4928607450d9e0abb932109ceToomas Soome noioctl,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_print,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_cleanup
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This function is called by the loader to enable PXE support if we
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are booted by PXE. The passed in pointer is a pointer to the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * PXENV+ structure.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_enable(void *pxeinfo)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxenv_p = (pxenv_t *)pxeinfo;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxenv_p->PXEPtr.offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * return true if pxe structures are found/initialized,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * also figures out our IP information via the pxe cached info struct
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_init(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome t_PXENV_GET_CACHED_INFO *gci_p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int counter;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t checksum;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t *checkptr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if(pxenv_p == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* look for "PXENV+" */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxenv_p = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* make sure the size is something we can handle */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxenv_p->Length > sizeof(*pxenv_p)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("PXENV+ structure too large, ignoring\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxenv_p = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * do byte checksum:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * add up each byte in the structure, the total should be 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome checksum = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome checkptr = (uint8_t *) pxenv_p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (counter = 0; counter < pxenv_p->Length; counter++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome checksum += *checkptr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (checksum != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("PXENV+ structure failed checksum, ignoring\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxenv_p = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * PXENV+ passed, so use that if !PXE is not available or
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the checksum fails.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call = pxenv_call;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxenv_p->Version >= 0x0200) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (;;) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_p = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome checksum = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome checkptr = (uint8_t *)pxe_p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (counter = 0; counter < pxe_p->StructLength;
199767f8919635c4928607450d9e0abb932109ceToomas Soome counter++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome checksum += *checkptr++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (checksum != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_p = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call = bangpxe_call;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\nPXE version %d.%d, real mode entry point ",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uint8_t) (pxenv_p->Version >> 8),
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uint8_t) (pxenv_p->Version & 0xFF));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_call == bangpxe_call)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("@%04x:%04x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_p->EntryPointSP.segment,
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_p->EntryPointSP.offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("@%04x:%04x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome gci_p = (t_PXENV_GET_CACHED_INFO *) scratch_buffer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(gci_p, sizeof(*gci_p));
199767f8919635c4928607450d9e0abb932109ceToomas Soome gci_p->PacketType = PXENV_PACKET_TYPE_BINL_REPLY;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call(PXENV_GET_CACHED_INFO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (gci_p->Status != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_perror(gci_p->Status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_p = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
199767f8919635c4928607450d9e0abb932109ceToomas Soome &bootplayer, gci_p->BufferSize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_strategy(void *devdata, int flag, daddr_t dblk, size_t offset, size_t size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *buf, size_t *rsize)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_open(struct open_file *f, ...)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_list args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *devname; /* Device part of file name (or NULL). */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char temp[FNAME_SIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_start(args, f);
199767f8919635c4928607450d9e0abb932109ceToomas Soome devname = va_arg(args, char*);
199767f8919635c4928607450d9e0abb932109ceToomas Soome va_end(args);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* On first open, do netif open, mount, etc. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_opens == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Find network interface. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_sock < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_sock = netif_open(devname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_sock < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_open: netif_open() failed\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENXIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_open: netif_open() succeeded\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootip.s_addr == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Do a bootp/dhcp request to find out where our
199767f8919635c4928607450d9e0abb932109ceToomas Soome * NFS/TFTP server is. Even if we dont get back
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the proper information, fall back to the server
199767f8919635c4928607450d9e0abb932109ceToomas Soome * which brought us to life and a default rootpath.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome bootp(pxe_sock, BOOTP_PXE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootip.s_addr == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootip.s_addr = bootplayer.sip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_NFS_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!rootpath[0])
199767f8919635c4928607450d9e0abb932109ceToomas Soome strcpy(rootpath, PXENFSROOTPATH);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootpath[i] == ':')
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i && i != FNAME_SIZE && rootpath[i] == ':') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootpath[i++] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (inet_addr(&rootpath[0]) != INADDR_NONE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootip.s_addr = inet_addr(&rootpath[0]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.ip", inet_ntoa(myip), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.netmask", intoa(netmask), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bootplayer.Hardware == ETHER_TYPE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(temp, "%6D", bootplayer.CAddr, ":");
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.hwaddr", temp, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_NFS_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_open: server addr: %s\n", inet_ntoa(rootip));
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_open: server path: %s\n", rootpath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_open: gateway ip: %s\n", inet_ntoa(gateip));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.path", rootpath, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.netif.server", inet_ntoa(rootip), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.tftproot.path", rootpath, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("dhcp.host-name", hostname, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("pxeboot.ip", inet_ntoa(myip), 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bootplayer.Hardware == ETHER_TYPE) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(temp, "%6D", bootplayer.CAddr, ":");
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("pxeboot.hwaddr", temp, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_opens++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_devdata = &pxe_sock;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_close(struct open_file *f)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_close: opens=%d\n", pxe_opens);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* On last close, do netif close, etc. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome f->f_devdata = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Extra close call? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_opens <= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_opens--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Not last close? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_opens > 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_NFS_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* get an NFS filehandle for our root filesystem */
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_setnfshandle(rootpath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_sock >= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_close: calling netif_close()\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome netif_close(pxe_sock);
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_sock = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_print(int verbose)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char line[255];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_call == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(line, sizeof (line), " pxe0: %s:%s\n", inet_ntoa(rootip),
199767f8919635c4928607450d9e0abb932109ceToomas Soome rootpath);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (pager_output(line));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_cleanup(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome t_PXENV_UNLOAD_STACK *unload_stack_p =
199767f8919635c4928607450d9e0abb932109ceToomas Soome (t_PXENV_UNLOAD_STACK *)scratch_buffer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p =
199767f8919635c4928607450d9e0abb932109ceToomas Soome (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_call == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call(PXENV_UNDI_SHUTDOWN);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_debug && undi_shutdown_p->Status != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome undi_shutdown_p->Status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call(PXENV_UNLOAD_STACK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_debug && unload_stack_p->Status != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome unload_stack_p->Status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_perror(int err)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef LOADER_NFS_SUPPORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Reach inside the libstand NFS code and dig out an NFS handle
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for the root filesystem.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef OLD_NFSV2
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_iodesc {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *iodesc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_FHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* structure truncated here */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern struct nfs_iodesc nfs_root_node;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern int rpc_port;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_rpcmountcall()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(d = socktodesc(pxe_sock)))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->myport = htons(--rpc_port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->destip = rootip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error = nfs_getrootfh(d, rootpath, nfs_root_node.fh)) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("NFS MOUNT RPC error: %d\n", error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.iodesc = d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_setnfshandle(char *rootpath)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char *fh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[2 * NFS_FHSIZE + 3], *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If NFS files were never opened, we need to do mount call
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ourselves. Use nfs_root_node.iodesc as flag indicating
199767f8919635c4928607450d9e0abb932109ceToomas Soome * previous NFS usage.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nfs_root_node.iodesc == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_rpcmountcall();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fh = &nfs_root_node.fh[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[0] = 'X';
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = &buf[1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(cp, "%02x", fh[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(cp, "X");
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.nfshandle", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else /* !OLD_NFSV2 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NFS_V3MAXFHSIZE 64
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct nfs_iodesc {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *iodesc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome off_t off;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t fhsize;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char fh[NFS_V3MAXFHSIZE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* structure truncated */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern struct nfs_iodesc nfs_root_node;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeextern int rpc_port;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_rpcmountcall()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct iodesc *d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int error;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(d = socktodesc(pxe_sock)))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->myport = htons(--rpc_port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->destip = rootip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((error = nfs_getrootfh(d, rootpath, &nfs_root_node.fhsize,
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.fh)) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("NFS MOUNT RPC error: %d\n", error);
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.fhsize = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome nfs_root_node.iodesc = d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_setnfshandle(char *rootpath)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_char *fh;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[2 * NFS_V3MAXFHSIZE + 3], *cp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If NFS files were never opened, we need to do mount call
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ourselves. Use nfs_root_node.iodesc as flag indicating
199767f8919635c4928607450d9e0abb932109ceToomas Soome * previous NFS usage.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (nfs_root_node.iodesc == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_rpcmountcall();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fh = &nfs_root_node.fh[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[0] = 'X';
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = &buf[1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(cp, "%02x", fh[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(cp, "X");
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.nfshandle", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "%d", nfs_root_node.fhsize);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("boot.nfsroot.nfshandlelen", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* OLD_NFSV2 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* LOADER_NFS_SUPPORT */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxenv_call(int func)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxenv_call %x\n", func);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(&v86, sizeof(v86));
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(data_buffer, sizeof(data_buffer));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome __pxenvseg = pxenv_p->RMEntry.segment;
199767f8919635c4928607450d9e0abb932109ceToomas Soome __pxenvoff = pxenv_p->RMEntry.offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.es = VTOPSEG(scratch_buffer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.edi = VTOPOFF(scratch_buffer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ebx = func;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86int();
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ctl = V86_FLAGS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomebangpxe_call(int func)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef PXE_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("bangpxe_call %x\n", func);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(&v86, sizeof(v86));
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(data_buffer, sizeof(data_buffer));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome __bangpxeseg = pxe_p->EntryPointSP.segment;
199767f8919635c4928607450d9e0abb932109ceToomas Soome __bangpxeoff = pxe_p->EntryPointSP.offset;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.edx = VTOPSEG(scratch_buffer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.eax = VTOPOFF(scratch_buffer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ebx = func;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86int();
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ctl = V86_FLAGS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soometime_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomegetsecs()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome time_t n = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome time(&n);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_netif_match(struct netif *nif, void *machdep_hint)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_netif_probe(struct netif *nif, void *machdep_hint)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pxe_call == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(udpopen_p, sizeof(*udpopen_p));
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpopen_p->src_ip = bootplayer.yip;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call(PXENV_UDP_OPEN);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (udpopen_p->status != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_netif_probe: failed %x\n", udpopen_p->status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_netif_end(struct netif *nif)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(udpclose_p, sizeof(*udpclose_p));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call(PXENV_UDP_CLOSE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (udpclose_p->status != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("pxe_end failed %x\n", udpclose_p->status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_netif_init(struct iodesc *desc, void *machdep_hint)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < 6; ++i)
199767f8919635c4928607450d9e0abb932109ceToomas Soome desc->myea[i] = bootplayer.CAddr[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome desc->xid = bootplayer.ident;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomepxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomessize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomesendudp(struct iodesc *h, void *pkt, size_t len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(udpwrite_p, sizeof(*udpwrite_p));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpwrite_p->ip = h->destip.s_addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpwrite_p->dst_port = h->destport;
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpwrite_p->src_port = h->myport;
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpwrite_p->buffer_size = len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpwrite_p->buffer.segment = VTOPSEG(pkt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpwrite_p->buffer.offset = VTOPOFF(pkt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (netmask == 0 || SAMENET(myip, h->destip, netmask))
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpwrite_p->gw = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpwrite_p->gw = gateip.s_addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call(PXENV_UDP_WRITE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX - I dont know why we need this. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome delay(1000);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (udpwrite_p->status != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX: This happens a lot. It shouldn't. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (udpwrite_p->status != 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("sendudp failed %x\n", udpwrite_p->status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomessize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomereadudp(struct iodesc *h, void *pkt, size_t len, time_t timeout)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct udphdr *uh = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome uh = (struct udphdr *) pkt - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(udpread_p, sizeof(*udpread_p));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpread_p->dest_ip = h->myip.s_addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpread_p->d_port = h->myport;
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpread_p->buffer_size = len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpread_p->buffer.segment = VTOPSEG(data_buffer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome udpread_p->buffer.offset = VTOPOFF(data_buffer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pxe_call(PXENV_UDP_READ);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX - I dont know why we need this. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome delay(1000);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (udpread_p->status != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX: This happens a lot. It shouldn't. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (udpread_p->status != 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("readudp failed %x\n", udpread_p->status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome bcopy(data_buffer, pkt, udpread_p->buffer_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome uh->uh_sport = udpread_p->s_port;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return udpread_p->buffer_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}