forked from EtchedPixels/FUZIX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
271 lines (234 loc) · 4.87 KB
/
util.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#define _XOPEN_SOURCE 700 /* For swab, strdup */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fuzix_fs.h"
#include "util.h"
int dev_fd;
int dev_offset;
int swapped;
int swizzling;
extern int swizzling;
int fd_open(char *name, int addflags)
{
if (bdopen(name, addflags) < 0)
exit(1);
/* printf("fd=%d, dev_offset = %d\n", dev_fd, dev_offset); */
return 0;
}
void fd_close(void)
{
bdclose();
}
void panic(char *s)
{
fprintf(stderr, "panic: %s\n", s);
exit(1);
}
uint16_t swizzle16(uint32_t v)
{
int top = v & 0xFFFF0000UL;
if (top && top != 0xFFFF0000) {
fprintf(stderr, "swizzle16 given a 32bit input\n");
exit(1);
}
if (swizzling)
return (v & 0xFF) << 8 | ((v & 0xFF00) >> 8);
else
return v;
}
uint32_t swizzle32(uint32_t v)
{
if (!swizzling)
return v;
return (v & 0xFF) << 24 | (v & 0xFF00) << 8 | (v & 0xFF0000) >> 8 |
(v & 0xFF000000) >> 24;
}
/*********************************************************************
Bdread() and bdwrite() are the block device interface routines. they
are given a buffer pointer, which contains the device, block number,
and data location. They basically validate the device and vector the
call.
Cdread() and cdwrite are the same for character (or "raw") devices,
and are handed a device number. Udata.u_base, count, and offset have
the rest of the data.
**********************************************************************/
static uint8_t *bdswap(uint8_t * p)
{
static uint8_t buf[512];
if (!swapped)
return p;
swab(p, buf, 512);
return buf;
}
static void bdswapkeep(uint8_t * p)
{
/* Irritatingly POSIX says swab(x,x,512) is undefined even though
it's actually pretty safe */
memcpy(p, bdswap(p), 512);
}
#ifdef LIBDSK
#include <libdsk.h>
static DSK_PDRIVER drive;
static DSK_GEOMETRY dg;
static int libdsk;
static int blkshift(unsigned int *blk)
{
switch(dg.dg_secsize) {
case 128:
*blk <<= 2;
return 4;
case 256:
*blk <<= 1;
return 2;
case 512:
return 1;
default:
fprintf(stderr, "ucp block size %ld not supported.\n",
dg.dg_secsize);
exit(1);
}
}
static int bdread_libdsk(unsigned int blk, uint8_t *dbase)
{
uint8_t *dp = dbase;
unsigned int n = blkshift(&blk);
while(n--) {
if (dsk_lread(drive, &dg, dp, blk++) < 0)
return -1;
dp += dg.dg_secsize;
}
if (swapped)
bdswapkeep(dbase);
return 0;
}
static int bdwrite_libdsk(unsigned int blk, uint8_t *dp)
{
unsigned int n = blkshift(&blk);
dp = bdswap(dp);
while(n--) {
if (dsk_lwrite(drive, &dg, dp, blk++) < 0)
return -1;
dp += dg.dg_secsize;
}
return 0;
}
static int bdopen_libdsk(const char *name, int addflags)
{
int err;
err = dsk_open(&drive, name, NULL, NULL);
if (err) {
fprintf(stderr, "Unable to open '%s' with libdsk.\n", name);
return -1;
}
err = dsk_getgeom(drive, &dg);
if (err)
return -1;
libdsk = 1;
return 0;
}
static void bdclose_libdsk(void)
{
dsk_close(&drive);
}
#endif
const uint8_t ide_magic[8] = {
'1','D','E','D','1','5','C','0'
};
static int bdread_raw(unsigned int blk, uint8_t *dp)
{
if (lseek(dev_fd, dev_offset + blk * 512,
SEEK_SET) == -1)
perror("lseek");
if (read(dev_fd, dp, 512) != 512)
panic("read() failed");
if (swapped)
bdswapkeep(dp);
return 0;
}
static int bdwrite_raw(unsigned int blk, uint8_t *dp)
{
lseek(dev_fd, dev_offset + blk * 512, SEEK_SET);
if (write(dev_fd, bdswap(dp), 512) != 512)
panic("write() failed");
return 0;
}
static int bdopen_raw(const char *name, int addflags)
{
uint8_t tmp[512];
char *namecopy = strdup(name);
char *sd = strrchr(namecopy, ':');
if (sd) {
*sd = 0;
sd++;
dev_offset = atoi(sd);
}
dev_fd = open(namecopy, O_RDWR|addflags, 0600);
if (dev_fd == -1) {
perror(namecopy);
free(namecopy);
return -1;
}
free(namecopy);
if (read(dev_fd, tmp, 512) != 512) {
/* Creating a volume so don't require a size */
if (addflags & O_CREAT)
return dev_fd;
fputs("Unable to read 512 bytes of data.\n", stderr);
exit(1);
}
if (memcmp(tmp, ide_magic, 8) == 0) {
puts("Volume is virtual IDE drive.");
dev_offset += 1024;
return 0;
}
if (memcmp(tmp, "RS-IDE", 6) == 0) {
puts("Volume is HDF.");
dev_offset += 534;
}
return dev_fd;
}
static void bdclose_raw(void)
{
if (close(dev_fd))
perror("close");
}
int bdread(unsigned int blk, uint8_t *dp)
{
#ifdef LIBDSK
if (libdsk)
return bdread_libdsk(blk, dp);
#endif
return bdread_raw(blk, dp);
}
int bdwrite(unsigned int blk, uint8_t *dp)
{
#ifdef LIBDSK
if (libdsk)
return bdwrite_libdsk(blk, dp);
#endif
return bdwrite_raw(blk, dp);
}
void bdclose(void)
{
#ifdef LIBDSK
if (libdsk)
bdclose_libdsk();
else
#endif
bdclose_raw();
}
int bdopen(const char *name, int addflags)
{
#ifdef LIBDSK
if (strncmp(name, "libdsk:", 7) == 0) {
return bdopen_libdsk(name + 7, addflags);
} else
#endif
return bdopen_raw(name, addflags);
}