ImageBackingStore.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. extern "C" {
  35. #include <scsi.h>
  36. }
  37. // SD card sector size is always 512 bytes
  38. extern SdFs SD;
  39. #define SD_SECTOR_SIZE 512
  40. // This class wraps SdFat library FsFile to allow access
  41. // through either FAT filesystem or as a raw sector range.
  42. //
  43. // Raw access is activated by using filename like "RAW:0:12345"
  44. // where the numbers are the first and last sector.
  45. //
  46. // If the platform supports a ROM drive, it is activated by using
  47. // filename "ROM:".
  48. class ImageBackingStore
  49. {
  50. public:
  51. // Empty image, cannot be accessed
  52. ImageBackingStore();
  53. // Parse image file parameters from filename.
  54. // Special filename formats:
  55. // RAW:start:end
  56. // ROM:
  57. ImageBackingStore(const char *filename, uint32_t scsi_block_size);
  58. // Can the image be read?
  59. bool isOpen();
  60. // Can the image be written?
  61. bool isWritable();
  62. // Is this in Raw mode? by passing the file system
  63. bool isRaw();
  64. // Is this internal ROM drive in microcontroller flash?
  65. bool isRom();
  66. // Is this a contigious block on the SD card? Allowing less overhead
  67. bool isContiguous();
  68. // Close the image so that .isOpen() will return false.
  69. bool close();
  70. // Return image size in bytes
  71. uint64_t size();
  72. // Check if the image sector range is contiguous, and the image is on
  73. // SD card, return the sector numbers.
  74. bool contiguousRange(uint32_t* bgnSector, uint32_t* endSector);
  75. // Set current position for following read/write operations
  76. bool seek(uint64_t pos);
  77. // Read data from the image file, returns number of bytes read, or negative on error.
  78. ssize_t read(void* buf, size_t count);
  79. // Write data to image file, returns number of bytes written, or negative on error.
  80. ssize_t write(const void* buf, size_t count);
  81. // Flush any pending changes to filesystem
  82. void flush();
  83. // Gets current position for following read/write operations
  84. // Result is only valid for regular files, not raw or flash access
  85. uint64_t position();
  86. size_t getFilename(char* buf, size_t buflen);
  87. protected:
  88. bool m_iscontiguous;
  89. bool m_israw;
  90. bool m_isrom;
  91. bool m_isreadonly_attr;
  92. romdrive_hdr_t m_romhdr;
  93. FsFile m_fsfile;
  94. SdCard *m_blockdev;
  95. uint32_t m_bgnsector;
  96. uint32_t m_endsector;
  97. uint32_t m_cursector;
  98. };