ZuluSCSI_disk.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * SCSI2SD V6 - Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  3. * Copyright (C) 2014 Doug Brown <doug@downtowndougbrown.com
  4. * ZuluSCSI™ - Copyright (c) 2022 Rabbit Hole Computing™
  5. * Copyright (c) 2023 joshua stein <jcs@jcs.org>
  6. *
  7. * It is derived from disk.h in SCSI2SD V6.
  8. *
  9. * This file is licensed under the GPL version 3 or any later version. 
  10. *
  11. * https://www.gnu.org/licenses/gpl-3.0.html
  12. * ----
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version. 
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. * GNU General Public License for more details. 
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  25. **/
  26. // SCSI disk access routines
  27. // Implements both SCSI2SD V6 disk.h functions and some extra.
  28. #pragma once
  29. #include <stdint.h>
  30. #include <scsi2sd.h>
  31. #include <scsiPhy.h>
  32. #include "ImageBackingStore.h"
  33. #include "ZuluSCSI_config.h"
  34. #include <CUEParser.h>
  35. extern "C" {
  36. #include <disk.h>
  37. #include <config.h>
  38. #include <scsi.h>
  39. }
  40. // Extended configuration stored alongside the normal SCSI2SD target information
  41. struct image_config_t: public S2S_TargetCfg
  42. {
  43. image_config_t() {};
  44. ImageBackingStore file;
  45. // For CD-ROM drive ejection
  46. bool ejected;
  47. uint8_t cdrom_events;
  48. bool reinsert_on_inquiry; // Reinsert on Inquiry command (to reinsert automatically after boot)
  49. bool reinsert_after_eject; // Reinsert next image after ejection
  50. // selects a physical button channel that will cause an eject action
  51. // default option of '0' disables this functionality
  52. uint8_t ejectButton;
  53. // For tape drive emulation, current position in blocks
  54. uint32_t tape_pos;
  55. // True if there is a subdirectory of images for this target
  56. bool image_directory;
  57. // True if the device type was determined by the drive prefix
  58. bool use_prefix;
  59. // the name of the currently mounted image in a dynamic image directory
  60. char current_image[MAX_FILE_PATH];
  61. // Index of image, for when image on-the-fly switching is used for CD drives
  62. // This is also used for dynamic directories to track how many images have been seen
  63. // Negative value forces restart from first image.
  64. int image_index;
  65. // Previously accessed CD-ROM track, cached for performance
  66. CUETrackInfo cdrom_trackinfo;
  67. // Loaded .bin file index for .cue/.bin with multiple files
  68. // Matches trackinfo.file_index
  69. int cdrom_binfile_index;
  70. // Cue sheet file for CD-ROM images
  71. FsFile cuesheetfile;
  72. // Right-align vendor / product type strings
  73. // Standard SCSI uses left alignment
  74. int rightAlignStrings;
  75. // Set Vendor / Product Id from image file name
  76. bool name_from_image;
  77. // Maximum amount of bytes to prefetch
  78. int prefetchbytes;
  79. // Warning about geometry settings
  80. bool geometrywarningprinted;
  81. // Clear any image state to zeros
  82. void clear();
  83. private:
  84. // There should be only one global instance of this struct per device, so make copy constructor private.
  85. image_config_t(const image_config_t&) = default;
  86. };
  87. // Should be polled intermittently to update the platform eject buttons.
  88. // Call with 'true' only if ejections should be performed immediately (typically when not busy)
  89. // Returns a mask of the buttons that registered an 'eject' action.
  90. uint8_t diskEjectButtonUpdate(bool immediate);
  91. // Reset all image configuration to empty reset state, close all images.
  92. void scsiDiskResetImages();
  93. // Close any files opened from SD card (prepare for remounting SD)
  94. void scsiDiskCloseSDCardImages();
  95. // Get blocksize from filename or use device setting in ini file
  96. uint32_t getBlockSize(char *filename, uint8_t scsi_id);
  97. // Get and set the eject button bit flags
  98. uint8_t getEjectButton(uint8_t idx);
  99. void setEjectButton(uint8_t idx, int8_t eject_button);
  100. bool scsiDiskOpenHDDImage(int target_idx, const char *filename, int scsi_lun, int blocksize, S2S_CFG_TYPE type = S2S_CFG_FIXED, bool use_prefix = false);
  101. void scsiDiskLoadConfig(int target_idx);
  102. // Checks if a filename extension is appropriate for further processing as a disk image.
  103. // The current implementation does not check the the filename prefix for validity.
  104. bool scsiDiskFilenameValid(const char* name);
  105. // Check if a directory contains a .cue sheet file.
  106. // This is used when single .cue sheet references multiple .bin files.
  107. bool scsiDiskFolderContainsCueSheet(FsFile *dir);
  108. // Clear the ROM drive header from flash
  109. bool scsiDiskClearRomDrive();
  110. // Program ROM drive and rename image file
  111. bool scsiDiskProgramRomDrive(const char *filename, int scsi_id, int blocksize, S2S_CFG_TYPE type);
  112. // Check if there is ROM drive configured in microcontroller flash
  113. bool scsiDiskCheckRomDrive();
  114. bool scsiDiskActivateRomDrive();
  115. // Returns true if there is at least one image active
  116. bool scsiDiskCheckAnyImagesConfigured();
  117. // Gets the next image filename for the target, if configured for multiple
  118. // images. As a side effect this advances image tracking to the next image.
  119. // Returns the length of the new image filename, or 0 if the target is not
  120. // configured for multiple images.
  121. int scsiDiskGetNextImageName(image_config_t &img, char *buf, size_t buflen);
  122. // Get pointer to extended image configuration based on target idx
  123. image_config_t &scsiDiskGetImageConfig(int target_idx);
  124. // Start data transfer from disk image to SCSI bus
  125. // Can be called by device type specific command implementations (such as READ CD)
  126. void scsiDiskStartRead(uint32_t lba, uint32_t blocks);
  127. // Start data transfer from SCSI bus to disk image
  128. void scsiDiskStartWrite(uint32_t lba, uint32_t blocks);
  129. // Returns true if there is at least one network device active
  130. bool scsiDiskCheckAnyNetworkDevicesConfigured();
  131. // Switch to next Drive image if multiple have been configured
  132. bool switchNextImage(image_config_t &img, const char* next_filename = nullptr);