ImageBackingStore.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Access layer to image files associated with a SCSI device.
  2. * Currently supported image storage modes:
  3. *
  4. * - Files on SD card
  5. * - Raw SD card partitions
  6. * - Microcontroller flash ROM drive
  7. */
  8. #pragma once
  9. #include <stdint.h>
  10. #include <unistd.h>
  11. #include <SdFat.h>
  12. #include "ROMDrive.h"
  13. extern "C" {
  14. #include <scsi.h>
  15. }
  16. // SD card sector size is always 512 bytes
  17. extern SdFs SD;
  18. #define SD_SECTOR_SIZE 512
  19. // This class wraps SdFat library FsFile to allow access
  20. // through either FAT filesystem or as a raw sector range.
  21. //
  22. // Raw access is activated by using filename like "RAW:0:12345"
  23. // where the numbers are the first and last sector.
  24. //
  25. // If the platform supports a ROM drive, it is activated by using
  26. // filename "ROM:".
  27. class ImageBackingStore
  28. {
  29. public:
  30. // Empty image, cannot be accessed
  31. ImageBackingStore();
  32. // Parse image file parameters from filename.
  33. // Special filename formats:
  34. // RAW:start:end
  35. // ROM:
  36. ImageBackingStore(const char *filename, uint32_t scsi_block_size);
  37. // Can the image be read?
  38. bool isOpen();
  39. // Can the image be written?
  40. bool isWritable();
  41. // Is this internal ROM drive in microcontroller flash?
  42. bool isRom();
  43. // Is the image using the raw SD card?
  44. bool isRaw();
  45. // Close the image so that .isOpen() will return false.
  46. bool close();
  47. // Return image size in bytes
  48. uint64_t size();
  49. // Check if the image sector range is contiguous, and the image is on
  50. // SD card, return the sector numbers.
  51. bool contiguousRange(uint32_t* bgnSector, uint32_t* endSector);
  52. // Set current position for following read/write operations
  53. bool seek(uint64_t pos);
  54. // Read data from the image file, returns number of bytes read, or negative on error.
  55. ssize_t read(void* buf, size_t count);
  56. // Write data to image file, returns number of bytes written, or negative on error.
  57. ssize_t write(const void* buf, size_t count);
  58. // Flush any pending changes to filesystem
  59. void flush();
  60. // Gets current position for following read/write operations
  61. // Result is only valid for regular files, not raw or flash access
  62. uint64_t position();
  63. protected:
  64. bool m_israw;
  65. bool m_isrom;
  66. bool m_isreadonly_attr;
  67. romdrive_hdr_t m_romhdr;
  68. FsFile m_fsfile;
  69. SdCard *m_blockdev;
  70. uint32_t m_bgnsector;
  71. uint32_t m_endsector;
  72. uint32_t m_cursector;
  73. };