Changeset 219 in xtideuniversalbios for trunk/Serial_Server


Ignore:
Timestamp:
Jan 25, 2012, 7:04:43 AM (12 years ago)
Author:
gregli@…
google:author:
gregli@hotmail.com
Message:

Serial Server, more minor improvements, added support for larger than 2 GB disks under Win32

Location:
trunk/Serial_Server
Files:
2 added
2 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/Serial_Server/library/FlatImage.h

    r217 r219  
    66//
    77
    8 #include <stdio.h>
    98#include "library.h"
    109
     
    1211{
    1312private:
    14     FILE *fp;
     13    class FileAccess fp;
    1514
    1615public:
    17     FlatImage( char *name, int p_readOnly, int p_drive, int create, unsigned long cyl, unsigned long head, unsigned long sect, int useCHS );
    18     ~FlatImage();
     16    FlatImage( char *name, int p_readOnly, int p_drive, int p_create, unsigned long p_cyl, unsigned long p_head, unsigned long p_sect, int p_useCHS )   :   Image( name, p_readOnly, p_drive, p_create, p_cyl, p_head, p_sect, p_useCHS )
     17    {
     18        long filesize;
    1919
    20     int seekSector( unsigned long cyl, unsigned long sect, unsigned long head );
    21     int seekSector( unsigned long lba );
    22     int writeSector( void *buff );
    23     int readSector( void *buff );
     20        if( p_create )
     21        {
     22            char buff[512];
     23            unsigned long size;
     24            double sizef;
     25            FileAccess cf;
     26
     27            size = (unsigned long) p_cyl * (unsigned long) p_sect * (unsigned long) p_head;
     28            if( size > cf.MaxSectors )
     29                log( -1, "'%s', can't create flat file with size greater than %lu 512-byte sectors", name, cf.MaxSectors );
     30            sizef = size / 2048.0;   // 512 byte sectors -> MB
     31
     32            cf.Create( name );
     33
     34            memset( &buff[0], 0, 512 );
     35            while( size-- )
     36                cf.Write( &buff[0], 512 );
     37       
     38            if( p_cyl > 1024 )
     39                log( 0, "Created file '%s', size %.1lf MB", name, sizef );
     40            else
     41                log( 0, "Created file '%s', geometry %u:%u:%u, size %.1lf MB", name, p_cyl, p_sect, p_head, sizef );
     42
     43            cf.Close();
     44        }
     45
     46        fp.Open( name );
     47
     48        totallba = fp.SizeSectors();
     49
     50        init( name, p_readOnly, p_drive, p_cyl, p_head, p_sect, p_useCHS );
     51    }
     52
     53    FlatImage::~FlatImage()
     54    {
     55        fp.Close();
     56    }
     57
     58    void seekSector( unsigned long lba )
     59    {
     60        fp.SeekSectors( lba );
     61    }
     62
     63    void writeSector( void *buff )
     64    {
     65        fp.Write( buff, 512 );
     66    }
     67
     68    void readSector( void *buff )
     69    {
     70        fp.Read( buff, 512 );
     71    }
    2472};
    2573
  • trunk/Serial_Server/library/Image.cpp

    r217 r219  
    1010#include <stdlib.h>
    1111#include <string.h>
     12#include <stdio.h>
    1213
    1314Image::Image( char *name, int p_readOnly, int p_drive )
     
    8283    sizef = totallba/2048.0;
    8384    if( useCHS )
    84         log( 0, "Opening '%s', CHS geometry %u:%u:%u, total size %.1lf MB", name, cyl, sect, head, sizef );
    85     else
    86         log( 0, "Opening '%s', total lba %lu, total size %.1lf MB", name, totallba, sizef );
     85        log( 0, "Opening '%s', CHS geometry %u:%u:%u, total LBA %lu, total size %.1lf MB", name, cyl, sect, head, totallba, sizef );
     86    else
     87        log( 0, "Opening '%s', total LBA %lu, total size %.1lf MB", name, totallba, sizef );
    8788}
    8889
  • trunk/Serial_Server/library/Library.h

    r217 r219  
    99#define LIBRARY_H_INCLUDED
    1010
    11 #include "stdio.h"
     11void log( int level, char *message, ... );
    1212
    13 void log( int level, char *message, ... );
    1413unsigned long GetTime(void);
    1514unsigned long GetTime_Timeout(void);
     
    2019{
    2120public:
    22     virtual int seekSector( unsigned long cyl, unsigned long sect, unsigned long head ) = 0;
    23     virtual int seekSector( unsigned long lba ) = 0;
     21    virtual void seekSector( unsigned long lba ) = 0;
    2422
    25     virtual int writeSector( void *buff ) = 0;
     23    virtual void writeSector( void *buff ) = 0;
    2624   
    27     virtual int readSector( void *buff ) = 0;
     25    virtual void readSector( void *buff ) = 0;
    2826
    2927    Image( char *name, int p_readOnly, int p_drive );
     
    5755struct baudRate *baudRateMatchDivisor( unsigned char divisor );
    5856
    59 class Serial
    60 {
    61 public:
    62     virtual unsigned long readCharacters( void *buff, unsigned long len ) = 0;
     57#ifdef WIN32
     58#include "../win32/win32serial.h"
     59#else
     60// there is no standard way to read/write and configure the serial port, OS specifc only
     61#endif
    6362
    64     virtual unsigned long writeCharacters( void *buff, unsigned long len ) = 0;
     63#ifdef WIN32
     64#include "../win32/win32file.h"
     65#else
     66#include "file.h"
     67#endif
    6568
    66     Serial( char *name, struct baudRate *p_baudRate )
    67     {
    68         speedEmulation = 0;
    69         resetConnection = 0;
    70         baudRate = p_baudRate;
    71     };
    72 
    73     virtual ~Serial() {};
    74 
    75     int speedEmulation;
    76     int resetConnection;
    77 
    78     struct baudRate *baudRate;
    79 };
    80 
    81 void processRequests( Serial *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel );
    82 
    83 #define ATA_COMMAND_LBA 0x40
    84 #define ATA_COMMAND_HEADMASK 0xf
    85 
    86 #define ATA_DriveAndHead_Drive 0x10
     69void processRequests( SerialAccess *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel );
    8770
    8871#endif
  • trunk/Serial_Server/library/Process.cpp

    r217 r219  
    99#include <memory.h>
    1010#include <string.h>
     11#include <stdio.h>
    1112
    1213union _buff {
     
    5455#define SERIAL_INQUIRE_PORTANDBAUD_PORTTRANSLATE( a ) ( ((a) & SERIAL_INQUIRE_PORTANDBAUD_PORT) << 1 | SERIAL_INQUIRE_PORTANDBAUD_STARTINGPORT )
    5556
     57#define ATA_COMMAND_LBA 0x40
     58#define ATA_COMMAND_HEADMASK 0xf
     59
     60#define ATA_DriveAndHead_Drive 0x10
     61
    5662void logBuff( char *message, unsigned long buffoffset, unsigned long readto, int verboseLevel )
    5763{
     
    7581}
    7682
    77 void processRequests( Serial *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel )
     83void processRequests( SerialAccess *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel )
    7884{
    7985    unsigned char workCommand;
     
    204210            // Echo back the CRC
    205211            //
    206             if( serial->writeCharacters( &buff.w[256], 2 ) != 2 )
    207                 log( 0, "Serial Port Write Error" );
     212            if( !serial->writeCharacters( &buff.w[256], 2 ) )
     213                break;
    208214
    209215            workOffset++;
     
    340346                buff.w[256] = checksum( &buff.w[0], 256 );
    341347
    342                 if( serial->writeCharacters( &buff.w[0], 514 ) != 514 )
    343                     log( 0, "Serial Port Write Error" );
     348                if( !serial->writeCharacters( &buff.w[0], 514 ) )
     349                    break;
    344350
    345351                if( verboseLevel >= 3 )
  • trunk/Serial_Server/makefile

    r211 r219  
    99
    1010CL = cl
    11 CLFLAGS = /Ox
     11CLFLAGS = /Ox /DWIN32
    1212
    13 WIN32OBJS = build/win32.obj build/win32serial.obj build/checksum.obj build/serial.obj build/flatimage.obj build/process.obj build/image.obj
     13WIN32OBJS = build/win32.obj build/checksum.obj build/serial.obj build/process.obj build/image.obj
    1414
    1515build/serdrive.exe: $(WIN32OBJS)
     
    2222    $(CL) /c $(CLFLAGS) $< /Fo$@
    2323
    24 $(WIN32OBJS):   library/library.h
     24$(WIN32OBJS):   library/library.h win32/win32file.h win32/win32serial.h library/file.h library/flatimage.h
    2525
    2626build/checksum_test.exe:    library/checksum.cpp
  • trunk/Serial_Server/win32/Win32.cpp

    r217 r219  
    1616#include "../library/library.h"
    1717#include "../library/flatimage.h"
    18 #include "Win32Serial.h"
    1918
    2019void usage(void)
     
    3029        "",
    3130        "  -n [megabytes]      Create new disk with given size or use -g geometry",
    32         "                      Maximum size is 137.4 GB (LBA28 limit)",
     31        "                      Maximum size is " USAGE_MAXSECTORS,
    3332        "                      (default is a 32 MB disk, with CHS geometry 65:63:16)",
    3433        "",
     
    7473    unsigned short wbuff[256];
    7574
    76     Serial *serial;
     75    SerialAccess serial;
    7776    Image *img;
    7877    struct baudRate *baudRate = NULL;
     
    178177    do
    179178    {
    180         serial = new Win32Serial( ComPort, baudRate );
    181 
    182         processRequests( serial, images[0], images[1], timeoutEnabled, verbose );
    183 
    184         delete serial;
    185 
    186         if( serial->resetConnection )
    187             log( 0, "Connection closed, reset..." );
    188     }
    189     while( serial->resetConnection );
     179        serial.Connect( ComPort, baudRate );
     180
     181        processRequests( &serial, images[0], images[1], timeoutEnabled, verbose );
     182
     183        serial.Disconnect();
     184
     185        if( serial.resetConnection )
     186            log( 0, "Serial Connection closed, reset..." );
     187    }
     188    while( serial.resetConnection );
    190189}
    191190
  • trunk/Serial_Server/win32/Win32Serial.h

    r209 r219  
    66//
    77
     8#include <stdio.h>
    89#include "windows.h"
    910#include "../library/library.h"
     
    1112#define PIPENAME "\\\\.\\pipe\\xtide"
    1213
    13 class Win32Serial : public Serial
     14class SerialAccess
    1415{
    1516public:
    16     Win32Serial( char *name, struct baudRate *baudRate );
    17     ~Win32Serial();
     17    void Connect( char *name, struct baudRate *p_baudRate )
     18    {
     19        char buff1[20], buff2[1024];
    1820
    19     unsigned long readCharacters( void *buff, unsigned long len );
    20     unsigned long writeCharacters( void *buff, unsigned long len );
     21        baudRate = p_baudRate;
     22
     23        pipe = NULL;
     24   
     25        if( !name )
     26        {
     27            for( int t = 1; t <= 30 && !name; t++ )
     28            {
     29                sprintf( buff1, "COM%d", t );
     30                if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) )
     31                    name = buff1;
     32            }
     33            if( !name )
     34                log( -1, "No physical COM ports found" );
     35        }
     36
     37        if( !strcmp( name, "PIPE" ) )
     38        {
     39            log( 0, "Opening named pipe %s (simulating %lu baud)", PIPENAME, baudRate->rate );
     40       
     41            pipe = CreateNamedPipeA( PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_REJECT_REMOTE_CLIENTS, 2, 1024, 1024, 0, NULL );
     42            if( !pipe )
     43                log( -1, "Could not CreateNamedPipe " PIPENAME );
     44       
     45            if( !ConnectNamedPipe( pipe, NULL ) )
     46                log( -1, "Could not ConnectNamedPipe" );
     47
     48            if( baudRate->divisor > 3 )
     49                log( -1, "Cannot simulate baud rates with hardware multipliers" );
     50
     51            speedEmulation = 1;
     52            resetConnection = 1;
     53        }
     54        else
     55        {
     56            if( QueryDosDeviceA( name, buff2, sizeof(buff2) ) )
     57            {
     58                COMMTIMEOUTS timeouts;
     59                DCB dcb;
     60
     61                log( 0, "Opening %s (%lu baud)", name, baudRate->rate );
     62           
     63                pipe = CreateFileA( name, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
     64                if( !pipe )
     65                    log( -1, "Could not Open \"%s\"", name );
     66           
     67                FillMemory(&dcb, sizeof(dcb), 0);
     68                FillMemory(&timeouts, sizeof(timeouts), 0);
     69
     70                dcb.DCBlength = sizeof(dcb);
     71                dcb.BaudRate = baudRate->rate;
     72                dcb.ByteSize = 8;
     73                dcb.StopBits = ONESTOPBIT;
     74                dcb.Parity = NOPARITY;
     75                if( !SetCommState( pipe, &dcb ) )
     76                    log( -1, "Could not SetCommState" );
     77
     78                if( !SetCommTimeouts( pipe, &timeouts ) )
     79                    log( -1, "Could not SetCommTimeouts" );
     80            }
     81            else
     82            {
     83                char logbuff[ 1024 ];
     84                int found = 0;
     85
     86                sprintf( logbuff, "serial port '%s' not found, detected COM ports:", name );
     87
     88                for( int t = 1; t <= 40; t++ )
     89                {
     90                    sprintf( buff1, "COM%d", t );
     91                    if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) )
     92                    {
     93                        strcat( logbuff, "\n    " );
     94                        strcat( logbuff, buff1 );
     95                        found = 1;
     96                    }
     97                }
     98                if( !found )
     99                    strcat( logbuff, "\n    (none)" );
     100               
     101                log( -1, logbuff );
     102            }
     103        }
     104    }
     105
     106    void Disconnect()
     107    {
     108        if( pipe )
     109        {
     110            CloseHandle( pipe );
     111            pipe = NULL;
     112        }
     113    }
     114
     115    unsigned long readCharacters( void *buff, unsigned long len )
     116    {
     117        unsigned long readLen;
     118        int ret;
     119
     120        ret = ReadFile( pipe, buff, len, &readLen, NULL );
     121
     122        if( !ret || readLen == 0 )
     123        {
     124            if( GetLastError() == ERROR_BROKEN_PIPE )
     125                return( 0 );
     126            else
     127                log( -1, "read serial failed (error code %d)", GetLastError() );
     128        }
     129
     130        return( readLen );
     131    }
     132
     133    int writeCharacters( void *buff, unsigned long len )
     134    {
     135        unsigned long writeLen;
     136        int ret;
     137
     138        ret = WriteFile( pipe, buff, len, &writeLen, NULL );
     139
     140        if( !ret || len != writeLen )
     141        {
     142            if( GetLastError() == ERROR_BROKEN_PIPE )
     143                return( 0 );
     144            else
     145                log( -1, "write serial failed (error code %d)", GetLastError() );
     146        }
     147
     148        return( 1 );
     149    }
     150
     151    SerialAccess()
     152    {
     153        pipe = NULL;
     154        speedEmulation = 0;
     155        resetConnection = 0;
     156        baudRate = NULL;
     157    }
     158
     159    ~SerialAccess()
     160    {
     161        Disconnect();
     162    }
     163
     164    int speedEmulation;
     165    int resetConnection;
     166
     167    struct baudRate *baudRate;
    21168
    22169private:
Note: See TracChangeset for help on using the changeset viewer.