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