17c478bdstevel@tonic-gate/**************************************************************************
27c478bdstevel@tonic-gateEtherboot -  BOOTP/TFTP Bootstrap Program
37c478bdstevel@tonic-gateUNDI NIC driver for Etherboot
47c478bdstevel@tonic-gate
57c478bdstevel@tonic-gateThis file Copyright (C) 2003 Michael Brown <mbrown@fensystems.co.uk>
67c478bdstevel@tonic-gateof Fen Systems Ltd. (http://www.fensystems.co.uk/).  All rights
77c478bdstevel@tonic-gatereserved.
87c478bdstevel@tonic-gate
97c478bdstevel@tonic-gate$Id: undi.c,v 1.8 2003/10/25 13:54:53 mcb30 Exp $
107c478bdstevel@tonic-gate***************************************************************************/
117c478bdstevel@tonic-gate
127c478bdstevel@tonic-gate/*
137c478bdstevel@tonic-gate * This program is free software; you can redistribute it and/or
147c478bdstevel@tonic-gate * modify it under the terms of the GNU General Public License as
157c478bdstevel@tonic-gate * published by the Free Software Foundation; either version 2, or (at
167c478bdstevel@tonic-gate * your option) any later version.
177c478bdstevel@tonic-gate */
187c478bdstevel@tonic-gate
197c478bdstevel@tonic-gate/* to get some global routines like printf */
207c478bdstevel@tonic-gate#include "etherboot.h"
217c478bdstevel@tonic-gate/* to get the interface to the body of the program */
227c478bdstevel@tonic-gate#include "nic.h"
237c478bdstevel@tonic-gate/* to get the PCI support functions, if this is a PCI NIC */
247c478bdstevel@tonic-gate#include "pci.h"
257c478bdstevel@tonic-gate/* UNDI and PXE defines.  Includes pxe.h. */
267c478bdstevel@tonic-gate#include "undi.h"
277c478bdstevel@tonic-gate/* 8259 PIC defines */
287c478bdstevel@tonic-gate#include "pic8259.h"
2999ed608szhou#include "bootp.h"
3099ed608szhou#include "tftp.h"
3199ed608szhou#include "shared.h"
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate/* NIC specific static variables go here */
347c478bdstevel@tonic-gatestatic undi_t undi = { NULL, NULL, NULL, NULL, NULL, NULL, NULL,
357c478bdstevel@tonic-gate		       NULL, NULL, 0, NULL, 0, NULL,
367c478bdstevel@tonic-gate		       0, 0, 0, 0,
377c478bdstevel@tonic-gate		       { 0, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 0, NULL },
387c478bdstevel@tonic-gate		       IRQ_NONE };
397c478bdstevel@tonic-gatestatic undi_base_mem_data_t undi_base_mem_data;
407c478bdstevel@tonic-gate
417c478bdstevel@tonic-gate#define UNDI_HEAP (void *)(512 << 10)
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate/* Function prototypes */
447c478bdstevel@tonic-gateint allocate_base_mem_data ( void );
457c478bdstevel@tonic-gateint free_base_mem_data ( void );
467c478bdstevel@tonic-gateint eb_pxenv_undi_shutdown ( void );
477c478bdstevel@tonic-gateint eb_pxenv_stop_undi ( void );
487c478bdstevel@tonic-gateint undi_unload_base_code ( void );
497c478bdstevel@tonic-gateint undi_full_shutdown ( void );
5099ed608szhouint eb_pxenv_get_cached_info (uint8_t, void **info);
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate/**************************************************************************
537c478bdstevel@tonic-gate * Utility functions
547c478bdstevel@tonic-gate **************************************************************************/
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate/* Checksum a block.
577c478bdstevel@tonic-gate */
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gateuint8_t checksum ( void *block, size_t size ) {
607c478bdstevel@tonic-gate	uint8_t sum = 0;
617c478bdstevel@tonic-gate	uint16_t i = 0;
627c478bdstevel@tonic-gate	for ( i = 0; i < size; i++ ) {
637c478bdstevel@tonic-gate		sum += ( ( uint8_t * ) block )[i];
647c478bdstevel@tonic-gate	}
657c478bdstevel@tonic-gate	return sum;
667c478bdstevel@tonic-gate}
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate/* Print the status of a !PXE structure
697c478bdstevel@tonic-gate */
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gatevoid pxe_dump ( void ) {
727c478bdstevel@tonic-gate#ifdef TRACE_UNDI
737c478bdstevel@tonic-gate	printf ( "API %hx:%hx St %hx:%hx UD %hx:%hx UC %hx:%hx "
747c478bdstevel@tonic-gate		 "BD %hx:%hx BC %hx:%hx\n",
757c478bdstevel@tonic-gate		 undi.pxe->EntryPointSP.segment, undi.pxe->EntryPointSP.offset,
767c478bdstevel@tonic-gate		 undi.pxe->Stack.Seg_Addr, undi.pxe->Stack.Seg_Size,
777c478bdstevel@tonic-gate		 undi.pxe->UNDIData.Seg_Addr, undi.pxe->UNDIData.Seg_Size,
787c478bdstevel@tonic-gate		 undi.pxe->UNDICode.Seg_Addr, undi.pxe->UNDICode.Seg_Size,
797c478bdstevel@tonic-gate		 undi.pxe->BC_Data.Seg_Addr, undi.pxe->BC_Data.Seg_Size,
807c478bdstevel@tonic-gate		 undi.pxe->BC_Code.Seg_Addr, undi.pxe->BC_Code.Seg_Size );
817c478bdstevel@tonic-gate#endif
827c478bdstevel@tonic-gate}
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate/* Allocate/free space for structures that must reside in base memory
857c478bdstevel@tonic-gate */
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gateint allocate_base_mem_data ( void ) {
887c478bdstevel@tonic-gate	/* In GRUB, anything is in base address, so we do not need
897c478bdstevel@tonic-gate	 * allocate anything */
907c478bdstevel@tonic-gate	undi.base_mem_data = &undi_base_mem_data;
917c478bdstevel@tonic-gate	memset ( undi.base_mem_data, 0, sizeof(undi_base_mem_data_t) );
927c478bdstevel@tonic-gate	undi.undi_call_info = &undi.base_mem_data->undi_call_info;
937c478bdstevel@tonic-gate	undi.pxs = &undi.base_mem_data->pxs;
947c478bdstevel@tonic-gate	undi.xmit_data = &undi.base_mem_data->xmit_data;
957c478bdstevel@tonic-gate	undi.xmit_buffer = undi.base_mem_data->xmit_buffer;
967c478bdstevel@tonic-gate#if 0				/* Etherboot Code */
977c478bdstevel@tonic-gate	/* Allocate space in base memory.
987c478bdstevel@tonic-gate	 * Initialise pointers to base memory structures.
997c478bdstevel@tonic-gate	 */
1007c478bdstevel@tonic-gate	if ( undi.base_mem_data == NULL ) {
1017c478bdstevel@tonic-gate		undi.base_mem_data =
1027c478bdstevel@tonic-gate			allot_base_memory ( sizeof(undi_base_mem_data_t) +
1037c478bdstevel@tonic-gate					    TRIVIAL_IRQ_HANDLER_SIZE );
1047c478bdstevel@tonic-gate		if ( undi.base_mem_data == NULL ) {
1057c478bdstevel@tonic-gate			printf ( "Failed to allocate base memory\n" );
1067c478bdstevel@tonic-gate			free_base_mem_data();
1077c478bdstevel@tonic-gate			return 0;
1087c478bdstevel@tonic-gate		}
1097c478bdstevel@tonic-gate		memset ( undi.base_mem_data, 0, sizeof(undi_base_mem_data_t) );
1107c478bdstevel@tonic-gate		undi.undi_call_info = &undi.base_mem_data->undi_call_info;
1117c478bdstevel@tonic-gate		undi.pxs = &undi.base_mem_data->pxs;
1127c478bdstevel@tonic-gate		undi.xmit_data = &undi.base_mem_data->xmit_data;
1137c478bdstevel@tonic-gate		undi.xmit_buffer = undi.base_mem_data->xmit_buffer;
1147c478bdstevel@tonic-gate		copy_trivial_irq_handler ( undi.base_mem_data->irq_handler,
1157c478bdstevel@tonic-gate					   TRIVIAL_IRQ_HANDLER_SIZE );
1167c478bdstevel@tonic-gate	}
1177c478bdstevel@tonic-gate#endif	/* Etherboot Code */
1187c478bdstevel@tonic-gate	return 1;
1197c478bdstevel@tonic-gate}
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gateint free_base_mem_data ( void ) {
1227c478bdstevel@tonic-gate	/* Just pretend to free something :-) */
1237c478bdstevel@tonic-gate	undi.base_mem_data = NULL;
1247c478bdstevel@tonic-gate	undi.undi_call_info = NULL;
1257c478bdstevel@tonic-gate	undi.pxs = NULL;
1267c478bdstevel@tonic-gate	undi.xmit_data = NULL;
1277c478bdstevel@tonic-gate	undi.xmit_buffer = NULL;
1287c478bdstevel@tonic-gate#if 0				/* Etherboot Code */
1297c478bdstevel@tonic-gate	if ( undi.base_mem_data != NULL ) {
1307c478bdstevel@tonic-gate		forget_base_memory ( undi.base_mem_data,
1317c478bdstevel@tonic-gate				     sizeof(undi_base_mem_data_t) +
1327c478bdstevel@tonic-gate				     TRIVIAL_IRQ_HANDLER_SIZE );
1337c478bdstevel@tonic-gate		undi.base_mem_data = NULL;
1347c478bdstevel@tonic-gate		undi.undi_call_info = NULL;
1357c478bdstevel@tonic-gate		undi.pxs = NULL;
1367c478bdstevel@tonic-gate		undi.xmit_data = NULL;
1377c478bdstevel@tonic-gate		undi.xmit_buffer = NULL;
1387c478bdstevel@tonic-gate		copy_trivial_irq_handler ( NULL, 0 );
1397c478bdstevel@tonic-gate	}
1407c478bdstevel@tonic-gate#endif	/* Etherboot Code */
1417c478bdstevel@tonic-gate	return 1;
1427c478bdstevel@tonic-gate}
1437c478bdstevel@tonic-gate
1447c478bdstevel@tonic-gatevoid assemble_firing_squad ( firing_squad_lineup_t *lineup,
1457c478bdstevel@tonic-gate			     void *start, size_t size,
1467c478bdstevel@tonic-gate			     firing_squad_shoot_t shoot ) {
1477c478bdstevel@tonic-gate	int target;
1487c478bdstevel@tonic-gate	int index;
1497c478bdstevel@tonic-gate	int bit;
1507c478bdstevel@tonic-gate	int start_kb = virt_to_phys(start) >> 10;
1517c478bdstevel@tonic-gate	int end_kb = ( virt_to_phys(start+size) + (1<<10) - 1 ) >> 10;
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate	for ( target = start_kb; target <= end_kb; target++ ) {
1547c478bdstevel@tonic-gate		index = FIRING_SQUAD_TARGET_INDEX ( target );
1557c478bdstevel@tonic-gate		bit = FIRING_SQUAD_TARGET_BIT ( target );
1567c478bdstevel@tonic-gate		lineup->targets[index] = ( shoot << bit ) |
1577c478bdstevel@tonic-gate			( lineup->targets[index] & ~( 1 << bit ) );
1587c478bdstevel@tonic-gate	}
1597c478bdstevel@tonic-gate}
1607c478bdstevel@tonic-gate
1617c478bdstevel@tonic-gatevoid shoot_targets ( firing_squad_lineup_t *lineup ) {
1627c478bdstevel@tonic-gate	int shoot_this_target = 0;
1637c478bdstevel@tonic-gate	int shoot_last_target = 0;
1647c478bdstevel@tonic-gate	int start_target = 0;
1657c478bdstevel@tonic-gate	int target;
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate	for ( target = 0; target <= 640; target++ ) {
1687c478bdstevel@tonic-gate		shoot_this_target = ( target == 640 ? 0 :
1697c478bdstevel@tonic-gate		      ( 1 << FIRING_SQUAD_TARGET_BIT(target) ) &
1707c478bdstevel@tonic-gate		      lineup->targets[FIRING_SQUAD_TARGET_INDEX(target)] );
1717c478bdstevel@tonic-gate		if ( shoot_this_target && !shoot_last_target ) {
1727c478bdstevel@tonic-gate			start_target = target;
1737c478bdstevel@tonic-gate		} else if ( shoot_last_target && !shoot_this_target ) {
1747c478bdstevel@tonic-gate			size_t range_size = ( target - start_target ) << 10;
1757c478bdstevel@tonic-gate			forget_base_memory ( phys_to_virt( start_target<<10 ),
1767c478bdstevel@tonic-gate					     range_size );
1777c478bdstevel@tonic-gate		}
1787c478bdstevel@tonic-gate		shoot_last_target = shoot_this_target;
1797c478bdstevel@tonic-gate	}
1807c478bdstevel@tonic-gate}
1817c478bdstevel@tonic-gate
1827c478bdstevel@tonic-gate/* Debug macros
1837c478bdstevel@tonic-gate */
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate#ifdef TRACE_UNDI
1867c478bdstevel@tonic-gate#define DBG(...) printf ( __VA_ARGS__ )
1877c478bdstevel@tonic-gate#else
1887c478bdstevel@tonic-gate#define DBG(...)
1897c478bdstevel@tonic-gate#endif
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gate#define UNDI_STATUS(pxs) ( (pxs)->Status == PXENV_EXIT_SUCCESS ? \
1927c478bdstevel@tonic-gate			      "SUCCESS" : \
1937c478bdstevel@tonic-gate			      ( (pxs)->Status == PXENV_EXIT_FAILURE ? \
1947c478bdstevel@tonic-gate				"FAILURE" : "UNKNOWN" ) )
1957c478bdstevel@tonic-gate
1967c478bdstevel@tonic-gate/**************************************************************************
1977c478bdstevel@tonic-gate * Base memory scanning functions
1987c478bdstevel@tonic-gate **************************************************************************/
1997c478bdstevel@tonic-gate
2007c478bdstevel@tonic-gate/* Locate the $PnP structure indicating a PnP BIOS.
2017c478bdstevel@tonic-gate */
2027c478bdstevel@tonic-gate
2037c478bdstevel@tonic-gateint hunt_pnp_bios ( void ) {
2047c478bdstevel@tonic-gate	uint32_t off = 0x10000;
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate	DBG ( "Hunting for PnP BIOS..." );
2077c478bdstevel@tonic-gate	while ( off > 0 ) {
2087c478bdstevel@tonic-gate		off -= 16;
2097c478bdstevel@tonic-gate		undi.pnp_bios = (pnp_bios_t *) phys_to_virt ( 0xf0000 + off );
2107c478bdstevel@tonic-gate		if ( undi.pnp_bios->signature == PNP_BIOS_SIGNATURE ) {
2117c478bdstevel@tonic-gate			DBG ( "found $PnP at f000:%hx...", off );
2127c478bdstevel@tonic-gate			if ( checksum(undi.pnp_bios,sizeof(pnp_bios_t)) !=0) {
2137c478bdstevel@tonic-gate				DBG ( "invalid checksum\n..." );
2147c478bdstevel@tonic-gate				continue;
2157c478bdstevel@tonic-gate			}
2167c478bdstevel@tonic-gate			DBG ( "ok\n" );
2177c478bdstevel@tonic-gate			return 1;
2187c478bdstevel@tonic-gate		}
2197c478bdstevel@tonic-gate	}
2207c478bdstevel@tonic-gate	DBG ( "none found\n" );
2217c478bdstevel@tonic-gate	undi.pnp_bios = NULL;
2227c478bdstevel@tonic-gate	return 0;
2237c478bdstevel@tonic-gate}
2247c478bdstevel@tonic-gate
2257c478bdstevel@tonic-gate/* Locate the !PXE structure indicating a loaded UNDI driver.
2267c478bdstevel@tonic-gate */
2277c478bdstevel@tonic-gate
2287c478bdstevel@tonic-gateint hunt_pixie ( void ) {
2297c478bdstevel@tonic-gate	static uint32_t ptr = 0;
2307c478bdstevel@tonic-gate	pxe_t *pxe = NULL;
2317c478bdstevel@tonic-gate
2327c478bdstevel@tonic-gate	DBG ( "Hunting for pixies..." );
2337c478bdstevel@tonic-gate	if ( ptr == 0 ) ptr = 0xa0000;
2347c478bdstevel@tonic-gate	while ( ptr > 0x10000 ) {
2357c478bdstevel@tonic-gate		ptr -= 16;
2367c478bdstevel@tonic-gate		pxe = (pxe_t *) phys_to_virt ( ptr );
2377c478bdstevel@tonic-gate		if ( memcmp ( pxe->Signature, "!PXE", 4 ) == 0 ) {
2387c478bdstevel@tonic-gate			DBG ( "found !PXE at %x...", ptr );
2397c478bdstevel@tonic-gate			if ( checksum ( pxe, sizeof(pxe_t) ) != 0 ) {
2407c478bdstevel@tonic-gate				DBG ( "invalid checksum\n..." );
2417c478bdstevel@tonic-gate				continue;
2427c478bdstevel@tonic-gate			}
2437c478bdstevel@tonic-gate			if ( ptr < get_free_base_memory() ) {
2447c478bdstevel@tonic-gate				DBG ( "in free base memory!\n\n"
2457c478bdstevel@tonic-gate					 "WARNING: a valid !PXE structure was "
2467c478bdstevel@tonic-gate					 "found in an area of memory marked "
2477c478bdstevel@tonic-gate					 "as free!\n\n" );
2487c478bdstevel@tonic-gate				undi.pxe = pxe;
2497c478bdstevel@tonic-gate				pxe_dump();
2507c478bdstevel@tonic-gate				undi.pxe = NULL;
2517c478bdstevel@tonic-gate				DBG ( "\nIgnoring and continuing, but this "
2527c478bdstevel@tonic-gate					 "may cause problems later!\n\n" );
2537c478bdstevel@tonic-gate				continue;
2547c478bdstevel@tonic-gate			}
2557c478bdstevel@tonic-gate			DBG ( "ok\n" );
2567c478bdstevel@tonic-gate			undi.pxe = pxe;
2577c478bdstevel@tonic-gate			pxe_dump();
2587c478bdstevel@tonic-gate			DBG ( "Resetting pixie...\n" );
2597c478bdstevel@tonic-gate			undi_unload_base_code();
2607c478bdstevel@tonic-gate			eb_pxenv_stop_undi();
2617c478bdstevel@tonic-gate			pxe_dump();
2627c478bdstevel@tonic-gate			return 1;
2637c478bdstevel@tonic-gate		}
2647c478bdstevel@tonic-gate	}
2657c478bdstevel@tonic-gate	DBG ( "none found\n" );
2667c478bdstevel@tonic-gate	ptr = 0;
2677c478bdstevel@tonic-gate	return 0;
2687c478bdstevel@tonic-gate}
2697c478bdstevel@tonic-gate
2707c478bdstevel@tonic-gate/* Locate PCI PnP ROMs.
2717c478bdstevel@tonic-gate */
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gateint hunt_rom ( void ) {
2747c478bdstevel@tonic-gate	static uint32_t ptr = 0;
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate	DBG ( "Hunting for ROMs..." );
2777c478bdstevel@tonic-gate	if ( ptr == 0 ) ptr = 0x100000;
2787c478bdstevel@tonic-gate	while ( ptr > 0x0c0000 ) {
2797c478bdstevel@tonic-gate		ptr -= 0x800;
2807c478bdstevel@tonic-gate		undi.rom = ( rom_t * ) phys_to_virt ( ptr );
2817c478bdstevel@tonic-gate		if ( undi.rom->signature == ROM_SIGNATURE ) {
2827c478bdstevel@tonic-gate			pcir_header_t *pcir_header = NULL;
2837c478bdstevel@tonic-gate			pnp_header_t *pnp_header = NULL;
2847c478bdstevel@tonic-gate
2857c478bdstevel@tonic-gate			DBG ( "found 55AA at %x...", ptr );
2867c478bdstevel@tonic-gate			if ( undi.rom->pcir_off == 0 ) {
2877c478bdstevel@tonic-gate				DBG ( "not a PCI ROM\n..." );
2887c478bdstevel@tonic-gate				continue;
2897c478bdstevel@tonic-gate			}
2907c478bdstevel@tonic-gate			pcir_header = (pcir_header_t*)( ( void * ) undi.rom +
2917c478bdstevel@tonic-gate							undi.rom->pcir_off );
2927c478bdstevel@tonic-gate			if ( pcir_header->signature != PCIR_SIGNATURE ) {
2937c478bdstevel@tonic-gate				DBG ( "invalid PCI signature\n..." );
2947c478bdstevel@tonic-gate				continue;
2957c478bdstevel@tonic-gate			}
2967c478bdstevel@tonic-gate			DBG ( "PCI:%hx:%hx...", pcir_header->vendor_id,
2977c478bdstevel@tonic-gate				 pcir_header->device_id );
2987c478bdstevel@tonic-gate			if ( ( pcir_header->vendor_id != undi.pci.vendor ) ||
2997c478bdstevel@tonic-gate			     ( pcir_header->device_id != undi.pci.dev_id ) ) {
3007c478bdstevel@tonic-gate				DBG ( "not me (%hx:%hx)\n...",
3017c478bdstevel@tonic-gate					 undi.pci.vendor,
3027c478bdstevel@tonic-gate					 undi.pci.dev_id );
3037c478bdstevel@tonic-gate				continue;
3047c478bdstevel@tonic-gate			}
3057c478bdstevel@tonic-gate			if ( undi.rom->pnp_off == 0 ) {
3067c478bdstevel@tonic-gate				DBG ( "not a PnP ROM\n..." );
3077c478bdstevel@tonic-gate				continue;
3087c478bdstevel@tonic-gate			}
3097c478bdstevel@tonic-gate			pnp_header = (pnp_header_t*)( ( void * ) undi.rom +
3107c478bdstevel@tonic-gate							 undi.rom->pnp_off );
3117c478bdstevel@tonic-gate			if ( pnp_header->signature != PNP_SIGNATURE ) {
3127c478bdstevel@tonic-gate				DBG ( "invalid $PnP signature\n..." );
3137c478bdstevel@tonic-gate				continue;
3147c478bdstevel@tonic-gate			}
3157c478bdstevel@tonic-gate			if ( checksum(pnp_header,sizeof(pnp_header_t)) != 0 ) {
3167c478bdstevel@tonic-gate				DBG ( "invalid PnP checksum\n..." );
3177c478bdstevel@tonic-gate				continue;
3187c478bdstevel@tonic-gate			}
3197c478bdstevel@tonic-gate			DBG ( "ok\n");
3207c478bdstevel@tonic-gate			printf ("ROM %s by %s\n",
3217c478bdstevel@tonic-gate				 pnp_header->product_str_off==0 ? "(unknown)" :
3227c478bdstevel@tonic-gate				 (void*)undi.rom+pnp_header->product_str_off,
3237c478bdstevel@tonic-gate				 pnp_header->manuf_str_off==0 ? "(unknown)" :
3247c478bdstevel@tonic-gate				 (void*)undi.rom+pnp_header->manuf_str_off );
3257c478bdstevel@tonic-gate			return 1;
3267c478bdstevel@tonic-gate		}
3277c478bdstevel@tonic-gate	}
3287c478bdstevel@tonic-gate	DBG ( "none found\n" );
3297c478bdstevel@tonic-gate	ptr = 0;
3307c478bdstevel@tonic-gate	undi.rom = NULL;
3317c478bdstevel@tonic-gate	return 0;
3327c478bdstevel@tonic-gate}
3337c478bdstevel@tonic-gate
3347c478bdstevel@tonic-gate/* Locate ROMs containing UNDI drivers.
3357c478bdstevel@tonic-gate */
3367c478bdstevel@tonic-gate
3377c478bdstevel@tonic-gateint hunt_undi_rom ( void ) {
3387c478bdstevel@tonic-gate	while ( hunt_rom() ) {
3397c478bdstevel@tonic-gate		if ( undi.rom->undi_rom_id_off == 0 ) {
3407c478bdstevel@tonic-gate			DBG ( "Not a PXE ROM\n" );
3417c478bdstevel@tonic-gate			continue;
3427c478bdstevel@tonic-gate		}
3437c478bdstevel@tonic-gate		undi.undi_rom_id = (undi_rom_id_t *)
3447c478bdstevel@tonic-gate			( (void *)undi.rom + undi.rom->undi_rom_id_off );
3457c478bdstevel@tonic-gate		if ( undi.undi_rom_id->signature != UNDI_SIGNATURE ) {
3467c478bdstevel@tonic-gate			DBG ( "Invalid UNDI signature\n" );
3477c478bdstevel@tonic-gate			continue;
3487c478bdstevel@tonic-gate		}
3497c478bdstevel@tonic-gate		printf ( "Revision %d.%d.%d",
3507c478bdstevel@tonic-gate			 undi.undi_rom_id->undi_rev[2],
3517c478bdstevel@tonic-gate			 undi.undi_rom_id->undi_rev[1],
3527c478bdstevel@tonic-gate			 undi.undi_rom_id->undi_rev[0] );
3537c478bdstevel@tonic-gate		return 1;
3547c478bdstevel@tonic-gate	}
3557c478bdstevel@tonic-gate	return 0;
3567c478bdstevel@tonic-gate}
3577c478bdstevel@tonic-gate
3587c478bdstevel@tonic-gate/**************************************************************************
3597c478bdstevel@tonic-gate * Low-level UNDI API call wrappers
3607c478bdstevel@tonic-gate **************************************************************************/
3617c478bdstevel@tonic-gate
3627c478bdstevel@tonic-gate/* Make a real-mode UNDI API call to the UNDI routine at
3637c478bdstevel@tonic-gate * routine_seg:routine_off, passing in three uint16 parameters on the
3647c478bdstevel@tonic-gate * real-mode stack.
3657c478bdstevel@tonic-gate * Calls the assembler wrapper routine __undi_call.
3667c478bdstevel@tonic-gate */
3677c478bdstevel@tonic-gate
3687c478bdstevel@tonic-gatestatic inline PXENV_EXIT_t _undi_call ( uint16_t routine_seg,
3697c478bdstevel@tonic-gate					uint16_t routine_off, uint16_t st0,
3707c478bdstevel@tonic-gate					uint16_t st1, uint16_t st2 ) {
3717c478bdstevel@tonic-gate	PXENV_EXIT_t ret = PXENV_EXIT_FAILURE;
3727c478bdstevel@tonic-gate
3737c478bdstevel@tonic-gate	undi.undi_call_info->routine.segment = routine_seg;
3747c478bdstevel@tonic-gate	undi.undi_call_info->routine.offset = routine_off;
3757c478bdstevel@tonic-gate	undi.undi_call_info->stack[0] = st0;
3767c478bdstevel@tonic-gate	undi.undi_call_info->stack[1] = st1;
3777c478bdstevel@tonic-gate	undi.undi_call_info->stack[2] = st2;
3787c478bdstevel@tonic-gate	ret = __undi_call ( SEGMENT( undi.undi_call_info ),
3797c478bdstevel@tonic-gate			    OFFSET( undi.undi_call_info ) );
3807c478bdstevel@tonic-gate
3817c478bdstevel@tonic-gate	/* UNDI API calls may rudely change the status of A20 and not
3827c478bdstevel@tonic-gate	 * bother to restore it afterwards.  Intel is known to be
3837c478bdstevel@tonic-gate	 * guilty of this.
3847c478bdstevel@tonic-gate	 *
3857c478bdstevel@tonic-gate	 * Note that we will return to this point even if A20 gets
3867c478bdstevel@tonic-gate	 * screwed up by the UNDI driver, because Etherboot always
3877c478bdstevel@tonic-gate	 * resides in an even megabyte of RAM.
3887c478bdstevel@tonic-gate	 */
3897c478bdstevel@tonic-gate	gateA20_set();
3907c478bdstevel@tonic-gate
3917c478bdstevel@tonic-gate	return ret;
3927c478bdstevel@tonic-gate}
3937c478bdstevel@tonic-gate
3947c478bdstevel@tonic-gate/* Make a real-mode call to the UNDI loader routine at
3957c478bdstevel@tonic-gate * routine_seg:routine_off, passing in the seg:off address of a
3967c478bdstevel@tonic-gate * pxenv_structure on the real-mode stack.
3977c478bdstevel@tonic-gate */
3987c478bdstevel@tonic-gate
3997c478bdstevel@tonic-gateint undi_call_loader ( void ) {
4007c478bdstevel@tonic-gate	PXENV_EXIT_t pxenv_exit = PXENV_EXIT_FAILURE;
4017c478bdstevel@tonic-gate
4027c478bdstevel@tonic-gate	pxenv_exit = _undi_call ( SEGMENT( undi.rom ),
4037c478bdstevel@tonic-gate				  undi.undi_rom_id->undi_loader_off,
4047c478bdstevel@tonic-gate				  OFFSET( undi.pxs ),
4057c478bdstevel@tonic-gate				  SEGMENT( undi.pxs ),
4067c478bdstevel@tonic-gate				  0 /* Unused for UNDI loader API */ );
4077c478bdstevel@tonic-gate	/* Return 1 for success, to be consistent with other routines */
4087c478bdstevel@tonic-gate	if ( pxenv_exit == PXENV_EXIT_SUCCESS ) return 1;
4097c478bdstevel@tonic-gate	DBG ( "UNDI loader call failed with status %#hx\n",
4107c478bdstevel@tonic-gate		 undi.pxs->Status );
4117c478bdstevel@tonic-gate	return 0;
4127c478bdstevel@tonic-gate}
4137c478bdstevel@tonic-gate
4147c478bdstevel@tonic-gate/* Make a real-mode UNDI API call, passing in the opcode and the
4157c478bdstevel@tonic-gate * seg:off address of a pxenv_structure on the real-mode stack.
4167c478bdstevel@tonic-gate *
4177c478bdstevel@tonic-gate * Two versions: undi_call() will automatically report any failure
4187c478bdstevel@tonic-gate * codes, undi_call_silent() will not.
4197c478bdstevel@tonic-gate */
4207c478bdstevel@tonic-gate
4217c478bdstevel@tonic-gateint undi_call_silent ( uint16_t opcode ) {
4227c478bdstevel@tonic-gate	PXENV_EXIT_t pxenv_exit = PXENV_EXIT_FAILURE;
4237c478bdstevel@tonic-gate
4247c478bdstevel@tonic-gate	pxenv_exit = _undi_call ( undi.pxe->EntryPointSP.segment,
4257c478bdstevel@tonic-gate				  undi.pxe->EntryPointSP.offset,
4267c478bdstevel@tonic-gate				  opcode,
4277c478bdstevel@tonic-gate				  OFFSET( undi.pxs ),
4287c478bdstevel@tonic-gate				  SEGMENT( undi.pxs ) );
4297c478bdstevel@tonic-gate	/* Return 1 for success, to be consistent with other routines */
4307c478bdstevel@tonic-gate	return pxenv_exit == PXENV_EXIT_SUCCESS ? 1 : 0;
4317c478bdstevel@tonic-gate}
4327c478bdstevel@tonic-gate
4337c478bdstevel@tonic-gateint undi_call ( uint16_t opcode ) {
4347c478bdstevel@tonic-gate	if ( undi_call_silent ( opcode ) ) return 1;
4357c478bdstevel@tonic-gate	DBG ( "UNDI API call %#hx failed with status %#hx\n",
4367c478bdstevel@tonic-gate		 opcode, undi.pxs->Status );
4377c478bdstevel@tonic-gate	return 0;
4387c478bdstevel@tonic-gate}
4397c478bdstevel@tonic-gate
4407c478bdstevel@tonic-gate/**************************************************************************
4417c478bdstevel@tonic-gate * High-level UNDI API call wrappers
4427c478bdstevel@tonic-gate **************************************************************************/
4437c478bdstevel@tonic-gate
4447c478bdstevel@tonic-gate/* Install the UNDI driver from a located UNDI ROM.
4457c478bdstevel@tonic-gate */
4467c478bdstevel@tonic-gate
4477c478bdstevel@tonic-gateint undi_loader ( void ) {
4487c478bdstevel@tonic-gate	pxe_t *pxe = NULL;
4497c478bdstevel@tonic-gate
4507c478bdstevel@tonic-gate	/* AX contains PCI bus:devfn (PCI specification) */
4517c478bdstevel@tonic-gate	undi.pxs->loader.ax = ( undi.pci.bus << 8 ) | undi.pci.devfn;
4527c478bdstevel@tonic-gate	/* BX and DX set to 0xffff for non-ISAPnP devices
4537c478bdstevel@tonic-gate	 * (BIOS boot specification)
4547c478bdstevel@tonic-gate	 */
4557c478bdstevel@tonic-gate	undi.pxs->loader.bx = 0xffff;
4567c478bdstevel@tonic-gate	undi.pxs->loader.dx = 0xffff;
4577c478bdstevel@tonic-gate	/* ES:DI points to PnP BIOS' $PnP structure
4587c478bdstevel@tonic-gate	 * (BIOS boot specification)
4597c478bdstevel@tonic-gate	 */
4607c478bdstevel@tonic-gate	undi.pxs->loader.es = 0xf000;
4617c478bdstevel@tonic-gate	undi.pxs->loader.di = virt_to_phys ( undi.pnp_bios ) - 0xf0000;
4627c478bdstevel@tonic-gate
4637c478bdstevel@tonic-gate	/* Allocate space for UNDI driver's code and data segments */
4647c478bdstevel@tonic-gate	undi.driver_code_size = undi.undi_rom_id->code_size;
4657c478bdstevel@tonic-gate	undi.driver_code = UNDI_HEAP;
4667c478bdstevel@tonic-gate	if ( undi.driver_code == NULL ) {
4677c478bdstevel@tonic-gate		printf ( "Could not allocate %d bytes for UNDI code segment\n",
4687c478bdstevel@tonic-gate			 undi.driver_code_size );
4697c478bdstevel@tonic-gate		return 0;
4707c478bdstevel@tonic-gate	}
4717c478bdstevel@tonic-gate	undi.pxs->loader.undi_cs = SEGMENT( undi.driver_code );
4727c478bdstevel@tonic-gate
4737c478bdstevel@tonic-gate	undi.driver_data_size = undi.undi_rom_id->data_size;
4747c478bdstevel@tonic-gate	undi.driver_data = (void *)((((unsigned long)UNDI_HEAP + undi.undi_rom_id->code_size) | (1024 -1)) + 1);
4757c478bdstevel@tonic-gate	if ( undi.driver_data == NULL ) {
4767c478bdstevel@tonic-gate		printf ( "Could not allocate %d bytes for UNDI code segment\n",
4777c478bdstevel@tonic-gate			 undi.driver_data_size );
4787c478bdstevel@tonic-gate		return 0;
4797c478bdstevel@tonic-gate	}
4807c478bdstevel@tonic-gate	undi.pxs->loader.undi_ds = SEGMENT( undi.driver_data );
4817c478bdstevel@tonic-gate
4827c478bdstevel@tonic-gate	DBG ( "Installing UNDI driver code to %hx:0000, data at %hx:0000\n",
4837c478bdstevel@tonic-gate		undi.pxs->loader.undi_cs, undi.pxs->loader.undi_ds );
4847c478bdstevel@tonic-gate
4857c478bdstevel@tonic-gate	/* Do the API call to install the loader */
4867c478bdstevel@tonic-gate	if ( ! undi_call_loader () ) return 0;
4877c478bdstevel@tonic-gate
4887c478bdstevel@tonic-gate	pxe = VIRTUAL( undi.pxs->loader.undi_cs, undi.pxs->loader.pxe_off );
4897c478bdstevel@tonic-gate	DBG ( "UNDI driver created a pixie at %hx:%hx...",
4907c478bdstevel@tonic-gate		 undi.pxs->loader.undi_cs, undi.pxs->loader.pxe_off );
4917c478bdstevel@tonic-gate	if ( memcmp ( pxe->Signature, "!PXE", 4 ) != 0 ) {
4927c478bdstevel@tonic-gate		DBG ( "invalid signature\n" );
4937c478bdstevel@tonic-gate		return 0;
4947c478bdstevel@tonic-gate	}
4957c478bdstevel@tonic-gate	if ( checksum ( pxe, sizeof(pxe_t) ) != 0 ) {
4967c478bdstevel@tonic-gate		DBG ( "invalid checksum\n" );
4977c478bdstevel@tonic-gate		return 0;
4987c478bdstevel@tonic-gate	}
4997c478bdstevel@tonic-gate	DBG ( "ok\n" );
5007c478bdstevel@tonic-gate	undi.pxe = pxe;
5017c478bdstevel@tonic-gate	pxe_dump();
5027c478bdstevel@tonic-gate	return 1;
5037c478bdstevel@tonic-gate}
5047c478bdstevel@tonic-gate
5057c478bdstevel@tonic-gate/* Start the UNDI driver.
5067c478bdstevel@tonic-gate */
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gateint eb_pxenv_start_undi ( void ) {
5097c478bdstevel@tonic-gate	int success = 0;
5107c478bdstevel@tonic-gate
5117c478bdstevel@tonic-gate	/* AX contains PCI bus:devfn (PCI specification) */
5127c478bdstevel@tonic-gate	undi.pxs->start_undi.ax = ( undi.pci.bus << 8 ) | undi.pci.devfn;
5137c478bdstevel@tonic-gate	/* BX and DX set to 0xffff for non-ISAPnP devices
5147c478bdstevel@tonic-gate	 * (BIOS boot specification)
5157c478bdstevel@tonic-gate	 */
5167c478bdstevel@tonic-gate	undi.pxs->start_undi.bx = 0xffff;
5177c478bdstevel@tonic-gate	undi.pxs->start_undi.dx = 0xffff;
5187c478bdstevel@tonic-gate	/* ES:DI points to PnP BIOS' $PnP structure
5197c478bdstevel@tonic-gate	 * (BIOS boot specification)
5207c478bdstevel@tonic-gate	 */
5217c478bdstevel@tonic-gate	undi.pxs->start_undi.es = 0xf000;
5227c478bdstevel@tonic-gate	undi.pxs->start_undi.di = virt_to_phys ( undi.pnp_bios ) - 0xf0000;
5237c478bdstevel@tonic-gate
5247c478bdstevel@tonic-gate	DBG ( "PXENV_START_UNDI => AX=%hx BX=%hx DX=%hx ES:DI=%hx:%hx\n",
5257c478bdstevel@tonic-gate	      undi.pxs->start_undi.ax,
5267c478bdstevel@tonic-gate	      undi.pxs->start_undi.bx, undi.pxs->start_undi.dx,
5277c478bdstevel@tonic-gate	      undi.pxs->start_undi.es, undi.pxs->start_undi.di );
5287c478bdstevel@tonic-gate	success = undi_call ( PXENV_START_UNDI );
5297c478bdstevel@tonic-gate	DBG ( "PXENV_START_UNDI <= Status=%s\n", UNDI_STATUS(undi.pxs) );
5307c478bdstevel@tonic-gate	if ( success ) undi.prestarted = 1;
5317c478bdstevel@tonic-gate	return success;
5327c478bdstevel@tonic-gate}
5337c478bdstevel@tonic-gate
5347c478bdstevel@tonic-gateint eb_pxenv_undi_startup ( void )	{
5357c478bdstevel@tonic-gate	int success = 0;
5367c478bdstevel@tonic-gate
5377c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_STARTUP => (void)\n" );
5387c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_STARTUP );
5397c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_STARTUP <= Status=%s\n", UNDI_STATUS(undi.pxs) );
5407c478bdstevel@tonic-gate	if ( success ) undi.started = 1;
5417c478bdstevel@tonic-gate	return success;
5427c478bdstevel@tonic-gate}
5437c478bdstevel@tonic-gate
5447c478bdstevel@tonic-gateint eb_pxenv_undi_cleanup ( void ) {
5457c478bdstevel@tonic-gate	int success = 0;
5467c478bdstevel@tonic-gate
5477c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_CLEANUP => (void)\n" );
5487c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_CLEANUP );
5497c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_CLEANUP <= Status=%s\n", UNDI_STATUS(undi.pxs) );
5507c478bdstevel@tonic-gate	return success;
5517c478bdstevel@tonic-gate}
5527c478bdstevel@tonic-gate
5537c478bdstevel@tonic-gateint eb_pxenv_undi_initialize ( void ) {
5547c478bdstevel@tonic-gate	int success = 0;
5557c478bdstevel@tonic-gate
5567c478bdstevel@tonic-gate	undi.pxs->undi_initialize.ProtocolIni = 0;
5577c478bdstevel@tonic-gate	memset ( &undi.pxs->undi_initialize.reserved, 0,
5587c478bdstevel@tonic-gate		 sizeof ( undi.pxs->undi_initialize.reserved ) );
5597c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_INITIALIZE => ProtocolIni=%x\n" );
5607c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_INITIALIZE );
5617c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_INITIALIZE <= Status=%s\n", UNDI_STATUS(undi.pxs) );
5627c478bdstevel@tonic-gate	if ( success ) undi.initialized = 1;
5637c478bdstevel@tonic-gate	return success;
5647c478bdstevel@tonic-gate}
5657c478bdstevel@tonic-gate
5667c478bdstevel@tonic-gateint eb_pxenv_undi_shutdown ( void ) {
5677c478bdstevel@tonic-gate	int success = 0;
5687c478bdstevel@tonic-gate
5697c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_SHUTDOWN => (void)\n" );
5707c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_SHUTDOWN );
5717c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_SHUTDOWN <= Status=%s\n", UNDI_STATUS(undi.pxs) );
5727c478bdstevel@tonic-gate	if ( success ) {
5737c478bdstevel@tonic-gate		undi.initialized = 0;
5747c478bdstevel@tonic-gate		undi.started = 0;
5757c478bdstevel@tonic-gate	}
5767c478bdstevel@tonic-gate	return success;
5777c478bdstevel@tonic-gate}
5787c478bdstevel@tonic-gate
5797c478bdstevel@tonic-gateint eb_pxenv_undi_open ( void ) {
5807c478bdstevel@tonic-gate	int success = 0;
5817c478bdstevel@tonic-gate
5827c478bdstevel@tonic-gate	undi.pxs->undi_open.OpenFlag = 0;
5837c478bdstevel@tonic-gate	undi.pxs->undi_open.PktFilter = FLTR_DIRECTED | FLTR_BRDCST;
5847c478bdstevel@tonic-gate
5857c478bdstevel@tonic-gate	/* Multicast support not yet implemented */
5867c478bdstevel@tonic-gate	undi.pxs->undi_open.R_Mcast_Buf.MCastAddrCount = 0;
5877c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_OPEN => OpenFlag=%hx PktFilter=%hx "
5887c478bdstevel@tonic-gate	      "MCastAddrCount=%hx\n",
5897c478bdstevel@tonic-gate	      undi.pxs->undi_open.OpenFlag, undi.pxs->undi_open.PktFilter,
5907c478bdstevel@tonic-gate	      undi.pxs->undi_open.R_Mcast_Buf.MCastAddrCount );
5917c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_OPEN );
5927c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_OPEN <= Status=%s\n", UNDI_STATUS(undi.pxs) );
5937c478bdstevel@tonic-gate	if ( success ) undi.opened = 1;
5947c478bdstevel@tonic-gate	return success;
5957c478bdstevel@tonic-gate}
5967c478bdstevel@tonic-gate
5977c478bdstevel@tonic-gateint eb_pxenv_undi_close ( void ) {
5987c478bdstevel@tonic-gate	int success = 0;
5997c478bdstevel@tonic-gate
6007c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_CLOSE => (void)\n" );
6017c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_CLOSE );
6027c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_CLOSE <= Status=%s\n", UNDI_STATUS(undi.pxs) );
6037c478bdstevel@tonic-gate	if ( success ) undi.opened = 0;
6047c478bdstevel@tonic-gate	return success;
6057c478bdstevel@tonic-gate}
6067c478bdstevel@tonic-gate
6077c478bdstevel@tonic-gateint eb_pxenv_undi_transmit_packet ( void ) {
6087c478bdstevel@tonic-gate	int success = 0;
6097c478bdstevel@tonic-gate	static const uint8_t broadcast[] = { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };
6107c478bdstevel@tonic-gate
6117c478bdstevel@tonic-gate	/* XMitFlag selects unicast / broadcast */
6127c478bdstevel@tonic-gate	if ( memcmp ( undi.xmit_data->destaddr, broadcast,
6137c478bdstevel@tonic-gate		      sizeof(broadcast) ) == 0 ) {
6147c478bdstevel@tonic-gate		undi.pxs->undi_transmit.XmitFlag = XMT_BROADCAST;
6157c478bdstevel@tonic-gate	} else {
6167c478bdstevel@tonic-gate		undi.pxs->undi_transmit.XmitFlag = XMT_DESTADDR;
6177c478bdstevel@tonic-gate	}
6187c478bdstevel@tonic-gate
6197c478bdstevel@tonic-gate	/* Zero reserved dwords */
6207c478bdstevel@tonic-gate	undi.pxs->undi_transmit.Reserved[0] = 0;
6217c478bdstevel@tonic-gate	undi.pxs->undi_transmit.Reserved[1] = 0;
6227c478bdstevel@tonic-gate
6237c478bdstevel@tonic-gate	/* Segment:offset pointer to DestAddr in base memory */
6247c478bdstevel@tonic-gate	undi.pxs->undi_transmit.DestAddr.segment =
6257c478bdstevel@tonic-gate		SEGMENT( undi.xmit_data->destaddr );
6267c478bdstevel@tonic-gate	undi.pxs->undi_transmit.DestAddr.offset =
6277c478bdstevel@tonic-gate		OFFSET( undi.xmit_data->destaddr );
6287c478bdstevel@tonic-gate
6297c478bdstevel@tonic-gate	/* Segment:offset pointer to TBD in base memory */
6307c478bdstevel@tonic-gate	undi.pxs->undi_transmit.TBD.segment = SEGMENT( &undi.xmit_data->tbd );
6317c478bdstevel@tonic-gate	undi.pxs->undi_transmit.TBD.offset = OFFSET( &undi.xmit_data->tbd );
6327c478bdstevel@tonic-gate
6337c478bdstevel@tonic-gate	/* Use only the "immediate" part of the TBD */
6347c478bdstevel@tonic-gate	undi.xmit_data->tbd.DataBlkCount = 0;
6357c478bdstevel@tonic-gate
6367c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_TRANSMIT_PACKET => Protocol=%hx XmitFlag=%hx ...\n"
6377c478bdstevel@tonic-gate	      "... DestAddr=%hx:%hx TBD=%hx:%hx ...\n",
6387c478bdstevel@tonic-gate	      undi.pxs->undi_transmit.Protocol,
6397c478bdstevel@tonic-gate	      undi.pxs->undi_transmit.XmitFlag,
6407c478bdstevel@tonic-gate	      undi.pxs->undi_transmit.DestAddr.segment,
6417c478bdstevel@tonic-gate	      undi.pxs->undi_transmit.DestAddr.offset,
6427c478bdstevel@tonic-gate	      undi.pxs->undi_transmit.TBD.segment,
6437c478bdstevel@tonic-gate	      undi.pxs->undi_transmit.TBD.offset );
6447c478bdstevel@tonic-gate	DBG ( "... TBD { ImmedLength=%hx Xmit=%hx:%hx DataBlkCount=%hx }\n",
6457c478bdstevel@tonic-gate	      undi.xmit_data->tbd.ImmedLength,
6467c478bdstevel@tonic-gate	      undi.xmit_data->tbd.Xmit.segment,
6477c478bdstevel@tonic-gate	      undi.xmit_data->tbd.Xmit.offset,
6487c478bdstevel@tonic-gate	      undi.xmit_data->tbd.DataBlkCount );
6497c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_TRANSMIT );
6507c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_TRANSMIT_PACKET <= Status=%s\n",
6517c478bdstevel@tonic-gate	      UNDI_STATUS(undi.pxs) );
6527c478bdstevel@tonic-gate	return success;
6537c478bdstevel@tonic-gate}
6547c478bdstevel@tonic-gate
6557c478bdstevel@tonic-gateint eb_pxenv_undi_set_station_address ( void ) {
6567c478bdstevel@tonic-gate	/* This will spuriously fail on some cards.  Ignore failures.
6577c478bdstevel@tonic-gate	 * We only ever use it to set the MAC address to the card's
6587c478bdstevel@tonic-gate	 * permanent value anyway, so it's a useless call (although we
6597c478bdstevel@tonic-gate	 * make it because PXE spec says we should).
6607c478bdstevel@tonic-gate	 */
6617c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_SET_STATION_ADDRESS => "
6627c478bdstevel@tonic-gate	      "StationAddress=%!\n",
6637c478bdstevel@tonic-gate	      undi.pxs->undi_set_station_address.StationAddress );
6647c478bdstevel@tonic-gate	undi_call_silent ( PXENV_UNDI_SET_STATION_ADDRESS );
6657c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_SET_STATION_ADDRESS <= Status=%s\n",
6667c478bdstevel@tonic-gate	      UNDI_STATUS(undi.pxs) );
6677c478bdstevel@tonic-gate	return 1;
6687c478bdstevel@tonic-gate}
6697c478bdstevel@tonic-gate
6707c478bdstevel@tonic-gateint eb_pxenv_undi_get_information ( void ) {
6717c478bdstevel@tonic-gate	int success = 0;
6727c478bdstevel@tonic-gate	memset ( undi.pxs, 0, sizeof ( undi.pxs ) );
6737c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_GET_INFORMATION => (void)\n" );
6747c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_GET_INFORMATION );
6757c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_GET_INFORMATION <= Status=%s "
6767c478bdstevel@tonic-gate	      "BaseIO=%hx IntNumber=%hx ...\n"
6777c478bdstevel@tonic-gate	      "... MaxTranUnit=%hx HwType=%hx HwAddrlen=%hx ...\n"
6787c478bdstevel@tonic-gate	      "... CurrentNodeAddress=%! PermNodeAddress=%! ...\n"
6797c478bdstevel@tonic-gate	      "... ROMAddress=%hx RxBufCt=%hx TxBufCt=%hx\n",
6807c478bdstevel@tonic-gate	      UNDI_STATUS(undi.pxs),
6817c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.BaseIo,
6827c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.IntNumber,
6837c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.MaxTranUnit,
6847c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.HwType,
6857c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.HwAddrLen,
6867c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.CurrentNodeAddress,
6877c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.PermNodeAddress,
6887c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.ROMAddress,
6897c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.RxBufCt,
6907c478bdstevel@tonic-gate	      undi.pxs->undi_get_information.TxBufCt );
6917c478bdstevel@tonic-gate	return success;
6927c478bdstevel@tonic-gate}
6937c478bdstevel@tonic-gate
6947c478bdstevel@tonic-gateint eb_pxenv_undi_get_iface_info ( void ) {
6957c478bdstevel@tonic-gate	int success = 0;
6967c478bdstevel@tonic-gate
6977c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_GET_IFACE_INFO => (void)\n" );
6987c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_GET_IFACE_INFO );
6997c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_GET_IFACE_INFO <= Status=%s IfaceType=%s ...\n"
7007c478bdstevel@tonic-gate	      "... LinkSpeed=%x ServiceFlags=%x\n",
7017c478bdstevel@tonic-gate	      UNDI_STATUS(undi.pxs),
7027c478bdstevel@tonic-gate	      undi.pxs->undi_get_iface_info.IfaceType,
7037c478bdstevel@tonic-gate	      undi.pxs->undi_get_iface_info.LinkSpeed,
7047c478bdstevel@tonic-gate	      undi.pxs->undi_get_iface_info.ServiceFlags );
7057c478bdstevel@tonic-gate	return success;
7067c478bdstevel@tonic-gate}
7077c478bdstevel@tonic-gate
7087c478bdstevel@tonic-gateint eb_pxenv_undi_isr ( void ) {
7097c478bdstevel@tonic-gate	int success = 0;
7107c478bdstevel@tonic-gate
7117c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_ISR => FuncFlag=%hx\n",
7127c478bdstevel@tonic-gate	      undi.pxs->undi_isr.FuncFlag );
7137c478bdstevel@tonic-gate	success = undi_call ( PXENV_UNDI_ISR );
7147c478bdstevel@tonic-gate	DBG ( "PXENV_UNDI_ISR <= Status=%s FuncFlag=%hx BufferLength=%hx ...\n"
7157c478bdstevel@tonic-gate	      "... FrameLength=%hx FrameHeaderLength=%hx Frame=%hx:%hx "
7167c478bdstevel@tonic-gate	      "ProtType=%hhx ...\n... PktType=%hhx\n",
7177c478bdstevel@tonic-gate	      UNDI_STATUS(undi.pxs), undi.pxs->undi_isr.FuncFlag,
7187c478bdstevel@tonic-gate	      undi.pxs->undi_isr.BufferLength,
7197c478bdstevel@tonic-gate	      undi.pxs->undi_isr.FrameLength,
7207c478bdstevel@tonic-gate	      undi.pxs->undi_isr.FrameHeaderLength,
7217c478bdstevel@tonic-gate	      undi.pxs->undi_isr.Frame.segment,
7227c478bdstevel@tonic-gate	      undi.pxs->undi_isr.Frame.offset,
7237c478bdstevel@tonic-gate	      undi.pxs->undi_isr.ProtType,
7247c478bdstevel@tonic-gate	      undi.pxs->undi_isr.PktType );
7257c478bdstevel@tonic-gate	return success;
7267c478bdstevel@tonic-gate}
7277c478bdstevel@tonic-gate
7287c478bdstevel@tonic-gateint eb_pxenv_stop_undi ( void ) {
7297c478bdstevel@tonic-gate	int success = 0;
7307c478bdstevel@tonic-gate
7317c478bdstevel@tonic-gate	DBG ( "PXENV_STOP_UNDI => (void)\n" );
7327c478bdstevel@tonic-gate	success = undi_call ( PXENV_STOP_UNDI );
7337c478bdstevel@tonic-gate	DBG ( "PXENV_STOP_UNDI <= Status=%s\n", UNDI_STATUS(undi.pxs) );
7347c478bdstevel@tonic-gate	if ( success ) undi.prestarted = 0;
7357c478bdstevel@tonic-gate	return success;
7367c478bdstevel@tonic-gate}
7377c478bdstevel@tonic-gate
7387c478bdstevel@tonic-gateint eb_pxenv_unload_stack ( void ) {
7397c478bdstevel@tonic-gate	int success = 0;
7407c478bdstevel@tonic-gate
7417c478bdstevel@tonic-gate	memset ( undi.pxs, 0, sizeof ( undi.pxs ) );
7427c478bdstevel@tonic-gate	DBG ( "PXENV_UNLOAD_STACK => (void)\n" );
7437c478bdstevel@tonic-gate	success = undi_call_silent ( PXENV_UNLOAD_STACK );
7447c478bdstevel@tonic-gate	DBG ( "PXENV_UNLOAD_STACK <= Status=%s ...\n... (%s)\n",
7457c478bdstevel@tonic-gate	      UNDI_STATUS(undi.pxs),
7467c478bdstevel@tonic-gate	      ( undi.pxs->Status == PXENV_STATUS_SUCCESS ?
7477c478bdstevel@tonic-gate		"base-code is ready to be removed" :
7487c478bdstevel@tonic-gate		( undi.pxs->Status == PXENV_STATUS_FAILURE ?
7497c478bdstevel@tonic-gate		  "the size of free base memory has been changed" :
7507c478bdstevel@tonic-gate		  ( undi.pxs->Status == PXENV_STATUS_KEEP_ALL ?
7517c478bdstevel@tonic-gate		    "the NIC interrupt vector has been changed" :
7527c478bdstevel@tonic-gate		    "UNEXPECTED STATUS CODE" ) ) ) );
7537c478bdstevel@tonic-gate	return success;
7547c478bdstevel@tonic-gate}
7557c478bdstevel@tonic-gate
7567c478bdstevel@tonic-gateint eb_pxenv_stop_base ( void ) {
7577c478bdstevel@tonic-gate	int success = 0;
7587c478bdstevel@tonic-gate
7597c478bdstevel@tonic-gate	DBG ( "PXENV_STOP_BASE => (void)\n" );
7607c478bdstevel@tonic-gate	success = undi_call ( PXENV_STOP_BASE );
7617c478bdstevel@tonic-gate	DBG ( "PXENV_STOP_BASE <= Status=%s\n", UNDI_STATUS(undi.pxs) );
7627c478bdstevel@tonic-gate	return success;
7637c478bdstevel@tonic-gate}
7647c478bdstevel@tonic-gate
7657c478bdstevel@tonic-gate/* Unload UNDI base code (if any present) and free memory.
7667c478bdstevel@tonic-gate */
7677c478bdstevel@tonic-gateint undi_unload_base_code ( void ) {
7687c478bdstevel@tonic-gate	/* In GRUB, we do not allocate anything, but we still can call
7697c478bdstevel@tonic-gate	 * to free the base space */
7707c478bdstevel@tonic-gate	void *bc_code = VIRTUAL( undi.pxe->BC_Code.Seg_Addr, 0 );
7717c478bdstevel@tonic-gate	size_t bc_code_size = undi.pxe->BC_Code.Seg_Size;
7727c478bdstevel@tonic-gate	void *bc_data = VIRTUAL( undi.pxe->BC_Data.Seg_Addr, 0 );
7737c478bdstevel@tonic-gate	size_t bc_data_size = undi.pxe->BC_Data.Seg_Size;
7747c478bdstevel@tonic-gate	void *bc_stck = VIRTUAL( undi.pxe->Stack.Seg_Addr, 0 );
7757c478bdstevel@tonic-gate	size_t bc_stck_size = undi.pxe->Stack.Seg_Size;
7767c478bdstevel@tonic-gate	firing_squad_lineup_t lineup;
7777c478bdstevel@tonic-gate
7787c478bdstevel@tonic-gate	/* Don't unload if there is no base code present */
7797c478bdstevel@tonic-gate	if ( undi.pxe->BC_Code.Seg_Addr == 0 ) return 1;
7807c478bdstevel@tonic-gate
7817c478bdstevel@tonic-gate	/* Since we never start the base code, the only time we should
7827c478bdstevel@tonic-gate	 * reach this is if we were loaded via PXE.  There are many
7837c478bdstevel@tonic-gate	 * different and conflicting versions of the "correct" way to
7847c478bdstevel@tonic-gate	 * unload the PXE base code, several of which appear within
7857c478bdstevel@tonic-gate	 * the PXE specification itself.  This one seems to work for
7867c478bdstevel@tonic-gate	 * our purposes.
7877c478bdstevel@tonic-gate	 */
7887c478bdstevel@tonic-gate	eb_pxenv_stop_base();
7897c478bdstevel@tonic-gate	//eb_pxenv_unload_stack();
7907c478bdstevel@tonic-gate/*	if ( ( undi.pxs->unload_stack.Status != PXENV_STATUS_SUCCESS ) &&
7917c478bdstevel@tonic-gate	     ( undi.pxs->unload_stack.Status != PXENV_STATUS_FAILURE ) ) {
7927c478bdstevel@tonic-gate		printf ( "Could not free memory allocated to PXE base code: "
7937c478bdstevel@tonic-gate			 "possible memory leak\n" );
7947c478bdstevel@tonic-gate		return 0;
7957c478bdstevel@tonic-gate		}*/
7967c478bdstevel@tonic-gate	/* Free data structures.  Forget what the PXE specification
7977c478bdstevel@tonic-gate	 * says about how to calculate the new size of base memory;
7987c478bdstevel@tonic-gate	 * basemem.c takes care of all that for us.  Note that we also
7997c478bdstevel@tonic-gate	 * have to free the stack (even though PXE spec doesn't say
8007c478bdstevel@tonic-gate	 * anything about it) because nothing else is going to do so.
8017c478bdstevel@tonic-gate	 *
8027c478bdstevel@tonic-gate	 * Structures will almost certainly not be kB-aligned and
8037c478bdstevel@tonic-gate	 * there's a reasonable chance that the UNDI code or data
8047c478bdstevel@tonic-gate	 * portions will lie in the same kB as the base code.  Since
8057c478bdstevel@tonic-gate	 * forget_base_memory works only in 1kB increments, this means
8067c478bdstevel@tonic-gate	 * we have to do some arcane trickery.
8077c478bdstevel@tonic-gate	 */
8087c478bdstevel@tonic-gate	memset ( &lineup, 0, sizeof(lineup) );
8097c478bdstevel@tonic-gate	if ( SEGMENT(bc_code) != 0 )
8107c478bdstevel@tonic-gate		assemble_firing_squad( &lineup, bc_code, bc_code_size, SHOOT );
8117c478bdstevel@tonic-gate	if ( SEGMENT(bc_data) != 0 )
8127c478bdstevel@tonic-gate		assemble_firing_squad( &lineup, bc_data, bc_data_size, SHOOT );
8137c478bdstevel@tonic-gate	if ( SEGMENT(bc_stck) != 0 )
8147c478bdstevel@tonic-gate		assemble_firing_squad( &lineup, bc_stck, bc_stck_size, SHOOT );
8157c478bdstevel@tonic-gate	/* Don't shoot any bits of the UNDI driver code or data */
8167c478bdstevel@tonic-gate	assemble_firing_squad ( &lineup,
8177c478bdstevel@tonic-gate				VIRTUAL(undi.pxe->UNDICode.Seg_Addr, 0),
8187c478bdstevel@tonic-gate				undi.pxe->UNDICode.Seg_Size, DONTSHOOT );
8197c478bdstevel@tonic-gate	assemble_firing_squad ( &lineup,
8207c478bdstevel@tonic-gate				VIRTUAL(undi.pxe->UNDIData.Seg_Addr, 0),
8217c478bdstevel@tonic-gate				undi.pxe->UNDIData.Seg_Size, DONTSHOOT );
8227c478bdstevel@tonic-gate	//shoot_targets ( &lineup );
8237c478bdstevel@tonic-gate	//undi.pxe->BC_Code.Seg_Addr = 0;
8247c478bdstevel@tonic-gate	//undi.pxe->BC_Data.Seg_Addr = 0;
8257c478bdstevel@tonic-gate	//undi.pxe->Stack.Seg_Addr = 0;
8267c478bdstevel@tonic-gate
8277c478bdstevel@tonic-gate	/* Free and reallocate our own base memory data structures, to
8287c478bdstevel@tonic-gate	 * allow the freed base-code blocks to be fully released.
8297c478bdstevel@tonic-gate	 */
8307c478bdstevel@tonic-gate	free_base_mem_data();
8317c478bdstevel@tonic-gate	if ( ! allocate_base_mem_data() ) {
8327c478bdstevel@tonic-gate		printf ( "FATAL: memory unaccountably lost\n" );
8337c478bdstevel@tonic-gate		while ( 1 ) {};
8347c478bdstevel@tonic-gate	}
8357c478bdstevel@tonic-gate
8367c478bdstevel@tonic-gate	return 1;
8377c478bdstevel@tonic-gate}
8387c478bdstevel@tonic-gate
8397c478bdstevel@tonic-gate/* UNDI full initialization
8407c478bdstevel@tonic-gate *
8417c478bdstevel@tonic-gate * This calls all the various UNDI initialization routines in sequence.
8427c478bdstevel@tonic-gate */
8437c478bdstevel@tonic-gate
8447c478bdstevel@tonic-gateint undi_full_startup ( void ) {
8457c478bdstevel@tonic-gate	if ( ! eb_pxenv_start_undi() ) return 0;
8467c478bdstevel@tonic-gate	if ( ! eb_pxenv_undi_startup() ) return 0;
8477c478bdstevel@tonic-gate	if ( ! eb_pxenv_undi_initialize() ) return 0;
8487c478bdstevel@tonic-gate	if ( ! eb_pxenv_undi_get_information() ) return 0;
8497c478bdstevel@tonic-gate	undi.irq = undi.pxs->undi_get_information.IntNumber;
8507c478bdstevel@tonic-gate	if ( ! install_undi_irq_handler ( undi.irq, undi.pxe->EntryPointSP ) ) {
8517c478bdstevel@tonic-gate		undi.irq = IRQ_NONE;
8527c478bdstevel@tonic-gate		return 0;
8537c478bdstevel@tonic-gate	}
8547c478bdstevel@tonic-gate	memmove ( &undi.pxs->undi_set_station_address.StationAddress,
8557c478bdstevel@tonic-gate		  &undi.pxs->undi_get_information.PermNodeAddress,
8567c478bdstevel@tonic-gate		  sizeof (undi.pxs->undi_set_station_address.StationAddress) );
8577c478bdstevel@tonic-gate	if ( ! eb_pxenv_undi_set_station_address() ) return 0;
8587c478bdstevel@tonic-gate	if ( ! eb_pxenv_undi_open() ) return 0;
8597c478bdstevel@tonic-gate	/* install_undi_irq_handler leaves irq disabled */
8607c478bdstevel@tonic-gate	enable_irq ( undi.irq );
8617c478bdstevel@tonic-gate	return 1;
8627c478bdstevel@tonic-gate}
8637c478bdstevel@tonic-gate
8647c478bdstevel@tonic-gate/* UNDI full shutdown
8657c478bdstevel@tonic-gate *
8667c478bdstevel@tonic-gate * This calls all the various UNDI shutdown routines in sequence and
8677c478bdstevel@tonic-gate * also frees any memory that it can.
8687c478bdstevel@tonic-gate */
8697c478bdstevel@tonic-gate
8707c478bdstevel@tonic-gateint undi_full_shutdown ( void ) {
8717c478bdstevel@tonic-gate	if ( undi.pxe != NULL ) {
8727c478bdstevel@tonic-gate		/* In case we didn't allocate the driver's memory in the first
8737c478bdstevel@tonic-gate		 * place, try to grab the code and data segments and sizes
8747c478bdstevel@tonic-gate		 * from the !PXE structure.
8757c478bdstevel@tonic-gate		 */
8767c478bdstevel@tonic-gate		if ( undi.driver_code == NULL ) {
8777c478bdstevel@tonic-gate			undi.driver_code = VIRTUAL(undi.pxe->UNDICode.Seg_Addr,
8787c478bdstevel@tonic-gate						   0 );
8797c478bdstevel@tonic-gate			undi.driver_code_size = undi.pxe->UNDICode.Seg_Size;
8807c478bdstevel@tonic-gate		}
8817c478bdstevel@tonic-gate		if ( undi.driver_data == NULL ) {
8827c478bdstevel@tonic-gate			undi.driver_data = VIRTUAL(undi.pxe->UNDIData.Seg_Addr,
8837c478bdstevel@tonic-gate						   0 );
8847c478bdstevel@tonic-gate			undi.driver_data_size = undi.pxe->UNDIData.Seg_Size;
8857c478bdstevel@tonic-gate		}
8867c478bdstevel@tonic-gate
8877c478bdstevel@tonic-gate		/* Ignore errors and continue in the hope of shutting
8887c478bdstevel@tonic-gate		 * down anyway
8897c478bdstevel@tonic-gate		 */
8907c478bdstevel@tonic-gate		if ( undi.opened ) eb_pxenv_undi_close();
8917c478bdstevel@tonic-gate		if ( undi.started ) {
8927c478bdstevel@tonic-gate			eb_pxenv_undi_cleanup();
8937c478bdstevel@tonic-gate			/* We may get spurious UNDI API errors at this
8947c478bdstevel@tonic-gate			 * point.  If startup() succeeded but
8957c478bdstevel@tonic-gate			 * initialize() failed then according to the
8967c478bdstevel@tonic-gate			 * spec, we should call shutdown().  However,
8977c478bdstevel@tonic-gate			 * some NICS will fail with a status code
8987c478bdstevel@tonic-gate			 * 0x006a (INVALID_STATE).
8997c478bdstevel@tonic-gate			 */
9007c478bdstevel@tonic-gate			eb_pxenv_undi_shutdown();
9017c478bdstevel@tonic-gate		}
9027c478bdstevel@tonic-gate		if ( undi.irq != IRQ_NONE ) {
9037c478bdstevel@tonic-gate			remove_undi_irq_handler ( undi.irq );
9047c478bdstevel@tonic-gate			undi.irq = IRQ_NONE;
9057c478bdstevel@tonic-gate		}
9067c478bdstevel@tonic-gate		undi_unload_base_code();
9077c478bdstevel@tonic-gate		if ( undi.prestarted ) {
9087c478bdstevel@tonic-gate			eb_pxenv_stop_undi();
9097c478bdstevel@tonic-gate			/* Success OR Failure indicates that memory
9107c478bdstevel@tonic-gate			 * can be freed.  Any other status code means
9117c478bdstevel@tonic-gate			 * that it can't.
9127c478bdstevel@tonic-gate			 */
9137c478bdstevel@tonic-gate			if (( undi.pxs->Status == PXENV_STATUS_KEEP_UNDI ) ||
9147c478bdstevel@tonic-gate			    ( undi.pxs->Status == PXENV_STATUS_KEEP_ALL ) ) {
9157c478bdstevel@tonic-gate				printf ("Could not free memory allocated to "
9167c478bdstevel@tonic-gate					"UNDI driver: possible memory leak\n");
9177c478bdstevel@tonic-gate				return 0;
9187c478bdstevel@tonic-gate			}
9197c478bdstevel@tonic-gate		}
9207c478bdstevel@tonic-gate	}
9217c478bdstevel@tonic-gate	/* Free memory allocated to UNDI driver */
9227c478bdstevel@tonic-gate	if ( undi.driver_code != NULL ) {
9237c478bdstevel@tonic-gate		/* Clear contents in order to eliminate !PXE and PXENV
9247c478bdstevel@tonic-gate		 * signatures to prevent spurious detection via base
9257c478bdstevel@tonic-gate		 * memory scan.
9267c478bdstevel@tonic-gate		 */
9277c478bdstevel@tonic-gate		memset ( undi.driver_code, 0, undi.driver_code_size );
9287c478bdstevel@tonic-gate		/* forget_base_memory ( undi.driver_code, undi.driver_code_size ); */
9297c478bdstevel@tonic-gate		undi.driver_code = NULL;
9307c478bdstevel@tonic-gate		undi.driver_code_size = 0;
9317c478bdstevel@tonic-gate	}
9327c478bdstevel@tonic-gate	if ( undi.driver_data != NULL ) {
9337c478bdstevel@tonic-gate		/* forget_base_memory ( undi.driver_data, undi.driver_data_size ); */
9347c478bdstevel@tonic-gate		undi.driver_data = NULL;
9357c478bdstevel@tonic-gate		undi.driver_data_size = 0;
9367c478bdstevel@tonic-gate	}
9377c478bdstevel@tonic-gate	/* !PXE structure now gone; memory freed */
9387c478bdstevel@tonic-gate	undi.pxe = NULL;
9397c478bdstevel@tonic-gate	return 1;
9407c478bdstevel@tonic-gate}
9417c478bdstevel@tonic-gate
9427c478bdstevel@tonic-gate/**************************************************************************
9437c478bdstevel@tonic-gatePOLL - Wait for a frame
9447c478bdstevel@tonic-gate***************************************************************************/
9457c478bdstevel@tonic-gatestatic int undi_poll(struct nic *nic, int retrieve)
9467c478bdstevel@tonic-gate{
9477c478bdstevel@tonic-gate	/* Fun, fun, fun.  UNDI drivers don't use polling; they use
9487c478bdstevel@tonic-gate	 * interrupts.  We therefore cheat and pretend that an
9497c478bdstevel@tonic-gate	 * interrupt has occurred every time undi_poll() is called.
9507c478bdstevel@tonic-gate	 * This isn't too much of a hack; PCI devices share IRQs and
9517c478bdstevel@tonic-gate	 * so the first thing that a proper ISR should do is call
9527c478bdstevel@tonic-gate	 * PXENV_UNDI_ISR to determine whether or not the UNDI NIC
9537c478bdstevel@tonic-gate	 * generated the interrupt; there is no harm done by spurious
9547c478bdstevel@tonic-gate	 * calls to PXENV_UNDI_ISR.  Similarly, we wouldn't be
9557c478bdstevel@tonic-gate	 * handling them any more rapidly than the usual rate of
9567c478bdstevel@tonic-gate	 * undi_poll() being called even if we did implement a full
9577c478bdstevel@tonic-gate	 * ISR.  So it should work.  Ha!
9587c478bdstevel@tonic-gate	 *
9597c478bdstevel@tonic-gate	 * Addendum (21/10/03).  Some cards don't play nicely with
9607c478bdstevel@tonic-gate	 * this trick, so instead of doing it the easy way we have to
9617c478bdstevel@tonic-gate	 * go to all the hassle of installing a genuine interrupt
9627c478bdstevel@tonic-gate	 * service routine and dealing with the wonderful 8259
9637c478bdstevel@tonic-gate	 * Programmable Interrupt Controller.  Joy.
9647c478bdstevel@tonic-gate	 *
9657c478bdstevel@tonic-gate	 * (02/01/2005). A real UNDI ISR is now implemented in,
9667c478bdstevel@tonic-gate	 * following Figure 3-4 in PXE spec 2.0.  The interrupt
9677c478bdstevel@tonic-gate	 * handler, undi_irq_handler, issues PXENV_UNDI_ISR_IN_START.
9687c478bdstevel@tonic-gate	 * If the interrupt is ours, the handler sends EOI and bumps the
9697c478bdstevel@tonic-gate	 * undi_irq_trigger_count. This polled routine is equivalent
9707c478bdstevel@tonic-gate	 * to the "driver strategy routine".
9717c478bdstevel@tonic-gate	 *
9727c478bdstevel@tonic-gate	 * Another issue is that upper layer await_*() does not handle
9737c478bdstevel@tonic-gate	 * coalesced packets. The UNDI implementation on broadcom chips
9747c478bdstevel@tonic-gate	 * appear to combine interrupts. If we loop through GET_NEXT,
9757c478bdstevel@tonic-gate	 * we may hand up coalesced packets, resulting in drops, and
9767c478bdstevel@tonic-gate	 * severe time delay. As a temperary hack, we return as soon as
9777c478bdstevel@tonic-gate	 * we get something, remembering where we were (IN_PROCESS
9787c478bdstevel@tonic-gate	 * or GET_NEXT). This assume packets are never broken up.
9797c478bdstevel@tonic-gate	 * XXX Need to fix upper layer to handle coalesced data.
9807c478bdstevel@tonic-gate	 */
9817c478bdstevel@tonic-gate
9827c478bdstevel@tonic-gate	static int undi_opcode = PXENV_UNDI_ISR_IN_PROCESS;
9837c478bdstevel@tonic-gate
9847c478bdstevel@tonic-gate	/* See if a hardware interrupt has occurred since the last poll().
9857c478bdstevel@tonic-gate	 */
9867c478bdstevel@tonic-gate	switch ( undi_opcode ) {
9877c478bdstevel@tonic-gate	case PXENV_UNDI_ISR_IN_PROCESS:
9887c478bdstevel@tonic-gate		if ( ! undi_irq_triggered ( undi.irq ) )
9897c478bdstevel@tonic-gate			return 0;
9907c478bdstevel@tonic-gate	default:
9917c478bdstevel@tonic-gate		break;
9927c478bdstevel@tonic-gate	}
9937c478bdstevel@tonic-gate
9947c478bdstevel@tonic-gate	/* We have an interrupt or there is something left from
9957c478bdstevel@tonic-gate	 * last poll. Either way, we need to call UNDI ISR.
9967c478bdstevel@tonic-gate	 */
9977c478bdstevel@tonic-gate	nic->packetlen = 0;
9987c478bdstevel@tonic-gate	undi.pxs->undi_isr.FuncFlag = undi_opcode;
9997c478bdstevel@tonic-gate	/* there is no good way to handle this error */
10007c478bdstevel@tonic-gate	if ( ! eb_pxenv_undi_isr() ) {
10017c478bdstevel@tonic-gate		printf ("undi isr call failed: opcode = %d\n", undi_opcode);
10027c478bdstevel@tonic-gate		return 0;
10037c478bdstevel@tonic-gate	}
10047c478bdstevel@tonic-gate	switch ( undi.pxs->undi_isr.FuncFlag ) {
10057c478bdstevel@tonic-gate	case PXENV_UNDI_ISR_OUT_DONE:
10067c478bdstevel@tonic-gate		/* Set opcode back to IN_PROCESS and wait for next intr */
10077c478bdstevel@tonic-gate		undi_opcode = PXENV_UNDI_ISR_IN_PROCESS;
10087c478bdstevel@tonic-gate		return 0;
10097c478bdstevel@tonic-gate	case PXENV_UNDI_ISR_OUT_TRANSMIT:
10107c478bdstevel@tonic-gate		/* We really don't care about transmission complete
10117c478bdstevel@tonic-gate		 * interrupts. Move on to next frame.
10127c478bdstevel@tonic-gate		 */
10137c478bdstevel@tonic-gate		undi_opcode = PXENV_UNDI_ISR_IN_GET_NEXT;
10147c478bdstevel@tonic-gate		return 0;
10157c478bdstevel@tonic-gate	case PXENV_UNDI_ISR_OUT_BUSY:
10167c478bdstevel@tonic-gate		/* This should never happen.
10177c478bdstevel@tonic-gate		 */
10187c478bdstevel@tonic-gate		undi_opcode = PXENV_UNDI_ISR_IN_GET_NEXT;
10197c478bdstevel@tonic-gate		printf ( "UNDI ISR thinks it's being re-entered!\n"
10207c478bdstevel@tonic-gate			 "Aborting receive\n" );
10217c478bdstevel@tonic-gate		return 0;
10227c478bdstevel@tonic-gate	case PXENV_UNDI_ISR_OUT_RECEIVE:
10237c478bdstevel@tonic-gate		/* Copy data to receive buffer and move on to next frame */
10247c478bdstevel@tonic-gate		undi_opcode = PXENV_UNDI_ISR_IN_GET_NEXT;
10257c478bdstevel@tonic-gate		memcpy ( nic->packet + nic->packetlen,
10267c478bdstevel@tonic-gate			 VIRTUAL( undi.pxs->undi_isr.Frame.segment,
10277c478bdstevel@tonic-gate				  undi.pxs->undi_isr.Frame.offset ),
10287c478bdstevel@tonic-gate			 undi.pxs->undi_isr.BufferLength );
10297c478bdstevel@tonic-gate		nic->packetlen += undi.pxs->undi_isr.BufferLength;
10307c478bdstevel@tonic-gate		break;
10317c478bdstevel@tonic-gate	default:
10327c478bdstevel@tonic-gate		undi_opcode = PXENV_UNDI_ISR_IN_PROCESS;
10337c478bdstevel@tonic-gate		printf ( "UNDI ISR returned bizzare status code %d\n",
10347c478bdstevel@tonic-gate			 undi.pxs->undi_isr.FuncFlag );
10357c478bdstevel@tonic-gate	}
10367c478bdstevel@tonic-gate
10377c478bdstevel@tonic-gate	return nic->packetlen > 0 ? 1 : 0;
10387c478bdstevel@tonic-gate}
10397c478bdstevel@tonic-gate
10407c478bdstevel@tonic-gate/**************************************************************************
10417c478bdstevel@tonic-gateTRANSMIT - Transmit a frame
10427c478bdstevel@tonic-gate***************************************************************************/
10437c478bdstevel@tonic-gatestatic void undi_transmit(
10447c478bdstevel@tonic-gate	struct nic *nic,
10457c478bdstevel@tonic-gate	const char *d,			/* Destination */
10467c478bdstevel@tonic-gate	unsigned int t,			/* Type */
10477c478bdstevel@tonic-gate	unsigned int s,			/* size */
10487c478bdstevel@tonic-gate	const char *p)			/* Packet */
10497c478bdstevel@tonic-gate{
10507c478bdstevel@tonic-gate	/* Inhibit compiler warning about unused parameter nic */
10517c478bdstevel@tonic-gate	if ( nic == NULL ) {};
10527c478bdstevel@tonic-gate
10537c478bdstevel@tonic-gate	/* Copy destination to buffer in base memory */
10547c478bdstevel@tonic-gate	memcpy ( undi.xmit_data->destaddr, d, sizeof(MAC_ADDR) );
10557c478bdstevel@tonic-gate
10567c478bdstevel@tonic-gate	/* Translate packet type to UNDI packet type */
10577c478bdstevel@tonic-gate	switch ( t ) {
10587c478bdstevel@tonic-gate	case IP :  undi.pxs->undi_transmit.Protocol = P_IP;   break;
10597c478bdstevel@tonic-gate	case ARP:  undi.pxs->undi_transmit.Protocol = P_ARP;  break;
10607c478bdstevel@tonic-gate	case RARP: undi.pxs->undi_transmit.Protocol = P_RARP; break;
10617c478bdstevel@tonic-gate	default: undi.pxs->undi_transmit.Protocol = P_UNKNOWN; break;
10627c478bdstevel@tonic-gate	}
10637c478bdstevel@tonic-gate
10647c478bdstevel@tonic-gate	/* Store packet length in TBD */
10657c478bdstevel@tonic-gate	undi.xmit_data->tbd.ImmedLength = s;
10667c478bdstevel@tonic-gate
10677c478bdstevel@tonic-gate	/* Check to see if data to be transmitted is currently in base
10687c478bdstevel@tonic-gate	 * memory.  If not, allocate temporary storage in base memory
10697c478bdstevel@tonic-gate	 * and copy it there.
10707c478bdstevel@tonic-gate	 */
10717c478bdstevel@tonic-gate	if ( SEGMENT( p ) <= 0xffff ) {
10727c478bdstevel@tonic-gate		undi.xmit_data->tbd.Xmit.segment = SEGMENT( p );
10737c478bdstevel@tonic-gate		undi.xmit_data->tbd.Xmit.offset = OFFSET( p );
10747c478bdstevel@tonic-gate	} else {
10757c478bdstevel@tonic-gate		memcpy ( undi.xmit_buffer, p, s );
10767c478bdstevel@tonic-gate		undi.xmit_data->tbd.Xmit.segment = SEGMENT( undi.xmit_buffer );
10777c478bdstevel@tonic-gate		undi.xmit_data->tbd.Xmit.offset = OFFSET( undi.xmit_buffer );
10787c478bdstevel@tonic-gate	}
10797c478bdstevel@tonic-gate
10807c478bdstevel@tonic-gate	eb_pxenv_undi_transmit_packet();
10817c478bdstevel@tonic-gate}
10827c478bdstevel@tonic-gate
10837c478bdstevel@tonic-gate/**************************************************************************
10847c478bdstevel@tonic-gateDISABLE - Turn off ethernet interface
10857c478bdstevel@tonic-gate***************************************************************************/
10867c478bdstevel@tonic-gatestatic void undi_disable(struct dev *dev)
10877c478bdstevel@tonic-gate{
10887c478bdstevel@tonic-gate	/* Inhibit compiler warning about unused parameter dev */
10897c478bdstevel@tonic-gate	if ( dev == NULL ) {};
10907c478bdstevel@tonic-gate	undi_full_shutdown();
10917c478bdstevel@tonic-gate	free_base_mem_data();
10927c478bdstevel@tonic-gate}
10937c478bdstevel@tonic-gate
10947c478bdstevel@tonic-gate/**************************************************************************
10957c478bdstevel@tonic-gatePROBE - Look for an adapter, this routine's visible to the outside
10967c478bdstevel@tonic-gate***************************************************************************/
10977c478bdstevel@tonic-gate
10987c478bdstevel@tonic-gate/* Locate an UNDI driver by first scanning through base memory for an
10997c478bdstevel@tonic-gate * installed driver and then by scanning for UNDI ROMs and attempting
11007c478bdstevel@tonic-gate * to install their drivers.
11017c478bdstevel@tonic-gate */
11027c478bdstevel@tonic-gate
11037c478bdstevel@tonic-gateint hunt_pixies_and_undi_roms ( void ) {
11047c478bdstevel@tonic-gate	static uint8_t hunt_type = HUNT_FOR_PIXIES;
11057c478bdstevel@tonic-gate
11067c478bdstevel@tonic-gate	if ( hunt_type == HUNT_FOR_PIXIES ) {
11077c478bdstevel@tonic-gate		if ( hunt_pixie() ) {
11087c478bdstevel@tonic-gate			return 1;
11097c478bdstevel@tonic-gate		}
11107c478bdstevel@tonic-gate	}
11117c478bdstevel@tonic-gate	hunt_type = HUNT_FOR_UNDI_ROMS;
11127c478bdstevel@tonic-gate	while ( hunt_undi_rom() ) {
11137c478bdstevel@tonic-gate		if ( undi_loader() ) {
11147c478bdstevel@tonic-gate			return 1;
11157c478bdstevel@tonic-gate		}
11167c478bdstevel@tonic-gate		undi_full_shutdown(); /* Free any allocated memory */
11177c478bdstevel@tonic-gate	}
11187c478bdstevel@tonic-gate	hunt_type = HUNT_FOR_PIXIES;
11197c478bdstevel@tonic-gate	return 0;
11207c478bdstevel@tonic-gate}
11217c478bdstevel@tonic-gate
11227c478bdstevel@tonic-gate/* The actual Etherboot probe routine.
11237c478bdstevel@tonic-gate */
11247c478bdstevel@tonic-gate
11257c478bdstevel@tonic-gatestatic int undi_probe(struct dev *dev, struct pci_device *pci)
11267c478bdstevel@tonic-gate{
11277c478bdstevel@tonic-gate	struct nic *nic = (struct nic *)dev;
11287c478bdstevel@tonic-gate
11297c478bdstevel@tonic-gate	/* Zero out global undi structure */
11307c478bdstevel@tonic-gate	memset ( &undi, 0, sizeof(undi) );
11317c478bdstevel@tonic-gate
11327c478bdstevel@tonic-gate	/* Store PCI parameters; we will need them to initialize the UNDI
11337c478bdstevel@tonic-gate	 * driver later.
11347c478bdstevel@tonic-gate	 */
11357c478bdstevel@tonic-gate	memcpy ( &undi.pci, pci, sizeof(undi.pci) );
11367c478bdstevel@tonic-gate
11377c478bdstevel@tonic-gate	/* Find the BIOS' $PnP structure */
11387c478bdstevel@tonic-gate	if ( ! hunt_pnp_bios() ) {
11397c478bdstevel@tonic-gate		printf ( "No PnP BIOS found; aborting\n" );
11407c478bdstevel@tonic-gate		return 0;
11417c478bdstevel@tonic-gate	}
11427c478bdstevel@tonic-gate
11437c478bdstevel@tonic-gate	/* Allocate base memory data structures */
11447c478bdstevel@tonic-gate	if ( ! allocate_base_mem_data() ) return 0;
11457c478bdstevel@tonic-gate
11467c478bdstevel@tonic-gate	/* Search thoroughly for UNDI drivers */
11477c478bdstevel@tonic-gate	for ( ; hunt_pixies_and_undi_roms(); undi_full_shutdown() ) {
11487c478bdstevel@tonic-gate		/* Try to initialise UNDI driver */
11497c478bdstevel@tonic-gate		DBG ( "Initializing UNDI driver.  Please wait...\n" );
11507c478bdstevel@tonic-gate		if ( ! undi_full_startup() ) {
11517c478bdstevel@tonic-gate			if ( undi.pxs->Status ==
11527c478bdstevel@tonic-gate			     PXENV_STATUS_UNDI_MEDIATEST_FAILED ) {
11537c478bdstevel@tonic-gate				DBG ( "Cable not connected (code %#hx)\n",
11547c478bdstevel@tonic-gate					 PXENV_STATUS_UNDI_MEDIATEST_FAILED );
11557c478bdstevel@tonic-gate			}
11567c478bdstevel@tonic-gate			continue;
11577c478bdstevel@tonic-gate		}
11587c478bdstevel@tonic-gate		/* Basic information: MAC, IO addr, IRQ */
11597c478bdstevel@tonic-gate		if ( ! eb_pxenv_undi_get_information() ) continue;
11607c478bdstevel@tonic-gate		DBG ( "Initialized UNDI NIC with IO %#hx, IRQ %d, MAC %!\n",
11617c478bdstevel@tonic-gate			 undi.pxs->undi_get_information.BaseIo,
11627c478bdstevel@tonic-gate			 undi.pxs->undi_get_information.IntNumber,
11637c478bdstevel@tonic-gate			 undi.pxs->undi_get_information.CurrentNodeAddress );
11647c478bdstevel@tonic-gate		/* Fill out MAC address in nic structure */
11657c478bdstevel@tonic-gate		memcpy ( nic->node_addr,
11667c478bdstevel@tonic-gate			 undi.pxs->undi_get_information.CurrentNodeAddress,
11677c478bdstevel@tonic-gate			 ETH_ALEN );
11687c478bdstevel@tonic-gate		/* More diagnostic information including link speed */
11697c478bdstevel@tonic-gate		if ( ! eb_pxenv_undi_get_iface_info() ) continue;
11707c478bdstevel@tonic-gate		printf ( "  NDIS type %s interface at %d Mbps\n",
11717c478bdstevel@tonic-gate			 undi.pxs->undi_get_iface_info.IfaceType,
11727c478bdstevel@tonic-gate			 undi.pxs->undi_get_iface_info.LinkSpeed / 1000000 );
11737c478bdstevel@tonic-gate		DBG ("UNDI Stack at %#hx:%#hx",UNDI_STACK_SEG, UNDI_STACK_OFF);
11747c478bdstevel@tonic-gate		dev->disable  = undi_disable;
11757c478bdstevel@tonic-gate		nic->poll     = undi_poll;
11767c478bdstevel@tonic-gate		nic->transmit = undi_transmit;
11777c478bdstevel@tonic-gate		return 1;
11787c478bdstevel@tonic-gate	}
11797c478bdstevel@tonic-gate	undi_disable ( dev ); /* To free base memory structures */
11807c478bdstevel@tonic-gate	return 0;
11817c478bdstevel@tonic-gate}
11827c478bdstevel@tonic-gate
11837c478bdstevel@tonic-gate/* UNDI driver states that it is suitable for any PCI NIC (i.e. any
11847c478bdstevel@tonic-gate * PCI device of class PCI_CLASS_NETWORK_ETHERNET).  If there are any
11857c478bdstevel@tonic-gate * obscure UNDI NICs that have the incorrect PCI class, add them to
11867c478bdstevel@tonic-gate * this list.
11877c478bdstevel@tonic-gate */
11887c478bdstevel@tonic-gatestatic struct pci_id undi_nics[] = {
11896148443Jakub Jermar	PCI_ROM(0x10de, 0x0057, "ck804", "nVidia Corporation CK804 Ethernet"),
11906148443Jakub Jermar	PCI_ROM(0x10de, 0x0373, "mcp55", "nVidia Corporation MCP55 Ethernet")
11917c478bdstevel@tonic-gate};
11927c478bdstevel@tonic-gate
11937c478bdstevel@tonic-gatestruct pci_driver undi_driver = {
11947c478bdstevel@tonic-gate	.type     = NIC_DRIVER,
11957c478bdstevel@tonic-gate	.name     = "UNDI",
11967c478bdstevel@tonic-gate	.probe    = undi_probe,
11977c478bdstevel@tonic-gate	.ids      = undi_nics,
11987c478bdstevel@tonic-gate 	.id_count = sizeof(undi_nics)/sizeof(undi_nics[0]),
11997c478bdstevel@tonic-gate	.class    = PCI_CLASS_NETWORK_ETHERNET,
12007c478bdstevel@tonic-gate};
120199ed608szhou
120299ed608szhou/************************************************
120399ed608szhou * Code for reusing the BIOS provided pxe stack
120499ed608szhou */
120599ed608szhou
120699ed608szhou/* Verify !PXE structure saved by pxeloader. */
120799ed608szhouint undi_bios_pxe(void **dhcpreply)
120899ed608szhou{
120999ed608szhou	pxe_t *pxe;
121099ed608szhou	uint16_t *ptr = (uint16_t *)0x7C80;
121199ed608szhou
121299ed608szhou	pxe = (pxe_t *) VIRTUAL(ptr[0], ptr[1]);
121399ed608szhou	if (memcmp(pxe->Signature, "!PXE", 4) != 0) {
121499ed608szhou		DBG ("invalid !PXE signature at %x:%x\n", ptr[0], ptr[1]);
121599ed608szhou		return 0;
121699ed608szhou	}
121799ed608szhou
121899ed608szhou	if (checksum(pxe, sizeof(pxe_t)) != 0) {
121999ed608szhou		DBG ("invalid checksum\n");
122099ed608szhou		return 0;
122199ed608szhou	}
122299ed608szhou
122399ed608szhou	/* Zero out global undi structure */
122499ed608szhou	memset (&undi, 0, sizeof(undi));
122599ed608szhou
122699ed608szhou	/* Allocate base memory data structures */
122799ed608szhou	if (! allocate_base_mem_data()) return 0;
122899ed608szhou
122999ed608szhou	undi.pxe = pxe;
123099ed608szhou	pxe_dump();
123199ed608szhou
123299ed608szhou	if (!eb_pxenv_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, dhcpreply)) {
123399ed608szhou		DBG ("failed to get cached DHCP reply\n");
123499ed608szhou		return 0;
123599ed608szhou	}
123699ed608szhou	return 1;
123799ed608szhou}
123899ed608szhou
123999ed608szhouvoid undi_pxe_disable(void)
124099ed608szhou{
124199ed608szhou	/* full shutdown is problematic for some machines */
124299ed608szhou	(void) eb_pxenv_undi_shutdown();
124399ed608szhou}
124499ed608szhou
124599ed608szhou/*
124699ed608szhou * Various helper functions for dhcp and tftp
124799ed608szhou */
124899ed608szhouint eb_pxenv_get_cached_info (uint8_t type, void **info)
124999ed608szhou{
125099ed608szhou	int success;
125199ed608szhou
125299ed608szhou	memset(undi.pxs, 0, sizeof (undi.pxs));
125399ed608szhou	/* Segment:offset pointer to DestAddr in base memory */
125499ed608szhou	undi.pxs->get_cached_info.PacketType = type;
125599ed608szhou	undi.pxs->get_cached_info.BufferSize = 0;
125699ed608szhou	undi.pxs->get_cached_info.Buffer.segment = 0;
125799ed608szhou	undi.pxs->get_cached_info.Buffer.offset = 0;
125899ed608szhou
125999ed608szhou	success = undi_call(PXENV_GET_CACHED_INFO);
126099ed608szhou	DBG ("PXENV_GET_CACHED_INFO <= Status=%s\n", UNDI_STATUS(undi.pxs));
126199ed608szhou
126299ed608szhou	*info = (void *)VIRTUAL(undi.pxs->get_cached_info.Buffer.segment,
126399ed608szhou	    undi.pxs->get_cached_info.Buffer.offset);
126499ed608szhou	return success;
126599ed608szhou}
126699ed608szhou
126799ed608szhou/* tftp help routines */
126899ed608szhouint eb_pxenv_tftp_open(char *file, IP4_t serverip, IP4_t gatewayip,
126999ed608szhou    uint16_t *pktlen)
127099ed608szhou{
127199ed608szhou	int success;
127299ed608szhou	memset(undi.pxs, 0, sizeof (undi.pxs));
127399ed608szhou	undi.pxs->tftp_open.ServerIPAddress = serverip;
127499ed608szhou	undi.pxs->tftp_open.GatewayIPAddress = gatewayip;
127599ed608szhou	undi.pxs->tftp_open.TFTPPort = htons(TFTP_PORT);
127699ed608szhou	undi.pxs->tftp_open.PacketSize = TFTP_MAX_PACKET;
127799ed608szhou	(void) sprintf(undi.pxs->tftp_open.FileName, "%s", file);
127899ed608szhou	success = undi_call(PXENV_TFTP_OPEN);
127999ed608szhou	DBG ("PXENV_TFTP_OPEN <= Status=%s\n", UNDI_STATUS(undi.pxs));
128099ed608szhou	*pktlen = undi.pxs->tftp_open.PacketSize;
128199ed608szhou	return success;
128299ed608szhou}
128399ed608szhou
128499ed608szhouint eb_pxenv_tftp_read(uint8_t *buf, uint16_t *len)
128599ed608szhou{
128699ed608szhou	static int tftp_count = 0;
128799ed608szhou
128899ed608szhou	int success;
128999ed608szhou	memset(undi.pxs, 0, sizeof (undi.pxs));
129099ed608szhou	undi.pxs->tftp_read.Buffer.segment = SEGMENT(buf);
129199ed608szhou	undi.pxs->tftp_read.Buffer.offset = OFFSET(buf);
129299ed608szhou	success = undi_call(PXENV_TFTP_READ);
129399ed608szhou	DBG ("PXENV_TFTP_READ <= Status=%s\n", UNDI_STATUS(undi.pxs));
129499ed608szhou	*len = undi.pxs->tftp_read.BufferSize;
129599ed608szhou	tftp_count++;
129699ed608szhou	if ((tftp_count % 1000) == 0)
12971fac5a6setje		noisy_printf(".");
129899ed608szhou	return success;
129999ed608szhou}
130099ed608szhou
130199ed608szhouint eb_pxenv_tftp_close(void)
130299ed608szhou{
130399ed608szhou	int success;
130499ed608szhou	memset(undi.pxs, 0, sizeof (undi.pxs));
130599ed608szhou	success = undi_call(PXENV_TFTP_CLOSE);
130699ed608szhou	DBG ("PXENV_TFTP_CLOSE <= Status=%s\n", UNDI_STATUS(undi.pxs));
130799ed608szhou	return success;
130899ed608szhou}
130999ed608szhou
131099ed608szhouint eb_pxenv_tftp_get_fsize(char *file, IP4_t serverip, IP4_t gatewayip,
131199ed608szhou    uint32_t *fsize)
131299ed608szhou{
131399ed608szhou	int success;
131499ed608szhou	memset(undi.pxs, 0, sizeof (undi.pxs));
131599ed608szhou	undi.pxs->tftp_open.ServerIPAddress = serverip;
131699ed608szhou	undi.pxs->tftp_open.GatewayIPAddress = gatewayip;
131799ed608szhou	(void) sprintf(undi.pxs->tftp_open.FileName, "%s", file);
131899ed608szhou	success = undi_call(PXENV_TFTP_GET_FSIZE);
131999ed608szhou	DBG ("PXENV_TFTP_GET_FSIZE <= Status=%s\n", UNDI_STATUS(undi.pxs));
132099ed608szhou	*fsize = undi.pxs->tftp_get_fsize.FileSize;
132199ed608szhou	return success;
132299ed608szhou}
1323