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

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

Moved the bulk of the serial code to the assembly library, for inclusion in other utilities. Fixed a bug in int13h.asm when floppy support was not enabled that was preventing foreign drives from working properly.

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