-
Notifications
You must be signed in to change notification settings - Fork 33
/
a314fs.c
1372 lines (1115 loc) · 33.1 KB
/
a314fs.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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018 Niklas Ekström
*/
#include <exec/types.h>
#include <exec/ports.h>
#include <exec/nodes.h>
#include <exec/libraries.h>
#include <devices/timer.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <libraries/filehandler.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <string.h>
#include <stdarg.h>
#include "../a314device/a314.h"
#include "../a314device/proto_a314.h"
#include "messages.h"
#define ID_314_DISK (('3' << 24) | ('1' << 16) | ('4' << 8))
#define REQ_RES_BUF_SIZE 256
#define BUFFER_SIZE 4096
// Not defined if using NDK13
#ifndef ACTION_IS_FILESYSTEM
#define ACTION_IS_FILESYSTEM 1027
#endif
#ifndef ACTION_EXAMINE_FH
#define ACTION_EXAMINE_FH 1034
#endif
#ifndef ACTION_SAME_LOCK
#define ACTION_SAME_LOCK 40
#endif
// Grab a reserved action type which seems to be unused
#define ACTION_UNSUPPORTED 65535
struct ExecBase *SysBase;
struct DosLibrary *DOSBase;
struct MsgPort *mp;
struct DeviceList *my_volume;
char default_volume_name[] = "\006PiDisk";
char device_name[32]; // "\004PI0:"
struct MsgPort *timer_mp;
struct timerequest *tr;
struct MsgPort *a314_mp;
struct A314_IORequest *a314_ior;
struct Library *A314Base;
long socket;
char *request_buffer = NULL;
BOOL request_buffer_in_a314_memory;
// These are allocated in A314 memory.
ULONG request_buffer_address;
ULONG data_buffer_address;
void MyNewList(struct List *l)
{
l->lh_Head = (struct Node *)&(l->lh_Tail);
l->lh_Tail = NULL;
l->lh_TailPred = (struct Node *)&(l->lh_Head);
}
struct MsgPort *MyCreatePort(char *name, long pri)
{
int sigbit = AllocSignal(-1);
if (sigbit == -1)
return NULL;
struct MsgPort *port = (struct MsgPort *)AllocMem(sizeof(struct MsgPort), MEMF_PUBLIC | MEMF_CLEAR);
if (!port)
{
FreeSignal(sigbit);
return NULL;
}
port->mp_Node.ln_Name = name;
port->mp_Node.ln_Pri = pri;
port->mp_Node.ln_Type = NT_MSGPORT;
port->mp_Flags = PA_SIGNAL;
port->mp_SigBit = sigbit;
port->mp_SigTask = FindTask(NULL);
if (name)
AddPort(port);
else
MyNewList(&(port->mp_MsgList));
return port;
}
struct IORequest *MyCreateExtIO(struct MsgPort *ioReplyPort, ULONG size)
{
if (!ioReplyPort)
return NULL;
struct IORequest *ioReq = (struct IORequest *)AllocMem(size, MEMF_PUBLIC | MEMF_CLEAR);
if (!ioReq)
return NULL;
ioReq->io_Message.mn_Node.ln_Type = NT_MESSAGE;
ioReq->io_Message.mn_Length = size;
ioReq->io_Message.mn_ReplyPort = ioReplyPort;
return ioReq;
}
#define DEBUG 0
#if !DEBUG
void dbg_init()
{
}
void dbg(const char* fmt, ...)
{
}
#else
struct FileHandle *dbg_log;
struct MsgPort *dbg_replyport;
struct StandardPacket *dbg_sp;
char dbg_buf[256];
void dbg_init()
{
dbg_log = (struct FileHandle *)BADDR(Open("CON:200/0/440/256/a314fs", MODE_NEWFILE));
dbg_replyport = MyCreatePort(NULL, 0);
dbg_sp = (struct StandardPacket *)AllocMem(sizeof(struct StandardPacket), MEMF_PUBLIC | MEMF_CLEAR);
}
void dbg_out(int l)
{
dbg_sp->sp_Msg.mn_Node.ln_Name = (char *)&(dbg_sp->sp_Pkt);
dbg_sp->sp_Pkt.dp_Link = &(dbg_sp->sp_Msg);
dbg_sp->sp_Pkt.dp_Port = dbg_replyport;
dbg_sp->sp_Pkt.dp_Type = ACTION_WRITE;
dbg_sp->sp_Pkt.dp_Arg1 = (long)dbg_log->fh_Arg1;
dbg_sp->sp_Pkt.dp_Arg2 = (long)dbg_buf;
dbg_sp->sp_Pkt.dp_Arg3 = (long)l - 1;
PutMsg((struct MsgPort *)dbg_log->fh_Type, (struct Message *)dbg_sp);
WaitPort(dbg_replyport);
GetMsg(dbg_replyport);
}
void dbg(const char* fmt, ...)
{
char numbuf[16];
const char *p = fmt;
char *q = dbg_buf;
va_list args;
va_start(args, fmt);
while (*p != 0)
{
char c = *p++;
if (c == '$')
{
c = *p++;
if (c == 'b')
{
UBYTE x = va_arg(args, UBYTE);
*q++ = '$';
for (int i = 0; i < 2; i++)
{
int ni = (x >> ((1 - i) * 4)) & 0xf;
*q++ = (ni >= 10) ? ('a' + (ni - 10)) : ('0' + ni);
}
}
else if (c == 'w')
{
UWORD x = va_arg(args, UWORD);
*q++ = '$';
for (int i = 0; i < 4; i++)
{
int ni = (x >> ((3 - i) * 4)) & 0xf;
*q++ = (ni >= 10) ? ('a' + (ni - 10)) : ('0' + ni);
}
}
else if (c == 'l')
{
ULONG x = va_arg(args, ULONG);
*q++ = '$';
for (int i = 0; i < 8; i++)
{
int ni = (x >> ((7 - i) * 4)) & 0xf;
*q++ = (ni >= 10) ? ('a' + (ni - 10)) : ('0' + ni);
}
}
else if (c == 'S')
{
unsigned char *s = (unsigned char *)va_arg(args, ULONG);
int l = *s++;
for (int i = 0; i < l; i++)
*q++ = *s++;
}
else if (c == 's')
{
unsigned char *s = (unsigned char *)va_arg(args, ULONG);
while (*s)
*q++ = *s++;
*q++ = 0;
}
}
else
{
*q++ = c;
}
}
*q++ = 0;
va_end(args);
dbg_out(q - dbg_buf);
}
#endif
char *DosAllocMem(int len)
{
long *p = (long *)AllocMem(len + 4, MEMF_PUBLIC | MEMF_CLEAR);
*p++ = len;
return((char *)p);
}
void DosFreeMem(char *p)
{
long *lp = (long *)p;
long len = *--lp;
FreeMem((char *)lp, len);
}
void reply_packet(struct DosPacket *dp)
{
struct MsgPort *reply_port = dp->dp_Port;
dp->dp_Port = mp;
PutMsg(reply_port, dp->dp_Link);
}
// Routines for talking to the A314 driver.
LONG a314_cmd_wait(UWORD command, char *buffer, int length)
{
a314_ior->a314_Request.io_Command = command;
a314_ior->a314_Request.io_Error = 0;
a314_ior->a314_Socket = socket;
a314_ior->a314_Buffer = buffer;
a314_ior->a314_Length = length;
return DoIO((struct IORequest *)a314_ior);
}
LONG a314_connect(char *name)
{
struct DateStamp ds;
DateStamp(&ds);
socket = ds.ds_Tick;
return a314_cmd_wait(A314_CONNECT, name, strlen(name));
}
LONG a314_read(char *buf, int length)
{
return a314_cmd_wait(A314_READ, buf, length);
}
LONG a314_write(char *buf, int length)
{
return a314_cmd_wait(A314_WRITE, buf, length);
}
LONG a314_eos()
{
return a314_cmd_wait(A314_EOS, NULL, 0);
}
LONG a314_reset()
{
return a314_cmd_wait(A314_RESET, NULL, 0);
}
void create_and_add_volume()
{
my_volume = (struct DeviceList *)DosAllocMem(sizeof(struct DeviceList));
my_volume->dl_Name = MKBADDR(default_volume_name);
my_volume->dl_Type = DLT_VOLUME;
my_volume->dl_Task = mp;
my_volume->dl_DiskType = ID_314_DISK;
DateStamp(&my_volume->dl_VolumeDate);
struct RootNode *root = (struct RootNode *)DOSBase->dl_Root;
struct DosInfo *info = (struct DosInfo *)BADDR(root->rn_Info);
Forbid();
my_volume->dl_Next = info->di_DevInfo;
info->di_DevInfo = MKBADDR(my_volume);
Permit();
}
void startup_fs_handler(struct DosPacket *dp)
{
unsigned char *name = (unsigned char *)BADDR(dp->dp_Arg1);
struct FileSysStartupMsg *fssm = (struct FileSysStartupMsg *)BADDR(dp->dp_Arg2);
struct DeviceNode *node = (struct DeviceNode *)BADDR(dp->dp_Arg3);
memcpy(device_name, name, *name + 1);
device_name[*name + 1] = 0;
node->dn_Task = mp;
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
reply_packet(dp);
dbg_init();
dbg("ACTION_STARTUP\n");
dbg(" device_name = $S\n", (ULONG)device_name);
dbg(" fssm = $l\n", (ULONG)fssm);
dbg(" node = $l\n", (ULONG)node);
timer_mp = MyCreatePort(NULL, 0);
tr = (struct timerequest *)MyCreateExtIO(timer_mp, sizeof(struct timerequest));
if (OpenDevice(TIMERNAME, UNIT_MICROHZ, (struct IORequest *)tr, 0) != 0)
{
// Om detta händer så är det inget mer vi kan göra.
// F.n. så antar vi att det inte händer.
dbg("Fatal error: unable to open timer.device\n");
return;
}
a314_mp = MyCreatePort(NULL, 0);
a314_ior = (struct A314_IORequest *)MyCreateExtIO(a314_mp, sizeof(struct A314_IORequest));
if (OpenDevice(A314_NAME, 0, (struct IORequest *)a314_ior, 0) != 0)
{
// Om detta failar så är det inget mer vi kan göra.
// F.n. så antar vi att det inte händer.
dbg("Fatal error: unable to open a314.device\n");
return;
}
A314Base = &(a314_ior->a314_Request.io_Device->dd_Library);
if (a314_connect("a314fs") != A314_CONNECT_OK)
{
dbg("Fatal error: unable to connect to a314fs on rasp\n");
// Detta skulle kunna hända.
// Om det händer så måste vi bara vänta och försöka igen om en stund.
// TODO: Här måste jag använda timer.device för att sätta en timer om 2 sekunder, då jag försöker ansluta igen.
return;
}
request_buffer = AllocMem(REQ_RES_BUF_SIZE, MEMF_A314);
request_buffer_in_a314_memory = request_buffer != NULL;
if (request_buffer_in_a314_memory)
request_buffer_address = TranslateAddressA314(request_buffer);
else
{
request_buffer = AllocMem(REQ_RES_BUF_SIZE, 0);
request_buffer_address = AllocMemA314(REQ_RES_BUF_SIZE);
}
data_buffer_address = AllocMemA314(BUFFER_SIZE);
// Så vi kan anta att vi kommer hit, och då har vi en ström till rasp-sidan, där vi kan skicka data.
create_and_add_volume();
dbg("Startup successful\n");
// Om vi senare skulle få problem med anslutningen så hantera det som att disken har skjutits ut, och försök ansluta igen inom två sekunder.
// Men f.n. är det nått vi inte hanterar.
}
void wait_for_response()
{
for (unsigned long i = 0; 1; i++)
{
if (*request_buffer)
{
dbg("--Got response after $l sleeps\n", i);
return;
}
tr->tr_node.io_Command = TR_ADDREQUEST;
tr->tr_node.io_Message.mn_ReplyPort = timer_mp;
tr->tr_time.tv_secs = 0;
tr->tr_time.tv_micro = 1000;
DoIO((struct IORequest *)tr);
}
}
void write_req_and_wait_for_res(int len)
{
if (!request_buffer_in_a314_memory)
WriteMemA314(request_buffer_address, request_buffer, len);
ULONG buf[2] = {request_buffer_address, len};
a314_write((char *)&buf[0], 8);
//wait_for_response();
a314_read((char *)&buf[1], 4);
len = buf[1];
if (!request_buffer_in_a314_memory)
ReadMemA314(request_buffer, request_buffer_address, len);
}
struct FileLock *create_and_add_file_lock(long key, long mode)
{
struct FileLock *lock = (struct FileLock *)DosAllocMem(sizeof(struct FileLock));
lock->fl_Key = key;
lock->fl_Access = mode;
lock->fl_Task = mp;
lock->fl_Volume = MKBADDR(my_volume);
Forbid();
lock->fl_Link = my_volume->dl_Lock;
my_volume->dl_Lock = MKBADDR(lock);
Permit();
return lock;
}
void action_locate_object(struct DosPacket *dp)
{
struct FileLock *parent = (struct FileLock *)BADDR(dp->dp_Arg1);
unsigned char *name = (unsigned char *)BADDR(dp->dp_Arg2);
long mode = dp->dp_Arg3;
dbg("ACTION_LOCATE_OBJECT\n");
dbg(" parent lock = $l\n", parent);
dbg(" name = $S\n", name);
dbg(" mode = $s\n", mode == SHARED_LOCK ? "SHARED_LOCK" : "EXCLUSIVE_LOCK");
struct LocateObjectRequest *req = (struct LocateObjectRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->key = parent == NULL ? 0 : parent->fl_Key;
req->mode = mode;
int nlen = *name;
memcpy(req->name, name, nlen + 1);
write_req_and_wait_for_res(sizeof(struct LocateObjectRequest) + nlen);
struct LocateObjectResponse *res = (struct LocateObjectResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = res->error_code;
}
else
{
struct FileLock *lock = create_and_add_file_lock(res->key, mode);
dbg(" Returning lock $l\n", lock);
dp->dp_Res1 = MKBADDR(lock);
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_free_lock(struct DosPacket *dp)
{
ULONG arg1 = dp->dp_Arg1;
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg1);
dbg("ACTION_FREE_LOCK\n");
dbg(" lock = $l\n", lock);
if (lock != NULL)
{
struct FreeLockRequest *req = (struct FreeLockRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->key = lock->fl_Key;
write_req_and_wait_for_res(sizeof(struct FreeLockRequest));
// Ignore the response. Must succeed.
//struct FreeLockResponse *res = (struct FreeLockResponse *)request_buffer;
Forbid();
if (my_volume->dl_Lock == arg1)
my_volume->dl_Lock = lock->fl_Link;
else
{
struct FileLock *prev = (struct FileLock *)BADDR(my_volume->dl_Lock);
while (prev->fl_Link != arg1)
prev = (struct FileLock *)BADDR(prev->fl_Link);
prev->fl_Link = lock->fl_Link;
}
Permit();
DosFreeMem((char *)lock);
}
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
reply_packet(dp);
}
void action_copy_dir(struct DosPacket *dp)
{
struct FileLock *parent = (struct FileLock *)BADDR(dp->dp_Arg1);
dbg("ACTION_COPY_DIR\n");
dbg(" lock to duplicate = $l\n", parent);
if (parent == NULL)
{
dp->dp_Res1 = 0;
dp->dp_Res2 = 0;
}
else
{
struct CopyDirRequest *req = (struct CopyDirRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->key = parent->fl_Key;
write_req_and_wait_for_res(sizeof(struct CopyDirRequest));
struct CopyDirResponse *res = (struct CopyDirResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = res->error_code;
}
else
{
struct FileLock *lock = create_and_add_file_lock(res->key, parent->fl_Access);
dbg(" Returning lock $l\n", lock);
dp->dp_Res1 = MKBADDR(lock);
dp->dp_Res2 = 0;
}
}
reply_packet(dp);
}
void action_parent(struct DosPacket *dp)
{
struct FileLock *prev_lock = (struct FileLock *)BADDR(dp->dp_Arg1);
dbg("ACTION_PARENT\n");
dbg(" lock = $l\n", prev_lock);
if (prev_lock == NULL)
{
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = ERROR_INVALID_LOCK;
}
else
{
struct ParentRequest *req = (struct ParentRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->key = prev_lock->fl_Key;
write_req_and_wait_for_res(sizeof(struct ParentRequest));
struct ParentResponse *res = (struct ParentResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = res->error_code;
}
else if (res->key == 0)
{
dp->dp_Res1 = 0;
dp->dp_Res2 = 0;
}
else
{
struct FileLock *lock = create_and_add_file_lock(res->key, SHARED_LOCK);
dbg(" Returning lock $l\n", lock);
dp->dp_Res1 = MKBADDR(lock);
dp->dp_Res2 = 0;
}
}
reply_packet(dp);
}
void action_examine_object(struct DosPacket *dp)
{
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg1);
struct FileInfoBlock *fib = (struct FileInfoBlock *)BADDR(dp->dp_Arg2);
dbg("ACTION_EXAMINE_OBJECT\n");
dbg(" lock = $l\n", lock);
dbg(" fib = $l\n", fib);
struct ExamineObjectRequest *req = (struct ExamineObjectRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->key = lock == NULL ? 0 : lock->fl_Key;
write_req_and_wait_for_res(sizeof(struct ExamineObjectRequest));
struct ExamineObjectResponse *res = (struct ExamineObjectResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = res->error_code;
}
else
{
int nlen = (unsigned char)(res->data[0]);
memcpy(fib->fib_FileName, res->data, nlen + 1);
fib->fib_FileName[nlen + 1] = 0;
int clen = (unsigned char)(res->data[nlen + 1]);
memcpy(fib->fib_Comment, &(res->data[nlen + 1]), clen + 1);
fib->fib_Comment[clen + 1] = 0;
fib->fib_DiskKey = res->disk_key;
fib->fib_DirEntryType = res->entry_type;
fib->fib_EntryType = res->entry_type;
fib->fib_Protection = res->protection;
fib->fib_Size = res->size;
fib->fib_NumBlocks = (res->size + 511) >> 9;
fib->fib_Date.ds_Days = res->date[0];
fib->fib_Date.ds_Minute = res->date[1];
fib->fib_Date.ds_Tick = res->date[2];
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_examine_next(struct DosPacket *dp)
{
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg1);
struct FileInfoBlock *fib = (struct FileInfoBlock *)BADDR(dp->dp_Arg2);
dbg("ACTION_EXAMINE_NEXT\n");
dbg(" lock = $l\n", lock);
dbg(" fib = $l\n", fib);
struct ExamineNextRequest *req = (struct ExamineNextRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->key = lock == NULL ? 0 : lock->fl_Key;
req->disk_key = fib->fib_DiskKey;
write_req_and_wait_for_res(sizeof(struct ExamineNextRequest));
struct ExamineNextResponse *res = (struct ExamineNextResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = res->error_code;
}
else
{
int nlen = (unsigned char)(res->data[0]);
memcpy(fib->fib_FileName, res->data, nlen + 1);
fib->fib_FileName[nlen + 1] = 0;
int clen = (unsigned char)(res->data[nlen + 1]);
memcpy(fib->fib_Comment, &(res->data[nlen + 1]), clen + 1);
fib->fib_Comment[clen + 1] = 0;
fib->fib_DiskKey = res->disk_key;
fib->fib_DirEntryType = res->entry_type;
fib->fib_EntryType = res->entry_type;
fib->fib_Protection = res->protection;
fib->fib_Size = res->size;
fib->fib_NumBlocks = (res->size + 511) >> 9;
fib->fib_Date.ds_Days = res->date[0];
fib->fib_Date.ds_Minute = res->date[1];
fib->fib_Date.ds_Tick = res->date[2];
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_examine_fh(struct DosPacket *dp)
{
ULONG arg1 = dp->dp_Arg1;
struct FileInfoBlock *fib = (struct FileInfoBlock *)BADDR(dp->dp_Arg2);
dbg("ACTION_EXAMINE_FH\n");
dbg(" arg1 = $l\n", arg1);
dbg(" fib = $l\n", fib);
struct ExamineFhRequest *req = (struct ExamineFhRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->arg1 = arg1;
write_req_and_wait_for_res(sizeof(struct ExamineFhRequest));
struct ExamineFhResponse *res = (struct ExamineFhResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = res->error_code;
}
else
{
int nlen = (unsigned char)(res->data[0]);
memcpy(fib->fib_FileName, res->data, nlen + 1);
fib->fib_FileName[nlen + 1] = 0;
int clen = (unsigned char)(res->data[nlen + 1]);
memcpy(fib->fib_Comment, &(res->data[nlen + 1]), clen + 1);
fib->fib_Comment[clen + 1] = 0;
fib->fib_DiskKey = res->disk_key;
fib->fib_DirEntryType = res->entry_type;
fib->fib_EntryType = res->entry_type;
fib->fib_Protection = res->protection;
fib->fib_Size = res->size;
fib->fib_NumBlocks = (res->size + 511) >> 9;
fib->fib_Date.ds_Days = res->date[0];
fib->fib_Date.ds_Minute = res->date[1];
fib->fib_Date.ds_Tick = res->date[2];
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_findxxx(struct DosPacket *dp)
{
struct FileHandle *fh = (struct FileHandle *)BADDR(dp->dp_Arg1);
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg2);
unsigned char *name = (unsigned char *)BADDR(dp->dp_Arg3);
if (dp->dp_Type == ACTION_FINDUPDATE)
dbg("ACTION_FINDUPDATE\n");
else if (dp->dp_Type == ACTION_FINDINPUT)
dbg("ACTION_FINDINPUT\n");
else if (dp->dp_Type == ACTION_FINDOUTPUT)
dbg("ACTION_FINDOUTPUT\n");
dbg(" file handle = $l\n", fh);
dbg(" lock = $l\n", lock);
dbg(" name = $S\n", name);
struct FindXxxRequest *req = (struct FindXxxRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->key = lock == NULL ? 0 : lock->fl_Key;
int nlen = *name;
memcpy(req->name, name, nlen + 1);
write_req_and_wait_for_res(sizeof(struct FindXxxRequest) + nlen);
struct FindXxxResponse *res = (struct FindXxxResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = res->error_code;
}
else
{
fh->fh_Arg1 = res->arg1;
fh->fh_Type = mp;
fh->fh_Port = DOSFALSE; // Not an interactive file.
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_read(struct DosPacket *dp)
{
ULONG arg1 = dp->dp_Arg1;
UBYTE *dst = (UBYTE *)dp->dp_Arg2;
int length = dp->dp_Arg3;
dbg("ACTION_READ\n");
dbg(" arg1 = $l\n", arg1);
dbg(" length = $l\n", length);
if (length == 0)
{
dp->dp_Res1 = -1;
dp->dp_Res2 = ERROR_INVALID_LOCK; // This is not the correct error.
reply_packet(dp);
return;
}
int total_read = 0;
while (length)
{
int to_read = length;
if (to_read > BUFFER_SIZE)
to_read = BUFFER_SIZE;
struct ReadRequest *req = (struct ReadRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->arg1 = arg1;
req->address = data_buffer_address;
req->length = to_read;
write_req_and_wait_for_res(sizeof(struct ReadRequest));
struct ReadResponse *res = (struct ReadResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = -1;
dp->dp_Res2 = res->error_code;
reply_packet(dp);
return;
}
if (res->actual)
{
ReadMemA314(dst, data_buffer_address, res->actual);
dst += res->actual;
total_read += res->actual;
length -= res->actual;
}
if (res->actual < to_read)
break;
}
dp->dp_Res1 = total_read;
dp->dp_Res2 = 0;
reply_packet(dp);
}
void action_write(struct DosPacket *dp)
{
ULONG arg1 = dp->dp_Arg1;
UBYTE *src = (UBYTE *)dp->dp_Arg2;
int length = dp->dp_Arg3;
dbg("ACTION_WRITE\n");
dbg(" arg1 = $l\n", arg1);
dbg(" length = $l\n", length);
int total_written = 0;
while (length)
{
int to_write = length;
if (to_write > BUFFER_SIZE)
to_write = BUFFER_SIZE;
WriteMemA314(data_buffer_address, src, to_write);
struct WriteRequest *req = (struct WriteRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->arg1 = arg1;
req->address = data_buffer_address;
req->length = to_write;
write_req_and_wait_for_res(sizeof(struct WriteRequest));
struct WriteResponse *res = (struct WriteResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = total_written;
dp->dp_Res2 = res->error_code;
reply_packet(dp);
return;
}
if (res->actual)
{
src += res->actual;
total_written += res->actual;
length -= res->actual;
}
if (res->actual < to_write)
break;
}
dp->dp_Res1 = total_written;
dp->dp_Res2 = 0;
reply_packet(dp);
}
void action_seek(struct DosPacket *dp)
{
ULONG arg1 = dp->dp_Arg1;
LONG new_pos = dp->dp_Arg2;
LONG mode = dp->dp_Arg3;
dbg("ACTION_SEEK\n");
dbg(" arg1 = $l\n", arg1);
dbg(" new_pos = $l\n", new_pos);
dbg(" mode = $l\n", mode);
struct SeekRequest *req = (struct SeekRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->arg1 = arg1;
req->new_pos = new_pos;
req->mode = mode;
write_req_and_wait_for_res(sizeof(struct SeekRequest));
struct SeekResponse *res = (struct SeekResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = -1;
dp->dp_Res2 = res->error_code;
}
else
{
dp->dp_Res1 = res->old_pos;
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_end(struct DosPacket *dp)
{
ULONG arg1 = dp->dp_Arg1;
dbg("ACTION_END\n");
dbg(" arg1 = $l\n", arg1);
struct EndRequest *req = (struct EndRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->arg1 = arg1;
write_req_and_wait_for_res(sizeof(struct EndRequest));
//struct EndResponse *res = (struct EndResponse *)request_buffer;
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
reply_packet(dp);
}
void action_delete_object(struct DosPacket *dp)
{
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg1);
unsigned char *name = (unsigned char *)BADDR(dp->dp_Arg2);
dbg("ACTION_DELETE_OBJECT\n");
dbg(" lock = $l\n", lock);
dbg(" name = $S\n", name);
struct DeleteObjectRequest *req = (struct DeleteObjectRequest *)request_buffer;
req->has_response = 0;
req->type = dp->dp_Type;
req->key = lock == NULL ? 0 : lock->fl_Key;
int nlen = *name;
memcpy(req->name, name, nlen + 1);
write_req_and_wait_for_res(sizeof(struct DeleteObjectRequest) + nlen);
struct DeleteObjectResponse *res = (struct DeleteObjectResponse *)request_buffer;
if (!res->success)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = res->error_code;
}
else