ImageBackingStore.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Portions - Copyright (C) 2023 Eric Helgeson
  3. * ZuluSCSI™ - Copyright (c) 2022-2023 Rabbit Hole Computing™
  4. *
  5. * This file is licensed under the GPL version 3 or any later version. 
  6. *
  7. * https://www.gnu.org/licenses/gpl-3.0.html
  8. * ----
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version. 
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. * GNU General Public License for more details. 
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  21. **/
  22. /* Access layer to image files associated with a SCSI device.
  23. * Currently supported image storage modes:
  24. *
  25. * - Files on SD card
  26. * - Raw SD card partitions
  27. * - Microcontroller flash ROM drive
  28. */
  29. #pragma once
  30. #include <stdint.h>
  31. #include <unistd.h>
  32. #include <SdFat.h>
  33. #include "ROMDrive.h"
  34. #include "ZuluSCSI_config.h"
  35. extern "C" {
  36. #include <scsi.h>
  37. }
  38. // SD card sector size is always 512 bytes
  39. extern SdFs SD;
  40. #define SD_SECTOR_SIZE 512
  41. // This class wraps SdFat library FsFile to allow access
  42. // through either FAT filesystem or as a raw sector range.
  43. //
  44. // Raw access is activated by using filename like "RAW:0:12345"
  45. // where the numbers are the first and last sector.
  46. //
  47. // If the platform supports a ROM drive, it is activated by using
  48. // filename "ROM:".
  49. class ImageBackingStore
  50. {
  51. public:
  52. // Empty image, cannot be accessed
  53. ImageBackingStore();
  54. // Parse image file parameters from filename.
  55. // Special filename formats:
  56. // RAW:start:end
  57. // ROM:
  58. ImageBackingStore(const char *filename, uint32_t scsi_block_size);
  59. // Can the image be read?
  60. bool isOpen();
  61. // Can the image be written?
  62. bool isWritable();
  63. // Is this in Raw mode? by passing the file system
  64. bool isRaw();
  65. // Is this internal ROM drive in microcontroller flash?
  66. bool isRom();
  67. // Is the image a folder, which contains multiple files (used for .bin/.cue)
  68. bool isFolder();
  69. // Is this a contigious block on the SD card? Allowing less overhead
  70. bool isContiguous();
  71. // Close the image so that .isOpen() will return false.
  72. bool close();
  73. // Return image size in bytes
  74. uint64_t size();
  75. // Check if the image sector range is contiguous, and the image is on
  76. // SD card, return the sector numbers.
  77. bool contiguousRange(uint32_t* bgnSector, uint32_t* endSector);
  78. // Set current position for following read/write operations
  79. bool seek(uint64_t pos);
  80. // Read data from the image file, returns number of bytes read, or negative on error.
  81. ssize_t read(void* buf, size_t count);
  82. // Write data to image file, returns number of bytes written, or negative on error.
  83. ssize_t write(const void* buf, size_t count);
  84. // Flush any pending changes to filesystem
  85. void flush();
  86. // Gets current position for following read/write operations
  87. // Result is only valid for regular files, not raw or flash access
  88. uint64_t position();
  89. size_t getFilename(char* buf, size_t buflen);
  90. // Change image if the image is a folder (used for .cue with multiple .bin)
  91. bool selectImageFile(const char *filename);
  92. size_t getFoldername(char* buf, size_t buflen);
  93. protected:
  94. bool m_iscontiguous;
  95. bool m_israw;
  96. bool m_isrom;
  97. bool m_isreadonly_attr;
  98. romdrive_hdr_t m_romhdr;
  99. FsFile m_fsfile;
  100. SdCard *m_blockdev;
  101. uint32_t m_bgnsector;
  102. uint32_t m_endsector;
  103. uint32_t m_cursector;
  104. bool m_isfolder;
  105. char m_foldername[MAX_FILE_PATH + 1];
  106. bool _internal_open(const char *filename);
  107. };