source: xtideuniversalbios/trunk/Serial_Server/library/Image.cpp @ 258

Last change on this file since 258 was 258, checked in by gregli@…, 12 years ago

Added floppy drive emulation over the serial connection (MODULE_SERIAL_FLOPPY). Along the way, various optimizations were made to stay within the 8K ROM size target. Also, serial code now returns the number of sectors transferred.

File size: 6.6 KB
RevLine 
[209]1//======================================================================
2//
3// Project:     XTIDE Universal BIOS, Serial Port Server
4//
5// File:        image.cpp - Abstract base class for disk image support
6//
7
8#include "library.h"
9#include <memory.h>
10#include <stdlib.h>
11#include <string.h>
[219]12#include <stdio.h>
[209]13
[258]14struct floppyInfo {
15    unsigned long size;
16    unsigned char type;
17    unsigned char cylinders;
18    unsigned char heads;
19    unsigned char sectors;
20} floppyInfos[] = 
21{
22{ 2949120 / 512, 6, 80, 2, 36 },   // 2.88MB 3.5"
23{ 1474560 / 512, 4, 80, 2, 18 },        // 1.44MB 3.5"
24{ 1228800 / 512, 2, 80, 2, 15 },     // 1.2MB 5.25"
25{ 737280 / 512, 3, 80, 1, 18 },    // 720KB 3.5"
26{ 368640 / 512, 1, 40, 2, 9 }, // 360KB 5.25"
27{ 327680 / 512, 0, 40, 2, 8 }, // 320KB 5.25"
28{ 184320 / 512, 0, 40, 1, 9 }, // 180KB 5.25" single sided
29{ 163840 / 512, 0, 40, 1, 8 }, // 160KB 5.25" single sided
30{ 0, 0, 0, 0, 0 }
31};
32
[209]33Image::Image( char *name, int p_readOnly, int p_drive )
34{
35}
36
37Image::Image( char *name, int p_readOnly, int p_drive, int p_create, unsigned long p_lba )
38{
39}
40
[217]41Image::Image( 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 )
[209]42{
43}
44
[217]45void Image::init( char *name, int p_readOnly, int p_drive, unsigned long p_cyl, unsigned long p_head, unsigned long p_sect, int p_useCHS )
[209]46{
[217]47    double sizef;
[258]48    char sizeChar;
49    struct floppyInfo *f;
[217]50
[209]51    for( char *c = shortFileName = name; *c; c++ )
52        if( *c == '\\' || *c == '/' || *c == ':' )
53            shortFileName = c+1;
54
55    if( *(shortFileName) == 0 )
[211]56    {
57        log( 1, "Can't parse '%s' for short file name\n\n", name );
58        shortFileName = "SerDrive";
59    }
[209]60 
61    readOnly = p_readOnly;
62    drive = p_drive;
[217]63
64    if( totallba > 0xfffffff )     // lba28 limit - 28 bits
65        log( -1, "'%s', Image size larger than LBA28 maximum of 137,438,952,960 bytes, %lu", name, totallba );
66
67    if( totallba == 0 )
68        log( -1, "'%s', Image size zero?" );
69
[258]70    floppy = 0;
71    for( f = floppyInfos; f->size && f->size != totallba; f++ ) ;
72    if( f->size )
73    {
74        floppy = 1;
75        floppyType = f->type;
76        p_useCHS = 1;
77        p_cyl = f->cylinders;
78        p_head = f->heads;
79        p_sect = f->sectors;
80    }
81
[217]82    if( p_useCHS )
83    {
84        if( p_cyl )
85        {
86            if( p_sect > 63 || (p_head > 16 || p_head < 1) || (p_cyl > 1024 || p_cyl < 1) )
87                log( -1, "'%s', parts of the CHS geometry (%lu:%lu:%lu) are out of the range (1-1024:1-16:1-63)", name, p_cyl, p_head, p_sect );
88            else if( totallba != (p_sect * p_head * p_cyl) )
89                log( -1, "'%s', file size does not match geometry", name );
90            sect = p_sect;
91            head = p_head;
92            cyl = p_cyl;
93        }
94        else
95        {
96            if( (totallba % 16) != 0 || ((totallba/16) % 63) != 0 )
97                log( -1, "'%s', file size does not match standard CHS geometry (x:16:63), please specify geometry explicitly with -g", name );
98            else
99            {
100                sect = 63;
101                head = 16;
102                cyl = (totallba / sect / head);
103                if( cyl > 1024 )
104                    log( -1, "'%s', CHS geometry of %lu:%lu:%lu is larger than maximum values 1024:16:63", name, cyl, head, sect );
105            }
106        }
107    }
108    else
109    {
110        sect = 0;
111        head = 0;
112        cyl = 0;
113    }
114    useCHS = p_useCHS;
115
116    sizef = totallba/2048.0;
[258]117    sizeChar = 'M';
118    if( sizef < 1 ) 
119    {
120        sizef *= 1024;
121        sizeChar = 'K';
122    }
[217]123    if( useCHS )
[258]124        log( 0, "%s: %s with CHS geometry %u:%u:%u, size %.2lf %cB",
125             name, (floppy ? "Floppy Disk" : "Hard Disk"), cyl, head, sect, sizef, sizeChar );
[217]126    else
[258]127        log( 0, "%s: %s with total sectors %lu, size %.2lf %cB", 
128             name, (floppy ? "Floppy Disk" : "Hard Disk"), totallba, sizef, sizeChar );
[209]129}
130
[217]131int Image::parseGeometry( char *str, unsigned long *p_cyl, unsigned long *p_head, unsigned long *p_sect )
[209]132{
133    char *c, *s, *h;
134    unsigned long cyl, sect, head;
135
136    c = str;
[217]137    for( h = c; *h && *h != ':' && *h != 'x' && *h != 'X'; h++ ) ;
138    if( !*h )
139        return( 0 );
140
141    *h = '\0';
142    h++;
143    for( s = h+1; *s && *s != ':' && *s != 'x' && *s != 'X'; s++ ) ; 
[209]144    if( !*s )
145        return( 0 );
146
147    *s = '\0';
148    s++;
149
150    cyl = atol(c);
[217]151    head = atol(h);
[209]152    sect = atol(s);
153
154    if( cyl == 0 || sect == 0 || head == 0 )
155        return( 0 );
156
157    *p_cyl = cyl;
[217]158    *p_head = head;
[209]159    *p_sect = sect;
160
161    return( 1 );
162}
163
164#define ATA_wGenCfg 0
165#define ATA_wCylCnt 1
166#define ATA_wHeadCnt 3
167#define ATA_wBpTrck 4
168#define ATA_wBpSect 5
169#define ATA_wSPT 6
170#define ATA_strSerial 10
171#define ATA_strFirmware 23
172#define ATA_strModel 27
173#define ATA_wCaps 49
174#define ATA_wCurCyls 54
175#define ATA_wCurHeads 55
176#define ATA_wCurSPT 56
177#define ATA_dwCurSCnt 57
178#define ATA_dwLBACnt 60
179
[258]180// Words carved out of the vendor specific area for our use
181//
182#define ATA_wSerialFloppyFlagAndType 158
183#define ATA_wSerialPortAndBaud 159
[209]184
[258]185// Defines used in the words above
186//
[209]187#define ATA_wCaps_LBA 0x200
188
189#define ATA_wGenCfg_FIXED 0x40
190
[258]191#define ATA_wSerialFloppyFlagAndType_Flag 0x10
192#define ATA_wSerialFloppyFlagAndType_TypePosition 5
193
[209]194struct comPorts {
195    unsigned long port;
196    unsigned char com;
197};
198struct comPorts supportedComPorts[] = 
199{ 
200  { 0x3f8, '1' }, 
201  { 0x2f8, '2' }, 
202  { 0x3e8, '3' }, 
203  { 0x2e8, '4' }, 
204  { 0x2f0, '5' }, 
205  { 0x3e0, '6' }, 
206  { 0x2e0, '7' }, 
207  { 0x260, '8' },
208  { 0x368, '9' },
209  { 0x268, 'A' },
210  { 0x360, 'B' },
211  { 0x270, 'C' },
212  { 0, 0 } 
213};
214
[233]215void Image::respondInquire( unsigned short *buff, struct baudRate *baudRate, unsigned short port, unsigned char scan )
[209]216{
[233]217    memset( &buff[0], 0, 514 );
[209]218
[233]219    if( scan )
[209]220    {
[233]221        unsigned short comPort = 0;
222        struct comPorts *cp;
223
224        if( port )
225        {
226            for( cp = supportedComPorts; cp->port && cp->port != port; cp++ ) ;
227            if( cp->port )
228                comPort = cp->com;
229        }
230
231        if( comPort )
232            sprintf( (char *) &buff[ATA_strModel], "%.15s (COM%c/%s)", shortFileName, comPort, baudRate->display );
233        else
234            sprintf( (char *) &buff[ATA_strModel], "%.25s (%s baud)", shortFileName, baudRate->display );
[209]235    }
236    else
[233]237        sprintf( (char *) &buff[ATA_strModel], "%.30s", shortFileName );
[209]238
239    strncpy( (char *) &buff[ATA_strSerial], "serial", 20 );
240    strncpy( (char *) &buff[ATA_strFirmware], "firmw", 8 );
241
242    for( int t = ATA_strModel; t < ATA_strModel+40; t++ )
243        buff[t] = (buff[t] >> 8) | (buff[t] << 8);
244
[217]245    if( useCHS )
246    {
247        buff[ ATA_wCylCnt ] = cyl;
248        buff[ ATA_wHeadCnt ] = head;
249        buff[ ATA_wSPT ] = sect;
250    }
251    else
252    {
253        buff[ ATA_wCaps ] = ATA_wCaps_LBA;
254        buff[ ATA_dwLBACnt ] = (unsigned short) (totallba & 0xffff);
255        buff[ ATA_dwLBACnt+1 ] = (unsigned short) (totallba >> 16);
256    }
257
[258]258    if( floppy )
259        buff[ ATA_wSerialFloppyFlagAndType ] = ATA_wSerialFloppyFlagAndType_Flag | (floppyType << ATA_wSerialFloppyFlagAndType_TypePosition);
260
261    // we always set this, so that the bulk of the BIOS will consider this disk as a hard disk
262    //
[209]263    buff[ ATA_wGenCfg ] = ATA_wGenCfg_FIXED;
264}
Note: See TracBrowser for help on using the repository browser.