1 | //======================================================================
|
---|
2 | //
|
---|
3 | // Project: XTIDE Universal BIOS, Serial Port Server
|
---|
4 | //
|
---|
5 | // File: Win32File.h - Microsoft Windows file system access.
|
---|
6 | //
|
---|
7 | // Routines for accessing the file system under Win32. It's important
|
---|
8 | // to use these direct Win32 calls for large files, since FILE * routines,
|
---|
9 | // in particular ftell() and fseek(), are limites to signed 32-bits (2 GB).
|
---|
10 | // These are also likely faster since they are more direct.
|
---|
11 | //
|
---|
12 |
|
---|
13 | #include <windows.h>
|
---|
14 | #include <stdio.h>
|
---|
15 | #include "../library/library.h"
|
---|
16 |
|
---|
17 | class FileAccess
|
---|
18 | {
|
---|
19 | public:
|
---|
20 | void Create( char *p_name )
|
---|
21 | {
|
---|
22 | fp = CreateFileA( p_name, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0 );
|
---|
23 | if( !fp )
|
---|
24 | log( -1, "'%s', could not create file", p_name );
|
---|
25 | name = p_name;
|
---|
26 | }
|
---|
27 |
|
---|
28 | void Open( char *p_name )
|
---|
29 | {
|
---|
30 | fp = CreateFileA( p_name, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
|
---|
31 | if( !fp )
|
---|
32 | log( -1, "'%s', could not open file", p_name );
|
---|
33 | name = p_name;
|
---|
34 | }
|
---|
35 |
|
---|
36 | void Close()
|
---|
37 | {
|
---|
38 | if( fp )
|
---|
39 | {
|
---|
40 | if( !CloseHandle( fp ) )
|
---|
41 | log( 0, "'%s', could not close file handle", name ? name : "unknown" );
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | unsigned long SizeSectors(void)
|
---|
46 | {
|
---|
47 | LARGE_INTEGER li;
|
---|
48 | unsigned long i;
|
---|
49 |
|
---|
50 | if( !GetFileSizeEx( fp, &li ) )
|
---|
51 | log( -1, "'%s', could not retrieve file size (error %ul)", name, GetLastError() );
|
---|
52 |
|
---|
53 | if( li.LowPart & 0x1ff )
|
---|
54 | log( -1, "'%s', file size is not a multiple of 512 byte sectors", name );
|
---|
55 |
|
---|
56 | if( li.HighPart > 0x1f )
|
---|
57 | log( -1, "'%s', file size greater than LBA28 limit of 137,438,952,960 bytes", name );
|
---|
58 |
|
---|
59 | i = ((li.HighPart << 23 ) & 0xff800000) | ((li.LowPart >> 9) & 0x7fffff);
|
---|
60 |
|
---|
61 | return( (unsigned long) i );
|
---|
62 | }
|
---|
63 |
|
---|
64 | void SeekSectors( unsigned long lba )
|
---|
65 | {
|
---|
66 | LARGE_INTEGER dist;
|
---|
67 |
|
---|
68 | dist.HighPart = lba >> 23;
|
---|
69 | dist.LowPart = lba << 9;
|
---|
70 |
|
---|
71 | if( !SetFilePointerEx( fp, dist, NULL, FILE_BEGIN ) )
|
---|
72 | log( -1, "'%s', Failed to seek to lba=%lu", name, lba );
|
---|
73 | }
|
---|
74 |
|
---|
75 | void Read( void *buff, unsigned long len )
|
---|
76 | {
|
---|
77 | unsigned long out_len;
|
---|
78 |
|
---|
79 | if( !ReadFile( fp, buff, len, &out_len, NULL ) || len != out_len )
|
---|
80 | log( -1, "'%s', ReadFile failed" );
|
---|
81 | }
|
---|
82 |
|
---|
83 | void Write( void *buff, unsigned long len )
|
---|
84 | {
|
---|
85 | unsigned long out_len;
|
---|
86 |
|
---|
87 | if( !WriteFile( fp, buff, len, &out_len, NULL ) || len != out_len )
|
---|
88 | log( -1, "'%s', WriteFile failed" );
|
---|
89 | }
|
---|
90 |
|
---|
91 | FileAccess()
|
---|
92 | {
|
---|
93 | fp = NULL;
|
---|
94 | name = NULL;
|
---|
95 | }
|
---|
96 |
|
---|
97 | // LBA 28 limit - 28-bits (could be 1 more, but not worth pushing it)
|
---|
98 | const static unsigned long MaxSectors = 0xfffffff;
|
---|
99 | #define USAGE_MAXSECTORS "137438 MB (LBA28 limit)"
|
---|
100 |
|
---|
101 | private:
|
---|
102 | HANDLE fp;
|
---|
103 | char *name;
|
---|
104 | };
|
---|
105 |
|
---|