/****************************************************************************/
/* */
/* FILE NAME: SERVER.C <B007> */
/* */
/* DESCRIPTION: This file contains the NFS server (validator side) */
/* */
/* OPERATING SYSTEM: RTEMS-v4.0 */
/* */
/* PERSON RESPONSIBLE: Isti */
/* */
/* +--------------------------------------------------------------------+ */
/* | Note: This code was written using 100% recycled instructions! | */
/* +--------------------------------------------------------------------+ */
/* */
/* Copyright � 2002-200x, Bendor Research Pty Ltd. A/ll rights reserved. */
/* Copyright � 2002-200x, VFJ Technology Pty Ltd. All rights reserved. */
/* */
/*==========================================================================*/
/* */
/* REVISION HISTORY: */
/* */
/* RevId Date Who Reason */
/* ---------------------------------------------------------------------- */
/* v1.00 16/10/02 ZK Initial release */
/* 26/10/02 IE Added more functions, fixed bugs */
/* */
/****************************************************************************/
//#define DEBUG
#include <sys/syslog.h>
#include "nfs_intern.h"
#include "huffman.h"
#include <util/rtclock.h>
#include <sys/filesys.h>
#include <sys/sysdefs.h>
/*==========================================================================*/
/* Function prototypes */
/*==========================================================================*/
static void NfsServer( void *unused );
static int DoDecompression ( void );
static void NfsServerProcessCmd( void );
static void NfsServerRegister( int handle );
static void NfsServerUnregister( int handle );
static void NfsServerFinish( void );
static void NfsServerOpen( void );
static void NfsServerClose( void );
static void NfsServerRead( void );
static void NfsServerWrite( void );
static void NfsServerSeek( void );
static void NfsServerFsize( void );
static void NfsServerDelete( void );
static void NfsServerRename( void );
static void NfsServerReaddir( void );
static void NfsServerFormat( void );
static void NfsServerSettime( void );
static void NfsServerTestcmd( void );
static void NfsServerUnknown( void );
//static int DoDecompression ( void );
/*==========================================================================*/
/* Local data */
/*==========================================================================*/
static NFS_DRV self; /* Device descriptor */
/*--------------------------------------------------------------------------*/
/* Function table */
/*--------------------------------------------------------------------------*/
static const NFS_FUN functab[] = {
{ NFS_CODE_BYE, NfsServerFinish },
{ NFS_CODE_OPEN, NfsServerOpen },
{ NFS_CODE_CLOSE, NfsServerClose },
{ NFS_CODE_READ, NfsServerRead },
{ NFS_CODE_WRITE, NfsServerWrite },
{ NFS_CODE_SEEK, NfsServerSeek },
{ NFS_CODE_FSIZE, NfsServerFsize },
{ NFS_CODE_DELETE, NfsServerDelete },
{ NFS_CODE_RENAME, NfsServerRename },
{ NFS_CODE_READDIR, NfsServerReaddir },
{ NFS_CODE_FORMAT, NfsServerFormat },
{ NFS_CODE_SETTIME, NfsServerSettime },
{ NFS_CODE_TESTCMD, NfsServerTestcmd },
{ 0, NULL }
};
/* Funzioni per la decompressione */
#define HUFF_MAXCODE 32 /* Max. length of a code word */
#define HUFF_DECTREE 512 /* Max. length of decoding tree */
static ubyte next;
static uword copy_comp; /* Compression flag */
static HUFFMAN copy_huff; /* Huffman encoder data */
static ulong copy_htab[ 256 ]; /* Huffman encoder statistics */
static uword copy_frst; /* First block flag */
static uword copy_down; /* Download flag */
static uword copy_actv = FALSE; /* Copy process is active */
static ulong copy_flen; /* Length of the file */
//static HUFFMAN copy_huff; /* Huffman encoder data */
//static ulong copy_htab[ 256 ]; /* Huffman encoder statistics */
static ubyte copy_fbuf[ NFS_MAXD ]; /* File buffer, uncompressed */
static uword copy_fsiz; /* File buffer content length */
static uword copy_finx; /* File buffer index */
static ubyte copy_cbuf[2*NFS_MAXD]; /* Compressed file buffer */
static uword copy_cfwp; /* Compressed file buffer write ptr */
static uword copy_cfrp; /* Compressed file buffer read ptr */
static ulong readed;
static void DecompressBlock(void);
static ubyte XnfsCopyDownloadGetNext( void );
static uword XnfsCopyDownloadPutNext( ubyte byte );
static void XnfsCopyDownloadAppend(long datalen, ubyte *data );
static int DecompressFile(char ftodecomp[NAME_MAX + 7]);
#define HEADER_COMPRESSED_FILE '@'
#define COMPRESSED_FILE "/flash/@@.tmp"
/*==========================================================================*/
/* Functions realising the server's capabilities */
/*==========================================================================*/
/*--------------------------------------------------------------------------*/
/* Initialises the NFS server */
/*--------------------------------------------------------------------------*/
DEVICE_INIT( NFS_SERVER )
{
int i;
// self.nam = "NFS SERVER";
self.nam = "NFS[S]";
self.usr.req = 0;
self.usr.stat = NFS_STAT_CLOSED;
for ( i = 0; i < NFS_MAXF; i++ ) self.fhd[ i ] = -1; /* IE: 26/10/02 */
sspawn( "NfsServer", NfsServer, NULL, 4096 );
return( 0 );
}
/*--------------------------------------------------------------------------*/
/* Starts the NFS server. The parameter is the HW ID of this node. */
/*--------------------------------------------------------------------------*/
void NfsServerStart( ulong hwid )
{
self.usr.hwid = hwid;
self.usr.req = NFS_START;
while ( self.usr.req ) sleep( 100 );
}
/*void DecompressionStart( void )
{
self.usr.stat=NFS_STAT_DECOMPRESSION_START;
self.usr.req = NFS_DECOMPRESS;
while ( self.usr.req ) sleep( 100 );
self.usr.stat=NFS_STAT_DECOMPRESSION_FINISHED;
}
*/
/*--------------------------------------------------------------------------*/
/* Stops the NFS server */
/*--------------------------------------------------------------------------*/
void NfsServerStop( void )
{
self.usr.req = NFS_STOP;
while ( self.usr.req ) sleep( 100 );
}
/*--------------------------------------------------------------------------*/
/* Returns the current status of the NFS server */
/*--------------------------------------------------------------------------*/
int NfsServerStatus( void )
{
return( self.usr.stat );
}
/*==========================================================================*/
/* The NFS-server process */
/*==========================================================================*/
static void NfsServer( void *unused )
{
self.xrf = -1;
if ( self.usr.stat = setjmp( self.err ) ) {
/*------------------------------------------------------------------*/
/* This is the state when we have to shut down the server */
/*------------------------------------------------------------------*/
NfsCloseAll( &self );
if ( self.xrf >= 0 ) {
ioctl( self.xrf, IOC_RFLINK_CLOSE );
close( self.xrf );
self.xrf = -1;
}
/*------------------------------------------------------------------*/
/* Wait for the user to acknowledge the error status and stop us */
/*------------------------------------------------------------------*/
for ( ;; ) {
if ( self.usr.req == NFS_STOP ) break;
self.usr.req = 0;
sleep( 100 );
}
if ( self.usr.stat != NFS_STAT_FINISHED )
self.usr.stat = NFS_STAT_CLOSED;
self.usr.req = 0;
}
for ( ;; ) {
/*------------------------------------------------------------------*/
/* Wait until we are started by the user */
/*--------------