1da8316e522681849e290ced18e4f5f2fb0e38d1
[SCSI2SD.git] / software / include / scsi2sd.h
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
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 /* Common type definitions shared between the firmware and config tools
25
26         The configuration data is stored in flash.
27
28         The flash is organised as 2 arrays of 256 rows, with each row
29         having 256 bytes. Total of 128kb.
30
31         Linear flash memory map:
32         -----------------------------------------
33         Array 1 |Row 255 | Bootloader metadata
34                         ---------------------------------
35                         |Row 254 |
36                         |Row 252 | Blank
37                         ---------------------------------
38                         |Row 251 |
39                         | ...    |
40                         |Row 236 | Config target 3
41                         | ...    |
42                         |Row 220 | Config target 2
43                         | ...    |
44                         |Row 204 | Config target 1
45                         | ...    |
46                         |Row 188 | Config target 0
47                         ---------------------------------
48                         |Row 187 | Board Config
49                         ---------------------------------
50                         |Row 186 |
51                         | ...    |
52                         |Row 0   |
53         --------|        |
54         Array 0 |Row 255 | Blank
55                         ---------------------------------
56                         |Row 121 |
57                         | ...    |
58                         |Row 37  | Application
59                         ---------------------------------
60                         |Row 36  |
61                         | ...    |
62                         |Row 0   | Bootloader
63
64 */
65
66 #include "stdint.h"
67
68 #define MAX_SCSI_TARGETS 4
69 #define SCSI_CONFIG_ARRAY 1
70 #define SCSI_CONFIG_ROWS 16
71
72 // 256 bytes data, 32 bytes ECC
73 #define SCSI_CONFIG_ROW_SIZE 256
74 #define SCSI_CONFIG_ROW_ECC 288
75 #define SCSI_CONFIG_BOARD_ROW 187
76 #define SCSI_CONFIG_0_ROW 188
77 #define SCSI_CONFIG_1_ROW 204
78 #define SCSI_CONFIG_2_ROW 220
79 #define SCSI_CONFIG_3_ROW 236
80
81 typedef enum
82 {
83         CONFIG_TARGET_ID_BITS = 0x07,
84         CONFIG_TARGET_ENABLED = 0x80
85 } CONFIG_TARGET_FLAGS;
86
87 typedef enum
88 {
89         CONFIG_ENABLE_UNIT_ATTENTION = 1,
90         CONFIG_ENABLE_PARITY = 2,
91         CONFIG_ENABLE_SCSI2 = 4,
92         CONFIG_DISABLE_GLITCH = 8,
93         CONFIG_ENABLE_CACHE = 16,
94         CONFIG_ENABLE_DISCONNECT = 32,
95         CONFIG_ENABLE_SEL_LATCH = 64,
96         CONFIG_MAP_LUNS_TO_IDS = 128
97 } CONFIG_FLAGS;
98
99 typedef enum
100 {
101         CONFIG_FIXED,
102         CONFIG_REMOVEABLE,
103         CONFIG_OPTICAL,
104         CONFIG_FLOPPY_14MB,
105         CONFIG_MO,
106         CONFIG_SEQUENTIAL
107
108 } CONFIG_TYPE;
109
110 typedef enum
111 {
112         CONFIG_QUIRKS_NONE,
113         CONFIG_QUIRKS_APPLE
114 } CONFIG_QUIRKS;
115
116 typedef struct __attribute__((packed))
117 {
118         uint8_t deviceType;
119         uint8_t pageCode;
120         uint8_t reserved;
121         uint8_t pageLength;
122         uint8_t data[0]; // pageLength bytes.
123 } VPD;
124
125 typedef struct __attribute__((packed))
126 {
127         // bits 7 -> 3 = CONFIG_TARGET_FLAGS
128         // bits 2 -> 0 = target SCSI ID.
129         uint8_t scsiId;
130
131         uint8_t deviceType; // CONFIG_TYPE
132         uint8_t flagsDEPRECATED; // CONFIG_FLAGS, removed in v4.5
133         uint8_t deviceTypeModifier; // Used in INQUIRY response.
134
135         uint32_t sdSectorStart;
136         uint32_t scsiSectors;
137
138         uint16_t bytesPerSector;
139
140         // Max allowed by legacy IBM-PC bios is 6 bits (63)
141         uint16_t sectorsPerTrack;
142
143         // MS-Dos up to 7.10 will crash on >= 256 heads.
144         uint16_t headsPerCylinder;
145
146
147         char vendor[8];
148         char prodId[16];
149         char revision[4];
150         char serial[16];
151
152         uint16_t quirks; // CONFIG_QUIRKS
153
154         uint8_t reserved[960]; // Pad out to 1024 bytes for main section.
155
156         uint8_t vpd[3072]; // Total size is 4k.
157 } TargetConfig;
158
159 typedef struct __attribute__((packed))
160 {
161         char magic[4]; // 'BCFG'
162         uint8_t flags; // CONFIG_FLAGS
163         uint8_t startupDelay; // Seconds.
164         uint8_t selectionDelay; // milliseconds. 255 = auto
165
166         uint8_t reserved[249]; // Pad out to 256 bytes
167 } BoardConfig;
168
169 typedef enum
170 {
171         CONFIG_NONE, // Invalid
172
173         // Command content:
174         // uint8_t CONFIG_PING
175         // Response:
176         // CONFIG_STATUS
177         CONFIG_PING,
178
179         // Command content:
180         // uint8_t CONFIG_WRITEFLASH
181         // uint8_t[256] flashData
182         // uint8_t flashArray
183         // uint8_t flashRow
184         // Response:
185         // CONFIG_STATUS
186         CONFIG_WRITEFLASH,
187
188         // Command content:
189         // uint8_t CONFIG_READFLASH
190         // uint8_t flashArray
191         // uint8_t flashRow
192         // Response:
193         // 256 bytes of flash
194         CONFIG_READFLASH,
195
196         // Command content:
197         // uint8_t CONFIG_REBOOT
198         // Response: None.
199         CONFIG_REBOOT,
200
201         // Command content:
202         // uint8_t CONFIG_INFO
203         // Response:
204         // uint8_t[16] CSD
205         // uint8_t[16] CID
206         CONFIG_SDINFO,
207
208         // Command content:
209         // uint8_t CONFIG_SCSITEST
210         // Response:
211         // CONFIG_STATUS
212         // uint8_t result code (0 = passed)
213         CONFIG_SCSITEST
214 } CONFIG_COMMAND;
215
216 typedef enum
217 {
218         CONFIG_STATUS_GOOD,
219         CONFIG_STATUS_ERR
220 } CONFIG_STATUS;
221
222
223
224
225 #ifdef __cplusplus
226 } // extern "C"
227
228         #include <type_traits>
229         static_assert(
230                 std::is_pod<TargetConfig>::value, "Misuse of TargetConfig struct"
231                 );
232         static_assert(
233                 sizeof(TargetConfig) == 4096,
234                 "TargetConfig struct size mismatch"
235                 );
236
237 #endif
238
239 #endif