scsi2sd.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_FLAGS;
  78. typedef enum
  79. {
  80. CONFIG_FIXED,
  81. CONFIG_REMOVEABLE,
  82. CONFIG_OPTICAL,
  83. CONFIG_FLOPPY_14MB
  84. } CONFIG_TYPE;
  85. typedef enum
  86. {
  87. CONFIG_QUIRKS_NONE,
  88. CONFIG_QUIRKS_APPLE
  89. } CONFIG_QUIRKS;
  90. typedef struct __attribute__((packed))
  91. {
  92. uint8_t deviceType;
  93. uint8_t pageCode;
  94. uint8_t reserved;
  95. uint8_t pageLength;
  96. uint8_t data[0]; // pageLength bytes.
  97. } VPD;
  98. typedef struct __attribute__((packed))
  99. {
  100. // bits 7 -> 3 = CONFIG_TARGET_FLAGS
  101. // bits 2 -> 0 = target SCSI ID.
  102. uint8_t scsiId;
  103. uint8_t deviceType; // CONFIG_TYPE
  104. uint8_t flags; // CONFIG_FLAGS
  105. uint8_t deviceTypeModifier; // Used in INQUIRY response.
  106. uint32_t sdSectorStart;
  107. uint32_t scsiSectors;
  108. uint16_t bytesPerSector;
  109. // Max allowed by legacy IBM-PC bios is 6 bits (63)
  110. uint16_t sectorsPerTrack;
  111. // MS-Dos up to 7.10 will crash on >= 256 heads.
  112. uint16_t headsPerCylinder;
  113. char vendor[8];
  114. char prodId[16];
  115. char revision[4];
  116. char serial[16];
  117. uint16_t quirks; // CONFIG_QUIRKS
  118. uint8_t reserved[960]; // Pad out to 1024 bytes for main section.
  119. uint8_t vpd[3072]; // Total size is 4k.
  120. } TargetConfig;
  121. typedef enum
  122. {
  123. CONFIG_NONE, // Invalid
  124. // Command content:
  125. // uint8_t CONFIG_PING
  126. // Response:
  127. // CONFIG_STATUS
  128. CONFIG_PING,
  129. // Command content:
  130. // uint8_t CONFIG_WRITEFLASH
  131. // uint8_t[256] flashData
  132. // uint8_t flashArray
  133. // uint8_t flashRow
  134. // Response:
  135. // CONFIG_STATUS
  136. CONFIG_WRITEFLASH,
  137. // Command content:
  138. // uint8_t CONFIG_READFLASH
  139. // uint8_t flashArray
  140. // uint8_t flashRow
  141. // Response:
  142. // 256 bytes of flash
  143. CONFIG_READFLASH,
  144. // Command content:
  145. // uint8_t CONFIG_REBOOT
  146. // Response: None.
  147. CONFIG_REBOOT,
  148. // Command content:
  149. // uint8_t CONFIG_INFO
  150. // Response:
  151. // uint8_t[16] CSD
  152. // uint8_t[16] CID
  153. CONFIG_SDINFO,
  154. // Command content:
  155. // uint8_t CONFIG_SCSITEST
  156. // Response:
  157. // CONFIG_STATUS
  158. // uint8_t result code (0 = passed)
  159. CONFIG_SCSITEST
  160. } CONFIG_COMMAND;
  161. typedef enum
  162. {
  163. CONFIG_STATUS_GOOD,
  164. CONFIG_STATUS_ERR
  165. } CONFIG_STATUS;
  166. #ifdef __cplusplus
  167. } // extern "C"
  168. #include <type_traits>
  169. static_assert(
  170. std::is_pod<TargetConfig>::value, "Misuse of TargetConfig struct"
  171. );
  172. static_assert(
  173. sizeof(TargetConfig) == 4096,
  174. "TargetConfig struct size mismatch"
  175. );
  176. #endif
  177. #endif