BlueSCSI_disk.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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-2025 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 "BlueSCSI_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
  54. uint32_t tape_pos; // current position in blocks
  55. uint32_t tape_mark_index; // a direct relationship to the file in a multi image file tape
  56. uint32_t tape_mark_count; // the number of marks
  57. uint32_t tape_mark_block_offset; // Sum of the the previous image file sizes at the current mark
  58. bool tape_load_next_file;
  59. // True if there is a subdirectory of images for this target
  60. bool image_directory;
  61. // True if the device type was determined by the drive prefix
  62. bool use_prefix;
  63. // the name of the currently mounted image in a dynamic image directory
  64. char current_image[MAX_FILE_PATH];
  65. // Index of image, for when image on-the-fly switching is used for CD drives
  66. // This is also used for dynamic directories to track how many images have been seen
  67. // Negative value forces restart from first image.
  68. int image_index;
  69. // Previously accessed CD-ROM track, cached for performance
  70. CUETrackInfo cdrom_trackinfo;
  71. // Loaded .bin file index for .cue/.bin with multiple files
  72. // Matches trackinfo.file_index
  73. int cdrom_binfile_index;
  74. // Cue sheet file for CD-ROM images
  75. FsFile cuesheetfile;
  76. // the bin file for the cue sheet, the directory for multi bin files, or closed if neither
  77. FsFile bin_container;
  78. // Right-align vendor / product type strings
  79. // Standard SCSI uses left alignment
  80. int rightAlignStrings;
  81. // Set Vendor / Product Id from image file name
  82. bool name_from_image;
  83. // Maximum amount of bytes to prefetch
  84. int prefetchbytes;
  85. // Warning about geometry settings
  86. bool geometrywarningprinted;
  87. // Clear any image state to zeros
  88. void clear();
  89. uint32_t get_capacity_lba();
  90. private:
  91. // There should be only one global instance of this struct per device, so make copy constructor private.
  92. image_config_t(const image_config_t&) = default;
  93. };
  94. // Should be polled intermittently to update the platform eject buttons.
  95. // Call with 'true' only if ejections should be performed immediately (typically when not busy)
  96. // Returns a mask of the buttons that registered an 'eject' action.
  97. uint8_t diskEjectButtonUpdate(bool immediate);
  98. // Reset all image configuration to empty reset state, close all images.
  99. void scsiDiskResetImages();
  100. // Close any files opened from SD card (prepare for remounting SD)
  101. void scsiDiskCloseSDCardImages();
  102. // Get blocksize from filename or use device setting in ini file
  103. uint32_t getBlockSize(char *filename, uint8_t scsi_id);
  104. // Get and set the eject button bit flags
  105. uint8_t getEjectButton(uint8_t idx);
  106. void setEjectButton(uint8_t idx, int8_t eject_button);
  107. bool scsiDiskOpenHDDImage(int target_idx, const char *filename, int scsi_lun, int blocksize, S2S_CFG_TYPE type = S2S_CFG_FIXED, bool use_prefix = false);
  108. void scsiDiskLoadConfig(int target_idx);
  109. // Checks if a filename extension is appropriate for further processing as a disk image.
  110. // The current implementation does not check the the filename prefix for validity.
  111. bool scsiDiskFilenameValid(const char* name);
  112. // Check if a directory contains a .cue sheet file.
  113. // This is used when single .cue sheet references multiple .bin files.
  114. bool scsiDiskFolderContainsCueSheet(FsFile *dir);
  115. // Checks if the directory name is for multi tagged tapes
  116. bool scsiDiskFolderIsTapeFolder(FsFile *dir);
  117. // Clear the ROM drive header from flash
  118. bool scsiDiskClearRomDrive();
  119. // Program ROM drive and rename image file
  120. bool scsiDiskProgramRomDrive(const char *filename, int scsi_id, int blocksize, S2S_CFG_TYPE type);
  121. // Check if there is ROM drive configured in microcontroller flash
  122. bool scsiDiskCheckRomDrive();
  123. bool scsiDiskActivateRomDrive();
  124. // Returns true if there is at least one image active
  125. bool scsiDiskCheckAnyImagesConfigured();
  126. // Finds filename with the lowest lexical order _after_ the given filename in
  127. // the given folder. If there is no file after the given one, or if there is
  128. // no current file, this will return the lowest filename encountered.
  129. int findNextImageAfter(image_config_t &img, const char* dirname, const char* filename, char* buf, size_t buflen, bool ignore_prefix = false);
  130. // Gets the next image filename for the target, if configured for multiple
  131. // images. As a side effect this advances image tracking to the next image.
  132. // Returns the length of the new image filename, or 0 if the target is not
  133. // configured for multiple images.
  134. int scsiDiskGetNextImageName(image_config_t &img, char *buf, size_t buflen);
  135. // Get pointer to extended image configuration based on target idx
  136. image_config_t &scsiDiskGetImageConfig(int target_idx);
  137. // Start data transfer from disk image to SCSI bus
  138. // Can be called by device type specific command implementations (such as READ CD)
  139. void scsiDiskStartRead(uint32_t lba, uint32_t blocks);
  140. // Start data transfer from SCSI bus to disk image
  141. void scsiDiskStartWrite(uint32_t lba, uint32_t blocks);
  142. // Returns true if there is at least one network device active
  143. bool scsiDiskCheckAnyNetworkDevicesConfigured();
  144. // Switch to next Drive image if multiple have been configured
  145. bool switchNextImage(image_config_t &img, const char* next_filename = nullptr);