source: xtideuniversalbios/trunk/Serial_Server/library/FlatImage.cpp @ 209

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

Initial checkin for the Serial Server code, to be run on a host computer with a hard disk image file. Connected via a serial line, this provides the I/O for the serial port support in the XTIDE bios. At present, this is a Win32 command line program, run without parameters for usage information.

File size: 2.7 KB
Line 
1//======================================================================
2//
3// Project:     XTIDE Universal BIOS, Serial Port Server
4//
5// File:        FlatImage.cpp - Basic flat disk image file support
6//
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <memory.h>
11
12#include "FlatImage.h"
13
14FlatImage::FlatImage( char *name, int p_readOnly, int p_drive, int p_create, unsigned long p_cyl, unsigned long p_sect, unsigned long p_head )   :   Image( name, p_readOnly, p_drive, p_create, p_cyl, p_sect, p_head )
15{
16    double sizef;
17
18    if( p_create )
19    {
20        char buff[512];
21        unsigned long size;
22        unsigned long size2;
23        double sizef;
24
25        fp = fopen( name, "r" );
26        if( fp )
27            log( 0, "Create Failure: '%s' already exists", name );
28       
29        if( !(fp = fopen( name, "w" )) )
30            log( 0, "Could not create file '%s'", name );
31
32        memset( &buff[0], 0, 512 );
33        size2 = size = (unsigned long) p_cyl * (unsigned long) p_sect * (unsigned long) p_head;
34        while( size-- )
35        {
36            if( fwrite( &buff[0], 1, 512, fp ) != 512 )
37                log( 0, "Create write black sector error" );
38        }
39        fclose( fp );
40       
41        sizef = size2/2048.0;
42        log( 1, "Created file '%s' with geometry %u:%u:%u, size %.1lf megabytes\n", name, p_cyl, p_sect, p_head, sizef );
43    }
44
45    fp = fopen( name, "r+" );
46    if( !fp )
47        log( 0, "Could not Open %s", name );
48
49    log( 1, "Opening disk image '%s'", name );
50
51    fseek( fp, 0, SEEK_END );
52    totallba = ftell( fp );
53
54    if( !totallba )
55        log( 0, "Could not get file size" );
56
57    if( totallba & 0x1ff )
58        log( 0, "File not made up of 512 byte sectors" );
59
60    totallba >>= 9;
61    if( totallba != (p_sect * p_head * p_cyl) )
62    {
63        if( p_sect || p_head || p_cyl )
64            log( 0, "File size does not match geometry" );
65        else if( (totallba % 16) != 0 || ((totallba/16) % 63) != 0 )
66            log( 0, "File size does not match standard geometry (x:16:63), please give explicitly with -g" );
67        else
68        {
69            sect = 63;
70            head = 16;
71            cyl = (totallba / sect / head);
72        }
73    }
74    else
75    {
76        sect = p_sect;
77        head = p_head;
78        cyl = p_cyl;
79    }
80
81    sizef = totallba/2048.0;
82    log( 1, "Using geometry %u:%u:%u, total size %.1lf megabytes", cyl, sect, head, sizef );
83}
84
85int FlatImage::seekSector( unsigned long cyl, unsigned long sect, unsigned long head )
86{
87    return( 0 );
88}
89
90int FlatImage::seekSector( unsigned long lba )
91{
92    return( fseek( fp, lba * 512, SEEK_SET ) );
93}
94
95int FlatImage::writeSector( void *buff )
96{
97    int r;
98
99    r = fwrite( buff, 1, 512, fp );
100    fflush( fp );
101   
102    return( r == 512 ? 0 : 1 );
103}
104
105int FlatImage::readSector( void *buff )
106{
107    return( fread( buff, 1, 512, fp ) == 512 ? 0 : 1 );
108}
109
110FlatImage::~FlatImage()
111{
112    if( fp )
113        fclose( fp );
114    fp = NULL;
115}
116
117
Note: See TracBrowser for help on using the repository browser.