scsi2sd.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef scsi2sd_h
  18. #define scsi2sd_h
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* Common type definitions shared between the firmware and config tools
  23. The configuration data is stored in flash.
  24. The flash is organised as 2 arrays of 256 rows, with each row
  25. having 256 bytes. Total of 128kb.
  26. Linear flash memory map:
  27. -----------------------------------------
  28. Array 1 |Row 255 | Bootloader metadata
  29. ---------------------------------
  30. |Row 254 |
  31. |Row 252 | Blank
  32. ---------------------------------
  33. |Row 251 |
  34. | ... |
  35. |Row 236 | Config target 3
  36. | ... |
  37. |Row 220 | Config target 2
  38. | ... |
  39. |Row 204 | Config target 1
  40. | ... |
  41. |Row 188 | Config target 0
  42. ---------------------------------
  43. |Row 235 |
  44. | ... |
  45. |Row 0 |
  46. --------| |
  47. Array 0 |Row 255 | Blank
  48. ---------------------------------
  49. |Row 121 |
  50. | ... |
  51. |Row 37 | Application
  52. ---------------------------------
  53. |Row 36 |
  54. | ... |
  55. |Row 0 | Bootloader
  56. */
  57. #include "stdint.h"
  58. #define MAX_SCSI_TARGETS 4
  59. #define SCSI_CONFIG_ARRAY 1
  60. #define SCSI_CONFIG_ROWS 16
  61. // 256 bytes data, 32 bytes ECC
  62. #define SCSI_CONFIG_ROW_SIZE 256
  63. #define SCSI_CONFIG_ROW_ECC 288
  64. #define SCSI_CONFIG_0_ROW 188
  65. #define SCSI_CONFIG_1_ROW 204
  66. #define SCSI_CONFIG_2_ROW 220
  67. #define SCSI_CONFIG_3_ROW 236
  68. typedef enum
  69. {
  70. CONFIG_TARGET_ID_BITS = 0x07,
  71. CONFIG_TARGET_ENABLED = 0x80
  72. } CONFIG_TARGET_FLAGS;
  73. typedef enum
  74. {
  75. CONFIG_ENABLE_UNIT_ATTENTION = 1,
  76. CONFIG_ENABLE_PARITY = 2,
  77. CONFIG_ENABLE_SCSI2 = 4,
  78. CONFIG_DISABLE_GLITCH = 8
  79. } CONFIG_FLAGS;
  80. typedef enum
  81. {
  82. CONFIG_FIXED,
  83. CONFIG_REMOVEABLE,
  84. CONFIG_OPTICAL,
  85. CONFIG_FLOPPY_14MB
  86. } CONFIG_TYPE;
  87. typedef enum
  88. {
  89. CONFIG_QUIRKS_NONE,
  90. CONFIG_QUIRKS_APPLE
  91. } CONFIG_QUIRKS;
  92. typedef struct __attribute__((packed))
  93. {
  94. uint8_t deviceType;
  95. uint8_t pageCode;
  96. uint8_t reserved;
  97. uint8_t pageLength;
  98. uint8_t data[0]; // pageLength bytes.
  99. } VPD;
  100. typedef struct __attribute__((packed))
  101. {
  102. // bits 7 -> 3 = CONFIG_TARGET_FLAGS
  103. // bits 2 -> 0 = target SCSI ID.
  104. uint8_t scsiId;
  105. uint8_t deviceType; // CONFIG_TYPE
  106. uint8_t flags; // CONFIG_FLAGS
  107. uint8_t deviceTypeModifier; // Used in INQUIRY response.
  108. uint32_t sdSectorStart;
  109. uint32_t scsiSectors;
  110. uint16_t bytesPerSector;
  111. // Max allowed by legacy IBM-PC bios is 6 bits (63)
  112. uint16_t sectorsPerTrack;
  113. // MS-Dos up to 7.10 will crash on >= 256 heads.
  114. uint16_t headsPerCylinder;
  115. char vendor[8];
  116. char prodId[16];
  117. char revision[4];
  118. char serial[16];
  119. uint16_t quirks; // CONFIG_QUIRKS
  120. uint8_t reserved[960]; // Pad out to 1024 bytes for main section.
  121. uint8_t vpd[3072]; // Total size is 4k.
  122. } TargetConfig;
  123. typedef enum
  124. {
  125. CONFIG_NONE, // Invalid
  126. // Command content:
  127. // uint8_t CONFIG_PING
  128. // Response:
  129. // CONFIG_STATUS
  130. CONFIG_PING,
  131. // Command content:
  132. // uint8_t CONFIG_WRITEFLASH
  133. // uint8_t[256] flashData
  134. // uint8_t flashArray
  135. // uint8_t flashRow
  136. // Response:
  137. // CONFIG_STATUS
  138. CONFIG_WRITEFLASH,
  139. // Command content:
  140. // uint8_t CONFIG_READFLASH
  141. // uint8_t flashArray
  142. // uint8_t flashRow
  143. // Response:
  144. // 256 bytes of flash
  145. CONFIG_READFLASH,
  146. // Command content:
  147. // uint8_t CONFIG_REBOOT
  148. // Response: None.
  149. CONFIG_REBOOT,
  150. // Command content:
  151. // uint8_t CONFIG_INFO
  152. // Response:
  153. // uint8_t[16] CSD
  154. // uint8_t[16] CID
  155. CONFIG_SDINFO,
  156. // Command content:
  157. // uint8_t CONFIG_SCSITEST
  158. // Response:
  159. // CONFIG_STATUS
  160. // uint8_t result code (0 = passed)
  161. CONFIG_SCSITEST
  162. } CONFIG_COMMAND;
  163. typedef enum
  164. {
  165. CONFIG_STATUS_GOOD,
  166. CONFIG_STATUS_ERR
  167. } CONFIG_STATUS;
  168. #ifdef __cplusplus
  169. } // extern "C"
  170. #include <type_traits>
  171. static_assert(
  172. std::is_pod<TargetConfig>::value, "Misuse of TargetConfig struct"
  173. );
  174. static_assert(
  175. sizeof(TargetConfig) == 4096,
  176. "TargetConfig struct size mismatch"
  177. );
  178. #endif
  179. #endif