diff options
Diffstat (limited to 'utils/ipodpatcher/ipodio-win32-scsi.c')
-rw-r--r-- | utils/ipodpatcher/ipodio-win32-scsi.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/utils/ipodpatcher/ipodio-win32-scsi.c b/utils/ipodpatcher/ipodio-win32-scsi.c new file mode 100644 index 0000000000..16460cfba3 --- /dev/null +++ b/utils/ipodpatcher/ipodio-win32-scsi.c | |||
@@ -0,0 +1,147 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2009 Dave Chapman | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | * | ||
21 | * Based on the getCapsUsingSCSIPassThrough() function from "cddrv.cpp": | ||
22 | * - http://www.farmanager.com/svn/trunk/unicode_far/cddrv.cpp | ||
23 | * | ||
24 | * Copyright (c) 1996 Eugene Roshal | ||
25 | * Copyright (c) 2000 Far Group | ||
26 | * All rights reserved. | ||
27 | * | ||
28 | * Redistribution and use in source and binary forms, with or without | ||
29 | * modification, are permitted provided that the following conditions | ||
30 | * are met: | ||
31 | * 1. Redistributions of source code must retain the above copyright | ||
32 | * notice, this list of conditions and the following disclaimer. | ||
33 | * 2. Redistributions in binary form must reproduce the above copyright | ||
34 | * notice, this list of conditions and the following disclaimer in the | ||
35 | * documentation and/or other materials provided with the distribution. | ||
36 | * 3. The name of the authors may not be used to endorse or promote products | ||
37 | * derived from this software without specific prior written permission. | ||
38 | * | ||
39 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
40 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
41 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
42 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
43 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
45 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
46 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
47 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
48 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
49 | * | ||
50 | ****************************************************************************/ | ||
51 | |||
52 | #if defined(_WIN32) | ||
53 | #include <windows.h> | ||
54 | #include <stddef.h> | ||
55 | #include <stdio.h> | ||
56 | |||
57 | #include "ipodio.h" | ||
58 | |||
59 | /* from ddk/ntddscsi.h */ | ||
60 | #define SCSI_IOCTL_DATA_OUT 0 | ||
61 | #define SCSI_IOCTL_DATA_IN 1 | ||
62 | #define SCSI_IOCTL_DATA_UNSPECIFIED 2 | ||
63 | |||
64 | #define IOCTL_SCSI_PASS_THROUGH \ | ||
65 | CTL_CODE(FILE_DEVICE_CONTROLLER, 0x0401, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) | ||
66 | |||
67 | typedef struct _SCSI_PASS_THROUGH { | ||
68 | USHORT Length; | ||
69 | UCHAR ScsiStatus; | ||
70 | UCHAR PathId; | ||
71 | UCHAR TargetId; | ||
72 | UCHAR Lun; | ||
73 | UCHAR CdbLength; | ||
74 | UCHAR SenseInfoLength; | ||
75 | UCHAR DataIn; | ||
76 | ULONG DataTransferLength; | ||
77 | ULONG TimeOutValue; | ||
78 | ULONG_PTR DataBufferOffset; | ||
79 | ULONG SenseInfoOffset; | ||
80 | UCHAR Cdb[16]; | ||
81 | } SCSI_PASS_THROUGH, *PSCSI_PASS_THROUGH; | ||
82 | |||
83 | typedef struct _SCSI_PASS_THROUGH_WITH_BUFFERS { | ||
84 | SCSI_PASS_THROUGH Spt; | ||
85 | ULONG Filler; /* realign buffers to double word boundary */ | ||
86 | UCHAR SenseBuf[32]; | ||
87 | UCHAR DataBuf[512]; | ||
88 | } SCSI_PASS_THROUGH_WITH_BUFFERS, *PSCSI_PASS_THROUGH_WITH_BUFFERS; | ||
89 | |||
90 | int ipod_scsi_inquiry(struct ipod_t* ipod, int page_code, | ||
91 | unsigned char* buf, int bufsize) | ||
92 | { | ||
93 | SCSI_PASS_THROUGH_WITH_BUFFERS sptwb; | ||
94 | ULONG length; | ||
95 | DWORD returned; | ||
96 | BOOL status; | ||
97 | |||
98 | if (bufsize > 255) { | ||
99 | fprintf(stderr,"[ERR] Invalid bufsize in ipod_scsi_inquiry\n"); | ||
100 | return -1; | ||
101 | } | ||
102 | |||
103 | memset(&sptwb, 0, sizeof(sptwb)); | ||
104 | |||
105 | sptwb.Spt.Length = sizeof(SCSI_PASS_THROUGH); | ||
106 | sptwb.Spt.PathId = 0; | ||
107 | sptwb.Spt.TargetId = 1; | ||
108 | sptwb.Spt.Lun = 0; | ||
109 | sptwb.Spt.CdbLength = 6; | ||
110 | sptwb.Spt.SenseInfoLength = 32; /* sbuf size */; | ||
111 | sptwb.Spt.DataIn = SCSI_IOCTL_DATA_IN; | ||
112 | sptwb.Spt.DataTransferLength = bufsize; | ||
113 | sptwb.Spt.TimeOutValue = 2; /* 2 seconds */ | ||
114 | sptwb.Spt.DataBufferOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, DataBuf); | ||
115 | sptwb.Spt.SenseInfoOffset = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, SenseBuf); | ||
116 | length = offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, DataBuf) + | ||
117 | sptwb.Spt.DataTransferLength; | ||
118 | |||
119 | /* Set cdb info */ | ||
120 | sptwb.Spt.Cdb[0] = 0x12; /* SCSI Inquiry */ | ||
121 | sptwb.Spt.Cdb[1] = 1; | ||
122 | sptwb.Spt.Cdb[2] = page_code; | ||
123 | sptwb.Spt.Cdb[3] = 0; | ||
124 | sptwb.Spt.Cdb[4] = bufsize; | ||
125 | sptwb.Spt.Cdb[5] = 0; | ||
126 | |||
127 | status = DeviceIoControl(ipod->dh, | ||
128 | IOCTL_SCSI_PASS_THROUGH, | ||
129 | &sptwb, | ||
130 | sizeof(SCSI_PASS_THROUGH), | ||
131 | &sptwb, | ||
132 | length, | ||
133 | &returned, | ||
134 | FALSE); | ||
135 | |||
136 | if (status) { | ||
137 | /* W32 sometimes returns more bytes with additional garbage. | ||
138 | * Make sure to not copy that garbage. */ | ||
139 | memcpy(buf, sptwb.DataBuf, | ||
140 | (DWORD)bufsize >= returned ? returned : (DWORD)bufsize); | ||
141 | return 0; | ||
142 | } else { | ||
143 | return -1; | ||
144 | } | ||
145 | } | ||
146 | #endif | ||
147 | |||