-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardfile_unix.c
executable file
·176 lines (137 loc) · 4.06 KB
/
hardfile_unix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* UAE - The Un*x Amiga Emulator
*
* Hardfile emulation for *nix systems
*
* Copyright 2003 Richard Drummond
* Based on hardfile_win32.c
*
* Note: this is a rather naive implementation. Sophistication and 64-bit
* support still to come . . .
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "filesys.h"
//#define HDF_DEBUG
#ifdef HDF_DEBUG
#define DEBUG_LOG write_log( "%s: ", __func__); write_log
#else
#define DEBUG_LOG(...) do ; while(0)
#endif
static int hdf_seek (struct hardfiledata *hfd, uae_u64 offset)
{
off_t ret;
DEBUG_LOG ("called with offset=0x%llx\n", offset);
if (hfd->offset != hfd->offset2 || hfd->size != hfd->size2) {
gui_message ("hd: memory corruption detected in seek");
abort ();
}
if (offset >= hfd->size) {
gui_message ("hd: tried to seek out of bounds! (0x%llx >= 0x%llx)\n", offset, hfd->size);
abort ();
}
offset += hfd->offset;
if (offset & (hfd->blocksize - 1)) {
gui_message ("hd: poscheck failed, offset not aligned to blocksize! (0x%llx & 0x%04.4x = 0x%04.4x)\n", offset, hfd->blocksize, offset & (hfd->blocksize - 1));
abort ();
}
if (offset >= 0x80000000 && sizeof (off_t) < sizeof (uae_u64)) {
DEBUG_LOG ("Failed to seek passed 2GB limit (0x%llx)\n", offset);
return -1;
}
ret = lseek ((int)hfd->handle, offset, SEEK_SET);
if (ret == -1) {
DEBUG_LOG ("seek failed\n");
return -1;
}
DEBUG_LOG ("seek okay\n");
return 0;
}
static void poscheck (struct hardfiledata *hfd, int len)
{
off_t pos;
if (hfd->offset != hfd->offset2 || hfd->size != hfd->size2) {
gui_message ("hd: memory corruption detected in poscheck");
abort ();
}
pos = lseek ((int)hfd->handle, 0, SEEK_CUR);
if (pos == -1 ) {
gui_message ("hd: poscheck failed. seek failure, error %d", errno);
abort ();
}
if (len < 0) {
gui_message ("hd: poscheck failed, negative length! (%d)", len);
abort ();
}
if ((uae_u64)pos < hfd->offset) {
gui_message ("hd: poscheck failed, offset out of bounds! (0x%llx < 0x%llx)", pos, hfd->offset);
abort ();
}
if ((uae_u64)pos >= hfd->offset + hfd->size || (uae_u64)pos >= hfd->offset + hfd->size + len) {
gui_message ("hd: poscheck failed, offset out of bounds! (0x%llx >= 0x%llx, LEN=%d)", pos, hfd->offset + hfd->size, len);
abort ();
}
if (pos & (hfd->blocksize - 1)) {
gui_message ("hd: poscheck failed, offset not aligned to blocksize! (0x%llx & 0x%04.4x = 0x%04.4x\n", pos, hfd->blocksize, pos & hfd->blocksize);
abort ();
}
}
int hdf_open (struct hardfiledata *hfd, char *name)
{
int handle;
DEBUG_LOG ("called with name=%s\n",name);
if ((handle = open(name, hfd->readonly ? O_RDONLY : O_RDWR)) != -1) {
int i;
hfd->handle = (void *) handle;
hfd->cache = 0;
i = strlen (name) - 1;
while (i >= 0) {
if ((i > 0 && (name[i - 1] == '/' || name[i - 1] == '\\')) || i == 0) {
strcpy (hfd->vendor_id, "UAE");
strncpy (hfd->product_id, name + i, 15);
strcpy (hfd->product_rev, "0.2");
break;
}
i--;
}
hfd->size = hfd->size2 = lseek (handle, 0, SEEK_END);
hfd->blocksize = 512;
lseek (handle, 0, SEEK_SET);
DEBUG_LOG ("okay\n");
return 1;
}
DEBUG_LOG ("failed\n");
return 0;
}
extern int hdf_dup (struct hardfiledata *hfd, void *src)
{
DEBUG_LOG ("called\n");
return 0;
}
void hdf_close (struct hardfiledata *hfd)
{
DEBUG_LOG ("called\n");
close ((int)hfd->handle);
}
int hdf_read (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len)
{
int n;
DEBUG_LOG ("called with offset=0x%llx len=%d\n", offset, len);
hfd->cache_valid = 0;
hdf_seek (hfd, offset);
poscheck (hfd, len);
n = read ((int)hfd->handle, buffer, len);
DEBUG_LOG ("read %d bytes\n", n);
return n;
}
int hdf_write (struct hardfiledata *hfd, void *buffer, uae_u64 offset, int len)
{
int n;
DEBUG_LOG ("called with offset=0x%llx len=%d\n", offset, len);
hfd->cache_valid = 0;
hdf_seek (hfd, offset);
poscheck (hfd, len);
n = write ((int)hfd->handle, buffer, len);
DEBUG_LOG ("Wrote %d bytes\n", n);
return n;
}