-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.c
More file actions
591 lines (537 loc) · 12.7 KB
/
Copy pathIO.c
File metadata and controls
591 lines (537 loc) · 12.7 KB
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include "Z-OS.h"
#include "Devices\File.h"
#include "Devices\Partition.h"
#include "Devices\FileSystems.h"
#include "Devices\VolumeManager.h"
List Devices = {0};
List FileSystems = {0};
List Partitions = {0};
List CurrentFiles = {0};
Int16 TypeDevice;
Int16 TypeFile;
Int16 TypeDirectory;
Int16 TypePartition;
// Device Objects
static void DeviceCreate(InternalObject* obj)
{
obj->Flags |= ObjectFlagPermanent;
}
static void DeviceDestroy(InternalObject* obj)
{
// This should never be called until we have more of a PnP system
}
static Int16 DeviceMount(UInt16 handle, UInt16 partition, char* hint, char* path)
{
InternalObject* obj;
Int16 ret;
//char hintHolder[8] = {0};
if ((ret = InternalObjectFromHandle(handle,&obj))) return ret;
if (obj && obj->Data)
{
PartInternal* part;
UInt16 partHandle;
InternalObject* partObj;
Int16 i;
PartitionTable* partTable;
DeviceInternal* dev = (DeviceInternal*)(obj->Data);
EnterCriticalSection();
// Check if this partition has been mounted before now
if (dev->IsMounted)
{
ExitCriticalSection();
return ErrorInUse;
}
// Create a partition object for this partition
if ((ret = CreateObject(TypePartition,&partHandle,path)))
{
ExitCriticalSection();
return ret;
}
else
{
puts("Created partition object successfully.\r\n");
}
part = zmalloc(sizeof(PartInternal));
if (!part)
{
ReleaseObject(partHandle);
ExitCriticalSection();
puts("Error allocating part object\r\n");
return ErrorOutOfMemory;
}
part->IsLive = True;
part->Device = dev;
if ((ret = InternalObjectFromHandle(partHandle,&partObj)))
{
ReleaseObject(partHandle);
zfree(part);
ExitCriticalSection();
puts("Error retrieving internal part object data\r\n");
return ret;
}
AddListItem(&Partitions,part);
partObj->Data = part;
partObj->Flags |= ObjectFlagPermanent;
ReleaseObject(partHandle);
partTable = zmalloc(sizeof(PartitionTable));
if (!partTable)
{
ExitCriticalSection();
puts("Error allocating partition table\r\n");
return ErrorOutOfMemory;
}
// Ask the volume manager to read the partition table
if (!LoadMBR(dev,partTable))
{
PartitionTableEntry* entry = EntryFromPartitionIndex(partTable,partition);
puts("Device is partitioned!\r\n");
// Create a partition object based on the partition table
if (GetNumPartitions(partTable) > partition && entry)
{
part->FirstByte = (UInt64)(entry->FirstSector) * (UInt64)(dev->Info.SectorSize);
part->ByteLength = (UInt64)(entry->NumSectors) * (UInt64)(dev->Info.SectorSize);
zfree(partTable);
}
else
{
zfree(partTable);
return ErrorInvalidArg;
}
}
else
{
zfree(partTable);
// Since the device is just one big partition, create a partition object to represent it all
part->FirstByte = 0;
ret = dev->Funcs.GetAvailableBytes(dev->DeviceId,&(part->ByteLength));
if (ret)
{
ExitCriticalSection();
return ret;
}
}
if (hint && !strcmp("RAW",hint))
{
// If the user only wanted to mount the drive in raw mode, we are finished.
ExitCriticalSection();
return ErrorSuccess;
}
for (i = 0; i < FileSystems.Length; i++)
{
FileSystemInternal* fs = GetListItem(&FileSystems,i);
// If a hint was set, look for that FS first
if (fs && ((hint && !strcmp(fs->Info.Name,hint)) || !hint))
{
puts("Trying to detect...\r\n");
// Try to detect the FS
if (fs->Funcs.Detect(part))
{
printf("Detect successful: %s\r\n",fs->Info.Name);
// Try to mount the FS
ret = fs->Funcs.MountDevice(part);
if (ret)
{
ExitCriticalSection();
return ret;
}
// Yay, we mounted it
dev->IsMounted = True;
part->FileSystem = fs;
part->IsLive = True;
ExitCriticalSection();
return ErrorSuccess;
}
else if (hint)
{
// If we were just checking the hinted FS, start over
// from the beginning of the list
hint = 0;
i = 0;
}
}
}
ExitCriticalSection();
return ErrorUnknownFS;
}
else
{
return ErrorInvalidHandle;
}
}
static Int16 DeviceUnmount(UInt16 handle,UInt16 partition)
{
InternalObject* obj;
Int16 ret;
if ((ret = InternalObjectFromHandle(handle,&obj))) return ret;
if (obj)
{
UInt16 i;
PartInternal* part = NULL;
EnterCriticalSection();
DeviceInternal* dev = obj->Data;
for (i = 0; i < Partitions.Length; i++)
{
part = GetListItem(&Partitions,i);
if (part->Device == dev && part->Index == partition) break;
else part = NULL;
}
if (!part) return ErrorUnmounted;
if (dev->IsMounted)
{
// TODO: Call with the proper partition object
ret = part->FileSystem->Funcs.UnmountDevice(part,False);
if (ret == ErrorSuccess)
{
part->IsLive = False;
part->FileSystem = Null;
}
// If it failed, just return to the user?
ExitCriticalSection();
return ret;
}
else
{
return ErrorUnmounted;
}
}
else
{
return ErrorInvalidHandle;
}
}
static Bool DeviceIsMounted(UInt16 handle)
{
InternalObject* obj;
Int16 ret;
if ((ret = InternalObjectFromHandle(handle,&obj))) return ret;
if (obj)
{
EnterCriticalSection();
if (((DeviceInternal*)(obj->Data))->IsMounted)
{
ret = True;
}
else
{
ret = False;
}
ExitCriticalSection();
return ret;
}
else
{
return False;
}
}
static Int16 DeviceSetPowerState(UInt16 handle, PowerStateEnum state)
{
return ErrorSuccess;
}
static Int16 DeviceSendCommand(UInt16 handle, Int16 cmd, Int8* buffer, UInt16 bufferLen)
{
return ErrorSuccess;
}
static Int16 DeviceRead(UInt16 handle, UInt8* buffer, UInt16 bufLen)
{
InternalObject* obj;
Int16 ret;
puts("DeviceRead called...\r\n");
if ((ret = InternalObjectFromHandle(handle,&obj))) return ret;
if (obj)
{
EnterCriticalSection();
ret = InternalReadDevice(((DeviceInternal*)(obj->Data)),0,buffer,bufLen,True);
ExitCriticalSection();
return ret;
}
else
{
return ErrorInvalidHandle;
}
}
static Int16 DeviceGetAvailableBytes(UInt16 handle, UInt64* bytes)
{
InternalObject* obj;
Int16 ret;
if ((ret = InternalObjectFromHandle(handle,&obj))) return ret;
if (obj)
{
EnterCriticalSection();
if (((DeviceInternal*)(obj->Data))->Info.CanSeek)
{
ret = ((DeviceInternal*)(obj->Data))->Funcs.GetAvailableBytes(((DeviceInternal*)(obj->Data))->DeviceId, bytes);
*bytes -= ((DeviceInternal*)(obj->Data))->Position;
ExitCriticalSection();
return ErrorSuccess;
}
ExitCriticalSection();
return ErrorUnseekable;
}
else
{
return ErrorInvalidHandle;
}
}
static Int16 DeviceWrite(UInt16 handle, UInt8* buffer, UInt16 bufLen)
{
InternalObject* obj;
Int16 ret;
if ((ret = InternalObjectFromHandle(handle,&obj))) return ret;
if (obj)
{
EnterCriticalSection();
ret = InternalWriteDevice(((DeviceInternal*)(obj->Data)),0,buffer,bufLen,True);
ExitCriticalSection();
return ret;
}
else
{
return ErrorInvalidHandle;
}
}
static Int16 DeviceSeek(UInt16 handle, Int64 position, SeekRelationEnum relation)
{
InternalObject* obj;
Int16 ret;
if ((ret = InternalObjectFromHandle(handle,&obj))) return ret;
if (obj)
{
EnterCriticalSection();
DeviceInternal* dev = (DeviceInternal*)(obj->Data);
if (!(dev->Info.CanSeek))
{
ExitCriticalSection();
return ErrorUnseekable;
}
switch (relation)
{
case Beginning:
dev->Position = position;
break;
case CurrentPos:
dev->Position += position;
break;
case End:
{
UInt64 bytes;
if ((ret = dev->Funcs.GetAvailableBytes(dev->DeviceId,&bytes)))
{
ExitCriticalSection();
return ret;
}
if (bytes <= position) dev->Position += bytes - position;
else dev->Position -= position - bytes;
break;
}
}
ExitCriticalSection();
return ErrorSuccess;
}
else
{
return ErrorInvalidHandle;
}
}
static Int16 DeviceFlush(UInt16 handle)
{
InternalObject* obj;
Int16 ret;
if ((ret = InternalObjectFromHandle(handle,&obj))) return ret;
if (obj)
{
EnterCriticalSection();
ret = ((DeviceInternal*)(obj->Data))->Funcs.Flush(((DeviceInternal*)(obj->Data))->DeviceId);
ExitCriticalSection();
return ret;
}
else
{
return ErrorInvalidHandle;
}
}
static Int16 DeviceGetInterface(UInt16 code, void** interface)
{
switch (code)
{
case CodeIGeneric:
{
static const IGeneric inter =
{
DeviceCreate,
DeviceDestroy
};
*interface = (void*)&inter;
break;
}
/*case CodeINamespace:
{
static const INamespace inter =
{
DeviceCreateObject
};
*interface = (void*)&inter;
break;
}*/
case CodeIDevice:
{
static const IDevice inter =
{
DeviceMount,
DeviceUnmount,
DeviceIsMounted,
DeviceSetPowerState,
DeviceSendCommand
};
*interface = (void*)&inter;
break;
}
case CodeISimpleIO:
{
static const ISimpleIO inter =
{
DeviceRead,
DeviceGetAvailableBytes,
DeviceWrite,
DeviceSeek,
DeviceFlush
};
*interface = (void*)&inter;
break;
}
default:
return ErrorInvalidInterface;
}
return ErrorSuccess;
}
Int16 NextDeviceId = 1;
Int16 RegisterDevice(char* name, DeviceInfo info, DeviceFuncs funcs, Int16* id)
{
DeviceInternal* device = zmalloc(sizeof(DeviceInternal));
InternalObject* obj;
UInt16 handle;
Int16 ret;
if (!device) return ErrorOutOfMemory;
device->Info = info;
device->Funcs = funcs;
// Add the device to the internal Devices list
EnterCriticalSection();
AddListItem(&Devices,device);
ExitCriticalSection();
// Create the device object and set it to point to
// the DeviceInternal structure
ret = CreateObject(TypeDevice,&handle,name);
InternalObjectFromHandle(handle,&obj);
obj->Data = device;
ReleaseObject(handle);
// Assign a unique device ID
*id = device->DeviceId = NextDeviceId++;
return ErrorSuccess;
}
Int16 GetDeviceFromID(Int16 id, DeviceInternal** device)
{
Int16 i;
EnterCriticalSection();
for (i = 0; i < Devices.Length; i++)
{
DeviceInternal* item = GetListItem(&Devices,i);
if (item && item->DeviceId == id)
{
*device = item;
ExitCriticalSection();
return ErrorSuccess;
}
}
ExitCriticalSection();
return ErrorNotFound;
}
Int16 InternalReadDevice(DeviceInternal* device, UInt64 pos, UInt8* buffer, UInt16 bufLen, Bool advance)
{
Int16 ret;
if (!device) return ErrorNullArg;
if (device->Info.CanRead)
{
if (device->Funcs.Read)
{
ret = device->Funcs.Read(device->DeviceId, advance ? device->Position : pos, buffer, bufLen);
if (!ret && advance) device->Position += bufLen;
}
else
{
ret = ErrorUnknown;
}
}
else if (device->Info.CanWrite)
{
ret = ErrorWriteOnly;
}
else if (device->Info.CanMount)
{
ret = ErrorMountOnly;
}
else
{
puts("InternalReadDevice returning ErrorUnreadable\r\n");
ret = ErrorUnreadable;
}
return ret;
}
Int16 InternalWriteDevice(DeviceInternal* device, UInt64 pos, UInt8* buffer, UInt16 bufLen, Bool advance)
{
Int16 ret;
if (!device) return ErrorNullArg;
if (device->Info.CanWrite)
{
if (device->Funcs.Read)
{
ret = device->Funcs.Write(device->DeviceId, advance ? (device->Position) : pos, buffer, bufLen);
if (!ret && advance) device->Position += bufLen;
}
else
{
ret = ErrorUnknown;
}
}
else if (device->Info.CanRead)
{
ret = ErrorReadOnly;
}
else if (device->Info.CanMount)
{
ret = ErrorMountOnly;
}
else
{
ret = ErrorUnreadable;
}
return ret;
}
Int16 InternalWritePart(PartInternal* part, UInt64 pos, UInt8* buffer, UInt16 bufLen)
{
if (!part || !(part->Device)) return ErrorNullArg;
return InternalWriteDevice(part->Device,pos + part->FirstByte,buffer,bufLen,False);
}
Int16 InternalReadPart(PartInternal* part, UInt64 pos, UInt8* buffer, UInt16 bufLen)
{
if (!part || !(part->Device)) return ErrorNullArg;
printf("Reading %u bytes at %llu\r\n",bufLen,pos);
return InternalReadDevice(part->Device,pos + part->FirstByte,buffer,bufLen,False);
}
Int16 RegisterFileSystem(FileSystemInfo info, FileSystemFuncs funcs)
{
FileSystemInternal* fs = zmalloc(sizeof(FileSystemInternal));
if (!fs) return ErrorOutOfMemory;
fs->Info = info;
fs->Funcs = funcs;
AddListItem(&FileSystems,fs);
return ErrorSuccess;
}
void InitializeIO(void)
{
TypeRegistration device = {0};
// Register the device type
device.Type = TypeDevice = GetUniqueTypeCode();
device.GetInterface = DeviceGetInterface;
RegisterTypeManager(device);
InitFiles();
InitPartitions();
}