scsi2sd.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 187 | Board Config
  44. ---------------------------------
  45. |Row 186 |
  46. | ... |
  47. |Row 0 |
  48. --------| |
  49. Array 0 |Row 255 | Blank
  50. ---------------------------------
  51. |Row 121 |
  52. | ... |
  53. |Row 37 | Application
  54. ---------------------------------
  55. |Row 36 |
  56. | ... |
  57. |Row 0 | Bootloader
  58. */
  59. #include "stdint.h"
  60. #define MAX_SCSI_TARGETS 4
  61. #define SCSI_CONFIG_ARRAY 1
  62. #define SCSI_CONFIG_ROWS 16
  63. // 256 bytes data, 32 bytes ECC
  64. #define SCSI_CONFIG_ROW_SIZE 256
  65. #define SCSI_CONFIG_ROW_ECC 288
  66. #define SCSI_CONFIG_BOARD_ROW 187
  67. #define SCSI_CONFIG_0_ROW 188
  68. #define SCSI_CONFIG_1_ROW 204
  69. #define SCSI_CONFIG_2_ROW 220
  70. #define SCSI_CONFIG_3_ROW 236
  71. typedef enum
  72. {
  73. CONFIG_TARGET_ID_BITS = 0x07,
  74. CONFIG_TARGET_ENABLED = 0x80
  75. } CONFIG_TARGET_FLAGS;
  76. typedef enum
  77. {
  78. CONFIG_ENABLE_UNIT_ATTENTION = 1,
  79. CONFIG_ENABLE_PARITY = 2,
  80. CONFIG_ENABLE_SCSI2 = 4,
  81. CONFIG_DISABLE_GLITCH = 8,
  82. CONFIG_ENABLE_CACHE = 16,
  83. CONFIG_ENABLE_DISCONNECT = 32
  84. } CONFIG_FLAGS;
  85. typedef enum
  86. {
  87. CONFIG_FIXED,
  88. CONFIG_REMOVEABLE,
  89. CONFIG_OPTICAL,
  90. CONFIG_FLOPPY_14MB,
  91. CONFIG_MO,
  92. CONFIG_SEQUENTIAL
  93. } CONFIG_TYPE;
  94. typedef enum
  95. {
  96. CONFIG_QUIRKS_NONE,
  97. CONFIG_QUIRKS_APPLE
  98. } CONFIG_QUIRKS;
  99. typedef struct __attribute__((packed))
  100. {
  101. uint8_t deviceType;
  102. uint8_t pageCode;
  103. uint8_t reserved;
  104. uint8_t pageLength;
  105. uint8_t data[0]; // pageLength bytes.
  106. } VPD;
  107. typedef struct __attribute__((packed))
  108. {
  109. // bits 7 -> 3 = CONFIG_TARGET_FLAGS
  110. // bits 2 -> 0 = target SCSI ID.
  111. uint8_t scsiId;
  112. uint8_t deviceType; // CONFIG_TYPE
  113. uint8_t flagsDEPRECATED; // CONFIG_FLAGS, removed in v4.5
  114. uint8_t deviceTypeModifier; // Used in INQUIRY response.
  115. uint32_t sdSectorStart;
  116. uint32_t scsiSectors;
  117. uint16_t bytesPerSector;
  118. // Max allowed by legacy IBM-PC bios is 6 bits (63)
  119. uint16_t sectorsPerTrack;
  120. // MS-Dos up to 7.10 will crash on >= 256 heads.
  121. uint16_t headsPerCylinder;
  122. char vendor[8];
  123. char prodId[16];
  124. char revision[4];
  125. char serial[16];
  126. uint16_t quirks; // CONFIG_QUIRKS
  127. uint8_t reserved[960]; // Pad out to 1024 bytes for main section.
  128. uint8_t vpd[3072]; // Total size is 4k.
  129. } TargetConfig;
  130. typedef struct __attribute__((packed))
  131. {
  132. char magic[4]; // 'BCFG'
  133. uint8_t flags; // CONFIG_FLAGS
  134. uint8_t startupDelay; // Seconds.
  135. uint8_t selectionDelay; // milliseconds. 255 = auto
  136. uint8_t reserved[249]; // Pad out to 256 bytes
  137. } BoardConfig;
  138. typedef enum
  139. {
  140. CONFIG_NONE, // Invalid
  141. // Command content:
  142. // uint8_t CONFIG_PING
  143. // Response:
  144. // CONFIG_STATUS
  145. CONFIG_PING,
  146. // Command content:
  147. // uint8_t CONFIG_WRITEFLASH
  148. // uint8_t[256] flashData
  149. // uint8_t flashArray
  150. // uint8_t flashRow
  151. // Response:
  152. // CONFIG_STATUS
  153. CONFIG_WRITEFLASH,
  154. // Command content:
  155. // uint8_t CONFIG_READFLASH
  156. // uint8_t flashArray
  157. // uint8_t flashRow
  158. // Response:
  159. // 256 bytes of flash
  160. CONFIG_READFLASH,
  161. // Command content:
  162. // uint8_t CONFIG_REBOOT
  163. // Response: None.
  164. CONFIG_REBOOT,
  165. // Command content:
  166. // uint8_t CONFIG_INFO
  167. // Response:
  168. // uint8_t[16] CSD
  169. // uint8_t[16] CID
  170. CONFIG_SDINFO,
  171. // Command content:
  172. // uint8_t CONFIG_SCSITEST
  173. // Response:
  174. // CONFIG_STATUS
  175. // uint8_t result code (0 = passed)
  176. CONFIG_SCSITEST
  177. } CONFIG_COMMAND;
  178. typedef enum
  179. {
  180. CONFIG_STATUS_GOOD,
  181. CONFIG_STATUS_ERR
  182. } CONFIG_STATUS;
  183. #ifdef __cplusplus
  184. } // extern "C"
  185. #include <type_traits>
  186. static_assert(
  187. std::is_pod<TargetConfig>::value, "Misuse of TargetConfig struct"
  188. );
  189. static_assert(
  190. sizeof(TargetConfig) == 4096,
  191. "TargetConfig struct size mismatch"
  192. );
  193. #endif
  194. #endif