File size: 28,167 Bytes
24bce15 | 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 | /*
* Copyright (c) 2020 Alexey Melnikov
* original a314 version by Niklas Ekström
*/
#include <exec/types.h>
#include <exec/ports.h>
#include <exec/nodes.h>
#include <exec/libraries.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 "messages.h"
#define UMKBADDR(x) (((ULONG)x) >> 2)
#define ID_DISK 0x536F7267
#define BUFFER_SIZE 4096
#define DEBUG 0
// Not defined if using NDK13
#ifndef ACTION_EXAMINE_FH
#define ACTION_EXAMINE_FH 1034
#endif
#ifndef ACTION_SAME_LOCK
#define ACTION_SAME_LOCK 40
#endif
struct ExecBase *SysBase;
struct DosLibrary *DOSBase;
struct MsgPort *mp;
struct DeviceList *my_volume;
char default_volume_name[] = "\006MiSTer";
char device_name[32];
// These are hardcoded shared memory
unsigned short *request_flg = (unsigned short *)0xDD4000; // 4B
char *request_buffer = (char *)0xDD4004; // 512B
char *data_buffer = (char *)0xDD5000; // 4KB
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;
}
#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/MiSTer_fs", 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(long len)
{
long *p = (long *)AllocMem(len + sizeof(long), MEMF_PUBLIC | MEMF_CLEAR);
*p++ = len;
return((char *)p);
}
void DosFreeMem(char *p)
{
long *lp = (long *)p;
long len = *--lp;
FreeMem((char *)lp, len + sizeof(long));
}
void reply_packet(struct DosPacket *dp)
{
struct MsgPort *reply_port = dp->dp_Port;
dp->dp_Port = mp;
PutMsg(reply_port, dp->dp_Link);
}
void create_and_add_volume()
{
my_volume = (struct DeviceList *)DosAllocMem(sizeof(struct DeviceList));
my_volume->dl_Name = (BSTR)UMKBADDR(default_volume_name);
my_volume->dl_Type = DLT_VOLUME;
my_volume->dl_Task = mp;
my_volume->dl_DiskType = ID_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 = UMKBADDR(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);
// 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 write_req_and_wait_for_res(int len)
{
*(int*)request_buffer = len;
unsigned short *p = request_flg;
p[1] = 0xA55A;
unsigned short n = p[0] + 1;
n = ((n + 77) << 8) | (n & 0xFF);
*p++ = n;
while(n != *p){};
}
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 = UMKBADDR(my_volume);
Forbid();
lock->fl_Link = my_volume->dl_Lock;
my_volume->dl_Lock = UMKBADDR(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, mode = $s\n", parent, mode == SHARED_LOCK ? "SHARED_LOCK" : "EXCLUSIVE_LOCK");
dbg(" name = $S\n", name);
struct LocateObjectRequest *req = (struct LocateObjectRequest *)request_buffer;
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($l), error code $l\n", res->success, 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) $l\n", res->key, lock);
dp->dp_Res1 = UMKBADDR(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->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->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 = UMKBADDR(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->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 = UMKBADDR(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:");
struct ExamineObjectRequest *req = (struct ExamineObjectRequest *)request_buffer;
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->file_name[0]);
memcpy(fib->fib_FileName, res->file_name, nlen + 1);
fib->fib_FileName[nlen + 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];
fib->fib_Comment[0] = 0;
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
dbg(" $S\n", res->file_name);
}
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: ");
struct ExamineNextRequest *req = (struct ExamineNextRequest *)request_buffer;
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(" END\n");
if(res->error_code != ERROR_NO_MORE_ENTRIES)
{
dbg(" Failed, error code $l\n", (LONG)res->error_code);
}
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = res->error_code;
}
else
{
int fnl = (unsigned char)(res->file_name[0]);
memcpy(fib->fib_FileName, res->file_name, fnl + 1);
fib->fib_FileName[fnl + 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];
fib->fib_Comment[0] = 0;
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
dbg(" $S\n", res->file_name);
}
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->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->file_name[0]);
memcpy(fib->fib_FileName, res->file_name, nlen + 1);
fib->fib_FileName[nlen + 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];
fib->fib_Comment[0] = 0;
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->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->type = dp->dp_Type;
req->arg1 = arg1;
req->address = (int)data_buffer;
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;
return;
}
if (res->actual)
{
memcpy(dst, data_buffer, 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;
memcpy(data_buffer, src, to_write);
struct WriteRequest *req = (struct WriteRequest *)request_buffer;
req->type = dp->dp_Type;
req->arg1 = arg1;
req->address = (int)data_buffer;
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;
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->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->type = dp->dp_Type;
req->arg1 = arg1;
write_req_and_wait_for_res(sizeof(struct EndRequest));
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: $S\n", name);
struct DeleteObjectRequest *req = (struct DeleteObjectRequest *)request_buffer;
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
{
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_rename_object(struct DosPacket *dp)
{
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg1);
unsigned char *name = (unsigned char *)BADDR(dp->dp_Arg2);
struct FileLock *target_dir = (struct FileLock *)BADDR(dp->dp_Arg3);
unsigned char *new_name = (unsigned char *)BADDR(dp->dp_Arg4);
dbg("ACTION_RENAME_OBJECT\n");
dbg(" lock = $l\n", lock);
dbg(" name = $S\n", name);
dbg(" target directory = $l\n", lock);
dbg(" new name = $S\n", new_name);
struct RenameObjectRequest *req = (struct RenameObjectRequest *)request_buffer;
req->type = dp->dp_Type;
req->key = lock == NULL ? 0 : lock->fl_Key;
req->target_dir = target_dir == NULL ? 0 : target_dir->fl_Key;
int nlen = *name;
int nnlen = *new_name;
req->name_len = nlen;
req->new_name_len = nnlen;
unsigned char *p = &(req->new_name_len) + 1;
memcpy(p, name + 1, nlen);
p += nlen;
memcpy(p, new_name + 1, nnlen);
write_req_and_wait_for_res(sizeof(struct RenameObjectRequest) + nlen + nnlen);
struct RenameObjectResponse *res = (struct RenameObjectResponse *)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
{
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_create_dir(struct DosPacket *dp)
{
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg1);
unsigned char *name = (unsigned char *)BADDR(dp->dp_Arg2);
dbg("ACTION_CREATE_DIR\n");
dbg(" lock = $l\n", lock);
dbg(" name = $S\n", name);
struct CreateDirRequest *req = (struct CreateDirRequest *)request_buffer;
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 CreateDirRequest) + nlen);
struct CreateDirResponse *res = (struct CreateDirResponse *)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, SHARED_LOCK);
dbg(" Returning lock $l\n", lock);
dp->dp_Res1 = UMKBADDR(lock);
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_set_protect(struct DosPacket *dp)
{
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg2);
unsigned char *name = (unsigned char *)BADDR(dp->dp_Arg3);
long mask = dp->dp_Arg4;
dbg("ACTION_SET_PROTECT\n");
dbg(" lock = $l\n", lock);
dbg(" name = $S\n", name);
dbg(" mask = $l\n", mask);
struct SetProtectRequest *req = (struct SetProtectRequest *)request_buffer;
req->type = dp->dp_Type;
req->key = lock == NULL ? 0 : lock->fl_Key;
req->mask = mask;
int nlen = *name;
memcpy(req->name, name, nlen + 1);
write_req_and_wait_for_res(sizeof(struct SetProtectRequest) + nlen);
struct SetProtectResponse *res = (struct SetProtectResponse *)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
{
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_set_comment(struct DosPacket *dp)
{
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg2);
unsigned char *name = (unsigned char *)BADDR(dp->dp_Arg3);
unsigned char *comment = (unsigned char *)BADDR(dp->dp_Arg4);
dbg("ACTION_SET_COMMENT\n");
dbg(" lock = $l\n", lock);
dbg(" name = $S\n", name);
dbg(" comment = $S\n", comment);
struct SetCommentRequest *req = (struct SetCommentRequest *)request_buffer;
req->type = dp->dp_Type;
req->key = lock == NULL ? 0 : lock->fl_Key;
int nlen = *name;
int clen = *comment;
req->name_len = nlen;
req->comment_len = clen;
unsigned char *p = &(req->comment_len) + 1;
memcpy(p, name + 1, nlen);
p += nlen;
memcpy(p, comment + 1, clen);
write_req_and_wait_for_res(sizeof(struct SetCommentRequest) + nlen + clen);
struct SetCommentResponse *res = (struct SetCommentResponse *)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
{
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
}
reply_packet(dp);
}
void action_same_lock(struct DosPacket *dp)
{
struct FileLock *lock1 = (struct FileLock *)BADDR(dp->dp_Arg1);
struct FileLock *lock2 = (struct FileLock *)BADDR(dp->dp_Arg2);
dbg("ACTION_SAME_LOCK\n");
dbg(" locks to compare = $l $l\n", lock1, lock2);
struct SameLockRequest *req = (struct SameLockRequest *)request_buffer;
req->type = dp->dp_Type;
req->key1 = lock1->fl_Key;
req->key2 = lock2->fl_Key;
write_req_and_wait_for_res(sizeof(struct SameLockRequest));
struct SameLockResponse *res = (struct SameLockResponse *)request_buffer;
dp->dp_Res1 = res->success ? DOSTRUE : DOSFALSE;
dp->dp_Res2 = res->error_code;
reply_packet(dp);
}
void fill_info_data(struct DosPacket *dp, struct InfoData *id)
{
struct DiskInfoRequest *req = (struct DiskInfoRequest *)request_buffer;
req->type = dp->dp_Type;
req->key = dp->dp_Arg1;
req->dummy1 = 0;
req->dummy2 = 0;
write_req_and_wait_for_res(sizeof(struct DiskInfoRequest));
struct DiskInfoResponse *res = (struct DiskInfoResponse *)request_buffer;
memset(id, 0, sizeof(struct InfoData));
id->id_DiskState = ID_VALIDATED;
id->id_NumBlocks = res->total;
id->id_NumBlocksUsed = res->used;
id->id_BytesPerBlock = 512;
id->id_DiskType = my_volume->dl_DiskType;
id->id_VolumeNode = UMKBADDR(my_volume);
id->id_InUse = DOSTRUE;
}
void action_info(struct DosPacket *dp)
{
struct FileLock *lock = (struct FileLock *)BADDR(dp->dp_Arg1);
struct InfoData *id = (struct InfoData *)BADDR(dp->dp_Arg2);
//dbg("ACTION_INFO\n");
//dbg(" lock = $l\n", lock);
fill_info_data(dp, id);
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
reply_packet(dp);
}
void action_disk_info(struct DosPacket *dp)
{
struct InfoData *id = (struct InfoData *)BADDR(dp->dp_Arg1);
dbg("ACTION_DISK_INFO\n");
fill_info_data(dp, id);
dp->dp_Res1 = DOSTRUE;
dp->dp_Res2 = 0;
reply_packet(dp);
}
void start(__reg("a0") struct DosPacket *startup_packet)
{
SysBase = *(struct ExecBase **)4;
DOSBase = (struct DosLibrary *)OpenLibrary(DOSNAME, 0);
mp = MyCreatePort(NULL, 0);
startup_fs_handler(startup_packet);
while (1)
{
WaitPort(mp);
struct StandardPacket *sp = (struct StandardPacket *)GetMsg(mp);
struct DosPacket *dp = (struct DosPacket *)(sp->sp_Msg.mn_Node.ln_Name);
switch (dp->dp_Type)
{
case ACTION_LOCATE_OBJECT: action_locate_object(dp); break;
case ACTION_FREE_LOCK: action_free_lock(dp); break;
case ACTION_COPY_DIR: action_copy_dir(dp); break;
case ACTION_PARENT: action_parent(dp); break;
case ACTION_EXAMINE_OBJECT: action_examine_object(dp); break;
case ACTION_EXAMINE_NEXT: action_examine_next(dp); break;
case ACTION_EXAMINE_FH: action_examine_fh(dp); break;
case ACTION_FINDUPDATE: action_findxxx(dp); break;
case ACTION_FINDINPUT: action_findxxx(dp); break;
case ACTION_FINDOUTPUT: action_findxxx(dp); break;
case ACTION_READ: action_read(dp); break;
case ACTION_WRITE: action_write(dp); break;
case ACTION_SEEK: action_seek(dp); break;
case ACTION_END: action_end(dp); break;
//case ACTION_TRUNCATE: action_truncate(dp); break;
case ACTION_DELETE_OBJECT: action_delete_object(dp); break;
case ACTION_RENAME_OBJECT: action_rename_object(dp); break;
case ACTION_CREATE_DIR: action_create_dir(dp); break;
case ACTION_SET_PROTECT: action_set_protect(dp); break;
case ACTION_SET_COMMENT: action_set_comment(dp); break;
//case ACTION_SET_DATE: action_set_date(dp); break;
case ACTION_SAME_LOCK: action_same_lock(dp); break;
case ACTION_DISK_INFO: action_disk_info(dp); break;
case ACTION_INFO: action_info(dp); break;
/*
case ACTION_CURRENT_VOLUME: action_current_volume(dp); break;
case ACTION_RENAME_DISK: action_rename_disk(dp); break;
case ACTION_DIE: //action_die(dp); break;
case ACTION_MORE_CACHE: //action_more_cache(dp); break;
case ACTION_FLUSH: //action_flush(dp); break;
case ACTION_INHIBIT: //action_inhibit(dp); break;
case ACTION_WRITE_PROTECT: //action_write_protect(dp); break;
*/
default:
dbg("Unknown action: $l\n", (ULONG)dp->dp_Type);
dp->dp_Res1 = DOSFALSE;
dp->dp_Res2 = ERROR_ACTION_NOT_KNOWN;
reply_packet(dp);
break;
}
}
dbg("Shutting down\n");
// More cleaning up is necessary if the handler is to exit.
CloseLibrary((struct Library *)DOSBase);
}
|