PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/filesys.cpp

https://github.com/tonioni/WinUAE
C++ | 10368 lines | 10225 code | 86 blank | 57 comment | 152 complexity | ec14108ce989ddb29ad600a1052dc3f7 MD5 | raw file
  1. /*
  2. * UAE - The Un*x Amiga Emulator
  3. *
  4. * Unix file system handler for AmigaDOS
  5. *
  6. * Copyright 1996 Ed Hanway
  7. * Copyright 1996, 1997 Bernd Schmidt
  8. *
  9. * Version 0.4: 970308
  10. *
  11. * Based on example code (c) 1988 The Software Distillery
  12. * and published in Transactor for the Amiga, Volume 2, Issues 2-5.
  13. * (May - August 1989)
  14. *
  15. * Known limitations:
  16. * Does not support several (useless) 2.0+ packet types.
  17. * May not return the correct error code in some cases.
  18. * Does not check for sane values passed by AmigaDOS. May crash the emulation
  19. * if passed garbage values.
  20. * Could do tighter checks on malloc return values.
  21. * Will probably fail spectacularly in some cases if the filesystem is
  22. * modified at the same time by another process while UAE is running.
  23. */
  24. #include "sysconfig.h"
  25. #include "sysdeps.h"
  26. #include "threaddep/thread.h"
  27. #include "options.h"
  28. #include "traps.h"
  29. #include "uae.h"
  30. #include "memory.h"
  31. #include "custom.h"
  32. #include "events.h"
  33. #include "newcpu.h"
  34. #include "filesys.h"
  35. #include "autoconf.h"
  36. #include "fsusage.h"
  37. #include "native2amiga.h"
  38. #include "scsidev.h"
  39. #include "uaeserial.h"
  40. #include "fsdb.h"
  41. #include "zfile.h"
  42. #include "zarchive.h"
  43. #include "gui.h"
  44. #include "gayle.h"
  45. #include "idecontrollers.h"
  46. #include "savestate.h"
  47. #include "a2091.h"
  48. #include "ncr_scsi.h"
  49. #include "cdtv.h"
  50. #include "sana2.h"
  51. #include "bsdsocket.h"
  52. #include "uaeresource.h"
  53. #include "inputdevice.h"
  54. #include "clipboard.h"
  55. #include "consolehook.h"
  56. #include "blkdev.h"
  57. #include "isofs_api.h"
  58. #include "scsi.h"
  59. #include "uaenative.h"
  60. #include "tabletlibrary.h"
  61. #include "cia.h"
  62. #include "picasso96.h"
  63. #include "cpuboard.h"
  64. #include "rommgr.h"
  65. #include "debug.h"
  66. #include "debugmem.h"
  67. #ifdef RETROPLATFORM
  68. #include "rp.h"
  69. #endif
  70. #define TRACING_ENABLED 1
  71. int log_filesys = 0;
  72. #define TRAPMD 1
  73. #if TRACING_ENABLED
  74. #if 0
  75. #define TRACE(x) if (log_filesys > 0 && (unit->volflags & MYVOLUMEINFO_CDFS)) { write_log x; }
  76. #else
  77. #define TRACE(x) if (log_filesys > 0) { write_log x; }
  78. #endif
  79. #define TRACEI(x) if (log_filesys > 0) { write_log x; }
  80. #define TRACE2(x) if (log_filesys >= 2) { write_log x; }
  81. #define TRACE3(x) if (log_filesys >= 3) { write_log x; }
  82. #define DUMPLOCK(c,u,x) dumplock(c,u,x)
  83. #else
  84. #define TRACE(x)
  85. #define DUMPLOCK(c,u,x)
  86. #define TRACE2(x)
  87. #define TRACE3(x)
  88. #endif
  89. #define KS12_BOOT_HACK 1
  90. #define UNIT_LED(unit) ((unit)->ui.unit_type == UNIT_CDFS ? LED_CD : LED_HD)
  91. static int bootrom_header;
  92. static uae_u32 dlg (uae_u32 a)
  93. {
  94. return (dbg (a + 0) << 24) | (dbg (a + 1) << 16) | (dbg (a + 2) << 8) | (dbg (a + 3) << 0);
  95. }
  96. static void aino_test (a_inode *aino)
  97. {
  98. #ifdef AINO_DEBUG
  99. a_inode *aino2 = aino, *aino3;
  100. for (;;) {
  101. if (!aino || !aino->next)
  102. return;
  103. if ((aino->checksum1 ^ aino->checksum2) != 0xaaaa5555) {
  104. write_log (_T("PANIC: corrupted or freed but used aino detected!"), aino);
  105. }
  106. aino3 = aino;
  107. aino = aino->next;
  108. if (aino->prev != aino3) {
  109. write_log (_T("PANIC: corrupted aino linking!\n"));
  110. break;
  111. }
  112. if (aino == aino2) break;
  113. }
  114. #endif
  115. }
  116. static void aino_test_init (a_inode *aino)
  117. {
  118. #ifdef AINO_DEBUG
  119. aino->checksum1 = (uae_u32)aino;
  120. aino->checksum2 = aino->checksum1 ^ 0xaaaa5555;
  121. #endif
  122. }
  123. #define UAEFS_VERSION "UAE fs 0.6"
  124. uaecptr filesys_initcode, filesys_initcode_ptr, filesys_initcode_real;
  125. static uaecptr bootrom_start;
  126. static uae_u32 fsdevname, fshandlername, filesys_configdev;
  127. static uae_u32 cdfs_devname, cdfs_handlername;
  128. static uaecptr afterdos_name, afterdos_id, afterdos_initcode;
  129. static uaecptr keymaphook_name, keymaphook_id, keymaphook_initcode;
  130. static uaecptr shell_execute_data, shell_execute_process;
  131. static int filesys_in_interrupt;
  132. static uae_u32 mountertask;
  133. static int automountunit = -1;
  134. static int autocreatedunit;
  135. static int cd_unit_offset, cd_unit_number;
  136. static uaecptr ROM_filesys_doio, ROM_filesys_doio_original;
  137. static uaecptr ROM_filesys_putmsg, ROM_filesys_putmsg_original;
  138. static uaecptr ROM_filesys_putmsg_return;
  139. static uaecptr ROM_filesys_hack_remove;
  140. static smp_comm_pipe shellexecute_pipe;
  141. static uae_u32 segtrack_mode = 0;
  142. #define FS_STARTUP 0
  143. #define FS_GO_DOWN 1
  144. #define DEVNAMES_PER_HDF 32
  145. #define UNIT_FILESYSTEM 0
  146. #define UNIT_CDFS 1
  147. typedef struct {
  148. int unit_type;
  149. int open; // >0 start as filesystem, <0 = allocated but do not start
  150. TCHAR *devname; /* device name, e.g. UAE0: */
  151. uaecptr devname_amiga;
  152. uaecptr startup;
  153. uaecptr devicenode;
  154. uaecptr parmpacket;
  155. TCHAR *volname; /* volume name, e.g. CDROM, WORK, etc. */
  156. int volflags; /* volume flags, readonly, stream uaefsdb support */
  157. TCHAR *rootdir; /* root native directory/hdf. empty drive if invalid path */
  158. TCHAR *rootdirdiff; /* "diff" file/directory */
  159. bool readonly; /* disallow write access? */
  160. bool locked; /* action write protect */
  161. bool unknown_media; /* ID_UNREADABLE_DISK */
  162. int bootpri; /* boot priority. -128 = no autoboot, -129 = no mount */
  163. int devno;
  164. bool wasisempty; /* if true, this unit was created empty */
  165. int canremove; /* if >0, this unit can be safely ejected and remounted */
  166. bool configureddrive; /* if true, this is drive that was manually configured */
  167. bool inject_icons; /* inject icons if directory filesystem */
  168. struct hardfiledata hf;
  169. struct zvolume *zarchive;
  170. /* Threading stuff */
  171. smp_comm_pipe *volatile unit_pipe, *volatile back_pipe;
  172. uae_thread_id tid;
  173. struct _unit *self;
  174. /* Reset handling */
  175. uae_sem_t reset_sync_sem;
  176. volatile int reset_state;
  177. /* RDB stuff */
  178. uaecptr rdb_devname_amiga[DEVNAMES_PER_HDF];
  179. int rdb_lowcyl;
  180. int rdb_highcyl;
  181. int rdb_cylblocks;
  182. uae_u8 *rdb_filesysstore;
  183. int rdb_filesyssize;
  184. TCHAR *filesysdir;
  185. /* filesystem seglist */
  186. uaecptr filesysseg;
  187. uae_u32 rdb_dostype;
  188. /* CDFS */
  189. bool cd_open;
  190. int cddevno;
  191. void *cdfs_superblock;
  192. } UnitInfo;
  193. struct uaedev_mount_info {
  194. UnitInfo ui[MAX_FILESYSTEM_UNITS];
  195. };
  196. static struct uaedev_mount_info mountinfo;
  197. /* minimal AmigaDOS definitions */
  198. /* field offsets in DosPacket */
  199. #define dp_Type 8
  200. #define dp_Res1 12
  201. #define dp_Res2 16
  202. #define dp_Arg1 20
  203. #define dp_Arg2 24
  204. #define dp_Arg3 28
  205. #define dp_Arg4 32
  206. #define dp_Arg5 36
  207. #define DP64_INIT -3L
  208. #define dp64_Type 8
  209. #define dp64_Res0 12
  210. #define dp64_Res2 16
  211. #define dp64_Res1 24
  212. #define dp64_Arg1 32
  213. #define dp64_Arg2 40
  214. #define dp64_Arg3 48
  215. #define dp64_Arg4 52
  216. #define dp64_Arg5 56
  217. #define dp_Max 60
  218. /* result codes */
  219. #define DOS_TRUE ((uae_u32)-1L)
  220. #define DOS_FALSE (0L)
  221. /* DirEntryTypes */
  222. #define ST_PIPEFILE -5
  223. #define ST_LINKFILE -4
  224. #define ST_FILE -3
  225. #define ST_ROOT 1
  226. #define ST_USERDIR 2
  227. #define ST_SOFTLINK 3
  228. #define ST_LINKDIR 4
  229. #if 1
  230. #define MAXFILESIZE32 (0xffffffff)
  231. #else
  232. /* technically correct but most native
  233. * filesystems don't enforce it
  234. */
  235. #define MAXFILESIZE32 (0x7fffffff)
  236. #endif
  237. #define MAXFILESIZE32_2G (0x7fffffff)
  238. /* Passed as type to Lock() */
  239. #define SHARED_LOCK -2 /* File is readable by others */
  240. #define ACCESS_READ -2 /* Synonym */
  241. #define EXCLUSIVE_LOCK -1 /* No other access allowed */
  242. #define ACCESS_WRITE -1 /* Synonym */
  243. /* packet types */
  244. #define ACTION_CURRENT_VOLUME 7
  245. #define ACTION_LOCATE_OBJECT 8
  246. #define ACTION_RENAME_DISK 9
  247. #define ACTION_FREE_LOCK 15
  248. #define ACTION_DELETE_OBJECT 16
  249. #define ACTION_RENAME_OBJECT 17
  250. #define ACTION_MORE_CACHE 18
  251. #define ACTION_COPY_DIR 19
  252. #define ACTION_SET_PROTECT 21
  253. #define ACTION_CREATE_DIR 22
  254. #define ACTION_EXAMINE_OBJECT 23
  255. #define ACTION_EXAMINE_NEXT 24
  256. #define ACTION_DISK_INFO 25
  257. #define ACTION_INFO 26
  258. #define ACTION_FLUSH 27
  259. #define ACTION_SET_COMMENT 28
  260. #define ACTION_PARENT 29
  261. #define ACTION_SET_DATE 34
  262. #define ACTION_FIND_WRITE 1004
  263. #define ACTION_FIND_INPUT 1005
  264. #define ACTION_FIND_OUTPUT 1006
  265. #define ACTION_END 1007
  266. #define ACTION_SEEK 1008
  267. #define ACTION_WRITE_PROTECT 1023
  268. #define ACTION_IS_FILESYSTEM 1027
  269. #define ACTION_READ 'R'
  270. #define ACTION_WRITE 'W'
  271. /* 2.0+ packet types */
  272. #define ACTION_INHIBIT 31
  273. #define ACTION_SET_FILE_SIZE 1022
  274. #define ACTION_LOCK_RECORD 2008
  275. #define ACTION_FREE_RECORD 2009
  276. #define ACTION_SAME_LOCK 40
  277. #define ACTION_CHANGE_MODE 1028
  278. #define ACTION_FH_FROM_LOCK 1026
  279. #define ACTION_COPY_DIR_FH 1030
  280. #define ACTION_PARENT_FH 1031
  281. #define ACTION_EXAMINE_ALL 1033
  282. #define ACTION_EXAMINE_FH 1034
  283. #define ACTION_EXAMINE_ALL_END 1035
  284. #define ACTION_FORMAT 1020
  285. #define ACTION_IS_FILESYSTEM 1027
  286. #define ACTION_ADD_NOTIFY 4097
  287. #define ACTION_REMOVE_NOTIFY 4098
  288. #define ACTION_READ_LINK 1024
  289. /* OS4 64-bit filesize packets */
  290. #define ACTION_FILESYSTEM_ATTR 3005
  291. #define ACTION_CHANGE_FILE_POSITION64 8001
  292. #define ACTION_GET_FILE_POSITION64 8002
  293. #define ACTION_CHANGE_FILE_SIZE64 8003
  294. #define ACTION_GET_FILE_SIZE64 8004
  295. /* MOS 64-bit filesize packets */
  296. #define ACTION_SEEK64 26400
  297. #define ACTION_SET_FILE_SIZE64 26401
  298. #define ACTION_LOCK_RECORD64 26402
  299. #define ACTION_FREE_RECORD64 26403
  300. #define ACTION_QUERY_ATTR 26407
  301. #define ACTION_EXAMINE_OBJECT64 26408
  302. #define ACTION_EXAMINE_NEXT64 26409
  303. #define ACTION_EXAMINE_FH64 26410
  304. /* not supported */
  305. #define ACTION_MAKE_LINK 1021
  306. #define DISK_TYPE_DOS 0x444f5300 /* DOS\0 */
  307. #define DISK_TYPE_DOS_FFS 0x444f5301 /* DOS\1 */
  308. #define CDFS_DOSTYPE 0x43440000 /* CDxx */
  309. typedef struct _dpacket {
  310. uaecptr packet_addr;
  311. uae_u8 *packet_data;
  312. uae_u8 packet_array[dp_Max];
  313. bool need_flush;
  314. } dpacket;
  315. typedef struct {
  316. uae_u32 uniq;
  317. /* The directory we're going through. */
  318. a_inode *aino;
  319. /* The file we're going to look up next. */
  320. a_inode *curr_file;
  321. } ExamineKey;
  322. struct lockrecord
  323. {
  324. struct lockrecord *next;
  325. dpacket *packet;
  326. uae_u64 pos;
  327. uae_u64 len;
  328. uae_u32 mode;
  329. uae_u32 timeout;
  330. uae_u32 msg;
  331. };
  332. typedef struct key {
  333. struct key *next;
  334. a_inode *aino;
  335. uae_u32 uniq;
  336. struct fs_filehandle *fd;
  337. uae_u64 file_pos;
  338. int dosmode;
  339. int createmode;
  340. int notifyactive;
  341. struct lockrecord *record;
  342. } Key;
  343. typedef struct notify {
  344. struct notify *next;
  345. uaecptr notifyrequest;
  346. TCHAR *fullname;
  347. TCHAR *partname;
  348. } Notify;
  349. typedef struct exallkey {
  350. uae_u32 id;
  351. struct fs_dirhandle *dirhandle;
  352. TCHAR *fn;
  353. uaecptr control;
  354. } ExAllKey;
  355. /* Since ACTION_EXAMINE_NEXT is so braindamaged, we have to keep
  356. * some of these around
  357. */
  358. #define EXKEYS 128
  359. #define EXALLKEYS 100
  360. #define MAX_AINO_HASH 128
  361. #define NOTIFY_HASH_SIZE 127
  362. /* handler state info */
  363. typedef struct _unit {
  364. struct _unit *next;
  365. /* Amiga stuff */
  366. uaecptr dosbase;
  367. /* volume points to our IO board, always 1:1 mapping */
  368. uaecptr volume;
  369. uaecptr port; /* Our port */
  370. uaecptr locklist;
  371. /* Native stuff */
  372. uae_s32 unit; /* unit number */
  373. UnitInfo ui; /* unit startup info */
  374. TCHAR tmpbuf3[256];
  375. /* Dummy message processing */
  376. uaecptr dummy_message;
  377. volatile unsigned int cmds_sent;
  378. volatile unsigned int cmds_complete;
  379. volatile unsigned int cmds_acked;
  380. /* ExKeys */
  381. ExamineKey examine_keys[EXKEYS];
  382. int next_exkey;
  383. unsigned int total_locked_ainos;
  384. /* ExAll */
  385. ExAllKey exalls[EXALLKEYS];
  386. int exallid;
  387. /* Keys */
  388. struct key *keys;
  389. struct lockrecord *waitingrecords;
  390. a_inode rootnode;
  391. unsigned int aino_cache_size;
  392. a_inode *aino_hash[MAX_AINO_HASH];
  393. unsigned int nr_cache_hits;
  394. unsigned int nr_cache_lookups;
  395. struct notify *notifyhash[NOTIFY_HASH_SIZE];
  396. int volflags;
  397. uae_u32 lockkey;
  398. bool inhibited;
  399. bool canremovable;
  400. /* increase when media is changed.
  401. * used to detect if cached aino is valid
  402. */
  403. int mountcount;
  404. int mount_changed;
  405. void *cdfs_superblock;
  406. TCHAR *mount_volume;
  407. TCHAR *mount_rootdir;
  408. bool mount_readonly;
  409. int mount_flags;
  410. int reinsertdelay;
  411. TCHAR *newvolume;
  412. TCHAR *newrootdir;
  413. bool newreadonly;
  414. int newflags;
  415. } Unit;
  416. int nr_units (void)
  417. {
  418. int cnt = 0;
  419. for (int i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  420. if (mountinfo.ui[i].open > 0)
  421. cnt++;
  422. }
  423. return cnt;
  424. }
  425. int nr_directory_units (struct uae_prefs *p)
  426. {
  427. int cnt = 0;
  428. if (p) {
  429. for (int i = 0; i < p->mountitems; i++) {
  430. if (p->mountconfig[i].ci.controller_type == HD_CONTROLLER_TYPE_UAE)
  431. cnt++;
  432. }
  433. } else {
  434. for (int i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  435. if (mountinfo.ui[i].open > 0 && mountinfo.ui[i].hf.ci.controller_type == HD_CONTROLLER_TYPE_UAE)
  436. cnt++;
  437. }
  438. }
  439. return cnt;
  440. }
  441. static int is_virtual (int unit_no)
  442. {
  443. int t = is_hardfile (unit_no);
  444. return t == FILESYS_VIRTUAL || t == FILESYS_CD;
  445. }
  446. int is_hardfile (int unit_no)
  447. {
  448. if (mountinfo.ui[unit_no].volname || mountinfo.ui[unit_no].wasisempty || mountinfo.ui[unit_no].unknown_media) {
  449. if (unit_no >= cd_unit_offset && unit_no < cd_unit_offset + cd_unit_number)
  450. return FILESYS_CD;
  451. return FILESYS_VIRTUAL;
  452. }
  453. if (mountinfo.ui[unit_no].hf.ci.sectors == 0) {
  454. if (mountinfo.ui[unit_no].hf.flags & 1)
  455. return FILESYS_HARDDRIVE;
  456. return FILESYS_HARDFILE_RDB;
  457. }
  458. return FILESYS_HARDFILE;
  459. }
  460. static void close_filesys_unit (UnitInfo *uip)
  461. {
  462. if (!uip->open)
  463. return;
  464. if (uip->hf.handle_valid)
  465. hdf_close (&uip->hf);
  466. if (uip->volname != 0)
  467. xfree (uip->volname);
  468. if (uip->devname != 0)
  469. xfree (uip->devname);
  470. if (uip->rootdir != 0)
  471. xfree (uip->rootdir);
  472. if (uip->unit_pipe)
  473. xfree (uip->unit_pipe);
  474. if (uip->back_pipe)
  475. xfree (uip->back_pipe);
  476. if (uip->cd_open) {
  477. sys_command_close (uip->cddevno);
  478. isofs_unmount (uip->cdfs_superblock);
  479. }
  480. uip->unit_pipe = 0;
  481. uip->back_pipe = 0;
  482. uip->hf.handle_valid = 0;
  483. uip->volname = 0;
  484. uip->devname = 0;
  485. uip->rootdir = 0;
  486. uip->open = 0;
  487. uip->cd_open = 0;
  488. }
  489. static uaedev_config_data *getuci (struct uaedev_config_data *uci, int nr)
  490. {
  491. return &uci[nr];
  492. }
  493. static UnitInfo *getuip (struct uae_prefs *p, int index)
  494. {
  495. if (index < 0)
  496. return NULL;
  497. index = p->mountconfig[index].configoffset;
  498. if (index < 0)
  499. return NULL;
  500. return &mountinfo.ui[index];
  501. }
  502. static int getuindex (struct uae_prefs *p, int index)
  503. {
  504. if (index < 0)
  505. return -1;
  506. return p->mountconfig[index].unitnum;
  507. }
  508. int get_filesys_unitconfig (struct uae_prefs *p, int index, struct mountedinfo *mi)
  509. {
  510. UnitInfo *ui = getuip (p, index);
  511. struct uaedev_config_data *uci = &p->mountconfig[index];
  512. UnitInfo uitmp;
  513. TCHAR filepath[MAX_DPATH];
  514. memset (mi, 0, sizeof (struct mountedinfo));
  515. memset (&uitmp, 0, sizeof uitmp);
  516. _tcscpy(mi->rootdir, uci->ci.rootdir);
  517. if (!ui) {
  518. ui = &uitmp;
  519. if (uci->ci.type == UAEDEV_DIR) {
  520. cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_DIR);
  521. _tcscpy(mi->rootdir, filepath);
  522. mi->ismounted = 1;
  523. if (filepath[0] == 0)
  524. return FILESYS_VIRTUAL;
  525. if (my_existsfile (filepath)) {
  526. mi->ismedia = 1;
  527. return FILESYS_VIRTUAL;
  528. }
  529. if (my_getvolumeinfo (filepath) < 0)
  530. return -1;
  531. mi->ismedia = true;
  532. return FILESYS_VIRTUAL;
  533. } else if (uci->ci.type == UAEDEV_HDF) {
  534. cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_HDF);
  535. _tcscpy(mi->rootdir, filepath);
  536. ui->hf.ci.readonly = true;
  537. ui->hf.ci.blocksize = uci->ci.blocksize;
  538. int err = hdf_open (&ui->hf, filepath);
  539. if (err <= 0) {
  540. mi->ismedia = false;
  541. mi->ismounted = true;
  542. mi->error = err;
  543. if (uci->ci.reserved == 0 && uci->ci.sectors == 0 && uci->ci.surfaces == 0) {
  544. if (ui->hf.flags & 1)
  545. return FILESYS_HARDDRIVE;
  546. return FILESYS_HARDFILE_RDB;
  547. }
  548. return -1;
  549. }
  550. mi->ismedia = true;
  551. if (ui->hf.drive_empty)
  552. mi->ismedia = 0;
  553. hdf_close (&ui->hf);
  554. } else if (uci->ci.type == UAEDEV_CD) {
  555. cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_CD);
  556. _tcscpy(mi->rootdir, filepath);
  557. struct device_info di;
  558. ui->hf.ci.readonly = true;
  559. ui->hf.ci.blocksize = uci->ci.blocksize;
  560. mi->size = -1;
  561. mi->ismounted = true;
  562. if (blkdev_get_info (p, ui->hf.ci.device_emu_unit, &di)) {
  563. mi->ismedia = di.media_inserted != 0;
  564. _tcscpy (mi->rootdir, di.label);
  565. }
  566. #if 0
  567. if (ui->hf.ci.cd_emu_unit == 0)
  568. _tcscpy (mi->rootdir, _T("CD"));
  569. else
  570. _stprintf (mi->rootdir, _T("CD %d"), ui->hf.ci.cd_emu_unit);
  571. #endif
  572. }
  573. } else if (uci->ci.type != UAEDEV_TAPE) {
  574. if (ui->hf.ci.controller_type == HD_CONTROLLER_TYPE_UAE) { // what is this? || (ui->controller && p->cs_ide)) {
  575. mi->ismounted = 1;
  576. if (uci->ci.type == UAEDEV_HDF)
  577. mi->ismedia = ui->hf.drive_empty ? false : true;
  578. else
  579. mi->ismedia = true;
  580. }
  581. }
  582. if (uci->ci.type == UAEDEV_TAPE) {
  583. cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_TAPE);
  584. _tcscpy(mi->rootdir, filepath);
  585. struct device_info di;
  586. int unitnum = getuindex (p, index);
  587. mi->size = -1;
  588. mi->ismounted = false;
  589. if (unitnum >= 0) {
  590. mi->ismounted = true;
  591. if (tape_get_info (unitnum, &di)) {
  592. mi->ismedia = di.media_inserted != 0;
  593. _tcscpy (mi->rootdir, di.label);
  594. }
  595. } else {
  596. struct scsi_data_tape *tape;
  597. unitnum = 0;
  598. tape = tape_alloc (unitnum, filepath, uci->ci.readonly);
  599. if (tape) {
  600. if (tape_get_info (unitnum, &di)) {
  601. mi->ismedia = di.media_inserted != 0;
  602. _tcscpy (mi->rootdir, di.label);
  603. }
  604. tape_free (tape);
  605. }
  606. }
  607. return FILESYS_TAPE;
  608. }
  609. if (mi->size < 0)
  610. return -1;
  611. mi->size = ui->hf.virtsize;
  612. if (uci->ci.highcyl) {
  613. uci->ci.cyls = mi->nrcyls = uci->ci.highcyl;
  614. } else {
  615. uci->ci.cyls = mi->nrcyls = (int)(uci->ci.sectors * uci->ci.surfaces ? (ui->hf.virtsize / uci->ci.blocksize) / (uci->ci.sectors * uci->ci.surfaces) : 0);
  616. }
  617. if (uci->ci.type == UAEDEV_DIR)
  618. return FILESYS_VIRTUAL;
  619. if (uci->ci.reserved == 0 && uci->ci.sectors == 0 && uci->ci.surfaces == 0) {
  620. if (ui->hf.flags & 1)
  621. return FILESYS_HARDDRIVE;
  622. return FILESYS_HARDFILE_RDB;
  623. }
  624. return FILESYS_HARDFILE;
  625. }
  626. static void stripsemicolon (TCHAR *s)
  627. {
  628. if (!s)
  629. return;
  630. while (_tcslen(s) > 0 && s[_tcslen(s) - 1] == ':')
  631. s[_tcslen(s) - 1] = 0;
  632. }
  633. static void stripspace (TCHAR *s)
  634. {
  635. if (!s)
  636. return;
  637. for (int i = 0; i < _tcslen (s); i++) {
  638. if (s[i] == ' ')
  639. s[i] = '_';
  640. }
  641. }
  642. static void striplength (TCHAR *s, int len)
  643. {
  644. if (!s)
  645. return;
  646. if (_tcslen (s) <= len)
  647. return;
  648. s[len] = 0;
  649. }
  650. static void fixcharset (TCHAR *s)
  651. {
  652. char tmp[MAX_DPATH];
  653. if (!s)
  654. return;
  655. ua_fs_copy (tmp, MAX_DPATH, s, '_');
  656. au_fs_copy (s, strlen (tmp) + 1, tmp);
  657. }
  658. TCHAR *validatevolumename (TCHAR *s, const TCHAR *def)
  659. {
  660. stripsemicolon (s);
  661. fixcharset (s);
  662. striplength (s, 30);
  663. if (_tcslen(s) == 0 && def) {
  664. xfree(s);
  665. s = my_strdup(def);
  666. }
  667. return s;
  668. }
  669. TCHAR *validatedevicename (TCHAR *s, const TCHAR *def)
  670. {
  671. stripsemicolon (s);
  672. stripspace (s);
  673. fixcharset (s);
  674. striplength (s, 30);
  675. if (_tcslen(s) == 0 && def) {
  676. xfree(s);
  677. s = my_strdup(def);
  678. }
  679. return s;
  680. }
  681. TCHAR *filesys_createvolname (const TCHAR *volname, const TCHAR *rootdir, struct zvolume *zv, const TCHAR *def)
  682. {
  683. TCHAR *nvol = NULL;
  684. int i, archivehd;
  685. TCHAR *p = NULL;
  686. TCHAR path[MAX_DPATH];
  687. cfgfile_resolve_path_out_load(rootdir, path, MAX_DPATH, PATH_DIR);
  688. archivehd = -1;
  689. if (my_existsfile (path))
  690. archivehd = 1;
  691. else if (my_existsdir (path))
  692. archivehd = 0;
  693. if (zv && zv->volumename && _tcslen(zv->volumename) > 0) {
  694. nvol = my_strdup(zv->volumename);
  695. nvol = validatevolumename (nvol, def);
  696. return nvol;
  697. }
  698. if ((!volname || _tcslen (volname) == 0) && path && archivehd >= 0) {
  699. p = my_strdup (path);
  700. for (i = _tcslen (p) - 1; i >= 0; i--) {
  701. TCHAR c = p[i];
  702. if (c == ':' || c == '/' || c == '\\') {
  703. if (i == _tcslen (p) - 1)
  704. continue;
  705. if (!_tcscmp (p + i, _T(":\\"))) {
  706. xfree (p);
  707. p = xmalloc (TCHAR, 10);
  708. p[0] = path[0];
  709. p[1] = 0;
  710. i = 0;
  711. } else {
  712. i++;
  713. }
  714. break;
  715. }
  716. }
  717. if (i >= 0)
  718. nvol = my_strdup (p + i);
  719. }
  720. if (!nvol && archivehd >= 0) {
  721. if (volname && _tcslen (volname) > 0)
  722. nvol = my_strdup (volname);
  723. else
  724. nvol = my_strdup (def);
  725. }
  726. if (!nvol) {
  727. if (volname && _tcslen (volname))
  728. nvol = my_strdup (volname);
  729. else
  730. nvol = my_strdup (_T(""));
  731. }
  732. nvol = validatevolumename (nvol, def);
  733. xfree (p);
  734. return nvol;
  735. }
  736. static int set_filesys_volume (const TCHAR *rootdir, int *flags, bool *readonly, bool *emptydrive, struct zvolume **zvp)
  737. {
  738. *emptydrive = 0;
  739. if (my_existsfile (rootdir)) {
  740. struct zvolume *zv;
  741. zv = zfile_fopen_archive (rootdir);
  742. if (!zv) {
  743. error_log (_T("'%s' is not a supported archive file."), rootdir);
  744. return -1;
  745. }
  746. *zvp = zv;
  747. *flags = MYVOLUMEINFO_ARCHIVE;
  748. *readonly = 1;
  749. } else {
  750. *flags = my_getvolumeinfo (rootdir);
  751. if (*flags < 0) {
  752. if (rootdir && rootdir[0])
  753. error_log (_T("directory '%s' not found, mounting as empty drive."), rootdir);
  754. *emptydrive = 1;
  755. *flags = 0;
  756. } else if ((*flags) & MYVOLUMEINFO_READONLY) {
  757. error_log (_T("'%s' set to read-only."), rootdir);
  758. *readonly = 1;
  759. }
  760. }
  761. return 1;
  762. }
  763. void uci_set_defaults (struct uaedev_config_info *uci, bool rdb)
  764. {
  765. memset (uci, 0, sizeof (struct uaedev_config_info));
  766. if (!rdb) {
  767. uci->sectors = 32;
  768. uci->reserved = 2;
  769. uci->surfaces = 1;
  770. }
  771. uci->blocksize = 512;
  772. uci->maxtransfer = 0x7fffffff;
  773. uci->mask = 0xffffffff;
  774. uci->bufmemtype = 1;
  775. uci->buffers = 50;
  776. uci->stacksize = 4000;
  777. uci->bootpri = 0;
  778. uci->priority = 10;
  779. uci->sectorsperblock = 1;
  780. uci->device_emu_unit = -1;
  781. }
  782. static void get_usedblocks(struct fs_usage *fsu, bool fs, int *pblocksize, uae_s64 *pnumblocks, uae_s64 *pinuse, bool reduce)
  783. {
  784. uae_s64 numblocks = 0, inuse;
  785. int blocksize = *pblocksize;
  786. if (fs && currprefs.filesys_limit) {
  787. if (fsu->total > (uae_s64)currprefs.filesys_limit * 1024) {
  788. uae_s64 oldtotal = fsu->total;
  789. fsu->total = currprefs.filesys_limit * 1024;
  790. fsu->avail = ((fsu->avail / 1024) * (fsu->total / 1024)) / (oldtotal / 1024);
  791. fsu->avail *= 1024;
  792. }
  793. }
  794. if (reduce) {
  795. while (blocksize < 32768 || numblocks == 0) {
  796. numblocks = fsu->total / blocksize;
  797. if (numblocks <= 10)
  798. numblocks = 10;
  799. // Value that does not overflow when multiplied by 100 (uses 128 to keep it simple)
  800. if (numblocks < 0x02000000)
  801. break;
  802. blocksize *= 2;
  803. }
  804. }
  805. numblocks = fsu->total / blocksize;
  806. inuse = (numblocks * blocksize - fsu->avail) / blocksize;
  807. if (inuse > numblocks)
  808. inuse = numblocks;
  809. if (pnumblocks)
  810. *pnumblocks = numblocks;
  811. if (pinuse)
  812. *pinuse = inuse;
  813. if (pblocksize)
  814. *pblocksize = blocksize;
  815. }
  816. static bool get_blocks(const TCHAR *rootdir, int unit, int flags, int *pblocksize, uae_s64 *pnumblocks, uae_s64 *pinuse, bool reduce)
  817. {
  818. struct fs_usage fsu;
  819. int ret;
  820. bool fs = false;
  821. int blocksize;
  822. blocksize = 512;
  823. if (flags & MYVOLUMEINFO_ARCHIVE) {
  824. ret = zfile_fs_usage_archive(rootdir, 0, &fsu);
  825. fs = true;
  826. } else {
  827. ret = get_fs_usage(rootdir, 0, &fsu);
  828. fs = true;
  829. }
  830. if (ret)
  831. return false;
  832. get_usedblocks(&fsu, fs, &blocksize, pnumblocks, pinuse, reduce);
  833. if (pblocksize)
  834. *pblocksize = blocksize;
  835. return ret == 0;
  836. }
  837. static int set_filesys_unit_1 (int nr, struct uaedev_config_info *ci, bool custom)
  838. {
  839. UnitInfo *ui;
  840. int i;
  841. bool emptydrive = false;
  842. bool iscd;
  843. struct uaedev_config_info c;
  844. memcpy (&c, ci, sizeof (struct uaedev_config_info));
  845. if (nr < 0) {
  846. for (nr = 0; nr < MAX_FILESYSTEM_UNITS; nr++) {
  847. if (!mountinfo.ui[nr].open)
  848. break;
  849. }
  850. if (nr == MAX_FILESYSTEM_UNITS) {
  851. error_log (_T("No slot allocated for this unit"));
  852. return -1;
  853. }
  854. }
  855. if (ci->controller_type != HD_CONTROLLER_TYPE_UAE || ci->type == UAEDEV_TAPE) {
  856. ui = &mountinfo.ui[nr];
  857. memset (ui, 0, sizeof (UnitInfo));
  858. memcpy (&ui->hf.ci, &c, sizeof (struct uaedev_config_info));
  859. ui->readonly = c.readonly;
  860. ui->unit_type = -1;
  861. ui->open = -1;
  862. return nr;
  863. }
  864. iscd = nr >= cd_unit_offset && nr < cd_unit_offset + cd_unit_number;
  865. if (!custom)
  866. cfgfile_resolve_path_load(c.rootdir, MAX_DPATH, iscd ? PATH_CD : (ci->volname[0] ? PATH_DIR : PATH_HDF));
  867. for (i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  868. if (nr == i || !mountinfo.ui[i].open || mountinfo.ui[i].rootdir == NULL || is_hardfile (i) == FILESYS_CD)
  869. continue;
  870. if (_tcslen(c.rootdir) > 0 && samepath(mountinfo.ui[i].rootdir, c.rootdir)) {
  871. error_log (_T("directory/hardfile '%s' already added."), c.rootdir);
  872. return -1;
  873. }
  874. }
  875. for (;;) {
  876. bool retry = false;
  877. for (i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  878. int hf = is_hardfile(i);
  879. if (nr == i || !mountinfo.ui[i].open || mountinfo.ui[i].rootdir == NULL)
  880. continue;
  881. if (hf != FILESYS_HARDDRIVE && hf != FILESYS_HARDFILE && hf != FILESYS_HARDFILE_RDB)
  882. continue;
  883. if (mountinfo.ui[i].hf.ci.controller_unit == c.controller_unit) {
  884. c.controller_unit++;
  885. if (c.controller_unit >= MAX_FILESYSTEM_UNITS) {
  886. error_log (_T("directory/hardfile '%s' invalid unit number."), c.rootdir);
  887. return -1;
  888. }
  889. retry = true;
  890. }
  891. }
  892. if (!retry)
  893. break;
  894. }
  895. ui = &mountinfo.ui[nr];
  896. memset (ui, 0, sizeof (UnitInfo));
  897. memcpy(&ui->hf.ci, &c, sizeof c);
  898. if (iscd) {
  899. ui->unit_type = UNIT_CDFS;
  900. emptydrive = 1;
  901. ui->volflags = MYVOLUMEINFO_CDFS | MYVOLUMEINFO_READONLY;
  902. c.readonly = true;
  903. } else if (c.volname[0]) {
  904. int flags = 0;
  905. uae_s64 numblocks;
  906. emptydrive = 1;
  907. if (c.rootdir[0]) {
  908. if (set_filesys_volume (c.rootdir, &flags, &c.readonly, &emptydrive, &ui->zarchive) < 0)
  909. return -1;
  910. }
  911. ui->volname = filesys_createvolname (c.volname, c.rootdir, ui->zarchive, _T("harddrive"));
  912. ui->volflags = flags;
  913. TCHAR *vs = au(UAEFS_VERSION);
  914. TCHAR *vsp = vs + _tcslen(vs) - 1;
  915. while (vsp != vs) {
  916. if (*vsp == ' ') {
  917. *vsp++ = 0;
  918. break;
  919. }
  920. vsp--;
  921. }
  922. _tcscpy(ui->hf.vendor_id, _T("UAE"));
  923. _tcscpy(ui->hf.product_id, vs);
  924. _tcscpy(ui->hf.product_rev, vsp);
  925. xfree(vs);
  926. ui->hf.ci.unit_feature_level = HD_LEVEL_SCSI_2;
  927. if (get_blocks(c.rootdir, nr, flags, &ui->hf.ci.blocksize, &numblocks, NULL, false))
  928. ui->hf.ci.max_lba = numblocks > 0xffffffff ? 0xffffffff : numblocks;
  929. else
  930. ui->hf.ci.max_lba = 0x00ffffff;
  931. } else {
  932. ui->unit_type = UNIT_FILESYSTEM;
  933. ui->hf.unitnum = nr;
  934. ui->volname = 0;
  935. if (ui->hf.ci.rootdir[0]) {
  936. if (hdf_open (&ui->hf) <= 0 && !c.readonly) {
  937. write_log (_T("Attempting to open '%s' in read-only mode.\n"), ui->hf.ci.rootdir);
  938. ui->hf.ci.readonly = c.readonly = true;
  939. if (hdf_open (&ui->hf) > 0) {
  940. error_log (_T("'%s' opened in read-only mode.\n"), ui->hf.ci.rootdir);
  941. }
  942. }
  943. } else {
  944. // empty drive?
  945. ui->hf.drive_empty = 1;
  946. }
  947. if (!ui->hf.drive_empty) {
  948. if (ui->hf.handle_valid == 0) {
  949. error_log (_T("Hardfile '%s' not found."), ui->hf.ci.rootdir);
  950. goto err;
  951. }
  952. if (ui->hf.ci.blocksize > ui->hf.virtsize || ui->hf.virtsize == 0) {
  953. error_log (_T("Hardfile '%s' too small."), ui->hf.ci.rootdir);
  954. goto err;
  955. }
  956. }
  957. if ((ui->hf.ci.blocksize & (ui->hf.ci.blocksize - 1)) != 0 || ui->hf.ci.blocksize == 0) {
  958. error_log (_T("Hardfile '%s' bad blocksize %d."), ui->hf.ci.rootdir, ui->hf.ci.blocksize);
  959. goto err;
  960. }
  961. if ((ui->hf.ci.sectors || ui->hf.ci.surfaces || ui->hf.ci.reserved) &&
  962. (ui->hf.ci.sectors < 1 || ui->hf.ci.surfaces < 1 || ui->hf.ci.surfaces > 1023 ||
  963. ui->hf.ci.reserved < 0 || ui->hf.ci.reserved > 1023) != 0) {
  964. error_log (_T("Hardfile '%s' bad hardfile geometry."), ui->hf.ci.rootdir);
  965. goto err;
  966. }
  967. if (!ui->hf.ci.highcyl) {
  968. ui->hf.ci.cyls = (int)(ui->hf.ci.sectors * ui->hf.ci.surfaces ? (ui->hf.virtsize / ui->hf.ci.blocksize) / (ui->hf.ci.sectors * ui->hf.ci.surfaces) : 0);
  969. }
  970. if (!ui->hf.ci.cyls)
  971. ui->hf.ci.cyls = ui->hf.ci.highcyl;
  972. if (!ui->hf.ci.cyls)
  973. ui->hf.ci.cyls = 1;
  974. }
  975. ui->self = 0;
  976. ui->reset_state = FS_STARTUP;
  977. ui->wasisempty = emptydrive;
  978. ui->canremove = emptydrive && (ci->flags & MYVOLUMEINFO_REUSABLE);
  979. ui->rootdir = my_strdup (c.rootdir);
  980. ui->devname = my_strdup (c.devname);
  981. stripsemicolon(ui->devname);
  982. if (c.filesys[0])
  983. ui->filesysdir = my_strdup (c.filesys);
  984. ui->readonly = c.readonly;
  985. if (c.bootpri < -129)
  986. c.bootpri = -129;
  987. if (c.bootpri > 127)
  988. c.bootpri = 127;
  989. ui->bootpri = c.bootpri;
  990. ui->inject_icons = c.inject_icons;
  991. ui->open = 1;
  992. return nr;
  993. err:
  994. if (ui->hf.handle_valid)
  995. hdf_close (&ui->hf);
  996. return -1;
  997. }
  998. static int set_filesys_unit (int nr, struct uaedev_config_info *ci, bool custom)
  999. {
  1000. int ret;
  1001. ret = set_filesys_unit_1 (nr, ci, custom);
  1002. return ret;
  1003. }
  1004. static int add_filesys_unit (struct uaedev_config_info *ci, bool custom)
  1005. {
  1006. int nr;
  1007. if (nr_units () >= MAX_FILESYSTEM_UNITS)
  1008. return -1;
  1009. nr = set_filesys_unit_1 (-1, ci, custom);
  1010. #ifdef RETROPLATFORM
  1011. if (nr >= 0) {
  1012. UnitInfo *ui = &mountinfo.ui[nr];
  1013. rp_hd_device_enable (nr, true);
  1014. if (ui->unit_type == UNIT_CDFS)
  1015. rp_cd_image_change(nr, ci->rootdir);
  1016. else
  1017. rp_harddrive_image_change(nr, ci->readonly, ci->rootdir);
  1018. }
  1019. #endif
  1020. return nr;
  1021. }
  1022. int kill_filesys_unitconfig (struct uae_prefs *p, int nr)
  1023. {
  1024. struct uaedev_config_data *uci;
  1025. if (nr < 0)
  1026. return 0;
  1027. uci = getuci (p->mountconfig, nr);
  1028. hardfile_do_disk_change (uci, 0);
  1029. if (uci->configoffset >= 0 && uci->ci.controller_type == HD_CONTROLLER_TYPE_UAE) {
  1030. filesys_media_change(uci->ci.rootdir, 0, uci);
  1031. } else {
  1032. pcmcia_disk_reinsert(p, &uci->ci, true);
  1033. }
  1034. while (nr < MOUNT_CONFIG_SIZE) {
  1035. memmove (&p->mountconfig[nr], &p->mountconfig[nr + 1], sizeof (struct uaedev_config_data));
  1036. nr++;
  1037. }
  1038. p->mountitems--;
  1039. memset (&p->mountconfig[MOUNT_CONFIG_SIZE - 1], 0, sizeof (struct uaedev_config_data));
  1040. return 1;
  1041. }
  1042. int move_filesys_unitconfig (struct uae_prefs *p, int nr, int to)
  1043. {
  1044. struct uaedev_config_data *uci1, *uci2, tmpuci;
  1045. uci1 = getuci (p->mountconfig, nr);
  1046. uci2 = getuci (p->mountconfig, to);
  1047. if (nr == to)
  1048. return 0;
  1049. memcpy (&tmpuci, uci1, sizeof (struct uaedev_config_data));
  1050. memcpy (uci1, uci2, sizeof (struct uaedev_config_data));
  1051. memcpy (uci2, &tmpuci, sizeof (struct uaedev_config_data));
  1052. return 1;
  1053. }
  1054. static void allocuci (struct uae_prefs *p, int nr, int idx, int unitnum)
  1055. {
  1056. struct uaedev_config_data *uci = &p->mountconfig[nr];
  1057. if (idx >= 0) {
  1058. UnitInfo *ui;
  1059. uci->configoffset = idx;
  1060. ui = &mountinfo.ui[idx];
  1061. ui->configureddrive = 1;
  1062. uci->unitnum = unitnum;
  1063. } else {
  1064. uci->configoffset = -1;
  1065. uci->unitnum = -1;
  1066. }
  1067. }
  1068. static void allocuci (struct uae_prefs *p, int nr, int idx)
  1069. {
  1070. allocuci (p, nr, idx, -1);
  1071. }
  1072. static const TCHAR *getunittype(struct uaedev_config_info *uci)
  1073. {
  1074. return uci->type == UAEDEV_CD ? _T("CD") : (uci->type == UAEDEV_TAPE ? _T("TAPE") : _T("HD"));
  1075. }
  1076. static int cpuboard_hd;
  1077. static romconfig cpuboard_dummy;
  1078. void add_cpuboard_unit(int unit, struct uaedev_config_info *uci, struct romconfig *rc)
  1079. {
  1080. int flags = (uci->controller_type >= HD_CONTROLLER_TYPE_IDE_FIRST && uci->controller_type <= HD_CONTROLLER_TYPE_IDE_LAST) ? EXPANSIONTYPE_IDE : EXPANSIONTYPE_SCSI;
  1081. const struct cpuboardtype *cbt = &cpuboards[currprefs.cpuboard_type];
  1082. cpuboard_hd = 0;
  1083. if (cbt->subtypes) {
  1084. if (cbt->subtypes[currprefs.cpuboard_subtype].add && (cbt->subtypes[currprefs.cpuboard_subtype].deviceflags & flags)) {
  1085. if (unit >= 0) {
  1086. write_log(_T("Adding CPUBoard '%s' %s unit %d ('%s')\n"),
  1087. cbt->subtypes[currprefs.cpuboard_subtype].name,
  1088. getunittype(uci), unit, uci->rootdir);
  1089. }
  1090. cbt->subtypes[currprefs.cpuboard_subtype].add(unit, uci, rc);
  1091. cpuboard_hd = 1;
  1092. }
  1093. }
  1094. }
  1095. static void add_cpuboard_unit_init(void)
  1096. {
  1097. memset(&cpuboard_dummy, 0, sizeof cpuboard_dummy);
  1098. cpuboard_dummy.device_id = 7;
  1099. if (currprefs.cpuboard_type) {
  1100. struct romconfig *rc = get_device_romconfig(&currprefs, ROMTYPE_CPUBOARD, 0);
  1101. if (!rc)
  1102. rc = &cpuboard_dummy;
  1103. const struct cpuboardtype *cbt = &cpuboards[currprefs.cpuboard_type];
  1104. if (cbt->subtypes) {
  1105. if (cbt->subtypes[currprefs.cpuboard_subtype].add) {
  1106. const struct cpuboardsubtype *cst = &cbt->subtypes[currprefs.cpuboard_subtype];
  1107. struct uaedev_config_info ci = { 0 };
  1108. write_log(_T("Initializing CPUBoard '%s' %s controller\n"),
  1109. cst->name, (cst->deviceflags & EXPANSIONTYPE_SCSI) ? _T("SCSI") : _T("IDE"));
  1110. cst->add(-1, &ci, rc);
  1111. }
  1112. }
  1113. }
  1114. }
  1115. static bool add_ide_unit(int type, int unit, struct uaedev_config_info *uci)
  1116. {
  1117. bool added = false;
  1118. if (type >= HD_CONTROLLER_TYPE_IDE_EXPANSION_FIRST && type <= HD_CONTROLLER_TYPE_IDE_LAST) {
  1119. for (int i = 0; expansionroms[i].name; i++) {
  1120. if (i == type - HD_CONTROLLER_TYPE_IDE_EXPANSION_FIRST) {
  1121. const struct expansionromtype *ert = &expansionroms[i];
  1122. if ((ert->deviceflags & 2) && is_board_enabled(&currprefs, ert->romtype, uci->controller_type_unit)) {
  1123. cpuboard_hd = 1;
  1124. if (ert->add) {
  1125. struct romconfig *rc = get_device_romconfig(&currprefs, ert->romtype, uci->controller_type_unit);
  1126. write_log(_T("Adding IDE %s '%s' unit %d ('%s')\n"), getunittype(uci),
  1127. ert->name, unit, uci->rootdir);
  1128. ert->add(unit, uci, rc);
  1129. }
  1130. if (cpuboard_hd)
  1131. added = true;
  1132. }
  1133. }
  1134. }
  1135. }
  1136. return added;
  1137. }
  1138. static bool add_scsi_unit(int type, int unit, struct uaedev_config_info *uci)
  1139. {
  1140. bool added = false;
  1141. if (type >= HD_CONTROLLER_TYPE_SCSI_EXPANSION_FIRST && type <= HD_CONTROLLER_TYPE_SCSI_LAST) {
  1142. for (int i = 0; expansionroms[i].name; i++) {
  1143. if (i == type - HD_CONTROLLER_TYPE_SCSI_EXPANSION_FIRST) {
  1144. const struct expansionromtype *ert = &expansionroms[i];
  1145. if ((ert->deviceflags & 1) && is_board_enabled(&currprefs, ert->romtype, uci->controller_type_unit)) {
  1146. cpuboard_hd = 1;
  1147. if (ert->add) {
  1148. struct romconfig *rc = get_device_romconfig(&currprefs, ert->romtype, uci->controller_type_unit);
  1149. write_log(_T("Adding SCSI %s '%s' unit %d ('%s')\n"), getunittype(uci),
  1150. ert->name, unit, uci->rootdir);
  1151. ert->add(unit, uci, rc);
  1152. }
  1153. if (cpuboard_hd)
  1154. added = true;
  1155. }
  1156. }
  1157. }
  1158. }
  1159. return added;
  1160. }
  1161. static void initialize_mountinfo (void)
  1162. {
  1163. int nr;
  1164. UnitInfo *uip = &mountinfo.ui[0];
  1165. cd_unit_offset = MAX_FILESYSTEM_UNITS;
  1166. autocreatedunit = 0;
  1167. for (nr = 0; nr < currprefs.mountitems; nr++) {
  1168. struct uaedev_config_data *uci = &currprefs.mountconfig[nr];
  1169. if (uci->ci.controller_type == HD_CONTROLLER_TYPE_UAE && (uci->ci.type == UAEDEV_DIR || uci->ci.type == UAEDEV_HDF)) {
  1170. struct uaedev_config_info ci;
  1171. memcpy (&ci, &uci->ci, sizeof (struct uaedev_config_info));
  1172. ci.flags = MYVOLUMEINFO_REUSABLE;
  1173. int idx = set_filesys_unit_1 (-1, &ci, false);
  1174. allocuci (&currprefs, nr, idx);
  1175. }
  1176. }
  1177. filesys_addexternals ();
  1178. nr = nr_units ();
  1179. cd_unit_offset = nr;
  1180. cd_unit_number = 0;
  1181. if (currprefs.scsi && currprefs.win32_automount_cddrives) {
  1182. uae_u32 mask = scsi_get_cd_drive_mask ();
  1183. for (int i = 0; i < 32; i++) {
  1184. if (mask & (1 << i)) {
  1185. struct uaedev_config_info ci = { 0 };
  1186. _stprintf (ci.devname, _T("CD%d"), i);
  1187. cd_unit_number++;
  1188. _tcscpy (ci.rootdir, _T("/"));
  1189. ci.readonly = true;
  1190. ci.sectors = 1;
  1191. ci.surfaces = 1;
  1192. ci.blocksize = 2048;
  1193. int idx = set_filesys_unit_1 (i + cd_unit_offset, &ci, true);
  1194. allocuci (&currprefs, nr, idx);
  1195. nr++;
  1196. }
  1197. }
  1198. }
  1199. for (nr = 0; nr < currprefs.mountitems; nr++) {
  1200. struct uaedev_config_data *uci = &currprefs.mountconfig[nr];
  1201. if (uci->ci.controller_type == HD_CONTROLLER_TYPE_UAE) {
  1202. if (uci->ci.type == UAEDEV_TAPE) {
  1203. struct uaedev_config_info ci;
  1204. memcpy (&ci, &uci->ci, sizeof (struct uaedev_config_info));
  1205. int unitnum = scsi_add_tape (&uci->ci);
  1206. if (unitnum >= 0) {
  1207. int idx = set_filesys_unit_1 (-1, &ci, false);
  1208. allocuci (&currprefs, nr, idx, unitnum);
  1209. }
  1210. }
  1211. }
  1212. }
  1213. // init all controllers first
  1214. add_cpuboard_unit_init();
  1215. for (int i = 0; expansionroms[i].name; i++) {
  1216. const struct expansionromtype *ert = &expansionroms[i];
  1217. for (int j = 0; j < MAX_DUPLICATE_EXPANSION_BOARDS; j++) {
  1218. struct romconfig *rc = get_device_romconfig(&currprefs, ert->romtype, j);
  1219. if ((ert->deviceflags & 3) && rc) {
  1220. if (ert->add) {
  1221. struct uaedev_config_info ci = { 0 };
  1222. ci.controller_type_unit = j;
  1223. ert->add(-1, &ci, rc);
  1224. }
  1225. }
  1226. }
  1227. }
  1228. for (nr = 0; nr < currprefs.mountitems; nr++) {
  1229. struct uaedev_config_info *uci = &currprefs.mountconfig[nr].ci;
  1230. int type = uci->controller_type;
  1231. int unit = uci->controller_unit;
  1232. bool added = false;
  1233. uci->uae_unitnum = nr;
  1234. if (type == HD_CONTROLLER_TYPE_UAE) {
  1235. continue;
  1236. } else if (type != HD_CONTROLLER_TYPE_IDE_AUTO && type >= HD_CONTROLLER_TYPE_IDE_FIRST && type <= HD_CONTROLLER_TYPE_IDE_LAST) {
  1237. added = add_ide_unit(type, unit, uci);
  1238. } else if (type == HD_CONTROLLER_TYPE_IDE_AUTO) {
  1239. for (int st = HD_CONTROLLER_TYPE_IDE_FIRST; st <= HD_CONTROLLER_TYPE_IDE_LAST; st++) {
  1240. added = add_ide_unit(st, unit, uci);
  1241. if (added)
  1242. break;
  1243. }
  1244. } else if (type != HD_CONTROLLER_TYPE_SCSI_AUTO && type >= HD_CONTROLLER_TYPE_SCSI_FIRST && type <= HD_CONTROLLER_TYPE_SCSI_LAST) {
  1245. added = add_scsi_unit(type, unit, uci);
  1246. } else if (type == HD_CONTROLLER_TYPE_SCSI_AUTO) {
  1247. for (int st = HD_CONTROLLER_TYPE_SCSI_FIRST; st <= HD_CONTROLLER_TYPE_SCSI_LAST; st++) {
  1248. added = add_scsi_unit(st, unit, uci);
  1249. if (added)
  1250. break;
  1251. }
  1252. #if 0
  1253. } else if (type == HD_CONTROLLER_TYPE_PCMCIA) {
  1254. if (uci->controller_type_unit == 0) {
  1255. gayle_add_pcmcia_sram_unit (uci);
  1256. added = true;
  1257. } else {
  1258. gayle_add_pcmcia_ide_unit (uci);
  1259. added = true;
  1260. }
  1261. #endif
  1262. }
  1263. if (added)
  1264. allocuci (&currprefs, nr, -1);
  1265. }
  1266. }
  1267. int sprintf_filesys_unit (TCHAR *buffer, int num)
  1268. {
  1269. UnitInfo *uip = mountinfo.ui;
  1270. if (uip[num].volname != 0)
  1271. _stprintf (buffer, _T("(DH%d:) Filesystem, %s: %s %s"), num, uip[num].volname,
  1272. uip[num].rootdir, uip[num].readonly ? _T("ro") : _T(""));
  1273. else
  1274. _stprintf (buffer, _T("(DH%d:) Hardfile, \"%s\", size %d Mbytes"), num,
  1275. uip[num].rootdir, (int)(uip[num].hf.virtsize / (1024 * 1024)));
  1276. return 0;
  1277. }
  1278. static void free_mountinfo (void)
  1279. {
  1280. int i;
  1281. for (i = 0; i < MAX_FILESYSTEM_UNITS; i++)
  1282. close_filesys_unit (mountinfo.ui + i);
  1283. gayle_free_units ();
  1284. }
  1285. struct hardfiledata *get_hardfile_data_controller(int nr)
  1286. {
  1287. UnitInfo *uip = mountinfo.ui;
  1288. for (int i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  1289. if (uip[i].open == 0)
  1290. continue;
  1291. if (uip[i].hf.ci.controller_unit == nr && uip[i].hf.ci.type != UAEDEV_DIR)
  1292. return &uip[i].hf;
  1293. }
  1294. for (int i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  1295. if (uip[i].open == 0)
  1296. continue;
  1297. if (uip[i].hf.ci.controller_unit == nr && uip[i].hf.ci.type == UAEDEV_DIR)
  1298. return &uip[i].hf;
  1299. }
  1300. return NULL;
  1301. }
  1302. struct hardfiledata *get_hardfile_data (int nr)
  1303. {
  1304. UnitInfo *uip = mountinfo.ui;
  1305. if (nr < 0 || nr >= MAX_FILESYSTEM_UNITS || uip[nr].open == 0 || is_virtual (nr))
  1306. return NULL;
  1307. return &uip[nr].hf;
  1308. }
  1309. static uae_u32 a_uniq, key_uniq;
  1310. #define PUT_PCK_RES1(p,v) do { put_long_host((p)->packet_data + dp_Res1, (v)); } while (0)
  1311. #define PUT_PCK_RES2(p,v) do { put_long_host((p)->packet_data + dp_Res2, (v)); } while (0)
  1312. #define GET_PCK_TYPE(p) ((uae_s32)(get_long_host((p)->packet_data + dp_Type)))
  1313. #define GET_PCK_RES1(p) ((uae_s32)(get_long_host((p)->packet_data + dp_Res1)))
  1314. #define GET_PCK_RES2(p) ((uae_s32)(get_long_host((p)->packet_data + dp_Res2)))
  1315. #define GET_PCK_ARG1(p) ((uae_s32)(get_long_host((p)->packet_data + dp_Arg1)))
  1316. #define GET_PCK_ARG2(p) ((uae_s32)(get_long_host((p)->packet_data + dp_Arg2)))
  1317. #define GET_PCK_ARG3(p) ((uae_s32)(get_long_host((p)->packet_data + dp_Arg3)))
  1318. #define GET_PCK_ARG4(p) ((uae_s32)(get_long_host((p)->packet_data + dp_Arg4)))
  1319. #define GET_PCK_ARG5(p) ((uae_s32)(get_long_host((p)->packet_data + dp_Arg5)))
  1320. #define PUT_PCK64_RES0(p,v) do { put_long_host((p)->packet_data + dp64_Res0, (v)); } while (0)
  1321. #define PUT_PCK64_RES1(p,v) do { put_long_host((p)->packet_data + dp64_Res1, (((uae_u64)v) >> 32)); put_long_host((p)->packet_data + dp64_Res1 + 4, ((uae_u32)v)); } while (0)
  1322. #define PUT_PCK64_RES2(p,v) do { put_long_host((p)->packet_data + dp64_Res2, (v)); } while (0)
  1323. #define GET_PCK64_TYPE(p) ((uae_s32)(get_long_host((p)->packet_data + dp64_Type)))
  1324. #define GET_PCK64_RES0(p) ((uae_s32)(get_long_host((p)->packet_data + dp64_Res0)))
  1325. #define GET_PCK64_RES1(p) ( (((uae_s64)(get_long_host((p)->packet_data + dp64_Res1))) << 32) | (((uae_s64)(get_long_host((p)->packet_data + dp64_Res1 + 4))) << 0) )
  1326. #define GET_PCK64_ARG1(p) ((uae_s32)(get_long_host((p)->packet_data + dp64_Arg1)))
  1327. #define GET_PCK64_ARG2(p) ( (((uae_s64)(get_long_host((p)->packet_data + dp64_Arg2))) << 32) | (((uae_s64)(get_long_host((p)->packet_data + dp64_Arg2 + 4))) << 0) )
  1328. #define GET_PCK64_ARG3(p) ((uae_s32)(get_long_host((p)->packet_data + dp64_Arg3)))
  1329. #define GET_PCK64_ARG4(p) ((uae_s32)(get_long_host((p)->packet_data + dp64_Arg4)))
  1330. #define GET_PCK64_ARG5(p) ( (((uae_s64)(get_long_host((p)->packet_data + dp64_Arg5))) << 32) | (((uae_s64)(get_long_host((p)->packet_data + dp64_Arg5 + 4))) << 0) )
  1331. static void readdpacket(TrapContext *ctx, dpacket *packet, uaecptr pck)
  1332. {
  1333. // Read enough to get also all 64-bit fields
  1334. packet->packet_addr = pck;
  1335. if (trap_is_indirect() || !valid_address(pck, dp_Max)) {
  1336. trap_get_bytes(ctx, packet->packet_array, pck, dp_Max);
  1337. packet->packet_data = packet->packet_array;
  1338. packet->need_flush = true;
  1339. } else {
  1340. packet->packet_data = get_real_address(pck);
  1341. packet->need_flush = false;
  1342. }
  1343. }
  1344. static void writedpacket(TrapContext *ctx, dpacket *packet)
  1345. {
  1346. if (!packet->need_flush)
  1347. return;
  1348. int type = GET_PCK_TYPE(packet);
  1349. if (type >= 8000 && type < 9000 && GET_PCK64_RES0(packet) == DP64_INIT) {
  1350. // 64-bit RESx fields
  1351. trap_put_bytes(ctx, packet->packet_data + 12, packet->packet_addr + 12, 32 - 12);
  1352. } else {
  1353. // dp_Res1 and dp_Res2
  1354. trap_put_bytes(ctx, packet->packet_data + 12, packet->packet_addr + 12, 20 - 12);
  1355. }
  1356. }
  1357. static void set_quadp(TrapContext *ctx, uaecptr p, uae_s64 v)
  1358. {
  1359. if (!trap_valid_address(ctx, p, 8))
  1360. return;
  1361. trap_put_quad(ctx, p, v);
  1362. }
  1363. static uae_u64 get_quadp(TrapContext *ctx, uaecptr p)
  1364. {
  1365. if (!trap_valid_address(ctx, p, 8))
  1366. return 0;
  1367. return trap_get_quad(ctx, p);
  1368. }
  1369. static int flush_cache (Unit *unit, int num);
  1370. static TCHAR *char1(TrapContext *ctx, uaecptr addr)
  1371. {
  1372. uae_char buf[1024];
  1373. #if TRAPMD
  1374. trap_get_string(ctx, (uae_u8*)buf, addr, sizeof(buf));
  1375. #else
  1376. unsigned int i = 0;
  1377. do {
  1378. buf[i] = trap_get_byte(ctx, addr);
  1379. addr++;
  1380. } while (buf[i++] && i < sizeof (buf));
  1381. #endif
  1382. return au_fs(buf);
  1383. }
  1384. static TCHAR *bstr1(TrapContext *ctx, uaecptr addr)
  1385. {
  1386. uae_char buf[257];
  1387. int n = trap_get_byte(ctx, addr);
  1388. addr++;
  1389. #if TRAPMD
  1390. trap_get_bytes(ctx, (uae_u8*)buf, addr, n);
  1391. #else
  1392. for (int i = 0; i < n; i++, addr++)
  1393. buf[i] = trap_get_byte(ctx, addr);
  1394. #endif
  1395. buf[n] = 0;
  1396. return au_fs(buf);
  1397. }
  1398. static TCHAR *bstr(TrapContext *ctx, Unit *unit, uaecptr addr)
  1399. {
  1400. int n = trap_get_byte(ctx, addr);
  1401. uae_char buf[257];
  1402. addr++;
  1403. #if TRAPMD
  1404. trap_get_bytes(ctx, (uae_u8*)buf, addr, n);
  1405. #else
  1406. for (int i = 0; i < n; i++, addr++)
  1407. buf[i] = trap_get_byte(ctx, addr);
  1408. #endif
  1409. buf[n] = 0;
  1410. au_fs_copy (unit->tmpbuf3, sizeof (unit->tmpbuf3) / sizeof (TCHAR), buf);
  1411. return unit->tmpbuf3;
  1412. }
  1413. static TCHAR *cstr(TrapContext *ctx, Unit *unit, uaecptr addr)
  1414. {
  1415. uae_char buf[257];
  1416. trap_get_string(ctx, buf, addr, sizeof buf);
  1417. au_fs_copy (unit->tmpbuf3, sizeof (unit->tmpbuf3) / sizeof (TCHAR), buf);
  1418. return unit->tmpbuf3;
  1419. }
  1420. static TCHAR *bstr_cut(TrapContext *ctx, Unit *unit, uaecptr addr)
  1421. {
  1422. TCHAR *p = unit->tmpbuf3;
  1423. int i, colon_seen = 0, off;
  1424. int n = trap_get_byte(ctx, addr);
  1425. uae_char buf[257];
  1426. off = 0;
  1427. addr++;
  1428. for (i = 0; i < n; i++, addr++) {
  1429. uae_u8 c = trap_get_byte(ctx, addr);
  1430. buf[i] = c;
  1431. if (c == '/' || (c == ':' && colon_seen++ == 0))
  1432. off = i + 1;
  1433. }
  1434. buf[i] = 0;
  1435. au_fs_copy (unit->tmpbuf3, sizeof (unit->tmpbuf3) / sizeof (TCHAR), buf);
  1436. return &p[off];
  1437. }
  1438. /* convert time_t to/from AmigaDOS time */
  1439. static const uae_s64 msecs_per_day = 24 * 60 * 60 * 1000;
  1440. static const uae_s64 diff = ((8 * 365 + 2) * (24 * 60 * 60)) * (uae_u64)1000;
  1441. void timeval_to_amiga (struct mytimeval *tv, int *days, int *mins, int *ticks, int tickcount)
  1442. {
  1443. /* tv.tv_sec is secs since 1-1-1970 */
  1444. /* days since 1-1-1978 */
  1445. /* mins since midnight */
  1446. /* ticks past minute @ 50Hz */
  1447. uae_s64 t = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  1448. t -= diff;
  1449. if (t < 0)
  1450. t = 0;
  1451. *days = t / msecs_per_day;
  1452. t -= *days * msecs_per_day;
  1453. *mins = t / (60 * 1000);
  1454. t -= *mins * (60 * 1000);
  1455. *ticks = t / (1000 / tickcount);
  1456. }
  1457. void amiga_to_timeval (struct mytimeval *tv, int days, int mins, int ticks, int tickcount)
  1458. {
  1459. uae_s64 t;
  1460. if (days < 0)
  1461. days = 0;
  1462. if (days > 9900 * 365)
  1463. days = 9900 * 365; // in future far enough?
  1464. if (mins < 0 || mins >= 24 * 60)
  1465. mins = 0;
  1466. if (ticks < 0 || ticks >= 60 * tickcount)
  1467. ticks = 0;
  1468. t = ticks * 20;
  1469. t += mins * (60 * 1000);
  1470. t += ((uae_u64)days) * msecs_per_day;
  1471. t += diff;
  1472. tv->tv_sec = t / 1000;
  1473. tv->tv_usec = (t % 1000) * 1000;
  1474. }
  1475. static Unit *units = 0;
  1476. static Unit *find_unit (uaecptr port)
  1477. {
  1478. Unit* u;
  1479. for (u = units; u; u = u->next)
  1480. if (u->port == port)
  1481. break;
  1482. return u;
  1483. }
  1484. static struct fs_dirhandle *fs_opendir (Unit *u, a_inode *aino)
  1485. {
  1486. struct fs_dirhandle *fsd = xmalloc (struct fs_dirhandle, 1);
  1487. fsd->fstype = (u->volflags & MYVOLUMEINFO_ARCHIVE) ? FS_ARCHIVE : ((u->volflags & MYVOLUMEINFO_CDFS) ? FS_CDFS : FS_DIRECTORY);
  1488. if (fsd->fstype == FS_ARCHIVE) {
  1489. fsd->zd = zfile_opendir_archive (aino->nname);
  1490. if (fsd->zd)
  1491. return fsd;
  1492. } else if (fsd->fstype == FS_DIRECTORY) {
  1493. fsd->od = my_opendir (aino->nname);
  1494. if (fsd->od)
  1495. return fsd;
  1496. } else if (fsd->fstype == FS_CDFS) {
  1497. fsd->isod = isofs_opendir (u->ui.cdfs_superblock, aino->uniq_external);
  1498. if (fsd->isod)
  1499. return fsd;
  1500. }
  1501. xfree (fsd);
  1502. return NULL;
  1503. }
  1504. static void fs_closedir (struct fs_dirhandle *fsd)
  1505. {
  1506. if (!fsd)
  1507. return;
  1508. if (fsd->fstype == FS_ARCHIVE)
  1509. zfile_closedir_archive (fsd->zd);
  1510. else if (fsd->fstype == FS_DIRECTORY)
  1511. my_closedir (fsd->od);
  1512. else if (fsd->fstype == FS_CDFS)
  1513. isofs_closedir (fsd->isod);
  1514. xfree (fsd);
  1515. }
  1516. static struct fs_filehandle *fs_openfile (Unit *u, a_inode *aino, int flags)
  1517. {
  1518. struct fs_filehandle *fsf = xmalloc (struct fs_filehandle, 1);
  1519. fsf->fstype = (u->volflags & MYVOLUMEINFO_ARCHIVE) ? FS_ARCHIVE : ((u->volflags & MYVOLUMEINFO_CDFS) ? FS_CDFS : FS_DIRECTORY);
  1520. if (fsf->fstype == FS_ARCHIVE) {
  1521. fsf->zf = zfile_open_archive (aino->nname, flags);
  1522. if (fsf->zf)
  1523. return fsf;
  1524. } else if (fsf->fstype == FS_DIRECTORY) {
  1525. fsf->of = my_open (aino->nname, flags);
  1526. if (fsf->of)
  1527. return fsf;
  1528. } else if (fsf->fstype == FS_CDFS) {
  1529. fsf->isof = isofs_openfile (u->ui.cdfs_superblock, aino->uniq_external, flags);
  1530. if (fsf->isof)
  1531. return fsf;
  1532. }
  1533. xfree (fsf);
  1534. return NULL;
  1535. }
  1536. static void fs_closefile (struct fs_filehandle *fsf)
  1537. {
  1538. if (!fsf)
  1539. return;
  1540. if (fsf->fstype == FS_ARCHIVE) {
  1541. zfile_close_archive (fsf->zf);
  1542. } else if (fsf->fstype == FS_DIRECTORY) {
  1543. my_close (fsf->of);
  1544. } else if (fsf->fstype == FS_CDFS) {
  1545. isofs_closefile (fsf->isof);
  1546. }
  1547. xfree (fsf);
  1548. }
  1549. static unsigned int fs_read (struct fs_filehandle *fsf, void *b, unsigned int size)
  1550. {
  1551. if (fsf->fstype == FS_ARCHIVE)
  1552. return zfile_read_archive (fsf->zf, b, size);
  1553. else if (fsf->fstype == FS_DIRECTORY)
  1554. return my_read (fsf->of, b, size);
  1555. else if (fsf->fstype == FS_CDFS)
  1556. return isofs_read (fsf->isof, b, size);
  1557. return 0;
  1558. }
  1559. static unsigned int fs_write (struct fs_filehandle *fsf, void *b, unsigned int size)
  1560. {
  1561. if (fsf->fstype == FS_DIRECTORY)
  1562. return my_write (fsf->of, b, size);
  1563. return 0;
  1564. }
  1565. /* return value = old position. -1 = error. */
  1566. static uae_s64 fs_lseek64 (struct fs_filehandle *fsf, uae_s64 offset, int whence)
  1567. {
  1568. if (fsf->fstype == FS_ARCHIVE)
  1569. return zfile_lseek_archive (fsf->zf, offset, whence);
  1570. else if (fsf->fstype == FS_DIRECTORY)
  1571. return my_lseek (fsf->of, offset, whence);
  1572. else if (fsf->fstype == FS_CDFS)
  1573. return isofs_lseek (fsf->isof, offset, whence);
  1574. return -1;
  1575. }
  1576. static uae_s32 fs_lseek (struct fs_filehandle *fsf, uae_s32 offset, int whence)
  1577. {
  1578. uae_s64 v = fs_lseek64 (fsf, offset, whence);
  1579. if (v < 0 || v > 0x7fffffff)
  1580. return -1;
  1581. return (uae_s32)v;
  1582. }
  1583. static uae_s64 fs_fsize64 (struct fs_filehandle *fsf)
  1584. {
  1585. if (fsf->fstype == FS_ARCHIVE)
  1586. return zfile_fsize_archive (fsf->zf);
  1587. else if (fsf->fstype == FS_DIRECTORY)
  1588. return my_fsize (fsf->of);
  1589. else if (fsf->fstype == FS_CDFS)
  1590. return isofs_fsize (fsf->isof);
  1591. return -1;
  1592. }
  1593. static uae_u32 fs_fsize (struct fs_filehandle *fsf)
  1594. {
  1595. return (uae_u32)fs_fsize64 (fsf);
  1596. }
  1597. static uae_s64 key_filesize(Key *k)
  1598. {
  1599. if (k->aino->vfso)
  1600. return k->aino->vfso->size;
  1601. return fs_fsize64 (k->fd);
  1602. }
  1603. static uae_s64 key_seek(Key *k, uae_s64 offset, int whence)
  1604. {
  1605. if (k->aino->vfso)
  1606. return k->file_pos;
  1607. return fs_lseek64 (k->fd, offset, whence);
  1608. }
  1609. static void set_highcyl(uaecptr volume, uae_u32 blocks)
  1610. {
  1611. put_long(volume + 184 - 32, blocks);
  1612. }
  1613. static void set_volume_name(Unit *unit, struct mytimeval *tv)
  1614. {
  1615. int namelen;
  1616. int i;
  1617. char *s;
  1618. s = ua_fs (unit->ui.volname, -1);
  1619. namelen = strlen (s);
  1620. if (namelen >= 58)
  1621. namelen = 58;
  1622. put_byte(unit->volume + 64, namelen);
  1623. for (i = 0; i < namelen; i++)
  1624. put_byte(unit->volume + 64 + 1 + i, s[i]);
  1625. put_byte(unit->volume + 64 + 1 + namelen, 0);
  1626. if (tv && (tv->tv_sec || tv->tv_usec)) {
  1627. int days, mins, ticks;
  1628. timeval_to_amiga (tv, &days, &mins, &ticks, 50);
  1629. put_long(unit->volume + 16, days);
  1630. put_long(unit->volume + 20, mins);
  1631. put_long(unit->volume + 24, ticks);
  1632. }
  1633. xfree (s);
  1634. unit->rootnode.aname = unit->ui.volname;
  1635. unit->rootnode.nname = unit->ui.rootdir;
  1636. unit->rootnode.mountcount = unit->mountcount;
  1637. }
  1638. static int filesys_isvolume(Unit *unit)
  1639. {
  1640. if (!unit->volume)
  1641. return 0;
  1642. return get_byte(unit->volume + 64) || unit->ui.unknown_media;
  1643. }
  1644. static void clear_exkeys (Unit *unit)
  1645. {
  1646. int i;
  1647. a_inode *a;
  1648. for (i = 0; i < EXKEYS; i++) {
  1649. unit->examine_keys[i].aino = 0;
  1650. unit->examine_keys[i].curr_file = 0;
  1651. unit->examine_keys[i].uniq = 0;
  1652. }
  1653. for (i = 0; i < EXALLKEYS; i++) {
  1654. fs_closedir (unit->exalls[i].dirhandle);
  1655. unit->exalls[i].dirhandle = NULL;
  1656. xfree (unit->exalls[i].fn);
  1657. unit->exalls[i].fn = NULL;
  1658. unit->exalls[i].id = 0;
  1659. }
  1660. unit->exallid = 0;
  1661. unit->next_exkey = 1;
  1662. a = &unit->rootnode;
  1663. while (a) {
  1664. a->exnext_count = 0;
  1665. if (a->locked_children) {
  1666. a->locked_children = 0;
  1667. unit->total_locked_ainos--;
  1668. }
  1669. a = a->next;
  1670. if (a == &unit->rootnode)
  1671. break;
  1672. }
  1673. }
  1674. static void filesys_delayed_eject(Unit *u)
  1675. {
  1676. if (u->reinsertdelay)
  1677. return;
  1678. u->reinsertdelay = 50;
  1679. u->newreadonly = false;
  1680. xfree(u->newrootdir);
  1681. u->newrootdir = NULL;
  1682. write_log(_T("FILESYS: delayed eject %d\n"), u->unit);
  1683. }
  1684. static void filesys_delayed_change (Unit *u, int frames, const TCHAR *rootdir, const TCHAR *volume, bool readonly, int flags)
  1685. {
  1686. if (u->reinsertdelay)
  1687. return;
  1688. u->reinsertdelay = frames;
  1689. u->newflags = flags;
  1690. u->newreadonly = readonly;
  1691. u->newrootdir = my_strdup (rootdir);
  1692. if (volume)
  1693. u->newvolume = my_strdup (volume);
  1694. filesys_eject (u->unit);
  1695. if (!rootdir || _tcslen (rootdir) == 0)
  1696. u->reinsertdelay = 0;
  1697. if (u->reinsertdelay > 0)
  1698. write_log (_T("FILESYS: delayed insert %d: '%s' ('%s')\n"), u->unit, volume ? volume : _T("<none>"), rootdir);
  1699. }
  1700. int filesys_eject(int nr)
  1701. {
  1702. UnitInfo *ui = &mountinfo.ui[nr];
  1703. Unit *u = ui->self;
  1704. if (!mountertask || u->mount_changed)
  1705. return 0;
  1706. if (ui->open <= 0 || u == NULL)
  1707. return 0;
  1708. if (!is_virtual (nr))
  1709. return 0;
  1710. if (!filesys_isvolume(u))
  1711. return 0;
  1712. u->mount_changed = -1;
  1713. u->mountcount++;
  1714. write_log(_T("FILESYS: volume '%s' removal request\n"), u->ui.volname);
  1715. // -1 = remove, -2 = remove + remove device node
  1716. put_byte(u->volume + 172 - 32, ui->unit_type == UNIT_CDFS ? -1 : -2);
  1717. uae_Signal(get_long(u->volume + 176 - 32), 1 << 13);
  1718. return 1;
  1719. }
  1720. static uae_u32 heartbeat;
  1721. static int heartbeat_count, heartbeat_count_cont;
  1722. static int heartbeat_task;
  1723. bool filesys_heartbeat(void)
  1724. {
  1725. return heartbeat_count_cont > 0;
  1726. }
  1727. // This uses filesystem process to reduce resource usage
  1728. void setsystime (void)
  1729. {
  1730. write_log("SETSYSTIME\n");
  1731. if (!currprefs.tod_hack || !rtarea_bank.baseaddr)
  1732. return;
  1733. write_log("SETSYSTIME2\n");
  1734. heartbeat = get_long_host(rtarea_bank.baseaddr + RTAREA_HEARTBEAT);
  1735. heartbeat_task = 1;
  1736. heartbeat_count = 10;
  1737. }
  1738. static void setsystime_vblank (void)
  1739. {
  1740. Unit *u;
  1741. TrapContext *ctx = NULL;
  1742. for (u = units; u; u = u->next) {
  1743. if (is_virtual(u->unit) && filesys_isvolume(u)) {
  1744. put_byte(u->volume + 173 - 32, get_byte(u->volume + 173 - 32) | 1);
  1745. uae_Signal(get_long(u->volume + 176 - 32), 1 << 13);
  1746. break;
  1747. }
  1748. }
  1749. }
  1750. static uae_u32 REGPARAM2 debugger_helper(TrapContext *ctx)
  1751. {
  1752. int mode = trap_get_dreg(ctx, 1);
  1753. switch (mode)
  1754. {
  1755. case 1:
  1756. // Execute debugger_boot() to get here.
  1757. write_log(_T("debugger #1\n"));
  1758. // return RunCommand(() parameters
  1759. // does nothing if D1 == 0.
  1760. break;
  1761. case 2:
  1762. // called when RunCommand() returns
  1763. // D0 = RunCommand() return code.
  1764. write_log(_T("debugger #2\n"));
  1765. break;
  1766. default:
  1767. write_log(_T("Unknown debugger hook %d\n"), mode);
  1768. return 0;
  1769. }
  1770. return 1;
  1771. }
  1772. static void debugger_boot(void)
  1773. {
  1774. Unit *u;
  1775. TrapContext *ctx = NULL;
  1776. for (u = units; u; u = u->next) {
  1777. if (is_virtual(u->unit) && filesys_isvolume(u)) {
  1778. put_byte(u->volume + 173 - 32, get_byte(u->volume + 173 - 32) | 2);
  1779. uae_Signal(get_long(u->volume + 176 - 32), 1 << 13);
  1780. break;
  1781. }
  1782. }
  1783. }
  1784. int filesys_insert (int nr, const TCHAR *volume, const TCHAR *rootdir, bool readonly, int flags)
  1785. {
  1786. UnitInfo *ui;
  1787. Unit *u;
  1788. TrapContext *ctx = NULL;
  1789. if (!mountertask)
  1790. return 0;
  1791. write_log (_T("filesys_insert(%d,'%s','%s','%d','%d)\n"), nr, volume ? volume : _T("<?>"), rootdir, readonly, flags);
  1792. if (nr < 0) {
  1793. for (u = units; u; u = u->next) {
  1794. if (is_virtual (u->unit)) {
  1795. if (!filesys_isvolume(u) && mountinfo.ui[u->unit].canremove > 0)
  1796. break;
  1797. }
  1798. }
  1799. if (!u) {
  1800. for (u = units; u; u = u->next) {
  1801. if (is_virtual (u->unit)) {
  1802. if (mountinfo.ui[u->unit].canremove > 0)
  1803. break;
  1804. }
  1805. }
  1806. }
  1807. if (!u)
  1808. return 0;
  1809. nr = u->unit;
  1810. ui = &mountinfo.ui[nr];
  1811. } else {
  1812. ui = &mountinfo.ui[nr];
  1813. u = ui->self;
  1814. }
  1815. if (ui->open <= 0 || u == NULL)
  1816. return 0;
  1817. if (u->reinsertdelay)
  1818. return -1;
  1819. if (!is_virtual(nr))
  1820. return 0;
  1821. if (filesys_isvolume(u)) {
  1822. filesys_delayed_change (u, 10, rootdir, volume, readonly, flags);
  1823. return 1 + nr;
  1824. }
  1825. u->mountcount++;
  1826. u->mount_changed = 1;
  1827. u->mount_volume = volume ? my_strdup (volume) : NULL;
  1828. u->mount_rootdir = my_strdup (rootdir);
  1829. u->mount_readonly = readonly;
  1830. u->mount_flags = flags;
  1831. write_log (_T("filesys_insert %d done!\n"), nr);
  1832. put_byte(u->volume + 172 - 32, -3); // wait for insert
  1833. uae_Signal (get_long(u->volume + 176 - 32), 1 << 13);
  1834. return 100 + nr;
  1835. }
  1836. static uae_u32 filesys_media_change_reply (int mode)
  1837. {
  1838. int nr;
  1839. UnitInfo *ui = NULL;
  1840. Unit *u;
  1841. for (nr = 0; nr < MAX_FILESYSTEM_UNITS; nr++) {
  1842. ui = &mountinfo.ui[nr];
  1843. u = ui->self;
  1844. if (u && u->mount_changed)
  1845. break;
  1846. }
  1847. if (nr >= MAX_FILESYSTEM_UNITS) {
  1848. write_log (_T("FILESYS: filesys_media_change_reply without mount_changed flag!?\n"));
  1849. return 0;
  1850. }
  1851. if (u->mount_changed < 0) {
  1852. // eject
  1853. if (mode == 0) {
  1854. write_log (_T("FILESYS: got media change reply, '%s' removal finished\n"), u->ui.volname);
  1855. flush_cache (u, -1);
  1856. isofs_unmount (u->ui.cdfs_superblock);
  1857. ui->cdfs_superblock = u->ui.cdfs_superblock = NULL;
  1858. zfile_fclose_archive (ui->zarchive);
  1859. ui->zarchive = NULL;
  1860. u->ui.unknown_media = false;
  1861. #ifdef RETROPLATFORM
  1862. if (ui->unit_type == UNIT_CDFS)
  1863. rp_cd_image_change (ui->cddevno, NULL);
  1864. else
  1865. rp_harddrive_image_change (nr, false, NULL);
  1866. #endif
  1867. } else {
  1868. u->mount_changed = 0;
  1869. }
  1870. return 1;
  1871. } else if (u->mount_changed > 0) {
  1872. if (mode == 0) {
  1873. // insert
  1874. struct mytimeval ctime = { 0 };
  1875. bool emptydrive = false;
  1876. struct uaedev_config_data *uci = NULL;
  1877. clear_exkeys (u);
  1878. xfree (u->ui.rootdir);
  1879. ui->rootdir = u->ui.rootdir = my_strdup (u->mount_rootdir);
  1880. flush_cache (u, -1);
  1881. xfree (u->ui.volname);
  1882. ui->volname = u->ui.volname = NULL;
  1883. if (ui->unit_type == UNIT_CDFS) {
  1884. uae_u64 uniq;
  1885. ui->cdfs_superblock = u->ui.cdfs_superblock = isofs_mount (ui->cddevno, &uniq);
  1886. u->rootnode.uniq_external = uniq;
  1887. u->ui.unknown_media = true;
  1888. if (!u->ui.cdfs_superblock)
  1889. return 0;
  1890. struct isofs_info ii;
  1891. set_highcyl(u->volume, 0);
  1892. bool r = isofs_mediainfo (ui->cdfs_superblock, &ii);
  1893. if (r && ii.media) {
  1894. u->ui.unknown_media = ii.unknown_media;
  1895. if (!ii.unknown_media) {
  1896. u->ui.volname = ui->volname = my_strdup (ii.volumename);
  1897. ctime.tv_sec = ii.creation;
  1898. ctime.tv_usec = 0;
  1899. set_highcyl(u->volume, ii.blocks);
  1900. #ifdef RETROPLATFORM
  1901. rp_cd_image_change (ui->cddevno, ii.devname);
  1902. #endif
  1903. }
  1904. }
  1905. } else {
  1906. if (set_filesys_volume (u->mount_rootdir, &u->mount_flags, &u->mount_readonly, &emptydrive, &ui->zarchive) < 0)
  1907. return 0;
  1908. if (emptydrive)
  1909. return 0;
  1910. xfree (u->ui.volname);
  1911. ui->volname = u->ui.volname = filesys_createvolname (u->mount_volume, u->mount_rootdir, ui->zarchive, _T("removable"));
  1912. #ifdef RETROPLATFORM
  1913. rp_harddrive_image_change (nr, u->mount_readonly, u->mount_rootdir);
  1914. #endif
  1915. uci = getuci (currprefs.mountconfig, nr);
  1916. }
  1917. if (u->ui.unknown_media) {
  1918. write_log (_T("FILESYS: inserted unreadable volume NR=%d RO=%d\n"), nr, u->mount_readonly);
  1919. } else {
  1920. write_log (_T("FILESYS: inserted volume NR=%d RO=%d '%s' ('%s')\n"), nr, u->mount_readonly, ui->volname, u->mount_rootdir);
  1921. set_volume_name(u, &ctime);
  1922. if (u->mount_flags >= 0)
  1923. ui->volflags = u->volflags = u->ui.volflags = u->mount_flags;
  1924. if (uci != NULL) {
  1925. _tcscpy (uci->ci.volname, ui->volname);
  1926. _tcscpy (uci->ci.rootdir, u->mount_rootdir);
  1927. }
  1928. if (u->mount_flags >= 0) {
  1929. ui->readonly = u->ui.readonly = u->mount_readonly;
  1930. if (uci != NULL)
  1931. uci->ci.readonly = u->mount_readonly;
  1932. }
  1933. put_byte(u->volume + 64, 0);
  1934. put_byte(u->volume + 172 - 32, 1);
  1935. }
  1936. xfree (u->mount_volume);
  1937. xfree (u->mount_rootdir);
  1938. u->mount_rootdir = NULL;
  1939. u->mount_volume = NULL;
  1940. } else {
  1941. u->mount_changed = 0;
  1942. }
  1943. return 1;
  1944. }
  1945. return 0;
  1946. }
  1947. // if multiple drag&drop mounting, queue them because mounting is async (needs Amiga-side code to run).
  1948. static int media_queued_total;
  1949. static int media_queued_cnt;
  1950. static TCHAR **media_queued_paths;
  1951. int filesys_media_change_queue(const TCHAR *rootdir, int total)
  1952. {
  1953. if (total < 0) {
  1954. media_queued_cnt = 0;
  1955. for (int i = 0; i < media_queued_total; i++) {
  1956. xfree(media_queued_paths[i]);
  1957. }
  1958. xfree(media_queued_paths);
  1959. media_queued_cnt = 0;
  1960. media_queued_total = 0;
  1961. media_queued_paths = NULL;
  1962. return 0;
  1963. }
  1964. if (total == 0) {
  1965. media_queued_cnt = 0;
  1966. return 0;
  1967. }
  1968. if (media_queued_total < total) {
  1969. media_queued_total = total;
  1970. media_queued_paths = xcalloc(TCHAR*, media_queued_total);
  1971. media_queued_cnt = 0;
  1972. }
  1973. media_queued_paths[media_queued_cnt++] = my_strdup(rootdir);
  1974. return 1;
  1975. }
  1976. int filesys_media_change (const TCHAR *rootdir, int inserted, struct uaedev_config_data *uci)
  1977. {
  1978. Unit *u;
  1979. UnitInfo *ui;
  1980. int nr = -1;
  1981. TCHAR volname[MAX_DPATH], *volptr;
  1982. TCHAR devname[MAX_DPATH];
  1983. TrapContext *ctx = NULL;
  1984. if (!mountertask)
  1985. return 0;
  1986. if (automountunit >= 0)
  1987. return -1;
  1988. write_log (_T("filesys_media_change('%s',%d,%p)\n"), rootdir, inserted, uci);
  1989. nr = -1;
  1990. for (u = units; u; u = u->next) {
  1991. if (is_virtual (u->unit)) {
  1992. ui = &mountinfo.ui[u->unit];
  1993. // inserted >= 2: drag&drop insert, do not replace existing normal drives
  1994. if (inserted < 2 && ui->rootdir && !memcmp (ui->rootdir, rootdir, _tcslen (rootdir)) && _tcslen (rootdir) + 3 >= _tcslen (ui->rootdir)) {
  1995. if (filesys_isvolume(u) && inserted) {
  1996. if (uci)ctx,
  1997. filesys_delayed_change (u, 50, rootdir, uci->ci.volname, uci->ci.readonly, 0);
  1998. return 0;
  1999. }
  2000. nr = u->unit;
  2001. break;
  2002. }
  2003. }
  2004. }
  2005. ui = NULL;
  2006. if (nr >= 0)
  2007. ui = &mountinfo.ui[nr];
  2008. /* only configured drives have automount support if automount is disabled */
  2009. if (!currprefs.win32_automount_removable && (!ui || !ui->configureddrive) && (inserted == 0 || inserted == 1))
  2010. return 0;
  2011. if (nr < 0 && !inserted)
  2012. return 0;
  2013. /* already mounted volume was ejected? */
  2014. if (nr >= 0 && !inserted)
  2015. return filesys_eject (nr);
  2016. if (inserted) {
  2017. struct uaedev_config_info ci = { 0 };
  2018. if (uci) {
  2019. volptr = my_strdup (uci->ci.volname);
  2020. } else {
  2021. struct uaedev_config_info ci2 = { 0 };
  2022. _tcscpy(ci2.rootdir, rootdir);
  2023. target_get_volume_name (&mountinfo, &ci2, 1, 0, -1);
  2024. _tcscpy(volname, ci2.volname);
  2025. volptr = volname;
  2026. if (!volname[0])
  2027. volptr = NULL;
  2028. if (ui && ui->configureddrive && ui->volname) {
  2029. volptr = volname;
  2030. _tcscpy (volptr, ui->volname);
  2031. }
  2032. }
  2033. if (!volptr) {
  2034. volptr = filesys_createvolname (NULL, rootdir, NULL, _T("removable"));
  2035. _tcscpy (volname, volptr);
  2036. xfree (volptr);
  2037. volptr = volname;
  2038. }
  2039. /* new volume inserted and it was previously mounted? */
  2040. if (nr >= 0) {
  2041. if (!filesys_isvolume(u)) /* not going to mount twice */
  2042. return filesys_insert(nr, volptr, rootdir, false, -1);
  2043. return 0;
  2044. }
  2045. if (inserted < 0) /* -1 = only mount if already exists */
  2046. return 0;
  2047. /* new volume inserted and it was not previously mounted?
  2048. * perhaps we have some empty device slots? */
  2049. nr = filesys_insert (-1, volptr, rootdir, 0, 0);
  2050. if (nr >= 100) {
  2051. if (uci)
  2052. uci->configoffset = nr - 100;
  2053. if (inserted == 3 && mountinfo.ui[nr - 100].canremove > 0)
  2054. mountinfo.ui[nr - 100].canremove = -1;
  2055. return nr;
  2056. } else if (nr > 0) {
  2057. if (inserted == 3 && mountinfo.ui[nr - 1].canremove > 0)
  2058. mountinfo.ui[nr - 1].canremove = -1;
  2059. return nr;
  2060. }
  2061. /* nope, uh, need black magic now.. */
  2062. if (uci)
  2063. _tcscpy (devname, uci->ci.devname);
  2064. else
  2065. _stprintf (devname, _T("RDH%d"), autocreatedunit++);
  2066. _tcscpy (ci.devname, devname);
  2067. _tcscpy (ci.volname, volptr);
  2068. _tcscpy (ci.rootdir, rootdir);
  2069. ci.flags = MYVOLUMEINFO_REUSABLE;
  2070. nr = add_filesys_unit (&ci, true);
  2071. if (nr < 0)
  2072. return 0;
  2073. if (inserted == 2)
  2074. mountinfo.ui[nr].canremove = 1;
  2075. if (inserted == 3)
  2076. mountinfo.ui[nr].canremove = -1;
  2077. automountunit = nr;
  2078. uae_Signal (mountertask, 1 << 13);
  2079. /* poof */
  2080. if (uci)
  2081. uci->configoffset = nr;
  2082. return 100 + nr;
  2083. }
  2084. return 0;
  2085. }
  2086. int hardfile_media_change (struct hardfiledata *hfd, struct uaedev_config_info *ci, bool inserted, bool timer)
  2087. {
  2088. if (!hfd)
  2089. return 0;
  2090. if (!timer)
  2091. hfd->reinsertdelay = 0;
  2092. if (hfd->reinsertdelay < 0) {
  2093. hfd->reinsertdelay = 0;
  2094. if (!hfd->isreinsert) {
  2095. hdf_close (hfd);
  2096. hardfile_send_disk_change (hfd, false);
  2097. if (hfd->delayedci.rootdir[0]) {
  2098. hfd->reinsertdelay = 50;
  2099. hfd->isreinsert = true;
  2100. write_log (_T("HARDFILE: delayed insert %d: '%s'\n"), hfd->unitnum, ci->rootdir[0] ? ci->rootdir : _T("<none>"));
  2101. return 0;
  2102. } else {
  2103. return 1;
  2104. }
  2105. }
  2106. memcpy (&hfd->ci, &hfd->delayedci, sizeof (struct uaedev_config_info));
  2107. if (hdf_open (hfd) <= 0) {
  2108. write_log (_T("HARDFILE: '%s' failed to open\n"), hfd->ci.rootdir);
  2109. return 0;
  2110. }
  2111. hardfile_send_disk_change (hfd, true);
  2112. return 1;
  2113. }
  2114. if (ci) {
  2115. memcpy (&hfd->delayedci, ci, sizeof (struct uaedev_config_info));
  2116. if (hfd && !hfd->drive_empty) {
  2117. hfd->reinsertdelay = 50;
  2118. hfd->isreinsert = false;
  2119. write_log (_T("HARDFILE: delayed eject %d: '%s'\n"), hfd->unitnum, hfd->ci.rootdir[0] ? hfd->ci.rootdir : _T("<none>"));
  2120. return 0;
  2121. }
  2122. if (!hfd) {
  2123. return 0;
  2124. }
  2125. hfd->reinsertdelay = 2;
  2126. hfd->isreinsert = true;
  2127. } else {
  2128. if (inserted) {
  2129. hfd->reinsertdelay = 2;
  2130. hfd->isreinsert = true;
  2131. memcpy (&hfd->delayedci, &hfd->ci, sizeof (struct uaedev_config_info));
  2132. } else {
  2133. hfd->reinsertdelay = 2;
  2134. hfd->isreinsert = false;
  2135. memcpy (&hfd->delayedci, &hfd->ci, sizeof (struct uaedev_config_info));
  2136. hfd->delayedci.rootdir[0] = 0;
  2137. }
  2138. }
  2139. return 0;
  2140. }
  2141. #if 0
  2142. int hardfile_remount (int nr)
  2143. {
  2144. /* this does work but every media reinsert duplicates the device.. */
  2145. #if 0
  2146. if (!mountertask)
  2147. return 0;
  2148. automountunit = nr;
  2149. uae_Signal (mountertask, 1 << 13);
  2150. #endif
  2151. return 1;
  2152. }
  2153. #endif
  2154. bool filesys_do_disk_change (int cdunitnum, bool insert)
  2155. {
  2156. int nr = cdunitnum + cd_unit_offset;
  2157. UnitInfo *ui = &mountinfo.ui[nr];
  2158. Unit *u = ui->self;
  2159. TrapContext *ctx = NULL;
  2160. if (!ui->cd_open)
  2161. return false;
  2162. if (insert) {
  2163. if (filesys_isvolume(u))
  2164. return false;
  2165. filesys_insert (nr, NULL, _T("/"), true, MYVOLUMEINFO_CDFS | MYVOLUMEINFO_READONLY);
  2166. return true;
  2167. } else {
  2168. if (!filesys_isvolume(u))
  2169. return false;
  2170. filesys_eject (nr);
  2171. return true;
  2172. }
  2173. }
  2174. /* flags and comments supported? */
  2175. static int fsdb_cando (Unit *unit)
  2176. {
  2177. if (unit->volflags & (MYVOLUMEINFO_ARCHIVE | MYVOLUMEINFO_CDFS))
  2178. return 1;
  2179. if (currprefs.filesys_custom_uaefsdb && (unit->volflags & MYVOLUMEINFO_STREAMS))
  2180. return 1;
  2181. if (!currprefs.filesys_no_uaefsdb)
  2182. return 1;
  2183. return 0;
  2184. }
  2185. static void prepare_for_open (TCHAR *name)
  2186. {
  2187. }
  2188. static void de_recycle_aino (Unit *unit, a_inode *aino)
  2189. {
  2190. aino_test (aino);
  2191. if (aino->next == 0 || aino == &unit->rootnode)
  2192. return;
  2193. aino->next->prev = aino->prev;
  2194. aino->prev->next = aino->next;
  2195. aino->next = aino->prev = 0;
  2196. unit->aino_cache_size--;
  2197. }
  2198. static void dispose_aino (Unit *unit, a_inode **aip, a_inode *aino)
  2199. {
  2200. int hash = aino->uniq % MAX_AINO_HASH;
  2201. if (unit->aino_hash[hash] == aino)
  2202. unit->aino_hash[hash] = 0;
  2203. if (aino->dirty && aino->parent)
  2204. fsdb_dir_writeback (aino->parent);
  2205. *aip = aino->sibling;
  2206. if (unit->volflags & MYVOLUMEINFO_ARCHIVE) {
  2207. ;
  2208. } else if (unit->volflags & MYVOLUMEINFO_CDFS) {
  2209. isofs_dispose_inode (unit->ui.cdfs_superblock, aino->uniq_external);
  2210. }
  2211. xfree (aino->aname);
  2212. xfree (aino->comment);
  2213. xfree (aino->nname);
  2214. xfree (aino);
  2215. }
  2216. static void free_all_ainos (Unit *u, a_inode *parent)
  2217. {
  2218. a_inode *a;
  2219. while ((a = parent->child)) {
  2220. free_all_ainos (u, a);
  2221. dispose_aino (u, &parent->child, a);
  2222. }
  2223. }
  2224. static int flush_cache (Unit *unit, int num)
  2225. {
  2226. int i = 0;
  2227. int cnt = 100;
  2228. //write_log (_T("FILESYS: flushing cache unit %d (max %d items)\n"), unit->unit, num);
  2229. if (num == 0)
  2230. num = -1;
  2231. while (i < num || num < 0) {
  2232. int ii = i;
  2233. a_inode *parent = unit->rootnode.prev->parent;
  2234. a_inode **aip;
  2235. aip = &parent->child;
  2236. aino_test (parent);
  2237. if (parent && !parent->locked_children) {
  2238. for (;;) {
  2239. a_inode *aino = *aip;
  2240. aino_test (aino);
  2241. if (aino == 0)
  2242. break;
  2243. /* Not recyclable if next == 0 (i.e., not chained into
  2244. recyclable list), or if parent directory is being
  2245. ExNext()ed. */
  2246. if (aino->next == 0) {
  2247. aip = &aino->sibling;
  2248. } else {
  2249. if (aino->shlock > 0 || aino->elock)
  2250. write_log (_T("panic: freeing locked a_inode!\n"));
  2251. de_recycle_aino (unit, aino);
  2252. dispose_aino (unit, aip, aino);
  2253. i++;
  2254. }
  2255. }
  2256. }
  2257. { //if (unit->rootnode.next != unit->rootnode.prev) {
  2258. /* In the previous loop, we went through all children of one
  2259. parent. Re-arrange the recycled list so that we'll find a
  2260. different parent the next time around.
  2261. (infinite loop if there is only one parent?)
  2262. */
  2263. int maxloop = 10000;
  2264. do {
  2265. unit->rootnode.next->prev = unit->rootnode.prev;
  2266. unit->rootnode.prev->next = unit->rootnode.next;
  2267. unit->rootnode.next = unit->rootnode.prev;
  2268. unit->rootnode.prev = unit->rootnode.prev->prev;
  2269. unit->rootnode.prev->next = unit->rootnode.next->prev = &unit->rootnode;
  2270. } while (unit->rootnode.prev->parent == parent && maxloop-- > 0);
  2271. }
  2272. if (i == ii)
  2273. cnt--;
  2274. if (cnt <= 0)
  2275. break;
  2276. }
  2277. return unit->aino_cache_size > 0 ? 0 : 1;
  2278. }
  2279. static void recycle_aino (Unit *unit, a_inode *new_aino)
  2280. {
  2281. aino_test (new_aino);
  2282. if (new_aino->dir || new_aino->shlock > 0
  2283. || new_aino->elock || new_aino == &unit->rootnode)
  2284. /* Still in use */
  2285. return;
  2286. TRACE3((_T("Recycling; cache size %u, total_locked %d\n"),
  2287. unit->aino_cache_size, unit->total_locked_ainos));
  2288. if (unit->aino_cache_size > 5000 + unit->total_locked_ainos) {
  2289. /* Reap a few. */
  2290. flush_cache (unit, 50);
  2291. #if 0
  2292. {
  2293. TCHAR buffer[40];
  2294. _stprintf (buffer, "%d ainos reaped.\n", i);
  2295. TRACE ((buffer));
  2296. }
  2297. #endif
  2298. }
  2299. aino_test (new_aino);
  2300. /* Chain it into circular list. */
  2301. new_aino->next = unit->rootnode.next;
  2302. new_aino->prev = &unit->rootnode;
  2303. new_aino->prev->next = new_aino;
  2304. new_aino->next->prev = new_aino;
  2305. aino_test (new_aino->next);
  2306. aino_test (new_aino->prev);
  2307. unit->aino_cache_size++;
  2308. }
  2309. void filesys_flush_cache (void)
  2310. {
  2311. }
  2312. static void update_child_names (Unit *unit, a_inode *a, a_inode *parent)
  2313. {
  2314. int l0 = _tcslen (parent->nname) + 2;
  2315. while (a != 0) {
  2316. TCHAR *name_start;
  2317. TCHAR *new_name;
  2318. TCHAR dirsep[2] = { FSDB_DIR_SEPARATOR, '\0' };
  2319. a->parent = parent;
  2320. name_start = _tcsrchr (a->nname, FSDB_DIR_SEPARATOR);
  2321. if (name_start == 0) {
  2322. write_log (_T("malformed file name"));
  2323. }
  2324. name_start++;
  2325. new_name = xmalloc (TCHAR, _tcslen (name_start) + l0);
  2326. _tcscpy (new_name, parent->nname);
  2327. _tcscat (new_name, dirsep);
  2328. _tcscat (new_name, name_start);
  2329. xfree (a->nname);
  2330. a->nname = new_name;
  2331. if (a->child)
  2332. update_child_names (unit, a->child, a);
  2333. a = a->sibling;
  2334. }
  2335. }
  2336. static void move_aino_children (Unit *unit, a_inode *from, a_inode *to)
  2337. {
  2338. aino_test (from);
  2339. aino_test (to);
  2340. to->child = from->child;
  2341. from->child = 0;
  2342. update_child_names (unit, to->child, to);
  2343. }
  2344. static void delete_aino (Unit *unit, a_inode *aino)
  2345. {
  2346. a_inode **aip;
  2347. TRACE((_T("deleting aino %x\n"), aino->uniq));
  2348. aino_test (aino);
  2349. aino->dirty = 1;
  2350. aino->deleted = 1;
  2351. de_recycle_aino (unit, aino);
  2352. /* If any ExKeys are currently pointing at us, advance them. */
  2353. if (aino->parent->exnext_count > 0) {
  2354. int i;
  2355. TRACE((_T("entering exkey validation\n")));
  2356. for (i = 0; i < EXKEYS; i++) {
  2357. ExamineKey *k = unit->examine_keys + i;
  2358. if (k->uniq == 0)
  2359. continue;
  2360. if (k->aino == aino->parent) {
  2361. TRACE((_T("Same parent found for %d\n"), i));
  2362. if (k->curr_file == aino) {
  2363. k->curr_file = aino->sibling;
  2364. TRACE((_T("Advancing curr_file\n")));
  2365. }
  2366. }
  2367. }
  2368. }
  2369. aip = &aino->parent->child;
  2370. while (*aip != aino && *aip != 0)
  2371. aip = &(*aip)->sibling;
  2372. if (*aip != aino) {
  2373. write_log (_T("Couldn't delete aino.\n"));
  2374. return;
  2375. }
  2376. dispose_aino (unit, aip, aino);
  2377. }
  2378. static a_inode *lookup_sub (a_inode *dir, uae_u32 uniq)
  2379. {
  2380. a_inode **cp = &dir->child;
  2381. a_inode *c, *retval;
  2382. for (;;) {
  2383. c = *cp;
  2384. if (c == 0)
  2385. return 0;
  2386. if (c->uniq == uniq) {
  2387. retval = c;
  2388. break;
  2389. }
  2390. if (c->dir) {
  2391. a_inode *a = lookup_sub (c, uniq);
  2392. if (a != 0) {
  2393. retval = a;
  2394. break;
  2395. }
  2396. }
  2397. cp = &c->sibling;
  2398. }
  2399. if (! dir->locked_children) {
  2400. /* Move to the front to speed up repeated lookups. Don't do this if
  2401. an ExNext is going on in this directory, or we'll terminally
  2402. confuse it. */
  2403. *cp = c->sibling;
  2404. c->sibling = dir->child;
  2405. dir->child = c;
  2406. }
  2407. return retval;
  2408. }
  2409. static a_inode *lookup_aino (Unit *unit, uae_u32 uniq)
  2410. {
  2411. a_inode *a;
  2412. int hash = uniq % MAX_AINO_HASH;
  2413. if (uniq == 0)
  2414. return &unit->rootnode;
  2415. a = unit->aino_hash[hash];
  2416. if (a == 0 || a->uniq != uniq)
  2417. a = lookup_sub (&unit->rootnode, uniq);
  2418. else
  2419. unit->nr_cache_hits++;
  2420. unit->nr_cache_lookups++;
  2421. unit->aino_hash[hash] = a;
  2422. aino_test (a);
  2423. return a;
  2424. }
  2425. static a_inode *aino_from_lock(TrapContext *ctx, Unit *unit, uaecptr lock)
  2426. {
  2427. return lookup_aino (unit, trap_get_long(ctx, lock + 4));
  2428. }
  2429. TCHAR *build_nname (const TCHAR *d, const TCHAR *n)
  2430. {
  2431. TCHAR dsep[2] = { FSDB_DIR_SEPARATOR, 0 };
  2432. TCHAR *p = xmalloc (TCHAR, _tcslen (d) + 1 + _tcslen (n) + 1);
  2433. _tcscpy (p, d);
  2434. _tcscat (p, dsep);
  2435. _tcscat (p, n);
  2436. return p;
  2437. }
  2438. TCHAR *build_aname (const TCHAR *d, const TCHAR *n)
  2439. {
  2440. TCHAR *p = xmalloc (TCHAR, _tcslen (d) + 1 + _tcslen (n) + 1);
  2441. _tcscpy (p, d);
  2442. _tcscat (p, _T("/"));
  2443. _tcscat (p, n);
  2444. return p;
  2445. }
  2446. /* This gets called to translate an Amiga name that some program used to
  2447. * a name that we can use on the native filesystem. */
  2448. static TCHAR *get_nname (Unit *unit, a_inode *base, TCHAR *rel, TCHAR **modified_rel, uae_u64 *uniq_ext)
  2449. {
  2450. TCHAR *found;
  2451. *modified_rel = 0;
  2452. if (unit->volflags & MYVOLUMEINFO_ARCHIVE) {
  2453. if (zfile_exists_archive (base->nname, rel))
  2454. return build_nname (base->nname, rel);
  2455. return NULL;
  2456. } else if (unit->volflags & MYVOLUMEINFO_CDFS) {
  2457. if (isofs_exists (unit->ui.cdfs_superblock, base->uniq_external, rel, uniq_ext))
  2458. return build_nname (base->nname, rel);
  2459. return NULL;
  2460. }
  2461. aino_test (base);
  2462. /* If we have a mapping of some other aname to "rel", we must pretend
  2463. * it does not exist.
  2464. * This can happen for example if an Amiga program creates a
  2465. * file called ".". We can't represent this in our filesystem,
  2466. * so we create a special file "uae_xxx" and record the mapping
  2467. * aname "." -> nname "uae_xxx" in the database. Then, the Amiga
  2468. * program looks up "uae_xxx" (yes, it's contrived). The filesystem
  2469. * should not make the uae_xxx file visible to the Amiga side. */
  2470. if (fsdb_used_as_nname (base, rel))
  2471. return 0;
  2472. /* A file called "." (or whatever else is invalid on this filesystem)
  2473. * does not exist, as far as the Amiga side is concerned. */
  2474. if (fsdb_name_invalid_dir (base, rel))
  2475. return 0;
  2476. /* See if we have a file that has the same name as the aname we are
  2477. * looking for. */
  2478. found = fsdb_search_dir (base->nname, rel);
  2479. if (found == 0)
  2480. return found;
  2481. if (found == rel)
  2482. return build_nname (base->nname, rel);
  2483. *modified_rel = found;
  2484. return build_nname (base->nname, found);
  2485. }
  2486. static TCHAR *create_nname (Unit *unit, a_inode *base, TCHAR *rel)
  2487. {
  2488. TCHAR *p;
  2489. aino_test (base);
  2490. /* We are trying to create a file called REL. */
  2491. /* If the name is used otherwise in the directory (or globally), we
  2492. * need a new unique nname. */
  2493. if (fsdb_name_invalid (base, rel) || fsdb_used_as_nname (base, rel)) {
  2494. #if 0
  2495. oh_dear:
  2496. #endif
  2497. if (currprefs.filesys_no_uaefsdb && !(base->volflags & MYVOLUMEINFO_STREAMS)) {
  2498. write_log (_T("illegal filename '%s', no stream supporting filesystem and uaefsdb disabled\n"), rel);
  2499. return 0;
  2500. }
  2501. p = fsdb_create_unique_nname (base, rel);
  2502. return p;
  2503. }
  2504. p = build_nname (base->nname, rel);
  2505. #if 0
  2506. /* Delete this code once we know everything works. */
  2507. if (access (p, R_OK) >= 0 || errno != ENOENT) {
  2508. write_log (_T("Filesystem in trouble... please report.\n"));
  2509. xfree (p);
  2510. goto oh_dear;
  2511. }
  2512. #endif
  2513. return p;
  2514. }
  2515. static int fill_file_attrs (Unit *u, a_inode *base, a_inode *c)
  2516. {
  2517. if (u->volflags & MYVOLUMEINFO_ARCHIVE) {
  2518. int isdir, flags;
  2519. TCHAR *comment;
  2520. zfile_fill_file_attrs_archive (c->nname, &isdir, &flags, &comment);
  2521. c->dir = isdir;
  2522. c->amigaos_mode = 0;
  2523. if (flags >= 0)
  2524. c->amigaos_mode = flags;
  2525. c->comment = comment;
  2526. return 1;
  2527. } else if (u->volflags & MYVOLUMEINFO_CDFS) {
  2528. int isdir, flags;
  2529. TCHAR *comment;
  2530. isofss_fill_file_attrs (u->ui.cdfs_superblock, base->uniq_external, &isdir, &flags, &comment, c->uniq_external);
  2531. c->dir = isdir;
  2532. c->amigaos_mode = 0;
  2533. if (flags >= 0)
  2534. c->amigaos_mode = flags;
  2535. c->comment = comment;
  2536. return 1;
  2537. } else {
  2538. return fsdb_fill_file_attrs (base, c);
  2539. }
  2540. return 0;
  2541. }
  2542. static int test_softlink (a_inode *aino)
  2543. {
  2544. int err;
  2545. if (aino->softlink && my_resolvesoftlink (aino->nname, -1, false))
  2546. err = ERROR_IS_SOFT_LINK;
  2547. else
  2548. err = ERROR_OBJECT_NOT_AROUND;
  2549. return err;
  2550. }
  2551. static void handle_softlink(TrapContext *ctx, Unit *unit, dpacket *packet, a_inode *aino)
  2552. {
  2553. PUT_PCK_RES1 (packet, DOS_FALSE);
  2554. PUT_PCK_RES2 (packet, test_softlink (aino));
  2555. }
  2556. /*
  2557. * This gets called if an ACTION_EXAMINE_NEXT happens and we hit an object
  2558. * for which we know the name on the native filesystem, but no corresponding
  2559. * Amiga filesystem name.
  2560. * @@@ For DOS filesystems, it might make sense to declare the new name
  2561. * "weak", so that it can get overriden by a subsequent call to get_nname().
  2562. * That way, if someone does "dir :" and there is a file "foobar.inf", and
  2563. * someone else tries to open "foobar.info", get_nname() could maybe made to
  2564. * figure out that this is supposed to be the file "foobar.inf".
  2565. * DOS sucks...
  2566. */
  2567. static TCHAR *get_aname (Unit *unit, a_inode *base, TCHAR *rel)
  2568. {
  2569. return my_strdup (rel);
  2570. }
  2571. static void init_child_aino_tree (Unit *unit, a_inode *base, a_inode *aino)
  2572. {
  2573. /* Update tree structure */
  2574. aino->parent = base;
  2575. aino->child = 0;
  2576. aino->sibling = base->child;
  2577. base->child = aino;
  2578. aino->next = aino->prev = 0;
  2579. aino->volflags = unit->volflags;
  2580. }
  2581. static void init_child_aino (Unit *unit, a_inode *base, a_inode *aino)
  2582. {
  2583. aino->uniq = ++a_uniq;
  2584. if (a_uniq == 0xFFFFFFFF) {
  2585. write_log (_T("Running out of a_inodes (prepare for big trouble)!\n"));
  2586. }
  2587. aino->shlock = 0;
  2588. aino->elock = 0;
  2589. aino->dirty = 0;
  2590. aino->deleted = 0;
  2591. aino->mountcount = unit->mountcount;
  2592. /* For directories - this one isn't being ExNext()ed yet. */
  2593. aino->locked_children = 0;
  2594. aino->exnext_count = 0;
  2595. /* But the parent might be. */
  2596. if (base->exnext_count) {
  2597. unit->total_locked_ainos++;
  2598. base->locked_children++;
  2599. }
  2600. init_child_aino_tree (unit, base, aino);
  2601. aino_test_init (aino);
  2602. aino_test (aino);
  2603. }
  2604. static a_inode *new_child_aino (Unit *unit, a_inode *base, TCHAR *rel)
  2605. {
  2606. TCHAR *modified_rel;
  2607. TCHAR *nn;
  2608. a_inode *aino = NULL;
  2609. int isvirtual = unit->volflags & (MYVOLUMEINFO_ARCHIVE | MYVOLUMEINFO_CDFS);
  2610. TRACE((_T("new_child_aino %s, %s\n"), base->aname, rel));
  2611. if (!isvirtual)
  2612. aino = fsdb_lookup_aino_aname (base, rel);
  2613. if (aino == 0) {
  2614. uae_u64 uniq_ext = 0;
  2615. nn = get_nname (unit, base, rel, &modified_rel, &uniq_ext);
  2616. if (nn == 0)
  2617. return 0;
  2618. aino = xcalloc (a_inode, 1);
  2619. if (aino == 0)
  2620. return 0;
  2621. aino->uniq_external = uniq_ext;
  2622. aino->aname = modified_rel ? modified_rel : my_strdup (rel);
  2623. aino->nname = nn;
  2624. aino->comment = 0;
  2625. aino->has_dbentry = 0;
  2626. if (!fill_file_attrs (unit, base, aino)) {
  2627. xfree (aino);
  2628. return 0;
  2629. }
  2630. if (aino->dir && !isvirtual)
  2631. fsdb_clean_dir (aino);
  2632. }
  2633. init_child_aino (unit, base, aino);
  2634. recycle_aino (unit, aino);
  2635. TRACE((_T("created aino %x, lookup, amigaos_mode %d\n"), aino->uniq, aino->amigaos_mode));
  2636. return aino;
  2637. }
  2638. static a_inode *create_child_aino (Unit *unit, a_inode *base, TCHAR *rel, int isdir)
  2639. {
  2640. a_inode *aino = xcalloc (a_inode, 1);
  2641. if (aino == 0)
  2642. return 0;
  2643. aino->nname = create_nname (unit, base, rel);
  2644. if (!aino->nname) {
  2645. free (aino);
  2646. return 0;
  2647. }
  2648. aino->aname = my_strdup (rel);
  2649. init_child_aino (unit, base, aino);
  2650. aino->amigaos_mode = 0;
  2651. aino->dir = isdir;
  2652. aino->comment = 0;
  2653. aino->has_dbentry = 0;
  2654. aino->dirty = 1;
  2655. recycle_aino (unit, aino);
  2656. TRACE((_T("created aino %x, create\n"), aino->uniq));
  2657. return aino;
  2658. }
  2659. static a_inode *lookup_child_aino (Unit *unit, a_inode *base, TCHAR *rel, int *err)
  2660. {
  2661. a_inode *c = base->child;
  2662. int l0 = _tcslen (rel);
  2663. aino_test (base);
  2664. aino_test (c);
  2665. if (base->dir == 0) {
  2666. *err = ERROR_OBJECT_WRONG_TYPE;
  2667. return 0;
  2668. }
  2669. while (c != 0) {
  2670. int l1 = _tcslen (c->aname);
  2671. if (l0 <= l1 && same_aname (rel, c->aname + l1 - l0)
  2672. && (l0 == l1 || c->aname[l1-l0-1] == '/') && c->mountcount == unit->mountcount)
  2673. break;
  2674. c = c->sibling;
  2675. }
  2676. if (c != 0)
  2677. return c;
  2678. c = new_child_aino (unit, base, rel);
  2679. if (c == 0)
  2680. *err = ERROR_OBJECT_NOT_AROUND;
  2681. return c;
  2682. }
  2683. /* Different version because for this one, REL is an nname. */
  2684. static a_inode *lookup_child_aino_for_exnext (Unit *unit, a_inode *base, TCHAR *rel, uae_u32 *err, uae_u64 uniq_external, struct virtualfilesysobject *vfso)
  2685. {
  2686. a_inode *c = base->child;
  2687. int l0 = _tcslen (rel);
  2688. int isvirtual = unit->volflags & (MYVOLUMEINFO_ARCHIVE | MYVOLUMEINFO_CDFS);
  2689. aino_test (base);
  2690. aino_test (c);
  2691. *err = 0;
  2692. while (c != 0) {
  2693. int l1 = _tcslen (c->nname);
  2694. /* Note: using _tcscmp here. */
  2695. if (l0 <= l1 && _tcscmp (rel, c->nname + l1 - l0) == 0
  2696. && (l0 == l1 || c->nname[l1-l0-1] == FSDB_DIR_SEPARATOR) && c->mountcount == unit->mountcount)
  2697. break;
  2698. c = c->sibling;
  2699. }
  2700. if (c != 0)
  2701. return c;
  2702. if (!isvirtual && !vfso)
  2703. c = fsdb_lookup_aino_nname (base, rel);
  2704. if (c == 0) {
  2705. c = xcalloc (a_inode, 1);
  2706. if (c == 0) {
  2707. *err = ERROR_NO_FREE_STORE;
  2708. return 0;
  2709. }
  2710. c->nname = build_nname (base->nname, rel);
  2711. c->aname = get_aname (unit, base, rel);
  2712. c->comment = 0;
  2713. c->uniq_external = uniq_external;
  2714. c->has_dbentry = 0;
  2715. if (vfso) {
  2716. c->dir = vfso->dir;
  2717. c->comment = my_strdup(vfso->comment);
  2718. c->amigaos_mode = vfso->amigaos_mode;
  2719. c->vfso = vfso;
  2720. } else if (!fill_file_attrs (unit, base, c)) {
  2721. xfree (c);
  2722. *err = ERROR_NO_FREE_STORE;
  2723. return 0;
  2724. }
  2725. if (c->dir && !isvirtual && !vfso)
  2726. fsdb_clean_dir (c);
  2727. }
  2728. init_child_aino (unit, base, c);
  2729. recycle_aino (unit, c);
  2730. TRACE((_T("created aino %s:%d, exnext\n"), c->nname, c->uniq));
  2731. return c;
  2732. }
  2733. static a_inode *get_aino (Unit *unit, a_inode *base, const TCHAR *rel, int *err)
  2734. {
  2735. TCHAR *tmp;
  2736. TCHAR *p;
  2737. a_inode *curr, *prev;
  2738. int i;
  2739. aino_test (base);
  2740. *err = 0;
  2741. TRACE((_T("get_path(%s,%s)\n"), base->aname, rel));
  2742. /* root-relative path? */
  2743. for (i = 0; rel[i] && rel[i] != '/' && rel[i] != ':'; i++)
  2744. ;
  2745. if (':' == rel[i])
  2746. rel += i+1;
  2747. tmp = my_strdup (rel);
  2748. p = tmp;
  2749. curr = base;
  2750. prev = NULL;
  2751. while (*p) {
  2752. /* start with a slash? go up a level. */
  2753. if (*p == '/') {
  2754. if (curr->parent != 0)
  2755. curr = curr->parent;
  2756. p++;
  2757. } else {
  2758. a_inode *next;
  2759. if (prev && prev->softlink) {
  2760. *err = test_softlink (prev);
  2761. curr = NULL;
  2762. break;
  2763. }
  2764. TCHAR *component_end;
  2765. component_end = _tcschr (p, '/');
  2766. if (component_end != 0)
  2767. *component_end = '\0';
  2768. next = lookup_child_aino (unit, curr, p, err);
  2769. if (next == 0) {
  2770. /* if only last component not found, return parent dir. */
  2771. if (*err != ERROR_OBJECT_NOT_AROUND || component_end != 0)
  2772. curr = NULL;
  2773. /* ? what error is appropriate? */
  2774. break;
  2775. }
  2776. prev = next;
  2777. curr = next;
  2778. if (component_end)
  2779. p = component_end+1;
  2780. else
  2781. break;
  2782. }
  2783. }
  2784. xfree (tmp);
  2785. return curr;
  2786. }
  2787. static uae_u32 notifyhash (const TCHAR *s)
  2788. {
  2789. uae_u32 hash = 0;
  2790. while (*s)
  2791. hash = (hash << 5) + *s++;
  2792. return hash % NOTIFY_HASH_SIZE;
  2793. }
  2794. static Notify *new_notify (Unit *unit, TCHAR *name)
  2795. {
  2796. Notify *n = xmalloc (Notify, 1);
  2797. uae_u32 hash = notifyhash (name);
  2798. n->next = unit->notifyhash[hash];
  2799. unit->notifyhash[hash] = n;
  2800. n->partname = name;
  2801. return n;
  2802. }
  2803. #if 0
  2804. static void free_notify_item (Notify *n)
  2805. {
  2806. xfree (n->fullname);
  2807. xfree (n->partname);
  2808. xfree (n);
  2809. }
  2810. #endif
  2811. static void free_notify (Unit *unit, int hash, Notify *n)
  2812. {
  2813. Notify *n1, *prev = 0;
  2814. for (n1 = unit->notifyhash[hash]; n1; n1 = n1->next) {
  2815. if (n == n1) {
  2816. if (prev)
  2817. prev->next = n->next;
  2818. else
  2819. unit->notifyhash[hash] = n->next;
  2820. break;
  2821. }
  2822. prev = n1;
  2823. }
  2824. }
  2825. static void startup_update_unit (Unit *unit, UnitInfo *uinfo)
  2826. {
  2827. if (!unit)
  2828. return;
  2829. xfree (unit->ui.volname);
  2830. memcpy (&unit->ui, uinfo, sizeof (UnitInfo));
  2831. unit->ui.devname = uinfo->devname;
  2832. unit->ui.volname = my_strdup (uinfo->volname); /* might free later for rename */
  2833. }
  2834. static Unit *startup_create_unit(TrapContext *ctx, UnitInfo *uinfo, int num)
  2835. {
  2836. int i;
  2837. Unit *unit, *u;
  2838. unit = xcalloc (Unit, 1);
  2839. /* keep list in insertion order */
  2840. u = units;
  2841. if (u) {
  2842. while (u->next)
  2843. u = u->next;
  2844. u->next = unit;
  2845. } else {
  2846. units = unit;
  2847. }
  2848. uinfo->self = unit;
  2849. unit->volume = 0;
  2850. unit->port = trap_get_areg(ctx, 5);
  2851. unit->unit = num;
  2852. startup_update_unit (unit, uinfo);
  2853. unit->cmds_complete = 0;
  2854. unit->cmds_sent = 0;
  2855. unit->cmds_acked = 0;
  2856. clear_exkeys (unit);
  2857. unit->total_locked_ainos = 0;
  2858. unit->keys = 0;
  2859. for (i = 0; i < NOTIFY_HASH_SIZE; i++) {
  2860. Notify *n = unit->notifyhash[i];
  2861. while (n) {
  2862. Notify *n2 = n;
  2863. n = n->next;
  2864. xfree (n2->fullname);
  2865. xfree (n2->partname);
  2866. xfree (n2);
  2867. }
  2868. unit->notifyhash[i] = 0;
  2869. }
  2870. unit->rootnode.aname = uinfo->volname;
  2871. unit->rootnode.nname = uinfo->rootdir;
  2872. unit->rootnode.sibling = 0;
  2873. unit->rootnode.next = unit->rootnode.prev = &unit->rootnode;
  2874. unit->rootnode.uniq = 0;
  2875. unit->rootnode.parent = 0;
  2876. unit->rootnode.child = 0;
  2877. unit->rootnode.dir = 1;
  2878. unit->rootnode.amigaos_mode = 0;
  2879. unit->rootnode.shlock = 0;
  2880. unit->rootnode.elock = 0;
  2881. unit->rootnode.comment = 0;
  2882. unit->rootnode.has_dbentry = 0;
  2883. unit->rootnode.volflags = uinfo->volflags;
  2884. aino_test_init (&unit->rootnode);
  2885. unit->aino_cache_size = 0;
  2886. for (i = 0; i < MAX_AINO_HASH; i++)
  2887. unit->aino_hash[i] = 0;
  2888. return unit;
  2889. }
  2890. static bool mount_cd(UnitInfo *uinfo, int nr, struct mytimeval *ctime, uae_u64 *uniq, uaecptr volume)
  2891. {
  2892. uinfo->cddevno = nr - cd_unit_offset;
  2893. if (!sys_command_open (uinfo->cddevno)) {
  2894. write_log (_T("Failed attempt to open CD unit %d\n"), uinfo->cddevno);
  2895. return false;
  2896. }
  2897. #ifdef RETROPLATFORM
  2898. rp_cd_device_enable (uinfo->cddevno, true);
  2899. #endif
  2900. uinfo->cdfs_superblock = isofs_mount(uinfo->cddevno, uniq);
  2901. uinfo->wasisempty = true;
  2902. struct isofs_info ii;
  2903. if (isofs_mediainfo (uinfo->cdfs_superblock, &ii)) {
  2904. xfree (uinfo->volname);
  2905. if (ii.media) {
  2906. uinfo->wasisempty = false;
  2907. if (!ii.unknown_media) {
  2908. uinfo->volname = my_strdup (ii.volumename);
  2909. if (ctime) {
  2910. ctime->tv_sec = ii.creation;
  2911. ctime->tv_usec = 0;
  2912. }
  2913. set_highcyl(volume, ii.totalblocks);
  2914. #ifdef RETROPLATFORM
  2915. rp_cd_image_change (uinfo->cddevno, ii.devname);
  2916. #endif
  2917. }
  2918. }
  2919. uinfo->unknown_media = ii.unknown_media;
  2920. }
  2921. uinfo->cd_open = true;
  2922. return true;
  2923. }
  2924. #ifdef UAE_FILESYS_THREADS
  2925. static void filesys_thread (void *unit_v);
  2926. #endif
  2927. static void filesys_start_thread (UnitInfo *ui, int nr)
  2928. {
  2929. ui->unit_pipe = 0;
  2930. ui->back_pipe = 0;
  2931. ui->reset_state = FS_STARTUP;
  2932. if (!isrestore ()) {
  2933. ui->startup = 0;
  2934. ui->self = 0;
  2935. }
  2936. #ifdef UAE_FILESYS_THREADS
  2937. if (is_virtual (nr)) {
  2938. ui->unit_pipe = xmalloc (smp_comm_pipe, 1);
  2939. ui->back_pipe = xmalloc (smp_comm_pipe, 1);
  2940. init_comm_pipe (ui->unit_pipe, 400, 3);
  2941. init_comm_pipe (ui->back_pipe, 100, 1);
  2942. uae_start_thread (_T("filesys"), filesys_thread, (void *)ui, &ui->tid);
  2943. }
  2944. #endif
  2945. if (isrestore ()) {
  2946. if (ui->unit_type == UNIT_CDFS) {
  2947. mount_cd(ui, nr, NULL, &ui->self->rootnode.uniq_external, ui->self->volume);
  2948. }
  2949. startup_update_unit (ui->self, ui);
  2950. }
  2951. }
  2952. static uae_u32 REGPARAM2 startup_handler(TrapContext *ctx)
  2953. {
  2954. uae_u32 mode = trap_get_dreg(ctx, 0);
  2955. if (mode == 1) {
  2956. uaecptr addr = 0;
  2957. if (currprefs.uaeboard > 1) {
  2958. // return board ram instead of allocating normal RAM
  2959. addr = uaeboard_alloc_ram(trap_get_dreg(ctx, 1));
  2960. }
  2961. return addr;
  2962. }
  2963. /* Just got the startup packet. It's in D3. DosBase is in A2,
  2964. * our allocated volume structure is in A3, A5 is a pointer to
  2965. * our port. */
  2966. uaecptr rootnode = trap_get_long(ctx, trap_get_areg(ctx, 2) + 34);
  2967. uaecptr dos_info = trap_get_long(ctx, rootnode + 24) << 2;
  2968. uaecptr pkt = trap_get_dreg(ctx, 3);
  2969. uaecptr arg1 = trap_get_long(ctx, pkt + dp_Arg1);
  2970. uaecptr arg2 = trap_get_long(ctx, pkt + dp_Arg2);
  2971. uaecptr arg3 = trap_get_long(ctx, pkt + dp_Arg3);
  2972. uaecptr devnode, volume;
  2973. int nr;
  2974. Unit *unit;
  2975. UnitInfo *uinfo;
  2976. int late = 0;
  2977. int ed, ef;
  2978. uae_u64 uniq = 0;
  2979. uae_u32 cdays;
  2980. struct mytimeval ctime = { 0 };
  2981. // 1.3:
  2982. // dp_Arg1 contains crap (Should be name of device)
  2983. // dp_Arg2 = works as documented
  2984. // dp_Arg3 = NULL (!?). (Should be DeviceNode)
  2985. for (nr = 0; nr < MAX_FILESYSTEM_UNITS; nr++) {
  2986. /* Hardfile volume name? */
  2987. if (mountinfo.ui[nr].open <= 0)
  2988. continue;
  2989. if (!is_virtual (nr))
  2990. continue;
  2991. if (mountinfo.ui[nr].startup == arg2)
  2992. break;
  2993. }
  2994. if (nr == MAX_FILESYSTEM_UNITS) {
  2995. write_log (_T("Attempt to mount unknown filesystem device\n"));
  2996. trap_put_long(ctx, pkt + dp_Res1, DOS_FALSE);
  2997. trap_put_long(ctx, pkt + dp_Res2, ERROR_DEVICE_NOT_MOUNTED);
  2998. return 0;
  2999. }
  3000. uinfo = mountinfo.ui + nr;
  3001. //devnode = arg3 << 2;
  3002. devnode = uinfo->devicenode;
  3003. volume = trap_get_areg(ctx, 3) + 32;
  3004. cdays = 3800 + nr;
  3005. if (uinfo->unit_type == UNIT_CDFS) {
  3006. ed = ef = 0;
  3007. if (!mount_cd(uinfo, nr, &ctime, &uniq, volume)) {
  3008. trap_put_long(ctx, pkt + dp_Res1, DOS_FALSE);
  3009. trap_put_long(ctx, pkt + dp_Res2, ERROR_DEVICE_NOT_MOUNTED);
  3010. return 0;
  3011. }
  3012. } else {
  3013. ed = my_existsdir (uinfo->rootdir);
  3014. ef = my_existsfile (uinfo->rootdir);
  3015. if (!uinfo->wasisempty && !ef && !ed) {
  3016. write_log (_T("Failed attempt to mount device '%s' (%s)\n"), uinfo->devname, uinfo->rootdir);
  3017. trap_put_long(ctx, pkt + dp_Res1, DOS_FALSE);
  3018. trap_put_long(ctx, pkt + dp_Res2, ERROR_DEVICE_NOT_MOUNTED);
  3019. return 0;
  3020. }
  3021. }
  3022. if (!uinfo->unit_pipe) {
  3023. late = 1;
  3024. filesys_start_thread(uinfo, nr);
  3025. }
  3026. unit = startup_create_unit(ctx, uinfo, nr);
  3027. unit->volflags = uinfo->volflags;
  3028. unit->rootnode.uniq_external = uniq;
  3029. /* write_comm_pipe_int (unit->ui.unit_pipe, -1, 1);*/
  3030. write_log (_T("FS: %s (flags=%08X,E=%d,ED=%d,EF=%d,native='%s') starting..\n"),
  3031. unit->ui.volname, unit->volflags, uinfo->wasisempty, ed, ef, unit->ui.rootdir);
  3032. /* fill in our process in the device node */
  3033. trap_put_long(ctx, devnode + 8, unit->port);
  3034. unit->dosbase = trap_get_areg(ctx, 2);
  3035. /* make new volume */
  3036. unit->volume = volume;
  3037. put_long(unit->volume + 180 - 32, devnode);
  3038. #ifdef UAE_FILESYS_THREADS
  3039. unit->locklist = trap_get_areg(ctx, 3) + 8;
  3040. #else
  3041. unit->locklist = trap_get_areg(ctx, 3);
  3042. #endif
  3043. unit->dummy_message = trap_get_areg(ctx, 3) + 12;
  3044. trap_put_long(ctx, unit->dummy_message + 10, 0);
  3045. /* Prepare volume information */
  3046. put_long(unit->volume + 4, 2); /* Type = dt_volume */
  3047. put_long(unit->volume + 12, 0); /* Lock */
  3048. put_long(unit->volume + 16, cdays); /* Creation Date */
  3049. put_long(unit->volume + 20, 0);
  3050. put_long(unit->volume + 24, 0);
  3051. put_long(unit->volume + 28, 0); /* lock list */
  3052. put_long(unit->volume + 40, (unit->volume + 64) >> 2); /* Name */
  3053. //put_long(unit->volume + 44, 68);
  3054. //put_long(unit->volume + 48, 68);
  3055. put_byte(unit->volume + 64, 0);
  3056. if (!uinfo->wasisempty && !uinfo->unknown_media) {
  3057. int isvirtual = unit->volflags & (MYVOLUMEINFO_ARCHIVE | MYVOLUMEINFO_CDFS);
  3058. /* Set volume if non-empty */
  3059. set_volume_name(unit, &ctime);
  3060. if (!isvirtual)
  3061. fsdb_clean_dir (&unit->rootnode);
  3062. }
  3063. put_long(unit->volume + 8, unit->port);
  3064. /* not FFS because it is not understood by WB1.x C:Info */
  3065. put_long(unit->volume + 32, DISK_TYPE_DOS);
  3066. trap_put_long(ctx, pkt + dp_Res1, DOS_TRUE);
  3067. return 1 | (late ? 2 : 0);
  3068. }
  3069. static bool is_writeprotected(Unit *unit)
  3070. {
  3071. return unit->ui.readonly || unit->ui.locked || currprefs.harddrive_read_only;
  3072. }
  3073. static void do_info(TrapContext *ctx, Unit *unit, dpacket *packet, uaecptr info, bool disk_info)
  3074. {
  3075. struct fs_usage fsu;
  3076. int ret, err = ERROR_NO_FREE_STORE;
  3077. int blocksize, nr;
  3078. uae_u32 dostype;
  3079. bool fs = false, media = false;
  3080. uae_u8 buf[36] = { 0 }; // InfoData
  3081. blocksize = 512;
  3082. dostype = get_long(unit->volume + 32);
  3083. nr = unit->unit;
  3084. if (unit->volflags & MYVOLUMEINFO_ARCHIVE) {
  3085. ret = zfile_fs_usage_archive (unit->ui.rootdir, 0, &fsu);
  3086. fs = true;
  3087. media = filesys_isvolume(unit) != 0;
  3088. } else if (unit->volflags & MYVOLUMEINFO_CDFS) {
  3089. struct isofs_info ii;
  3090. ret = isofs_mediainfo (unit->ui.cdfs_superblock, &ii) ? 0 : 1;
  3091. if (!ret) {
  3092. media = ii.media;
  3093. nr = unit->unit - cd_unit_offset;
  3094. blocksize = ii.blocksize;
  3095. if (ii.media) {
  3096. fsu.total = ii.blocks * ii.blocksize;
  3097. fsu.avail = 0;
  3098. }
  3099. }
  3100. } else {
  3101. ret = get_fs_usage (unit->ui.rootdir, 0, &fsu);
  3102. if (ret)
  3103. err = dos_errno ();
  3104. fs = true;
  3105. media = filesys_isvolume(unit) != 0;
  3106. }
  3107. if (ret != 0) {
  3108. PUT_PCK_RES1 (packet, DOS_FALSE);
  3109. PUT_PCK_RES2 (packet, err);
  3110. return;
  3111. }
  3112. put_long_host(buf, 0); /* errors */
  3113. put_long_host(buf + 4, nr); /* unit number */
  3114. put_long_host(buf + 8, is_writeprotected(unit) ? 80 : 82); /* state */
  3115. put_long_host(buf + 20, blocksize); /* bytesperblock */
  3116. put_long_host(buf + 32, 0); /* inuse */
  3117. if (unit->ui.unknown_media) {
  3118. if (!disk_info) {
  3119. PUT_PCK_RES1 (packet, DOS_FALSE);
  3120. PUT_PCK_RES2 (packet, ERROR_NOT_A_DOS_DISK);
  3121. return;
  3122. }
  3123. put_long_host(buf + 12, 0);
  3124. put_long_host(buf + 16, 0);
  3125. put_long_host(buf + 24, ('B' << 24) | ('A' << 16) | ('D' << 8) | (0 << 0)); /* ID_UNREADABLE_DISK */
  3126. put_long_host(buf + 28, 0);
  3127. } else if (!media) {
  3128. if (!disk_info) {
  3129. PUT_PCK_RES1 (packet, DOS_FALSE);
  3130. PUT_PCK_RES2 (packet, ERROR_NO_DISK);
  3131. return;
  3132. }
  3133. put_long_host(buf + 12, 0);
  3134. put_long_host(buf + 16, 0);
  3135. put_long_host(buf + 24, -1); /* ID_NO_DISK_PRESENT */
  3136. put_long_host(buf + 28, 0);
  3137. } else {
  3138. uae_s64 numblocks, inuse;
  3139. get_usedblocks(&fsu, fs, &blocksize, &numblocks, &inuse, true);
  3140. //write_log(_T("total %lld avail %lld Blocks %lld Inuse %lld blocksize %d\n"), fsu.total, fsu.avail, numblocks, inuse, blocksize);
  3141. put_long_host(buf + 12, (uae_u32)numblocks); /* numblocks */
  3142. put_long_host(buf + 16, (uae_u32)inuse); /* inuse */
  3143. put_long_host(buf + 20, blocksize); /* bytesperblock */
  3144. put_long_host(buf + 24, dostype == DISK_TYPE_DOS && kickstart_version >= 36 ? DISK_TYPE_DOS_FFS : dostype); /* disk type */
  3145. put_long_host(buf + 28, unit->volume >> 2); /* volume node */
  3146. put_long_host(buf + 32, (get_long(unit->volume + 28) || unit->keys) ? -1 : 0); /* inuse */
  3147. }
  3148. trap_put_bytes(ctx, buf, info, sizeof buf);
  3149. PUT_PCK_RES1 (packet, DOS_TRUE);
  3150. }
  3151. static void action_disk_info(TrapContext *ctx, Unit *unit, dpacket *packet)
  3152. {
  3153. TRACE((_T("ACTION_DISK_INFO\n")));
  3154. do_info(ctx, unit, packet, GET_PCK_ARG1 (packet) << 2, true);
  3155. }
  3156. static void action_info(TrapContext *ctx, Unit *unit, dpacket *packet)
  3157. {
  3158. TRACE((_T("ACTION_INFO\n")));
  3159. do_info(ctx, unit, packet, GET_PCK_ARG2 (packet) << 2, false);
  3160. }
  3161. static void free_key (Unit *unit, Key *k)
  3162. {
  3163. Key *k1;
  3164. Key *prev = 0;
  3165. for (k1 = unit->keys; k1; k1 = k1->next) {
  3166. if (k == k1) {
  3167. if (prev)
  3168. prev->next = k->next;
  3169. else
  3170. unit->keys = k->next;
  3171. break;
  3172. }
  3173. prev = k1;
  3174. }
  3175. for (struct lockrecord *lr = k->record; lr;) {
  3176. struct lockrecord *next = lr->next;
  3177. xfree (lr);
  3178. lr = next;
  3179. }
  3180. if (k->fd != NULL)
  3181. fs_closefile (k->fd);
  3182. xfree(k);
  3183. }
  3184. static Key *lookup_key (Unit *unit, uae_u32 uniq)
  3185. {
  3186. Key *k;
  3187. unsigned int total = 0;
  3188. /* It's hardly worthwhile to optimize this - most of the time there are
  3189. * only one or zero keys. */
  3190. for (k = unit->keys; k; k = k->next) {
  3191. total++;
  3192. if (uniq == k->uniq)
  3193. return k;
  3194. }
  3195. write_log (_T("Error: couldn't find key %u / %u!\n"), uniq, total);
  3196. return 0;
  3197. }
  3198. static Key *new_key (Unit *unit)
  3199. {
  3200. Key *k = xcalloc (Key, 1);
  3201. k->uniq = ++key_uniq;
  3202. k->fd = NULL;
  3203. k->file_pos = 0;
  3204. k->next = unit->keys;
  3205. unit->keys = k;
  3206. return k;
  3207. }
  3208. #if TRACING_ENABLED
  3209. static void dumplock(TrapContext *ctx, Unit *unit, uaecptr lock)
  3210. {
  3211. a_inode *a;
  3212. TRACE((_T("LOCK: 0x%x"), lock));
  3213. if (!lock) {
  3214. TRACE((_T("\n")));
  3215. return;
  3216. }
  3217. TRACE((_T("{ next=0x%x, mode=%d, handler=0x%x, volume=0x%x, aino %x "),
  3218. trap_get_long(ctx, lock) << 2, trap_get_long(ctx, lock + 8),
  3219. trap_get_long(ctx, lock + 12), trap_get_long(ctx, lock + 16),
  3220. trap_get_long(ctx, lock + 4)));
  3221. if (log_filesys > 0) {
  3222. a = aino_from_lock(ctx, unit, lock);
  3223. if (a == 0) {
  3224. TRACE((_T("not found!")));
  3225. } else {
  3226. TRACE((_T("%s"), a->nname));
  3227. }
  3228. }
  3229. TRACE((_T(" }\n")));
  3230. }
  3231. #endif
  3232. static a_inode *find_aino(TrapContext* ctx, Unit *unit, uaecptr lock, const TCHAR *name, int *err)
  3233. {
  3234. a_inode *a;
  3235. if (lock) {
  3236. a_inode *olda = aino_from_lock(ctx, unit, lock);
  3237. if (olda == 0) {
  3238. /* That's the best we can hope to do. */
  3239. a = get_aino (unit, &unit->rootnode, name, err);
  3240. } else {
  3241. TRACE((_T("aino: 0x%08lx"), (unsigned long int)olda->uniq));
  3242. TRACE((_T(" \"%s\"\n"), olda->nname));
  3243. a = get_aino (unit, olda, name, err);
  3244. }
  3245. } else {
  3246. a = get_aino (unit, &unit->rootnode, name, err);
  3247. }
  3248. if (a) {
  3249. TRACE((_T("aino=\"%s\"\n"), a->nname));
  3250. }
  3251. aino_test (a);
  3252. return a;
  3253. }
  3254. static uaecptr make_lock(TrapContext *ctx, Unit *unit, uae_u32 uniq, long mode)
  3255. {
  3256. /* allocate lock from the list kept by the assembly code */
  3257. uaecptr lock;
  3258. #if TRAPMD
  3259. struct trapmd md1[] =
  3260. {
  3261. { TRAPCMD_GET_LONG, { unit->locklist }, 1, 0 },
  3262. { TRAPCMD_GET_LONG, { 0 }, 2, 1 },
  3263. { TRAPCMD_PUT_LONG, { unit->locklist, 0 } },
  3264. };
  3265. trap_multi(ctx, md1, sizeof md1 / sizeof(struct trapmd));
  3266. lock = md1[0].params[0] + 4;
  3267. #else
  3268. lock = trap_get_long(ctx, unit->locklist);
  3269. trap_put_long(ctx, unit->locklist, trap_get_long(ctx, lock));
  3270. lock += 4;
  3271. #endif
  3272. #if TRAPMD
  3273. struct trapmd md2[] =
  3274. {
  3275. { TRAPCMD_PUT_LONG, { lock + 4, uniq } },
  3276. { TRAPCMD_PUT_LONG, { lock + 8, mode } },
  3277. { TRAPCMD_PUT_LONG, { lock + 12, unit->port } },
  3278. { TRAPCMD_PUT_LONG, { lock + 16, unit->volume >> 2 } },
  3279. /* prepend to lock chain */
  3280. { TRAPCMD_GET_LONG, { unit->volume + 28 }, 5, 1 },
  3281. { TRAPCMD_PUT_LONG, { lock, 0 } },
  3282. { TRAPCMD_PUT_LONG, { unit->volume + 28, lock >> 2 } }
  3283. };
  3284. trap_multi(ctx, md2, sizeof md2 / sizeof(struct trapmd));
  3285. #else
  3286. trap_put_long(ctx, lock + 4, uniq);
  3287. trap_put_long(ctx, lock + 8, mode);
  3288. trap_put_long(ctx, lock + 12, unit->port);
  3289. trap_put_long(ctx, lock + 16, unit->volume >> 2);
  3290. /* prepend to lock chain */
  3291. trap_put_long(ctx, lock, trap_get_long(ctx, unit->volume + 28));
  3292. trap_put_long(ctx, unit->volume + 28, lock >> 2);
  3293. #endif
  3294. DUMPLOCK(ctx, unit, lock);
  3295. return lock;
  3296. }
  3297. #define NOTIFY_CLASS 0x40000000
  3298. #define NOTIFY_CODE 0x1234
  3299. #define NRF_SEND_MESSAGE 1
  3300. #define NRF_SEND_SIGNAL 2
  3301. #define NRF_WAIT_REPLY 8
  3302. #define NRF_NOTIFY_INITIAL 16
  3303. #define NRF_MAGIC (1 << 31)
  3304. static void notify_send(TrapContext *ctx, Unit *unit, Notify *n)
  3305. {
  3306. uaecptr nr = n->notifyrequest;
  3307. int flags = trap_get_long(ctx, nr + 12);
  3308. if (flags & NRF_SEND_MESSAGE) {
  3309. if (!(flags & NRF_WAIT_REPLY) || ((flags & NRF_WAIT_REPLY) && !(flags & NRF_MAGIC))) {
  3310. uae_NotificationHack (unit->port, nr);
  3311. } else if (flags & NRF_WAIT_REPLY) {
  3312. trap_put_long(ctx, nr + 12, trap_get_long(ctx, nr + 12) | NRF_MAGIC);
  3313. }
  3314. } else if (flags & NRF_SEND_SIGNAL) {
  3315. uae_Signal (trap_get_long(ctx, nr + 16), 1 << trap_get_byte(ctx, nr + 20));
  3316. }
  3317. }
  3318. static void notify_check(TrapContext *ctx, Unit *unit, a_inode *a)
  3319. {
  3320. Notify *n;
  3321. int hash = notifyhash (a->aname);
  3322. for (n = unit->notifyhash[hash]; n; n = n->next) {
  3323. uaecptr nr = n->notifyrequest;
  3324. if (same_aname (n->partname, a->aname)) {
  3325. int err;
  3326. a_inode *a2 = find_aino(ctx, unit, 0, n->fullname, &err);
  3327. if (err == 0 && a == a2)
  3328. notify_send(ctx, unit, n);
  3329. }
  3330. }
  3331. if (a->parent) {
  3332. hash = notifyhash (a->parent->aname);
  3333. for (n = unit->notifyhash[hash]; n; n = n->next) {
  3334. uaecptr nr = n->notifyrequest;
  3335. if (same_aname (n->partname, a->parent->aname)) {
  3336. int err;
  3337. a_inode *a2 = find_aino(ctx, unit, 0, n->fullname, &err);
  3338. if (err == 0 && a->parent == a2)
  3339. notify_send(ctx, unit, n);
  3340. }
  3341. }
  3342. }
  3343. }
  3344. static void action_add_notify(TrapContext *ctx, Unit *unit, dpacket *packet)
  3345. {
  3346. uaecptr nr = GET_PCK_ARG1 (packet);
  3347. int flags;
  3348. Notify *n;
  3349. TCHAR *name, *p, *partname;
  3350. TRACE((_T("ACTION_ADD_NOTIFY\n")));
  3351. name = char1(ctx, trap_get_long(ctx, nr + 4));
  3352. flags = trap_get_long(ctx, nr + 12);
  3353. if (!(flags & (NRF_SEND_MESSAGE | NRF_SEND_SIGNAL))) {
  3354. PUT_PCK_RES1 (packet, DOS_FALSE);
  3355. PUT_PCK_RES2 (packet, ERROR_BAD_NUMBER);
  3356. return;
  3357. }
  3358. #if 0
  3359. write_log (_T("Notify:\n"));
  3360. write_log (_T("nr_Name '%s'\n"), char1 (trap_get_long(ctx, nr + 0)));
  3361. write_log (_T("nr_FullName '%s'\n"), name);
  3362. write_log (_T("nr_UserData %08X\n"), trap_get_long(ctx, nr + 8));
  3363. write_log (_T("nr_Flags %08X\n"), flags);
  3364. if (flags & NRF_SEND_MESSAGE) {
  3365. write_log (_T("Message NotifyRequest, port = %08X\n"), trap_get_long(ctx, nr + 16));
  3366. } else if (flags & NRF_SEND_SIGNAL) {
  3367. write_log (_T("Signal NotifyRequest, Task = %08X signal = %d\n"), trap_get_long(ctx, nr + 16), trap_get_long(ctx, nr + 20));
  3368. } else {
  3369. write_log (_T("corrupt NotifyRequest\n"));
  3370. }
  3371. #endif
  3372. p = name + _tcslen (name) - 1;
  3373. if (p[0] == ':')
  3374. p--;
  3375. while (p > name && p[0] != ':' && p[0] != '/')
  3376. p--;
  3377. if (p[0] == ':' || p[0] == '/')
  3378. p++;
  3379. partname = my_strdup (p);
  3380. n = new_notify (unit, partname);
  3381. n->notifyrequest = nr;
  3382. n->fullname = name;
  3383. if (flags & NRF_NOTIFY_INITIAL) {
  3384. int err;
  3385. a_inode *a = find_aino(ctx, unit, 0, n->fullname, &err);
  3386. if (err == 0)
  3387. notify_send(ctx, unit, n);
  3388. }
  3389. PUT_PCK_RES1 (packet, DOS_TRUE);
  3390. }
  3391. static void action_remove_notify(TrapContext *ctx, Unit *unit, dpacket *packet)
  3392. {
  3393. uaecptr nr = GET_PCK_ARG1 (packet);
  3394. Notify *n;
  3395. int hash;
  3396. TRACE((_T("ACTION_REMOVE_NOTIFY\n")));
  3397. for (hash = 0; hash < NOTIFY_HASH_SIZE; hash++) {
  3398. for (n = unit->notifyhash[hash]; n; n = n->next) {
  3399. if (n->notifyrequest == nr) {
  3400. //write_log (_T("NotifyRequest %08X freed\n"), n->notifyrequest);
  3401. xfree (n->fullname);
  3402. xfree (n->partname);
  3403. free_notify (unit, hash, n);
  3404. PUT_PCK_RES1 (packet, DOS_TRUE);
  3405. return;
  3406. }
  3407. }
  3408. }
  3409. write_log (_T("Tried to free non-existing NotifyRequest %08X\n"), nr);
  3410. PUT_PCK_RES1 (packet, DOS_TRUE);
  3411. }
  3412. static void free_lock(TrapContext *ctx, Unit *unit, uaecptr lock)
  3413. {
  3414. if (! lock)
  3415. return;
  3416. uaecptr curlock = get_long(unit->volume + 28);
  3417. if (lock == curlock << 2) {
  3418. put_long(unit->volume + 28, trap_get_long(ctx, lock));
  3419. } else {
  3420. uaecptr current = curlock;
  3421. uaecptr next = 0;
  3422. while (current) {
  3423. next = trap_get_long(ctx, current << 2);
  3424. if (lock == next << 2)
  3425. break;
  3426. current = next;
  3427. }
  3428. if (!current) {
  3429. write_log (_T("tried to unlock non-existing lock %x\n"), lock);
  3430. return;
  3431. }
  3432. trap_put_long(ctx, current << 2, trap_get_long(ctx, lock));
  3433. }
  3434. lock -= 4;
  3435. #if TRAPMD
  3436. struct trapmd md2[] =
  3437. {
  3438. { TRAPCMD_GET_LONG, { unit->locklist }, 1, 1 },
  3439. { TRAPCMD_PUT_LONG, { lock } },
  3440. { TRAPCMD_PUT_LONG, { unit->locklist, lock } }
  3441. };
  3442. trap_multi(ctx, md2, sizeof md2 / sizeof(struct trapmd));
  3443. #else
  3444. trap_put_long(ctx, lock, trap_get_long(ctx, unit->locklist));
  3445. trap_put_long(ctx, unit->locklist, lock);
  3446. #endif
  3447. }
  3448. static void action_lock(TrapContext *ctx, Unit *unit, dpacket *packet)
  3449. {
  3450. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  3451. uaecptr name = GET_PCK_ARG2 (packet) << 2;
  3452. int mode = GET_PCK_ARG3 (packet);
  3453. a_inode *a;
  3454. int err;
  3455. if (mode != SHARED_LOCK && mode != EXCLUSIVE_LOCK) {
  3456. TRACE((_T("Bad mode %d (should be %d or %d).\n"), mode, SHARED_LOCK, EXCLUSIVE_LOCK));
  3457. mode = SHARED_LOCK;
  3458. }
  3459. TRACE((_T("ACTION_LOCK(0x%08x, \"%s\", %d)\n"), lock, bstr(ctx, unit, name), mode));
  3460. DUMPLOCK(ctx, unit, lock);
  3461. a = find_aino(ctx, unit, lock, bstr(ctx, unit, name), &err);
  3462. if (err == 0 && a->softlink) {
  3463. err = test_softlink (a);
  3464. }
  3465. if (err == 0 && (a->elock || (mode != SHARED_LOCK && a->shlock > 0))) {
  3466. err = ERROR_OBJECT_IN_USE;
  3467. }
  3468. /* Lock() doesn't do access checks. */
  3469. if (err != 0) {
  3470. PUT_PCK_RES1 (packet, DOS_FALSE);
  3471. PUT_PCK_RES2 (packet, err);
  3472. return;
  3473. }
  3474. if (mode == SHARED_LOCK)
  3475. a->shlock++;
  3476. else
  3477. a->elock = 1;
  3478. de_recycle_aino (unit, a);
  3479. PUT_PCK_RES1(packet, make_lock(ctx, unit, a->uniq, mode) >> 2);
  3480. }
  3481. static void action_read_link_add_parent(Unit *u, a_inode *a, TCHAR *path)
  3482. {
  3483. if (a == NULL)
  3484. return;
  3485. action_read_link_add_parent(u, a->parent, path);
  3486. if (a == &u->rootnode) {
  3487. _tcscat (path, a->aname);
  3488. _tcscat (path, _T(":"));
  3489. } else {
  3490. if (path[0] && path[_tcslen (path) - 1] != ':')
  3491. _tcscat (path, _T("/"));
  3492. _tcscat (path, a->aname);
  3493. }
  3494. }
  3495. #define LINK_HARD 0
  3496. #define LINK_SOFT 1
  3497. static void action_make_link(TrapContext *ctx, Unit *unit, dpacket *packet)
  3498. {
  3499. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  3500. uaecptr name = GET_PCK_ARG2 (packet) << 2;
  3501. uaecptr target = GET_PCK_ARG3 (packet);
  3502. int type = GET_PCK_ARG4 (packet);
  3503. a_inode *a1, *a2;
  3504. int err;
  3505. TCHAR tmp[256], tmp2[MAX_DPATH], tmp3[MAX_DPATH];
  3506. _tcscpy (tmp, bstr(ctx, unit, name));
  3507. a1 = aino_from_lock(ctx, unit, lock);
  3508. if (type == LINK_HARD) {
  3509. // we don't support hard links
  3510. uaecptr tlock = target << 2;
  3511. a2 = aino_from_lock(ctx, unit, tlock);
  3512. write_log (_T("ACTION_MAKE_LINK(HARD,'%s','%s','%s')\n"),
  3513. a1 ? a1->aname : _T("?"), tmp,
  3514. a2 ? a2->aname : _T("?"));
  3515. PUT_PCK_RES1 (packet, DOS_FALSE);
  3516. PUT_PCK_RES2 (packet, ERROR_NOT_IMPLEMENTED);
  3517. } else {
  3518. a_inode *a3;
  3519. TCHAR *link = cstr(ctx, unit, target);
  3520. write_log (_T("ACTION_MAKE_LINK(SOFT,'%s','%s','%s')\n"),
  3521. a1 ? a1->aname : _T("?"), tmp, link);
  3522. if (!a1) {
  3523. PUT_PCK_RES1 (packet, DOS_FALSE);
  3524. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  3525. return;
  3526. }
  3527. // try to find softlink target
  3528. for (Unit *u = units; u; u = u->next) {
  3529. if (u->volflags & (MYVOLUMEINFO_ARCHIVE | MYVOLUMEINFO_CDFS))
  3530. continue;
  3531. a3 = find_aino(ctx, u, 0, link, &err);
  3532. if (err || !a3)
  3533. continue;
  3534. _tcscpy (tmp2, a1->nname);
  3535. _tcscat (tmp2, FSDB_DIR_SEPARATOR_S);
  3536. _tcscat (tmp2, tmp);
  3537. tmp3[0] = 0;
  3538. action_read_link_add_parent (u, a3, tmp3);
  3539. if (!my_createshortcut (tmp2, a3->nname, tmp3)) {
  3540. PUT_PCK_RES1 (packet, DOS_FALSE);
  3541. PUT_PCK_RES2 (packet, dos_errno ());
  3542. }
  3543. return;
  3544. }
  3545. // real Amiga softlinks would accept invalid paths too, we won't.
  3546. PUT_PCK_RES1 (packet, DOS_FALSE);
  3547. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  3548. }
  3549. }
  3550. static void action_read_link(TrapContext *ctx, Unit *unit, dpacket *packet)
  3551. {
  3552. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  3553. uaecptr name = GET_PCK_ARG2 (packet);
  3554. uaecptr newname = GET_PCK_ARG3 (packet);
  3555. int size = GET_PCK_ARG4 (packet);
  3556. a_inode *a, *matched_aino;
  3557. Unit *u = NULL, *u2 = NULL, *matched_unit;
  3558. int err, i, matched_len;
  3559. TCHAR tmp[MAX_DPATH];
  3560. TCHAR *namep, *extrapath;
  3561. extrapath = NULL;
  3562. namep = cstr(ctx, unit, name);
  3563. for (;;) {
  3564. a = find_aino(ctx, unit, lock, namep, &err);
  3565. if (err != ERROR_IS_SOFT_LINK)
  3566. break;
  3567. for (i = _tcslen (namep) - 1; i > 0; i--) {
  3568. if (namep[i] == '/') {
  3569. namep[i] = 0;
  3570. xfree (extrapath);
  3571. extrapath = my_strdup (namep + i + 1);
  3572. break;
  3573. }
  3574. }
  3575. }
  3576. if (!a->softlink)
  3577. err = ERROR_OBJECT_WRONG_TYPE;
  3578. if (err != 0) {
  3579. xfree(extrapath);
  3580. PUT_PCK_RES1 (packet, DOS_FALSE);
  3581. PUT_PCK_RES2 (packet, err);
  3582. return;
  3583. }
  3584. _tcscpy (tmp, a->nname);
  3585. write_log (_T("Resolving softlink '%s'\n"), tmp);
  3586. if (!my_resolvesoftlink (tmp, sizeof tmp / sizeof (TCHAR), false)) {
  3587. xfree(extrapath);
  3588. PUT_PCK_RES1 (packet, DOS_FALSE);
  3589. // not sure what to return
  3590. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  3591. return;
  3592. }
  3593. write_log (_T("-> '%s'\n"), tmp);
  3594. matched_aino = NULL;
  3595. matched_unit = NULL;
  3596. err = 0;
  3597. matched_len = 0;
  3598. for (u = units; u; u = u->next) {
  3599. if (!(u->volflags & (MYVOLUMEINFO_ARCHIVE | MYVOLUMEINFO_CDFS))) {
  3600. TCHAR path[MAX_DPATH];
  3601. i = my_issamevolume (u->rootnode.nname, tmp, path);
  3602. if (i > matched_len) {
  3603. a = find_aino(ctx, u, 0, path, &err);
  3604. if (a && !err) {
  3605. write_log (_T("Match found from '%s' (%d)\n"), u->rootnode.aname, i);
  3606. matched_aino = a;
  3607. matched_unit = u;
  3608. matched_len = i;
  3609. }
  3610. }
  3611. }
  3612. }
  3613. if (!matched_aino) {
  3614. xfree(extrapath);
  3615. write_log (_T("Path not found in any mounted drive\n"));
  3616. PUT_PCK_RES1 (packet, DOS_FALSE);
  3617. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  3618. return;
  3619. }
  3620. tmp[0] = 0;
  3621. action_read_link_add_parent (matched_unit, matched_aino, tmp);
  3622. if (extrapath) {
  3623. _tcscat (tmp, _T("/"));
  3624. _tcscat (tmp, extrapath);
  3625. }
  3626. xfree (extrapath);
  3627. write_log (_T("got target '%s'\n"), tmp);
  3628. char *s = ua_fs (tmp, -1);
  3629. for (i = 0; s[i]; i++) {
  3630. if (i >= size - 1)
  3631. break;
  3632. trap_put_byte(ctx, newname + i, s[i]);
  3633. }
  3634. xfree (s);
  3635. trap_put_byte(ctx, newname + i, 0);
  3636. }
  3637. static void action_free_lock(TrapContext *ctx, Unit *unit, dpacket *packet)
  3638. {
  3639. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  3640. a_inode *a;
  3641. TRACE((_T("ACTION_FREE_LOCK(0x%x)\n"), lock));
  3642. DUMPLOCK(ctx, unit, lock);
  3643. a = aino_from_lock(ctx, unit, lock);
  3644. if (a == 0) {
  3645. PUT_PCK_RES1 (packet, DOS_FALSE);
  3646. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  3647. return;
  3648. }
  3649. if (a->elock)
  3650. a->elock = 0;
  3651. else
  3652. a->shlock--;
  3653. recycle_aino (unit, a);
  3654. free_lock(ctx, unit, lock);
  3655. PUT_PCK_RES1 (packet, DOS_TRUE);
  3656. }
  3657. static uaecptr action_dup_lock_2(TrapContext *ctx, Unit *unit, dpacket *packet, uae_u32 uniq)
  3658. {
  3659. uaecptr out;
  3660. a_inode *a;
  3661. a = lookup_aino (unit, uniq);
  3662. if (a == 0) {
  3663. PUT_PCK_RES1 (packet, DOS_FALSE);
  3664. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  3665. return 0;
  3666. }
  3667. /* DupLock()ing exclusive locks isn't possible, says the Autodoc, but
  3668. * at least the RAM-Handler seems to allow it. Let's see what happens
  3669. * if we don't. */
  3670. if (a->elock) {
  3671. PUT_PCK_RES1 (packet, DOS_FALSE);
  3672. PUT_PCK_RES2 (packet, ERROR_OBJECT_IN_USE);
  3673. return 0;
  3674. }
  3675. a->shlock++;
  3676. de_recycle_aino(unit, a);
  3677. out = make_lock(ctx, unit, a->uniq, -2) >> 2;
  3678. PUT_PCK_RES1 (packet, out);
  3679. return out;
  3680. }
  3681. static void action_dup_lock(TrapContext *ctx, Unit *unit, dpacket *packet)
  3682. {
  3683. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  3684. TRACE((_T("ACTION_DUP_LOCK(0x%x)\n"), lock));
  3685. if (!lock) {
  3686. PUT_PCK_RES1 (packet, 0);
  3687. return;
  3688. }
  3689. action_dup_lock_2(ctx, unit, packet, trap_get_long(ctx, lock + 4));
  3690. }
  3691. static void action_lock_from_fh(TrapContext *ctx, Unit *unit, dpacket *packet)
  3692. {
  3693. Key *k = lookup_key (unit, GET_PCK_ARG1 (packet));
  3694. TRACE((_T("ACTION_COPY_DIR_FH(0x%x,'%s')\n"), GET_PCK_ARG1 (packet), k ? k->aino->aname : _T("<null>")));
  3695. if (k == 0) {
  3696. PUT_PCK_RES1 (packet, DOS_FALSE);
  3697. return;
  3698. }
  3699. action_dup_lock_2(ctx, unit, packet, k->aino->uniq);
  3700. }
  3701. static void free_exkey (Unit *unit, a_inode *aino)
  3702. {
  3703. if (--aino->exnext_count == 0) {
  3704. TRACE ((_T("Freeing ExKey and reducing total_locked from %d by %d\n"),
  3705. unit->total_locked_ainos, aino->locked_children));
  3706. unit->total_locked_ainos -= aino->locked_children;
  3707. aino->locked_children = 0;
  3708. }
  3709. }
  3710. static void move_exkeys (Unit *unit, a_inode *from, a_inode *to)
  3711. {
  3712. to->exnext_count = from->exnext_count;
  3713. to->locked_children = from->locked_children;
  3714. from->exnext_count = 0;
  3715. from->locked_children = 0;
  3716. }
  3717. static bool get_statinfo(Unit *unit, a_inode *aino, struct mystat *statbuf)
  3718. {
  3719. bool ok = true;
  3720. memset (statbuf, 0, sizeof(struct mystat));
  3721. /* No error checks - this had better work. */
  3722. if (unit->volflags & MYVOLUMEINFO_ARCHIVE)
  3723. ok = zfile_stat_archive (aino->nname, statbuf) != 0;
  3724. else if (unit->volflags & MYVOLUMEINFO_CDFS)
  3725. ok = isofs_stat (unit->ui.cdfs_superblock, aino->uniq_external, statbuf);
  3726. else
  3727. my_stat (aino->nname, statbuf);
  3728. return ok;
  3729. }
  3730. static void get_fileinfo(TrapContext *ctx, Unit *unit, dpacket *packet, uaecptr info, a_inode *aino, bool longfilesize)
  3731. {
  3732. struct mystat statbuf;
  3733. int days, mins, ticks;
  3734. int i, n, entrytype, blocksize;
  3735. uae_s64 numblocks;
  3736. int fsdb_can = fsdb_cando (unit);
  3737. const TCHAR *xs;
  3738. char *x, *x2;
  3739. uae_u8 *buf;
  3740. uae_u8 buf_array[260] = { 0 };
  3741. if (trap_is_indirect() || !valid_address(info, (sizeof buf_array) - 36) || !real_address_allowed()) {
  3742. buf = buf_array;
  3743. } else {
  3744. buf = get_real_address(info);
  3745. }
  3746. if (aino->vfso) {
  3747. fsdb_can = 1;
  3748. statbuf.mode = aino->vfso->amigaos_mode;
  3749. statbuf.mtime.tv_sec = 0;
  3750. statbuf.mtime.tv_usec = 0;
  3751. statbuf.size = aino->vfso->size;
  3752. } else if (!get_statinfo(unit, aino, &statbuf)) {
  3753. PUT_PCK_RES1 (packet, DOS_FALSE);
  3754. PUT_PCK_RES2 (packet, ERROR_NOT_A_DOS_DISK);
  3755. return;
  3756. }
  3757. put_long_host(buf + 0, aino->uniq);
  3758. if (aino->parent == 0) {
  3759. /* Guru book says ST_ROOT = 1 (root directory, not currently used)
  3760. * but some programs really expect 2 from root dir..
  3761. */
  3762. entrytype = ST_USERDIR;
  3763. xs = unit->ui.volname;
  3764. } else {
  3765. entrytype = aino->softlink ? ST_SOFTLINK : (aino->dir ? ST_USERDIR : ST_FILE);
  3766. xs = aino->aname;
  3767. }
  3768. put_long_host(buf + 4, entrytype);
  3769. /* AmigaOS docs say these have to contain the same value. */
  3770. put_long_host(buf + 120, entrytype);
  3771. TRACE((_T("name=\"%s\"\n"), xs));
  3772. x2 = x = ua_fs (xs, -1);
  3773. n = strlen (x);
  3774. if (n > 107)
  3775. n = 107;
  3776. if (n > abs(currprefs.filesys_max_name))
  3777. n = abs(currprefs.filesys_max_name);
  3778. i = 8;
  3779. put_byte_host(buf + i, n); i++;
  3780. while (n--)
  3781. put_byte_host(buf + i, *x), i++, x++;
  3782. while (i < 108)
  3783. put_byte_host(buf + i, 0), i++;
  3784. xfree (x2);
  3785. put_long_host(buf + 116, fsdb_can ? aino->amigaos_mode : fsdb_mode_supported (aino));
  3786. if (kickstart_version >= 36) {
  3787. put_word_host(buf + 224, 0); // OwnerUID
  3788. put_word_host(buf + 226, 0); // OwnerGID
  3789. }
  3790. blocksize = (unit->volflags & MYVOLUMEINFO_CDFS) ? 2048 : 512;
  3791. numblocks = (statbuf.size + blocksize - 1) / blocksize;
  3792. put_long_host(buf + 128, numblocks > MAXFILESIZE32 ? MAXFILESIZE32 : numblocks);
  3793. if (longfilesize) {
  3794. /* MorphOS 64-bit file length support */
  3795. put_long_host(buf + 124, statbuf.size > MAXFILESIZE32_2G ? 0 : (uae_u32)statbuf.size);
  3796. put_long_host(buf + 228, statbuf.size >> 32);
  3797. put_long_host(buf + 232, (uae_u32)statbuf.size);
  3798. put_long_host(buf + 236, numblocks >> 32);
  3799. put_long_host(buf + 240, (uae_u32)numblocks);
  3800. } else {
  3801. put_long_host(buf + 124, statbuf.size > MAXFILESIZE32 ? MAXFILESIZE32 : (uae_u32)statbuf.size);
  3802. }
  3803. timeval_to_amiga (&statbuf.mtime, &days, &mins, &ticks, 50);
  3804. put_long_host(buf + 132, days);
  3805. put_long_host(buf + 136, mins);
  3806. put_long_host(buf + 140, ticks);
  3807. if (aino->comment == 0 || !fsdb_can)
  3808. put_long_host(buf + 144, 0);
  3809. else {
  3810. TRACE((_T("comment=\"%s\"\n"), aino->comment));
  3811. i = 144;
  3812. xs = aino->comment;
  3813. if (!xs)
  3814. xs= _T("");
  3815. x2 = x = ua_fs(xs, -1);
  3816. n = strlen(x);
  3817. if (n > 78)
  3818. n = 78;
  3819. put_byte_host(buf + i, n); i++;
  3820. while (n--)
  3821. put_byte_host(buf + i, *x), i++, x++;
  3822. while (i < 224)
  3823. put_byte_host(buf + i, 0), i++;
  3824. xfree (x2);
  3825. }
  3826. if (buf == buf_array) {
  3827. // Must not write Fib_reserved at the end.
  3828. if (kickstart_version >= 36) {
  3829. // FIB + fib_OwnerUID and fib_OwnerGID
  3830. trap_put_bytes(ctx, buf, info, (sizeof buf_array) - 32);
  3831. } else {
  3832. // FIB only
  3833. trap_put_bytes(ctx, buf, info, (sizeof buf_array) - 36);
  3834. }
  3835. }
  3836. PUT_PCK_RES1 (packet, DOS_TRUE);
  3837. }
  3838. int get_native_path(TrapContext *ctx, uae_u32 lock, TCHAR *out)
  3839. {
  3840. int i = 0;
  3841. for (i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  3842. if (mountinfo.ui[i].self) {
  3843. a_inode *a = aino_from_lock(ctx, mountinfo.ui[i].self, lock << 2);
  3844. if (a) {
  3845. _tcscpy (out, a->nname);
  3846. return 0;
  3847. }
  3848. }
  3849. }
  3850. return -1;
  3851. }
  3852. #define REC_EXCLUSIVE 0
  3853. #define REC_EXCLUSIVE_IMMED 1
  3854. #define REC_SHARED 2
  3855. #define REC_SHARED_IMMED 3
  3856. static struct lockrecord *new_record (dpacket *packet, uae_u64 pos, uae_u64 len, uae_u32 mode, uae_u32 timeout, uae_u32 msg)
  3857. {
  3858. struct lockrecord *lr = xcalloc (struct lockrecord, 1);
  3859. lr->packet = packet;
  3860. lr->pos = pos;
  3861. lr->len = len;
  3862. lr->mode = mode;
  3863. lr->timeout = timeout * vblank_hz / 50;
  3864. lr->msg = msg;
  3865. return lr;
  3866. }
  3867. static bool record_hit (Unit *unit, Key *k, uae_u64 pos, uae_u64 len, uae_u32 mode)
  3868. {
  3869. bool exclusive = mode == REC_EXCLUSIVE || mode == REC_EXCLUSIVE_IMMED;
  3870. for (Key *k2 = unit->keys; k2; k2 = k2->next) {
  3871. if (k2->aino->uniq == k->aino->uniq) {
  3872. if (k2 == k)
  3873. continue;
  3874. for (struct lockrecord *lr = k2->record; lr; lr = lr->next) {
  3875. bool exclusive2 = lr->mode == REC_EXCLUSIVE || lr->mode == REC_EXCLUSIVE_IMMED;
  3876. if (exclusive || exclusive2) {
  3877. uae_u64 a1 = pos;
  3878. uae_u64 a2 = pos + len;
  3879. uae_u64 b1 = lr->pos;
  3880. uae_u64 b2 = lr->pos + lr->len;
  3881. if (len && lr->len) {
  3882. bool hit = (a1 >= b1 && a1 < b2) || (a2 > b1 && a2 < b2) || (b1 >= a1 && b1 < a2) || (b2 > a1 && b2 < a2);
  3883. if (hit)
  3884. return true;
  3885. }
  3886. }
  3887. }
  3888. }
  3889. }
  3890. return false;
  3891. }
  3892. static void record_timeout(TrapContext *ctx, Unit *unit)
  3893. {
  3894. bool retry = true;
  3895. while (retry) {
  3896. retry = false;
  3897. struct lockrecord *prev = NULL;
  3898. for (struct lockrecord *lr = unit->waitingrecords; lr; lr = lr->next) {
  3899. lr->timeout--;
  3900. if (lr->timeout == 0) {
  3901. Key *k = lookup_key (unit, GET_PCK_ARG1 (lr->packet));
  3902. PUT_PCK_RES1 (lr->packet, DOS_FALSE);
  3903. PUT_PCK_RES2 (lr->packet, ERROR_LOCK_TIMEOUT);
  3904. // mark packet as complete
  3905. trap_put_long(ctx, lr->msg + 4, 0xfffffffe);
  3906. uae_Signal (get_long(unit->volume + 176 - 32), 1 << 13);
  3907. if (prev)
  3908. prev->next = lr->next;
  3909. else
  3910. unit->waitingrecords = lr->next;
  3911. write_log (_T("queued record timed out '%s',%lld,%lld,%d,%d\n"), k ? k->aino->nname : _T("NULL"), lr->pos, lr->len, lr->mode, lr->timeout);
  3912. xfree (lr);
  3913. retry = true;
  3914. break;
  3915. }
  3916. prev = lr;
  3917. }
  3918. }
  3919. }
  3920. static void record_check_waiting(TrapContext *ctx, Unit *unit)
  3921. {
  3922. bool retry = true;
  3923. while (retry) {
  3924. retry = false;
  3925. struct lockrecord *prev = NULL;
  3926. for (struct lockrecord *lr = unit->waitingrecords; lr; lr = lr->next) {
  3927. Key *k = lookup_key (unit, GET_PCK_ARG1 (lr->packet));
  3928. if (!k || !record_hit (unit, k, lr->pos, lr->len, lr->mode)) {
  3929. if (prev)
  3930. prev->next = lr->next;
  3931. else
  3932. unit->waitingrecords = lr->next;
  3933. write_log (_T("queued record released '%s',%llud,%llu,%d,%d\n"), k->aino->nname, lr->pos, lr->len, lr->mode, lr->timeout);
  3934. // mark packet as complete
  3935. trap_put_long(ctx, lr->msg + 4, 0xffffffff);
  3936. xfree (lr);
  3937. retry = true;
  3938. break;
  3939. }
  3940. prev = lr;
  3941. }
  3942. }
  3943. }
  3944. static int action_lock_record(TrapContext *ctx, Unit *unit, dpacket *packet, uae_u32 msg)
  3945. {
  3946. Key *k = lookup_key (unit, GET_PCK_ARG1 (packet));
  3947. uae_u32 pos = GET_PCK_ARG2 (packet);
  3948. uae_u32 len = GET_PCK_ARG3 (packet);
  3949. uae_u32 mode = GET_PCK_ARG4 (packet);
  3950. uae_u32 timeout = GET_PCK_ARG5 (packet);
  3951. bool exclusive = mode == REC_EXCLUSIVE || mode == REC_EXCLUSIVE_IMMED;
  3952. write_log (_T("action_lock_record('%s',%d,%d,%d,%d)\n"), k ? k->aino->nname : _T("null"), pos, len, mode, timeout);
  3953. if (!k || mode > REC_SHARED_IMMED) {
  3954. PUT_PCK_RES1 (packet, DOS_FALSE);
  3955. PUT_PCK_RES2 (packet, ERROR_OBJECT_WRONG_TYPE);
  3956. return 1;
  3957. }
  3958. if (mode == REC_EXCLUSIVE_IMMED || mode == REC_SHARED_IMMED)
  3959. timeout = 0;
  3960. if (record_hit (unit, k, pos, len, mode)) {
  3961. if (timeout && msg) {
  3962. // queue it and do not reply
  3963. struct lockrecord *lr = new_record (packet, pos, len, mode, timeout, msg);
  3964. if (unit->waitingrecords) {
  3965. lr->next = unit->waitingrecords;
  3966. unit->waitingrecords = lr;
  3967. } else {
  3968. unit->waitingrecords = lr;
  3969. }
  3970. write_log (_T("-> collision, timeout queued\n"));
  3971. return -1;
  3972. }
  3973. PUT_PCK_RES1 (packet, DOS_FALSE);
  3974. PUT_PCK_RES2 (packet, ERROR_LOCK_COLLISION);
  3975. write_log (_T("-> ERROR_LOCK_COLLISION\n"));
  3976. return 1;
  3977. }
  3978. struct lockrecord *lr = new_record (packet, pos, len, mode, timeout, 0);
  3979. if (k->record) {
  3980. lr->next = k->record;
  3981. k->record = lr;
  3982. } else {
  3983. k->record = lr;
  3984. }
  3985. PUT_PCK_RES1 (packet, DOS_TRUE);
  3986. write_log (_T("-> OK\n"));
  3987. return 1;
  3988. }
  3989. static void action_free_record(TrapContext *ctx, Unit *unit, dpacket *packet)
  3990. {
  3991. Key *k = lookup_key (unit, GET_PCK_ARG1 (packet));
  3992. uae_u32 pos = GET_PCK_ARG2 (packet);
  3993. uae_u32 len = GET_PCK_ARG3 (packet);
  3994. write_log (_T("action_free_record('%s',%d,%d)\n"), k ? k->aino->nname : _T("null"), pos, len);
  3995. if (!k) {
  3996. PUT_PCK_RES1 (packet, DOS_FALSE);
  3997. PUT_PCK_RES2 (packet, ERROR_OBJECT_WRONG_TYPE);
  3998. return;
  3999. }
  4000. struct lockrecord *prev = NULL;
  4001. for (struct lockrecord *lr = k->record; lr; lr = lr->next) {
  4002. if (lr->pos == pos && lr->len == len) {
  4003. if (prev)
  4004. prev->next = lr->next;
  4005. else
  4006. k->record = lr->next;
  4007. xfree (lr);
  4008. write_log (_T("->OK\n"));
  4009. record_check_waiting(ctx, unit);
  4010. PUT_PCK_RES1 (packet, DOS_TRUE);
  4011. return;
  4012. }
  4013. }
  4014. write_log (_T("-> ERROR_RECORD_NOT_LOCKED\n"));
  4015. PUT_PCK_RES1 (packet, DOS_FALSE);
  4016. PUT_PCK_RES2 (packet, ERROR_RECORD_NOT_LOCKED);
  4017. }
  4018. #define EXALL_DEBUG 0
  4019. #define EXALL_END 0xde1111ad
  4020. static ExAllKey *getexall (Unit *unit, uaecptr control, int id)
  4021. {
  4022. int i;
  4023. if (id < 0) {
  4024. for (i = 0; i < EXALLKEYS; i++) {
  4025. if (unit->exalls[i].id == 0) {
  4026. unit->exallid++;
  4027. if (unit->exallid == EXALL_END)
  4028. unit->exallid++;
  4029. unit->exalls[i].id = unit->exallid;
  4030. unit->exalls[i].control = control;
  4031. return &unit->exalls[i];
  4032. }
  4033. }
  4034. } else if (id > 0) {
  4035. for (i = 0; i < EXALLKEYS; i++) {
  4036. if (unit->exalls[i].id == id)
  4037. return &unit->exalls[i];
  4038. }
  4039. }
  4040. return NULL;
  4041. }
  4042. static int exalldo(TrapContext *ctx, uaecptr exalldata, uae_u32 exalldatasize, uae_u32 type, uaecptr control, Unit *unit, a_inode *aino)
  4043. {
  4044. uaecptr exp = exalldata;
  4045. int i;
  4046. int size, size2;
  4047. int entrytype;
  4048. const TCHAR *xs = NULL, *commentx = NULL;
  4049. uae_u32 flags = 15;
  4050. int days, mins, ticks;
  4051. struct mystat statbuf;
  4052. int fsdb_can = fsdb_cando (unit);
  4053. uae_u16 uid = 0, gid = 0;
  4054. char *x = NULL, *comment = NULL;
  4055. int ret = 0;
  4056. memset (&statbuf, 0, sizeof statbuf);
  4057. if (unit->volflags & MYVOLUMEINFO_ARCHIVE)
  4058. zfile_stat_archive (aino->nname, &statbuf);
  4059. else if (unit->volflags & MYVOLUMEINFO_CDFS)
  4060. isofs_stat (unit->ui.cdfs_superblock, aino->uniq_external, &statbuf);
  4061. else
  4062. my_stat (aino->nname, &statbuf);
  4063. if (aino->parent == 0) {
  4064. entrytype = ST_USERDIR;
  4065. xs = unit->ui.volname;
  4066. } else {
  4067. entrytype = aino->softlink ? ST_SOFTLINK : (aino->dir ? ST_USERDIR : ST_FILE);
  4068. xs = aino->aname;
  4069. }
  4070. x = ua_fs (xs, -1);
  4071. size = 0;
  4072. size2 = 4;
  4073. if (type >= 1) {
  4074. size2 += 4;
  4075. size += strlen (x) + 1;
  4076. size = (size + 3) & ~3;
  4077. }
  4078. if (type >= 2)
  4079. size2 += 4;
  4080. if (type >= 3)
  4081. size2 += 4;
  4082. if (type >= 4) {
  4083. flags = fsdb_can ? aino->amigaos_mode : fsdb_mode_supported (aino);
  4084. size2 += 4;
  4085. }
  4086. if (type >= 5) {
  4087. timeval_to_amiga (&statbuf.mtime, &days, &mins, &ticks, 50);
  4088. size2 += 12;
  4089. }
  4090. if (type >= 6) {
  4091. size2 += 4;
  4092. if (aino->comment == 0 || !fsdb_can)
  4093. commentx = _T("");
  4094. else
  4095. commentx = aino->comment;
  4096. comment = ua_fs (commentx, -1);
  4097. size += strlen (comment) + 1;
  4098. size = (size + 3) & ~3;
  4099. }
  4100. if (type >= 7) {
  4101. size2 += 4;
  4102. uid = 0;
  4103. gid = 0;
  4104. }
  4105. if (type >= 8) {
  4106. size2 += 8;
  4107. }
  4108. i = trap_get_long(ctx, control + 0);
  4109. while (i > 0) {
  4110. exp = trap_get_long(ctx, exp); /* ed_Next */
  4111. i--;
  4112. }
  4113. if (exalldata + exalldatasize - exp < size + size2)
  4114. goto end; /* not enough space */
  4115. #if EXALL_DEBUG > 0
  4116. write_log (_T("ID=%d, %d, %08x: '%s'%s\n"),
  4117. trap_get_long(ctx, control + 4), trap_get_long(ctx, control + 0), exp, xs, aino->dir ? _T(" [DIR]") : _T(""));
  4118. #endif
  4119. trap_put_long(ctx, exp, exp + size + size2); /* ed_Next */
  4120. if (type >= 1) {
  4121. trap_put_long(ctx, exp + 4, exp + size2);
  4122. for (i = 0; i <= strlen (x); i++) {
  4123. trap_put_byte(ctx, exp + size2, x[i]);
  4124. size2++;
  4125. }
  4126. }
  4127. if (type >= 2)
  4128. trap_put_long(ctx, exp + 8, entrytype);
  4129. if (type >= 3)
  4130. trap_put_long(ctx, exp + 12, statbuf.size > MAXFILESIZE32 ? MAXFILESIZE32 : statbuf.size);
  4131. if (type >= 4)
  4132. trap_put_long(ctx, exp + 16, flags);
  4133. if (type >= 5) {
  4134. trap_put_long(ctx, exp + 20, days);
  4135. trap_put_long(ctx, exp + 24, mins);
  4136. trap_put_long(ctx, exp + 28, ticks);
  4137. }
  4138. if (type >= 6) {
  4139. trap_put_long(ctx, exp + 32, exp + size2);
  4140. trap_put_byte(ctx, exp + size2, strlen (comment));
  4141. for (i = 0; i <= strlen (comment); i++) {
  4142. trap_put_byte(ctx, exp + size2, comment[i]);
  4143. size2++;
  4144. }
  4145. }
  4146. if (type >= 7) {
  4147. trap_put_word(ctx, exp + 36, uid);
  4148. trap_put_word(ctx, exp + 38, gid);
  4149. }
  4150. if (type >= 8) {
  4151. trap_put_long(ctx, exp + 40, statbuf.size >> 32);
  4152. trap_put_long(ctx, exp + 44, (uae_u32)statbuf.size);
  4153. }
  4154. trap_put_long(ctx, control + 0, trap_get_long(ctx, control + 0) + 1);
  4155. ret = 1;
  4156. end:
  4157. xfree (x);
  4158. xfree (comment);
  4159. return ret;
  4160. }
  4161. static bool filesys_name_invalid(const TCHAR *fn)
  4162. {
  4163. return _tcslen (fn) > currprefs.filesys_max_name;
  4164. }
  4165. static int filesys_readdir(struct fs_dirhandle *d, TCHAR *fn, uae_u64 *uniq)
  4166. {
  4167. int ok = 0;
  4168. if (d->fstype == FS_ARCHIVE)
  4169. ok = zfile_readdir_archive(d->zd, fn);
  4170. else if (d->fstype == FS_DIRECTORY)
  4171. ok = my_readdir(d->od, fn);
  4172. else if (d->fstype == FS_CDFS)
  4173. ok = isofs_readdir(d->isod, fn, uniq);
  4174. return ok;
  4175. }
  4176. static int action_examine_all_do(TrapContext *ctx, Unit *unit, uaecptr lock, ExAllKey *eak, uaecptr exalldata, uae_u32 exalldatasize, uae_u32 type, uaecptr control)
  4177. {
  4178. a_inode *aino, *base = NULL;
  4179. int ok;
  4180. uae_u32 err;
  4181. struct fs_dirhandle *d;
  4182. TCHAR fn[MAX_DPATH];
  4183. if (lock != 0)
  4184. base = aino_from_lock(ctx, unit, lock);
  4185. if (base == 0)
  4186. base = &unit->rootnode;
  4187. for (;;) {
  4188. uae_u64 uniq = 0;
  4189. d = eak->dirhandle;
  4190. if (!eak->fn) {
  4191. do {
  4192. ok = filesys_readdir(d, fn, &uniq);
  4193. } while (ok && d->fstype == FS_DIRECTORY && (filesys_name_invalid (fn) || fsdb_name_invalid_dir (NULL, fn)));
  4194. if (!ok)
  4195. return 0;
  4196. } else {
  4197. _tcscpy (fn, eak->fn);
  4198. xfree (eak->fn);
  4199. eak->fn = NULL;
  4200. }
  4201. aino = lookup_child_aino_for_exnext (unit, base, fn, &err, uniq, NULL);
  4202. if (!aino)
  4203. return 0;
  4204. eak->id = unit->exallid++;
  4205. trap_put_long(ctx, control + 4, eak->id);
  4206. if (!exalldo(ctx, exalldata, exalldatasize, type, control, unit, aino)) {
  4207. eak->fn = my_strdup (fn); /* no space in exallstruct, save current entry */
  4208. break;
  4209. }
  4210. }
  4211. return 1;
  4212. }
  4213. static int action_examine_all_end(TrapContext *ctx, Unit *unit, dpacket *packet)
  4214. {
  4215. uae_u32 id;
  4216. uae_u32 doserr = 0;
  4217. ExAllKey *eak;
  4218. uaecptr control = GET_PCK_ARG5 (packet);
  4219. if (kickstart_version < 36)
  4220. return 0;
  4221. id = trap_get_long(ctx, control + 4);
  4222. eak = getexall (unit, control, id);
  4223. #if EXALL_DEBUG > 0
  4224. write_log (_T("EXALL_END ID=%d %x\n"), id, eak);
  4225. #endif
  4226. if (!eak) {
  4227. write_log (_T("FILESYS: EXALL_END non-existing ID %d\n"), id);
  4228. doserr = ERROR_OBJECT_WRONG_TYPE;
  4229. } else {
  4230. eak->id = 0;
  4231. fs_closedir (eak->dirhandle);
  4232. xfree (eak->fn);
  4233. eak->fn = NULL;
  4234. eak->dirhandle = NULL;
  4235. }
  4236. if (doserr) {
  4237. PUT_PCK_RES1 (packet, DOS_FALSE);
  4238. PUT_PCK_RES2 (packet, doserr);
  4239. } else {
  4240. PUT_PCK_RES1 (packet, DOS_TRUE);
  4241. }
  4242. return 1;
  4243. }
  4244. static int action_examine_all(TrapContext *ctx, Unit *unit, dpacket *packet)
  4245. {
  4246. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  4247. uaecptr exalldata = GET_PCK_ARG2 (packet);
  4248. uae_u32 exalldatasize = GET_PCK_ARG3 (packet);
  4249. uae_u32 type = GET_PCK_ARG4 (packet);
  4250. uaecptr control = GET_PCK_ARG5 (packet);
  4251. ExAllKey *eak = NULL;
  4252. a_inode *base = NULL;
  4253. struct fs_dirhandle *d;
  4254. int ok, i;
  4255. uaecptr exp;
  4256. uae_u32 id, doserr = ERROR_NO_MORE_ENTRIES;
  4257. ok = 0;
  4258. #if EXALL_DEBUG > 0
  4259. write_log (_T("exall: %08x %08x-%08x %d %d %08x\n"),
  4260. lock, exalldata, exalldata + exalldatasize, exalldatasize, type, control);
  4261. write_log (_T("exall: MatchString %08x, MatchFunc %08x\n"),
  4262. trap_get_long(ctx, control + 8), trap_get_long(ctx, control + 12));
  4263. #endif
  4264. trap_put_long(ctx, control + 0, 0); /* eac_Entries */
  4265. /* EXAMINE ALL might use dos.library MatchPatternNoCase() which is >=36 */
  4266. if (kickstart_version < 36)
  4267. return 0;
  4268. if (type == 0 || type > 8) {
  4269. doserr = ERROR_BAD_NUMBER;
  4270. goto fail;
  4271. }
  4272. PUT_PCK_RES1 (packet, DOS_TRUE);
  4273. id = trap_get_long(ctx, control + 4);
  4274. if (id == EXALL_END) {
  4275. write_log (_T("FILESYS: EXALL called twice with ERROR_NO_MORE_ENTRIES\n"));
  4276. goto fail; /* already ended exall() */
  4277. }
  4278. if (id) {
  4279. eak = getexall (unit, control, id);
  4280. if (!eak) {
  4281. write_log (_T("FILESYS: EXALL non-existing ID %d\n"), id);
  4282. doserr = ERROR_OBJECT_WRONG_TYPE;
  4283. goto fail;
  4284. }
  4285. if (!action_examine_all_do(ctx, unit, lock, eak, exalldata, exalldatasize, type, control))
  4286. goto fail;
  4287. if (trap_get_long(ctx, control + 0) == 0) {
  4288. /* uh, no space for first entry.. */
  4289. doserr = ERROR_NO_FREE_STORE;
  4290. goto fail;
  4291. }
  4292. } else {
  4293. eak = getexall (unit, control, -1);
  4294. if (!eak)
  4295. goto fail;
  4296. if (lock != 0)
  4297. base = aino_from_lock(ctx, unit, lock);
  4298. if (base == 0)
  4299. base = &unit->rootnode;
  4300. #if EXALL_DEBUG > 0
  4301. write_log("exall: ID=%d '%s'\n", eak->id, base->nname);
  4302. #endif
  4303. d = fs_opendir (unit, base);
  4304. if (!d)
  4305. goto fail;
  4306. eak->dirhandle = d;
  4307. trap_put_long(ctx, control + 4, eak->id);
  4308. if (!action_examine_all_do(ctx, unit, lock, eak, exalldata, exalldatasize, type, control))
  4309. goto fail;
  4310. if (trap_get_long(ctx, control + 0) == 0) {
  4311. /* uh, no space for first entry.. */
  4312. doserr = ERROR_NO_FREE_STORE;
  4313. goto fail;
  4314. }
  4315. }
  4316. ok = 1;
  4317. fail:
  4318. /* Clear last ed_Next. This "list" is quite non-Amiga like.. */
  4319. exp = exalldata;
  4320. i = trap_get_long(ctx, control + 0);
  4321. for (;;) {
  4322. if (i <= 1) {
  4323. if (exp)
  4324. trap_put_long(ctx, exp, 0);
  4325. break;
  4326. }
  4327. exp = trap_get_long(ctx, exp); /* ed_Next */
  4328. i--;
  4329. }
  4330. #if EXALL_DEBUG > 0
  4331. write_log("ok=%d, err=%d, eac_Entries = %d\n", ok, ok ? -1 : doserr, trap_get_long(ctx, control + 0));
  4332. #endif
  4333. if (!ok) {
  4334. PUT_PCK_RES1 (packet, DOS_FALSE);
  4335. PUT_PCK_RES2 (packet, doserr);
  4336. if (eak) {
  4337. eak->id = 0;
  4338. fs_closedir (eak->dirhandle);
  4339. eak->dirhandle = NULL;
  4340. xfree (eak->fn);
  4341. eak->fn = NULL;
  4342. }
  4343. if (doserr == ERROR_NO_MORE_ENTRIES)
  4344. trap_put_long(ctx, control + 4, EXALL_END);
  4345. }
  4346. return 1;
  4347. }
  4348. static uae_u32 exall_helper(TrapContext *ctx)
  4349. {
  4350. int i;
  4351. Unit *u;
  4352. uaecptr pck = trap_get_areg(ctx, 4);
  4353. dpacket packet;
  4354. readdpacket(ctx, &packet, pck);
  4355. uaecptr control = get_long_host(packet.packet_data + dp_Arg5);
  4356. uae_u32 id = trap_get_long(ctx, control + 4);
  4357. #if EXALL_DEBUG > 0
  4358. write_log (_T("FILESYS: EXALL extra round ID=%d\n"), id);
  4359. #endif
  4360. if (id == EXALL_END)
  4361. return 1;
  4362. for (u = units; u; u = u->next) {
  4363. for (i = 0; i < EXALLKEYS; i++) {
  4364. if (u->exalls[i].id == id && u->exalls[i].control == control) {
  4365. action_examine_all(ctx, u, &packet);
  4366. }
  4367. }
  4368. }
  4369. return 1;
  4370. }
  4371. static uae_u32 REGPARAM2 fsmisc_helper(TrapContext *ctx)
  4372. {
  4373. int mode = trap_get_dreg(ctx, 0);
  4374. switch (mode)
  4375. {
  4376. case 0:
  4377. return exall_helper(ctx);
  4378. case 1:
  4379. return filesys_media_change_reply (0);
  4380. case 2:
  4381. return filesys_media_change_reply (1);
  4382. case 3:
  4383. uae_u32 t = getlocaltime ();
  4384. uae_u32 secs = (uae_u32)t - (8 * 365 + 2) * 24 * 60 * 60;
  4385. return secs;
  4386. }
  4387. return 0;
  4388. }
  4389. static void action_examine_object(TrapContext *ctx, Unit *unit, dpacket *packet)
  4390. {
  4391. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  4392. uaecptr info = GET_PCK_ARG2 (packet) << 2;
  4393. a_inode *aino = 0;
  4394. TRACE((_T("ACTION_EXAMINE_OBJECT(0x%x,0x%x)\n"), lock, info));
  4395. DUMPLOCK(ctx, unit, lock);
  4396. if (lock != 0)
  4397. aino = aino_from_lock(ctx, unit, lock);
  4398. if (aino == 0)
  4399. aino = &unit->rootnode;
  4400. get_fileinfo(ctx, unit, packet, info, aino, false);
  4401. }
  4402. extern unsigned char def_tool[];
  4403. extern unsigned int def_tool_len;
  4404. extern unsigned char def_project[];
  4405. extern unsigned int def_project_len;
  4406. extern unsigned char def_drawer[];
  4407. extern unsigned int def_drawer_len;
  4408. static struct virtualfilesysobject vfso_icon_tool;
  4409. static struct virtualfilesysobject vfso_icon_project;
  4410. static struct virtualfilesysobject vfso_icon_drawer;
  4411. static void load_injected_icon(struct virtualfilesysobject *vfso, const TCHAR *fn, uae_u8 *default_data, int default_size)
  4412. {
  4413. uae_u8 *data = NULL;
  4414. int size;
  4415. xfree(vfso->data);
  4416. if (fn && fn[0])
  4417. data = zfile_load_file(fn, &size);
  4418. if (!data) {
  4419. vfso->data = xmalloc(uae_u8, default_size);
  4420. memcpy(vfso->data, default_data, default_size);
  4421. vfso->size = default_size;
  4422. return;
  4423. }
  4424. vfso->data = data;
  4425. vfso->size = size;
  4426. }
  4427. static void load_injected_icons(void)
  4428. {
  4429. load_injected_icon(&vfso_icon_tool, currprefs.filesys_inject_icons_tool, def_tool, def_tool_len);
  4430. load_injected_icon(&vfso_icon_project, currprefs.filesys_inject_icons_project, def_project, def_project_len);
  4431. load_injected_icon(&vfso_icon_drawer, currprefs.filesys_inject_icons_drawer, def_drawer, def_drawer_len);
  4432. }
  4433. static void inject_icons_to_directory(Unit *unit, a_inode *base)
  4434. {
  4435. for (a_inode *aino = base->child; aino; aino = aino->sibling) {
  4436. int len = _tcslen(aino->aname);
  4437. if (len >= 5 && !_tcsicmp(aino->aname + len - 5, _T(".info")))
  4438. continue;
  4439. TCHAR tmp[256];
  4440. _stprintf(tmp, _T("%s.info"), aino->aname);
  4441. bool match = false;
  4442. for (a_inode *aino2 = base->child; aino2; aino2 = aino2->sibling) {
  4443. if (!_tcsicmp(aino2->aname, tmp))
  4444. match = true;
  4445. }
  4446. if (match)
  4447. continue;
  4448. uae_u32 err;
  4449. struct virtualfilesysobject *vfso;
  4450. if (aino->dir) {
  4451. vfso = &vfso_icon_drawer;
  4452. } else {
  4453. if (aino->amigaos_mode & A_FIBF_EXECUTE)
  4454. vfso = &vfso_icon_project;
  4455. else
  4456. vfso = &vfso_icon_tool;
  4457. }
  4458. lookup_child_aino_for_exnext(unit, base, tmp, &err, 0, vfso);
  4459. }
  4460. }
  4461. /* Read a directory's contents, create a_inodes for each file, and
  4462. mark them as locked in memory so that recycle_aino will not reap
  4463. them.
  4464. We do this to avoid problems with the host OS: we don't want to
  4465. leave the directory open on the host side until all ExNext()s have
  4466. finished - they may never finish! */
  4467. static void populate_directory (Unit *unit, a_inode *base)
  4468. {
  4469. struct fs_dirhandle *d;
  4470. a_inode *aino;
  4471. d = fs_opendir (unit, base);
  4472. if (!d)
  4473. return;
  4474. for (aino = base->child; aino; aino = aino->sibling) {
  4475. base->locked_children++;
  4476. unit->total_locked_ainos++;
  4477. }
  4478. TRACE3((_T("Populating directory, child %s, locked_children %d\n"),
  4479. base->child ? base->child->nname : _T("<NULL>"), base->locked_children));
  4480. for (;;) {
  4481. uae_u64 uniq = 0;
  4482. TCHAR fn[MAX_DPATH];
  4483. int ok;
  4484. uae_u32 err;
  4485. /* Find next file that belongs to the Amiga fs (skipping things
  4486. like "..", "." etc. */
  4487. do {
  4488. ok = filesys_readdir(d, fn, &uniq);
  4489. } while (ok && d->fstype == FS_DIRECTORY && (filesys_name_invalid (fn) || fsdb_name_invalid_dir (NULL, fn)));
  4490. if (!ok)
  4491. break;
  4492. /* This calls init_child_aino, which will notice that the parent is
  4493. being ExNext()ed, and it will increment the locked counts. */
  4494. aino = lookup_child_aino_for_exnext (unit, base, fn, &err, uniq, NULL);
  4495. }
  4496. fs_closedir (d);
  4497. if (currprefs.filesys_inject_icons || unit->ui.inject_icons)
  4498. inject_icons_to_directory(unit, base);
  4499. }
  4500. static bool do_examine(TrapContext *ctx, Unit *unit, dpacket *packet, a_inode *aino, uaecptr info, bool longfilesize)
  4501. {
  4502. for (;;) {
  4503. TCHAR *name;
  4504. if (!aino)
  4505. break;
  4506. name = aino->nname;
  4507. get_fileinfo(ctx, unit, packet, info, aino, longfilesize);
  4508. if (!aino->vfso && !(unit->volflags & (MYVOLUMEINFO_ARCHIVE | MYVOLUMEINFO_CDFS)) && !fsdb_exists(name)) {
  4509. TRACE ((_T("%s orphaned"), name));
  4510. return false;
  4511. }
  4512. return true;
  4513. }
  4514. TRACE((_T("no more entries\n")));
  4515. free_exkey (unit, aino->parent);
  4516. PUT_PCK_RES1 (packet, DOS_FALSE);
  4517. PUT_PCK_RES2 (packet, ERROR_NO_MORE_ENTRIES);
  4518. return true;
  4519. }
  4520. #define EXNEXT_DEBUG 0
  4521. static void action_examine_next(TrapContext *ctx, Unit *unit, dpacket *packet, bool largefilesize)
  4522. {
  4523. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  4524. uaecptr info = GET_PCK_ARG2 (packet) << 2;
  4525. a_inode *aino = 0, *daino = 0;
  4526. uae_u32 uniq;
  4527. TRACE((_T("ACTION_EXAMINE_NEXT(0x%x,0x%x,%d)\n"), lock, info, largefilesize));
  4528. gui_flicker_led (UNIT_LED(unit), unit->unit, 1);
  4529. DUMPLOCK(ctx, unit, lock);
  4530. if (lock != 0)
  4531. aino = aino_from_lock(ctx, unit, lock);
  4532. if (aino == 0)
  4533. aino = &unit->rootnode;
  4534. uniq = trap_get_long(ctx, info);
  4535. for (;;) {
  4536. if (uniq == aino->uniq) {
  4537. // first exnext
  4538. if (!aino->dir) {
  4539. write_log (_T("ExNext called for a file! %s:%d (Houston?)\n"), aino->nname, uniq);
  4540. goto no_more_entries;
  4541. }
  4542. if (aino->exnext_count++ == 0)
  4543. populate_directory (unit, aino);
  4544. if (!aino->child)
  4545. goto no_more_entries;
  4546. daino = aino->child;
  4547. } else {
  4548. daino = lookup_aino(unit, uniq);
  4549. if (!daino) {
  4550. #if EXNEXT_DEBUG
  4551. write_log(_T("EXNEXT but next entry is missing! (%d)\n"), uniq);
  4552. #endif
  4553. // deleted? Look for next larger uniq in same directory.
  4554. daino = aino->child;
  4555. while (daino && uniq >= daino->uniq) {
  4556. daino = daino->sibling;
  4557. }
  4558. #if EXNEXT_DEBUG
  4559. if (daino) {
  4560. write_log(_T("Using next found entry %d\n"), uniq);
  4561. }
  4562. #endif
  4563. // didn't find, what about previous?
  4564. if (!daino) {
  4565. daino = aino->child;
  4566. while (daino && uniq >= daino->uniq) {
  4567. if (daino->sibling && daino->sibling->uniq >= uniq) {
  4568. #if EXNEXT_DEBUG
  4569. write_log(_T("Using previous entry %d\n"), uniq);
  4570. #endif
  4571. break;
  4572. }
  4573. daino = daino->sibling;
  4574. }
  4575. }
  4576. // didn't find any but there are still entries? restart from beginning.
  4577. if (!daino && aino->child) {
  4578. daino = aino->child;
  4579. #if EXNEXT_DEBUG
  4580. write_log(_T("Re-starting from beginning %d\n"), daino->uniq);
  4581. #endif
  4582. }
  4583. } else {
  4584. daino = daino->sibling;
  4585. }
  4586. }
  4587. if (!daino)
  4588. goto no_more_entries;
  4589. if (daino->parent != aino) {
  4590. write_log(_T("Houston, we have a BIG problem. %s is not parent of %s\n"), daino->nname, aino->nname);
  4591. goto no_more_entries;
  4592. }
  4593. uniq = daino->uniq;
  4594. if (daino->mountcount != unit->mountcount)
  4595. continue;
  4596. if (!do_examine(ctx, unit, packet, daino, info, largefilesize))
  4597. continue;
  4598. return;
  4599. }
  4600. no_more_entries:
  4601. free_exkey(unit, aino);
  4602. PUT_PCK_RES1(packet, DOS_FALSE);
  4603. PUT_PCK_RES2 (packet, ERROR_NO_MORE_ENTRIES);
  4604. }
  4605. static void do_find(TrapContext *ctx, Unit *unit, dpacket *packet, int mode, int create, int fallback)
  4606. {
  4607. uaecptr fh = GET_PCK_ARG1 (packet) << 2;
  4608. uaecptr lock = GET_PCK_ARG2 (packet) << 2;
  4609. uaecptr name = GET_PCK_ARG3 (packet) << 2;
  4610. a_inode *aino;
  4611. Key *k;
  4612. struct fs_filehandle *fd = NULL;
  4613. int err;
  4614. mode_t openmode;
  4615. int aino_created = 0;
  4616. int isvirtual = unit->volflags & (MYVOLUMEINFO_ARCHIVE | MYVOLUMEINFO_CDFS);
  4617. TRACE((_T("ACTION_FIND_*(0x%08x,0x%08x,\"%s\",%d,%d)\n"), fh, lock, bstr(ctx, unit, name), mode, create));
  4618. TRACE((_T("fh=%x lock=%x name=%x\n"), fh, lock, name));
  4619. DUMPLOCK(ctx, unit, lock);
  4620. aino = find_aino(ctx, unit, lock, bstr(ctx, unit, name), &err);
  4621. if (aino == 0 || (err != 0 && err != ERROR_OBJECT_NOT_AROUND)) {
  4622. /* Whatever it is, we can't handle it. */
  4623. PUT_PCK_RES1 (packet, DOS_FALSE);
  4624. PUT_PCK_RES2 (packet, err);
  4625. return;
  4626. }
  4627. if (aino->softlink) {
  4628. handle_softlink(ctx, unit, packet, aino);
  4629. return;
  4630. }
  4631. if (err == 0) {
  4632. /* Object exists. */
  4633. if (aino->dir) {
  4634. PUT_PCK_RES1 (packet, DOS_FALSE);
  4635. PUT_PCK_RES2 (packet, ERROR_OBJECT_WRONG_TYPE);
  4636. return;
  4637. }
  4638. if (aino->elock || (create == 2 && aino->shlock > 0)) {
  4639. PUT_PCK_RES1 (packet, DOS_FALSE);
  4640. PUT_PCK_RES2 (packet, ERROR_OBJECT_IN_USE);
  4641. return;
  4642. }
  4643. if (create && aino->vfso) {
  4644. PUT_PCK_RES1 (packet, DOS_FALSE);
  4645. PUT_PCK_RES2 (packet, ERROR_DELETE_PROTECTED);
  4646. return;
  4647. }
  4648. if (create == 2 && (aino->amigaos_mode & A_FIBF_DELETE) != 0) {
  4649. PUT_PCK_RES1 (packet, DOS_FALSE);
  4650. PUT_PCK_RES2 (packet, ERROR_DELETE_PROTECTED);
  4651. return;
  4652. }
  4653. if (create != 2) {
  4654. if ((((mode & aino->amigaos_mode) & A_FIBF_WRITE) != 0 || is_writeprotected(unit))
  4655. && fallback)
  4656. {
  4657. mode &= ~A_FIBF_WRITE;
  4658. }
  4659. /* Kick 1.3 doesn't check read and write access bits - maybe it would be
  4660. * simpler just not to do that either. */
  4661. if ((mode & A_FIBF_WRITE) != 0 && is_writeprotected(unit)) {
  4662. PUT_PCK_RES1 (packet, DOS_FALSE);
  4663. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  4664. return;
  4665. }
  4666. if (((mode & aino->amigaos_mode) & A_FIBF_WRITE) != 0
  4667. || mode == 0)
  4668. {
  4669. PUT_PCK_RES1 (packet, DOS_FALSE);
  4670. PUT_PCK_RES2 (packet, ERROR_WRITE_PROTECTED);
  4671. return;
  4672. }
  4673. if (((mode & aino->amigaos_mode) & A_FIBF_READ) != 0) {
  4674. PUT_PCK_RES1 (packet, DOS_FALSE);
  4675. PUT_PCK_RES2 (packet, ERROR_READ_PROTECTED);
  4676. return;
  4677. }
  4678. }
  4679. } else if (create == 0) {
  4680. PUT_PCK_RES1 (packet, DOS_FALSE);
  4681. PUT_PCK_RES2 (packet, err);
  4682. return;
  4683. } else {
  4684. /* Object does not exist. aino points to containing directory. */
  4685. aino = create_child_aino(unit, aino, my_strdup (bstr_cut(ctx, unit, name)), 0);
  4686. if (aino == 0) {
  4687. PUT_PCK_RES1 (packet, DOS_FALSE);
  4688. PUT_PCK_RES2 (packet, ERROR_DISK_IS_FULL); /* best we can do */
  4689. return;
  4690. }
  4691. aino_created = 1;
  4692. }
  4693. prepare_for_open (aino->nname);
  4694. if (!aino->vfso) {
  4695. openmode = (((mode & A_FIBF_READ) == 0 ? O_WRONLY
  4696. : (mode & A_FIBF_WRITE) == 0 ? O_RDONLY
  4697. : O_RDWR)
  4698. | (create ? O_CREAT : 0)
  4699. | (create == 2 ? O_TRUNC : 0));
  4700. fd = fs_openfile (unit, aino, openmode | O_BINARY);
  4701. if (fd == NULL) {
  4702. if (aino_created)
  4703. delete_aino (unit, aino);
  4704. PUT_PCK_RES1 (packet, DOS_FALSE);
  4705. /* archive and fd == NULL = corrupt archive or out of memory */
  4706. PUT_PCK_RES2 (packet, isvirtual ? ERROR_OBJECT_NOT_AROUND : dos_errno ());
  4707. return;
  4708. }
  4709. }
  4710. k = new_key (unit);
  4711. k->fd = fd;
  4712. k->aino = aino;
  4713. k->dosmode = mode;
  4714. k->createmode = create;
  4715. k->notifyactive = create ? 1 : 0;
  4716. if (create && isvirtual)
  4717. fsdb_set_file_attrs (aino);
  4718. trap_put_long(ctx, fh + 36, k->uniq);
  4719. if (create == 2) {
  4720. aino->elock = 1;
  4721. // clear comment if file already existed
  4722. if (aino->comment) {
  4723. xfree (aino->comment);
  4724. aino->comment = 0;
  4725. }
  4726. fsdb_set_file_attrs (aino);
  4727. } else {
  4728. aino->shlock++;
  4729. }
  4730. de_recycle_aino (unit, aino);
  4731. PUT_PCK_RES1 (packet, DOS_TRUE);
  4732. }
  4733. static void action_fh_from_lock(TrapContext *ctx, Unit *unit, dpacket *packet)
  4734. {
  4735. uaecptr fh = GET_PCK_ARG1 (packet) << 2;
  4736. uaecptr lock = GET_PCK_ARG2 (packet) << 2;
  4737. a_inode *aino;
  4738. Key *k;
  4739. struct fs_filehandle *fd;
  4740. mode_t openmode;
  4741. int mode;
  4742. TRACE((_T("ACTION_FH_FROM_LOCK(0x%x,0x%x)\n"), fh, lock));
  4743. DUMPLOCK(ctx, unit,lock);
  4744. if (!lock) {
  4745. PUT_PCK_RES1 (packet, DOS_FALSE);
  4746. PUT_PCK_RES2 (packet, 0);
  4747. return;
  4748. }
  4749. aino = aino_from_lock(ctx, unit, lock);
  4750. if (aino == 0)
  4751. aino = &unit->rootnode;
  4752. if (aino->softlink) {
  4753. handle_softlink(ctx, unit, packet, aino);
  4754. return;
  4755. }
  4756. mode = aino->amigaos_mode; /* Use same mode for opened filehandle as existing Lock() */
  4757. prepare_for_open (aino->nname);
  4758. TRACE ((_T(" mode is %d\n"), mode));
  4759. openmode = (((mode & A_FIBF_READ) ? O_WRONLY
  4760. : (mode & A_FIBF_WRITE) ? O_RDONLY
  4761. : O_RDWR));
  4762. /* the files on CD really can have the write-bit set. */
  4763. if (is_writeprotected(unit))
  4764. openmode = O_RDONLY;
  4765. fd = fs_openfile (unit, aino, openmode | O_BINARY);
  4766. if (fd == NULL) {
  4767. PUT_PCK_RES1 (packet, DOS_FALSE);
  4768. PUT_PCK_RES2 (packet, dos_errno ());
  4769. return;
  4770. }
  4771. k = new_key (unit);
  4772. k->fd = fd;
  4773. k->aino = aino;
  4774. trap_put_long(ctx, fh + 36, k->uniq);
  4775. /* I don't think I need to play with shlock count here, because I'm
  4776. opening from an existing lock ??? */
  4777. de_recycle_aino (unit, aino);
  4778. free_lock(ctx, unit, lock); /* lock must be unlocked */
  4779. PUT_PCK_RES1 (packet, DOS_TRUE);
  4780. /* PUT_PCK_RES2 (packet, k->uniq); - this shouldn't be necessary, try without it */
  4781. }
  4782. static void action_find_input(TrapContext *ctx, Unit *unit, dpacket *packet)
  4783. {
  4784. do_find(ctx, unit, packet, A_FIBF_READ | A_FIBF_WRITE, 0, 1);
  4785. }
  4786. static void action_find_output(TrapContext *ctx, Unit *unit, dpacket *packet)
  4787. {
  4788. if (is_writeprotected(unit)) {
  4789. PUT_PCK_RES1 (packet, DOS_FALSE);
  4790. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  4791. return;
  4792. }
  4793. do_find(ctx, unit, packet, A_FIBF_READ | A_FIBF_WRITE, 2, 0);
  4794. }
  4795. static void action_find_write(TrapContext *ctx, Unit *unit, dpacket *packet)
  4796. {
  4797. if (is_writeprotected(unit)) {
  4798. PUT_PCK_RES1 (packet, DOS_FALSE);
  4799. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  4800. return;
  4801. }
  4802. do_find(ctx, unit, packet, A_FIBF_READ | A_FIBF_WRITE, 1, 0);
  4803. }
  4804. /* change file/dir's parent dir modification time */
  4805. static void updatedirtime (a_inode *a1, int now)
  4806. {
  4807. struct mystat statbuf;
  4808. if (!a1->parent)
  4809. return;
  4810. if (!now) {
  4811. if (!my_stat (a1->nname, &statbuf))
  4812. return;
  4813. my_utime (a1->parent->nname, &statbuf.mtime);
  4814. } else {
  4815. my_utime (a1->parent->nname, NULL);
  4816. }
  4817. }
  4818. static void action_end(TrapContext *ctx, Unit *unit, dpacket *packet)
  4819. {
  4820. Key *k;
  4821. TRACE((_T("ACTION_END(0x%x)\n"), GET_PCK_ARG1 (packet)));
  4822. k = lookup_key (unit, GET_PCK_ARG1 (packet));
  4823. if (k != 0) {
  4824. if (k->notifyactive) {
  4825. notify_check(ctx, unit, k->aino);
  4826. updatedirtime (k->aino, 1);
  4827. }
  4828. if (k->aino->elock)
  4829. k->aino->elock = 0;
  4830. else
  4831. k->aino->shlock--;
  4832. recycle_aino (unit, k->aino);
  4833. free_key (unit, k);
  4834. }
  4835. PUT_PCK_RES1 (packet, DOS_TRUE);
  4836. PUT_PCK_RES2 (packet, 0);
  4837. }
  4838. static void action_read(TrapContext *ctx, Unit *unit, dpacket *packet)
  4839. {
  4840. Key *k = lookup_key (unit, GET_PCK_ARG1 (packet));
  4841. uaecptr addr = GET_PCK_ARG2 (packet);
  4842. uae_u32 size = GET_PCK_ARG3 (packet);
  4843. uae_u32 actual = 0;
  4844. if (k == 0) {
  4845. PUT_PCK_RES1 (packet, DOS_FALSE);
  4846. /* PUT_PCK_RES2 (packet, EINVAL); */
  4847. return;
  4848. }
  4849. TRACE((_T("ACTION_READ(%s,0x%x,%d)\n"), k->aino->nname, addr, size));
  4850. gui_flicker_led (UNIT_LED(unit), unit->unit, 1);
  4851. if (size == 0) {
  4852. PUT_PCK_RES1 (packet, 0);
  4853. PUT_PCK_RES2 (packet, 0);
  4854. } else if (k->aino->vfso) {
  4855. uae_s64 filesize = k->aino->vfso->size;
  4856. for (int i = 0; i < size && k->file_pos < filesize; i++) {
  4857. trap_put_byte(ctx, addr + i, k->aino->vfso->data[k->file_pos]);
  4858. k->file_pos++;
  4859. actual++;
  4860. }
  4861. PUT_PCK_RES1 (packet, actual);
  4862. size = 0;
  4863. } else {
  4864. /* check if filesize < size */
  4865. uae_s64 filesize, cur;
  4866. filesize = key_filesize(k);
  4867. cur = k->file_pos;
  4868. if (size > filesize - cur)
  4869. size = filesize - cur;
  4870. if (size == 0) {
  4871. PUT_PCK_RES1 (packet, 0);
  4872. PUT_PCK_RES2 (packet, 0);
  4873. } else if (!trap_valid_address(ctx, addr, size)) {
  4874. /* it really crosses memory boundary */
  4875. uae_u8 *buf;
  4876. /* ugh this is inefficient but easy */
  4877. if (key_seek(k, k->file_pos, SEEK_SET) < 0) {
  4878. PUT_PCK_RES1 (packet, 0);
  4879. PUT_PCK_RES2 (packet, dos_errno ());
  4880. return;
  4881. }
  4882. buf = xmalloc (uae_u8, size);
  4883. if (!buf) {
  4884. PUT_PCK_RES1 (packet, -1);
  4885. PUT_PCK_RES2 (packet, ERROR_NO_FREE_STORE);
  4886. return;
  4887. }
  4888. actual = fs_read (k->fd, buf, size);
  4889. if ((uae_s32)actual == -1) {
  4890. PUT_PCK_RES1 (packet, 0);
  4891. PUT_PCK_RES2 (packet, dos_errno ());
  4892. } else {
  4893. PUT_PCK_RES1 (packet, actual);
  4894. trap_put_bytes(ctx, buf, addr, actual);
  4895. k->file_pos += actual;
  4896. }
  4897. xfree (buf);
  4898. size = 0;
  4899. }
  4900. }
  4901. if (size) {
  4902. if (key_seek(k, k->file_pos, SEEK_SET) < 0) {
  4903. PUT_PCK_RES1(packet, 0);
  4904. PUT_PCK_RES2(packet, dos_errno());
  4905. return;
  4906. }
  4907. if (trap_is_indirect() || !real_address_allowed()) {
  4908. uae_u8 buf[RTAREA_TRAP_DATA_EXTRA_SIZE];
  4909. actual = 0;
  4910. while (size > 0) {
  4911. int toread = size > RTAREA_TRAP_DATA_EXTRA_SIZE ? RTAREA_TRAP_DATA_EXTRA_SIZE : size;
  4912. int read = fs_read(k->fd, buf, toread);
  4913. if (read < 0) {
  4914. actual = -1;
  4915. break;
  4916. }
  4917. if (read == 0)
  4918. break;
  4919. trap_put_bytes(ctx, buf, addr, read);
  4920. size -= read;
  4921. addr += read;
  4922. actual += read;
  4923. if (read < toread)
  4924. break;
  4925. }
  4926. } else {
  4927. /* normal fast read */
  4928. uae_u8 *realpt = get_real_address (addr);
  4929. actual = fs_read (k->fd, realpt, size);
  4930. }
  4931. if (actual == 0) {
  4932. PUT_PCK_RES1 (packet, 0);
  4933. PUT_PCK_RES2 (packet, 0);
  4934. } else if (actual < 0) {
  4935. PUT_PCK_RES1 (packet, 0);
  4936. PUT_PCK_RES2 (packet, dos_errno ());
  4937. } else {
  4938. PUT_PCK_RES1 (packet, actual);
  4939. k->file_pos += actual;
  4940. }
  4941. }
  4942. TRACE((_T("=%d\n"), actual));
  4943. }
  4944. static void action_write(TrapContext *ctx, Unit *unit, dpacket *packet)
  4945. {
  4946. Key *k = lookup_key (unit, GET_PCK_ARG1 (packet));
  4947. uaecptr addr = GET_PCK_ARG2 (packet);
  4948. uae_u32 size = GET_PCK_ARG3 (packet);
  4949. uae_u32 actual;
  4950. uae_u8 *buf;
  4951. if (k == 0) {
  4952. PUT_PCK_RES1 (packet, DOS_FALSE);
  4953. /* PUT_PCK_RES2 (packet, EINVAL); */
  4954. return;
  4955. }
  4956. gui_flicker_led (UNIT_LED(unit), unit->unit, 2);
  4957. TRACE((_T("ACTION_WRITE(%s,0x%x,%d)\n"), k->aino->nname, addr, size));
  4958. if (is_writeprotected(unit) || k->aino->vfso) {
  4959. PUT_PCK_RES1 (packet, DOS_FALSE);
  4960. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  4961. return;
  4962. }
  4963. if (size == 0) {
  4964. actual = 0;
  4965. PUT_PCK_RES1 (packet, 0);
  4966. PUT_PCK_RES2 (packet, 0);
  4967. } else if (trap_valid_address(ctx, addr, size)) {
  4968. if (key_seek(k, k->file_pos, SEEK_SET) < 0) {
  4969. PUT_PCK_RES1(packet, 0);
  4970. PUT_PCK_RES2(packet, dos_errno());
  4971. return;
  4972. }
  4973. if (trap_is_indirect() || !real_address_allowed()) {
  4974. uae_u8 buf[RTAREA_TRAP_DATA_EXTRA_SIZE];
  4975. actual = 0;
  4976. int sizecnt = size;
  4977. while (sizecnt > 0) {
  4978. int towrite = sizecnt > RTAREA_TRAP_DATA_EXTRA_SIZE ? RTAREA_TRAP_DATA_EXTRA_SIZE : sizecnt;
  4979. trap_get_bytes(ctx, buf, addr, towrite);
  4980. int write = fs_write(k->fd, buf, towrite);
  4981. if (write < 0) {
  4982. actual = -1;
  4983. break;
  4984. }
  4985. if (write == 0)
  4986. break;
  4987. sizecnt -= write;
  4988. addr += write;
  4989. actual += write;
  4990. if (write < towrite)
  4991. break;
  4992. }
  4993. } else {
  4994. uae_u8 *realpt = get_real_address (addr);
  4995. actual = fs_write (k->fd, realpt, size);
  4996. }
  4997. } else {
  4998. /* ugh this is inefficient but easy */
  4999. if (key_seek(k, k->file_pos, SEEK_SET) < 0) {
  5000. PUT_PCK_RES1 (packet, 0);
  5001. PUT_PCK_RES2 (packet, dos_errno ());
  5002. return;
  5003. }
  5004. buf = xmalloc (uae_u8, size);
  5005. if (!buf) {
  5006. PUT_PCK_RES1 (packet, -1);
  5007. PUT_PCK_RES2 (packet, ERROR_NO_FREE_STORE);
  5008. return;
  5009. }
  5010. trap_get_bytes(ctx, buf, addr, size);
  5011. actual = fs_write (k->fd, buf, size);
  5012. xfree (buf);
  5013. }
  5014. TRACE((_T("=%d\n"), actual));
  5015. PUT_PCK_RES1 (packet, actual);
  5016. if (actual != size)
  5017. PUT_PCK_RES2 (packet, dos_errno ());
  5018. if ((uae_s32)actual != -1)
  5019. k->file_pos += actual;
  5020. k->notifyactive = 1;
  5021. }
  5022. static void action_seek(TrapContext *ctx, Unit *unit, dpacket *packet)
  5023. {
  5024. Key *k = lookup_key (unit, GET_PCK_ARG1 (packet));
  5025. int pos = (uae_s32)GET_PCK_ARG2 (packet);
  5026. int mode = (uae_s32)GET_PCK_ARG3 (packet);
  5027. uae_s64 res;
  5028. uae_s64 cur;
  5029. int whence = SEEK_CUR;
  5030. uae_s64 temppos, filesize;
  5031. if (k == 0) {
  5032. PUT_PCK_RES1 (packet, -1);
  5033. PUT_PCK_RES2 (packet, ERROR_INVALID_LOCK);
  5034. return;
  5035. }
  5036. if (mode > 0)
  5037. whence = SEEK_END;
  5038. if (mode < 0)
  5039. whence = SEEK_SET;
  5040. cur = k->file_pos;
  5041. TRACE((_T("ACTION_SEEK(%s,%d,%d)=%lld\n"), k->aino->nname, pos, mode, cur));
  5042. gui_flicker_led (UNIT_LED(unit), unit->unit, 1);
  5043. filesize = key_filesize(k);
  5044. if (whence == SEEK_CUR)
  5045. temppos = cur + pos;
  5046. if (whence == SEEK_SET)
  5047. temppos = pos;
  5048. if (whence == SEEK_END)
  5049. temppos = filesize + pos;
  5050. if (filesize < temppos || temppos < 0) {
  5051. PUT_PCK_RES1 (packet, -1);
  5052. PUT_PCK_RES2 (packet, ERROR_SEEK_ERROR);
  5053. return;
  5054. }
  5055. res = key_seek(k, pos, whence);
  5056. if (-1 == res || cur > MAXFILESIZE32) {
  5057. PUT_PCK_RES1 (packet, -1);
  5058. PUT_PCK_RES2 (packet, ERROR_SEEK_ERROR);
  5059. key_seek(k, cur, SEEK_SET);
  5060. } else {
  5061. PUT_PCK_RES1 (packet, cur);
  5062. k->file_pos = key_seek(k, 0, SEEK_CUR);
  5063. }
  5064. }
  5065. static void action_set_protect(TrapContext *ctx, Unit *unit, dpacket *packet)
  5066. {
  5067. uaecptr lock = GET_PCK_ARG2 (packet) << 2;
  5068. uaecptr name = GET_PCK_ARG3 (packet) << 2;
  5069. uae_u32 mask = GET_PCK_ARG4 (packet);
  5070. a_inode *a;
  5071. int err;
  5072. TRACE((_T("ACTION_SET_PROTECT(0x%x,\"%s\",0x%x)\n"), lock, bstr(ctx, unit, name), mask));
  5073. if (is_writeprotected(unit)) {
  5074. PUT_PCK_RES1 (packet, DOS_FALSE);
  5075. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  5076. return;
  5077. }
  5078. a = find_aino(ctx, unit, lock, bstr(ctx, unit, name), &err);
  5079. if (err != 0) {
  5080. PUT_PCK_RES1 (packet, DOS_FALSE);
  5081. PUT_PCK_RES2 (packet, err);
  5082. return;
  5083. }
  5084. if (a->softlink) {
  5085. handle_softlink(ctx, unit, packet, a);
  5086. return;
  5087. }
  5088. a->amigaos_mode = mask;
  5089. if (!fsdb_cando (unit))
  5090. a->amigaos_mode = fsdb_mode_supported (a);
  5091. err = fsdb_set_file_attrs (a);
  5092. if (err != 0) {
  5093. PUT_PCK_RES1 (packet, DOS_FALSE);
  5094. PUT_PCK_RES2 (packet, err);
  5095. } else {
  5096. PUT_PCK_RES1 (packet, DOS_TRUE);
  5097. }
  5098. notify_check(ctx, unit, a);
  5099. gui_flicker_led (UNIT_LED(unit), unit->unit, 2);
  5100. }
  5101. static void action_set_comment(TrapContext *ctx, Unit * unit, dpacket *packet)
  5102. {
  5103. uaecptr lock = GET_PCK_ARG2 (packet) << 2;
  5104. uaecptr name = GET_PCK_ARG3 (packet) << 2;
  5105. uaecptr comment = GET_PCK_ARG4 (packet) << 2;
  5106. TCHAR *commented = NULL;
  5107. a_inode *a;
  5108. int err;
  5109. if (is_writeprotected(unit)) {
  5110. PUT_PCK_RES1 (packet, DOS_FALSE);
  5111. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  5112. return;
  5113. }
  5114. if (fsdb_cando (unit)) {
  5115. commented = bstr(ctx, unit, comment);
  5116. if (_tcslen (commented) > 80) {
  5117. PUT_PCK_RES1 (packet, DOS_FALSE);
  5118. PUT_PCK_RES2 (packet, ERROR_COMMENT_TOO_BIG);
  5119. return;
  5120. }
  5121. if (_tcslen (commented) > 0) {
  5122. TCHAR *p = commented;
  5123. commented = xmalloc (TCHAR, 81);
  5124. _tcsncpy (commented, p, 80);
  5125. commented[80] = 0;
  5126. } else {
  5127. commented = NULL;
  5128. }
  5129. }
  5130. TRACE ((_T("ACTION_SET_COMMENT(0x%x,\"%s\")\n"), lock, commented));
  5131. a = find_aino(ctx, unit, lock, bstr(ctx, unit, name), &err);
  5132. if (err != 0) {
  5133. PUT_PCK_RES1 (packet, DOS_FALSE);
  5134. PUT_PCK_RES2 (packet, err);
  5135. maybe_free_and_out:
  5136. if (commented)
  5137. xfree (commented);
  5138. return;
  5139. }
  5140. if (a->softlink) {
  5141. handle_softlink(ctx, unit, packet, a);
  5142. goto maybe_free_and_out;
  5143. }
  5144. PUT_PCK_RES1 (packet, DOS_TRUE);
  5145. PUT_PCK_RES2 (packet, 0);
  5146. if (a->comment == 0 && commented == 0)
  5147. goto maybe_free_and_out;
  5148. if (a->comment != 0 && commented != 0 && _tcscmp (a->comment, commented) == 0)
  5149. goto maybe_free_and_out;
  5150. if (a->comment)
  5151. xfree (a->comment);
  5152. a->comment = commented;
  5153. fsdb_set_file_attrs (a);
  5154. notify_check(ctx, unit, a);
  5155. gui_flicker_led (UNIT_LED(unit), unit->unit, 2);
  5156. }
  5157. static void action_same_lock(TrapContext *ctx, Unit *unit, dpacket *packet)
  5158. {
  5159. uaecptr lock1 = GET_PCK_ARG1 (packet) << 2;
  5160. uaecptr lock2 = GET_PCK_ARG2 (packet) << 2;
  5161. TRACE((_T("ACTION_SAME_LOCK(0x%x,0x%x)\n"), lock1, lock2));
  5162. DUMPLOCK(ctx, unit, lock1); DUMPLOCK(ctx, unit, lock2);
  5163. if (!lock1 || !lock2) {
  5164. PUT_PCK_RES1 (packet, lock1 == lock2 ? DOS_TRUE : DOS_FALSE);
  5165. } else {
  5166. PUT_PCK_RES1 (packet, trap_get_long(ctx, lock1 + 4) == trap_get_long(ctx, lock2 + 4) ? DOS_TRUE : DOS_FALSE);
  5167. }
  5168. }
  5169. static void action_change_mode(TrapContext *ctx, Unit *unit, dpacket *packet)
  5170. {
  5171. #define CHANGE_LOCK 0
  5172. #define CHANGE_FH 1
  5173. /* will be CHANGE_FH or CHANGE_LOCK value */
  5174. int type = GET_PCK_ARG1 (packet);
  5175. /* either a file-handle or lock */
  5176. uaecptr object = GET_PCK_ARG2 (packet) << 2;
  5177. /* will be EXCLUSIVE_LOCK/SHARED_LOCK if CHANGE_LOCK,
  5178. * or MODE_OLDFILE/MODE_NEWFILE/MODE_READWRITE if CHANGE_FH *
  5179. * Above is wrong, it is always *_LOCK. TW. */
  5180. int mode = GET_PCK_ARG3 (packet);
  5181. unsigned long uniq;
  5182. a_inode *a = NULL, *olda = NULL;
  5183. int err = 0;
  5184. TRACE((_T("ACTION_CHANGE_MODE(0x%x,%d,%d)\n"), object, type, mode));
  5185. if (! object || (type != CHANGE_FH && type != CHANGE_LOCK)) {
  5186. PUT_PCK_RES1 (packet, DOS_FALSE);
  5187. PUT_PCK_RES2 (packet, ERROR_INVALID_LOCK);
  5188. return;
  5189. }
  5190. if (type == CHANGE_LOCK) {
  5191. uniq = trap_get_long(ctx, object + 4);
  5192. } else {
  5193. Key *k = lookup_key (unit, trap_get_long(ctx, object + 36));
  5194. if (!k) {
  5195. PUT_PCK_RES1 (packet, DOS_FALSE);
  5196. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  5197. return;
  5198. }
  5199. uniq = k->aino->uniq;
  5200. }
  5201. a = lookup_aino (unit, uniq);
  5202. if (! a) {
  5203. err = ERROR_INVALID_LOCK;
  5204. } else {
  5205. if (mode == -1) {
  5206. if (a->shlock > 1) {
  5207. err = ERROR_OBJECT_IN_USE;
  5208. } else {
  5209. a->shlock = 0;
  5210. a->elock = 1;
  5211. }
  5212. } else { /* Must be SHARED_LOCK == -2 */
  5213. a->elock = 0;
  5214. a->shlock++;
  5215. }
  5216. }
  5217. if (err) {
  5218. PUT_PCK_RES1 (packet, DOS_FALSE);
  5219. PUT_PCK_RES2 (packet, err);
  5220. return;
  5221. } else {
  5222. de_recycle_aino (unit, a);
  5223. PUT_PCK_RES1 (packet, DOS_TRUE);
  5224. }
  5225. }
  5226. static void action_parent_common(TrapContext *ctx, Unit *unit, dpacket *packet, unsigned long uniq)
  5227. {
  5228. a_inode *olda = lookup_aino (unit, uniq);
  5229. if (olda == 0) {
  5230. PUT_PCK_RES1 (packet, DOS_FALSE);
  5231. PUT_PCK_RES2 (packet, ERROR_INVALID_LOCK);
  5232. return;
  5233. }
  5234. if (olda->parent == 0) {
  5235. PUT_PCK_RES1 (packet, 0);
  5236. PUT_PCK_RES2 (packet, 0);
  5237. return;
  5238. }
  5239. if (olda->parent->elock) {
  5240. PUT_PCK_RES1 (packet, DOS_FALSE);
  5241. PUT_PCK_RES2 (packet, ERROR_OBJECT_IN_USE);
  5242. return;
  5243. }
  5244. olda->parent->shlock++;
  5245. de_recycle_aino (unit, olda->parent);
  5246. PUT_PCK_RES1(packet, make_lock(ctx, unit, olda->parent->uniq, -2) >> 2);
  5247. }
  5248. static void action_parent_fh(TrapContext *ctx, Unit *unit, dpacket *packet)
  5249. {
  5250. Key *k = lookup_key (unit, GET_PCK_ARG1 (packet));
  5251. if (!k) {
  5252. PUT_PCK_RES1 (packet, DOS_FALSE);
  5253. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  5254. return;
  5255. }
  5256. action_parent_common (ctx, unit, packet, k->aino->uniq);
  5257. }
  5258. static void action_parent(TrapContext *ctx, Unit *unit, dpacket *packet)
  5259. {
  5260. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  5261. TRACE((_T("ACTION_PARENT(0x%x)\n"),lock));
  5262. if (!lock) {
  5263. PUT_PCK_RES1 (packet, 0);
  5264. PUT_PCK_RES2 (packet, 0);
  5265. } else {
  5266. action_parent_common(ctx, unit, packet, trap_get_long(ctx, lock + 4));
  5267. }
  5268. TRACE((_T("=%x %d\n"), GET_PCK_RES1 (packet), GET_PCK_RES2 (packet)));
  5269. }
  5270. static void action_create_dir(TrapContext *ctx, Unit *unit, dpacket *packet)
  5271. {
  5272. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  5273. uaecptr name = GET_PCK_ARG2 (packet) << 2;
  5274. a_inode *aino;
  5275. int err;
  5276. TRACE((_T("ACTION_CREATE_DIR(0x%x,\"%s\")\n"), lock, bstr(ctx, unit, name)));
  5277. if (is_writeprotected(unit)) {
  5278. PUT_PCK_RES1 (packet, DOS_FALSE);
  5279. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  5280. return;
  5281. }
  5282. aino = find_aino(ctx, unit, lock, bstr(ctx, unit, name), &err);
  5283. if (aino == 0 || (err != 0 && err != ERROR_OBJECT_NOT_AROUND)) {
  5284. PUT_PCK_RES1 (packet, DOS_FALSE);
  5285. PUT_PCK_RES2 (packet, err);
  5286. return;
  5287. }
  5288. if (err == 0) {
  5289. /* Object exists. */
  5290. PUT_PCK_RES1 (packet, DOS_FALSE);
  5291. PUT_PCK_RES2 (packet, ERROR_OBJECT_EXISTS);
  5292. return;
  5293. }
  5294. /* Object does not exist. aino points to containing directory. */
  5295. aino = create_child_aino(unit, aino, my_strdup (bstr_cut(ctx, unit, name)), 1);
  5296. if (aino == 0) {
  5297. PUT_PCK_RES1 (packet, DOS_FALSE);
  5298. PUT_PCK_RES2 (packet, ERROR_DISK_IS_FULL); /* best we can do */
  5299. return;
  5300. }
  5301. if (my_mkdir (aino->nname) == -1) {
  5302. PUT_PCK_RES1 (packet, DOS_FALSE);
  5303. PUT_PCK_RES2 (packet, dos_errno ());
  5304. return;
  5305. }
  5306. aino->shlock = 1;
  5307. fsdb_set_file_attrs (aino);
  5308. de_recycle_aino (unit, aino);
  5309. notify_check(ctx, unit, aino);
  5310. updatedirtime (aino, 0);
  5311. PUT_PCK_RES1(packet, make_lock(ctx, unit, aino->uniq, -2) >> 2);
  5312. gui_flicker_led (UNIT_LED(unit), unit->unit, 2);
  5313. }
  5314. static void action_examine_fh(TrapContext *ctx, Unit *unit, dpacket *packet, bool largefilesize)
  5315. {
  5316. Key *k;
  5317. a_inode *aino = 0;
  5318. uaecptr info = GET_PCK_ARG2 (packet) << 2;
  5319. TRACE((_T("ACTION_EXAMINE_FH(0x%x,0x%x,%d)\n"),
  5320. GET_PCK_ARG1 (packet), GET_PCK_ARG2 (packet), largefilesize ));
  5321. k = lookup_key (unit, GET_PCK_ARG1 (packet));
  5322. if (k != 0)
  5323. aino = k->aino;
  5324. if (aino == 0)
  5325. aino = &unit->rootnode;
  5326. get_fileinfo(ctx, unit, packet, info, aino, largefilesize);
  5327. }
  5328. /* For a nice example of just how contradictory documentation can be, see the
  5329. * Autodoc for DOS:SetFileSize and the Packets.txt description of this packet...
  5330. * This implementation tries to mimic the behaviour of the Kick 3.1 ramdisk
  5331. * (which seems to match the Autodoc description). */
  5332. static void action_set_file_size(TrapContext *ctx, Unit *unit, dpacket *packet)
  5333. {
  5334. Key *k, *k1;
  5335. off_t offset = GET_PCK_ARG2 (packet);
  5336. int mode = (uae_s32)GET_PCK_ARG3 (packet);
  5337. int whence = SEEK_CUR;
  5338. if (mode > 0)
  5339. whence = SEEK_END;
  5340. if (mode < 0)
  5341. whence = SEEK_SET;
  5342. TRACE((_T("ACTION_SET_FILE_SIZE(0x%lx, %d, 0x%x)\n"), GET_PCK_ARG1 (packet), offset, mode));
  5343. k = lookup_key (unit, GET_PCK_ARG1 (packet));
  5344. if (k == 0) {
  5345. PUT_PCK_RES1 (packet, DOS_TRUE);
  5346. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  5347. return;
  5348. }
  5349. if (k->aino->vfso) {
  5350. PUT_PCK_RES1 (packet, DOS_FALSE);
  5351. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  5352. return;
  5353. }
  5354. /* Fail if file is >=2G, it is not safe operation. */
  5355. if (key_filesize(k) > MAXFILESIZE32_2G) {
  5356. PUT_PCK_RES1 (packet, DOS_TRUE);
  5357. PUT_PCK_RES2 (packet, ERROR_BAD_NUMBER); /* ? */
  5358. return;
  5359. }
  5360. gui_flicker_led (UNIT_LED(unit), unit->unit, 1);
  5361. k->notifyactive = 1;
  5362. /* If any open files have file pointers beyond this size, truncate only
  5363. * so far that these pointers do not become invalid. */
  5364. for (k1 = unit->keys; k1; k1 = k1->next) {
  5365. if (k != k1 && k->aino == k1->aino) {
  5366. if (k1->file_pos > offset)
  5367. offset = (off_t)k1->file_pos;
  5368. }
  5369. }
  5370. /* Write one then truncate: that should give the right size in all cases. */
  5371. fs_lseek (k->fd, offset, whence);
  5372. offset = fs_lseek (k->fd, 0, SEEK_CUR);
  5373. fs_write (k->fd, /* whatever */(uae_u8*)&k1, 1);
  5374. if (k->file_pos > offset)
  5375. k->file_pos = offset;
  5376. fs_lseek (k->fd, (off_t)k->file_pos, SEEK_SET);
  5377. /* Brian: no bug here; the file _must_ be one byte too large after writing
  5378. * The write is supposed to guarantee that the file can't be smaller than
  5379. * the requested size, the truncate guarantees that it can't be larger.
  5380. * If we were to write one byte earlier we'd clobber file data. */
  5381. if (my_truncate (k->aino->nname, offset) == -1) {
  5382. PUT_PCK_RES1 (packet, DOS_TRUE);
  5383. PUT_PCK_RES2 (packet, dos_errno ());
  5384. return;
  5385. }
  5386. PUT_PCK_RES1 (packet, offset);
  5387. PUT_PCK_RES2 (packet, 0);
  5388. }
  5389. static int relock_do(Unit *unit, a_inode *a1)
  5390. {
  5391. Key *k1, *knext;
  5392. int wehavekeys = 0;
  5393. for (k1 = unit->keys; k1; k1 = knext) {
  5394. knext = k1->next;
  5395. if (k1->aino == a1 && k1->fd) {
  5396. wehavekeys++;
  5397. fs_closefile (k1->fd);
  5398. write_log (_T("handle %p freed\n"), k1->fd);
  5399. }
  5400. }
  5401. return wehavekeys;
  5402. }
  5403. static void relock_re (Unit *unit, a_inode *a1, a_inode *a2, int failed)
  5404. {
  5405. Key *k1, *knext;
  5406. for (k1 = unit->keys; k1; k1 = knext) {
  5407. knext = k1->next;
  5408. if (k1->aino == a1 && k1->fd) {
  5409. int mode = (k1->dosmode & A_FIBF_READ) == 0 ? O_WRONLY : (k1->dosmode & A_FIBF_WRITE) == 0 ? O_RDONLY : O_RDWR;
  5410. mode |= O_BINARY;
  5411. if (failed) {
  5412. /* rename still failed, restore fd */
  5413. k1->fd = fs_openfile (unit, a1, mode);
  5414. write_log (_T("restoring old handle '%s' %d\n"), a1->nname, k1->dosmode);
  5415. } else {
  5416. /* transfer fd to new name */
  5417. if (a2) {
  5418. k1->aino = a2;
  5419. k1->fd = fs_openfile (unit, a2, mode);
  5420. write_log (_T("restoring new handle '%s' %d\n"), a2->nname, k1->dosmode);
  5421. } else {
  5422. write_log (_T("no new handle, deleting old lock(s).\n"));
  5423. }
  5424. }
  5425. if (k1->fd == NULL) {
  5426. write_log (_T("relocking failed '%s' -> '%s'\n"), a1->nname, a2->nname);
  5427. free_key (unit, k1);
  5428. } else {
  5429. key_seek(k1, k1->file_pos, SEEK_SET);
  5430. }
  5431. }
  5432. }
  5433. }
  5434. static void action_delete_object(TrapContext *ctx, Unit *unit, dpacket *packet)
  5435. {
  5436. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  5437. uaecptr name = GET_PCK_ARG2 (packet) << 2;
  5438. a_inode *a;
  5439. int err;
  5440. TRACE((_T("ACTION_DELETE_OBJECT(0x%x,\"%s\")\n"), lock, bstr(ctx, unit, name)));
  5441. if (is_writeprotected(unit)) {
  5442. PUT_PCK_RES1 (packet, DOS_FALSE);
  5443. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  5444. return;
  5445. }
  5446. a = find_aino(ctx, unit, lock, bstr(ctx, unit, name), &err);
  5447. if (err != 0) {
  5448. PUT_PCK_RES1 (packet, DOS_FALSE);
  5449. PUT_PCK_RES2 (packet, err);
  5450. return;
  5451. }
  5452. if (a->amigaos_mode & A_FIBF_DELETE) {
  5453. PUT_PCK_RES1 (packet, DOS_FALSE);
  5454. PUT_PCK_RES2 (packet, ERROR_DELETE_PROTECTED);
  5455. return;
  5456. }
  5457. if (a->shlock > 0 || a->elock) {
  5458. PUT_PCK_RES1 (packet, DOS_FALSE);
  5459. PUT_PCK_RES2 (packet, ERROR_OBJECT_IN_USE);
  5460. return;
  5461. }
  5462. if (!a->vfso) {
  5463. if (a->dir) {
  5464. /* This should take care of removing the fsdb if no files remain. */
  5465. fsdb_dir_writeback (a);
  5466. if (my_rmdir (a->nname) == -1) {
  5467. PUT_PCK_RES1 (packet, DOS_FALSE);
  5468. PUT_PCK_RES2 (packet, dos_errno ());
  5469. return;
  5470. }
  5471. } else {
  5472. if (my_unlink (a->nname, false) == -1) {
  5473. PUT_PCK_RES1 (packet, DOS_FALSE);
  5474. PUT_PCK_RES2 (packet, dos_errno ());
  5475. return;
  5476. }
  5477. }
  5478. }
  5479. notify_check(ctx, unit, a);
  5480. updatedirtime (a, 1);
  5481. if (a->child != 0) {
  5482. write_log (_T("Serious error in action_delete_object.\n"));
  5483. a->deleted = 1;
  5484. } else {
  5485. delete_aino (unit, a);
  5486. }
  5487. PUT_PCK_RES1 (packet, DOS_TRUE);
  5488. gui_flicker_led (UNIT_LED(unit), unit->unit, 2);
  5489. }
  5490. static void action_set_date(TrapContext *ctx, Unit *unit, dpacket *packet)
  5491. {
  5492. uaecptr lock = GET_PCK_ARG2 (packet) << 2;
  5493. uaecptr name = GET_PCK_ARG3 (packet) << 2;
  5494. uaecptr date = GET_PCK_ARG4 (packet);
  5495. a_inode *a;
  5496. struct mytimeval tv;
  5497. int err = 0;
  5498. TRACE((_T("ACTION_SET_DATE(0x%x,\"%s\")\n"), lock, bstr(ctx, unit, name)));
  5499. if (is_writeprotected(unit)) {
  5500. PUT_PCK_RES1 (packet, DOS_FALSE);
  5501. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  5502. return;
  5503. }
  5504. a = find_aino(ctx, unit, lock, bstr(ctx, unit, name), &err);
  5505. if (err != 0) {
  5506. PUT_PCK_RES1 (packet, DOS_FALSE);
  5507. PUT_PCK_RES2 (packet, err);
  5508. return;
  5509. }
  5510. if (a->softlink) {
  5511. handle_softlink(ctx, unit, packet, a);
  5512. return;
  5513. }
  5514. if (!a->vfso) {
  5515. amiga_to_timeval (&tv, trap_get_long(ctx, date), trap_get_long(ctx, date + 4), trap_get_long(ctx, date + 8), 50);
  5516. //write_log (_T("%llu.%u (%d,%d,%d) %s\n"), tv.tv_sec, tv.tv_usec, trap_get_long(ctx, date), trap_get_long(ctx, date + 4), trap_get_long(ctx, date + 8), a->nname);
  5517. if (!my_utime (a->nname, &tv))
  5518. err = dos_errno ();
  5519. }
  5520. if (err != 0) {
  5521. PUT_PCK_RES1 (packet, DOS_FALSE);
  5522. PUT_PCK_RES2 (packet, err);
  5523. return;
  5524. } else {
  5525. notify_check(ctx, unit, a);
  5526. PUT_PCK_RES1 (packet, DOS_TRUE);
  5527. }
  5528. gui_flicker_led (UNIT_LED(unit), unit->unit, 2);
  5529. }
  5530. static void action_rename_object(TrapContext *ctx, Unit *unit, dpacket *packet)
  5531. {
  5532. uaecptr lock1 = GET_PCK_ARG1 (packet) << 2;
  5533. uaecptr name1 = GET_PCK_ARG2 (packet) << 2;
  5534. uaecptr lock2 = GET_PCK_ARG3 (packet) << 2;
  5535. uaecptr name2 = GET_PCK_ARG4 (packet) << 2;
  5536. a_inode *a1, *a2;
  5537. int err1, err2;
  5538. Key *k1, *knext;
  5539. int wehavekeys = 0;
  5540. TRACE((_T("ACTION_RENAME_OBJECT(0x%x,\"%s\","), lock1, bstr(ctx, unit, name1)));
  5541. TRACE((_T("0x%x,\"%s\")\n"), lock2, bstr(ctx, unit, name2)));
  5542. if (is_writeprotected(unit)) {
  5543. PUT_PCK_RES1 (packet, DOS_FALSE);
  5544. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  5545. return;
  5546. }
  5547. a1 = find_aino(ctx, unit, lock1, bstr(ctx, unit, name1), &err1);
  5548. if (err1 != 0) {
  5549. PUT_PCK_RES1 (packet, DOS_FALSE);
  5550. PUT_PCK_RES2 (packet, err1);
  5551. return;
  5552. }
  5553. /* rename always fails if file is open for writing */
  5554. for (k1 = unit->keys; k1; k1 = knext) {
  5555. knext = k1->next;
  5556. if (k1->aino == a1 && k1->fd && k1->createmode == 2) {
  5557. PUT_PCK_RES1 (packet, DOS_FALSE);
  5558. PUT_PCK_RES2 (packet, ERROR_OBJECT_IN_USE);
  5559. return;
  5560. }
  5561. }
  5562. /* See whether the other name already exists in the filesystem. */
  5563. a2 = find_aino(ctx, unit, lock2, bstr(ctx, unit, name2), &err2);
  5564. if (a2 == a1) {
  5565. /* Renaming to the same name, but possibly different case. */
  5566. if (_tcscmp (a1->aname, bstr_cut(ctx, unit, name2)) == 0) {
  5567. /* Exact match -> do nothing. */
  5568. notify_check(ctx, unit, a1);
  5569. updatedirtime (a1, 1);
  5570. PUT_PCK_RES1 (packet, DOS_TRUE);
  5571. return;
  5572. }
  5573. a2 = a2->parent;
  5574. } else if (a2 == 0 || err2 != ERROR_OBJECT_NOT_AROUND) {
  5575. PUT_PCK_RES1 (packet, DOS_FALSE);
  5576. PUT_PCK_RES2 (packet, err2 == 0 ? ERROR_OBJECT_EXISTS : err2);
  5577. return;
  5578. }
  5579. a2 = create_child_aino (unit, a2, bstr_cut(ctx, unit, name2), a1->dir);
  5580. if (a2 == 0) {
  5581. PUT_PCK_RES1 (packet, DOS_FALSE);
  5582. PUT_PCK_RES2 (packet, ERROR_DISK_IS_FULL); /* best we can do */
  5583. return;
  5584. }
  5585. if (!a1->vfso) {
  5586. if (-1 == my_rename (a1->nname, a2->nname)) {
  5587. int ret = -1;
  5588. /* maybe we have open file handles that caused failure? */
  5589. write_log (_T("rename '%s' -> '%s' failed, trying relocking..\n"), a1->nname, a2->nname);
  5590. wehavekeys = relock_do (unit, a1);
  5591. /* try again... */
  5592. ret = my_rename (a1->nname, a2->nname);
  5593. /* restore locks */
  5594. relock_re (unit, a1, a2, ret == -1 ? 1 : 0);
  5595. if (ret == -1) {
  5596. delete_aino (unit, a2);
  5597. PUT_PCK_RES1 (packet, DOS_FALSE);
  5598. PUT_PCK_RES2 (packet, dos_errno ());
  5599. return;
  5600. }
  5601. }
  5602. }
  5603. notify_check(ctx, unit, a1);
  5604. notify_check(ctx, unit, a2);
  5605. a2->comment = a1->comment;
  5606. a1->comment = 0;
  5607. a2->amigaos_mode = a1->amigaos_mode;
  5608. a2->uniq = a1->uniq;
  5609. a2->elock = a1->elock;
  5610. a2->shlock = a1->shlock;
  5611. a2->has_dbentry = a1->has_dbentry;
  5612. a2->db_offset = a1->db_offset;
  5613. a2->dirty = 0;
  5614. a2->vfso = a1->vfso;
  5615. move_exkeys (unit, a1, a2);
  5616. move_aino_children (unit, a1, a2);
  5617. delete_aino (unit, a1);
  5618. a2->dirty = 1;
  5619. if (a2->parent)
  5620. fsdb_dir_writeback (a2->parent);
  5621. updatedirtime (a2, 1);
  5622. fsdb_set_file_attrs (a2);
  5623. if (a2->elock > 0 || a2->shlock > 0 || wehavekeys > 0)
  5624. de_recycle_aino (unit, a2);
  5625. PUT_PCK_RES1 (packet, DOS_TRUE);
  5626. gui_flicker_led (UNIT_LED(unit), unit->unit, 2);
  5627. }
  5628. static void action_current_volume(TrapContext *ctx, Unit *unit, dpacket *packet)
  5629. {
  5630. if (filesys_isvolume(unit))
  5631. PUT_PCK_RES1 (packet, unit->volume >> 2);
  5632. else
  5633. PUT_PCK_RES1 (packet, 0);
  5634. }
  5635. static void action_rename_disk(TrapContext *ctx, Unit *unit, dpacket *packet)
  5636. {
  5637. uaecptr name = GET_PCK_ARG1 (packet) << 2;
  5638. TRACE((_T("ACTION_RENAME_DISK(\"%s\")\n"), bstr(ctx, unit, name)));
  5639. if (is_writeprotected(unit)) {
  5640. PUT_PCK_RES1 (packet, DOS_FALSE);
  5641. PUT_PCK_RES2 (packet, ERROR_DISK_WRITE_PROTECTED);
  5642. return;
  5643. }
  5644. /* get volume name */
  5645. xfree (unit->ui.volname);
  5646. unit->ui.volname = bstr1(ctx, name);
  5647. set_volume_name(unit, 0);
  5648. PUT_PCK_RES1 (packet, DOS_TRUE);
  5649. }
  5650. static void action_is_filesystem(TrapContext *ctx, Unit *unit, dpacket *packet)
  5651. {
  5652. TRACE((_T("ACTION_IS_FILESYSTEM()\n")));
  5653. PUT_PCK_RES1 (packet, DOS_TRUE);
  5654. }
  5655. static void action_flush(TrapContext *ctx, Unit *unit, dpacket *packet)
  5656. {
  5657. TRACE((_T("ACTION_FLUSH()\n")));
  5658. PUT_PCK_RES1 (packet, DOS_TRUE);
  5659. flush_cache (unit, 0);
  5660. }
  5661. static void action_more_cache(TrapContext *ctx, Unit *unit, dpacket *packet)
  5662. {
  5663. TRACE((_T("ACTION_MORE_CACHE()\n")));
  5664. PUT_PCK_RES1 (packet, 50); /* bug but AmigaOS expects it */
  5665. if (GET_PCK_ARG1 (packet) != 0)
  5666. flush_cache (unit, 0);
  5667. }
  5668. static void action_inhibit(TrapContext *ctx, Unit *unit, dpacket *packet)
  5669. {
  5670. int old = unit->inhibited;
  5671. PUT_PCK_RES1 (packet, DOS_TRUE);
  5672. flush_cache (unit, 0);
  5673. unit->inhibited = GET_PCK_ARG1 (packet) != 0;
  5674. TRACE((_T("ACTION_INHIBIT(%d:%d)\n"), unit->unit, unit->inhibited));
  5675. if (unit->ui.canremove && !unit->ui.configureddrive && unit->ui.open && old && !GET_PCK_ARG1(packet)) {
  5676. filesys_delayed_eject(unit);
  5677. }
  5678. }
  5679. static void action_write_protect(TrapContext *ctx, Unit *unit, dpacket *packet)
  5680. {
  5681. TRACE((_T("ACTION_WRITE_PROTECT()\n")));
  5682. PUT_PCK_RES1 (packet, DOS_TRUE);
  5683. if (GET_PCK_ARG1 (packet)) {
  5684. if (!unit->ui.locked) {
  5685. unit->ui.locked = true;
  5686. unit->lockkey = GET_PCK_ARG2 (packet);
  5687. }
  5688. } else {
  5689. if (unit->ui.locked) {
  5690. if (unit->lockkey == GET_PCK_ARG2 (packet) || unit->lockkey == 0) {
  5691. unit->ui.locked = false;
  5692. } else {
  5693. PUT_PCK_RES1 (packet, DOS_FALSE);
  5694. PUT_PCK_RES2 (packet, 0);
  5695. }
  5696. }
  5697. }
  5698. }
  5699. /* OS4 */
  5700. #define TAG_DONE 0
  5701. #define TAG_IGNORE 1
  5702. #define TAG_MORE 2
  5703. #define TAG_SKIP 3
  5704. static void action_filesystem_attr(TrapContext *ctx, Unit *unit, dpacket *packet)
  5705. {
  5706. int versize = 0;
  5707. uaecptr verbuffer = 0;
  5708. uaecptr taglist = GET_PCK_ARG1(packet);
  5709. for (;;) {
  5710. uae_u32 tag = trap_get_long(ctx, taglist);
  5711. uae_u32 tagp = taglist + 4;
  5712. if (tag == TAG_DONE)
  5713. break;
  5714. taglist += 8;
  5715. if (tag == TAG_IGNORE)
  5716. continue;
  5717. if (tag == TAG_MORE) {
  5718. uae_u32 val = trap_get_long(ctx, tagp);
  5719. taglist = val;
  5720. continue;
  5721. }
  5722. if (tag == TAG_SKIP) {
  5723. uae_u32 val = trap_get_long(ctx, tagp);
  5724. taglist += val * 8;
  5725. continue;
  5726. }
  5727. uae_u32 retval = 0;
  5728. bool doret = false;
  5729. switch(tag)
  5730. {
  5731. case 0x80002332: // FSA_MaxFileNameLengthR
  5732. retval = currprefs.filesys_max_name;
  5733. doret = true;
  5734. break;
  5735. case 0x80002334: // FSA_VersionNumberR
  5736. retval = (0 << 16) | (5 << 0);
  5737. doret = true;
  5738. break;
  5739. case 0x80002335: // FSA_DOSTypeR
  5740. retval = get_long(unit->volume + 32);
  5741. doret = true;
  5742. break;
  5743. case 0x80002336: // FSA_ActivityFlushTimeoutR
  5744. case 0x80002338: // FSA_InactivityFlushTimeoutR
  5745. retval = 0;
  5746. doret = true;
  5747. break;
  5748. case 0x8000233a: // FSA_MaxRecycledEntriesR
  5749. case 0x8000233c: // FSA_HasRecycledEntriesR
  5750. retval = 0;
  5751. doret = true;
  5752. break;
  5753. case 0x8000233d: // FSA_VersionStringR
  5754. verbuffer = trap_get_long(ctx, tagp);
  5755. break;
  5756. case 0x8000233e: // FSA_VersionStringR_BufSize
  5757. versize = trap_get_long(ctx, tagp);
  5758. break;
  5759. default:
  5760. write_log(_T("action_filesystem_attr unknown tag %08x\n"), tag);
  5761. PUT_PCK64_RES1(packet, DOS_FALSE);
  5762. PUT_PCK64_RES2(packet, ERROR_NOT_IMPLEMENTED);
  5763. return;
  5764. }
  5765. if (doret)
  5766. trap_put_long(ctx, trap_get_long(ctx, tagp), retval);
  5767. }
  5768. if (verbuffer && versize) {
  5769. trap_put_string(ctx, UAEFS_VERSION, verbuffer, versize);
  5770. }
  5771. PUT_PCK_RES1(packet, TRUE);
  5772. PUT_PCK_RES2(packet, 0);
  5773. }
  5774. static void action_change_file_position64(TrapContext *ctx, Unit *unit, dpacket *packet)
  5775. {
  5776. Key *k = lookup_key (unit, GET_PCK64_ARG1 (packet));
  5777. uae_s64 pos = GET_PCK64_ARG2 (packet);
  5778. int mode = (uae_s32)GET_PCK64_ARG3 (packet);
  5779. long whence = SEEK_CUR;
  5780. uae_s64 res, cur;
  5781. PUT_PCK64_RES0 (packet, DP64_INIT);
  5782. if (k == 0) {
  5783. PUT_PCK64_RES1 (packet, DOS_FALSE);
  5784. PUT_PCK64_RES2 (packet, ERROR_INVALID_LOCK);
  5785. return;
  5786. }
  5787. if (mode > 0)
  5788. whence = SEEK_END;
  5789. if (mode < 0)
  5790. whence = SEEK_SET;
  5791. TRACE((_T("ACTION_CHANGE_FILE_POSITION64(%s,%lld,%d)\n"), k->aino->nname, pos, mode));
  5792. gui_flicker_led (UNIT_LED(unit), unit->unit, 1);
  5793. cur = k->file_pos;
  5794. {
  5795. uae_s64 temppos;
  5796. uae_s64 filesize = key_filesize(k);
  5797. if (whence == SEEK_CUR)
  5798. temppos = cur + pos;
  5799. if (whence == SEEK_SET)
  5800. temppos = pos;
  5801. if (whence == SEEK_END)
  5802. temppos = filesize + pos;
  5803. if (filesize < temppos) {
  5804. res = -1;
  5805. PUT_PCK64_RES1 (packet, res);
  5806. PUT_PCK64_RES2 (packet, ERROR_SEEK_ERROR);
  5807. return;
  5808. }
  5809. }
  5810. res = key_seek(k, pos, whence);
  5811. if (-1 == res) {
  5812. PUT_PCK64_RES1 (packet, DOS_FALSE);
  5813. PUT_PCK64_RES2 (packet, ERROR_SEEK_ERROR);
  5814. } else {
  5815. PUT_PCK64_RES1 (packet, TRUE);
  5816. PUT_PCK64_RES2 (packet, 0);
  5817. k->file_pos = key_seek(k, 0, SEEK_CUR);
  5818. }
  5819. TRACE((_T("= oldpos %lld newpos %lld\n"), cur, k->file_pos));
  5820. }
  5821. static void action_get_file_position64(TrapContext *ctx, Unit *unit, dpacket *packet)
  5822. {
  5823. Key *k = lookup_key (unit, GET_PCK64_ARG1 (packet));
  5824. PUT_PCK64_RES0 (packet, DP64_INIT);
  5825. if (k == 0) {
  5826. PUT_PCK64_RES1 (packet, -1);
  5827. PUT_PCK64_RES2 (packet, ERROR_INVALID_LOCK);
  5828. return;
  5829. }
  5830. TRACE((_T("ACTION_GET_FILE_POSITION64(%s)=%lld\n"), k->aino->nname, k->file_pos));
  5831. PUT_PCK64_RES1 (packet, k->file_pos);
  5832. PUT_PCK64_RES2 (packet, 0);
  5833. }
  5834. static void action_change_file_size64(TrapContext *ctx, Unit *unit, dpacket *packet)
  5835. {
  5836. Key *k, *k1;
  5837. uae_s64 offset = GET_PCK64_ARG2 (packet);
  5838. int mode = (uae_s32)GET_PCK64_ARG3 (packet);
  5839. int whence = SEEK_CUR;
  5840. PUT_PCK64_RES0 (packet, DP64_INIT);
  5841. if (mode > 0)
  5842. whence = SEEK_END;
  5843. if (mode < 0)
  5844. whence = SEEK_SET;
  5845. TRACE((_T("ACTION_CHANGE_FILE_SIZE64(0x%x, %lld, 0x%x)\n"), GET_PCK64_ARG1 (packet), offset, mode));
  5846. k = lookup_key (unit, GET_PCK64_ARG1 (packet));
  5847. if (k == 0) {
  5848. PUT_PCK64_RES1 (packet, DOS_FALSE);
  5849. PUT_PCK64_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  5850. return;
  5851. }
  5852. gui_flicker_led (UNIT_LED(unit), unit->unit, 1);
  5853. k->notifyactive = 1;
  5854. /* If any open files have file pointers beyond this size, truncate only
  5855. * so far that these pointers do not become invalid. */
  5856. for (k1 = unit->keys; k1; k1 = k1->next) {
  5857. if (k != k1 && k->aino == k1->aino) {
  5858. if (k1->file_pos > offset)
  5859. offset = k1->file_pos;
  5860. }
  5861. }
  5862. /* Write one then truncate: that should give the right size in all cases. */
  5863. fs_lseek (k->fd, offset, whence);
  5864. offset = key_seek(k, offset, whence);
  5865. fs_write (k->fd, /* whatever */(uae_u8*)&k1, 1);
  5866. if (k->file_pos > offset)
  5867. k->file_pos = offset;
  5868. key_seek(k, k->file_pos, SEEK_SET);
  5869. if (my_truncate (k->aino->nname, offset) == -1) {
  5870. PUT_PCK64_RES1 (packet, DOS_FALSE);
  5871. PUT_PCK64_RES2 (packet, dos_errno ());
  5872. return;
  5873. }
  5874. PUT_PCK64_RES1 (packet, DOS_TRUE);
  5875. PUT_PCK64_RES2 (packet, 0);
  5876. }
  5877. static void action_get_file_size64(TrapContext *ctx, Unit *unit, dpacket *packet)
  5878. {
  5879. Key *k = lookup_key (unit, GET_PCK64_ARG1 (packet));
  5880. uae_s64 filesize;
  5881. PUT_PCK64_RES0 (packet, DP64_INIT);
  5882. if (k == 0) {
  5883. PUT_PCK64_RES1 (packet, -1);
  5884. PUT_PCK64_RES2 (packet, ERROR_INVALID_LOCK);
  5885. return;
  5886. }
  5887. TRACE((_T("ACTION_GET_FILE_SIZE64(%s)\n"), k->aino->nname));
  5888. filesize = key_filesize(k);
  5889. TRACE((_T("ACTION_GET_FILE_SIZE64(%s)=%lld\n"), k->aino->nname, filesize));
  5890. if (filesize >= 0) {
  5891. PUT_PCK64_RES1 (packet, filesize);
  5892. PUT_PCK64_RES2 (packet, 0);
  5893. return;
  5894. }
  5895. PUT_PCK64_RES1 (packet, -1);
  5896. PUT_PCK64_RES2 (packet, ERROR_SEEK_ERROR);
  5897. }
  5898. /* MOS */
  5899. static void action_examine_object64(TrapContext *ctx, Unit *unit, dpacket *packet)
  5900. {
  5901. uaecptr lock = GET_PCK_ARG1 (packet) << 2;
  5902. uaecptr info = GET_PCK_ARG2 (packet) << 2;
  5903. a_inode *aino = 0;
  5904. TRACE((_T("ACTION_EXAMINE_OBJECT(0x%x,0x%x)\n"), lock, info));
  5905. DUMPLOCK(ctx, unit, lock);
  5906. if (lock != 0)
  5907. aino = aino_from_lock(ctx, unit, lock);
  5908. if (aino == 0)
  5909. aino = &unit->rootnode;
  5910. get_fileinfo(ctx, unit, packet, info, aino, true);
  5911. }
  5912. static void action_set_file_size64(TrapContext *ctx, Unit *unit, dpacket *packet)
  5913. {
  5914. Key *k, *k1;
  5915. uae_s64 offset = get_quadp(ctx, GET_PCK_ARG2 (packet));
  5916. int mode = (uae_s32)GET_PCK_ARG3 (packet);
  5917. int whence = SEEK_CUR;
  5918. if (mode > 0)
  5919. whence = SEEK_END;
  5920. if (mode < 0)
  5921. whence = SEEK_SET;
  5922. TRACE((_T("ACTION_SET_FILE_SIZE64(0x%x, %lld, 0x%x)\n"), GET_PCK_ARG1 (packet), offset, mode));
  5923. k = lookup_key (unit, GET_PCK_ARG1 (packet));
  5924. if (k == 0) {
  5925. PUT_PCK_RES1 (packet, DOS_FALSE);
  5926. PUT_PCK_RES2 (packet, ERROR_OBJECT_NOT_AROUND);
  5927. return;
  5928. }
  5929. gui_flicker_led (UNIT_LED(unit), unit->unit, 1);
  5930. k->notifyactive = 1;
  5931. /* If any open files have file pointers beyond this size, truncate only
  5932. * so far that these pointers do not become invalid. */
  5933. for (k1 = unit->keys; k1; k1 = k1->next) {
  5934. if (k != k1 && k->aino == k1->aino) {
  5935. if (k1->file_pos > offset)
  5936. offset = k1->file_pos;
  5937. }
  5938. }
  5939. /* Write one then truncate: that should give the right size in all cases. */
  5940. fs_lseek (k->fd, offset, whence);
  5941. offset = key_seek(k, offset, whence);
  5942. fs_write (k->fd, /* whatever */(uae_u8*)&k1, 1);
  5943. if (k->file_pos > offset)
  5944. k->file_pos = offset;
  5945. key_seek(k, k->file_pos, SEEK_SET);
  5946. if (my_truncate (k->aino->nname, offset) == -1) {
  5947. PUT_PCK_RES1 (packet, DOS_FALSE);
  5948. PUT_PCK_RES2 (packet, dos_errno ());
  5949. return;
  5950. }
  5951. PUT_PCK_RES1 (packet, DOS_TRUE);
  5952. set_quadp(ctx, GET_PCK_ARG4(packet), offset);
  5953. }
  5954. static void action_seek64(TrapContext *ctx, Unit *unit, dpacket *packet)
  5955. {
  5956. Key *k = lookup_key(unit, GET_PCK_ARG1(packet));
  5957. uae_s64 pos = get_quadp(ctx, GET_PCK64_ARG2(packet));
  5958. int mode = GET_PCK_ARG3(packet);
  5959. long whence = SEEK_CUR;
  5960. uae_s64 res, cur;
  5961. if (k == 0) {
  5962. PUT_PCK_RES1 (packet, DOS_FALSE);
  5963. PUT_PCK_RES2 (packet, ERROR_INVALID_LOCK);
  5964. return;
  5965. }
  5966. if (mode > 0)
  5967. whence = SEEK_END;
  5968. if (mode < 0)
  5969. whence = SEEK_SET;
  5970. TRACE((_T("ACTION_SEEK64(%s,%lld,%d)\n"), k->aino->nname, pos, mode));
  5971. gui_flicker_led (UNIT_LED(unit), unit->unit, 1);
  5972. cur = k->file_pos;
  5973. {
  5974. uae_s64 temppos;
  5975. uae_s64 filesize = key_filesize(k);
  5976. if (whence == SEEK_CUR)
  5977. temppos = cur + pos;
  5978. if (whence == SEEK_SET)
  5979. temppos = pos;
  5980. if (whence == SEEK_END)
  5981. temppos = filesize + pos;
  5982. if (filesize < temppos) {
  5983. res = -1;
  5984. PUT_PCK_RES1 (packet, res);
  5985. PUT_PCK_RES2 (packet, ERROR_SEEK_ERROR);
  5986. return;
  5987. }
  5988. }
  5989. res = key_seek(k, pos, whence);
  5990. if (-1 == res) {
  5991. PUT_PCK_RES1 (packet, DOS_FALSE);
  5992. PUT_PCK_RES2 (packet, ERROR_SEEK_ERROR);
  5993. } else {
  5994. PUT_PCK_RES1 (packet, TRUE);
  5995. set_quadp(ctx, GET_PCK_ARG3(packet), cur);
  5996. k->file_pos = key_seek(k, 0, SEEK_CUR);
  5997. }
  5998. TRACE((_T("= oldpos %lld newpos %lld\n"), cur, k->file_pos));
  5999. }
  6000. static int action_lock_record64(TrapContext *ctx, Unit *unit, dpacket *packet, uae_u32 msg)
  6001. {
  6002. Key *k = lookup_key(unit, GET_PCK_ARG1(packet));
  6003. uae_u64 pos = get_quadp(ctx, GET_PCK_ARG2(packet));
  6004. uae_u64 len = get_quadp(ctx, GET_PCK_ARG3(packet));
  6005. uae_u32 mode = GET_PCK_ARG4(packet);
  6006. uae_u32 timeout = GET_PCK_ARG5(packet);
  6007. bool exclusive = mode == REC_EXCLUSIVE || mode == REC_EXCLUSIVE_IMMED;
  6008. write_log (_T("action_lock_record64('%s',%lld,%lld,%d,%d)\n"), k ? k->aino->nname : _T("null"), pos, len, mode, timeout);
  6009. if (!k || mode > REC_SHARED_IMMED) {
  6010. PUT_PCK_RES1 (packet, DOS_FALSE);
  6011. PUT_PCK_RES2 (packet, ERROR_OBJECT_WRONG_TYPE);
  6012. return 1;
  6013. }
  6014. if (mode == REC_EXCLUSIVE_IMMED || mode == REC_SHARED_IMMED)
  6015. timeout = 0;
  6016. if (record_hit (unit, k, pos, len, mode)) {
  6017. if (timeout && msg) {
  6018. // queue it and do not reply
  6019. struct lockrecord *lr = new_record (packet, pos, len, mode, timeout, msg);
  6020. if (unit->waitingrecords) {
  6021. lr->next = unit->waitingrecords;
  6022. unit->waitingrecords = lr;
  6023. } else {
  6024. unit->waitingrecords = lr;
  6025. }
  6026. write_log (_T("-> collision, timeout queued\n"));
  6027. return -1;
  6028. }
  6029. PUT_PCK_RES1 (packet, DOS_FALSE);
  6030. PUT_PCK_RES2 (packet, ERROR_LOCK_COLLISION);
  6031. write_log (_T("-> ERROR_LOCK_COLLISION\n"));
  6032. return 1;
  6033. }
  6034. struct lockrecord *lr = new_record(packet, pos, len, mode, timeout, 0);
  6035. if (k->record) {
  6036. lr->next = k->record;
  6037. k->record = lr;
  6038. } else {
  6039. k->record = lr;
  6040. }
  6041. PUT_PCK_RES1 (packet, DOS_TRUE);
  6042. write_log (_T("-> OK\n"));
  6043. return 1;
  6044. }
  6045. static void action_free_record64(TrapContext *ctx, Unit *unit, dpacket *packet)
  6046. {
  6047. Key *k = lookup_key(unit, GET_PCK_ARG1(packet));
  6048. uae_u64 pos = get_quadp(ctx, GET_PCK_ARG2(packet));
  6049. uae_u64 len = get_quadp(ctx, GET_PCK_ARG3 (packet));
  6050. write_log (_T("action_free_record('%s',%lld,%lld)\n"), k ? k->aino->nname : _T("null"), pos, len);
  6051. if (!k) {
  6052. PUT_PCK_RES1 (packet, DOS_FALSE);
  6053. PUT_PCK_RES2 (packet, ERROR_OBJECT_WRONG_TYPE);
  6054. return;
  6055. }
  6056. struct lockrecord *prev = NULL;
  6057. for (struct lockrecord *lr = k->record; lr; lr = lr->next) {
  6058. if (lr->pos == pos && lr->len == len) {
  6059. if (prev)
  6060. prev->next = lr->next;
  6061. else
  6062. k->record = lr->next;
  6063. xfree (lr);
  6064. write_log (_T("->OK\n"));
  6065. record_check_waiting(ctx, unit);
  6066. PUT_PCK_RES1 (packet, DOS_TRUE);
  6067. return;
  6068. }
  6069. }
  6070. write_log (_T("-> ERROR_RECORD_NOT_LOCKED\n"));
  6071. PUT_PCK_RES1 (packet, DOS_FALSE);
  6072. PUT_PCK_RES2 (packet, ERROR_RECORD_NOT_LOCKED);
  6073. }
  6074. static int filesys_shellexecute2_process(int mode, TrapContext *ctx);
  6075. /* We don't want multiple interrupts to be active at the same time. I don't
  6076. * know whether AmigaOS takes care of that, but this does. */
  6077. static uae_sem_t singlethread_int_sem;
  6078. #define SHELLEXEC_MAX_CMD_LEN 512
  6079. static uae_u32 REGPARAM2 exter_int_helper(TrapContext *ctx)
  6080. {
  6081. UnitInfo *uip = mountinfo.ui;
  6082. uaecptr port;
  6083. int n = trap_get_dreg(ctx, 0);
  6084. static int unit_no;
  6085. if (n == 20) {
  6086. // d1 = shellexec process
  6087. shell_execute_process = trap_get_dreg(ctx, 1);
  6088. return 0;
  6089. } else if (n == 21) {
  6090. trap_set_areg(ctx, 0, 0);
  6091. if (comm_pipe_has_data(&shellexecute_pipe)) {
  6092. TCHAR *p = (TCHAR*)read_comm_pipe_pvoid_blocking(&shellexecute_pipe);
  6093. if (p) {
  6094. int maxsize = SHELLEXEC_MAX_CMD_LEN - 1;
  6095. if (shell_execute_data) {
  6096. uae_char *src = ua(p);
  6097. uae_u8 *dst = uaeboard_map_ram(shell_execute_data);
  6098. uae_char *srcp = src;
  6099. while (maxsize-- > 0) {
  6100. uae_u8 v = *srcp++;
  6101. *dst++ = v;
  6102. if (!v)
  6103. break;
  6104. }
  6105. *dst = 0;
  6106. xfree(src);
  6107. }
  6108. trap_set_areg(ctx, 0, shell_execute_data);
  6109. }
  6110. xfree(p);
  6111. }
  6112. return 0;
  6113. } else if (n == 22) {
  6114. // ack
  6115. return 0;
  6116. } else if (n >= 30 && n <= 39) {
  6117. return filesys_shellexecute2_process(n, ctx);
  6118. }
  6119. if (n == 1) {
  6120. /* Release a message_lock. This is called as soon as the message is
  6121. * received by the assembly code. We use the opportunity to check
  6122. * whether we have some locks that we can give back to the assembler
  6123. * code.
  6124. * Note that this is called from the main loop, unlike the other cases
  6125. * in this switch statement which are called from the interrupt handler.
  6126. */
  6127. #ifdef UAE_FILESYS_THREADS
  6128. {
  6129. Unit *unit = find_unit(trap_get_areg(ctx, 5));
  6130. uaecptr msg = trap_get_areg(ctx, 4);
  6131. unit->cmds_complete = unit->cmds_acked;
  6132. while (comm_pipe_has_data(unit->ui.back_pipe)) {
  6133. uaecptr locks, lockend, lockv;
  6134. int cnt = 0;
  6135. locks = read_comm_pipe_int_blocking(unit->ui.back_pipe);
  6136. lockend = locks;
  6137. while ((lockv = trap_get_long(ctx, lockend)) != 0) {
  6138. if (lockv == lockend) {
  6139. write_log(_T("filesystem lock queue corrupted!\n"));
  6140. break;
  6141. }
  6142. lockend = lockv;
  6143. cnt++;
  6144. }
  6145. TRACE3((_T("message_lock: %d %x %x %x\n"), cnt, locks, lockend, trap_get_areg(ctx, 3)));
  6146. trap_put_long(ctx, lockend, trap_get_long(ctx, trap_get_areg(ctx, 3)));
  6147. trap_put_long(ctx, trap_get_areg(ctx, 3), locks);
  6148. }
  6149. }
  6150. #else
  6151. write_log(_T("exter_int_helper should not be called with arg 1!\n"));
  6152. #endif
  6153. return 0;
  6154. }
  6155. if (n == 10) {
  6156. if (uae_sem_trywait(&singlethread_int_sem) != 0) {
  6157. /* Pretend it isn't for us. We might get it again later. */
  6158. do_uae_int_requested();
  6159. return 0;
  6160. }
  6161. filesys_in_interrupt++;
  6162. unit_no = 0;
  6163. }
  6164. if (n >= 10) {
  6165. /* Find work that needs to be done:
  6166. * return d0 = 0: none
  6167. * d0 = 1: PutMsg(), port in a0, message in a1
  6168. * d0 = 2: Signal(), task in a1, signal set in d1
  6169. * d0 = 3: ReplyMsg(), message in a1
  6170. * d0 = 4: Cause(), interrupt in a1
  6171. * d0 = 5: Send FileNofication message, port in a0, notifystruct in a1
  6172. */
  6173. #ifdef SUPPORT_THREADS
  6174. /* First, check signals/messages */
  6175. while (comm_pipe_has_data(&native2amiga_pending)) {
  6176. int cmd = read_comm_pipe_int_blocking(&native2amiga_pending);
  6177. if (cmd & 0x80) {
  6178. cmd &= ~0x80;
  6179. // tell the sender that message was processed
  6180. UAE_PROCESSED func = (UAE_PROCESSED)read_comm_pipe_pvoid_blocking(&native2amiga_pending);
  6181. func(cmd);
  6182. }
  6183. switch (cmd) {
  6184. case 0: /* Signal() */
  6185. trap_set_areg(ctx, 1, read_comm_pipe_u32_blocking(&native2amiga_pending));
  6186. trap_set_dreg(ctx, 1, read_comm_pipe_u32_blocking(&native2amiga_pending));
  6187. return 2;
  6188. case 1: /* PutMsg() */
  6189. trap_set_areg(ctx, 0, read_comm_pipe_u32_blocking(&native2amiga_pending));
  6190. trap_set_areg(ctx, 1, read_comm_pipe_u32_blocking(&native2amiga_pending));
  6191. return 1;
  6192. case 2: /* ReplyMsg() */
  6193. trap_set_areg(ctx, 1, read_comm_pipe_u32_blocking(&native2amiga_pending));
  6194. return 3;
  6195. case 3: /* Cause() */
  6196. trap_set_areg(ctx, 1, read_comm_pipe_u32_blocking(&native2amiga_pending));
  6197. return 4;
  6198. case 4: /* NotifyHack() */
  6199. trap_set_areg(ctx, 0, read_comm_pipe_u32_blocking(&native2amiga_pending));
  6200. trap_set_areg(ctx, 1, read_comm_pipe_u32_blocking(&native2amiga_pending));
  6201. return 5;
  6202. case 5: /* shell execute */
  6203. {
  6204. TCHAR *p = (TCHAR*)read_comm_pipe_pvoid_blocking(&native2amiga_pending);
  6205. write_comm_pipe_pvoid(&shellexecute_pipe, p, 0);
  6206. if (shell_execute_data) {
  6207. if (!shell_execute_process)
  6208. break;
  6209. trap_set_areg(ctx, 1, shell_execute_process - 92);
  6210. trap_set_dreg(ctx, 1, 1 << 13);
  6211. return 2; // signal process
  6212. }
  6213. shell_execute_data = uaeboard_alloc_ram(SHELLEXEC_MAX_CMD_LEN);
  6214. return 6; // create process
  6215. }
  6216. case 6: /* shell execute 2 */
  6217. {
  6218. trap_set_areg(ctx, 0, read_comm_pipe_u32_blocking(&native2amiga_pending));
  6219. return 7;
  6220. }
  6221. default:
  6222. write_log(_T("exter_int_helper: unknown native action %X\n"), cmd);
  6223. break;
  6224. }
  6225. }
  6226. #endif
  6227. /* Find some unit that needs a message sent, and return its port,
  6228. * or zero if all are done.
  6229. * Take care not to dereference self for units that didn't have their
  6230. * startup packet sent. */
  6231. for (;;) {
  6232. if (unit_no >= MAX_FILESYSTEM_UNITS)
  6233. goto end;
  6234. if (uip[unit_no].open > 0 && uip[unit_no].self != 0
  6235. && uip[unit_no].self->cmds_acked == uip[unit_no].self->cmds_complete
  6236. && uip[unit_no].self->cmds_acked != uip[unit_no].self->cmds_sent)
  6237. break;
  6238. unit_no++;
  6239. }
  6240. uip[unit_no].self->cmds_acked = uip[unit_no].self->cmds_sent;
  6241. port = uip[unit_no].self->port;
  6242. if (port) {
  6243. trap_set_areg(ctx, 0, port);
  6244. trap_set_areg(ctx, 1, find_unit(port)->dummy_message);
  6245. unit_no++;
  6246. return 1;
  6247. }
  6248. end:
  6249. /* Exit the interrupt, and release the single-threading lock. */
  6250. filesys_in_interrupt--;
  6251. uae_sem_post(&singlethread_int_sem);
  6252. }
  6253. return 0;
  6254. }
  6255. static int handle_packet(TrapContext *ctx, Unit *unit, dpacket *pck, uae_u32 msg, int isvolume)
  6256. {
  6257. bool noidle = false;
  6258. int ret = 1;
  6259. uae_s32 type = GET_PCK_TYPE (pck);
  6260. PUT_PCK_RES2 (pck, 0);
  6261. TRACE((_T("unit=%p packet=%d\n"), unit, type));
  6262. if (unit->inhibited && isvolume
  6263. && type != ACTION_INHIBIT && type != ACTION_MORE_CACHE
  6264. && type != ACTION_DISK_INFO) {
  6265. PUT_PCK_RES1 (pck, DOS_FALSE);
  6266. PUT_PCK_RES2 (pck, ERROR_NOT_A_DOS_DISK);
  6267. return 1;
  6268. }
  6269. if (type != ACTION_INHIBIT && type != ACTION_CURRENT_VOLUME
  6270. && type != ACTION_IS_FILESYSTEM && type != ACTION_MORE_CACHE
  6271. && type != ACTION_WRITE_PROTECT && type != ACTION_DISK_INFO
  6272. && !isvolume) {
  6273. PUT_PCK_RES1 (pck, DOS_FALSE);
  6274. PUT_PCK_RES2 (pck, unit->ui.unknown_media ? ERROR_NOT_A_DOS_DISK : ERROR_NO_DISK);
  6275. return 1;
  6276. }
  6277. switch (type) {
  6278. case ACTION_LOCATE_OBJECT: action_lock (ctx, unit, pck); break;
  6279. case ACTION_FREE_LOCK: action_free_lock (ctx, unit, pck); break;
  6280. case ACTION_COPY_DIR: action_dup_lock (ctx, unit, pck); break;
  6281. case ACTION_DISK_INFO: action_disk_info (ctx, unit, pck); break;
  6282. case ACTION_INFO: action_info (ctx, unit, pck); break;
  6283. case ACTION_EXAMINE_OBJECT: action_examine_object (ctx, unit, pck); break;
  6284. case ACTION_EXAMINE_NEXT: noidle = true; action_examine_next (ctx, unit, pck, false); break;
  6285. case ACTION_FIND_INPUT: action_find_input (ctx, unit, pck); break;
  6286. case ACTION_FIND_WRITE: action_find_write (ctx, unit, pck); break;
  6287. case ACTION_FIND_OUTPUT: action_find_output (ctx, unit, pck); break;
  6288. case ACTION_END: action_end (ctx, unit, pck); break;
  6289. case ACTION_READ: noidle = true; action_read(ctx, unit, pck); break;
  6290. case ACTION_WRITE: noidle = true; action_write (ctx, unit, pck); break;
  6291. case ACTION_SEEK: action_seek (ctx, unit, pck); break;
  6292. case ACTION_SET_PROTECT: action_set_protect (ctx, unit, pck); break;
  6293. case ACTION_SET_COMMENT: action_set_comment (ctx, unit, pck); break;
  6294. case ACTION_SAME_LOCK: action_same_lock (ctx, unit, pck); break;
  6295. case ACTION_PARENT: action_parent (ctx, unit, pck); break;
  6296. case ACTION_CREATE_DIR: action_create_dir (ctx, unit, pck); break;
  6297. case ACTION_DELETE_OBJECT: action_delete_object (ctx, unit, pck); break;
  6298. case ACTION_RENAME_OBJECT: action_rename_object (ctx, unit, pck); break;
  6299. case ACTION_SET_DATE: action_set_date (ctx, unit, pck); break;
  6300. case ACTION_CURRENT_VOLUME: action_current_volume (ctx, unit, pck); break;
  6301. case ACTION_RENAME_DISK: action_rename_disk (ctx, unit, pck); break;
  6302. case ACTION_IS_FILESYSTEM: action_is_filesystem (ctx, unit, pck); break;
  6303. case ACTION_FLUSH: action_flush (ctx, unit, pck); break;
  6304. case ACTION_MORE_CACHE: action_more_cache (ctx, unit, pck); break;
  6305. case ACTION_INHIBIT: action_inhibit (ctx, unit, pck); break;
  6306. case ACTION_WRITE_PROTECT: action_write_protect (ctx, unit, pck); break;
  6307. /* 2.0+ packet types */
  6308. case ACTION_SET_FILE_SIZE: action_set_file_size (ctx, unit, pck); break;
  6309. case ACTION_EXAMINE_FH: action_examine_fh (ctx, unit, pck, false); break;
  6310. case ACTION_FH_FROM_LOCK: action_fh_from_lock (ctx, unit, pck); break;
  6311. case ACTION_COPY_DIR_FH: action_lock_from_fh (ctx, unit, pck); break;
  6312. case ACTION_CHANGE_MODE: action_change_mode (ctx, unit, pck); break;
  6313. case ACTION_PARENT_FH: action_parent_fh (ctx, unit, pck); break;
  6314. case ACTION_ADD_NOTIFY: action_add_notify (ctx, unit, pck); break;
  6315. case ACTION_REMOVE_NOTIFY: action_remove_notify (ctx, unit, pck); break;
  6316. case ACTION_EXAMINE_ALL: noidle = true; ret = action_examine_all(ctx, unit, pck); break;
  6317. case ACTION_EXAMINE_ALL_END: ret = action_examine_all_end(ctx, unit, pck); break;
  6318. case ACTION_LOCK_RECORD: ret = action_lock_record (ctx, unit, pck, msg); break;
  6319. case ACTION_FREE_RECORD: action_free_record (ctx, unit, pck); break;
  6320. case ACTION_READ_LINK: action_read_link (ctx, unit, pck); break;
  6321. case ACTION_MAKE_LINK: action_make_link (ctx, unit, pck); break;
  6322. /* OS4 packet types */
  6323. case ACTION_FILESYSTEM_ATTR: action_filesystem_attr(ctx, unit, pck); break;
  6324. case ACTION_CHANGE_FILE_POSITION64: action_change_file_position64 (ctx, unit, pck); break;
  6325. case ACTION_GET_FILE_POSITION64: action_get_file_position64 (ctx, unit, pck); break;
  6326. case ACTION_CHANGE_FILE_SIZE64: action_change_file_size64 (ctx, unit, pck); break;
  6327. case ACTION_GET_FILE_SIZE64: action_get_file_size64 (ctx, unit, pck); break;
  6328. /* MOS packet types */
  6329. case ACTION_SEEK64: action_seek64(ctx, unit, pck); break;
  6330. case ACTION_SET_FILE_SIZE64: action_set_file_size64(ctx, unit, pck); break;
  6331. case ACTION_EXAMINE_OBJECT64: action_examine_object64(ctx, unit, pck); break;
  6332. case ACTION_EXAMINE_NEXT64: noidle = true; action_examine_next(ctx, unit, pck, true); break;
  6333. case ACTION_EXAMINE_FH64: action_examine_fh(ctx, unit, pck, true); break;
  6334. case ACTION_LOCK_RECORD64: ret = action_lock_record64(ctx, unit, pck, msg); break;
  6335. case ACTION_FREE_RECORD64: action_free_record64(ctx, unit, pck); break;
  6336. /* unsupported packets */
  6337. case ACTION_FORMAT:
  6338. write_log (_T("FILESYS: UNSUPPORTED PACKET %x\n"), type);
  6339. return 0;
  6340. default:
  6341. write_log (_T("FILESYS: UNKNOWN PACKET %x\n"), type);
  6342. return 0;
  6343. }
  6344. if (noidle) {
  6345. m68k_cancel_idle();
  6346. }
  6347. return ret;
  6348. }
  6349. #ifdef UAE_FILESYS_THREADS
  6350. static int filesys_iteration(UnitInfo *ui)
  6351. {
  6352. uaecptr pck;
  6353. uaecptr msg;
  6354. uae_u32 morelocks;
  6355. TrapContext *ctx;
  6356. ctx = (TrapContext*)read_comm_pipe_pvoid_blocking(ui->unit_pipe);
  6357. pck = read_comm_pipe_u32_blocking(ui->unit_pipe);
  6358. msg = read_comm_pipe_u32_blocking(ui->unit_pipe);
  6359. morelocks = (uae_u32)read_comm_pipe_int_blocking(ui->unit_pipe);
  6360. if (ui->reset_state == FS_GO_DOWN) {
  6361. trap_background_set_complete(ctx);
  6362. if (pck != 0)
  6363. return 1;
  6364. /* Death message received. */
  6365. uae_sem_post (&ui->reset_sync_sem);
  6366. /* Die. */
  6367. return 0;
  6368. }
  6369. dpacket packet;
  6370. readdpacket(ctx, &packet, pck);
  6371. int isvolume = 0;
  6372. #if TRAPMD
  6373. trapmd md[] = {
  6374. { TRAPCMD_GET_LONG, { morelocks }, 2, 0 },
  6375. { TRAPCMD_GET_LONG, { ui->self->locklist }, 2, 1 },
  6376. { TRAPCMD_PUT_LONG },
  6377. { TRAPCMD_PUT_LONG, { ui->self->locklist, morelocks }},
  6378. { ui->self->volume ? TRAPCMD_GET_BYTE : TRAPCMD_NOP, { ui->self->volume + 64 }},
  6379. };
  6380. trap_multi(ctx, md, sizeof md / sizeof(struct trapmd));
  6381. if (ui->self->volume) {
  6382. isvolume = md[4].params[0] || ui->self->ui.unknown_media;
  6383. }
  6384. #else
  6385. trap_put_long(ctx, trap_get_long(ctx, morelocks), trap_get_long(ctx, ui->self->locklist));
  6386. trap_put_long(ctx, ui->self->locklist, morelocks);
  6387. if (ui->self->volume) {
  6388. isvolume = trap_get_byte(ctx, ui->self->volume + 64) || ui->self->ui.unknown_media;
  6389. }
  6390. #endif
  6391. int ret = handle_packet(ctx, ui->self, &packet, msg, isvolume);
  6392. if (!ret) {
  6393. PUT_PCK_RES1 (&packet, DOS_FALSE);
  6394. PUT_PCK_RES2 (&packet, ERROR_ACTION_NOT_KNOWN);
  6395. }
  6396. writedpacket(ctx, &packet);
  6397. trapmd md2[] = {
  6398. { TRAPCMD_PUT_LONG, { msg + 4, 0xffffffff } },
  6399. { TRAPCMD_GET_LONG, { ui->self->locklist } },
  6400. { TRAPCMD_PUT_LONG, { ui->self->locklist, 0 } }
  6401. };
  6402. struct trapmd *mdp;
  6403. int mdcnt;
  6404. if (ret >= 0) {
  6405. mdp = &md2[0];
  6406. mdcnt = 3;
  6407. /* Mark the packet as processed for the list scan in the assembly code. */
  6408. //trap_put_long(ctx, msg + 4, 0xffffffff);
  6409. } else {
  6410. mdp = &md2[1];
  6411. mdcnt = 2;
  6412. }
  6413. /* Acquire the message lock, so that we know we can safely send the message. */
  6414. ui->self->cmds_sent++;
  6415. /* Send back the locks. */
  6416. trap_multi(ctx, mdp, mdcnt);
  6417. if (md2[1].params[0] != 0)
  6418. write_comm_pipe_int(ui->back_pipe, (int)md2[1].params[0], 0);
  6419. /* The message is sent by our interrupt handler, so make sure an interrupt happens. */
  6420. do_uae_int_requested();
  6421. #if 0
  6422. uae_u32 v = trap_get_long(ctx, ui->self->locklist);
  6423. if (v != 0)
  6424. write_comm_pipe_int (ui->back_pipe, (int)v, 0);
  6425. trap_put_long(ctx, ui->self->locklist, 0);
  6426. #endif
  6427. trap_background_set_complete(ctx);
  6428. return 1;
  6429. }
  6430. static void filesys_thread (void *unit_v)
  6431. {
  6432. UnitInfo *ui = (UnitInfo *)unit_v;
  6433. uae_set_thread_priority (NULL, 1);
  6434. for (;;) {
  6435. if (!filesys_iteration(ui)) {
  6436. return;
  6437. }
  6438. }
  6439. }
  6440. #endif
  6441. /* Talk about spaghetti code... */
  6442. static uae_u32 REGPARAM2 filesys_handler(TrapContext *ctx)
  6443. {
  6444. bool packet_valid = false;
  6445. Unit *unit = find_unit(trap_get_areg(ctx, 5));
  6446. uaecptr packet_addr = trap_get_dreg(ctx, 3);
  6447. uaecptr message_addr = trap_get_areg(ctx, 4);
  6448. if (!trap_valid_address(ctx, packet_addr, 36) || !trap_valid_address(ctx, message_addr, 14)) {
  6449. write_log (_T("FILESYS: Bad address %x/%x passed for packet.\n"), packet_addr, message_addr);
  6450. goto error2;
  6451. }
  6452. if (!unit || !unit->volume) {
  6453. write_log (_T("FILESYS: was not initialized.\n"));
  6454. goto error;
  6455. }
  6456. #ifdef UAE_FILESYS_THREADS
  6457. {
  6458. uae_u32 morelocks;
  6459. if (!unit->ui.unit_pipe)
  6460. goto error;
  6461. /* Get two more locks and hand them over to the other thread. */
  6462. #if TRAPMD
  6463. struct trapmd md[] = {
  6464. // morelocks = trap_get_long(ctx, trap_get_areg(ctx, 3));
  6465. /* 0 */ { TRAPCMD_GET_LONG, { trap_get_areg(ctx, 3) }, 1, 0 },
  6466. // morelocksptr = trap_get_long(ctx, morelocks)
  6467. /* 1 */ { TRAPCMD_GET_LONG, { 0 } },
  6468. // result 1 to index 4
  6469. /* 2 */ { TRAPCMD_NOP, { 0 }, 4, 0 },
  6470. // result 1 to index 6
  6471. /* 3 */ { TRAPCMD_NOP, { 0 }, 6, 0 },
  6472. // trap_get_long(ctx, morelocksptr)
  6473. /* 4 */ { TRAPCMD_GET_LONG, { 0 }, 5, 1 },
  6474. // trap_put_long(ctx, trap_get_areg(ctx, 3), result 4
  6475. /* 5 */ { TRAPCMD_PUT_LONG, { trap_get_areg(ctx, 3) } },
  6476. // trap_put_long(ctx, morelocksptr, 0);
  6477. /* 6 */ { TRAPCMD_PUT_LONG, { 0, 0 } },
  6478. // trap_put_long(ctx, message_addr + 4, 0);
  6479. /* 7 */ { TRAPCMD_PUT_LONG, { message_addr + 4, 0 } }
  6480. };
  6481. trap_multi(ctx, md, sizeof md / sizeof(struct trapmd));
  6482. morelocks = md[0].params[0];
  6483. #else
  6484. uae_u32 morelocksptr;
  6485. morelocks = trap_get_long(ctx, trap_get_areg(ctx, 3));
  6486. morelocksptr = trap_get_long(ctx, morelocks);
  6487. trap_put_long(ctx, trap_get_areg(ctx, 3), trap_get_long(ctx, morelocksptr));
  6488. trap_put_long(ctx, morelocksptr, 0);
  6489. /* The packet wasn't processed yet. */
  6490. trap_put_long(ctx, message_addr + 4, 0);
  6491. #endif
  6492. trap_set_background(ctx);
  6493. write_comm_pipe_pvoid(unit->ui.unit_pipe, ctx, 0);
  6494. write_comm_pipe_u32(unit->ui.unit_pipe, packet_addr, 0);
  6495. write_comm_pipe_u32(unit->ui.unit_pipe, message_addr, 0);
  6496. write_comm_pipe_int(unit->ui.unit_pipe, (int)morelocks, 1);
  6497. /* Don't reply yet. */
  6498. return 1;
  6499. }
  6500. #endif
  6501. dpacket packet;
  6502. readdpacket(ctx, &packet, packet_addr);
  6503. packet_valid = true;
  6504. if (! handle_packet(ctx, unit, &packet, 0, filesys_isvolume(unit))) {
  6505. error:
  6506. if (!packet_valid)
  6507. readdpacket(ctx, &packet, packet_addr);
  6508. PUT_PCK_RES1 (&packet, DOS_FALSE);
  6509. PUT_PCK_RES2 (&packet, ERROR_ACTION_NOT_KNOWN);
  6510. }
  6511. TRACE((_T("reply: %8x, %d\n"), GET_PCK_RES1(&packet), GET_PCK_RES2(&packet)));
  6512. writedpacket(ctx, &packet);
  6513. error2:
  6514. trap_put_long(ctx, message_addr + 4, 0xffffffff);
  6515. return 0;
  6516. }
  6517. void filesys_start_threads (void)
  6518. {
  6519. int i;
  6520. filesys_in_interrupt = 0;
  6521. for (i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  6522. UnitInfo *ui = &mountinfo.ui[i];
  6523. if (ui->open <= 0)
  6524. continue;
  6525. filesys_start_thread (ui, i);
  6526. }
  6527. }
  6528. void filesys_free_handles (void)
  6529. {
  6530. Unit *u, *u1;
  6531. for (u = units; u; u = u1) {
  6532. Key *k1, *knext;
  6533. u1 = u->next;
  6534. for (k1 = u->keys; k1; k1 = knext) {
  6535. knext = k1->next;
  6536. if (k1->fd)
  6537. fs_closefile (k1->fd);
  6538. xfree (k1);
  6539. }
  6540. u->keys = NULL;
  6541. struct lockrecord *lrnext;
  6542. for (struct lockrecord *lr = u->waitingrecords; lr; lr = lrnext) {
  6543. lrnext = lr->next;
  6544. xfree (lr);
  6545. }
  6546. u->waitingrecords = NULL;
  6547. free_all_ainos (u, &u->rootnode);
  6548. u->rootnode.next = u->rootnode.prev = &u->rootnode;
  6549. u->aino_cache_size = 0;
  6550. xfree (u->newrootdir);
  6551. xfree (u->newvolume);
  6552. u->newrootdir = NULL;
  6553. u->newvolume = NULL;
  6554. }
  6555. }
  6556. static void filesys_reset2 (void)
  6557. {
  6558. Unit *u, *u1;
  6559. filesys_free_handles ();
  6560. for (u = units; u; u = u1) {
  6561. u1 = u->next;
  6562. xfree (u);
  6563. }
  6564. units = 0;
  6565. key_uniq = 0;
  6566. a_uniq = 0;
  6567. free_mountinfo ();
  6568. }
  6569. void filesys_reset (void)
  6570. {
  6571. if (isrestore ())
  6572. return;
  6573. load_injected_icons();
  6574. filesys_reset2 ();
  6575. initialize_mountinfo ();
  6576. shell_execute_data = 0;
  6577. shell_execute_process = 0;
  6578. }
  6579. static void filesys_prepare_reset2 (void)
  6580. {
  6581. UnitInfo *uip;
  6582. int i;
  6583. uip = mountinfo.ui;
  6584. #ifdef UAE_FILESYS_THREADS
  6585. for (i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  6586. if (uip[i].open > 0 && uip[i].unit_pipe != 0) {
  6587. uae_sem_init (&uip[i].reset_sync_sem, 0, 0);
  6588. uip[i].reset_state = FS_GO_DOWN;
  6589. /* send death message */
  6590. write_comm_pipe_pvoid(uip[i].unit_pipe, NULL, 0);
  6591. write_comm_pipe_int(uip[i].unit_pipe, 0, 0);
  6592. write_comm_pipe_int(uip[i].unit_pipe, 0, 0);
  6593. write_comm_pipe_int(uip[i].unit_pipe, 0, 1);
  6594. uae_sem_wait (&uip[i].reset_sync_sem);
  6595. uae_end_thread (&uip[i].tid);
  6596. }
  6597. }
  6598. #endif
  6599. filesys_free_handles();
  6600. #if 0
  6601. Unit *u;
  6602. u = units;
  6603. while (u != 0) {
  6604. free_all_ainos (u, &u->rootnode);
  6605. u->rootnode.next = u->rootnode.prev = &u->rootnode;
  6606. u->aino_cache_size = 0;
  6607. u = u->next;
  6608. }
  6609. #endif
  6610. }
  6611. void filesys_prepare_reset (void)
  6612. {
  6613. if (isrestore ())
  6614. return;
  6615. filesys_prepare_reset2 ();
  6616. }
  6617. /* don't forget filesys.asm! */
  6618. #define PP_MAXSIZE 4 * 96
  6619. #define PP_FSSIZE 400
  6620. #define PP_FSPTR 404
  6621. #define PP_ADDTOFSRES 408
  6622. #define PP_FSRES 412
  6623. #define PP_FSRES_CREATED 416
  6624. #define PP_DEVICEPROC 420
  6625. #define PP_EXPLIB 424
  6626. #define PP_FSHDSTART 428
  6627. static int trackdisk_hack_state;
  6628. static int putmsg_hack_state;
  6629. static int putmsg_hack_filesystemtask;
  6630. static uae_u32 ks12hack_deviceproc;
  6631. static bool bcplonlydos(void)
  6632. {
  6633. return kickstart_version && kickstart_version < 33;
  6634. }
  6635. static const uae_u8 bootblock_ofs[] = {
  6636. 0x44,0x4f,0x53,0x00,0xc0,0x20,0x0f,0x19,0x00,0x00,0x03,0x70,0x43,0xfa,0x00,0x18,
  6637. 0x4e,0xae,0xff,0xa0,0x4a,0x80,0x67,0x0a,0x20,0x40,0x20,0x68,0x00,0x16,0x70,0x00,
  6638. 0x4e,0x75,0x70,0xff,0x60,0xfa,0x64,0x6f,0x73,0x2e,0x6c,0x69,0x62,0x72,0x61,0x72,
  6639. 0x79
  6640. };
  6641. static uae_u32 REGPARAM2 filesys_putmsg_return(TrapContext *ctx)
  6642. {
  6643. uaecptr message = trap_get_areg(ctx, 1);
  6644. uaecptr dospacket = trap_get_long(ctx, message + 10);
  6645. UnitInfo *uip = mountinfo.ui;
  6646. if (!ks12hack_deviceproc && uip[0].parmpacket)
  6647. ks12hack_deviceproc = trap_get_long(ctx, uip[0].parmpacket + PP_DEVICEPROC);
  6648. if (ks12hack_deviceproc) {
  6649. uae_u32 port = ks12hack_deviceproc;
  6650. if (port) {
  6651. uaecptr proc = trap_get_long(ctx, trap_get_long(ctx, 4) + 276); // ThisTask
  6652. trap_put_long(ctx, proc + 168, port); // pr_FileSystemTask
  6653. trap_set_areg(ctx, 0, port);
  6654. write_log(_T("Pre-KS 1.3 automount hack: patch boot handler process. DP=%08x Proc %08x pr_FileSystemTask=%08x.\n"), dospacket, proc, port);
  6655. }
  6656. }
  6657. return trap_get_dreg(ctx, 0);
  6658. }
  6659. static uae_u32 REGPARAM2 filesys_putmsg(TrapContext *ctx)
  6660. {
  6661. trap_set_areg(ctx, 7, trap_get_areg(ctx, 7) - 4);
  6662. trap_put_long(ctx, trap_get_areg(ctx, 7), trap_get_long(ctx, ROM_filesys_putmsg_original));
  6663. if (putmsg_hack_state) {
  6664. uaecptr message = trap_get_areg(ctx, 1);
  6665. uaecptr dospacket = trap_get_long(ctx, message + 10);
  6666. if (dospacket && !(dospacket & 3) && trap_valid_address(ctx, dospacket, 48)) {
  6667. int type = trap_get_long(ctx, dospacket + 8);
  6668. // write_log(_T("Port=%08x Msg=%08x DP=%08x dp_Link=%08x dp_Port=%08x dp_Type=%d\n"),
  6669. // m68k_areg(regs, 0), m68k_areg(regs, 1), dospacket, get_long(dospacket), get_long(dospacket + 4), type);
  6670. if (type == ACTION_LOCATE_OBJECT) {
  6671. write_log(_T("Pre-KS 1.3 automount hack: init drives.\n"));
  6672. putmsg_hack_state = 0;
  6673. if (putmsg_hack_filesystemtask) {
  6674. trap_set_areg(ctx, 7, trap_get_areg(ctx, 7) - 4);
  6675. trap_put_long(ctx, trap_get_areg(ctx, 7), ROM_filesys_putmsg_return);
  6676. }
  6677. trap_set_areg(ctx, 7, trap_get_areg(ctx, 7) - 4);
  6678. trap_put_long(ctx, trap_get_areg(ctx, 7), filesys_initcode);
  6679. trap_set_areg(ctx, 7, trap_get_areg(ctx, 7) - 4);
  6680. trap_put_long(ctx, trap_get_areg(ctx, 7), ROM_filesys_hack_remove);
  6681. return trap_get_dreg(ctx, 0);
  6682. }
  6683. }
  6684. }
  6685. return trap_get_dreg(ctx, 0);
  6686. }
  6687. static uae_u32 REGPARAM2 filesys_doio(TrapContext *ctx)
  6688. {
  6689. uaecptr ioreq = trap_get_areg(ctx, 1);
  6690. uaecptr unit = trap_get_long(ctx, ioreq + 24); // io_Unit
  6691. if (trackdisk_hack_state && unit && trap_valid_address(ctx, unit, 14)) {
  6692. uaecptr name = trap_get_long(ctx, unit + 10); // ln_Name
  6693. if (name && trap_valid_address(ctx, name, 20)) {
  6694. uae_u8 *addr = get_real_address(name);
  6695. if (!memcmp(addr, "trackdisk.device", 17)) {
  6696. int cmd = trap_get_word(ctx, ioreq + 28); // io_Command
  6697. uaecptr data = trap_get_long(ctx, ioreq + 40);
  6698. int len = trap_get_long(ctx, ioreq + 36);
  6699. //write_log(_T("%08x %d\n"), ioreq, cmd);
  6700. switch (cmd)
  6701. {
  6702. case 2: // CMD_READ
  6703. {
  6704. // trackdisk.device reading boot block
  6705. uae_u8 *d = get_real_address(data);
  6706. memset(d, 0, 1024);
  6707. memcpy(d, bootblock_ofs, sizeof bootblock_ofs);
  6708. trap_put_long(ctx, ioreq + 32, len); // io_Actual
  6709. trackdisk_hack_state = 0;
  6710. write_log(_T("Pre-KS 1.3 automount hack: DF0: boot block faked.\n"));
  6711. }
  6712. break;
  6713. case 9: // TD_MOTOR
  6714. trap_put_long(ctx, ioreq + 32, trackdisk_hack_state < 0 ? 0 : 1);
  6715. trackdisk_hack_state = len ? 1 : -1;
  6716. break;
  6717. case 13: // TD_CHANGENUM
  6718. trap_put_long(ctx, ioreq + 32, 1); // io_Actual
  6719. break;
  6720. case 14: // TD_CHANGESTATE
  6721. trap_put_long(ctx, ioreq + 32, 0);
  6722. break;
  6723. }
  6724. return 0;
  6725. }
  6726. }
  6727. }
  6728. trap_set_areg(ctx, 7, trap_get_areg(ctx, 7) - 4);
  6729. trap_put_long(ctx, trap_get_areg(ctx, 7), trap_get_long(ctx, ROM_filesys_doio_original));
  6730. return 0;
  6731. }
  6732. static uaecptr add_resident(TrapContext *ctx, uaecptr resaddr, uaecptr myres)
  6733. {
  6734. uaecptr sysbase, reslist, prevjmp, resptr;
  6735. uae_s8 myrespri = trap_get_byte(ctx, myres + 13); // rt_Pri
  6736. sysbase = trap_get_long(ctx, 4);
  6737. prevjmp = 0;
  6738. reslist = trap_get_long(ctx, sysbase + 300); // ResModules
  6739. for (;;) {
  6740. resptr = trap_get_long(ctx, reslist);
  6741. if (!resptr)
  6742. break;
  6743. if (resptr & 0x80000000) {
  6744. prevjmp = reslist;
  6745. reslist = resptr & 0x7fffffff;
  6746. continue;
  6747. }
  6748. uae_s8 respri = trap_get_byte(ctx, resptr + 13); // rt_Pri
  6749. uaecptr resname = trap_get_long(ctx, resptr + 14); // rt_Name
  6750. if (resname) {
  6751. uae_char resnamebuf[256];
  6752. trap_get_string(ctx, resnamebuf, resname, sizeof resnamebuf);
  6753. if (myrespri >= respri)
  6754. break;
  6755. }
  6756. prevjmp = 0;
  6757. reslist += 4;
  6758. }
  6759. if (prevjmp) {
  6760. trap_put_long(ctx, prevjmp, 0x80000000 | resaddr);
  6761. } else {
  6762. trap_put_long(ctx, reslist, 0x80000000 | resaddr);
  6763. }
  6764. trap_put_long(ctx, resaddr, myres);
  6765. trap_put_long(ctx, resaddr + 4, resptr);
  6766. trap_put_long(ctx, resaddr + 8, 0x80000000 | (reslist + 4));
  6767. resaddr += 3 * 4;
  6768. return resaddr;
  6769. }
  6770. static uae_u32 REGPARAM2 filesys_diagentry (TrapContext *ctx)
  6771. {
  6772. UnitInfo *uip = mountinfo.ui;
  6773. uaecptr resaddr = trap_get_areg(ctx, 2);
  6774. uaecptr expansion = trap_get_areg(ctx, 5);
  6775. uaecptr first_resident, last_resident, tmp;
  6776. uaecptr resaddr_hack = 0;
  6777. uae_u8 *baseaddr;
  6778. filesys_configdev = trap_get_areg(ctx, 3);
  6779. if (currprefs.uaeboard > 1) {
  6780. baseaddr = uaeboard_bank.baseaddr;
  6781. resaddr += 0x100;
  6782. } else {
  6783. baseaddr = filesys_bank.baseaddr;
  6784. resaddr += 0x10;
  6785. }
  6786. if (baseaddr) {
  6787. put_long_host(baseaddr + 0x2100, EXPANSION_explibname);
  6788. put_long_host(baseaddr + 0x2104, filesys_configdev);
  6789. put_long_host(baseaddr + 0x2108, EXPANSION_doslibname);
  6790. put_word_host(baseaddr + 0x210c, 0);
  6791. put_word_host(baseaddr + 0x210e, nr_units());
  6792. put_word_host(baseaddr + 0x2110, 0);
  6793. put_word_host(baseaddr + 0x2112, 1 | (currprefs.uae_hide_autoconfig || currprefs.uaeboard > 1 ? 16 : 0));
  6794. }
  6795. native2amiga_startup();
  6796. write_log (_T("filesystem: diagentry %08x configdev %08x\n"), resaddr, filesys_configdev);
  6797. first_resident = resaddr;
  6798. if (ROM_hardfile_resid != 0) {
  6799. /* Build a struct Resident. This will set up and initialize
  6800. * the uae.device */
  6801. trap_put_word(ctx, resaddr + 0x0, 0x4AFC);
  6802. trap_put_long(ctx, resaddr + 0x2, resaddr);
  6803. trap_put_long(ctx, resaddr + 0x6, resaddr + 0x1A); /* Continue scan here */
  6804. trap_put_word(ctx, resaddr + 0xA, 0x8132); /* RTF_AUTOINIT|RTF_COLDSTART; Version 50 */
  6805. trap_put_word(ctx, resaddr + 0xC, 0x0305); /* NT_DEVICE; pri 05 */
  6806. trap_put_long(ctx, resaddr + 0xE, ROM_hardfile_resname);
  6807. trap_put_long(ctx, resaddr + 0x12, ROM_hardfile_resid);
  6808. trap_put_long(ctx, resaddr + 0x16, ROM_hardfile_init); /* calls filesys_init */
  6809. }
  6810. resaddr += 0x1A;
  6811. if (!KS12_BOOT_HACK || expansion)
  6812. first_resident = resaddr;
  6813. /* The good thing about this function is that it always gets called
  6814. * when we boot. So we could put all sorts of stuff that wants to be done
  6815. * here.
  6816. * We can simply add more Resident structures here. Although the Amiga OS
  6817. * only knows about the one at address DiagArea + 0x10, we scan for other
  6818. * Resident structures and inject them to ResList in priority order
  6819. */
  6820. // KS 2.x RTF_AFTERDOS is broken
  6821. if (kickstart_version >= 39) {
  6822. trap_put_word(ctx, resaddr + 0x0, 0x4afc);
  6823. trap_put_long(ctx, resaddr + 0x2, resaddr);
  6824. trap_put_long(ctx, resaddr + 0x6, resaddr + 0x1A);
  6825. trap_put_word(ctx, resaddr + 0xA, 0x0432); /* RTF_AFTERDOS; Version 50 */
  6826. trap_put_word(ctx, resaddr + 0xC, 0x0000 | AFTERDOS_INIT_PRI); /* NT_UNKNOWN; pri */
  6827. trap_put_long(ctx, resaddr + 0xE, afterdos_name);
  6828. trap_put_long(ctx, resaddr + 0x12, afterdos_id);
  6829. trap_put_long(ctx, resaddr + 0x16, afterdos_initcode);
  6830. resaddr += 0x1A;
  6831. }
  6832. resaddr = uaeres_startup(ctx, resaddr);
  6833. #ifdef BSDSOCKET
  6834. resaddr = bsdlib_startup(ctx, resaddr);
  6835. #endif
  6836. #ifdef WITH_UAENATIVE
  6837. resaddr = uaenative_startup(ctx, resaddr);
  6838. #endif
  6839. #ifdef SCSIEMU
  6840. resaddr = scsidev_startup(ctx, resaddr);
  6841. #endif
  6842. #ifdef SANA2
  6843. resaddr = netdev_startup(ctx, resaddr);
  6844. #endif
  6845. #ifdef UAESERIAL
  6846. resaddr = uaeserialdev_startup(ctx, resaddr);
  6847. #endif
  6848. #ifdef WITH_TABLETLIBRARY
  6849. resaddr = tabletlib_startup(ctx, resaddr);
  6850. #endif
  6851. #ifdef RETROPLATFORM
  6852. if (rp_isactive()) {
  6853. trap_put_word(ctx, resaddr + 0x0, 0x4afc);
  6854. trap_put_long(ctx, resaddr + 0x2, resaddr);
  6855. trap_put_long(ctx, resaddr + 0x6, resaddr + 0x1A);
  6856. trap_put_word(ctx, resaddr + 0xA, 0x0100); /* RTF_COLDSTART; Version 0 */
  6857. trap_put_word(ctx, resaddr + 0xC, 0x0000); /* NT_UNKNOWN; pri */
  6858. trap_put_long(ctx, resaddr + 0xE, keymaphook_name);
  6859. trap_put_long(ctx, resaddr + 0x12, keymaphook_id);
  6860. trap_put_long(ctx, resaddr + 0x16, keymaphook_initcode);
  6861. resaddr += 0x1A;
  6862. }
  6863. #endif
  6864. last_resident = resaddr;
  6865. /* call setup_exter */
  6866. trap_put_word(ctx, resaddr + 0, 0x7000 | (currprefs.uaeboard > 1 ? 3 : 1)); /* moveq #x,d0 */
  6867. trap_put_word(ctx, resaddr + 2, 0x2079); /* move.l RTAREA_BASE+setup_exter,a0 */
  6868. trap_put_long(ctx, resaddr + 4, rtarea_base + bootrom_header + 4 + 5 * 4);
  6869. trap_put_word(ctx, resaddr + 8, 0xd1fc); /* add.l #RTAREA_BASE+bootrom_header,a0 */
  6870. trap_put_long(ctx, resaddr + 10, rtarea_base + bootrom_header);
  6871. trap_put_word(ctx, resaddr + 14, 0x4e90); /* jsr (a0) */
  6872. resaddr += 16;
  6873. trackdisk_hack_state = 0;
  6874. putmsg_hack_state = 0;
  6875. putmsg_hack_filesystemtask = 0;
  6876. ks12hack_deviceproc = 0;
  6877. if (KS12_BOOT_HACK && nr_units() && filesys_configdev == 0 && baseaddr) {
  6878. resaddr_hack = resaddr;
  6879. putmsg_hack_state = -1;
  6880. if (uip[0].bootpri > -128) {
  6881. resaddr += 2 * 22;
  6882. trackdisk_hack_state = -1;
  6883. putmsg_hack_filesystemtask = 1;
  6884. } else {
  6885. resaddr += 1 * 22;
  6886. }
  6887. }
  6888. trap_put_word(ctx, resaddr + 0, 0x7001); /* moveq #1,d0 */
  6889. trap_put_word(ctx, resaddr + 2, RTS);
  6890. resaddr += 4;
  6891. ROM_filesys_doio_original = resaddr;
  6892. ROM_filesys_putmsg_original = resaddr + 4;
  6893. resaddr += 8;
  6894. ROM_filesys_hack_remove = resaddr;
  6895. if (putmsg_hack_state) {
  6896. // remove patches
  6897. put_long(resaddr + 0, 0x48e7fffe); // movem.l d0-d7/a0-a6,-(sp)
  6898. put_word(resaddr + 4, 0x224e); // move.l a6,a1
  6899. resaddr += 6;
  6900. if (trackdisk_hack_state) {
  6901. put_word(resaddr + 0, 0x307c); // move.w #x,a0
  6902. put_word(resaddr + 2, -0x1c8);
  6903. put_word(resaddr + 4, 0x2039); // move.l x,d0
  6904. put_long(resaddr + 6, ROM_filesys_doio_original);
  6905. put_word(resaddr + 10, 0x4eae); // jsr x(a6)
  6906. put_word(resaddr + 12, -0x1a4);
  6907. resaddr += 14;
  6908. }
  6909. put_word(resaddr + 0, 0x307c); // move.w #x,a0
  6910. put_word(resaddr + 2, -0x16e);
  6911. put_word(resaddr + 4, 0x2039); // move.l x,d0
  6912. put_long(resaddr + 6, ROM_filesys_putmsg_original);
  6913. put_word(resaddr + 10, 0x4eae); // jsr x(a6)
  6914. put_word(resaddr + 12, -0x1a4);
  6915. resaddr += 14;
  6916. put_long(resaddr + 0, 0x4cdf7fff); // movem.l (sp)+,d0-d7/a0-a6
  6917. resaddr += 4;
  6918. uaecptr temp = here();
  6919. org(filesys_initcode_ptr);
  6920. dl(resaddr);
  6921. org(temp);
  6922. put_word(resaddr, 0x4e75); // rts
  6923. resaddr += 2;
  6924. uaecptr resaddr_tmp = resaddr;
  6925. resaddr = resaddr_hack;
  6926. if (trackdisk_hack_state) {
  6927. // Pre-KS 1.3 trackdisk.device boot block injection hack. Patch DoIO()
  6928. put_word(resaddr + 0, 0x224e); // move.l a6,a1
  6929. put_word(resaddr + 2, 0x307c); // move.w #x,a0
  6930. put_word(resaddr + 4, -0x1c8);
  6931. put_word(resaddr + 6, 0x203c); // move.l #x,d0
  6932. put_long(resaddr + 8, ROM_filesys_doio);
  6933. put_word(resaddr + 12, 0x4eae); // jsr x(a6)
  6934. put_word(resaddr + 14, -0x1a4);
  6935. put_word(resaddr + 16, 0x23c0); // move.l d0,x
  6936. put_long(resaddr + 18, ROM_filesys_doio_original);
  6937. resaddr += 22;
  6938. }
  6939. // Pre-KS 1.3 automount hack. Patch PutMsg()
  6940. put_word(resaddr + 0, 0x224e); // move.l a6,a1
  6941. put_word(resaddr + 2, 0x307c); // move.w #x,a0
  6942. put_word(resaddr + 4, -0x16e);
  6943. put_word(resaddr + 6, 0x203c); // move.l #x,d0
  6944. put_long(resaddr + 8, ROM_filesys_putmsg);
  6945. put_word(resaddr + 12, 0x4eae); // jsr x(a6)
  6946. put_word(resaddr + 14, -0x1a4);
  6947. put_word(resaddr + 16, 0x23c0); // move.l d0,x
  6948. put_long(resaddr + 18, ROM_filesys_putmsg_original);
  6949. resaddr += 22;
  6950. // filesys.asm make_dev D7
  6951. put_word_host(baseaddr + 0x2112, 1 | 2 | 8 | 16);
  6952. resaddr = resaddr_tmp;
  6953. } else {
  6954. // undo possible KS 1.2 hack
  6955. uaecptr temp = here();
  6956. org(filesys_initcode_ptr);
  6957. dl(filesys_initcode_real);
  6958. org(temp);
  6959. }
  6960. trap_set_areg(ctx, 0, last_resident);
  6961. if (currprefs.uae_hide_autoconfig && expansion) {
  6962. bool found = true;
  6963. while (found) {
  6964. uaecptr node = trap_get_long(ctx, expansion + 0x3c);
  6965. found = false;
  6966. while (trap_get_long(ctx, node)) {
  6967. if (trap_get_word(ctx, node + 0x10 + 4) == 2011) {
  6968. uae_u8 prod = trap_get_byte(ctx, node + 0x10 + 1);
  6969. if (prod != 2) {
  6970. // remove all 2011 boards except filesystem
  6971. found = true;
  6972. uaecptr succ = trap_get_long(ctx, node);
  6973. uaecptr pred = trap_get_long(ctx, node + 4);
  6974. trap_put_long(ctx, pred, succ);
  6975. trap_put_long(ctx, succ + 4, pred);
  6976. break;
  6977. }
  6978. // replace filesystem with A590/A2091 IDs..
  6979. trap_put_byte(ctx, node + 0x10 + 1, 3);
  6980. trap_put_word(ctx, node + 0x10 + 4, 514);
  6981. }
  6982. node = trap_get_long(ctx, node);
  6983. }
  6984. }
  6985. }
  6986. tmp = first_resident;
  6987. while (tmp < last_resident && tmp >= first_resident) {
  6988. if (trap_get_word(ctx, tmp) == 0x4AFC && trap_get_long(ctx, tmp + 0x2) == tmp) {
  6989. resaddr = add_resident(ctx, resaddr, tmp);
  6990. tmp = trap_get_long(ctx, tmp + 0x6);
  6991. } else {
  6992. tmp += 2;
  6993. }
  6994. }
  6995. return 1;
  6996. }
  6997. #define FS_TEST_HACK 0
  6998. #if FS_TEST_HACK
  6999. static uae_u32 REGPARAM2 ks13patchstartup(TrapContext *ctx)
  7000. {
  7001. uaecptr startup = trap_get_areg(ctx, 0);
  7002. uaecptr doslib = 0x00c06230;
  7003. uaecptr root = get_long(doslib + 34);
  7004. uaecptr node = (get_long(root + 24) << 2) + 4;
  7005. uaecptr dp_arg2 = get_long(startup + 24);
  7006. for (;;) {
  7007. node = get_long(node) << 2;
  7008. if (!node)
  7009. break;
  7010. if (get_long(node + 4) != 0)
  7011. continue;
  7012. if (get_long(node + 28) != dp_arg2)
  7013. continue;
  7014. put_long(startup + 20, get_long(node + 40)); // dp_Arg1 = dol_Node
  7015. put_long(startup + 28, node >> 2); // dp_Arg3 = dl
  7016. activate_debugger();
  7017. return dp_arg2 << 2;
  7018. }
  7019. return 0;
  7020. }
  7021. static void patchhackfs(uaecptr seg)
  7022. {
  7023. int o = rtarea_base + 0xFEFA;
  7024. put_long(seg + 9806, 0x70ff70ff);
  7025. seg += 9632;
  7026. put_word(seg, 0x4eb9);
  7027. put_long(seg + 2, o);
  7028. put_word(seg + 6, 0x2440); // move.l d0,a2
  7029. org(o);
  7030. calltrap(deftrap2(ks13patchstartup, 0, _T("ks13patchstartup")));
  7031. dw(RTS);
  7032. }
  7033. #endif
  7034. static uae_u32 REGPARAM2 filesys_dev_bootfilesys (TrapContext *ctx)
  7035. {
  7036. uaecptr devicenode = trap_get_areg(ctx, 3);
  7037. uaecptr parmpacket = trap_get_areg(ctx, 1);
  7038. uaecptr fsres = trap_get_long(ctx, parmpacket + PP_FSRES);
  7039. uaecptr fsnode;
  7040. uae_u32 dostype, dostype2;
  7041. int no = trap_get_dreg(ctx, 6) & 0x7fffffff;
  7042. int unit_no = no & 65535;
  7043. UnitInfo *uip = &mountinfo.ui[unit_no];
  7044. int iscd = (trap_get_dreg(ctx, 6) & 0x80000000) != 0 || uip->unit_type == UNIT_CDFS;
  7045. int type;
  7046. if (iscd) {
  7047. if (!trap_get_long(ctx, devicenode + 16))
  7048. trap_put_long(ctx, devicenode + 16, cdfs_handlername);
  7049. return 0;
  7050. } else {
  7051. type = is_hardfile (unit_no);
  7052. }
  7053. if (type == FILESYS_VIRTUAL) {
  7054. if (!trap_get_long(ctx, devicenode + 16))
  7055. trap_put_long(ctx, devicenode + 16, fshandlername);
  7056. return 0;
  7057. }
  7058. dostype = trap_get_long(ctx, parmpacket + 80);
  7059. if (trap_get_long(ctx, parmpacket + PP_FSPTR) && !trap_get_long(ctx, parmpacket + PP_ADDTOFSRES)) {
  7060. uaecptr fsptr = trap_get_long(ctx, parmpacket + PP_FSPTR);
  7061. uip->filesysseg = fsptr;
  7062. // filesystem but was not added to fs.resource
  7063. uae_u32 pf = trap_get_long(ctx, parmpacket + PP_FSHDSTART + 8); // fse_PatchFlags
  7064. for (int i = 0; i < 32; i++) {
  7065. if (pf & (1 << i))
  7066. trap_put_long(ctx, devicenode + 4 + i * 4, trap_get_long(ctx, parmpacket + PP_FSHDSTART + 8 + 4 + i * 4));
  7067. }
  7068. uaecptr seglist = fsptr >> 2;
  7069. if (bcplonlydos()) {
  7070. trap_put_long(ctx, devicenode + 4 + 3 * 4, seglist);
  7071. seglist = (trap_get_long(ctx, rtarea_base + bootrom_header + 4 + 6 * 4) + rtarea_base + bootrom_header) >> 2;
  7072. }
  7073. trap_put_long(ctx, devicenode + 4 + 7 * 4, seglist);
  7074. write_log(_T("Filesys %08x entry=%08x\n"), dostype, fsptr);
  7075. return 1;
  7076. }
  7077. fsnode = trap_get_long(ctx, fsres + 18);
  7078. while (trap_get_long(ctx, fsnode)) {
  7079. dostype2 = trap_get_long(ctx, fsnode + 14);
  7080. if (dostype2 == dostype) {
  7081. uae_u32 pf = trap_get_long(ctx, fsnode + 22); // fse_PatchFlags
  7082. for (int i = 0; i < 32; i++) {
  7083. if (pf & (1 << i)) {
  7084. uae_u32 data = trap_get_long(ctx, fsnode + 22 + 4 + i * 4);
  7085. if (i == 7) {
  7086. if (bcplonlydos()) { // seglist
  7087. // point seglist to bcpl wrapper and put original seglist in dn_Handler
  7088. trap_put_long(ctx, devicenode + 4 + 3 * 4, trap_get_long(ctx, fsnode + 22 + 4 + 7 * 4));
  7089. data = (trap_get_long(ctx, rtarea_base + bootrom_header + 4 + 6 * 4) + rtarea_base + bootrom_header) >> 2;
  7090. }
  7091. write_log(_T("Filesys %08x entry=%08x\n"), dostype, data << 2);
  7092. #if FS_TEST_HACK
  7093. patchhackfs(data << 2);
  7094. #endif
  7095. }
  7096. trap_put_long(ctx, devicenode + 4 + i * 4, data);
  7097. }
  7098. }
  7099. return 1;
  7100. }
  7101. fsnode = trap_get_long(ctx, fsnode);
  7102. }
  7103. if (type == FILESYS_HARDFILE) {
  7104. uae_u32 pf = trap_get_long(ctx, parmpacket + PP_FSHDSTART + 8); // fse_PatchFlags
  7105. for (int i = 0; i < 32; i++) {
  7106. if (pf & (1 << i))
  7107. trap_put_long(ctx, devicenode + 4 + i * 4, trap_get_long(ctx, parmpacket + PP_FSHDSTART + 8 + 4 + i * 4));
  7108. }
  7109. trap_put_long(ctx, devicenode + 4 + 7 * 4, 0); // seglist
  7110. }
  7111. uaecptr file_system_proc = trap_get_dreg(ctx, 1);
  7112. if (bcplonlydos() && file_system_proc && trap_get_long(ctx, devicenode + 4 + 7 * 4) == 0) {
  7113. // 1.1 or older, seglist == 0: get ROM OFS seglist from "File System" process.
  7114. // 1.2 and newer automatically use ROM OFS if seglist is zero.
  7115. // d1 = "File System" process pointer.
  7116. uaecptr p = trap_get_long(ctx, file_system_proc + 0x80) << 2; // pr_SegList
  7117. if (p) {
  7118. uae_u32 cnt = trap_get_long(ctx, p);
  7119. if (cnt > 0 && cnt < 16) {
  7120. uaecptr handlerseg = trap_get_long(ctx, p + cnt * 4);
  7121. write_log(_T("Pre-KS 1.2 handler segment %08x.\n"), handlerseg << 2);
  7122. trap_put_long(ctx, devicenode + 4 + 7 * 4, handlerseg);
  7123. }
  7124. }
  7125. }
  7126. return 0;
  7127. }
  7128. // called from bcplwrapper
  7129. static uae_u32 REGPARAM2 filesys_bcpl_wrapper(TrapContext *ctx)
  7130. {
  7131. const int patches[] = { 0x782, 0x7b8, 0x159c, 0x15b4, 0 };
  7132. uaecptr devicenode = trap_get_long(ctx, trap_get_dreg(ctx, 1) + 0x1c) << 2;
  7133. // fetch original seglist from dn_Handler
  7134. uaecptr seglist = trap_get_long(ctx, devicenode + 4 + 3 * 4) << 2;
  7135. uaecptr patchfunc = trap_get_areg(ctx, 1);
  7136. seglist += 4;
  7137. trap_set_areg(ctx, 0, seglist);
  7138. for (int i = 0; patches[i]; i++) {
  7139. int offset = patches[i];
  7140. if (get_long(seglist + offset + 2) != 0x4eaefd90) {
  7141. write_log(_T("FFS patch failed, comparison mismatch.\n"));
  7142. return 0;
  7143. }
  7144. }
  7145. for (int i = 0; patches[i]; i++) {
  7146. int offset = patches[i];
  7147. trap_put_word(ctx, seglist + offset, 0x4eb9);
  7148. trap_put_long(ctx, seglist + offset + 2, patchfunc);
  7149. patchfunc += 4;
  7150. }
  7151. uae_u16 ver = trap_get_word(ctx, trap_get_areg(ctx, 6) + 20);
  7152. if (ver < 31) {
  7153. // OpenLibrary -> OldOpenLibrary
  7154. trap_put_word(ctx, seglist + 0x7f4, -0x198);
  7155. trap_put_word(ctx, seglist + 0x2a6e, -0x198);
  7156. }
  7157. write_log(_T("FFS pre-1.2 patched\n"));
  7158. return 0;
  7159. }
  7160. static uae_u32 REGPARAM2 filesys_init_storeinfo (TrapContext *ctx)
  7161. {
  7162. int ret = -1;
  7163. switch (trap_get_dreg(ctx, 1))
  7164. {
  7165. case 1:
  7166. mountertask = trap_get_areg(ctx, 1);
  7167. #ifdef PICASSO96
  7168. picasso96_alloc(ctx);
  7169. #endif
  7170. break;
  7171. case 2: // new unit number query
  7172. ret = automountunit;
  7173. automountunit = -2;
  7174. break;
  7175. case 3:
  7176. return 0;
  7177. case 4: // mount done (d0=automountunit)
  7178. automountunit = -1;
  7179. return 0;
  7180. }
  7181. return ret;
  7182. }
  7183. /* Remember a pointer AmigaOS gave us so we can later use it to identify
  7184. * which unit a given startup message belongs to. */
  7185. static uae_u32 REGPARAM2 filesys_dev_remember (TrapContext *ctx)
  7186. {
  7187. int no = trap_get_dreg(ctx, 6) & 0x7fffffff;
  7188. int unit_no = no & 65535;
  7189. int sub_no = no >> 16;
  7190. UnitInfo *uip = &mountinfo.ui[unit_no];
  7191. int iscd = (trap_get_dreg(ctx, 6) & 0x80000000) != 0 || uip->unit_type == UNIT_CDFS;
  7192. uaecptr devicenode = trap_get_areg(ctx, 3);
  7193. uaecptr parmpacket = trap_get_areg(ctx, 1);
  7194. int fssize;
  7195. uae_u8 *fs;
  7196. uip->devicenode = devicenode;
  7197. fssize = uip->rdb_filesyssize;
  7198. fs = uip->rdb_filesysstore;
  7199. /* copy filesystem loaded from RDB */
  7200. if (trap_get_long(ctx, parmpacket + PP_FSPTR)) {
  7201. uaecptr addr = trap_get_long(ctx, parmpacket + PP_FSPTR);
  7202. trap_put_bytes(ctx, fs, addr, fssize);
  7203. // filesystem debugging, negative FSSIZE = debug mode.
  7204. if (segtrack_mode & 2) {
  7205. trap_put_long(ctx, parmpacket + PP_FSSIZE, -trap_get_long(ctx, parmpacket + PP_FSSIZE));
  7206. }
  7207. }
  7208. xfree (fs);
  7209. uip->rdb_filesysstore = 0;
  7210. uip->rdb_filesyssize = 0;
  7211. if (trap_get_dreg(ctx, 3) >= 0)
  7212. uip->startup = trap_get_long(ctx, devicenode + 28);
  7213. return devicenode;
  7214. }
  7215. static int legalrdbblock (UnitInfo *uip, int block)
  7216. {
  7217. if (block <= 0)
  7218. return 0;
  7219. if (block >= uip->hf.virtsize / uip->hf.ci.blocksize)
  7220. return 0;
  7221. return 1;
  7222. }
  7223. static uae_u32 rl (uae_u8 *p)
  7224. {
  7225. return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3]);
  7226. }
  7227. static uae_u16 rw (uae_u8 *p)
  7228. {
  7229. return (p[0] << 8) | (p[1]);
  7230. }
  7231. static int rdb_checksum (const uae_char *id, uae_u8 *p, int block)
  7232. {
  7233. uae_u32 sum = 0;
  7234. int i, blocksize;
  7235. if (memcmp (id, p, 4))
  7236. return 0;
  7237. blocksize = rl (p + 4);
  7238. if (blocksize < 1 || blocksize * 4 > FILESYS_MAX_BLOCKSIZE)
  7239. return 0;
  7240. for (i = 0; i < blocksize; i++)
  7241. sum += rl (p + i * 4);
  7242. sum = -sum;
  7243. if (sum) {
  7244. TCHAR *s = au (id);
  7245. write_log (_T("RDB: block %d ('%s') checksum error\n"), block, s);
  7246. xfree (s);
  7247. return 0;
  7248. }
  7249. return 1;
  7250. }
  7251. static int device_isdup (TrapContext *ctx, uaecptr expbase, TCHAR *devname)
  7252. {
  7253. uaecptr bnode, dnode, name;
  7254. int len, i;
  7255. TCHAR dname[256];
  7256. if (!expbase)
  7257. return 0;
  7258. bnode = trap_get_long(ctx, expbase + 74); /* expansion.library bootnode list */
  7259. while (trap_get_long(ctx, bnode)) {
  7260. dnode = trap_get_long(ctx, bnode + 16); /* device node */
  7261. name = trap_get_long(ctx, dnode + 40) << 2; /* device name BSTR */
  7262. len = trap_get_byte(ctx, name);
  7263. for (i = 0; i < len; i++)
  7264. dname[i] = trap_get_byte(ctx, name + 1 + i);
  7265. dname[len] = 0;
  7266. if (!_tcsicmp(devname, dname))
  7267. return 1;
  7268. bnode = trap_get_long(ctx, bnode);
  7269. }
  7270. return 0;
  7271. }
  7272. static TCHAR *device_dupfix (TrapContext *ctx, uaecptr expbase, TCHAR *devname)
  7273. {
  7274. int modified;
  7275. TCHAR newname[256];
  7276. _tcscpy (newname, devname);
  7277. modified = 1;
  7278. while (modified) {
  7279. modified = 0;
  7280. if (device_isdup (ctx, expbase, newname)) {
  7281. if (_tcslen (newname) > 2 && newname[_tcslen (newname) - 2] == '_') {
  7282. newname[_tcslen (newname) - 1]++;
  7283. } else {
  7284. _tcscat (newname, _T("_0"));
  7285. }
  7286. modified = 1;
  7287. }
  7288. }
  7289. return my_strdup (newname);
  7290. }
  7291. static const TCHAR *dostypes(TCHAR *dt, uae_u32 dostype)
  7292. {
  7293. int j;
  7294. j = 0;
  7295. for (int i = 0; i < 4; i++) {
  7296. uae_u8 c = dostype >> ((3 - i) * 8);
  7297. if (c >= ' ' && c <= 'z') {
  7298. dt[j++] = c;
  7299. } else {
  7300. dt[j++] = '\\';
  7301. _stprintf (&dt[j], _T("%d"), c);
  7302. j += _tcslen (&dt[j]);
  7303. }
  7304. }
  7305. dt[j] = 0;
  7306. return dt;
  7307. }
  7308. static void dump_partinfo (struct hardfiledata *hfd, uae_u8 *pp)
  7309. {
  7310. TCHAR *s;
  7311. uae_u32 dostype;
  7312. uae_u64 size;
  7313. int blocksize, surfaces, spb, spt, reserved;
  7314. int lowcyl, highcyl;
  7315. uae_u32 block, flags;
  7316. uae_u8 buf[512];
  7317. TCHAR dt[32];
  7318. flags = rl (pp + 20);
  7319. pp[37 + pp[36]] = 0;
  7320. s = au ((char*)pp + 37);
  7321. pp += 128;
  7322. dostype = rl (pp + 64);
  7323. spb = rl (pp + 16);
  7324. blocksize = rl (pp + 4) * 4;
  7325. surfaces = rl (pp + 12);
  7326. spt = rl (pp + 20);
  7327. reserved = rl (pp + 24);
  7328. lowcyl = rl (pp + 36);
  7329. highcyl = rl (pp + 40);
  7330. size = ((uae_u64)blocksize) * surfaces * spt * (highcyl - lowcyl + 1);
  7331. write_log (_T("Partition '%s' Dostype=%08X (%s) Flags: %08X\n"), s[0] ? s : _T("_NULL_"), dostype, dostypes (dt, dostype), flags);
  7332. write_log (_T("BlockSize: %d, Surfaces: %d, SectorsPerBlock %d\n"),
  7333. blocksize, surfaces, spb);
  7334. write_log (_T("SectorsPerTrack: %d, Reserved: %d, LowCyl %d, HighCyl %d, Size %dM\n"),
  7335. spt, reserved, lowcyl, highcyl, (uae_u32)(size >> 20));
  7336. write_log (_T("Buffers: %d, BufMemType: %08x, MaxTransfer: %08x, Mask: %08x, BootPri: %d\n"),
  7337. rl (pp + 44), rl (pp + 48), rl (pp + 52), rl (pp + 56), rl (pp + 60));
  7338. write_log (_T("Total blocks: %lld, Total disk blocks: %lld\n"), (uae_s64)surfaces * spt * (highcyl - lowcyl + 1), hfd->virtsize / blocksize);
  7339. if (hfd->drive_empty) {
  7340. write_log (_T("Empty drive\n"));
  7341. } else {
  7342. block = lowcyl * surfaces * spt;
  7343. if (hdf_read (hfd, buf, (uae_u64)blocksize * block, sizeof buf)) {
  7344. write_log (_T("First block %d dostype: %08X (%s)\n"), block, rl (buf), dostypes (dt, rl (buf)));
  7345. } else {
  7346. write_log (_T("First block %d read failed!\n"), block);
  7347. }
  7348. xfree (s);
  7349. if ((uae_u64)highcyl * spt * surfaces * blocksize > hfd->virtsize) {
  7350. write_log (_T("RDB: WARNING: end of partition > size of disk! (%llu > %llu)\n"),
  7351. (uae_u64)highcyl * spt * surfaces * blocksize, hfd->virtsize);
  7352. }
  7353. }
  7354. }
  7355. static void dumprdbblock(const uae_u8 *buf, int block)
  7356. {
  7357. int w = 16;
  7358. write_log(_T("RDB block %d:\n"), block);
  7359. for (int i = 0; i < 512; i += w) {
  7360. TCHAR outbuf[81];
  7361. for (int j = 0; j < w; j++) {
  7362. uae_u8 v = buf[i + j];
  7363. _stprintf(outbuf + 2 * j, _T("%02X"), v);
  7364. outbuf[2 * w + 1 + j] = (v >= 32 && v <= 126) ? v : '.';
  7365. }
  7366. outbuf[2 * w] = ' ';
  7367. outbuf[2 * w + 1 + w] = 0;
  7368. write_log(_T("%s\n"), outbuf);
  7369. }
  7370. }
  7371. static void dump_rdb (UnitInfo *uip, struct hardfiledata *hfd, uae_u8 *bufrdb, uae_u8 *buf, int readblocksize)
  7372. {
  7373. TCHAR dt[32];
  7374. write_log (_T("RDB: HostID: %08x Flags: %08x\n"),
  7375. rl (bufrdb + 3 * 4), rl (bufrdb + 5 * 4));
  7376. write_log (_T("RDB: BL: %d BH: %d LC: %d HC: %d CB: %d HB: %d\n"),
  7377. rl (bufrdb + 128), rl (bufrdb + 132), rl (bufrdb + 136), rl (bufrdb + 140), rl (bufrdb + 144), rl (bufrdb + 152));
  7378. for (int i = 0; i < 100; i++) {
  7379. int partblock;
  7380. if (i == 0)
  7381. partblock = rl (bufrdb + 28);
  7382. else
  7383. partblock = rl (buf + 4 * 4);
  7384. if (partblock == 0xffffffff)
  7385. break;
  7386. write_log (_T("RDB: PART block %d:\n"), partblock);
  7387. if (!legalrdbblock (uip, partblock)) {
  7388. write_log (_T("RDB: corrupt PART pointer %d\n"), partblock);
  7389. dumprdbblock(i == 0 ? bufrdb : buf, partblock);
  7390. break;
  7391. }
  7392. memset (buf, 0, readblocksize);
  7393. hdf_read (hfd, buf, partblock * hfd->ci.blocksize, readblocksize);
  7394. if (!rdb_checksum ("PART", buf, partblock)) {
  7395. write_log (_T("RDB: checksum error PART block %d\n"), partblock);
  7396. dumprdbblock(buf, partblock);
  7397. break;
  7398. }
  7399. dump_partinfo (hfd, buf);
  7400. }
  7401. for (int i = 0; i < 100; i++) {
  7402. int fileblock;
  7403. if (i == 0)
  7404. fileblock = rl (bufrdb + 32);
  7405. else
  7406. fileblock = rl (buf + 4 * 4);
  7407. if (fileblock == 0xffffffff)
  7408. break;
  7409. write_log (_T("RDB: LSEG block %d:\n"), fileblock);
  7410. if (!legalrdbblock (uip, fileblock)) {
  7411. write_log (_T("RDB: corrupt FSHD pointer %d\n"), fileblock);
  7412. dumprdbblock(i == 0 ? bufrdb : buf, fileblock);
  7413. break;
  7414. }
  7415. memset (buf, 0, readblocksize);
  7416. hdf_read (hfd, buf, fileblock * hfd->ci.blocksize, readblocksize);
  7417. if (!rdb_checksum ("FSHD", buf, fileblock)) {
  7418. write_log (_T("RDB: checksum error FSHD block %d\n"), fileblock);
  7419. dumprdbblock(buf, fileblock);
  7420. break;
  7421. }
  7422. uae_u32 dostype = rl (buf + 32);
  7423. int version = (buf[36] << 8) | buf[37];
  7424. int revision = (buf[38] << 8) | buf[39];
  7425. write_log (_T("LSEG: %08x (%s) %d.%d\n"), dostype, dostypes (dt, dostype), version, revision);
  7426. }
  7427. }
  7428. static void get_new_device (TrapContext *ctx, int type, uaecptr parmpacket, TCHAR **devname, uaecptr *devname_amiga, int unit_no)
  7429. {
  7430. TCHAR buffer[80];
  7431. uaecptr expbase = trap_get_long(ctx, parmpacket + PP_EXPLIB);
  7432. if (*devname == 0 || _tcslen (*devname) == 0) {
  7433. int un = unit_no;
  7434. for (;;) {
  7435. _stprintf (buffer, type == FILESYS_CD ? _T("CD%d") : _T("DH%d"), un++);
  7436. if (type == FILESYS_CD)
  7437. *devname = my_strdup (buffer);
  7438. if (!device_isdup(ctx, expbase, buffer))
  7439. break;
  7440. }
  7441. } else {
  7442. _tcscpy (buffer, *devname);
  7443. }
  7444. *devname_amiga = ds (device_dupfix(ctx, expbase, buffer));
  7445. if (type == FILESYS_CD)
  7446. write_log (_T("FS: mounted CD unit %s\n"), buffer);
  7447. else if (type == FILESYS_VIRTUAL)
  7448. write_log (_T("FS: mounted virtual unit %s (%s)\n"), buffer, mountinfo.ui[unit_no].rootdir);
  7449. else
  7450. write_log (_T("FS: mounted HDF unit %s (%04x-%08x, %s)\n"), buffer,
  7451. (uae_u32)(mountinfo.ui[unit_no].hf.virtsize >> 32),
  7452. (uae_u32)(mountinfo.ui[unit_no].hf.virtsize),
  7453. mountinfo.ui[unit_no].rootdir);
  7454. }
  7455. #define rdbmnt write_log (_T("Mounting uaehf.device %d (%d) (size=%llu):\n"), unit_no, partnum, hfd->virtsize)
  7456. static int pt_babe(TrapContext *ctx, uae_u8 *bufrdb, UnitInfo *uip, int unit_no, int partnum, uaecptr parmpacket)
  7457. {
  7458. uae_u8 bufrdb2[FILESYS_MAX_BLOCKSIZE] = { 0 };
  7459. struct hardfiledata *hfd = &uip->hf;
  7460. struct uaedev_config_info *ci = &uip[unit_no].hf.ci;
  7461. uae_u32 bad;
  7462. uae_u32 bs = (bufrdb[0x20] << 24) | (bufrdb[0x21] << 16) | (bufrdb[0x22] << 8) | (bufrdb[0x23] << 0);
  7463. int bscnt;
  7464. for (bscnt = 512; bscnt <= 32768; bscnt <<= 1) {
  7465. if (bs == (bscnt >> 2))
  7466. break;
  7467. }
  7468. if (bscnt > 32768)
  7469. return 0;
  7470. bad = rl(bufrdb + 4);
  7471. if (bad) {
  7472. if (bad * hfd->ci.blocksize > FILESYS_MAX_BLOCKSIZE)
  7473. return 0;
  7474. hdf_read_rdb(hfd, bufrdb2, bad * hfd->ci.blocksize, hfd->ci.blocksize);
  7475. if (bufrdb2[0] != 0xBA || bufrdb2[1] != 0xD1)
  7476. return 0;
  7477. }
  7478. if (partnum > 0)
  7479. return -2;
  7480. if (uip->bootpri <= -129) {
  7481. return -1;
  7482. }
  7483. if (bad) {
  7484. uae_u16 badnum = (bufrdb2[2] << 8) | bufrdb2[3];
  7485. if (badnum) {
  7486. write_log(_T("A2090 partition table: bad block table is not empty, bad blocks not yet supported!\n"));
  7487. }
  7488. }
  7489. hfd->rdbcylinders = rl(bufrdb + 0x08);
  7490. hfd->rdbsectors = rw(bufrdb + 0x18);
  7491. hfd->rdbheads = rw(bufrdb + 0x1a);
  7492. write_log (_T("Mounting uaehf.device:%d %d (%d):\n"), uip->hf.ci.controller_unit, unit_no, partnum);
  7493. get_new_device(ctx, FILESYS_HARDFILE_RDB, parmpacket, &uip->devname, &uip->rdb_devname_amiga[partnum], unit_no);
  7494. trap_put_long(ctx, parmpacket, uip->rdb_devname_amiga[partnum]);
  7495. trap_put_long(ctx, parmpacket + 4, ROM_hardfile_resname);
  7496. trap_put_long(ctx, parmpacket + 8, uip->devno);
  7497. trap_put_long(ctx, parmpacket + 12, 0); /* Device flags */
  7498. for (int i = 0; i < 12 * 4; i++)
  7499. trap_put_byte(ctx, parmpacket + 16 + i, bufrdb[0x1c + i]);
  7500. uae_u32 dostype = 0x444f5300;
  7501. if (ci->dostype) // forced dostype?
  7502. dostype = ci->dostype;
  7503. // A2090 Environment Table Size is only 11
  7504. int ts = trap_get_long(ctx, parmpacket + 16 + 0 * 4);
  7505. while (ts < 16) {
  7506. ts++;
  7507. if (ts == 12) // BufMem Type
  7508. trap_put_long(ctx, parmpacket + 16 + 12 * 4, 1);
  7509. if (ts == 13) // Max Transfer
  7510. trap_put_long(ctx, parmpacket + 16 + 13 * 4, 0x7fffffff);
  7511. if (ts == 14) // Mask
  7512. trap_put_long(ctx, parmpacket + 16 + 14 * 4, 0xfffffffe);
  7513. if (ts == 15) // BootPri
  7514. trap_put_long(ctx, parmpacket + 16 + 15 * 4, uip->bootpri);
  7515. if (ts == 16) // DosType
  7516. trap_put_long(ctx, parmpacket + 16 + 16 * 4, dostype);
  7517. }
  7518. trap_put_long(ctx, parmpacket + 16 + 0 * 4, ts);
  7519. uip->rdb_dostype = dostype;
  7520. if (uip->bootpri <= -128) /* not bootable */
  7521. trap_set_dreg(ctx, 7, trap_get_dreg(ctx, 7) & ~1);
  7522. return 2;
  7523. }
  7524. static int pt_rdsk (TrapContext *ctx, uae_u8 *bufrdb, int rdblock, UnitInfo *uip, int unit_no, int partnum, uaecptr parmpacket)
  7525. {
  7526. int blocksize, readblocksize, badblock, driveinitblock;
  7527. TCHAR dt[32];
  7528. uae_u8 *fsmem = 0, *buf = 0;
  7529. int partblock, fileblock, lsegblock;
  7530. uae_u32 flags;
  7531. struct hardfiledata *hfd = &uip->hf;
  7532. uae_u32 dostype;
  7533. uaecptr fsres, fsnode;
  7534. int err = 0;
  7535. int oldversion, oldrevision;
  7536. int newversion, newrevision;
  7537. TCHAR *s;
  7538. bool showdebug = partnum == 0;
  7539. int cnt;
  7540. blocksize = rl (bufrdb + 16);
  7541. readblocksize = blocksize > hfd->ci.blocksize ? blocksize : hfd->ci.blocksize;
  7542. badblock = rl (bufrdb + 24);
  7543. if (badblock != -1) {
  7544. write_log (_T("RDB: badblock list %08x\n"), badblock);
  7545. dumprdbblock(bufrdb, rdblock);
  7546. }
  7547. driveinitblock = rl (bufrdb + 36);
  7548. if (driveinitblock != -1) {
  7549. write_log (_T("RDB: driveinit = %08x\n"), driveinitblock);
  7550. dumprdbblock(bufrdb, rdblock);
  7551. }
  7552. hfd->rdbcylinders = rl (bufrdb + 64);
  7553. hfd->rdbsectors = rl (bufrdb + 68);
  7554. hfd->rdbheads = rl (bufrdb + 72);
  7555. #if 0
  7556. {
  7557. int cyls, secs, heads;
  7558. getchsgeometry_hdf (hfd, hfd->virtsize, &cyls, &secs, &heads);
  7559. if (cyls * secs * heads > hfd->cylinders * hfd->sectors * hfd->heads) {
  7560. hfd->cylinders = cyls;
  7561. hfd->sectors = secs;
  7562. hfd->heads = heads;
  7563. }
  7564. }
  7565. #endif
  7566. fileblock = rl (bufrdb + 32);
  7567. buf = xmalloc (uae_u8, readblocksize);
  7568. if (showdebug) {
  7569. if ((uae_u64)hfd->rdbcylinders * hfd->rdbsectors * hfd->rdbheads * blocksize > hfd->virtsize)
  7570. write_log (_T("RDB: WARNING: RDSK header disk size > disk size! (%llu > %llu)\n"),
  7571. (uae_u64)hfd->rdbcylinders * hfd->rdbsectors * hfd->rdbheads * blocksize, hfd->virtsize);
  7572. write_log (_T("RDSK dump start\n"));
  7573. write_log (_T("RDSK at %d, C=%d S=%d H=%d\n"),
  7574. rdblock, hfd->rdbcylinders, hfd->rdbsectors, hfd->rdbheads);
  7575. dump_rdb (uip, hfd, bufrdb, buf, readblocksize);
  7576. write_log (_T("RDSK dump end\n"));
  7577. }
  7578. for (int i = 0; i <= partnum; i++) {
  7579. if (i == 0)
  7580. partblock = rl (bufrdb + 28);
  7581. else
  7582. partblock = rl (buf + 4 * 4);
  7583. if (!legalrdbblock (uip, partblock)) {
  7584. err = -2;
  7585. goto error;
  7586. }
  7587. memset (buf, 0, readblocksize);
  7588. hdf_read (hfd, buf, partblock * hfd->ci.blocksize, readblocksize);
  7589. if (!rdb_checksum ("PART", buf, partblock)) {
  7590. err = -2;
  7591. write_log(_T("RDB: checksum error in PART block %d\n"), partblock);
  7592. dumprdbblock(buf, partblock);
  7593. goto error;
  7594. }
  7595. }
  7596. rdbmnt;
  7597. flags = rl (buf + 20);
  7598. if ((flags & 2) || uip->bootpri <= -129) { /* do not mount */
  7599. err = -1;
  7600. write_log (_T("RDB: Automount disabled, not mounting\n"));
  7601. goto error;
  7602. }
  7603. if (!(flags & 1) || uip->bootpri <= -128) /* not bootable */
  7604. trap_set_dreg(ctx, 7, trap_get_dreg(ctx, 7) & ~1);
  7605. buf[37 + buf[36]] = 0; /* zero terminate BSTR */
  7606. s = au ((char*)buf + 37);
  7607. uip->rdb_devname_amiga[partnum] = ds(device_dupfix(ctx, trap_get_long(ctx, parmpacket + PP_EXPLIB), s));
  7608. xfree (s);
  7609. trap_put_long(ctx, parmpacket, uip->rdb_devname_amiga[partnum]); /* name */
  7610. trap_put_long(ctx, parmpacket + 4, ROM_hardfile_resname);
  7611. trap_put_long(ctx, parmpacket + 8, uip->devno);
  7612. trap_put_long(ctx, parmpacket + 12, 0); /* Device flags */
  7613. for (int i = 0; i < (17 + 15) * 4; i++)
  7614. trap_put_byte(ctx, parmpacket + 16 + i, buf[128 + i]);
  7615. dostype = trap_get_long(ctx, parmpacket + 80);
  7616. uip->rdb_dostype = dostype;
  7617. if (dostype == 0) {
  7618. write_log (_T("RDB: mount failed, dostype=0\n"));
  7619. err = -1;
  7620. goto error;
  7621. }
  7622. if (dostype == 0xffffffff) {
  7623. write_log (_T("RDB: WARNING: dostype = 0xFFFFFFFF. FFS bug can report partition in \"no disk inserted\" state!!\n"));
  7624. }
  7625. err = 2;
  7626. /* load custom filesystems if needed */
  7627. if (fileblock == -1 || !legalrdbblock (uip, fileblock))
  7628. goto error;
  7629. fsres = trap_get_long(ctx, parmpacket + PP_FSRES);
  7630. if (!fsres) {
  7631. write_log (_T("RDB: FileSystem.resource not found, this shouldn't happen!\n"));
  7632. goto error;
  7633. }
  7634. fsnode = trap_get_long(ctx, fsres + 18);
  7635. while (trap_get_long(ctx, fsnode)) {
  7636. if (trap_get_long(ctx, fsnode + 14) == dostype)
  7637. break;
  7638. fsnode = trap_get_long(ctx, fsnode);
  7639. }
  7640. oldversion = oldrevision = -1;
  7641. if (trap_get_long(ctx, fsnode)) {
  7642. oldversion = trap_get_word(ctx, fsnode + 18);
  7643. oldrevision = trap_get_word(ctx, fsnode + 20);
  7644. } else {
  7645. fsnode = 0;
  7646. }
  7647. for (;;) {
  7648. if (fileblock == -1) {
  7649. if (!fsnode)
  7650. write_log (_T("RDB: FS %08X (%s) not in FileSystem.resource or in RDB\n"), dostype, dostypes (dt, dostype));
  7651. goto error;
  7652. }
  7653. if (!legalrdbblock (uip, fileblock)) {
  7654. write_log (_T("RDB: corrupt FSHD pointer %d\n"), fileblock);
  7655. goto error;
  7656. }
  7657. memset (buf, 0, readblocksize);
  7658. hdf_read (hfd, buf, fileblock * hfd->ci.blocksize, readblocksize);
  7659. if (!rdb_checksum ("FSHD", buf, fileblock)) {
  7660. write_log (_T("RDB: checksum error in FSHD block %d\n"), fileblock);
  7661. dumprdbblock(buf, fileblock);
  7662. goto error;
  7663. }
  7664. fileblock = rl (buf + 16);
  7665. uae_u32 rdbdostype = rl (buf + 32);
  7666. if (((dostype >> 8) == (rdbdostype >> 8) && (dostype != DISK_TYPE_DOS && (dostype & 0xffffff00) == DISK_TYPE_DOS)) || (dostype == rdbdostype))
  7667. break;
  7668. }
  7669. newversion = (buf[36] << 8) | buf[37];
  7670. newrevision = (buf[38] << 8) | buf[39];
  7671. write_log (_T("RDB: RDB filesystem %08X (%s) version %d.%d\n"), dostype, dostypes (dt, dostype), newversion, newrevision);
  7672. if (fsnode) {
  7673. write_log (_T("RDB: %08X (%s) in FileSystem.resource version %d.%d\n"), dostype, dostypes (dt, dostype), oldversion, oldrevision);
  7674. }
  7675. if (newversion * 65536 + newrevision <= oldversion * 65536 + oldrevision && oldversion >= 0) {
  7676. write_log (_T("RDB: FS in FileSystem.resource is newer or same, ignoring RDB filesystem\n"));
  7677. goto error;
  7678. }
  7679. for (int i = 0; i < 140; i++)
  7680. trap_put_byte(ctx, parmpacket + PP_FSHDSTART + i, buf[32 + i]);
  7681. trap_put_long(ctx, parmpacket + PP_FSHDSTART, dostype);
  7682. /* we found required FSHD block */
  7683. fsmem = xmalloc (uae_u8, 262144);
  7684. lsegblock = rl (buf + 72);
  7685. cnt = 0;
  7686. for (;;) {
  7687. int pb = lsegblock;
  7688. if (!legalrdbblock (uip, lsegblock))
  7689. goto error;
  7690. memset (buf, 0, readblocksize);
  7691. hdf_read (hfd, buf, lsegblock * hfd->ci.blocksize, readblocksize);
  7692. if (!rdb_checksum("LSEG", buf, lsegblock)) {
  7693. write_log(_T("RDB: checksum error in LSEG block %d\n"), lsegblock);
  7694. dumprdbblock(buf, lsegblock);
  7695. goto error;
  7696. }
  7697. lsegblock = rl (buf + 16);
  7698. if (lsegblock == pb)
  7699. goto error;
  7700. if ((cnt + 1) * (blocksize - 20) >= 262144)
  7701. goto error;
  7702. memcpy (fsmem + cnt * (blocksize - 20), buf + 20, blocksize - 20);
  7703. cnt++;
  7704. if (lsegblock == -1)
  7705. break;
  7706. }
  7707. write_log (_T("RDB: Filesystem loaded, %d bytes\n"), cnt * (blocksize - 20));
  7708. trap_put_long(ctx, parmpacket + PP_FSSIZE, cnt * (blocksize - 20)); /* RDB filesystem size hack */
  7709. trap_put_long(ctx, parmpacket + PP_ADDTOFSRES, -1);
  7710. uip->rdb_filesysstore = fsmem;
  7711. uip->rdb_filesyssize = cnt * (blocksize - 20);
  7712. xfree (buf);
  7713. return 2;
  7714. error:
  7715. xfree (buf);
  7716. xfree (fsmem);
  7717. return err;
  7718. }
  7719. static int rdb_mount (TrapContext *ctx, UnitInfo *uip, int unit_no, int partnum, uaecptr parmpacket)
  7720. {
  7721. struct hardfiledata *hfd = &uip->hf;
  7722. int lastblock = 63, rdblock;
  7723. uae_u8 bufrdb[FILESYS_MAX_BLOCKSIZE];
  7724. write_log (_T("%s:\n"), uip->rootdir);
  7725. if (hfd->drive_empty) {
  7726. rdbmnt;
  7727. write_log (_T("ignored, drive is empty\n"));
  7728. return -2;
  7729. }
  7730. if (hfd->ci.blocksize == 0) {
  7731. rdbmnt;
  7732. write_log (_T("failed, blocksize == 0\n"));
  7733. return -1;
  7734. }
  7735. if (lastblock * hfd->ci.blocksize > hfd->virtsize) {
  7736. rdbmnt;
  7737. write_log (_T("failed, too small (%d*%d > %llu)\n"), lastblock, hfd->ci.blocksize, hfd->virtsize);
  7738. return -2;
  7739. }
  7740. if (hfd->ci.blocksize > FILESYS_MAX_BLOCKSIZE) {
  7741. write_log(_T("failed, too large block size %d\n"), hfd->ci.blocksize);
  7742. return -2;
  7743. }
  7744. for (rdblock = 0; rdblock < lastblock; rdblock++) {
  7745. hdf_read_rdb (hfd, bufrdb, rdblock * hfd->ci.blocksize, hfd->ci.blocksize);
  7746. if (rdblock == 0 && bufrdb[0] == 0xBA && bufrdb[1] == 0xBE) {
  7747. // A2090 "BABE" partition table?
  7748. int v = pt_babe(ctx, bufrdb, uip, unit_no, partnum, parmpacket);
  7749. if (v)
  7750. return v;
  7751. continue;
  7752. }
  7753. if (rdb_checksum ("RDSK", bufrdb, rdblock) || rdb_checksum ("CDSK", bufrdb, rdblock)) {
  7754. return pt_rdsk(ctx, bufrdb, rdblock, uip, unit_no, partnum, parmpacket);
  7755. }
  7756. if (!memcmp ("RDSK", bufrdb, 4)) {
  7757. bufrdb[0xdc] = 0;
  7758. bufrdb[0xdd] = 0;
  7759. bufrdb[0xde] = 0;
  7760. bufrdb[0xdf] = 0;
  7761. if (rdb_checksum ("RDSK", bufrdb, rdblock)) {
  7762. write_log (_T("Windows 95/98/ME trashed RDB detected, fixing..\n"));
  7763. hdf_write (hfd, bufrdb, rdblock * hfd->ci.blocksize, hfd->ci.blocksize);
  7764. return pt_rdsk(ctx, bufrdb, rdblock, uip, unit_no, partnum, parmpacket);
  7765. }
  7766. }
  7767. }
  7768. rdbmnt;
  7769. write_log (_T("failed, no supported partition tables detected\n"));
  7770. return -2;
  7771. }
  7772. static void addfakefilesys (TrapContext *ctx, uaecptr parmpacket, uae_u32 dostype, int ver, int rev, struct uaedev_config_info *ci)
  7773. {
  7774. uae_u32 flags;
  7775. flags = 0x180;
  7776. for (int i = 0; i < 140; i++)
  7777. trap_put_byte(ctx, parmpacket + PP_FSHDSTART + i, 0);
  7778. if (dostype) {
  7779. trap_put_long(ctx, parmpacket + 80, dostype);
  7780. trap_put_long(ctx, parmpacket + PP_FSHDSTART, dostype);
  7781. }
  7782. if (ver >= 0 && rev >= 0)
  7783. trap_put_long(ctx, parmpacket + PP_FSHDSTART + 4, (ver << 16) | rev);
  7784. trap_put_long(ctx, parmpacket + PP_FSHDSTART + 12 + 4 * 4, ci->stacksize);
  7785. flags |= 0x10;
  7786. if (ci->bootpri != -129) {
  7787. trap_put_long(ctx, parmpacket + PP_FSHDSTART + 12 + 5 * 4, ci->bootpri);
  7788. flags |= 0x20;
  7789. }
  7790. trap_put_long(ctx, parmpacket + PP_FSHDSTART + 12 + 8 * 4, dostype == DISK_TYPE_DOS || bcplonlydos() ? 0 : -1); // globvec
  7791. // if OFS = seglist -> NULL
  7792. if (dostype == DISK_TYPE_DOS)
  7793. flags &= ~0x080;
  7794. trap_put_long(ctx, parmpacket + PP_FSHDSTART + 8, flags); // patchflags
  7795. }
  7796. static uaecptr getfakefilesysseg (UnitInfo *uip)
  7797. {
  7798. if (uip->filesysdir && _tcslen (uip->filesysdir) > 0) {
  7799. for (int i = 0; &mountinfo.ui[i] != uip; i++) {
  7800. UnitInfo *uip2 = &mountinfo.ui[i];
  7801. if (!uip2->filesysdir)
  7802. continue;
  7803. if (_tcsicmp (uip2->filesysdir, uip->filesysdir) != 0)
  7804. continue;
  7805. if (uip2->filesysseg)
  7806. return uip2->filesysseg;
  7807. }
  7808. }
  7809. return 0;
  7810. }
  7811. static int dofakefilesys (TrapContext *ctx, UnitInfo *uip, uaecptr parmpacket, struct uaedev_config_info *ci)
  7812. {
  7813. int size;
  7814. TCHAR tmp[MAX_DPATH];
  7815. TCHAR dt[32];
  7816. uae_u8 buf[512];
  7817. struct zfile *zf;
  7818. int ver = -1, rev = -1;
  7819. uae_u32 dostype;
  7820. bool autofs = false;
  7821. // we already have custom filesystem loaded for earlier hardfile?
  7822. if (!ci->forceload) {
  7823. uaecptr seg = getfakefilesysseg (uip);
  7824. if (seg) {
  7825. // yes, re-use it.
  7826. trap_put_long(ctx, parmpacket + PP_FSSIZE, 0);
  7827. trap_put_long(ctx, parmpacket + PP_FSPTR, seg);
  7828. trap_put_long(ctx, parmpacket + PP_ADDTOFSRES, 0);
  7829. write_log (_T("RDB: faked RDB filesystem '%s' reused, entry=%08x\n"), uip->filesysdir, seg << 2);
  7830. return FILESYS_HARDFILE;
  7831. }
  7832. }
  7833. if (!ci->dostype) {
  7834. memset (buf, 0, 4);
  7835. hdf_read (&uip->hf, buf, 0, 512);
  7836. dostype = (buf[0] << 24) | (buf[1] << 16) |(buf[2] << 8) | buf[3];
  7837. } else {
  7838. dostype = ci->dostype;
  7839. }
  7840. if (dostype == 0) {
  7841. addfakefilesys(ctx, parmpacket, dostype, ver, rev, ci);
  7842. return FILESYS_HARDFILE;
  7843. }
  7844. if (dostype == DISK_TYPE_DOS && (!uip->filesysdir || !uip->filesysdir[0])) {
  7845. write_log (_T("RDB: OFS, using ROM default FS.\n"));
  7846. return FILESYS_HARDFILE;
  7847. }
  7848. tmp[0] = 0;
  7849. if (uip->filesysdir && _tcslen (uip->filesysdir) > 0) {
  7850. cfgfile_resolve_path_out_load(uip->filesysdir, tmp, MAX_DPATH, PATH_HDF);
  7851. } else if ((dostype & 0xffffff00) == DISK_TYPE_DOS) {
  7852. _tcscpy (tmp, currprefs.romfile);
  7853. int i = _tcslen (tmp);
  7854. while (i > 0 && tmp[i - 1] != '/' && tmp[i - 1] != '\\')
  7855. i--;
  7856. _tcscpy (tmp + i, _T("FastFileSystem"));
  7857. autofs = true;
  7858. }
  7859. if (tmp[0] == 0) {
  7860. write_log (_T("RDB: no filesystem for dostype 0x%08X (%s)\n"), dostype, dostypes (dt, dostype));
  7861. addfakefilesys(ctx, parmpacket, dostype, ver, rev, ci);
  7862. if ((dostype & 0xffffff00) == DISK_TYPE_DOS)
  7863. return FILESYS_HARDFILE;
  7864. write_log (_T("RDB: mounted without filesys\n"));
  7865. return FILESYS_HARDFILE;
  7866. }
  7867. write_log (_T("RDB: fakefilesys, trying to load '%s', dostype 0x%08X (%s)\n"), tmp, dostype, dostypes (dt, dostype));
  7868. zf = zfile_fopen (tmp, _T("rb"), ZFD_NORMAL);
  7869. if (!zf) {
  7870. addfakefilesys(ctx, parmpacket, dostype, ver, rev, ci);
  7871. write_log (_T("RDB: filesys not found, mounted without forced filesys\n"));
  7872. return FILESYS_HARDFILE;
  7873. }
  7874. uae_u32 fsres, fsnode;
  7875. int oldversion = -1;
  7876. int oldrevision = -1;
  7877. fsres = trap_get_long(ctx, parmpacket + PP_FSRES);
  7878. fsnode = trap_get_long(ctx, fsres + 18);
  7879. while (trap_get_long(ctx, fsnode)) {
  7880. uae_u32 fsdostype = trap_get_long(ctx, fsnode + 14);
  7881. if (fsdostype == dostype) {
  7882. oldversion = trap_get_word(ctx, fsnode + 18);
  7883. oldrevision = trap_get_word(ctx, fsnode + 20);
  7884. write_log (_T("RDB: %08X (%s) in FileSystem.resource version %d.%d\n"), dostype, dostypes(dt, dostype), oldversion, oldrevision);
  7885. break;
  7886. }
  7887. fsnode = trap_get_long(ctx, fsnode);
  7888. }
  7889. // if automatically found FastFileSystem, do not replace matching FileSystem.resource FS
  7890. if (autofs && oldversion >= 0) {
  7891. zfile_fclose (zf);
  7892. addfakefilesys(ctx, parmpacket, dostype, ver, rev, ci);
  7893. write_log (_T("RDB: not replacing FileSystem.resource\n"));
  7894. return FILESYS_HARDFILE;
  7895. }
  7896. zfile_fseek (zf, 0, SEEK_END);
  7897. size = zfile_ftell (zf);
  7898. if (size > 0) {
  7899. zfile_fseek (zf, 0, SEEK_SET);
  7900. uip->rdb_filesysstore = xmalloc (uae_u8, size);
  7901. zfile_fread (uip->rdb_filesysstore, size, 1, zf);
  7902. for (int i = 0; i < size - 6; i++) {
  7903. uae_u8 *p = uip->rdb_filesysstore + i;
  7904. if (p[0] == 'V' && p[1] == 'E' && p[2] == 'R' && p[3] == ':' && p[4] == ' ') {
  7905. uae_u8 *p2;
  7906. p += 5;
  7907. p2 = p;
  7908. while (*p2 && p2 - uip->rdb_filesysstore < size)
  7909. p2++;
  7910. if (p2[0] == 0) {
  7911. while (*p && (ver < 0 || rev < 0)) {
  7912. if (*p == ' ') {
  7913. p++;
  7914. ver = atol ((char*)p);
  7915. if (ver < 0)
  7916. ver = 0;
  7917. while (*p) {
  7918. if (*p == ' ')
  7919. break;
  7920. if (*p == '.') {
  7921. p++;
  7922. rev = atol ((char*)p);
  7923. if (rev < 0)
  7924. rev = 0;
  7925. } else {
  7926. p++;
  7927. }
  7928. }
  7929. break;
  7930. } else {
  7931. p++;
  7932. }
  7933. }
  7934. }
  7935. break;
  7936. }
  7937. }
  7938. }
  7939. zfile_fclose (zf);
  7940. uip->rdb_filesyssize = size;
  7941. // DOS\0 is not in fs.resource and fs.resource already existed?
  7942. if (dostype == DISK_TYPE_DOS && oldversion < 0)
  7943. oldversion = 0;
  7944. trap_put_long(ctx, parmpacket + PP_FSSIZE, uip->rdb_filesyssize);
  7945. trap_put_long(ctx, parmpacket + PP_ADDTOFSRES, oldversion < 0 ? -1 : 0);
  7946. addfakefilesys(ctx, parmpacket, dostype, ver, rev, ci);
  7947. write_log (_T("RDB: faked RDB filesystem %08X (%s %d.%d) loaded. ADD2FS=%d\n"), dostype, dostypes (dt, dostype), ver, rev, oldversion < 0 ? 1 : 0);
  7948. return FILESYS_HARDFILE;
  7949. }
  7950. /* Fill in per-unit fields of a parampacket */
  7951. static uae_u32 REGPARAM2 filesys_dev_storeinfo (TrapContext *ctx)
  7952. {
  7953. int no = trap_get_dreg(ctx, 6) & 0x7fffffff;
  7954. int unit_no = no & 65535;
  7955. int sub_no = no >> 16;
  7956. int type;
  7957. if (unit_no >= MAX_FILESYSTEM_UNITS)
  7958. return -2;
  7959. if (sub_no >= MAX_FILESYSTEM_UNITS)
  7960. return -2;
  7961. UnitInfo *uip = mountinfo.ui;
  7962. uaecptr parmpacket = trap_get_areg(ctx, 0);
  7963. int iscd = (trap_get_dreg(ctx, 6) & 0x80000000) != 0 || uip[unit_no].unit_type == UNIT_CDFS;
  7964. struct uaedev_config_info *ci = &uip[unit_no].hf.ci;
  7965. uip[unit_no].parmpacket = parmpacket;
  7966. if (!ks12hack_deviceproc)
  7967. ks12hack_deviceproc = trap_get_long(ctx, parmpacket + PP_DEVICEPROC);
  7968. trap_put_long(ctx, parmpacket + PP_DEVICEPROC, 0);
  7969. trap_put_long(ctx, parmpacket + PP_ADDTOFSRES, 0);
  7970. trap_put_long(ctx, parmpacket + PP_FSSIZE, 0);
  7971. if (iscd) {
  7972. TCHAR *cdname = NULL;
  7973. uaecptr cdname_amiga;
  7974. int cd_unit_no = unit_no - cd_unit_offset;
  7975. if (sub_no)
  7976. return -2;
  7977. type = FILESYS_CD;
  7978. get_new_device(ctx, type, parmpacket, &uip[unit_no].devname, &uip[unit_no].devname_amiga, cd_unit_no);
  7979. cdname_amiga = uip[unit_no].devname_amiga;
  7980. uip[unit_no].devno = unit_no;
  7981. type = FILESYS_VIRTUAL;
  7982. gui_flicker_led (LED_CD, cd_unit_no, 0);
  7983. write_log (_T("Mounting uaescsi.device %d: (%d)\n"), cd_unit_no, unit_no);
  7984. trap_put_long(ctx, parmpacket + 0, cdname_amiga);
  7985. trap_put_long(ctx, parmpacket + 4, cdfs_devname);
  7986. trap_put_long(ctx, parmpacket + 8, cd_unit_no);
  7987. trap_put_long(ctx, parmpacket + 12, 0); /* Device flags */
  7988. trap_put_long(ctx, parmpacket + 16, 19); /* Env. size */
  7989. trap_put_long(ctx, parmpacket + 20, 2048 >> 2); /* longwords per block */
  7990. trap_put_long(ctx, parmpacket + 24, 0); /* unused */
  7991. trap_put_long(ctx, parmpacket + 28, 1); /* heads */
  7992. trap_put_long(ctx, parmpacket + 32, 1); /* sectors per block */
  7993. trap_put_long(ctx, parmpacket + 36, 1); /* sectors per track */
  7994. trap_put_long(ctx, parmpacket + 40, 0); /* reserved blocks */
  7995. trap_put_long(ctx, parmpacket + 44, 0); /* unused */
  7996. trap_put_long(ctx, parmpacket + 48, 0); /* interleave */
  7997. trap_put_long(ctx, parmpacket + 52, 0); /* lowCyl */
  7998. trap_put_long(ctx, parmpacket + 56, 0); /* hiCyl */
  7999. trap_put_long(ctx, parmpacket + 60, 50); /* Number of buffers */
  8000. trap_put_long(ctx, parmpacket + 64, 1); /* Buffer mem type */
  8001. trap_put_long(ctx, parmpacket + 68, 0x7FFFFFFE); /* largest transfer */
  8002. trap_put_long(ctx, parmpacket + 72, 0xFFFFFFFE); /* dma mask */
  8003. trap_put_long(ctx, parmpacket + 76, scsi_get_cd_drive_media_mask () & (1 << cd_unit_no) ? -127 : -128); /* bootPri */
  8004. trap_put_long(ctx, parmpacket + 80, CDFS_DOSTYPE | (((cd_unit_no / 10) + '0') << 8) | ((cd_unit_no % 10) + '0'));
  8005. trap_put_long(ctx, parmpacket + 84, 0); /* baud */
  8006. trap_put_long(ctx, parmpacket + 88, 0); /* control */
  8007. trap_put_long(ctx, parmpacket + 92, 0); /* bootblocks */
  8008. return type;
  8009. } else {
  8010. gui_flicker_led (LED_HD, unit_no, 0);
  8011. type = is_hardfile (unit_no);
  8012. if (type == FILESYS_HARDFILE_RDB || type == FILESYS_HARDDRIVE) {
  8013. /* RDB hardfile */
  8014. uip[unit_no].devno = uip[unit_no].hf.ci.controller_unit;
  8015. return rdb_mount(ctx, &uip[unit_no], unit_no, sub_no, parmpacket);
  8016. }
  8017. if (sub_no)
  8018. return -2;
  8019. write_log (_T("Mounting uaehf.device:%d %d (%d):\n"), uip->hf.ci.controller_unit, unit_no, sub_no);
  8020. get_new_device(ctx, type, parmpacket, &uip[unit_no].devname, &uip[unit_no].devname_amiga, unit_no);
  8021. uip[unit_no].devno = uip[unit_no].hf.ci.controller_unit;
  8022. trap_put_long(ctx, parmpacket, uip[unit_no].devname_amiga);
  8023. trap_put_long(ctx, parmpacket + 8, uip[unit_no].devno);
  8024. trap_put_long(ctx, parmpacket + 12, 0); /* Device flags */
  8025. trap_put_long(ctx, parmpacket + 16, 16); /* Env. size */
  8026. trap_put_long(ctx, parmpacket + 24, 0); /* unused */
  8027. trap_put_long(ctx, parmpacket + 44, 0); /* unused */
  8028. trap_put_long(ctx, parmpacket + 48, 0); /* interleave */
  8029. trap_put_long(ctx, parmpacket + 60, 50); /* Number of buffers */
  8030. trap_put_long(ctx, parmpacket + 64, 1); /* Buffer mem type */
  8031. trap_put_long(ctx, parmpacket + 68, 0x7FFFFFFE); /* largest transfer */
  8032. trap_put_long(ctx, parmpacket + 72, 0xFFFFFFFE); /* dma mask */
  8033. trap_put_long(ctx, parmpacket + 76, uip[unit_no].bootpri); /* bootPri */
  8034. if (type == FILESYS_VIRTUAL) {
  8035. // generate some sane-looking geometry if some program really cares..
  8036. uae_s64 hicyl = 100;
  8037. uae_u32 heads = 16;
  8038. if (currprefs.filesys_limit) {
  8039. hicyl = ((currprefs.filesys_limit * 1024) / 512) / (heads * 127);
  8040. } else {
  8041. struct fs_usage fsu;
  8042. if (!get_fs_usage(uip[unit_no].rootdir, 0, &fsu)) {
  8043. for (;;) {
  8044. hicyl = (fsu.total / 512) / (heads * 127);
  8045. if (hicyl < 65536 || heads == 64)
  8046. break;
  8047. heads *= 2;
  8048. }
  8049. }
  8050. }
  8051. trap_put_long(ctx, parmpacket + 4, fsdevname);
  8052. trap_put_long(ctx, parmpacket + 20, 512 >> 2); /* longwords per block */
  8053. trap_put_long(ctx, parmpacket + 28, heads); /* heads */
  8054. trap_put_long(ctx, parmpacket + 32, 1); /* sectors per block */
  8055. trap_put_long(ctx, parmpacket + 36, 127); /* sectors per track */
  8056. trap_put_long(ctx, parmpacket + 40, 2); /* reserved blocks */
  8057. trap_put_long(ctx, parmpacket + 52, 1); /* lowCyl */
  8058. trap_put_long(ctx, parmpacket + 56, (uae_u32)hicyl); /* hiCyl */
  8059. trap_put_long(ctx, parmpacket + 80, DISK_TYPE_DOS); /* DOS\0 */
  8060. } else {
  8061. uae_u8 buf[512];
  8062. trap_put_long(ctx, parmpacket + 4, ROM_hardfile_resname);
  8063. trap_put_long(ctx, parmpacket + 20, ci->blocksize >> 2); /* longwords per block */
  8064. trap_put_long(ctx, parmpacket + 28, ci->surfaces); /* heads */
  8065. trap_put_long(ctx, parmpacket + 32, ci->sectorsperblock); /* sectors per block */
  8066. trap_put_long(ctx, parmpacket + 36, ci->sectors); /* sectors per track */
  8067. trap_put_long(ctx, parmpacket + 40, ci->reserved); /* reserved blocks */
  8068. trap_put_long(ctx, parmpacket + 52, ci->lowcyl); /* lowCyl */
  8069. trap_put_long(ctx, parmpacket + 56, ci->highcyl <= 0 ? ci->cyls - 1 : ci->highcyl - 1); /* hiCyl */
  8070. trap_put_long(ctx, parmpacket + 48, ci->interleave); /* interleave */
  8071. trap_put_long(ctx, parmpacket + 60, ci->buffers); /* Number of buffers */
  8072. trap_put_long(ctx, parmpacket + 64, ci->bufmemtype); /* Buffer mem type */
  8073. trap_put_long(ctx, parmpacket + 68, ci->maxtransfer); /* largest transfer */
  8074. trap_put_long(ctx, parmpacket + 72, ci->mask); /* dma mask */
  8075. trap_put_long(ctx, parmpacket + 80, DISK_TYPE_DOS); /* DOS\0 */
  8076. memset(buf, 0, sizeof buf);
  8077. if (ci->dostype) { // forced dostype?
  8078. trap_put_long(ctx, parmpacket + 80, ci->dostype); /* dostype */
  8079. } else if (hdf_read (&uip[unit_no].hf, buf, 0, sizeof buf)) {
  8080. uae_u32 dt = rl (buf);
  8081. if (dt != 0x00000000 && dt != 0xffffffff)
  8082. trap_put_long(ctx, parmpacket + 80, dt);
  8083. }
  8084. memset(buf, 0, sizeof buf);
  8085. char *s = ua_fs(uip[unit_no].devname, -1);
  8086. buf[36] = strlen(s);
  8087. for (int i = 0; i < buf[36]; i++)
  8088. buf[37 + i] = s[i];
  8089. xfree(s);
  8090. for (int i = 0; i < 80; i++)
  8091. buf[i + 128] = trap_get_byte(ctx, parmpacket + 16 + i);
  8092. dump_partinfo (&uip[unit_no].hf, buf);
  8093. }
  8094. if (type == FILESYS_HARDFILE)
  8095. type = dofakefilesys(ctx, &uip[unit_no], parmpacket, ci);
  8096. if (uip[unit_no].bootpri < -127 || (type == FILESYS_HARDFILE && ci->rootdir[0] == 0))
  8097. trap_set_dreg(ctx, 7, trap_get_dreg(ctx, 7) & ~1); /* do not boot */
  8098. if (uip[unit_no].bootpri < -128)
  8099. return -1; /* do not mount */
  8100. return type;
  8101. }
  8102. }
  8103. static uae_u32 REGPARAM2 mousehack_done (TrapContext *ctx)
  8104. {
  8105. int mode = trap_get_dreg(ctx, 1);
  8106. if (mode < 10) {
  8107. uaecptr diminfo = trap_get_areg(ctx, 2);
  8108. uaecptr dispinfo = trap_get_areg(ctx, 3);
  8109. uaecptr vp = trap_get_areg(ctx, 4);
  8110. return input_mousehack_status(ctx, mode, diminfo, dispinfo, vp, trap_get_dreg(ctx, 2));
  8111. } else if (mode == 10) {
  8112. amiga_clipboard_die(ctx);
  8113. } else if (mode == 11) {
  8114. amiga_clipboard_got_data(ctx, trap_get_areg(ctx, 2), trap_get_dreg(ctx, 2), trap_get_dreg(ctx, 0) + 8);
  8115. } else if (mode == 12) {
  8116. return amiga_clipboard_want_data(ctx);
  8117. } else if (mode == 13) {
  8118. return amiga_clipboard_proc_start(ctx);
  8119. } else if (mode == 14) {
  8120. amiga_clipboard_task_start(ctx, trap_get_dreg(ctx, 0));
  8121. } else if (mode == 15) {
  8122. amiga_clipboard_init(ctx);
  8123. } else if (mode == 16) {
  8124. uaecptr a2 = trap_get_areg(ctx, 2);
  8125. input_mousehack_mouseoffset(a2);
  8126. } else if (mode == 17) {
  8127. uae_u32 v = 0;
  8128. if (currprefs.clipboard_sharing)
  8129. v |= 1;
  8130. if (consolehook_activate())
  8131. v |= 2;
  8132. if (currprefs.uaeboard > 2)
  8133. v |= 4;
  8134. trap_dos_active();
  8135. return v;
  8136. } else if (mode == 18) {
  8137. put_long_host(rtarea_bank.baseaddr + RTAREA_EXTERTASK, trap_get_dreg(ctx, 0));
  8138. put_long_host(rtarea_bank.baseaddr + RTAREA_TRAPTASK, trap_get_dreg(ctx, 2));
  8139. return rtarea_base + RTAREA_HEARTBEAT;
  8140. } else if (mode == 19) {
  8141. // boot rom copy
  8142. // d2 = ram address
  8143. return boot_rom_copy(ctx, trap_get_dreg(ctx, 2), 0);
  8144. } else if (mode == 20) {
  8145. // boot rom copy done
  8146. return boot_rom_copy(ctx, trap_get_dreg(ctx, 2), 1);
  8147. } else if (mode == 21) {
  8148. // keymap hook
  8149. #ifdef RETROPLATFORM
  8150. rp_keymap(ctx, trap_get_areg(ctx, 1), trap_get_dreg(ctx, 0));
  8151. #endif
  8152. return 1;
  8153. } else if (mode == 101) {
  8154. consolehook_ret(ctx, trap_get_areg(ctx, 1), trap_get_areg(ctx, 2));
  8155. } else if (mode == 102) {
  8156. uaecptr ret = consolehook_beginio(ctx, trap_get_areg(ctx, 1));
  8157. trap_put_long(ctx, trap_get_areg(ctx, 7) + 4 * 4, ret);
  8158. } else if (mode == 200) {
  8159. uae_u32 v;
  8160. // a0 = data, d0 = length, a1 = task, d3 = stack size (in), stack ptr (out)
  8161. // a2 = debugdata, d2 = debuglength
  8162. // d4 = flags
  8163. if ((trap_get_dreg(ctx, 4) & 3) != 1) {
  8164. write_log(_T("unsupported uaedbg version\n"));
  8165. return 0;
  8166. }
  8167. uae_u32 stack = trap_get_dreg(ctx, 3);
  8168. v = debugmem_reloc(trap_get_areg(ctx, 0), trap_get_dreg(ctx, 0),
  8169. trap_get_areg(ctx, 2), trap_get_dreg(ctx, 2),
  8170. trap_get_areg(ctx, 1), &stack);
  8171. trap_set_dreg(ctx, 2, stack);
  8172. return v;
  8173. } else if (mode == 201) {
  8174. debugmem_break(8);
  8175. return 1;
  8176. } else if (mode == 202) {
  8177. // a0 = seglist, a1 = name, d2 = lock
  8178. debugmem_addsegs(ctx, trap_get_areg(ctx, 0), trap_get_areg(ctx, 1), trap_get_dreg(ctx, 2), true);
  8179. return 1;
  8180. } else if (mode == 203) {
  8181. // a0 = seglist
  8182. debugmem_remsegs(trap_get_areg(ctx, 0));
  8183. return 1;
  8184. } else if (mode == 204 || mode == 206) {
  8185. // d0 = size, a1 = flags
  8186. uae_u32 v = debugmem_allocmem(mode == 206, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 1), trap_get_areg(ctx, 0));
  8187. if (v) {
  8188. trap_set_areg(ctx, 0, v);
  8189. return v;
  8190. } else {
  8191. trap_set_areg(ctx, 0, 0);
  8192. trap_set_dreg(ctx, 1, trap_get_areg(ctx, 1));
  8193. return trap_get_dreg(ctx, 0);
  8194. }
  8195. } else if (mode == 205 || mode == 207) {
  8196. return debugmem_freemem(mode == 207, trap_get_areg(ctx, 1), trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0));
  8197. } else if (mode == 208) {
  8198. // segtrack: bit 0
  8199. // fsdebug: bit 1
  8200. segtrack_mode = currprefs.debugging_features;
  8201. return segtrack_mode;
  8202. } else if (mode == 209) {
  8203. // called if segtrack was enabled
  8204. return 0;
  8205. } else if (mode == 210) {
  8206. // debug trapcode
  8207. debugmem_trap(trap_get_areg(ctx, 0));
  8208. } else if (mode == 212) {
  8209. // a0 = seglist, a1 = name, d2 = lock
  8210. debugmem_addsegs(ctx, trap_get_areg(ctx, 0), trap_get_areg(ctx, 1), trap_get_dreg(ctx, 2), false);
  8211. return 1;
  8212. } else if (mode == 213) {
  8213. // a0 = seglist
  8214. debugmem_remsegs(trap_get_areg(ctx, 0));
  8215. return 1;
  8216. } else if (mode == 299) {
  8217. return debugmem_exit();
  8218. } else {
  8219. write_log (_T("Unknown mousehack hook %d\n"), mode);
  8220. }
  8221. return 1;
  8222. }
  8223. void filesys_vsync (void)
  8224. {
  8225. TrapContext *ctx = NULL;
  8226. Unit *u;
  8227. if (uae_boot_rom_type <= 0)
  8228. return;
  8229. if (currprefs.uaeboard > 1 && !(uae_int_requested & 1)) {
  8230. bool req = false;
  8231. // check if there is waiting requests without signal
  8232. if (!uae_sem_trywait(&singlethread_int_sem)) {
  8233. if (comm_pipe_has_data(&native2amiga_pending))
  8234. req = true;
  8235. if (!req) {
  8236. UnitInfo *uip = mountinfo.ui;
  8237. int unit_no = 0;
  8238. for (;;) {
  8239. if (unit_no >= MAX_FILESYSTEM_UNITS)
  8240. break;
  8241. if (uip[unit_no].open > 0 && uip[unit_no].self != 0
  8242. && uip[unit_no].self->cmds_acked == uip[unit_no].self->cmds_complete
  8243. && uip[unit_no].self->cmds_acked != uip[unit_no].self->cmds_sent)
  8244. break;
  8245. unit_no++;
  8246. }
  8247. if (unit_no < MAX_FILESYSTEM_UNITS) {
  8248. if (uip[unit_no].self->port)
  8249. req = true;
  8250. }
  8251. }
  8252. uae_sem_post(&singlethread_int_sem);
  8253. if (req)
  8254. do_uae_int_requested();
  8255. }
  8256. }
  8257. if (media_queued_total > 0) {
  8258. if (automountunit == -1) {
  8259. bool cont = true;
  8260. for (u = units; u; u = u->next) {
  8261. if (u->reinsertdelay > 0) {
  8262. cont = false;
  8263. break;
  8264. }
  8265. }
  8266. if (cont) {
  8267. TCHAR *mountpath = media_queued_paths[media_queued_cnt++];
  8268. if (mountpath) {
  8269. filesys_media_change(mountpath, 3, NULL);
  8270. }
  8271. if (media_queued_cnt >= media_queued_total) {
  8272. for (int i = 0; i < media_queued_total; i++) {
  8273. xfree(media_queued_paths[i]);
  8274. }
  8275. xfree(media_queued_paths);
  8276. media_queued_cnt = 0;
  8277. media_queued_total = 0;
  8278. media_queued_paths = NULL;
  8279. for (int i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
  8280. if (mountinfo.ui[i].canremove < 0)
  8281. mountinfo.ui[i].canremove = 1;
  8282. }
  8283. }
  8284. }
  8285. }
  8286. }
  8287. if (heartbeat == get_long_host(rtarea_bank.baseaddr + RTAREA_HEARTBEAT)) {
  8288. if (heartbeat_count > 0)
  8289. heartbeat_count--;
  8290. if (heartbeat_count_cont > 0)
  8291. heartbeat_count_cont--;
  8292. return;
  8293. }
  8294. heartbeat = get_long_host(rtarea_bank.baseaddr + RTAREA_HEARTBEAT);
  8295. heartbeat_count_cont = 10;
  8296. cia_heartbeat ();
  8297. for (u = units; u; u = u->next) {
  8298. if (u->reinsertdelay > 0) {
  8299. u->reinsertdelay--;
  8300. if (u->reinsertdelay == 0) {
  8301. if (u->newrootdir == NULL && u->newvolume == NULL) {
  8302. filesys_eject(u->unit);
  8303. } else {
  8304. filesys_insert(u->unit, u->newvolume, u->newrootdir, u->newreadonly, u->newflags);
  8305. }
  8306. xfree (u->newvolume);
  8307. u->newvolume = NULL;
  8308. xfree (u->newrootdir);
  8309. u->newrootdir = NULL;
  8310. }
  8311. }
  8312. record_timeout(ctx, u);
  8313. }
  8314. for (int i = 0; i < currprefs.mountitems; i++) {
  8315. struct hardfiledata *hfd = get_hardfile_data (currprefs.mountconfig[i].configoffset);
  8316. if (!hfd)
  8317. continue;
  8318. if (hfd->reinsertdelay > 0) {
  8319. hfd->reinsertdelay--;
  8320. if (hfd->reinsertdelay == 0) {
  8321. hfd->reinsertdelay = -1;
  8322. hardfile_media_change (hfd, &hfd->delayedci, true, true);
  8323. }
  8324. }
  8325. }
  8326. if (heartbeat_count <= 0)
  8327. return;
  8328. if (heartbeat_task & 1) {
  8329. setsystime_vblank ();
  8330. heartbeat_task &= ~1;
  8331. }
  8332. }
  8333. void filesys_cleanup(void)
  8334. {
  8335. filesys_free_handles();
  8336. free_mountinfo();
  8337. destroy_comm_pipe(&shellexecute_pipe);
  8338. uae_sem_destroy(&singlethread_int_sem);
  8339. shell_execute_data = 0;
  8340. }
  8341. void filesys_install (void)
  8342. {
  8343. uaecptr loop;
  8344. TRACEI ((_T("Installing filesystem\n")));
  8345. uae_sem_init (&singlethread_int_sem, 0, 1);
  8346. init_comm_pipe(&shellexecute_pipe, 100, 1);
  8347. ROM_filesys_resname = ds_ansi ("UAEfs.resource");
  8348. ROM_filesys_resid = ds_ansi (UAEFS_VERSION);
  8349. fsdevname = ROM_hardfile_resname;
  8350. fshandlername = ds_bstr_ansi ("uaefs");
  8351. cdfs_devname = ds_ansi ("uaescsi.device");
  8352. cdfs_handlername = ds_bstr_ansi ("uaecdfs");
  8353. afterdos_name = ds_ansi("UAE afterdos");
  8354. afterdos_id = ds_ansi("UAE afterdos 0.2");
  8355. keymaphook_name = ds_ansi("UAE keymaphook");
  8356. keymaphook_id = ds_ansi("UAE keymaphook 0.1");
  8357. ROM_filesys_diagentry = here ();
  8358. calltrap (deftrap2 (filesys_diagentry, 0, _T("filesys_diagentry")));
  8359. dw(0x4ED0); /* JMP (a0) - jump to code that inits Residents */
  8360. ROM_filesys_doio = here();
  8361. calltrap(deftrap2(filesys_doio, 0, _T("filesys_doio")));
  8362. dw(RTS);
  8363. ROM_filesys_putmsg = here();
  8364. calltrap(deftrap2(filesys_putmsg, 0, _T("filesys_putmsg")));
  8365. dw(RTS);
  8366. ROM_filesys_putmsg_return = here();
  8367. calltrap(deftrap2(filesys_putmsg_return, 0, _T("filesys_putmsg_return")));
  8368. dw(RTS);
  8369. loop = here ();
  8370. org (rtarea_base + RTAREA_HEARTBEAT);
  8371. dl (0);
  8372. heartbeat = 0;
  8373. heartbeat_task = 0;
  8374. org (rtarea_base + 0xFF18);
  8375. calltrap (deftrap2 (filesys_dev_bootfilesys, 0, _T("filesys_dev_bootfilesys")));
  8376. dw (RTS);
  8377. /* Special trap for the assembly make_dev routine */
  8378. org (rtarea_base + 0xFF20);
  8379. calltrap (deftrap2 (filesys_dev_remember, 0, _T("filesys_dev_remember")));
  8380. dw (RTS);
  8381. org (rtarea_base + 0xFF28);
  8382. calltrap (deftrap2 (filesys_dev_storeinfo, 0, _T("filesys_dev_storeinfo")));
  8383. dw (RTS);
  8384. org (rtarea_base + 0xFF30);
  8385. calltrap (deftrap2 (filesys_handler, 0, _T("filesys_handler")));
  8386. dw (RTS);
  8387. org (rtarea_base + 0xFF38);
  8388. calltrap (deftrapres(mousehack_done, 0, _T("misc_funcs")));
  8389. dw (RTS);
  8390. org (rtarea_base + 0xFF40);
  8391. calltrap (deftrap2 (startup_handler, 0, _T("startup_handler")));
  8392. dw (RTS);
  8393. org (rtarea_base + 0xFF48);
  8394. calltrap (deftrap2 (filesys_init_storeinfo, TRAPFLAG_EXTRA_STACK, _T("filesys_init_storeinfo")));
  8395. dw (RTS);
  8396. org (rtarea_base + 0xFF50);
  8397. calltrap (deftrap2 (exter_int_helper, 0, _T("exter_int_helper")));
  8398. dw (RTS);
  8399. org (rtarea_base + 0xFF58);
  8400. calltrap (deftrap2 (fsmisc_helper, 0, _T("fsmisc_helper")));
  8401. dw (RTS);
  8402. org(rtarea_base + 0xFF68);
  8403. calltrap(deftrap2(filesys_bcpl_wrapper, 0, _T("filesys_bcpl_wrapper")));
  8404. dw(RTS);
  8405. org(rtarea_base + 0xFF78);
  8406. calltrap(deftrap2(debugger_helper, 0, _T("debugger_helper")));
  8407. dw(RTS);
  8408. org (loop);
  8409. create_ks12_boot();
  8410. create_68060_nofpu();
  8411. }
  8412. uaecptr filesys_get_entry(int index)
  8413. {
  8414. return bootrom_start + dlg(bootrom_start + bootrom_header + index * 4 - 4) + bootrom_header - 4;
  8415. }
  8416. void filesys_install_code (void)
  8417. {
  8418. uae_u32 b, items;
  8419. bootrom_header = 3 * 4;
  8420. align(4);
  8421. bootrom_start = here ();
  8422. #include "filesys_bootrom.cpp"
  8423. items = dlg (bootrom_start + 8) & 0xffff;
  8424. /* The last offset comes from the code itself, look for it near the top. */
  8425. EXPANSION_bootcode = bootrom_start + bootrom_header + items * 4 - 4;
  8426. b = bootrom_start + bootrom_header + 3 * 4 - 4;
  8427. filesys_initcode = bootrom_start + dlg (b) + bootrom_header - 4;
  8428. afterdos_initcode = filesys_get_entry(8);
  8429. keymaphook_initcode = filesys_get_entry(11);
  8430. // Fill struct resident
  8431. TCHAR buf[256];
  8432. TCHAR *s = au(UAEFS_VERSION);
  8433. int y, m, d;
  8434. target_getdate(&y, &m, &d);
  8435. _stprintf(buf, _T("%s (%d.%d.%d)\r\n"), s, d, m, y);
  8436. uaecptr idstring = ds(buf);
  8437. xfree(s);
  8438. b = here();
  8439. items = dlg(bootrom_start) * 2;
  8440. for (int i = 0; i < items; i += 2) {
  8441. if (((dbg(bootrom_start + i + 0) << 8) | (dbg(bootrom_start + i + 1) << 0)) == 0x4afc) {
  8442. org(bootrom_start + i + 2);
  8443. dl(bootrom_start + i);
  8444. dl(dlg(here()) + bootrom_start + i); // endskip
  8445. db(0); // flags
  8446. db(1); // version
  8447. db(0); // type
  8448. db(0); // pri
  8449. dl(dlg(here()) + bootrom_start + i); // name
  8450. dl(idstring); // idstring
  8451. dl(dlg(here()) + bootrom_start + i); // init
  8452. break;
  8453. }
  8454. }
  8455. org(b);
  8456. }
  8457. #ifdef _WIN32
  8458. #include "od-win32/win32_filesys.cpp"
  8459. #endif
  8460. static uae_u8 *restore_filesys_hardfile (UnitInfo *ui, uae_u8 *src)
  8461. {
  8462. struct hardfiledata *hfd = &ui->hf;
  8463. TCHAR *s;
  8464. hfd->virtsize = restore_u64 ();
  8465. hfd->offset = restore_u64 ();
  8466. hfd->ci.highcyl = restore_u32 ();
  8467. hfd->ci.sectors = restore_u32 ();
  8468. hfd->ci.surfaces = restore_u32 ();
  8469. hfd->ci.reserved = restore_u32 ();
  8470. hfd->ci.blocksize = restore_u32 ();
  8471. hfd->ci.readonly = restore_u32 () != 0;
  8472. hfd->flags = restore_u32 ();
  8473. hfd->rdbcylinders = restore_u32 ();
  8474. hfd->rdbsectors = restore_u32 ();
  8475. hfd->rdbheads = restore_u32 ();
  8476. s = restore_string ();
  8477. _tcscpy (hfd->vendor_id, s);
  8478. xfree (s);
  8479. s = restore_string ();
  8480. _tcscpy (hfd->product_id, s);
  8481. xfree (s);
  8482. s = restore_string ();
  8483. _tcscpy (hfd->product_rev, s);
  8484. xfree (s);
  8485. s = restore_string ();
  8486. _tcscpy (hfd->ci.devname, s);
  8487. xfree (s);
  8488. return src;
  8489. }
  8490. static uae_u8 *save_filesys_hardfile (UnitInfo *ui, uae_u8 *dst)
  8491. {
  8492. struct hardfiledata *hfd = &ui->hf;
  8493. save_u64 (hfd->virtsize);
  8494. save_u64 (hfd->offset);
  8495. save_u32 (hfd->ci.highcyl);
  8496. save_u32 (hfd->ci.sectors);
  8497. save_u32 (hfd->ci.surfaces);
  8498. save_u32 (hfd->ci.reserved);
  8499. save_u32 (hfd->ci.blocksize);
  8500. save_u32 (hfd->ci.readonly);
  8501. save_u32 (hfd->flags);
  8502. save_u32 (hfd->rdbcylinders);
  8503. save_u32 (hfd->rdbsectors);
  8504. save_u32 (hfd->rdbheads);
  8505. save_string (hfd->vendor_id);
  8506. save_string (hfd->product_id);
  8507. save_string (hfd->product_rev);
  8508. save_string (hfd->ci.devname);
  8509. return dst;
  8510. }
  8511. static a_inode *restore_filesys_get_base (Unit *u, TCHAR *npath)
  8512. {
  8513. TCHAR *path, *p, *p2;
  8514. a_inode *a;
  8515. int cnt, err, i;
  8516. /* no '/' = parent is root */
  8517. if (!_tcschr (npath, '/'))
  8518. return &u->rootnode;
  8519. /* iterate from root to last to previous path part,
  8520. * create ainos if not already created.
  8521. */
  8522. path = xcalloc(TCHAR, _tcslen (npath) + 2);
  8523. cnt = 1;
  8524. for (;;) {
  8525. _tcscpy (path, npath);
  8526. p = path;
  8527. for (i = 0; i < cnt ;i++) {
  8528. if (i > 0)
  8529. p++;
  8530. while (*p != '/' && *p != 0)
  8531. p++;
  8532. }
  8533. if (*p) {
  8534. *p = 0;
  8535. err = 0;
  8536. get_aino (u, &u->rootnode, path, &err);
  8537. if (err) {
  8538. write_log (_T("*** FS: missing path '%s'!\n"), path);
  8539. return NULL;
  8540. }
  8541. cnt++;
  8542. } else {
  8543. break;
  8544. }
  8545. }
  8546. /* find base (parent) of last path part */
  8547. _tcscpy (path, npath);
  8548. p = path;
  8549. a = u->rootnode.child;
  8550. for (;;) {
  8551. if (*p == 0) {
  8552. write_log (_T("*** FS: base aino NOT found '%s' ('%s')\n"), a->nname, npath);
  8553. xfree (path);
  8554. return NULL;
  8555. }
  8556. p2 = p;
  8557. while(*p2 != '/' && *p2 != '\\' && *p2 != 0)
  8558. p2++;
  8559. *p2 = 0;
  8560. while (a) {
  8561. if (!same_aname(p, a->aname)) {
  8562. a = a->sibling;
  8563. continue;
  8564. }
  8565. p = p2 + 1;
  8566. if (*p == 0) {
  8567. write_log (_T("FS: base aino found '%s' ('%s')\n"), a->nname, npath);
  8568. xfree (path);
  8569. return a;
  8570. }
  8571. a = a->child;
  8572. break;
  8573. }
  8574. if (!a) {
  8575. write_log (_T("*** FS: path part '%s' not found ('%s')\n"), p, npath);
  8576. xfree (path);
  8577. return NULL;
  8578. }
  8579. }
  8580. }
  8581. static TCHAR *makenativepath (UnitInfo *ui, TCHAR *apath)
  8582. {
  8583. TCHAR *pn;
  8584. /* create native path. FIXME: handle 'illegal' characters */
  8585. pn = xcalloc (TCHAR, _tcslen (apath) + 1 + _tcslen (ui->rootdir) + 1);
  8586. _stprintf (pn, _T("%s/%s"), ui->rootdir, apath);
  8587. if (FSDB_DIR_SEPARATOR != '/') {
  8588. for (int i = 0; i < _tcslen (pn); i++) {
  8589. if (pn[i] == '/')
  8590. pn[i] = FSDB_DIR_SEPARATOR;
  8591. }
  8592. }
  8593. return pn;
  8594. }
  8595. static uae_u8 *restore_aino (UnitInfo *ui, Unit *u, uae_u8 *src)
  8596. {
  8597. TCHAR *p, *p2, *pn;
  8598. uae_u32 flags;
  8599. int missing;
  8600. a_inode *base, *a;
  8601. missing = 0;
  8602. a = xcalloc (a_inode, 1);
  8603. a->uniq = restore_u64 ();
  8604. a->locked_children = restore_u32 ();
  8605. a->exnext_count = restore_u32 ();
  8606. a->shlock = restore_u32 ();
  8607. flags = restore_u32 ();
  8608. if (flags & 1)
  8609. a->elock = 1;
  8610. if (flags & 4)
  8611. a->uniq_external = restore_u64 ();
  8612. /* full Amiga-side path without drive, eg. "C/SetPatch" */
  8613. p = restore_string ();
  8614. /* root (p = volume label) */
  8615. if (a->uniq == 0) {
  8616. a->nname = my_strdup (ui->rootdir);
  8617. a->aname = p;
  8618. a->dir = 1;
  8619. if (ui->volflags < 0) {
  8620. write_log (_T("FS: Volume '%s' ('%s') missing!\n"), a->aname, a->nname);
  8621. } else {
  8622. a->volflags = ui->volflags;
  8623. recycle_aino (u, a);
  8624. write_log (_T("FS: Lock (root) '%s' ('%s')\n"), a->aname, a->nname);
  8625. }
  8626. return src;
  8627. }
  8628. p2 = _tcsrchr(p, '/');
  8629. if (p2)
  8630. p2++;
  8631. else
  8632. p2 = p;
  8633. pn = makenativepath (ui, p);
  8634. a->nname = pn;
  8635. a->aname = my_strdup (p2);
  8636. /* create path to parent dir */
  8637. if (p2 != p)
  8638. p2[0] = 0;
  8639. /* find parent of a->aname (Already restored previously. I hope..) */
  8640. base = restore_filesys_get_base (u, p);
  8641. xfree(p);
  8642. if (flags & 2) {
  8643. a->dir = 1;
  8644. if (!my_existsdir(a->nname))
  8645. write_log (_T("*** FS: Directory '%s' missing!\n"), a->nname);
  8646. else
  8647. fsdb_clean_dir (a);
  8648. } else {
  8649. if (!my_existsfile(a->nname))
  8650. write_log (_T("*** FS: File '%s' missing!\n"), a->nname);
  8651. }
  8652. if (base) {
  8653. fill_file_attrs (u, base, a);
  8654. init_child_aino_tree (u, base, a);
  8655. } else {
  8656. write_log (_T("*** FS: parent directory missing '%s' ('%s')\n"), a->aname, a->nname);
  8657. missing = 1;
  8658. }
  8659. if (missing) {
  8660. write_log (_T("*** FS: Lock restore failed '%s' ('%s')\n"), a->aname, a->nname);
  8661. xfree (a->nname);
  8662. xfree (a->aname);
  8663. xfree (a);
  8664. } else {
  8665. write_log (_T("FS: Lock '%s' ('%s')\n"), a->aname, a->nname);
  8666. recycle_aino (u, a);
  8667. }
  8668. return src;
  8669. }
  8670. static uae_u8 *restore_key (UnitInfo *ui, Unit *u, uae_u8 *src)
  8671. {
  8672. int uniq;
  8673. TCHAR *p, *pn;
  8674. mode_t openmode;
  8675. int err;
  8676. int missing;
  8677. a_inode *a;
  8678. Key *k;
  8679. uae_u64 savedsize, size, pos;
  8680. missing = 0;
  8681. k = xcalloc (Key, 1);
  8682. k->uniq = restore_u64 ();
  8683. k->file_pos = restore_u32 ();
  8684. k->createmode = restore_u32 ();
  8685. k->dosmode = restore_u32 ();
  8686. savedsize = restore_u32 ();
  8687. uniq = restore_u64 ();
  8688. p = restore_string ();
  8689. pos = restore_u64 ();
  8690. size = restore_u64 ();
  8691. if (size) {
  8692. savedsize = size;
  8693. k->file_pos = pos;
  8694. }
  8695. pn = makenativepath (ui, p);
  8696. openmode = ((k->dosmode & A_FIBF_READ) == 0 ? O_WRONLY
  8697. : (k->dosmode & A_FIBF_WRITE) == 0 ? O_RDONLY
  8698. : O_RDWR);
  8699. write_log (_T("FS: open file '%s' ('%s'), pos=%llu\n"), p, pn, k->file_pos);
  8700. a = get_aino (u, &u->rootnode, p, &err);
  8701. if (!a)
  8702. write_log (_T("*** FS: Open file aino creation failed '%s'\n"), p);
  8703. missing = 1;
  8704. if (a) {
  8705. missing = 0;
  8706. k->aino = a;
  8707. if (a->uniq != uniq)
  8708. write_log (_T("*** FS: Open file '%s' aino id %d != %d\n"), p, uniq, a->uniq);
  8709. if (!my_existsfile (pn)) {
  8710. write_log (_T("*** FS: Open file '%s' is missing, creating dummy file!\n"), p);
  8711. if (savedsize < 10 * 1024 * 1024) {
  8712. k->fd = fs_openfile (u, a, openmode | O_CREAT |O_BINARY);
  8713. if (k->fd) {
  8714. uae_u8 *buf = xcalloc (uae_u8, 10000);
  8715. uae_u64 sp = savedsize;
  8716. while (sp) {
  8717. uae_u32 s = sp >= 10000 ? 10000 : sp;
  8718. fs_write (k->fd, buf, s);
  8719. sp -= s;
  8720. }
  8721. xfree(buf);
  8722. write_log (_T("*** FS: dummy file created\n"));
  8723. } else {
  8724. write_log (_T("*** FS: Open file '%s', couldn't create dummy file!\n"), p);
  8725. }
  8726. } else {
  8727. write_log (_T("*** FS: Too big, ignored\n"));
  8728. }
  8729. } else {
  8730. k->fd = fs_openfile (u, a, openmode | O_BINARY);
  8731. }
  8732. if (!k->fd) {
  8733. write_log (_T("*** FS: Open file '%s' failed to open!\n"), p);
  8734. missing = 1;
  8735. } else {
  8736. uae_s64 s;
  8737. s = key_filesize(k);
  8738. if (s != savedsize)
  8739. write_log (_T("FS: restored file '%s' size changed! orig=%llu, now=%lld!!\n"), p, savedsize, s);
  8740. if (k->file_pos > s) {
  8741. write_log (_T("FS: restored filepos larger than size of file '%s'!! %llu > %lld\n"), p, k->file_pos, s);
  8742. k->file_pos = s;
  8743. }
  8744. key_seek(k, k->file_pos, SEEK_SET);
  8745. }
  8746. }
  8747. xfree (p);
  8748. if (missing) {
  8749. xfree (k);
  8750. } else {
  8751. k->next = u->keys;
  8752. u->keys = k;
  8753. }
  8754. return src;
  8755. }
  8756. static uae_u8 *restore_notify (UnitInfo *ui, Unit *u, uae_u8 *src)
  8757. {
  8758. Notify *n = xcalloc (Notify, 1);
  8759. uae_u32 hash;
  8760. TCHAR *s;
  8761. n->notifyrequest = restore_u32 ();
  8762. s = restore_string ();
  8763. n->fullname = xmalloc (TCHAR, _tcslen (ui->volname) + 2 + _tcslen (s) + 1);
  8764. _stprintf (n->fullname, _T("%s:%s"), ui->volname, s);
  8765. xfree(s);
  8766. s = _tcsrchr (n->fullname, '/');
  8767. if (s)
  8768. s++;
  8769. else
  8770. s = n->fullname;
  8771. n->partname = my_strdup (s);
  8772. hash = notifyhash (n->fullname);
  8773. n->next = u->notifyhash[hash];
  8774. u->notifyhash[hash] = n;
  8775. write_log (_T("FS: notify %08X '%s' '%s'\n"), n->notifyrequest, n->fullname, n->partname);
  8776. return src;
  8777. }
  8778. static uae_u8 *restore_exkey (UnitInfo *ui, Unit *u, uae_u8 *src)
  8779. {
  8780. restore_u64 ();
  8781. restore_u64 ();
  8782. restore_u64 ();
  8783. return src;
  8784. }
  8785. static uae_u8 *restore_filesys_virtual (UnitInfo *ui, uae_u8 *src, int num)
  8786. {
  8787. Unit *u = startup_create_unit(NULL, ui, num);
  8788. int cnt;
  8789. u->dosbase = restore_u32 ();
  8790. u->volume = restore_u32 ();
  8791. u->port = restore_u32 ();
  8792. u->locklist = restore_u32 ();
  8793. u->dummy_message = restore_u32 ();
  8794. u->cmds_sent = restore_u64 ();
  8795. u->cmds_complete = restore_u64 ();
  8796. u->cmds_acked = restore_u64 ();
  8797. u->next_exkey = restore_u32 ();
  8798. u->total_locked_ainos = restore_u32 ();
  8799. u->volflags = ui->volflags;
  8800. cnt = restore_u32 ();
  8801. write_log (_T("FS: restoring %d locks\n"), cnt);
  8802. while (cnt-- > 0)
  8803. src = restore_aino (ui, u, src);
  8804. cnt = restore_u32 ();
  8805. write_log (_T("FS: restoring %d open files\n"), cnt);
  8806. while (cnt-- > 0)
  8807. src = restore_key (ui, u, src);
  8808. cnt = restore_u32 ();
  8809. write_log (_T("FS: restoring %d notifications\n"), cnt);
  8810. while (cnt-- > 0)
  8811. src = restore_notify (ui, u, src);
  8812. cnt = restore_u32 ();
  8813. write_log (_T("FS: restoring %d exkeys\n"), cnt);
  8814. while (cnt-- > 0)
  8815. src = restore_exkey (ui, u, src);
  8816. return src;
  8817. }
  8818. static TCHAR *getfullaname (a_inode *a)
  8819. {
  8820. TCHAR *p;
  8821. int first = 1;
  8822. p = xcalloc (TCHAR, MAX_DPATH);
  8823. while (a) {
  8824. int len = _tcslen (a->aname);
  8825. memmove (p + len + 1, p, (_tcslen (p) + 1) * sizeof (TCHAR));
  8826. memcpy (p, a->aname, len * sizeof (TCHAR));
  8827. if (!first)
  8828. p[len] = '/';
  8829. first = 0;
  8830. a = a->parent;
  8831. if (a && a->uniq == 0)
  8832. return p;
  8833. }
  8834. return p;
  8835. }
  8836. /* scan and save all Lock()'d files */
  8837. static int recurse_aino (UnitInfo *ui, a_inode *a, int cnt, uae_u8 **dstp)
  8838. {
  8839. uae_u8 *dst = NULL;
  8840. int dirty = 0;
  8841. a_inode *a2 = a;
  8842. if (dstp)
  8843. dst = *dstp;
  8844. while (a) {
  8845. //write_log("recurse '%s' '%s' %d %08x\n", a->aname, a->nname, a->uniq, a->parent);
  8846. if (a->elock || a->shlock || a->uniq == 0) {
  8847. if (dst) {
  8848. TCHAR *fn = NULL;
  8849. write_log (_T("uniq=%d %lld s=%d e=%d d=%d '%s' '%s'\n"), a->uniq, a->uniq_external, a->shlock, a->elock, a->dir, a->aname, a->nname);
  8850. if (a->aname) {
  8851. fn = getfullaname (a);
  8852. write_log (_T("->'%s'\n"), fn);
  8853. }
  8854. save_u64 (a->uniq);
  8855. save_u32 (a->locked_children);
  8856. save_u32 (a->exnext_count);
  8857. save_u32 (a->shlock);
  8858. save_u32 ((a->elock ? 1 : 0) | (a->dir ? 2 : 0) | 4);
  8859. save_u64 (a->uniq_external);
  8860. save_string (fn);
  8861. xfree(fn);
  8862. }
  8863. cnt++;
  8864. }
  8865. if (a->dirty)
  8866. dirty = 1;
  8867. if (a->child)
  8868. cnt = recurse_aino (ui, a->child, cnt, &dst);
  8869. a = a->sibling;
  8870. }
  8871. if (dirty && a2->parent)
  8872. fsdb_dir_writeback (a2->parent);
  8873. if (dst)
  8874. *dstp = dst;
  8875. return cnt;
  8876. }
  8877. static uae_u8 *save_key (uae_u8 *dst, Key *k)
  8878. {
  8879. TCHAR *fn = getfullaname (k->aino);
  8880. uae_u64 size;
  8881. save_u64 (k->uniq);
  8882. save_u32 ((uae_u32)k->file_pos);
  8883. save_u32 (k->createmode);
  8884. save_u32 (k->dosmode);
  8885. size = fs_fsize (k->fd);
  8886. save_u32 ((uae_u32)size);
  8887. save_u64 (k->aino->uniq);
  8888. save_string (fn);
  8889. save_u64 (k->file_pos);
  8890. save_u64 (size);
  8891. write_log (_T("'%s' uniq=%d size=%lld seekpos=%lld mode=%d dosmode=%d\n"),
  8892. fn, k->uniq, size, k->file_pos, k->createmode, k->dosmode);
  8893. xfree (fn);
  8894. return dst;
  8895. }
  8896. static uae_u8 *save_notify (UnitInfo *ui, uae_u8 *dst, Notify *n)
  8897. {
  8898. TCHAR *s;
  8899. save_u32 (n->notifyrequest);
  8900. s = n->fullname;
  8901. if (_tcslen (s) >= _tcslen (ui->volname) && !_tcsncmp (n->fullname, ui->volname, _tcslen (ui->volname)))
  8902. s = n->fullname + _tcslen (ui->volname) + 1;
  8903. save_string (s);
  8904. write_log (_T("FS: notify %08X '%s'\n"), n->notifyrequest, n->fullname);
  8905. return dst;
  8906. }
  8907. static uae_u8 *save_exkey (uae_u8 *dst, ExamineKey *ek)
  8908. {
  8909. save_u64 (ek->uniq);
  8910. save_u64 (ek->aino->uniq);
  8911. save_u64 (ek->curr_file->uniq);
  8912. return dst;
  8913. }
  8914. static uae_u8 *save_filesys_virtual (UnitInfo *ui, uae_u8 *dst)
  8915. {
  8916. Unit *u = ui->self;
  8917. Key *k;
  8918. int cnt, i, j;
  8919. write_log (_T("FSSAVE: '%s'\n"), ui->devname);
  8920. save_u32 (u->dosbase);
  8921. save_u32 (u->volume);
  8922. save_u32 (u->port);
  8923. save_u32 (u->locklist);
  8924. save_u32 (u->dummy_message);
  8925. save_u64 (u->cmds_sent);
  8926. save_u64 (u->cmds_complete);
  8927. save_u64 (u->cmds_acked);
  8928. save_u32 (u->next_exkey);
  8929. save_u32 (u->total_locked_ainos);
  8930. cnt = recurse_aino (ui, &u->rootnode, 0, NULL);
  8931. save_u32 (cnt);
  8932. write_log (_T("%d open locks\n"), cnt);
  8933. cnt = recurse_aino (ui, &u->rootnode, 0, &dst);
  8934. cnt = 0;
  8935. for (k = u->keys; k; k = k->next)
  8936. cnt++;
  8937. save_u32 (cnt);
  8938. write_log (_T("%d open files\n"), cnt);
  8939. for (k = u->keys; k; k = k->next)
  8940. dst = save_key (dst, k);
  8941. for (j = 0; j < 2; j++) {
  8942. cnt = 0;
  8943. for (i = 0; i < NOTIFY_HASH_SIZE; i++) {
  8944. Notify *n = u->notifyhash[i];
  8945. while (n) {
  8946. if (j > 0)
  8947. dst = save_notify (ui, dst, n);
  8948. cnt++;
  8949. n = n->next;
  8950. }
  8951. }
  8952. if (j == 0) {
  8953. save_u32 (cnt);
  8954. write_log (_T("%d notify requests\n"), cnt);
  8955. }
  8956. }
  8957. for (j = 0; j < 2; j++) {
  8958. cnt = 0;
  8959. for (i = 0; i < EXKEYS; i++) {
  8960. ExamineKey *ek = &u->examine_keys[i];
  8961. if (ek->uniq) {
  8962. cnt++;
  8963. if (j > 0)
  8964. dst = save_exkey (dst, ek);
  8965. }
  8966. }
  8967. if (j == 0) {
  8968. save_u32 (cnt);
  8969. write_log (_T("%d exkeys\n"), cnt);
  8970. }
  8971. }
  8972. write_log (_T("END\n"));
  8973. return dst;
  8974. }
  8975. static TCHAR *new_filesys_root_path, *new_filesys_fs_path;
  8976. uae_u8 *save_filesys_common (int *len)
  8977. {
  8978. uae_u8 *dstbak, *dst;
  8979. if (nr_units () == 0)
  8980. return NULL;
  8981. dstbak = dst = xmalloc (uae_u8, 1000);
  8982. save_u32 (2);
  8983. save_u64 (a_uniq);
  8984. save_u64 (key_uniq);
  8985. *len = dst - dstbak;
  8986. return dstbak;
  8987. }
  8988. uae_u8 *restore_filesys_common (uae_u8 *src)
  8989. {
  8990. if (restore_u32 () != 2)
  8991. return src;
  8992. cd_unit_offset = MAX_FILESYSTEM_UNITS;
  8993. cd_unit_number = 0;
  8994. filesys_prepare_reset2 ();
  8995. filesys_reset2 ();
  8996. a_uniq = restore_u64 ();
  8997. key_uniq = restore_u64 ();
  8998. xfree(new_filesys_root_path);
  8999. xfree(new_filesys_fs_path);
  9000. new_filesys_root_path = NULL;
  9001. new_filesys_fs_path = NULL;
  9002. return src;
  9003. }
  9004. uae_u8 *save_filesys_paths(int num, int *len)
  9005. {
  9006. uae_u8 *dstbak, *dst;
  9007. UnitInfo *ui;
  9008. int type = is_hardfile(num);
  9009. int ptype;
  9010. ui = &mountinfo.ui[num];
  9011. if (ui->open <= 0)
  9012. return NULL;
  9013. dstbak = dst = xmalloc(uae_u8, 4 + 4 + 2 + 2 * 2 * MAX_DPATH);
  9014. if (type == FILESYS_VIRTUAL || type == FILESYS_CD)
  9015. ptype = SAVESTATE_PATH_VDIR;
  9016. else if (type == FILESYS_HARDFILE || type == FILESYS_HARDFILE_RDB)
  9017. ptype = SAVESTATE_PATH_HDF;
  9018. else if (type == FILESYS_HARDDRIVE)
  9019. ptype = SAVESTATE_PATH_HD;
  9020. else
  9021. ptype = SAVESTATE_PATH;
  9022. save_u32(0);
  9023. save_u32(ui->devno);
  9024. save_u16(type);
  9025. save_path_full(ui->rootdir, ptype);
  9026. save_path_full(ui->filesysdir, SAVESTATE_PATH);
  9027. *len = dst - dstbak;
  9028. return dstbak;
  9029. }
  9030. uae_u8 *save_filesys (int num, int *len)
  9031. {
  9032. uae_u8 *dstbak, *dst;
  9033. UnitInfo *ui;
  9034. int type = is_hardfile (num);
  9035. int ptype;
  9036. ui = &mountinfo.ui[num];
  9037. if (ui->open <= 0)
  9038. return NULL;
  9039. /* not initialized yet, do not save */
  9040. if ((type == FILESYS_VIRTUAL || type == FILESYS_CD) && ui->self == NULL)
  9041. return NULL;
  9042. write_log (_T("FS_FILESYS: '%s' '%s'\n"), ui->devname, ui->volname ? ui->volname : _T("<no name>"));
  9043. dstbak = dst = xmalloc (uae_u8, 100000);
  9044. save_u32 (2); /* version */
  9045. save_u32 (ui->devno);
  9046. save_u16 (type);
  9047. if (type == FILESYS_VIRTUAL || type == FILESYS_CD)
  9048. ptype = SAVESTATE_PATH_VDIR;
  9049. else if (type == FILESYS_HARDFILE || type == FILESYS_HARDFILE_RDB)
  9050. ptype = SAVESTATE_PATH_HDF;
  9051. else if (type == FILESYS_HARDDRIVE)
  9052. ptype = SAVESTATE_PATH_HD;
  9053. else
  9054. ptype = SAVESTATE_PATH;
  9055. save_path(ui->rootdir, ptype);
  9056. save_string (ui->devname);
  9057. save_string (ui->volname);
  9058. save_path (ui->filesysdir, SAVESTATE_PATH);
  9059. save_u8 (ui->bootpri);
  9060. save_u8 (ui->readonly);
  9061. save_u32 (ui->startup);
  9062. save_u32 (filesys_configdev);
  9063. if (type == FILESYS_VIRTUAL || type == FILESYS_CD)
  9064. dst = save_filesys_virtual (ui, dst);
  9065. if (type == FILESYS_HARDFILE || type == FILESYS_HARDFILE_RDB)
  9066. dst = save_filesys_hardfile (ui, dst);
  9067. *len = dst - dstbak;
  9068. return dstbak;
  9069. }
  9070. uae_u8 *restore_filesys_paths(uae_u8 *src)
  9071. {
  9072. restore_u32();
  9073. restore_u32();
  9074. restore_u16();
  9075. new_filesys_root_path = restore_path_full();
  9076. new_filesys_fs_path = restore_path_full();
  9077. return src;
  9078. }
  9079. uae_u8 *restore_filesys (uae_u8 *src)
  9080. {
  9081. int type, devno;
  9082. UnitInfo *ui;
  9083. TCHAR *devname = 0, *volname = 0, *rootdir = 0, *filesysdir = 0;
  9084. uae_u32 startup;
  9085. struct uaedev_config_info *ci;
  9086. if (restore_u32() != 2)
  9087. goto end2;
  9088. devno = restore_u32 ();
  9089. ui = &mountinfo.ui[devno];
  9090. ci = &ui->hf.ci;
  9091. uci_set_defaults (ci, false);
  9092. type = restore_u16 ();
  9093. if (type == FILESYS_VIRTUAL) {
  9094. rootdir = restore_path (SAVESTATE_PATH_VDIR);
  9095. } else if (type == FILESYS_CD) {
  9096. rootdir = restore_path (SAVESTATE_PATH_VDIR);
  9097. if (cd_unit_offset == MAX_FILESYSTEM_UNITS)
  9098. cd_unit_offset = devno;
  9099. cd_unit_number++;
  9100. } else if (type == FILESYS_HARDFILE || type == FILESYS_HARDFILE_RDB) {
  9101. rootdir = restore_path (SAVESTATE_PATH_HDF);
  9102. } else if (type == FILESYS_HARDDRIVE) {
  9103. rootdir = restore_path (SAVESTATE_PATH_HD);
  9104. } else {
  9105. rootdir = restore_path (SAVESTATE_PATH);
  9106. }
  9107. devname = restore_string ();
  9108. volname = restore_string ();
  9109. filesysdir = restore_path (SAVESTATE_PATH);
  9110. ci->bootpri = restore_u8 ();
  9111. ci->readonly = restore_u8 () != 0;
  9112. startup = restore_u32 ();
  9113. filesys_configdev = restore_u32 ();
  9114. if (new_filesys_root_path) {
  9115. xfree(rootdir);
  9116. rootdir = new_filesys_root_path;
  9117. new_filesys_root_path = NULL;
  9118. }
  9119. if (new_filesys_fs_path) {
  9120. xfree(filesysdir);
  9121. filesysdir = new_filesys_fs_path;
  9122. new_filesys_fs_path = NULL;
  9123. }
  9124. if (type == FILESYS_HARDFILE || type == FILESYS_HARDFILE_RDB) {
  9125. src = restore_filesys_hardfile (ui, src);
  9126. xfree (volname);
  9127. volname = NULL;
  9128. }
  9129. _tcscpy (ci->rootdir, rootdir);
  9130. _tcscpy (ci->devname, devname);
  9131. _tcscpy (ci->volname, volname ? volname : _T(""));
  9132. _tcscpy (ci->filesys, filesysdir);
  9133. if (set_filesys_unit (devno, ci, false) < 0) {
  9134. write_log (_T("filesys '%s' failed to restore\n"), rootdir);
  9135. goto end;
  9136. }
  9137. ui->devno = devno;
  9138. ui->startup = startup;
  9139. if (type == FILESYS_VIRTUAL || type == FILESYS_CD)
  9140. src = restore_filesys_virtual (ui, src, devno);
  9141. write_log (_T("'%s' restored\n"), rootdir);
  9142. end:
  9143. xfree (rootdir);
  9144. xfree (devname);
  9145. xfree (volname);
  9146. xfree (filesysdir);
  9147. end2:
  9148. xfree(new_filesys_root_path);
  9149. xfree(new_filesys_fs_path);
  9150. new_filesys_root_path = NULL;
  9151. new_filesys_fs_path = NULL;
  9152. return src;
  9153. }
  9154. int save_filesys_cando (void)
  9155. {
  9156. if (nr_units () == 0)
  9157. return -1;
  9158. return filesys_in_interrupt ? 0 : 1;
  9159. }
  9160. #define SE2_MIN_FILE_LENGTH 32
  9161. struct ShellExecute2
  9162. {
  9163. uae_u32 size;
  9164. char *file, *parms, *currentdir;
  9165. char *fileparms;
  9166. uae_u32 stack;
  9167. uae_s32 priority;
  9168. uae_u32 flags;
  9169. uae_u32 id;
  9170. uae_u32 binsize;
  9171. uae_u8 *bin;
  9172. char *tmpout;
  9173. uaecptr aoutbuf;
  9174. char *binname;
  9175. int aoutlen;
  9176. uaecptr process;
  9177. uaecptr buffer;
  9178. uae_u32 exitcode;
  9179. shellexecute2_callback cb;
  9180. char *outbuf;
  9181. int outlen;
  9182. void *userdata;
  9183. };
  9184. #define SHELLEXEC_MAX 2
  9185. static struct ShellExecute2 shellexecute2[SHELLEXEC_MAX];
  9186. static void shellexecute2_free(struct ShellExecute2 *se2)
  9187. {
  9188. xfree(se2->file);
  9189. xfree(se2->currentdir);
  9190. xfree(se2->parms);
  9191. xfree(se2->bin);
  9192. xfree(se2->outbuf);
  9193. memset(se2, 0, sizeof(struct ShellExecute2));
  9194. }
  9195. #define ShellExecute2_Struct_Start (5 * 4)
  9196. #define ShellExecute2_Struct_Start2 (9 * 4)
  9197. static int filesys_shellexecute2_process(int mode, TrapContext *ctx)
  9198. {
  9199. struct ShellExecute2 *se2 = &shellexecute2[0];
  9200. char tmp[256];
  9201. write_log(_T("filesys_shellexecute2_process %d\n"), mode);
  9202. if (mode == 30) {
  9203. // request Amiga side buffer size
  9204. if (se2->bin) {
  9205. xfree(se2->file);
  9206. sprintf(tmp, "T:__uae_bin_%08X_%08x", se2->process, se2->id);
  9207. se2->file = strdup(tmp);
  9208. }
  9209. int size = ShellExecute2_Struct_Start + ShellExecute2_Struct_Start2;
  9210. size += 2 * (strlen(se2->file) + 1);
  9211. size += strlen(se2->currentdir) + 1;
  9212. size += 2 * (strlen(se2->parms) + 1);
  9213. size += 32; // space for tmp_out
  9214. size += 256; // space for out buffer
  9215. size++;
  9216. size += se2->binsize + 4;
  9217. if (se2->binsize) {
  9218. size += 32; // space for bin name
  9219. }
  9220. return size;
  9221. }
  9222. if (mode == 31) {
  9223. // a0 = buffer
  9224. // d1 = process pointer
  9225. se2->buffer = trap_get_areg(ctx, 0);
  9226. se2->process = trap_get_dreg(ctx, 1);
  9227. if (!se2->buffer) {
  9228. // amiga side out of memory
  9229. shellexecute2_free(se2);
  9230. return 0;
  9231. }
  9232. uaecptr dptr = se2->buffer + ShellExecute2_Struct_Start + ShellExecute2_Struct_Start2;
  9233. trap_put_long(ctx, se2->buffer + 4, dptr);
  9234. if (se2->bin) {
  9235. xfree(se2->file);
  9236. sprintf(tmp, "T:__uae_bin_%08X_%08x", se2->process, se2->id);
  9237. se2->file = strdup(tmp);
  9238. trap_put_long(ctx, se2->buffer + 52, dptr);
  9239. }
  9240. dptr += trap_put_string(ctx, se2->file, dptr, -1) + 1;
  9241. trap_put_long(ctx, se2->buffer + 8, dptr);
  9242. dptr += trap_put_string(ctx, se2->parms, dptr, -1) + 1;
  9243. trap_put_long(ctx, se2->buffer + 12, dptr);
  9244. dptr += trap_put_string(ctx, se2->currentdir, dptr, -1) + 1;
  9245. trap_put_long(ctx, se2->buffer + 16, dptr);
  9246. dptr += trap_put_string(ctx, se2->file, dptr, -1) + 1;
  9247. if (se2->parms[0]) {
  9248. trap_put_byte(ctx, dptr - 1, ' ');
  9249. dptr += trap_put_string(ctx, se2->parms, dptr, -1) + 1;
  9250. }
  9251. if (se2->flags & 2) {
  9252. trap_put_long(ctx, se2->buffer + 44, dptr);
  9253. sprintf(tmp, "T:__uae_out_%08X_%08x", se2->process, se2->id);
  9254. dptr += trap_put_string(ctx, tmp, dptr, -1) + 1;
  9255. se2->aoutbuf = dptr;
  9256. trap_put_long(ctx, se2->buffer + 48, dptr);
  9257. dptr += 128 + 1;
  9258. } else {
  9259. trap_put_long(ctx, se2->buffer + 44, 0);
  9260. }
  9261. dptr += 3;
  9262. dptr &= ~3;
  9263. uaecptr ptr = se2->buffer + ShellExecute2_Struct_Start;
  9264. trap_put_long(ctx, ptr, se2->stack);
  9265. ptr += 4;
  9266. trap_put_long(ctx, ptr, se2->priority);
  9267. ptr += 4;
  9268. trap_put_long(ctx, ptr, se2->flags);
  9269. ptr += 4;
  9270. trap_put_long(ctx, ptr, se2->id);
  9271. ptr += 4;
  9272. if (se2->bin) {
  9273. trap_put_long(ctx, ptr, se2->binsize);
  9274. ptr += 4;
  9275. trap_put_long(ctx, ptr, dptr);
  9276. ptr += 4;
  9277. trap_put_bytes(ctx, se2->bin, dptr, se2->binsize);
  9278. dptr += se2->binsize;
  9279. } else {
  9280. trap_put_long(ctx, ptr, 0);
  9281. ptr += 4;
  9282. trap_put_long(ctx, ptr, 0);
  9283. ptr += 4;
  9284. }
  9285. return 1;
  9286. }
  9287. if (mode == 32) {
  9288. // called by new process, requests buffer
  9289. return se2->buffer;
  9290. }
  9291. if (mode == 34) {
  9292. // collect output
  9293. if (se2->aoutbuf) {
  9294. int len = trap_get_dreg(ctx, 1);
  9295. if (len > 0 && len <= 128) {
  9296. int outoffset = se2->aoutlen;
  9297. trap_get_bytes(ctx, tmp, se2->aoutbuf, 128);
  9298. tmp[len] = 0;
  9299. se2->aoutlen += len;
  9300. if (se2->aoutlen + 1 >= se2->outlen) {
  9301. se2->outlen += 1000;
  9302. if (!se2->outbuf) {
  9303. se2->outbuf = xmalloc(char, se2->outlen);
  9304. } else {
  9305. se2->outbuf = xrealloc(char, se2->outbuf, se2->outlen);
  9306. }
  9307. }
  9308. strcpy(se2->outbuf + outoffset, tmp);
  9309. }
  9310. }
  9311. return 1;
  9312. }
  9313. if (mode == 33) {
  9314. // exit status
  9315. // d0 = status
  9316. // a1 = buffer
  9317. se2->exitcode = trap_get_dreg(ctx, 1);
  9318. if (se2->cb) {
  9319. se2->cb(se2->id, se2->exitcode, se2->flags, se2->outbuf, se2->userdata);
  9320. }
  9321. shellexecute2_free(se2);
  9322. }
  9323. return 0;
  9324. }
  9325. static void shellexec2_boot(struct ShellExecute2 *se2)
  9326. {
  9327. if (kickstart_version >= 36) {
  9328. uae_ShellExecute2(se2->id);
  9329. } else {
  9330. Unit *u;
  9331. TrapContext *ctx = NULL;
  9332. for (u = units; u; u = u->next) {
  9333. if (is_virtual(u->unit) && filesys_isvolume(u)) {
  9334. put_byte(u->volume + 173 - 32, get_byte(u->volume + 173 - 32) | 4);
  9335. uae_Signal(get_long(u->volume + 176 - 32), 1 << 13);
  9336. break;
  9337. }
  9338. }
  9339. }
  9340. }
  9341. int filesys_shellexecute2(TCHAR *file, TCHAR *currentdir, TCHAR *parms, uae_u32 stack, uae_s32 priority, uae_u32 id, uae_u32 flags, uae_u8 *bin, uae_u32 binsize, shellexecute2_callback cb, void *userdata)
  9342. {
  9343. struct ShellExecute2 *se2 = &shellexecute2[0];
  9344. if (uae_boot_rom_type <= 0) {
  9345. return 0;
  9346. }
  9347. if (se2->file) {
  9348. return 0;
  9349. }
  9350. se2->file = file ? ua(file) : "";
  9351. se2->currentdir = currentdir ? ua(currentdir) : "";
  9352. se2->parms = parms ? ua(parms) : "";
  9353. se2->id = id;
  9354. se2->stack = stack;
  9355. se2->priority = priority;
  9356. se2->flags = flags;
  9357. if (bin && binsize > 0) {
  9358. se2->bin = xmalloc(uae_u8, binsize);
  9359. memcpy(se2->bin, bin, binsize);
  9360. se2->binsize = binsize;
  9361. }
  9362. se2->cb = cb;
  9363. se2->userdata = userdata;
  9364. shellexec2_boot(se2);
  9365. return 1;
  9366. }