2 * JMraidcon - A console interface to the JMicron JMB394 H/W RAID controller
3 * Copyright (C) 2010 Werner Johansson, <wj@xnk.nu>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program 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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <sys/ioctl.h>
29 #include <arpa/inet.h>
31 #define SECTORSIZE (512)
32 #define READ_CMD (0x28)
33 #define WRITE_CMD (0x2a)
34 #define RW_CMD_LEN (10)
36 const uint8_t probe1[] = { 0x25, 0x03, 0x7b, 0x19, 0x0b, 0xa8, 0x75, 0x3c, 0,0,0,0,0,0,0,0 };
37 const uint8_t probe1end[] = { 0xdb, 0xa1, 0xec, 0x10, 0xd9, 0x10, 0x6d, 0x70 };
39 const uint8_t probe2[] = { 0x25, 0x03, 0x7b, 0x19, 0x37, 0xe3, 0x88, 0x03, 0,0,0,0,0,0,0,0 };
40 const uint8_t probe2end[] = { 0xdb, 0xa1, 0xec, 0x10, 0x1e, 0x51, 0x58, 0x69 };
42 const uint8_t probe3[] = { 0x25, 0x03, 0x7b, 0x19, 0xf3, 0x05, 0x97, 0x68, 0,0,0,0,0,0,0,0 };
43 const uint8_t probe3end[] = { 0xdb, 0xa1, 0xec, 0x10, 0x07, 0x4b, 0x23, 0xfe };
45 const uint8_t probe4[] = { 0x25, 0x03, 0x7b, 0x19, 0x3a, 0x52, 0x0c, 0xe0, 0,0,0,0,0,0,0,0 };
46 const uint8_t probe4end[] = { 0xdb, 0xa1, 0xec, 0x10, 0xdb, 0x7a, 0xe5, 0x5b };
48 // First 4 bytes are always the same for all the scrambled commands, next 4 bytes forms an incrementing command id
49 const uint8_t probe6[]={ 0x22, 0x03, 0x7b, 0x19, 0x06,0x00,0x00,0x00, 0x00, 0x01, 0x02, 0xff, 0x01 }; // This returns very little info (at the end)?
50 const uint8_t probe7[]={ 0x22, 0x03, 0x7b, 0x19, 0x0b,0x00,0x00,0x00, 0x00, 0x01, 0x01, 0xff }; // This cmd returns the "RAID Manager" name
51 const uint8_t probe8[]={ 0x22, 0x03, 0x7b, 0x19, 0x0c,0x00,0x00,0x00, 0x00, 0x01, 0x02, 0xff, 0x0a };
52 const uint8_t probe9[]={ 0x22, 0x03, 0x7b, 0x19, 0x0e,0x00,0x00,0x00, 0x00, 0x02, 0x01, 0xff }; // This returns the names of disks attached (or in a specific RAID volume?)
53 const uint8_t probe11[]={ 0x22, 0x03, 0x7b, 0x19, 0x10,0x00,0x00,0x00, 0x00, 0x02, 0x02, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Identify disk0?
54 const uint8_t probe12[]={ 0x22, 0x03, 0x7b, 0x19, 0x11,0x00,0x00,0x00, 0x00, 0x02, 0x02, 0xff, 0x01, 0x00, 0x00, 0x00, 0x01 }; // Identify disk1?
55 const uint8_t probe13[]={ 0x22, 0x03, 0x7b, 0x19, 0x12,0x00,0x00,0x00, 0x00, 0x02, 0x02, 0xff, 0x02, 0x00, 0x00, 0x00, 0x02 }; // Identify disk2?
56 const uint8_t probe14[]={ 0x22, 0x03, 0x7b, 0x19, 0x13,0x00,0x00,0x00, 0x00, 0x02, 0x02, 0xff, 0x03, 0x00, 0x00, 0x00, 0x03 }; // Identify disk3?
57 const uint8_t probe15[]={ 0x22, 0x03, 0x7b, 0x19, 0x14,0x00,0x00,0x00, 0x00, 0x02, 0x02, 0xff, 0x04, 0x00, 0x00, 0x00, 0x04 }; // Identify disk4?
58 const uint8_t probe16[]={ 0x22, 0x03, 0x7b, 0x19, 0x15,0x00,0x00,0x00, 0x00, 0x03, 0x02, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00 }; // AWARD I5, wtf??
59 const uint8_t probe17[]={ 0x22, 0x03, 0x7b, 0x19, 0x16,0x00,0x00,0x00, 0x00, 0x03, 0x02, 0xff, 0x01, 0x00, 0x00, 0x00, 0x01 }; // ??
60 const uint8_t probe18[]={ 0x22, 0x03, 0x7b, 0x19, 0x17,0x00,0x00,0x00, 0x00, 0x03, 0x02, 0xff, 0x02, 0x00, 0x00, 0x00, 0x02 }; // ??
61 const uint8_t probe19[]={ 0x22, 0x03, 0x7b, 0x19, 0x18,0x00,0x00,0x00, 0x00, 0x03, 0x02, 0xff, 0x03, 0x00, 0x00, 0x00, 0x03 }; // ??
62 const uint8_t probe20[]={ 0x22, 0x03, 0x7b, 0x19, 0x19,0x00,0x00,0x00, 0x00, 0x03, 0x02, 0xff, 0x04, 0x00, 0x00, 0x00, 0x04 }; // ??
63 const uint8_t probe21[]={ 0x22, 0x03, 0x7b, 0x19, 0x1a,0x00,0x00,0x00, 0x00, 0x01, 0x03, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; // Returns nothing?
64 const uint8_t probe23[]={ 0x22, 0x03, 0x7b, 0x19, 0x1c,0x00,0x00,0x00, 0x00, 0x02, 0x03, 0xff, 0x00, 0x02, 0x00, 0xe0, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0xc2, 0x00, 0xa0, 0x00, 0xb0 }; // SMART data disk0?
65 const uint8_t probe24[]={ 0x22, 0x03, 0x7b, 0x19, 0x1d,0x00,0x00,0x00, 0x00, 0x02, 0x03, 0xff, 0x00, 0x02, 0x00, 0xe0, 0x00, 0x00, 0xd1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0xc2, 0x00, 0xa0, 0x00, 0xb0 }; // SMART data disk0 part2?
66 const uint8_t probe25[]={ 0x22, 0x03, 0x7b, 0x19, 0x1e,0x00,0x00,0x00, 0x00, 0x02, 0x03, 0xff, 0x01, 0x02, 0x00, 0xe0, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0xc2, 0x00, 0xa0, 0x00, 0xb0 }; // SMART data disk1?
67 const uint8_t probe26[]={ 0x22, 0x03, 0x7b, 0x19, 0x1f,0x00,0x00,0x00, 0x00, 0x02, 0x03, 0xff, 0x01, 0x02, 0x00, 0xe0, 0x00, 0x00, 0xd1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0xc2, 0x00, 0xa0, 0x00, 0xb0 }; // SMART data disk1 part2?
68 const uint8_t probe27[]={ 0x22, 0x03, 0x7b, 0x19, 0x20,0x00,0x00,0x00, 0x00, 0x02, 0x03, 0xff, 0x02, 0x02, 0x00, 0xe0, 0x00, 0x00, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0xc2, 0x00, 0xa0, 0x00, 0xb0 }; // SMART data disk2?
69 const uint8_t probe28[]={ 0x22, 0x03, 0x7b, 0x19, 0x21,0x00,0x00,0x00, 0x00, 0x02, 0x03, 0xff, 0x02, 0x02, 0x00, 0xe0, 0x00, 0x00, 0xd1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0xc2, 0x00, 0xa0, 0x00, 0xb0 }; // SMART data disk2 part2?
72 #warning FIXME: Should not use a hard-coded sector number (0x21), even though it is backed up and restored afterwards
73 uint8_t rwCmdBlk[RW_CMD_LEN] =
74 {READ_CMD, 0x00, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x01, 0x00}; // SECTOR NUMBER 0x21!!!!!!
76 uint32_t Do_JM_Cmd( int theFD, uint32_t* theCmd, uint32_t* theResp ) {
79 // Calculate CRC for the request
80 uint32_t myCRC = JM_CRC( theCmd, 0x7f );
82 // Stash the CRC at the end
83 #warning FIXME: CRC in request must be little-endian, no matter what the host arch is
85 // printf("Command CRC: 0x%08x\n", myCRC);
87 // Make the data look really 31337 (or not)
90 io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
91 rwCmdBlk[0] = WRITE_CMD;
92 io_hdr.dxferp = theCmd;
93 ioctl(theFD, SG_IO, &io_hdr);
95 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
96 rwCmdBlk[0] = READ_CMD;
97 io_hdr.dxferp = theResp;
98 ioctl(theFD, SG_IO, &io_hdr);
100 // Make the 31337-looking response sane
103 myCRC = JM_CRC( theResp, 0x7f);
104 #warning FIXME: CRC in response is little-endian, no matter what the host arch is
105 if( myCRC != theResp[0x7f] ) {
106 printf( "Warning: Response CRC 0x%08x does not match the calculated 0x%08x!!\n", theResp[0x7f], myCRC );
112 static void hexdump(uint8_t* thePtr, uint32_t theLen) {
114 for(looper=0; looper<theLen; looper++) {
115 printf("0x%02x, ", thePtr[looper]);
116 if((looper&0x0f)==0x0f) {
118 for(asc=looper-0x0f; asc<looper; asc++) {
119 uint8_t theOut=thePtr[asc];
120 if(theOut<0x20 || theOut>0x7f) theOut='.';
129 static void TestCmd( int theFD, uint8_t* theCmd, uint32_t theLen) {
130 uint8_t tempBuf1[SECTORSIZE];
131 uint8_t tempBuf2[SECTORSIZE];
133 // Entire sector is always sent, so zero fill cmd
134 memset( tempBuf1, 0, SECTORSIZE );
135 memcpy( tempBuf1, theCmd, theLen );
137 printf( "Sending command:\n");
138 hexdump( tempBuf1, (theLen+0x0f)&0x1f0 );
139 Do_JM_Cmd( theFD, (uint32_t*)tempBuf1, (uint32_t*)tempBuf2 );
140 printf( "Response:\n");
141 hexdump(tempBuf2, SECTORSIZE);
144 int main(int argc, char * argv[])
147 uint8_t saveBuf[SECTORSIZE];
148 uint8_t probeBuf[SECTORSIZE];
149 uint8_t sense_buffer[32];
151 printf("JMraidcon version x, Copyright (C) 2010 Werner Johansson\n" \
152 "JMraidcon comes with ABSOLUTELY NO WARRANTY.\n" \
153 "This is free software, and you are welcome\n" \
154 "to redistribute it under certain conditions.\n\n" );
157 printf("Usage : JMraidcon /dev/sd<X>\n");
161 if ((sg_fd = open(argv[1], O_RDWR)) < 0) {
162 printf("Cannot open device");
166 // Check if the opened device looks like a sg one.
167 // Inspired by the sg_simple0 example
168 if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
169 printf("%s is not an sg device, or old sg driver\n", argv[1]);
173 // Setup the ioctl struct
174 memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
175 io_hdr.interface_id = 'S';
176 io_hdr.cmd_len = sizeof(rwCmdBlk);
177 io_hdr.mx_sb_len = sizeof(sense_buffer);
178 io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
179 io_hdr.dxfer_len = SECTORSIZE;
180 io_hdr.dxferp = saveBuf;
181 io_hdr.cmdp = rwCmdBlk;
182 io_hdr.sbp = sense_buffer;
183 io_hdr.timeout = 3000;
185 // Add more error handling like this later
186 if( ioctl( sg_fd, SG_IO, &io_hdr ) < 0 ) {
187 printf("ioctl SG_IO failed");
191 // Generate and send the initial "wakeup" data
192 // I haven't gotten the CRC figured out on these ones yet
193 // Note that these (and all other writes) should be directed to an unused sector!!
194 for( uint32_t i=0x10; i<0x1f8; i++ ) {
195 probeBuf[i] = i&0xff;
198 io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
199 rwCmdBlk[0] = WRITE_CMD;
200 io_hdr.dxferp = probeBuf;
202 memcpy(probeBuf, probe1, 0x10);
203 memcpy(probeBuf+0x1f8, probe1end, 0x08);
204 ioctl(sg_fd, SG_IO, &io_hdr);
205 memcpy(probeBuf, probe2, 0x10);
206 memcpy(probeBuf+0x1f8, probe2end, 0x08);
207 ioctl(sg_fd, SG_IO, &io_hdr);
208 memcpy(probeBuf, probe3, 0x10);
209 memcpy(probeBuf+0x1f8, probe3end, 0x08);
210 ioctl(sg_fd, SG_IO, &io_hdr);
211 memcpy(probeBuf, probe4, 0x10);
212 memcpy(probeBuf+0x1f8, probe4end, 0x08);
213 ioctl(sg_fd, SG_IO, &io_hdr);
215 // Initial probe complete, now send scrambled commands to the same sector
217 TestCmd( sg_fd, (uint8_t*)probe6, sizeof(probe6) );
218 TestCmd( sg_fd, (uint8_t*)probe7, sizeof(probe7) );
219 TestCmd( sg_fd, (uint8_t*)probe8, sizeof(probe8) );
220 TestCmd( sg_fd, (uint8_t*)probe9, sizeof(probe9) );
221 TestCmd( sg_fd, (uint8_t*)probe11, sizeof(probe11) );
222 TestCmd( sg_fd, (uint8_t*)probe12, sizeof(probe12) );
223 TestCmd( sg_fd, (uint8_t*)probe13, sizeof(probe13) );
224 TestCmd( sg_fd, (uint8_t*)probe14, sizeof(probe14) );
225 TestCmd( sg_fd, (uint8_t*)probe15, sizeof(probe15) );
226 TestCmd( sg_fd, (uint8_t*)probe16, sizeof(probe16) );
227 TestCmd( sg_fd, (uint8_t*)probe17, sizeof(probe17) );
228 TestCmd( sg_fd, (uint8_t*)probe18, sizeof(probe18) );
229 TestCmd( sg_fd, (uint8_t*)probe19, sizeof(probe19) );
230 TestCmd( sg_fd, (uint8_t*)probe20, sizeof(probe20) );
231 TestCmd( sg_fd, (uint8_t*)probe21, sizeof(probe21) );
232 TestCmd( sg_fd, (uint8_t*)probe23, sizeof(probe23) );
233 TestCmd( sg_fd, (uint8_t*)probe24, sizeof(probe24) );
234 TestCmd( sg_fd, (uint8_t*)probe25, sizeof(probe25) );
235 TestCmd( sg_fd, (uint8_t*)probe26, sizeof(probe26) );
236 TestCmd( sg_fd, (uint8_t*)probe27, sizeof(probe27) );
237 TestCmd( sg_fd, (uint8_t*)probe28, sizeof(probe28) );
239 // Restore the original data to the sector
240 io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
241 rwCmdBlk[0] = WRITE_CMD;
242 io_hdr.dxferp = saveBuf;
243 ioctl(sg_fd, SG_IO, &io_hdr);