ImageBackingStore.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 internal ROM drive in microcontroller flash?
  63. bool isRom();
  64. // Is this backed by raw passthrough
  65. bool isRaw();
  66. // Close the image so that .isOpen() will return false.
  67. bool close();
  68. // Return image size in bytes
  69. uint64_t size();
  70. // Check if the image sector range is contiguous, and the image is on
  71. // SD card, return the sector numbers.
  72. bool contiguousRange(uint32_t* bgnSector, uint32_t* endSector);
  73. // Set current position for following read/write operations
  74. bool seek(uint64_t pos);
  75. // Read data from the image file, returns number of bytes read, or negative on error.
  76. ssize_t read(void* buf, size_t count);
  77. // Write data to image file, returns number of bytes written, or negative on error.
  78. ssize_t write(const void* buf, size_t count);
  79. // Flush any pending changes to filesystem
  80. void flush();
  81. // Gets current position for following read/write operations
  82. // Result is only valid for regular files, not raw or flash access
  83. uint64_t position();
  84. size_t getFilename(char* buf, size_t buflen);
  85. protected:
  86. bool m_israw;
  87. bool m_isrom;
  88. bool m_isreadonly_attr;
  89. romdrive_hdr_t m_romhdr;
  90. FsFile m_fsfile;
  91. SdCard *m_blockdev;
  92. uint32_t m_bgnsector;
  93. uint32_t m_endsector;
  94. uint32_t m_cursector;
  95. };